2

I am having some trouble displaying the result of my 8 bit adder verilog

module Adder(a,b,cin,s,co);
input [7:0]a;
input [7:0]b;
output [7:0]s;
output co;
wire [6:0] u;
input cin;

Carry c1(a[0],b[0],cin,s[0],u[0]);
Carry c2(a[1],b[1],u[0],s[1],u[1]);
Carry c3(a[2],b[2],u[1],s[2],u[2]);
Carry c4(a[3],b[3],u[2],s[3],u[3]);
Carry c5(a[4],b[4],u[3],s[4],u[4]);
Carry c6(a[5],b[5],u[4],s[5],u[5]);
Carry c7(a[6],b[6],u[5],s[6],u[6]);
Carry c8(a[7],b[7],u[6],s[7],co);

endmodule

module Carry(a,b,cin,s,co);

input wire a;
input wire b;
input wire cin;
output wire co;
output wire s;
assign co = (a & b) | (b & cin) | (a & cin);
assign s = (~a & ~b & cin) | (~a & b & ~cin) | (a & ~b & ~cin)| (a & b & cin);

endmodule

module testbench;

          reg [7:0]a;
          reg [7:0]b;
          reg cin;
          wire [7:0]s;
          wire co;
          Adder add(a, b, cin, s, co);
          initial begin
    $dumpfile("result.vcd");
    $dumpvars;
         a <= 00000010; b <= 00000010; cin <= 0;
     #5 

      $monitor("time=%4d: %b + %b + %b: sum = %b, carry = %b\n",$time,a,b,cin,s,co);
end
endmodule

although it adds everything correctly, it's not adding the numbers I originally wanted.

time=   5: 00001010 + 00001010 + 0: sum = 00010100, carry = 0

How can i fix it so that instead of adding those numbers, it would add the numbers i'd want. (a = 00000010; b = 00000010; cin = 0;) I already tried changing the numbers around and it does not work except when they're 00000001.

1 Answer 1

1

By default, Verilog interprets a numerical literal value as decimal. The value 00000010 is decimal 10. The $monitor statement uses %b, and it correctly displays the decimal value 10 as 1010. The sum of 10 + 10 is 20 (decimal), which is correctly displayed as 10100 (binary).

For Verilog to interpret 00000010 as binary, you need to specify the base as 'b00000010:

            a <= 'b00000010; b <= 'b00000010; cin <= 0;

Refer to IEEE Std 1800-2012, section 5.7.1 Integer literal constants.


The reason that 1 + 1 works is that 1 is the special case where 00000001 (decimal) is the same value as 'b00000001 (binary).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.