I have some problems with a question that ask me to get the number of one's in the longest one's sequence in a 16 bit input using a 5 bit number in verilog. For example
(0111011111010101 = 00101 the answer is five duo to the sequence in the middle)
(1111111111111111 = 100000) I would like to know how to do it using both combinational and sequential circuits.
Electronic – How to get the longest one’s sequence in verilog
verilog
Best Answer
You can solve this question with a shift and kill method.
Let's say you have N input bits and N is 16.
Step 1)
Load a N bit wide shift register with your input in parallel. Add a zero bit at LSB.
reset your result counter to 0
Step 2)
Perform a 'shift' operation that expands existing zeros to the left neighbor bit.
Increment the result counter.
Step 3)
Check if the register is all zero.
false -> repeat step 2 and 3
true -> result counter holds your longest one sequence.
Example:
Minimum execution time is 1 load and 1 compare -> can be solved in 1 cycle.
Maximum execution time is 1 load and N shifts -> 17 cycles in the example.
Real execution time is 1 load and n shifts if n is the length of the longest one sequence -> 5 cycles in the example.
How to calculate the bits while shifting?
Let's name the shift register
a
.How many hardware is used?
- N registers for a
- N LUTs for load and shift before each register
- a ld(N+1) bit counter
- a N bit zero comparator
On Xilinx hardware: Xilinx FPGAs have 6-input LUTs which can be used as 2 5-input LUTs moreover there are 2 FF per LUT. Synthesis can map a 16 bit shift register into only 2 slices and the comperator into a third slice.
As requested - a 5 bit counter