C7 · Lesson 37 · Finite state machines
A circuit that steps through states
A traffic light, a vending machine, a USB controller and the control unit of a processor have something in common: each is always in one of a fixed set of states, and on each clock tick it moves to a next state chosen by its current state and its inputs. That's a finite state machine. Designing one is a recipe: draw the states and arrows, encode the states in flip-flops, and write the next-state logic as Boolean equations. The rest of this course's tools then take over.
Play
- A traffic light controller with three states: green, yellow, red. Two flip-flops hold the state; the lamps decode it.
- Watch the sequence on the timing diagram. Which state comes after red?
- Each state lasts one clock here. How would you make green last longer?
Predict
The controller above encodes green as S1 S0 = 00, yellow 01, red 10. Its next-state equations are S1 ← S0 and S0 ← ¬S1·¬S0. What happens after red?
Green. From red (10): S1 ← S0 = 0 and S0 ← ¬1·¬0 = 0, giving 00. The cycle is 00 → 01 → 10 → 00. The fourth code, 11, is never used; a careful design checks that it leads back into the cycle too, in case a glitch or a power-up ever lands the machine there. (Here, 11 goes to 10, red, so it recovers.)
Name it
Designing a state machine
- States and transitions. Draw a circle for each state and an arrow for each transition, labeled with the input condition that causes it.
- Encoding. Assign each state a binary code. N states need at least flip-flops. One-hot encoding uses N flip-flops, one per state, which wastes flip-flops but makes the logic simple and fast.
- Next-state table. For each current state and input, list the next state's code.
- Logic. Each flip-flop's D input is a Boolean function of the current state bits and inputs: simplify with K-maps.
- Outputs. In a Moore machine, outputs depend only on the state (glitch-free, one cycle behind). In a Mealy machine, outputs depend on the state and the inputs (faster to respond, but they can glitch with the inputs).
Unused codes. With 3 states in 2 flip-flops, one code is spare. Make sure every unused code leads back to a valid state.
Timing. For a state to last several clocks, add a counter: stay in the state until the counter reaches a set value, then move on and reset it.
Worked example
A traffic light cycles green, yellow, red. How many flip-flops does a binary encoding need, and how many codes are unused?
Show the solution
- Count the flip-flops.
- Count the unused codes. Two flip-flops give 4 codes, leaving 1 unused: the reset logic must make sure the light never starts there.
Answer 2 flip-flops, with 1 of the 4 codes unused.
Worked example
That light advances one state per clock, starting at green. Which color shows after 7 clocks?
Show the solution
- Divide by the cycle length. The cycle has length 3, and
- Step past green. Two full cycles end back at green, so the light is one step past green: yellow.
Answer yellow.
Worked example
A 2-bit up/down counter is a four-state machine: on U it adds 1, on D it subtracts 1, wrapping around. Starting at 00, it sees U, U, D, U, U, U. Where does it end?
Show the solution
- Follow the inputs. 00 → 01 → 10 → 01 → 10 → 11 → 00: the last U wraps from 11 back to 00.
- As a state machine. Each state has two outgoing arrows, one for U and one for D, and the whole behavior is a table of 4 × 2 transitions.
- Designing it. Writing out that table is the design; the logic follows from it mechanically.
Answer it ends at 00.
Check
At least how many flip-flops does a binary-encoded state machine with 5 states need?
Check
How many flip-flops does a one-hot state machine with 6 states use?
Check
A 3-state machine cycles 00 → 01 → 10 → 00 in its code S1 S0. Write the next-state equation for S0.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A vending machine's "dispense" output must react within the same clock cycle that the last coin is detected. Which kind of machine is that?
Check
In the traffic light (0 green, 1 yellow, 2 red), the machine is in state 1. Which state comes next?
Check
A state machine has 7 states.
(a)
How many flip-flops does a binary encoding need?
(b)
How many codes go unused?
Check
A state machine has s states and i input bits.
Step 1
How many flip-flops does a binary encoding need?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many does one-hot need?
Step 3
How many transitions must the next-state table specify?
Explain why
Why does a state machine need flip-flops, rather than just combinational logic?
A state machine's behavior depends on where it is in a sequence, not just on its present inputs: the same input can lead to different outputs depending on what happened before. Combinational logic has no memory, so it can only respond to the present inputs. Flip-flops hold the current state from one clock cycle to the next, and the combinational logic computes the next state from that stored state and the inputs; the clock then moves the machine forward one step at a time.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.