Sub topic 8
Multiplexers and implementing functions with them
This chapter assumes you can read a truth table, write a function as a sum of minterms (covered in the chapter on minterms and maxterms), and use Boolean algebra to simplify an expression. It covers what a multiplexer is, how to build larger ones, and how to implement any Boolean function with one. That last skill is a frequent GATE question.
What a multiplexer does
A multiplexer (mux) is a combinational circuit that copies one of several data inputs to a single output. A set of select lines chooses which input is copied. A mux with n select lines has 2^n data inputs, so it is called a 2^n-to-1 mux.
For a 4-to-1 mux with data inputs I0 to I3 and selects S1 (most significant) and S0:
Y = S1'·S0'·I0 + S1'·S0·I1 + S1·S0'·I2 + S1·S0·I3
Each term is a minterm of the select lines ANDed with one data input. This form is the basis of every implementation technique in this chapter.
| S1 | S0 | Y |
|---|---|---|
| 0 | 0 | I0 |
| 0 | 1 | I1 |
| 1 | 0 | I2 |
| 1 | 1 | I3 |
In Verilog, the select value can index the data vector directly:
module mux4(input [3:0] d, input [1:0] s, output y);
assign y = d[s];
endmoduleHere d[0] is I0 and d[3] is I3. The select value {S1, S0} is read as an unsigned number, so S1 is the high bit.
Building larger multiplexers
You can build a large mux from smaller ones arranged as a tree. The first level uses the low-order select bits. The last level uses the high-order select bits.
- An 8-to-1 mux can be built from two 4-to-1 muxes and one 2-to-1 mux. The two 4-to-1 muxes share S1 and S0. Their outputs feed the 2-to-1 mux, which uses S2.
- A 16-to-1 mux can be built from five 4-to-1 muxes: four in the first level and one in the second.
For a tree built from k-to-1 muxes, the number of muxes needed for an N-input mux is (N − 1) / (k − 1).
| Target | Built from | Muxes needed |
|---|---|---|
| 8-to-1 | 2-to-1 | 7 |
| 8-to-1 | 4-to-1 | 2 + 1 (one 2-to-1) |
| 16-to-1 | 4-to-1 | 5 |
| 64-to-1 | 4-to-1 | 21 |
Implementing a function with a 2^n-to-1 mux
If a function has n variables, use an n-select mux of size 2^n. Connect the variables to the select lines. Then tie each data input to the function's value in that row of the truth table, which is either 0 or 1.
The mux equation already sums the minterms of the select lines. So data input Ik is 1 exactly when minterm mk is in the function. No other gates are needed.
Implementing an n-variable function with a 2^(n−1)-to-1 mux
This is the more useful technique and the one GATE tests most often. You use n − 1 variables as selects and feed the last variable, or its complement, into the data inputs. This halves the mux size.
The procedure:
- Choose which n − 1 variables are the selects. The remaining variable is the data variable, call it X.
- List the truth table. Group rows into pairs that share the same select values, one with X = 0 and one with X = 1.
- For each pair, read the output as X = 0 and X = 1, then pick the data input:
- 0 and 0 gives constant 0.
- 1 and 1 gives constant 1.
- 0 and 1 gives X.
- 1 and 0 gives X'.
- Wire these values to the data inputs in select order.
Worked example
Implement F(A, B, C) = Σm(1, 2, 6, 7) with a 4-to-1 mux. Use A as S1, B as S0, and C as the data variable.
| A B | Minterms (C=0, C=1) | Output pair | Data input |
|---|---|---|---|
| 0 0 | m0, m1 | 0, 1 | C |
| 0 1 | m2, m3 | 1, 0 | C' |
| 1 0 | m4, m5 | 0, 0 | 0 |
| 1 1 | m6, m7 | 1, 1 | 1 |
So I0 = C, I1 = C', I2 = 0 and I3 = 1. The one inverter on C is the only extra gate.
module f_via_mux(input a, input b, input c, output f);
// d[0]=c, d[1]=~c, d[2]=0, d[3]=1
wire [3:0] d = {1'b1, 1'b0, ~c, c};
assign f = d[{a, b}];
endmoduleChoosing the select variables
Any variable can be the data variable. Different choices give different data inputs, and some choices need fewer inverters. A question may fix the connections, for example "A and B on the selects." If it doesn't, try the choices and pick the one with the simplest data inputs.
If the select order is permuted, for example B as S1 and A as S0, the rows are paired and ordered differently. Always recompute the table for the exact connection given.
Going further: smaller muxes with more logic on the data inputs
You can also feed expressions of several variables into the data inputs. For example, a 4-variable function can use a 4-to-1 mux if the data inputs are functions of two variables, such as AND, OR or XOR gates. This saves mux size at the cost of extra gates. Questions that ask for the "minimum number of 2-to-1 muxes" often reward this approach.
Example: a full adder from muxes
Use a and b as selects and cin as the data variable.
| a b | Sum (cin=0, 1) | Sum input | Cout (cin=0, 1) | Cout input |
|---|---|---|---|---|
| 0 0 | 0, 1 | cin | 0, 0 | 0 |
| 0 1 | 1, 0 | cin' | 0, 1 | cin |
| 1 0 | 1, 0 | cin' | 0, 1 | cin |
| 1 1 | 0, 1 | cin | 1, 1 | 1 |
Two 4-to-1 muxes sharing the selects a and b produce both outputs.
Muxes as universal building blocks
A 2-to-1 mux with Y = S'·I0 + S·I1 can produce basic gates when you tie inputs to constants:
- NOT: S = A, I0 = 1, I1 = 0.
- AND: S = A, I0 = 0, I1 = B.
- OR: S = A, I0 = B, I1 = 1.
Since NOT and AND together are complete, the 2-to-1 mux is a universal element when constants are available.
The demultiplexer
A demultiplexer (demux) does the reverse. It sends one data input to one of 2^n outputs chosen by the selects, and the other outputs stay at 0. A 1-to-2^n demux is the same circuit as an n-to-2^n decoder with its enable input used as the data input. The next chapter covers decoders.
Common errors
- Reversing the select order. S1 is the most significant bit. Swapping it reverses the data input numbering.
- Mixing up the pair when the data variable is not the least significant bit. The two rows in a pair are not adjacent in the truth table, so list them carefully.
- Forgetting that X' needs an inverter. Count it when a question asks for total gates.
- Using the wrong tree formula. With k-to-1 muxes the count is (N − 1) / (k − 1), and it applies only when N is a power of k. Otherwise you need mixed sizes, as in 8-to-1 from 4-to-1 and 2-to-1.
Exercise
Implement F(A,B,C,D) = Σm(0,1,5,6,7,12,15) with an 8-to-1 mux, using A, B, C as S2, S1, S0. What are I0 to I7?
Show answer
Pair the minterms by ABC. ABC=000 has m0, m1 giving 1,1, so I0 = 1. ABC=001 has m2, m3 giving 0,0, so I1 = 0. ABC=010 has m4, m5 giving 0,1, so I2 = D. ABC=011 has m6, m7 giving 1,1, so I3 = 1. ABC=100 has m8, m9 giving 0,0, so I4 = 0. ABC=101 has m10, m11 giving 0,0, so I5 = 0. ABC=110 has m12, m13 giving 1,0, so I6 = D'. ABC=111 has m14, m15 giving 0,1, so I7 = D.
Exercise
How many 4-to-1 multiplexers are needed to build a 64-to-1 multiplexer?
Show answer
(64 − 1) / (4 − 1) = 21. The tree has 16 muxes in the first level, 4 in the second and 1 in the third.
Exercise
Show how a 2-to-1 mux implements XOR of A and B. What extra gate is needed?
Show answer
Let S = A, I0 = B and I1 = B'. Then Y = A'·B + A·B' = A XOR B. One inverter is needed to produce B'.