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.
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.
- Binary addition with carries, the same as decimal addition on paper, and why 4 bits plus 4 bits need 5.
- A full adder is two XOR, two AND and one OR gate: the gates of exercise B1 doing arithmetic.
- It joins two earlier exercises: the decoder of B7 three times, fed by an arithmetic block.
- In SystemVerilog the whole adder is
{ carry, sum } = a + b;, and synthesis builds the gates.
2The key element: the full adder
A full adder from gates
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
| a | b | c | s | c′ |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
a + b + c in binary: c′ is the twos bit, s the ones bit.
4008: four of them
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
| A | B | A + B | COUT S4 S3 S2 S1 | the sum display shows |
|---|---|---|---|---|
| 3 | 4 | 7 | 0 0111 | 7 |
| 9 | 5 | 14 | 0 1110 | blank |
| 8 | 8 | 16 | 1 0000 | 0 and the dot |
| 15 | 15 | 30 | 1 1110 | blank 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.
| code | D C B A | a | b | c | d | e | f | g | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 0 0 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 0 0 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
| 2 | 0 0 1 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 2 |
| 3 | 0 0 1 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 3 |
| 4 | 0 1 0 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 4 |
| 5 | 0 1 0 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 5 |
| 6 | 0 1 1 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 6 |
| 7 | 0 1 1 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 7 |
| 8 | 1 0 0 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 8 |
| 9 | 1 0 0 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 9 |
| 10 | 1 0 1 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
| 11 | 1 0 1 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
| 12 | 1 1 0 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
| 13 | 1 1 0 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
| 14 | 1 1 1 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
| 15 | 1 1 1 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | blank |
Waveforms
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 · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4008 | 4-bit full adder | 1 | $1.00 | leeselectronic.com |
| CD4511 | BCD-to-7-segment decoders for A, B and the sum | 3 | $0.65 | Elexp (elexp.com) |
| 7-segment display, common cathode | A, B and the sum (the Basics kit has two green and one red) | 3 | $0.90 | elexp.com |
| Resistor, 560 Ω, ½ W | seven per display and one for the decimal point | 22 | $0.04 | elexp.com |
| Button modules | A1-A4 and B1-B4: eight modules from the two packs of five | 8 | $0.66 / pack | aliexpress.us |
| Breadboard, 830 points | two boards, one above the other | 2 | $3.00 | amazon.com |
| Breadboard power module | 5 V on the rails of the bottom board | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Male-male Dupont jumpers | 59 wires on and between the boards | 59 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 24 leads to the buttons | 24 | $1.30 / pack | aliexpress.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.
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
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.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A4 | input | number A, bit 3 (weight 8) | A4 |
| 2 | B3 | input | number B, bit 2 (weight 4) | B3 |
| 3 | A3 | input | number A, bit 2 (weight 4) | A3 |
| 4 | B2 | input | number B, bit 1 (weight 2) | B2 |
| 5 | A2 | input | number A, bit 1 (weight 2) | A2 |
| 6 | B1 | input | number B, bit 0 (weight 1) | B1 |
| 7 | A1 | input | number A, bit 0 (weight 1) | A1 |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | CIN | input | carry in, added to A + B | GND |
| 10 | S1 | output | sum, bit 0 (weight 1) | S1 |
| 11 | S2 | output | sum, bit 1 (weight 2) | S2 |
| 12 | S3 | output | sum, bit 2 (weight 4) | S3 |
| 13 | S4 | output | sum, bit 3 (weight 8) | S4 |
| 14 | COUT | output | carry out, the fifth bit of the sum (weight 16) | CO |
| 15 | B4 | input | number B, bit 3 (weight 8) | B4 |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4511: BCD-to-7-segment latch, decoder and driver
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.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | B | input | BCD input, bit 1 (weight 2) | A2 |
| 2 | C | input | BCD input, bit 2 (weight 4) | A3 |
| 3 | LT | input | lamp test: 0 lights all segments. Active low | +5V |
| 4 | BI | input | blanking: 0 turns all segments off. Active low | +5V |
| 5 | LE | input | latch enable: 0 = follow the inputs, 1 = hold the code | GND |
| 6 | D | input | BCD input, bit 3 (weight 8) | A4 |
| 7 | A | input | BCD input, bit 0 (weight 1) | A1 |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | e | output | segment e (bottom left); 1 = lit; drives up to 25 mA through a resistor | A.e |
| 10 | d | output | segment d (bottom); 1 = lit; drives up to 25 mA through a resistor | A.d |
| 11 | c | output | segment c (bottom right); 1 = lit; drives up to 25 mA through a resistor | A.c |
| 12 | b | output | segment b (top right); 1 = lit; drives up to 25 mA through a resistor | A.b |
| 13 | a | output | segment a (top); 1 = lit; drives up to 25 mA through a resistor | A.a |
| 14 | g | output | segment g (middle); 1 = lit; drives up to 25 mA through a resistor | A.g |
| 15 | f | output | segment f (top left); 1 = lit; drives up to 25 mA through a resistor | A.f |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
Common-cathode display
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
adder_display.sv describes; the
resistors and the displays are outside the logic. · SVG · PNGRead 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
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.
| Net | From | To | Wire |
|---|---|---|---|
| A4 | button A4, OUT | 4511 A 6 (D), 4008 1 (A4) | M-F, M-M |
| A3 | button A3, OUT | 4511 A 2 (C), 4008 3 (A3) | M-F, M-M |
| A2 | button A2, OUT | 4511 A 1 (B), 4008 5 (A2) | M-F, M-M |
| A1 | button A1, OUT | 4511 A 7 (A), 4008 7 (A1) | M-F, M-M |
| B4 | button B4, OUT | 4511 B 6 (D), 4008 15 (B4) | M-F, M-M |
| B3 | button B3, OUT | 4511 B 2 (C), 4008 2 (B3) | M-F, M-M |
| B2 | button B2, OUT | 4511 B 1 (B), 4008 4 (B2) | M-F, M-M |
| B1 | button B1, OUT | 4511 B 7 (A), 4008 6 (B1) | M-F, M-M |
| S4 | 4008 13 (S4) | 4511 S 6 (D) | M-M |
| S3 | 4008 12 (S3) | 4511 S 2 (C) | M-M |
| S2 | 4008 11 (S2) | 4511 S 1 (B) | M-M |
| S1 | 4008 10 (S1) | 4511 S 7 (A) | M-M |
| CO | 4008 14 (COUT) | RDP | M-M |
| DP′ | — | RDP, A + B 5 (DP) | |
| A.a | 4511 A 13 (a) | RaA | M-M |
| A.b | 4511 A 12 (b) | RbA | M-M |
| A.c | 4511 A 11 (c) | RcA | M-M |
| A.d | 4511 A 10 (d) | RdA | M-M (loose jumper) |
| A.e | 4511 A 9 (e) | ReA | M-M (loose jumper) |
| A.f | 4511 A 15 (f) | RfA | M-M |
| A.g | 4511 A 14 (g) | RgA | M-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.a | 4511 B 13 (a) | RaB | M-M (loose jumper) |
| B.b | 4511 B 12 (b) | RbB | M-M |
| B.c | 4511 B 11 (c) | RcB | M-M (loose jumper) |
| B.d | 4511 B 10 (d) | RdB | M-M (loose jumper) |
| B.e | 4511 B 9 (e) | ReB | M-M |
| B.f | 4511 B 15 (f) | RfB | M-M |
| B.g | 4511 B 14 (g) | RgB | M-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.a | 4511 S 13 (a) | RaS | M-M |
| S.b | 4511 S 12 (b) | RbS | M-M (loose jumper) |
| S.c | 4511 S 11 (c) | RcS | M-M |
| S.d | 4511 S 10 (d) | RdS | M-M |
| S.e | 4511 S 9 (e) | ReS | M-M |
| S.f | 4511 S 15 (f) | RfS | M-M (loose jumper) |
| S.g | 4511 S 14 (g) | RgS | M-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 | +5V | 4511 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) | |
| GND | GND | 4511 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 connected | A 5 (DP), A 8 (CC), B 5 (DP), B 8 (CC), A + B 8 (CC) |
7Schematic 3: two breadboards
Build it step by step
- Do exercise B7 first, or at least its step 1: it teaches the display.
- Put the two boards one above the other. Plug the power module into the left end of the bottom board.
- 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.
- 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.
- 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.
- 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).
- 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.
- With the power off, the eight buttons above the top board. Connect their − pins first.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Rails of the second board (from the board with the power module) · 4 wires | ||||
| 1 | +5V | board 2: top + rail, column 8 | board 1: bottom + rail, column 8 | |
| 2 | GND | board 2: top − rail, column 10 | board 1: bottom − rail, column 10 | |
| 3 | +5V | board 1: top + rail, column 63 | board 1: bottom + rail, column 63 | |
| 4 | GND | board 1: top − rail, column 62 | board 1: bottom − rail, column 62 | |
| Power (red to +, black to −) · 8 wires | ||||
| 5 | +5V | board 1: 25j (4511 A pin 16, VDD) | board 1: bottom + rail, column 25 | |
| 6 | GND | board 1: 18a (4511 A pin 8, VSS) | board 1: top − rail, column 18 | |
| 7 | +5V | board 1: 35j (4008 pin 16, VDD) | board 1: bottom + rail, column 35 | |
| 8 | GND | board 1: 28a (4008 pin 8, VSS) | board 1: top − rail, column 28 | |
| 9 | +5V | board 1: 45j (4511 B pin 16, VDD) | board 1: bottom + rail, column 45 | |
| 10 | GND | board 1: 38a (4511 B pin 8, VSS) | board 1: top − rail, column 38 | |
| 11 | +5V | board 2: 35j (4511 S pin 16, VDD) | board 2: bottom + rail, column 35 | |
| 12 | GND | board 2: 28a (4511 S pin 8, VSS) | board 2: top − rail, column 28 | |
| Fixed inputs: CMOS inputs must not float · 13 wires | ||||
| 13 | GND | board 1: 8j (display A pin 3, CC) | board 1: bottom − rail, column 8 | |
| 14 | GND | board 1: 21a (4511 A pin 5, LE) | board 1: top − rail, column 21 | |
| 15 | +5V | board 1: 22a (4511 A pin 4, BI) | board 1: top + rail, column 22 | |
| 16 | +5V | board 1: 23a (4511 A pin 3, LT) | board 1: top + rail, column 23 | |
| 17 | GND | board 1: 28j (4008 pin 9, CIN) | board 1: bottom − rail, column 28 | |
| 18 | GND | board 1: 41a (4511 B pin 5, LE) | board 1: top − rail, column 41 | |
| 19 | +5V | board 1: 42a (4511 B pin 4, BI) | board 1: top + rail, column 42 | |
| 20 | +5V | board 1: 43a (4511 B pin 3, LT) | board 1: top + rail, column 43 | |
| 21 | GND | board 1: 55j (display B pin 3, CC) | board 1: bottom − rail, column 55 | |
| 22 | GND | board 2: 31a (4511 S pin 5, LE) | board 2: top − rail, column 31 | |
| 23 | +5V | board 2: 32a (4511 S pin 4, BI) | board 2: top + rail, column 32 | |
| 24 | +5V | board 2: 33a (4511 S pin 3, LT) | board 2: top + rail, column 33 | |
| 25 | GND | board 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 | ||||
| RbA | 560 Ω | board 1: 10b | board 1: 14b | |
| RaA | 560 Ω | board 1: 9a | board 1: 13a | |
| RfA | 560 Ω | board 1: 7c | board 1: 12c | |
| RcA | 560 Ω | board 1: 9j | board 1: 13j | |
| RdA | 560 Ω | board 1: 7i | board 1: 12i | |
| RgA | 560 Ω | board 1: 6a | board 1: 2a | |
| ReA | 560 Ω | board 1: 6j | board 1: 2j | |
| RgB | 560 Ω | board 1: 53b | board 1: 49b | |
| RfB | 560 Ω | board 1: 54a | board 1: 50a | |
| RaB | 560 Ω | board 1: 56c | board 1: 51c | |
| ReB | 560 Ω | board 1: 53j | board 1: 49j | |
| RdB | 560 Ω | board 1: 54i | board 1: 50i | |
| RbB | 560 Ω | board 1: 57a | board 1: 61a | |
| RcB | 560 Ω | board 1: 56j | board 1: 60j | |
| RgS | 560 Ω | board 2: 43b | board 2: 39b | |
| RfS | 560 Ω | board 2: 44a | board 2: 40a | |
| RaS | 560 Ω | board 2: 46c | board 2: 41c | |
| ReS | 560 Ω | board 2: 43j | board 2: 39j | |
| RdS | 560 Ω | board 2: 44i | board 2: 40i | |
| RbS | 560 Ω | board 2: 47a | board 2: 51a | |
| RcS | 560 Ω | board 2: 46j | board 2: 50j | |
| RDP | 560 Ω | board 2: 47i | board 2: 51i | |
| Signal wires · 34 wires | ||||
| 26 | A.a | board 1: 22g (4511 A pin 13, a) | board 1: 13d (RaA) | |
| 27 | A.b | board 1: 21i (4511 A pin 12, b) | board 1: 14e (RbA) | |
| 28 | A.c | board 1: 20h (4511 A pin 11, c) | board 1: 13h (RcA) | |
| 29 | A.d (loose jumper) | board 1: 19h (4511 A pin 10, d) | board 1: 12h (RdA) | |
| 30 | A.e (loose jumper) | board 1: 18h (4511 A pin 9, e) | board 1: 2h (ReA) | |
| 31 | A.f | board 1: 24g (4511 A pin 15, f) | board 1: 12d (RfA) | |
| 32 | A.g | board 1: 23j (4511 A pin 14, g) | board 1: 2d (RgA) | |
| 33 | B.a (loose jumper) | board 1: 42h (4511 B pin 13, a) | board 1: 51d (RaB) | |
| 34 | B.b | board 1: 41j (4511 B pin 12, b) | board 1: 61e (RbB) | |
| 35 | B.c (loose jumper) | board 1: 40h (4511 B pin 11, c) | board 1: 60h (RcB) | |
| 36 | B.d (loose jumper) | board 1: 39h (4511 B pin 10, d) | board 1: 50h (RdB) | |
| 37 | B.e | board 1: 38g (4511 B pin 9, e) | board 1: 49f (ReB) | |
| 38 | B.f | board 1: 44g (4511 B pin 15, f) | board 1: 50d (RfB) | |
| 39 | B.g | board 1: 43i (4511 B pin 14, g) | board 1: 49c (RgB) | |
| 40 | S.a | board 2: 32g (4511 S pin 13, a) | board 2: 41e (RaS) | |
| 41 | S.b (loose jumper) | board 2: 31h (4511 S pin 12, b) | board 2: 51c (RbS) | |
| 42 | S.c | board 2: 30j (4511 S pin 11, c) | board 2: 50h (RcS) | |
| 43 | S.d | board 2: 29h (4511 S pin 10, d) | board 2: 40h (RdS) | |
| 44 | S.e | board 2: 28i (4511 S pin 9, e) | board 2: 39i (ReS) | |
| 45 | S.f (loose jumper) | board 2: 34h (4511 S pin 15, f) | board 2: 40c (RfS) | |
| 46 | S.g | board 2: 33g (4511 S pin 14, g) | board 2: 39d (RgS) | |
| 47 | A1 | board 1: 19b (4511 A pin 7, A) | board 1: 29b (4008 pin 7, A1) | |
| 48 | A2 | board 1: 25d (4511 A pin 1, B) | board 1: 31d (4008 pin 5, A2) | |
| 49 | A3 | board 1: 24b (4511 A pin 2, C) | board 1: 33a (4008 pin 3, A3) | |
| 50 | A4 | board 1: 20c (4511 A pin 6, D) | board 1: 35c (4008 pin 1, A4) | |
| 51 | B1 | board 1: 39b (4511 B pin 7, A) | board 1: 30b (4008 pin 6, B1) | |
| 52 | B2 | board 1: 45d (4511 B pin 1, B) | board 1: 32d (4008 pin 4, B2) | |
| 53 | B3 | board 1: 44b (4511 B pin 2, C) | board 1: 34a (4008 pin 2, B3) | |
| 54 | B4 | board 1: 40c (4511 B pin 6, D) | board 1: 34g (4008 pin 15, B4) | |
| 55 | S1 | board 1: 29g (4008 pin 10, S1) | board 2: 29d (4511 S pin 7, A) | |
| 56 | S2 | board 1: 30g (4008 pin 11, S2) | board 2: 35c (4511 S pin 1, B) | |
| 57 | S3 | board 1: 31g (4008 pin 12, S3) | board 2: 34d (4511 S pin 2, C) | |
| 58 | S4 | board 1: 32h (4008 pin 13, S4) | board 2: 30d (4511 S pin 6, D) | |
| 59 | CO | board 1: 33h (4008 pin 14, COUT) | board 2: 51f (RDP) | |
| Modules on male-female jumpers · 24 wires | ||||
| 60 | +5V | button A4, + | board 1: top + rail, column 10 | |
| 61 | A4 | button A4, OUT | board 1: 20a (4511 A pin 6, D) | |
| 62 | GND | button A4, − | board 1: top − rail, column 12 | |
| 63 | +5V | button A3, + | board 1: top + rail, column 15 | |
| 64 | A3 | button A3, OUT | board 1: 24a (4511 A pin 2, C) | |
| 65 | GND | button A3, − | board 1: top − rail, column 17 | |
| 66 | +5V | button A2, + | board 1: top + rail, column 20 | |
| 67 | A2 | button A2, OUT | board 1: 25a (4511 A pin 1, B) | |
| 68 | GND | button A2, − | board 1: top − rail, column 22 | |
| 69 | +5V | button A1, + | board 1: top + rail, column 25 | |
| 70 | A1 | button A1, OUT | board 1: 29a (4008 pin 7, A1) | |
| 71 | GND | button A1, − | board 1: top − rail, column 27 | |
| 72 | +5V | button B4, + | board 1: top + rail, column 33 | |
| 73 | B4 | button B4, OUT | board 1: 40a (4511 B pin 6, D) | |
| 74 | GND | button B4, − | board 1: top − rail, column 35 | |
| 75 | +5V | button B3, + | board 1: top + rail, column 38 | |
| 76 | B3 | button B3, OUT | board 1: 44a (4511 B pin 2, C) | |
| 77 | GND | button B3, − | board 1: top − rail, column 40 | |
| 78 | +5V | button B2, + | board 1: top + rail, column 44 | |
| 79 | B2 | button B2, OUT | board 1: 45a (4511 B pin 1, B) | |
| 80 | GND | button B2, − | board 1: top − rail, column 45 | |
| 81 | +5V | button B1, + | board 1: top + rail, column 48 | |
| 82 | B1 | button B1, OUT | board 1: 39a (4511 B pin 7, A) | |
| 83 | GND | button B1, − | board 1: top − rail, column 50 | |
Test it
- No button pressed: all three displays show 0.
- Test each display alone: press A1, A2, A4 one at a time and see 1, 2 and 4 on display A; the same with B.
- Press A1 and B2: 1 + 2 = 3. Press A4 and B1: 8 + 1 = 9.
- Press A4 and B4: 8 + 8 = 16, shown as 0 with the dot lit. Press A2, A4 and B4: 10 + 8 = 18, which is 2 with the dot. But A shows nothing: 10 is not a digit for the 4511.
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.
// 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.
// 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
// 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.
// 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.
- Copy a lab, for example
labs/1_basics/1_05_7seven_segment_letter, to a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addadder_display.sv. - Run
./03_synthesize_for_fpga.bashand./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.
- Copy a directory such as
hackathon/problems/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addadder_display.sv. - Run
./03_synthesize_for_fpga.bashand./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
- Carry in. Move CARRY IN of the 4008 (pin 9) from GND to a free button: now the circuit computes A + B + 1. What is 15 + 15 + 1?
- Hexadecimal. Change the decoder in
adder_display.svto show A–F for 10–15, as the HEX decoder that Habr lesson 1.2 suggests would, so that every sum from 0 to 31 can be read as the dot and one hexadecimal digit. - Two digits of sum. Show the 5-bit result as a two-digit decimal number on two displays. In SystemVerilog that is a division by 10; on the breadboard it needs more than one adder. Which is easier?
- Eight bits (exercise M4): chain two 4008s, CARRY OUT of the low one to CARRY IN of the high one.
- Subtraction. A − B is A + NOT B + 1 in binary. Put the four bits of B through a 4069 and set CARRY IN to 1: the 4008 now subtracts. What does it show for 3 − 5?
Files of this exercise
- adder_display.sv: the generic design (section 8)
- cd4008.sv, cd4511.sv: chip models (section 9)
- adder_display_chips.sv: the chip-level design (section 10)
- tb.sv and run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the three schematics, the pinouts and the waveforms;
the Russian versions end in
_ru
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.