RTL Design / Module 6: FSMs & Control
Lesson 6.4

Safe FSM design — illegal states & recovery

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.

Safe FSM

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.

1

Where illegal states come from

CauseExampleBinary 3-bit (5 states)One-hot 5-bit
Unused encodingsOnly 5 of 8 codes used101, 110, 11127 invalid patterns
SEU / soft errorCosmic ray flips FFAny codeMulti-hot (e.g. 00110)
Reset failureAsync reset deassert raceMetastable garbageAll-zero
Power-upFFs random before resetRandom 3-bitRandom 5-bit
Illegal state recovery diagram

The default branch is your safety net — not optional in production FSMs.

2

The golden pattern — default next = IDLE

safe_fsm.v
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
Trap — wrong default

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.

3

One-hot illegal state recovery

onehot_safe.v
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.

4

Safe outputs during illegal states

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.

safe_outputs.v
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;
Trap

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.

5

Watchdog — belt-and-suspenders for safety-critical FSMs

When recovery to IDLE is not enough (automotive, medical), a watchdog counter forces reset if the FSM stays out of IDLE too long.

FSM watchdog timer
fsm_watchdog.v
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;
6

X-optimism — simulation lies to you

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.

ScenarioOptimistic simReal silicon
State = X at time 0May behave as IDLERandom legal/illegal bit pattern
Illegal code with no defaultMay appear to "work" if X→0FSM stuck or wrong outputs forever
Reset deassert raceMay not show metastabilityOccasional illegal entry
x_pessimism_tb.v
// 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.

7

Sync reset + illegal state — belt and suspenders

sync_reset_safe.v
// 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
8

Safe FSM design checklist

  • default: next = IDLE in every next-state case — default is safe state, not hold
  • Reset forces a named legal state (IDLE), not raw 0 unless IDLE=0 is documented
  • All unused encodings listed in header comment
  • One-hot: detect multi-hot and zero-hot explicitly if tool doesn't
  • TB injects illegal state and verifies recovery
  • Outputs in illegal state are safe (Moore: IDLE outputs; or explicit !legal qualifier)
  • No X-optimism assumptions — run with pessimistic X-prop if available
  • Consider watchdog timer for safety-critical FSMs (automotive, medical)
▶ Test illegal-state recovery

🎯 What to remember

  • Illegal states are inevitable — plan recovery, don't hope they never happen.
  • default: next = IDLE is the single most important FSM line in production RTL.
  • Never default next = state unless you have a documented reason.
  • Sim may lie (X-optimism) — force illegal states in TB to prove recovery.
Advertisement