I have a Verilog program for a memory module that looks really simple but is behaving oddly, and I cannot see why. If anyone can find my mistake, I would be eternally grateful. This is the memory program:
module mem_WidthxDepth ( clk_i, wr_addr_i, rd_addr_i, wr_i, data_in_i, data_out_o ); parameter Width = 8; parameter Depth = 8; //AW = Address Width localparam AW = $clog2 (Depth); //IO input clk_i; input [AW-1:0] wr_addr_i; input [AW-1:0] rd_addr_i; input wr_i; input [Width-1:0] data_in_i; output [Width-1:0] data_out_o; //Memory declaration. reg [Width-1:0] Mem [0:Depth-1]; //Write into the memory always @ (posedge clk_i) if (wr_i) Mem[wr_addr_i] <= data_in_i; //Read from the memory assign data_out_o = Mem [rd_addr_i]; endmodule and this is the testbench code:
module mem_tb; reg clk_i; reg [2:0] wr_addr_i; reg [2:0] rd_addr_i; reg wr_i; reg [7:0] data_in_i; wire [7:0] data_out_o; // Instantiate the memory mem_WidthxDepth mem ( clk_i, wr_addr_i, rd_addr_i, wr_i, data_in_i, data_out_o ); // Clock generation always #5 clk_i = ~clk_i; initial begin clk_i = 0; wr_i = 0; rd_addr_i = 1; // Write data into FIFO for (integer i = 0; i < 8; i = i + 1) begin @(posedge clk_i); wr_i = 1'b1; wr_addr_i = i[2:0]; data_in_i = i[7:0]; $display("Write %d", data_in_i); end // Stop writing @(negedge clk_i); wr_i = 0; // Read data back for (integer i = 0; i < 8; i = i + 1) begin @(posedge clk_i); rd_addr_i = i[2:0]; $display("Read %d", data_out_o); end // Finish simulation $finish; end // Waveform generation initial begin $dumpfile("mem_tb.vcd"); $dumpvars(0, mem_tb); end endmodule so it should just write 0 to 7 into memory addresses 0 to 7 then read back the numbers. But, when I run it using iverilog (on Ubuntu) I get:
renniej@gramrat:/mnt/d/rhs/Students/Tejas/VLSI/L6$ iverilog -o mem_tb.vvp mem_Wi dthxDepth.v mem_tb.v renniej@gramrat:/mnt/d/rhs/Students/Tejas/VLSI/L6$ vvp mem_tb.vvp VCD info: dumpfile mem_tb.vcd opened for output. Write 0 Write 1 Write 2 Write 3 Write 4 Write 5 Write 6 Write 7 Read 0 Read x Read 2 Read x Read 4 Read x Read 6 Read x mem_tb.v:49: $finish called at 155 (1s) For some reason, every second write and/or read appears to fail. If I look at the signals for the memory module in gtkwave I get:
which shows that data_out_o is undefined every second read, i.e. apparently it was never written. But, I just cannot see what is going wrong. This is such simple code that I cannot see where it is failing.
