Write a verilog code for BIN(binary) TO GRAY?

All QuestionsCategory: VerilogWrite a verilog code for BIN(binary) TO GRAY?
Chetan Shidling Staff asked 5 years ago

I need code.

1 Answers
Chetan Shidling Staff answered 5 years ago

//BIN TO GRAY

module BIN_GRAY(g,b);
input [2:0] b; //3-bits 0,1,2
output wire [2:0] g;
assign g[2]=b[2];
assign g[1]=b[2]^b[1];
assign g[0]=b[1]^b[0];

//TEST BENCH

module bin_Gray_tb;

// Inputs

reg [2:0] b;

// Outputs

wire [2:0] g;

// Instantiate the Unit Under Test (UUT)

BIN_GRAY uut (
.g(g),
.b(b)
);
initial begin

// Initialize Inputs

b = 0;

// Wait 100 ns for global reset to finish

#100;

// Add stimulus here

b=3’b001;
#100;
b=3’b010;
#100;
b=3’b100;
#100;
end

endmodule