one-hot to binary

M

Michael

Guest
Hello,

Does 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
etc

Thanks in advance,
Michael
 
Hi,
I think the problem is same as searching for a '1' in the string.
so u can check out any search method.there are a lot of them
like binary search,linear search etc...
Vits
Michael wrote:
Hello,

Does 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
etc

Thanks in advance,
Michael
 
Works on up to 32 bit numbers:


function integer convert;
input grey_num;
integer grey_num,i;
for (i=0; i<32; i=i+1) if (grey_num) convert = i+1;
endfunction


-Alex


Michael wrote:
Hello,

Does 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
etc

Thanks in advance,
Michael
 
There is one stric condition: there is ony ONE '1' there.
I believe there are good implementations exist.
vittal wrote:
Hi,
I think the problem is same as searching for a '1' in the string.
so u can check out any search method.there are a lot of them
like binary search,linear search etc...
Vits
Michael wrote:
Hello,

Does 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
etc

Thanks in advance,
Michael
 
It looks like a big mux. You don't reck the number has ONLY ONE '1'.
Alex wrote:
Works on up to 32 bit numbers:


function integer convert;
input grey_num;
integer grey_num,i;
for (i=0; i<32; i=i+1) if (grey_num) convert = i+1;
endfunction


-Alex


Michael wrote:
Hello,

Does 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
etc

Thanks in advance,
Michael
 
On 21 Sep 2006 05:05:35 -0700, "Michael" <michaelst@gmail.com> wrote:

Does 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) 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
jonathan.bromley@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.
 
Then, add agrument check to function:

function integer convert;
input grey_num;
integer grey_num,i, sum;
begin
sum = 0;
for (i=0; i<32; i=i+1) begin
sum = sum + grey_num;
if (sum > 1) $display("<%m> ERROR: %b is not grey code", grey_num);
if (grey_num) convert = i+1;
end
end
endfunction

Note, you still don't have to bother with parameters.

-Alex


Michael wrote:
It looks like a big mux. You don't reck the number has ONLY ONE '1'.
Alex wrote:
Works on up to 32 bit numbers:


function integer convert;
input grey_num;
integer grey_num,i;
for (i=0; i<32; i=i+1) if (grey_num) convert = i+1;
endfunction


-Alex


Michael wrote:
Hello,

Does 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
etc

Thanks in advance,
Michael
 

Welcome to EDABoard.com

Sponsor

Back
Top