Breadboard exercises/ B9EnglishРусский

Exercise B9 · basics · adder

4-bit adder with three displays

Eight buttons give two numbers, A and B, from 0 to 15. A 4008 adds them, and three 4511 decoders show A, B and the sum on three seven-segment displays; the carry lights the decimal point. It is the largest of the Basics exercises: five chips, three displays and two breadboards.

4 chips + 3 displays4008 adder, three 4511 decoders
59 + 24 wireson and between the boards + leads to the buttons
22 resistors560 Ω, seven per display and one for the dot
256 pairsA and B from 0 to 15; the sum display shows 0–9

1What it is and why it matters

What it does. Buttons A4–A1 give the number A, buttons B4–B1 the number B, each in binary with weights 8, 4, 2 and 1. The 4008 adds them: its four sum outputs S4–S1 and the carry make a 5-bit result from 0 to 30. Two 4511s show A and B, a third shows the four sum bits, and the carry, the fifth bit of the sum, lights the decimal point of the sum display. This is the last exercise of the Basics list in “An improved way of teaching school kids digital electronics”: “Demo 4008, a 4-bit combinational adder. Use three 4511 chips to display the arguments and the result on three seven-segment displays.” Matvei, an 11-year-old student, built this kind of display adder, first on a breadboard and then soldered (pictures 12 and 13 of “Teaching children electronics is due for a major redesign”); lesson 1.2 of the Habr article describes the adder with buttons and the displays.

How it works. The 4008 contains four full adders, one per bit. Each adds one bit of A, one bit of B and the carry from the bit below, gives one bit of the sum and passes its own carry up. The carry out of the top bit is the fifth bit of the result. The adder is combinational: change a button and the sum follows, with no clock.

The 4511 shows only 0–9. A sum from 10 to 15, or from 26 to 30, blanks the sum display, and a sum of 16 or more also lights the dot: 16 shows as 0 with the dot. Keep A + B at 9 or below to read every sum, or use the hexadecimal decoder that Habr lesson 1.2 suggests instead of the 4511 (a task in section 15).

Where it is used. Adders are the heart of the arithmetic logic unit of every processor, and they compute addresses, increment program counters and count. A 4-bit adder with carry in and carry out is a slice: chain two for 8 bits (exercise M4), sixteen for 64. Showing binary values as digits is the job of every display controller.

Why learn it.

2The key element: the full adder

A full adder from gates

abcsc′

s = a XOR b XOR c; the carry c′ is 1 when at least two of a, b and c are 1.

Its truth table

abcsc′
00000
00110
01010
01101
10010
10101
11001
11111

a + b + c in binary: c′ is the twos bit, s the ones bit.

4008: four of them

4008A1A2A3A4B1B2B3B4CINS1S2S3S4COUT

A1–A4 and B1–B4 are the numbers, CIN the carry into bit 1, S1–S4 the sum, COUT the carry out.

A few sums

ABA + BCOUT S4 S3 S2 S1the sum display shows
3470 01117
95140 1110blank
88161 00000 and the dot
1515301 1110blank and the dot

The decoder

Each display has its own 4511, the decoder of exercise B7, with the same truth table: digits 0–9, blank for 10–15, 6 without the top bar and 9 without the bottom one.

codeD C B Aabcdefg
00 0 0 011111100
10 0 0 101100001
20 0 1 011011012
30 0 1 111110013
40 1 0 001100114
50 1 0 110110115
60 1 1 000111116
70 1 1 111100007
81 0 0 011111118
91 0 0 111100119
101 0 1 00000000blank
111 0 1 10000000blank
121 1 0 00000000blank
131 1 0 10000000blank
141 1 1 00000000blank
151 1 1 10000000blank

Waveforms

A1501234567891011121314150123B456sum356789101112131415012346789carry
From the simulation of tb.sv in Icarus Verilog, twenty of the 256 pairs. B is 5 while A goes from 0 to 15: the sum goes 5, 6, … 15, then wraps to 0 when A reaches 11 and the carry becomes 1, because 11 + 5 = 16. · SVG · PNG

3Parts

PartIn this circuitQtyPriceSupplier
CD40084-bit full adder1$1.00leeselectronic.com
CD4511BCD-to-7-segment decoders for A, B and the sum3$0.65Elexp (elexp.com)
7-segment display, common cathodeA, B and the sum (the Basics kit has two green and one red)3$0.90elexp.com
Resistor, 560 Ω, ½ Wseven per display and one for the decimal point22$0.04elexp.com
Button modulesA1-A4 and B1-B4: eight modules from the two packs of five8$0.66 / packaliexpress.us
Breadboard, 830 pointstwo boards, one above the other2$3.00amazon.com
Breadboard power module5 V on the rails of the bottom board1$1.00aliexpress.us
USB-to-DC cable, 9 Vpowers the module from a USB charger1$1.17aliexpress.us
Male-male Dupont jumpers59 wires on and between the boards59$1.30 / packaliexpress.us
Male-female Dupont jumpers24 leads to the buttons24$1.30 / packaliexpress.us

Prices from the LSSK components sheet; all of these parts are in the Basics kit, which has two boards, three 4511s, three displays, 25 resistors and two packs of five buttons for this exercise.

The dot is dimmer. The 4511 outputs can give up to 25 mA, but the carry output of the 4008 is an ordinary CMOS output that gives a milliamp or two, so the decimal point lights more weakly than the segments. It is still easy to see.

4The chips, pin by pin

All the chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every CMOS input must be connected to something; here CARRY IN of the 4008 and the control inputs of the 4511s are tied to the rails. The drawings show the chips from above, notch on the left, pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.

4008: 4-bit full adder

1A42B33A34B25A26B17A18VSS9CIN10S111S212S313S414COUT15B416VDD40084-bit adder
CD4008B. The inputs of A and B alternate along the bottom row; B4 is on the top row. · SVG · PNG

Polarity. All inputs and outputs are active high: 1 means +5 V. A1 and B1 are the lowest bits. CARRY IN is added to the sum; tied to GND here. The 4008 computes CARRY OUT with extra look-ahead logic, so it does not wait for the carry to ripple through all four bits; the result is the same.

On the breadboard the 4008 sits turned around, notch on the right: its inputs A1–A4 and B1–B3 are then on the top row, next to the buttons, and the sum on the bottom row, next to the board below.

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

4511: BCD-to-7-segment latch, decoder and driver

1B2C3LT4BI5LE6D7A8VSS9e10d11c12b13a14g15f16VDD4511BCD → 7 segments
CD4511B, the same chip as in exercise B7. · SVG · PNG

The inputs A–D take four bits, A the lowest. ~LT and ~BI are active low and held at +5V, LE at GND. The table below shows the 4511 of number A; the 4511s of B and of the sum are wired the same way with their own nets. All three sit turned around, notch on the right, like the 4511 of exercise B7.

PinNameDirectionFunctionIn this circuit
1BinputBCD input, bit 1 (weight 2)A2
2CinputBCD input, bit 2 (weight 4)A3
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)A4
7AinputBCD input, bit 0 (weight 1)A1
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

Common-cathode display

abcdefgDP1e2d3CC4c5DP6b7a8CC9f10gcommon cathode:pins 3 and 8 to GND
The display seen from above, decimal point at the bottom right. Pins 3 and 8 are the common cathode. · SVG · PNG

The same display as in exercise B7; if you have not done it, explore one display with a resistor and a probe wire first, as in its step 1. Here the decimal point of the sum display is used too: pin 5 gets the carry through a resistor.

5Schematic 1: adders and decoders

AB444a3b3FA3s3c3a2b2FA2s2c2a1b1FA1s1c1a0b0FA0s00c0carry c444sum4511BCD → 77 × 560 ΩA4511BCD → 77 × 560 ΩB4511BCD → 77 × 560 ΩA + Bcarry → the dot of the sum displayexample: A = 9, B = 8, sum 17 = 1 0001: the sum display shows 1, and the carry lights the dot4008: FOUR FULL ADDERS
The circuit without chips: four full adders in a chain, as in the 4008, and three decoders with their displays. Thick lines are buses of four bits. This is what adder_display.sv describes; the resistors and the displays are outside the logic. · SVG · PNG

Read the adder from right to left, the way you add on paper: FA0 adds the lowest bits with a carry of 0, its carry c1 goes to FA1, and so on; c4 from FA3 is the carry of the whole sum. The buses A, B and the sum each go to a decoder.

6Schematic 2: connections between the chips

12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 A12345678910111213141516A4B3A3B2A2B1A1CINS1S2S3S4COUTB440084-bit adder12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 Be1d2CC3c4DP5b6a7CC8f9g10Ae1d2CC3c4DP5b6a7CC8f9g10B12345678910111213141516BCLTBILEDAedcbagf4511BCD → 7 segments4511 Se1d2CC3c4DP5b6a7CC8f9g10A + BA.aA.bA.cA.dA.eA.fA.gB.aB.bB.cB.dB.eB.fB.gS.aS.bS.cS.dS.eS.fS.gA1A2A3A4B1B2B3B4S2S3COA4A3A2A1B4B3B2B1+5V
The same circuit as chips, pin by pin, one row per breadboard. The chips are drawn turned around, notch on the right, as they sit on the boards. Each segment pin of a display has its resistor drawn on it. The sum bits go from the bottom of the 4008 down to the 4511 of the sum on the lower board. · SVG · PNG

Every connection of the circuit, net by net. A segment has two nets: A.a from the 4511 of number A to its resistor, A.a′ from the resistor to the display pin. adder_display_chips.sv in section 10 is written from the same list.

NetFromToWire
A4button A4, OUT4511 A 6 (D), 4008 1 (A4)M-F, M-M
A3button A3, OUT4511 A 2 (C), 4008 3 (A3)M-F, M-M
A2button A2, OUT4511 A 1 (B), 4008 5 (A2)M-F, M-M
A1button A1, OUT4511 A 7 (A), 4008 7 (A1)M-F, M-M
B4button B4, OUT4511 B 6 (D), 4008 15 (B4)M-F, M-M
B3button B3, OUT4511 B 2 (C), 4008 2 (B3)M-F, M-M
B2button B2, OUT4511 B 1 (B), 4008 4 (B2)M-F, M-M
B1button B1, OUT4511 B 7 (A), 4008 6 (B1)M-F, M-M
S44008 13 (S4)4511 S 6 (D)M-M
S34008 12 (S3)4511 S 2 (C)M-M
S24008 11 (S2)4511 S 1 (B)M-M
S14008 10 (S1)4511 S 7 (A)M-M
CO4008 14 (COUT)RDPM-M
DP′—RDP, A + B 5 (DP)
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 (loose jumper)
A.e4511 A 9 (e)ReAM-M (loose jumper)
A.f4511 A 15 (f)RfAM-M
A.g4511 A 14 (g)RgAM-M
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 (loose jumper)
B.b4511 B 12 (b)RbBM-M
B.c4511 B 11 (c)RcBM-M (loose jumper)
B.d4511 B 10 (d)RdBM-M (loose jumper)
B.e4511 B 9 (e)ReBM-M
B.f4511 B 15 (f)RfBM-M
B.g4511 B 14 (g)RgBM-M
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
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 (loose jumper)
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), 4008 16 (VDD), 4511 B 16 (VDD), 4511 S 16 (VDD), 4511 A 4 (BI), 4511 A 3 (LT), 4511 B 4 (BI), 4511 B 3 (LT), 4511 S 4 (BI), 4511 S 3 (LT)
GNDGND4511 A 8 (VSS), 4008 8 (VSS), 4511 B 8 (VSS), 4511 S 8 (VSS), A 3 (CC), 4511 A 5 (LE), 4008 9 (CIN), 4511 B 5 (LE), B 3 (CC), 4511 S 5 (LE), A + B 3 (CC)
not connectedA 5 (DP), A 8 (CC), B 5 (DP), B 8 (CC), A + B 8 (CC)

7Schematic 3: two breadboards

−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 1−+−+123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263abcdefghijBoard 25V5VPOWER MODULE · 5V9V USB-DC4511 A40084511 B4511 SABA + BA.aA.bA.cA.dA.eA.fA.gB.aB.bB.cB.dB.eB.fB.gS.aS.bS.cS.dS.eS.fS.gA1A2A3A4B1B2B3B4S2S3S4CO+OUT−A4+OUT−A3+OUT−A2+OUT−A1+OUT−B4+OUT−B3+OUT−B2+OUT−B1
Every wire and resistor in its hole, on two boards one above the other. The top board carries display A, the 4511 of A, the 4008, the 4511 of B and display B; the eight buttons hang above it. The bottom board carries the 4511 and the display of the sum and the power module; two wires bring +5V and GND up to the top board, and two wires at the right end join its top and bottom rails. Seven segment wires are drawn as loose jumpers, because they cross over a display or other wires. · SVG · PNG
A4A3A2A1B4B3B2B1S4S3S2S1CODP′A.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 exercise B7 first, or at least its step 1: it teaches the display.
  2. Put the two boards one above the other. Plug the power module into the left end of the bottom board.
  3. Top board: insert display A with pin 1 in 6h, the 4511 of A with its notch on the right and pin 1 in 25e, the 4008 with its notch on the right and pin 1 in 35e, the 4511 of B with its notch on the right and pin 1 in 45e, and display B with pin 1 in 53h.
  4. Bottom board: the 4511 of the sum with its notch on the right and pin 1 in 35e, and display S with pin 1 in 43h.
  5. The rails: the two wires from the bottom board to the top board, and the two at the right end of the top board. Then power and the fixed inputs of every chip, and the common cathode of every display.
  6. The 22 resistors, as in the checklist. Then the segment wires of each display, one display at a time; test each display before you go on (see below).
  7. The data wires: A1–A4 and B1–B4 from the 4511s to the 4008, the sum S1–S4 from the 4008 down to the 4511 of the sum, and the carry down to the resistor of the decimal point.
  8. With the power off, the eight buttons above the top board. Connect their − pins first.
  9. Turn the power on.

Wiring checklist

·

#NetFromTo
Rails of the second board (from the board with the power module) · 4 wires
1+5Vboard 2: top + rail, column 8board 1: bottom + rail, column 8
2GNDboard 2: top − rail, column 10board 1: bottom − rail, column 10
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 −) · 8 wires
5+5Vboard 1: 25j (4511 A pin 16, VDD)board 1: bottom + rail, column 25
6GNDboard 1: 18a (4511 A pin 8, VSS)board 1: top − rail, column 18
7+5Vboard 1: 35j (4008 pin 16, VDD)board 1: bottom + rail, column 35
8GNDboard 1: 28a (4008 pin 8, VSS)board 1: top − rail, column 28
9+5Vboard 1: 45j (4511 B pin 16, VDD)board 1: bottom + rail, column 45
10GNDboard 1: 38a (4511 B pin 8, VSS)board 1: top − rail, column 38
11+5Vboard 2: 35j (4511 S pin 16, VDD)board 2: bottom + rail, column 35
12GNDboard 2: 28a (4511 S pin 8, VSS)board 2: top − rail, column 28
Fixed inputs: CMOS inputs must not float · 13 wires
13GNDboard 1: 8j (display A pin 3, CC)board 1: bottom − rail, column 8
14GNDboard 1: 21a (4511 A pin 5, LE)board 1: top − rail, column 21
15+5Vboard 1: 22a (4511 A pin 4, BI)board 1: top + rail, column 22
16+5Vboard 1: 23a (4511 A pin 3, LT)board 1: top + rail, column 23
17GNDboard 1: 28j (4008 pin 9, CIN)board 1: bottom − rail, column 28
18GNDboard 1: 41a (4511 B pin 5, LE)board 1: top − rail, column 41
19+5Vboard 1: 42a (4511 B pin 4, BI)board 1: top + rail, column 42
20+5Vboard 1: 43a (4511 B pin 3, LT)board 1: top + rail, column 43
21GNDboard 1: 55j (display B pin 3, CC)board 1: bottom − rail, column 55
22GNDboard 2: 31a (4511 S pin 5, LE)board 2: top − rail, column 31
23+5Vboard 2: 32a (4511 S pin 4, BI)board 2: top + rail, column 32
24+5Vboard 2: 33a (4511 S pin 3, LT)board 2: top + rail, column 33
25GNDboard 2: 45j (display A + B pin 3, CC)board 2: bottom − rail, column 45
Resistors, 560 Ω (not wires: put each resistor in these two holes) · 22 resistors
RbA560 Ωboard 1: 10bboard 1: 14b
RaA560 Ωboard 1: 9aboard 1: 13a
RfA560 Ωboard 1: 7cboard 1: 12c
RcA560 Ωboard 1: 9jboard 1: 13j
RdA560 Ωboard 1: 7iboard 1: 12i
RgA560 Ωboard 1: 6aboard 1: 2a
ReA560 Ωboard 1: 6jboard 1: 2j
RgB560 Ωboard 1: 53bboard 1: 49b
RfB560 Ωboard 1: 54aboard 1: 50a
RaB560 Ωboard 1: 56cboard 1: 51c
ReB560 Ωboard 1: 53jboard 1: 49j
RdB560 Ωboard 1: 54iboard 1: 50i
RbB560 Ωboard 1: 57aboard 1: 61a
RcB560 Ωboard 1: 56jboard 1: 60j
RgS560 Ωboard 2: 43bboard 2: 39b
RfS560 Ωboard 2: 44aboard 2: 40a
RaS560 Ωboard 2: 46cboard 2: 41c
ReS560 Ωboard 2: 43jboard 2: 39j
RdS560 Ωboard 2: 44iboard 2: 40i
RbS560 Ωboard 2: 47aboard 2: 51a
RcS560 Ωboard 2: 46jboard 2: 50j
RDP560 Ωboard 2: 47iboard 2: 51i
Signal wires · 34 wires
26A.aboard 1: 22g (4511 A pin 13, a)board 1: 13d (RaA)
27A.bboard 1: 21i (4511 A pin 12, b)board 1: 14e (RbA)
28A.cboard 1: 20h (4511 A pin 11, c)board 1: 13h (RcA)
29A.d (loose jumper)board 1: 19h (4511 A pin 10, d)board 1: 12h (RdA)
30A.e (loose jumper)board 1: 18h (4511 A pin 9, e)board 1: 2h (ReA)
31A.fboard 1: 24g (4511 A pin 15, f)board 1: 12d (RfA)
32A.gboard 1: 23j (4511 A pin 14, g)board 1: 2d (RgA)
33B.a (loose jumper)board 1: 42h (4511 B pin 13, a)board 1: 51d (RaB)
34B.bboard 1: 41j (4511 B pin 12, b)board 1: 61e (RbB)
35B.c (loose jumper)board 1: 40h (4511 B pin 11, c)board 1: 60h (RcB)
36B.d (loose jumper)board 1: 39h (4511 B pin 10, d)board 1: 50h (RdB)
37B.eboard 1: 38g (4511 B pin 9, e)board 1: 49f (ReB)
38B.fboard 1: 44g (4511 B pin 15, f)board 1: 50d (RfB)
39B.gboard 1: 43i (4511 B pin 14, g)board 1: 49c (RgB)
40S.aboard 2: 32g (4511 S pin 13, a)board 2: 41e (RaS)
41S.b (loose jumper)board 2: 31h (4511 S pin 12, b)board 2: 51c (RbS)
42S.cboard 2: 30j (4511 S pin 11, c)board 2: 50h (RcS)
43S.dboard 2: 29h (4511 S pin 10, d)board 2: 40h (RdS)
44S.eboard 2: 28i (4511 S pin 9, e)board 2: 39i (ReS)
45S.f (loose jumper)board 2: 34h (4511 S pin 15, f)board 2: 40c (RfS)
46S.gboard 2: 33g (4511 S pin 14, g)board 2: 39d (RgS)
47A1board 1: 19b (4511 A pin 7, A)board 1: 29b (4008 pin 7, A1)
48A2board 1: 25d (4511 A pin 1, B)board 1: 31d (4008 pin 5, A2)
49A3board 1: 24b (4511 A pin 2, C)board 1: 33a (4008 pin 3, A3)
50A4board 1: 20c (4511 A pin 6, D)board 1: 35c (4008 pin 1, A4)
51B1board 1: 39b (4511 B pin 7, A)board 1: 30b (4008 pin 6, B1)
52B2board 1: 45d (4511 B pin 1, B)board 1: 32d (4008 pin 4, B2)
53B3board 1: 44b (4511 B pin 2, C)board 1: 34a (4008 pin 2, B3)
54B4board 1: 40c (4511 B pin 6, D)board 1: 34g (4008 pin 15, B4)
55S1board 1: 29g (4008 pin 10, S1)board 2: 29d (4511 S pin 7, A)
56S2board 1: 30g (4008 pin 11, S2)board 2: 35c (4511 S pin 1, B)
57S3board 1: 31g (4008 pin 12, S3)board 2: 34d (4511 S pin 2, C)
58S4board 1: 32h (4008 pin 13, S4)board 2: 30d (4511 S pin 6, D)
59COboard 1: 33h (4008 pin 14, COUT)board 2: 51f (RDP)
Modules on male-female jumpers · 24 wires
60+5Vbutton A4, +board 1: top + rail, column 10
61A4button A4, OUTboard 1: 20a (4511 A pin 6, D)
62GNDbutton A4, −board 1: top − rail, column 12
63+5Vbutton A3, +board 1: top + rail, column 15
64A3button A3, OUTboard 1: 24a (4511 A pin 2, C)
65GNDbutton A3, −board 1: top − rail, column 17
66+5Vbutton A2, +board 1: top + rail, column 20
67A2button A2, OUTboard 1: 25a (4511 A pin 1, B)
68GNDbutton A2, −board 1: top − rail, column 22
69+5Vbutton A1, +board 1: top + rail, column 25
70A1button A1, OUTboard 1: 29a (4008 pin 7, A1)
71GNDbutton A1, −board 1: top − rail, column 27
72+5Vbutton B4, +board 1: top + rail, column 33
73B4button B4, OUTboard 1: 40a (4511 B pin 6, D)
74GNDbutton B4, −board 1: top − rail, column 35
75+5Vbutton B3, +board 1: top + rail, column 38
76B3button B3, OUTboard 1: 44a (4511 B pin 2, C)
77GNDbutton B3, −board 1: top − rail, column 40
78+5Vbutton B2, +board 1: top + rail, column 44
79B2button B2, OUTboard 1: 45a (4511 B pin 1, B)
80GNDbutton B2, −board 1: top − rail, column 45
81+5Vbutton B1, +board 1: top + rail, column 48
82B1button B1, OUTboard 1: 39a (4511 B pin 7, A)
83GNDbutton B1, −board 1: top − rail, column 50

Test it

If it does not work: test the displays first; a wrong digit on display A or B is a wiring fault in that display's block, as in exercise B7. If A and B are right but the sum is wrong, check the A and B wires from the 4511s to the 4008: the inputs of the 4008 alternate between A and B. If nothing on the top board works, check the rail wires from the bottom board.

8SystemVerilog: the generic design

adder_display.sv describes schematic 1. The for loop with a genvar builds the four full adders, one copy per bit; the function decode is the decoder of exercise B7, used three times. It is synthesizable.

adder_display.svfile
// Exercise B9: a 4-bit adder whose two numbers and sum are shown on three
// 7-segment displays.
//
// Generic version that matches schematic 1: four full adders in a chain
// (a ripple-carry adder) and three BCD-to-7-segment decoders that draw the
// digits like the 4511, not tied to any chip.  Synthesizable.

module adder_display
(
    input  logic [3:0] a,            // The first number, from buttons A4-A1
    input  logic [3:0] b,            // The second number, from buttons B4-B1

    output logic [3:0] sum,          // a + b without the carry
    output logic       carry,        // The fifth bit of the sum: 1 when a + b > 15

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

    //------------------------------------------------------------------------
    // Four full adders.  Each adds one bit of a, one bit of b and the carry
    // from the bit below, and passes its own carry to the bit above.

    logic [4:0] c;

    assign c [0] = 1'b0;  // The carry into bit 0, like CARRY IN of the 4008 tied to GND

    for (genvar i = 0; i < 4; i ++)
    begin : full_adder
        assign sum [i]   = a [i] ^ b [i] ^ c [i];
        assign c [i + 1] = a [i] & b [i] | (a [i] ^ b [i]) & c [i];
    end

    assign carry = c [4];

    //------------------------------------------------------------------------
    // Three decoders.  Like the 4511, codes 10-15 blank the display, so the
    // sum shows only up to 9.

    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

The four full adders could also be written assign { carry, sum } = a + b;: synthesis knows how to build an adder. Writing them out shows what is inside the 4008, and the testbench checks both forms against the same reference.

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. Both models are synthesizable and leave out the power pins.

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

10SystemVerilog: the chip-level design

adder_display_chips.sv connects the four chip models exactly as schematic 2 and the breadboards connect the chips, with the same ports as adder_display.sv. The displays and their resistors are not in the design: its outputs are the outputs of the 4511s and of the 4008. 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.

adder_display_chips.svfile
// Exercise B9, chip-level version: the chips and wires of schematic 2 and
// of the breadboards.  Same ports as adder_display.sv.  The displays and
// their resistors are not in the design: its outputs are the outputs of
// the 4511s and of the 4008.
//
// Generated by tools/bb_lib.py from the checked breadboard wire list;
// change the layout in tools/bb_b9.py, not this file.  Synthesizable.

module adder_display_chips
(
    input  logic [3:0] a,
    input  logic [3:0] b,
    output logic [3:0] sum,
    output logic       carry,
    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;

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

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

    cd4511 i_ua
    (
        .bcd_b        ( a [1]         ),  // Pin 1 <- button A2
        .bcd_c        ( a [2]         ),  // Pin 2 <- button A3
        .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 A4
        .bcd_a        ( a [0]         ),  // Pin 7 <- button A1
        .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
    );

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

    // U4008: CD4008 4-bit full adder, pin 1 in column 28 of board 1

    cd4008 i_u4008
    (
        .a4        ( a [3]   ),  // Pin 1 <- button A4
        .b3        ( b [2]   ),  // Pin 2 <- button B3
        .a3        ( a [2]   ),  // Pin 3 <- button A3
        .b2        ( b [1]   ),  // Pin 4 <- button B2
        .a2        ( a [1]   ),  // Pin 5 <- button A2
        .b1        ( b [0]   ),  // Pin 6 <- button B1
        .a1        ( a [0]   ),  // Pin 7 <- button A1
        .carry_in  ( gnd     ),  // Pin 9 <- GND
        .s1        ( sum [0] ),  // Pin 10 -> S1
        .s2        ( sum [1] ),  // Pin 11 -> S2
        .s3        ( sum [2] ),  // Pin 12 -> S3
        .s4        ( sum [3] ),  // Pin 13 -> S4
        .carry_out ( carry   ),  // Pin 14 -> CO
        .b4        ( b [3]   )   // Pin 15 <- button B4
    );

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

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

    cd4511 i_ub
    (
        .bcd_b        ( b [1]         ),  // Pin 1 <- button B2
        .bcd_c        ( b [2]         ),  // Pin 2 <- button B3
        .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 B4
        .bcd_a        ( b [0]         ),  // Pin 7 <- button B1
        .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
    );

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

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

    cd4511 i_us
    (
        .bcd_b        ( sum [1]         ),  // Pin 1 <- U4008 pin 11 (S2)
        .bcd_c        ( sum [2]         ),  // Pin 2 <- U4008 pin 12 (S3)
        .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 <- U4008 pin 13 (S4)
        .bcd_a        ( sum [0]         ),  // Pin 7 <- U4008 pin 10 (S1)
        .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 for all 256 pairs of A and B and compares them with a reference model: the sum computed as an integer and the truth table of the 4511 for the three displays. The design has no clock, but the testbench has one, like the testbenches of Basics-Graphics-Music: it sets A and B right after a rising edge and checks the outputs at the next one. It prints only the pairs with B = 5; the summary counts all of them.

tb.sv174 lines · show the codefile
// Testbench for exercise B9.
//
// Runs the generic design (adder_display.sv) and the chip-level design
// (adder_display_chips.sv) side by side for all 256 pairs of A and B and
// compares both with a reference model: the sum as an integer and the
// truth table of the 4511 for the three displays.  Works with Icarus
// Verilog, Verilator (--binary) and Questa.
//
// The design has no clock, but like the testbenches of Basics-Graphics-Music
// this one drives and checks signals only at rising edges of its own clock:
// it sets A and B after one edge and checks the outputs at the next.  It
// prints only the pairs with B = 5; the summary counts all of them.

// 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 [3:0] a, b;

    wire  [3:0] sum,         sum_chips;
    wire        carry,       carry_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;

    adder_display i_generic
    (
        .a           ( a           ),
        .b           ( b           ),
        .sum         ( sum         ),
        .carry       ( carry       ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

    adder_display_chips i_chips
    (
        .a           ( a                 ),
        .b           ( b                 ),
        .sum         ( sum_chips         ),
        .carry       ( carry_chips       ),
        .abcdefg_a   ( abcdefg_a_chips   ),
        .abcdefg_b   ( abcdefg_b_chips   ),
        .abcdefg_sum ( abcdefg_sum_chips )
    );

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

    initial
    begin
        clk = '0;

        forever
            # 5 clk = ~ clk;
    end

    //------------------------------------------------------------------------
    // Reference model

    function automatic logic [6:0] segments (input int code);
        case (code)
        0:       return 7'b111_1110;
        1:       return 7'b011_0000;
        2:       return 7'b110_1101;
        3:       return 7'b111_1001;
        4:       return 7'b011_0011;
        5:       return 7'b101_1011;
        6:       return 7'b001_1111;
        7:       return 7'b111_0000;
        8:       return 7'b111_1111;
        9:       return 7'b111_0011;
        default: return 7'b000_0000;
        endcase
    endfunction

    wire [4:0] total = 5' (int' (a) + int' (b));  // 0 to 30

    wire ok_generic =    { carry, sum }       === total
                      && abcdefg_a            === segments (int' (a))
                      && abcdefg_b            === segments (int' (b))
                      && abcdefg_sum          === segments (int' (total [3:0]));

    wire ok_chips   =    { carry_chips, sum_chips } === total
                      && abcdefg_a_chips          === segments (int' (a))
                      && abcdefg_b_chips          === segments (int' (b))
                      && abcdefg_sum_chips        === segments (int' (total [3:0]));

    // The digit shown on a display, for the trace

    function automatic string shown (input logic [6:0] s);
        for (int k = 0; k < 10; k ++)
            if (s === segments (k))
                return $sformatf ("%0d", k);

        return s === 7'b0 ? " " : "?";
    endfunction

    //------------------------------------------------------------------------
    // Checking at every rising clock edge

    // Initial values are fine in a testbench
    // verilator lint_off PROCASSINIT

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

    int n_checks = 0;
    int n_errors = 0;
    int n_pairs  = 0;

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

            if (b == 4'd5 || ! ok_generic || ! ok_chips)
                $display ("%2d + %2d = %2d   %b %b   %s    %s    %s     %b    %s  %s",
                    a, b, total, carry, sum, shown (abcdefg_a), shown (abcdefg_b), shown (abcdefg_sum),
                    carry, ok_generic ? "ok     " : "WRONG  ", ok_chips ? "ok   " : "WRONG");
        end

    //------------------------------------------------------------------------
    // Driving A and B: all 256 pairs

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

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

        $display ("  A +  B =  sum  C S4-S1  A    B    sum  dot   generic  chips");

        checking <= '1;

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

        // Stop checking one clock edge before $finish, so that every
        // simulator reports the same number of checks

        done <= '1;
        @ (posedge clk);

        $finish;
    end

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

    final
    begin
        if (n_errors == 0 && n_pairs == 256)
            $display ("PASS: %0d checks, no errors", n_checks);
        else
            $display ("FAIL: %0d errors in %0d checks, %0d pairs", n_errors, n_checks, n_pairs);
    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 B9: 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 adder_display.sv adder_display_chips.sv cd4008.sv cd4511.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 B9: 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 adder_display.sv adder_display_chips.sv cd4008.sv cd4511.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 B9: 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 adder_display.sv adder_display_chips.sv cd4008.sv cd4511.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; a blank in the sum column is a sum from 10 to 15:

VCD info: dumpfile tb.vcd opened for output.
  A +  B =  sum  C S4-S1  A    B    sum  dot   generic  chips
 0 +  5 =  5   0 0101   0    5    5     0    ok       ok   
 1 +  5 =  6   0 0110   1    5    6     0    ok       ok   
 2 +  5 =  7   0 0111   2    5    7     0    ok       ok   
 3 +  5 =  8   0 1000   3    5    8     0    ok       ok   
 4 +  5 =  9   0 1001   4    5    9     0    ok       ok   
 5 +  5 = 10   0 1010   5    5          0    ok       ok   
 6 +  5 = 11   0 1011   6    5          0    ok       ok   
 7 +  5 = 12   0 1100   7    5          0    ok       ok   
 8 +  5 = 13   0 1101   8    5          0    ok       ok   
 9 +  5 = 14   0 1110   9    5          0    ok       ok   
10 +  5 = 15   0 1111        5          0    ok       ok   
11 +  5 = 16   1 0000        5    0     1    ok       ok   
12 +  5 = 17   1 0001        5    1     1    ok       ok   
13 +  5 = 18   1 0010        5    2     1    ok       ok   
14 +  5 = 19   1 0011        5    3     1    ok       ok   
15 +  5 = 20   1 0100        5    4     1    ok       ok   
tb.sv:161: $finish called at 2565000 (1ps)
PASS: 512 checks, no errors

To see that the testbench catches mistakes, break something and run it again: drop the carry term from the full adder in adder_display.sv (c [i + 1] = a [i] & b [i];), or swap two sum wires in adder_display_chips.sv. The script reports FAIL and prints every wrong pair.

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, reset, keys, switches, LEDs, and a multi-digit 7-segment display.

  1. Copy a lab, for example labs/1_basics/1_05_7seven_segment_letter, to a new directory.
  2. Replace its lab_top.sv with bgm_lab/lab_top.sv from this exercise and add adder_display.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash; the first run asks which board you have.

Switches 3–0 are A, switches 7–4 are B (on a board without switches, change the code to use keys). The three rightmost digits show A, B and the sum, with the carry on the dot of the sum. The display of an FPGA board is multiplexed: it lights one digit at a time, and a counter on clk chooses the digit, quickly enough that all three seem lit. That counter does not make a clock; it only chooses the digit.

bgm_lab/lab_top.sv121 lines · show the codefile
// Exercise B9 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a lab, for example labs/1_basics/1_05_7seven_segment_letter, to a
// new directory, replace its lab_top.sv with this file and add
// adder_display.sv.  For the chip-level design add adder_display_chips.sv,
// cd4008.sv and cd4511.sv instead, and change the module name below.
//
// Switches 3-0 are A, switches 7-4 are B (on a board without switches use
// keys).  The display shows A, B and the sum on its three rightmost digits,
// the carry on the dot of the sum, and the LEDs show the carry and the sum.
// The digits take turns: a counter on clk picks one at a time, quickly
// enough that all three seem lit.  The counter does not make a clock; it
// only chooses the digit.

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

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

    wire  [7:0] switches = 8' (sw);
    logic [3:0] sum;
    logic       carry;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    adder_display i_adder_display
    (
        .a           ( switches [3:0] ),
        .b           ( switches [7:4] ),
        .sum         ( sum            ),
        .carry       ( carry          ),
        .abcdefg_a   ( abcdefg_a      ),
        .abcdefg_b   ( abcdefg_b      ),
        .abcdefg_sum ( abcdefg_sum    )
    );

    assign led = w_led' ({ carry, sum });

    //------------------------------------------------------------------------
    // Three digits on a dynamic display: one at a time, about 1 ms each

    logic [17:0] refresh;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            refresh <= '0;
        else
            refresh <= refresh + 1'd1;

    always_comb
        case (refresh [17:16])
        2'd0:    begin abcdefgh = { abcdefg_sum, carry }; digit = w_digit' (1); end
        2'd1:    begin abcdefgh = { abcdefg_b,   1'b0  }; digit = w_digit' (2); end
        default: begin abcdefgh = { abcdefg_a,   1'b0  }; digit = w_digit' (4); end
        endcase

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 a directory such as hackathon/problems/1_gates_and_muxes.
  2. Replace its hackathon_top.sv with bgm_hackathon/hackathon_top.sv and add adder_display.sv.
  3. Run ./03_synthesize_for_fpga.bash and ./04_configure_fpga.bash.

Keys 3–0 are A and keys 7–4 are B; hold them like the buttons on the breadboard. The three rightmost digits show A, B and the sum, and the screen draws the sum as a large digit with the carry as its dot.

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

// Exercise B9 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its
// hackathon_top.sv with this file and add adder_display.sv.
//
// Keys 3-0 are A, keys 7-4 are B: hold them like the buttons on the
// breadboard.  The three rightmost digits show A, B and the sum, the dot of
// the sum is the carry, the LEDs show the carry and the sum, and the screen
// draws the sum as a large digit.

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

    adder_display i_adder_display
    (
        .a           ( key [3:0]   ),
        .b           ( key [7:4]   ),
        .sum         ( sum         ),
        .carry       ( carry       ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

    assign led = { 3'b0, carry, sum };

    //------------------------------------------------------------------------
    // Three digits on the dynamic display: one at a time, about 2 ms each.
    // The counter only chooses the digit; it does not make a clock.

    logic [17:0] refresh;

    always_ff @ (posedge clock)
        if (reset)
            refresh <= '0;
        else
            refresh <= refresh + 1'd1;

    always_comb
        case (refresh [17:16])
        2'd0:    begin abcdefgh = { abcdefg_sum, carry }; digit = 8'b0000_0001; end
        2'd1:    begin abcdefgh = { abcdefg_b,   1'b0  }; digit = 8'b0000_0010; end
        default: begin abcdefgh = { abcdefg_a,   1'b0  }; digit = 8'b0000_0100; end
        endcase

    //------------------------------------------------------------------------
    // The sum as a large digit on the 480 x 272 screen, the carry as a dot

    wire seg_a = x >= 210 & x < 270 & y >=  36 & y <  56;
    wire seg_g = x >= 210 & x < 270 & y >= 126 & y < 146;
    wire seg_d = x >= 210 & x < 270 & y >= 216 & y < 236;
    wire seg_f = x >= 190 & x < 210 & y >=  56 & y < 126;
    wire seg_b = x >= 270 & x < 290 & y >=  56 & y < 126;
    wire seg_e = x >= 190 & x < 210 & y >= 146 & y < 216;
    wire seg_c = x >= 270 & x < 290 & y >= 146 & y < 216;
    wire dot   = x >= 300 & x < 320 & y >= 216 & y < 236;

    wire [7:0] here = { seg_a, seg_b, seg_c, seg_d, seg_e, seg_f, seg_g, dot };

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

        if (| (here & { abcdefg_sum, carry }))
            green = 63;
        else if (| here)
        begin
            red   = 2;
            green = 8;
            blue  = 2;
        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.

Switches 3–0 are A and switches 7–4 are B, all eight switches of the board. The demo board has one digit, so it shows the sum, with the carry on the dot; read A and B from the switches. The bidirectional pins carry the sum in binary, for LEDs or a logic analyzer on the BIDIR Pmod. The adder needs no clock.

tiny_tapeout/project.v55 lines · show the codefile
/*
 * Exercise B9 on Tiny Tapeout: the adder.  DIP switches 3-0 are A,
 * switches 7-4 are B; the 7-segment display of the demo board shows the
 * sum the way the 4511 does, and the dot shows the carry.
 *
 * Add adder_display.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
);

    wire [3:0] sum;
    wire       carry;
    wire [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    adder_display i_adder_display (
        .a           ( ui_in [3:0] ),
        .b           ( ui_in [7:4] ),
        .sum         ( sum         ),
        .carry       ( carry       ),
        .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,
    // uo_out [7] is the dot.  abcdefg has a at the top, so the bits go in
    // reverse order.  The board has one digit, so A and B are not shown:
    // read them from the switches.  The bidirectional pins show the sum in
    // binary, for a logic analyzer or LEDs on the BIDIR Pmod.

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

    assign uio_out = { 3'b0, carry, sum };
    assign uio_oe  = 8'b0001_1111;

    // The adder needs no clock or reset.
    // List all unused inputs to prevent warnings

    wire _unused = & { ena, clk, rst_n, uio_in, abcdefg_a, abcdefg_b, 1'b0 };

endmodule

In info.yaml list both files and use the module name as the top module:

project:
  top_module: "tt_um_example"   # rename to tt_um_<your GitHub name>_adder_display
  source_files:
    - "project.v"
    - "adder_display.sv"

15Going further

Tasks for students

Files of this exercise

Sources

The exercise is the last of the Basics list in “An improved way of teaching school kids digital electronics using venerable small-scale integration ICs”. Lesson 1.2 of the Habr article about the electronics course describes the 4008 with buttons, the 4511 with a resistor per segment and the carry on the decimal point. Matvei's display adder is in pictures 12 and 13 of “Teaching children electronics is due for a major redesign”. The 4511 is described in the Texas Instruments datasheet for the CD4511B; Texas Instruments no longer publishes the CD4008B datasheet, and its pinout here is from the Intersil CD4008BMS and Motorola MC14008B datasheets, which agree.