Professional Documents
Culture Documents
Contents of The Lecture
Contents of The Lecture
B3 B2 B1 B0 G3 G2 G1 G0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1
0 0 1 1 0 0 1 0
GMR Institute of Technology
0 1 0 0 0 1 1 0
0 1 0 1 0 1 1 1
0 1 1 0 0 1 0 1
0 1 1 1 0 1 0 0
1 0 0 0 1 1 0 0
1 0 0 1 1 1 0 1
1 0 1 0 1 1 1 1
1 0 1 1 1 1 1 0
1 1 0 0 1 0 1 0
1 1 0 1 1 0 1 1
1 1 1 0 1 0 0 1
1 1 1 1 1 0 0 0 60
Verilog Code of a 4 Bit Binary to Gray Code converter
Structure Model
module binarytogray
(b3,b2,b1,b0,g3,g2,g1,g0);
input b3,b2,b1,b0 ;
output g3,g2,g1,g0;
xor x0(g0,b0,b1);
xor x1(g1,b1,b2);
xor x2(g2,b2,b3);
buf a1 (g3,b3);
endmodule 61
Test Bench of 4 Bit Binary to Gray Code converter
Behaviour Model
module binarytogray (b3,b2,b1,b0,g3,g2,g1,g0);
input b3,b2,b1,b0 ;
output g3,g2,g1,g0;
reg g3,g2,g1,g0;
always @(b3,b2,b1,b0)
begin
g0=b1 ^ b0;
GMR Institute of Technology
g1=b2 ^ b1;
g2=b3 ^ b2;
g3=b3;
end
endmodule
Test Bench
module testbench;
reg b3; reg b2; reg b1; reg b0; wire g3; wire g2; wire g1; wire g0;
binarytogray uut (b3,b2,b1,b0,g3,g2,g1,g0);
initial
begin
$monitor($time,“b3=%b,b2=%b,b1=%b,b0=%b,g3=%b, g2=%b, g1=%b,
g0=%b", b3,b2,b1,b0,g3,g2,g1,g0);
#5 b3=0;b2=0;b1=0;b0=1
#5 b3=0;b2=1;b1=0;b0=1
#5 b3=1;b2=0;b1=0;b0=1
#5 b3=0;b2=0;b1=1;b0=1
#5 $finish;
end
endmodule 62
System Design using Verilog
G3 G2 G1 G0 B3 B2 B1 B0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1
0 0 1 1 0 0 1 0
GMR Institute of Technology
0 1 0 0 0 1 1 1
0 1 0 1 0 1 1 0
0 1 1 0 0 1 0 0
0 1 1 1 0 1 0 0
1 0 0 0 1 1 1 1
1 0 0 1 1 1 1 0
1 0 1 0 1 1 0 0
1 0 1 1 1 1 0 1
1 1 0 0 1 0 0 0
1 1 0 1 1 0 0 1
1 1 1 0 1 0 1 1
1 1 1 1 1 0 1 0 64
Verilog Code 4 Bit of 4 Bit Gray to Binary Code converter
Structure Model
module gray_to_binary(g,b);
input [3:0]g;
inout [3:0]b;
buf b1 (b[3],g[3]);
xor x1 (b[2],g[3],g[2]);
xor x2 (b[1],b[2],g[1]);
xor x3 (b[0],b[1],g[0]);
endmodule
65
Test Bench 4 Bit of 4 Bit Gray to Binary Code converter
Structure Model
module gray_to_binary(g,b);
input [3:0]g;
inout [3:0]b;
buf b1 (b[3],g[3]);
xor x1 (b[2],g[3],g[2]);
xor x2 (b[1],b[2],g[1]);
xor x3 (b[0],b[1],g[0]);
GMR Institute of Technology
endmodule
Test Bench
module testbench;
reg [3:0]g; wire [3:0]b;
gray_to_binary uut (.g(g),.b(b));
initial
begin
$monitor($time,"g=%b,b=%b", g,b);
#5 g=4'b0000;
#5 g=4'b0001;
#5 g=4'b0010;
#5 g=4'b0011;
#5 g=4'b0100;
#5 g=4'b1011;
#5 $finish;
end 66