State Encoding
A digital designer specifies state machines using symbolic identifiers, such as IDLE, FETCH, and EXECUTE. During synthesis, the compiler assigns each state identifier a unique binary bit pattern stored in flip-flops.
The choice of state assignment affects circuit performance. The assignment determines Boolean equations for next-state logic, total flip-flop count, combinational gate depth, and dynamic power dissipation.
Digital designers evaluate four primary state encoding schemes.
Binary Encoding
Binary encoding assigns states sequential binary integers ($0, 1, 2, \dots$). An $N$-state machine requires the minimum number of flip-flops:
$$n = \lceil \log_2 N \rceil$$
For a four-state machine:
IDLE = 2'b00FETCH = 2'b01DECODE = 2'b10EXECUTE = 2'b11
Binary encoding minimizes register flip-flop count. In Application-Specific Integrated Circuits (ASICs), where every flip-flop consumes silicon area, binary encoding saves area.
However, state transitions frequently require multiple bits to switch simultaneously (for example, 01 to 10 transitions both bits). Complex next-state combinational decoders can reduce maximum clock frequency.
One-Hot Encoding
One-hot encoding assigns one dedicated flip-flop to each state. In an $N$-state machine, exactly one flip-flop holds logic 1 at any time.
For a four-state machine:
IDLE = 4'b0001FETCH = 4'b0010DECODE = 4'b0100EXECUTE = 4'b1000
The next-state input equation for state flip-flop $S_i$ is simply the logical OR of all incoming transition conditions. For example, if state S0 and state S2 transition to S3 when $X = 1$:
$$D_{S3} = (Q_{S0} \cdot X) + (Q_{S2} \cdot X)$$
The next-state logic requires only basic AND-OR gates.
FPGA synthesis tools default to one-hot encoding. FPGAs contain abundant flip-flops, and shallow next-state logic fits directly into single Look-Up Tables (LUTs), maximizing clock frequency.
However, only $N$ out of $2^N$ binary combinations are legal. The circuit requires robust reset initialization to prevent invalid state traps.
Gray Code Encoding
Gray code encoding ensures that adjacent sequential states differ by exactly one binary bit:
For a four-state sequential machine:
IDLE = 2'b00FETCH = 2'b01DECODE = 2'b11EXECUTE = 2'b10
Because only one bit switches per state transition, Gray code minimizes dynamic switching power ($P_{\text{dyn}} \propto \alpha C V^2 f$).
Gray code is also essential for multi-bit clock domain crossings (such as FIFO read and write pointers) to prevent bus synchronization hazards. Gray code benefits sequential machines that progress through states in linear order.
Output-Coded Encoding
Output-coded encoding assigns state bits so that specific state register outputs serve directly as circuit outputs.
Consider a Moore machine with two control outputs ($Z_1, Z_0$):
| State | State Register ($Q_1 Q_0$) | Outputs ($Z_1 Z_0$) |
|---|---|---|
| IDLE | 00 | 00 |
| LOAD | 01 | 01 |
| RUN | 10 | 10 |
| DONE | 11 | 11 |
Output signals connect directly to flip-flop outputs without combinational logic gates.
This encoding eliminates output propagation delays and saves gate area. However, it requires unique output combinations for each state.
Comparison of the Four Schemes
Consider a four-state cyclic counter (IDLE $\to$ ACTIVE $\to$ HOLD $\to$ DONE $\to$ IDLE) with output done asserted only in state DONE:
- Binary (2 Flip-Flops):
- Next-state logic: $D_1 = Q_0$, $D_0 = \overline{Q_1}$
- Output logic: $\text{done} = Q_1 \cdot Q_0$
- One-Hot (4 Flip-Flops):
- Next-state logic: $D_{\text{ACTIVE}} = Q_{\text{IDLE}}$, $D_{\text{HOLD}} = Q_{\text{ACTIVE}}$, $D_{\text{DONE}} = Q_{\text{HOLD}}$, $D_{\text{IDLE}} = Q_{\text{DONE}}$
- Output logic: $\text{done} = Q_{\text{DONE}}$ (direct wire connection)
- Gray Code (2 Flip-Flops):
- Next-state logic: Evaluated with single-bit transitions
- Output logic: $\text{done} = Q_1 \cdot \overline{Q_0}$
- Output-Coded (3 Flip-Flops):
- Assign
DONEto3'b100and other states to000,001,010. - Output logic: $\text{done} = Q_2$ (direct wire connection)
- Assign
| Metric | Binary | One-Hot | Gray Code | Output-Coded |
|---|---|---|---|---|
| Flip-Flops for $N$ States | $\lceil \log_2 N \rceil$ | $N$ | $\lceil \log_2 N \rceil$ | Variable ($\ge \lceil \log_2 N \rceil$) |
| Next-State Combinational Logic | Moderate to high | Minimal | Moderate | Moderate |
| Output Combinational Logic | Full decoder logic | Simple OR gates or wires | Full decoder logic | Direct wire connections |
| Optimal Target Platform | ASIC area-critical paths | High-speed FPGAs | Low-power sequential systems | Output-delay critical paths |
Synthesis Tool State Assignment
EDA synthesis tools analyze the target architecture and select an appropriate encoding scheme automatically:
- For FPGA targets, tools default to one-hot encoding.
- For ASIC targets, tools frequently select binary or minimal-area encodings.
Designers can specify state encodings explicitly using synthesis attributes (such as (* fsm_encoding = "one_hot" *) in Vivado) or by defining explicit numerical values in SystemVerilog enum types.
Practice Exercises
Problem 1
A state machine contains 12 states. Calculate the number of flip-flops required for binary encoding and one-hot encoding. Explain why one-hot encoding can achieve higher operating frequency in FPGAs.
Solution
Calculate register requirements:
- Binary encoding: $n = \lceil \log_2 12 \rceil = 4$ flip-flops.
- One-hot encoding: $n = 12$ flip-flops.
In an FPGA, flip-flops are distributed in every logic slice. One-hot next-state equations require only simple wide OR gates. These equations fit into single Look-Up Table (LUT) levels, eliminating cascading logic delays and increasing maximum operating frequency.
Problem 2
Assign a 3-bit Gray code sequence to these five sequential states: RESET, WAIT, LOAD, COMPUTE, and STORE. Verify that only one bit transitions between consecutive states.
Solution
Assign a valid Gray code sequence:
RESET:3'b000WAIT:3'b001(bit 0 transitions)LOAD:3'b011(bit 1 transitions)COMPUTE:3'b010(bit 0 transitions)STORE:3'b110(bit 2 transitions)
Each transition changes exactly one binary bit, reducing dynamic switching power.
Problem 3
An 8-state state machine exhibits a critical path propagation delay of $8.5\text{ ns}$ when using binary encoding. When synthesized with one-hot encoding, the critical path delay drops to $5.2\text{ ns}$. Calculate the maximum clock frequency for both implementations.
Solution
Calculate maximum frequencies ($f_{\text{max}} = 1 / t_{\text{pd}}$):
- Binary implementation:
$$f_{\text{max}} = \frac{1}{8.5\text{ ns}} \approx 117.6\text{ MHz}$$
- One-hot implementation:
$$f_{\text{max}} = \frac{1}{5.2\text{ ns}} \approx 192.3\text{ MHz}$$
One-hot encoding increases maximum operating frequency by approximately 63 percent due to simplified next-state logic.
The next article establishes the systematic eight-step design flow for finite state machines.