Breadboard exercises/ G1EnglishРусский

Exercise G1 · getting started

Getting started: power, LEDs, a gate and two buttons

The first circuit of the course: a breadboard with its power module, a 5 V LED, the 8-LED module, one chip and two buttons. The LED lights only while both buttons are pressed: one AND gate, and every part you will use in the later exercises.

1 chip4081, one of its four AND gates
10 + 9 wireson the board + jumper leads to the modules
5 Vfrom a USB charger through the power module
no clockthe LED follows the buttons at once

1What it is and why it matters

What it does. Button A and button B each give 0 while up and 1 while pressed. Both go to one AND gate of a 4081 chip, and the gate's output lights a 5 V LED plugged into the board. LEDs 1 and 2 of the LED module show A and B, so you see the inputs and the output at once. The LED lights only while A and B are pressed.

How it works. The power module turns the 9 V of the USB-to-DC cable into a steady 5 V on the long rails of the breadboard. The chip takes its power from the rails, the buttons give it 0 V or 5 V, and its output pin drives the LED. Every later exercise is built the same way: chips across the gap, power from the rails, modules on jumper leads.

Where it is used. An AND gate is the simplest decision: “both conditions hold”. A safety guard that runs a machine only while both hands press two buttons is this circuit; so is “write to memory only when the address matches and the write signal is on”, inside every processor.

Why learn it.

The getting-started guide walks through the same steps with photographs of the parts; this page adds the schematics, the wiring checklist and the SystemVerilog.

2The key elements: board, power, LEDs, buttons, a gate

The breadboard

An 830-point breadboard has 63 numbered columns. In each column the five holes a–e are connected to each other inside, and so are the five holes f–j; the gap in the middle separates the two halves. Along the top and the bottom run the rails: a red + row and a blue − row, each connected along its whole length. A chip goes across the gap, so each of its pins gets its own column of four free holes for wires.

The power module

The power module plugs into the rails at the left end. It has a barrel jack, a switch and two jumpers that set each pair of rails to 3.3 V or 5 V: set both to 5 V. It is powered from a USB charger through a USB-to-DC cable that raises the 5 V of USB to 9 V; the module brings it back down to a steady 5 V.

The LEDs

The 5 V LED has its current-limiting resistor inside, so it can go straight between a signal and GND: long leg (+) to the signal, short leg (−) to GND. The 8-LED module has eight such LEDs and a common GND pin, G.

The buttons

A button module has three pins: + to the + rail, − to the − rail and OUT, which is 0 V while the button is up and 5 V while it is pressed: a pull-down resistor on the module holds OUT at 0 when nobody presses.

The AND gate

ABY
ABY = A AND B
000
010
100
111

Y = 1 only when A and B are both 1. In SystemVerilog: a & b.

Waveforms

ABY
From the simulation of tb.sv in Icarus Verilog. The testbench sets A and B right after a rising edge of its clock (dotted lines) and checks Y at the next edge. Y is 1 only where A and B are both 1. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD4081quad 2-input AND: Y = A AND B (one gate used)1$0.55Elexp (elexp.com)
Button modulesA and B; 2 from the pack of five2$0.66 / packaliexpress.us
8-LED moduleshows A and B1$0.38aliexpress.us
5 V LED, greenY, plugged into the board1$0.45elexp.com
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 jumpers10 wires on the board (or pre-cut wires)10$1.30 / packaliexpress.us
Male-female Dupont jumpers9 leads to the buttons and the LED module9$1.30 / packaliexpress.us

Prices from the LSSK components sheet; all of these parts are in every kit. Pre-cut wires lie flatter than Dupont jumpers and match the drawing better.

Use the 9 V USB-to-DC cable with the power module. A 5 V cable gives only about 3.5 V on the rails, because the module's regulator needs a few volts more than it gives. The chips still work at 3.5 V, but the LEDs are dim.

4The chip, pin by pin

The chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every input must be connected to something, because a CMOS input left open picks up noise and can switch at random. The drawings show each chip from above with the notch on the left, as in the datasheet: pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.

4081: quad 2-input AND

1A2B3J4K5C6D7VSS8E9F10L11M12G13H14VDD4081
CD4081B. Four independent AND gates. Pin 14 is +5 V, pin 7 is GND. · SVG · PNG

Gate J has its inputs on pins 1 and 2 and its output on pin 3: it makes Y = A AND B. The other three gates are not used, but their six inputs still go to the rails, top pins to +, bottom pins to −. Their outputs, pins 4, 10 and 11, stay open: an output may be left unconnected, an input may not.

Polarity. 1 means +5 V and 0 means 0 V; the LED lights for 1.

PinNameDirectionFunctionIn this circuit
1Ainputinput of the gate with its output on pin 3A
2Binputinput of the gate with its output on pin 3B
3JoutputJ = A AND BY
4KoutputK = C AND Dnot connected
5Cinputinput of the gate with its output on pin 4tied to GND
6Dinputinput of the gate with its output on pin 4tied to GND
7VSSpowerground, 0 VGND
8Einputinput of the gate with its output on pin 10tied to +5V
9Finputinput of the gate with its output on pin 10tied to +5V
10LoutputL = E AND Fnot connected
11MoutputM = G AND Hnot connected
12Ginputinput of the gate with its output on pin 11tied to +5V
13Hinputinput of the gate with its output on pin 11tied to +5V
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: the gate

AB4081ALED module, 1BLED module, 2Y5 V LED
The circuit as a gate, without the chip. This is what first_gate.sv describes. A dot means a connection. · SVG · PNG

Each button signal goes to the gate and to an LED of the module; the gate's output goes to the 5 V LED.

6Schematic 2: connections

12345678910111213144081+−5 V LEDABYABLED module: LEDs 1-2+5V
The same circuit with the chip, pin by pin. The chip is drawn from above with the notch on the left; pin numbers are next to the pins. The gate in use is drawn in full, the unused ones faded, their inputs tied to +5V or GND. The boxes at the bottom are the modules. · SVG · PNG
NetFromToWire
Abutton A, OUT4081 1 (A), LED module, LED 1M-F
Bbutton B, OUT4081 2 (B), LED module, LED 2M-F
Y4081 3 (J)5 V LED, + legM-M
+5V+5V4081 14 (VDD), 4081 13 (H), 4081 12 (G), 4081 9 (F), 4081 8 (E)
GNDGND4081 7 (VSS), 5 V LED, − leg, 4081 5 (C), 4081 6 (D)
not connected4081 4 (K), 4081 10 (L), 4081 11 (M)

7Schematic 3: the breadboard

−+−+12345678910111213141516171819202122232425262728293031323334353637383940abcdefghij5V5VPOWER MODULE · 5V9V USB-DC4081+−5 V LEDY+OUT−A+OUT−BG1A2B3456788-LED MODULELEDs 1-2 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 board goes on to column 63 on the right. · SVG · PNG
ABY+5VGND

Build it step by step

  1. The power module. Plug it into the left end of the board so that its pins go into the rails, + to the red rows. Set both jumpers to 5 V, leave the switch off and connect the USB-to-DC cable.
  2. The first LED. Plug the 5 V LED into holes 24f and 25f, long leg (+) in 24f. Put a black wire from 25j to the bottom − rail and, for now, a red wire from 24j to the bottom + rail. Switch the power on: the LED lights. Switch it off again and take the red wire out.
  3. The chip. Insert the 4081 across the gap, notch to the left, pin 1 in hole 14f. Press it down evenly; if the legs are splayed, bend them in a little against the table first.
  4. Power for the chip: a red wire from pin 14 (column 14, top half) to the top + rail and a black wire from pin 7 (column 20, bottom half) to the bottom − rail.
  5. The tie-offs: pins 13, 12, 9 and 8 to the top + rail, pins 5 and 6 to the bottom − rail.
  6. The output: a wire from pin 3 (column 16, bottom half) to the + leg of the LED in column 24.
  7. The modules, with the power still off: for each button + to the + rail, − to the − rail and OUT to pin 1 (button A) or pin 2 (button B); G of the LED module to the − rail, LED 1 to column 14, LED 2 to column 15.
  8. Switch the power on.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 3 wires
1+5V14a (4081 pin 14, VDD)top + rail, column 14
2GND20j (4081 pin 7, VSS)bottom − rail, column 20
3GND25j (5 V LED, − leg)bottom − rail, column 25
Fixed inputs: CMOS inputs must not float · 6 wires
4+5V15a (4081 pin 13, H)top + rail, column 15
5+5V16a (4081 pin 12, G)top + rail, column 16
6GND18j (4081 pin 5, C)bottom − rail, column 18
7+5V19a (4081 pin 9, F)top + rail, column 19
8GND19j (4081 pin 6, D)bottom − rail, column 19
9+5V20a (4081 pin 8, E)top + rail, column 20
Signal wires · 1 wire
10Y16g (4081 pin 3, J)24g (5 V LED, + leg)
Modules on male-female jumpers · 9 wires
11+5Vbutton A, +bottom + rail, column 8
12Abutton A, OUT14j (4081 pin 1, A)
13GNDbutton A, −bottom − rail, column 10
14+5Vbutton B, +bottom + rail, column 14
15Bbutton B, OUT15j (4081 pin 2, B)
16GNDbutton B, −bottom − rail, column 16
17GNDLED module, Gbottom − rail, column 24
18ALED module, LED 114i (4081 pin 1, A)
19BLED module, LED 215i (4081 pin 2, B)

Test it

If it does not work: check the power first: the power module's LED is on, and the first LED lit in step 2. If LED 1 or 2 of the module never lights, the button's + or − lead is missing. If the 5 V LED never lights, check that pin 3 goes to its long leg and its short leg to GND. If the LED flickers when you touch the wires, an input is floating: look for a missing tie-off. If the chip gets warm, switch the power off at once: it is probably inserted the wrong way round.

8SystemVerilog: the generic design

first_gate.sv describes schematic 1: one assign statement. It is synthesizable: an FPGA tool turns it into one lookup table, an ASIC flow into one AND cell.

first_gate.svfile
// Exercise G1: the first circuit, one AND gate.
//
// Generic version that matches schematic 1: the output Y, here a_and_b,
// is 1 only while both buttons are pressed.  Synthesizable.

module first_gate
(
    input  logic a,
    input  logic b,

    output logic a_and_b  // Y: 1 when both inputs are 1
);

    assign a_and_b = a & b;

endmodule

An assign is not a step of a program: it is a wire that always carries the value of its expression, like the output pin of the gate.

9SystemVerilog model of the chip

One module per chip, with ports named as in the datasheet and the pin number of each port in a comment. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.

cd4081.svfile
// CD4081B: quad 2-input AND gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.
//
// The 4081, 4071 and 4070 have the same pinout: only the function of the
// gates differs.

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

    assign j = a & b;
    assign k = c & d;
    assign l = e & f;
    assign m = g & h;

endmodule

10SystemVerilog: the chip-level design

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

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

module first_gate_chips
(
    input  logic       a,
    input  logic       b,
    output logic       a_and_b
);

    // 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_k;
    wire unused_u1_l;
    wire unused_u1_m;

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

    // U1: CD4081 quad 2-input AND, pin 1 in column 14

    cd4081 i_u1
    (
        .a ( a           ),  // Pin 1 <- button A
        .b ( b           ),  // Pin 2 <- button B
        .j ( a_and_b     ),  // Pin 3 -> Y
        .k ( unused_u1_k ),  // Pin 4, unused output
        .c ( gnd         ),  // Pin 5 <- GND, unused input
        .d ( gnd         ),  // Pin 6 <- GND, unused input
        .e ( vdd         ),  // Pin 8 <- +5V, unused input
        .f ( vdd         ),  // Pin 9 <- +5V, unused input
        .l ( unused_u1_l ),  // Pin 10, unused output
        .m ( unused_u1_m ),  // Pin 11, unused output
        .g ( vdd         ),  // Pin 12 <- +5V, unused input
        .h ( vdd         )   // Pin 13 <- +5V, unused input
    );

endmodule

The 5 V LED and the LED module are not in the design: its ports are the signals they show.

11Testbench and simulation scripts

tb.sv runs both designs side by side and compares them with the truth table of AND. The circuit has no clock, but the testbench has one, like the testbenches of Basics-Graphics-Music: it sets A and B right after a rising edge and checks Y at the next one. It goes through the four rows twice, counting up and in Gray code order, and prints PASS only if both designs match every time and every row was seen.

tb.sv150 lines · show the codefile
// Testbench for exercise G1.
//
// Runs the generic design (first_gate.sv) and the chip-level design
// (first_gate_chips.sv) side by side and compares both with
// the truth table of AND.  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 4 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;

    wire  a_and_b, a_and_b_chips;

    first_gate i_generic
    (
        .a       ( a       ),
        .b       ( b       ),
        .a_and_b ( a_and_b )
    );

    first_gate_chips i_chips
    (
        .a       ( a             ),
        .b       ( b             ),
        .a_and_b ( a_and_b_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model: the truth table of AND.
    // Bits, from the left: a_and_b.

    function automatic logic [0:0] expected (input logic in_a, input logic in_b);
        case ({ in_a, in_b })
        2'b11:   return 1'b1;
        default: return 1'b0;
        endcase
    endfunction

    wire [0:0] out       = { a_and_b };
    wire [0:0] out_chips = { a_and_b_chips };
    wire [0:0] out_model = expected (a, b);

    //------------------------------------------------------------------------
    // 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 [3: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 | 4'b1 << { a, b };

            $display ("%5d  %b  %b  %b  %s  %s",
                cycle + 1, a, b, a_and_b,
                out       === out_model ? "ok     " : "WRONG  ",
                out_chips === out_model ? "ok   "   : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving the inputs: all combinations, counting up, 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  Y  generic  chips");

        checking <= '1;

        for (int i = 0; i < 4; i ++)
        begin
            { a, b } <= 2' (i);
            @ (posedge clk);
        end

        for (int i = 0; i < 4; i ++)
        begin
            { a, b } <= 2' (i ^ (i >> 1));
            @ (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), 4);
    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 G1: 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 first_gate.sv first_gate_chips.sv cd4081.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 G1: 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 first_gate.sv first_gate_chips.sv cd4081.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 G1: 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 first_gate.sv first_gate_chips.sv cd4081.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  Y  generic  chips
    1  0  0  0  ok       ok   
    2  0  1  0  ok       ok   
    3  1  0  0  ok       ok   
    4  1  1  1  ok       ok   
    5  0  0  0  ok       ok   
    6  0  1  0  ok       ok   
    7  1  1  1  ok       ok   
    8  1  0  0  ok       ok   
tb.sv:136: $finish called at 85000 (1ps)
PASS: 16 checks, no errors

To see that the testbench catches mistakes, break something and run it again: change a & b to a | b in first_gate.sv, or connect pin 3 of the 4081 to unused_u1_k and pin 4 to a_and_b in first_gate_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_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 first_gate.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 and 1 are A and B, LED 0 is Y. To run the chip-level design instead, add first_gate_chips.sv and cd4081.sv and change the module name in the instance.

bgm_lab/lab_top.sv94 lines · show the codefile
// Exercise G1 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 first_gate.sv. For the
// chip-level design add first_gate_chips.sv and cd4081.sv instead, and change
// the module name below.
//
// Keys: key [0] a, key [1] b. The LEDs: led [0] a_and_b. A board with fewer
// LEDs shows the first ones.

`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 a_and_b;

    first_gate i_first_gate
    (
        .a       ( key [0] ),
        .b       ( key [1] ),
        .a_and_b ( a_and_b )
    );

    assign led = w_led' ({ a_and_b });

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

Keys 0 and 1 are A and B. The screen shows three square lamps: A and B in blue, Y in green.

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

// Exercise G1 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 first_gate.sv.
//
// Keys: key [0] a, key [1] b. The LEDs: led [0] a_and_b. The screen shows 3
// lamps in a row: A, B, Y; inputs in blue, outputs in green.
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];

    logic a_and_b;

    first_gate i_first_gate
    (
        .a       ( a       ),
        .b       ( b       ),
        .a_and_b ( a_and_b )
    );

    assign led      = { 7'b0, a_and_b };

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

    //------------------------------------------------------------------------
    // 3 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 [2:0] lamp = { a_and_b, b, a };

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

        for (int i = 0; i < 3; i ++)
            if (x >= 9' (156 + i * 64) & x < 9' (196 + 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.

Switches 0 and 1 are A and B; segment a, the top bar of the display, is Y.

tiny_tapeout/project.v47 lines · show the codefile
/*
 * Exercise G1 on Tiny Tapeout: one AND gate, with the DIP switches as
 * inputs and the 7-segment display of the demo board as lamps.
 *
 * Add first_gate.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

    wire a_and_b;

    first_gate i_first_gate (
        .a       ( ui_in [0] ),
        .b       ( ui_in [1] ),
        .a_and_b ( a_and_b   )
    );

    // 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) a_and_b.

    assign uo_out  = { 7'b0, a_and_b };

    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>_first_gate
  source_files:
    - "project.v"
    - "first_gate.sv"

15Going further

Tasks for students

Files of this exercise

Sources

The getting-started steps follow notes 1–5 of “A prequel to FPGA and ASIC design using small-scale integration chips” and lesson 1.1 of the Habr article about the electronics course, which describes the 5 V LEDs and the buttons with built-in pull-down resistors. The 4081 is described in the Texas Instruments datasheet for the CD4081B.