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.

A Chip-Select Decoder with Its Enable Polarity WrongOne bus cycle per step. The outputs are active-low: the selected device sees a 0.
0123reqaddr025cs_n wanted11111111111110111111111111011111cs_n got11111110111111111111101111111111idle, but device 0 selectedrequest, no device
Scroll horizontally ↔
  1. Cycle 0

    The CPU is idle (req = 0). The wrong wiring enables the decoder, so device 0 sees a 0 and wakes up.

  2. Cycle 1

    The CPU requests device 2. The enable is now high, which turns the decoder off: no device answers.

  3. Cycle 2

    Idle again, and device 2 is selected because the address still points at it.

  4. 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 2-to-4 Decoder
A 2-to-4 Decoderg0 · NOTn0=0g1 · NOTn1=0g2 · ANDy0=0g3 · ANDy1=0g4 · ANDy2=0g5 · ANDy3=1a0=1a1=1
a0=1 a1=1
g0 · NOTa0=1 → n0=0
g1 · NOTa1=1 → n1=0
g2 · ANDn1=0, n0=0 → y0=0
g3 · ANDn1=0, a0=1 → y1=0
g4 · ANDa1=1, n0=0 → y2=0
g5 · ANDa1=1, a0=1 → y3=1
Read the connections
GateKindInputsOutput
g0NOTa0n0
g1NOTa1n1
g2ANDn1, n0y0
g3ANDn1, a0y1
g4ANDa1, n0y2
g5ANDa1, a0y3

Outputs: y3, y2, y1, y0. MUX inputs: d0, d1, select. Latch/flip-flop inputs: data, enable/clock. Unknown values are marked x; high impedance is folded into x in this model.

Gate delay 17. Settled after 17 gate delays from t = 0.

Output values at each gate delay01234567891011121314151617y3x00000000000000011y2x00000000000111110y1x00000111110000000y0xx1111100000000000
Scroll horizontally ↔
Every computed frame
Timey3y2y1y0
0xxxx
1000x
20001
30001
40001
50001
60011
70010
80010
90010
100010
110000
120100
130100
140100
150100
161100
171000

  1. Step 2 · a = 00. Both inverters give 1, so only the AND gate for y0 sees two 1s. y = 0001.
  2. Step 7 · a = 01 at time 5. y0 falls and y1 rises two gate delays later. Exactly one output is 1 again.
  3. Step 12 · a = 10 at time 10. Now y2 is the one output at 1.
  4. Step 17 · a = 11 at time 15. y3 = a1·a0. Click the input pins to try any value.

The decoder2 preset: two inverters and four AND gates, one per minterm.

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_nay_n (active-low)Selected
1any11111111none
0011111110output 0
0211111011output 2
0511011111output 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 labCellsLogic levels
2-to-4, no enable41
3-to-8 with enable142
4-to-16, no enable242

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.

A 4-to-16 Decoder from Two 3-to-8 DecodersArrows show signals. a[2:0] reaches both decoders. a[3] enables one and, through an inverter, disables the other.
  • I/O
  • Datapath
  • Data
  • Control
CallValueReturnWaiting
A 4-to-16 decoder from two 3-to-8 decodersArrows show signals. a[2:0] reaches both decoders. a[3] enables one and, through an inverter, disables the other. io a[2:0] low bits io a[3] top bit datapath Inverter a[3]′ datapath 3-to-8 low enabled when a[3] = 0 datapath 3-to-8 high enabled when a[3] = 1 io y[7:0] outputs 0 to 7 io y[15:8] outputs 8 to 15 aaa[3]enenone-hotone-hot
  1. io a[2:0]low bits
    • a → 3-to-8 low
    • a → 3-to-8 high
  2. io a[3]top bit
    • a[3] → Inverter
    • en → 3-to-8 high
  3. datapath 3-to-8 lowenabled when a[3] = 0
    • one-hot → y[7:0]
  4. datapath 3-to-8 highenabled when a[3] = 1
    • one-hot → y[15:8]
  5. datapath Invertera[3]′
    • en → 3-to-8 low
  6. io y[15:8]outputs 8 to 15
  7. io y[7:0]outputs 0 to 7
  1. Address 11 = 1011

    The low bits 011 go to both decoders. The top bit is 1.

    Follow the calls, values and waits
    1. data 011: a[2:0] → 3-to-8 low
    2. data 011: a[2:0] → 3-to-8 high
  2. 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
    1. data 1: a[3] → 3-to-8 high
    2. data en = 0: Inverter → 3-to-8 low
  3. 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
    1. 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 binRowd (difference)bout (borrow out)
0 0 0000
0 0 1111
0 1 0211
0 1 1301
1 0 0410
1 0 1500
1 1 0600
1 1 1711

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.

A 2-to-4 Decoder Used as a DemultiplexerOne step per bit. sel stays at 2 for four bits, then moves to 0.
01234sel20datay0000010000000001
Scroll horizontally ↔
  1. Cycle 0

    data = 0 turns the decoder off, so every output is 0.

  2. Cycle 1

    data = 1 enables it, and output 2 copies the 1.

  3. Cycle 3

    Output 2 has followed the stream 0, 1, 1, 0. The other outputs stayed 0.

  4. 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
  1. 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.

  2. 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.

  3. 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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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 wrong

Original 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

Spot the bugEach option is the body of an always_comb block of a 2-to-4 decoder with an active-high enable. The bench tries all eight rows. Which body fails?
ConceptAddress bits a[1] and a[0] of a 2-to-4 decoder are swapped on the board. Which addresses still select the right output?
ConceptWhat does a 1-to-4 demultiplexer do when its data input is 0?
ConceptHow many 3-to-8 decoders, plus one 2-to-4 decoder on their enables, make a 5-to-32 decoder?
Predict the outputA 3-to-8 decoder with active-low outputs is enabled with a = 6. Predict the exact line.
module tb;
  logic [2:0] a = 3'd6;
  initial $display("%b", ~(8'b1 << a));
endmodule

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.

Practice

Report a problem with this page