CBL_BinaryToGray : A combinational logic converting a binary code to a gray code
This model describes a combinational logic converting a binary code to a gray code. Gray codes are non-weighted codes, where two successive values differ only by one bit.
module CBL_BinaryToGray #( 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=0; i<width; i++) begin if (i < width-1) out[i] = in[i+1] ^ in[i]; else out[i] = 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 in_width = 4; parameter out_width = in_width; reg [in_width-1:0] in; wire [out_width-1:0] out; CBL_BinaryToGray #(.width(in_width)) DUT (.in, .out); initial begin in = 0; repeat ((1 << in_width) - 1) begin #(1ns); in = in + 1; end end always @(out) begin $display("IN: %b --> OUT: %b", in, out); end endmodule