C7 · Lesson 50 · Sandbox
Designing with gates and states
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?
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?
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.
Name it
The two designs
4-bit ripple-carry adder. Four full adders. Stage k computes
with and . For unsigned numbers, 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 | 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, and , and simplifying, gives the logic you'll build below.
A nickel arrives three times (N is high across three clock edges). V pulses on the third.
Worked example
The 4-bit adder adds 9 and 8. What are its sum and carry out?
Show the solution
- Add.
- 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.
Worked example
The vending machine receives 7 nickels, one per clock. How many items vend, and what state is it in?
Show the solution
- Count the items. It vends on every third nickel: 2 items.
- Find the state. The seventh nickel leaves it in the 5¢ state (01).
Answer 2 items, ending in the 5¢ state (01).
Check
Write a full adder's carry out, , from inputs A, B and carry in C.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
From the vending machine's table, write the next value of .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write the next value of .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write the dispense output V.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A 4-bit adder adds 0101 and 0001 with no carry in. What's the carry out?
Explain why
Why does a ripple-carry adder get slower as it gets wider?
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
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.