RTL Design / Module 4: Combinational Design from Scratch
Lesson 4.2

Adders & comparators โ€” think hardware cost, not only code

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.

Core idea

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.

1

Half-adders and full-adders โ€” the atomic units

Addition in hardware starts with two-input cells:

CellInputsOutputsLogic
Half-addera, bsum, carrysum = a^b, carry = a&b
Full-addera, b, cinsum, coutsum = 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.

full_adder.v
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
2

Ripple-carry adder โ€” simple but slow

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 chain

Ripple carry is pedagogically clear; production wide adders use faster carry networks.

ripple_add.v
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
Analogy

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.

3

The + operator โ€” what synthesis actually builds

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 lineTypical hardwareCost note
assign y = a + b;N-bit adderOne adder per expression
y = a + b + c;Often 2 adders or 1 wider treeDon't assume C merges for free
a + 1Incrementer (cheaper than general add)Constant operand helps tools
a - bAdder + inverted b + cin=1Same cost class as add
plus_operator.v
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.

4

Comparators โ€” equality vs less-than

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.

CompareRTLHardware cost
Equala == bLow โ€” XOR + NOR tree
Not equala != bLow โ€” invert equal
Unsigned <a < bMedium โ€” subtractor path
Unsigned >a > bMedium โ€” swap operands
comparators.v
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
5

Signed compare traps

Verilog treats plain wire vectors as unsigned. Signed comparison requires $signed() cast or declaring operands as signed types in SystemVerilog.

Trap

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.

signed_compare.v
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));
Industry note

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.

6

Overflow flag vs carry-out

These are not interchangeable:

  • Carry-out (C): Bit N of a + b โ€” meaningful for unsigned wrap and multi-precision add.
  • Overflow (V): Signed overflow โ€” result doesn't fit in N-bit two's complement. True when adding two positives gives negative, or two negatives gives positive.
a (signed)bsum 8-bitCV (overflow)
+127+1-12801
+1+1+200
-1-1-210
overflow_flag.v
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
7

Hardware cost on a real datapath

Imagine a 32-bit ALU path that must add, subtract, and compare every cycle. Rough relative costs:

โšก

Cheap

Equality, bitwise AND/OR/XOR, increment by 1, mux select.

โš™๏ธ

Medium

General add/sub, unsigned <, shift by constant.

๐Ÿ”ฅ

Expensive

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.

โ–ถ Test add + compare in the simulator
8

Design checklist

  • Declare vector widths explicitly โ€” never rely on implicit 32-bit integers
  • Widen to WIDTH+1 when you need carry-out or overflow
  • Document signed vs unsigned compare policy in header comments
  • Use $signed() or SV signed keyword deliberately, not accidentally
  • Share one adder for add/sub/compare when timing allows
  • Overflow โ‰  carry โ€” implement the flag your ISA spec actually needs

๐ŸŽฏ What to remember

  • Ripple carry is O(N) delay โ€” fine for narrow, problematic for 32+ bits at high Fmax.
  • + synthesizes to real adders โ€” chaining adds costs real area.
  • == is cheap; < is subtractor-class expensive.
  • Unsigned default bites signed algorithms โ€” cast or declare signed.
  • Overflow flag answers a different question than carry-out.
Advertisement