Volume 13 Beginner 5 sub-modules ~20 min read

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.

You will learn
  • 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
You need
  • Volume 04: the three-process style
  • Volume 05: state encodings
  • Setup time, from sub-module 0.3

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.

Where the time goes in one 10 ns clock cycle for a registered, a decoded and a Mealy output registered decoded Mealy time left for the next block: 9.5 ns output logic 7.5 ns left the input arrives late output logic 3 ns left next clock edge 0 2 4 6 8 10 ns
Figure 13.1 - Where the time goes in one 10 ns clock cycle (100 MHz), after the clock edge. The short gold part is the flip-flop's clock-to-Q delay. A registered output leaves the next block 9.5 ns, a decoded output 7.5 ns, and a Mealy output whose input came late from another block only 3 ns.

Three kinds of output, three budgets:

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.

Remember

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.

Common mistake

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.

Quick check

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.

A decoding glitch: Q1 rises a moment before Q0 falls, so the decoded output briefly sees the code 11 0 1 2 3 4 5 clk state 01 10 q1 q0 z_gates z_reg
Figure 13.2 - The machine steps from 01 to 10 at edge 2. Q1 rises a moment before Q0 falls, so for a fraction of the cycle the gates see 11 and z_gates flickers to 1. The registered version, z_reg, comes from a flip-flop and stays at 0.

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:

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.

Common mistake

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.

Quick check

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:

Remember

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.

The critical loop of a state machine: from the state register through the next-state logic and back Next-state logic State register logic delay flip-flops setup clk-to-Q inputs clk the loop that sets the clock speed
Figure 13.3 - The critical loop, in red. In one clock period, the state must leave the register (clock-to-Q), pass through the next-state logic, and reach the register's input a setup time before the next edge.

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:

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.

Common mistake

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.

Quick check

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.

Look-ahead: the registered done_r rises at the same edge as the compared done, but comes from a flip-flop 0 1 2 3 4 5 clk count 6 7 8 9 0 1 done done_r
Figure 13.4 - A small counter that wraps from 9 to 0 stands in for the long one. Both signals are 1 while the count is 9. The signal done comes out of compare logic, so it rises and falls a little after the edge. The flag done_r was decided one cycle earlier and comes from a flip-flop, so it changes cleanly at the edge.

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 it like this

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.

Common mistake

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.

Quick check

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:

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:

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
Common mistake

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.

Quick check

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

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

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.

Practice 2

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.

Practice 3

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

Interview question 1

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."

Interview question 2

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.