Overview
Encoders and decoders are complementary circuits for code conversion. An encoder compresses many input lines into a smaller binary code. A decoder expands a binary code back into individual output lines.
Interactive Priority Encoder (4-to-2)
Click any input to toggle it ON. If multiple inputs are HIGH, the highest-numbered active input takes priority.
4-to-2 Priority Encoder Truth Table
| I3 | I2 | I1 | I0 | 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 |
Verilog: Priority Encoder
module priority_enc4 (
input wire [3:0] in,
output reg [1:0] y,
output reg valid
);
always_comb begin
valid = 1'b1;
casez (in) // casez: 'z' matches X and Z (don't care)
4'b1???: {y, valid} = {2'd3, 1'b1};
4'b01??: {y, valid} = {2'd2, 1'b1};
4'b001?: {y, valid} = {2'd1, 1'b1};
4'b0001: {y, valid} = {2'd0, 1'b1};
default: {y, valid} = {2'd0, 1'b0}; // no input active
endcase
end
endmodule
Interactive 2-to-4 Decoder
Select the 2-bit input (A1, A0) to activate the corresponding output:
2-to-4 Decoder Truth Table
| A1 | A0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
Verilog: 2-to-4 Decoder (with enable)
module dec2to4 (
input wire [1:0] a,
input wire en, // active-high enable
output reg [3:0] y
);
always_comb begin
y = 4'b0;
if (en) y = 4'b1 << a; // shift 1 to position 'a'
end
endmodule
// Active-low version (like 74HC139):
// assign y_n = ~(4'b1 << a) | {4{~en}};
Verilog: 3-to-8 Decoder (74HC138 equivalent)
module dec3to8 (
input wire [2:0] a,
input wire g1, // active-high enable
input wire g2a_n, g2b_n, // active-low enables
output reg [7:0] y_n // active-low outputs
);
wire en = g1 & ~g2a_n & ~g2b_n;
always_comb
y_n = en ? ~(8'b1 << a) : 8'hFF;
endmodule
BCD-to-7-Segment Decoder
Click a digit to see which segments illuminate on the 7-segment display.
| Digit | BCD | a | b | c | d | e | f | g |
|---|
Verilog: BCD-to-7-Segment
module bcd_to_7seg (
input wire [3:0] bcd,
output reg [6:0] seg // {a,b,c,d,e,f,g} active-high
);
always_comb
case (bcd)
// abcdefg
4'd0: seg = 7'b1111110;
4'd1: seg = 7'b0110000;
4'd2: seg = 7'b1101101;
4'd3: seg = 7'b1111001;
4'd4: seg = 7'b0110011;
4'd5: seg = 7'b1011011;
4'd6: seg = 7'b1011111;
4'd7: seg = 7'b1110000;
4'd8: seg = 7'b1111111;
4'd9: seg = 7'b1111011;
default: seg = 7'b0000000; // blank for invalid BCD
endcase
endmodule
Binary-to-Gray Code Converter
Gray code ensures adjacent values differ by exactly one bit — critical for rotary encoders and asynchronous FIFO pointers.
Verilog: Binary-to-Gray & Gray-to-Binary
// Binary to Gray: G[i] = B[i] ^ B[i+1] (G[MSB] = B[MSB])
module bin2gray #(parameter N = 8) (
input wire [N-1:0] bin,
output wire [N-1:0] gray
);
assign gray = bin ^ (bin >> 1);
endmodule
// Gray to Binary: B[MSB] = G[MSB]; B[i] = B[i+1] ^ G[i]
module gray2bin #(parameter N = 8) (
input wire [N-1:0] gray,
output reg [N-1:0] bin
);
integer i;
always_comb begin
bin[N-1] = gray[N-1];
for (i = N-2; i >= 0; i--)
bin[i] = bin[i+1] ^ gray[i];
end
endmodule
Decoder as Minterm Generator
A 2-to-4 decoder simultaneously generates all 4 minterms of 2 variables. Any 2-variable Boolean function can be implemented by ORing the appropriate outputs:
| Function | Boolean | Decoder implementation |
|---|---|---|
| AND | A·B | Y3 alone |
| OR | A+B | Y1 OR Y2 OR Y3 |
| XOR | A⊕B | Y1 OR Y2 |
| XNOR | A⊙B | Y0 OR Y3 |
| NAND | Ā+B̄ | Y0 OR Y1 OR Y2 |
VLSI Applications
Interrupt Controller (Priority Encoder)
A CPU interrupt controller (like the x86 PIC 8259A or ARM GIC) accepts interrupt request lines from multiple peripherals. A priority encoder resolves simultaneous requests: it outputs the highest-priority active IRQ number and a valid signal to the CPU. The CPU uses the encoded output as an index into the interrupt vector table.
Memory Address Decoder
The memory controller uses a decoder to select which DRAM bank is active based on high-order address bits. A 3-to-8 decoder can manage 8 separate 512 MB banks to compose a 4 GB memory space. The enable input of the decoder is driven by a chip-select logic that ensures only one bank is active at a time, preventing bus contention.
One-Hot to Binary (Encoder in Arbiters)
Round-robin arbiters grant access to one requester at a time, producing a one-hot grant signal. A priority encoder converts this one-hot grant back to a binary index, used to steer the winning requester's data onto the shared bus.