S3 · Lesson 4 · Finite state machines in HDL
Moore and Mealy machines, state encoding, and the three-block style
Control logic, protocols, sequencers and interface handshakes are all finite state machines. C7 designed them on paper with Karnaugh maps; real designs write them in HDL and let the synthesizer do the algebra, but the designer still makes the choices that matter: Moore or Mealy outputs, and how to encode the states. Binary encoding uses the fewest flip-flops, one-hot the simplest and fastest logic, Gray the fewest bits changing per transition. And every encoding leaves unused codes that a radiation upset or power glitch can land in, so a robust machine says what happens there too.
- A Moore machine that raises Y after seeing two 1s in a row on X, with states S0 (00), S1 (01) and S2 (10) in two flip-flops:
Q0 <= X & ~Q1 & ~Q0,Q1 <= X & (Q0 | Q1), andY = Q1. - Y comes from the state alone, so it changes only just after clock edges, one clock after the second 1 arrives.
- Code 11 is never used. Work out where the machine goes from it: here, S2 if X is 1 and S0 if it's 0, so it recovers on its own.
Predict
A state machine with 10 states is encoded one-hot. How many flip-flops does it use, and how many unused codes does it leave?
Ten flip-flops, one per state, and = 1014 codes that should never occur (any pattern without exactly one 1). Binary would use 4 flip-flops and leave 6 unused. One-hot's next-state logic is simple (each state's flip-flop is set by an OR of its incoming transitions), so it's fast, and FPGAs have flip-flops to spare. A safe design detects illegal codes, for one-hot by checking that exactly one bit is set, and returns to a known state.
Name it
Moore and Mealy
- Moore: outputs depend only on the state. They change just after clock edges and are glitch-free if registered.
- Mealy: outputs depend on state and inputs, so they can react in the same cycle, often with one fewer state, but they're combinational paths from input to output and can glitch.
A detector for an L-bit pattern needs about L states as a Mealy machine and L + 1 as a Moore machine.
The three-block style
always @(posedge clk) if (rst) state <= S0; else state <= next; // state register
always @* begin next = state; case (state) ... endcase end // next-state logic
always @* begin y = 0; case (state) ... endcase end // output logic
Defaults (next = state;, y = 0;) prevent latches and make unlisted cases hold.
State encoding
| Encoding | Flip-flops for N states | Notes |
|---|---|---|
| binary | fewest flip-flops, most complex logic | |
| Gray | one bit changes per step along a sequence: low power, safe across clock domains | |
| one-hot | N | simple, fast logic; easy to decode |
Unused states
Binary encoding of N states leaves unused codes; one-hot leaves . A default branch that sends any illegal state to the reset state makes the machine recover from upsets, though synthesis tools may optimize it away unless told to keep "safe" encoding.
Worked example
A UART receiver controller has 11 states. Compare binary, Gray and one-hot encodings.
Show the solution
- Binary and Gray. Each uses flip-flops, leaving
- One-hot. It uses 11 flip-flops, leaving
- When Gray helps. If the machine mostly steps through states in order (start bit, 8 data bits, stop bit), Gray encoding changes one flip-flop per step, which saves power and makes the state safe to sample from another clock domain.
Answer binary and Gray need 4 flip-flops with 5 unused codes; one-hot needs 11 with 2037 unused codes.
Worked example
Design the state count for detecting the pattern 1011 on a serial input, as Moore and Mealy machines.
Show the solution
- Mealy machine. It needs a state for each prefix matched so far: nothing, 1, 10 and 101. That's 4 states, with the output raised on the transition that completes 1011.
- Moore machine. It also needs a state meaning "just matched 1011", where the output is 1, so 5 states.
- Overlapping matches. With overlaps allowed, the machine goes from "matched" to the state for the longest suffix that's also a prefix, here "1".
Answer 4 states as a Mealy machine and 5 as a Moore machine.
Worked example
An 8-state machine steps through its states in a fixed order, like a counter. How many flip-flops toggle per step on average with binary and with Gray encoding?
Show the solution
- Binary toggles. Over one full cycle of 3-bit binary, bit 0 toggles 8 times, bit 1 four times and bit 2 twice:
- Gray toggles. A Gray sequence changes exactly one bit per step, 1.0.
- The saving. Clock and data power scale with toggles, so Gray saves of the state register's switching.
Answer 1.75 flip-flops toggle per step in binary and 1.0 in Gray, a 43% saving.
Check
A binary-encoded machine has 11 states. How many unused codes does it leave?
Check
A one-hot machine has 7 states. How many unused (illegal) codes does it leave?
Check
How many states does a Moore machine need to detect a non-overlapping 5-bit pattern?
Check
How many more flip-flops does one-hot use than binary for 13 states?
Check
In the two-ones detector (S0 = 00, S1 = 01, S2 = 10), write the next value of Q1: it's 1 when X = 1 and the machine is in S1 or S2.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
An 8-bit binary counter wraps from all ones back to zero. How many flip-flops change at once? (A Gray counter would change one.)
Check
A state machine has 7 states.
(a)
How many flip-flops does binary encoding use?
(b)
How many codes does it leave unused?
(c)
How many codes does one-hot encoding leave unused?
Check
An n-bit state register steps through all codes in order, once around.
Step 1
How many bit toggles does a binary count make in one full cycle?
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 per step on average?
Step 3
What fraction of that does a Gray sequence need?
Explain why
Why do Moore outputs avoid glitches that Mealy outputs can have?
A Moore machine's outputs are a function of the state alone, and the state lives in flip-flops that all change together just after a clock edge. Once the output logic settles after that edge, the outputs stay put for the rest of the cycle, and if they're taken straight from flip-flops (or registered) they don't glitch at all. A Mealy machine's outputs also depend on the inputs, which can change at any time during the cycle, possibly with several bits changing at slightly different moments. The output logic then passes those transients through as glitches, and it creates a combinational path from the machine's inputs to its outputs that the surrounding timing must accommodate.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.