In software, a + b feels free. In silicon, every add is a chain of carry logic โ
and every compare is either cheap (equality) or surprisingly expensive (less-than on wide buses).
This lesson connects ripple carry, the + operator,
== vs <, signed traps, and the
overflow flag so you can reason about cost before you write RTL.
An N-bit adder is N full-adders chained by carry. A less-than compare on unsigned values can reuse a subtractor's sign bit; signed compare needs extra care. Overflow is not the same as carry-out.
Addition in hardware starts with two-input cells:
| Cell | Inputs | Outputs | Logic |
|---|---|---|---|
| Half-adder | a, b | sum, carry | sum = a^b, carry = a&b |
| Full-adder | a, b, cin | sum, cout | sum = a^b^cin, cout majority |
A full-adder is roughly two XOR depths for sum and a carry-propagate network. Stack N of them and you get an N-bit ripple-carry adder.
module full_adder ( input wire a, b, cin, output wire sum, cout ); assign sum = a ^ b ^ cin; assign cout = (a & b) | (cin & (a ^ b)); endmodule
In a ripple-carry chain, bit i's carry must settle before bit i+1 can finish. Delay grows linearly with width: roughly O(N) gate delays.
Ripple carry is pedagogically clear; production wide adders use faster carry networks.
module ripple_add #(
parameter WIDTH = 8
) (
input wire [WIDTH-1:0] a, b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
wire [WIDTH:0] carry;
assign carry[0] = cin;
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : gen_add
full_adder u_fa (
.a(a[i]), .b(b[i]), .cin(carry[i]),
.sum(sum[i]), .cout(carry[i+1])
);
end
endgenerate
assign cout = carry[WIDTH];
endmodule
Ripple carry is like a line of people passing a message โ each person waits for the previous one to finish before speaking. Carry-lookahead is everyone shouting their partial answer at once and combining in parallel.
Verilog + on unsigned vectors synthesizes to an adder. The tool picks ripple, carry-lookahead, or a hard macro depending on width, timing constraints, and target (FPGA DSP block vs ASIC cell).
| RTL line | Typical hardware | Cost note |
|---|---|---|
assign y = a + b; | N-bit adder | One adder per expression |
y = a + b + c; | Often 2 adders or 1 wider tree | Don't assume C merges for free |
a + 1 | Incrementer (cheaper than general add) | Constant operand helps tools |
a - b | Adder + inverted b + cin=1 | Same cost class as add |
module add_simple #(
parameter WIDTH = 16
) (
input wire [WIDTH-1:0] a, b,
output wire [WIDTH-1:0] sum,
output wire carry_out
);
// Synthesis infers adder โ width must be explicit
assign {carry_out, sum} = a + b;
endmodule
Width trap: assign y = a + b; when a is 8 bits and y is 8 bits silently discards carry-out. If you need overflow detection, widen the result to WIDTH+1 bits first.
Equality (==): XOR each bit pair, NOR the results โ very cheap, shallow logic, one gate depth per bit plus a tree.
Less-than (<): For unsigned, compute a - b and check the MSB (borrow/sign). That's essentially a subtractor โ much heavier than equality.
| Compare | RTL | Hardware cost |
|---|---|---|
| Equal | a == b | Low โ XOR + NOR tree |
| Not equal | a != b | Low โ invert equal |
| Unsigned < | a < b | Medium โ subtractor path |
| Unsigned > | a > b | Medium โ swap operands |
module compare8 ( input wire [7:0] a, b, output wire eq, output wire lt_unsigned ); assign eq = (a == b); assign lt_unsigned = (a < b); // unsigned by default for wire vectors endmodule
Verilog treats plain wire vectors as unsigned. Signed comparison requires $signed() cast or declaring operands as signed types in SystemVerilog.
a < b when a = 8'hFF (-1 signed) and b = 8'h01 (1) returns false unsigned (255 < 1 is false) but true signed (-1 < 1). This bug ships in production FIFO pointer compares.
module signed_compare8 ( input wire signed [7:0] a, b, // SV: signed declaration output wire lt_signed ); assign lt_signed = (a < b); // signed compare endmodule // Verilog-2001 style cast wire lt_sv; assign lt_sv = ($signed(a) < $signed(b));
Document compare semantics in the module header: "All compares unsigned unless noted." Reviewers grep for < and > on counters and pointers โ signed mistakes are a top-10 RTL bug class.
These are not interchangeable:
a + b โ meaningful for unsigned wrap and multi-precision add.| a (signed) | b | sum 8-bit | C | V (overflow) |
|---|---|---|---|---|
| +127 | +1 | -128 | 0 | 1 |
| +1 | +1 | +2 | 0 | 0 |
| -1 | -1 | -2 | 1 | 0 |
module add_with_flags #(
parameter WIDTH = 8
) (
input wire [WIDTH-1:0] a, b,
output wire [WIDTH-1:0] sum,
output wire carry, // unsigned carry-out
output wire overflow // signed overflow
);
wire [WIDTH:0] wide_sum;
assign wide_sum = a + b;
assign sum = wide_sum[WIDTH-1:0];
assign carry = wide_sum[WIDTH];
// V = (a[N-1] == b[N-1]) && (sum[N-1] != a[N-1])
assign overflow = (~(a[WIDTH-1] ^ b[WIDTH-1])) &
(a[WIDTH-1] ^ sum[WIDTH-1]);
endmodule
Imagine a 32-bit ALU path that must add, subtract, and compare every cycle. Rough relative costs:
Equality, bitwise AND/OR/XOR, increment by 1, mux select.
General add/sub, unsigned <, shift by constant.
Multiple parallel adders, wide signed compares every cycle, variable shifts.
Sharing wins: One adder can serve ADD and SUB (invert B, cin=1) and unsigned < (check subtract sign). Don't instantiate three subtractors if one shared adder with muxed operands suffices.
$signed() or SV signed keyword deliberately, not accidentally+ synthesizes to real adders โ chaining adds costs real area.== is cheap; < is subtractor-class expensive.