School/□ Systems Engineer/Week 24
A state machine in hardware
Turn a state-transition table into a circuit and it is always the same shape: a register and a cloud of gates. Why that shape?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
Every synchronous digital design, from a traffic-light controller to a CPU's control unit, follows this same template. Once you can turn a table into equations you can build any controller from gates and flip-flops.
- ◇ Implement an FSM as a state register plus next-state and output logic.
- ◇ Encode states in binary and derive next-state equations.
- ◇ Explain why the clock edge makes the design predictable.
2.Predict
A toggle machine has one flip-flop Q and input T. Each clock, Q flips if T = 1 and stays if T = 0. What is the next-state equation?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the logic lab, build the next-state logic for the parity machine: inputs Q (current state) and T (new bit), output D = Q XOR T. Then build the 2:1 multiplexer preset and explain how a multiplexer could choose between 'hold' and 'load' for a register.
Explorer 05 — Digital logic
Build answers out of ones and zeros
Every digital chip is built from a handful of simple gates. Flip the inputs to watch signals travel through a circuit, then wire your own gates to meet a target truth table.
- 1 (high)
- 0 (low)
- Unknown
- Inverting output
A single two-input gate. Change its type and watch the truth table change.
Inputs
Gates (1 of 8)
G1 = 0
Add a gate
Outputs
- Gates
- 1
- Depth
- 1
- Longest chain of gates a signal passes through
- Rows
- 4
- 2^2 input combinations
- Check
- —
| Set inputs | A | B | Y |
|---|---|---|---|
| 0 | 0 | 0 | |
| 0 | 1 | 0 | |
| 1 | 0 | 0 | |
| 1 | 1 | 1 |
State
Inputs A = 0, B = 0. Outputs Y = 0.
Inputs A = 0, B = 0. Outputs Y = 0.
How to read this
Signals flow left to right. Circles on the left are the inputs, boxes are gates and squares on the right are outputs. A thick bright wire carries a 1, a thin grey wire carries a 0, and each gate shows the value it outputs. A small circle on a gate's output means it inverts (NOT, NAND, NOR, XNOR).
The truth table lists every possible input combination and what the circuit outputs for each, so it describes the circuit completely. Two circuits with the same truth table are equivalent, however differently they are built. That is why NAND alone can make any other gate. Choose a row to set the inputs to it.
Assumptions & limitations
- Ideal logic: every wire is exactly 0 or 1. Real gates use voltage ranges (for example near 0 V and near 3.3 V or 5 V) and an input between them is undefined.
- Gates switch instantly here. Real gates take nanoseconds each, so a deeper circuit is slower, and outputs can glitch briefly while signals race through different paths.
- Only combinational circuits are allowed: no output may feed back into its own input. Feedback is how memory (latches and flip-flops) works, but it needs timing this model does not simulate, so loops are reported as an error.
- Gates here have one input (NOT) or two inputs. Real chips also have three- and four-input gates, and limits on how many inputs one output can drive (fan-out).
- Gate counts are a simple measure of size; real designs also weigh speed, power and the number of transistors per gate.
Can't use the interactive? Read a description instead
In the logic lab, D = Q XOR T gives D = 0, 1, 1, 0 for (Q, T) = 00, 01, 10, 11: the next state is the opposite of Q exactly when T = 1. The 2:1 multiplexer outputs input A when its select is 0 and input B when it is 1, which is how a register chooses between keeping its value and loading a new one.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which part of a hardware FSM holds the current state?
Practice 2
A controller has 9 states with binary encoding. How many flip-flops?
5.Understand
The template
State register: n D flip-flops holding the current state code. Next-state logic: combinational gates computing the next code from the current code and the inputs. Output logic: gates computing outputs from the state (Moore) or state and inputs (Mealy). On each rising clock edge the register loads the next state.
From table to equations
Give each state a binary code (S0 = 00, S1 = 01, S2 = 10). Rewrite the table as a truth table with inputs (Q1, Q0, x) and outputs (Q1⁺, Q0⁺). Each column is a Boolean function you can simplify and build from gates. For the 11 detector with this encoding: Q0⁺ = x·¬Q1·¬Q0, Q1⁺ = x·(Q1 + Q0), output = Q1.
Why a clock?
Between edges the gates settle; at the edge, every flip-flop samples together. So the circuit only ever moves from one well-defined state to the next, no matter how messy the gate delays are, as long as the logic settles before the next edge. That timing condition comes back in RTL design.
Explain it another way
The register is the machine's short-term memory; the gates are its rulebook. The clock is a metronome telling it when to turn the page.
Encoding choice matters: binary uses ⌈log₂N⌉ flip-flops; one-hot uses N flip-flops but simpler, faster logic, popular on FPGAs.
Q⁺ = δ(Q, x), y = λ(Q). For the parity machine Q⁺ = Q ⊕ x, y = Q. Test by enumerating all (Q, x) rows against the table.
Simple controllers in washing machines and lifts were once exactly this: a few flip-flops and a programmable logic array implementing the next-state equations.
Worked example 1
Verify Q0⁺ = x·¬Q1·¬Q0 and Q1⁺ = x·(Q1 + Q0) for the 11 detector with S0 = 00, S1 = 01, S2 = 10.
x = 0: both are 0, so every state goes to S0. ✓. x = 1 from S0 (00): Q0⁺ = 1, Q1⁺ = 0 → S1 ✓. From S1 (01): Q0⁺ = 0, Q1⁺ = 1 → S2 ✓. From S2 (10): Q0⁺ = 0, Q1⁺ = 1 → S2 ✓. Output y = Q1 is 1 only in S2 ✓.
Common mix-ups
- The clock tells the gates when to compute.
✓ Gates compute continuously; the clock tells the flip-flops when to capture the result.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
The same 9-state controller uses one-hot encoding. How many flip-flops?
Practice 4 · a fresh version every time
A Moore machine outputs 1 for one clock whenever the last 3 input bits were 101 (overlaps allowed). Input, first bit first: 000101110111. How many clocks have output 1?
Practice 5
Why does the state register load only on the clock edge?
7.Apply it
Write the next-state equations for the 'odd number of 1s' machine and for a 2-bit counter that counts 0, 1, 2, 3, 0… when its input E = 1 and holds when E = 0. Build the counter's Q0⁺ logic in the logic lab.
8.Reflect
Concepts: Super-loops and finite state machines · Sequential logic and memory