Codiode

Explicit One Hot Encoding

MediumVerilog / SystemVerilogBuildFree

One-hot encoding spends one flip-flop per state and in return makes the next-state logic very shallow. This module implements an overlapping sequence detector that identifies the bit pattern 1011 from a serial input stream. To guarantee synthesis tools do not compress the state registers into a dense binary encoding, the FSM explicitly exposes its internal 5-bit one-hot state vector as an output.

The state machine must transition through five distinct one-hot states. When the sequence 1011 is detected, the module asserts the detected signal for one clock cycle and sets the state vector to the final state. Because the detector is overlapping, a subsequent 0 after a successful detection must transition the FSM directly to the state representing a received 10.

Positive-edge triggered clock clk and active-low asynchronous reset rst_n govern the timing. On reset, the FSM must return to the initial state (5'b00001) and detected must be 0.

Worked Trace:

  • Cycle 1: rst_n=0, d=0 → state_out=5'b00001, detected=0 (Reset to IDLE)
  • Cycle 2: rst_n=1, d=1 → state_out=5'b00010, detected=0 (Received 1)
  • Cycle 3: rst_n=1, d=0 → state_out=5'b00100, detected=0 (Received 10)
  • Cycle 4: rst_n=1, d=1 → state_out=5'b01000, detected=0 (Received 101)
  • Cycle 5: rst_n=1, d=1 → state_out=5'b10000, detected=1 (Received 1011)
  • Cycle 6: rst_n=1, d=0 → state_out=5'b00100, detected=0 (Overlapping 10)
Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; resets state to 5'b00001
d input 1 Serial data input
detected output 1 Asserted high when sequence 1011 is detected
state_out output 5 Current one-hot state vector

Constraints

  • The FSM must be implemented using exactly 5 states.
  • The state_out vector must always contain exactly one high bit (one-hot encoding).
  • The reset state must be 5'b00001.
  • Sequence detection must support overlapping patterns.
  • The output detected must be combinationally derived from the current state or updated synchronously with the state transition.

Topics

FSMOne-HotSynthesisSequence Detector

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

Free to solve. A Codiode account keeps your progress.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of FSM Coding

Problem 4 of 15 in the FSM Coding track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.