An FSM with N states needs one code per state. RTL simulation exposes the encoding through state bits, widths, probes and fault injection. Only the correctly abstracted interface behavior during legal operation is intended to be encoding-independent. Encoding affects state-register cost, decode structure and fault behavior; mapped timing and recovery still require implementation evidence.
Encoding | Flops for 8 states | Idea |
|---|---|---|
Binary | 3 | Smallest register; decode logic is deeper |
One-hot | 8 | One flop per state; decode is a single bit; more flops, shallow logic |
Gray | 3 | Adjacent ring steps differ in one bit; abort/reset jumps need a separate audit |
Johnson (twisted ring) | 4 | A shifting pattern of ones; each state decodes from two adjacent bits |
4 lessons, about 17 min
Binary: ⌈log₂ 20⌉ = 5 flip-flops. One-hot: 20 flip-flops.
The whole topic on one page, taken from each lesson's Remember box. Read it before an interview, or print it.
Encodings Compared
Binary is the smallest code, one-hot is the easiest to decode, and the Gray and Johnson rings change one bit per step.
Aborts, resets and truncated rings break the one-bit rule, so audit every transition.
Do not use a raw Gray FSM state for CDC. Transfer a stable snapshot with a handshake.
Rule of thumb: one bit per step only helps if every transition, including abort and reset, obeys it.
Area and Speed Trade-off
Generic cell counts are not silicon area or delay, and they name no speed winner.
One-hot can give shallow next-state logic when each state has few incoming transitions, and a single-state output is just one bit.
Compare state-to-state and state-to-output paths on the real flow, with the same constraints for each encoding.
Rule of thumb: pick the encoding from the implemented timing report, not from the FSM size.
Illegal States and Recovery
An enum or a default branch specifies RTL behaviour. It does not promise mapped fault recovery.
Returning to a legal state does not undo a write or request already sent, so suppress side effects while the state is invalid.
A legal-but-wrong state (S0 corrupted into S5) looks valid to an illegal-code detector.
Rule of thumb: state the fault model first, then build the detector and the recovery to match it.
Choosing an Encoding
Size, platform and safety needs suggest candidates. Mapped resource and timing results, and the fault contract, decide.
Use named enum values everywhere, and never decode a state bit outside the FSM.
Read the synthesis log: tools report re-encoded FSMs, so check that the result matches the intent.
Rule of thumb: binary for small area-driven FSMs, one-hot for larger speed-driven ones, and check the log either way.
Mini project: an SPI master, controller and datapath
Practice question: One-hot versus binary encoding: trade-offs and pitfalls
Practice question: Gray encoding for low power and CDC safety
Code lab: Build a one-hot control FSM
Practice question: FSM safe-state recovery with illegal state handling
Interview: run a mock interview on this topic