Multiplexer (mux)

Two sensors share an eight-bit output. Switch sel to see which reading appears at y, then reproduce it in SystemVerilog.

Sensor A reports hexadecimal 31 and sensor B reports 52. The display has a single eight-bit input, and the operator wants to switch between the two sources. A multiplexer, or mux, uses one select signal sel to route a source to the shared output y.

The select signal chooses the source

With sel=0, y takes all eight bits from A. When sel changes to one, y takes all eight bits from B. A, B and y are each eight bits wide. The select signal needs one bit because it identifies one of two sources. At the bit level, each y[i] comes from either a[i] or b[i]. All eight choices use the same sel.

Eight-bit inputs A and B connect to a mux. A one-bit sel selects a source for eight-bit output y. Sel zero routes 31 and sel one routes 52.
Each input carries an eight-bit word. Changing sel switches the source of the whole word.

A SystemVerilog continuous assignment expresses the selection.

module mux8 (
    input  logic [7:0] a, b,
    input  logic       sel,
    output logic [7:0] y
);
    assign y = sel ? b : a;
endmodule

The value after ? is B, selected when the condition is true. The value after : is A, selected when the condition is false. This example drives sel with known zeros and ones. The assignment continuously determines the output from the inputs. The module has no clock port or register to save the previous output.

Change the inputs after switching

Start with A=31 and B=52. Switch sel from zero to one, and the output follows B to 52. Leave sel high and change A to 9C. B is still selected, so y stays at 52. Now change B to 6E. The output follows the selected source and becomes 6E. Finally, switch sel back to zero. The mux routes A’s current value, 9C.

Change atObserve atabsely
0 ns1 ns3152031
10 ns11 ns3152152
20 ns21 ns9C52152
30 ns31 ns9C6E16E
40 ns41 ns9C6E09C
In the Icarus waveform, y becomes 52 when sel switches at 10 ns. Changing unselected A at 20 ns leaves y at 52. Changing selected B at 30 ns changes y to 6E. Selecting A at 40 ns makes y 9C.
The waveform comes from an ideal Icarus simulation. Dashed lines mark input changes. The table shows what the testbench reads just afterward.

A physical mux takes time for signals to propagate through gates and wires before the output settles. This RTL has no cell delays, so the waveform reflects the change at the same simulation time. The testbench observes the value 1 ns later to separate stimulus from checking. That interval sets the observation time.

A mux selects, and a register saves

Connect y to the D input of a downstream register. Suppose the register captures 52 between 10 and 30 ns. That sample appears at Q. At 30 ns the mux changes to 6E, and D follows it. Q holds the saved 52 until the next rising edge that allows an update. The mux output shows the current selection. Q shows the last captured value.

Three sources leave one code to define

Add source C and use two select bits. The codes 00, 01, and 10 can select A, B, and C. Give the fourth code a defined behavior, for example y=0 and valid=0 when the code is 11. A selected input can legitimately carry zero, so the receiver uses valid to tell whether it has a reading.

With an always_comb block and a case statement, assign both y and valid on every path. One way is to default both outputs to zero, then override them in each of the three legal branches. The unused code gets the defaults. This avoids inferring a latch from an unassigned branch.

Download the runnable SystemVerilog example. The Icarus test checks the table’s five observations and writes the raw VCD waveform.

Interactive lab

Select B, then edit A. Predict which change will reach the output. Each input accepts a hexadecimal byte from 00 to FF.

Y 0x31

Last eight input changes
EventABselY

Teaching film

Read the transcript

Run the RTL yourself

Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

Download Multiplexer (mux) RTL (.zip)

Check your understanding

1. A is 31, B is 52 and sel is 0. What is Y in hex?
2. With sel high, only A changes to 9C. Which output changes?
3. With sel high, B changes from 52 to 6E. When does the RTL model show the new Y?
4. Both inputs are 31. After sel changes, can Y alone tell you which one is selected?
5. Each input is eight bits wide. How many select bits does a two-input mux need?