16-bit Leading Zero Counter Loop
Floating-point normalization and integer division hardware rely on leading zero counters to determine the magnitude of an operand. Manually writing priority encoders for wide data paths using cascaded conditional statements is prone to copy paste errors and scales poorly to larger bus widths.
The module computes the number of consecutive zero bits in a 16-bit input word, starting from the most significant bit (MSB, index 15) and scanning downwards to the least significant bit (LSB, index 0). If the MSB is 1, the count is 0. If the input is entirely zeros, the count is 16.
You must implement this logic using a combinational for loop inside an always_comb or always @* block. The loop should iterate through the bits and dynamically determine the leading zero count, allowing the synthesizer to unroll it into a priority encoder structure without requiring a massive 16-branch if statement.
| Signal | Direction | Width | Description | |--------|-----------|-------|-------------| | in | input | 16 | The 16-bit input data vector | | zeros| output | 5 | Number of leading zeros; ranges from 0 to 16 |
Constraints
- Purely combinational logic; no clock or reset ports.
- The
zerosoutput must be exactly 5 bits wide to represent the maximum value of 16. - If
inis 16'h0000,zerosmust evaluate to 16. - You must use a procedural loop (e.g., a
forloop) rather than manualifchains orcasestatements.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against a real 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.