Breadboard exercises/ M8EnglishРусский

Exercise M8 · mini-project · multiplexer

Dual 4:1 multiplexer from a 4555 and two 4086 AND-OR-INVERT gates

The dual 4:1 multiplexer of M7 once more, each channel one 4086 AND-OR-INVERT gate and an inverter.

4 chips4555, 4086, 4069
40 + 11 wireson the board + jumper leads to the modules
2 × (4 → 1)select B A
no clockthe output follows the inputs at once
Multiplexers from a decoder and gates

1What it is and why it matters

What it does. Buttons B and A give the select, wires X0–X3 and Y0–Y3 the data. Half of the 4555 makes the select one-hot, Q0–Q3. Each 4086 takes four Q·data terms: JX = NOT (Q0·X0 + Q1·X1 + Q2·X2 + Q3·X3) and the same for Y. Two inverters of a 4069 turn JX and JY into X and Y.

How it works. The 4086 holds four AND gates, the OR and the inversion: one chip per channel instead of a 4081 and half of a 4072. INHIBIT is at GND and ENABLE at +5V, so each 4086 is a plain 4-wide AND-OR-INVERT gate. M7 and M8 compute the same; the testbench checks both against the same reference.

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.

2The key elements: decode, gate, merge

B ADecoder output at 1Data inputs let throughX, Y with the wires as on the board
00Q0X0, Y01, 0
01Q1X1, Y10, 1
10Q2X2, Y21, 1
11Q3X3, Y31, 0

Waveforms

X3…X01101Y3…Y0011001110100B A012301230XY
From the simulation of tb.sv in Icarus Verilog: the first steps, with the data wires as on the breadboard and the select counting up. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD4555dual 1-of-4 decoder: one half turns the select into Q0-Q31$0.29Jameco / Futurlec
CD40864-wide 2-input AND-OR-INVERT: four AND terms, ORed and inverted (two chips)2$0.70Futurlec (futurlec.com)
CD4069hex inverter: undoes the inversion of the 40861$0.30Elexp (elexp.com)
Button modulesB, A; 2 from the pack of five2$0.66 / packaliexpress.us
8-LED moduleshows the select and the output1$0.38aliexpress.us
Breadboard, 830 points1$3.00amazon.com
Breadboard power module5 V on the rails1$1.00aliexpress.us
USB-to-DC cable, 9 Vpowers the module from a USB charger1$1.17aliexpress.us
Pre-cut jumper wires40 wires on the board (or male-male Dupont jumpers)40$11.95 / packjameco.com
Male-female Dupont jumpers11 leads to the buttons and the LED module11$1.30 / packaliexpress.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.

4555: dual 1-of-4 decoder

1Ea2Aa3Ba4Q0a5Q1a6Q2a7Q3a8VSS9Q3b10Q2b11Q1b12Q0b13Bb14Ab15Eb16VDD45552 × 1 of 4 decoder
CD4555B. · SVG · PNG

Two 1-of-4 decoders. Half a is used: select A on pin 2, B on pin 3, its enable ~Ea (pin 1, active low) at GND, outputs Q0–Q3 on pins 4–7. Half b is switched off: ~Eb (pin 15) at +5V, its inputs tied.

PinNameDirectionFunctionIn this circuit
1Eainputenable of decoder a, active low: 1 sets all its outputs to 0GND
2Aainputselect bit A of decoder a (weight 1)A
3Bainputselect bit B of decoder a (weight 2)B
4Q0aoutputdecoder a: 1 when B A = 0Q0
5Q1aoutputdecoder a: 1 when B A = 1Q1
6Q2aoutputdecoder a: 1 when B A = 2Q2
7Q3aoutputdecoder a: 1 when B A = 3Q3
8VSSpowerground, 0 VGND
9Q3boutputdecoder b: 1 when B A = 3not connected
10Q2boutputdecoder b: 1 when B A = 2not connected
11Q1boutputdecoder b: 1 when B A = 1not connected
12Q0boutputdecoder b: 1 when B A = 0not connected
13Bbinputselect bit B of decoder btied to +5V
14Abinputselect bit A of decoder btied to +5V
15Ebinputenable of decoder b, active low+5V
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4086: 4-wide 2-input AND-OR-INVERT

1A2B3J4NC5E6F7VSS8G9H10INH11EN12C13D14VDD4086AND-OR-INVERT 4 × 2
CD4086B. · SVG · PNG

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.

PinNameDirectionFunctionIn this circuit
1Ainputinput A of the first ANDQ0
2Binputinput B of the first ANDX0 (+5V)
3JoutputJ = NOT (A·B + C·D + E·F + G·H + INH + NOT EN)J1
4NC—not connected inside the chipnot connected
5Einputinput EQ2
6Finputinput FX2 (+5V)
7VSSpowerground, 0 VGND
8Ginputinput GQ3
9Hinputinput HX3 (+5V)
10INHinputINHIBIT/EXPAND: 1 forces J to 0; GND for a plain 4-wide gateGND
11ENinputENABLE/EXPAND: 0 forces J to 0; +5V for a plain 4-wide gate+5V
12Cinputinput CQ1
13Dinputinput DX1 (GND)
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4069: hex inverter

1A2G3B4H5C6I7VSS8J9D10K11E12L13F14VDD4069
CD4069B. · SVG · PNG

Hex inverter: undoes the inversion at the output of the 4086.

PinNameDirectionFunctionIn this circuit
1Ainputinput of the gate with its output on pin 2J1
2GoutputG = NOT AX
3Binputinput of the gate with its output on pin 4J2
4HoutputH = NOT BY
5Cinputinput of the gate with its output on pin 6tied to GND
6IoutputI = NOT Cnot connected
7VSSpowerground, 0 VGND
8JoutputJ = NOT Dnot connected
9Dinputinput of the gate with its output on pin 8tied to +5V
10KoutputK = NOT Enot connected
11Einputinput of the gate with its output on pin 10tied to +5V
12LoutputL = NOT Fnot connected
13Finputinput of the gate with its output on pin 12tied to +5V
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: decoder and gates

BAX0X1X2X3Y0Y1Y2Y34555BAEQ0Q1Q2Q3decoder408640864086408640864086408640864086408640694069BLED 1ALED 2XLED 3YLED 4
The circuit as a decoder and gates. This is what mux_4_dual_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 · PNG

6Schematic 2: connections between the chips

12345678910111213141516EaAaBaQ0aQ1aQ2aQ3aQ3bQ2bQ1bQ0bBbAbEb45552 × 1 of 4 decoder1234567891011121314ABJNCEFGHINHENCD4086AND-OR-INVERT 4 × 21234567891011121314ABJNCEFGHINHENCD4086AND-OR-INVERT 4 × 212345678910111213144069ABQ0Q1Q2Q3J1J2XYBALED module: LEDs 1-4Y1→ +5VY3→ GNDX1→ GNDX3→ +5VY0→ GNDY2→ +5VX0→ +5VX2→ +5V+5V
The same circuit as chips, pin by pin. The boxes with an arrow are the data wires, with the rail they start on. · SVG · PNG

Every connection, net by net. mux_4_dual_aoi_chips.sv in section 10 is written from the same list.

NetFromToWire
Bbutton B, OUT4555 3 (Ba), LED module, LED 1M-F, M-M
Abutton A, OUT4555 2 (Aa), LED module, LED 2M-F, M-M
Q04555 4 (Q0a)4086 1 (A), 4086 1 (A)M-M
Q14555 5 (Q1a)4086 12 (C), 4086 12 (C)M-M
Q24555 6 (Q2a)4086 5 (E), 4086 5 (E)M-M
Q34555 7 (Q3a)4086 8 (G), 4086 8 (G)M-M
X0X0 wire: +5V4086 2 (B)M-M
X1X1 wire: GND4086 13 (D)M-M
X2X2 wire: +5V4086 6 (F)M-M
X3X3 wire: +5V4086 9 (H)M-M
Y0Y0 wire: GND4086 2 (B)M-M
Y1Y1 wire: +5V4086 13 (D)M-M
Y2Y2 wire: +5V4086 6 (F)M-M
Y3Y3 wire: GND4086 9 (H)M-M
J14086 3 (J)4069 1 (A)M-M
J24086 3 (J)4069 3 (B)M-M
X4069 2 (G)LED module, LED 3M-F
Y4069 4 (H)LED module, LED 4M-F
+5V+5V4555 16 (VDD), 4086 14 (VDD), 4086 14 (VDD), 4069 14 (VDD), 4555 15 (Eb), 4555 14 (Ab), 4555 13 (Bb), 4086 11 (EN), 4086 11 (EN), 4069 13 (F), 4069 11 (E), 4069 9 (D)
GNDGND4555 8 (VSS), 4086 7 (VSS), 4086 7 (VSS), 4069 7 (VSS), 4555 1 (Ea), 4086 10 (INH), 4086 10 (INH), 4069 5 (C)
not connected4555 9 (Q3b), 4555 10 (Q2b), 4555 11 (Q1b), 4555 12 (Q0b), 4086 4 (NC), 4086 4 (NC), 4069 6 (I), 4069 8 (J), 4069 10 (K), 4069 12 (L)

7Schematic 3: the breadboard

−+−+1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950abcdefghij5V5VPOWER MODULE · 5V9V USB-DC4555408640864069X0X1X2X3Y0Y1Y2Y3Q0Q0Q1Q1Q2Q2Q3Q3J1J2+OUT−B+OUT−AG1B2A3X4Y56788-LED MODULELEDs 1-4 used
Every wire in its hole. Column numbers are printed along the top; rows a–e are above the gap, f–j below it. The buttons hang above the board, the LED module below it. The wires with a tag are the data: + is 1, − is 0. Wires drawn as loose arcs could not be laid flat between the others. · SVG · PNG
BAQ0Q1Q2Q3X0X1X2X3Y0Y1Y2Y3J1J2XY+5VGND

Build it step by step

  1. 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.
  2. Insert the chips across the gap, notch to the left: the 4555 with pin 1 in hole 10f; the 4086 with pin 1 in hole 19f; the 4086 with pin 1 in hole 27f; the 4069 with pin 1 in hole 36f. Read the numbers on the chips: many look alike.
  3. 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.
  4. The fixed inputs and tie-offs of the checklist: ~Ea of the 4555 to GND and ~Eb to +5V, INHIBIT of each 4086 to GND and ENABLE as the checklist says, the inputs of unused gates to the nearest rail.
  5. The data wires, each from its pin to a rail, in the positions of the checklist.
  6. The signal wires from the decoder to the gates and between the chips. Tick each wire as you insert it.
  7. With the power off, the buttons above the board and the LED module below it.
  8. Turn the power on.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 8 wires
1+5V10a (4555 pin 16, VDD)top + rail, column 10
2GND17j (4555 pin 8, VSS)bottom − rail, column 17
3+5V27a (4086 pin 14, VDD)top + rail, column 27
4GND33j (4086 pin 7, VSS)bottom − rail, column 33
5+5V19a (4086 pin 14, VDD)top + rail, column 19
6GND25j (4086 pin 7, VSS)bottom − rail, column 25
7+5V36a (4069 pin 14, VDD)top + rail, column 36
8GND42j (4069 pin 7, VSS)bottom − rail, column 42
Fixed inputs: CMOS inputs must not float · 12 wires
17GND10j (4555 pin 1, Ea)bottom − rail, column 10
18+5V11a (4555 pin 15, Eb)top + rail, column 11
19+5V12a (4555 pin 14, Ab)top + rail, column 12
20+5V13a (4555 pin 13, Bb)top + rail, column 13
21+5V22a (4086 pin 11, EN)top + rail, column 22
22GND23a (4086 pin 10, INH)top − rail, column 23
23+5V30a (4086 pin 11, EN)top + rail, column 30
24GND31a (4086 pin 10, INH)top − rail, column 31
25+5V37a (4069 pin 13, F)top + rail, column 37
26+5V39a (4069 pin 11, E)top + rail, column 39
27GND40j (4069 pin 5, C)bottom − rail, column 40
28+5V41a (4069 pin 9, D)top + rail, column 41
Mode wires: move them between + and − to change the mode · 8 wires
9X028j (4086 pin 2, B)bottom + rail, column 28
10X128a (4086 pin 13, D)top − rail, column 28
11X232j (4086 pin 6, F)bottom + rail, column 32
12X332a (4086 pin 9, H)top + rail, column 32
13Y020j (4086 pin 2, B)bottom − rail, column 20
14Y120a (4086 pin 13, D)top + rail, column 20
15Y224j (4086 pin 6, F)bottom + rail, column 24
16Y324a (4086 pin 9, H)top − rail, column 24
Signal wires · 12 wires
29A11g (4555 pin 2, Aa)8e
30B12g (4555 pin 3, Ba)9e
31Q013h (4555 pin 4, Q0a)19h (4086 pin 1, A)
32Q019g (4086 pin 1, A)27g (4086 pin 1, A)
33Q114g (4555 pin 5, Q1a)21d (4086 pin 12, C)
34Q121c (4086 pin 12, C)29c (4086 pin 12, C)
35Q215i (4555 pin 6, Q2a)23i (4086 pin 5, E)
36Q223h (4086 pin 5, E)31h (4086 pin 5, E)
37Q316g (4555 pin 7, Q3a)25d (4086 pin 8, G)
38Q325b (4086 pin 8, G)33b (4086 pin 8, G)
39J129g (4086 pin 3, J)36g (4069 pin 1, A)
40J221j (4086 pin 3, J)38j (4069 pin 3, B)
Modules on male-female jumpers · 11 wires
41+5Vbutton B, +top + rail, column 9
42Bbutton B, OUT9a
43GNDbutton B, −top − rail, column 12
44+5Vbutton A, +top + rail, column 15
45Abutton A, OUT8a
46GNDbutton A, −top − rail, column 17
47GNDLED module, Gbottom − rail, column 22
48BLED module, LED 112j (4555 pin 3, Ba)
49ALED module, LED 211j (4555 pin 2, Aa)
50XLED module, LED 337j (4069 pin 2, G)
51YLED module, LED 439j (4069 pin 4, H)

Test it

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_4_dual_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.

mux_4_dual_aoi.svfile
// Exercise M8: a dual 4:1 multiplexer from a decoder, two AND-OR-INVERT
// gates and two inverters.
//
// Generic version that matches schematic 1, not tied to any chip.  Each
// channel is one AND-OR-INVERT gate, like the 4086, followed by an
// inverter.  Synthesizable.

module mux_4_dual_aoi
(
    input  logic [3:0] x_ch,   // x_ch [0] is X0
    input  logic [3:0] y_ch,   // y_ch [0] is Y0
    input  logic [1:0] sel,    // B A: sel [1] is B, weight 2

    output logic       x_out,
    output logic       y_out
);

    wire [3:0] q = 4'b1 << sel;  // The decoder

    wire jx = ~| (q & x_ch);    // AND-OR-INVERT: NOT (Q0 X0 + Q1 X1 + Q2 X2 + Q3 X3)
    wire jy = ~| (q & y_ch);

    assign x_out = ~ jx;
    assign y_out = ~ jy;

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.

cd4555.svfile
// CD4555B: dual binary 1-of-4 decoder, outputs high on select.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.  Synthesizable.
//
// In each half the output whose number is B A goes to 1.  The enable input
// is active low: while it is 1, all four outputs of that half stay 0.

module cd4555
(
    input  logic enable_a_n,  // Pin 1:  0 = decoder a on
    input  logic a_a,         // Pin 2:  weight 1
    input  logic b_a,         // Pin 3:  weight 2
    output logic q0_a,        // Pin 4
    output logic q1_a,        // Pin 5
    output logic q2_a,        // Pin 6
    output logic q3_a,        // Pin 7
                              // Pin 8:  VSS
    output logic q3_b,        // Pin 9
    output logic q2_b,        // Pin 10
    output logic q1_b,        // Pin 11
    output logic q0_b,        // Pin 12
    input  logic b_b,         // Pin 13: weight 2
    input  logic a_b,         // Pin 14: weight 1
    input  logic enable_b_n   // Pin 15: 0 = decoder b on
                              // Pin 16: VDD
);

    assign q0_a = ~ enable_a_n & ~ b_a & ~ a_a;
    assign q1_a = ~ enable_a_n & ~ b_a &   a_a;
    assign q2_a = ~ enable_a_n &   b_a & ~ a_a;
    assign q3_a = ~ enable_a_n &   b_a &   a_a;

    assign q0_b = ~ enable_b_n & ~ b_b & ~ a_b;
    assign q1_b = ~ enable_b_n & ~ b_b &   a_b;
    assign q2_b = ~ enable_b_n &   b_b & ~ a_b;
    assign q3_b = ~ enable_b_n &   b_b &   a_b;

endmodule
cd4086.svfile
// 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
cd4069.svfile
// 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_4_dual_aoi_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect the chips, with the same ports as mux_4_dual_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.

mux_4_dual_aoi_chips.svfile
// Exercise M8, chip-level version: the chips and wires of schematic 2 and
// of the breadboard.  Same ports as mux_4_dual_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_4_dual_aoi_chips
(
    input  logic [3:0] x_ch,
    input  logic [3:0] y_ch,
    input  logic [1:0] sel,
    output logic       x_out,
    output logic       y_out
);

    // Connections between the chips that are not ports

    wire q0;
    wire q1;
    wire q2;
    wire q3;
    wire j2;
    wire j1;

    // 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_q3_b;
    wire unused_u1_q2_b;
    wire unused_u1_q1_b;
    wire unused_u1_q0_b;
    wire unused_u4_i;
    wire unused_u4_j;
    wire unused_u4_k;
    wire unused_u4_l;

    //------------------------------------------------------------------------

    // U1: CD4555 dual 1-of-4 decoder, pin 1 in column 10

    cd4555 i_u1
    (
        .enable_a_n ( gnd            ),  // Pin 1 <- GND
        .a_a        ( sel [0]        ),  // Pin 2 <- button A
        .b_a        ( sel [1]        ),  // Pin 3 <- button B
        .q0_a       ( q0             ),  // Pin 4 -> Q0
        .q1_a       ( q1             ),  // Pin 5 -> Q1
        .q2_a       ( q2             ),  // Pin 6 -> Q2
        .q3_a       ( q3             ),  // Pin 7 -> Q3
        .q3_b       ( unused_u1_q3_b ),  // Pin 9, unused output
        .q2_b       ( unused_u1_q2_b ),  // Pin 10, unused output
        .q1_b       ( unused_u1_q1_b ),  // Pin 11, unused output
        .q0_b       ( unused_u1_q0_b ),  // Pin 12, unused output
        .b_b        ( vdd            ),  // Pin 13 <- +5V, unused input
        .a_b        ( vdd            ),  // Pin 14 <- +5V, unused input
        .enable_b_n ( vdd            )   // Pin 15 <- +5V
    );

    //------------------------------------------------------------------------

    // U3: CD4086 expandable 4-wide 2-input AND-OR-INVERT gate, pin 1 in column 19

    cd4086 i_u3
    (
        .a       ( q0       ),  // Pin 1 <- U1 pin 4 (Q0a)
        .b       ( y_ch [0] ),  // Pin 2 <- Y0 wire, at GND on the breadboard
        .j       ( j2       ),  // Pin 3 -> J2
        .e       ( q2       ),  // Pin 5 <- U1 pin 6 (Q2a)
        .f       ( y_ch [2] ),  // Pin 6 <- Y2 wire, at +5V on the breadboard
        .g       ( q3       ),  // Pin 8 <- U1 pin 7 (Q3a)
        .h       ( y_ch [3] ),  // Pin 9 <- Y3 wire, at GND on the breadboard
        .inhibit ( gnd      ),  // Pin 10 <- GND
        .enable  ( vdd      ),  // Pin 11 <- +5V
        .c       ( q1       ),  // Pin 12 <- U1 pin 5 (Q1a)
        .d       ( y_ch [1] )   // Pin 13 <- Y1 wire, at +5V on the breadboard
    );

    //------------------------------------------------------------------------

    // U2: CD4086 expandable 4-wide 2-input AND-OR-INVERT gate, pin 1 in column 27

    cd4086 i_u2
    (
        .a       ( q0       ),  // Pin 1 <- U1 pin 4 (Q0a)
        .b       ( x_ch [0] ),  // Pin 2 <- X0 wire, at +5V on the breadboard
        .j       ( j1       ),  // Pin 3 -> J1
        .e       ( q2       ),  // Pin 5 <- U1 pin 6 (Q2a)
        .f       ( x_ch [2] ),  // Pin 6 <- X2 wire, at +5V on the breadboard
        .g       ( q3       ),  // Pin 8 <- U1 pin 7 (Q3a)
        .h       ( x_ch [3] ),  // Pin 9 <- X3 wire, at +5V on the breadboard
        .inhibit ( gnd      ),  // Pin 10 <- GND
        .enable  ( vdd      ),  // Pin 11 <- +5V
        .c       ( q1       ),  // Pin 12 <- U1 pin 5 (Q1a)
        .d       ( x_ch [1] )   // Pin 13 <- X1 wire, at GND on the breadboard
    );

    //------------------------------------------------------------------------

    // U4: CD4069 hex inverter (six NOT gates), pin 1 in column 36

    cd4069 i_u4
    (
        .a ( j1          ),  // Pin 1 <- U2 pin 3 (J)
        .g ( x_out       ),  // Pin 2 -> X
        .b ( j2          ),  // Pin 3 <- U3 pin 3 (J)
        .h ( y_out       ),  // Pin 4 -> Y
        .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 1024 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.sv162 lines · show the codefile
// Testbench for exercise M8.
//
// Runs the generic design (mux_4_dual_aoi.sv) and the chip-level design
// (mux_4_dual_aoi_chips.sv) side by side and compares both with
// the channels 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 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 [3:0] x_ch;
    logic [3:0] y_ch;
    logic [1:0] sel;

    wire  x_out, x_out_chips;
    wire  y_out, y_out_chips;

    mux_4_dual_aoi i_generic
    (
        .x_ch  ( x_ch  ),
        .y_ch  ( y_ch  ),
        .sel   ( sel   ),
        .x_out ( x_out ),
        .y_out ( y_out )
    );

    mux_4_dual_aoi_chips i_chips
    (
        .x_ch  ( x_ch        ),
        .y_ch  ( y_ch        ),
        .sel   ( sel         ),
        .x_out ( x_out_chips ),
        .y_out ( y_out_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the channels with the number of the select.
    // Bits, from the left: x_out, y_out.

    function automatic logic [1:0] expected
    (
        input logic [3:0] in_x_ch,
        input logic [3:0] in_y_ch,
        input logic [1:0] in_sel
    );
        return { in_x_ch [in_sel], in_y_ch [in_sel] };
    endfunction

    wire [1:0] out       = { x_out, y_out };
    wire [1:0] out_chips = { x_out_chips, y_out_chips };
    wire [1:0] out_model = expected (x_ch, y_ch, 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 [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 };

            // Print the rows with the data wires as on the breadboard, and every error

            if (x_ch == 4'b1101 & y_ch == 4'b0110 | out !== out_model | out_chips !== out_model)
                $display ("%5d  %b    %b    %b  %b  %b  %s  %s",
                    cycle + 1, x_ch, y_ch, sel, x_out, y_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  X3..X0  Y3..Y0  BA  X  Y  generic  chips");

        checking <= '1;

        for (int i = 0; i < 1024; i ++)
        begin
            { x_ch, y_ch, sel } <= 10' (i) ^ 10'h358;
            @ (posedge clk);
        end

        for (int i = 0; i < 1024; i ++)
        begin
            { x_ch, y_ch, sel } <= 10' (i ^ (i >> 1)) ^ 10'h358;
            @ (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 M8: 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_4_dual_aoi.sv mux_4_dual_aoi_chips.sv cd4555.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 M8: 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_4_dual_aoi.sv mux_4_dual_aoi_chips.sv cd4555.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 M8: 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_4_dual_aoi.sv mux_4_dual_aoi_chips.sv cd4555.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  X3..X0  Y3..Y0  BA  X  Y  generic  chips
    1  1101    0110    00  1  0  ok       ok   
    2  1101    0110    01  0  1  ok       ok   
    3  1101    0110    10  1  1  ok       ok   
    4  1101    0110    11  1  0  ok       ok   
 1025  1101    0110    00  1  0  ok       ok   
 1026  1101    0110    01  0  1  ok       ok   
 1027  1101    0110    11  1  0  ok       ok   
 1028  1101    0110    10  1  1  ok       ok   
tb.sv:148: $finish called at 20485000 (1ps)
PASS: 4096 checks, no errors

To see that the testbench catches mistakes, break something and run it again: remove the inverter, assign x_out = jx;, in mux_4_dual_aoi.sv, or connect ENABLE of one 4086 to gnd in mux_4_dual_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.

  1. Copy labs/1_basics/1_02_mux to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add mux_4_dual_aoi.sv. The BGM scripts compile every .sv file of the directory except tb.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./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, B, A. To run the chip-level design instead, add mux_4_dual_aoi_chips.sv and the chip models and change the module name in the instance.

bgm_lab/lab_top.sv100 lines · show the codefile
// Exercise M8 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_4_dual_aoi.sv. For the chip-level design add
// mux_4_dual_aoi_chips.sv and cd4555.sv, cd4086.sv, cd4069.sv instead, and
// change the module name below.
//
// Keys: key [1:0] sel. The LEDs: led [0] x_out, led [1] y_out. A board with
// fewer LEDs shows the first ones.
//
// The data are fixed at X3-X0 = 1101 and Y3-Y0 = 0110, as the wires on the
// breadboard.

`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 x_out;
    logic y_out;

    mux_4_dual_aoi i_mux_4_dual_aoi
    (
        .sel   ( key [1:0] ),
        .x_ch  ( 4'b1101   ),
        .y_ch  ( 4'b0110   ),
        .x_out ( x_out     ),
        .y_out ( y_out     )
    );

    assign led = w_led' ({ y_out, x_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.

  1. Copy hackathon/problems/1_gates_and_muxes.
  2. Replace its hackathon_top.sv with bgm_hackathon/hackathon_top.sv and add mux_4_dual_aoi.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./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.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 M8 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_4_dual_aoi.sv.
//
// Keys: key [1:0] sel. The LEDs: led [0] x_out, led [1] y_out. The screen
// shows 4 lamps in a row: A, B, X, Y; inputs in blue, outputs in green.
//
// The data are fixed at X3-X0 = 1101 and Y3-Y0 = 0110, as the wires on the
// breadboard.
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 [1:0] sel = key [1:0];

    logic x_out;
    logic y_out;

    mux_4_dual_aoi i_mux_4_dual_aoi
    (
        .sel   ( sel     ),
        .x_ch  ( 4'b1101 ),
        .y_ch  ( 4'b0110 ),
        .x_out ( x_out   ),
        .y_out ( y_out   )
    );

    assign led      = { 6'b0, y_out, x_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 = { y_out, x_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 < 2)
                    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, B, A; the data are fixed in project.v. The segments of the display show the outputs.

tiny_tapeout/project.v52 lines · show the codefile
/*
 * Exercise M8 on Tiny Tapeout: a dual 4: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_4_dual_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 1-0 are sel
    // The data are fixed at X3-X0 = 1101 and Y3-Y0 = 0110, as the wires on
    // the breadboard.

    wire x_out;
    wire y_out;

    mux_4_dual_aoi i_mux_4_dual_aoi (
        .sel   ( ui_in [1:0] ),
        .x_ch  ( 4'b1101     ),
        .y_ch  ( 4'b0110     ),
        .x_out ( x_out       ),
        .y_out ( y_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) x_out, b (top right) y_out.

    assign uo_out  = { 6'b0, y_out, x_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:2], 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_4_dual_aoi
  source_files:
    - "project.v"
    - "mux_4_dual_aoi.sv"

15Going further

Tasks for students

Files of this exercise

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 CD4555B, CD4086B and CD4069B.