Breadboard exercises/ P3EnglishРусский

Exercise P3 · project · FIFO and flow control

FIFO adder: three 40105s and a 4008 with backpressure

Two numbers that arrive at different times can only be added when both are there. Two FIFOs hold the operands until they can be paired, an adder adds each pair, and a third FIFO holds the sums until someone takes them. When that FIFO is full, everything before it waits. This is how the blocks of a GPU or a network chip pass data to each other.

7 chips3 × 40105, 4008, 4082, 2 × 4013
70 + 41 wireson and between the boards + leads to the modules
2 boardsthe inputs on top, the adder and C below
~1 Hza transfer takes two clocks

1What it is and why it matters

What it does. Three data buttons give a number from 0 to 7. PUSH A puts it into FIFO A, PUSH B into FIFO B, at any time and in any order. Whenever both A and B have a number and C has room, the circuit takes the two numbers at the heads of A and B, adds them and puts the sum into FIFO C. POP C takes the oldest sum out. LED module 1 shows A and B, LED module 2 shows C.

How it works. The FIFOs are the 40105 of exercise BB8, and the push and pop buttons go through 4013 flip-flops as there. The 4008 always adds the two heads. A 4-input AND of a 4082 gives GO = DOR A AND DOR B AND DIR C AND NOT T: both operands are there, C has room, and no transfer is under way. Flip-flop 2 of the second 4013 turns GO into the transfer pulse T. Its rising edge is SI of C: C takes the sum. One clock later its falling edge is SO of A and B: they drop the pair, and the next pair moves up.

Where it is used. Inside a chip, blocks work at their own pace: one makes data, another uses them. FIFOs between them absorb the differences. When a consumer stops, its input FIFO fills and its “room” flag tells the producer to wait; that flag travels backwards through the whole pipeline. This is backpressure, and the pair of signals “I have data” and “I have room”, here DOR and DIR, is the valid/ready handshake of modern chip design.

Why learn it.

2The key elements: a transfer and backpressure

One pair, step by step:

StepHead of AHead of BHead of CGOT
PUSH A 3300
PUSH B 23200
clock: GO = 13210
clock: T = 1, C takes 3 + 232501
clock: T = 0, A and B drop the pair500
POP C00

If C is full, DIR C is 0, GO stays 0 and A and B keep their words, however many pairs wait. Every POP C makes room for one more transfer. A push into a full A or B is ignored, as in exercise BB8.

Waveforms

CLKPUSH APUSH BDATA032012345456DOR ADOR BTDOR CC55POP C
From the simulation of tb.sv in Icarus Verilog: 3 into A, then 2 into B; the transfer pulse T moves the sum 5 into C, and POP C takes it out. Then five numbers into A and three into B: three transfers follow, one every two clocks. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD4013dual D flip-flop: #1 for PUSH A and POP C, #2 for PUSH B and the transfer pulse2$0.35Elexp (elexp.com)
CD74HC40105FIFO of sixteen 4-bit words: A and B for the operands, C for the sums3$1.74ti.com
CD40084-bit full adder: the head of A plus the head of B1$1.00leeselectronic.com
CD4082dual 4-input AND: GO (one of two used)1$0.35Elexp (elexp.com)
Button modulesD2-D0, PUSH A, PUSH B, POP C and RESET; 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 modulemodule 1: A and B; module 2: C2$0.38aliexpress.us
Breadboard, 830 pointstwo boards, one above the other2$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 wires70 wires on the board (or male-male Dupont jumpers)70$11.95 / packjameco.com
Male-female Dupont jumpers41 leads to the clock module, the buttons and the LED modules41$1.30 / packaliexpress.us

Prices from the LSSK components sheet; these chips are in the FIFO kit. The FIFO build guide of the kit builds the same circuit in two stages, BB8 first.

4The chips, pin by pin

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

40105: FIFO of sixteen 4-bit words

1OE2DIR3SI4D05D16D27D38GND9MR10Q311Q212Q113Q014DOR15SO16VCC4010516 × 4 FIFO
CD74HC40105. · SVG · PNG

As in exercise BB8, three times. A and B: SI from the PUSH flip-flops, SO from T, D0–D2 from the data buttons, D3 at GND, Q0–Q2 to the 4008, DOR to the 4082. C: SI from T, D0–D3 from the sum, SO from the POP C flip-flop, DIR to the 4082, Q0–Q3 and DOR to LED module 2. MR of all three is RESET, OE is at GND.

PinNameDirectionFunctionIn this circuit
1OEinputthree-state control: 0 turns Q0-Q3 on, 1 turns them offGND
2DIRoutputdata-in ready: 1 while there is room for a wordnot connected
3SIinputa rising edge, 0 to 1, loads D0-D3 at the end of the queueSI A
4D0inputdata input, bit 0D0
5D1inputdata input, bit 1D1
6D2inputdata input, bit 2D2
7D3inputdata input, bit 3GND
8GNDpowerground, 0 VGND
9MRinputmaster reset: 1 empties the queue at once, without SI or SO. Active highRST
10Q3outputthe word at the head of the queue, bit 3not connected
11Q2outputthe word at the head of the queue, bit 2A2
12Q1outputthe word at the head of the queue, bit 1A1
13Q0outputthe word at the head of the queue, bit 0A0
14DORoutputdata-out ready: 1 while Q0-Q3 show a wordDOR A
15SOinputa falling edge, 1 to 0, drops the word at the head; the next one moves upT
16VCCpowersupply, +5 V (2 to 6 V allowed)+5V

4008: 4-bit full adder

1A42B33A34B25A26B17A18VSS9CIN10S111S212S313S414COUT15B416VDD40084-bit adder
CD4008B. · SVG · PNG

As in exercise B8. A1–A3 and B1–B3 get the heads of A and B, A4, B4 and CARRY IN are at GND, and S1–S4 go to D0–D3 of C. The sum of two 3-bit numbers, at most 14, always fits into 4 bits.

PinNameDirectionFunctionIn this circuit
1A4inputnumber A, bit 3 (weight 8)GND
2B3inputnumber B, bit 2 (weight 4)B2
3A3inputnumber A, bit 2 (weight 4)A2
4B2inputnumber B, bit 1 (weight 2)B1
5A2inputnumber A, bit 1 (weight 2)A1
6B1inputnumber B, bit 0 (weight 1)B0
7A1inputnumber A, bit 0 (weight 1)A0
8VSSpowerground, 0 VGND
9CINinputcarry in, added to A + BGND
10S1outputsum, bit 0 (weight 1)S0
11S2outputsum, bit 1 (weight 2)S1
12S3outputsum, bit 2 (weight 4)S2
13S4outputsum, bit 3 (weight 8)S3
14COUToutputcarry out, the fifth bit of the sum (weight 16)not connected
15B4inputnumber B, bit 3 (weight 8)GND
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4082: dual 4-input AND

1J2A3B4C5D6NC7VSS8NC9E10F11G12H13K14VDD4082
CD4082B. · SVG · PNG

One gate makes GO from DOR A, DOR B, DIR C and NOT T. The inputs of the other gate are tied to GND.

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

4013: dual D flip-flop

1Q12Q13CLK14R15D16S17VSS8S29D210R211CLK212Q213Q214VDD40132 × D flip-flop
CD4013B. · SVG · PNG

Two chips. #1: one flip-flop samples PUSH A and drives SI of A, the other samples POP C and drives SO of C from its inverted output. #2: one flip-flop samples PUSH B, the other is the transfer flip-flop: D gets GO, Q is T and ~Q is NOT T. All take the clock, RESET goes to their reset inputs, the set inputs are at GND.

PinNameDirectionFunctionIn this circuit
1Q1outputflip-flop 1 outputnot connected
2Q1outputflip-flop 1 output, invertedSO C
3CLOCK1inputflip-flop 1 stores D1 on the rising edge, 0 to 1CLK
4RESET1input1 sets Q1 = 0 at once, without the clock. Active highRST
5D1inputflip-flop 1 data inputPOP C
6SET1input1 sets Q1 = 1 at once, without the clock. Active highGND
7VSSpowerground, 0 VGND
8SET2input1 sets Q2 = 1 at once. Active highGND
9D2inputflip-flop 2 data inputPUSH A
10RESET2input1 sets Q2 = 0 at once. Active highRST
11CLOCK2inputflip-flop 2 stores D2 on the rising edgeCLK
12Q2outputflip-flop 2 output, invertednot connected
13Q2outputflip-flop 2 outputSI A
14VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: FIFOs, adder and control

CLKRESETD2D1D0PUSH APUSH BPOP CDRQ4013 #1DRQ4013 #1DRQ4013 #240105 ASISOD2D1D0MRDORQ2Q1Q0FIFO A40105 BSISOD2D1D0MRDORQ2Q1Q0FIFO B40824008A3A2A1B3B2B1S4S3S2S1adderDRQQ4013 #240105 CSISOD3D2D1D0MRDORQ3Q2Q1Q0DIRFIFO CDOR CLED 1Q3LED 2Q2LED 3Q1LED 4Q0LED 5DIR CLED 8
The circuit as blocks. This is what fifo_adder.sv describes. The lines along the top and the bottom bring T, NOT T and DIR C back. · SVG · PNG

6Schematic 2: connections between the chips

1234567891011121314Q1Q1CLK1R1D1S1S2D2R2CLK2Q2Q240132 × D flip-flop4013 #112345678910111213141516OEDIRSID0D1D2D3MRQ3Q2Q1Q0DORSO4010516 × 4 FIFO40105 A12345678910111213141516OEDIRSID0D1D2D3MRQ3Q2Q1Q0DORSO4010516 × 4 FIFO40105 B1234567891011121314Q1Q1CLK1R1D1S1S2D2R2CLK2Q2Q240132 × D flip-flop4013 #212345678910111213141516A4B3A3B2A2B1A1CINS1S2S3S4COUTB440084-bit adder12345678910111213141516OEDIRSID0D1D2D3MRQ3Q2Q1Q0DORSO4010516 × 4 FIFO40105 C12345678910111213144082CLKRSTPUSH APUSH BPOP CSI ASI BSO CD0D1D2A2B0B1B2DOR ADOR BS0S1S2S3DIR CTGOTDOR CC3C2C1C0D2D1D0PUSH APUSH BPOP CRESETclockLED module: LEDs 1-8LED module: LEDs 1, 2, 3, 4, 5, 7, 8+5V
The same circuit as chips, pin by pin, one row per board. · SVG · PNG
NetFromToWire
CLK555 clock module, OUT4013 #1 11 (CLK2), 4013 #1 3 (CLK1), 4013 #2 3 (CLK1), 4013 #2 11 (CLK2)M-F, M-M
RSTbutton RESET, OUT4013 #1 10 (R2), 4013 #1 4 (R1), 4013 #2 4 (R1), 4013 #2 10 (R2), 40105 A 9 (MR), 40105 B 9 (MR), 40105 C 9 (MR)M-F, M-M
D2button D2, OUT40105 A 6 (D2), 40105 B 6 (D2)M-F, M-M (loose jumper)
D1button D1, OUT40105 A 5 (D1), 40105 B 5 (D1)M-F, M-M
D0button D0, OUT40105 A 4 (D0), 40105 B 4 (D0)M-F, M-M
PUSH Abutton PUSH A, OUT4013 #1 9 (D2)M-F
PUSH Bbutton PUSH B, OUT4013 #2 5 (D1)M-F, M-M
POP Cbutton POP C, OUT4013 #1 5 (D1)M-F, M-M
SI A4013 #1 13 (Q2)40105 A 3 (SI)M-M
SI B4013 #2 1 (Q1)40105 B 3 (SI)M-M
SO C4013 #1 2 (Q1)40105 C 15 (SO)M-M
DOR A40105 A 14 (DOR)4082 4 (C), LED module, LED 1M-F, M-M
A240105 A 11 (Q2)4008 3 (A3), LED module, LED 2M-F, M-M
A140105 A 12 (Q1)4008 5 (A2), LED module, LED 3M-F, M-M
A040105 A 13 (Q0)4008 7 (A1), LED module, LED 4M-F, M-M
DOR B40105 B 14 (DOR)4082 2 (A), LED module, LED 5M-F, M-M
B240105 B 11 (Q2)4008 2 (B3), LED module, LED 6M-F, M-M
B140105 B 12 (Q1)4008 4 (B2), LED module, LED 7M-F, M-M
B040105 B 13 (Q0)4008 6 (B1), LED module, LED 8M-F, M-M
S34008 13 (S4)40105 C 7 (D3)M-M
S24008 12 (S3)40105 C 6 (D2)M-M
S14008 11 (S2)40105 C 5 (D1)M-M
S04008 10 (S1)40105 C 4 (D0)M-M
DIR C40105 C 2 (DIR)4082 3 (B), LED module, LED 8M-F, M-M
GO4082 1 (J)4013 #2 9 (D2)M-M
T4013 #2 13 (Q2)40105 A 15 (SO), 40105 B 15 (SO), 40105 C 3 (SI), LED module, LED 7M-F, M-M (loose jumper)
T4013 #2 12 (Q2)4082 5 (D)M-M
DOR C40105 C 14 (DOR)LED module, LED 1M-F
C340105 C 10 (Q3)LED module, LED 2M-F
C240105 C 11 (Q2)LED module, LED 3M-F
C140105 C 12 (Q1)LED module, LED 4M-F
C040105 C 13 (Q0)LED module, LED 5M-F
+5V+5V4013 #1 14 (VDD), 40105 A 16 (VCC), 40105 B 16 (VCC), 4013 #2 14 (VDD), 4008 16 (VDD), 40105 C 16 (VCC), 4082 14 (VDD), 4082 12 (H), 4082 11 (G), 4082 10 (F), 4082 9 (E)
GNDGND4013 #1 7 (VSS), 40105 A 8 (GND), 40105 B 8 (GND), 4013 #2 7 (VSS), 4008 8 (VSS), 40105 C 8 (GND), 4082 7 (VSS), 4013 #1 6 (S1), 4013 #1 8 (S2), 40105 A 7 (D3), 40105 A 1 (OE), 40105 B 7 (D3), 40105 B 1 (OE), 4013 #2 6 (S1), 4013 #2 8 (S2), 4008 1 (A4), 4008 15 (B4), 4008 9 (CIN), 40105 C 1 (OE)
not connected4013 #1 1 (Q1), 4013 #1 12 (Q2), 40105 A 2 (DIR), 40105 A 10 (Q3), 40105 B 2 (DIR), 40105 B 10 (Q3), 4013 #2 2 (Q1), 4008 14 (COUT), 4082 6 (NC), 4082 8 (NC), 4082 13 (K)

7Schematic 3: the breadboards

−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 1−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 25V5VPOWER MODULE · 5V9V USB-DC4013 #140105 A40105 B4013 #2400840105 C4082CLKCLKCLKRSTRSTRSTRSTRSTRSTPUSH BSI ASI BSO CD0D1D2A0A1A2B0B1B2DOR AS0S1S2S3DIR CTGOTT+OUT−D2+OUT−D1+OUT−D0+OUT−PUSH A+OUT−PUSH B+OUT−POP C+OUT−RESET−OUT+555CLOCK 555~1 HzG1DOR A2A23A14A05DOR B6B27B18B08-LED MODULELEDs 1-8 usedG1DOR C2C33C24C15C067T8DIR C8-LED MODULELEDs 1, 2, 3, 4, 5, 7, 8 used
Every wire in its hole, on two boards: the input FIFOs and their flip-flops on the top board, the adder, the AND and FIFO C on the bottom board with the power module. The buttons and the clock module hang above the top board, the two LED modules below the bottom one. Wires drawn as loose arcs could not be laid flat. · SVG · PNG
CLKRSTD2D1D0PUSH APUSH BPOP CSI ASI BSO CDOR AA2A1A0DOR BB2B1B0S3S2S1S0DIR CGOTTDOR CC3C2C1C0+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: the 4013 #1, notch to the left, pin 1 in hole 5f of board 1; the 40105 A turned around, notch to the right, pin 1 in hole 20e of board 1; the 40105 B turned around, notch to the right, pin 1 in hole 31e of board 1; the 4013 #2, notch to the left, pin 1 in hole 35f of board 1; the 4008, notch to the left, pin 1 in hole 7f of board 2; the 40105 C turned around, notch to the right, pin 1 in hole 26e of board 2; the 4082, notch to the left, pin 1 in hole 30f of board 2. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
  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 40105 these pins are called VCC and GND. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
  4. The rail jumpers on the right: they bring +5V and GND from the bottom board to the top one.
  5. The fixed inputs and the signal wires, board by board, following the checklist. Tick each wire as you insert it.
  6. With the power off, the modules: D2–D0, PUSH A, PUSH B, POP C, RESET and the clock module above the top board, the two LED modules below the bottom one.
  7. Turn the power on, set the clock module to about one blink per second, and press RESET once.

Wiring checklist

·

#NetFromTo
Rails of the second board (from the board with the power module) · 4 wires
1+5Vboard 2: top + rail, column 2board 1: bottom + rail, column 2
2GNDboard 2: top − rail, column 4board 1: bottom − rail, column 4
3+5Vboard 1: top + rail, column 63board 1: bottom + rail, column 63
4GNDboard 1: top − rail, column 62board 1: bottom − rail, column 62
Power (red to +, black to −) · 14 wires
5+5Vboard 1: 5a (4013 #1 pin 14, VDD)board 1: top + rail, column 5
6GNDboard 1: 11j (4013 #1 pin 7, VSS)board 1: bottom − rail, column 11
7+5Vboard 1: 20j (40105 A pin 16, VCC)board 1: bottom + rail, column 20
8GNDboard 1: 13a (40105 A pin 8, GND)board 1: top − rail, column 13
9+5Vboard 1: 31j (40105 B pin 16, VCC)board 1: bottom + rail, column 31
10GNDboard 1: 24a (40105 B pin 8, GND)board 1: top − rail, column 24
11+5Vboard 1: 35a (4013 #2 pin 14, VDD)board 1: top + rail, column 35
12GNDboard 1: 41j (4013 #2 pin 7, VSS)board 1: bottom − rail, column 41
13+5Vboard 2: 7a (4008 pin 16, VDD)board 2: top + rail, column 7
14GNDboard 2: 14j (4008 pin 8, VSS)board 2: bottom − rail, column 14
15+5Vboard 2: 26j (40105 C pin 16, VCC)board 2: bottom + rail, column 26
16GNDboard 2: 19a (40105 C pin 8, GND)board 2: top − rail, column 19
17+5Vboard 2: 30a (4082 pin 14, VDD)board 2: top + rail, column 30
18GNDboard 2: 36j (4082 pin 7, VSS)board 2: bottom − rail, column 36
Fixed inputs: CMOS inputs must not float · 16 wires
19GNDboard 1: 10j (4013 #1 pin 6, S1)board 1: bottom − rail, column 10
20GNDboard 1: 11a (4013 #1 pin 8, S2)board 1: top − rail, column 11
21GNDboard 1: 14a (40105 A pin 7, D3)board 1: top − rail, column 14
22GNDboard 1: 20a (40105 A pin 1, OE)board 1: top − rail, column 20
23GNDboard 1: 25a (40105 B pin 7, D3)board 1: top − rail, column 25
24GNDboard 1: 31a (40105 B pin 1, OE)board 1: top − rail, column 31
25GNDboard 1: 40j (4013 #2 pin 6, S1)board 1: bottom − rail, column 40
26GNDboard 1: 41a (4013 #2 pin 8, S2)board 1: top − rail, column 41
27GNDboard 2: 7j (4008 pin 1, A4)board 2: bottom − rail, column 7
28GNDboard 2: 8a (4008 pin 15, B4)board 2: top − rail, column 8
29GNDboard 2: 14a (4008 pin 9, CIN)board 2: top − rail, column 14
30GNDboard 2: 26a (40105 C pin 1, OE)board 2: top − rail, column 26
31+5Vboard 2: 32a (4082 pin 12, H)board 2: top + rail, column 32
32+5Vboard 2: 33a (4082 pin 11, G)board 2: top + rail, column 33
33+5Vboard 2: 34a (4082 pin 10, F)board 2: top + rail, column 34
34+5Vboard 2: 35a (4082 pin 9, E)board 2: top + rail, column 35
Signal wires · 36 wires
35CLKboard 1: 8c (4013 #1 pin 11, CLK2)board 1: 7h (4013 #1 pin 3, CLK1)
36CLKboard 1: 8d (4013 #1 pin 11, CLK2)board 1: 38c (4013 #2 pin 11, CLK2)
37CLKboard 1: 38b (4013 #2 pin 11, CLK2)board 1: 37i (4013 #2 pin 3, CLK1)
38RSTboard 1: 9b (4013 #1 pin 10, R2)board 1: 8g (4013 #1 pin 4, R1)
39RSTboard 1: 8h (4013 #1 pin 4, R1)board 1: 13g (40105 A pin 9, MR)
40RSTboard 1: 13h (40105 A pin 9, MR)board 1: 24g (40105 B pin 9, MR)
41RSTboard 1: 24h (40105 B pin 9, MR)board 1: 38g (4013 #2 pin 4, R1)
42RSTboard 1: 38h (4013 #2 pin 4, R1)board 1: 39c (4013 #2 pin 10, R2)
43RSTboard 1: 24i (40105 B pin 9, MR)board 2: 19h (40105 C pin 9, MR)
44PUSH Bboard 1: 39g (4013 #2 pin 5, D1)board 1: 34e
45POP Cboard 1: 9h (4013 #1 pin 5, D1)board 1: 12e
46SI Aboard 1: 6d (4013 #1 pin 13, Q2)board 1: 18d (40105 A pin 3, SI)
47SI Bboard 1: 35i (4013 #2 pin 1, Q1)board 1: 29b (40105 B pin 3, SI)
48SO Cboard 1: 6g (4013 #1 pin 2, Q1)board 2: 25g (40105 C pin 15, SO)
49D0board 1: 17b (40105 A pin 4, D0)board 1: 28b (40105 B pin 4, D0)
50D1board 1: 16b (40105 A pin 5, D1)board 1: 27a (40105 B pin 5, D1)
51D2 (loose jumper)board 1: 15c (40105 A pin 6, D2)board 1: 26c (40105 B pin 6, D2)
52A0board 1: 17g (40105 A pin 13, Q0)board 2: 13i (4008 pin 7, A1)
53A1board 1: 16g (40105 A pin 12, Q1)board 2: 11g (4008 pin 5, A2)
54A2board 1: 15g (40105 A pin 11, Q2)board 2: 9h (4008 pin 3, A3)
55B0board 1: 28g (40105 B pin 13, Q0)board 2: 12i (4008 pin 6, B1)
56B1board 1: 27g (40105 B pin 12, Q1)board 2: 10g (4008 pin 4, B2)
57B2board 1: 26j (40105 B pin 11, Q2)board 2: 8h (4008 pin 2, B3)
58DOR Aboard 1: 18g (40105 A pin 14, DOR)board 2: 33g (4082 pin 4, C)
59DOR Bboard 1: 29g (40105 B pin 14, DOR)board 2: 31i (4082 pin 2, A)
60S0board 2: 13d (4008 pin 10, S1)board 2: 23d (40105 C pin 4, D0)
61S1board 2: 12c (4008 pin 11, S2)board 2: 22c (40105 C pin 5, D1)
62S2board 2: 11b (4008 pin 12, S3)board 2: 21b (40105 C pin 6, D2)
63S3board 2: 10a (4008 pin 13, S4)board 2: 20a (40105 C pin 7, D3)
64DIR Cboard 2: 25c (40105 C pin 2, DIR)board 2: 32i (4082 pin 3, B)
65Tboard 1: 37b (4013 #2 pin 12, Q2)board 2: 34g (4082 pin 5, D)
66GOboard 2: 30g (4082 pin 1, J)board 1: 40d (4013 #2 pin 9, D2)
67Tboard 1: 36d (4013 #2 pin 13, Q2)board 1: 30g (40105 B pin 15, SO)
68Tboard 1: 30i (40105 B pin 15, SO)board 1: 19i (40105 A pin 15, SO)
69T (loose jumper)board 1: 30h (40105 B pin 15, SO)board 2: 27h
70Tboard 2: 27fboard 2: 24d (40105 C pin 3, SI)
Modules on male-female jumpers · 41 wires
71+5Vbutton D2, +board 1: top + rail, column 4
72D2button D2, OUTboard 1: 15a (40105 A pin 6, D2)
73GNDbutton D2, −board 1: top − rail, column 7
74+5Vbutton D1, +board 1: top + rail, column 10
75D1button D1, OUTboard 1: 16a (40105 A pin 5, D1)
76GNDbutton D1, −board 1: top − rail, column 12
77+5Vbutton D0, +board 1: top + rail, column 15
78D0button D0, OUTboard 1: 17a (40105 A pin 4, D0)
79GNDbutton D0, −board 1: top − rail, column 17
80+5Vbutton PUSH A, +board 1: top + rail, column 21
81PUSH Abutton PUSH A, OUTboard 1: 10a (4013 #1 pin 9, D2)
82GNDbutton PUSH A, −board 1: top − rail, column 23
83+5Vbutton PUSH B, +board 1: top + rail, column 28
84PUSH Bbutton PUSH B, OUTboard 1: 34a
85GNDbutton PUSH B, −board 1: top − rail, column 30
86+5Vbutton POP C, +board 1: top + rail, column 34
87POP Cbutton POP C, OUTboard 1: 12a
88GNDbutton POP C, −board 1: top − rail, column 37
89+5Vbutton RESET, +board 1: top + rail, column 42
90RSTbutton RESET, OUTboard 1: 39a (4013 #2 pin 10, R2)
91GNDbutton RESET, −board 1: top − rail, column 44
92GND555 clock module, −board 1: top − rail, column 49
93CLK555 clock module, OUTboard 1: 38a (4013 #2 pin 11, CLK2)
94+5V555 clock module, +board 1: top + rail, column 51
95GNDLED module, Gboard 2: bottom − rail, column 10
96DOR ALED module, LED 1board 2: 33j (4082 pin 4, C)
97A2LED module, LED 2board 2: 9j (4008 pin 3, A3)
98A1LED module, LED 3board 2: 11j (4008 pin 5, A2)
99A0LED module, LED 4board 2: 13j (4008 pin 7, A1)
100DOR BLED module, LED 5board 2: 31j (4082 pin 2, A)
101B2LED module, LED 6board 2: 8j (4008 pin 2, B3)
102B1LED module, LED 7board 2: 10j (4008 pin 4, B2)
103B0LED module, LED 8board 2: 12j (4008 pin 6, B1)
104GNDLED module, Gboard 2: bottom − rail, column 32
105DOR CLED module, LED 1board 2: 24j (40105 C pin 14, DOR)
106C3LED module, LED 2board 2: 20j (40105 C pin 10, Q3)
107C2LED module, LED 3board 2: 21j (40105 C pin 11, Q2)
108C1LED module, LED 4board 2: 22j (40105 C pin 12, Q1)
109C0LED module, LED 5board 2: 23j (40105 C pin 13, Q0)
110TLED module, LED 7board 2: 27j
111DIR CLED module, LED 8board 2: 32j (4082 pin 3, B)

Test it

If it does not work: build and test FIFO A with its flip-flop first, as in exercise BB8. If a sum lands in C but A and B never drop the pair, check T to SO of A and B. If transfers never start, check the four inputs of the 4082, and that NOT T comes from ~Q of the transfer flip-flop. If C holds wrong sums, check the order of the wires between the 4008 and A, B and C.

8SystemVerilog: the generic design

fifo_adder.sv has three FIFOs written as arrays with read and write counters, as in exercise BB8, an adder, and the transfer flip-flop. It is synthesizable and runs from one clock.

fifo_adder.svfile
// Exercise P3: the FIFO adder.  Numbers pushed into FIFO A and FIFO B at
// different times are added in pairs; the sums wait in FIFO C until they
// are popped.  When C is full, A and B keep their words: backpressure.
//
// Generic version that matches schematic 1, not tied to any chip.  Like
// the 4013s of the breadboard, the design samples the buttons at the
// rising edge of clk and acts once per press.  A transfer takes two clock
// edges, as the transfer flip-flop does it:
//
//     go = A has data & B has data & C has room & ~ transfer
//     the edge that sees go:        transfer goes to 1, C takes the sum of
//                                   the heads of A and B
//     the edge after it:            transfer goes to 0, A and B drop the
//                                   pair
//
// The operands are 3 bits wide, so the sum always fits into 4 bits.  rst
// empties the FIFOs and clears the flip-flops at once.  Synthesizable.

module fifo_adder
(
    input  logic       clk,
    input  logic       rst,

    input  logic       push_a,
    input  logic       push_b,
    input  logic       pop_c,
    input  logic [2:0] data,

    output logic [2:0] head_a,
    output logic       dor_a,      // A has data
    output logic [2:0] head_b,
    output logic       dor_b,      // B has data
    output logic [3:0] head_c,
    output logic       dor_c,      // C has data
    output logic       dir_c,      // C has room
    output logic       transfer    // The transfer pulse, T
);

    //------------------------------------------------------------------------
    // The buttons at the last clock edge, like the 4013 flip-flops

    logic push_a_q, push_b_q, pop_c_q;

    wire push_a_now = push_a & ~ push_a_q;
    wire push_b_now = push_b & ~ push_b_q;
    wire pop_c_now  = pop_c  & ~ pop_c_q;

    //------------------------------------------------------------------------
    // Three FIFOs of sixteen words: write and read counters with one bit
    // more than the address, equal when empty, 16 apart when full

    logic [2:0] word_a [0:15], word_b [0:15];
    logic [3:0] word_c [0:15];
    logic [4:0] wr_a, rd_a, wr_b, rd_b, wr_c, rd_c;

    wire dir_a = wr_a != { ~ rd_a [4], rd_a [3:0] };
    wire dir_b = wr_b != { ~ rd_b [4], rd_b [3:0] };

    assign dor_a  = wr_a != rd_a;
    assign dor_b  = wr_b != rd_b;
    assign dor_c  = wr_c != rd_c;
    assign dir_c  = wr_c != { ~ rd_c [4], rd_c [3:0] };

    assign head_a = word_a [rd_a [3:0]];
    assign head_b = word_b [rd_b [3:0]];
    assign head_c = word_c [rd_c [3:0]];

    //------------------------------------------------------------------------
    // The adder and the transfer

    wire [3:0] sum = 4' (head_a) + 4' (head_b);
    wire       go  = dor_a & dor_b & dir_c & ~ transfer;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
        begin
            push_a_q <= 1'b0;
            push_b_q <= 1'b0;
            pop_c_q  <= 1'b0;
            transfer <= 1'b0;
            wr_a     <= '0;
            rd_a     <= '0;
            wr_b     <= '0;
            rd_b     <= '0;
            wr_c     <= '0;
            rd_c     <= '0;
        end
        else
        begin
            push_a_q <= push_a;
            push_b_q <= push_b;
            pop_c_q  <= pop_c;
            transfer <= go;

            if (push_a_now & dir_a)
                wr_a <= wr_a + 5'd1;

            if (push_b_now & dir_b)
                wr_b <= wr_b + 5'd1;

            if (go)
                wr_c <= wr_c + 5'd1;

            if (transfer)
            begin
                rd_a <= rd_a + 5'd1;
                rd_b <= rd_b + 5'd1;
            end

            if (pop_c_now & dor_c)
                rd_c <= rd_c + 5'd1;
        end

    always_ff @ (posedge clk)
    begin
        if (push_a_now & dir_a)
            word_a [wr_a [3:0]] <= data;

        if (push_b_now & dir_b)
            word_b [wr_b [3:0]] <= data;

        if (go)
            word_c [wr_c [3:0]] <= sum;
    end

endmodule

9SystemVerilog models of the chips

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

cd4013.svfile
// CD4013B: dual D flip-flop with set and reset.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out.  Synthesizable.
//
// Each flip-flop stores D at the rising edge of its clock.  SET and RESET
// are active high and asynchronous: they act at once, without the clock.
// With SET and RESET both high the chip sets both Q and ~Q high; the model
// does the same while they are both high.  Do not release them at the same
// moment: which of the two wins is not defined.

module cd4013
(
    output logic q1,      // Pin 1
    output logic q1_n,    // Pin 2:  ~Q1
    input  logic clock1,  // Pin 3
    input  logic reset1,  // Pin 4:  1 = Q1 goes to 0 at once
    input  logic d1,      // Pin 5
    input  logic set1,    // Pin 6:  1 = Q1 goes to 1 at once
                          // Pin 7:  VSS
    input  logic set2,    // Pin 8
    input  logic d2,      // Pin 9
    input  logic reset2,  // Pin 10
    input  logic clock2,  // Pin 11
    output logic q2_n,    // Pin 12: ~Q2
    output logic q2       // Pin 13
                          // Pin 14: VDD
);

    logic r1, r2;

    always_ff @ (posedge clock1 or posedge reset1 or posedge set1)
        if (reset1)
            r1 <= 1'b0;
        else if (set1)
            r1 <= 1'b1;
        else
            r1 <= d1;

    always_ff @ (posedge clock2 or posedge reset2 or posedge set2)
        if (reset2)
            r2 <= 1'b0;
        else if (set2)
            r2 <= 1'b1;
        else
            r2 <= d2;

    assign q1   =   r1 | (set1 & reset1);
    assign q1_n = ~ r1 | (set1 & reset1);
    assign q2   =   r2 | (set2 & reset2);
    assign q2_n = ~ r2 | (set2 & reset2);

endmodule
cd40105.svfile
// CD74HC40105: FIFO of sixteen 4-bit words.
//
// Ports in pin order, named as in the datasheet.  The power pins, GND (8)
// and VCC (16), are left out.
//
// A rising edge on SI (shift in) puts D0-D3 at the end of the queue while
// DIR (data-in ready) is 1; with all sixteen places full DIR is 0 and SI is
// ignored.  While DOR (data-out ready) is 1, Q0-Q3 show the word at the
// head of the queue; a falling edge on SO (shift out) drops it and the next
// word moves up.  With the queue empty DOR is 0 and SO is ignored.  MR
// (master reset) is active high and asynchronous: it empties the queue at
// once.  OE (three-state control) 1 turns Q0-Q3 off.
//
// The chip passes a word from place to place inside, so a word takes up to
// about half a microsecond at 5 V to reach the output, and DIR drops for a
// moment after every load.  The model leaves these delays out: a word is at
// the output right after the edge that loads it.  It also leaves out two
// details: if SI rises while the queue is full, the chip loads the word as
// soon as a place frees, if SI is still 1; and if SI is 1 at the end of MR,
// the chip loads D0-D3.  The model ignores both.
//
// Synthesizable, with SI and SO as two clocks.

module cd40105
(
    input  logic oe,   // Pin 1:  1 = Q0-Q3 off
    output logic dir,  // Pin 2:  1 = room for a word
    input  logic si,   // Pin 3:  rising edge = load
    input  logic d0,   // Pin 4
    input  logic d1,   // Pin 5
    input  logic d2,   // Pin 6
    input  logic d3,   // Pin 7
                       // Pin 8:  GND
    input  logic mr,   // Pin 9:  1 = empty the queue at once
    output logic q3,   // Pin 10
    output logic q2,   // Pin 11
    output logic q1,   // Pin 12
    output logic q0,   // Pin 13
    output logic dor,  // Pin 14: 1 = a word on Q0-Q3
    input  logic so    // Pin 15: falling edge = drop the head
                       // Pin 16: VCC
);

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

    // Write and read counters with one extra bit: equal when the queue is
    // empty, 16 apart when it is full

    logic [4:0] wr, rd;

    always_ff @ (posedge si or posedge mr)
        if (mr)
            wr <= '0;
        else if (dir)
            wr <= wr + 5'd1;

    always_ff @ (posedge si)
        if (dir & ~ mr)
            word [wr [3:0]] <= { d3, d2, d1, d0 };

    always_ff @ (negedge so or posedge mr)
        if (mr)
            rd <= '0;
        else if (dor)
            rd <= rd + 5'd1;

    assign dor = wr != rd;
    assign dir = wr != { ~ rd [4], rd [3:0] };

    assign { q3, q2, q1, q0 } = oe ? 4'bz : word [rd [3:0]];

endmodule
cd4008.svfile
// CD4008B: 4-bit full adder with a fast carry output.
//
// Ports in pin order, named as in the datasheet: A1-A4 and B1-B4 are the
// two numbers (A1 and B1 the lowest bits), S1-S4 the sum.  The power pins,
// VSS (8) and VDD (16), are left out.  Synthesizable.
//
// Inside are four full adders.  The chip computes CARRY OUT with extra
// look-ahead logic, so it does not wait for the carry to ripple through all
// four stages; the result is the same.

module cd4008
(
    input  logic a4,         // Pin 1
    input  logic b3,         // Pin 2
    input  logic a3,         // Pin 3
    input  logic b2,         // Pin 4
    input  logic a2,         // Pin 5
    input  logic b1,         // Pin 6
    input  logic a1,         // Pin 7
                             // Pin 8:  VSS
    input  logic carry_in,   // Pin 9
    output logic s1,         // Pin 10
    output logic s2,         // Pin 11
    output logic s3,         // Pin 12
    output logic s4,         // Pin 13
    output logic carry_out,  // Pin 14
    input  logic b4          // Pin 15
                             // Pin 16: VDD
);

    assign { carry_out, s4, s3, s2, s1 } =   { 1'b0, a4, a3, a2, a1 }
                                           + { 1'b0, b4, b3, b2, b1 }
                                           + { 4'b0, carry_in };

endmodule
cd4082.svfile
// CD4082B: dual 4-input AND gate.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (7)
// and VDD (14), are left out, and so are the unconnected pins 6 and 8.
// Synthesizable.

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

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

endmodule

10SystemVerilog: the chip-level design

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

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

module fifo_adder_chips
(
    input  logic       clk,
    input  logic       rst,
    input  logic       push_a,
    input  logic       push_b,
    input  logic       pop_c,
    input  logic [2:0] data,
    output logic [2:0] head_a,
    output logic       dor_a,
    output logic [2:0] head_b,
    output logic       dor_b,
    output logic [3:0] head_c,
    output logic       dor_c,
    output logic       dir_c,
    output logic       transfer
);

    // Connections between the chips that are not ports

    wire pC;
    wire sA;
    wire sB;
    wire go;
    wire tb;
    wire s0;
    wire s1;
    wire s2;
    wire s3;

    // 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_q1;
    wire unused_u1_q2_n;
    wire unused_fa_dir;
    wire unused_fa_q3;
    wire unused_fb_dir;
    wire unused_fb_q3;
    wire unused_u2_q1_n;
    wire unused_add_carry_out;
    wire unused_and_k;

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

    // U1: CD4013 dual D flip-flop with set and reset, pin 1 in column 5 of board 1

    cd4013 i_u1
    (
        .q1     ( unused_u1_q1   ),  // Pin 1, unused output
        .q1_n   ( pC             ),  // Pin 2 -> SO C
        .clock1 ( clk            ),  // Pin 3 <- 555 clock module
        .reset1 ( rst            ),  // Pin 4 <- button RESET
        .d1     ( pop_c          ),  // Pin 5 <- button POP C
        .set1   ( gnd            ),  // Pin 6 <- GND
        .set2   ( gnd            ),  // Pin 8 <- GND
        .d2     ( push_a         ),  // Pin 9 <- button PUSH A
        .reset2 ( rst            ),  // Pin 10 <- button RESET
        .clock2 ( clk            ),  // Pin 11 <- 555 clock module
        .q2_n   ( unused_u1_q2_n ),  // Pin 12, unused output
        .q2     ( sA             )   // Pin 13 -> SI A
    );

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

    // FA: CD74HC40105 FIFO of sixteen 4-bit words, pin 1 in column 13 of board 1

    cd40105 i_fa
    (
        .oe  ( gnd           ),  // Pin 1 <- GND
        .dir ( unused_fa_dir ),  // Pin 2, unused output
        .si  ( sA            ),  // Pin 3 <- U1 pin 13 (Q2)
        .d0  ( data [0]      ),  // Pin 4 <- button D0
        .d1  ( data [1]      ),  // Pin 5 <- button D1
        .d2  ( data [2]      ),  // Pin 6 <- button D2
        .d3  ( gnd           ),  // Pin 7 <- GND
        .mr  ( rst           ),  // Pin 9 <- button RESET
        .q3  ( unused_fa_q3  ),  // Pin 10, unused output
        .q2  ( head_a [2]    ),  // Pin 11 -> A2
        .q1  ( head_a [1]    ),  // Pin 12 -> A1
        .q0  ( head_a [0]    ),  // Pin 13 -> A0
        .dor ( dor_a         ),  // Pin 14 -> DOR A
        .so  ( transfer      )   // Pin 15 <- U2 pin 13 (Q2)
    );

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

    // FB: CD74HC40105 FIFO of sixteen 4-bit words, pin 1 in column 24 of board 1

    cd40105 i_fb
    (
        .oe  ( gnd           ),  // Pin 1 <- GND
        .dir ( unused_fb_dir ),  // Pin 2, unused output
        .si  ( sB            ),  // Pin 3 <- U2 pin 1 (Q1)
        .d0  ( data [0]      ),  // Pin 4 <- button D0
        .d1  ( data [1]      ),  // Pin 5 <- button D1
        .d2  ( data [2]      ),  // Pin 6 <- button D2
        .d3  ( gnd           ),  // Pin 7 <- GND
        .mr  ( rst           ),  // Pin 9 <- button RESET
        .q3  ( unused_fb_q3  ),  // Pin 10, unused output
        .q2  ( head_b [2]    ),  // Pin 11 -> B2
        .q1  ( head_b [1]    ),  // Pin 12 -> B1
        .q0  ( head_b [0]    ),  // Pin 13 -> B0
        .dor ( dor_b         ),  // Pin 14 -> DOR B
        .so  ( transfer      )   // Pin 15 <- U2 pin 13 (Q2)
    );

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

    // U2: CD4013 dual D flip-flop with set and reset, pin 1 in column 35 of board 1

    cd4013 i_u2
    (
        .q1     ( sB             ),  // Pin 1 -> SI B
        .q1_n   ( unused_u2_q1_n ),  // Pin 2, unused output
        .clock1 ( clk            ),  // Pin 3 <- 555 clock module
        .reset1 ( rst            ),  // Pin 4 <- button RESET
        .d1     ( push_b         ),  // Pin 5 <- button PUSH B
        .set1   ( gnd            ),  // Pin 6 <- GND
        .set2   ( gnd            ),  // Pin 8 <- GND
        .d2     ( go             ),  // Pin 9 <- AND pin 1 (J)
        .reset2 ( rst            ),  // Pin 10 <- button RESET
        .clock2 ( clk            ),  // Pin 11 <- 555 clock module
        .q2_n   ( tb             ),  // Pin 12 -> ~T
        .q2     ( transfer       )   // Pin 13 -> T
    );

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

    // ADD: CD4008 4-bit full adder, pin 1 in column 7 of board 2

    cd4008 i_add
    (
        .a4        ( gnd                  ),  // Pin 1 <- GND
        .b3        ( head_b [2]           ),  // Pin 2 <- FB pin 11 (Q2)
        .a3        ( head_a [2]           ),  // Pin 3 <- FA pin 11 (Q2)
        .b2        ( head_b [1]           ),  // Pin 4 <- FB pin 12 (Q1)
        .a2        ( head_a [1]           ),  // Pin 5 <- FA pin 12 (Q1)
        .b1        ( head_b [0]           ),  // Pin 6 <- FB pin 13 (Q0)
        .a1        ( head_a [0]           ),  // Pin 7 <- FA pin 13 (Q0)
        .carry_in  ( gnd                  ),  // Pin 9 <- GND
        .s1        ( s0                   ),  // Pin 10 -> S0
        .s2        ( s1                   ),  // Pin 11 -> S1
        .s3        ( s2                   ),  // Pin 12 -> S2
        .s4        ( s3                   ),  // Pin 13 -> S3
        .carry_out ( unused_add_carry_out ),  // Pin 14, unused output
        .b4        ( gnd                  )   // Pin 15 <- GND
    );

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

    // FC: CD74HC40105 FIFO of sixteen 4-bit words, pin 1 in column 19 of board 2

    cd40105 i_fc
    (
        .oe  ( gnd        ),  // Pin 1 <- GND
        .dir ( dir_c      ),  // Pin 2 -> DIR C
        .si  ( transfer   ),  // Pin 3 <- U2 pin 13 (Q2)
        .d0  ( s0         ),  // Pin 4 <- ADD pin 10 (S1)
        .d1  ( s1         ),  // Pin 5 <- ADD pin 11 (S2)
        .d2  ( s2         ),  // Pin 6 <- ADD pin 12 (S3)
        .d3  ( s3         ),  // Pin 7 <- ADD pin 13 (S4)
        .mr  ( rst        ),  // Pin 9 <- button RESET
        .q3  ( head_c [3] ),  // Pin 10 -> C3
        .q2  ( head_c [2] ),  // Pin 11 -> C2
        .q1  ( head_c [1] ),  // Pin 12 -> C1
        .q0  ( head_c [0] ),  // Pin 13 -> C0
        .dor ( dor_c      ),  // Pin 14 -> DOR C
        .so  ( pC         )   // Pin 15 <- U1 pin 2 (~Q1)
    );

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

    // AND: CD4082 dual 4-input AND, pin 1 in column 30 of board 2

    cd4082 i_and
    (
        .j ( go           ),  // Pin 1 -> GO
        .a ( dor_b        ),  // Pin 2 <- FB pin 14 (DOR)
        .b ( dir_c        ),  // Pin 3 <- FC pin 2 (DIR)
        .c ( dor_a        ),  // Pin 4 <- FA pin 14 (DOR)
        .d ( tb           ),  // Pin 5 <- U2 pin 12 (~Q2)
        .e ( vdd          ),  // Pin 9 <- +5V, unused input
        .f ( vdd          ),  // Pin 10 <- +5V, unused input
        .g ( vdd          ),  // Pin 11 <- +5V, unused input
        .h ( vdd          ),  // Pin 12 <- +5V, unused input
        .k ( unused_and_k )   // Pin 13, unused output
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side and compares them with a reference model of three queues and the transfer rule. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It adds one pair, then several pairs pushed out of step, fills C until A and B have to wait, pops C so that they move again, makes 200 clocks of pseudo-random presses, resets in the middle and checks that reset acts at once. It changes the data only while both PUSH buttons are released. The output below is shortened.

tb.sv352 lines · show the codefile
// Testbench for exercise P3.
//
// Runs the generic design (fifo_adder.sv) and the chip-level design
// (fifo_adder_chips.sv) side by side and compares both with a reference
// model: three queues kept as arrays with the head in place 0, and the
// transfer rule written out.  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 adds one pair, then several
// pairs pushed in a different order, fills C until A and B have to wait,
// pops C so that they move again, makes pseudo-random presses, resets in
// the middle, and checks that reset acts at once.

// 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       rst;
    logic       push_a = 1'b0;
    logic       push_b = 1'b0;
    logic       pop_c  = 1'b0;
    logic [2:0] data   = '0;

    wire  [2:0] head_a,   head_a_chips;
    wire        dor_a,    dor_a_chips;
    wire  [2:0] head_b,   head_b_chips;
    wire        dor_b,    dor_b_chips;
    wire  [3:0] head_c,   head_c_chips;
    wire        dor_c,    dor_c_chips;
    wire        dir_c,    dir_c_chips;
    wire        transfer, transfer_chips;

    fifo_adder i_generic
    (
        .clk      ( clk      ),
        .rst      ( rst      ),
        .push_a   ( push_a   ),
        .push_b   ( push_b   ),
        .pop_c    ( pop_c    ),
        .data     ( data     ),
        .head_a   ( head_a   ),
        .dor_a    ( dor_a    ),
        .head_b   ( head_b   ),
        .dor_b    ( dor_b    ),
        .head_c   ( head_c   ),
        .dor_c    ( dor_c    ),
        .dir_c    ( dir_c    ),
        .transfer ( transfer )
    );

    fifo_adder_chips i_chips
    (
        .clk      ( clk            ),
        .rst      ( rst            ),
        .push_a   ( push_a         ),
        .push_b   ( push_b         ),
        .pop_c    ( pop_c          ),
        .data     ( data           ),
        .head_a   ( head_a_chips   ),
        .dor_a    ( dor_a_chips    ),
        .head_b   ( head_b_chips   ),
        .dor_b    ( dor_b_chips    ),
        .head_c   ( head_c_chips   ),
        .dor_c    ( dor_c_chips    ),
        .dir_c    ( dir_c_chips    ),
        .transfer ( transfer_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

    //------------------------------------------------------------------------
    // Reference model: three queues with the head in place 0, the buttons at
    // the last edge, and the transfer pulse

    logic [2:0] queue_a [0:15], queue_b [0:15];
    logic [3:0] queue_c [0:15];
    int         count_a, count_b, count_c;
    logic       push_a_was, push_b_was, pop_c_was, t;

    wire put_a  = push_a & ~ push_a_was & count_a < 16;
    wire put_b  = push_b & ~ push_b_was & count_b < 16;
    wire go     = count_a > 0 && count_b > 0 && count_c < 16 && ! t;
    wire take_c = pop_c & ~ pop_c_was & count_c > 0;

    always @ (posedge clk or posedge rst)
        if (rst)
        begin
            count_a    <= 0;
            count_b    <= 0;
            count_c    <= 0;
            push_a_was <= 1'b0;
            push_b_was <= 1'b0;
            pop_c_was  <= 1'b0;
            t          <= 1'b0;
        end
        else
        begin
            push_a_was <= push_a;
            push_b_was <= push_b;
            pop_c_was  <= pop_c;
            t          <= go;

            // A and B drop the pair at the falling edge of t, one clock
            // after C took the sum

            if (t)
                for (int k = 0; k < 15; k ++)
                begin
                    queue_a [k] <= queue_a [k + 1];
                    queue_b [k] <= queue_b [k + 1];
                end

            if (take_c)
                for (int k = 0; k < 15; k ++)
                    queue_c [k] <= queue_c [k + 1];

            // After the shifts above, so that these writes win

            if (put_a)
                queue_a [t ? count_a - 1 : count_a] <= data;

            if (put_b)
                queue_b [t ? count_b - 1 : count_b] <= data;

            if (go)
                queue_c [take_c ? count_c - 1 : count_c] <= 4' (queue_a [0]) + 4' (queue_b [0]);

            count_a <= count_a + (put_a ? 1 : 0) - (t ? 1 : 0);
            count_b <= count_b + (put_b ? 1 : 0) - (t ? 1 : 0);
            count_c <= count_c + (go    ? 1 : 0) - (take_c ? 1 : 0);
        end

    // Heads are checked only while their FIFO has data

    function automatic logic ok (input logic [2:0] ha, input logic ra, input logic [2:0] hb, input logic rb,
                                 input logic [3:0] hc, input logic vc, input logic fc, input logic tr);
        return ra === (count_a > 0) && rb === (count_b > 0) && vc === (count_c > 0) && fc === (count_c < 16)
            && tr === t && (count_a == 0 || ha === queue_a [0]) && (count_b == 0 || hb === queue_b [0])
            && (count_c == 0 || hc === queue_c [0]);
    endfunction

    wire ok_generic = ok (head_a, dor_a, head_b, dor_b, head_c, dor_c, dir_c, transfer);
    wire ok_chips   = ok (head_a_chips, dor_a_chips, head_b_chips, dor_b_chips, head_c_chips, dor_c_chips,
                          dir_c_chips, transfer_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.

    // Initial values are fine in a testbench, and the checker samples rst at
    // the clock edge on purpose, although the designs reset asynchronously

    // verilator lint_off PROCASSINIT
    // verilator lint_off SYNCASYNCNET

    logic was_reset = 1'b0;
    logic done      = 1'b0;  // Set by the stimulus before $finish

    int cycle       = 0;
    int n_checks    = 0;
    int n_errors    = 0;
    int n_transfers = 0;
    int n_stalled   = 0;  // Clock edges at which A and B had data but C was full

    always @ (posedge clk)
    begin
        if (rst)
            was_reset <= 1'b1;

        if ((rst | was_reset) & ~ done)
        begin
            cycle       <= cycle + 1;
            n_checks    <= n_checks + 2;
            n_errors    <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1);
            n_transfers <= n_transfers + (~ rst && go ? 1 : 0);
            n_stalled   <= n_stalled + (~ rst && count_a > 0 && count_b > 0 && count_c == 16 ? 1 : 0);

            $display ("%5d  %b   %b %b %b  %0d  | %b %0d  %b %0d | %b | %b %b %2d %2d  %s  %s",
                cycle + 1, rst, push_a, push_b, pop_c, data, dor_a, head_a, dor_b, head_b, transfer,
                dor_c, dir_c, head_c, count_c, ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end
    end

    //------------------------------------------------------------------------
    // Asynchronous reset: right after rst rises, all FIFOs must be empty.

    int n_async = 0;

    always @ (posedge rst)
    begin
        # 1;

        if ({ dor_a, dor_b, dor_c, dir_c, transfer, dor_a_chips, dor_b_chips, dor_c_chips, dir_c_chips,
              transfer_chips } !== 10'b00010_00010)
        begin
            $display ("WRONG: reset did not act at once");
            n_errors <= n_errors + 1;
        end

        n_async <= n_async + 1;
    end

    //------------------------------------------------------------------------
    // Driving the inputs.  As on the breadboard, the data bits change only
    // while both PUSH buttons are released: a FIFO reads them at the very
    // edge that loads them.

    // One press of a button: hold it through one edge, release it for one

    task automatic press (input logic a_on, input logic b_on, input logic c_on, input logic [2:0] value);
        data   <= value;
        push_a <= a_on;
        push_b <= b_on;
        pop_c  <= c_on;
        @ (posedge clk);
        push_a <= 1'b0;
        push_b <= 1'b0;
        pop_c  <= 1'b0;
        @ (posedge clk);
    endtask

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

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

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

        $display ("cycle  rst PA PB PC  D  | A     B    | T | C          generic  chips");

        // Before reset the FIFOs are unknown, as after power-up

        rst <= 1'bx;
        repeat (2) @ (posedge clk);
        rst <= 1'b1;
        repeat (2) @ (posedge clk);
        rst <= 1'b0;

        // The example of section 2: 3 into A, 2 into B, the sum 5 in C

        press (1, 0, 0, 3);
        press (0, 1, 0, 2);
        repeat (3) @ (posedge clk);
        press (0, 0, 1, 0);

        // Five numbers into A, then three into B: three pairs move

        for (int i = 1; i <= 5; i ++)
            press (1, 0, 0, 3' (i));

        for (int i = 4; i <= 6; i ++)
            press (0, 1, 0, 3' (i));

        repeat (4) @ (posedge clk);

        // B catches up with a push into both at once

        press (1, 1, 0, 7);
        press (0, 1, 0, 1);
        press (0, 1, 0, 2);
        repeat (6) @ (posedge clk);

        // Fill C without popping until A and B have to wait

        for (int i = 0; i < 18; i ++)
            press (1, 1, 0, 3' (i));

        repeat (6) @ (posedge clk);

        // Pop C: every pop makes room for one more pair

        for (int i = 0; i < 24; i ++)
            press (0, 0, 1, 0);

        // Pseudo-random presses, the same in every simulator.  The data
        // change only while both PUSH buttons are released.

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

            if (! push_a && ! push_b)
                data <= seed [26:24];

            push_a <= seed [31] & seed [30];
            push_b <= seed [29] & seed [28];
            pop_c  <= seed [27] & (seed [23] | seed [22]);

            @ (posedge clk);
        end

        push_a <= 1'b0;
        push_b <= 1'b0;
        pop_c  <= 1'b0;
        @ (posedge clk);

        // Reset in the middle, with words in the FIFOs

        press (1, 1, 0, 5);
        press (1, 0, 0, 6);
        rst <= 1'b1;
        @ (posedge clk);
        rst <= 1'b0;
        @ (posedge clk);

        press (1, 1, 0, 4);
        repeat (4) @ (posedge clk);

        // 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 && n_transfers >= 30 && n_stalled > 0 && n_async >= 2)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d transfers, %0d stalled edges, %0d resets",
                      n_errors, n_checks, n_transfers, n_stalled, n_async);
    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 P3: 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 fifo_adder.sv fifo_adder_chips.sv cd4013.sv cd40105.sv cd4008.sv cd4082.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 P3: 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 fifo_adder.sv fifo_adder_chips.sv cd4013.sv cd40105.sv cd4008.sv cd4082.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 P3: 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 fifo_adder.sv fifo_adder_chips.sv cd4013.sv cd40105.sv cd4008.sv cd4082.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  rst PA PB PC  D  | A     B    | T | C          generic  chips
    1  1   0 0 0  0  | 0 x  0 x | 0 | 0 1  x  0  ok          ok
    2  1   0 0 0  0  | 0 x  0 x | 0 | 0 1  x  0  ok          ok
    3  0   1 0 0  3  | 0 x  0 x | 0 | 0 1  x  0  ok          ok
    4  0   0 0 0  3  | 1 3  0 x | 0 | 0 1  x  0  ok          ok
    5  0   0 1 0  2  | 1 3  0 x | 0 | 0 1  x  0  ok          ok
    6  0   0 0 0  2  | 1 3  1 2 | 0 | 0 1  x  0  ok          ok
    7  0   0 0 0  2  | 1 3  1 2 | 1 | 1 1  5  1  ok          ok
    8  0   0 0 0  2  | 0 x  0 x | 0 | 1 1  5  1  ok          ok
    9  0   0 0 0  2  | 0 x  0 x | 0 | 1 1  5  1  ok          ok
   10  0   0 0 1  0  | 0 x  0 x | 0 | 1 1  5  1  ok          ok
   11  0   0 0 0  0  | 0 x  0 x | 0 | 0 1  x  0  ok          ok
   12  0   1 0 0  1  | 0 x  0 x | 0 | 0 1  x  0  ok          ok
   13  0   0 0 0  1  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   14  0   1 0 0  2  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   15  0   0 0 0  2  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   16  0   1 0 0  3  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   17  0   0 0 0  3  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   18  0   1 0 0  4  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   19  0   0 0 0  4  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   20  0   1 0 0  5  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   21  0   0 0 0  5  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   22  0   0 1 0  4  | 1 1  0 x | 0 | 0 1  x  0  ok          ok
   23  0   0 0 0  4  | 1 1  1 4 | 0 | 0 1  x  0  ok          ok
   24  0   0 1 0  5  | 1 1  1 4 | 1 | 1 1  5  1  ok          ok
   25  0   0 0 0  5  | 1 2  1 5 | 0 | 1 1  5  1  ok          ok
   26  0   0 1 0  6  | 1 2  1 5 | 1 | 1 1  5  2  ok          ok
   27  0   0 0 0  6  | 1 3  1 6 | 0 | 1 1  5  2  ok          ok
   28  0   0 0 0  6  | 1 3  1 6 | 1 | 1 1  5  3  ok          ok
  …
  345  0   0 0 0  4  | 0 6  0 4 | 0 | 1 1  8  1  ok          ok
  346  0   0 0 0  4  | 0 6  0 4 | 0 | 1 1  8  1  ok          ok
tb.sv:338: $finish called at 3485000 (1ps)
PASS: 692 checks, no errors

To see that the testbench catches mistakes, break something and run it again: delete & ~ transfer from go in fifo_adder.sv, or connect the input of the 4082 that gets NOT T to +5V in fifo_adder_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 fifo_adder.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 2–0 are the data, key 0 is PUSH A, key 1 PUSH B and key 2 POP C. LEDs 3–0 show the head of C, LED 4 DOR C, LED 5 DIR C, LED 6 DOR A and LED 7 DOR B. The rightmost digit shows the head of C in hexadecimal. To run the chip-level design instead, add fifo_adder_chips.sv and the four chip models and change the module name in the instance.

bgm_lab/lab_top.sv138 lines · show the codefile
// Exercise P3 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 fifo_adder.sv.  For the chip-level
// design add fifo_adder_chips.sv and cd4013.sv, cd40105.sv, cd4008.sv,
// cd4082.sv instead, and change the module name below.
//
// The adder runs from slow_clk, 1 Hz on every BGM board, and rst is the
// reset of the board.  Switches 2-0 are the data, key 0 is PUSH A, key 1
// PUSH B and key 2 POP C: hold a key until the next tick of slow_clk.
// LEDs 3-0 show the head of C, LED 4 DOR C, LED 5 DIR C, LED 6 DOR A and
// LED 7 DOR B.  The rightmost digit shows the head of C in hexadecimal.

`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 red        = '0;
       assign green      = '0;
       assign blue       = '0;
       assign sound      = '0;
       assign uart_tx    = '1;

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

    logic [2:0] head_a, head_b;
    logic [3:0] head_c;
    logic       dor_a, dor_b, dor_c, dir_c, transfer;

    fifo_adder i_fifo_adder
    (
        .clk      ( slow_clk ),
        .rst      ( rst      ),
        .push_a   ( key [0]  ),
        .push_b   ( key [1]  ),
        .pop_c    ( key [2]  ),
        .data     ( sw [2:0] ),
        .head_a   ( head_a   ),
        .dor_a    ( dor_a    ),
        .head_b   ( head_b   ),
        .dor_b    ( dor_b    ),
        .head_c   ( head_c   ),
        .dor_c    ( dor_c    ),
        .dir_c    ( dir_c    ),
        .transfer ( transfer )
    );

    assign led = w_led' ({ dor_b, dor_a, dir_c, dor_c, head_c });

    // The head of C as a hexadecimal digit, dark while C is empty

    logic [6:0] abcdefg;

    always_comb
        if (~ dor_c)
            abcdefg = 7'b000_0000;
        else
            case (head_c)
            4'h0: abcdefg = 7'b111_1110;
            4'h1: abcdefg = 7'b011_0000;
            4'h2: abcdefg = 7'b110_1101;
            4'h3: abcdefg = 7'b111_1001;
            4'h4: abcdefg = 7'b011_0011;
            4'h5: abcdefg = 7'b101_1011;
            4'h6: abcdefg = 7'b101_1111;
            4'h7: abcdefg = 7'b111_0000;
            4'h8: abcdefg = 7'b111_1111;
            4'h9: abcdefg = 7'b111_1011;
            4'ha: abcdefg = 7'b111_0111;
            4'hb: abcdefg = 7'b001_1111;
            4'hc: abcdefg = 7'b100_1110;
            4'hd: abcdefg = 7'b011_1101;
            4'he: abcdefg = 7'b100_1111;
            4'hf: abcdefg = 7'b100_0111;
            endcase

    assign abcdefgh = { abcdefg, 1'b0 };
    assign digit    = w_digit' (1);

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

Keys 2–0 are the data, key 5 is PUSH A, key 6 PUSH B and key 7 POP C. The screen shows three columns of lamps for A, B and C: the head of each FIFO, with DOR below it, DIR under C, and the transfer lamp T between B and C.

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

// Exercise P3 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 fifo_adder.sv.
//
// The adder runs from slow_clock.  Keys 2-0 are the data, key 5 is PUSH A,
// key 6 PUSH B and key 7 POP C: hold the data keys, then hold a PUSH key
// until the next tick of slow_clock.  The LEDs and the rightmost digit are
// as in lab_top.  The screen shows three columns of lamps, A, B and C: the
// head of each FIFO, bit 3 on top, and below it whether it has data; under
// C also whether it has room, and the transfer lamp T between B and C.

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 [2:0] head_a, head_b;
    logic [3:0] head_c;
    logic       dor_a, dor_b, dor_c, dir_c, transfer;

    fifo_adder i_fifo_adder
    (
        .clk      ( slow_clock ),
        .rst      ( reset      ),
        .push_a   ( key [5]    ),
        .push_b   ( key [6]    ),
        .pop_c    ( key [7]    ),
        .data     ( key [2:0]  ),
        .head_a   ( head_a     ),
        .dor_a    ( dor_a      ),
        .head_b   ( head_b     ),
        .dor_b    ( dor_b      ),
        .head_c   ( head_c     ),
        .dor_c    ( dor_c      ),
        .dir_c    ( dir_c      ),
        .transfer ( transfer   )
    );

    assign led = { dor_b, dor_a, dir_c, dor_c, head_c };

    // The head of C as a hexadecimal digit, dark while C is empty

    logic [6:0] abcdefg;

    always_comb
        if (~ dor_c)
            abcdefg = 7'b000_0000;
        else
            case (head_c)
            4'h0: abcdefg = 7'b111_1110;
            4'h1: abcdefg = 7'b011_0000;
            4'h2: abcdefg = 7'b110_1101;
            4'h3: abcdefg = 7'b111_1001;
            4'h4: abcdefg = 7'b011_0011;
            4'h5: abcdefg = 7'b101_1011;
            4'h6: abcdefg = 7'b101_1111;
            4'h7: abcdefg = 7'b111_0000;
            4'h8: abcdefg = 7'b111_1111;
            4'h9: abcdefg = 7'b111_1011;
            4'ha: abcdefg = 7'b111_0111;
            4'hb: abcdefg = 7'b001_1111;
            4'hc: abcdefg = 7'b100_1110;
            4'hd: abcdefg = 7'b011_1101;
            4'he: abcdefg = 7'b100_1111;
            4'hf: abcdefg = 7'b100_0111;
            endcase

    assign abcdefgh = { abcdefg, 1'b0 };
    assign digit    = 8'b0000_0001;

    //------------------------------------------------------------------------
    // Columns at x = 80 (A), 200 (B) and 340 (C), 40 pixels wide.  Rows of
    // 30 pixels: the head bits 3-0 from y = 30, then DOR, then DIR for C.
    // Data in blue, flags in green, T in orange at x = 270.

    wire [3:0] col_a = { 1'b0, head_a & { 3 { dor_a } } };
    wire [3:0] col_b = { 1'b0, head_b & { 3 { dor_b } } };
    wire [3:0] col_c = head_c & { 4 { dor_c } };

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

        for (int i = 0; i < 4; i ++)
            if (y >= 9' (30 + i * 36) & y < 9' (60 + i * 36))
            begin
                if (x >= 80 & x < 120)
                    blue = col_a [3 - i] ? 31 : 5;

                if (x >= 200 & x < 240)
                    blue = col_b [3 - i] ? 31 : 5;

                if (x >= 340 & x < 380)
                    blue = col_c [3 - i] ? 31 : 5;
            end

        if (y >= 180 & y < 210)
        begin
            if (x >= 80 & x < 120)
                green = dor_a ? 63 : 10;

            if (x >= 200 & x < 240)
                green = dor_b ? 63 : 10;

            if (x >= 340 & x < 380)
                green = dor_c ? 63 : 10;
        end

        if (y >= 216 & y < 246 & x >= 340 & x < 380)
            green = dir_c ? 63 : 10;

        if (y >= 102 & y < 132 & x >= 270 & x < 310)
        begin
            red   = transfer ? 31 : 6;
            green = transfer ? 30 : 6;
        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 FIFO adder, just as the 555 module clocks the chips on the breadboard.

DIP switches 0–2 are the data, switch 3 is PUSH A, switch 4 PUSH B and switch 5 POP C. The display shows the head of C in hexadecimal, and its dot lights while C is full. The flags and the head of A go to the BIDIR Pmod.

tiny_tapeout/project.v90 lines · show the codefile
/*
 * Exercise P3 on Tiny Tapeout: the FIFO adder.  Numbers pushed into FIFO A
 * and FIFO B are added in pairs into FIFO C.  The 7-segment display of the
 * demo board shows the head of C in hexadecimal.
 *
 * Add fifo_adder.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-2 are the data, switch 3 is PUSH A, switch 4 PUSH B and switch 5
    // POP C: turn the switch on, wait for a tick of the clock, and turn it
    // off again.  The reset button of the board empties the FIFOs.

    wire [2:0] head_a, head_b;
    wire [3:0] head_c;
    wire       dor_a, dor_b, dor_c, dir_c, transfer;

    fifo_adder i_fifo_adder (
        .clk      ( clk         ),
        .rst      ( ~ rst_n     ),
        .push_a   ( ui_in [3]   ),
        .push_b   ( ui_in [4]   ),
        .pop_c    ( ui_in [5]   ),
        .data     ( ui_in [2:0] ),
        .head_a   ( head_a      ),
        .dor_a    ( dor_a       ),
        .head_b   ( head_b      ),
        .dor_b    ( dor_b       ),
        .head_c   ( head_c      ),
        .dor_c    ( dor_c       ),
        .dir_c    ( dir_c       ),
        .transfer ( transfer    )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  The head of C as a hexadecimal digit, dark
    // while C is empty; the dot lights while C is full.

    reg [6:0] gfedcba;

    always @(*)
        if (~ dor_c)
            gfedcba = 7'b0000000;
        else
            case (head_c)
            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  = { ~ dir_c, gfedcba };

    // The flags and the head of A on the BIDIR Pmod

    assign uio_out = { head_a, transfer, dor_c, dor_b, dor_a, 1'b0 };
    assign uio_oe  = 8'hfe;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in [7:6], uio_in, head_b, 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>_fifo_adder
  source_files:
    - "project.v"
    - "fifo_adder.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 Projects list of “An improved way of teaching school kids digital electronics” and in lesson 1.5 of the Habr article about the electronics course, which proposes three 40105 FIFOs and a 4008 to add operands that arrive in different clock cycles, with backpressure, and notes that the circuit had not been tried yet. The netlist here is the one of the FIFO build guide of the LSSK FIFO kit, checked there by a pin-level simulation with chip delays. The chips are described in the Texas Instruments datasheets for the CD74HC40105, CD4008B, CD4082B and CD4013B.