J
Jonathan Bromley
Guest
On Thu, 7 Apr 2011 10:47:17 +0100, Chris Hinsley wrote:
the port list in the old Verilog-95 style because it makes
parameterization a little safer (I can protect INPUT_BITS
against accidental change by making it a localparam).
module ONEHOT_TO_CODE(in_onehot, out_code);
parameter OUTPUT_BITS = 2;
localparam INPUT_BITS = 1 << OUTPUT_BITS;
input [INPUT_BITS-1:0] in_onehot;
output [OUTPUT_BITS-1:0] out_code;
reg [OUTPUT_BITS-1:0] out_code;
always @(in_onehot) begin : onehot_or_tree
integer bit_pos;
out_code = 0;
for (bit_pos = 0; bit_pos < INPUT_BITS; bit_pos=bit_pos+1)
if (in_onehot[bit_pos])
out_code = out_code | bit_pos;
end
endmodule
Jan's solution is also fine but in fact it's a priority encoder
rather than a one-hot encoder. A sufficiently smart synthesis
tool *might* be able to look upstream and detect that the input
signal is indeed one-hot, and perform the right optimizations;
but the simple change to OR-logic in my example above makes
the optimization certain, even if the onehot code is a primary
input for which the tool has no knowledge.
--
Jonathan Bromley
Try this in your setup and see how it shapes up. I wroteAnd I'm having problems getting a paramatized form that produces what I
want, ie somthing that dosn't suck.
My macro did produce the correct result, and did it in what I thought
was an efficent way useing 'or' gates. If you have a simpler way to do
it I'd be grateful to see it.
the port list in the old Verilog-95 style because it makes
parameterization a little safer (I can protect INPUT_BITS
against accidental change by making it a localparam).
module ONEHOT_TO_CODE(in_onehot, out_code);
parameter OUTPUT_BITS = 2;
localparam INPUT_BITS = 1 << OUTPUT_BITS;
input [INPUT_BITS-1:0] in_onehot;
output [OUTPUT_BITS-1:0] out_code;
reg [OUTPUT_BITS-1:0] out_code;
always @(in_onehot) begin : onehot_or_tree
integer bit_pos;
out_code = 0;
for (bit_pos = 0; bit_pos < INPUT_BITS; bit_pos=bit_pos+1)
if (in_onehot[bit_pos])
out_code = out_code | bit_pos;
end
endmodule
Jan's solution is also fine but in fact it's a priority encoder
rather than a one-hot encoder. A sufficiently smart synthesis
tool *might* be able to look upstream and detect that the input
signal is indeed one-hot, and perform the right optimizations;
but the simple change to OR-logic in my example above makes
the optimization certain, even if the onehot code is a primary
input for which the tool has no knowledge.
--
Jonathan Bromley