Real chips don't stay in legal states forever. SEU glitches, reset races, and power-up garbage can land your FSM in an illegal encoding. Safe FSM design means every unreachable code has a defined escape path — usually default: next = IDLE — and you understand how simulators vs silicon treat X values.
An FSM where every bit pattern in the state register either is a legal state or transitions to a known safe state within one cycle — without depending on external stimulus.
Production chips assume FSMs will wander. Cosmic rays, brownouts, and reset races are not edge cases — they are operating conditions. Safe FSM design is the difference between a block that recovers silently and one that needs a power cycle.
| Cause | Example | Binary 3-bit (5 states) | One-hot 5-bit |
|---|---|---|---|
| Unused encodings | Only 5 of 8 codes used | 101, 110, 111 | 27 invalid patterns |
| SEU / soft error | Cosmic ray flips FF | Any code | Multi-hot (e.g. 00110) |
| Reset failure | Async reset deassert race | Metastable garbage | All-zero |
| Power-up | FFs random before reset | Random 3-bit | Random 5-bit |
The default branch is your safety net — not optional in production FSMs.
module safe_fsm (
input wire clk,
input wire rst_n,
input wire start,
input wire done,
output wire busy
);
localparam IDLE = 2'd0;
localparam WORK = 2'd1;
localparam WAIT = 2'd2;
// 2'd3 is ILLEGAL — must be handled
reg [1:0] state, next;
always @(posedge clk or negedge rst_n)
if (!rst_n) state <= IDLE;
else state <= next;
always @(*) begin
next = IDLE; // ★ safe default — not state!
case (state)
IDLE: if (start) next = WORK;
WORK: if (done) next = WAIT;
WAIT: next = IDLE;
default: next = IDLE; // ★ illegal → recover
endcase
end
assign busy = (state != IDLE);
endmodule
next = state as default means illegal states stick forever. Always default to a safe state (IDLE), not hold. Exception: if you intentionally want to freeze for debug, document it and add an explicit watchdog.
localparam IDLE = 4'b0001;
localparam S1 = 4'b0010;
localparam S2 = 4'b0100;
localparam S3 = 4'b1000;
reg [3:0] state, next;
// Detect illegal: zero-hot or multi-hot
wire state_legal = (state == IDLE) | (state == S1) |
(state == S2) | (state == S3);
always @(*) begin
next = IDLE; // safe default
if (!state_legal)
next = IDLE;
else
case (1'b1)
state[0]: if (go) next = S1;
state[1]: next = S2;
state[2]: next = S3;
state[3]: next = IDLE;
default: next = IDLE;
endcase
end
Synthesis option: Some tools support safe_state or reset_state attributes that automatically add recovery logic. Still write explicit default in RTL — don't rely on the tool silently.
Recovery fixes the state register — but outputs must also be safe while the FSM is illegal. Moore outputs decoded from state may glitch; add an explicit legal qualifier.
wire state_legal = (state == IDLE) | (state == WORK) | (state == WAIT); // Moore outputs gated by legality — idle-safe when corrupted assign busy = state_legal & (state != IDLE); assign irq_pulse = state_legal & (state == WAIT); // Or force all outputs low when illegal: assign busy = state_legal ? (state != IDLE) : 1'b0;
Illegal state with valid-looking outputs can start a DMA transfer or assert an interrupt. Gate every externally visible output with state_legal or decode only from known states.
When recovery to IDLE is not enough (automotive, medical), a watchdog counter forces reset if the FSM stays out of IDLE too long.
localparam TIMEOUT = 1024;
reg [15:0] wdg_ctr;
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
wdg_ctr <= 16'd0;
else if (state == IDLE)
wdg_ctr <= 16'd0;
else if (wdg_ctr == TIMEOUT)
wdg_ctr <= TIMEOUT; // saturate
else
wdg_ctr <= wdg_ctr + 1'b1;
end
wire force_idle = (wdg_ctr >= TIMEOUT);
// In next-state block: if (force_idle) next = IDLE;
In simulation, an uninitialized or X-state register may be "optimistically" resolved to 0 by some tools, masking illegal-state bugs. In silicon, the FF holds a real 0 or 1 — not X.
| Scenario | Optimistic sim | Real silicon |
|---|---|---|
| State = X at time 0 | May behave as IDLE | Random legal/illegal bit pattern |
| Illegal code with no default | May appear to "work" if X→0 | FSM stuck or wrong outputs forever |
| Reset deassert race | May not show metastability | Occasional illegal entry |
// Force illegal state in sim to verify recovery
initial begin
rst_n = 0; #10 rst_n = 1;
force dut.state = 2'b11; // illegal
#1;
release dut.state;
@(posedge clk);
if (dut.state !== dut.IDLE)
$display("FAIL: illegal state did not recover to IDLE");
else
$display("PASS: recovered to IDLE");
end
Verification habit: Every FSM testbench should inject at least one illegal state and confirm recovery within 1–2 cycles. Add this to your FSM review checklist.
// Industry pattern: async assert, sync deassert + explicit reset state
reg [1:0] state, next;
reg rst_sync;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) rst_sync <= 1'b0;
else rst_sync <= 1'b1;
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
state <= IDLE; // known state out of reset
else if (!rst_sync)
state <= IDLE; // hold IDLE during sync release
else
state <= next;
end
default: next = IDLE in every next-state case — default is safe state, not holdIDLE), not raw 0 unless IDLE=0 is documented!legal qualifier)default: next = IDLE is the single most important FSM line in production RTL.next = state unless you have a documented reason.