开发者

Displaying "X" for output results?

开发者 https://www.devze.com 2022-12-07 22:48 出处:网络
8 Bit CLA Adder module cla8(a, b, cin, sum, cout); input [7:0] a; input [7:0] b; input cin; output [7:0] sum;

8 Bit CLA Adder


module cla8(a, b, cin, sum, cout);
  input [7:0] a;
  input [7:0] b;
  input cin;
  
  output [7:0] sum;
  output cout;

  
  wire p0, g0, p1, g1, p2, g2, p3, g3, p4, g4, p5, g5, p6, g6, p7, g7;
  wire c8, c7, c6, c5, c4, c3, c2, c1;
  
  assign p0 = a[0] ^ b[0];
  assign p1 = a[1] ^ b[1];
  assign p2 = a[2] ^ b[2];  
  assign p3 = a[3] ^ b[3];  
  assign p4 = a[4] ^ b[4];
  assign p5 = a[5] ^ b[5];
  assign p6 = a[6] ^ b[6];
  assign p7 = a[7] ^ b[7];
  
  assign g0 = a[0] & b[0];
  assign g1 = a[1] & b[1];
  assign g2 = a[2] & b[2];
  assign g3 = a[3] & b[3];
  assign g4 = a[4] & b[4];
  assign g5 = a[5] & b[5];
  assign g6 = a[6] & b[6];
  assign g7 = a[7] & b[7];
  
  
  assign c0 = cin;
  
  assign c1 = g0|(p0 & c0); 
  
  assign c2 = g1|(p1 & g0)|(p1 & p0 & c0);
  
  assign c3 = g2|(p2 & g1)|(p2 & p1 & g0)|(p2 & p1 & p0 & c0);
  
  assign c4 = g3|(p3 & g2)|(p3 & p2 & g1)|(p3 & p2 & p1 & g0)|(p3 & p2 & p1 &开发者_如何学Pythonamp; p0 & c0);                            
  assign c5 = g4|(p4 & g3)|(p4 & p3 & g2)|(p4 & p3 & p2 & g1)|(p4 & p3 & p2 & p1 & g0)|                  (p4 & p3 & p2 & p1 & p0 & c0);
 
  assign c6 = g5|(p5 & g4)|(p5 & p4 & g3)|(p5 & p4 & p3 & g2)|(p5 & p4 & p3 & p2 & g1)|                  (p5 & p4 & p3 & p2 & p1 & g0)|(p5 & p4 & p3 & p2 & p1 & p0 & c0);
  
  assign c7 = g6|(p6 & g5)|(p6 & p5 & g4)|(p6 & p5 & p4 & g3)|(p6 & p5 & p4 & p3 & g2)|                   (p6 & p5 & p4 & p3 & p2 & g1)|(p6 & p5 & p4 & p3 & p2 & p1 & g0)|                           (p6 & p5 & p4 & p3 & p2 & p1 & p0 & c0);
  
  assign c8 = g7|(p7 & g6)|(p7 & p6 & g5)|(p7 & p6 & p5 & g4)|(p7 & p6 & p5 & p4 & g3)|                   (p7 & p6 & p5 & p4 & p3 & g2)|(p7 & p6 & p5 & p4 & p3 & p2 & g1)|                           (p7 & p6 & p5 & p4 & p3 & p2 & p1 & g0)|
                (p7 & p6 & p5 & p4 & p3 & p2 & p1 & p0 & c0);
   
  
  
  assign sum[0]  = p0 ^ c0;
  assign sum[1]  = p1 ^ c1;
  assign sum[2]  = p2 ^ c2;
  assign sum[3]  = p3 ^ c3;
  assign sum[4]  = p4 ^ c4;
  assign sum[5]  = p5 ^ c5;
  assign sum[6]  = p6 ^ c6;
  assign sum[7]  = p7 ^ c7;
  
  assign cout = c8;
  
  
endmodule

TestBench

module cla8bit_testbench;
  reg[7:0] a;
  reg[7:0] b;
  reg cin;
  wire [7:0] sum;
  wire cout;
  
  cla8 uut(.a(a),
           .b(b),
           .cin(cin),
           .sum(sum),
           .cout(cout));
  
  initial begin
    $dumpfile("dump.vcd");
    $dumpvars(1);
    
    //With Carry
    a = 8'b11110000;
    b = 8'b11001100;
    cin = 0;
    $display("In the case involving carry, For A = %8b, B = %8b, and Cin = %1d: the Sum will be %8b and Cout will be %1d.", a, b, cin, sum, cout);
  
    #20
 
    $display("In the case without involving carry, For A = %8b, B = %8b, and Cin = %1d: the Sum will be %8b and Cout will be %1d.", a, b, cin, sum, cout);   
    //Without Carry
    a = 8'b11110000;
    b = 8'b00001101;
    cin = 0;

    #100;
 
  end
endmodule

Results showed

# KERNEL: In the case involving carry, For A = xxxxxxxx, B = xxxxxxxx, and Cin = x: the Sum will be xxxxxxxx and Cout will be x.
# KERNEL: In the case without involving carry, For A = 11110000, B = 11001100, and Cin = 0: the Sum will be 10111100 and Cout will be 1.

However, Results are supposed to show

# KERNEL: In the case involving carry, For A = 11110000, B = 11001100, and Cin = 0: the Sum will be 10111100 and Cout will be 1.
# KERNEL: In the case without involving carry, For A = 11110000, B = 00001101, and Cin = 0: the Sum will be 11111101 and Cout will be 0.


The testbench code has a simulation race condition. The 1st $display statement executes at time 0. Also at time 0 a, b and cin start with their initial value of x because reg types are initialized to x (the unknown value). Again at time 0, you change the values of these 3 signals to known values (a = 8'b11110000, etc.). Since these 3 things occur during the same time step, the result is indeterminate.

One way to get the result you want is to add a delay before the 1st $display statement, such as #1.

A similar reason applies for the 2nd $display statement; to fix that, also add delay. Here is modified code that produces the desired result:

  initial begin
    $dumpfile("dump.vcd");
    $dumpvars(1);

    //With Carry
    a = 8'b11110000;
    b = 8'b11001100;
    cin = 0;
    #1;
    $display("In the case involving carry, For A = %8b, B = %8b, and Cin = %1d: the Sum will be %8b and Cout will be %1d.", a, b, cin, sum, cout);

    #20;
    //Without Carry
    a = 8'b11110000;
    b = 8'b00001101;
    cin = 0;
    #1;
    $display("In the case without involving carry, For A = %8b, B = %8b, and Cin = %1d: the Sum will be %8b and Cout will be %1d.", a, b, cin, sum, cout);

    #100;

  end

Output:

In the case involving carry, For A = 11110000, B = 11001100, and Cin = 0: the Sum will be 10111100 and Cout will be 1.
In the case without involving carry, For A = 11110000, B = 00001101, and Cin = 0: the Sum will be 11111101 and Cout will be 0.

Another approach is to use a single $monitor statement instead of multiple $display statements:

  initial begin
    $dumpfile("dump.vcd");
    $dumpvars(1);
    $monitorb(a,, b,, cin,, sum,, cout);

    //With Carry
    a = 8'b11110000;
    b = 8'b11001100;
    cin = 0;

    #20;
    //Without Carry
    a = 8'b11110000;
    b = 8'b00001101;
    cin = 0;

    #100;
  end

Out:

11110000 11001100 0 10111100 1
11110000 00001101 0 11111101 0

See also $strobe

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号