Exercise BB4c · building blocks · multiplexer
Multiplexer 4053: three switches of two
Three independent changeover switches in one chip: each of A, B and C picks its X or its Y channel, and one INH opens all three.
- BB4a: 4051, 8 channels
- BB4b: 4052, 2 × 4 channels
- BB4c: 4053, 3 × 2 channels
- BB4d: 4019, AND/OR select
1What it is and why it matters
What it does. The 4053 has three switches, A, B and C. Each has two channels, X and Y, a common pin and its own select input: 0 joins the common pin to X, 1 to Y. Here the channels are wires to the rails; the LED module shows the three selects, INH and the three common pins.
How it works. A 2:1 multiplexer is the smallest one: one select bit, two inputs. The 4053 puts three of them in one package with a common INH, so they can also switch three signals together if the three selects are joined.
Where it is used. A multiplexer chooses which of several signals goes on: the register a processor reads, the source of a bus, the input of an ALU. In an FPGA every lookup table is a multiplexer whose data inputs are the bits of its configuration memory. The analog switches of the 4051–4053 also route audio and sensor signals to one converter.
Why learn it.
- Choosing between data under control of a few bits is the heart of every datapath.
- A multiplexer with its data inputs set to constants is a lookup table: it can compute any function of its select inputs.
- High impedance, the third state of an output, appears here for the first time: with INH = 1 the output is not 0 or 1, it is disconnected.
2The key elements: select, data, output
| Switch | Select input: channel | Common pin |
|---|---|---|
| A | 0: AX, 1: AY | A COM |
| B | 0: BX, 1: BY | B COM |
| C | 0: CX, 1: CY | C COM |
Waveforms
tb.sv in Icarus Verilog: the first 16 steps, with the data wires
as on the breadboard. With INH = 1 the output is z, drawn grey. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4053 | triple 2-channel analog multiplexer | 1 | $0.45 | Elexp (elexp.com) |
| Button modules | C, B, A, INH; 4 from the pack of five | 4 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows the selects and the outputs | 1 | $0.38 | aliexpress.us |
| Breadboard, 830 points | 1 | $3.00 | amazon.com | |
| Breadboard power module | 5 V on the rails | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Pre-cut jumper wires | 14 wires on the board (or male-male Dupont jumpers) | 14 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 20 leads to the buttons and the LED module | 20 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; the 4053 is in the Comprehensive kit.
4The chip, pin by pin
The chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every input must be connected to something, because a CMOS input left open picks up noise and can switch at random. The drawings show each chip from above with the notch on the left, as in the datasheet: pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.
4053: triple 2-channel analog multiplexer
Switch A: AX pin 12, AY pin 13, A COM pin 14, select pin 11. Switch B: BX pin 2, BY pin 1, B COM pin 15, select pin 10. Switch C: CX pin 5, CY pin 3, C COM pin 4, select pin 9. INH is pin 6, VEE pin 7 (to GND).
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | BY | in/out | channel BY: joined to its common pin when its select input is 1 | BY (GND) |
| 2 | BX | in/out | channel BX: joined to its common pin when its select input is 0 | BX (+5V) |
| 3 | CY | in/out | channel CY: joined to its common pin when its select input is 1 | CY (+5V) |
| 4 | C COM | in/out | common in/out of switch C | C COM |
| 5 | CX | in/out | channel CX: joined to its common pin when its select input is 0 | CX (GND) |
| 6 | INH | input | INH: 1 opens all switches | INH |
| 7 | VEE | power | negative supply of the analog switches: GND here, for signals from 0 to 5 V | GND |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | C | input | select of switch C: 0 = CX, 1 = CY | C |
| 10 | B | input | select of switch B: 0 = BX, 1 = BY | B |
| 11 | A | input | select of switch A: 0 = AX, 1 = AY | A |
| 12 | AX | in/out | channel AX: joined to its common pin when its select input is 0 | AX (GND) |
| 13 | AY | in/out | channel AY: joined to its common pin when its select input is 1 | AY (+5V) |
| 14 | A COM | in/out | common in/out of switch A | A COM |
| 15 | B COM | in/out | common in/out of switch B | B COM |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the multiplexer as a block
mux_2_triple.sv describes.
The open circles are the wires to the rails that set the data. · SVG · PNG6Schematic 2: connections
| Net | From | To | Wire |
|---|---|---|---|
| C | button C, OUT | 4053 9 (C), LED module, LED 1 | M-F, M-M |
| B | button B, OUT | 4053 10 (B), LED module, LED 2 | M-F, M-M |
| A | button A, OUT | 4053 11 (A), LED module, LED 3 | M-F, M-M |
| INH | button INH, OUT | 4053 6 (INH), LED module, LED 4 | M-F, M-M |
| AX | AX wire: GND | 4053 12 (AX) | M-M |
| AY | AY wire: +5V | 4053 13 (AY) | M-M |
| BX | BX wire: +5V | 4053 2 (BX) | M-M |
| BY | BY wire: GND | 4053 1 (BY) | M-M |
| CX | CX wire: GND | 4053 5 (CX) | M-M |
| CY | CY wire: +5V | 4053 3 (CY) | M-M |
| A COM | — | 4053 14 (Ac), LED module, LED 5 | M-F |
| B COM | — | 4053 15 (Bc), LED module, LED 6 | M-F |
| C COM | — | 4053 4 (Cc), LED module, LED 7 | M-F, M-M |
| +5V | +5V | 4053 16 (VDD) | |
| GND | GND | 4053 8 (VSS), 4053 7 (VEE) |
7Schematic 3: the breadboard
Build it step by step
- Do the getting-started steps first: how the breadboard is connected inside, the power module with both jumpers on 5 V, how a chip sits across the gap.
- Insert the chips across the gap: the 4053 turned around, notch to the right, pin 1 in hole 15e. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
- Power: for each chip a red wire from its VDD pin (14, or 16 on a 16-pin chip) to the + rail and a black wire from its VSS pin (7 or 8) to the − rail, each to the rail on its own side of the gap. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
- VEE, pin 7, to the bottom − rail: the switches pass logic levels between 0 and 5 V.
- The data wires, each from its pin to a rail, in the positions of the checklist.
- The signal wires of the checklist, which bring pins to the half of the board where the module leads land.
- With the power off, the buttons above the board and the LED module below it.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 2 wires | ||||
| 1 | +5V | 15j (4053 pin 16, VDD) | bottom + rail, column 15 | |
| 2 | GND | 8a (4053 pin 8, VSS) | top − rail, column 8 | |
| Fixed inputs: CMOS inputs must not float · 1 wire | ||||
| 9 | GND | 9a (4053 pin 7, VEE) | top − rail, column 9 | |
| Mode wires: move them between + and − to change the mode · 6 wires | ||||
| 3 | AX | 11j (4053 pin 12, AX) | bottom − rail, column 11 | |
| 4 | AY | 12j (4053 pin 13, AY) | bottom + rail, column 12 | |
| 5 | BX | 14a (4053 pin 2, BX) | top + rail, column 14 | |
| 6 | BY | 15a (4053 pin 1, BY) | top − rail, column 15 | |
| 7 | CX | 11a (4053 pin 5, CX) | top − rail, column 11 | |
| 8 | CY | 13a (4053 pin 3, CY) | top + rail, column 13 | |
| Signal wires · 5 wires | ||||
| 10 | C | 8g (4053 pin 9, C) | 7e | |
| 11 | B | 9g (4053 pin 10, B) | 6e | |
| 12 | A | 10g (4053 pin 11, A) | 16e | |
| 13 | INH | 10c (4053 pin 6, INH) | 17f | |
| 14 | C COM | 12d (4053 pin 4, Cc) | 18f | |
| Modules on male-female jumpers · 20 wires | ||||
| 15 | +5V | button C, + | top + rail, column 5 | |
| 16 | C | button C, OUT | 7a | |
| 17 | GND | button C, − | top − rail, column 7 | |
| 18 | +5V | button B, + | top + rail, column 10 | |
| 19 | B | button B, OUT | 6a | |
| 20 | GND | button B, − | top − rail, column 12 | |
| 21 | +5V | button A, + | top + rail, column 15 | |
| 22 | A | button A, OUT | 16a | |
| 23 | GND | button A, − | top − rail, column 17 | |
| 24 | +5V | button INH, + | top + rail, column 20 | |
| 25 | INH | button INH, OUT | 10a (4053 pin 6, INH) | |
| 26 | GND | button INH, − | top − rail, column 22 | |
| 27 | GND | LED module, G | bottom − rail, column 24 | |
| 28 | C | LED module, LED 1 | 8j (4053 pin 9, C) | |
| 29 | B | LED module, LED 2 | 9j (4053 pin 10, B) | |
| 30 | A | LED module, LED 3 | 10j (4053 pin 11, A) | |
| 31 | INH | LED module, LED 4 | 17j | |
| 32 | A COM | LED module, LED 5 | 13j (4053 pin 14, Ac) | |
| 33 | B COM | LED module, LED 6 | 14j (4053 pin 15, Bc) | |
| 34 | C COM | LED module, LED 7 | 18j | |
Test it
- All buttons up: each common pin shows its X channel.
- Press A: only A COM changes, to AY. B and C work alone in the same way.
- Press INH: all three outputs go dark.
If it does not work: if the output shows the wrong channel, check the select leads: a swapped pair gives a different but consistent order. If one channel never shows, check its data wire. If an output stays dark, check INH (GND when the button is up) and VEE at GND.
8SystemVerilog: the generic design
mux_2_triple.sv describes schematic 1 in a few lines. It is synthesizable.
// Exercise BB4c: three 2-channel multiplexers, like the 4053. // // Generic version that matches schematic 1, not tied to any chip. Each of // the three switches A, B, C has its own select input: 0 passes its X // channel, 1 its Y channel. With inh = 1 the outputs are not driven (high // impedance). Synthesizable; in an FPGA keep inh at 0. module mux_2_triple ( input logic [2:0] x_ch, // x_ch [0] is AX, x_ch [1] BX, x_ch [2] CX input logic [2:0] y_ch, // y_ch [0] is AY, ... input logic [2:0] sel, // sel [0] is the select A of switch A, ... input logic inh, // 1 = all switches open output logic [2:0] com // com [0] is A COM, ... ); for (genvar i = 0; i < 3; i ++) begin : switch_i assign com [i] = inh ? 1'bz : sel [i] ? y_ch [i] : x_ch [i]; end endmodule
9SystemVerilog model of the chip
One module per chip, with ports named as in the datasheet and the pin number of each port in a comment. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.
// CD4053B: triple 2-channel analog multiplexer/demultiplexer. // // Ports in pin order, named as in the datasheet. The power pins, VEE (7), // VSS (8) and VDD (16), are left out. // // Digital model of three analog switches, each with its own select: 0 joins // the X channel to the common pin, 1 the Y channel. INH = 1 opens all // switches and the commons float. Synthesizable while INH is 0. module cd4053 ( input logic by, // Pin 1 input logic bx, // Pin 2 input logic cy, // Pin 3 output logic c_com, // Pin 4: C common in/out input logic cx, // Pin 5 input logic inh, // Pin 6: 1 = all switches open // Pin 7: VEE // Pin 8: VSS input logic c, // Pin 9: select of switch C input logic b, // Pin 10: select of switch B input logic a, // Pin 11: select of switch A input logic ax, // Pin 12 input logic ay, // Pin 13 output logic a_com, // Pin 14: A common in/out output logic b_com // Pin 15: B common in/out // Pin 16: VDD ); assign a_com = inh ? 1'bz : (a ? ay : ax); assign b_com = inh ? 1'bz : (b ? by : bx); assign c_com = inh ? 1'bz : (c ? cy : cx); endmodule
The model passes logic levels from the channels to the common pins only. The real chip is a set of switches that conduct both ways; that cannot be written as a synthesizable logic model.
10SystemVerilog: the chip-level design
mux_2_triple_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as mux_2_triple.sv. Unused inputs are tied to vdd or gnd
as on the breadboard, and unused outputs go to wires named unused_…. The file is generated from the
breadboard wire list after a pin-level check of that list, so the code, the drawings and the checklist cannot
disagree.
// Exercise BB4c, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as mux_2_triple.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_bb4.py, not this file. Synthesizable. module mux_2_triple_chips ( input logic [2:0] x_ch, input logic [2:0] y_ch, input logic [2:0] sel, input logic inh, output logic [2:0] com ); //------------------------------------------------------------------------ // U1: CD4053 triple 2-channel analog multiplexer, pin 1 in column 8 cd4053 i_u1 ( .by ( y_ch [1] ), // Pin 1 <- BY wire, at GND on the breadboard .bx ( x_ch [1] ), // Pin 2 <- BX wire, at +5V on the breadboard .cy ( y_ch [2] ), // Pin 3 <- CY wire, at +5V on the breadboard .c_com ( com [2] ), // Pin 4 -> C COM .cx ( x_ch [2] ), // Pin 5 <- CX wire, at GND on the breadboard .inh ( inh ), // Pin 6 <- button INH .c ( sel [2] ), // Pin 9 <- button C .b ( sel [1] ), // Pin 10 <- button B .a ( sel [0] ), // Pin 11 <- button A .ax ( x_ch [0] ), // Pin 12 <- AX wire, at GND on the breadboard .ay ( y_ch [0] ), // Pin 13 <- AY wire, at +5V on the breadboard .a_com ( com [0] ), // Pin 14 -> A COM .b_com ( com [1] ) // Pin 15 -> B COM ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with a reference model. The circuit has
no clock, but the testbench has one, like the testbenches of Basics-Graphics-Music: it sets the inputs right after a
rising edge and checks the outputs at the next one. It drives every combination of the data, the selects and INH
twice, counting up from the data wires as on the breadboard and then in Gray code order, and prints the rows with the
data as on the breadboard, and any row with an error. It compares with ===, so z must match z. The output below is shortened.
tb.sv171 lines · show the codefile
// Testbench for exercise BB4c. // // Runs the generic design (mux_2_triple.sv) and the chip-level design // (mux_2_triple_chips.sv) side by side and compares both with // for each switch X or Y as its select says, or z while INH = 1. Works with Icarus Verilog, Verilator (--binary) // and Questa. // // The circuit has no clock, but like the testbenches of Basics-Graphics-Music // this one drives and checks signals only at rising edges of its own clock: // it changes the inputs after one edge and checks the outputs at the next. // It tries all 1024 input combinations twice: counting up, and then in // Gray code order, where one input changes at a time, as when a student // presses or releases one button. // Time units. The design files have none: Yosys does not accept timeunit // and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps // in its own testbench. This line sets the same units here and for the // design files compiled after it, so tb.sv comes first in the file list. `timescale 1ns / 1ps module tb; logic clk; logic [2:0] x_ch; logic [2:0] y_ch; logic [2:0] sel; logic inh; wire [2:0] com, com_chips; mux_2_triple i_generic ( .x_ch ( x_ch ), .y_ch ( y_ch ), .sel ( sel ), .inh ( inh ), .com ( com ) ); mux_2_triple_chips i_chips ( .x_ch ( x_ch ), .y_ch ( y_ch ), .sel ( sel ), .inh ( inh ), .com ( com_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: for each switch X or Y as its select says, or z while INH = 1. // Bits, from the left: com. function automatic logic [2:0] expected ( input logic [2:0] in_x_ch, input logic [2:0] in_y_ch, input logic [2:0] in_sel, input logic in_inh ); logic [2:0] y; for (int i = 0; i < 3; i ++) y [i] = in_sel [i] ? in_y_ch [i] : in_x_ch [i]; if (in_inh) y = 3'bzzz; return y; endfunction wire [2:0] out = { com }; wire [2:0] out_chips = { com_chips }; wire [2:0] out_model = expected (x_ch, y_ch, sel, inh); //------------------------------------------------------------------------ // Checking at every rising clock edge: the inputs still hold the values the // stimulus set at the previous edge, and the outputs have settled. // Initial values are fine in a testbench // verilator lint_off PROCASSINIT logic checking = 1'b0; // Set by the stimulus after the first inputs logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; logic [1023:0] rows_seen = '0; // Which input combinations were checked always @ (posedge clk) if (checking & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (out === out_model ? 0 : 1) + (out_chips === out_model ? 0 : 1); rows_seen <= rows_seen | 1024'b1 << { x_ch, y_ch, sel, inh }; // Print the rows with the data wires as on the breadboard, and every error if (x_ch == 3'b010 & y_ch == 3'b101 | out !== out_model | out_chips !== out_model) $display ("%5d %b %b %b %b %b %s %s", cycle + 1, x_ch, y_ch, sel, inh, com, out === out_model ? "ok " : "WRONG ", out_chips === out_model ? "ok " : "WRONG"); end //------------------------------------------------------------------------ // Driving the inputs: all combinations, counting up from the data wires as on the // breadboard, then in Gray code order initial begin $dumpfile ("tb.vcd"); `ifdef __ICARUS__ $dumpvars (0, tb); // Without a scope Icarus also tries to dump $unit `else $dumpvars; `endif $display ("cycle CX..AX CY..AY CBA INH COM C..A generic chips"); checking <= '1; for (int i = 0; i < 1024; i ++) begin { x_ch, y_ch, inh, sel } <= 10' (i) ^ 10'h150; @ (posedge clk); end for (int i = 0; i < 1024; i ++) begin { x_ch, y_ch, inh, sel } <= 10' (i ^ (i >> 1)) ^ 10'h150; @ (posedge clk); end // Stop checking one clock edge before $finish, so that every // simulator reports the same number of checks done <= '1; @ (posedge clk); $finish; end //------------------------------------------------------------------------ final begin if (n_errors == 0 && & rows_seen) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, %0d of %0d input combinations seen", n_errors, n_checks, $countones (rows_seen), 1024); end endmodule
Each script compiles the files, runs the simulation, writes a log and the waveform
tb.vcd, and exits with an error unless the test passes. Run them from any directory:
./run_icarus.bash, ./run_verilator.bash or ./run_questa.bash;
./clean.bash removes what they make. The waveform opens in GTKWave or Surfer.
run_icarus.bash22 lines · show the codefile
#!/usr/bin/env bash # Exercise BB4c: run the testbench with Icarus Verilog 12 or later. # The output goes to icarus.log and the waveform to tb.vcd, # for GTKWave or Surfer. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv mux_2_triple.sv mux_2_triple_chips.sv cd4053.sv) if ! command -v iverilog > /dev/null then printf '%s\n' "$0: iverilog not found, install Icarus Verilog 12 or later" >&2 exit 1 fi iverilog -g2012 -o tb.vvp "${files[@]}" vvp -n tb.vvp | tee icarus.log grep -q '^PASS' icarus.log
run_verilator.bash29 lines · show the codefile
#!/usr/bin/env bash # Exercise BB4c: run the testbench with Verilator 5. --binary builds # a simulator with the testbench's delays; it needs a C++20 compiler. # The output goes to verilator.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv mux_2_triple.sv mux_2_triple_chips.sv cd4053.sv) # OSS CAD Suite unpacked in the home directory has Verilator too if ! command -v verilator > /dev/null && [ -x "$HOME/oss-cad-suite/bin/verilator" ] then PATH="$HOME/oss-cad-suite/bin:$PATH" fi if ! command -v verilator > /dev/null then printf '%s\n' "$0: verilator not found, install Verilator 5" >&2 exit 1 fi verilator --binary -Wall --trace --top-module tb -o tb "${files[@]}" obj_dir/tb | tee verilator.log grep -q '^PASS' verilator.log
run_questa.bash64 lines · show the codefile
#!/usr/bin/env bash # Exercise BB4c: run the testbench with Questa, for example the free # Questa Starter Edition that comes with Quartus Prime Lite. # The output goes to questa.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv mux_2_triple.sv mux_2_triple_chips.sv cd4053.sv) # When vsim is not in PATH, look for it where Quartus installs it if ! command -v vsim > /dev/null then for dir in "$HOME"/intelFPGA_lite/*/questa_fse/bin \ "$HOME"/altera_lite/*/questa_fse/bin \ /opt/intelFPGA_lite/*/questa_fse/bin \ /opt/altera_lite/*/questa_fse/bin do if [ -x "$dir/vsim" ] then PATH="$dir:$PATH" break fi done fi if ! command -v vsim > /dev/null then printf '%s\n' "$0: vsim not found, install Questa or add it to PATH" >&2 exit 1 fi # The Starter Edition needs a free license file from the Intel # licensing site; Quartus keeps it in ~/flexlm/license.dat license="$HOME/flexlm/license.dat" # ${SALT_LICENSE_SERVER-} is empty when the variable is not set, # where a plain $SALT_LICENSE_SERVER would stop the script (set -u) salt_license="${SALT_LICENSE_SERVER-}" if [ -z "$salt_license" ] && [ -f "$license" ] then export SALT_LICENSE_SERVER="$license" fi # Older Questa versions read LM_LICENSE_FILE; ${LM_LICENSE_FILE-} # is empty when the variable is not set lm_license="${LM_LICENSE_FILE-}" if [ -z "$lm_license" ] && [ -f "$license" ] then export LM_LICENSE_FILE="$license" fi rm -rf work vlib work > /dev/null vlog -sv -lint "${files[@]}" vsim -c tb -do "run -all; quit -f" | tee questa.log grep -q 'PASS' questa.log
Tested with Icarus Verilog 12.0 and 14.0 (development), Verilator 5.053 with -Wall and
no warnings, and Questa Altera Starter FPGA Edition 2025.2 from Quartus Prime Lite 25.1. The code uses only what
Icarus Verilog 13 supports. This is the output, from Icarus Verilog:
VCD info: dumpfile tb.vcd opened for output.
cycle CX..AX CY..AY CBA INH COM C..A generic chips
1 010 101 000 0 010 ok ok
2 010 101 001 0 011 ok ok
3 010 101 010 0 000 ok ok
4 010 101 011 0 001 ok ok
5 010 101 100 0 110 ok ok
6 010 101 101 0 111 ok ok
7 010 101 110 0 100 ok ok
8 010 101 111 0 101 ok ok
9 010 101 000 1 zzz ok ok
10 010 101 001 1 zzz ok ok
11 010 101 010 1 zzz ok ok
12 010 101 011 1 zzz ok ok
13 010 101 100 1 zzz ok ok
14 010 101 101 1 zzz ok ok
15 010 101 110 1 zzz ok ok
16 010 101 111 1 zzz ok ok
1025 010 101 000 0 010 ok ok
1026 010 101 001 0 011 ok ok
1027 010 101 011 0 001 ok ok
1028 010 101 010 0 000 ok ok
1029 010 101 110 0 100 ok ok
1030 010 101 111 0 101 ok ok
…
tb.sv:157: $finish called at 20485000 (1ps)
PASS: 4096 checks, no errors
To see that the testbench catches mistakes, break something and run it again: swap y_ch [i] and x_ch [i] in mux_2_triple.sv, or connect the select of switch A to the B button in mux_2_triple_chips.sv. The script
reports FAIL and the trace shows the lines marked WRONG.
12On an FPGA board: lab_top in BGM
The Basics-Graphics-Music (BGM) repository runs the same labs on dozens
of FPGA boards. Each lab has a lab_top module with the same ports on every board: clock, a 1 Hz
slow_clk, reset, keys, LEDs, a 7-segment display.
- Copy
labs/1_basics/1_02_muxto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addmux_2_triple.sv. The BGM scripts compile every.svfile of the directory excepttb.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash; the first run asks which board you have.
The FPGA board has fewer inputs than the breadboard has wires, so the data are fixed in lab_top.sv as
the wires of the breadboard: change the constants to try others. The keys are C, B, A; INH is held at 0, because an FPGA has no high-impedance wires inside. To run
the chip-level design instead, add mux_2_triple_chips.sv and cd4053.sv and change the module name in
the instance.
bgm_lab/lab_top.sv100 lines · show the codefile
// Exercise BB4c on an FPGA board with the Basics-Graphics-Music (BGM) labs: // https://github.com/yuri-panchul/basics-graphics-music // // Copy labs/1_basics/1_02_mux to a new directory, replace its lab_top.sv with // this file and add mux_2_triple.sv. For the chip-level design add // mux_2_triple_chips.sv and cd4053.sv instead, and change the module name // below. // // Keys: key [2:0] sel. The LEDs: led [2:0] com. A board with fewer LEDs shows // the first ones. // // The channels are fixed at CX BX AX = 010 and CY BY AY = 101, as the wires // on the breadboard. INH is held at 0: an FPGA has no high-impedance wires // inside. `include "config.svh" module lab_top # ( parameter clk_mhz = 50, w_key = 4, w_sw = 8, w_led = 8, w_digit = 8, w_gpio = 100, screen_width = 640, screen_height = 480, w_red = 4, w_green = 4, w_blue = 4, w_x = $clog2 ( screen_width ), w_y = $clog2 ( screen_height ) ) ( input clk, input slow_clk, input rst, // Keys, switches, LEDs input [w_key - 1:0] key, input [w_sw - 1:0] sw, output logic [w_led - 1:0] led, // A dynamic seven-segment display output logic [ 7:0] abcdefgh, output logic [w_digit - 1:0] digit, // Graphics input [w_x - 1:0] x, input [w_y - 1:0] y, output logic [w_red - 1:0] red, output logic [w_green - 1:0] green, output logic [w_blue - 1:0] blue, // Microphone, sound output and UART input [ 23:0] mic, output [ 15:0] sound, input uart_rx, output uart_tx, // General-purpose Input/Output inout [w_gpio - 1:0] gpio ); //------------------------------------------------------------------------ assign abcdefgh = '0; assign digit = '0; assign red = '0; assign green = '0; assign blue = '0; assign sound = '0; assign uart_tx = '1; //------------------------------------------------------------------------ logic [2:0] com; mux_2_triple i_mux_2_triple ( .sel ( key [2:0] ), .x_ch ( 3'b010 ), .y_ch ( 3'b101 ), .inh ( 1'b0 ), .com ( com ) ); assign led = w_led' ({ com }); endmodule
13On an FPGA board: hackathon_top in BGM
The hackathon setup of BGM uses a Tang Nano 9K with a 480×272 screen and a TM1638 board of keys,
LEDs and digits. Its top module, hackathon_top, has simpler ports; the first line of the file names
the board, so the scripts need no questions.
- Copy
hackathon/problems/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addmux_2_triple.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
The keys are C, B, A, the data are fixed as in lab_top; the screen shows the inputs in blue and the
outputs in green.
bgm_hackathon/hackathon_top.sv84 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise BB4c in the hackathon setup of Basics-Graphics-Music (BGM): // https://github.com/yuri-panchul/basics-graphics-music // // Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its // hackathon_top.sv with this file and add mux_2_triple.sv. // // Keys: key [2:0] sel. The LEDs: led [2:0] com. The screen shows 6 lamps in a // row: A, B, C, A COM, B COM, C COM; inputs in blue, outputs in green. // // The channels are fixed at CX BX AX = 010 and CY BY AY = 101, as the wires // on the breadboard. INH is held at 0: an FPGA has no high-impedance wires // inside. module hackathon_top ( input logic clock, input logic slow_clock, input logic reset, input logic [7:0] key, output logic [7:0] led, // A dynamic seven-segment display output logic [7:0] abcdefgh, output logic [7:0] digit, // LCD screen interface input logic [8:0] x, input logic [8:0] y, output logic [4:0] red, output logic [5:0] green, output logic [4:0] blue, inout logic [3:0] gpio ); //------------------------------------------------------------------------ wire [2:0] sel = key [2:0]; logic [2:0] com; mux_2_triple i_mux_2_triple ( .sel ( sel ), .x_ch ( 3'b010 ), .y_ch ( 3'b101 ), .inh ( 1'b0 ), .com ( com ) ); assign led = { 5'b0, com }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 6 square lamps on the 480 x 272 screen, 40 pixels wide, 64 apart: // the inputs in blue, the outputs in green. Bright when 1, dim when 0. wire [5:0] lamp = { com, sel }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 6; i ++) if (x >= 9' (60 + i * 64) & x < 9' (100 + i * 64) & y >= 116 & y < 156) begin if (i < 3) blue = lamp [i] ? 31 : 6; else green = lamp [i] ? 63 : 12; end end endmodule
14On a chip: Tiny Tapeout
Tiny Tapeout makes real chips from student designs. Each design
is a module with the ports below; on the demo board ui_in comes from DIP switches and
uo_out drives a 7-segment display.
This design has no clock and no state: project.v connects the DIP switches to its inputs
and its outputs to the display. The project still gets a clock and a reset from the board; they stay unused.
The switches are C, B, A; the data are fixed in project.v, INH at 0. The segments of the display show the outputs.
tiny_tapeout/project.v52 lines · show the codefile
/* * Exercise BB4c on Tiny Tapeout: a triple 2-channel multiplexer, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add mux_2_triple.sv to source_files in info.yaml next to project.v. * Rename the module to tt_um_<your GitHub name>_<project name> and use the * same name as top_module in info.yaml. */ `default_nettype none module tt_um_example ( input wire [7:0] ui_in, // DIP switches, INPUT Pmod, RP2350 GPIO17-24 output wire [7:0] uo_out, // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40 input wire [7:0] uio_in, // BIDIR Pmod, RP2350 GPIO25-32 output wire [7:0] uio_out, // (same pins) output wire [7:0] uio_oe, // 1 = your design drives the pin input wire ena, // set by the on-chip project mux, not a board pin input wire clk, // RP2350 GPIO16 input wire rst_n // RP2350 GPIO14 ); // Switches 2-0 are sel // The channels are fixed at CX BX AX = 010 and CY BY AY = 101, as the // wires on the breadboard. INH is held at 0: an FPGA has no // high-impedance wires inside. wire [2:0] com; mux_2_triple i_mux_2_triple ( .sel ( ui_in [2:0] ), .x_ch ( 3'b010 ), .y_ch ( 3'b101 ), .inh ( 1'b0 ), .com ( com ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. Each output lights one segment: // a (top) com [0], b (top right) com [1], c (bottom right) com [2]. assign uo_out = { 5'b0, com }; assign uio_out = 8'b0; assign uio_oe = 8'b0; // The circuit needs no clock or reset. // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, ui_in [7:3], uio_in, 1'b0 }; endmodule
In info.yaml list the files and use the module name as the top module:
project:
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_mux_2_triple
source_files:
- "project.v"
- "mux_2_triple.sv"
15Going further
Tasks for students
- One select for all. Join the three select inputs: the 4053 now switches a 3-bit word between X and Y.
- A 2:1 multiplexer from gates. Build Y = S ? B : A from an inverter, two AND gates and an OR gate, and compare it with one switch of the 4053.
- XOR from a switch. Connect X to a signal D and Y to NOT D: the output is D XOR S. Why?
Files of this exercise
- mux_2_triple.sv: the generic design (section 8)
- cd4053.sv: chip models (section 9)
- mux_2_triple_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
Sources
The multiplexers 4051, 4052, 4053 and 4019 are in the Building blocks list of “An improved way of teaching school kids digital electronics” and in lesson 1.6 of the Habr article about the electronics course. The 4053 is described in the Texas Instruments datasheet for the CD4053B.