S34 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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), and Y = 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?

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

EncodingFlip-flops for N statesNotes
binary⌈log2⁡N⌉fewest flip-flops, most complex logic
Gray⌈log2⁡N⌉one bit changes per step along a sequence: low power, safe across clock domains
one-hotNsimple, fast logic; easy to decode

Unused states

Binary encoding of N states leaves 2⌈log2⁡N⌉−N unused codes; one-hot leaves 2N−N. 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
  1. Binary and Gray. Each uses ⌈log2⁡11⌉=4 flip-flops, leaving 16−11=5unused codes
  2. One-hot. It uses 11 flip-flops, leaving 2048−11=2037unused codes
  3. 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
  1. 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.
  2. Moore machine. It also needs a state meaning "just matched 1011", where the output is 1, so 5 states.
  3. 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
  1. Binary toggles. Over one full cycle of 3-bit binary, bit 0 toggles 8 times, bit 1 four times and bit 2 twice: 8+4+28=1.75per step
  2. Gray toggles. A Gray sequence changes exactly one bit per step, 1.0.
  3. The saving. Clock and data power scale with toggles, so Gray saves 1−11.75=43% 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 2n 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°.

Explain why

Why do Moore outputs avoid glitches that Mealy outputs can have?

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.