CBL_OneHotToBinary

CBL_OneHotToBinary : A priority encoder logic converting a one-hot code to a binary code

This model describes a combinational logic converting a (2^N)-bit one-hot code to an N-bit binary code. In case more than one input bits have a value of 1, the model functions as an priority encoder. For example, the model converts an 8-bit input to a 3-bit binary-coded output following the patterns below:

8'b00000001  -->  3'b000
8'b0000001x  -->  3'b001
8'b000001xx  -->  3'b010
8'b00001xxx  -->  3'b011
8'b0001xxxx  -->  3'b100
8'b001xxxxx  -->  3'b101
8'b01xxxxxx  -->  3'b110
8'b1xxxxxxx  -->  3'b111

where x denotes a don’t-care bit.

module CBL_OneHotToBinary #(
    parameter width = 4                 // bit width of the binary-coded output
)(
    input [(1<<width)-1:0] in,          // one-hot-coded input
    output reg [width-1:0] out          // binary-coded output
);

int i;
reg [width-1:0] _out;

always @(in) begin
    _out = 'bx;
    for (i=0; i<(1<<width); i=i+1) begin
        if (in[i] == 1'b1) _out = i;
    end
    out = _out;
end

endmodule

Input/Output Terminals

Name I/O Type Description
in [(1<<width)-1:0] input wire one-hot-coded input
out [width-1:0] output reg binary-coded output

Parameters

Name Type Default Description
width integer 4 bit width of the binary-coded output

List of Testbenches

tb_check : A testbench for checking the basic functionality of a one-hot-to-binary converter (priority encoder)

tb_check : A testbench for checking the basic functionality of a one-hot-to-binary converter (priority encoder)

This testbench checks the basic functionality of a one-hot-to-binary converter (priority encoder) by enumerating all the possible one-hot-coded input values and listing its corresponding outputs.

module tb_check ();
parameter width = 4;

reg [(1<<width)-1:0] in;
wire [width-1:0] out;

CBL_OneHotToBinary #(.width(width)) DUT (.in, .out);

initial begin
    in = 1;

    repeat ((1 << width) - 1) begin
        #(1ns);
        in = in << 1;
    end
end

always @(out) begin
    $display("IN: %b --> OUT: %b", in, out);
end

endmodule