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.
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 at | Observe at | a | b | sel | y |
|---|---|---|---|---|---|
| 0 ns | 1 ns | 31 | 52 | 0 | 31 |
| 10 ns | 11 ns | 31 | 52 | 1 | 52 |
| 20 ns | 21 ns | 9C | 52 | 1 | 52 |
| 30 ns | 31 ns | 9C | 6E | 1 | 6E |
| 40 ns | 41 ns | 9C | 6E | 0 | 9C |
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.
| Event | A | B | sel | Y |
|---|
Teaching film
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)