From state diagrams to hardware implementation
Prerequisites¶
This tutorial assumes you have completed:
D Flip-Flops in Digital Design — understanding edge-triggered storage and clock signals
Timing Diagrams in Digital Design — reading waveforms and understanding signal timing
You should also be familiar with:
Binary numbers and basic Boolean algebra (AND, OR, NOT)
Logic gates and how to combine them into circuits
What is a Finite State Machine?¶
Finite State Machines (FSMs) are fundamental building blocks in digital design. They model systems that can be in one of a finite number of states, transitioning between states based on inputs.
An FSM consists of:
States: A finite set of conditions the system can be in
Inputs: External signals that influence state transitions
Outputs: Signals produced by the machine
Transitions: Rules for moving between states based on inputs
Initial State: The starting state when the system powers on
Think of a traffic light: it cycles through states (Red → Green → Yellow → Red), with timing inputs controlling transitions and the light color as output.
State Diagrams: Visualizing FSMs¶
State diagrams use circles for states and arrows for transitions. Let’s visualize a simple two-state FSM that toggles between ON and OFF based on a button press.
This is a Moore machine — the output (LED on/off) depends only on the current state.
Color convention: Green = output HIGH (LED on), Grey = output LOW (LED off)
This FSM has:
2 states: OFF, ON
1 input: button (0 or 1)
1 output: LED (0=off, 1=on) — determined by the current state
State OFF → LED = 0
State ON → LED = 1
Transitions: Toggle state when button=1, stay in current state when button=0
Initial state: OFF (indicated by the incoming arrow with no source state)
Understanding transitions:
button=1 means: “the button signal was HIGH when sampled at the clock edge”
button=0 means: “the button signal was LOW when sampled at the clock edge”
State changes occur synchronously on the rising clock edge
Note: This is a Moore machine because the output depends only on which state we’re in, not on the input value.
State Table¶
State diagrams are visual, but state tables provide a complete, unambiguous specification that maps directly to hardware.
The same FSM can be represented as a table showing all possible transitions:
| Current State | Input (button) | Output (LED) | Next State |
|---|---|---|---|
| OFF | 0 | 0 | OFF |
| OFF | 1 | 0 | ON |
| ON | 0 | 1 | ON |
| ON | 1 | 1 | OFF |
How to read this table (Moore machine):
Output column: Shows the LED value while in the current state (before the clock edge)
Next State column: Shows where we go after the clock edge based on the input
Example walkthrough:
Row 1: In state OFF (LED=0). Input button=0. LED outputs 0, stay in OFF.
Row 2: In state OFF (LED=0). Input button=1. LED outputs 0, transition to ON.
After clock edge: Now in ON state → LED becomes 1
Row 3: In state ON (LED=1). Input button=0. LED outputs 1, stay in ON.
Row 4: In state ON (LED=1). Input button=1. LED outputs 1, transition to OFF.
After clock edge: Now in OFF state → LED becomes 0
Key insight: The output shown is what the current state produces. After the clock edge, you enter the next state and its output becomes active.
Moore vs Mealy Machines¶
There are two fundamental types of FSMs, differing in how outputs are determined:
| Aspect | Moore | Mealy |
|---|---|---|
| Output depends on | Current state only | Current state AND inputs |
| Output associated with | States (inside circles) | Transitions (on arrows) |
| States needed | More states may be needed | Fewer states possible |
| Timing complexity | Simpler | More complex |
| Output changes | Only on clock edges (synchronous) | Can change with inputs (asynchronous) |
| Notation | State/Output | Input/Output on arrows |
| Typical use cases | Controllers, most general-purpose FSMs, CPU control units | Sequence detectors, protocol handlers, designs requiring minimal states |
| Adoption in practice | Most common — easier to design, debug, and verify; preferred for synthesis tools | Used when state minimization is critical or faster response needed |
Why Moore is more common:
Outputs are glitch-free — they only change on clock edges, avoiding hazards
Easier timing analysis — output timing is predictable and synchronous
Simpler verification — outputs depend only on state, not input combinations
Better for synthesis — HDL tools optimize Moore machines more reliably
When to use Mealy:
Need one clock cycle faster response (output changes immediately with input)
State count matters (limited flip-flops, or very complex state space)
Designing protocol interfaces where fast acknowledgment is critical
Example: Sequence Detector¶
Let’s visualize both for the same problem: detecting when input X has been 1 for two consecutive clock cycles. Output Z goes HIGH when we’ve seen two 1s in a row.
Color convention: Blue = pattern not found yet (Z=0), Green = two consecutive 1s detected (Z=1)
Key observation from this example:
Notice how the Mealy machine achieves the same detection with only 2 states compared to Moore’s 3 states. This is because Mealy’s output depends on both state and input, allowing more information to be encoded per state. However, this comes at the cost of more complex timing analysis since outputs can change asynchronously with inputs.
State Tables¶
Now let’s look at the formal state table representations for both Moore and Mealy versions of our sequence detector.
Moore Machine State Table¶
For the sequence detector Moore machine:
| Current State | Input (X) | Output (Z) | Next State |
|---|---|---|---|
| S0 | 0 | 0 | S0 |
| S0 | 1 | 0 | S1 |
| S1 | 0 | 0 | S0 |
| S1 | 1 | 0 | S2 |
| S2 | 0 | 1 | S0 |
| S2 | 1 | 1 | S2 |
This table follows the Moore machine format: outputs depend only on the current state, and state transitions occur on clock edges.
Mealy Machine State Table¶
For the Mealy version:
| Current State | X=0 (Next/Out) | X=1 (Next/Out) |
|---|---|---|
| S0 | S0/0 | S1/0 |
| S1 | S0/0 | S1/1 |
How to read this table (Mealy machine):
Notation: “NextState/Output” shows both next state and output for that input
Output timing: Output is produced combinationally (changes as soon as input changes, independent of the clock)
Key difference: Output can change before the clock edge when input changes (asynchronous to state transitions)
Example: Row 2, column “X=1” shows “S1/1” means: currently in S1 with input X=1, output Z=1 is produced combinationally (immediately when X becomes 1, not waiting for a clock edge), and we’ll stay in S1 after the next clock edge.
Moore vs Mealy timing:
Moore: Output changes only on clock edges (when state changes) — synchronous
Mealy: Output changes immediately when input changes (combinational logic) — can be asynchronous to clock
Implementing the Sequence Detector in Hardware¶
Now let’s walk through how to translate our sequence detector state diagram into actual digital hardware. This involves two main steps:
State Encoding — Assign binary codes to each state
Derive Boolean Equations — Use Karnaugh maps to find minimal logic expressions
We’ll use the 3-state Moore machine from the sequence detector example above.
Step 1: State Encoding¶
To implement an FSM in hardware, we need to assign binary codes to states. The number of flip-flops required is:
For our 3-state Moore machine: flip-flops.
Why? 2 flip-flops can represent distinct states, which is the minimum needed for 3 states. With only 1 flip-flop we’d have states (insufficient). The 4th state (binary 11) will be unused in our design—we call this a “don’t care” state.
Common Encoding Schemes¶
Choosing an encoding:
Binary: Minimizes flip-flops; good for resource-constrained designs
Gray code: Adjacent states differ by one bit; reduces glitches in outputs
One-hot: One flip-flop per state; simplifies next-state logic, common in FPGAs
For our sequence detector, we’ll use binary encoding:
| State | Q₁ | Q₀ |
|---|---|---|
| S0 | 0 | 0 |
| S1 | 0 | 1 |
| S2 | 1 | 0 |
Encoded State Table¶
Now we create the complete state table with binary-encoded states. This table shows:
Current state encoding (Q₁, Q₀)
Input (X)
Output (Z) — for Moore machines, depends only on current state
Next state encoding (D₁, D₀) — the values we need to feed to the flip-flops
| Q₁ | Q₀ | X | Z | D₁ | D₀ | Notes |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | S0, X=0 → stay S0 |
| 0 | 0 | 1 | 0 | 0 | 1 | S0, X=1 → go to S1 |
| 0 | 1 | 0 | 0 | 0 | 0 | S1, X=0 → back to S0 |
| 0 | 1 | 1 | 0 | 1 | 0 | S1, X=1 → go to S2 |
| 1 | 0 | 0 | 1 | 0 | 0 | S2, X=0 → back to S0 |
| 1 | 0 | 1 | 1 | 1 | 0 | S2, X=1 → stay S2 |
| 1 | 1 | - | - | - | - | Unused state (don’t care) |
Key columns:
D₁, D₀: The next-state values — these become the K-map outputs
Z: Output is 1 only in state S2 (Q₁=1, Q₀=0) — Moore machine property
Step 2: Derive Boolean Equations Using Karnaugh Maps¶
We use Karnaugh maps to derive minimal Boolean expressions for D1, D0, and Z from the encoded state table above (Step 1).
Understanding K-Map Structure¶
For each output (D1, D0, or Z), we create a K-map with:
Rows: Q1Q0 combinations in Gray code order (00, 01, 11, 10) - adjacent rows differ by only one bit
Columns: X values (0, 1)
Let’s see how to fill these K-maps from the state table:
Filling the K-Map for D1 (Next Q1)¶
To fill the D1 K-map, we transfer each value from the D1 column in the encoded state table to the corresponding K-map cell:
Process:
For each row in the state table, find its K-map location using Q1Q0 (row) and X (column)
Copy the D1 value (0, 1, or -) to that K-map cell
Repeat for all rows
The rows where D1=1 are highlighted because these 1s are what we’ll group for Boolean minimization.
Filling the K-Map for D0 (Next Q0)¶
To fill the D0 K-map, we look at the “D0 (Next Q0)” column in the encoded state table.
Example: Row with Q1=0, Q0=0, X=1 → D0=1, so K-map cell at row “00”, column “1” contains 1
Notice that only one cell has D0=1. Most cells are 0.
Filling the K-Map for Z (Output)¶
To fill the Z K-map, we look at the “Z” column in the encoded state table.
Key insight for Moore machines: Z depends only on the current state (Q1, Q0), NOT on input X.
Example: Rows with Q1=1, Q0=0 → Z=1, so K-map cells at row “10” contain 1 for BOTH X=0 and X=1
This is why the entire row has the same value!
Grouping Adjacent 1s for Minimization¶
Once all K-maps are filled, we look for rectangular groupings of adjacent 1s. These groupings help us derive minimal Boolean expressions.
Grouping rules:
Group 1s in powers of 2 (1, 2, 4, 8 cells)
Groups can be rectangular (rows, columns, or blocks)
Larger groups = simpler equations
Don’t cares (‘-’) can be included in groups to make them larger
Let’s see the complete K-maps with groupings:
Final Boolean Equations:
These equations can be implemented with AND, OR, and NOT gates to create the next-state and output logic for our FSM.
The General FSM Architecture¶
Every synchronous FSM follows this pattern:
State Register: Flip-flops store the current state (Q values)
Next-State Logic: Combinational circuit computing D inputs from current state and inputs
Output Logic: Combinational circuit computing outputs
Moore: Output depends only on Q (current state)
Mealy: Output depends on Q and inputs
Clock: Synchronizes state transitions
Complete Example: Traffic Light Controller¶
Let’s design a simplified traffic light controller for a single intersection. The light cycles through:
Green (30 time units)
Yellow (5 time units)
Red (30 time units)
For simplicity, we’ll use a timer_done input signal that goes high when the current phase should end.
State Table for Traffic Light Controller (Binary Encoding)¶
We’ll use binary encoding for the three states:
| State | Q1 Q0 |
|---|---|
| GREEN | 0 0 |
| YELLOW | 0 1 |
| RED | 1 0 |
Why binary encoding? For 3 states, we need only ⌈log₂(3)⌉ = 2 flip-flops. Binary encoding is efficient for this small state space.
The complete state table with encoded states:
| Current State | Q1 Q0 | timer_done | R | Y | G | Next State | D1 D0 |
|---|---|---|---|---|---|---|---|
| GREEN | 0 0 | 0 | 0 | 0 | 1 | GREEN | 0 0 |
| GREEN | 0 0 | 1 | 0 | 0 | 1 | YELLOW | 0 1 |
| YELLOW | 0 1 | 0 | 0 | 1 | 0 | YELLOW | 0 1 |
| YELLOW | 0 1 | 1 | 0 | 1 | 0 | RED | 1 0 |
| RED | 1 0 | 0 | 1 | 0 | 0 | RED | 1 0 |
| RED | 1 0 | 1 | 1 | 0 | 0 | GREEN | 0 0 |
How to read this table:
Current State & Q1,Q0: The state we’re in and its binary encoding
timer_done: Input signal (1 = time to advance to next light)
R, Y, G: Output signals while in the current state (Moore machine - depends only on current state encoding)
Next State & D1,D0: Where we transition after the clock edge, and the flip-flop input values needed
Note: This reads naturally left-to-right: “In [current state] with [input], outputs are [R,Y,G], then transition to [next state]”
Boolean Equations¶
From the state table:
where = timer_done.
Timing Analysis¶
Understanding FSM timing is critical for correct operation. Let’s trace through our sequence detector.
About the input sequence: The X pattern [0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0] was carefully chosen to demonstrate:
First detection (cycles 1-3): Two consecutive 1s → S0→S1→S2, Z becomes 1
Reset mid-sequence (cycle 3-4): X=0 breaks the streak → back to S0
Second detection (cycles 4-7): Three consecutive 1s → detects at cycle 6, stays in S2 for cycle 7
Double reset (cycles 7-8): Two 0s in a row → confirms we’re back at S0
Third detection (cycles 9-11): Another pair of 1s → detection at cycle 11
Why this pattern is instructive:
Shows multiple detections in one trace
Demonstrates that Z=1 persists while X=1 continues (cycles 6-7)
Shows immediate reset to S0 on X=0 from any state
The 0s at different points show the FSM properly restarting
Reading the timing diagram:
State changes occur at the rising edge of the clock
The new state depends on the current state and input at the clock edge
In a Moore machine, output Z changes only when the state changes
Detection (Z=1) occurs when we reach state S2, which happens after seeing two consecutive 1s
Common FSM Design Patterns¶
Pattern 1: Sequence Detector¶
Detects a specific bit pattern in a stream. We built one above.
Pattern 2: Counter¶
Cycles through states in order. Each state represents a count value.
Pattern 3: Controller¶
Orchestrates multi-step operations. States represent phases of an operation (like the traffic light).
Pattern 4: Arbiter¶
Manages access to shared resources. States track which requester has access.
Key Takeaways¶
FSMs are the foundation of sequential digital design — nearly every digital system contains FSMs
Moore vs Mealy is a fundamental design choice:
Moore: Simpler timing, outputs change only on clock edges
Mealy: Potentially fewer states, faster response to inputs
State encoding affects implementation complexity:
Binary minimizes flip-flops
One-hot simplifies combinational logic
The general architecture is always:
State register (flip-flops)
Next-state combinational logic
Output combinational logic
Clock: Synchronizes state transitions
Design process:
Draw state diagram from requirements
Create state table
Choose state encoding
Derive Boolean equations
Implement with gates and flip-flops (or HDL)
FSMs are everywhere in digital design — from simple button debouncers to complex CPU control units. Master them, and you have a powerful tool for any sequential logic problem.