Sub topic 9
Decoders, Encoders and Priority Encoders
Assume you know binary representation, Boolean operators, and basic combinational logic. A decoder converts an \(n\)-bit binary input into one of \(2^n\) active output lines. An encoder performs the reverse operation: it converts an active input line into its binary index. A priority encoder also handles cases where several input lines are active.
Decoders
A 2-to-4 decoder has two input bits, \(A1A0\), and four outputs. For each input combination, exactly one output is active.
| \(A_1A_0\) | \(Y_0\) | \(Y_1\) | \(Y_2\) | \(Y_3\) |
|---|---|---|---|---|
| 00 | 1 | 0 | 0 | 0 |
| 01 | 0 | 1 | 0 | 0 |
| 10 | 0 | 0 | 1 | 0 |
| 11 | 0 | 0 | 0 | 1 |
For active-high outputs:
\[ Y0=\overline{A1}\,\overline{A0},\quad Y1=\overline{A1}A0,\quad Y2=A1\overline{A0},\quad Y3=A1A0 \]
Each output is one minterm, so a decoder can implement a Boolean function by ORing the selected output lines. For example, \(F(A1,A0)=\Sigma m(1,3)\) is implemented as \(F=Y1+Y3\).
module decoder2to4(
input wire [1:0] a,
input wire enable,
output wire [3:0] y
);
assign y = enable ? (4'b0001 << a) : 4'b0000;
endmoduleSome decoders use active-low outputs. In that case, the selected output is 0 and all other outputs are 1. Always check the symbol or truth table before writing equations.
Encoders
An ordinary 4-to-2 encoder assumes exactly one input is active. Its inputs \(D0\) through \(D3\) represent positions 0 through 3. The binary output is:
\[ B1=D2+D3,\qquad B0=D1+D3 \]
The equations are valid only under the one-hot assumption. If \(D1\) and \(D2\) are both 1, the output does not identify a unique input.
A valid signal is often added:
\[ V=D0+D1+D2+D3 \]
This distinguishes “no input is active” from the code for input 0.
Priority encoders
A priority encoder defines an answer when multiple inputs are active. Suppose \(D3\) has the highest priority and \(D0\) the lowest. The encoder returns 3 whenever \(D3=1\), regardless of the other inputs. If \(D3=0\), it checks \(D2\), then \(D1\), then \(D_0\).
module priority_encoder4(
input wire [3:0] d,
output reg [1:0] code,
output reg valid
);
always @(*) begin
valid = 1'b1;
casex (d)
4'b1xxx: code = 2'd3;
4'b01xx: code = 2'd2;
4'b001x: code = 2'd1;
4'b0001: code = 2'd0;
default: begin
code = 2'd0;
valid = 1'b0;
end
endcase
end
endmoduleExercise
A 3-to-8 decoder has active-high outputs. Which output is active for input 101, and how can the decoder implement F(A,B,C) = Σm(1,5,7)?
Show answer
Output Y5 is active for input 101. Connect Y1, Y5, and Y7 to a three-input OR gate to produce F.