CBL_GrayToBinary

CBL_GrayToBinary : A combinational logic converting a gray code to a binary code

This model describes a combinational logic converting a gray code to a binary code. Gray codes are non-weighted codes, where two successive values differ only by one bit.

module CBL_GrayToBinary #(
    parameter width = 4             // bit width
)(
    input [width-1:0] in,           // input bits
    output reg [width-1:0] out      // output bits
);

int i;

always @(in) begin
    for (i=width-1; i>=0; i--) begin
        if (i == width-1) out[i] = in[i];
        else out[i] = out[i+1] ^ in[i];
    end
end

endmodule

Input/Output Terminals

Name I/O Type Description
in [width-1:0] input wire input bits
out [width-1:0] output reg output bits

Parameters

Name Type Default Description
width integer 4 bit width

List of Testbenches

tb_check : A testbench for checking the basic functionality of a combinational logic

tb_check : A testbench for checking the basic functionality of a combinational logic

This testbench checks the basic functionality of a combinational logic by enumerating all the possible input values and listing its corresponding outputs.

module tb_check ();
parameter width = 4;

reg [width-1:0] in;
wire [width-1:0] in_gray;
wire [width-1:0] out;

CBL_BinaryToGray #(.width(width)) DRV (.in(in), .out(in_gray));
CBL_GrayToBinary #(.width(width)) DUT (.in(in_gray), .out(out));

initial begin
    in = 0;

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

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

endmodule