Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
  • Sequential Design27
  • FSM & Control27
    • Controller and Datapath
    • Moore vs Mealy
    • State Encoding
      • Encodings Compared
      • Area and Speed Trade-off
      • Illegal States and Recovery
      • Choosing an Encoding
    • Multi-Cycle Sequencers
    • UART Transmitter
    • Memory Controller
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹FSM & Control
  1. Topics
  2. RTL Design
  3. FSM & Control
  4. State Encoding

Part 4 · FSM & Control · Intermediate

State Encoding

How FSM states map to flip-flops: binary, Gray, one-hot and Johnson codes, costs and illegal states.

  • 4topics
  • 4lessons

Topics in this section

  1. 1Encodings Compared1 lesson
  2. 2Area and Speed Trade-off1 lesson
  3. 3Illegal States and Recovery1 lesson
  4. 4Choosing an Encoding1 lesson
On this page

    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

    Lessons in this topic

    4 lessons, about 17 min

    1. 1Encodings ComparedYou can write the binary, Gray, one-hot and Johnson codes for eight states and say which ones change one bit per step and which do not.5 min
    2. 2Area and Speed Trade-offYou can compare binary, Gray and one-hot builds of the same FSM, say what generic cell counts do and do not show, and name when one-hot may help timing.4 min
    3. 3Illegal States and RecoveryYou can list the illegal patterns of a one-hot FSM, add detection and recovery to S0, and say what recovery does not undo.5 min
    4. 4Choosing an EncodingYou can choose a first encoding from size, platform and safety needs, and write the FSM so that the encoding can change without touching other code.3 min

    Interview

    Expect: “Compare binary and one-hot encoding”, “Why is one-hot popular on FPGAs?”, and “What is an illegal state and how do you handle it?”.

    Check yourself An FSM has 20 states. How many flip-flops do binary and one-hot encodings need?

    Binary: ⌈log₂ 20⌉ = 5 flip-flops. One-hot: 20 flip-flops.

    Revision sheet

    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.

    Practise it

    • 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

    PreviousNext: Encodings Compared

    Up next

    Up nextEncodings Compared

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Controller and Datapath
    • Moore vs Mealy
    • Multi-Cycle Sequencers