Exercise P1 · project · serial arithmetic
Bit-serial adder: a 4032 between two 4014s and a 4015
Two numbers become two streams of bits, an adder with a one-bit memory adds them one bit per clock, and the bits of the sum are collected back into a number. A project for a group of three, one breadboard each: the same split of work into blocks as in a real chip design team.
1What it is and why it matters
What it does. Buttons A3–A0 and B3–B0 give two numbers, shown at once on the displays A and B. Press LOAD through one blink of the clock module: the numbers go into two shift registers. At each of the next four blinks one bit of each number, lowest first, goes through the adder, and the bit of the sum goes into a third shift register. After the fourth blink the sum display shows A + B.
How it works. A 4014 is a serializer: LOAD copies A into it in parallel, A0 into the stage next to the output, and every clock moves the bits one stage on. The 4032 is a full adder with a flip-flop for the carry: its sum output is A XOR B XOR the stored carry, and at every rising edge it stores the new carry for the next bit. The 4015 is a deserializer: it takes the sum bits one by one. LOAD also clears the carry and the 4015.
Where it is used. Serial links such as USB, PCI Express and SATA send numbers one bit after another, and their chips have serializers and deserializers, SerDes, at both ends. Early computers and many calculators did all their arithmetic bit-serially, because one adder used again and again costs fewer parts than one adder per bit.
Why learn it.
- It shows the trade between space and time: the 4008 of exercise B8 adds four bits at once with four adders; here one adder does it in four clocks.
- It shows what a carry flip-flop does, and why it must be cleared before a new pair of numbers.
- It is a team project: three students build three boards that must agree on the clock, LOAD and the bit streams.
2The key elements: one bit per clock
A = 5 = 0101 and B = 3 = 0011, after LOAD:
| Clock edge after LOAD | A bit | B bit | Carry in | Sum bit | Carry out | 4015 Q1A…Q4A |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 0 | 1 | 0000 |
| 2 | 0 | 1 | 1 | 0 | 1 | 0000 |
| 3 | 1 | 0 | 1 | 0 | 1 | 0000 |
| 4 | 0 | 0 | 1 | 1 | 0 | 1000 = 8 |
| 5 | 0 | 0 | 0 | 0 | 0 | 0100 |
After the fourth edge the 4015 holds 1000, the sum 8, and the display shows it. The fifth edge brings the fifth bit of the sum, here 0, into Q1A, and the later edges push the sum along: the display shows the right sum for one period of the clock. Slow the clock module down to read it, and press LOAD to add again. The 4511 shows digits 0 to 9 only, so choose A + B of at most 9.
Waveforms
tb.sv in Icarus Verilog: 5 + 3, then 9 + 8. The streams A→ and B→
are the Q8 outputs of the 4014s, S→ the sum output of the 4032; the last row is the 4015. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4511 | BCD-to-7-segment decoders for A, B and the sum | 3 | $0.65 | Elexp (elexp.com) |
| CD4014 | 8-stage parallel-in, serial-out registers: the serializers of A and B | 2 | $0.19 | Jameco (jameco.com) |
| CD4032 | triple serial adder: adder 1 is used | 1 | $0.31 | store.comet.bg |
| CD4015 | dual 4-stage shift register: register A is the deserializer of the sum | 1 | $0.35 | Elexp (elexp.com) |
| Button modules | A3-A0, B3-B0 and LOAD; the first pack of five | 5 | $0.66 / pack | aliexpress.us |
| Button modules | 4 more from the second pack | 4 | $2.03 / pack | aliexpress.us |
| 555 clock module | the clock, set to about 1 Hz | 1 | $0.53 | aliexpress.us |
| 7-segment display, common cathode | A, B and the sum | 3 | $0.90 | elexp.com |
| Resistor, 560 Ω, ½ W | one per segment | 21 | $0.04 | elexp.com |
| Breadboard, 830 points | two boards, one above the other | 3 | $3.00 | amazon.com |
| Breadboard power module | 5 V on the rails | 1 | $1.00 | aliexpress.us |
| USB-to-DC cable, 9 V | powers the module from a USB charger | 1 | $1.17 | aliexpress.us |
| Pre-cut jumper wires | 100 wires on the board (or male-male Dupont jumpers) | 100 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 30 leads to the clock module and the buttons | 30 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; these chips are in the Comprehensive kit.
4The chips, pin by pin
The chips are CMOS 4000B series: they work from 3 to 18 V, here from the 5 V of the power module. Every input must be connected to something, because a CMOS input left open picks up noise and can switch at random. The drawings show each chip from above with the notch on the left, as in the datasheet: pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.
4014: 8-stage shift register, parallel in, serial out
As in exercise BB2. While PAR/SER (pin 9) is 1, a rising edge of CLOCK (pin 10) loads P1–P8; while it is 0, the bits move one stage toward Q8 (pin 3). Here P8, P7, P6, P5 (pins 1, 15, 14, 13) get bits 0 to 3 of the number, P4–P1 and SERIAL IN are at GND, so zeros follow the four bits.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | P8 | input | parallel input, stage 8 | A0 |
| 2 | Q6 | output | output of stage 6 | not connected |
| 3 | Q8 | output | output of stage 8, the serial output | A→ |
| 4 | P4 | input | parallel input, stage 4 | GND |
| 5 | P3 | input | parallel input, stage 3 | GND |
| 6 | P2 | input | parallel input, stage 2 | GND |
| 7 | P1 | input | parallel input, stage 1 | GND |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | PAR/SER | input | 1 = load P1-P8 at the next clock edge, 0 = shift; works with the clock (synchronous) | LOAD |
| 10 | CLOCK | input | everything happens on the rising edge | CLK |
| 11 | SERIAL IN | input | serial input: goes into stage 1 when shifting | GND |
| 12 | Q7 | output | output of stage 7 | not connected |
| 13 | P5 | input | parallel input, stage 5 | A3 |
| 14 | P6 | input | parallel input, stage 6 | A2 |
| 15 | P7 | input | parallel input, stage 7 | A1 |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4032: triple serial adder
Three adders share CLOCK (pin 3) and CARRY RESET (pin 6). Adder 1 is used: A1 (pin 10) and B1 (pin 11) get the streams, SUM1 (pin 9) gives the sum bit. The carry is stored at the rising edge of the clock; CARRY RESET at 1 clears it, here at the LOAD edge. INVERT1 (pin 7) at GND keeps the sum as it is, and the inputs of the other two adders are at GND.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | SUM3 | output | sum of adder 3 | not connected |
| 2 | INVERT3 | input | 1 inverts the sum of adder 3 | tied to +5V |
| 3 | CLOCK | input | the carries are stored at the rising edge | CLK |
| 4 | SUM2 | output | sum of adder 2 | not connected |
| 5 | INVERT2 | input | 1 inverts the sum of adder 2 | tied to +5V |
| 6 | CARRY RESET | input | 1 at a rising edge clears the carries for the next word | LOAD |
| 7 | INVERT1 | input | 1 inverts the sum of adder 1 | GND |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | SUM1 | output | sum of adder 1: A1 XOR B1 XOR the stored carry | S→ |
| 10 | A1 | input | adder 1, number A, one bit per clock, lowest first | A→ |
| 11 | B1 | input | adder 1, number B, one bit per clock | B→ |
| 12 | B2 | input | adder 2, number B | tied to GND |
| 13 | A2 | input | adder 2, number A | tied to GND |
| 14 | B3 | input | adder 3, number B | tied to GND |
| 15 | A3 | input | adder 3, number A | tied to GND |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4015: dual 4-stage shift register
As in exercise B6. Register A takes the sum bits on DATA A (pin 7) at every rising edge of CLOCK A (pin 9); RESET A (pin 6) is LOAD, which clears it at once. The newest bit is in Q1A, so after four edges Q1A, Q2A, Q3A, Q4A (pins 5, 4, 3, 10) are S3, S2, S1, S0 and go to D, C, B, A of the sum 4511.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | CLOCK B | input | register B shifts on the rising edge | tied to +5V |
| 2 | Q4B | output | register B, stage 4 (the oldest bit) | not connected |
| 3 | Q3A | output | register A, stage 3 | Q3A |
| 4 | Q2A | output | register A, stage 2 | Q2A |
| 5 | Q1A | output | register A, stage 1 (the newest bit) | Q1A |
| 6 | RESET A | input | 1 clears register A at once, without the clock. Active high | LOAD |
| 7 | DATA A | input | register A, serial input | S→ |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | CLOCK A | input | register A shifts on the rising edge | CLK |
| 10 | Q4A | output | register A, stage 4 (the oldest bit) | Q4A |
| 11 | Q3B | output | register B, stage 3 | not connected |
| 12 | Q2B | output | register B, stage 2 | not connected |
| 13 | Q1B | output | register B, stage 1 | not connected |
| 14 | RESET B | input | 1 clears register B at once. Active high | tied to GND |
| 15 | DATA B | input | register B, serial input | tied to GND |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4511: BCD to 7-segment decoder
As in exercise B7, three times: for A, for B and for the sum, each with a common-cathode display and seven resistors.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | B | input | BCD input, bit 1 (weight 2) | A1 |
| 2 | C | input | BCD input, bit 2 (weight 4) | A2 |
| 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) | A3 |
| 7 | A | input | BCD input, bit 0 (weight 1) | A0 |
| 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 |
5Schematic 1: serializers, adder, deserializer
serial_adder.sv describes; the three 4511s
and displays are drawn as the outputs. · SVG · PNG6Schematic 2: connections between the chips
| Net | From | To | Wire |
|---|---|---|---|
| CLK | 555 clock module, OUT | 4014 A 10 (CLK), 4014 B 10 (CLK), 4032 3 (CLK), 4015 9 (CLKA) | M-F, M-M |
| LOAD | button LOAD, OUT | 4014 A 9 (P/S), 4014 B 9 (P/S), 4032 6 (CR), 4015 6 (RA) | M-F, M-M |
| A3 | button A3, OUT | 4014 A 13 (P5), 4511 A 6 (D) | M-F, M-M |
| A2 | button A2, OUT | 4014 A 14 (P6), 4511 A 2 (C) | M-F, M-M |
| A1 | button A1, OUT | 4014 A 15 (P7), 4511 A 1 (B) | M-F, M-M |
| A0 | button A0, OUT | 4014 A 1 (P8), 4511 A 7 (A) | M-F, M-M |
| B3 | button B3, OUT | 4014 B 13 (P5), 4511 B 6 (D) | M-F, M-M (loose jumper) |
| B2 | button B2, OUT | 4014 B 14 (P6), 4511 B 2 (C) | M-F, M-M |
| B1 | button B1, OUT | 4014 B 15 (P7), 4511 B 1 (B) | M-F, M-M |
| B0 | button B0, OUT | 4014 B 1 (P8), 4511 B 7 (A) | M-F, M-M |
| A→ | 4014 A 3 (Q8) | 4032 10 (A1) | M-M |
| B→ | 4014 B 3 (Q8) | 4032 11 (B1) | M-M |
| S→ | 4032 9 (S1) | 4015 7 (DA) | M-M |
| Q1A | 4015 5 (Q1A) | 4511 S 6 (D) | M-M |
| Q2A | 4015 4 (Q2A) | 4511 S 2 (C) | M-M |
| Q3A | 4015 3 (Q3A) | 4511 S 1 (B) | M-M |
| Q4A | 4015 10 (Q4A) | 4511 S 7 (A) | M-M |
| 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 |
| A.e | 4511 A 9 (e) | ReA | M-M |
| A.f | 4511 A 15 (f) | RfA | M-M (loose jumper) |
| A.g | 4511 A 14 (g) | RgA | M-M (loose jumper) |
| A.a′ | — | RaA, A 7 (a) | |
| A.b′ | — | RbA, A 6 (b) | |
| A.c′ | — | RcA, A 4 (c) | |
| A.d′ | — | RdA, A 2 (d) | |
| A.e′ | — | ReA, A 1 (e) | |
| A.f′ | — | RfA, A 9 (f) | |
| A.g′ | — | RgA, A 10 (g) | |
| B.a | 4511 B 13 (a) | RaB | M-M |
| B.b | 4511 B 12 (b) | RbB | M-M |
| B.c | 4511 B 11 (c) | RcB | M-M |
| B.d | 4511 B 10 (d) | RdB | M-M |
| 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 (loose jumper) |
| B.a′ | — | RaB, B 7 (a) | |
| B.b′ | — | RbB, B 6 (b) | |
| B.c′ | — | RcB, B 4 (c) | |
| B.d′ | — | RdB, B 2 (d) | |
| B.e′ | — | ReB, B 1 (e) | |
| B.f′ | — | RfB, B 9 (f) | |
| B.g′ | — | RgB, B 10 (g) | |
| S.a | 4511 S 13 (a) | RaS | M-M (loose jumper) |
| 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 |
| 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), 4014 A 16 (VDD), 4511 B 16 (VDD), 4014 B 16 (VDD), 4032 16 (VDD), 4015 16 (VDD), 4511 S 16 (VDD), 4511 A 4 (BI), 4511 A 3 (LT), 4511 B 4 (BI), 4511 B 3 (LT), 4032 5 (INV2), 4032 2 (INV3), 4015 1 (CLKB), 4511 S 4 (BI), 4511 S 3 (LT) | |
| GND | GND | 4511 A 8 (VSS), 4014 A 8 (VSS), 4511 B 8 (VSS), 4014 B 8 (VSS), 4032 8 (VSS), 4015 8 (VSS), 4511 S 8 (VSS), A 3 (CC), 4511 A 5 (LE), 4014 A 4 (P4), 4014 A 5 (P3), 4014 A 11 (SER), 4014 A 6 (P2), 4014 A 7 (P1), B 3 (CC), 4511 B 5 (LE), 4014 B 7 (P1), 4014 B 6 (P2), 4014 B 11 (SER), 4014 B 5 (P3), 4014 B 4 (P4), 4032 7 (INV1), 4032 12 (B2), 4032 13 (A2), 4032 14 (B3), 4032 15 (A3), 4015 14 (RB), 4015 15 (DB), 4511 S 5 (LE), A + B 3 (CC) | |
| not connected | 4014 A 2 (Q6), 4014 A 12 (Q7), 4014 B 2 (Q6), 4014 B 12 (Q7), 4032 1 (S3), 4032 4 (S2), 4015 2 (Q4B), 4015 11 (Q3B), 4015 12 (Q2B), 4015 13 (Q1B), A 5 (DP), A 8 (CC), B 5 (DP), B 8 (CC), A + B 5 (DP), A + B 8 (CC) |
7Schematic 3: the breadboards
Build it step by step
- Do the getting-started steps first: how the breadboard is connected inside, the power module with both jumpers on 5 V, how a chip sits across the gap.
- Split the work: one student builds the top board, one the middle board, one the bottom board. Agree first on the wires between the boards: the clock, LOAD, the two streams and the B buttons.
- Insert the chips across the gap: the 4511 A turned around, notch to the right, pin 1 in hole 27e of board 1; the 4014 A, notch to the left, pin 1 in hole 35f of board 1; the 4511 B turned around, notch to the right, pin 1 in hole 25e of board 2; the 4014 B turned around, notch to the right, pin 1 in hole 39e of board 2; the 4032 turned around, notch to the right, pin 1 in hole 13e of board 3; the 4015 turned around, notch to the right, pin 1 in hole 25e of board 3; the 4511 S turned around, notch to the right, pin 1 in hole 45e of board 3. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
- Power: for each chip a red wire from its VDD pin (14, or 16 on a 16-pin chip) to the + rail and a black wire from its VSS pin (7 or 8) to the − rail, each to the rail on its own side of the gap. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
- The rail jumpers on the right: they bring +5V and GND from the bottom board to the other two.
- Each display unit: the 4511, its display and the seven resistors, as in exercise B7.
- The fixed inputs and the signal wires, board by board, following the checklist. Tick each wire as you insert it.
- With the power off, the modules: A3–A0, B3–B0, LOAD and the clock module above the top board.
- Turn the power on and set the clock module to its slowest.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Rails of the second board (from the board with the power module) · 8 wires | ||||
| 1 | +5V | board 2: top + rail, column 2 | board 1: bottom + rail, column 2 | |
| 2 | GND | board 2: top − rail, column 4 | board 1: bottom − rail, column 4 | |
| 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 | |
| 5 | +5V | board 3: top + rail, column 2 | board 2: bottom + rail, column 2 | |
| 6 | GND | board 3: top − rail, column 4 | board 2: bottom − rail, column 4 | |
| 7 | +5V | board 2: top + rail, column 63 | board 2: bottom + rail, column 63 | |
| 8 | GND | board 2: top − rail, column 62 | board 2: bottom − rail, column 62 | |
| Power (red to +, black to −) · 14 wires | ||||
| 9 | +5V | board 1: 27j (4511 A pin 16, VDD) | board 1: bottom + rail, column 27 | |
| 10 | GND | board 1: 20a (4511 A pin 8, VSS) | board 1: top − rail, column 20 | |
| 11 | +5V | board 1: 35a (4014 A pin 16, VDD) | board 1: top + rail, column 35 | |
| 12 | GND | board 1: 42j (4014 A pin 8, VSS) | board 1: bottom − rail, column 42 | |
| 13 | +5V | board 2: 25j (4511 B pin 16, VDD) | board 2: bottom + rail, column 25 | |
| 14 | GND | board 2: 18a (4511 B pin 8, VSS) | board 2: top − rail, column 18 | |
| 15 | +5V | board 2: 39j (4014 B pin 16, VDD) | board 2: bottom + rail, column 39 | |
| 16 | GND | board 2: 32a (4014 B pin 8, VSS) | board 2: top − rail, column 32 | |
| 17 | +5V | board 3: 13j (4032 pin 16, VDD) | board 3: bottom + rail, column 13 | |
| 18 | GND | board 3: 6a (4032 pin 8, VSS) | board 3: top − rail, column 6 | |
| 19 | +5V | board 3: 25j (4015 pin 16, VDD) | board 3: bottom + rail, column 25 | |
| 20 | GND | board 3: 18a (4015 pin 8, VSS) | board 3: top − rail, column 18 | |
| 21 | +5V | board 3: 45j (4511 S pin 16, VDD) | board 3: bottom + rail, column 45 | |
| 22 | GND | board 3: 38a (4511 S pin 8, VSS) | board 3: top − rail, column 38 | |
| Fixed inputs: CMOS inputs must not float · 32 wires | ||||
| 23 | GND | board 1: 10j (display A pin 3, CC) | board 1: bottom − rail, column 10 | |
| 24 | GND | board 1: 23a (4511 A pin 5, LE) | board 1: top − rail, column 23 | |
| 25 | +5V | board 1: 24a (4511 A pin 4, BI) | board 1: top + rail, column 24 | |
| 26 | +5V | board 1: 25a (4511 A pin 3, LT) | board 1: top + rail, column 25 | |
| 27 | GND | board 1: 38j (4014 A pin 4, P4) | board 1: bottom − rail, column 38 | |
| 28 | GND | board 1: 39j (4014 A pin 5, P3) | board 1: bottom − rail, column 39 | |
| 29 | GND | board 1: 40a (4014 A pin 11, SER) | board 1: top − rail, column 40 | |
| 30 | GND | board 1: 40j (4014 A pin 6, P2) | board 1: bottom − rail, column 40 | |
| 31 | GND | board 1: 41j (4014 A pin 7, P1) | board 1: bottom − rail, column 41 | |
| 32 | GND | board 2: 8j (display B pin 3, CC) | board 2: bottom − rail, column 8 | |
| 33 | GND | board 2: 21a (4511 B pin 5, LE) | board 2: top − rail, column 21 | |
| 34 | +5V | board 2: 22a (4511 B pin 4, BI) | board 2: top + rail, column 22 | |
| 35 | +5V | board 2: 23a (4511 B pin 3, LT) | board 2: top + rail, column 23 | |
| 36 | GND | board 2: 33a (4014 B pin 7, P1) | board 2: top − rail, column 33 | |
| 37 | GND | board 2: 34a (4014 B pin 6, P2) | board 2: top − rail, column 34 | |
| 38 | GND | board 2: 34j (4014 B pin 11, SER) | board 2: bottom − rail, column 34 | |
| 39 | GND | board 2: 35a (4014 B pin 5, P3) | board 2: top − rail, column 35 | |
| 40 | GND | board 2: 36a (4014 B pin 4, P4) | board 2: top − rail, column 36 | |
| 41 | GND | board 3: 7a (4032 pin 7, INV1) | board 3: top − rail, column 7 | |
| 42 | +5V | board 3: 9a (4032 pin 5, INV2) | board 3: top + rail, column 9 | |
| 43 | GND | board 3: 9j (4032 pin 12, B2) | board 3: bottom − rail, column 9 | |
| 44 | GND | board 3: 10j (4032 pin 13, A2) | board 3: bottom − rail, column 10 | |
| 45 | GND | board 3: 11j (4032 pin 14, B3) | board 3: bottom − rail, column 11 | |
| 46 | +5V | board 3: 12a (4032 pin 2, INV3) | board 3: top + rail, column 12 | |
| 47 | GND | board 3: 12j (4032 pin 15, A3) | board 3: bottom − rail, column 12 | |
| 48 | GND | board 3: 23j (4015 pin 14, RB) | board 3: bottom − rail, column 23 | |
| 49 | GND | board 3: 24j (4015 pin 15, DB) | board 3: bottom − rail, column 24 | |
| 50 | +5V | board 3: 25a (4015 pin 1, CLKB) | board 3: top + rail, column 25 | |
| 51 | GND | board 3: 41a (4511 S pin 5, LE) | board 3: top − rail, column 41 | |
| 52 | +5V | board 3: 42a (4511 S pin 4, BI) | board 3: top + rail, column 42 | |
| 53 | +5V | board 3: 43a (4511 S pin 3, LT) | board 3: top + rail, column 43 | |
| 54 | GND | board 3: 55j (display A + B pin 3, CC) | board 3: bottom − rail, column 55 | |
| Resistors, 560 Ω (not wires: put each resistor in these two holes) · 21 resistors | ||||
| RbA | 560 Ω | board 1: 12b | board 1: 16b | |
| RaA | 560 Ω | board 1: 11a | board 1: 15a | |
| RfA | 560 Ω | board 1: 9c | board 1: 14c | |
| RcA | 560 Ω | board 1: 11j | board 1: 15j | |
| RdA | 560 Ω | board 1: 9i | board 1: 14i | |
| RgA | 560 Ω | board 1: 8a | board 1: 4a | |
| ReA | 560 Ω | board 1: 8j | board 1: 4j | |
| RbB | 560 Ω | board 2: 10b | board 2: 14b | |
| RaB | 560 Ω | board 2: 9a | board 2: 13a | |
| RfB | 560 Ω | board 2: 7c | board 2: 12c | |
| RcB | 560 Ω | board 2: 9j | board 2: 13j | |
| RdB | 560 Ω | board 2: 7i | board 2: 12i | |
| RgB | 560 Ω | board 2: 6a | board 2: 2a | |
| ReB | 560 Ω | board 2: 6j | board 2: 2j | |
| RgS | 560 Ω | board 3: 53b | board 3: 49b | |
| RfS | 560 Ω | board 3: 54a | board 3: 50a | |
| RaS | 560 Ω | board 3: 56c | board 3: 51c | |
| ReS | 560 Ω | board 3: 53j | board 3: 49j | |
| RdS | 560 Ω | board 3: 54i | board 3: 50i | |
| RbS | 560 Ω | board 3: 57a | board 3: 61a | |
| RcS | 560 Ω | board 3: 56j | board 3: 60j | |
| Signal wires · 46 wires | ||||
| 55 | A.a | board 1: 24i (4511 A pin 13, a) | board 1: 15d (RaA) | |
| 56 | A.b | board 1: 23h (4511 A pin 12, b) | board 1: 16e (RbA) | |
| 57 | A.c | board 1: 22g (4511 A pin 11, c) | board 1: 15f (RcA) | |
| 58 | A.d | board 1: 21g (4511 A pin 10, d) | board 1: 14g (RdA) | |
| 59 | A.e | board 1: 20j (4511 A pin 9, e) | board 1: 4i (ReA) | |
| 60 | A.f (loose jumper) | board 1: 26h (4511 A pin 15, f) | board 1: 14d (RfA) | |
| 61 | A.g (loose jumper) | board 1: 25h (4511 A pin 14, g) | board 1: 4c (RgA) | |
| 62 | B.a | board 2: 22i (4511 B pin 13, a) | board 2: 13d (RaB) | |
| 63 | B.b | board 2: 21h (4511 B pin 12, b) | board 2: 14e (RbB) | |
| 64 | B.c | board 2: 20g (4511 B pin 11, c) | board 2: 13f (RcB) | |
| 65 | B.d | board 2: 19g (4511 B pin 10, d) | board 2: 12g (RdB) | |
| 66 | B.e | board 2: 18j (4511 B pin 9, e) | board 2: 2i (ReB) | |
| 67 | B.f | board 2: 24g (4511 B pin 15, f) | board 2: 12d (RfB) | |
| 68 | B.g (loose jumper) | board 2: 23h (4511 B pin 14, g) | board 2: 2c (RgB) | |
| 69 | S.a (loose jumper) | board 3: 42h (4511 S pin 13, a) | board 3: 51d (RaS) | |
| 70 | S.b (loose jumper) | board 3: 41h (4511 S pin 12, b) | board 3: 61c (RbS) | |
| 71 | S.c | board 3: 40j (4511 S pin 11, c) | board 3: 60i (RcS) | |
| 72 | S.d | board 3: 39g (4511 S pin 10, d) | board 3: 50f (RdS) | |
| 73 | S.e | board 3: 38g (4511 S pin 9, e) | board 3: 49g (ReS) | |
| 74 | S.f | board 3: 44h (4511 S pin 15, f) | board 3: 50d (RfS) | |
| 75 | S.g | board 3: 43i (4511 S pin 14, g) | board 3: 49c (RgS) | |
| 76 | CLK | board 1: 41d (4014 A pin 10, CLK) | board 2: 33h (4014 B pin 10, CLK) | |
| 77 | CLK | board 2: 33g (4014 B pin 10, CLK) | board 3: 18g (4015 pin 9, CLKA) | |
| 78 | CLK | board 3: 18h (4015 pin 9, CLKA) | board 3: 11c (4032 pin 3, CLK) | |
| 79 | LOAD | board 1: 42c (4014 A pin 9, P/S) | board 2: 32j (4014 B pin 9, P/S) | |
| 80 | LOAD | board 2: 32g (4014 B pin 9, P/S) | board 3: 20a (4015 pin 6, RA) | |
| 81 | LOAD | board 3: 20d (4015 pin 6, RA) | board 3: 8d (4032 pin 6, CR) | |
| 82 | A→ | board 1: 37g (4014 A pin 3, Q8) | board 3: 7i (4032 pin 10, A1) | |
| 83 | B→ | board 2: 37c (4014 B pin 3, Q8) | board 3: 8h (4032 pin 11, B1) | |
| 84 | S→ | board 3: 6g (4032 pin 9, S1) | board 3: 19c (4015 pin 7, DA) | |
| 85 | A0 | board 1: 35g (4014 A pin 1, P8) | board 1: 21b (4511 A pin 7, A) | |
| 86 | A1 | board 1: 36d (4014 A pin 15, P7) | board 1: 27d (4511 A pin 1, B) | |
| 87 | A2 | board 1: 37c (4014 A pin 14, P6) | board 1: 26c (4511 A pin 2, C) | |
| 88 | A3 | board 1: 38b (4014 A pin 13, P5) | board 1: 22b (4511 A pin 6, D) | |
| 89 | B0 | board 2: 39d (4014 B pin 1, P8) | board 2: 19d (4511 B pin 7, A) | |
| 90 | B0 | board 2: 39c (4014 B pin 1, P8) | board 1: 43e | |
| 91 | B1 | board 2: 38g (4014 B pin 15, P7) | board 2: 25b (4511 B pin 1, B) | |
| 92 | B1 | board 2: 38h (4014 B pin 15, P7) | board 1: 34b | |
| 93 | B2 | board 2: 37g (4014 B pin 14, P6) | board 2: 24a (4511 B pin 2, C) | |
| 94 | B2 | board 2: 37h (4014 B pin 14, P6) | board 1: 31e | |
| 95 | B3 (loose jumper) | board 2: 36h (4014 B pin 13, P5) | board 2: 20c (4511 B pin 6, D) | |
| 96 | B3 | board 2: 36i (4014 B pin 13, P5) | board 1: 28e | |
| 97 | Q1A | board 3: 21d (4015 pin 5, Q1A) | board 3: 40d (4511 S pin 6, D) | |
| 98 | Q2A | board 3: 22c (4015 pin 4, Q2A) | board 3: 44c (4511 S pin 2, C) | |
| 99 | Q3A | board 3: 23b (4015 pin 3, Q3A) | board 3: 45b (4511 S pin 1, B) | |
| 100 | Q4A | board 3: 19g (4015 pin 10, Q4A) | board 3: 39a (4511 S pin 7, A) | |
| Modules on male-female jumpers · 30 wires | ||||
| 101 | +5V | button A3, + | board 1: top + rail, column 4 | |
| 102 | A3 | button A3, OUT | board 1: 22a (4511 A pin 6, D) | |
| 103 | GND | button A3, − | board 1: top − rail, column 6 | |
| 104 | +5V | button A2, + | board 1: top + rail, column 9 | |
| 105 | A2 | button A2, OUT | board 1: 26a (4511 A pin 2, C) | |
| 106 | GND | button A2, − | board 1: top − rail, column 11 | |
| 107 | +5V | button A1, + | board 1: top + rail, column 14 | |
| 108 | A1 | button A1, OUT | board 1: 27a (4511 A pin 1, B) | |
| 109 | GND | button A1, − | board 1: top − rail, column 16 | |
| 110 | +5V | button A0, + | board 1: top + rail, column 19 | |
| 111 | A0 | button A0, OUT | board 1: 21a (4511 A pin 7, A) | |
| 112 | GND | button A0, − | board 1: top − rail, column 21 | |
| 113 | +5V | button B3, + | board 1: top + rail, column 26 | |
| 114 | B3 | button B3, OUT | board 1: 28a | |
| 115 | GND | button B3, − | board 1: top − rail, column 27 | |
| 116 | +5V | button B2, + | board 1: top + rail, column 30 | |
| 117 | B2 | button B2, OUT | board 1: 31a | |
| 118 | GND | button B2, − | board 1: top − rail, column 32 | |
| 119 | +5V | button B1, + | board 1: top + rail, column 34 | |
| 120 | B1 | button B1, OUT | board 1: 34a | |
| 121 | GND | button B1, − | board 1: top − rail, column 37 | |
| 122 | +5V | button B0, + | board 1: top + rail, column 40 | |
| 123 | B0 | button B0, OUT | board 1: 43a | |
| 124 | GND | button B0, − | board 1: top − rail, column 42 | |
| 125 | +5V | button LOAD, + | board 1: top + rail, column 46 | |
| 126 | LOAD | button LOAD, OUT | board 1: 42a (4014 A pin 9, P/S) | |
| 127 | GND | button LOAD, − | board 1: top − rail, column 48 | |
| 128 | GND | 555 clock module, − | board 1: top − rail, column 52 | |
| 129 | CLK | 555 clock module, OUT | board 1: 41a (4014 A pin 10, CLK) | |
| 130 | +5V | 555 clock module, + | board 1: top + rail, column 54 | |
Test it
- Hold A2 and A0 (5) and B1 and B0 (3): the displays A and B show 5 and 3.
- Press LOAD through one blink: the sum display shows 0.
- Watch four more blinks: the sum display shows 0, 0, 0 and then 8, the sum. At the fifth blink it shows 4: the sum moves on.
- Try 2 + 2, 4 + 5, 0 + 7. A sum above 9 leaves the sum display dark.
If it does not work: test each board on its own first: the displays A and B must follow their buttons. If the sum is always A or always B, one stream does not reach the 4032. If it is wrong by a carry, check CARRY RESET and the clock of the 4032. If the bits come out in the wrong order, P5–P8 of a 4014 are reversed.
8SystemVerilog: the generic design
serial_adder.sv has the same four parts: two serializers that shift right, a full adder with a carry
flip-flop, a deserializer, and three decoders. It is synthesizable.
// Exercise P1: a bit-serial adder. Two 4-bit numbers are turned into // streams of bits, added one bit per clock with a stored carry, and the // sum bits are collected back into a number. // // Generic version that matches schematic 1, not tied to any chip. // // load at a rising edge of clk puts a and b into the serializers and // clears the carry. At every later edge each serializer moves one bit out, // lowest first, then zeros; the adder adds the two bits and the carry, and // the deserializer takes the sum bit. load also clears the deserializer // at once. After four edges sum holds a + b without the fifth bit, and // later edges push it along. Like the 4511, the decoders blank the codes // 10-15. Synthesizable. module serial_adder ( input logic clk, input logic load, input logic [3:0] a, input logic [3:0] b, output logic serial_a, // The bit of a on its way, like Q8 of the 4014 output logic serial_b, output logic serial_sum, // The sum bit, like SUM1 of the 4032 output logic [3:0] sum, // The deserializer, newest bit in sum [3] output logic [6:0] abcdefg_a, // Segments a-g of each display: abcdefg [6] is a output logic [6:0] abcdefg_b, output logic [6:0] abcdefg_sum ); //------------------------------------------------------------------------ // The serializers: the bits still to send, the next one at the bottom logic [3:0] shift_a, shift_b; always_ff @ (posedge clk) if (load) begin shift_a <= a; shift_b <= b; end else begin shift_a <= shift_a >> 1; shift_b <= shift_b >> 1; end assign serial_a = shift_a [0]; assign serial_b = shift_b [0]; //------------------------------------------------------------------------ // The serial adder: one full adder and a flip-flop for the carry logic carry; assign serial_sum = serial_a ^ serial_b ^ carry; always_ff @ (posedge clk) if (load) carry <= 1'b0; else carry <= serial_a & serial_b | (serial_a ^ serial_b) & carry; //------------------------------------------------------------------------ // The deserializer always_ff @ (posedge clk or posedge load) if (load) sum <= '0; else sum <= { serial_sum, sum [3:1] }; //------------------------------------------------------------------------ // Three decoders, like the 4511: codes 10-15 blank the display function automatic logic [6:0] decode (input logic [3:0] code); case (code) 4'd0: decode = 7'b111_1110; 4'd1: decode = 7'b011_0000; 4'd2: decode = 7'b110_1101; 4'd3: decode = 7'b111_1001; 4'd4: decode = 7'b011_0011; 4'd5: decode = 7'b101_1011; 4'd6: decode = 7'b001_1111; 4'd7: decode = 7'b111_0000; 4'd8: decode = 7'b111_1111; 4'd9: decode = 7'b111_0011; default: decode = 7'b000_0000; endcase endfunction assign abcdefg_a = decode (a); assign abcdefg_b = decode (b); assign abcdefg_sum = decode (sum); endmodule
9SystemVerilog models of the chips
One module per chip, with ports named as in the datasheet and the pin number of each port in a comment. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.
// 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
// CD4014B: 8-stage static shift register, synchronous parallel or serial // input, serial output. // // Ports in pin order, named as in the datasheet. The power pins, VSS (8) // and VDD (16), are left out. Synthesizable. // // Everything happens on the rising edge of CLOCK. With PARALLEL/SERIAL // CONTROL = 1 the edge loads P1-P8 into the eight stages; with 0 it shifts: // SERIAL IN goes into stage 1 and every stage takes the bit of the stage // before it. Q8, the output of the last stage, is the serial output; Q6 and // Q7 are there too. The chip has no reset. module cd4014 ( input logic p8, // Pin 1 output logic q6, // Pin 2 output logic q8, // Pin 3 input logic p4, // Pin 4 input logic p3, // Pin 5 input logic p2, // Pin 6 input logic p1, // Pin 7 // Pin 8: VSS input logic parallel_serial, // Pin 9: 1 = load, 0 = shift input logic clock, // Pin 10 input logic serial_in, // Pin 11 output logic q7, // Pin 12 input logic p5, // Pin 13 input logic p6, // Pin 14 input logic p7 // Pin 15 // Pin 16: VDD ); logic [8:1] stage; always_ff @ (posedge clock) if (parallel_serial) stage <= { p8, p7, p6, p5, p4, p3, p2, p1 }; else stage <= { stage [7:1], serial_in }; assign q6 = stage [6]; assign q7 = stage [7]; assign q8 = stage [8]; endmodule
// CD4032B: triple serial adder. // // Ports in pin order, named as in the datasheet. The power pins, VSS (8) // and VDD (16), are left out. // // Each adder takes two numbers one bit per clock, lowest bit first. Its // SUM output is A XOR B XOR the stored carry, at once, without the clock; // the rising edge of CLOCK stores the carry for the next bit. INVERT at 1 // inverts the sum. The datasheet asks to clear the carries with CARRY // RESET at 1 one bit before the first bit of the next word; the model // clears them at the rising edge of CLOCK that sees CARRY RESET at 1. // The three adders share CLOCK and CARRY RESET. Synthesizable. module cd4032 ( output logic sum3, // Pin 1 input logic invert3, // Pin 2 input logic clock, // Pin 3 output logic sum2, // Pin 4 input logic invert2, // Pin 5 input logic carry_reset, // Pin 6: 1 at a rising edge = carries 0 input logic invert1, // Pin 7 // Pin 8: VSS output logic sum1, // Pin 9 input logic a1, // Pin 10 input logic b1, // Pin 11 input logic b2, // Pin 12 input logic a2, // Pin 13 input logic b3, // Pin 14 input logic a3 // Pin 15 // Pin 16: VDD ); wire [3:1] a = { a3, a2, a1 }; wire [3:1] b = { b3, b2, b1 }; wire [3:1] inv = { invert3, invert2, invert1 }; logic [3:1] carry; always_ff @ (posedge clock) if (carry_reset) carry <= '0; else carry <= a & b | (a ^ b) & carry; assign { sum3, sum2, sum1 } = a ^ b ^ carry ^ inv; endmodule
// CD4015B: dual 4-stage shift register, serial input, parallel output. // // Ports in pin order, named as in the datasheet. The power pins, VSS (8) // and VDD (16), are left out. Synthesizable. // // Each register shifts on the rising edge of its clock: DATA goes into Q1, // Q1 into Q2, Q2 into Q3, Q3 into Q4. RESET is active high and // asynchronous: it clears all four stages at once, without the clock. // Connect Q4A to DATA B to make one 8-stage register. module cd4015 ( input logic clock_b, // Pin 1 output logic q4b, // Pin 2 output logic q3a, // Pin 3 output logic q2a, // Pin 4 output logic q1a, // Pin 5 input logic reset_a, // Pin 6: 1 = Q1A-Q4A go to 0 at once input logic data_a, // Pin 7 // Pin 8: VSS input logic clock_a, // Pin 9 output logic q4a, // Pin 10 output logic q3b, // Pin 11 output logic q2b, // Pin 12 output logic q1b, // Pin 13 input logic reset_b, // Pin 14: 1 = Q1B-Q4B go to 0 at once input logic data_b // Pin 15 // Pin 16: VDD ); logic [4:1] ra, rb; // ra [1] is Q1A, ra [4] is Q4A always_ff @ (posedge clock_a or posedge reset_a) if (reset_a) ra <= '0; else ra <= { ra [3:1], data_a }; always_ff @ (posedge clock_b or posedge reset_b) if (reset_b) rb <= '0; else rb <= { rb [3:1], data_b }; assign { q4a, q3a, q2a, q1a } = ra; assign { q4b, q3b, q2b, q1b } = rb; endmodule
10SystemVerilog: the chip-level design
serial_adder_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as serial_adder.sv. Unused inputs are tied to vdd or gnd
as on the breadboard, and unused outputs go to wires named unused_…. The file is generated from the
breadboard wire list after a pin-level check of that list, so the code, the drawings and the checklist cannot
disagree.
// Exercise P1, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as serial_adder.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_p1.py, not this file. Synthesizable. module serial_adder_chips ( input logic clk, input logic load, input logic [3:0] a, input logic [3:0] b, output logic serial_a, output logic serial_b, output logic serial_sum, output logic [3:0] sum, output logic [6:0] abcdefg_a, output logic [6:0] abcdefg_b, output logic [6:0] abcdefg_sum ); // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; wire vdd = 1'b1; // Outputs that go nowhere wire unused_usa_q6; wire unused_usa_q7; wire unused_usb_q6; wire unused_usb_q7; wire unused_u32_sum3; wire unused_u32_sum2; wire unused_u15_q4b; wire unused_u15_q3b; wire unused_u15_q2b; wire unused_u15_q1b; //------------------------------------------------------------------------ // UDA: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 20 of board 1 cd4511 i_uda ( .bcd_b ( a [1] ), // Pin 1 <- button A1 .bcd_c ( a [2] ), // Pin 2 <- button A2 .lamp_test_n ( vdd ), // Pin 3 <- +5V .blank_n ( vdd ), // Pin 4 <- +5V .latch_enable ( gnd ), // Pin 5 <- GND .bcd_d ( a [3] ), // Pin 6 <- button A3 .bcd_a ( a [0] ), // Pin 7 <- button A0 .seg_e ( abcdefg_a [2] ), // Pin 9 -> A.e .seg_d ( abcdefg_a [3] ), // Pin 10 -> A.d .seg_c ( abcdefg_a [4] ), // Pin 11 -> A.c .seg_b ( abcdefg_a [5] ), // Pin 12 -> A.b .seg_a ( abcdefg_a [6] ), // Pin 13 -> A.a .seg_g ( abcdefg_a [0] ), // Pin 14 -> A.g .seg_f ( abcdefg_a [1] ) // Pin 15 -> A.f ); //------------------------------------------------------------------------ // USA: CD4014 8-stage shift register, parallel or serial in, serial out, pin 1 in column 35 of board 1 cd4014 i_usa ( .p8 ( a [0] ), // Pin 1 <- button A0 .q6 ( unused_usa_q6 ), // Pin 2, unused output .q8 ( serial_a ), // Pin 3 -> A→ .p4 ( gnd ), // Pin 4 <- GND .p3 ( gnd ), // Pin 5 <- GND .p2 ( gnd ), // Pin 6 <- GND .p1 ( gnd ), // Pin 7 <- GND .parallel_serial ( load ), // Pin 9 <- button LOAD .clock ( clk ), // Pin 10 <- 555 clock module .serial_in ( gnd ), // Pin 11 <- GND .q7 ( unused_usa_q7 ), // Pin 12, unused output .p5 ( a [3] ), // Pin 13 <- button A3 .p6 ( a [2] ), // Pin 14 <- button A2 .p7 ( a [1] ) // Pin 15 <- button A1 ); //------------------------------------------------------------------------ // UDB: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 18 of board 2 cd4511 i_udb ( .bcd_b ( b [1] ), // Pin 1 <- button B1 .bcd_c ( b [2] ), // Pin 2 <- button B2 .lamp_test_n ( vdd ), // Pin 3 <- +5V .blank_n ( vdd ), // Pin 4 <- +5V .latch_enable ( gnd ), // Pin 5 <- GND .bcd_d ( b [3] ), // Pin 6 <- button B3 .bcd_a ( b [0] ), // Pin 7 <- button B0 .seg_e ( abcdefg_b [2] ), // Pin 9 -> B.e .seg_d ( abcdefg_b [3] ), // Pin 10 -> B.d .seg_c ( abcdefg_b [4] ), // Pin 11 -> B.c .seg_b ( abcdefg_b [5] ), // Pin 12 -> B.b .seg_a ( abcdefg_b [6] ), // Pin 13 -> B.a .seg_g ( abcdefg_b [0] ), // Pin 14 -> B.g .seg_f ( abcdefg_b [1] ) // Pin 15 -> B.f ); //------------------------------------------------------------------------ // USB: CD4014 8-stage shift register, parallel or serial in, serial out, pin 1 in column 32 of board 2 cd4014 i_usb ( .p8 ( b [0] ), // Pin 1 <- button B0 .q6 ( unused_usb_q6 ), // Pin 2, unused output .q8 ( serial_b ), // Pin 3 -> B→ .p4 ( gnd ), // Pin 4 <- GND .p3 ( gnd ), // Pin 5 <- GND .p2 ( gnd ), // Pin 6 <- GND .p1 ( gnd ), // Pin 7 <- GND .parallel_serial ( load ), // Pin 9 <- button LOAD .clock ( clk ), // Pin 10 <- 555 clock module .serial_in ( gnd ), // Pin 11 <- GND .q7 ( unused_usb_q7 ), // Pin 12, unused output .p5 ( b [3] ), // Pin 13 <- button B3 .p6 ( b [2] ), // Pin 14 <- button B2 .p7 ( b [1] ) // Pin 15 <- button B1 ); //------------------------------------------------------------------------ // U32: CD4032 triple serial adder, pin 1 in column 6 of board 3 cd4032 i_u32 ( .sum3 ( unused_u32_sum3 ), // Pin 1, unused output .invert3 ( vdd ), // Pin 2 <- +5V, unused input .clock ( clk ), // Pin 3 <- 555 clock module .sum2 ( unused_u32_sum2 ), // Pin 4, unused output .invert2 ( vdd ), // Pin 5 <- +5V, unused input .carry_reset ( load ), // Pin 6 <- button LOAD .invert1 ( gnd ), // Pin 7 <- GND .sum1 ( serial_sum ), // Pin 9 -> S→ .a1 ( serial_a ), // Pin 10 <- USA pin 3 (Q8) .b1 ( serial_b ), // Pin 11 <- USB pin 3 (Q8) .b2 ( gnd ), // Pin 12 <- GND, unused input .a2 ( gnd ), // Pin 13 <- GND, unused input .b3 ( gnd ), // Pin 14 <- GND, unused input .a3 ( gnd ) // Pin 15 <- GND, unused input ); //------------------------------------------------------------------------ // U15: CD4015 dual 4-stage shift register, serial in, parallel out, pin 1 in column 18 of board 3 cd4015 i_u15 ( .clock_b ( vdd ), // Pin 1 <- +5V, unused input .q4b ( unused_u15_q4b ), // Pin 2, unused output .q3a ( sum [1] ), // Pin 3 -> Q3A .q2a ( sum [2] ), // Pin 4 -> Q2A .q1a ( sum [3] ), // Pin 5 -> Q1A .reset_a ( load ), // Pin 6 <- button LOAD .data_a ( serial_sum ), // Pin 7 <- U32 pin 9 (S1) .clock_a ( clk ), // Pin 9 <- 555 clock module .q4a ( sum [0] ), // Pin 10 -> Q4A .q3b ( unused_u15_q3b ), // Pin 11, unused output .q2b ( unused_u15_q2b ), // Pin 12, unused output .q1b ( unused_u15_q1b ), // Pin 13, unused output .reset_b ( gnd ), // Pin 14 <- GND, unused input .data_b ( gnd ) // Pin 15 <- GND, unused input ); //------------------------------------------------------------------------ // UDS: CD4511 BCD-to-7-segment latch, decoder and driver, pin 1 in column 38 of board 3 cd4511 i_uds ( .bcd_b ( sum [1] ), // Pin 1 <- U15 pin 3 (Q3A) .bcd_c ( sum [2] ), // Pin 2 <- U15 pin 4 (Q2A) .lamp_test_n ( vdd ), // Pin 3 <- +5V .blank_n ( vdd ), // Pin 4 <- +5V .latch_enable ( gnd ), // Pin 5 <- GND .bcd_d ( sum [3] ), // Pin 6 <- U15 pin 5 (Q1A) .bcd_a ( sum [0] ), // Pin 7 <- U15 pin 10 (Q4A) .seg_e ( abcdefg_sum [2] ), // Pin 9 -> S.e .seg_d ( abcdefg_sum [3] ), // Pin 10 -> S.d .seg_c ( abcdefg_sum [4] ), // Pin 11 -> S.c .seg_b ( abcdefg_sum [5] ), // Pin 12 -> S.b .seg_a ( abcdefg_sum [6] ), // Pin 13 -> S.a .seg_g ( abcdefg_sum [0] ), // Pin 14 -> S.g .seg_f ( abcdefg_sum [1] ) // Pin 15 -> S.f ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with a reference model that takes A + B at
LOAD and expects the bits of A, B and the sum, lowest first, in the streams and in the deserializer. Like the
testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock edges. It adds five chosen pairs,
one of them with LOAD held for three edges, and ten pseudo-random ones, and checks the three displays too. The output
below is shortened.
tb.sv251 lines · show the codefile
// Testbench for exercise P1. // // Runs the generic design (serial_adder.sv) and the chip-level design // (serial_adder_chips.sv) side by side and compares both with a reference // model that knows the answer in advance: at LOAD it takes a + b, and the // bit streams are the bits of the numbers and of the sum, lowest first. // Works with Icarus Verilog, Verilator (--binary) and Questa. // // Like the testbenches of Basics-Graphics-Music, it drives and samples // signals only at rising clock edges. It adds chosen and pseudo-random // pairs, holds LOAD for several edges once, and checks the bit streams, // the deserializer and the three displays at every edge. // Time units. The design files have none: Yosys does not accept timeunit // and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps // in its own testbench. This line sets the same units here and for the // design files compiled after it, so tb.sv comes first in the file list. `timescale 1ns / 1ps module tb; // load is a synchronous load and an asynchronous clear, as on the // breadboard // verilator lint_off SYNCASYNCNET logic clk; logic load = 1'b0; logic [3:0] a = '0; logic [3:0] b = '0; wire serial_a, serial_a_chips; wire serial_b, serial_b_chips; wire serial_sum, serial_sum_chips; wire [3:0] sum, sum_chips; wire [6:0] abcdefg_a, abcdefg_a_chips; wire [6:0] abcdefg_b, abcdefg_b_chips; wire [6:0] abcdefg_sum, abcdefg_sum_chips; serial_adder i_generic ( .clk ( clk ), .load ( load ), .a ( a ), .b ( b ), .serial_a ( serial_a ), .serial_b ( serial_b ), .serial_sum ( serial_sum ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); serial_adder_chips i_chips ( .clk ( clk ), .load ( load ), .a ( a ), .b ( b ), .serial_a ( serial_a_chips ), .serial_b ( serial_b_chips ), .serial_sum ( serial_sum_chips ), .sum ( sum_chips ), .abcdefg_a ( abcdefg_a_chips ), .abcdefg_b ( abcdefg_b_chips ), .abcdefg_sum ( abcdefg_sum_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns. // On the breadboard one period is about a second. initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the numbers taken at the last LOAD edge, their sum, // and the number of edges since then logic [3:0] a_loaded, b_loaded; int k; always @ (posedge clk) if (load) begin a_loaded <= a; b_loaded <= b; k <= 0; end else k <= k + 1; wire [4:0] total = 5' (a_loaded) + 5' (b_loaded); function automatic logic bit_of (input logic [4:0] value, input int i); return i >= 0 && i < 5 ? value [i] : 1'b0; endfunction // After k edges the deserializer holds the sum bits k-1 (in sum [3]) // down to k-4; LOAD at 1 clears it at once logic [3:0] sum_expected; always_comb for (int i = 0; i < 4; i ++) sum_expected [3 - i] = ~ load & bit_of (total, k - 1 - i); function automatic logic [6:0] digit (input logic [3:0] code); case (code) 4'd0: digit = 7'b111_1110; 4'd1: digit = 7'b011_0000; 4'd2: digit = 7'b110_1101; 4'd3: digit = 7'b111_1001; 4'd4: digit = 7'b011_0011; 4'd5: digit = 7'b101_1011; 4'd6: digit = 7'b001_1111; 4'd7: digit = 7'b111_0000; 4'd8: digit = 7'b111_1111; 4'd9: digit = 7'b111_0011; default: digit = 7'b000_0000; endcase endfunction wire [27:0] expected = { bit_of (5' (a_loaded), k), bit_of (5' (b_loaded), k), bit_of (total, k), sum_expected, digit (a), digit (b), digit (sum_expected) }; wire [27:0] out = { serial_a, serial_b, serial_sum, sum, abcdefg_a, abcdefg_b, abcdefg_sum }; wire [27:0] out_chips = { serial_a_chips, serial_b_chips, serial_sum_chips, sum_chips, abcdefg_a_chips, abcdefg_b_chips, abcdefg_sum_chips }; wire ok_generic = out === expected; wire ok_chips = out_chips === expected; //------------------------------------------------------------------------ // Checking at every rising clock edge, from the first LOAD on. At the // edge both designs and the model still show the clock cycle that ends // there, the same values that a flip-flop would sample. // Initial values are fine in a testbench, and the checker samples load // at the clock edge on purpose, although it clears the deserializer at // once // verilator lint_off PROCASSINIT logic was_loaded = 1'b0; logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; int n_sums = 0; // Complete sums seen in the deserializer int n_carry = 0; // Sums with a fifth bit always @ (posedge clk) begin if (load) was_loaded <= 1'b1; if (was_loaded & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1); if (~ load && k == 4) begin n_sums <= n_sums + 1; n_carry <= n_carry + (total [4] ? 1 : 0); end $display ("%5d %b %2d %2d %b %b %b %b %s %s", cycle + 1, load, a, b, serial_a, serial_b, serial_sum, sum, ok_generic ? "ok " : "WRONG ", ok_chips ? "ok" : "WRONG"); end end //------------------------------------------------------------------------ // Driving the inputs // One addition: set the buttons, press LOAD through one or more edges, // then let six edges pass task automatic add (input logic [3:0] x, input logic [3:0] y, input int load_edges); a <= x; b <= y; load <= 1'b1; repeat (load_edges) @ (posedge clk); load <= 1'b0; repeat (6) @ (posedge clk); endtask logic [31:0] seed = 32'd13; initial begin $dumpfile ("tb.vcd"); `ifdef __ICARUS__ $dumpvars (0, tb); // Without a scope Icarus also tries to dump $unit `else $dumpvars; `endif $display ("cycle LOAD A B A> B> S> 4015 generic chips"); // Before the first LOAD the registers are unknown, as after power-up repeat (2) @ (posedge clk); add ( 5, 3, 1); // The example of section 2 add ( 9, 8, 1); // 17: a fifth bit add (15, 15, 1); add ( 0, 0, 1); add ( 7, 2, 3); // LOAD held for three edges // Pseudo-random pairs, the same in every simulator for (int i = 0; i < 10; i ++) begin seed = seed * 32'd1664525 + 32'd1013904223; add (seed [31:28], seed [27:24], 1); end // Stop checking one clock edge before $finish, so that every // simulator reports the same number of checks done <= '1; @ (posedge clk); $finish; end //------------------------------------------------------------------------ final begin if (n_errors == 0 && n_sums == 15 && n_carry >= 2) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, %0d sums, %0d with a fifth bit", n_errors, n_checks, n_sums, n_carry); end endmodule
Each script compiles the files, runs the simulation, writes a log and the waveform
tb.vcd, and exits with an error unless the test passes. Run them from any directory:
./run_icarus.bash, ./run_verilator.bash or ./run_questa.bash;
./clean.bash removes what they make. The waveform opens in GTKWave or Surfer.
run_icarus.bash22 lines · show the codefile
#!/usr/bin/env bash # Exercise P1: run the testbench with Icarus Verilog 12 or later. # The output goes to icarus.log and the waveform to tb.vcd, # for GTKWave or Surfer. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.sv) if ! command -v iverilog > /dev/null then printf '%s\n' "$0: iverilog not found, install Icarus Verilog 12 or later" >&2 exit 1 fi iverilog -g2012 -o tb.vvp "${files[@]}" vvp -n tb.vvp | tee icarus.log grep -q '^PASS' icarus.log
run_verilator.bash29 lines · show the codefile
#!/usr/bin/env bash # Exercise P1: run the testbench with Verilator 5. --binary builds # a simulator with the testbench's delays; it needs a C++20 compiler. # The output goes to verilator.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.sv) # OSS CAD Suite unpacked in the home directory has Verilator too if ! command -v verilator > /dev/null && [ -x "$HOME/oss-cad-suite/bin/verilator" ] then PATH="$HOME/oss-cad-suite/bin:$PATH" fi if ! command -v verilator > /dev/null then printf '%s\n' "$0: verilator not found, install Verilator 5" >&2 exit 1 fi verilator --binary -Wall --trace --top-module tb -o tb "${files[@]}" obj_dir/tb | tee verilator.log grep -q '^PASS' verilator.log
run_questa.bash64 lines · show the codefile
#!/usr/bin/env bash # Exercise P1: run the testbench with Questa, for example the free # Questa Starter Edition that comes with Quartus Prime Lite. # The output goes to questa.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv serial_adder.sv serial_adder_chips.sv cd4511.sv cd4014.sv cd4032.sv cd4015.sv) # When vsim is not in PATH, look for it where Quartus installs it if ! command -v vsim > /dev/null then for dir in "$HOME"/intelFPGA_lite/*/questa_fse/bin \ "$HOME"/altera_lite/*/questa_fse/bin \ /opt/intelFPGA_lite/*/questa_fse/bin \ /opt/altera_lite/*/questa_fse/bin do if [ -x "$dir/vsim" ] then PATH="$dir:$PATH" break fi done fi if ! command -v vsim > /dev/null then printf '%s\n' "$0: vsim not found, install Questa or add it to PATH" >&2 exit 1 fi # The Starter Edition needs a free license file from the Intel # licensing site; Quartus keeps it in ~/flexlm/license.dat license="$HOME/flexlm/license.dat" # ${SALT_LICENSE_SERVER-} is empty when the variable is not set, # where a plain $SALT_LICENSE_SERVER would stop the script (set -u) salt_license="${SALT_LICENSE_SERVER-}" if [ -z "$salt_license" ] && [ -f "$license" ] then export SALT_LICENSE_SERVER="$license" fi # Older Questa versions read LM_LICENSE_FILE; ${LM_LICENSE_FILE-} # is empty when the variable is not set lm_license="${LM_LICENSE_FILE-}" if [ -z "$lm_license" ] && [ -f "$license" ] then export LM_LICENSE_FILE="$license" fi rm -rf work vlib work > /dev/null vlog -sv -lint "${files[@]}" vsim -c tb -do "run -all; quit -f" | tee questa.log grep -q 'PASS' questa.log
Tested with Icarus Verilog 12.0 and 14.0 (development), Verilator 5.053 with -Wall and
no warnings, and Questa Altera Starter FPGA Edition 2025.2 from Quartus Prime Lite 25.1. The code uses only what
Icarus Verilog 13 supports. This is the output, from Icarus Verilog:
VCD info: dumpfile tb.vcd opened for output.
cycle LOAD A B A> B> S> 4015 generic chips
1 0 5 3 1 1 0 0000 ok ok
2 0 5 3 0 1 0 0000 ok ok
3 0 5 3 1 0 0 0000 ok ok
4 0 5 3 0 0 1 0000 ok ok
5 0 5 3 0 0 0 1000 ok ok
6 0 5 3 0 0 0 0100 ok ok
7 1 9 8 0 0 0 0000 ok ok
8 0 9 8 1 0 1 0000 ok ok
9 0 9 8 0 0 0 1000 ok ok
10 0 9 8 0 0 0 0100 ok ok
11 0 9 8 1 1 0 0010 ok ok
12 0 9 8 0 0 1 0001 ok ok
13 0 9 8 0 0 0 1000 ok ok
14 1 15 15 0 0 0 0000 ok ok
15 0 15 15 1 1 0 0000 ok ok
16 0 15 15 1 1 1 0000 ok ok
17 0 15 15 1 1 1 1000 ok ok
18 0 15 15 1 1 1 1100 ok ok
19 0 15 15 0 0 1 1110 ok ok
20 0 15 15 0 0 0 1111 ok ok
21 1 0 0 0 0 0 0000 ok ok
22 0 0 0 0 0 0 0000 ok ok
23 0 0 0 0 0 0 0000 ok ok
24 0 0 0 0 0 0 0000 ok ok
25 0 0 0 0 0 0 0000 ok ok
26 0 0 0 0 0 0 0000 ok ok
27 0 0 0 0 0 0 0000 ok ok
28 1 7 2 0 0 0 0000 ok ok
…
105 0 5 4 0 0 0 1001 ok ok
106 0 5 4 0 0 0 0100 ok ok
tb.sv:237: $finish called at 1095000 (1ps)
PASS: 212 checks, no errors
To see that the testbench catches mistakes, break something and run it again: replace carry <= 1'b0 with carry <= carry in serial_adder.sv, or
connect CARRY RESET of the 4032 to GND instead of LOAD in serial_adder_chips.sv. The script
reports FAIL and the trace shows the lines marked WRONG.
12On an FPGA board: lab_top in BGM
The Basics-Graphics-Music (BGM) repository runs the same labs on dozens
of FPGA boards. Each lab has a lab_top module with the same ports on every board: clock, a 1 Hz
slow_clk, reset, keys, LEDs, a 7-segment display.
- Copy
labs/1_basics/1_07_shift_registerto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addserial_adder.sv. The BGM scripts compile every.svfile of the directory excepttb.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash; the first run asks which board you have.
The design runs from slow_clk, which every BGM board makes at 1 Hz, so it steps once
a second like the breadboard with the 555 module. BGM makes it in peripherals/slow_clk_gen.sv: a
counter on the board clock toggles a flip-flop, and on AMD (Xilinx) and Intel (Altera) FPGAs a BUFG or
global primitive puts that signal on the FPGA clock network, so it reaches all flip-flops like a real
clock.
Switches 3–0 are A, switches 7–4 are B and key 0 is LOAD. LEDs 3–0 show the deserializer and LEDs 6–4 the streams
of A, B and the sum. The rightmost digit shows the deserializer as the 4511 would. To run the chip-level design
instead, add serial_adder_chips.sv and the four chip models and change the module name in the
instance.
bgm_lab/lab_top.sv111 lines · show the codefile
// Exercise P1 on an FPGA board with the Basics-Graphics-Music (BGM) labs: // https://github.com/yuri-panchul/basics-graphics-music // // Copy labs/1_basics/1_07_shift_register to a new directory, replace its // lab_top.sv with this file and add serial_adder.sv. For the chip-level // design add serial_adder_chips.sv and cd4014.sv, cd4032.sv, cd4015.sv, // cd4511.sv instead, and change the module name below. // // The adder runs from slow_clk, 1 Hz on every BGM board. Switches 3-0 are // A, switches 7-4 are B, key 0 is LOAD: hold it through a tick of slow_clk. // LEDs 3-0 show the deserializer, LEDs 6-4 the bit streams of A, B and // the sum. The rightmost digit shows the deserializer as the 4511 would. // LOAD is a synchronous load for the serializers and the carry and an // asynchronous clear for the deserializer, as on the breadboard // verilator lint_off SYNCASYNCNET `include "config.svh" module lab_top # ( parameter clk_mhz = 50, w_key = 4, w_sw = 8, w_led = 8, w_digit = 8, w_gpio = 100, screen_width = 640, screen_height = 480, w_red = 4, w_green = 4, w_blue = 4, w_x = $clog2 ( screen_width ), w_y = $clog2 ( screen_height ) ) ( input clk, input slow_clk, input rst, // Keys, switches, LEDs input [w_key - 1:0] key, input [w_sw - 1:0] sw, output logic [w_led - 1:0] led, // A dynamic seven-segment display output logic [ 7:0] abcdefgh, output logic [w_digit - 1:0] digit, // Graphics input [w_x - 1:0] x, input [w_y - 1:0] y, output logic [w_red - 1:0] red, output logic [w_green - 1:0] green, output logic [w_blue - 1:0] blue, // Microphone, sound output and UART input [ 23:0] mic, output [ 15:0] sound, input uart_rx, output uart_tx, // General-purpose Input/Output inout [w_gpio - 1:0] gpio ); //------------------------------------------------------------------------ assign red = '0; assign green = '0; assign blue = '0; assign sound = '0; assign uart_tx = '1; //------------------------------------------------------------------------ logic serial_a, serial_b, serial_sum; logic [3:0] sum; logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder i_serial_adder ( .clk ( slow_clk ), .load ( key [0] ), .a ( sw [3:0] ), .b ( sw [7:4] ), .serial_a ( serial_a ), .serial_b ( serial_b ), .serial_sum ( serial_sum ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); assign led = w_led' ({ serial_sum, serial_b, serial_a, sum }); assign abcdefgh = { abcdefg_sum, 1'b0 }; assign digit = w_digit' (1); endmodule
13On an FPGA board: hackathon_top in BGM
The hackathon setup of BGM uses a Tang Nano 9K with a 480×272 screen and a TM1638 board of keys,
LEDs and digits. Its top module, hackathon_top, has simpler ports; the first line of the file names
the board, so the scripts need no questions.
- Copy
hackathon/problems/5_shift_register. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addserial_adder.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Eight keys are not enough for two 4-bit numbers and LOAD, so the numbers have 3 bits here: keys 2–0 are A, keys 5–3 are B and key 7 is LOAD. The screen shows the three streams on the left and the deserializer on the right.
bgm_hackathon/hackathon_top.sv96 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise P1 in the hackathon setup of Basics-Graphics-Music (BGM): // https://github.com/yuri-panchul/basics-graphics-music // // Copy a directory such as hackathon/problems/5_shift_register, replace its // hackathon_top.sv with this file and add serial_adder.sv. // // The adder runs from slow_clock. Eight keys are not enough for two 4-bit // numbers and LOAD, so A and B are 3 bits here: keys 2-0 are A, keys 5-3 // are B and key 7 is LOAD. The LEDs and the rightmost digit are as in // lab_top. The screen shows the bit streams of A, B and the sum as three // lamps on the left and the deserializer as four lamps on the right. // LOAD is a synchronous load for the serializers and the carry and an // asynchronous clear for the deserializer, as on the breadboard // verilator lint_off SYNCASYNCNET module hackathon_top ( input logic clock, input logic slow_clock, input logic reset, input logic [7:0] key, output logic [7:0] led, // A dynamic seven-segment display output logic [7:0] abcdefgh, output logic [7:0] digit, // LCD screen interface input logic [8:0] x, input logic [8:0] y, output logic [4:0] red, output logic [5:0] green, output logic [4:0] blue, inout logic [3:0] gpio ); //------------------------------------------------------------------------ logic serial_a, serial_b, serial_sum; logic [3:0] sum; logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder i_serial_adder ( .clk ( slow_clock ), .load ( key [7] ), .a ( { 1'b0, key [2:0] } ), .b ( { 1'b0, key [5:3] } ), .serial_a ( serial_a ), .serial_b ( serial_b ), .serial_sum ( serial_sum ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); assign led = { 1'b0, serial_sum, serial_b, serial_a, sum }; assign abcdefgh = { abcdefg_sum, 1'b0 }; assign digit = 8'b0000_0001; //------------------------------------------------------------------------ // The streams A, B, S on the left, top to bottom; the deserializer on // the right, bit 3 first. Bright for 1, dim for 0. wire [2:0] stream = { serial_a, serial_b, serial_sum }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 3; i ++) if (x >= 60 & x < 100 & y >= 9' (56 + i * 60) & y < 9' (96 + i * 60)) begin green = stream [2 - i] ? 40 : 8; blue = stream [2 - i] ? 31 : 6; end for (int i = 0; i < 4; i ++) if (x >= 9' (200 + i * 60) & x < 9' (240 + i * 60) & y >= 116 & y < 156) green = sum [3 - i] ? 63 : 12; end endmodule
14On a chip: Tiny Tapeout
Tiny Tapeout makes real chips from student designs. Each design
is a module with the ports below; on the demo board ui_in comes from DIP switches and
uo_out drives a 7-segment display.
The demo board makes the project clock itself and can set it anywhere from 1 Hz to
66.5 MHz (see Clock in the Tiny Tapeout docs). Set it to
1 Hz, and project.v connects clk straight to the adder, just as the 555 module clocks the
chips on the breadboard.
DIP switches 0–3 are A and switches 4–7 are B. All eight are taken, so LOAD comes from BIDIR pin 0: connect it to VCC through one tick of the clock. The display shows the deserializer as a 4511 would, and its dot is the sum bit on its way. The deserializer and the streams also go to BIDIR pins 2–7.
tiny_tapeout/project.v64 lines · show the codefile
/* * Exercise P1 on Tiny Tapeout: a bit-serial adder. The 7-segment display * of the demo board shows the deserializer as a 4511 would. * * Add serial_adder.sv to source_files in info.yaml next to project.v. * Rename the module to tt_um_<your GitHub name>_<project name> and use the * same name as top_module in info.yaml. */ `default_nettype none // LOAD is a synchronous load for the serializers and the carry and an // asynchronous clear for the deserializer, as on the breadboard // verilator lint_off SYNCASYNCNET module tt_um_example ( input wire [7:0] ui_in, // DIP switches, INPUT Pmod, RP2350 GPIO17-24 output wire [7:0] uo_out, // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40 input wire [7:0] uio_in, // BIDIR Pmod, RP2350 GPIO25-32 output wire [7:0] uio_out, // (same pins) output wire [7:0] uio_oe, // 1 = your design drives the pin input wire ena, // set by the on-chip project mux, not a board pin input wire clk, // RP2350 GPIO16 input wire rst_n // RP2350 GPIO14 ); // The demo board makes the project clock; set it to 1 Hz. DIP switches // 0-3 are A and switches 4-7 are B. All eight are taken, so LOAD comes // from BIDIR pin 0: connect it to VCC through one tick of the clock. wire serial_a, serial_b, serial_sum; wire [3:0] sum; wire [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder i_serial_adder ( .clk ( clk ), .load ( uio_in [0] ), .a ( ui_in [3:0] ), .b ( ui_in [7:4] ), .serial_a ( serial_a ), .serial_b ( serial_b ), .serial_sum ( serial_sum ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g; the // dot is the sum bit on its way assign uo_out = { serial_sum, abcdefg_sum [0], abcdefg_sum [1], abcdefg_sum [2], abcdefg_sum [3], abcdefg_sum [4], abcdefg_sum [5], abcdefg_sum [6] }; // The deserializer and the streams of A and B on BIDIR pins 2-7 assign uio_out = { sum, serial_b, serial_a, 2'b0 }; assign uio_oe = 8'b1111_1100; // List all unused inputs to prevent warnings wire _unused = & { ena, rst_n, uio_in [7:1], abcdefg_a, abcdefg_b, 1'b0 }; endmodule
In info.yaml set the clock to 1 Hz, list the files and use the module name as the top module:
project:
clock_hz: 1 # 1 Hz from the demo board
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_serial_adder
source_files:
- "project.v"
- "serial_adder.sv"
slow_clk with a counter,
but on AMD and Intel FPGAs it routes that signal onto the clock network with BUFG or
global (section 12). In an ASIC a counter bit used as a clock is bad practice: it is
a second clock with its own clock tree, its edges come later than those of clk by a delay nobody
controls, and every signal between the two clocks crosses clock domains. Here the board makes the 1 Hz clock,
so no divider is needed. When a chip needs a slow tick from a fast clock, keep one clock and add a clock enable: a
pulse one clock long that lets the slow logic change only on the edges where it is 1.15Going further
Tasks for students
- Gates instead of the 4032. Exercise P2 replaces the 4032 by a full adder from gates and a 4013 for the carry: that is what is inside the 4032.
- Hold the result. Add a counter that counts four clocks after LOAD and then stops the deserializer, so that the sum stays on the display. Which chips would you use?
- Eight bits. Use all eight stages of the 4014s and both halves of the 4015. How many clocks does an addition take now?
- Subtraction. INVERT of the 4032 complements the sum. How can a serial adder subtract? Think of B inverted and a carry of 1 at the start.
- In SystemVerilog: make the width a parameter and compare the size of a serial adder and a parallel one after synthesis.
Files of this exercise
- serial_adder.sv: the generic design (section 8)
- cd4511.sv, cd4014.sv, cd4032.sv and cd4015.sv: chip models (section 9)
- serial_adder_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
Sources
The exercise is in the Projects list of “An improved way of teaching school kids digital electronics” and in lesson 1.4 of the Habr article about the electronics course, as the task for the third group. Pictures 12 to 14 of “Teaching children electronics is due for a major redesign” show the adder demos of 11-year-old Matvei and the diagram of the bit-serial adder. The chips are described in the Texas Instruments datasheets for the CD4014B, CD4015B and CD4511B and the STMicroelectronics datasheet for the HCF4032B.