Now that you know how to build a full adder, make 3 instances of it to create a 3-bit binary ripple-carry adder. The adder adds two 3-bit numbers and a carry-in to produce a 3-bit sum and carry out. To encourage you to actually instantiate full adders, also output the carry-out from each full adder in the ripple-carry adder. cout[2] is the final carry-out from the last full adder, and is the carry-out you usually see.
moduletop_module(input[3:0]x,input[3:0]y,output[4:0]sum);// This circuit is a 4-bit ripple-carry adder with carry-out.assignsum=x+y;// Verilog addition automatically produces the carry-out bit.// Verilog quirk: Even though the value of (x+y) includes the carry-out, (x+y) is still considered to be a 4-bit number (The max width of the two operands).// This is correct:assignsum=(x+y);// But this is incorrect:assignsum={x+y};// Concatenation operator: This discards the carry-out!endmodule
Assume that you have two 8-bit 2's complement numbers, a[7:0] and b[7:0]. These numbers are added to produce s[7:0]. Also compute whether a (signed) overflow has occurred.
Hint:
A signed overflow occurs when adding two positive numbers produces a negative result, or adding two negative numbers produces a positive result. There are several methods to detect overflow: It could be computed by comparing the signs of the input and output numbers, or derived from the carry-out of bit n and n-1.
moduletop_module(input[99:0]a,input[99:0]b,inputcin,outputcout,output[99:0]sum);// The concatenation {cout, sum} is a 101-bit vector.assign{cout,sum}=a+b+cin;endmodule