Codiode

Find First One Priority Encoder

HardVerilog / SystemVerilogBuild1 solved

When several requests arrive at once, an arbiter has to pick exactly one. A priority encoder does that by reporting the position of the first set bit.

The module computes the binary index of the least significant bit that is set to 1 in a parameterized request vector. It outputs this index alongside a valid flag indicating whether any bit was set. If the input vector is entirely zeros, the valid flag must be de-asserted and the index output must default to zero. The circuit must evaluate completely within a single clock cycle without inferring any state elements.

Parameter/Port Direction Width Description
WIDTH parameter integer Width of the input vector; defaults to 16
req input WIDTH Request input vector
first_one output $clog2(WIDTH) Binary index of the lowest set bit; 0 if req is 0
valid output 1 Asserted to 1 if at least one bit in req is 1; otherwise 0

Constraints

  • Purely combinational logic; no latches or flip-flops may be inferred.
  • The module must correctly handle any parameterized WIDTH value.
  • valid must be 0 and first_one must be 0 when req is all zeros.
  • Priority is strictly given to the least significant bit (index 0 has highest priority).

Topics

ParameterizedCombinationalPriority EncoderArbitration

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 Combinational RTL

Problem 9 of 15 in the Combinational RTL track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

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