Decoders and Demultiplexers
Use a decoder as a minterm generator: select one device, build any function with OR gates, and route data as a demultiplexer.
You Will Be Able To
- Describe a decoder as a generator of every minterm of its inputs.
- Use enables and active-low outputs, and cascade two decoders into a bigger one.
- Build a function from one decoder and OR gates.
- Use a decoder as a demultiplexer by driving the enable with data.
- Lesson
- G4.3
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A board selects one of eight devices with a 3-to-8 decoder. Its enable is active-low, but the request from the CPU is active-high. The designer wired it straight in. Now a device is selected while the CPU is idle, and none during a request.
- Cycle 0
The CPU is idle (req = 0). The wrong wiring enables the decoder, so device 0 sees a 0 and wakes up.
- Cycle 1
The CPU requests device 2. The enable is now high, which turns the decoder off: no device answers.
- Cycle 2
Idle again, and device 2 is selected because the address still points at it.
- Cycle 3
A request for device 5 selects nothing. Every access fails, in a pattern that looks random.
Measured on the simulator: the hook bench runs a correct and a miswired copy side by side.
A Decoder Makes Every Minterm
An n-to-2^n decoder has n inputs and one output per input value. Output k is 1 when the input equals k. So each output is one minterm of the inputs: the 2-to-4 decoder gives a′b′, a′b, ab′ and ab.
A one-hot output is a word with exactly one 1. A decoder turns a binary number into a one-hot word. Memories use one to pick a row, and buses use one to pick a device.
Enables and Active-Low Outputs
An enable input turns the whole decoder on or off. When it is off, no output is selected. Many decoder parts have active-low outputs: the selected output is 0 and all others are 1. A name ending in _n marks an active-low signal.
| en_n | a | y_n (active-low) | Selected |
|---|---|---|---|
| 1 | any | 11111111 | none |
| 0 | 0 | 11111110 | output 0 |
| 0 | 2 | 11111011 | output 2 |
| 0 | 5 | 11011111 | output 5 |
A 3-to-8 decoder with an active-low enable and active-low outputs, as in the hook bench. Measured on the simulator.
Many memory and peripheral chips have active-low chip-select pins. An active-low decoder drives them directly, with no inverters in between.
| Decoder, written as in the lab | Cells | Logic levels |
|---|---|---|
| 2-to-4, no enable | 4 | 1 |
| 3-to-8 with enable | 14 | 2 |
| 4-to-16, no enable | 24 | 2 |
Measured on this site's synthesis flow. The cell count grows with the number of outputs; the depth stays small.
Cascading Two Decoders
Two 3-to-8 decoders make a 4-to-16. The low three address bits go to both. The top bit goes to the enables: one decoder is on when it is 0, the other when it is 1.
- I/O
- Datapath
- Data
- Control
- Address 11 = 1011
The low bits 011 go to both decoders. The top bit is 1.
Follow the calls, values and waits
- data 011: a[2:0] → 3-to-8 low
- data 011: a[2:0] → 3-to-8 high
- Only the high decoder is on
a[3] = 1 enables the high decoder. The inverter gives 0 to the low decoder, which turns off.
Follow the calls, values and waits
- data 1: a[3] → 3-to-8 high
- data en = 0: Inverter → 3-to-8 low
- Output 8 + 3
The high decoder raises its output 3, which is y[11]. All of y[7:0] stay 0.
Follow the calls, values and waits
- data y[11] = 1: 3-to-8 high → y[15:8]
Read the diagram as text
- a[2:0]: low bits
- a[3]: top bit
- Inverter: a[3]′
- 3-to-8 low: enabled when a[3] = 0
- 3-to-8 high: enabled when a[3] = 1
- y[7:0]: outputs 0 to 7
- y[15:8]: outputs 8 to 15
- a[2:0] → 3-to-8 low: a
- a[2:0] → 3-to-8 high: a
- a[3] → Inverter: a[3]
- Inverter → 3-to-8 low: en
- a[3] → 3-to-8 high: en
- 3-to-8 low → y[7:0]: one-hot
- 3-to-8 high → y[15:8]: one-hot
Two 3-to-8 decoders and an inverter make a 4-to-16. Four 4-to-16s and a 2-to-4 on the enables make a 6-to-64.
A Decoder Plus OR Gates Makes Any Function
Every output of a decoder is one minterm. A sum of minterms is an OR of some outputs. So one decoder and one OR gate per output build any function of its inputs, with no minimization.
| x y bin | Row | d (difference) | bout (borrow out) |
|---|---|---|---|
| 0 0 0 | 0 | 0 | 0 |
| 0 0 1 | 1 | 1 | 1 |
| 0 1 0 | 2 | 1 | 1 |
| 0 1 1 | 3 | 0 | 1 |
| 1 0 0 | 4 | 1 | 0 |
| 1 0 1 | 5 | 0 | 0 |
| 1 1 0 | 6 | 0 | 0 |
| 1 1 1 | 7 | 1 | 1 |
A full subtractor computes x − y − bin. d = Σm(1, 2, 4, 7) and bout = Σm(1, 2, 3, 7).
The lab below wires these two sums to a 3-to-8 decoder. The outputs share the decoder. Each output costs only its OR gate.
This pays off when a block has many outputs over the same few inputs. With active-low outputs, use NAND gates in place of OR: a NAND gives 1 when any of its inputs is 0.
A Demultiplexer Is a Decoder with Data on the Enable
A demultiplexer (demux) is the reverse of a mux. It sends one data input to the output that the select names. All other outputs stay 0.
A decoder with an enable already does this. Put the select on the address inputs and the data on the enable. When the data is 1, the selected output is 1. When it is 0, every output is 0.
- Cycle 0
data = 0 turns the decoder off, so every output is 0.
- Cycle 1
data = 1 enables it, and output 2 copies the 1.
- Cycle 3
Output 2 has followed the stream 0, 1, 1, 0. The other outputs stayed 0.
- Cycle 4
sel moves to 0. The next bit appears on output 0.
Measured on the simulator. y is printed as y3 y2 y1 y0.
Asked in interviews
How do you build any function from a decoder?
Each decoder output is one minterm of the inputs. OR together the outputs of the minterms where the function is 1. With active-low outputs, use a NAND instead.
What is the difference between a decoder and a demultiplexer?
They are the same circuit used two ways. A decoder turns a number into one active output. A demux routes a data input to one output; the data drives the decoder's enable.
How do you make a 4-to-16 decoder from 3-to-8 decoders?
Feed the low three bits to two 3-to-8 decoders. Use the top bit to enable one of them and its complement to enable the other.
Try It
The bench works out x − y − bin itself and compares it with {bout, d}. All eight rows agree.
Press Synthesize. The decoder and the two four-input ORs shrink to 5 cells in 3 levels. The tool found that d is an XOR of three inputs.
Give fsub two other functions. Make d = Σm(1, 2, 4), exactly one input at 1, and bout = Σm(7), all three at 1. In the bench, set diff to 2 * (x & y & bin) + (x + y + bin == 1).
Tie the enable to 1'b0 and run. Every decoder output stays 0, so d and bout are 0 in every row.
Synthesize the design to measure its cells and inspect the mapped gates.
x=0 y=0 bin=0 bout=0 d=0 x=0 y=0 bin=1 bout=1 d=1 x=0 y=1 bin=0 bout=1 d=1 x=0 y=1 bin=1 bout=1 d=0 x=1 y=0 bin=0 bout=0 d=1 x=1 y=0 bin=1 bout=0 d=0 x=1 y=1 bin=0 bout=0 d=0 x=1 y=1 bin=1 bout=1 d=1 0 of 8 rows wrongOriginal design · 5 cells · 0 flip-flops · 0 latches · 3 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
// tb.sv: all eight rows, compared with x - y - bin worked out by the bench (read-only).
module tb;
logic x, y, bin, d, bout;
int wrong = 0;
fsub dut (.x(x), .y(y), .bin(bin), .d(d), .bout(bout));
initial begin
for (int m = 0; m < 8; m++) begin
int diff;
{x, y, bin} = m[2:0];
#1;
diff = int'(x) - int'(y) - int'(bin); // from -2 to 1
if ({bout, d} !== 2'(diff)) wrong++;
$display("x=%b y=%b bin=%b bout=%b d=%b", x, y, bin, bout, d);
end
$display("%0d of 8 rows wrong", wrong);
end
endmodule
Check Yourself
Common Mistakes
Wrong Drive an active-low enable with an active-high signal.
The decoder is then on when it should be off, and off when it should be on. The hook shows a device selected while idle.
Fix Match the polarity: invert the request, or pick a decoder whose enable has the polarity you have.
Wrong Read active-low outputs as active-high.
On an active-low decoder the selected output is the one 0. An OR of those outputs is 1 almost all the time.
Fix Combine active-low minterms with a NAND gate: it gives 1 when any chosen output is 0.
Wrong Wire the address bits to the decoder in the wrong order.
Address 1 (001) then selects output 4 (100). Addresses 0, 2, 5 and 7 still look right, so a short test misses it.
Fix Keep the most significant bit on the most significant input, and test every address.
Wrong Forget the enable when you cascade decoders.
Without it, both decoders drive an output for every address, so two devices are selected at once.
Fix Use the top address bit on the enables: one decoder for each value of that bit.
Remember
- An n-to-2^n decoder gives one output per input value: every minterm, one-hot.
- An enable turns the whole decoder off; active-low outputs select with a 0.
- Cascade decoders by putting the top address bits on the enables.
- One decoder plus an OR (or NAND for active-low) per output builds any function.
- A demultiplexer is a decoder with the data on its enable.