D10 · Lesson 6 · Finite state machines in Verilog
Writing a state machine as a state register, next-state logic and output logic, Moore and Mealy outputs, encoding states in binary or one-hot, a rising-edge detector as the smallest useful example, and timers built from counters for machines that wait
The controller of lesson 3, a traffic light, a USB protocol engine and a vending machine are all finite state machines: a register remembering which state the system is in, logic deciding the next state from the current state and the inputs, and logic producing outputs. Verilog designers write them in a fixed, recognizable pattern, because a state machine written any other way is hard to read and easy to get wrong. The smallest useful one fits in two lines: a detector that fires for one clock when a button goes from released to pressed.
- A rising-edge detector: a flip-flop Q remembers the input X from the previous clock, and the output P = X & ~Q is 1 for exactly the first clock cycle after X rises.
- Hold X high for many cycles: P still pulses once. That's how a button press becomes a single event.
- This is a two-state machine (Q = 0 or 1) with a Mealy output, one that depends on the input as well as the state.
Predict
A traffic light must stay green for 30 s, and its state machine counts ticks of a 1 kHz clock. How many bits does its timer need?
30 s at 1 kHz is 30,000 ticks, and = 16,384 < 30,000 ≤ = 32,768, so a 15-bit counter. The state machine resets the counter on entering green and moves on when it reaches 29,999: timers like this are how state machines wait.
Name it
The three-part pattern
// 1. state register
always @(posedge clk)
if (reset) state <= IDLE;
else state <= next;
// 2. next-state logic
always @(*) begin
next = state;
case (state)
IDLE: if (coin) next = PAID;
PAID: if (push) next = IDLE;
endcase
end
// 3. output logic
assign unlocked = (state == PAID);
Moore and Mealy
- A Moore output depends only on the state, so it changes only at clock edges and is glitch-free.
- A Mealy output depends on the state and the inputs, so it can respond in the same cycle, as the edge detector's P does, but it can glitch when the inputs do.
Encoding
- Binary: S states in flip-flops; n flip-flops hold up to states.
- One-hot: one flip-flop per state, exactly one set at a time; more flip-flops but simpler, faster logic, which suits FPGAs with their plentiful flip-flops.
- Gray: adjacent states differ in one bit, useful when the state crosses clock domains (lesson 8).
Timers
A machine that waits counts clock ticks: waiting T seconds at f hertz takes counter bits.
Worked example
A vending machine has 9 states. How many flip-flops do binary and one-hot encodings need? How long a timer for a 250 ms debounce at 50 MHz?
Show the solution
- Binary encoding. giving 16 codes, 7 unused.
- One-hot encoding. One flip-flop per state: 9.
- Debounce ticks. 0.25 s at 50 MHz is
- Timer width.
Answer 4 flip-flops in binary and 9 in one-hot, and a 24-bit timer for the debounce.
Worked example
X is 0 for two clocks, then 1 for five, 0 for one, and 1 for three. How many pulses does the edge detector produce?
Show the solution
- What makes a pulse. The edge detector makes one pulse at each 0-to-1 transition, regardless of how long X then stays high.
- Count the transitions. X rises once after its first two clocks of 0, and again after its one clock of 0.
Answer two pulses.
Worked example
A detector for the overlapping sequence 101 is built as a Moore and as a Mealy machine. How many states does each need?
Show the solution
- Mealy machine. Three states (nothing yet, seen 1, seen 10), with the output 1 on the transition from "seen 10" on input 1.
- Moore machine. It needs a fourth state, "seen 101", whose output is 1, and the detection appears a clock later.
- The trade-off. Mealy machines are smaller and faster; Moore outputs are glitch-free.
Answer 3 states as a Mealy machine and 4 as a Moore machine.
Check
A flip-flop Q holds the input X from the previous clock. Write the rising-edge detector's output P, which is 1 when X is 1 now but was 0 last clock.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A state machine must wait 1 s, counting a 1 kHz clock. How many bits does its counter need?
Check
How many states can a binary-encoded state machine with 5 flip-flops have at most?
Check
A state machine's output must never glitch, even when its inputs are noisy. Which kind of output should it use?
Check
A debounce timer must wait 50 ms on a 25 MHz clock.
(a)
How many ticks is that?
(b)
How wide must the counter be?
Check
A detector for an overlapping 4-bit sequence with no internal repeats (like 1000…01 patterns) is built as a Mealy machine.
(a)
How many states does it need (one for each prefix seen, from none to 4 − 1 bits)?
(b)
And as a Moore machine, which needs a state for the full match?
Explain why
Why is a state machine written as separate state-register, next-state and output parts?
The three parts are different kinds of hardware: the state register is a bank of flip-flops that changes only at clock edges, while the next-state and output logic are combinational circuits that react continuously. Writing each in its own block makes the code mirror that structure, so the synthesizer infers exactly the flip-flops intended and nothing else, and a reader can see at a glance which signals are registered. It also keeps the combinational parts complete, with a default next state and every output assigned, which avoids accidental latches, and it makes the transitions easy to check against the state diagram the design started from.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.