Encoding and Safe State Machines
A state machine with five distinct states requires three binary state bits. Three bits define eight possible binary combinations. Therefore, three values represent unused states. If a hardware glitch forces the register into an unassigned state, the circuit must recover deterministically.
State Encoding Architectures
Binary Encoding
Binary encoding uses $\lceil \log_2 N \rceil$ flip-flops for $N$ states:
localparam [1:0] RED = 2'b00,
GREEN = 2'b01,
YELLOW = 2'b10,
PED = 2'b11;
reg [1:0] state, next_state;
Binary encoding minimizes flip-flop count. It requires multi-level combinational decoding, which is standard for ASIC standard cell architectures.
One-Hot Encoding
One-hot encoding allocates one dedicated flip-flop per state:
localparam [3:0] RED = 4'b0001,
GREEN = 4'b0010,
YELLOW = 4'b0100,
PED = 4'b1000;
reg [3:0] state, next_state;
Detecting an active state requires reading only a single bit (such as state[1]). On FPGA architectures with abundant register resources, one-hot encodings improve maximum operating frequency.
Gray Code Encoding
Gray code transitions change only one bit per step:
localparam [1:0] RED = 2'b00,
GREEN = 2'b01,
YELLOW = 2'b11,
PED = 2'b10;
Gray encoding minimizes switching activity in sequential pipelines. It provides limited benefit when machines execute complex branching.
You can specify encoding attributes directly in synthesis tools:
(* fsm_encoding = "one_hot" *) reg [3:0] state, next_state;
Handling Unused and Illegal States
A binary state machine with $N$ states contains unused states defined by:
$$2^{\lceil \log_2 N \rceil} - N$$
For example, a five-state machine contains three unused states. Power-supply noise, clock jitter, or radiation-induced particle strikes can flip register bits into illegal states. Aerospace and automotive standards (such as ISO 26262) mandate robust recovery mechanisms.
If a case statement omits a default branch, an illegal state retains its invalid value indefinitely, causing hardware deadlock.
Implementing Safe Recovery
Include an explicit default branch in the next-state evaluation logic:
always @(*) begin
next_state = state;
case (state)
IDLE: if (start) next_state = ACTIVE;
ACTIVE: if (done) next_state = DONE;
DONE: next_state = IDLE;
default: next_state = IDLE; // Recovers to safe state
endcase
end
If an electrical glitch forces an illegal bit pattern, the next active clock edge transitions the machine safely to IDLE. In one-hot state machines, illegal states include all patterns that do not contain exactly one active bit.
Mission-critical systems use formal verification tools to prove that state machines recover from every possible register encoding without lockup.
Practice Exercise
Analyze a three-state machine using binary encoding. Identify the unused state, add recovery logic, and verify recovery in simulation using Verilog force statements.
Solution
Three states require two bits ($2^2 = 4$). The value 2'b11 represents an unused state.
localparam [1:0] IDLE = 2'b00, RUN = 2'b01, DONE = 2'b10;
always @(*) begin
next_state = state;
case (state)
IDLE: if (go) next_state = RUN;
RUN: if (stop) next_state = DONE;
DONE: next_state = IDLE;
default: next_state = IDLE; // Recovers from 2'b11
endcase
end
In the verification testbench:
- Apply
force dut.state = 2'b11;. - Wait for 10 ns.
- Apply
release dut.state;. - Trigger one clock edge.
The simulation waveform confirms that dut.state transitions to IDLE on the subsequent clock edge.
The final article in Module 5 presents complete end-to-end worked examples of synthesizable finite state machines.