Breadboard exercises/ P1EnglishРусский

Exercise P1 · project · serial arithmetic

Bit-serial adder: a 4032 between two 4014s and a 4015

Two numbers become two streams of bits, an adder with a one-bit memory adds them one bit per clock, and the bits of the sum are collected back into a number. A project for a group of three, one breadboard each: the same split of work into blocks as in a real chip design team.

7 chips2 × 4014, 4032, 4015, 3 × 4511
100 + 30 wireson and between the boards + leads to the modules
3 boardsA, B, and the adder with the sum
~1 Hzone bit per clock

1What it is and why it matters

What it does. Buttons A3–A0 and B3–B0 give two numbers, shown at once on the displays A and B. Press LOAD through one blink of the clock module: the numbers go into two shift registers. At each of the next four blinks one bit of each number, lowest first, goes through the adder, and the bit of the sum goes into a third shift register. After the fourth blink the sum display shows A + B.

How it works. A 4014 is a serializer: LOAD copies A into it in parallel, A0 into the stage next to the output, and every clock moves the bits one stage on. The 4032 is a full adder with a flip-flop for the carry: its sum output is A XOR B XOR the stored carry, and at every rising edge it stores the new carry for the next bit. The 4015 is a deserializer: it takes the sum bits one by one. LOAD also clears the carry and the 4015.

Where it is used. Serial links such as USB, PCI Express and SATA send numbers one bit after another, and their chips have serializers and deserializers, SerDes, at both ends. Early computers and many calculators did all their arithmetic bit-serially, because one adder used again and again costs fewer parts than one adder per bit.

Why learn it.

2The key elements: one bit per clock

A = 5 = 0101 and B = 3 = 0011, after LOAD:

Clock edge after LOADA bitB bitCarry inSum bitCarry out4015 Q1A…Q4A
1110010000
2011010000
3101010000
4001101000 = 8
5000000100

After the fourth edge the 4015 holds 1000, the sum 8, and the display shows it. The fifth edge brings the fifth bit of the sum, here 0, into Q1A, and the later edges push the sum along: the display shows the right sum for one period of the clock. Slow the clock module down to read it, and press LOAD to add again. The 4511 shows digits 0 to 9 only, so choose A + B of at most 9.

Waveforms

CLKLOADA059B038A→B→S→401500001000010000001000010000100001
From the simulation of tb.sv in Icarus Verilog: 5 + 3, then 9 + 8. The streams A→ and B→ are the Q8 outputs of the 4014s, S→ the sum output of the 4032; the last row is the 4015. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD4511BCD-to-7-segment decoders for A, B and the sum3$0.65Elexp (elexp.com)
CD40148-stage parallel-in, serial-out registers: the serializers of A and B2$0.19Jameco (jameco.com)
CD4032triple serial adder: adder 1 is used1$0.31store.comet.bg
CD4015dual 4-stage shift register: register A is the deserializer of the sum1$0.35Elexp (elexp.com)
Button modulesA3-A0, B3-B0 and LOAD; the first pack of five5$0.66 / packaliexpress.us
Button modules4 more from the second pack4$2.03 / packaliexpress.us
555 clock modulethe clock, set to about 1 Hz1$0.53aliexpress.us
7-segment display, common cathodeA, B and the sum3$0.90elexp.com
Resistor, 560 Ω, ½ Wone per segment21$0.04elexp.com
Breadboard, 830 pointstwo boards, one above the other3$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 wires100 wires on the board (or male-male Dupont jumpers)100$11.95 / packjameco.com
Male-female Dupont jumpers30 leads to the clock module and the buttons30$1.30 / packaliexpress.us

Prices from the LSSK components sheet; these chips are in the Comprehensive kit.

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.

4014: 8-stage shift register, parallel in, serial out

1P82Q63Q84P45P36P27P18VSS9P/S10CLK11SER12Q713P514P615P716VDD40148-bit PISO register
CD4014B. · SVG · PNG

As in exercise BB2. While PAR/SER (pin 9) is 1, a rising edge of CLOCK (pin 10) loads P1–P8; while it is 0, the bits move one stage toward Q8 (pin 3). Here P8, P7, P6, P5 (pins 1, 15, 14, 13) get bits 0 to 3 of the number, P4–P1 and SERIAL IN are at GND, so zeros follow the four bits.

PinNameDirectionFunctionIn this circuit
1P8inputparallel input, stage 8A0
2Q6outputoutput of stage 6not connected
3Q8outputoutput of stage 8, the serial outputA→
4P4inputparallel input, stage 4GND
5P3inputparallel input, stage 3GND
6P2inputparallel input, stage 2GND
7P1inputparallel input, stage 1GND
8VSSpowerground, 0 VGND
9PAR/SERinput1 = load P1-P8 at the next clock edge, 0 = shift; works with the clock (synchronous)LOAD
10CLOCKinputeverything happens on the rising edgeCLK
11SERIAL INinputserial input: goes into stage 1 when shiftingGND
12Q7outputoutput of stage 7not connected
13P5inputparallel input, stage 5A3
14P6inputparallel input, stage 6A2
15P7inputparallel input, stage 7A1
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4032: triple serial adder

1S32INV33CLK4S25INV26CR7INV18VSS9S110A111B112B213A214B315A316VDD40323 × serial adder
CD4032B. · SVG · PNG

Three adders share CLOCK (pin 3) and CARRY RESET (pin 6). Adder 1 is used: A1 (pin 10) and B1 (pin 11) get the streams, SUM1 (pin 9) gives the sum bit. The carry is stored at the rising edge of the clock; CARRY RESET at 1 clears it, here at the LOAD edge. INVERT1 (pin 7) at GND keeps the sum as it is, and the inputs of the other two adders are at GND.

PinNameDirectionFunctionIn this circuit
1SUM3outputsum of adder 3not connected
2INVERT3input1 inverts the sum of adder 3tied to +5V
3CLOCKinputthe carries are stored at the rising edgeCLK
4SUM2outputsum of adder 2not connected
5INVERT2input1 inverts the sum of adder 2tied to +5V
6CARRY RESETinput1 at a rising edge clears the carries for the next wordLOAD
7INVERT1input1 inverts the sum of adder 1GND
8VSSpowerground, 0 VGND
9SUM1outputsum of adder 1: A1 XOR B1 XOR the stored carryS→
10A1inputadder 1, number A, one bit per clock, lowest firstA→
11B1inputadder 1, number B, one bit per clockB→
12B2inputadder 2, number Btied to GND
13A2inputadder 2, number Atied to GND
14B3inputadder 3, number Btied to GND
15A3inputadder 3, number Atied to GND
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4015: dual 4-stage shift register

1CLKB2Q4B3Q3A4Q2A5Q1A6RA7DA8VSS9CLKA10Q4A11Q3B12Q2B13Q1B14RB15DB16VDD40152 × 4-bit shift register
CD4015B. · SVG · PNG

As in exercise B6. Register A takes the sum bits on DATA A (pin 7) at every rising edge of CLOCK A (pin 9); RESET A (pin 6) is LOAD, which clears it at once. The newest bit is in Q1A, so after four edges Q1A, Q2A, Q3A, Q4A (pins 5, 4, 3, 10) are S3, S2, S1, S0 and go to D, C, B, A of the sum 4511.

PinNameDirectionFunctionIn this circuit
1CLOCK Binputregister B shifts on the rising edgetied to +5V
2Q4Boutputregister B, stage 4 (the oldest bit)not connected
3Q3Aoutputregister A, stage 3Q3A
4Q2Aoutputregister A, stage 2Q2A
5Q1Aoutputregister A, stage 1 (the newest bit)Q1A
6RESET Ainput1 clears register A at once, without the clock. Active highLOAD
7DATA Ainputregister A, serial inputS→
8VSSpowerground, 0 VGND
9CLOCK Ainputregister A shifts on the rising edgeCLK
10Q4Aoutputregister A, stage 4 (the oldest bit)Q4A
11Q3Boutputregister B, stage 3not connected
12Q2Boutputregister B, stage 2not connected
13Q1Boutputregister B, stage 1not connected
14RESET Binput1 clears register B at once. Active hightied to GND
15DATA Binputregister B, serial inputtied to GND
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

4511: BCD to 7-segment decoder

1B2C3LT4BI5LE6D7A8VSS9e10d11c12b13a14g15f16VDD4511BCD → 7 segments
CD4511B. · SVG · PNG

As in exercise B7, three times: for A, for B and for the sum, each with a common-cathode display and seven resistors.

PinNameDirectionFunctionIn this circuit
1BinputBCD input, bit 1 (weight 2)A1
2CinputBCD input, bit 2 (weight 4)A2
3LTinputlamp test: 0 lights all segments. Active low+5V
4BIinputblanking: 0 turns all segments off. Active low+5V
5LEinputlatch enable: 0 = follow the inputs, 1 = hold the codeGND
6DinputBCD input, bit 3 (weight 8)A3
7AinputBCD input, bit 0 (weight 1)A0
8VSSpowerground, 0 VGND
9eoutputsegment e (bottom left); 1 = lit; drives up to 25 mA through a resistorA.e
10doutputsegment d (bottom); 1 = lit; drives up to 25 mA through a resistorA.d
11coutputsegment c (bottom right); 1 = lit; drives up to 25 mA through a resistorA.c
12boutputsegment b (top right); 1 = lit; drives up to 25 mA through a resistorA.b
13aoutputsegment a (top); 1 = lit; drives up to 25 mA through a resistorA.a
14goutputsegment g (middle); 1 = lit; drives up to 25 mA through a resistorA.g
15foutputsegment f (top left); 1 = lit; drives up to 25 mA through a resistorA.f
16VDDpowersupply, +5 V (3 to 18 V allowed)+5V

5Schematic 1: serializers, adder, deserializer

CLKLOADA3A2A1A0B3B2B1B04014 AP5P6P7P8P/SCLKQ8serializer4014 BP5P6P7P8P/SCLKQ8serializer4032A1B1CRCLKS1serial adder4511 ADCBAa–g4511 BDCBAa–g4015 ADRCLKQ1Q2Q3Q4deserializer4511 SDCBAa–gAdisplayBdisplayA + Bdisplay
The circuit as blocks. This is what serial_adder.sv describes; the three 4511s and displays are drawn as the outputs. · SVG · PNG

6Schematic 2: connections between the chips

12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 A12345678910111213141516P8Q6Q8P4P3P2P1P/SCLKSERQ7P5P6P740148-bit PISO register4014 Ae1d2CC3c4DP5b6a7CC8f9g10A12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 B12345678910111213141516P8Q6Q8P4P3P2P1P/SCLKSERQ7P5P6P740148-bit PISO register4014 Be1d2CC3c4DP5b6a7CC8f9g10B12345678910111213141516S3INV3CLKS2INV2CRINV1S1A1B1B2A2B3A340323 × serial adder12345678910111213141516CLKBQ4BQ3AQ2AQ1ARADACLKAQ4AQ3BQ2BQ1BRBDB40152 × 4-bit shift register12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 Se1d2CC3c4DP5b6a7CC8f9g10A + BA.aA.bA.cA.dA.eA.fA.gB.aB.bB.cB.dB.eB.fB.gS.aS.bS.cS.dS.eS.fS.gCLKLOADA→B→S→A0A1A2A3B0B1B2B3Q1AQ2AQ3AQ4AA3A2A1A0B3B2B1B0LOADclock+5V
The same circuit as chips, pin by pin, one row per board. · SVG · PNG
NetFromToWire
CLK555 clock module, OUT4014 A 10 (CLK), 4014 B 10 (CLK), 4032 3 (CLK), 4015 9 (CLKA)M-F, M-M
LOADbutton LOAD, OUT4014 A 9 (P/S), 4014 B 9 (P/S), 4032 6 (CR), 4015 6 (RA)M-F, M-M
A3button A3, OUT4014 A 13 (P5), 4511 A 6 (D)M-F, M-M
A2button A2, OUT4014 A 14 (P6), 4511 A 2 (C)M-F, M-M
A1button A1, OUT4014 A 15 (P7), 4511 A 1 (B)M-F, M-M
A0button A0, OUT4014 A 1 (P8), 4511 A 7 (A)M-F, M-M
B3button B3, OUT4014 B 13 (P5), 4511 B 6 (D)M-F, M-M (loose jumper)
B2button B2, OUT4014 B 14 (P6), 4511 B 2 (C)M-F, M-M
B1button B1, OUT4014 B 15 (P7), 4511 B 1 (B)M-F, M-M
B0button B0, OUT4014 B 1 (P8), 4511 B 7 (A)M-F, M-M
A→4014 A 3 (Q8)4032 10 (A1)M-M
B→4014 B 3 (Q8)4032 11 (B1)M-M
S→4032 9 (S1)4015 7 (DA)M-M
Q1A4015 5 (Q1A)4511 S 6 (D)M-M
Q2A4015 4 (Q2A)4511 S 2 (C)M-M
Q3A4015 3 (Q3A)4511 S 1 (B)M-M
Q4A4015 10 (Q4A)4511 S 7 (A)M-M
A.a4511 A 13 (a)RaAM-M
A.b4511 A 12 (b)RbAM-M
A.c4511 A 11 (c)RcAM-M
A.d4511 A 10 (d)RdAM-M
A.e4511 A 9 (e)ReAM-M
A.f4511 A 15 (f)RfAM-M (loose jumper)
A.g4511 A 14 (g)RgAM-M (loose jumper)
A.a′—RaA, A 7 (a)
A.b′—RbA, A 6 (b)
A.c′—RcA, A 4 (c)
A.d′—RdA, A 2 (d)
A.e′—ReA, A 1 (e)
A.f′—RfA, A 9 (f)
A.g′—RgA, A 10 (g)
B.a4511 B 13 (a)RaBM-M
B.b4511 B 12 (b)RbBM-M
B.c4511 B 11 (c)RcBM-M
B.d4511 B 10 (d)RdBM-M
B.e4511 B 9 (e)ReBM-M
B.f4511 B 15 (f)RfBM-M
B.g4511 B 14 (g)RgBM-M (loose jumper)
B.a′—RaB, B 7 (a)
B.b′—RbB, B 6 (b)
B.c′—RcB, B 4 (c)
B.d′—RdB, B 2 (d)
B.e′—ReB, B 1 (e)
B.f′—RfB, B 9 (f)
B.g′—RgB, B 10 (g)
S.a4511 S 13 (a)RaSM-M (loose jumper)
S.b4511 S 12 (b)RbSM-M (loose jumper)
S.c4511 S 11 (c)RcSM-M
S.d4511 S 10 (d)RdSM-M
S.e4511 S 9 (e)ReSM-M
S.f4511 S 15 (f)RfSM-M
S.g4511 S 14 (g)RgSM-M
S.a′—RaS, A + B 7 (a)
S.b′—RbS, A + B 6 (b)
S.c′—RcS, A + B 4 (c)
S.d′—RdS, A + B 2 (d)
S.e′—ReS, A + B 1 (e)
S.f′—RfS, A + B 9 (f)
S.g′—RgS, A + B 10 (g)
+5V+5V4511 A 16 (VDD), 4014 A 16 (VDD), 4511 B 16 (VDD), 4014 B 16 (VDD), 4032 16 (VDD), 4015 16 (VDD), 4511 S 16 (VDD), 4511 A 4 (BI), 4511 A 3 (LT), 4511 B 4 (BI), 4511 B 3 (LT), 4032 5 (INV2), 4032 2 (INV3), 4015 1 (CLKB), 4511 S 4 (BI), 4511 S 3 (LT)
GNDGND4511 A 8 (VSS), 4014 A 8 (VSS), 4511 B 8 (VSS), 4014 B 8 (VSS), 4032 8 (VSS), 4015 8 (VSS), 4511 S 8 (VSS), A 3 (CC), 4511 A 5 (LE), 4014 A 4 (P4), 4014 A 5 (P3), 4014 A 11 (SER), 4014 A 6 (P2), 4014 A 7 (P1), B 3 (CC), 4511 B 5 (LE), 4014 B 7 (P1), 4014 B 6 (P2), 4014 B 11 (SER), 4014 B 5 (P3), 4014 B 4 (P4), 4032 7 (INV1), 4032 12 (B2), 4032 13 (A2), 4032 14 (B3), 4032 15 (A3), 4015 14 (RB), 4015 15 (DB), 4511 S 5 (LE), A + B 3 (CC)
not connected4014 A 2 (Q6), 4014 A 12 (Q7), 4014 B 2 (Q6), 4014 B 12 (Q7), 4032 1 (S3), 4032 4 (S2), 4015 2 (Q4B), 4015 11 (Q3B), 4015 12 (Q2B), 4015 13 (Q1B), A 5 (DP), A 8 (CC), B 5 (DP), B 8 (CC), A + B 5 (DP), A + B 8 (CC)

7Schematic 3: the breadboards

−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 1−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 2−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 35V5VPOWER MODULE · 5V9V USB-DC4511 A4014 A4511 B4014 B403240154511 SABA + BA.aA.bA.cA.dA.eA.fA.gB.aB.bB.cB.dB.eB.fB.gS.aS.bS.cS.dS.eS.fS.gCLKCLKCLKLOADLOADLOADA→B→S→A0A1A2A3B0B0B1B1B2B2B3B3Q1AQ2AQ3AQ4A+OUT−A3+OUT−A2+OUT−A1+OUT−A0+OUT−B3+OUT−B2+OUT−B1+OUT−B0+OUT−LOAD−OUT+555CLOCK 555~1 Hz
Every wire in its hole, on three boards one above the other: A on the top board, B in the middle, the adder, the 4015 and the sum on the bottom board with the power module. The rail jumpers on the right bring power to the other two boards. Wires drawn as loose arcs could not be laid flat. · SVG · PNG
CLKLOADA3A2A1A0B3B2B1B0A→B→S→Q1AQ2AQ3AQ4AA.aA.bA.cA.dA.eA.fA.gA.a′A.b′A.c′A.d′A.e′A.f′A.g′B.aB.bB.cB.dB.eB.fB.gB.a′B.b′B.c′B.d′B.e′B.f′B.g′S.aS.bS.cS.dS.eS.fS.gS.a′S.b′S.c′S.d′S.e′S.f′S.g′+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. Split the work: one student builds the top board, one the middle board, one the bottom board. Agree first on the wires between the boards: the clock, LOAD, the two streams and the B buttons.
  3. Insert the chips across the gap: the 4511 A turned around, notch to the right, pin 1 in hole 27e of board 1; the 4014 A, notch to the left, pin 1 in hole 35f of board 1; the 4511 B turned around, notch to the right, pin 1 in hole 25e of board 2; the 4014 B turned around, notch to the right, pin 1 in hole 39e of board 2; the 4032 turned around, notch to the right, pin 1 in hole 13e of board 3; the 4015 turned around, notch to the right, pin 1 in hole 25e of board 3; the 4511 S turned around, notch to the right, pin 1 in hole 45e of board 3. 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.
  4. 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 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.
  5. The rail jumpers on the right: they bring +5V and GND from the bottom board to the other two.
  6. Each display unit: the 4511, its display and the seven resistors, as in exercise B7.
  7. The fixed inputs and the signal wires, board by board, following the checklist. Tick each wire as you insert it.
  8. With the power off, the modules: A3–A0, B3–B0, LOAD and the clock module above the top board.
  9. Turn the power on and set the clock module to its slowest.

Wiring checklist

·

#NetFromTo
Rails of the second board (from the board with the power module) · 8 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
5+5Vboard 3: top + rail, column 2board 2: bottom + rail, column 2
6GNDboard 3: top − rail, column 4board 2: bottom − rail, column 4
7+5Vboard 2: top + rail, column 63board 2: bottom + rail, column 63
8GNDboard 2: top − rail, column 62board 2: bottom − rail, column 62
Power (red to +, black to −) · 14 wires
9+5Vboard 1: 27j (4511 A pin 16, VDD)board 1: bottom + rail, column 27
10GNDboard 1: 20a (4511 A pin 8, VSS)board 1: top − rail, column 20
11+5Vboard 1: 35a (4014 A pin 16, VDD)board 1: top + rail, column 35
12GNDboard 1: 42j (4014 A pin 8, VSS)board 1: bottom − rail, column 42
13+5Vboard 2: 25j (4511 B pin 16, VDD)board 2: bottom + rail, column 25
14GNDboard 2: 18a (4511 B pin 8, VSS)board 2: top − rail, column 18
15+5Vboard 2: 39j (4014 B pin 16, VDD)board 2: bottom + rail, column 39
16GNDboard 2: 32a (4014 B pin 8, VSS)board 2: top − rail, column 32
17+5Vboard 3: 13j (4032 pin 16, VDD)board 3: bottom + rail, column 13
18GNDboard 3: 6a (4032 pin 8, VSS)board 3: top − rail, column 6
19+5Vboard 3: 25j (4015 pin 16, VDD)board 3: bottom + rail, column 25
20GNDboard 3: 18a (4015 pin 8, VSS)board 3: top − rail, column 18
21+5Vboard 3: 45j (4511 S pin 16, VDD)board 3: bottom + rail, column 45
22GNDboard 3: 38a (4511 S pin 8, VSS)board 3: top − rail, column 38
Fixed inputs: CMOS inputs must not float · 32 wires
23GNDboard 1: 10j (display A pin 3, CC)board 1: bottom − rail, column 10
24GNDboard 1: 23a (4511 A pin 5, LE)board 1: top − rail, column 23
25+5Vboard 1: 24a (4511 A pin 4, BI)board 1: top + rail, column 24
26+5Vboard 1: 25a (4511 A pin 3, LT)board 1: top + rail, column 25
27GNDboard 1: 38j (4014 A pin 4, P4)board 1: bottom − rail, column 38
28GNDboard 1: 39j (4014 A pin 5, P3)board 1: bottom − rail, column 39
29GNDboard 1: 40a (4014 A pin 11, SER)board 1: top − rail, column 40
30GNDboard 1: 40j (4014 A pin 6, P2)board 1: bottom − rail, column 40
31GNDboard 1: 41j (4014 A pin 7, P1)board 1: bottom − rail, column 41
32GNDboard 2: 8j (display B pin 3, CC)board 2: bottom − rail, column 8
33GNDboard 2: 21a (4511 B pin 5, LE)board 2: top − rail, column 21
34+5Vboard 2: 22a (4511 B pin 4, BI)board 2: top + rail, column 22
35+5Vboard 2: 23a (4511 B pin 3, LT)board 2: top + rail, column 23
36GNDboard 2: 33a (4014 B pin 7, P1)board 2: top − rail, column 33
37GNDboard 2: 34a (4014 B pin 6, P2)board 2: top − rail, column 34
38GNDboard 2: 34j (4014 B pin 11, SER)board 2: bottom − rail, column 34
39GNDboard 2: 35a (4014 B pin 5, P3)board 2: top − rail, column 35
40GNDboard 2: 36a (4014 B pin 4, P4)board 2: top − rail, column 36
41GNDboard 3: 7a (4032 pin 7, INV1)board 3: top − rail, column 7
42+5Vboard 3: 9a (4032 pin 5, INV2)board 3: top + rail, column 9
43GNDboard 3: 9j (4032 pin 12, B2)board 3: bottom − rail, column 9
44GNDboard 3: 10j (4032 pin 13, A2)board 3: bottom − rail, column 10
45GNDboard 3: 11j (4032 pin 14, B3)board 3: bottom − rail, column 11
46+5Vboard 3: 12a (4032 pin 2, INV3)board 3: top + rail, column 12
47GNDboard 3: 12j (4032 pin 15, A3)board 3: bottom − rail, column 12
48GNDboard 3: 23j (4015 pin 14, RB)board 3: bottom − rail, column 23
49GNDboard 3: 24j (4015 pin 15, DB)board 3: bottom − rail, column 24
50+5Vboard 3: 25a (4015 pin 1, CLKB)board 3: top + rail, column 25
51GNDboard 3: 41a (4511 S pin 5, LE)board 3: top − rail, column 41
52+5Vboard 3: 42a (4511 S pin 4, BI)board 3: top + rail, column 42
53+5Vboard 3: 43a (4511 S pin 3, LT)board 3: top + rail, column 43
54GNDboard 3: 55j (display A + B pin 3, CC)board 3: bottom − rail, column 55
Resistors, 560 Ω (not wires: put each resistor in these two holes) · 21 resistors
RbA560 Ωboard 1: 12bboard 1: 16b
RaA560 Ωboard 1: 11aboard 1: 15a
RfA560 Ωboard 1: 9cboard 1: 14c
RcA560 Ωboard 1: 11jboard 1: 15j
RdA560 Ωboard 1: 9iboard 1: 14i
RgA560 Ωboard 1: 8aboard 1: 4a
ReA560 Ωboard 1: 8jboard 1: 4j
RbB560 Ωboard 2: 10bboard 2: 14b
RaB560 Ωboard 2: 9aboard 2: 13a
RfB560 Ωboard 2: 7cboard 2: 12c
RcB560 Ωboard 2: 9jboard 2: 13j
RdB560 Ωboard 2: 7iboard 2: 12i
RgB560 Ωboard 2: 6aboard 2: 2a
ReB560 Ωboard 2: 6jboard 2: 2j
RgS560 Ωboard 3: 53bboard 3: 49b
RfS560 Ωboard 3: 54aboard 3: 50a
RaS560 Ωboard 3: 56cboard 3: 51c
ReS560 Ωboard 3: 53jboard 3: 49j
RdS560 Ωboard 3: 54iboard 3: 50i
RbS560 Ωboard 3: 57aboard 3: 61a
RcS560 Ωboard 3: 56jboard 3: 60j
Signal wires · 46 wires
55A.aboard 1: 24i (4511 A pin 13, a)board 1: 15d (RaA)
56A.bboard 1: 23h (4511 A pin 12, b)board 1: 16e (RbA)
57A.cboard 1: 22g (4511 A pin 11, c)board 1: 15f (RcA)
58A.dboard 1: 21g (4511 A pin 10, d)board 1: 14g (RdA)
59A.eboard 1: 20j (4511 A pin 9, e)board 1: 4i (ReA)
60A.f (loose jumper)board 1: 26h (4511 A pin 15, f)board 1: 14d (RfA)
61A.g (loose jumper)board 1: 25h (4511 A pin 14, g)board 1: 4c (RgA)
62B.aboard 2: 22i (4511 B pin 13, a)board 2: 13d (RaB)
63B.bboard 2: 21h (4511 B pin 12, b)board 2: 14e (RbB)
64B.cboard 2: 20g (4511 B pin 11, c)board 2: 13f (RcB)
65B.dboard 2: 19g (4511 B pin 10, d)board 2: 12g (RdB)
66B.eboard 2: 18j (4511 B pin 9, e)board 2: 2i (ReB)
67B.fboard 2: 24g (4511 B pin 15, f)board 2: 12d (RfB)
68B.g (loose jumper)board 2: 23h (4511 B pin 14, g)board 2: 2c (RgB)
69S.a (loose jumper)board 3: 42h (4511 S pin 13, a)board 3: 51d (RaS)
70S.b (loose jumper)board 3: 41h (4511 S pin 12, b)board 3: 61c (RbS)
71S.cboard 3: 40j (4511 S pin 11, c)board 3: 60i (RcS)
72S.dboard 3: 39g (4511 S pin 10, d)board 3: 50f (RdS)
73S.eboard 3: 38g (4511 S pin 9, e)board 3: 49g (ReS)
74S.fboard 3: 44h (4511 S pin 15, f)board 3: 50d (RfS)
75S.gboard 3: 43i (4511 S pin 14, g)board 3: 49c (RgS)
76CLKboard 1: 41d (4014 A pin 10, CLK)board 2: 33h (4014 B pin 10, CLK)
77CLKboard 2: 33g (4014 B pin 10, CLK)board 3: 18g (4015 pin 9, CLKA)
78CLKboard 3: 18h (4015 pin 9, CLKA)board 3: 11c (4032 pin 3, CLK)
79LOADboard 1: 42c (4014 A pin 9, P/S)board 2: 32j (4014 B pin 9, P/S)
80LOADboard 2: 32g (4014 B pin 9, P/S)board 3: 20a (4015 pin 6, RA)
81LOADboard 3: 20d (4015 pin 6, RA)board 3: 8d (4032 pin 6, CR)
82A→board 1: 37g (4014 A pin 3, Q8)board 3: 7i (4032 pin 10, A1)
83B→board 2: 37c (4014 B pin 3, Q8)board 3: 8h (4032 pin 11, B1)
84S→board 3: 6g (4032 pin 9, S1)board 3: 19c (4015 pin 7, DA)
85A0board 1: 35g (4014 A pin 1, P8)board 1: 21b (4511 A pin 7, A)
86A1board 1: 36d (4014 A pin 15, P7)board 1: 27d (4511 A pin 1, B)
87A2board 1: 37c (4014 A pin 14, P6)board 1: 26c (4511 A pin 2, C)
88A3board 1: 38b (4014 A pin 13, P5)board 1: 22b (4511 A pin 6, D)
89B0board 2: 39d (4014 B pin 1, P8)board 2: 19d (4511 B pin 7, A)
90B0board 2: 39c (4014 B pin 1, P8)board 1: 43e
91B1board 2: 38g (4014 B pin 15, P7)board 2: 25b (4511 B pin 1, B)
92B1board 2: 38h (4014 B pin 15, P7)board 1: 34b
93B2board 2: 37g (4014 B pin 14, P6)board 2: 24a (4511 B pin 2, C)
94B2board 2: 37h (4014 B pin 14, P6)board 1: 31e
95B3 (loose jumper)board 2: 36h (4014 B pin 13, P5)board 2: 20c (4511 B pin 6, D)
96B3board 2: 36i (4014 B pin 13, P5)board 1: 28e
97Q1Aboard 3: 21d (4015 pin 5, Q1A)board 3: 40d (4511 S pin 6, D)
98Q2Aboard 3: 22c (4015 pin 4, Q2A)board 3: 44c (4511 S pin 2, C)
99Q3Aboard 3: 23b (4015 pin 3, Q3A)board 3: 45b (4511 S pin 1, B)
100Q4Aboard 3: 19g (4015 pin 10, Q4A)board 3: 39a (4511 S pin 7, A)
Modules on male-female jumpers · 30 wires
101+5Vbutton A3, +board 1: top + rail, column 4
102A3button A3, OUTboard 1: 22a (4511 A pin 6, D)
103GNDbutton A3, −board 1: top − rail, column 6
104+5Vbutton A2, +board 1: top + rail, column 9
105A2button A2, OUTboard 1: 26a (4511 A pin 2, C)
106GNDbutton A2, −board 1: top − rail, column 11
107+5Vbutton A1, +board 1: top + rail, column 14
108A1button A1, OUTboard 1: 27a (4511 A pin 1, B)
109GNDbutton A1, −board 1: top − rail, column 16
110+5Vbutton A0, +board 1: top + rail, column 19
111A0button A0, OUTboard 1: 21a (4511 A pin 7, A)
112GNDbutton A0, −board 1: top − rail, column 21
113+5Vbutton B3, +board 1: top + rail, column 26
114B3button B3, OUTboard 1: 28a
115GNDbutton B3, −board 1: top − rail, column 27
116+5Vbutton B2, +board 1: top + rail, column 30
117B2button B2, OUTboard 1: 31a
118GNDbutton B2, −board 1: top − rail, column 32
119+5Vbutton B1, +board 1: top + rail, column 34
120B1button B1, OUTboard 1: 34a
121GNDbutton B1, −board 1: top − rail, column 37
122+5Vbutton B0, +board 1: top + rail, column 40
123B0button B0, OUTboard 1: 43a
124GNDbutton B0, −board 1: top − rail, column 42
125+5Vbutton LOAD, +board 1: top + rail, column 46
126LOADbutton LOAD, OUTboard 1: 42a (4014 A pin 9, P/S)
127GNDbutton LOAD, −board 1: top − rail, column 48
128GND555 clock module, −board 1: top − rail, column 52
129CLK555 clock module, OUTboard 1: 41a (4014 A pin 10, CLK)
130+5V555 clock module, +board 1: top + rail, column 54

Test it

If it does not work: test each board on its own first: the displays A and B must follow their buttons. If the sum is always A or always B, one stream does not reach the 4032. If it is wrong by a carry, check CARRY RESET and the clock of the 4032. If the bits come out in the wrong order, P5–P8 of a 4014 are reversed.

8SystemVerilog: the generic design

serial_adder.sv has the same four parts: two serializers that shift right, a full adder with a carry flip-flop, a deserializer, and three decoders. It is synthesizable.

serial_adder.svfile
// Exercise P1: a bit-serial adder.  Two 4-bit numbers are turned into
// streams of bits, added one bit per clock with a stored carry, and the
// sum bits are collected back into a number.
//
// Generic version that matches schematic 1, not tied to any chip.
//
// load at a rising edge of clk puts a and b into the serializers and
// clears the carry.  At every later edge each serializer moves one bit out,
// lowest first, then zeros; the adder adds the two bits and the carry, and
// the deserializer takes the sum bit.  load also clears the deserializer
// at once.  After four edges sum holds a + b without the fifth bit, and
// later edges push it along.  Like the 4511, the decoders blank the codes
// 10-15.  Synthesizable.

module serial_adder
(
    input  logic       clk,
    input  logic       load,
    input  logic [3:0] a,
    input  logic [3:0] b,

    output logic       serial_a,     // The bit of a on its way, like Q8 of the 4014
    output logic       serial_b,
    output logic       serial_sum,   // The sum bit, like SUM1 of the 4032

    output logic [3:0] sum,          // The deserializer, newest bit in sum [3]

    output logic [6:0] abcdefg_a,    // Segments a-g of each display: abcdefg [6] is a
    output logic [6:0] abcdefg_b,
    output logic [6:0] abcdefg_sum
);

    //------------------------------------------------------------------------
    // The serializers: the bits still to send, the next one at the bottom

    logic [3:0] shift_a, shift_b;

    always_ff @ (posedge clk)
        if (load)
        begin
            shift_a <= a;
            shift_b <= b;
        end
        else
        begin
            shift_a <= shift_a >> 1;
            shift_b <= shift_b >> 1;
        end

    assign serial_a = shift_a [0];
    assign serial_b = shift_b [0];

    //------------------------------------------------------------------------
    // The serial adder: one full adder and a flip-flop for the carry

    logic carry;

    assign serial_sum = serial_a ^ serial_b ^ carry;

    always_ff @ (posedge clk)
        if (load)
            carry <= 1'b0;
        else
            carry <= serial_a & serial_b | (serial_a ^ serial_b) & carry;

    //------------------------------------------------------------------------
    // The deserializer

    always_ff @ (posedge clk or posedge load)
        if (load)
            sum <= '0;
        else
            sum <= { serial_sum, sum [3:1] };

    //------------------------------------------------------------------------
    // Three decoders, like the 4511: codes 10-15 blank the display

    function automatic logic [6:0] decode (input logic [3:0] code);
        case (code)
        4'd0:    decode = 7'b111_1110;
        4'd1:    decode = 7'b011_0000;
        4'd2:    decode = 7'b110_1101;
        4'd3:    decode = 7'b111_1001;
        4'd4:    decode = 7'b011_0011;
        4'd5:    decode = 7'b101_1011;
        4'd6:    decode = 7'b001_1111;
        4'd7:    decode = 7'b111_0000;
        4'd8:    decode = 7'b111_1111;
        4'd9:    decode = 7'b111_0011;
        default: decode = 7'b000_0000;
        endcase
    endfunction

    assign abcdefg_a   = decode (a);
    assign abcdefg_b   = decode (b);
    assign abcdefg_sum = decode (sum);

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.

cd4511.svfile
// CD4511B: BCD-to-7-segment latch, decoder and driver.
//
// Ports in pin order, named as in the datasheet: BCD inputs A-D (A is the
// lowest bit), segment outputs a-g.  The power pins, VSS (8) and VDD (16),
// are left out.  Synthesizable; LATCH ENABLE makes a latch, which FPGA
// tools accept but warn about.
//
// The outputs drive a common-cathode display through resistors: 1 lights
// the segment.  Codes 10 to 15 blank the display.  The chip draws 6 without
// the top segment and 9 without the bottom one.
//
// LAMP TEST (active low) lights all segments; BLANKING (active low) turns
// them all off; LAMP TEST wins.  LATCH ENABLE: 0 = the decoder follows the
// inputs, 1 = it keeps the code it had when LATCH ENABLE rose.

module cd4511
(
    input  logic bcd_b,         // Pin 1
    input  logic bcd_c,         // Pin 2
    input  logic lamp_test_n,   // Pin 3:  0 = all segments on
    input  logic blank_n,       // Pin 4:  0 = all segments off
    input  logic latch_enable,  // Pin 5:  1 = hold the code
    input  logic bcd_d,         // Pin 6
    input  logic bcd_a,         // Pin 7
                                // Pin 8:  VSS
    output logic seg_e,         // Pin 9
    output logic seg_d,         // Pin 10
    output logic seg_c,         // Pin 11
    output logic seg_b,         // Pin 12
    output logic seg_a,         // Pin 13
    output logic seg_g,         // Pin 14
    output logic seg_f          // Pin 15
                                // Pin 16: VDD
);

    logic [3:0] code;

    // This is a latch, but Verilator 5 does not see it and warns
    // verilator lint_off NOLATCH

    always_latch
        if (~ latch_enable)
            code = { bcd_d, bcd_c, bcd_b, bcd_a };

    logic [6:0] abcdefg;

    always_comb
        if (~ lamp_test_n)
            abcdefg = 7'b111_1111;
        else if (~ blank_n)
            abcdefg = 7'b000_0000;
        else
            case (code)
            4'd0:    abcdefg = 7'b111_1110;
            4'd1:    abcdefg = 7'b011_0000;
            4'd2:    abcdefg = 7'b110_1101;
            4'd3:    abcdefg = 7'b111_1001;
            4'd4:    abcdefg = 7'b011_0011;
            4'd5:    abcdefg = 7'b101_1011;
            4'd6:    abcdefg = 7'b001_1111;  // No top segment
            4'd7:    abcdefg = 7'b111_0000;
            4'd8:    abcdefg = 7'b111_1111;
            4'd9:    abcdefg = 7'b111_0011;  // No bottom segment
            default: abcdefg = 7'b000_0000;  // 10-15: blank
            endcase

    assign { seg_a, seg_b, seg_c, seg_d, seg_e, seg_f, seg_g } = abcdefg;

endmodule
cd4014.svfile
// CD4014B: 8-stage static shift register, synchronous parallel or serial
// input, serial output.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.  Synthesizable.
//
// Everything happens on the rising edge of CLOCK.  With PARALLEL/SERIAL
// CONTROL = 1 the edge loads P1-P8 into the eight stages; with 0 it shifts:
// SERIAL IN goes into stage 1 and every stage takes the bit of the stage
// before it.  Q8, the output of the last stage, is the serial output; Q6 and
// Q7 are there too.  The chip has no reset.

module cd4014
(
    input  logic p8,               // Pin 1
    output logic q6,               // Pin 2
    output logic q8,               // Pin 3
    input  logic p4,               // Pin 4
    input  logic p3,               // Pin 5
    input  logic p2,               // Pin 6
    input  logic p1,               // Pin 7
                                   // Pin 8:  VSS
    input  logic parallel_serial,  // Pin 9:  1 = load, 0 = shift
    input  logic clock,            // Pin 10
    input  logic serial_in,        // Pin 11
    output logic q7,               // Pin 12
    input  logic p5,               // Pin 13
    input  logic p6,               // Pin 14
    input  logic p7                // Pin 15
                                   // Pin 16: VDD
);

    logic [8:1] stage;

    always_ff @ (posedge clock)
        if (parallel_serial)
            stage <= { p8, p7, p6, p5, p4, p3, p2, p1 };
        else
            stage <= { stage [7:1], serial_in };

    assign q6 = stage [6];
    assign q7 = stage [7];
    assign q8 = stage [8];

endmodule
cd4032.svfile
// CD4032B: triple serial adder.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.
//
// Each adder takes two numbers one bit per clock, lowest bit first.  Its
// SUM output is A XOR B XOR the stored carry, at once, without the clock;
// the rising edge of CLOCK stores the carry for the next bit.  INVERT at 1
// inverts the sum.  The datasheet asks to clear the carries with CARRY
// RESET at 1 one bit before the first bit of the next word; the model
// clears them at the rising edge of CLOCK that sees CARRY RESET at 1.
// The three adders share CLOCK and CARRY RESET.  Synthesizable.

module cd4032
(
    output logic sum3,         // Pin 1
    input  logic invert3,      // Pin 2
    input  logic clock,        // Pin 3
    output logic sum2,         // Pin 4
    input  logic invert2,      // Pin 5
    input  logic carry_reset,  // Pin 6:  1 at a rising edge = carries 0
    input  logic invert1,      // Pin 7
                               // Pin 8:  VSS
    output logic sum1,         // Pin 9
    input  logic a1,           // Pin 10
    input  logic b1,           // Pin 11
    input  logic b2,           // Pin 12
    input  logic a2,           // Pin 13
    input  logic b3,           // Pin 14
    input  logic a3            // Pin 15
                               // Pin 16: VDD
);

    wire  [3:1] a   = { a3, a2, a1 };
    wire  [3:1] b   = { b3, b2, b1 };
    wire  [3:1] inv = { invert3, invert2, invert1 };

    logic [3:1] carry;

    always_ff @ (posedge clock)
        if (carry_reset)
            carry <= '0;
        else
            carry <= a & b | (a ^ b) & carry;

    assign { sum3, sum2, sum1 } = a ^ b ^ carry ^ inv;

endmodule
cd4015.svfile
// CD4015B: dual 4-stage shift register, serial input, parallel output.
//
// Ports in pin order, named as in the datasheet.  The power pins, VSS (8)
// and VDD (16), are left out.  Synthesizable.
//
// Each register shifts on the rising edge of its clock: DATA goes into Q1,
// Q1 into Q2, Q2 into Q3, Q3 into Q4.  RESET is active high and
// asynchronous: it clears all four stages at once, without the clock.
// Connect Q4A to DATA B to make one 8-stage register.

module cd4015
(
    input  logic clock_b,  // Pin 1
    output logic q4b,      // Pin 2
    output logic q3a,      // Pin 3
    output logic q2a,      // Pin 4
    output logic q1a,      // Pin 5
    input  logic reset_a,  // Pin 6:  1 = Q1A-Q4A go to 0 at once
    input  logic data_a,   // Pin 7
                           // Pin 8:  VSS
    input  logic clock_a,  // Pin 9
    output logic q4a,      // Pin 10
    output logic q3b,      // Pin 11
    output logic q2b,      // Pin 12
    output logic q1b,      // Pin 13
    input  logic reset_b,  // Pin 14: 1 = Q1B-Q4B go to 0 at once
    input  logic data_b    // Pin 15
                           // Pin 16: VDD
);

    logic [4:1] ra, rb;  // ra [1] is Q1A, ra [4] is Q4A

    always_ff @ (posedge clock_a or posedge reset_a)
        if (reset_a)
            ra <= '0;
        else
            ra <= { ra [3:1], data_a };

    always_ff @ (posedge clock_b or posedge reset_b)
        if (reset_b)
            rb <= '0;
        else
            rb <= { rb [3:1], data_b };

    assign { q4a, q3a, q2a, q1a } = ra;
    assign { q4b, q3b, q2b, q1b } = rb;

endmodule

10SystemVerilog: the chip-level design

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

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

module serial_adder_chips
(
    input  logic       clk,
    input  logic       load,
    input  logic [3:0] a,
    input  logic [3:0] b,
    output logic       serial_a,
    output logic       serial_b,
    output logic       serial_sum,
    output logic [3:0] sum,
    output logic [6:0] abcdefg_a,
    output logic [6:0] abcdefg_b,
    output logic [6:0] abcdefg_sum
);

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

    wire gnd = 1'b0;
    wire vdd = 1'b1;

    // Outputs that go nowhere

    wire unused_usa_q6;
    wire unused_usa_q7;
    wire unused_usb_q6;
    wire unused_usb_q7;
    wire unused_u32_sum3;
    wire unused_u32_sum2;
    wire unused_u15_q4b;
    wire unused_u15_q3b;
    wire unused_u15_q2b;
    wire unused_u15_q1b;

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

    // UDA: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 20 of board 1

    cd4511 i_uda
    (
        .bcd_b        ( a [1]         ),  // Pin 1 <- button A1
        .bcd_c        ( a [2]         ),  // Pin 2 <- button A2
        .lamp_test_n  ( vdd           ),  // Pin 3 <- +5V
        .blank_n      ( vdd           ),  // Pin 4 <- +5V
        .latch_enable ( gnd           ),  // Pin 5 <- GND
        .bcd_d        ( a [3]         ),  // Pin 6 <- button A3
        .bcd_a        ( a [0]         ),  // Pin 7 <- button A0
        .seg_e        ( abcdefg_a [2] ),  // Pin 9 -> A.e
        .seg_d        ( abcdefg_a [3] ),  // Pin 10 -> A.d
        .seg_c        ( abcdefg_a [4] ),  // Pin 11 -> A.c
        .seg_b        ( abcdefg_a [5] ),  // Pin 12 -> A.b
        .seg_a        ( abcdefg_a [6] ),  // Pin 13 -> A.a
        .seg_g        ( abcdefg_a [0] ),  // Pin 14 -> A.g
        .seg_f        ( abcdefg_a [1] )   // Pin 15 -> A.f
    );

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

    // USA: CD4014 8-stage shift register, parallel or serial in, serial out, pin 1 in column 35 of board 1

    cd4014 i_usa
    (
        .p8              ( a [0]         ),  // Pin 1 <- button A0
        .q6              ( unused_usa_q6 ),  // Pin 2, unused output
        .q8              ( serial_a      ),  // Pin 3 -> A→
        .p4              ( gnd           ),  // Pin 4 <- GND
        .p3              ( gnd           ),  // Pin 5 <- GND
        .p2              ( gnd           ),  // Pin 6 <- GND
        .p1              ( gnd           ),  // Pin 7 <- GND
        .parallel_serial ( load          ),  // Pin 9 <- button LOAD
        .clock           ( clk           ),  // Pin 10 <- 555 clock module
        .serial_in       ( gnd           ),  // Pin 11 <- GND
        .q7              ( unused_usa_q7 ),  // Pin 12, unused output
        .p5              ( a [3]         ),  // Pin 13 <- button A3
        .p6              ( a [2]         ),  // Pin 14 <- button A2
        .p7              ( a [1]         )   // Pin 15 <- button A1
    );

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

    // UDB: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 18 of board 2

    cd4511 i_udb
    (
        .bcd_b        ( b [1]         ),  // Pin 1 <- button B1
        .bcd_c        ( b [2]         ),  // Pin 2 <- button B2
        .lamp_test_n  ( vdd           ),  // Pin 3 <- +5V
        .blank_n      ( vdd           ),  // Pin 4 <- +5V
        .latch_enable ( gnd           ),  // Pin 5 <- GND
        .bcd_d        ( b [3]         ),  // Pin 6 <- button B3
        .bcd_a        ( b [0]         ),  // Pin 7 <- button B0
        .seg_e        ( abcdefg_b [2] ),  // Pin 9 -> B.e
        .seg_d        ( abcdefg_b [3] ),  // Pin 10 -> B.d
        .seg_c        ( abcdefg_b [4] ),  // Pin 11 -> B.c
        .seg_b        ( abcdefg_b [5] ),  // Pin 12 -> B.b
        .seg_a        ( abcdefg_b [6] ),  // Pin 13 -> B.a
        .seg_g        ( abcdefg_b [0] ),  // Pin 14 -> B.g
        .seg_f        ( abcdefg_b [1] )   // Pin 15 -> B.f
    );

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

    // USB: CD4014 8-stage shift register, parallel or serial in, serial out, pin 1 in column 32 of board 2

    cd4014 i_usb
    (
        .p8              ( b [0]         ),  // Pin 1 <- button B0
        .q6              ( unused_usb_q6 ),  // Pin 2, unused output
        .q8              ( serial_b      ),  // Pin 3 -> B→
        .p4              ( gnd           ),  // Pin 4 <- GND
        .p3              ( gnd           ),  // Pin 5 <- GND
        .p2              ( gnd           ),  // Pin 6 <- GND
        .p1              ( gnd           ),  // Pin 7 <- GND
        .parallel_serial ( load          ),  // Pin 9 <- button LOAD
        .clock           ( clk           ),  // Pin 10 <- 555 clock module
        .serial_in       ( gnd           ),  // Pin 11 <- GND
        .q7              ( unused_usb_q7 ),  // Pin 12, unused output
        .p5              ( b [3]         ),  // Pin 13 <- button B3
        .p6              ( b [2]         ),  // Pin 14 <- button B2
        .p7              ( b [1]         )   // Pin 15 <- button B1
    );

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

    // U32: CD4032 triple serial adder, pin 1 in column 6 of board 3

    cd4032 i_u32
    (
        .sum3        ( unused_u32_sum3 ),  // Pin 1, unused output
        .invert3     ( vdd             ),  // Pin 2 <- +5V, unused input
        .clock       ( clk             ),  // Pin 3 <- 555 clock module
        .sum2        ( unused_u32_sum2 ),  // Pin 4, unused output
        .invert2     ( vdd             ),  // Pin 5 <- +5V, unused input
        .carry_reset ( load            ),  // Pin 6 <- button LOAD
        .invert1     ( gnd             ),  // Pin 7 <- GND
        .sum1        ( serial_sum      ),  // Pin 9 -> S→
        .a1          ( serial_a        ),  // Pin 10 <- USA pin 3 (Q8)
        .b1          ( serial_b        ),  // Pin 11 <- USB pin 3 (Q8)
        .b2          ( gnd             ),  // Pin 12 <- GND, unused input
        .a2          ( gnd             ),  // Pin 13 <- GND, unused input
        .b3          ( gnd             ),  // Pin 14 <- GND, unused input
        .a3          ( gnd             )   // Pin 15 <- GND, unused input
    );

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

    // U15: CD4015 dual 4-stage shift register, serial in, parallel out, pin 1 in column 18 of board 3

    cd4015 i_u15
    (
        .clock_b ( vdd            ),  // Pin 1 <- +5V, unused input
        .q4b     ( unused_u15_q4b ),  // Pin 2, unused output
        .q3a     ( sum [1]        ),  // Pin 3 -> Q3A
        .q2a     ( sum [2]        ),  // Pin 4 -> Q2A
        .q1a     ( sum [3]        ),  // Pin 5 -> Q1A
        .reset_a ( load           ),  // Pin 6 <- button LOAD
        .data_a  ( serial_sum     ),  // Pin 7 <- U32 pin 9 (S1)
        .clock_a ( clk            ),  // Pin 9 <- 555 clock module
        .q4a     ( sum [0]        ),  // Pin 10 -> Q4A
        .q3b     ( unused_u15_q3b ),  // Pin 11, unused output
        .q2b     ( unused_u15_q2b ),  // Pin 12, unused output
        .q1b     ( unused_u15_q1b ),  // Pin 13, unused output
        .reset_b ( gnd            ),  // Pin 14 <- GND, unused input
        .data_b  ( gnd            )   // Pin 15 <- GND, unused input
    );

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

    // UDS: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 38 of board 3

    cd4511 i_uds
    (
        .bcd_b        ( sum [1]         ),  // Pin 1 <- U15 pin 3 (Q3A)
        .bcd_c        ( sum [2]         ),  // Pin 2 <- U15 pin 4 (Q2A)
        .lamp_test_n  ( vdd             ),  // Pin 3 <- +5V
        .blank_n      ( vdd             ),  // Pin 4 <- +5V
        .latch_enable ( gnd             ),  // Pin 5 <- GND
        .bcd_d        ( sum [3]         ),  // Pin 6 <- U15 pin 5 (Q1A)
        .bcd_a        ( sum [0]         ),  // Pin 7 <- U15 pin 10 (Q4A)
        .seg_e        ( abcdefg_sum [2] ),  // Pin 9 -> S.e
        .seg_d        ( abcdefg_sum [3] ),  // Pin 10 -> S.d
        .seg_c        ( abcdefg_sum [4] ),  // Pin 11 -> S.c
        .seg_b        ( abcdefg_sum [5] ),  // Pin 12 -> S.b
        .seg_a        ( abcdefg_sum [6] ),  // Pin 13 -> S.a
        .seg_g        ( abcdefg_sum [0] ),  // Pin 14 -> S.g
        .seg_f        ( abcdefg_sum [1] )   // Pin 15 -> S.f
    );

endmodule

11Testbench and simulation scripts

tb.sv runs both designs side by side and compares them with a reference model that takes A + B at LOAD and expects the bits of A, B and the sum, lowest first, in the streams and in the deserializer. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It adds five chosen pairs, one of them with LOAD held for three edges, and ten pseudo-random ones, and checks the three displays too. The output below is shortened.

tb.sv251 lines · show the codefile
// Testbench for exercise P1.
//
// Runs the generic design (serial_adder.sv) and the chip-level design
// (serial_adder_chips.sv) side by side and compares both with a reference
// model that knows the answer in advance: at LOAD it takes a + b, and the
// bit streams are the bits of the numbers and of the sum, lowest first.
// 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 chosen and pseudo-random
// pairs, holds LOAD for several edges once, and checks the bit streams,
// the deserializer and the three displays at every edge.

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

    // load is a synchronous load and an asynchronous clear, as on the
    // breadboard

    // verilator lint_off SYNCASYNCNET

    logic       clk;
    logic       load = 1'b0;
    logic [3:0] a    = '0;
    logic [3:0] b    = '0;

    wire        serial_a,    serial_a_chips;
    wire        serial_b,    serial_b_chips;
    wire        serial_sum,  serial_sum_chips;
    wire  [3:0] sum,         sum_chips;
    wire  [6:0] abcdefg_a,   abcdefg_a_chips;
    wire  [6:0] abcdefg_b,   abcdefg_b_chips;
    wire  [6:0] abcdefg_sum, abcdefg_sum_chips;

    serial_adder i_generic
    (
        .clk         ( clk         ),
        .load        ( load        ),
        .a           ( a           ),
        .b           ( b           ),
        .serial_a    ( serial_a    ),
        .serial_b    ( serial_b    ),
        .serial_sum  ( serial_sum  ),
        .sum         ( sum         ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

    serial_adder_chips i_chips
    (
        .clk         ( clk               ),
        .load        ( load              ),
        .a           ( a                 ),
        .b           ( b                 ),
        .serial_a    ( serial_a_chips    ),
        .serial_b    ( serial_b_chips    ),
        .serial_sum  ( serial_sum_chips  ),
        .sum         ( sum_chips         ),
        .abcdefg_a   ( abcdefg_a_chips   ),
        .abcdefg_b   ( abcdefg_b_chips   ),
        .abcdefg_sum ( abcdefg_sum_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: the numbers taken at the last LOAD edge, their sum,
    // and the number of edges since then

    logic [3:0] a_loaded, b_loaded;
    int         k;

    always @ (posedge clk)
        if (load)
        begin
            a_loaded <= a;
            b_loaded <= b;
            k        <= 0;
        end
        else
            k <= k + 1;

    wire [4:0] total = 5' (a_loaded) + 5' (b_loaded);

    function automatic logic bit_of (input logic [4:0] value, input int i);
        return i >= 0 && i < 5 ? value [i] : 1'b0;
    endfunction

    // After k edges the deserializer holds the sum bits k-1 (in sum [3])
    // down to k-4; LOAD at 1 clears it at once

    logic [3:0] sum_expected;

    always_comb
        for (int i = 0; i < 4; i ++)
            sum_expected [3 - i] = ~ load & bit_of (total, k - 1 - i);

    function automatic logic [6:0] digit (input logic [3:0] code);
        case (code)
        4'd0:    digit = 7'b111_1110;
        4'd1:    digit = 7'b011_0000;
        4'd2:    digit = 7'b110_1101;
        4'd3:    digit = 7'b111_1001;
        4'd4:    digit = 7'b011_0011;
        4'd5:    digit = 7'b101_1011;
        4'd6:    digit = 7'b001_1111;
        4'd7:    digit = 7'b111_0000;
        4'd8:    digit = 7'b111_1111;
        4'd9:    digit = 7'b111_0011;
        default: digit = 7'b000_0000;
        endcase
    endfunction

    wire [27:0] expected = { bit_of (5' (a_loaded), k), bit_of (5' (b_loaded), k), bit_of (total, k),
                             sum_expected, digit (a), digit (b), digit (sum_expected) };

    wire [27:0] out       = { serial_a, serial_b, serial_sum, sum, abcdefg_a, abcdefg_b, abcdefg_sum };
    wire [27:0] out_chips = { serial_a_chips, serial_b_chips, serial_sum_chips, sum_chips,
                              abcdefg_a_chips, abcdefg_b_chips, abcdefg_sum_chips };

    wire ok_generic = out       === expected;
    wire ok_chips   = out_chips === expected;

    //------------------------------------------------------------------------
    // Checking at every rising clock edge, from the first LOAD on.  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 load
    // at the clock edge on purpose, although it clears the deserializer at
    // once

    // verilator lint_off PROCASSINIT

    logic was_loaded = 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_sums   = 0;  // Complete sums seen in the deserializer
    int n_carry  = 0;  // Sums with a fifth bit

    always @ (posedge clk)
    begin
        if (load)
            was_loaded <= 1'b1;

        if (was_loaded & ~ done)
        begin
            cycle    <= cycle + 1;
            n_checks <= n_checks + 2;
            n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1);

            if (~ load && k == 4)
            begin
                n_sums  <= n_sums + 1;
                n_carry <= n_carry + (total [4] ? 1 : 0);
            end

            $display ("%5d  %b     %2d  %2d   %b  %b  %b   %b  %s  %s",
                cycle + 1, load, a, b, serial_a, serial_b, serial_sum, sum,
                ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok" : "WRONG");
        end
    end

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

    // One addition: set the buttons, press LOAD through one or more edges,
    // then let six edges pass

    task automatic add (input logic [3:0] x, input logic [3:0] y, input int load_edges);
        a    <= x;
        b    <= y;
        load <= 1'b1;
        repeat (load_edges) @ (posedge clk);
        load <= 1'b0;
        repeat (6) @ (posedge clk);
    endtask

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

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

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

        $display ("cycle  LOAD   A   B   A> B> S>  4015  generic  chips");

        // Before the first LOAD the registers are unknown, as after power-up

        repeat (2) @ (posedge clk);

        add ( 5,  3, 1);  // The example of section 2
        add ( 9,  8, 1);  // 17: a fifth bit
        add (15, 15, 1);
        add ( 0,  0, 1);
        add ( 7,  2, 3);  // LOAD held for three edges

        // Pseudo-random pairs, the same in every simulator

        for (int i = 0; i < 10; i ++)
        begin
            seed = seed * 32'd1664525 + 32'd1013904223;
            add (seed [31:28], seed [27:24], 1);
        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 && n_sums == 15 && n_carry >= 2)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d sums, %0d with a fifth bit", n_errors, n_checks, n_sums,
                      n_carry);
    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 P1: 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 serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.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 P1: 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 serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.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 P1: 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 serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.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  LOAD   A   B   A> B> S>  4015  generic  chips
    1  0      5   3   1  1  0   0000  ok          ok
    2  0      5   3   0  1  0   0000  ok          ok
    3  0      5   3   1  0  0   0000  ok          ok
    4  0      5   3   0  0  1   0000  ok          ok
    5  0      5   3   0  0  0   1000  ok          ok
    6  0      5   3   0  0  0   0100  ok          ok
    7  1      9   8   0  0  0   0000  ok          ok
    8  0      9   8   1  0  1   0000  ok          ok
    9  0      9   8   0  0  0   1000  ok          ok
   10  0      9   8   0  0  0   0100  ok          ok
   11  0      9   8   1  1  0   0010  ok          ok
   12  0      9   8   0  0  1   0001  ok          ok
   13  0      9   8   0  0  0   1000  ok          ok
   14  1     15  15   0  0  0   0000  ok          ok
   15  0     15  15   1  1  0   0000  ok          ok
   16  0     15  15   1  1  1   0000  ok          ok
   17  0     15  15   1  1  1   1000  ok          ok
   18  0     15  15   1  1  1   1100  ok          ok
   19  0     15  15   0  0  1   1110  ok          ok
   20  0     15  15   0  0  0   1111  ok          ok
   21  1      0   0   0  0  0   0000  ok          ok
   22  0      0   0   0  0  0   0000  ok          ok
   23  0      0   0   0  0  0   0000  ok          ok
   24  0      0   0   0  0  0   0000  ok          ok
   25  0      0   0   0  0  0   0000  ok          ok
   26  0      0   0   0  0  0   0000  ok          ok
   27  0      0   0   0  0  0   0000  ok          ok
   28  1      7   2   0  0  0   0000  ok          ok
  …
  105  0      5   4   0  0  0   1001  ok          ok
  106  0      5   4   0  0  0   0100  ok          ok
tb.sv:237: $finish called at 1095000 (1ps)
PASS: 212 checks, no errors

To see that the testbench catches mistakes, break something and run it again: replace carry <= 1'b0 with carry <= carry in serial_adder.sv, or connect CARRY RESET of the 4032 to GND instead of LOAD in serial_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 serial_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 3–0 are A, switches 7–4 are B and key 0 is LOAD. LEDs 3–0 show the deserializer and LEDs 6–4 the streams of A, B and the sum. The rightmost digit shows the deserializer as the 4511 would. To run the chip-level design instead, add serial_adder_chips.sv and the four chip models and change the module name in the instance.

bgm_lab/lab_top.sv111 lines · show the codefile
// Exercise P1 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 serial_adder.sv.  For the chip-level
// design add serial_adder_chips.sv and cd4014.sv, cd4032.sv, cd4015.sv,
// cd4511.sv instead, and change the module name below.
//
// The adder runs from slow_clk, 1 Hz on every BGM board.  Switches 3-0 are
// A, switches 7-4 are B, key 0 is LOAD: hold it through a tick of slow_clk.
// LEDs 3-0 show the deserializer, LEDs 6-4 the bit streams of A, B and
// the sum.  The rightmost digit shows the deserializer as the 4511 would.

// LOAD is a synchronous load for the serializers and the carry and an
// asynchronous clear for the deserializer, as on the breadboard
// verilator lint_off SYNCASYNCNET

`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       serial_a, serial_b, serial_sum;
    logic [3:0] sum;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    serial_adder i_serial_adder
    (
        .clk         ( slow_clk    ),
        .load        ( key [0]     ),
        .a           ( sw [3:0]    ),
        .b           ( sw [7:4]    ),
        .serial_a    ( serial_a    ),
        .serial_b    ( serial_b    ),
        .serial_sum  ( serial_sum  ),
        .sum         ( sum         ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

    assign led      = w_led' ({ serial_sum, serial_b, serial_a, sum });

    assign abcdefgh = { abcdefg_sum, 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 serial_adder.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Eight keys are not enough for two 4-bit numbers and LOAD, so the numbers have 3 bits here: keys 2–0 are A, keys 5–3 are B and key 7 is LOAD. The screen shows the three streams on the left and the deserializer on the right.

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

// Exercise P1 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 serial_adder.sv.
//
// The adder runs from slow_clock.  Eight keys are not enough for two 4-bit
// numbers and LOAD, so A and B are 3 bits here: keys 2-0 are A, keys 5-3
// are B and key 7 is LOAD.  The LEDs and the rightmost digit are as in
// lab_top.  The screen shows the bit streams of A, B and the sum as three
// lamps on the left and the deserializer as four lamps on the right.

// LOAD is a synchronous load for the serializers and the carry and an
// asynchronous clear for the deserializer, as on the breadboard
// verilator lint_off SYNCASYNCNET

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       serial_a, serial_b, serial_sum;
    logic [3:0] sum;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    serial_adder i_serial_adder
    (
        .clk         ( slow_clock         ),
        .load        ( key [7]            ),
        .a           ( { 1'b0, key [2:0] } ),
        .b           ( { 1'b0, key [5:3] } ),
        .serial_a    ( serial_a           ),
        .serial_b    ( serial_b           ),
        .serial_sum  ( serial_sum         ),
        .sum         ( sum                ),
        .abcdefg_a   ( abcdefg_a          ),
        .abcdefg_b   ( abcdefg_b          ),
        .abcdefg_sum ( abcdefg_sum        )
    );

    assign led      = { 1'b0, serial_sum, serial_b, serial_a, sum };

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

    //------------------------------------------------------------------------
    // The streams A, B, S on the left, top to bottom; the deserializer on
    // the right, bit 3 first.  Bright for 1, dim for 0.

    wire [2:0] stream = { serial_a, serial_b, serial_sum };

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

        for (int i = 0; i < 3; i ++)
            if (x >= 60 & x < 100 & y >= 9' (56 + i * 60) & y < 9' (96 + i * 60))
            begin
                green = stream [2 - i] ? 40 : 8;
                blue  = stream [2 - i] ? 31 : 6;
            end

        for (int i = 0; i < 4; i ++)
            if (x >= 9' (200 + i * 60) & x < 9' (240 + i * 60) & y >= 116 & y < 156)
                green = sum [3 - i] ? 63 : 12;
    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 adder, just as the 555 module clocks the chips on the breadboard.

DIP switches 0–3 are A and switches 4–7 are B. All eight are taken, so LOAD comes from BIDIR pin 0: connect it to VCC through one tick of the clock. The display shows the deserializer as a 4511 would, and its dot is the sum bit on its way. The deserializer and the streams also go to BIDIR pins 2–7.

tiny_tapeout/project.v64 lines · show the codefile
/*
 * Exercise P1 on Tiny Tapeout: a bit-serial adder.  The 7-segment display
 * of the demo board shows the deserializer as a 4511 would.
 *
 * Add serial_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

// LOAD is a synchronous load for the serializers and the carry and an
// asynchronous clear for the deserializer, as on the breadboard
// verilator lint_off SYNCASYNCNET

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 A and switches 4-7 are B.  All eight are taken, so LOAD comes
    // from BIDIR pin 0: connect it to VCC through one tick of the clock.

    wire       serial_a, serial_b, serial_sum;
    wire [3:0] sum;
    wire [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    serial_adder i_serial_adder (
        .clk         ( clk          ),
        .load        ( uio_in [0]   ),
        .a           ( ui_in  [3:0] ),
        .b           ( ui_in  [7:4] ),
        .serial_a    ( serial_a     ),
        .serial_b    ( serial_b     ),
        .serial_sum  ( serial_sum   ),
        .sum         ( sum          ),
        .abcdefg_a   ( abcdefg_a    ),
        .abcdefg_b   ( abcdefg_b    ),
        .abcdefg_sum ( abcdefg_sum  )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g; the
    // dot is the sum bit on its way

    assign uo_out  = { serial_sum, abcdefg_sum [0], abcdefg_sum [1], abcdefg_sum [2], abcdefg_sum [3],
                       abcdefg_sum [4], abcdefg_sum [5], abcdefg_sum [6] };

    // The deserializer and the streams of A and B on BIDIR pins 2-7

    assign uio_out = { sum, serial_b, serial_a, 2'b0 };
    assign uio_oe  = 8'b1111_1100;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, rst_n, uio_in [7:1], abcdefg_a, abcdefg_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>_serial_adder
  source_files:
    - "project.v"
    - "serial_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.4 of the Habr article about the electronics course, as the task for the third group. Pictures 12 to 14 of “Teaching children electronics is due for a major redesign” show the adder demos of 11-year-old Matvei and the diagram of the bit-serial adder. The chips are described in the Texas Instruments datasheets for the CD4014B, CD4015B and CD4511B and the STMicroelectronics datasheet for the HCF4032B.