State Machine Timing and Performance
A state machine that works in simulation still has to work at speed, on real silicon. This volume looks at time: how long an output takes to appear, where glitches come from, what limits a machine's clock frequency, and the tricks - registered outputs, look-ahead and one-hot - that make machines both fast and clean.
- Why registered outputs give the next block more time, and never glitch
- Where output glitches come from, and when they matter
- How the next-state loop sets the maximum clock frequency
- Look-ahead: making a decision one cycle early
- How FPGA and ASIC targets change the best design
13.1 Registered vs combinational outputs
An output that comes through gates after the state register arrives later in the clock cycle. An output that comes straight from a flip-flop is ready just after the clock edge, and leaves the next block almost the whole cycle.
Where the time goes
Nothing in a circuit is instant. After a rising clock edge, a flip-flop takes a moment to show its new value - its clock-to-Q delay. Then every gate that value passes through adds its own propagation delay. And the flip-flop at the end of the path needs its input steady for the setup time before the next edge. All of this must fit inside one clock period. How the period is shared out is the timing budget.
Now think about a state machine's output, and the block that receives it. Whatever time the output takes to appear is time taken away from that block.
Three kinds of output, three budgets:
- A registered output comes straight from a flip-flop. After the small clock-to-Q delay, it is ready, and the next block has almost the whole cycle.
- A decoded output - a Moore output made by gates from the state bits - arrives a little later: clock-to-Q, then the output logic.
- A Mealy output can be the worst. It depends on an input, and that input may itself come from another block's logic, arriving late in the cycle. The two delays add up.
Making an output registered
You met the method in Volume 04: decide the output from next_state, and let a flip-flop hold it.
// decoded: vend comes out of gates, after the state register
assign vend = (state == VEND);
// registered: vend comes straight out of a flip-flop, decided one cycle early
always @(posedge clk)
if (rst) vend_r <= 1'b0;
else vend_r <= (next_state == VEND);
Both change at the same clock edge. But vend_r comes out of a flip-flop, so it is ready sooner, and it is clean.
A common rule in chip design: register the outputs of every block. Then no path runs through gates in two blocks one after the other, and each block's timing can be checked on its own.
Chaining Mealy outputs from block to block. Each Mealy output passes its input straight through gates. So a chain of them becomes one long path of logic across several blocks, and the whole chain must fit in one clock cycle. It is a common way for a design that works slowly to fail at full speed.
Which kind of output leaves the receiving block the most time in the cycle?
Show the answer
Answer: B. A registered output is ready just after the clock-to-Q delay, early in the cycle. A decoded output also waits for the output logic, and a Mealy output may wait for a late input as well.
13.2 Glitch-free outputs
When several state bits change at one clock edge, they never change at exactly the same instant. Gates decoding them can pass through a wrong value for a moment - a glitch - unless the output comes straight from a flip-flop.
How a glitch is born
Take a machine with the output z = Q1·Q0: z is 1 only in state 11. Now let the machine step from 01 to 10. Both bits change. In a real chip, one flip-flop is always a little faster than the other. If Q1 rises before Q0 falls, then for a moment the gates see 11 - and z flickers to 1, although the machine never visited state 11. A short false pulse like this is a glitch.
When does a glitch matter?
Not always. If z only feeds flip-flops on the same clock, the glitch is over long before the next edge, and those flip-flops never see it. A glitch does harm when something reacts to z at once:
- z drives a clock, or an asynchronous reset, of another circuit
- z is read by a circuit on a different clock
- z leaves the chip and drives another device
- z counts events, as a pulse counter would
Four ways to be glitch-free
| Method | How it works |
|---|---|
| Registered output | decide it from next_state; a flip-flop drives it (Volume 04) |
| Output encoding | choose codes so that the output is a state bit itself (Volume 05) |
| Gray codes | on the transitions that matter, only one bit changes, so there is no false in-between code |
| Careful one-hot | with one-hot, a transition turns one bit off and another on - see the box below |
Going deeper: a glitch hidden in one-hot
In the one-hot washing machine of Volume 05, pump = DRAIN + SPIN. When the machine steps from DRAIN to SPIN, the DRAIN bit falls and the SPIN bit rises. If DRAIN falls a moment first, both are 0 for an instant, and pump flickers off - although it should stay on. For an output that must be clean, register it, however the states are encoded.
Reasoning about glitches from simulation. An RTL simulator changes all the bits of a register at exactly the same moment, so decoding glitches never appear in its waveforms. They appear in real hardware - and in timing simulations of the placed design. Design them out; do not wait to see them.
A decoded output glitches for a moment after some clock edges. It feeds only flip-flops on the same clock. Is that a problem?
Show the answer
Answer: A. Flip-flops look only at the moment of the clock edge. The glitch comes and goes early in the cycle, so by the next edge the output is steady. Glitches matter when something reacts at once - a clock, an asynchronous reset, another clock domain or another chip.
13.3 The FSM critical path
A state machine's clock speed is limited by its loop: from the state register, through the next-state logic, and back into the state register - all within one clock period.
The loop
Every state machine has a loop at its heart. The state leaves the register, passes through the next-state logic, and must arrive back at the register's input in time for the next edge. So the clock period must be at least:
clock period ≥ clock-to-Q delay + next-state logic delay + setup time
The fastest clock that satisfies this is the machine's maximum clock frequency, or fmax. The slowest path in a design is its critical path, and in a state machine it is usually this loop.
What makes the loop slow
The first two terms are fixed by the flip-flops. The one you control is the next-state logic. It grows slow when:
- many state bits must be decoded - large binary-coded machines
- long priority chains of
if ... else if ... else if, where each test waits for the one before - wide comparisons in the loop, such as
timer == 1000000 - arithmetic feeds a decision, as the GCD's comparator feeds its controller in Volume 09
A worked example
Suppose each logic level - one gate on the path - takes 0.5 ns, and the clock-to-Q delay and the setup time are 0.5 ns each.
| Machine | Logic levels in the loop | Clock period | fmax |
|---|---|---|---|
| Binary encoding | 6 | 0.5 + 6 × 0.5 + 0.5 = 4.0 ns | 250 MHz |
| One-hot encoding | 2 | 0.5 + 2 × 0.5 + 0.5 = 2.0 ns | 500 MHz |
The same machine, twice as fast, only because the next-state logic is shallower. This is the real reason for the one-hot advice in Volume 05. The full set of timing equations is in the timing volume of the Verilog course.
Trying to speed up the loop by putting a register in the middle of the next-state logic. That changes the machine: the next state would arrive one cycle late, so the machine would decide on old information. A loop cannot simply be cut; it has to be made shallower - or the slow part moved out of it, as the next sub-module shows.
A state machine has a clock-to-Q delay of 0.4 ns, next-state logic of 2.2 ns and a setup time of 0.4 ns. What is its fmax?
Show the answer
Answer: C. The clock period must be at least 0.4 + 2.2 + 0.4 = 3.0 ns. One cycle every 3.0 ns is 1 / 3.0 ns, about 333 million cycles per second - 333 MHz.
13.4 Look-ahead and pipelined control
When a decision is too slow to make in time, make it one cycle earlier and store the answer in a flip-flop. The machine then uses a ready-made result instead of waiting for slow logic.
Deciding one cycle early
Look at a timer that ends a state when it reaches 999,999. The obvious code compares every cycle:
// slow: a 20-bit compare sits in the state machine's loop
wire done = (count == 20'd999_999);
// fast: compare one cycle early, and keep the answer in a flip-flop
reg done_r;
always @(posedge clk)
if (rst) done_r <= 1'b0;
else done_r <= (count == 20'd999_998); // 1 exactly while count is 999_999
If the count goes up by one each cycle, then one cycle after it equals 999,998, it equals 999,999. So done_r, registered from the earlier compare, is 1 in exactly the same cycle as done. But done_r comes straight from a flip-flop: the 20-bit compare has moved out of the loop into a cycle of its own. This trick is called look-ahead.
The registered outputs of Volume 04 - decided from next_state - are the same idea applied to the
outputs: look one cycle ahead, and store the answer.
Control that travels with the data
In a pipeline, a piece of data moves through several stages, one stage per cycle, with registers between them. The controller must tell each stage what to do when the data reaches it, not when the data starts. So the control signals are delayed through their own chain of registers, one per stage, and travel alongside the data.
Think of a car moving down a factory line. The instruction "paint it red" is written on a card that rides on the car. When the car reaches the paint station, the card is there too. Nobody has to remember which car was which.
Using look-ahead where the value can jump. The trick above works because the count only ever goes up by one. If the count can also be loaded with a new value, the early compare must include that case too. Otherwise done_r can be wrong in the first cycle after a load.
A counter counts up by one each cycle. To have a registered flag that is 1 exactly while the count is 50, what should the flip-flop store at each edge?
Show the answer
Answer: D. The flag must be 1 in the cycle when the count is 50. A registered value shows, in each cycle, what was computed in the cycle before - when the count was one less. So the flip-flop stores count == 49.
13.5 FSMs on FPGA vs ASIC
The best way to build a state machine depends on the target. An FPGA has flip-flops to spare and counts logic in levels of look-up tables. On an ASIC, every flip-flop costs area and power.
On an FPGA
An FPGA's logic is built from look-up tables, or LUTs: tiny memories that can hold any function of their inputs - typically up to six. Each LUT sits beside one or more flip-flops. The architecture is explained in the FPGA course.
What this means for state machines:
- Flip-flops are almost free. They are there whether you use them or not, so one-hot costs little.
- Speed is counted in LUT levels. A next-state function of six inputs or fewer fits in one LUT. One-hot keeps most next-state functions that small.
- The tools help. FPGA synthesis tools usually re-encode state machines as one-hot by themselves (Volume 05).
- Flip-flops start known. After an FPGA is loaded, its flip-flops hold known starting values. Some designs rely on this instead of a reset - a habit to use with care, because a reset may still be needed later.
On an ASIC
An ASIC is built from standard cells - gates and flip-flops from the chip maker's library, as the ASIC course shows. There:
- A flip-flop is several times the size of a simple gate, and it uses power on every clock edge. For small machines, binary or Gray encoding usually wins.
- Power matters. An idle machine's clock can be switched off - clock gating - so its flip-flops stop using power.
- Testing is built in. Chips are tested after they are made by linking the flip-flops into long shift registers, called scan chains. Every state register takes part, so the design must not depend on tricks that scan would break.
Side by side
| FPGA | ASIC | |
|---|---|---|
| Flip-flops | plentiful - one beside every LUT | expensive in area and power |
| Usual encoding | one-hot | binary or Gray for small machines |
| Speed limit | LUT levels in the loop | gate delays in the loop |
| Reset style often preferred | synchronous | asynchronous, released in step with the clock |
| Power saving | lower toggle rates, fewer resources | clock gating, fewer flip-flops |
Porting an FPGA design to an ASIC without looking at its state machines. A dozen one-hot machines that cost nothing on the FPGA can cost real area and power on the chip. Review the encodings - and the reset style - when the target changes.
Why is one-hot encoding usually a good choice on an FPGA?
Show the answer
Answer: B. Every FPGA logic block already has flip-flops, so extra ones cost almost nothing. One-hot next-state functions depend on only a few bits, so each fits in one or two LUT levels, which keeps the loop short and the clock fast.
What you learned
- Every path must fit in the clock period: clock-to-Q, then logic, then the next flip-flop's setup time.
- Registered outputs are ready just after the edge and never glitch; decoded and Mealy outputs arrive later.
- Glitches come from state bits changing at slightly different moments; they matter when something reacts at once.
- A state machine's fmax is set by its loop: clock-to-Q + next-state logic + setup. Shallower logic means a faster clock.
- Look-ahead makes a decision one cycle early and stores it in a flip-flop, moving slow logic out of the loop.
- FPGAs favour one-hot and count LUT levels; ASICs pay for every flip-flop, and use clock gating and scan.
Key words from this volume
Every word below has a plain-English entry in the glossary.
- Clock-to-Q delay
- Propagation delay
- Setup time
- Timing budget
- Registered output
- Glitch
- Maximum clock frequency (fmax)
- Critical path
- Logic level
- Look-ahead
- Pipeline
- Look-up table (LUT)
- Standard cell
- Clock gating
- Scan chain
Practice
Budget the cycle
A design runs at 200 MHz. Its flip-flops have a clock-to-Q delay of 0.3 ns and a setup time of 0.2 ns. How much time is left for the next-state logic?
Show the solution
At 200 MHz, one clock period is 1 / 200,000,000 s = 5 ns. Take away the flip-flop's delays: 5 - 0.3 - 0.2 = 4.5 ns for the next-state logic. At 0.5 ns per logic level, that is at most nine levels.
Spot the glitch
A machine's output is led = (state == 2'b11). The machine steps from 01 to 10. Could led glitch?
Could it glitch on the step from 00 to 01?
Show the solution
From 01 to 10, both bits change. If bit 1 rises before bit 0 falls, the gates briefly see 11, and led flickers on - yes, it can glitch. From 00 to 01, only bit 0 changes, so the code passes only through 00 and 01, never 11 - no glitch. Registering led, or choosing codes so that no step passes near 11, removes the risk.
Precompute a flag
This counter is loaded with a value, then counts down by one each cycle until it reaches 0, where it stays:
always @(posedge clk)
if (rst) count <= 0;
else if (load) count <= load_value;
else if (count != 0) count <= count - 1;
Write a registered flag zero_r that is 1 exactly while the count is 0.
Show the solution
In each cycle, zero_r shows what the flip-flop stored at the edge before. So at each edge it must store whether the new count will be 0. Follow the counter's own rules:
- After reset, the count is 0.
- After a load, the count is load_value.
- Otherwise, a count of 1 steps down to 0, and a count of 0 stays at 0.
always @(posedge clk)
if (rst) zero_r <= 1'b1;
else if (load) zero_r <= (load_value == 0);
else zero_r <= (count <= 1);
A common slip is to store only count == 1. Then zero_r is 1 for just one cycle, and drops back to
0 while the count is still resting at 0.
Interview corner
Your FSM misses timing
"Your state machine fails timing at the target clock frequency. What can you do?"
Show the solution
"First find the critical path in the timing report - usually the loop from the state register through the next-state logic. Then shorten it. Switch to one-hot, so each next-state bit depends on only a few others. Move slow conditions out of the loop with look-ahead: compare a counter one cycle early and register the flag, or register status signals coming from the datapath. Flatten long if-else priority chains. Split a large machine into smaller ones that talk through registered handshakes. And register the outputs, so paths do not continue into the next block. Cutting the loop with a plain register is not an option - that changes the machine's behaviour."
Moore or Mealy, for speed?
"Which is faster, a Moore or a Mealy machine?"
Show the solution
"It depends what you mean by fast. A Mealy machine responds a cycle earlier, so its latency is lower. But its outputs are combinational paths from the inputs, which can lengthen the critical path - especially when Mealy outputs feed other blocks. A Moore machine with registered outputs responds a cycle later, but every path it drives starts at a flip-flop, which usually allows a higher clock frequency. For throughput at high clock rates, registered outputs usually win."
Next, Volume 14 gathers the whole course onto one page, with solved design problems, concept questions, spot-the-bug drills and flashcards, ready for exams and interviews.