Post by MichaelDoes anyone know some optimal Verilog generic function for
convertion one-hot number into binary?
For example,
4'b0001 -> 1
4'b0010 -> 2
4'b0100 -> 3
4'b1000 -> 4
Elsewhere you've already said that you can guarantee that there
is only one bit set in the input word. In that case, the following
works well:
module onehot_to_binary
#(parameter output_bits=3, input_bits=1<<output_bits)
( input wire onehot[input_bits-1:0],
output reg binary[output_bits-1:0]);
always @onehot begin : converter
integer i;
reg result [output_bits-1:0];
result = 0;
for (i=0; i<input_bits; i=i+1)
if (onehot[i]) result = result | i;
binary = result;
end
endmodule
Of course it will give crazy results if you have more than one bit
set in the input "onehot" vector.
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
***@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.