I've been trying to implement a 32-bit adder by instantiating two 16-bit adders. The code is compiling but failing some test cases. I don't know what is going wrong in this.
verilog code
module top_module(
input [0:31] a,
input [0:31] b,
output [0:31] sum);
add16 adder2 (.sum(sum[16:31]), .a(a[16:31]), .b(b[16:31]));
add16 adder1 (.sum(sum[0:15]), .a(a[0:15]), .b(b[0:15]));
endmodule
This is the top level figure of the circuit:
This is the output waveform:
2 Answers 2
To chain smaller adders to make larger ones, you need the building-block adders to have carry in and carry out connections and you need to link those connections from one ader to the next.
Your diagram does not match your code, your diagram shows a carry connection, but your code does not.
You need to expand your add16 module to support carry in and out connections if it does not already do so, then connect them up as in the diagram you have drawn.
P.S. This is the sort of thing you would normally only do as an excercise, normally you just use the + operator to add things and let the synthesis tool sort it out.
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire w1,cout;
add16 a1(a[15:0],b[15:0],1'b0,sum[15:0],w1);
add16 a2(a[31:16],b[31:16],w1,sum[31:16],cout);
endmodule
add16
module. \$\endgroup\$