Breadboard exercises/ BB9EnglishРусский

Exercise BB9 · building block · register file

40108 register file: four words, two read ports

Four 4-bit registers in one chip, with one door to write and two doors to read: you write a word into any register with buttons and see any two registers at once on the LEDs. Every processor keeps its registers in a file like this one.

1 chip40108, 24 pins, 0.6 inch wide
10 + 33 wireson the board + jumper leads to the modules
4 × 4words × bits; 1 write port, 2 read ports
~1 Hzfrom the 555 clock module

1What it is and why it matters

What it does. The 40108 holds four words of four bits, numbered 0 to 3. To write, hold some of the data buttons D3–D0 for the value and some of W1, W0 for the number of the word, and hold WRITE until the clock module blinks. To read, four wires set two read addresses: R1A, R0A choose the word on bus A, shown on LEDs 1–4, and R1B, R0B choose the word on bus B, on LEDs 5–8. The two buses show their words all the time, without the clock.

How it works. Inside are sixteen flip-flops, four per word. A decoder turns the write address into four enables, so at the clock edge only the chosen word takes the data, and only while WRITE ENABLE is 1. Two multiplexers, one per bus, pick a word by its read address. The outputs are three-state: a 0 on 3-STATE A or B turns that bus off, so several chips can share the same wires.

Where it is used. A processor keeps its working registers in a register file. An instruction such as add x3, x1, x2 reads x1 and x2 through two read ports in the same clock cycle and writes the sum into x3 through the write port at the end of the cycle. A RISC-V processor has 32 registers of 32 bits with exactly this arrangement: two read ports and one write port.

Why learn it.

2The key elements: write one, read two

The read wires start at R1A R0A = 00 and R1B R0B = 01: bus A shows word 0, bus B word 1. Then the presses below, each held through one clock edge, with the read wires moved as shown:

Press, then the clock edgeWord 0Word 1Word 2Word 3R1A R0ABus A (LEDs 1-4)R1B R0BBus B (LEDs 5-8)
write 5 to word 05???00010101?
write 10 to word 1510??000101011010
write 3 to word 25103?100011011010
write 12 to word 3510312100011111100
D changes, WRITE released510312111100000101
write 9 to word 0910312001001111100

Before the first write the words hold anything: the 40108 has no reset. The fifth row changes the data buttons without WRITE: nothing is written, because the clock is blocked while WRITE ENABLE is 0.

Waveforms

CLKWEW01230D0510312159RA0230123012A531259103129103RB130301B1012512910
From the simulation of tb.sv in Icarus Verilog: the writes of the table, then reads of every pair of words. A bus changes at once when its read address changes, and at the very clock edge that writes the word it shows. Grey means a word not written yet. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD401084 × 4 multiport register: one write port, two read ports1$28.84electronicplus.com
Button modulesthe data D3-D0, the write address W1 W0 and WRITE; the first pack of five5$0.66 / packaliexpress.us
Button modules2 more from the second pack2$2.03 / packaliexpress.us
555 clock modulethe clock, set to about 1 Hz1$0.53aliexpress.us
8-LED modulebus A on LEDs 1-4, bus B on LEDs 5-81$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 wires10 wires on the board (or male-male Dupont jumpers)10$11.95 / packjameco.com
Male-female Dupont jumpers33 leads to the clock module, the buttons and the LED module33$1.30 / packaliexpress.us

Prices from the LSSK components sheet. The 40108 is out of production, hard to find and expensive; it is an optional part of the Comprehensive kit. The Motorola MC14580B is pin-compatible.

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.

40108: 4 × 4 multiport register

1Q3B2Q2B3ENA4Q0A5Q1A6Q2A7Q3A8W09W110R1B11R0B12VSS13R0A14R1A15WE16CLK17D318D219D120D021ENB22Q0B23Q1B24VDD401084 × 4 register file
CD40108B. A 24-pin chip, 0.6 inch between the rows of pins: VDD is pin 24 and VSS pin 12. · SVG · PNG

The data go in on D0–D3 (pins 20–17). WRITE 1 and WRITE 0 (pins 9, 8) are the write address, WRITE ENABLE (pin 15) lets CLOCK (pin 16) write on its rising edge. READ 1A and READ 0A (pins 14, 13) choose the word on bus A, Q0A–Q3A (pins 4–7); READ 1B and READ 0B (pins 10, 11) choose the word on bus B, Q0B–Q3B (pins 22, 23, 2, 1). Note how the bits of bus B are spread over both rows. 3-STATE A and B (pins 3, 21) are at +5V, so both buses are always on.

A wide chip. The two rows of pins are 0.6 inch apart, twice as far as on the other chips of the kit. On the breadboard the pins go into rows d and h, and the body covers rows e, f and g.

PinNameDirectionFunctionIn this circuit
1Q3 Boutputbus B, bit 3Q3B
2Q2 Boutputbus B, bit 2Q2B
33-STATE Ainput1 turns bus A on, 0 turns it off (three-state)+5V
4Q0 Aoutputbus A, bit 0Q0A
5Q1 Aoutputbus A, bit 1Q1A
6Q2 Aoutputbus A, bit 2Q2A
7Q3 Aoutputbus A, bit 3Q3A
8WRITE 0inputwrite address, bit 0W0
9WRITE 1inputwrite address, bit 1W1
10READ 1Binputread address of bus B, bit 1R1B (GND)
11READ 0Binputread address of bus B, bit 0R0B (+5V)
12VSSpowerground, 0 VGND
13READ 0Ainputread address of bus A, bit 0R0A (GND)
14READ 1Ainputread address of bus A, bit 1R1A (GND)
15WRITE ENABLEinput1 lets the clock write, 0 blocks itWE
16CLOCKinputthe word at the write address takes D0-D3 at the rising edge, 0 to 1, while WRITE ENABLE is 1CLK
17D3inputdata input, bit 3D3
18D2inputdata input, bit 2D2
19D1inputdata input, bit 1D1
20D0inputdata input, bit 0D0
213-STATE Binput1 turns bus B on, 0 turns it off (three-state)+5V
22Q0 Boutputbus B, bit 0Q0B
23Q1 Boutputbus B, bit 1Q1B
24VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: the register file

CLKWRITEW1W0D3D2D1D0R1AR0AR1BR0B40108CLKWEW1W0D3D2D1D0R1AR0AR1BR0BENAENBQ3AQ2AQ1AQ0AQ3BQ2BQ1BQ0Bregister file 4 × 4Q3ALED 1Q2ALED 2Q1ALED 3Q0ALED 4Q3BLED 5Q2BLED 6Q1BLED 7Q0BLED 8
The circuit as a block. This is what register_file.sv describes. The four read wires are on the left, below the buttons. · SVG · PNG

6Schematic 2: connections to the chip

123456789101112131415161718192021222324Q3BQ2BENAQ0AQ1AQ2AQ3AW0W1R1BR0BR0AR1AWECLKD3D2D1D0ENBQ0BQ1B401084 × 4 register fileCLKWEW0W1D0D1D2D3Q0AQ1AQ2AQ3AQ0BQ1BQ2BQ3BD3D2D1D0WRITEclockLED module: LEDs 1-8W1W0R1A→ GNDR0A→ GNDR1B→ GNDR0B→ +5V+5V
The same circuit pin by pin. The boxes marked “→ GND” or “→ +5V” are the read wires in their starting places. · SVG · PNG
NetFromToWire
CLK555 clock module, OUT40108 16 (CLK)M-F
WEbutton WRITE, OUT40108 15 (WE)M-F
W1button W1, OUT40108 9 (W1)M-F
W0button W0, OUT40108 8 (W0)M-F
D3button D3, OUT40108 17 (D3)M-F
D2button D2, OUT40108 18 (D2)M-F
D1button D1, OUT40108 19 (D1)M-F
D0button D0, OUT40108 20 (D0)M-F
R1AR1A wire: GND40108 14 (R1A)M-M
R0AR0A wire: GND40108 13 (R0A)M-M
R1BR1B wire: GND40108 10 (R1B)M-M
R0BR0B wire: +5V40108 11 (R0B)M-M
Q3A40108 7 (Q3A)LED module, LED 1M-F
Q2A40108 6 (Q2A)LED module, LED 2M-F
Q1A40108 5 (Q1A)LED module, LED 3M-F
Q0A40108 4 (Q0A)LED module, LED 4M-F
Q3B40108 1 (Q3B)LED module, LED 5M-F
Q2B40108 2 (Q2B)LED module, LED 6M-F
Q1B40108 23 (Q1B)LED module, LED 7M-F, M-M
Q0B40108 22 (Q0B)LED module, LED 8M-F, M-M
+5V+5V40108 24 (VDD), 40108 3 (ENA), 40108 21 (ENB)
GNDGND40108 12 (VSS)

7Schematic 3: the breadboard

−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghij5V5VPOWER MODULE · 5V9V USB-DC40108R0AR1AR0BR1BQ0B+OUT−D3+OUT−D2+OUT−D1+OUT−D0+OUT−WRITE−OUT+555CLOCK 555~1 HzG1Q3A2Q2A3Q1A4Q0A5Q3B6Q2B7Q1B8Q0B8-LED MODULELEDs 1-8 used+OUT−W1+OUT−W0
Every wire in its hole. Column numbers are printed along the top; rows a–e are above the gap, f–j below it. The data buttons, WRITE and the clock module hang above the board; the LED module and the W1 and W0 buttons below it. The dashed lines are the four read wires to the rails. · SVG · PNG
CLKWEW1W0D3D2D1D0R1AR0AR1BR0BQ3AQ2AQ1AQ0AQ3BQ2BQ1BQ0B+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 40108 with pin 1 in hole 15h. Read the numbers on the chips: many look alike. The 40108 is a wide chip, 0.6 inch between the rows of pins: the pins go into rows d and h, and the body covers rows e, f and g, so each pin has three free holes above it or two below it.
  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. On the 40108 VDD is pin 24 and VSS pin 12. Make sure no wire touches + and − at the same time.
  4. The fixed inputs: 3-STATE A and 3-STATE B to +5V.
  5. The four read wires: R1A, R0A and R1B to GND, R0B to +5V. These are the wires you move later.
  6. With the power off, the modules: D3–D0, WRITE and the clock module above the board, the LED module and the W1 and W0 buttons below it. Tick each wire as you insert it.
  7. Turn the power on and set the clock module to about one blink per second.

Wiring checklist

·

#NetFromTo
Power (red to +, black to −) · 2 wires
1+5V15a (40108 pin 24, VDD)top + rail, column 15
2GND26j (40108 pin 12, VSS)bottom − rail, column 26
Fixed inputs: CMOS inputs must not float · 2 wires
7+5V17j (40108 pin 3, ENA)bottom + rail, column 17
8+5V18a (40108 pin 21, ENB)top + rail, column 18
Mode wires: move them between + and − to change the mode · 4 wires
3R0A26a (40108 pin 13, R0A)top − rail, column 26
4R1A25a (40108 pin 14, R1A)top − rail, column 25
5R0B25j (40108 pin 11, R0B)bottom + rail, column 25
6R1B24j (40108 pin 10, R1B)bottom − rail, column 24
Signal wires · 2 wires
9Q0B17b (40108 pin 22, Q0B)13f
10Q1B16c (40108 pin 23, Q1B)14f
Modules on male-female jumpers · 33 wires
11+5Vbutton D3, +top + rail, column 5
12D3button D3, OUT22a (40108 pin 17, D3)
13GNDbutton D3, −top − rail, column 7
14+5Vbutton D2, +top + rail, column 10
15D2button D2, OUT21a (40108 pin 18, D2)
16GNDbutton D2, −top − rail, column 12
17+5Vbutton D1, +top + rail, column 14
18D1button D1, OUT20a (40108 pin 19, D1)
19GNDbutton D1, −top − rail, column 17
20+5Vbutton D0, +top + rail, column 20
21D0button D0, OUT19a (40108 pin 20, D0)
22GNDbutton D0, −top − rail, column 22
23+5Vbutton WRITE, +top + rail, column 30
24WEbutton WRITE, OUT24a (40108 pin 15, WE)
25GNDbutton WRITE, −top − rail, column 32
26GND555 clock module, −top − rail, column 37
27CLK555 clock module, OUT23a (40108 pin 16, CLK)
28+5V555 clock module, +top + rail, column 39
29GNDLED module, Gbottom − rail, column 12
30Q3ALED module, LED 121j (40108 pin 7, Q3A)
31Q2ALED module, LED 220j (40108 pin 6, Q2A)
32Q1ALED module, LED 319j (40108 pin 5, Q1A)
33Q0ALED module, LED 418j (40108 pin 4, Q0A)
34Q3BLED module, LED 515j (40108 pin 1, Q3B)
35Q2BLED module, LED 616j (40108 pin 2, Q2B)
36Q1BLED module, LED 714j
37Q0BLED module, LED 813j
38+5Vbutton W1, +bottom + rail, column 33
39W1button W1, OUT23j (40108 pin 9, W1)
40GNDbutton W1, −bottom − rail, column 35
41+5Vbutton W0, +bottom + rail, column 38
42W0button W0, OUT22j (40108 pin 8, W0)
43GNDbutton W0, −bottom − rail, column 40

Test it

If it does not work: if a write never shows, check the clock wire to pin 16 and the WRITE button to pin 15. If a word lands in the wrong place, W1 and W0 are swapped. If a bus shows a word you did not choose, check the order of its read wires. If a bus shows nothing at all, or flickers, its 3-STATE pin is not at +5V.

8SystemVerilog: the generic design

register_file.sv is an array of four words, one always_ff block for the write port and one assign per read port. It has no reset, like the chip. Change the sizes and it is the register file of a processor. It is synthesizable.

register_file.svfile
// Exercise BB9: a register file of four 4-bit words with one write port and
// two read ports, like the 40108.
//
// Generic version that matches schematic 1, not tied to any chip.  While
// we (write enable) is 1, the rising edge of clk writes wdata into the word
// at waddr.  The two read ports show the words at raddr_a and raddr_b at
// any time, without the clock.  There is no reset: as in the 40108, the
// words hold anything until they are written.  Synthesizable.
//
// A processor keeps its registers in a file like this one, only larger:
// an instruction such as add x3, x1, x2 reads x1 and x2 through the two
// read ports and writes x3 through the write port.

module register_file
(
    input  logic       clk,

    input  logic       we,       // write enable
    input  logic [1:0] waddr,    // write address
    input  logic [3:0] wdata,

    input  logic [1:0] raddr_a,  // read port A
    output logic [3:0] rdata_a,

    input  logic [1:0] raddr_b,  // read port B
    output logic [3:0] rdata_b
);

    logic [3:0] word [0:3];

    always_ff @ (posedge clk)
        if (we)
            word [waddr] <= wdata;

    assign rdata_a = word [raddr_a];
    assign rdata_b = word [raddr_b];

endmodule

9SystemVerilog model of the chip

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

cd40108.svfile
// CD40108B: 4 x 4 multiport register, a register file of four 4-bit words
// with one write port and two read ports.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (12)
// and VDD (24), are left out.
//
// While WRITE ENABLE is 1, the rising edge of CLOCK writes D0-D3 into the
// word that WRITE 1 and WRITE 0 select; while it is 0 the clock does
// nothing.  Bus A shows the word that READ 1A and READ 0A select, bus B the
// word that READ 1B and READ 0B select, at any time, without the clock.
// 3-STATE A and B are active high: 0 turns the outputs of that bus off.
// The chip has no reset: after power-up the words hold anything.
//
// Synthesizable.

module cd40108
(
    output logic q3b,           // Pin 1
    output logic q2b,           // Pin 2
    input  logic enable_a,      // Pin 3:  3-STATE A, 0 = bus A off
    output logic q0a,           // Pin 4
    output logic q1a,           // Pin 5
    output logic q2a,           // Pin 6
    output logic q3a,           // Pin 7
    input  logic write0,        // Pin 8:  write address, bit 0
    input  logic write1,        // Pin 9:  write address, bit 1
    input  logic read1b,        // Pin 10: read address of bus B, bit 1
    input  logic read0b,        // Pin 11: read address of bus B, bit 0
                                // Pin 12: VSS
    input  logic read0a,        // Pin 13: read address of bus A, bit 0
    input  logic read1a,        // Pin 14: read address of bus A, bit 1
    input  logic write_enable,  // Pin 15: 1 = the clock writes
    input  logic clock,         // Pin 16
    input  logic d3,            // Pin 17
    input  logic d2,            // Pin 18
    input  logic d1,            // Pin 19
    input  logic d0,            // Pin 20
    input  logic enable_b,      // Pin 21: 3-STATE B, 0 = bus B off
    output logic q0b,           // Pin 22
    output logic q1b            // Pin 23
                                // Pin 24: VDD
);

    logic [3:0] word [0:3];

    always_ff @ (posedge clock)
        if (write_enable)
            word [{ write1, write0 }] <= { d3, d2, d1, d0 };

    assign { q3a, q2a, q1a, q0a } = enable_a ? word [{ read1a, read0a }] : 4'bz;
    assign { q3b, q2b, q1b, q0b } = enable_b ? word [{ read1b, read0b }] : 4'bz;

endmodule

10SystemVerilog: the chip-level design

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

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

module register_file_chips
(
    input  logic       clk,
    input  logic       we,
    input  logic [1:0] waddr,
    input  logic [3:0] wdata,
    input  logic [1:0] raddr_a,
    input  logic [1:0] raddr_b,
    output logic [3:0] rdata_a,
    output logic [3:0] rdata_b
);

    // The power rails of the breadboard, for tied-off inputs

    wire vdd = 1'b1;

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

    // U1: CD40108 4 × 4 multiport register: four 4-bit words, one write port, two read ports, pin 1 in column 15

    cd40108 i_u1
    (
        .q3b          ( rdata_b [3] ),  // Pin 1 -> Q3B
        .q2b          ( rdata_b [2] ),  // Pin 2 -> Q2B
        .enable_a     ( vdd         ),  // Pin 3 <- +5V
        .q0a          ( rdata_a [0] ),  // Pin 4 -> Q0A
        .q1a          ( rdata_a [1] ),  // Pin 5 -> Q1A
        .q2a          ( rdata_a [2] ),  // Pin 6 -> Q2A
        .q3a          ( rdata_a [3] ),  // Pin 7 -> Q3A
        .write0       ( waddr [0]   ),  // Pin 8 <- button W0
        .write1       ( waddr [1]   ),  // Pin 9 <- button W1
        .read1b       ( raddr_b [1] ),  // Pin 10 <- R1B wire, at GND on the breadboard
        .read0b       ( raddr_b [0] ),  // Pin 11 <- R0B wire, at +5V on the breadboard
        .read0a       ( raddr_a [0] ),  // Pin 13 <- R0A wire, at GND on the breadboard
        .read1a       ( raddr_a [1] ),  // Pin 14 <- R1A wire, at GND on the breadboard
        .write_enable ( we          ),  // Pin 15 <- button WRITE
        .clock        ( clk         ),  // Pin 16 <- 555 clock module
        .d3           ( wdata [3]   ),  // Pin 17 <- button D3
        .d2           ( wdata [2]   ),  // Pin 18 <- button D2
        .d1           ( wdata [1]   ),  // Pin 19 <- button D1
        .d0           ( wdata [0]   ),  // Pin 20 <- button D0
        .enable_b     ( vdd         ),  // Pin 21 <- +5V
        .q0b          ( rdata_b [0] ),  // Pin 22 -> Q0B
        .q1b          ( rdata_b [1] )   // Pin 23 -> Q1B
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side from the same clock and inputs and compares them with a reference model of four words. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It plays the writes of section 2, reads every pair of words, and ends with 120 clocks of pseudo-random writes and reads. The chip has no reset, so the testbench checks a word only after it has been written. The output below is shortened.

tb.sv227 lines · show the codefile
// Testbench for exercise BB9.
//
// Runs the generic design (register_file.sv) and the chip-level design
// (register_file_chips.sv) side by side and compares both with a reference
// model of four words.  Works with Icarus Verilog, Verilator (--binary) and
// Questa.
//
// Like the testbenches of Basics-Graphics-Music, it drives and samples
// signals only at rising clock edges.  It plays the writes of section 2,
// reads every pair of words through the two ports, changes the data and
// the address while WRITE is released, and ends with pseudo-random writes
// and reads.  The 40108 has no reset, so a word is checked only after it
// was written.

// 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       we      = 1'b0;
    logic [1:0] waddr   = '0;
    logic [3:0] wdata   = '0;
    logic [1:0] raddr_a = 2'd0;
    logic [1:0] raddr_b = 2'd1;

    wire  [3:0] rdata_a, rdata_a_chips;
    wire  [3:0] rdata_b, rdata_b_chips;

    register_file i_generic
    (
        .clk     ( clk     ),
        .we      ( we      ),
        .waddr   ( waddr   ),
        .wdata   ( wdata   ),
        .raddr_a ( raddr_a ),
        .rdata_a ( rdata_a ),
        .raddr_b ( raddr_b ),
        .rdata_b ( rdata_b )
    );

    register_file_chips i_chips
    (
        .clk     ( clk           ),
        .we      ( we            ),
        .waddr   ( waddr         ),
        .wdata   ( wdata         ),
        .raddr_a ( raddr_a       ),
        .raddr_b ( raddr_b       ),
        .rdata_a ( rdata_a_chips ),
        .rdata_b ( rdata_b_chips )
    );

    //------------------------------------------------------------------------
    // Clock with a period of 10 ns.
    // On the breadboard one period is about a second.

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Initial values are fine in a testbench

    // verilator lint_off PROCASSINIT

    //------------------------------------------------------------------------
    // Reference model: four words, and which of them were written

    logic [3:0] model [0:3];
    logic [3:0] known = '0;

    always @ (posedge clk)
        if (we)
        begin
            model [waddr] <= wdata;
            known [waddr] <= 1'b1;
        end

    wire ok_a       = ! known [raddr_a] || rdata_a       === model [raddr_a];
    wire ok_b       = ! known [raddr_b] || rdata_b       === model [raddr_b];
    wire ok_a_chips = ! known [raddr_a] || rdata_a_chips === model [raddr_a];
    wire ok_b_chips = ! known [raddr_b] || rdata_b_chips === model [raddr_b];

    wire ok_generic = ok_a && ok_b;
    wire ok_chips   = ok_a_chips && ok_b_chips;

    //------------------------------------------------------------------------
    // Checking at every rising clock edge.  At the edge both designs and the
    // model still show the clock cycle that ends there, the same values that
    // a flip-flop would sample.

    logic started = 1'b0;  // There is no reset: set by the stimulus
    logic done    = 1'b0;  // Set by the stimulus before $finish

    int cycle    = 0;
    int n_checks = 0;
    int n_errors = 0;

    logic [7:0] seen = '0;  // Word k read through port A (bit k) and B (bit 4 + k) after it was written

    always @ (posedge clk)
    begin
        if (started & ~ done)
        begin
            cycle    <= cycle + 1;
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1);

            if (known [raddr_a])
                seen [3' (raddr_a)] <= 1'b1;

            if (known [raddr_b])
                seen [3' (4 + raddr_b)] <= 1'b1;

            $display ("%5d  %b   %0d    %2d  | %0d  %2d  | %0d  %2d  | %h %h %h %h  %s  %s",
                cycle + 1, we, waddr, wdata, raddr_a, rdata_a, raddr_b, rdata_b,
                model [0], model [1], model [2], model [3],
                ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end
    end

    //------------------------------------------------------------------------
    // Driving the inputs

    // One press of WRITE: hold it for one clock edge, then release it

    task automatic write_word (input logic [1:0] address, input logic [3:0] value,
                               input logic [1:0] read_a, input logic [1:0] read_b);
        we      <= 1'b1;
        waddr   <= address;
        wdata   <= value;
        raddr_a <= read_a;
        raddr_b <= read_b;
        @ (posedge clk);
        we      <= 1'b0;
        @ (posedge clk);
    endtask

    logic [31:0] seed = 32'd7;

    initial
    begin
        $dumpfile ("tb.vcd");

        `ifdef __ICARUS__
            $dumpvars (0, tb);  // Without a scope Icarus also tries to dump $unit
        `else
            $dumpvars;
        `endif

        $display ("cycle  WE  W    D   | RA  A  | RB  B  | words 0-3  generic  chips");

        // Before the first write the words are unknown, as after power-up

        repeat (2) @ (posedge clk);
        started <= 1'b1;

        // The example of section 2

        write_word (0,  5, 0, 1);
        write_word (1, 10, 0, 1);
        write_word (2,  3, 2, 1);
        write_word (3, 12, 2, 3);

        // D changes, but WRITE is released: nothing is written

        wdata   <= 15;
        raddr_a <= 3;
        raddr_b <= 0;
        @ (posedge clk);
        @ (posedge clk);

        write_word (0,  9, 0, 3);

        // Every pair of read addresses

        for (int k = 0; k < 16; k ++)
        begin
            raddr_a <= 2' (k);
            raddr_b <= 2' (k >> 2);
            @ (posedge clk);
        end

        // Pseudo-random writes and reads, the same in every simulator

        for (int i = 0; i < 120; i ++)
        begin
            seed = seed * 32'd1664525 + 32'd1013904223;

            we      <= seed [31] & seed [30];
            waddr   <= seed [29:28];
            wdata   <= seed [27:24];
            raddr_a <= seed [23:22];
            raddr_b <= seed [21:20];

            @ (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 && seen == 8'hff)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, words read %b", n_errors, n_checks, seen);
    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 BB9: 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 register_file.sv register_file_chips.sv cd40108.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 BB9: 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 register_file.sv register_file_chips.sv cd40108.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 BB9: 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 register_file.sv register_file_chips.sv cd40108.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  WE  W    D   | RA  A  | RB  B  | words 0-3  generic  chips
    1  1   0     5  | 0   x  | 1   x  | x x x x  ok          ok
    2  0   0     5  | 0   5  | 1   x  | 5 x x x  ok          ok
    3  1   1    10  | 0   5  | 1   x  | 5 x x x  ok          ok
    4  0   1    10  | 0   5  | 1  10  | 5 a x x  ok          ok
    5  1   2     3  | 2   x  | 1  10  | 5 a x x  ok          ok
    6  0   2     3  | 2   3  | 1  10  | 5 a 3 x  ok          ok
    7  1   3    12  | 2   3  | 3   x  | 5 a 3 x  ok          ok
    8  0   3    12  | 2   3  | 3  12  | 5 a 3 c  ok          ok
    9  0   3    15  | 3  12  | 0   5  | 5 a 3 c  ok          ok
   10  0   3    15  | 3  12  | 0   5  | 5 a 3 c  ok          ok
   11  1   0     9  | 0   5  | 3  12  | 5 a 3 c  ok          ok
   12  0   0     9  | 0   9  | 3  12  | 9 a 3 c  ok          ok
   13  0   0     9  | 0   9  | 0   9  | 9 a 3 c  ok          ok
   14  0   0     9  | 1  10  | 0   9  | 9 a 3 c  ok          ok
   15  0   0     9  | 2   3  | 0   9  | 9 a 3 c  ok          ok
   16  0   0     9  | 3  12  | 0   9  | 9 a 3 c  ok          ok
   17  0   0     9  | 0   9  | 1  10  | 9 a 3 c  ok          ok
   18  0   0     9  | 1  10  | 1  10  | 9 a 3 c  ok          ok
   19  0   0     9  | 2   3  | 1  10  | 9 a 3 c  ok          ok
   20  0   0     9  | 3  12  | 1  10  | 9 a 3 c  ok          ok
   21  0   0     9  | 0   9  | 2   3  | 9 a 3 c  ok          ok
   22  0   0     9  | 1  10  | 2   3  | 9 a 3 c  ok          ok
   23  0   0     9  | 2   3  | 2   3  | 9 a 3 c  ok          ok
   24  0   0     9  | 3  12  | 2   3  | 9 a 3 c  ok          ok
   25  0   0     9  | 0   9  | 3  12  | 9 a 3 c  ok          ok
   26  0   0     9  | 1  10  | 3  12  | 9 a 3 c  ok          ok
   27  0   0     9  | 2   3  | 3  12  | 9 a 3 c  ok          ok
   28  0   0     9  | 3  12  | 3  12  | 9 a 3 c  ok          ok
  …
  147  1   2     9  | 0   6  | 3  14  | 6 a 2 e  ok          ok
  148  0   3     2  | 1  10  | 0   6  | 6 a 9 e  ok          ok
tb.sv:214: $finish called at 1505000 (1ps)
PASS: 296 checks, no errors

To see that the testbench catches mistakes, break something and run it again: replace if (we) with if (1) in register_file.sv, so that every clock edge writes, or swap READ 0A and READ 1A in register_file_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_07_shift_register to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add register_file.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 design runs from slow_clk, which every BGM board makes at 1 Hz, so it steps once a second like the breadboard with the 555 module. BGM makes it in peripherals/slow_clk_gen.sv: a counter on the board clock toggles a flip-flop, and on AMD (Xilinx) and Intel (Altera) FPGAs a BUFG or global primitive puts that signal on the FPGA clock network, so it reaches all flip-flops like a real clock.

Switches 3–0 are the data and switches 5–4 the address: the word to write, and the word that read port A shows. Switches 7–6 are the address of read port B. Hold key 0, WRITE, until the next tick of slow_clk. LEDs 7–4 show port A and LEDs 3–0 port B. To run the chip-level design instead, add register_file_chips.sv and cd40108.sv and change the module name in the instance.

bgm_lab/lab_top.sv102 lines · show the codefile
// Exercise BB9 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_07_shift_register to a new directory, replace its
// lab_top.sv with this file and add register_file.sv.  For the chip-level
// design add register_file_chips.sv and cd40108.sv instead, and change the
// module name below.
//
// The register file runs from slow_clk, 1 Hz on every BGM board.  Switches
// 3-0 are the data and switches 5-4 the address: the word to write and the
// word that read port A shows.  Switches 7-6 are the address of read port
// B.  Hold key 0, WRITE, until the next tick of slow_clk.  LEDs 7-4 show
// read port A and LEDs 3-0 read port B.

`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 [3:0] rdata_a, rdata_b;

    register_file i_register_file
    (
        .clk     ( slow_clk  ),
        .we      ( key [0]   ),
        .waddr   ( sw  [5:4] ),
        .wdata   ( sw  [3:0] ),
        .raddr_a ( sw  [5:4] ),
        .rdata_a ( rdata_a   ),
        .raddr_b ( sw  [7:6] ),
        .rdata_b ( rdata_b   )
    );

    assign led = w_led' ({ rdata_a, rdata_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/5_shift_register.
  2. Replace its hackathon_top.sv with bgm_hackathon/hackathon_top.sv and add register_file.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Keys 3–0 are the data and keys 5–4 the address, for writing and for read port A. Hold key 7, WRITE, until the next tick of slow_clock. Read port B shows the four words in turn, one per tick, so you can watch the whole file. The screen shows port A in blue on the top row and port B in orange below it, with the address of port B on the left.

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

// Exercise BB9 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/5_shift_register, replace its
// hackathon_top.sv with this file and add register_file.sv.
//
// The register file runs from slow_clock.  Keys 3-0 are the data and keys
// 5-4 the address: the word to write and the word that read port A shows.
// Hold key 7, WRITE, until the next tick of slow_clock.  Read port B shows
// the four words in turn, one per tick.  LEDs 7-4 show port A, LEDs 3-0
// port B.  The screen shows port A in blue on the top row, port B in orange
// below it, and the address of port B on the left.

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
);

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

    logic [1:0] raddr_b;
    logic [3:0] rdata_a, rdata_b;

    // Read port B scans the four words

    always_ff @ (posedge slow_clock or posedge reset)
        if (reset)
            raddr_b <= '0;
        else
            raddr_b <= raddr_b + 1'd1;

    register_file i_register_file
    (
        .clk     ( slow_clock ),
        .we      ( key [7]    ),
        .waddr   ( key [5:4]  ),
        .wdata   ( key [3:0]  ),
        .raddr_a ( key [5:4]  ),
        .rdata_a ( rdata_a    ),
        .raddr_b ( raddr_b    ),
        .rdata_b ( rdata_b    )
    );

    assign led      = { rdata_a, rdata_b };

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

    //------------------------------------------------------------------------
    // Square lamps on the 480 x 272 screen, bit 3 on the left: port A in
    // blue on the top row, port B in orange on the bottom row, and the two
    // address bits of port B in grey before it.  Bright for 1, dim for 0.

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

        for (int i = 0; i < 4; i ++)
        begin
            if (x >= 9' (200 + i * 50) & x < 9' (240 + i * 50) & y >= 80 & y < 120)
            begin
                green = rdata_a [3 - i] ? 40 : 8;
                blue  = rdata_a [3 - i] ? 31 : 6;
            end

            if (x >= 9' (200 + i * 50) & x < 9' (240 + i * 50) & y >= 152 & y < 192)
            begin
                red   = rdata_b [3 - i] ? 31 : 6;
                green = rdata_b [3 - i] ? 32 : 6;
            end
        end

        for (int i = 0; i < 2; i ++)
            if (x >= 9' (60 + i * 50) & x < 9' (100 + i * 50) & y >= 152 & y < 192)
            begin
                red   = raddr_b [1 - i] ? 20 : 4;
                green = raddr_b [1 - i] ? 40 : 8;
                blue  = raddr_b [1 - i] ? 20 : 4;
            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.

The demo board makes the project clock itself and can set it anywhere from 1 Hz to 66.5 MHz (see Clock in the Tiny Tapeout docs). Set it to 1 Hz, and project.v connects clk straight to the register file, just as the 555 module clocks the chips on the breadboard.

DIP switches 0–3 are the data and switches 4–5 the address, for writing and for read port A. Switch 6 is WRITE: turn it on, wait for a tick of the clock, and turn it off again. The display shows port A as a hexadecimal digit. BIDIR pins 0–1 are the address of port B and pins 4–7 show its word. Like the 40108, the design has no reset.

tiny_tapeout/project.v81 lines · show the codefile
/*
 * Exercise BB9 on Tiny Tapeout: a register file of four 4-bit words with
 * one write port and two read ports.  The 7-segment display of the demo
 * board shows read port A as a hexadecimal digit, and read port B goes to
 * the BIDIR Pmod.
 *
 * Add register_file.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
);

    // The demo board makes the project clock; set it to 1 Hz.  DIP switches
    // 0-3 are the data and switches 4-5 the address: the word to write and
    // the word that read port A shows.  Switch 6 is WRITE: turn it on, wait
    // for a tick of the clock, and turn it off again.  BIDIR pins 0-1 are
    // the address of read port B, pins 4-7 show its word.  There is no
    // reset, as in the 40108.

    wire [3:0] rdata_a, rdata_b;

    register_file i_register_file (
        .clk     ( clk          ),
        .we      ( ui_in  [6]   ),
        .waddr   ( ui_in  [5:4] ),
        .wdata   ( ui_in  [3:0] ),
        .raddr_a ( ui_in  [5:4] ),
        .rdata_a ( rdata_a      ),
        .raddr_b ( uio_in [1:0] ),
        .rdata_b ( rdata_b      )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g.
    // Read port A as a hexadecimal digit: 0-9, then A, b, C, d, E, F.

    reg [6:0] gfedcba;

    always @(*)
        case (rdata_a)
        4'h0: gfedcba = 7'b0111111;
        4'h1: gfedcba = 7'b0000110;
        4'h2: gfedcba = 7'b1011011;
        4'h3: gfedcba = 7'b1001111;
        4'h4: gfedcba = 7'b1100110;
        4'h5: gfedcba = 7'b1101101;
        4'h6: gfedcba = 7'b1111101;
        4'h7: gfedcba = 7'b0000111;
        4'h8: gfedcba = 7'b1111111;
        4'h9: gfedcba = 7'b1101111;
        4'ha: gfedcba = 7'b1110111;
        4'hb: gfedcba = 7'b1111100;
        4'hc: gfedcba = 7'b0111001;
        4'hd: gfedcba = 7'b1011110;
        4'he: gfedcba = 7'b1111001;
        4'hf: gfedcba = 7'b1110001;
        endcase

    assign uo_out  = { 1'b0, gfedcba };

    // Read port B on BIDIR pins 4-7; pins 0-3 are inputs

    assign uio_out = { rdata_b, 4'b0 };
    assign uio_oe  = 8'hf0;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, rst_n, ui_in [7], uio_in [7:2], 1'b0 };

endmodule

In info.yaml set the clock to 1 Hz, list the files and use the module name as the top module:

project:
  clock_hz:   1                 # 1 Hz from the demo board
  top_module: "tt_um_example"   # rename to tt_um_<your GitHub name>_register_file
  source_files:
    - "project.v"
    - "register_file.sv"
No clock divider on a chip. BGM makes slow_clk with a counter, but on AMD and Intel FPGAs it routes that signal onto the clock network with BUFG or global (section 12). In an ASIC a counter bit used as a clock is bad practice: it is a second clock with its own clock tree, its edges come later than those of clk by a delay nobody controls, and every signal between the two clocks crosses clock domains. Here the board makes the 1 Hz clock, so no divider is needed. When a chip needs a slow tick from a fast clock, keep one clock and add a clock enable: a pulse one clock long that lets the slow logic change only on the edges where it is 1.

15Going further

Tasks for students

Files of this exercise

Sources

The exercise is in the Building blocks list of “An improved way of teaching school kids digital electronics” and in lesson 1.6 of the Habr article about the electronics course, which calls the chip rare. The chip is described in the Intersil CD40108BMS and STMicroelectronics HCF40108B datasheets.