1
\$\begingroup\$

I have the task of designing a 8x8 bit memory in Verilog using CMOS.

Currently, I have the following:

  • Bitcell - takes in signals sel (if its selected), rw (read = 0, write = 1) and input. If sel=0, output is high impedance. If sel=1, it either reads, or writes (and outputs the written value).
  • word_8 - Collection of 8 bitcells that also take in sel and rw (forwarded to each cell individually). 8 input and 8 output wires to each bitcell.
  • decoder_3to8 - decoder that's supposed to take in an address, and select the word which is supposed to be read.

I have individual testbenches for all those, and they all pass without issues.

This is my 8x8 cell:

module memory_8x8 (
    input[2:0] addr,
    input[7:0] inp,
    input op,
    input select,
    output[7:0] outp
);

wire[7:0] selected_cell;

decoder_3to8 decoder(
    .valid(select),
    .addr(addr),
    .sel(selected_cell)
);

genvar i;
generate
    for (i = 0; i < 8; i = i + 1) begin
        word_8 word_instance(
            .sel(selected_cell[i]),
            .rw(op),
            .inp(inp),
            .outp(outp)
        );
    end
endgenerate

I have tried setting up a basicly testbench for this too. Basically:

  1. Write cell 0

  2. Write cell 1

  3. Read cell 0

    select = 0;
    op = 0;
    addr = 3'b000;
    inp = 8'b00000000;
    
    expected_outp = 8'bXXXXXXXX;
    
    #10;
    
    // Stage 0
    $display("Test stage 0 - Write to cell 0, write to cell 1, read cell 0\n");
    
    // Write to cell 0
    select = 1;
    op = 1;
    addr = 3'b000;
    inp = 8'hAA;
    expected_outp = inp;
    #10;
    if (outp !== expected_outp) begin
        $display("Write to cell 0 FAILED at time %0t ns: Address %b, Expected %b, Got %b",
                $time, addr, expected_outp, outp);
    end else begin
        $display("Write to cell 0 PASSED at time %0t ns: Address %b, Data %b",
                $time, addr, outp);
    end
    
    // Write to cell 1
    addr = 3'b001;
    inp = 8'hBB;
    expected_outp = inp;
    #10;
    if (outp !== expected_outp) begin
        $display("Write to cell 1 FAILED at time %0t ns: Address %b, Expected %b, Got %b",
                $time, addr, expected_outp, outp);
    end else begin
        $display("Write to cell 1 PASSED at time %0t ns: Address %b, Data %b",
                $time, addr, outp);
    end
    
    // Read cell 0
    op = 0;
    addr = 3'b000;
    expected_outp = 8'hAA;
    #10;
    if (outp !== expected_outp) begin
        $display("Read cell 0 FAILED at time %0t ns: Address %b, Expected %b, Got %b",
                $time, addr, expected_outp, outp);
    end else begin
        $display("Read cell 0 PASSED at time %0t ns: Address %b, Data %b",
                $time, addr, outp);
    end
    
    select = 0;
    #10;
    
    // Read cell 2
    op = 0;
    addr = 3'b001;
    expected_outp = 8'hBB;
    #10;
    if (outp !== expected_outp) begin
        $display("Read cell 1 FAILED at time %0t ns: Address %b, Expected %b, Got %b",
                $time, addr, expected_outp, outp);
    end else begin
        $display("Read cell 1 PASSED at time %0t ns: Address %b, Data %b",
                $time, addr, outp);
    end
    

This gives the following output:

Test stage 0 - Write to cell 0, write to cell 1, read cell 0
        Selected cell: 00000001
Write to cell 0 PASSED at time 20000 ns: Address 000, Data 10101010
        Selected cell: 00000010
Write to cell 1 PASSED at time 30000 ns: Address 001, Data 10111011
        Selected cell: 00000001
Read cell 0 FAILED at time 40000 ns: Address 000, Expected 10101010, Got 10111011
        Selected cell: 00000000
Read cell 1 FAILED at time 60000 ns: Address 001, Expected 10111011, Got zzzzzzzz

Here however, the value written to cell 1 seems to be saved in cell 0, while cell 1 doesn't. Any ideas?

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Any ideas?

Yes, there are plenty of ideas.

Since your simulation log indicates there are problems with outp, you need to focus your debug efforts there.

Just by inspection, this code looks suspicious inside the memory_8x8 module:

        .outp(outp)

You declared outp as an 8-bit net with this line:

output[7:0] outp

Assuming the outp port of the word_8 module is declared as an output, then you probably have signal contention (the details of which depend on how you declared the port).

For example, if you declared it as 1-bit like this:

module word_8 (output outp, ...

then all 8 bits would drive the same outp[0] net in memory_8x8.

Your simulation log might have warnings about port width mismatches in that case. Take a closer look at your logs.

One way to fix that bug is to use the following line inside the generate block:

        .outp(outp[i])

It is generally easier and more efficient to debug failing simulations by looking at waveforms. Looking only at the log file is usually insufficient.

Also focus on the connections to the memory_8x8 module instance ports in the testbench, especially for outp.

\$\endgroup\$
4
  • \$\begingroup\$ Thakn you a lot for detailed answer! The output of word_8 is given by output[7:0] outp. Shouldnt it work as it's the same type (wire[7:0]) as output for the 8x8 cell? The logs give no errors. Generally, for each word_8 it should output high inpedance (ZZZZZZZZ) if its not selected, so only one device outputs on the bus. Is that a bad approach? \$\endgroup\$
    – Gronnmann
    Commented Nov 15 at 19:59
  • \$\begingroup\$ @Gronnmann: You're welcome. I am happy to try to help. To answer the question in your comment: No, it does not work that way either. I suspect you are misusing generate loops. Depending on what your code really looks like, you might not even need to use generate. Here is my recommendation to you: Accept this answer since it provides a general solution. Then, open a new question in which you post a complete Verilog code example (including testbench code). ... \$\endgroup\$
    – toolic
    Commented Nov 15 at 20:09
  • \$\begingroup\$ @Gronnmann: ... Trying to provide answers in comments like this is not practical, and making large edits to a question gets confusing. \$\endgroup\$
    – toolic
    Commented Nov 15 at 20:10
  • \$\begingroup\$ Allright, thank you! \$\endgroup\$
    – Gronnmann
    Commented Nov 15 at 21:10

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.