CBL_ThermToBinary

CBL_ThermToBinary : A combinational logic converting a thermometer code to a binary code

This model describes a combinational logic converting a (2^N-1)-bit thermometer code to an N-bit binary code. For example, the model converts a 7-bit thermometer-coded input to a 3-bit binary-coded output following the patterns below.

7'b0000000  --> 3'b000
7'b0000001  --> 3'b001
7'b0000011  --> 3'b010
7'b0000111  --> 3'b011
7'b0001111  --> 3'b100
7'b0011111  --> 3'b101
7'b0111111  --> 3'b110
7'b1111111  --> 3'b111

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

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

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

endmodule

Input/Output Terminals

Name I/O Type Description
in [(1<<width)-2:0] input wire thermometer-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 thermometer-to-binary converter

tb_check : A testbench for checking the basic functionality of a thermometer-to-binary converter

This testbench checks the basic functionality of a thermometer-to-binary converter by enumerating all the possible thermometer-coded input values and listing its corresponding outputs.

module tb_check ();
parameter width = 4;

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

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

initial begin
    in = 0;

    repeat ((1 << width) - 1) begin
        #(1ns);
        in = {in[(1<<width)-3:0], 1'b1};
    end
end

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

endmodule