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.
<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.
States: IDLE, REQ, GNT, XFER, DONE. Five states need ⌈log₂5⌉ = 3 bits for binary; 5 bits for one-hot.
| State | Binary | One-hot | Gray |
|---|---|---|---|
| IDLE | 3'b000 | 5'b00001 | 3'b000 |
| REQ | 3'b001 | 5'b00010 | 3'b001 |
| GNT | 3'b010 | 5'b00100 | 3'b011 |
| XFER | 3'b011 | 5'b01000 | 3'b010 |
| DONE | 3'b100 | 5'b10000 | 3'b110 |
| Unused codes | 3 (101,110,111) | 27 (any multi-hot / all-zero) | 3 (101,111,100) |
| Encoding | State FF count | Next-state logic | Max Fmax tendency | Glitch risk on decode |
|---|---|---|---|---|
| Binary | ⌈log₂N⌉ — smallest | Wide input case statements | Moderate | Moderate (multi-bit change) |
| One-hot | N — largest | Simple: each bit = one transition | Often highest | Low per-bit (1-bit flip) |
| Gray | ⌈log₂N⌉ | Must preserve Gray adjacency | Moderate | Lowest (1-bit between neighbors) |
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.
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.
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.
// 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
// 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
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.
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: one fat decoder feeds next-state. One-hot: N shallow cones — often wins on critical-path Fmax.
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 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
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.
// 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
Many small FSMs → binary saves FF count across the chip.
Single hot-path FSM → one-hot; run synthesis compare.
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.
auto first, then force encoding if QoR gap is clear