Skip to content

Commit

Permalink
Update runtime & tb to work in absolute addresses, not offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Aba committed Nov 7, 2023
1 parent 50e56ab commit 2820d01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
27 changes: 15 additions & 12 deletions c/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,19 @@ extern EXT_C void load_y (uint8_t *p_done, uint8_t *pt_done_proc, const uint32_
}


extern EXT_C void load_x (uint8_t *p_done, uint8_t *bundle_read_done, int32_t *p_offset, int32_t *p_bpt) {
extern EXT_C void load_x (uint8_t *p_done, uint8_t *bundle_read_done, uint64_t *p_base_addr, int32_t *p_bpt) {

static int32_t ib=0, ip=0, it=0, offset_next=0;
static char bundle_read_done_next = 0;
int32_t offset = offset_next;
int32_t bpt = ip == 0 ? bundles[ib].x_bpt_p0 : bundles[ib].x_bpt;

*p_offset = offset;
*p_bpt = bpt;
*p_base_addr = (uint64_t)&mem.x + offset_next;
*p_bpt = ip == 0 ? bundles[ib].x_bpt_p0 : bundles[ib].x_bpt;
*bundle_read_done = bundle_read_done_next;

bundle_read_done_next = 0;
// Nested for loop [for ib: for ip: for it: {}] inverted to increment once per call
++ it; if (it >= bundles[ib].t) { it = 0;
offset_next += bpt;
offset_next += *p_bpt;
++ ip; if (ip >= bundles[ib].p) { ip = 0;

bundle_read_done_next = 1;
Expand All @@ -423,14 +421,14 @@ extern EXT_C void load_x (uint8_t *p_done, uint8_t *bundle_read_done, int32_t *p
}


extern EXT_C void load_w (uint8_t *p_done, int32_t *p_offset, int32_t *p_bpt) {
extern EXT_C void load_w (uint8_t *p_done, uint64_t *p_base_addr, int32_t *p_bpt) {

static int32_t ib=0, ip=0, it=0, offset_next=0;

int32_t offset = offset_next;
int32_t bpt = ip == 0 ? bundles[ib].w_bpt_p0 : bundles[ib].w_bpt;

*p_offset = offset;
*p_base_addr = (uint64_t)&mem.w + offset;
*p_bpt = bpt;

// Nested for loop [for ib: for ip: for it: {}] inverted to increment once per call
Expand All @@ -444,7 +442,7 @@ extern EXT_C void load_w (uint8_t *p_done, int32_t *p_offset, int32_t *p_bpt) {
}


extern EXT_C void fill_memory (){
extern EXT_C void fill_memory (uint64_t *p_w_base, uint64_t *p_x_base){
FILE *fp;
char f_path [1000];

Expand All @@ -464,13 +462,18 @@ extern EXT_C void fill_memory (){

for (int32_t i=0; i<B_WORDS; i++)
printf("i:%d, bias:%d\n", i, mem.b[i]);

*p_w_base = (uint64_t)&mem.w;
*p_x_base = (uint64_t)&mem.x;
}

extern EXT_C int8_t get_byte_wx (int32_t addr, int32_t mode){
if (mode==0) return mem.w[addr];
else if (mode==1) return mem.x[addr];
extern EXT_C int8_t get_byte (uint64_t addr){
return *(int8_t*)addr;
}

extern EXT_C char get_is_bundle_write_done(){
return is_bundle_write_done;
}
extern EXT_C void set_is_bundle_write_done(uint8_t val){
is_bundle_write_done = val;
}
9 changes: 5 additions & 4 deletions test/sv/dma.sv
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ module DMA_M2S #(
logic [BYTES_PER_BEAT-1:0][7:0] s_data_val;
logic [BYTES_PER_BEAT-1:0] s_keep_val;

int status, i_bytes=0;
int status;
longint unsigned i_bytes=0;
bit prev_handshake=1; // data is released first
bit prev_slast=0;

import "DPI-C" function byte get_byte_wx (int addr, int mode);
import "DPI-C" function byte get_byte (longint unsigned addr);

task axis_push (input int offset, input int bytes_per_transfer);
task axis_push (input longint unsigned base_addr, input int bytes_per_transfer);
{s_valid, s_data, s_last, s_keep} = '0;

wait(aresetn); // wait for slave to begin
Expand All @@ -38,7 +39,7 @@ module DMA_M2S #(
s_keep_val[i] = 0;
end
else begin
s_data_val[i] = get_byte_wx(offset + i_bytes, MODE);
s_data_val[i] = get_byte(base_addr + i_bytes);
// $display("DMA: start:%d, i_bytes:%d, val:%d", offset, i_bytes, $signed(s_data_val[i]));
s_keep_val[i] = 1;
i_bytes += 1;
Expand Down
30 changes: 16 additions & 14 deletions test/sv/dnn_engine_tb.sv
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,34 @@ module dnn_engine_tb;
DMA_M2S #(S_WEIGHTS_WIDTH_LF, VALID_PROB, 0) source_k (aclk, aresetn, s_axis_weights_tready, s_axis_weights_tvalid, s_axis_weights_tlast, s_axis_weights_tdata, s_axis_weights_tkeep);

bit y_done=0, x_done=0, w_done=0, bundle_read_done=0, bundle_write_done=0;
int w_offset=0, w_bpt=0, x_offset=0, x_bpt=0;
longint unsigned w_base=0, x_base=0;
int w_bpt=0, x_bpt=0;

import "DPI-C" function void load_x(inout bit x_done, bundle_read_done, inout int x_offset, x_bpt);
import "DPI-C" function void load_w(inout bit w_done, inout int w_offset, w_bpt);
import "DPI-C" function void load_x(inout bit x_done, bundle_read_done, inout longint unsigned x_base, inout int x_bpt);
import "DPI-C" function void load_w(inout bit w_done, inout longint unsigned w_base, inout int w_bpt);
import "DPI-C" function void load_y(inout bit y_done, inout bit m_t_done_proc, inout bit [31:0] y_sram [ROWS*COLS-1:0]);
import "DPI-C" function void fill_memory();
import "DPI-C" function byte get_byte_wx (int addr, int mode);
import "DPI-C" function void fill_memory(inout longint unsigned w_base, x_base);
import "DPI-C" function byte get_byte (longint unsigned addr);
import "DPI-C" function byte get_is_bundle_write_done();
import "DPI-C" function void set_is_bundle_write_done(input bit val);


// W DMA
initial
while (1) begin
load_w (w_done, w_offset, w_bpt);
source_k.axis_push(w_offset, w_bpt);
$display("Done weights dma at offset=%d, bpt=%d \n", w_offset, w_bpt);
load_w (w_done, w_base, w_bpt);
source_k.axis_push(w_base, w_bpt);
$display("Done weights dma at offset=%h, bpt=%d \n", w_base, w_bpt);
if (w_done) break;
end

// X DMA
initial
while (1) begin
load_x (x_done, bundle_read_done, x_offset, x_bpt);
source_x.axis_push(x_offset, x_bpt);
load_x (x_done, bundle_read_done, x_base, x_bpt);
source_x.axis_push(x_base, x_bpt);
while(bundle_read_done && !get_is_bundle_write_done()) #10ps;
$display("Done input dma at offset=%d, bpt=%d \n", x_offset, x_bpt);
$display("Done input dma at offset=%h, bpt=%d \n", x_base, x_bpt);
if (x_done) break;
end

Expand Down Expand Up @@ -111,12 +113,12 @@ module dnn_engine_tb;
initial begin
aresetn = 0;

fill_memory();
fill_memory(w_base, x_base);

for (int i=0; i<50; i++)
$display("weights: i:%d, w:%b", i, get_byte_wx(i, 0));
$display("weights: i:%h, w:%b", i, get_byte(w_base + i));
for (int i=0; i<10; i++)
$display("inputs: i:%d, w:%b", i, get_byte_wx(i,1));
$display("inputs : i:%h, w:%b", i, get_byte(x_base + i));

repeat(2) @(posedge aclk) #1;
aresetn = 1;
Expand Down

0 comments on commit 2820d01

Please sign in to comment.