Codiode

One-Hot State Decoder

MediumVerilog / SystemVerilogBuild

One-hot state registers are fast, but debug ports and other modules want a binary index. unique case tells synthesis that exactly one bit is set, so it builds the conversion without priority logic.

The module continuously monitors an 8-bit input vector. It decodes this input into a 3-bit binary value corresponding to the active bit index. For example, if bit 3 is high, the binary output is 3. A validation flag must also be generated. This flag asserts high when the input is a valid one-hot value. If the input is not one-hot (meaning all zeros, or multiple bits set simultaneously), the validation flag must be driven low and the binary output must default to zero.

Timing and Reset Rules:

  • This module is purely combinational logic.
  • There is no clock or reset signal.
  • Both outputs must evaluate immediately based on the current input state.
  • Invalid input conditions must immediately force the outputs to their default safe values.
Signal Direction Width Description
one_hot_in input 8 The one-hot encoded state vector
binary_out output 3 The decoded binary index of the active bit (0 to 7)
valid output 1 Asserts to 1 if exactly one bit of one_hot_in is high; 0 otherwise

Constraints

  • All output values must be evaluated combinationally.
  • The design must handle all 256 possible input values safely without inferring latches.
  • binary_out must be strictly 3'b000 when valid is 0.
  • The implementation should utilize the unique case SystemVerilog construct to guarantee parallel logic synthesis.

Topics

CombinationalDecoderSystemVerilogUnique Case

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.

This problem is part of Codiode Pro. The statement above is free to read.

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 Interview Circuit

Problem 10 of 10 in the Interview Circuit track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

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