---
title: "Sandbox: a 4-bit adder and a vending-machine FSM"
heading: Designing with gates and states
eyebrow: C7 · Lesson 50 · Sandbox
xp: 70
---

::: hook
Two classic designs, worked from the specification to the logic. First, a 4-bit ripple-carry adder: four full adders in a chain, the circuit at the core of every processor's ALU. Second, a vending machine that sells a 15¢ item for nickels: a state machine that counts coins and dispenses on the third. Each design ends in Boolean equations, which you'll write and the cards will check against the truth table.

::: play
- A 4-bit adder: set A (A3 to A0) and B (B3 to B0) with the switches, and Cin. The outputs are the 4-bit sum and the carry out.
- Add 1 to 15 (1111 + 0001). Watch the carry ripple up through all four stages, one gate delay at a time.
- What does Cout mean when the inputs are unsigned numbers?
:::
:::

```sim logic
input A3, A2, A1, A0, B3, B2, B1, B0, Cin
output S3, S2, S1, S0, Cout
assign P0 = A0 ^ B0
assign S0 = P0 ^ Cin
assign C1 = A0 & B0 | Cin & P0
assign P1 = A1 ^ B1
assign S1 = P1 ^ C1
assign C2 = A1 & B1 | C1 & P1
assign P2 = A2 ^ B2
assign S2 = P2 ^ C2
assign C3 = A2 & B2 | C2 & P2
assign P3 = A3 ^ B3
assign S3 = P3 ^ C3
assign Cout = A3 & B3 | C3 & P3
set A3 = 1
set A2 = 1
set A1 = 1
set A0 = 1
```

::: predict
## In a 4-bit ripple-carry adder with a gate delay of 1 tick per gate, roughly how long does adding 1111 + 0001 take to settle?

- [ ] 1 tick
- [ ] 2 ticks
- [x] About 8 or more ticks: the carry passes through every stage
- [ ] It depends on the clock

::: reveal
Around 8 or more. Each stage's carry out needs its carry in, and each carry takes a couple of gate delays (an AND then an OR), so the carry from bit 0 ripples through four stages before the sum settles. That's the worst case, and it grows with the number of bits: a 64-bit ripple adder is far too slow, which is why processors use carry-lookahead adders.
:::
:::

::: nameit
## The two designs

**4-bit ripple-carry adder.** Four full adders. Stage k computes

$$S_k = A_k \oplus B_k \oplus C_k \qquad C_{k+1} = A_kB_k + C_k(A_k \oplus B_k)$$

with $C_0 = C_\text{in}$ and $C_4 = C_\text{out}$. For unsigned numbers, $C_\text{out} = 1$ means the sum didn't fit in four bits. For signed numbers, overflow is instead when the carries into and out of the top stage differ.

**Vending machine** (15¢ item, nickels only, one coin per clock). States by cents so far: 0 (code 00), 5 (01), 10 (10). Input N = 1 when a nickel arrives this cycle; output V = 1 dispenses.

| state $S_1S_0$ | N = 0 next | N = 1 next | V |
|---|---|---|---|
| 00 (0¢) | 00 | 01 | 0 |
| 01 (5¢) | 01 | 10 | 0 |
| 10 (10¢) | 10 | 00 | N |

The dispense output depends on N as well as the state: a Mealy output. Writing each next-state bit as a function of N, $S_1$ and $S_0$, and simplifying, gives the logic you'll build below.
:::

```sim logic
input N
output V, S1, S0
clock CLK 8
always @(posedge CLK) begin
  S1 <= N & S0 | ~N & S1;
  S0 <= N & ~S1 & ~S0 | ~N & S0;
end
assign V = N & S1
stim N 0@0 1@6 0@12 1@22 0@28 1@38 0@44
wave: CLK, N, S1, S0, V
time: 56
```

A nickel arrives three times (N is high across three clock edges). V pulses on the third.


::: example
## The 4-bit adder adds 9 and 8. What are its sum and carry out?

::: solution
1. **Add.**
   $$\begin{aligned} 9 + 8 &= 17 \\ &= 1\,0001_2 \end{aligned}$$
2. **Split the bits.** The 4-bit sum is 0001 and the carry out is 1.

**Answer:** the sum is 0001 and the carry out is 1.
:::
:::

::: example
## The vending machine receives 7 nickels, one per clock. How many items vend, and what state is it in?

::: solution
1. **Count the items.** It vends on every third nickel: 2 items.
2. **Find the state.** The seventh nickel leaves it in the 5¢ state (01).

**Answer:** 2 items, ending in the 5¢ state (01).
:::
:::

::: check
kind: boolean
vars: A, B, C
answer: A & B | C & (A ^ B)

## Write a full adder's carry out, $C_\text{out}$, from inputs A, B and carry in C.

::: right
Right. A carry comes from both inputs, or from one input plus the carry in. (AB + AC + BC works too.)
:::

::: misses
- [A & B] That misses the carries made with the carry in.
- [A ^ B ^ C] That's the sum bit.
- [any] Not yet. $C_\text{out}$ is 1 when at least two of A, B and C are 1.
:::
:::

::: check
kind: boolean
vars: N, S1, S0
answer: N & ~S1 & ~S0 | ~N & S0

## From the vending machine's table, write the next value of $S_0$.

::: right
Right. $S_0$ is next 1 when a nickel arrives at 0¢, or when no nickel arrives at 5¢.
:::

::: misses
- [N & ~S1 & ~S0] That forgets to hold 5¢ when no coin arrives.
- [~N & S0] That forgets the move from 0¢ to 5¢.
- [any] Not yet. Find each row where the next $S_0$ is 1.
:::
:::

::: check
kind: boolean
vars: N, S1, S0
answer: N & S0 | ~N & S1

## Write the next value of $S_1$.

::: right
Right. $S_1$ is next 1 when a nickel arrives at 5¢, or when no nickel arrives at 10¢.
:::

::: misses
- [N & S0] That forgets to hold 10¢ when no coin arrives.
- [any] Not yet. Find each row where the next $S_1$ is 1.
:::
:::

::: check
kind: boolean
vars: N, S1, S0
answer: N & S1

## Write the dispense output V.

::: right
Right. A nickel arriving at 10¢.
:::

::: misses
- [S1] That would dispense at 10¢ even with no coin.
- [any] Not yet. Dispense when the third nickel arrives.
:::
:::

::: check
given: a = 0..15 int; b = 0..15 int
answer: floor((a + b)/16)
tolerance: 0.01

## A 4-bit adder adds {a:bin4} and {b:bin4} with no carry in. What's the carry out?

::: right
Right: {answer}. It's 1 exactly when the sum exceeds 15.
:::

::: misses
- [1 - floor((a + b)/16)] Carry out is 1 when the sum doesn't fit in 4 bits.
- [any] Not yet. Add them; is the sum 16 or more?
:::
:::

::: explain
## Why does a ripple-carry adder get slower as it gets wider?

::: model
Each stage's sum and carry out depend on the carry coming in from the stage below, so a stage can't settle until every stage below it has. In the worst case, such as adding 1 to all ones, a carry is generated at the bottom and has to pass through every stage in turn, each adding its own gate delays. So the settling time grows in proportion to the number of bits, which limits the clock speed of anything that uses the adder.
:::
:::

::: practice

::: check
kind: boolean
vars: A, B, C
answer: A ^ B ^ C

## A full adder's sum bit?

::: misses
- [any] The XOR of all three.
:::
:::

::: check
given: n = 4 | 8 | 16 | 32
answer: 2*n
tolerance: 0

## Gate delays for an {n}-bit ripple carry, at 2 per stage?

::: misses
- [n] Two per stage.
- [any] $2n$.
:::
:::

::: check
given: a = 0..7 int; b = 0..7 int
answer: a + b - 16*floor((a + b)/16)
base: 2
width: 4

## {a:bin4} + {b:bin4}: the 4-bit sum?

::: misses
- [any] Add with carries; keep 4 bits.
:::
:::
:::
