1
\$\begingroup\$

What can I do to improve my testbench for testing a 64x4 RAM memory in Verilog to obtain the desired result?

I have written a test bench to test a simple 64x4 RAM memory in Verilog, and it seems to "work" partially in that it outputs the requested address data after the subsequent request, and this is after duplication of the first requested address data with the subsequent one.

To better explain it, the first request yields the correct outcome. But, the following request yields the same result as the first.

And subsequent requests will then yield the data for the 2nd request, then the data for the 3rd request, and so on and so forth.

I have tried to modify the code various times, but keep on getting the same result. And after returning to this after a hiatus of 2 years, I would really like to put this one to bed.

The code for the testbench is as follows:

 module tb_Memory();
    reg   Enable, ReadWrite;
    reg   [3:0] DataIn;
    reg   [5:0] Address;
    wire      [3:0] DataOut;
    wire      [3:0] Mem [0:63];

    Memory    M0 (Enable, ReadWrite, Address, DataIn, DataOut);

    initial begin  
       Enable = 0; 
       DataIn = 4'b0000;
    end

    initial fork
       #2 Enable = 1;
       #5 Address = 6'h00; 
       #8 DataIn = 4'b1101;
       #10 ReadWrite = 0;
  
       #25 Address = 6'h01; 
       #26 Enable = 0; 
       #28 Enable = 1; 
       #28 DataIn = 4'b1010;
       #45 Address = 6'h02; 
       #46 Enable = 0; 
       #48 Enable = 1; 
       #48 DataIn = 4'b1111;
       #65 Address = 6'h03; 
       #66 Enable = 0; 
       #68 Enable = 1;
       #68 DataIn = 4'b1100;
       #85 Address = 6'h04; 
       #86 Enable = 0; 
       #88 Enable = 1; 
       #88 DataIn = 4'b0111;
       #105 Address = 6'h05; 
       #106 Enable = 0; 
       #108 Enable = 1; 
       #108 DataIn = 4'b1110;
       #125 Address = 6'h06; 
       #126 Enable = 0; 
       #128 Enable = 1; 
       #128 DataIn = 4'b0101;
       #145 Address = 6'h07; 
       #146 Enable = 0; 
       #148 Enable = 1;
       #148 DataIn = 4'b1001;
       #160 Enable = 0; 
       #162 Enable = 1; 
       #165 Address = 6'h08; 
       #168 DataIn = 4'b0001;
       #180 Enable = 0; 
       #182 Enable = 1; 
       #185 Address = 6'h09; 
       #188 DataIn = 4'b0110;
       #200 Enable = 0; 
       #202 Enable = 1; 
       #205 Address = 6'h0a; 
       #208 DataIn = 4'b1011;
       #220 Enable = 0; 
       #222 Enable = 1; 
       #237 Enable = 0; 
       #239 Enable = 1;
// Read data from memory addresses
       #240 Address = 6'h08; 
       #242 ReadWrite = 1; 
       #257 Enable = 0; 
       #259 Enable = 1;
       #260 Address = 6'h07; 
  
       #277 Enable = 0; 
       #279 Enable = 1;
       #280 Address = 6'h09; 
  
       #297 Enable = 0; 
       #299 Enable = 1;
       #300 Address = 6'h03; 
  
       #317 Enable = 0; 
       #319 Enable = 1;
       #320 Address = 6'h04; 
  
       #337 Enable = 0; 
       #339 Enable = 1;
       #340 Address = 6'h0a; 
  
       #357 Enable = 0; 
       #359 Enable = 1;
       #360 Address = 6'h06; 
    join
 endmodule

Any assistance that anyone can provide will be very much appreciated.

RAM Waveform

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You didn't give us the details on how your memory works, but a general approach is to replace all that fork/join code with Verilog task's and task calls:

initial begin  
   Enable = 0; 
   DataIn = 4'b0000;

    #10 ReadWrite = 0;

    // Write specific data to specific addresses
    write(6'h00, 4'b1101);
    write(6'h01, 4'b1010);

    // Write random data to random addresses
    repeat (10) begin
        write($random, $random);
    end
end

task write (input [5:0] addr, [3:0] din);
    #20 Address = addr; 
    #1 Enable = 0; 
    #2 Enable = 1; 
    DataIn = din;
endtask

task read (input [5:0] addr, [3:0] dexpect);
    #20 Address = addr; 
    #1 Enable = 0; 
    #2 Enable = 1; 
    if (DataOut !== dexpect) $display("Error addr=%h dout=%h", addr, DataOut);
endtask
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.