Sub topic 9
Decoders, encoders and priority encoders
This chapter assumes you can read a truth table and write a minterm. It also assumes you know that a function in canonical SOP form is the OR of its minterms. Decoders and encoders are the standard "address to one-hot" and "one-hot to address" blocks. GATE questions on them ask you to build functions, cascade small parts into bigger ones, and trace outputs when several inputs are active.
Decoders
An n-to-2^n decoder takes an n-bit input and drives exactly one of 2^n output lines. Each output is one minterm of the inputs. With an enable input EN, the output for input value i is Y_i = EN · m_i. When EN = 0, every output is 0.
| EN | A1 | A0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|---|
| 0 | X | X | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 | 1 |
Each output is a single AND gate (with inverted inputs where the minterm needs them). So Y2 = EN · A1 · A0'.
Many real decoders, such as the 74138, have active-low outputs. The selected output goes to 0 and all others stay at 1. In that case Y_i' = (EN · m_i)', which is a NAND gate per output.
Verilog model
module decoder3to8(input en, input [2:0] a, output [7:0] y);
assign y = en ? (8'b1 << a) : 8'b0;
endmoduleImplementing functions with a decoder
Because the outputs are minterms, any function in canonical SOP form needs only one OR gate after the decoder.
- With an active-high decoder,
F = Σm(i, j, k)isY_i + Y_j + Y_k. - With an active-low decoder, use a NAND gate on the active-low outputs. By De Morgan,
(Y_i' · Y_j' · Y_k')' = Y_i + Y_j + Y_k.
A full adder is a good example. With inputs A, B, Cin as the decoder address:
Sum = Σm(1, 2, 4, 7)Cout = Σm(3, 5, 6, 7)
So a 3-to-8 decoder plus two 4-input OR gates (or NAND gates, if the decoder is active-low) builds a full adder.
If a function has more than half of the minterms, build its complement instead. For an active-high decoder, F = (Σ of the missing minterms)', which is a NOR gate. This gives a smaller gate.
Building larger decoders from smaller ones
Use the enable input. A 4-to-16 decoder can be made from two 3-to-8 decoders.
- The low three address bits A2..A0 go to both decoders.
- A3 selects between them.
- The first decoder is enabled by A3', so it produces Y0 to Y7.
- The second decoder is enabled by A3, so it produces Y8 to Y15.
For a two-level tree, such as a 4-to-16 decoder from 2-to-4 decoders, use the high bits to select. One 2-to-4 decoder on A3 A2 drives the enables of four 2-to-4 decoders on A1 A0. That is 1 + 4 = 5 decoders. In general, an n-to-2^n decoder built from k-to-2^k decoders in a tree needs the first stage plus one decoder per first-stage output.
Decoder as demultiplexer
A decoder with enable is also a 1-to-2^n demultiplexer. Treat EN as the data input and the address lines as the select lines. The data bit then appears only on the output chosen by the address.
Encoders
An encoder does the reverse of a decoder. A 2^n-to-n encoder has 2^n inputs and n outputs. It assumes that exactly one input is 1 and outputs that input's index in binary.
For an 8-to-3 (octal-to-binary) encoder, each output bit is the OR of the inputs whose index has that bit set:
A2 = D4 + D5 + D6 + D7A1 = D2 + D3 + D6 + D7A0 = D1 + D3 + D5 + D7
A plain encoder has two flaws:
- If two inputs are 1 at once, the output ORs their codes into a wrong value. With D3 and D4 both high, the result is 111, which is the code for D7.
- All inputs 0 gives output 000, which is the same as "D0 is active".
Priority encoders
A priority encoder removes the first flaw by defining what happens when several inputs are active: the highest-numbered input wins. It also adds a valid output V that removes the second flaw. V = 1 when at least one input is active.
| D3 | D2 | D1 | D0 | Y1 | Y0 | V |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | X | X | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | X | 0 | 1 | 1 |
| 0 | 1 | X | X | 1 | 0 | 1 |
| 1 | X | X | X | 1 | 1 | 1 |
An X in an input column means "don't care". Those rows each stand for several input combinations. The Boolean equations come from this table:
Y1 = D3 + D2Y0 = D3 + D2' · D1V = D3 + D2 + D1 + D0
Check Y0: if D3 = 1, Y0 = 1. If D3 = 0 and D2 = 1, the term D2' · D1 is 0, so Y0 = 0. If D3 = D2 = 0, Y0 = D1. Each case matches the table.
module prio4(input [3:0] d, output reg [1:0] y, output reg v);
always @(*) begin
v = 1'b1;
if (d[3]) y = 2'd3;
else if (d[2]) y = 2'd2;
else if (d[1]) y = 2'd1;
else if (d[0]) y = 2'd0;
else begin
y = 2'd0;
v = 1'b0;
end
end
endmoduleThe if ... else if chain expresses priority directly. The first true condition wins, which is the behavior of the table.
Common errors
| Error | Correction |
|---|---|
| Forgetting that a 74138-style decoder has active-low outputs | Use NAND, not OR, to combine outputs into an SOP function |
| Using a plain encoder with several active inputs | Use a priority encoder, or guarantee one-hot inputs |
| Treating output 00 of a priority encoder as "D0 active" | Read V first |
| Counting decoders wrongly in a tree | Count one first-stage decoder plus one per first-stage output |
| Wiring the enable of a cascaded decoder in the wrong polarity | The first half is enabled by A_msb', the second half by A_msb |
Exercise
A 3-to-8 decoder has active-low outputs Y0' to Y7'. Which single gate on the outputs implements F(A, B, C) = Σm(0, 2, 5, 7)? Give its type and number of inputs.
Show answer
A 4-input NAND gate fed by Y0', Y2', Y5' and Y7'. By De Morgan, NAND(Y0', Y2', Y5', Y7') = Y0 + Y2 + Y5 + Y7, which is Σm(0, 2, 5, 7).
Exercise
How many 3-to-8 decoders (with enable) are needed to build a 5-to-32 decoder, if a 2-to-4 decoder is allowed for the first stage?
Show answer
Five parts: one 2-to-4 decoder on the top two address bits, whose four outputs drive the enables of four 3-to-8 decoders on the low three bits. That gives 4 × 8 = 32 outputs. If only 3-to-8 decoders are available, use a fifth one with a spare address input tied to 0 (or EN) for the first stage, so the total is still 1 + 4 = 5 decoders.
Exercise
A 4-input priority encoder (D3 highest) receives D3 D2 D1 D0 = 0110. What are Y1, Y0 and V?
Show answer
D2 is the highest active input, so Y1 Y0 = 10 and V = 1. Check with the equations: Y1 = D3 + D2 = 1 and Y0 = D3 + D2'·D1 = 0 + 0·1 = 0.