Breadboard exercises/ B3dEnglishРусский

Exercise B3d · basics · logic gates

Explore a gate chip: the 8-input gates

Two chips with one 8-input gate each, and two outputs per gate: the 4068 gives AND and NAND, the 4078 OR and NOR. Four inputs come from buttons; the other four are wires to a rail that you move by hand.

2 chips4068 AND/NAND, 4078 OR/NOR
18 + 21 wireson the board + jumper leads to the modules
8 inputs4 buttons + 4 wires per chip
no clockthe outputs follow the buttons at once
Four pinout families

1What it is and why it matters

What it does. Buttons A, B, C and D drive one gate of each chip of the family: 4068 (AND), 4068 (NAND), 4078 (OR), 4078 (NOR). The LED module shows the 4 inputs on LEDs 1–4 and the 4 outputs on LEDs 5–8, so each press of the buttons is one row of 4 truth tables at once. The exercise comes from lesson 1.1 of the Habr article: each student gets one chip and its datasheet, describes the function in words and writes the truth table. This page puts the whole family on one board; a student with one chip builds only its part, with the same wires.

How it works. Chips of one family have the same pinout: the 8-input gates: one gate with inputs on pins 2–5 and 9–12, the plain output on pin 1 and the inverted one on pin 13. Pins 6 and 8 are not connected inside. Only the gate inside differs. The datasheet says which gate, and a few presses on the board confirm it.

Where it is used. Every digital circuit is made of such gates. Wide AND and OR gates recognise one combination of many signals: an address decoder, the “all bits zero” flag of an ALU, the state of a state machine. Inverting gates, NAND and NOR, are the simplest in CMOS, and libraries of standard cells for chip design are full of them.

Why learn it.

2The key elements: the gates of the family

AND

ABCDEFGHY

NAND

ABCDEFGHY

OR

ABCDEFGHY

NOR

ABCDEFGHY

A small circle on the output, a bubble, means NOT: NAND is NOT AND, NOR is NOT OR, XNOR is NOT XOR.

The functions in words

ChipGateIn wordsIn SystemVerilog
4068AND1 only when all inputs are 1& { h, g, f, e, d, c, b, a }
4068NAND0 only when all inputs are 1~& { h, ..., a }
4078OR0 only when all inputs are 0| { h, g, f, e, d, c, b, a }
4078NOR1 only when all inputs are 0~| { h, ..., a }

The truth tables

Every row of the board: the inputs and all 4 outputs. Inputs E–H are at their usual values, 1 for the 4068 and 0 for the 4078.

ABCDANDNANDORNOR
00000101
00010110
00100110
00110110
01000110
01010110
01100110
01110110
10000110
10010110
10100110
10110110
11000110
11010110
11100110
11111010

Waveforms

ABCDANDNANDORNOR
From the simulation of tb.sv in Icarus Verilog. The testbench sets the inputs right after a rising edge of its clock (dotted lines) and checks the outputs at the next edge. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD40788-input OR and NOR: both outputs used1$0.32Elexp (elexp.com)
CD40688-input AND and NAND: both outputs used1$0.39Elexp (elexp.com)
Button modulesA, B, C, D; 4 from the pack of five4$0.66 / packaliexpress.us
8-LED moduleshows the inputs and every 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
Male-male Dupont jumpers18 wires on the board (or pre-cut wires)18$1.30 / packaliexpress.us
Male-female Dupont jumpers21 leads to the buttons and the LED module21$1.30 / packaliexpress.us

Prices from the LSSK components sheet; these chips are in the Comprehensive kit. A class can share the chips: each student needs only one.

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.

4068: 8-input AND and NAND

1K2A3B4C5D6NC7VSS8NC9E10F11G12H13J14VDD40688-input AND / NAND
CD4068B. · SVG · PNG

8-input AND on pin 1 (K) and NAND on pin 13 (J). Inputs E–H, pins 9–12, are wires to the + rail: a 1 does not change an AND, so the gate works as a 4-input AND of the buttons.

PinNameDirectionFunctionIn this circuit
1KoutputK = A AND B AND C AND D AND E AND F AND G AND HAND
2Ainputinput of the gate with its output on pin 1 / 13A
3Binputinput of the gate with its output on pin 1 / 13B
4Cinputinput of the gate with its output on pin 1 / 13C
5Dinputinput of the gate with its output on pin 1 / 13D
6NC—not connected inside the chipnot connected
7VSSpowerground, 0 VGND
8NC—not connected inside the chipnot connected
9Einputinput of the gate with its output on pin 1 / 13E (+5V)
10Finputinput of the gate with its output on pin 1 / 13F (+5V)
11Ginputinput of the gate with its output on pin 1 / 13G (+5V)
12Hinputinput of the gate with its output on pin 1 / 13H (+5V)
13JoutputJ = NOT (A AND B AND C AND D AND E AND F AND G AND H)NAND
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4078: 8-input OR and NOR

1K2A3B4C5D6NC7VSS8NC9E10F11G12H13J14VDD40788-input OR / NOR
CD4078B. · SVG · PNG

8-input OR on pin 1 and NOR on pin 13. Its inputs E–H are wires to the − rail: a 0 does not change an OR.

PinNameDirectionFunctionIn this circuit
1KoutputK = A OR B OR C OR D OR E OR F OR G OR HOR
2Ainputinput of the gate with its output on pin 1 / 13A
3Binputinput of the gate with its output on pin 1 / 13C
4Cinputinput of the gate with its output on pin 1 / 13B
5Dinputinput of the gate with its output on pin 1 / 13D
6NC—not connected inside the chipnot connected
7VSSpowerground, 0 VGND
8NC—not connected inside the chipnot connected
9Einputinput of the gate with its output on pin 1 / 13E (GND)
10Finputinput of the gate with its output on pin 1 / 13F (GND)
11Ginputinput of the gate with its output on pin 1 / 13G (GND)
12Hinputinput of the gate with its output on pin 1 / 13H (GND)
13JoutputJ = NOT (A OR B OR C OR D OR E OR F OR G OR H)NOR
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: the gates

ABCD4068406840784078ALED 1BLED 2CLED 3DLED 4ANDLED 5NANDLED 6ORLED 7NORLED 8
The circuit as gates, without chips. This is what gates_8_input.sv describes. A dot means a connection; crossing lines without a dot are not connected. The chip under each gate is the one that holds it on the breadboard. · SVG · PNG

The four inputs E–H of each chip are drawn as fixed values: +5V for the 4068, GND for the 4078.

6Schematic 2: connections between the chips

1234567891011121314KABCDNCNCEFGHJ40788-input OR / NOR1234567891011121314KABCDNCNCEFGHJ40688-input AND / NANDABCDANDNANDORNORABCDLED module: LEDs 1-8H→ GNDG→ GNDF→ GNDE→ GNDH→ +5VG→ +5VF→ +5VE→ +5V+5V
The same circuit as chips, pin by pin. Each chip is drawn from above; a chip turned around on the breadboard is drawn turned around here too. The gates in use are drawn in full, the unused ones faded, their inputs tied to +5V or GND. The boxes at the bottom are the modules. · SVG · PNG

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

NetFromToWire
Abutton A, OUT4068 2 (A), 4078 2 (A), LED module, LED 1M-F, M-M
Bbutton B, OUT4068 3 (B), 4078 4 (C), LED module, LED 2M-F, M-M
Cbutton C, OUT4068 4 (C), 4078 3 (B), LED module, LED 3M-F, M-M
Dbutton D, OUT4068 5 (D), 4078 5 (D), LED module, LED 4M-F, M-M
AND4068 1 (K)LED module, LED 5M-F
NAND4068 13 (J)LED module, LED 6M-F, M-M
OR4078 1 (K)LED module, LED 7M-F
NOR4078 13 (J)LED module, LED 8M-F, M-M
+5V+5V4078 14 (VDD), 4068 14 (VDD)
GNDGND4078 7 (VSS), 4068 7 (VSS)
not connected4078 6 (NC), 4078 8 (NC), 4068 6 (NC), 4068 8 (NC)

7Schematic 3: the breadboard

−+−+1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950abcdefghij5V5VPOWER MODULE · 5V9V USB-DC40784068EFGHEFGHABCD+OUT−A+OUT−B+OUT−C+OUT−DG1A2B3C4D5AND6NAND7OR8NOR8-LED MODULELEDs 1-8 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 and the LED module hang below the board on male-female jumpers. The grey wires with letters are inputs E–H: move them between + and −. · SVG · PNG
ABCDANDNANDORNOR+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 4078 with pin 1 in hole 8f; the 4068 with pin 1 in hole 16f. 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 E–H wires: pins 9–12 of the 4068 to the top + rail, pins 9–12 of the 4078 to the top − rail. These are the wires you will move in the test.
  5. The tie-offs: the inputs of the unused gates to the nearest rail, top pins to +, bottom pins to −.
  6. The signal wires, in the order of the checklist: each input line from chip to chip. Tick each wire as you insert it.
  7. With the power off, the modules: the buttons and the LED module below the board. Connect the − of each button and G of the LED module first.
  8. Turn the power on.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 4 wires
1+5V8a (4078 pin 14, VDD)top + rail, column 8
2GND14j (4078 pin 7, VSS)bottom − rail, column 14
3+5V16a (4068 pin 14, VDD)top + rail, column 16
4GND22j (4068 pin 7, VSS)bottom − rail, column 22
Mode wires: move them between + and − to change the mode · 8 wires
5E21a (4068 pin 9, E)top + rail, column 21
6F20a (4068 pin 10, F)top + rail, column 20
7G19a (4068 pin 11, G)top + rail, column 19
8H18a (4068 pin 12, H)top + rail, column 18
9E13a (4078 pin 9, E)top − rail, column 13
10F12a (4078 pin 10, F)top − rail, column 12
11G11a (4078 pin 11, G)top − rail, column 11
12H10a (4078 pin 12, H)top − rail, column 10
Signal wires · 6 wires
13A17g (4068 pin 2, A)9g (4078 pin 2, A)
14B18g (4068 pin 3, B)11g (4078 pin 4, C)
15C19h (4068 pin 4, C)10i (4078 pin 3, B)
16D20h (4068 pin 5, D)12h (4078 pin 5, D)
17NAND17d (4068 pin 13, J)15f
18NOR9d (4078 pin 13, J)7f
Modules on male-female jumpers · 21 wires
19+5Vbutton A, +bottom + rail, column 10
20Abutton A, OUT9j (4078 pin 2, A)
21GNDbutton A, −bottom − rail, column 12
22+5Vbutton B, +bottom + rail, column 15
23Bbutton B, OUT18j (4068 pin 3, B)
24GNDbutton B, −bottom − rail, column 17
25+5Vbutton C, +bottom + rail, column 20
26Cbutton C, OUT19j (4068 pin 4, C)
27GNDbutton C, −bottom − rail, column 21
28+5Vbutton D, +bottom + rail, column 25
29Dbutton D, OUT20j (4068 pin 5, D)
30GNDbutton D, −bottom − rail, column 27
31GNDLED module, Gbottom − rail, column 30
32ALED module, LED 117j (4068 pin 2, A)
33BLED module, LED 218i (4068 pin 3, B)
34CLED module, LED 319i (4068 pin 4, C)
35DLED module, LED 420i (4068 pin 5, D)
36ANDLED module, LED 516j (4068 pin 1, K)
37NANDLED module, LED 615j
38ORLED module, LED 78j (4078 pin 1, K)
39NORLED module, LED 87j

Test it

If it does not work: when one output is wrong and the others are right, the inputs are fine: check the wires of that chip, and its power. An LED that flickers when you touch the wires has a floating input: look for a missing tie-off. If a chip gets warm, turn the power off at once: it is probably inserted the wrong way round.

8SystemVerilog: the generic design

gates_8_input.sv describes schematic 1: one assign statement per gate. It is synthesizable.

gates_8_input.svfile
// Exercise B3d: the 8-input gate chips 4068 (AND and NAND) and 4078 (OR
// and NOR).
//
// Generic version that matches schematic 1: inputs A-D are shared by both
// chips, and each chip has its own inputs E-H, which on the breadboard are
// wires to a rail: +5V for the 4068, GND for the 4078.  Synthesizable.

module gates_8_input
(
    input  logic       a,
    input  logic       b,
    input  logic       c,
    input  logic       d,
    input  logic [3:0] rest_and,  // E-H of the 4068, rest_and [0] is E
    input  logic [3:0] rest_or,   // E-H of the 4078, rest_or [0] is E

    output logic       y_and,     // 1 only when all eight inputs of the 4068 are 1
    output logic       y_nand,
    output logic       y_or,      // 0 only when all eight inputs of the 4078 are 0
    output logic       y_nor
);

    wire [7:0] and_inputs = { rest_and, d, c, b, a };
    wire [7:0] or_inputs  = { rest_or,  d, c, b, a };

    assign y_and  =   & and_inputs;
    assign y_nand = ~& and_inputs;
    assign y_or   =   | or_inputs;
    assign y_nor  = ~| or_inputs;

endmodule

The reduction operators & and | in front of a vector combine all its bits: & { a, b, c, d } is a & b & c & d, and ~& its NAND.

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.

cd4068.svfile
// CD4068B: 8-input AND/NAND gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out, and so are the unconnected pins 6 and 8.
// Synthesizable.
//
// One 8-input gate with two outputs: the plain one on pin 1 (K) and the
// inverted one on pin 13 (J).

module cd4068
(
    output logic k,  // Pin 1:  k = a & b & c & d & e & f & g & h
    input  logic a,  // Pin 2
    input  logic b,  // Pin 3
    input  logic c,  // Pin 4
    input  logic d,  // Pin 5
                     // Pin 6:  not connected
                     // Pin 7:  VSS
                     // Pin 8:  not connected
    input  logic e,  // Pin 9
    input  logic f,  // Pin 10
    input  logic g,  // Pin 11
    input  logic h,  // Pin 12
    output logic j   // Pin 13: j = ~ (a & b & c & d & e & f & g & h)
                     // Pin 14: VDD
);

    assign j = ~ (a & b & c & d & e & f & g & h);
    assign k = a & b & c & d & e & f & g & h;

endmodule
cd4078.svfile
// CD4078B: 8-input OR/NOR gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out, and so are the unconnected pins 6 and 8.
// Synthesizable.
//
// One 8-input gate with two outputs: the plain one on pin 1 (K) and the
// inverted one on pin 13 (J).

module cd4078
(
    output logic k,  // Pin 1:  k = a | b | c | d | e | f | g | h
    input  logic a,  // Pin 2
    input  logic b,  // Pin 3
    input  logic c,  // Pin 4
    input  logic d,  // Pin 5
                     // Pin 6:  not connected
                     // Pin 7:  VSS
                     // Pin 8:  not connected
    input  logic e,  // Pin 9
    input  logic f,  // Pin 10
    input  logic g,  // Pin 11
    input  logic h,  // Pin 12
    output logic j   // Pin 13: j = ~ (a | b | c | d | e | f | g | h)
                     // Pin 14: VDD
);

    assign j = ~ (a | b | c | d | e | f | g | h);
    assign k = a | b | c | d | e | f | g | h;

endmodule

10SystemVerilog: the chip-level design

gates_8_input_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect the chips, with the same ports as gates_8_input.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.

gates_8_input_chips.svfile
// Exercise B3d, chip-level version: the chips and wires of schematic 2 and
// of the breadboard.  Same ports as gates_8_input.sv.
//
// Generated by tools/bb_lib.py from the checked breadboard wire list;
// change the layout in tools/bb_b3.py, not this file.  Synthesizable.

module gates_8_input_chips
(
    input  logic       a,
    input  logic       b,
    input  logic       c,
    input  logic       d,
    input  logic [3:0] rest_and,
    input  logic [3:0] rest_or,
    output logic       y_and,
    output logic       y_nand,
    output logic       y_or,
    output logic       y_nor
);



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

    // U2: CD4078 8-input OR/NOR, pin 1 in column 8

    cd4078 i_u2
    (
        .k ( y_or        ),  // Pin 1 -> OR
        .a ( a           ),  // Pin 2 <- button A
        .b ( c           ),  // Pin 3 <- button C
        .c ( b           ),  // Pin 4 <- button B
        .d ( d           ),  // Pin 5 <- button D
        .e ( rest_or [0] ),  // Pin 9 <- E wire, at GND on the breadboard
        .f ( rest_or [1] ),  // Pin 10 <- F wire, at GND on the breadboard
        .g ( rest_or [2] ),  // Pin 11 <- G wire, at GND on the breadboard
        .h ( rest_or [3] ),  // Pin 12 <- H wire, at GND on the breadboard
        .j ( y_nor       )   // Pin 13 -> NOR
    );

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

    // U1: CD4068 8-input AND/NAND, pin 1 in column 16

    cd4068 i_u1
    (
        .k ( y_and        ),  // Pin 1 -> AND
        .a ( a            ),  // Pin 2 <- button A
        .b ( b            ),  // Pin 3 <- button B
        .c ( c            ),  // Pin 4 <- button C
        .d ( d            ),  // Pin 5 <- button D
        .e ( rest_and [0] ),  // Pin 9 <- E wire, at +5V on the breadboard
        .f ( rest_and [1] ),  // Pin 10 <- F wire, at +5V on the breadboard
        .g ( rest_and [2] ),  // Pin 11 <- G wire, at +5V on the breadboard
        .h ( rest_and [3] ),  // Pin 12 <- H wire, at +5V on the breadboard
        .j ( y_nand       )   // Pin 13 -> NAND
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side and compares them with a reference model: the AND outputs are 1 when all eight inputs of the 4068 are 1, the OR outputs when any input of the 4078 is 1. It drives all 4096 combinations of the buttons and of the two groups of E–H wires, twice, counting up and in Gray code order, and prints the rows where E–H are as on the breadboard, and any row with an error.

tb.sv186 lines · show the codefile
// Testbench for exercise B3d.
//
// Runs the generic design (gates_8_input.sv) and the chip-level design
// (gates_8_input_chips.sv) side by side and compares both with
// a comparison of the inputs with all ones and all zeros.  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 4096 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       a;
    logic       b;
    logic       c;
    logic       d;
    logic [3:0] rest_and;
    logic [3:0] rest_or;

    wire  y_and,  y_and_chips;
    wire  y_nand, y_nand_chips;
    wire  y_or,   y_or_chips;
    wire  y_nor,  y_nor_chips;

    gates_8_input i_generic
    (
        .a        ( a        ),
        .b        ( b        ),
        .c        ( c        ),
        .d        ( d        ),
        .rest_and ( rest_and ),
        .rest_or  ( rest_or  ),
        .y_and    ( y_and    ),
        .y_nand   ( y_nand   ),
        .y_or     ( y_or     ),
        .y_nor    ( y_nor    )
    );

    gates_8_input_chips i_chips
    (
        .a        ( a            ),
        .b        ( b            ),
        .c        ( c            ),
        .d        ( d            ),
        .rest_and ( rest_and     ),
        .rest_or  ( rest_or      ),
        .y_and    ( y_and_chips  ),
        .y_nand   ( y_nand_chips ),
        .y_or     ( y_or_chips   ),
        .y_nor    ( y_nor_chips  )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: a comparison of the inputs with all ones and all zeros.
    // Bits, from the left: y_and, y_nand, y_or, y_nor.

    function automatic logic [3:0] expected
    (
        input logic       in_a,
        input logic       in_b,
        input logic       in_c,
        input logic       in_d,
        input logic [3:0] in_rest_and,
        input logic [3:0] in_rest_or
    );
        // The 4068 sees A-D and its own E-H, the 4078 A-D and its own E-H

        logic [7:0] and_inputs = { in_a, in_b, in_c, in_d, in_rest_and };
        logic [7:0] or_inputs  = { in_a, in_b, in_c, in_d, in_rest_or };

        return { and_inputs == 8'hff, and_inputs != 8'hff,
                 or_inputs  != 8'h00, or_inputs  == 8'h00 };
    endfunction

    wire [3:0] out       = { y_and, y_nand, y_or, y_nor };
    wire [3:0] out_chips = { y_and_chips, y_nand_chips, y_or_chips, y_nor_chips };
    wire [3:0] out_model = expected (a, b, c, d, rest_and, rest_or);

    //------------------------------------------------------------------------
    // 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 [4095: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 | 4096'b1 << { a, b, c, d, rest_and, rest_or };

            // Print the rows with E-H as on the breadboard, and every error

            if (rest_and == 4'hf & rest_or == 4'h0 | out !== out_model | out_chips !== out_model)
                $display ("%5d  %b  %b  %b  %b  %b      %b      %b    %b     %b   %b    %s  %s",
                    cycle + 1, a, b, c, d, rest_and, rest_or, y_and, y_nand, y_or, y_nor,
                    out       === out_model ? "ok     " : "WRONG  ",
                    out_chips === out_model ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs: all combinations, counting up from E-H as on the
    // breadboard (1 for the 4068, 0 for the 4078), 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  A  B  C  D  E-H 4068  E-H 4078  AND  NAND  OR  NOR  generic  chips");

        checking <= '1;

        for (int i = 0; i < 4096; i ++)
        begin
            { rest_and, rest_or, a, b, c, d } <= 12' (i) ^ 12'hf00;
            @ (posedge clk);
        end

        for (int i = 0; i < 4096; i ++)
        begin
            { rest_and, rest_or, a, b, c, d } <= 12' (i ^ (i >> 1)) ^ 12'hf00;
            @ (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), 4096);
    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 B3d: 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 gates_8_input.sv gates_8_input_chips.sv cd4078.sv cd4068.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 B3d: 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 gates_8_input.sv gates_8_input_chips.sv cd4078.sv cd4068.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 B3d: 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 gates_8_input.sv gates_8_input_chips.sv cd4078.sv cd4068.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  A  B  C  D  E-H 4068  E-H 4078  AND  NAND  OR  NOR  generic  chips
    1  0  0  0  0  1111      0000      0    1     0   1    ok       ok   
    2  0  0  0  1  1111      0000      0    1     1   0    ok       ok   
    3  0  0  1  0  1111      0000      0    1     1   0    ok       ok   
    4  0  0  1  1  1111      0000      0    1     1   0    ok       ok   
    5  0  1  0  0  1111      0000      0    1     1   0    ok       ok   
    6  0  1  0  1  1111      0000      0    1     1   0    ok       ok   
    7  0  1  1  0  1111      0000      0    1     1   0    ok       ok   
    8  0  1  1  1  1111      0000      0    1     1   0    ok       ok   
    9  1  0  0  0  1111      0000      0    1     1   0    ok       ok   
   10  1  0  0  1  1111      0000      0    1     1   0    ok       ok   
   11  1  0  1  0  1111      0000      0    1     1   0    ok       ok   
   12  1  0  1  1  1111      0000      0    1     1   0    ok       ok   
   13  1  1  0  0  1111      0000      0    1     1   0    ok       ok   
   14  1  1  0  1  1111      0000      0    1     1   0    ok       ok   
   15  1  1  1  0  1111      0000      0    1     1   0    ok       ok   
   16  1  1  1  1  1111      0000      1    0     1   0    ok       ok   
 4097  0  0  0  0  1111      0000      0    1     0   1    ok       ok   
 4098  0  0  0  1  1111      0000      0    1     1   0    ok       ok   
 4099  0  0  1  1  1111      0000      0    1     1   0    ok       ok   
 4100  0  0  1  0  1111      0000      0    1     1   0    ok       ok   
 4101  0  1  1  0  1111      0000      0    1     1   0    ok       ok   
 4102  0  1  1  1  1111      0000      0    1     1   0    ok       ok   
 4103  0  1  0  1  1111      0000      0    1     1   0    ok       ok   
 4104  0  1  0  0  1111      0000      0    1     1   0    ok       ok   
 4105  1  1  0  0  1111      0000      0    1     1   0    ok       ok   
 4106  1  1  0  1  1111      0000      0    1     1   0    ok       ok   
 4107  1  1  1  1  1111      0000      1    0     1   0    ok       ok   
 4108  1  1  1  0  1111      0000      0    1     1   0    ok       ok   
 4109  1  0  1  0  1111      0000      0    1     1   0    ok       ok   
 4110  1  0  1  1  1111      0000      0    1     1   0    ok       ok   
 4111  1  0  0  1  1111      0000      0    1     1   0    ok       ok   
 4112  1  0  0  0  1111      0000      0    1     1   0    ok       ok   
tb.sv:172: $finish called at 81925000 (1ps)
PASS: 16384 checks, no errors

To see that the testbench catches mistakes, break something and run it again: remove rest_and from and_inputs in gates_8_input.sv, so that the AND ignores inputs E–H. 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_01_and_or_not_xor_de_morgan to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add gates_8_input.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.

Keys 0, 1, 2 and 3 are A, B, C and D; LEDs 0–3 show AND, NAND, OR and NOR. Inputs E–H are fixed as on the breadboard. To run the chip-level design instead, add gates_8_input_chips.sv and the chip models and change the module name in the instance.

bgm_lab/lab_top.sv108 lines · show the codefile
// Exercise B3d on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_01_and_or_not_xor_de_morgan to a new directory,
// replace its lab_top.sv with this file and add gates_8_input.sv. For the
// chip-level design add gates_8_input_chips.sv and cd4068.sv, cd4078.sv
// instead, and change the module name below.
//
// Keys: key [0] a, key [1] b, key [2] c, key [3] d. The LEDs: led [0] y_and,
// led [1] y_nand, led [2] y_or, led [3] y_nor. A board with fewer LEDs shows
// the first ones.
//
// Inputs E-H of each chip are fixed as on the breadboard: 1 for the 4068, 0
// for the 4078.

`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 y_and;
    logic y_nand;
    logic y_or;
    logic y_nor;

    gates_8_input i_gates_8_input
    (
        .a        ( key [0] ),
        .b        ( key [1] ),
        .c        ( key [2] ),
        .d        ( key [3] ),
        .rest_and ( 4'b1111 ),
        .rest_or  ( 4'b0000 ),
        .y_and    ( y_and   ),
        .y_nand   ( y_nand  ),
        .y_or     ( y_or    ),
        .y_nor    ( y_nor   )
    );

    assign led = w_led' ({ y_nor, y_or, y_nand, y_and });

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 gates_8_input.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Keys 0–3 are the inputs. The screen shows 8 square lamps in a row: the inputs in blue and the outputs in green, like the LED module on the breadboard.

bgm_hackathon/hackathon_top.sv95 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon
// This module uses few parameterization and relaxed typing rules

// Exercise B3d 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 gates_8_input.sv.
//
// Keys: key [0] a, key [1] b, key [2] c, key [3] d. The LEDs: led [0] y_and,
// led [1] y_nand, led [2] y_or, led [3] y_nor. The screen shows 8 lamps in a
// row: A, B, C, D, AND, NAND, OR, NOR; inputs in blue, outputs in green.
//
// Inputs E-H of each chip are fixed as on the breadboard: 1 for the 4068, 0
// for the 4078.
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 a = key [0];
    wire b = key [1];
    wire c = key [2];
    wire d = key [3];

    logic y_and;
    logic y_nand;
    logic y_or;
    logic y_nor;

    gates_8_input i_gates_8_input
    (
        .a        ( a       ),
        .b        ( b       ),
        .c        ( c       ),
        .d        ( d       ),
        .rest_and ( 4'b1111 ),
        .rest_or  ( 4'b0000 ),
        .y_and    ( y_and   ),
        .y_nand   ( y_nand  ),
        .y_or     ( y_or    ),
        .y_nor    ( y_nor   )
    );

    assign led      = { 4'b0, y_nor, y_or, y_nand, y_and };

    assign abcdefgh = '0;
    assign digit    = '0;

    //------------------------------------------------------------------------
    // 8 square lamps on the 480 x 272 screen, 40 pixels wide, 57 apart:
    // the inputs in blue, the outputs in green.  Bright when 1, dim when 0.

    wire [7:0] lamp = { y_nor, y_or, y_nand, y_and, d, c, b, a };

    always_comb
    begin
        red   = 0;
        green = 0;
        blue  = 0;

        for (int i = 0; i < 8; i ++)
            if (x >= 9' (20 + i * 57) & x < 9' (60 + i * 57) & y >= 116 & y < 156)
            begin
                if (i < 4)
                    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.

Switches 0–3 are the inputs; segments a, b, c and d of the display show AND, NAND, OR and NOR.

tiny_tapeout/project.v60 lines · show the codefile
/*
 * Exercise B3d on Tiny Tapeout: the 8-input gates, with the DIP switches as
 * inputs and the 7-segment display of the demo board as lamps.
 *
 * Add gates_8_input.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
);

    // Switch 0 is a, switch 1 is b, switch 2 is c, switch 3 is d
    // Inputs E-H of each chip are fixed as on the breadboard: 1 for the 4068,
    // 0 for the 4078.

    wire y_and;
    wire y_nand;
    wire y_or;
    wire y_nor;

    gates_8_input i_gates_8_input (
        .a        ( ui_in [0] ),
        .b        ( ui_in [1] ),
        .c        ( ui_in [2] ),
        .d        ( ui_in [3] ),
        .rest_and ( 4'b1111   ),
        .rest_or  ( 4'b0000   ),
        .y_and    ( y_and     ),
        .y_nand   ( y_nand    ),
        .y_or     ( y_or      ),
        .y_nor    ( y_nor     )
    );

    // 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) y_and, b (top right) y_nand, c (bottom right) y_or, d (bottom)
    // y_nor.

    assign uo_out  = { 4'b0, y_nor, y_or, y_nand, y_and };

    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:4], 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>_gates_8_input
  source_files:
    - "project.v"
    - "gates_8_input.sv"

15Going further

Tasks for students

Files of this exercise

Sources

Lesson 1.1 of the Habr article about the electronics course describes the datasheet exercise with these chips; the LSSK components sheet lists them. The chips are described in the Texas Instruments datasheets for the CD4068B and CD4078B.