Adder
Start with a full adder and follow its carry into the next bit. With four-bit inputs, 15 plus 1 yields 16 with the fifth bit kept and 0 with a four-bit result. Use the example to set the output width and distinguish unsigned carry from signed overflow.
The collector adds the four-bit words F and 1. As unsigned values, they add to sixteen. A four-bit output shows only zero. Keeping the fifth bit lets the receiver see whether the addition carried out.
Keep the carry and check the signed range separately
Zero-extend A and B to five bits, then add the incoming carry cin. F+1 gives 1_0000. The top bit goes to carry and the four low bits go to sum. The carry input can come from a previous arithmetic stage. 7+0+1 and 7+1+0 then give the same result.
At each bit, add the two input bits and the carry from the previous bit. A full adder takes those three bits and produces a sum bit and a carry. In F+1, the low bit adds 1+1+0, producing sum zero and carry one. Each higher bit adds 1+0+1 and passes the carry up. This explains the arithmetic. Synthesis can choose a different carry structure.
Read F as four-bit two’s-complement −1 and F+1 has signed sum zero, inside the range −8 to 7. Its overflow is zero. For 7+1, the mathematical signed result is eight and the low pattern 1000 would read as −8. This time overflow=1. The unsigned result is eight, so carry=0. The RTL detects signed overflow when the operands have the same sign and the result changes sign.
Synthesizable SystemVerilog
module adder4 (
input logic [3:0] a,
input logic [3:0] b,
input logic cin,
output logic [3:0] sum,
output logic carry,
output logic overflow
);
logic [4:0] wide_sum;
assign wide_sum = {1'b0, a} + {1'b0, b} + {4'b0, cin};
assign {carry, sum} = wide_sum;
assign overflow = (a[3] == b[3]) && (sum[3] != a[3]);
endmodule
Check the results against the waveform
Data values are hexadecimal. Control signals and flags are shown separately.
| Change at | a | b | cin | sum | carry | overflow |
|---|---|---|---|---|---|---|
| 0 ns | F | 1 | 0 | 0 | 1 | 0 |
| 10 ns | 7 | 1 | 0 | 8 | 0 | 1 |
| 20 ns | 7 | 0 | 1 | 8 | 0 | 1 |
| 30 ns | 8 | 8 | 0 | 0 | 1 | 1 |
| 40 ns | 4 | 3 | 0 | 7 | 0 | 0 |
If the interface uses an unsigned modulo result, sum=0 is the expected answer. Pass {carry,sum} together to preserve the full unsigned sum.
Download the runnable SystemVerilog lab. Icarus checks the 5 rows above and 512 binary input combinations. It reads each directed result 1 ns after changing the inputs. The wait sets an observation time in the testbench. The waveform is functional evidence. Physical delay needs a timing model.
Interactive lab
Start with carry for F+1, then inspect overflow for 7+1. Change cin to include carry from an earlier stage.
- Low sum (hex)
- Unsigned carry
- Signed overflow
Show the last eight observations
Teaching film
Run the RTL yourself
Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.
Download Adder RTL (.zip)