Draft preview. Lessons are being written and reviewed.

Digital Design / Module 2: Combinational building blocks

6. Multiplexers

Use the mux as hardware's if-else.

Lecture coming soon

See it move

The idea

A multiplexer is hardware's if-else. Select lines choose which input appears at the output. A 2^N-to-1 mux has N select lines, so a 4:1 mux has two.

In software, an if runs only one branch. In hardware, both inputs exist and are computed all the time, and the mux just chooses between them. Picture this every time you write a conditional in RTL.

assign y = sel ? a : b;       // 2:1 mux

always @(*) begin             // 4:1 mux
  case (s)
    2'b00:   y = d0;
    2'b01:   y = d1;
    2'b10:   y = d2;
    default: y = d3;
  endcase
end

A mux can implement any function

Connect the select lines to the function's inputs, then tie each data pin to that row's truth-table output. A 2^N-to-1 mux therefore implements any N-input function. If one input goes on the data pins, you can use a mux half that size. FPGAs rely on this idea: their look-up tables are essentially muxes selecting from stored bits.

Worked example: XOR with one 2:1 mux

When A = 0, the output should equal B. When A = 1, it should equal B'. So use A as the select, B on the 0 input, and an inverted B on the 1 input.

Common mistake

Writing long if / else if chains when the conditions are mutually exclusive. Each else if can add a level of priority logic, making the last branch the slowest. A case statement over one select signal usually gives a flatter, faster mux.

Key ideas

  • A 2^N-to-1 mux has N select lines.
  • Any N-input function fits on a 2^N-to-1 mux, or 2^(N-1)-to-1 with an input on the data pins.
  • Long priority chains add delay; a case-style mux is flatter.

Your exercise

Implement XOR using one 2-to-1 mux and an inverter.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: With s = 0 the output shows d[1], and with s = 1 it shows d[0].

Interview questions

coreBuild a 4:1 mux from 2:1 muxes.

basicA 32:1 mux needs how many select lines? If you build it from 2:1 muxes, how many do you need?

basicIn assign y = sel ? a : b;, which input reaches y when sel is 0? Is the logic that produces the other input switched off?

coreImplement the 3-input majority function using one 4:1 mux, with A and B on the select lines.

coreMake an AND gate and an OR gate, each from a single 2:1 mux.

coreThis block is meant to be a 3:1 mux. What hardware does it really describe, and how would you fix it?

always @(*) begin
  if (s == 2'd0)      y = d0;
  else if (s == 2'd1) y = d1;
  else if (s == 2'd2) y = d2;
end

coreIn simulation, sel is x. What does y = sel ? a : b give when a = 1100 and b = 1010?

reg sel = 1'bx;
reg [3:0] a = 4'b1100, b = 4'b1010;
wire [3:0] y = sel ? a : b;

advancedAn FPGA look-up table with 6 inputs is essentially a mux choosing among stored bits. How many configuration bits does it hold, and how many different functions can it implement?

advancedHere is a mux whose select oh is one-hot. Why is this form attractive, and what happens if two select bits are high?

// oh is one-hot
assign y = ({4{oh[0]}} & m0) |
           ({4{oh[1]}} & m1) |
           ({4{oh[2]}} & m2);

Practise these 9 questions with animations