Lesson 14 +35 XP

Code Converters & Parity Generator / Checker

Code Converters & Parity Generator / Checker

Different parts of a system often use different binary codes: a keypad produces BCD, an arithmetic unit wants Excess-3 or binary, a shaft encoder produces Gray code. A code converter is a combinational circuit that translates one code into another. This lesson designs the classic converters and then the parity generator and parity checker, which protect data against single-bit errors.

1. Design Procedure for a Code Converter

A code converter is designed like any combinational circuit (Lesson 9):

  1. Write the truth table: input code on the left, output code on the right.
  2. Mark input combinations that never occur as don't-cares.
  3. Simplify each output bit with a K-map.
  4. Draw the circuit.

2. BCD to Excess-3 Converter

Input ABCD (BCD, 0 to 9); output wxyz (Excess-3, the input plus 3). Inputs 1010 to 1111 are don't-cares.

DigitA B C Dw x y z
000000011
100010100
200100101
300110110
401000111
501011000
601101001
701111010
810001011
910011100
SolvedExample 1 · Simplifying the four outputs (Mano)

Find minimal expressions for w, x, y, z with d(10, 11, 12, 13, 14, 15).

  1. w = ∑ m(5, 6, 7, 8, 9): octet A (rows 11 and 10 with the don't-cares), quads BD (5, 7, 13, 15) and BC (6, 7, 14, 15). So w = A + BC + BD.
  2. x = ∑ m(1, 2, 3, 4, 9): quad B'D (1, 3, 9, 11), quad B'C (2, 3, 10, 11), pair BC'D' (4, 12). So x = B'C + B'D + BC'D'.
  3. y = ∑ m(0, 3, 4, 7, 8): quad C'D' (0, 4, 8, 12) and quad CD (3, 7, 11, 15). So y = CD + C'D' = (C oplus D)'.
  4. z = ∑ m(0, 2, 4, 6, 8): the octet of all cells with D = 0. So z = D'.
Answerw = A + BC + BD, x = B'C + B'D + BC'D', y = CD + C'D', z = D'

K-maps of the BCD to Excess-3 converterK-maps of the BCD to Excess-3 converter

Mano also shows a cheaper multilevel form by sharing the term C + D: x = B'(C + D) + B(C + D)' and w = A + B(C + D). This reuses one OR gate in two outputs.

Logic diagram of the BCD to Excess-3 converterLogic diagram of the BCD to Excess-3 converter

3. Excess-3 to BCD Converter

The reverse converter subtracts 3. Its inputs wxyz range over 0011 to 1100; the six other combinations (0000, 0001, 0010, 1101, 1110, 1111) are don't-cares. Simplification gives

A = wx + wyz qquad B = x'y' + x'z' + xyz qquad C = y'z + yz' = y oplus z qquad D = z'

4. Binary and Gray Code Converters

From Lesson 3, binary to Gray is one XOR per bit and Gray to binary is a chain of XORs. These can also be derived with K-maps, which is a common exam exercise.

SolvedExample 2 · 4-bit binary to Gray by K-map

Derive the 4-bit binary to Gray converter from its truth table.

  1. G3 is 1 for inputs 8 to 15, so G3 = B3.
  2. G2 is 1 for inputs 4 to 11 (the Gray codes 0110 to 1110 have G2 = 1): the map gives B3' B2 + B3 B2' = B3 oplus B2.
  3. In the same way G1 = B2 oplus B1 and G0 = B1 oplus B0, each map being a checkerboard pair of quads.
AnswerG3 = B3, G2 = B3 oplus B2, G1 = B2 oplus B1, G0 = B1 oplus B0 (three XOR gates)
ConverterEquationsGates
Binary to GrayG3 = B3, Gi = Bi+1 oplus Bi3 XOR, all in parallel
Gray to binaryB3 = G3, Bi = Bi+1 oplus Gi3 XOR in a chain

Binary to Gray and Gray to binary convertersBinary to Gray and Gray to binary converters

BCD to binary

A two-digit BCD number D1 D0 has the binary value 10 D1 + D0 = 8 D1 + 2 D1 + D0. A converter can be built with adders that add the digit shifted left by 3 and by 1. For example 0010 0111 (27): 8 · 2 + 2 · 2 + 7 = 27 = 110112.

5. Parity Generator

The parity generator at the transmitter creates the parity bit P from the message bits (Lesson 3). For a 3-bit message xyz:

x y zP (even)P (odd)
0 0 001
0 0 110
0 1 010
0 1 101
1 0 010
1 0 101
1 1 001
1 1 110

The even-parity column is 1 when the message has an odd number of 1s, so that the four transmitted bits have an even number. Its K-map is a checkerboard, which is the XOR function:

P_{even} = x oplus y oplus z qquad P_{odd} = (x oplus y oplus z)'

6. Parity Checker

The parity checker at the receiver takes all four received bits and computes the check bit C:

C = x oplus y oplus z oplus P

For even parity, C = 0 means the received word has an even number of 1s (no error detected), and C = 1 means a parity error. Its truth table has 16 rows; C = 1 for the eight rows with an odd number of 1s: C = ∑ m(1, 2, 4, 7, 8, 11, 13, 14).

3-bit even parity generator and 4-bit parity checker3-bit even parity generator and 4-bit parity checker

SolvedExample 3 · Generating and checking parity

The message 101 is sent with even parity. (a) Find P. (b) Find C if received correctly. (c) Find C if the first bit is flipped.

  1. (a) P = 1 oplus 0 oplus 1 = 0. The transmitted word is 1010 (two 1s, even).
  2. (b) C = 1 oplus 0 oplus 1 oplus 0 = 0: no error.
  3. (c) Received 0010: C = 0 oplus 0 oplus 1 oplus 0 = 1: error detected.
AnswerP = 0; C = 0 when correct and C = 1 after the error

For n message bits, a tree of XOR gates generates the parity in about log2 n gate delays: an 8-bit generator needs 7 two-input XOR gates in 3 levels. The 74280 is a 9-bit parity generator/checker with both even and odd outputs.

7. Converters and Parity in Verilog

The converter uses the four equations of Example 1. The parity circuits use the reduction XOR operator ^m, which XORs all bits of m together:

VERILOG
// BCD to Excess-3 converter (gate equations) and an even-parity generator / checker
module bcd_to_xs3 (
    input  [3:0] bcd,      // A B C D
    output [3:0] xs3       // w x y z
);
    wire a = bcd[3], b = bcd[2], c = bcd[1], d = bcd[0];
    assign xs3[3] = a | (b & c) | (b & d);                  // w
    assign xs3[2] = (~b & c) | (~b & d) | (b & ~c & ~d);    // x
    assign xs3[1] = ~(c ^ d);                               // y = CD + C'D'
    assign xs3[0] = ~d;                                     // z
endmodule

module parity_gen (
    input  [2:0] m,
    output       p
);
    assign p = ^m;          // even parity bit: XOR of the message bits
endmodule

module parity_chk (
    input  [2:0] m,
    input        p,
    output       err
);
    assign err = ^{m, p};   // 1 when the received word has an odd number of 1s
endmodule

module tb;
    reg  [3:0] bcd;
    wire [3:0] xs3;
    reg  [2:0] msg;
    wire       p, err;
    reg        flip;
    integer i;

    bcd_to_xs3 u1 (.bcd(bcd), .xs3(xs3));
    parity_gen u2 (.m(msg), .p(p));
    parity_chk u3 (.m(msg ^ {flip, 2'b00}), .p(p), .err(err));

    initial begin
        for (i = 0; i < 10; i = i + 1) begin
            bcd = i;
            #1 $display("BCD %b -> XS-3 %b", bcd, xs3);
        end
        msg = 3'b101; flip = 0;
        #1 $display("message %b  P=%b  received ok:      error=%b", msg, p, err);
        flip = 1;
        #1 $display("message %b  P=%b  MSB flipped:      error=%b", msg, p, err);
    end
endmodule

The output reproduces the Excess-3 table and Example 3:

OUTPUT
BCD 0000 -> XS-3 0011
BCD 0001 -> XS-3 0100
BCD 0010 -> XS-3 0101
BCD 0011 -> XS-3 0110
BCD 0100 -> XS-3 0111
BCD 0101 -> XS-3 1000
BCD 0110 -> XS-3 1001
BCD 0111 -> XS-3 1010
BCD 1000 -> XS-3 1011
BCD 1001 -> XS-3 1100
message 101  P=0  received ok:      error=0
message 101  P=0  MSB flipped:      error=1
💡 Tip

Key points to remember

  • Code converter = truth table + don't-cares for unused input codes + K-maps.
  • BCD to Excess-3: w = A + BC + BD, x = B'C + B'D + BC'D', y = (C oplus D)', z = D'.
  • Binary to Gray: parallel XORs; Gray to binary: chained XORs.
  • Even-parity generator P = x oplus y oplus z; checker C = x oplus y oplus z oplus P, C = 1 means error.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Design a BCD to Excess-3 code converter. Give the truth table, K-maps and logic diagram. (10 Marks)
  • Write: the 10-row table with don't-cares, the four maps, the four equations, the circuit.
  • Q2. Design a 4-bit binary to Gray code converter and a Gray to binary converter. (10 Marks)
  • Q3. Design a 3-bit even parity generator and the corresponding 4-bit parity checker. (5 Marks)
  • Q4. The message 101 is sent with even parity and its first bit is corrupted. Show how the checker detects it. (2.5 Marks)
  • Q5. Design an Excess-3 to BCD converter. (5 Marks)