Exercise M6 · mini-project · multiplexer
8:1 multiplexer from a 4028 and two 4086 AND-OR-INVERT gates
The same 8:1 multiplexer as M5, with the AND gates and the OR replaced by two 4086 AND-OR-INVERT gates. The first one's output enables the second, and one inverter undoes the inversion.
- M5: 8:1 multiplexer from a 4028 decoder, AND and OR gates
- M6: 8:1 multiplexer from a 4028 and two 4086 AND-OR-INVERT gates
- M7: Dual 4:1 multiplexer from a 4555 decoder, AND and OR gates
- M8: Dual 4:1 multiplexer from a 4555 and two 4086 AND-OR-INVERT gates
1What it is and why it matters
What it does. Buttons C, B and A give the select, wires D0–D7 the data, and the 4028 makes the select one-hot. The first 4086 computes J1 = NOT (Q0·D0 + Q1·D1 + Q2·D2 + Q3·D3). Its J1 goes to ENABLE of the second 4086, whose J2 = NOT (Q4·D4 + … + Q7·D7 + NOT J1). NOT J1 is the OR of the first four terms, so J2 is the inverted OR of all eight. A 4069 inverter turns J2 into the output.
How it works. AND-OR-INVERT is the natural form of CMOS logic: inverting gates are the simplest (exercise B2). The 4086 has two extra inputs, INHIBIT and ENABLE, made for joining chips into wider gates. The exercise list suggests the inverted output of the first gate on INHIBIT of the second; feeding J1 to ENABLE is the same thing without an extra inverter, because ENABLE = 0 acts like INHIBIT = 1. Compared with M5, the AND gates and the OR are inside two chips instead of three, and the eight terms are joined in two steps.
Where it is used. Every datapath chooses between sources: which register goes to the ALU, which result goes back. Inside an FPGA and in standard-cell chips such choices are made of exactly these pieces: decoders, AND gates and OR gates, or AND-OR-INVERT cells.
Why learn it.
- It opens the black box of BB4: a multiplexer is a decoder plus gates.
- One-hot signals from a decoder appear again in state machines and in memory addressing.
- It shows that one function has many circuits; the testbench proves they agree.
2The key elements: decode, gate, merge
| C B A | Decoder output at 1 | Data input let through | OUT, wires as on the board |
|---|---|---|---|
| 000 | Q0 | D0 | 1 |
| 001 | Q1 | D1 | 0 |
| 010 | Q2 | D2 | 1 |
| 011 | Q3 | D3 | 1 |
| 100 | Q4 | D4 | 0 |
| 101 | Q5 | D5 | 0 |
| 110 | Q6 | D6 | 1 |
| 111 | Q7 | D7 | 0 |
Waveforms
tb.sv in Icarus Verilog: the first steps, with the data wires as on
the breadboard and the select counting up. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4028 | BCD-to-decimal decoder: the select as one-hot Q0-Q7 | 1 | $0.35 | Elexp (elexp.com) |
| CD4086 | 4-wide 2-input AND-OR-INVERT: four AND terms, ORed and inverted (two chips) | 2 | $0.70 | Futurlec (futurlec.com) |
| CD4069 | hex inverter: undoes the inversion of the 4086 | 1 | $0.30 | Elexp (elexp.com) |
| Button modules | C, B, A; 3 from the pack of five | 3 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows the select and the output | 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 | 38 wires on the board (or male-male Dupont jumpers) | 38 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 14 leads to the buttons and the LED module | 14 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; these chips are in the Comprehensive kit.
4The chips, 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.
4028: BCD-to-decimal decoder
The decoder: inputs A, B, C on pins 10, 13, 12, D on pin 11 tied to GND, so that only Q0–Q7 can be 1. The outputs are spread over both rows (see BB5).
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | Q4 | output | 1 when the code D C B A is 4, 0 otherwise | Q4 |
| 2 | Q2 | output | 1 when the code D C B A is 2, 0 otherwise | Q2 |
| 3 | Q0 | output | 1 when the code D C B A is 0, 0 otherwise | Q0 |
| 4 | Q7 | output | 1 when the code D C B A is 7, 0 otherwise | Q7 |
| 5 | Q9 | output | 1 when the code D C B A is 9, 0 otherwise | not connected |
| 6 | Q5 | output | 1 when the code D C B A is 5, 0 otherwise | Q5 |
| 7 | Q6 | output | 1 when the code D C B A is 6, 0 otherwise | Q6 |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | Q8 | output | 1 when the code D C B A is 8, 0 otherwise | not connected |
| 10 | A | input | code bit A (weight 1) | A |
| 11 | D | input | code bit D (weight 8) | GND |
| 12 | C | input | code bit C (weight 4) | C |
| 13 | B | input | code bit B (weight 2) | B |
| 14 | Q1 | output | 1 when the code D C B A is 1, 0 otherwise | Q1 |
| 15 | Q3 | output | 1 when the code D C B A is 3, 0 otherwise | Q3 |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4086: 4-wide 2-input AND-OR-INVERT
J = NOT (A·B + C·D + E·F + G·H + INHIBIT + NOT ENABLE): four AND gates, an OR and a NOT in one chip. INHIBIT (pin 10) at 1 or ENABLE (pin 11) at 0 forces J to 0; for a plain 4-wide gate INHIBIT goes to GND and ENABLE to +5V. Pin 4 is not connected.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input A of the first AND | Q0 |
| 2 | B | input | input B of the first AND | D0 (+5V) |
| 3 | J | output | J = NOT (A·B + C·D + E·F + G·H + INH + NOT EN) | J1 |
| 4 | NC | — | not connected inside the chip | not connected |
| 5 | E | input | input E | Q2 |
| 6 | F | input | input F | D2 (+5V) |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | G | input | input G | Q3 |
| 9 | H | input | input H | D3 (+5V) |
| 10 | INH | input | INHIBIT/EXPAND: 1 forces J to 0; GND for a plain 4-wide gate | GND |
| 11 | EN | input | ENABLE/EXPAND: 0 forces J to 0; +5V for a plain 4-wide gate | +5V |
| 12 | C | input | input C | Q1 |
| 13 | D | input | input D | D1 (GND) |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4069: hex inverter
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 2 | tied to GND |
| 2 | G | output | G = NOT A | not connected |
| 3 | B | input | input of the gate with its output on pin 4 | J2 |
| 4 | H | output | H = NOT B | OUT |
| 5 | C | input | input of the gate with its output on pin 6 | tied to GND |
| 6 | I | output | I = NOT C | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | J | output | J = NOT D | not connected |
| 9 | D | input | input of the gate with its output on pin 8 | tied to +5V |
| 10 | K | output | K = NOT E | not connected |
| 11 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 12 | L | output | L = NOT F | not connected |
| 13 | F | input | input of the gate with its output on pin 12 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: decoder and gates
mux_8_aoi.sv describes. The
open circles are the data wires to the rails; the chip under each gate is the one that holds it on the breadboard. · SVG · PNG6Schematic 2: connections between the chips
Every connection, net by net. mux_8_aoi_chips.sv in section 10 is written from the same list.
| Net | From | To | Wire |
|---|---|---|---|
| C | button C, OUT | 4028 12 (C), LED module, LED 1 | M-F, M-M |
| B | button B, OUT | 4028 13 (B), LED module, LED 2 | M-F, M-M |
| A | button A, OUT | 4028 10 (A), LED module, LED 3 | M-F, M-M |
| Q0 | 4028 3 (Q0) | 4086 1 (A) | M-M |
| Q1 | 4028 14 (Q1) | 4086 12 (C) | M-M |
| Q2 | 4028 2 (Q2) | 4086 5 (E) | M-M |
| Q3 | 4028 15 (Q3) | 4086 8 (G) | M-M |
| Q4 | 4028 1 (Q4) | 4086 1 (A) | M-M |
| Q5 | 4028 6 (Q5) | 4086 12 (C) | M-M |
| Q6 | 4028 7 (Q6) | 4086 5 (E) | M-M |
| Q7 | 4028 4 (Q7) | 4086 8 (G) | M-M |
| D0 | D0 wire: +5V | 4086 2 (B) | M-M |
| D1 | D1 wire: GND | 4086 13 (D) | M-M |
| D2 | D2 wire: +5V | 4086 6 (F) | M-M |
| D3 | D3 wire: +5V | 4086 9 (H) | M-M |
| D4 | D4 wire: GND | 4086 2 (B) | M-M |
| D5 | D5 wire: GND | 4086 13 (D) | M-M |
| D6 | D6 wire: +5V | 4086 6 (F) | M-M |
| D7 | D7 wire: GND | 4086 9 (H) | M-M |
| J1 | 4086 3 (J) | 4086 11 (EN) | M-M |
| J2 | 4086 3 (J) | 4069 3 (B) | M-M |
| OUT | 4069 4 (H) | LED module, LED 4 | M-F |
| +5V | +5V | 4028 16 (VDD), 4086 14 (VDD), 4086 14 (VDD), 4069 14 (VDD), 4086 11 (EN), 4069 13 (F), 4069 11 (E), 4069 9 (D) | |
| GND | GND | 4028 8 (VSS), 4086 7 (VSS), 4086 7 (VSS), 4069 7 (VSS), 4086 10 (INH), 4028 11 (D), 4086 10 (INH), 4069 1 (A), 4069 5 (C) | |
| not connected | 4028 5 (Q9), 4028 9 (Q8), 4086 4 (NC), 4086 4 (NC), 4069 2 (G), 4069 6 (I), 4069 8 (J), 4069 10 (K), 4069 12 (L) |
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, notch to the left: the 4086 with pin 1 in hole 7f; the 4028 with pin 1 in hole 15f; the 4086 with pin 1 in hole 26f; the 4069 with pin 1 in hole 34f. Read the numbers on the chips: many look alike.
- 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. Make sure no wire touches + and − at the same time.
- The fixed inputs and tie-offs of the checklist: D of the 4028 to GND, INHIBIT of each 4086 to GND and ENABLE as the checklist says, the inputs of unused gates to the nearest rail.
- The data wires, each from its pin to a rail, in the positions of the checklist.
- The signal wires from the decoder to the gates and between the chips. Tick each wire as you insert it.
- 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 −) · 8 wires | ||||
| 1 | +5V | 15a (4028 pin 16, VDD) | top + rail, column 15 | |
| 2 | GND | 22j (4028 pin 8, VSS) | bottom − rail, column 22 | |
| 3 | +5V | 7a (4086 pin 14, VDD) | top + rail, column 7 | |
| 4 | GND | 13j (4086 pin 7, VSS) | bottom − rail, column 13 | |
| 5 | +5V | 26a (4086 pin 14, VDD) | top + rail, column 26 | |
| 6 | GND | 32j (4086 pin 7, VSS) | bottom − rail, column 32 | |
| 7 | +5V | 34a (4069 pin 14, VDD) | top + rail, column 34 | |
| 8 | GND | 40j (4069 pin 7, VSS) | bottom − rail, column 40 | |
| Fixed inputs: CMOS inputs must not float · 9 wires | ||||
| 17 | +5V | 10a (4086 pin 11, EN) | top + rail, column 10 | |
| 18 | GND | 11a (4086 pin 10, INH) | top − rail, column 11 | |
| 19 | GND | 20a (4028 pin 11, D) | top − rail, column 20 | |
| 20 | GND | 30a (4086 pin 10, INH) | top − rail, column 30 | |
| 21 | GND | 34j (4069 pin 1, A) | bottom − rail, column 34 | |
| 22 | +5V | 35a (4069 pin 13, F) | top + rail, column 35 | |
| 23 | +5V | 37a (4069 pin 11, E) | top + rail, column 37 | |
| 24 | GND | 38j (4069 pin 5, C) | bottom − rail, column 38 | |
| 25 | +5V | 39a (4069 pin 9, D) | top + rail, column 39 | |
| Mode wires: move them between + and − to change the mode · 8 wires | ||||
| 9 | D0 | 8j (4086 pin 2, B) | bottom + rail, column 8 | |
| 10 | D1 | 8a (4086 pin 13, D) | top − rail, column 8 | |
| 11 | D2 | 12j (4086 pin 6, F) | bottom + rail, column 12 | |
| 12 | D3 | 12a (4086 pin 9, H) | top + rail, column 12 | |
| 13 | D4 | 27j (4086 pin 2, B) | bottom − rail, column 27 | |
| 14 | D5 | 27a (4086 pin 13, D) | top − rail, column 27 | |
| 15 | D6 | 31j (4086 pin 6, F) | bottom + rail, column 31 | |
| 16 | D7 | 31a (4086 pin 9, H) | top − rail, column 31 | |
| Signal wires · 13 wires | ||||
| 26 | A | 21d (4028 pin 10, A) | 24f | |
| 27 | B | 18d (4028 pin 13, B) | 14f | |
| 28 | C | 19c (4028 pin 12, C) | 23f | |
| 29 | Q0 | 17g (4028 pin 3, Q0) | 7g (4086 pin 1, A) | |
| 30 | Q1 | 17a (4028 pin 14, Q1) | 9a (4086 pin 12, C) | |
| 31 | Q2 | 16g (4028 pin 2, Q2) | 11g (4086 pin 5, E) | |
| 32 | Q3 | 16c (4028 pin 15, Q3) | 13c (4086 pin 8, G) | |
| 33 | Q4 | 15i (4028 pin 1, Q4) | 26i (4086 pin 1, A) | |
| 34 | Q5 | 20g (4028 pin 6, Q5) | 28d (4086 pin 12, C) | |
| 35 | Q6 | 21h (4028 pin 7, Q6) | 30h (4086 pin 5, E) | |
| 36 | Q7 | 18g (4028 pin 4, Q7) | 32d (4086 pin 8, G) | |
| 37 | J1 | 9g (4086 pin 3, J) | 29b (4086 pin 11, EN) | |
| 38 | J2 | 28g (4086 pin 3, J) | 36g (4069 pin 3, B) | |
| Modules on male-female jumpers · 14 wires | ||||
| 39 | +5V | button C, + | top + rail, column 18 | |
| 40 | C | button C, OUT | 19a (4028 pin 12, C) | |
| 41 | GND | button C, − | top − rail, column 21 | |
| 42 | +5V | button B, + | top + rail, column 23 | |
| 43 | B | button B, OUT | 18a (4028 pin 13, B) | |
| 44 | GND | button B, − | top − rail, column 25 | |
| 45 | +5V | button A, + | top + rail, column 28 | |
| 46 | A | button A, OUT | 21a (4028 pin 10, A) | |
| 47 | GND | button A, − | top − rail, column 29 | |
| 48 | GND | LED module, G | bottom − rail, column 20 | |
| 49 | C | LED module, LED 1 | 23j | |
| 50 | B | LED module, LED 2 | 14j | |
| 51 | A | LED module, LED 3 | 24j | |
| 52 | OUT | LED module, LED 4 | 37j (4069 pin 4, H) | |
Test it
- All buttons up: OUT shows D0. Step through the select: OUT shows D0 … D7.
- Pull the wire from ENABLE of the second 4086 and put it on +5V: now the data D0–D3 never reach the output. Put it back.
If it does not work: first check the decoder: put a spare LED lead on each of its outputs in turn while you step through the select. Then check one gate at a time. If one data input never appears, its wire or its gate is wrong; if the wrong one appears, two decoder wires are swapped.
8SystemVerilog: the generic design
mux_8_aoi.sv describes schematic 1: the decoder as a shift of a single 1, the AND gates as one bitwise AND, and the merge. It is synthesizable.
// Exercise M6: an 8:1 multiplexer from a decoder, two AND-OR-INVERT gates // and an inverter. // // Generic version that matches schematic 1, not tied to any chip. Each // AND-OR-INVERT gate, like the 4086, ORs four AND terms and inverts the // result. The output of the first one goes to ENABLE of the second: when // one of the first four terms is 1, j1 is 0 and forces j2 to 0, just as a // fifth term would. The inverter turns j2 back into the selected data // bit. Synthesizable. module mux_8_aoi ( input logic [7:0] d, // d [0] is D0 input logic [2:0] sel, // C B A: sel [2] is C, weight 4 output logic mux_out ); wire [7:0] q = 8'b1 << sel; // The decoder, like the 4028 with D = 0 wire j1 = ~ (q [0] & d [0] | q [1] & d [1] | q [2] & d [2] | q [3] & d [3]); wire j2 = ~ (q [4] & d [4] | q [5] & d [5] | q [6] & d [6] | q [7] & d [7] | ~ j1); assign mux_out = ~ j2; endmodule
9SystemVerilog models of the chips
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.
// CD4028B: BCD-to-decimal decoder. // // Ports in pin order, named as in the datasheet. The power pins, VSS (8) // and VDD (16), are left out. Synthesizable. // // The output whose number is the code D C B A is 1, all others 0; codes // 10-15 set every output to 0. With D = 0 it is a 1-of-8 decoder. module cd4028 ( output logic q4, // Pin 1 output logic q2, // Pin 2 output logic q0, // Pin 3 output logic q7, // Pin 4 output logic q9, // Pin 5 output logic q5, // Pin 6 output logic q6, // Pin 7 // Pin 8: VSS output logic q8, // Pin 9 input logic a, // Pin 10: weight 1 input logic d, // Pin 11: weight 8 input logic c, // Pin 12: weight 4 input logic b, // Pin 13: weight 2 output logic q1, // Pin 14 output logic q3 // Pin 15 // Pin 16: VDD ); wire [3:0] code = { d, c, b, a }; assign q0 = code == 4'd0; assign q1 = code == 4'd1; assign q2 = code == 4'd2; assign q3 = code == 4'd3; assign q4 = code == 4'd4; assign q5 = code == 4'd5; assign q6 = code == 4'd6; assign q7 = code == 4'd7; assign q8 = code == 4'd8; assign q9 = code == 4'd9; endmodule
// CD4086B: expandable 4-wide 2-input AND-OR-INVERT gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out, and so is the unconnected pin 4. // Synthesizable. // // J = ~ (A & B | C & D | E & F | G & H | INHIBIT | ~ ENABLE). For a plain // 4-wide AND-OR-INVERT gate tie INHIBIT to GND and ENABLE to +5V. A // second 4086 can extend the gate through INHIBIT or ENABLE. module cd4086 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3 // Pin 4: not connected input logic e, // Pin 5 input logic f, // Pin 6 // Pin 7: VSS input logic g, // Pin 8 input logic h, // Pin 9 input logic inhibit, // Pin 10: 1 = J goes to 0 input logic enable, // Pin 11: 0 = J goes to 0 input logic c, // Pin 12 input logic d // Pin 13 // Pin 14: VDD ); assign j = ~ (a & b | c & d | e & f | g & h | inhibit | ~ enable); endmodule
// CD4069UB: hex inverter (six NOT gates). // // Ports in pin order, named as in the datasheet: inputs A-F, outputs G-L. // The power pins, VSS (7) and VDD (14), are left out. Synthesizable. module cd4069 ( input logic a, // Pin 1 output logic g, // Pin 2: g = ~ a input logic b, // Pin 3 output logic h, // Pin 4: h = ~ b input logic c, // Pin 5 output logic i, // Pin 6: i = ~ c // Pin 7: VSS output logic j, // Pin 8: j = ~ d input logic d, // Pin 9 output logic k, // Pin 10: k = ~ e input logic e, // Pin 11 output logic l, // Pin 12: l = ~ f input logic f // Pin 13 // Pin 14: VDD ); assign g = ~ a; assign h = ~ b; assign i = ~ c; assign j = ~ d; assign k = ~ e; assign l = ~ f; endmodule
10SystemVerilog: the chip-level design
mux_8_aoi_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as mux_8_aoi.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 M6, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as mux_8_aoi.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_m5678.py, not this file. Synthesizable. module mux_8_aoi_chips ( input logic [7:0] d, input logic [2:0] sel, output logic mux_out ); // Connections between the chips that are not ports wire q0; wire j1; wire q2; wire q3; wire q1; wire q4; wire q7; wire q5; wire q6; wire j2; // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; wire vdd = 1'b1; // Outputs that go nowhere wire unused_u1_q9; wire unused_u1_q8; wire unused_u4_g; wire unused_u4_i; wire unused_u4_j; wire unused_u4_k; wire unused_u4_l; //------------------------------------------------------------------------ // U2: CD4086 expandable 4-wide 2-input AND-OR-INVERT gate, pin 1 in column 7 cd4086 i_u2 ( .a ( q0 ), // Pin 1 <- U1 pin 3 (Q0) .b ( d [0] ), // Pin 2 <- D0 wire, at +5V on the breadboard .j ( j1 ), // Pin 3 -> J1 .e ( q2 ), // Pin 5 <- U1 pin 2 (Q2) .f ( d [2] ), // Pin 6 <- D2 wire, at +5V on the breadboard .g ( q3 ), // Pin 8 <- U1 pin 15 (Q3) .h ( d [3] ), // Pin 9 <- D3 wire, at +5V on the breadboard .inhibit ( gnd ), // Pin 10 <- GND .enable ( vdd ), // Pin 11 <- +5V .c ( q1 ), // Pin 12 <- U1 pin 14 (Q1) .d ( d [1] ) // Pin 13 <- D1 wire, at GND on the breadboard ); //------------------------------------------------------------------------ // U1: CD4028 BCD-to-decimal decoder, pin 1 in column 15 cd4028 i_u1 ( .q4 ( q4 ), // Pin 1 -> Q4 .q2 ( q2 ), // Pin 2 -> Q2 .q0 ( q0 ), // Pin 3 -> Q0 .q7 ( q7 ), // Pin 4 -> Q7 .q9 ( unused_u1_q9 ), // Pin 5, unused output .q5 ( q5 ), // Pin 6 -> Q5 .q6 ( q6 ), // Pin 7 -> Q6 .q8 ( unused_u1_q8 ), // Pin 9, unused output .a ( sel [0] ), // Pin 10 <- button A .d ( gnd ), // Pin 11 <- GND .c ( sel [2] ), // Pin 12 <- button C .b ( sel [1] ), // Pin 13 <- button B .q1 ( q1 ), // Pin 14 -> Q1 .q3 ( q3 ) // Pin 15 -> Q3 ); //------------------------------------------------------------------------ // U3: CD4086 expandable 4-wide 2-input AND-OR-INVERT gate, pin 1 in column 26 cd4086 i_u3 ( .a ( q4 ), // Pin 1 <- U1 pin 1 (Q4) .b ( d [4] ), // Pin 2 <- D4 wire, at GND on the breadboard .j ( j2 ), // Pin 3 -> J2 .e ( q6 ), // Pin 5 <- U1 pin 7 (Q6) .f ( d [6] ), // Pin 6 <- D6 wire, at +5V on the breadboard .g ( q7 ), // Pin 8 <- U1 pin 4 (Q7) .h ( d [7] ), // Pin 9 <- D7 wire, at GND on the breadboard .inhibit ( gnd ), // Pin 10 <- GND .enable ( j1 ), // Pin 11 <- U2 pin 3 (J) .c ( q5 ), // Pin 12 <- U1 pin 6 (Q5) .d ( d [5] ) // Pin 13 <- D5 wire, at GND on the breadboard ); //------------------------------------------------------------------------ // U4: CD4069 hex inverter (six NOT gates), pin 1 in column 34 cd4069 i_u4 ( .a ( gnd ), // Pin 1 <- GND, unused input .g ( unused_u4_g ), // Pin 2, unused output .b ( j2 ), // Pin 3 <- U3 pin 3 (J) .h ( mux_out ), // Pin 4 -> OUT .c ( gnd ), // Pin 5 <- GND, unused input .i ( unused_u4_i ), // Pin 6, unused output .j ( unused_u4_j ), // Pin 8, unused output .d ( vdd ), // Pin 9 <- +5V, unused input .k ( unused_u4_k ), // Pin 10, unused output .e ( vdd ), // Pin 11 <- +5V, unused input .l ( unused_u4_l ), // Pin 12, unused output .f ( vdd ) // Pin 13 <- +5V, unused input ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with a reference model: the data bit
with the number of the select, written as an index. 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 all 2048 combinations of data and select 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. The output
below is shortened.
tb.sv151 lines · show the codefile
// Testbench for exercise M6. // // Runs the generic design (mux_8_aoi.sv) and the chip-level design // (mux_8_aoi_chips.sv) side by side and compares both with // the data input with the number of the select. 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 2048 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 [7:0] d; logic [2:0] sel; wire mux_out, mux_out_chips; mux_8_aoi i_generic ( .d ( d ), .sel ( sel ), .mux_out ( mux_out ) ); mux_8_aoi_chips i_chips ( .d ( d ), .sel ( sel ), .mux_out ( mux_out_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the data input with the number of the select. // Bits, from the left: mux_out. function automatic logic [0:0] expected (input logic [7:0] in_d, input logic [2:0] in_sel); return in_d [in_sel]; endfunction wire [0:0] out = { mux_out }; wire [0:0] out_chips = { mux_out_chips }; wire [0:0] out_model = expected (d, sel); //------------------------------------------------------------------------ // 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 [2047: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 | 2048'b1 << { d, sel }; // Print the rows with the data wires as on the breadboard, and every error if (d == 8'b01001101 | out !== out_model | out_chips !== out_model) $display ("%5d %b %b %b %s %s", cycle + 1, d, sel, mux_out, 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 D7..D0 CBA OUT generic chips"); checking <= '1; for (int i = 0; i < 2048; i ++) begin { d, sel } <= 11' (i) ^ 11'h268; @ (posedge clk); end for (int i = 0; i < 2048; i ++) begin { d, sel } <= 11' (i ^ (i >> 1)) ^ 11'h268; @ (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), 2048); 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 M6: 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_8_aoi.sv mux_8_aoi_chips.sv cd4028.sv cd4086.sv cd4069.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 M6: 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_8_aoi.sv mux_8_aoi_chips.sv cd4028.sv cd4086.sv cd4069.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 M6: 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_8_aoi.sv mux_8_aoi_chips.sv cd4028.sv cd4086.sv cd4069.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 D7..D0 CBA OUT generic chips
1 01001101 000 1 ok ok
2 01001101 001 0 ok ok
3 01001101 010 1 ok ok
4 01001101 011 1 ok ok
5 01001101 100 0 ok ok
6 01001101 101 0 ok ok
7 01001101 110 1 ok ok
8 01001101 111 0 ok ok
2049 01001101 000 1 ok ok
2050 01001101 001 0 ok ok
2051 01001101 011 1 ok ok
2052 01001101 010 1 ok ok
2053 01001101 110 1 ok ok
2054 01001101 111 0 ok ok
2055 01001101 101 0 ok ok
2056 01001101 100 0 ok ok
tb.sv:137: $finish called at 40965000 (1ps)
PASS: 8192 checks, no errors
To see that the testbench catches mistakes, break something and run it again: remove | ~ j1 from j2 in mux_8_aoi.sv, or connect ENABLE of the second 4086 to vdd instead of j1 in mux_8_aoi_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_8_aoi.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 data are fixed in lab_top.sv as the wires of the breadboard: change the constants to try others.
The keys are the select, C, B, A. To run the chip-level design instead, add mux_8_aoi_chips.sv and the chip models
and change the module name in the instance.
bgm_lab/lab_top.sv97 lines · show the codefile
// Exercise M6 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_8_aoi.sv. For the chip-level design add // mux_8_aoi_chips.sv and cd4028.sv, cd4086.sv, cd4069.sv instead, and change // the module name below. // // Keys: key [2:0] sel. The LEDs: led [0] mux_out. A board with fewer LEDs // shows the first ones. // // The data are fixed at D7-D0 = 01001101, as the wires on the breadboard; // change the constant to try others. `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 mux_out; mux_8_aoi i_mux_8_aoi ( .sel ( key [2:0] ), .d ( 8'b01001101 ), .mux_out ( mux_out ) ); assign led = w_led' ({ mux_out }); 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_8_aoi.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
The keys are the select, the data are fixed as in lab_top; the screen shows the select in blue and the
output in green.
bgm_hackathon/hackathon_top.sv81 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise M6 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_8_aoi.sv. // // Keys: key [2:0] sel. The LEDs: led [0] mux_out. The screen shows 4 lamps in // a row: A, B, C, OUT; inputs in blue, outputs in green. // // The data are fixed at D7-D0 = 01001101, as the wires on the breadboard; // change the constant to try others. 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 mux_out; mux_8_aoi i_mux_8_aoi ( .sel ( sel ), .d ( 8'b01001101 ), .mux_out ( mux_out ) ); assign led = { 7'b0, mux_out }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 4 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 [3:0] lamp = { mux_out, sel }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 4; i ++) if (x >= 9' (124 + i * 64) & x < 9' (164 + 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 the select, C, B, A; the data are fixed in project.v. The segments of the display show
the output.
tiny_tapeout/project.v49 lines · show the codefile
/* * Exercise M6 on Tiny Tapeout: an 8:1 multiplexer from a decoder and gates, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add mux_8_aoi.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 data are fixed at D7-D0 = 01001101, as the wires on the breadboard; // change the constant to try others. wire mux_out; mux_8_aoi i_mux_8_aoi ( .sel ( ui_in [2:0] ), .d ( 8'b01001101 ), .mux_out ( mux_out ) ); // 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) mux_out. assign uo_out = { 7'b0, mux_out }; 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_8_aoi
source_files:
- "project.v"
- "mux_8_aoi.sv"
15Going further
Tasks for students
- Without the inverter. Which output would you get if you left out the 4069? Is it still useful?
- INHIBIT instead of ENABLE. Do it the way the exercise list says: J1 through a spare inverter of the 4069 to INHIBIT of the second 4086, and ENABLE to +5V.
- Dual 4:1. Exercise M8 uses the same two chips for a dual 4:1 multiplexer.
Files of this exercise
- mux_8_aoi.sv: the generic design (section 8)
- cd4028.sv, cd4086.sv and cd4069.sv: chip models (section 9)
- mux_8_aoi_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 exercise is in the Mini-projects 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 chips are described in the Texas Instruments datasheets for the CD4028B, CD4086B and CD4069B.