1
votes

I designed a 2-digit BCD adder using Verilog. I want to automate the verification procedure. For that, I defined a set of pre-defined input/output combinations in the Verilog testbench. But, my testbench is misbehaving.

Here is my design and testbench:

Design


    `define XLSB X[3:0]
    `define XMSB X[7:4]
    `define YLSB Y[3:0]
    `define YMSB Y[7:4]
    `define Z0 Z[3:0]
    `define Z1 Z[7:4]
    `define Z2 Z[11:8]
    module bcdadder(X,Y,Z);
    input [7:0] X;
    input [7:0] Y;
    output [11:0] Z;
    wire [4:0]S0;
    wire [4:0]S1;
    wire C;
    assign S0=`XLSB+`YLSB;
    assign `Z0=(S0>9) ? S0[3:0]+6 : S0[3:0];
    assign C=(S0>9) ? 1'b1:1'b0;
    assign S1=`XMSB+`YMSB+ C;
    assign `Z1=(S1>9) ? (S1[3:0]+6) : S1[3:0];
    assign `Z2=(S1>9) ? 4'b0001 : 4'b0000 ;
    endmodule

Testbench


    module tb_bcdadder;
    reg [7:0] X_1;
    reg [7:0] Y_1;
    wire [11:0] Z_1;
    reg [7:0] X_1_Array[0:9];
    reg [7:0] Y_1_Array[0:9];
    reg [11:0] result[0:9]; 
    
    initial begin
    
    X_1_Array[0] = 38;
    X_1_Array[1] = 39;
    X_1_Array[2] = 40;
    X_1_Array[3] = 41;
    X_1_Array[4] = 42;
    X_1_Array[5] = 43;
    X_1_Array[6] = 44;
    X_1_Array[7] = 45;
    X_1_Array[8] = 46;
    X_1_Array[9] = 47;
    
    Y_1_Array[0] = 48;
    Y_1_Array[1] = 49;
    Y_1_Array[2] = 50;
    Y_1_Array[3] = 51;
    Y_1_Array[4] = 52;
    Y_1_Array[5] = 53;
    Y_1_Array[6] = 54;
    Y_1_Array[7] = 55;
    Y_1_Array[8] = 56;
    Y_1_Array[9] = 57;
    
     result[0]=86;
     result[1]=88;
     result[2]=90;
     result[3]=92;
     result[4]=94;
     result[5]=96;
     result[6]=98;
     result[7]=100;
     result[8]=102;
     result[9]=104;
     end
    integer i;
    
    bcdadder b1(.X(X_1),.Y(Y_1),.Z(Z_1));
    always begin
            
        for(i = 0; i <= 9; i=i+1)
        begin
            $display(i);
            X_1[7:4] <= X_1_Array[i][7:4];
            X_1[3:0] <= X_1_Array[i][3:0];
            Y_1[7:4] <= Y_1_Array[i][7:4];
            Y_1[3:0] <= Y_1_Array[i][3:0];
            #(40);
            if((result[i][11:8] == Z_1[11:8]) && (result[i][7:4] == Z_1[7:4]) && (result[i][3:0] == Z_1[3:0]))
            begin
                $display("Correct!!!");
            end
            else begin
                $display("Wrong!!!");
            end
        end
        $display("Test Finished");
        $finish();
    end
    initial begin
    $monitor("T=%0t Y=%d X=%d result=%d",$time,Y_1,X_1,Z_1);
    $dumpfile("adder1.vcd");
    $dumpvars(1);
    end
    
    endmodule

Logs


    vu2swz@PPLP02:~/Desktop/fpga/top_down$ ./a.out 
              0
    VCD info: dumpfile adder1.vcd opened for output.
    T=0 Y= 48 X= 38 result=  86
    Correct!!!
              1
    T=40 Y= 49 X= 39 result=  88
    Correct!!!
              2
    T=80 Y= 50 X= 40 result=  96
    Wrong!!!
              3
    T=120 Y= 51 X= 41 result=  98
    Wrong!!!
              4
    T=160 Y= 52 X= 42 result= 100
    Wrong!!!
              5
    T=200 Y= 53 X= 43 result= 102
    Wrong!!!
              6
    T=240 Y= 54 X= 44 result= 104
    Wrong!!!
              7
    T=280 Y= 55 X= 45 result= 106
    Wrong!!!
              8
    T=320 Y= 56 X= 46 result= 108
    Wrong!!!
              9
    T=360 Y= 57 X= 47 result= 110
    Wrong!!!
    Test Finished

Why I am getting the output wrong? I feel my design is correct.

1

1 Answers

1
votes

If your design is correct, then the result array in your testbench is incorrect.

For verification, it is always best to create a model of the design so that you can calculate expected values. I show how you can use a function in the testbench to act as a reference model of the design behavior. When I run the simulation, I always see Correct!!!.

module tb_bcdadder;
    reg [7:0] X_1;
    reg [7:0] Y_1;
    wire [11:0] Z_1;
    reg [7:0] X_1_Array[0:9];
    reg [7:0] Y_1_Array[0:9];
    reg [11:0] result;

initial begin
    X_1_Array[0] = 38;
    X_1_Array[1] = 39;
    X_1_Array[2] = 40;
    X_1_Array[3] = 41;
    X_1_Array[4] = 42;
    X_1_Array[5] = 43;
    X_1_Array[6] = 44;
    X_1_Array[7] = 45;
    X_1_Array[8] = 46;
    X_1_Array[9] = 47;

    Y_1_Array[0] = 48;
    Y_1_Array[1] = 49;
    Y_1_Array[2] = 50;
    Y_1_Array[3] = 51;
    Y_1_Array[4] = 52;
    Y_1_Array[5] = 53;
    Y_1_Array[6] = 54;
    Y_1_Array[7] = 55;
    Y_1_Array[8] = 56;
    Y_1_Array[9] = 57;
end

integer i;

bcdadder b1(.X(X_1),.Y(Y_1),.Z(Z_1));

always begin
    for (i = 0; i <= 9; i=i+1) begin
        $display(i);
        X_1 <= X_1_Array[i];
        Y_1 <= Y_1_Array[i];
        #(40);
        result = bcd_add(X_1, Y_1);
        if (Z_1 === result) begin
            $display("Correct!!!");
        end else begin
            $display("Wrong!!! i=%0d result=%0d, Z_1=%0d", i, result, Z_1);
        end
    end
    $display("Test Finished");
    $finish();
end

initial begin
    $monitor("T=%0t Y=%d X=%d result=%d",$time,Y_1,X_1,Z_1);
    $dumpfile("adder1.vcd");
    $dumpvars(1);
end

function [11:0] bcd_add (input [7:0] a, b);
    reg [4:0] S0;
    reg [4:0] S1;
    reg C;
    begin
        S0 = a[3:0] + b[3:0];
        C  = (S0>9);
        S1 = a[7:4] + b[7:4] + C;
        bcd_add[3:0]  = (S0>9) ? (S0[3:0]+6) : S0[3:0];
        bcd_add[7:4]  = (S1>9) ? (S1[3:0]+6) : S1[3:0];
        bcd_add[11:8] = (S1>9) ? 4'b0001 : 4'b0000;
    end
endfunction

endmodule