Documente Academic
Documente Profesional
Documente Cultură
Tools Used:
Cadence Digital Labs.
Binary to gray
Theory:
The logical circuit which converts the binary code to equivalent gray code is
known as binary to gray code converter. An n-bit gray code can be obtained by
reflecting an n-1 bit code about an axis after 2n-1 rows and putting the MSB (Most
Significant Bit) of 0 above the axis and the MSB of 1 below the axis. Reflection of
Gray codes is shown below.
The 4 bit binary to gray code conversion table is given below:
Schematic:
Verilog code:
module binarytogray(g,b);
input [3:0]b;
output [3:0]g;
assign g[3]=b[3];
assign g[2]=b[3]^b[2];
assign g[1]=b[2]^b[1];
assign g[0]=b[1]^b[0];
endmodule
Testbench:
module binarytogray_tb;
/// Inputs
reg [3:0] b;
// Outputs
wire [3:0] g;
initial begin
// Initialize Inputs
b = 0000;
end
endmodule
Waveform:
.g file
Theory:
This gray to binary conversion method also uses the working concept of EX-OR
logic gate among the bits of gray as well as binary bits. The following example
with step by step procedure may help to know the conversion concept of gray code
to binary code.
To change gray to binary code, take down the MSB digit of the gray code number,
as the primary digit or the MSB of the gray code is similar to the binary digit.
To get the next straight binary bit, it uses the XOR operation among the primary
bit or MSB bit of binary to the next bit of the gray code.
Similarly, to get the third straight binary bit, it uses the XOR operation among the
second bit or MSB bit of binary to the third MSD bit of the gray code and so on.
From the above operation, finally we can get the binary values like b3 = g3, b2 =
b3 XOR g2, b1= b2 XOR g1, b0 = b1 XOR g0.
For example take the gray value g3, g2, g1, g0 = 0011 and find the binary code b3,
b2, b1, b0 based on the above concept
b3=g3=0
b2 = b3 XOR g2 = 0 XOR 0 =0
b1= b2 XOR g1= 0 XOR 1 = 1
b0= b1 XOR g0= 1 XOR 1 = 0
The final binary code for the value of gray 0011 is 0010
Gray to Binary Code Converter Table
Decimal Number Gray Code
Binary Code
0 0000 0000
1 0001 0001
2 0010 0011
3 0010 0011
4 0110 0100
5 0111 0101
6 0101 0110
7 0100 0111
8 1100 1000
9 1101 1001
10 1111 1010
11 1110 1011
12 1010 1100
13 1011 1101
14 1001 1110
15 1000 1111
Schematic:
Verilog code:
module graytobinary(b,g);
input [3:0]g;
output [3:0]b;
assign b[3]=g[3];
assign b[2]=g[3]^g[2];
assign b[1]=g[3]^g[2]^g[1];
assign b[0]=g[3]^g[2]^g[1]^g[0];
endmodule
Testbench:
module graytobinary_tb;
// Inputs
reg [3:0] g;
// Outputs
wire [3:0] b;
initial begin
// Initialize Inputs
g = 4'b0000;
end
endmodule
Waveform:
.g file
Testbench:
module tb_sipo;
reg rst,clk,a;
wire [3:0]q;
sipo uut(.clk(clk),.rst(rst),.a(a),.q(q));
initial
clk=1'b0;
always #10 clk=~clk;
initial begin
rst=1'b1; a=1'b1;
#500 rst=1'b0;
#100 a=1'b0;
#100 a=1'b1;
#100 a=1'b0;
#100 a=1'b0;
#100 a=1'b1;
#100 a=1'b0;
end
initial
#100 $stop;
endmodule
Waveform:
.g file:
create_clock -name clock -period file -waveform {0 5}
set -input -delay 1.0 -clock clk [get _ports "a"]
set -input -delay 1.0 -clock clk [get _ports "clk"]
set -input -delay 1.0 -clock clk [get _ports "rst"]
set -output -delay 1.0 -clock clk [get _ports "q[0]"]
set -output -delay 1.0 -clock clk [get _ports "q[1]"]
set -output -delay 1.0 -clock clk [get _ports "q[2]"]
set -output -delay 1.0 -clock clk [get _ports "q[3]"]
Theory:
A shift register basically consists of several single bit “D-Type
Data Latches”, one for each data bit, either a logic “0” or a “1”, connected
together in a serial type daisy-chain arrangement so that the output from
one data latch becomes the input of the next latch and so on. Shift
Registers are used for data storage or for the movement of data and are
therefore commonly used inside calculators or computers to store data
such as two binary numbers before they are added together, or to convert
the data from either a serial to parallel or parallel to serial format. The
individual data latches that make up a single shift register are all driven by
a common clock ( Clk ) signal making them synchronous devices.
Schematic:
Verilog code:
module piso(clk,rst,a,q);
input clk,rst;
input [3:0] a;
output q;
reg q;
reg [3:0] temp;
always@ (posedge clk or posedge rst)
begin
if (rst==1'b1)
begin q<=1'b0;
temp<=a;
end
else begin
q<=temp[0];
temp<=temp>>1'b1;
end
end
endmodule
Testbench:
module tb_piso;
reg clk,rst;
reg [3:0] a;
wire q;
piso uut(.clk(clk),.rst(rst),.a(a),.q(q));
initial
clk=1'b1;
always #10 clk=~clk;
initial begin
rst=1'b1;
a=4'b1101;
#300 rst=1'b0;
#200 rst=1'b1;
#200 rst=1'b0;
end
initial #100 $stop;
endmodule
Waveform:
Results: System verilog for binary to gray and gray to binary converter and shift
registers are verified.