C7 · Lesson 39 · Designing finite state machines
From a word description to flip-flops and gates
Lesson 37 read state machines; this lesson designs them. The recipe is always the same: turn the word description into a state diagram, the diagram into a table, choose a binary code for each state, and derive the next-state and output logic from the table, exactly as for any combinational circuit. The choices along the way, Moore or Mealy outputs, binary or one-hot codes, and which states can be merged, decide how many flip-flops and gates the machine needs and when its outputs respond.
- A Moore machine that detects the bit pattern 101 on its input X, overlapping matches allowed. Its four states (nothing yet, "1", "10", "101") are coded in two flip-flops, , and the output Z is 1 in the "101" state.
- The input stream is 1, 0, 1, 0, 1, 1, 0, 1 on successive clock edges. Z goes high three times: the second 101 shares its first 1 with the end of the first.
Predict
A machine must detect the sequence 1011 (overlapping allowed). As a Moore machine, how many states does it need?
5: one for "nothing matched yet" and one for each prefix matched so far (1, 10, 101, 1011), with the output 1 in the last. A Mealy machine can produce the output on the transition into the final match, so it needs only 4.
Name it
The design procedure
- State diagram: a circle per state, an arrow per transition labeled with the input that causes it. Each state should mean something you can say in words ("the last two bits were 10").
- State table: for every state and input combination, the next state and the output.
- Encoding: give each state a binary code.
- Logic: the next-state bits and outputs are combinational functions of the present state bits and inputs; minimize them with K-maps.
- Build: flip-flops for the state, that logic, and a reset into the starting state.
Moore and Mealy
- Moore: outputs depend only on the state. They change only at clock edges and are glitch-free, but respond a cycle after the input that causes them.
- Mealy: outputs depend on the state and the present inputs. They respond within the same cycle and often need fewer states, but an input glitch can reach the output.
Encodings
| Encoding | Flip-flops for N states | Logic |
|---|---|---|
| binary | denser; next-state logic can be complex | |
| Gray | one bit changes per step in sequential machines | |
| one-hot | N | one flip-flop per state; very simple logic, favored in FPGAs |
With binary coding the unused codes become don't-cares, but the reset logic must make sure the machine can't start or get stuck in one.
Minimization
Two states are equivalent if they give the same outputs and, for every input, go to equivalent next states; equivalent states merge. Merging repeatedly until nothing more combines leaves the fewest states.
Worked example
Derive the next-state and output logic of the 101 detector, with S0 = 00, S1 ("1") = 01, S2 ("10") = 10, S3 ("101") = 11.
Show the solution
- Low bit. On X = 1 every state goes to a state whose low bit is 1 (S1 or S3), and on X = 0 to one whose low bit is 0, so
- High bit. is 1 for S1 → S2 (X = 0), S2 → S3 (X = 1) and S3 → S2 (X = 0):
- Output. The output is the S3 state:
Answer , and .
Worked example
A controller has 6 states. Compare binary and one-hot encodings.
Show the solution
- Binary. It needs = 3 flip-flops and leaves 2 unused codes.
- One-hot. It needs 6 flip-flops, but each next-state equation is just an OR of the transitions into that state, with no decoding of the present state.
Answer binary takes 3 flip-flops and leaves 2 codes unused; one-hot takes 6 flip-flops but needs no decoding of the present state.
Worked example
A detector must flag the pattern 1011 in a serial stream, overlaps allowed. How many states do Moore and Mealy versions need, and how many flip-flops?
Show the solution
- Moore. It needs a state for each prefix matched so far, "", "1", "10", "101", plus one for "1011" whose output is 1: five states, = 3 flip-flops.
- Mealy. It raises its output on the transition that completes the pattern, so it needs no separate "found" state: four states, 2 flip-flops.
- Timing. The Mealy output appears in the same cycle as the last bit; the Moore output a clock later, but glitch-free.
Answer Moore needs five states and 3 flip-flops; Mealy needs four states and 2 flip-flops.
Check
How many flip-flops does a binary-encoded machine with 5 states need?
Check
How many unused codes does a binary encoding of 5 states leave?
Check
In the 101 detector (S0 = 00, S1 = 01, S2 = 10, S3 = 11), write the next-state bit .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
For the same detector, write .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
How many states does a Moore machine need to detect a particular 4-bit sequence?
Check
Two states give the same output. On input 0 both go to state A; on input 1 one goes to B and the other to C, which are themselves equivalent. What can you do?
Check
The 101 detector (S0 = 00, S1 = 01, S2 = 10, S3 = 11; S1 goes to S2 on 0, S2 to S3 on 1, S3 to S2 on 0, and every other move lands on S0 or S1) has next-state bit . Write it.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A detector must flag a 4-bit pattern, overlaps allowed.
(a)
How many states does a Moore version need?
(b)
How many flip-flops does a Mealy version need?
Explain why
Why does a Mealy machine usually need fewer states than a Moore machine for the same job?
A Moore machine's outputs belong to its states, so any situation that needs a different output must be a separate state, even if it behaves the same afterward. A Mealy machine's outputs depend on the current input as well, so it can produce the output on the transition itself, the moment the deciding input arrives. The state that would exist only to hold that output isn't needed, so a sequence detector, for example, needs one fewer state. The price is that the output follows the input directly, within the cycle.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.