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.
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.
- It is the first piece of a processor datapath: with an adder between the read ports and the write port it can already add two registers.
- It shows the difference between writing, which waits for the clock, and reading, which does not.
- It shows memory addressing in its simplest form: a decoder for writing and multiplexers for reading.
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 edge | Word 0 | Word 1 | Word 2 | Word 3 | R1A R0A | Bus A (LEDs 1-4) | R1B R0B | Bus B (LEDs 5-8) |
|---|---|---|---|---|---|---|---|---|
| write 5 to word 0 | 5 | ? | ? | ? | 00 | 0101 | 01 | ? |
| write 10 to word 1 | 5 | 10 | ? | ? | 00 | 0101 | 01 | 1010 |
| write 3 to word 2 | 5 | 10 | 3 | ? | 10 | 0011 | 01 | 1010 |
| write 12 to word 3 | 5 | 10 | 3 | 12 | 10 | 0011 | 11 | 1100 |
| D changes, WRITE released | 5 | 10 | 3 | 12 | 11 | 1100 | 00 | 0101 |
| write 9 to word 0 | 9 | 10 | 3 | 12 | 00 | 1001 | 11 | 1100 |
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
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 · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD40108 | 4 × 4 multiport register: one write port, two read ports | 1 | $28.84 | electronicplus.com |
| Button modules | the data D3-D0, the write address W1 W0 and WRITE; the first pack of five | 5 | $0.66 / pack | aliexpress.us |
| Button modules | 2 more from the second pack | 2 | $2.03 / pack | aliexpress.us |
| 555 clock module | the clock, set to about 1 Hz | 1 | $0.53 | aliexpress.us |
| 8-LED module | bus A on LEDs 1-4, bus B on LEDs 5-8 | 1 | $0.38 | aliexpress.us |
| Breadboard, 830 points | 1 | $3.00 | amazon.com | |
| Breadboard power module | 5 V on the rails | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Pre-cut jumper wires | 10 wires on the board (or male-male Dupont jumpers) | 10 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 33 leads to the clock module, the buttons and the LED module | 33 | $1.30 / pack | aliexpress.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
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.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | Q3 B | output | bus B, bit 3 | Q3B |
| 2 | Q2 B | output | bus B, bit 2 | Q2B |
| 3 | 3-STATE A | input | 1 turns bus A on, 0 turns it off (three-state) | +5V |
| 4 | Q0 A | output | bus A, bit 0 | Q0A |
| 5 | Q1 A | output | bus A, bit 1 | Q1A |
| 6 | Q2 A | output | bus A, bit 2 | Q2A |
| 7 | Q3 A | output | bus A, bit 3 | Q3A |
| 8 | WRITE 0 | input | write address, bit 0 | W0 |
| 9 | WRITE 1 | input | write address, bit 1 | W1 |
| 10 | READ 1B | input | read address of bus B, bit 1 | R1B (GND) |
| 11 | READ 0B | input | read address of bus B, bit 0 | R0B (+5V) |
| 12 | VSS | power | ground, 0 V | GND |
| 13 | READ 0A | input | read address of bus A, bit 0 | R0A (GND) |
| 14 | READ 1A | input | read address of bus A, bit 1 | R1A (GND) |
| 15 | WRITE ENABLE | input | 1 lets the clock write, 0 blocks it | WE |
| 16 | CLOCK | input | the word at the write address takes D0-D3 at the rising edge, 0 to 1, while WRITE ENABLE is 1 | CLK |
| 17 | D3 | input | data input, bit 3 | D3 |
| 18 | D2 | input | data input, bit 2 | D2 |
| 19 | D1 | input | data input, bit 1 | D1 |
| 20 | D0 | input | data input, bit 0 | D0 |
| 21 | 3-STATE B | input | 1 turns bus B on, 0 turns it off (three-state) | +5V |
| 22 | Q0 B | output | bus B, bit 0 | Q0B |
| 23 | Q1 B | output | bus B, bit 1 | Q1B |
| 24 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the register file
register_file.sv describes. The four read
wires are on the left, below the buttons. · SVG · PNG6Schematic 2: connections to the chip
| Net | From | To | Wire |
|---|---|---|---|
| CLK | 555 clock module, OUT | 40108 16 (CLK) | M-F |
| WE | button WRITE, OUT | 40108 15 (WE) | M-F |
| W1 | button W1, OUT | 40108 9 (W1) | M-F |
| W0 | button W0, OUT | 40108 8 (W0) | M-F |
| D3 | button D3, OUT | 40108 17 (D3) | M-F |
| D2 | button D2, OUT | 40108 18 (D2) | M-F |
| D1 | button D1, OUT | 40108 19 (D1) | M-F |
| D0 | button D0, OUT | 40108 20 (D0) | M-F |
| R1A | R1A wire: GND | 40108 14 (R1A) | M-M |
| R0A | R0A wire: GND | 40108 13 (R0A) | M-M |
| R1B | R1B wire: GND | 40108 10 (R1B) | M-M |
| R0B | R0B wire: +5V | 40108 11 (R0B) | M-M |
| Q3A | 40108 7 (Q3A) | LED module, LED 1 | M-F |
| Q2A | 40108 6 (Q2A) | LED module, LED 2 | M-F |
| Q1A | 40108 5 (Q1A) | LED module, LED 3 | M-F |
| Q0A | 40108 4 (Q0A) | LED module, LED 4 | M-F |
| Q3B | 40108 1 (Q3B) | LED module, LED 5 | M-F |
| Q2B | 40108 2 (Q2B) | LED module, LED 6 | M-F |
| Q1B | 40108 23 (Q1B) | LED module, LED 7 | M-F, M-M |
| Q0B | 40108 22 (Q0B) | LED module, LED 8 | M-F, M-M |
| +5V | +5V | 40108 24 (VDD), 40108 3 (ENA), 40108 21 (ENB) | |
| GND | GND | 40108 12 (VSS) |
7Schematic 3: the breadboard
Build it step by step
- Do the getting-started steps first: how the breadboard is connected inside, the power module with both jumpers on 5 V, how a chip sits across the gap.
- Insert the chips across the gap, notch to the left: the 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.
- 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.
- The fixed inputs: 3-STATE A and 3-STATE B to +5V.
- The four read wires: R1A, R0A and R1B to GND, R0B to +5V. These are the wires you move later.
- 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.
- Turn the power on and set the clock module to about one blink per second.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 2 wires | ||||
| 1 | +5V | 15a (40108 pin 24, VDD) | top + rail, column 15 | |
| 2 | GND | 26j (40108 pin 12, VSS) | bottom − rail, column 26 | |
| Fixed inputs: CMOS inputs must not float · 2 wires | ||||
| 7 | +5V | 17j (40108 pin 3, ENA) | bottom + rail, column 17 | |
| 8 | +5V | 18a (40108 pin 21, ENB) | top + rail, column 18 | |
| Mode wires: move them between + and − to change the mode · 4 wires | ||||
| 3 | R0A | 26a (40108 pin 13, R0A) | top − rail, column 26 | |
| 4 | R1A | 25a (40108 pin 14, R1A) | top − rail, column 25 | |
| 5 | R0B | 25j (40108 pin 11, R0B) | bottom + rail, column 25 | |
| 6 | R1B | 24j (40108 pin 10, R1B) | bottom − rail, column 24 | |
| Signal wires · 2 wires | ||||
| 9 | Q0B | 17b (40108 pin 22, Q0B) | 13f | |
| 10 | Q1B | 16c (40108 pin 23, Q1B) | 14f | |
| Modules on male-female jumpers · 33 wires | ||||
| 11 | +5V | button D3, + | top + rail, column 5 | |
| 12 | D3 | button D3, OUT | 22a (40108 pin 17, D3) | |
| 13 | GND | button D3, − | top − rail, column 7 | |
| 14 | +5V | button D2, + | top + rail, column 10 | |
| 15 | D2 | button D2, OUT | 21a (40108 pin 18, D2) | |
| 16 | GND | button D2, − | top − rail, column 12 | |
| 17 | +5V | button D1, + | top + rail, column 14 | |
| 18 | D1 | button D1, OUT | 20a (40108 pin 19, D1) | |
| 19 | GND | button D1, − | top − rail, column 17 | |
| 20 | +5V | button D0, + | top + rail, column 20 | |
| 21 | D0 | button D0, OUT | 19a (40108 pin 20, D0) | |
| 22 | GND | button D0, − | top − rail, column 22 | |
| 23 | +5V | button WRITE, + | top + rail, column 30 | |
| 24 | WE | button WRITE, OUT | 24a (40108 pin 15, WE) | |
| 25 | GND | button WRITE, − | top − rail, column 32 | |
| 26 | GND | 555 clock module, − | top − rail, column 37 | |
| 27 | CLK | 555 clock module, OUT | 23a (40108 pin 16, CLK) | |
| 28 | +5V | 555 clock module, + | top + rail, column 39 | |
| 29 | GND | LED module, G | bottom − rail, column 12 | |
| 30 | Q3A | LED module, LED 1 | 21j (40108 pin 7, Q3A) | |
| 31 | Q2A | LED module, LED 2 | 20j (40108 pin 6, Q2A) | |
| 32 | Q1A | LED module, LED 3 | 19j (40108 pin 5, Q1A) | |
| 33 | Q0A | LED module, LED 4 | 18j (40108 pin 4, Q0A) | |
| 34 | Q3B | LED module, LED 5 | 15j (40108 pin 1, Q3B) | |
| 35 | Q2B | LED module, LED 6 | 16j (40108 pin 2, Q2B) | |
| 36 | Q1B | LED module, LED 7 | 14j | |
| 37 | Q0B | LED module, LED 8 | 13j | |
| 38 | +5V | button W1, + | bottom + rail, column 33 | |
| 39 | W1 | button W1, OUT | 23j (40108 pin 9, W1) | |
| 40 | GND | button W1, − | bottom − rail, column 35 | |
| 41 | +5V | button W0, + | bottom + rail, column 38 | |
| 42 | W0 | button W0, OUT | 22j (40108 pin 8, W0) | |
| 43 | GND | button W0, − | bottom − rail, column 40 | |
Test it
- After power-up the LEDs show whatever the words hold.
- Hold D2 and D0 and press WRITE until the clock blinks: LEDs 1–4 show 0101, the number 5, in word 0.
- Hold W0, D3 and D1 and press WRITE: LEDs 5–8 show 1010, the number 10, in word 1.
- Write 3 into word 2 (W1) and 12 into word 3 (W1 and W0). Move the read wires and check that both buses can show every word, even the same word at once.
- Change the data buttons without WRITE: nothing changes.
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.
// 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.
// 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.
// 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.
- Copy
labs/1_basics/1_07_shift_registerto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addregister_file.sv. The BGM scripts compile every.svfile of the directory excepttb.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash; the first run asks which board you have.
The 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.
- Copy
hackathon/problems/5_shift_register. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addregister_file.sv. - Run
./03_synthesize_for_fpga.bashand./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"
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
- Swap. How many writes does it take to swap word 0 and word 1? You may use word 2.
- Add two registers. Connect bus A and bus B to the inputs of a 4008 adder (exercise B8) and its sum to D0–D3. Now one press of WRITE stores the sum of two words in a third: the datapath of a processor in two chips.
- Wider words. Two 40108s with their address, WRITE ENABLE and clock pins joined hold four 8-bit words.
- More words. Two 40108s hold eight 4-bit words: a third address bit chooses the chip with WRITE ENABLE for writing and with 3-STATE for reading, and the outputs of both chips share the same wires.
- In SystemVerilog: write the register file of RISC-V: 32 registers of 32 bits, where register 0 always reads 0.
Files of this exercise
- register_file.sv: the generic design (section 8)
- cd40108.sv: chip models (section 9)
- register_file_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
Sources
The exercise is in the 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.