# The GAL PLD by nikeborome

VIEWS: 62 PAGES: 32

• pg 1
```									Designing Combinational Logic
Circuits in Verilog - 2

Discussion 7.3
Designing Combinational Logic
Circuits in Verilog - 2
• Binary to Gray code converter
• Gray code to binary converter
• Binary-to-BCD converter
Gray Code
Definition: An ordering of 2n binary numbers such that
only one bit changes from one entry to the next.
Binary coding {0...7}: {000, 001, 010, 011, 100, 101, 110, 111}
Gray coding {0...7}:   {000, 001, 011, 010, 110, 111, 101, 100}

Not unique

One method for generating a Gray code sequence:
right-most bit that produces a new string.
Binary - Gray Code Conversions
Gray code: G[i], i = n – 1 : 0
Binary code: B[i], i = n – 1 : 0
Binary coding {0...7}: {000, 001, 010, 011, 100, 101, 110, 111}
Gray coding {0...7}:     {000, 001, 011, 010, 110, 111, 101, 100}
Convert Binary to Gray:
Copy the most significant bit.
For each smaller i
G[i] = B[i+1] ^ B[i]

Convert Gray to Binary:
Copy the most significant bit.
For each smaller i
B[i] = B[i+1] ^ G[i]
Gray Code
Binary   Gray Code
B[2:0]    G[2:0]
Note that the least significant bit
that can be changed without           000        000
repeating a value is the bit
that is changed
001        001
010        011
Binary to Gray code
011        010
G[2] = B[2];                        100        110
G[1:0] = B[2:1] ^ B[1:0];
101        111
110        101
111        100
Binary to Gray code                       MSB

grayCode = binary ^ (binary >> 1)

G(msb) = B(msb);
for(j = msb-1; j >= 0; j=j-1)       B[j]         G[j]
G(j) = B(j+1) ^ B(j);

msb = 5 for 6-bit codes

LSB
bin2gray.v

module bin2gray ( B ,G );

input [3:0] B ;
wire [3:0] B ;

output [3:0] G ;
wire [3:0] G ;

assign G[3] = B[3];
assign G[2:0] = B[3:1] ^ B[2:0];

endmodule

Convert Binary to Gray:
Copy the most significant bit.
For each smaller i
G[i] = B[i+1] ^ B[i]
Binary to Gray Code Conversion
Designing Combinational Logic
Circuits in Verilog - 2
• Binary to Gray code converter
• Gray code to binary converter
• Binary-to-BCD converter
Binary - Gray Code Conversions
Gray code: G[i], i = n – 1 : 0
Binary code: B[i], i = n – 1 : 0
Binary coding {0...7}: {000, 001, 010, 011, 100, 101, 110, 111}
Gray coding {0...7}:     {000, 001, 011, 010, 110, 111, 101, 100}
Convert Binary to Gray:
Copy the most significant bit.
For each smaller i
G[i] = B[i+1] ^ B[i]

Convert Gray to Binary:
Copy the most significant bit.
For each smaller i
B[i] = B[i+1] ^ G[i]
Gray Code
Binary   Gray Code
B[2:0]    G[2:0]
Gray code to Binary         000        000
B[2] = G[2];
001        001
B[1:0] = B[2:1] ^ G[1:0];   010        011
011        010
100        110
101        111
110        101
111        100
Gray code to Binary
B(msb) = G(msb);
for(j = msb-1; j >= 0; j--)
B(j) = B(j+1) ^ G(j);

MSB

G[j]                          B[j]

LSB
Gray code to Binary
module gray2bin6 ( G ,B );

input [5:0] G ;
wire [5:0] G ;

output [5:0] B ;
wire [5:0] B ;

assign B[5] = G[5];
assign B[4:0] = B[5:1] ^ G[4:0];

endmodule
B(msb) = G(msb);
for(j = msb-1; j >= 0; j=j-1)
B(j) = B(j+1) ^ G(j);
gray2bin.v

module gray2bin ( G ,B );

input [3:0] G ;
wire [3:0] G ;

output [3:0] B ;
reg [3:0] B ;
Convert Gray to Binary:
integer i;
Copy the most significant bit.
For each smaller i
always @(G)
B[i] = B[i+1] ^ G[i]
begin
B[3] = G[3];
for(i=2; i >= 0; i = i-1)
B[i] = B[i+1] ^ G[i];
end

endmodule
Gray Code to Binary Conversion
Designing Combinational Logic
Circuits in Verilog - 2
• Binary to Gray code converter
• Gray code to binary converter
• Binary-to-BCD converter
S1. Shift the binary number left one bit.
22. If 8 shifts have taken place, the BCD number is in the
Hundreds, Tens, and Units column.
33. If the binary value in any of the BCD columns is 5 or greater,
add 3 to that value in that BCD column.
44. Go to 1.

Operation    Hundreds   Tens      Units          Binary
HEX                                        F          F
Start                                   1 1 1 1    1 1 1 1
Steps to convert an 8-bit binary number to BCD

Operation   Hundreds       Tens           Units                 Binary
HEX                                                       F           F
Start                                               1   1 1   1   1 1 1 1
Shift 1                                          1   1   1 1   1   1 1 1
Shift 2                                    1     1   1   1 1   1   1 1
Shift 3                                  1 1     1   1   1 1   1   1
Add 3                                1   0 1     0   1   1 1   1   1
Shift 4                          1   0   1 0     1   1   1 1   1
Add 3                            1   1   0 0     0   1   1 1   1
Shift 5                     1    1   0   0 0     1   1   1 1
Shift 6                   1 1    0   0   0 1     1   1   1
Add 3                 1   0 0    1   0   0 1     1   1   1
Shift 7           1   0   0 1    0   0   1 1     1   1
Add 3             1   0   0 1    0   1   0 1     0   1
Shift 8         1 0   0   1 0    1   0   1 0     1
BCD           2           5              5
A3 A2 A1 A0
A3   A2   A1   A0   S3   S2   S1   S0
0   0    0    0    0    0    0    0
0   0    0    1    0    0    0    1
0   0    1    0    0    0    1    0
C          0   0    1    1    0    0    1    1
0   1    0    0    0    1    0    0
0   1    0    1    1    0    0    0
S3 S2 S1 S0    0   1    1    0    1    0    0    1
0   1    1    1    1    0    1    0
1   0    0    0    1    0    1    1
1   0    0    1    1    1    0    0
1   0    1    0    X    X    X    X
1   0    1    1    X    X    X    X
1   1    0    0    X    X    X    X
1   1    0    1    X    X    X    X
1   1    1    0    X    X    X    X
1   1    1    1    X    X    X    X
K-Map for S3
A1 A0
A3   A2   A1   A0   S3   S2   S1   S0   A3 A2   00     01   11   10
0   0    0    0    0    0    0    0
0   0    0    1    0    0    0    1       00
0   0    1    0    0    0    1    0
0   0    1    1    0    0    1    1
0   1    0    0    0    1    0    0       01          1     1       1
0   1    0    1    1    0    0    0
0   1    1    0    1    0    0    1
0   1    1    1    1    0    1    0       11   X      X    X    X
1   0    0    0    1    0    1    1
1   0    0    1    1    1    0    0
1   0    1    0    X    X    X    X       10      1   1     X   X
1   0    1    1    X    X    X    X
1   1    0    0    X    X    X    X
1   1    0    1    X    X    X    X
1   1    1    0    X    X    X    X                S3 = A3
1   1    1    1    X    X    X    X
| A2 & A0
| A2 & A1
Binary-to-BCD
Converter

RTL Solution
Steps to convert a 6-bit binary number to BCD

Operation       Tens           Units             Binary
B                                       5 4 3 2 1 0
1. Clear all bits of z to zero           HEX                                        3       F
2. Shift B left 3 bits                   Start                                    1 1 1 1 1 1
Shift 1                               1   1 1 1 1 1
z[8:3] = B[5:0];               Shift 2                          1 1      1 1 1 1
3. Do 3 times                           Shift 3                        1 1 1      1 1 1
Add 3                     1 0 1 0         1 1 1
if Units >4 then add 3 to Units                          1   0 1 0 1         1 1
Shift 4
(note: Units = z[9:6])             Add 3                 1   1 0 0 0         1 1
1 1      0 0 0 1         1
Shift z left 1 bit                 Shift 5
1 1 0     0 0 1 1
Shift 6
4. Tens = P[6:4] = z[12:10]              BCD            6               3
Units = P[3:0] = z[9:6]                P        7          4   3           0
z       13         10 9            6 5                0
Operation           Tens                    Units                     Binary
binbcd6.v      B                                                             5 4 3 2 1 0
3       F
HEX
module binbcd6(B,P);                                                                                1 1 1 1 1 1
Start
input [5:0] B;                      Shift 1                                                 1       1 1 1 1 1
output [6:0] P;                     Shift 2                                         1 1             1 1 1 1

reg [6:0] P;                        Shift 3                                     1 1 1               1 1 1
Add 3                                  1 0 1 0                  1 1 1
reg [12:0] z;                                                        1     0 1 0 1                  1 1
Shift 4
integer i;                          Add 3                            1     1 0 0 0                  1 1
Shift 5                    1 1         0 0 0 1                  1

always @(B)                         Shift 6                1 1 0           0 0 1 1
BCD                       6                 3
begin                                            7                 4     3                0
P
for(i = 0; i <= 12; i = i+1)       z           13                10 9                   6 5                   0
z[i] = 0;
z[8:3] = B;                                                        Hex 3F
6-bit binary input
for(i = 0; i <= 2; i = i+1)
begin                                           0 B5 B4 B3 B2 B1 B0
1     1     1    1       1       1

if(z[9:6] > 4)                                        C1
z[9:6] = z[9:6] + 3;                       1       0      1    0

z[12:1] = z[11:0];                                           C2
end                                                     1      0    0    0

P = z[12:6];                            0                           C3

end                                                  1       1      0    0    0       1       1

endmodule                                      P7 P6 P5 P4 P3 P2 P1 P0

tens                     units
6                        3
binbcd6.v                              Hex 3F
6-bit binary input

Operation       Tens           Units             Binary           0 B5 B4 B3 B2 B1 B0
1     1     1    1       1   1
B                                       5 4 3 2 1 0
HEX                                        3       F
C1
Start                                    1 1 1 1 1 1
Shift 1                               1   1 1 1 1 1              1       0      1    0

Shift 2                          1 1      1 1 1 1
C2
Shift 3                        1 1 1      1 1 1
Add 3                     1 0 1 0         1 1 1                          1      0    0    0
Shift 4               1   0 1 0 1         1 1
0                       C3
Add 3                 1   1 0 0 0         1 1
Shift 5          1 1      0 0 0 1         1                      1       1      0    0    0       1   1
Shift 6         1 1 0     0 0 1 1
P7 P6 P5 P4 P3 P2 P1 P0
BCD            6               3
P        7          4   3           0
z       13         10 9            6 5                0       tens                     units
6                        3
BCD output
Operation   Hundreds        Tens               Units                  Binary
module binbcd8(B,P);                  B                                                        7         4 3       0
HEX                                                             F           F
input [7:0] B;                       Start                                                     1   1 1   1   1 1 1 1

output [9:0] P;        binbcd8.v    Shift 1
Shift 2                                            1
1
1
1
1
1 1
1 1
1
1
1 1 1
1 1
Shift 3                                       1    1   1   1   1 1   1   1
reg [9:0] P;                        Add 3                                    1    0    1   0   1   1 1   1   1
Shift 4                             1    0    1    0   1   1   1 1   1
reg [17:0] z;                       Add 3                               1    1    0    0   0   1   1 1   1
Shift 5                         1   1    0    0    0   1   1   1 1
integer i;                          Shift 6                    1    1   0    0    0    1   1   1   1
Add 3                 1    0    0   1    0    0    1   1   1   1
always @(B)                         Shift 7          1    0    0    1   0    0    1    1   1   1
Add 3            1    0    0    1   0    1    0    1   0   1
begin                             Shift 8        1 0    0    1    0   1    0    1    0   1
BCD          2                5                  5
for(i = 0; i <= 17; i = i+1)      P            9 8     7             4   3             0

z[i] = 0;                      z           17 16   15            12   11            8   7         4   3     0

z[10:3] = B;

for(i = 1; i <= 5; i = i+1)
begin
if(z[11:8] > 4)
z[11:8] = z[11:8] + 3;
if(z[15:12] > 4)
z[15:12] = z[15:12] + 3;
z[17:1] = z[16:0];
end
P = z[17:8];
end
endmodule
binbcd8.v
Operation   Hundreds        Tens               Units                  Binary
B                                                        7         4 3       0
HEX                                                             F           F
Start                                                     1   1 1   1   1 1 1 1
Shift 1                                                1   1   1 1   1   1 1 1
Shift 2                                            1   1   1   1 1   1   1 1
Shift 3                                       1    1   1   1   1 1   1   1
Add 3                                    1    0    1   0   1   1 1   1   1
Shift 4                             1    0    1    0   1   1   1 1   1
Add 3                               1    1    0    0   0   1   1 1   1
Shift 5                         1   1    0    0    0   1   1   1 1
Shift 6                    1    1   0    0    0    1   1   1   1
Add 3                 1    0    0   1    0    0    1   1   1   1
Shift 7          1    0    0    1   0    0    1    1   1   1
Add 3            1    0    0    1   0    1    0    1   0   1
Shift 8        1 0    0    1    0   1    0    1    0   1
BCD          2                5                  5
P            9 8     7             4   3             0
z           17 16   15            12   11            8   7         4   3     0
module binbcd9(B,P);
9-bit Binary Input
input [8:0] B;        binbcd9.v
output [10:0] P;                         0 B8 B7 B6 B5 B4 B3 B2 B1 B0

reg [10:0] P;                                   C1

reg [19:0] z;
integer i;                                           C2

always @(B)                                               C3
0
begin
for(i = 0; i <= 19; i = i+1)             C7                    C4
z[i] = 0;
z[11:3] = B;                                  C8                    C5

for(i = 0; i <= 5; i = i+1)                       C9                    C6

begin
if(z[12:9] > 4)               P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0

z[12:9] = z[12:9] + 3;      Hundreds             Tens                  Units

if(z[16:13] > 4)                               BCD Output

z[16:13] = z[16:13] + 3;   Figure 5. 9-bit Binary-to-BCD Converter

z[19:1] = z[18:0];
end
P = z[19:9];
end
endmodule
9-bit Binary Input

0 B8 B7 B6 B5 B4 B3 B2 B1 B0
binbcd9.v
C1

C2

0                   C3

C7                    C4

C8                    C5

C9                    C6

P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0

Hundreds             Tens                  Units
BCD Output

Figure 5. 9-bit Binary-to-BCD Converter
16-bit
Binary-to-BCD
Converter
binbcd16.v

module binbcd16(B,P);
input [15:0] B;
output [18:0] P;

reg [18:0] P;
reg [31:0] z;
integer i;
always @(B)
begin
for(i = 0; i <= 31; i = i+1)
z[i] = 0;
z[18:3] = B;

for(i = 0; i <= 12; i = i+1)
begin
if(z[19:16] > 4)
z[19:16] = z[19:16]   + 3;
if(z[23:20] > 4)
z[23:20] = z[23:20]   + 3;
if(z[27:24] > 4)
z[27:24] = z[27:24]   + 3;
if(z[31:28] > 4)
z[31:28] = z[31:28]   + 3;
z[31:1] = z[30:0];
end
P = z[31:16];
end
endmodule
binbcd16.v

```
To top