One-hot state machine

Give every state its own flip-flop for fast, readable FSMs.

When to use it

For most FPGA state machines, one-hot encoding (exactly one flip-flop high per state) beats binary encoding. Flip-flops are plentiful, and the next-state and output logic collapses into tiny, fast expressions, you just test one bit per state. It's also trivial to read on a waveform.

How it works

This machine walks S0 → S1 → S2 → S3 and wraps, one step each time en is high, by rotating the hot bit. In a real FSM each state's transition is its own small term; you rarely need a big case. Reset forces a known one-hot state (never let a one-hot machine start at all-zeros).

Gotchas

Try it

Runs on our simulator, edit the code and press Run to see the waveform.

Open & fork in the playground → Ship the verified version: lfpga add lfpga_onehot_mux

The runnable example above is a teaching version. The libfpga library ships a hardened, parameterised, CI-tested module (lfpga_onehot_mux) for production use.

Next recipe: PWM generator