|
| 1 | +// src/data_mem.veryl |
| 2 | + |
| 3 | +module DataMem #( |
| 4 | + param Words: u32 = 32, |
| 5 | +) ( |
| 6 | + i_clk : input clock , |
| 7 | + i_reset : input reset_async_high , |
| 8 | + i_we : input logic , |
| 9 | + i_addr : input logic <32>, // byte address, but only word operations supported |
| 10 | + i_data : input logic <32>, |
| 11 | + i_dbg_addr: input logic <32>, |
| 12 | + o_data : output logic <32>, |
| 13 | + o_dbg_data: output logic <32>, |
| 14 | +) { |
| 15 | + var mem: logic<32> [Words]; // mem is Words sized array of 32 bit data |
| 16 | + |
| 17 | + // synchronized update |
| 18 | + always_ff (i_clk, i_reset) { |
| 19 | + if_reset { |
| 20 | + for i: u32 in 0..Words { |
| 21 | + mem[i] = 0; |
| 22 | + } |
| 23 | + } else { |
| 24 | + if i_we { |
| 25 | + mem[i_addr >> 2] = i_data; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + // asynchronous read |
| 30 | + assign o_data = mem[i_addr >> 2]; |
| 31 | + assign o_dbg_data = mem[i_dbg_addr >> 2]; |
| 32 | +} |
| 33 | + |
| 34 | +#[test(data_mem)] |
| 35 | +embed (inline) sv{{{ |
| 36 | + module test; |
| 37 | + logic i_clk; |
| 38 | + logic i_reset; |
| 39 | + logic i_we; |
| 40 | + logic [31:0] i_addr; |
| 41 | + logic [31:0] i_data; |
| 42 | + logic [31:0] i_dbg_addr; |
| 43 | + |
| 44 | + logic [31:0] o_data; |
| 45 | + logic [31:0] o_dbg_data; |
| 46 | + |
| 47 | + vips_DataMem data_mem(i_clk, i_reset, i_we, i_addr, i_data, i_dbg_addr, o_data, o_dbg_data); |
| 48 | + |
| 49 | + task clock; |
| 50 | + begin |
| 51 | + i_clk = 0; #10; i_clk = 1; #10; |
| 52 | + end |
| 53 | + endtask |
| 54 | + |
| 55 | + initial begin |
| 56 | + i_reset = 1; |
| 57 | + i_we = 0; |
| 58 | + |
| 59 | + i_we = 1; |
| 60 | + i_addr = 'h00; |
| 61 | + i_data = 'hdead_beef; |
| 62 | + i_dbg_addr = 'h00; |
| 63 | + |
| 64 | + clock(); |
| 65 | + assert (o_dbg_data == 0); |
| 66 | + |
| 67 | + i_reset = 0; |
| 68 | + clock(); |
| 69 | + assert (o_dbg_data == 'hdead_beef); |
| 70 | + |
| 71 | + i_addr = 'h04; |
| 72 | + i_data = 'hdead_c0de; |
| 73 | + i_dbg_addr = 'h04; |
| 74 | + clock(); |
| 75 | + assert (o_dbg_data == 'hdead_c0de); |
| 76 | + assert (o_data == 'hdead_c0de); |
| 77 | + i_addr = 'h00; |
| 78 | + i_dbg_addr = 'h08; |
| 79 | + #1; // just to progress time |
| 80 | + assert (o_data == 'hdead_beef); |
| 81 | + assert (o_dbg_data == 0); |
| 82 | + |
| 83 | + i_reset = 1; |
| 84 | + #1; // just to progress time |
| 85 | + |
| 86 | + clock(); |
| 87 | + assert (o_data == 0); |
| 88 | + assert (o_dbg_data == 0); |
| 89 | + |
| 90 | + |
| 91 | + $finish; |
| 92 | + end |
| 93 | + endmodule |
| 94 | +}}} |
0 commit comments