16-bit Leading Zero Counter Loop
Normalisation and division hardware need to know how many zeros sit above the first 1 in a word. A for loop describes the count for any width without a hand-written if chain.
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 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 Datapath Design
Problem 11 of 27 in the Datapath Design track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks