Draft preview. Lessons are being written and reviewed.

Digital Design / Module 2: Combinational building blocks

8. Half, full and ripple adders

Build addition out of gates.

Lecture coming soon

See it move

The idea

Addition in hardware works just as it does on paper: add one column, pass the carry to the next. A single column is a full adder.

SignalEquation
SumA ^ B ^ Cin
Carry outAB + Cin(A ^ B)

The sum bit is the parity of the three inputs. A carry comes out when at least two of them are 1. A half adder has no carry input, so it is just an XOR and an AND.

Ripple-carry

Chain N full adders, with each carry out feeding the next carry in.

module full_adder(input a, b, cin, output s, cout);
  assign s    = a ^ b ^ cin;
  assign cout = (a & b) | (cin & (a ^ b));
endmodule

module add4(input [3:0] a, b, input cin,
            output [3:0] s, output cout);
  wire [4:0] c;
  assign c[0] = cin;
  assign cout = c[4];
  full_adder fa0(.a(a[0]), .b(b[0]), .cin(c[0]), .s(s[0]), .cout(c[1]));
  full_adder fa1(.a(a[1]), .b(b[1]), .cin(c[1]), .s(s[1]), .cout(c[2]));
  full_adder fa2(.a(a[2]), .b(b[2]), .cin(c[2]), .s(s[2]), .cout(c[3]));
  full_adder fa3(.a(a[3]), .b(b[3]), .cin(c[3]), .s(s[3]), .cout(c[4]));
endmodule

The limit

The worst case is a carry born at bit 0 that travels all the way to the top, as in 0111 + 0001. Delay therefore grows in proportion to width. Doubling the width doubles the worst-case delay, which becomes a serious problem at 32 or 64 bits.

Common mistake

Thinking that the worst case depends on the values you happened to simulate. Static timing checks the longest structural path, whichever values you tried. A design can pass every simulation and still fail timing because of a carry chain your tests rarely exercised.

Key ideas

  • Sum is A xor B xor Cin.
  • Carry out is AB + Cin(A xor B).
  • Ripple delay grows linearly with width.

Your exercise

Write a 4-bit ripple-carry adder structurally.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: 3 + 1 gives 0 with no carry out, but 1 + 1 gives 2. Some additions are right and some are badly wrong.

Interview questions

coreWhat limits a ripple adder?

basicWhat is the difference between a half adder and a full adder, in gates?

basicA full adder has A = 1, B = 1 and carry in = 1. What are the sum and carry out?

coreHow many bits does the sum of two N-bit unsigned numbers need? Check with 4 bits.

coreThe lesson writes carry out as AB + Cin(A XOR B). Show that it equals the majority AB + ACin + BCin.

coreIn a 32-bit ripple-carry adder, each full adder's carry path takes 50 ps (illustrative). Roughly what is the worst-case delay, and which input pattern causes it?

coreThis line is meant to average two 8-bit unsigned values. With a = 200 and b = 100, it gives 22 instead of 150. Why?

wire [7:0] a, b;
wire [7:0] avg = (a + b) >> 1;

advancedA 64-bit ripple adder passes millions of random simulation vectors but fails static timing. Why didn't random tests hit the slow path, and does it matter?

advancedYou need the sum of three 8-bit unsigned numbers in one cycle. Besides two chained adders, how can it be done, and how wide is the result?

Practise these 9 questions with animations