RTL Design / Module 6: FSMs & Control
Lesson 6.3

State encoding — binary vs one-hot vs Gray

Your FSM's state register bit pattern is a synthesis knob. Binary minimizes flip-flops. One-hot simplifies next-state logic and often runs faster. Gray minimizes bit transitions — critical when state crosses clock domains or feeds combo output decoders that must not glitch.

Rule of thumb

<8 states → binary is fine. High-speed or FPGA control FSMs → try one-hot. Counters/pointers crossing domains → Gray. Let synthesis explore, but guide it with encoding pragmas when you know the bottleneck.

Encoding is not cosmetic — it changes how many flip-flops you pay for, how wide your next-state cone is, and whether a glitch on a state bus can corrupt downstream logic. Teams document encoding choice in the module header because re-encoding after tape-in is a full regression event.

1

Encoding comparison — 5-state example

States: IDLE, REQ, GNT, XFER, DONE. Five states need ⌈log₂5⌉ = 3 bits for binary; 5 bits for one-hot.

StateBinaryOne-hotGray
IDLE3'b0005'b000013'b000
REQ3'b0015'b000103'b001
GNT3'b0105'b001003'b011
XFER3'b0115'b010003'b010
DONE3'b1005'b100003'b110
Unused codes3 (101,110,111)27 (any multi-hot / all-zero)3 (101,111,100)
EncodingState FF countNext-state logicMax Fmax tendencyGlitch risk on decode
Binary⌈log₂N⌉ — smallestWide input case statementsModerateModerate (multi-bit change)
One-hotN — largestSimple: each bit = one transitionOften highestLow per-bit (1-bit flip)
Gray⌈log₂N⌉Must preserve Gray adjacencyModerateLowest (1-bit between neighbors)
2

Why one-hot is fast — transition diagram

In one-hot encoding each state owns one flip-flop. The next-state logic for bit k answers: "should state k be active next cycle?" — a shallow AND-OR instead of a wide binary decoder feeding a fat case statement.

One-hot state bit transitions

Each state is one flip-flop. Next-state for bit k is a simple function of current one-hot bits + inputs.

FPGA note: Xilinx/Intel FPGAs often default small FSMs to one-hot because LUTs are plentiful and routing is fast. ASIC flows with tight FF budgets may force binary unless timing reports show a hot path.

3

Gray encoding — adjacent states differ by 1 bit

Gray code ring

Gray: each step flips exactly one bit. Used in async FIFO pointers and linear/cyclic state rings.

FSM note: Gray encoding for arbitrary FSM graphs is non-trivial — states in your diagram may not be physically adjacent. Gray works naturally for linear/cyclic counters; for general FSMs, binary or one-hot is more common.

4

Verilog — binary encoding

binary_fsm.v
// Binary encoding — fewest state flip-flops
localparam IDLE = 3'd0;
localparam REQ  = 3'd1;
localparam GNT  = 3'd2;
localparam XFER = 3'd3;
localparam DONE = 3'd4;

reg [2:0] state, next;   // 3 FFs for 5 states

always @(posedge clk or negedge rst_n)
  if (!rst_n) state <= IDLE;
  else        state <= next;

always @(*) begin
  next = state;
  case (state)
    IDLE: if (start)    next = REQ;
    REQ:  if (granted)  next = GNT;
    GNT:  if (xfer_ok)  next = XFER;
    XFER:               next = DONE;
    DONE:               next = IDLE;
    default:             next = IDLE;  // illegal → safe
  endcase
end
5

Verilog — one-hot encoding

onehot_fsm.v
// One-hot encoding — N flip-flops, fast next-state
localparam IDLE = 5'b00001;
localparam REQ  = 5'b00010;
localparam GNT  = 5'b00100;
localparam XFER = 5'b01000;
localparam DONE = 5'b10000;

reg [4:0] state, next;   // 5 FFs for 5 states

always @(*) begin
  next = 5'b00001;  // default IDLE (safe)
  case (1'b1)        // one-hot case style
    state[0]: if (start)    next = REQ;
    state[1]: if (granted)  next = GNT;
    state[2]: if (xfer_ok)  next = XFER;
    state[3]:               next = DONE;
    state[4]:               next = IDLE;
    default:                next = IDLE;
  endcase
end
Trap

One-hot requires default: next = IDLE — if state becomes multi-hot (e.g. 5'b00110) or all-zero, without recovery the FSM is dead. Lesson 6.4 covers safe recovery.

6

Binary decode fan-in — why wide case statements cost timing

With binary encoding, the next-state block must decode the current state vector through a wide multiplexer tree. More states → deeper logic cone → harder Fmax. One-hot sidesteps the decoder: each state bit's next value is a local function.

Binary vs one-hot decode fan-in

Binary: one fat decoder feeds next-state. One-hot: N shallow cones — often wins on critical-path Fmax.

7

Verilog — Gray-encoded linear FSM

For a ring/cyclic FSM (not arbitrary graphs), Gray encoding preserves single-bit transitions. You must assign Gray codes manually and verify adjacency in your state diagram.

gray_fsm.v
// Gray encoding — adjacent states differ by 1 bit
localparam IDLE = 3'b000;
localparam REQ  = 3'b001;
localparam GNT  = 3'b011;
localparam XFER = 3'b010;
localparam DONE = 3'b110;

reg [2:0] state, next;

always @(*) begin
  next = IDLE;  // safe default
  case (state)
    IDLE: if (start)    next = REQ;
    REQ:  if (granted)  next = GNT;
    GNT:  if (xfer_ok)  next = XFER;
    XFER:               next = DONE;
    DONE:               next = IDLE;  // 110→000: 2 bits flip — not ideal Gray wrap
    default:             next = IDLE;
  endcase
end
Trap

Gray for arbitrary FSM graphs requires Hamiltonian-path analysis — if your diagram has branches, Gray adjacency may be impossible without adding dummy states. Don't force Gray on messy control graphs.

8

Synthesis encoding hints

encoding_pragmas.v
// Synopsys / common style (check your tool docs)
(* fsm_encoding = "one_hot" *) reg [4:0] state;
// Alternatives: "sequential", "gray", "auto"

// Xilinx: attribute syn_encoding
// Intel: safe_state_machine on

// General RTL rule: use localparam names, not raw vectors,
// so you can change encoding in one place without touching case logic
📉

Area-critical

Many small FSMs → binary saves FF count across the chip.

⚡

Speed-critical

Single hot-path FSM → one-hot; run synthesis compare.

🔀

CDC / bus

State exported across clocks → Gray or send one-hot + sync.

Synthesis workflow: Run with fsm_encoding = "auto" first. Compare area/timing reports. If one FSM dominates critical path, re-run with one_hot or sequential (binary) and document the delta in the module header.

▶ Test encodings in simulator
9

Encoding selection checklist

  • Count states N; calculate binary bits vs one-hot bits
  • Run synthesis with auto first, then force encoding if QoR gap is clear
  • One-hot: always include illegal-state recovery (default → IDLE)
  • Binary: full case + default; document unused codes
  • Gray: only when transition graph is linear/ring or for pointer-style counters
  • Never change encoding without re-running full regression
  • Document encoding choice in module header comment

🎯 What to remember

  • Binary = fewest FFs, wider decode. One-hot = more FFs, simpler/faster next-state.
  • Gray = 1-bit transitions — great for counters and CDC pointers, harder for arbitrary graphs.
  • Unused codes are inevitable — plan recovery (next lesson).
  • Let synthesis experiment, but override when you know the bottleneck.
Advertisement