Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Combinational Logic

Adders, Subtractors and a Simple ALU in Verilog

Build arithmetic hardware in Verilog: ripple-carry adders, carry-lookahead idea, subtractors with two's complement, comparators, flags and a simple parameterized ALU, with C models.

Arithmetic is where combinational design meets real computation. In this lesson you chain full adders into a ripple-carry adder, turn it into a subtractor with two's complement, add status flags, and combine operations into a small ALU (arithmetic logic unit). Everything is combinational: no clock.

Ripple-carry adder

Chain full adders so each carry-out feeds the next carry-in. A 4-bit version:

VERILOG
module ripple_adder4 (
    input  wire [3:0] a, b,
    input  wire       cin,
    output wire [3:0] sum,
    output wire       cout
);
    wire [4:0] c;
    assign c[0] = cin;

    genvar i;
    generate
        for (i = 0; i < 4; i = i + 1) begin : fa
            assign sum[i]  = a[i] ^ b[i] ^ c[i];
            assign c[i+1]  = (a[i] & b[i]) | (c[i] & (a[i] ^ b[i]));
        end
    endgenerate

    assign cout = c[4];
endmodule

generate with genvar replicates hardware at build time: this loop creates four sets of assignments (one per bit), not a runtime loop. The carry ripples from bit 0 upward, so the delay grows with the width: the sum of bit 3 waits for three earlier carries.

In everyday RTL you just write the operator and let the synthesizer pick the best structure:

VERILOG
assign {cout, sum} = a + b + cin;

The concatenation on the left captures the carry in the extra bit. On FPGAs the tool uses fast dedicated carry chains, so the plain + is usually faster than your hand-built version.

Carry-lookahead idea

To avoid waiting for the ripple, compute for each bit a generate g = a & b (this bit creates a carry) and a propagate p = a ^ b (this bit passes a carry along). Then:

TEXT
c1 = g0 | (p0 & c0)
c2 = g1 | (p1 & g0) | (p1 & p0 & c0)
c3 = g2 | (p2 & g1) | (p2 & p1 & g0) | (p2 & p1 & p0 & c0)

All carries come from AND-OR terms of the inputs in parallel, so delay no longer grows linearly. Synthesis tools use such structures automatically when you ask for speed.

Subtraction with two's complement

a - b = a + (~b) + 1. Invert b, set the carry-in to 1, and use the same adder. One adder can add or subtract with a control bit sub:

VERILOG
module addsub8 (
    input  wire [7:0] a, b,
    input  wire       sub,
    output wire [7:0] result,
    output wire       carry
);
    wire [7:0] b_eff = sub ? ~b : b;
    assign {carry, result} = a + b_eff + sub;
endmodule

For subtraction the carry output means "no borrow": carry = 1 when a >= b in unsigned arithmetic.

Flags

Status flags summarize a result and drive branches in a CPU.

Flag Meaning Expression (8-bit)
Zero (Z) Result is 0 result == 8'd0
Negative (N) Sign bit result[7]
Carry (C) Unsigned carry out carry
Overflow (V) Signed result does not fit (a[7] == b_eff[7]) && (result[7] != a[7])

Signed overflow happens when adding two numbers of the same sign gives a result with the opposite sign.

Comparators

VERILOG
assign eq = (a == b);
assign lt = (a <  b);          // unsigned
assign slt = ($signed(a) < $signed(b));  // signed

A comparator is a subtractor whose flags are examined. The synthesizer picks an efficient structure for you.

A simple parameterized ALU

VERILOG
module alu #(parameter W = 8) (
    input  wire [W-1:0] a, b,
    input  wire [2:0]   op,
    output reg  [W-1:0] y,
    output wire         zero
);
    always @* begin
        case (op)
            3'd0: y = a + b;
            3'd1: y = a - b;
            3'd2: y = a & b;
            3'd3: y = a | b;
            3'd4: y = a ^ b;
            3'd5: y = ~a;
            3'd6: y = a << 1;
            3'd7: y = a >> 1;
            default: y = {W{1'b0}};
        endcase
    end
    assign zero = (y == {W{1'b0}});
endmodule

The default assignment keeps the block latch-free. Every operation exists in hardware at once and the case just selects one result: a mux at the end of many parallel units. That is why adding opcodes costs area even when an operation is rarely used.

flowchart LR
  a --> ADD["a + b"]
  b --> ADD
  a --> SUB["a - b"]
  b --> SUB
  a --> LOG["and / or / xor"]
  b --> LOG
  ADD --> MUX["op mux"]
  SUB --> MUX
  LOG --> MUX
  op --> MUX
  MUX --> y
  MUX --> Z["zero flag"]

All operations compute in parallel; the opcode only selects which result reaches the output.

Stand-in: ripple adder, subtractor and flags in C

This stand-in (not Verilog) builds an 8-bit adder bit by bit from full-adder logic, uses it as an adder/subtractor and computes the flags, then compares with C arithmetic.

C Stand-in: ripple-carry add/sub with flags (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

For 127 + 1, the signed overflow flag is 1 (positive + positive gave a negative 128), while for 50 - 100 the carry flag is 0, meaning a borrow occurred.

Stand-in: a small ALU in C

C Stand-in: 8-bit ALU with zero flag (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Note sub and xor both give zero when the operands are equal, which is how a CPU tests equality.

Common mistakes

  • Losing the carry: wire [7:0] s = a + b; drops bit 8. Use a wider target or the {cout, sum} form.
  • Mixing signed and unsigned operands so a signed compare silently becomes unsigned (see operators).
  • Forgetting the + sub carry-in in the subtractor, giving a result that is off by one.
  • Leaving the ALU case without a default, which can infer a latch.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How is a - b computed with an adder?

    Two's complement negation is invert and add one; the +1 is fed in as carry-in.
  2. 2. Why is a ripple-carry adder slow for wide operands?

    The carry propagates bit by bit from LSB to MSB.
  3. 3. When does signed overflow occur in an addition?

    Adding two positives that produce a negative (or two negatives giving a positive) means the signed result did not fit.
  4. 4. What does the carry flag mean after a subtraction a - b (as built above)?

    With the inverted-b method, carry = 1 means no borrow.

Summary

  • A ripple-carry adder chains full adders; generate loops replicate them at build time, and plain + lets the tool choose a faster structure.
  • Carry lookahead uses generate and propagate signals to compute carries in parallel.
  • Subtract with a + ~b + 1; flags Z, N, C and V summarize results.
  • An ALU computes all operations in parallel and selects one with a mux driven by the opcode.

Next: Flip-Flops and always @(posedge clk)