Exercise P2 · project · serial arithmetic
Bit-serial adder from gates and a flip-flop for the carry
The bit-serial adder of exercise P1, with its 4032 opened up: a full adder from five gates and one D flip-flop that keeps the carry from one clock to the next. This is what the 4032 has inside, built by hand.
1What it is and why it matters
What it does. The same as exercise P1: buttons give A and B, LOAD through one blink of the clock module puts them into two 4014 serializers, and after four more blinks the sum display shows A + B. The difference is in the middle: the bits are added by gates, and the carry waits in a flip-flop.
How it works. The full adder of exercise M2 adds the bit of A, the bit of B and the carry C: P = A XOR B, the sum S = P XOR C, and the new carry C' = A AND B OR P AND C. Flip-flop 1 of a 4013 stores C' at every rising edge of the clock, so the carry of one bit is added to the next bit one clock later. LOAD resets the flip-flop at once, so every new pair of numbers starts with a carry of 0.
Where it is used. A full adder with a carry register is the arithmetic unit of every bit-serial machine, from early computers to the serial multipliers in today's signal-processing chips. In a parallel adder the same full adder is repeated for every bit and the carry flows through wires instead of through time.
Why learn it.
- It opens a chip: the 4032 of P1 turns out to be gates and a flip-flop the students already know.
- It shows the difference between a carry in space (exercise M3, B8) and a carry in time (here).
- It is the largest team project of the list: ten chips on three boards.
2The key elements: a full adder and a carry flip-flop
The full adder for all eight inputs:
| A | B | C | P = A⊕B | S = P⊕C | G = A·B | T = P·C | C' = G + T |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
The flip-flop turns the last column into the C of the next clock. With A = 5 and B = 3 the carries are 1, 1, 1 and then 0, and the sum bits 0, 0, 0, 1: the sum 8, as in the table of exercise P1. The display shows it after the fourth edge, for one period of the clock; choose A + B of at most 9, because the 4511 shows digits 0 to 9 only.
Waveforms
tb.sv in Icarus Verilog: 5 + 3, then 9 + 8. C is the carry
flip-flop: LOAD clears it at once, and every edge stores the carry of the bit just added. · 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) |
| CD4013 | dual D flip-flop: flip-flop 1 keeps the carry | 1 | $0.35 | Elexp (elexp.com) |
| CD4071 | quad OR: the carry out (one of four used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4081 | quad AND: G and T (two of four used) | 1 | $0.55 | Elexp (elexp.com) |
| CD4070 | quad XOR: P and S (two of four used) | 1 | $0.45 | Elexp (elexp.com) |
| 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 | 127 wires on the board (or male-male Dupont jumpers) | 127 | $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 P1: P8, P7, P6, P5 (pins 1, 15, 14, 13) get bits 0 to 3 of the number, PAR/SER (pin 9) is LOAD, CLOCK is pin 10, the stream comes out of Q8 (pin 3), and P4–P1 and SERIAL IN are at GND.
| 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 |
4070: quad 2-input XOR
Two gates: P = A XOR B and S = P XOR C. The inputs of the other two are tied to GND.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | B→ |
| 2 | B | input | input of the gate with its output on pin 3 | A→ |
| 3 | J | output | J = A XOR B | P |
| 4 | K | output | K = C XOR D | S→ |
| 5 | C | input | input of the gate with its output on pin 4 | C |
| 6 | D | input | input of the gate with its output on pin 4 | P |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to GND |
| 9 | F | input | input of the gate with its output on pin 10 | tied to GND |
| 10 | L | output | L = E XOR F | not connected |
| 11 | M | output | M = G XOR H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to GND |
| 13 | H | input | input of the gate with its output on pin 11 | tied to GND |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4081: quad 2-input AND
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | tied to +5V |
| 2 | B | input | input of the gate with its output on pin 3 | tied to +5V |
| 3 | J | output | J = A AND B | not connected |
| 4 | K | output | K = C AND D | G |
| 5 | C | input | input of the gate with its output on pin 4 | B→ |
| 6 | D | input | input of the gate with its output on pin 4 | A→ |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to GND |
| 9 | F | input | input of the gate with its output on pin 10 | tied to GND |
| 10 | L | output | L = E AND F | not connected |
| 11 | M | output | M = G AND H | T |
| 12 | G | input | input of the gate with its output on pin 11 | P |
| 13 | H | input | input of the gate with its output on pin 11 | C |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4071: quad 2-input OR
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | tied to GND |
| 2 | B | input | input of the gate with its output on pin 3 | tied to GND |
| 3 | J | output | J = A OR B | not connected |
| 4 | K | output | K = C OR D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | T |
| 9 | F | input | input of the gate with its output on pin 10 | G |
| 10 | L | output | L = E OR F | C' |
| 11 | M | output | M = G OR H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4013: dual D flip-flop
As in exercise B4. Flip-flop 1 keeps the carry: D1 (pin 5) gets C', Q1 (pin 1) is C, CLOCK1 (pin 3) is the clock and RESET1 (pin 4) is LOAD. SET1 (pin 6) is at GND, and the inputs of flip-flop 2 are tied to the rails.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | Q1 | output | flip-flop 1 output | C |
| 2 | Q1 | output | flip-flop 1 output, inverted | not connected |
| 3 | CLOCK1 | input | flip-flop 1 stores D1 on the rising edge, 0 to 1 | CLK |
| 4 | RESET1 | input | 1 sets Q1 = 0 at once, without the clock. Active high | LOAD |
| 5 | D1 | input | flip-flop 1 data input | C' |
| 6 | SET1 | input | 1 sets Q1 = 1 at once, without the clock. Active high | GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | SET2 | input | 1 sets Q2 = 1 at once. Active high | tied to +5V |
| 9 | D2 | input | flip-flop 2 data input | tied to +5V |
| 10 | RESET2 | input | 1 sets Q2 = 0 at once. Active high | tied to +5V |
| 11 | CLOCK2 | input | flip-flop 2 stores D2 on the rising edge | tied to +5V |
| 12 | Q2 | output | flip-flop 2 output, inverted | not connected |
| 13 | Q2 | output | flip-flop 2 output | not connected |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4015: dual 4-stage shift register
As in exercise P1: register A collects the sum bits, and LOAD clears it at once.
| 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
| 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, gates, deserializer
serial_adder_gates.sv describes.
The line along the bottom brings the carry C back from the flip-flop to the gates. · SVG · PNG6Schematic 2: connections between the chips
| Net | From | To | Wire |
|---|---|---|---|
| CLK | 555 clock module, OUT | 4014 A 10 (CLK), 4014 B 10 (CLK), 4013 3 (CLK1), 4015 9 (CLKA) | M-F, M-M |
| LOAD | button LOAD, OUT | 4014 A 9 (P/S), 4014 B 9 (P/S), 4013 4 (R1), 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) | 4070 2 (B), 4081 6 (D) | M-M (loose jumper) |
| B→ | 4014 B 3 (Q8) | 4070 1 (A), 4081 5 (C) | M-M |
| P | 4070 3 (J) | 4070 6 (D), 4081 12 (G) | M-M |
| G | 4081 4 (K) | 4071 9 (F) | M-M |
| T | 4081 11 (M) | 4071 8 (E) | M-M |
| C' | 4071 10 (L) | 4013 5 (D1) | M-M (loose jumper) |
| C | 4013 1 (Q1) | 4070 5 (C), 4081 13 (H) | M-M (loose jumper) |
| S→ | 4070 4 (K) | 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), 4013 14 (VDD), 4071 14 (VDD), 4081 14 (VDD), 4070 14 (VDD), 4015 16 (VDD), 4511 S 16 (VDD), 4511 A 4 (BI), 4511 A 3 (LT), 4511 B 4 (BI), 4511 B 3 (LT), 4013 11 (CLK2), 4013 10 (R2), 4013 9 (D2), 4013 8 (S2), 4071 13 (H), 4071 12 (G), 4081 2 (B), 4081 1 (A), 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), 4013 7 (VSS), 4071 7 (VSS), 4081 7 (VSS), 4070 7 (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), 4013 6 (S1), 4071 1 (A), 4071 2 (B), 4071 5 (C), 4071 6 (D), 4081 8 (E), 4081 9 (F), 4070 8 (E), 4070 9 (F), 4070 12 (G), 4070 13 (H), 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), 4013 2 (Q1), 4013 12 (Q2), 4013 13 (Q2), 4071 3 (J), 4071 4 (K), 4071 11 (M), 4081 3 (J), 4081 10 (L), 4070 10 (L), 4070 11 (M), 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 as in exercise P1: one student per board. Agree first on the wires between the boards: the clock, LOAD, the two streams, the carry C and C'.
- 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 4013, notch to the left, pin 1 in hole 43f of board 2; the 4071, notch to the left, pin 1 in hole 4f of board 3; the 4081 turned around, notch to the right, pin 1 in hole 18e of board 3; the 4070 turned around, notch to the right, pin 1 in hole 27e of board 3; the 4015 turned around, notch to the right, pin 1 in hole 37e of board 3; the 4511 S turned around, notch to the right, pin 1 in hole 47e 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 −) · 20 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 2: 43a (4013 pin 14, VDD) | board 2: top + rail, column 43 | |
| 18 | GND | board 2: 49j (4013 pin 7, VSS) | board 2: bottom − rail, column 49 | |
| 19 | +5V | board 3: 4a (4071 pin 14, VDD) | board 3: top + rail, column 4 | |
| 20 | GND | board 3: 10j (4071 pin 7, VSS) | board 3: bottom − rail, column 10 | |
| 21 | +5V | board 3: 18j (4081 pin 14, VDD) | board 3: bottom + rail, column 18 | |
| 22 | GND | board 3: 12a (4081 pin 7, VSS) | board 3: top − rail, column 12 | |
| 23 | +5V | board 3: 27j (4070 pin 14, VDD) | board 3: bottom + rail, column 27 | |
| 24 | GND | board 3: 21a (4070 pin 7, VSS) | board 3: top − rail, column 21 | |
| 25 | +5V | board 3: 37j (4015 pin 16, VDD) | board 3: bottom + rail, column 37 | |
| 26 | GND | board 3: 30a (4015 pin 8, VSS) | board 3: top − rail, column 30 | |
| 27 | +5V | board 3: 47j (4511 S pin 16, VDD) | board 3: bottom + rail, column 47 | |
| 28 | GND | board 3: 40a (4511 S pin 8, VSS) | board 3: top − rail, column 40 | |
| Fixed inputs: CMOS inputs must not float · 44 wires | ||||
| 29 | GND | board 1: 10j (display A pin 3, CC) | board 1: bottom − rail, column 10 | |
| 30 | GND | board 1: 23a (4511 A pin 5, LE) | board 1: top − rail, column 23 | |
| 31 | +5V | board 1: 24a (4511 A pin 4, BI) | board 1: top + rail, column 24 | |
| 32 | +5V | board 1: 25a (4511 A pin 3, LT) | board 1: top + rail, column 25 | |
| 33 | GND | board 1: 38j (4014 A pin 4, P4) | board 1: bottom − rail, column 38 | |
| 34 | GND | board 1: 39j (4014 A pin 5, P3) | board 1: bottom − rail, column 39 | |
| 35 | GND | board 1: 40a (4014 A pin 11, SER) | board 1: top − rail, column 40 | |
| 36 | GND | board 1: 40j (4014 A pin 6, P2) | board 1: bottom − rail, column 40 | |
| 37 | GND | board 1: 41j (4014 A pin 7, P1) | board 1: bottom − rail, column 41 | |
| 38 | GND | board 2: 8j (display B pin 3, CC) | board 2: bottom − rail, column 8 | |
| 39 | GND | board 2: 21a (4511 B pin 5, LE) | board 2: top − rail, column 21 | |
| 40 | +5V | board 2: 22a (4511 B pin 4, BI) | board 2: top + rail, column 22 | |
| 41 | +5V | board 2: 23a (4511 B pin 3, LT) | board 2: top + rail, column 23 | |
| 42 | GND | board 2: 33a (4014 B pin 7, P1) | board 2: top − rail, column 33 | |
| 43 | GND | board 2: 34a (4014 B pin 6, P2) | board 2: top − rail, column 34 | |
| 44 | GND | board 2: 34j (4014 B pin 11, SER) | board 2: bottom − rail, column 34 | |
| 45 | GND | board 2: 35a (4014 B pin 5, P3) | board 2: top − rail, column 35 | |
| 46 | GND | board 2: 36a (4014 B pin 4, P4) | board 2: top − rail, column 36 | |
| 47 | +5V | board 2: 46a (4013 pin 11, CLK2) | board 2: top + rail, column 46 | |
| 48 | +5V | board 2: 47a (4013 pin 10, R2) | board 2: top + rail, column 47 | |
| 49 | +5V | board 2: 48a (4013 pin 9, D2) | board 2: top + rail, column 48 | |
| 50 | GND | board 2: 48j (4013 pin 6, S1) | board 2: bottom − rail, column 48 | |
| 51 | +5V | board 2: 49a (4013 pin 8, S2) | board 2: top + rail, column 49 | |
| 52 | GND | board 3: 4j (4071 pin 1, A) | board 3: bottom − rail, column 4 | |
| 53 | +5V | board 3: 5a (4071 pin 13, H) | board 3: top + rail, column 5 | |
| 54 | GND | board 3: 5j (4071 pin 2, B) | board 3: bottom − rail, column 5 | |
| 55 | +5V | board 3: 6a (4071 pin 12, G) | board 3: top + rail, column 6 | |
| 56 | GND | board 3: 8j (4071 pin 5, C) | board 3: bottom − rail, column 8 | |
| 57 | GND | board 3: 9j (4071 pin 6, D) | board 3: bottom − rail, column 9 | |
| 58 | GND | board 3: 12j (4081 pin 8, E) | board 3: bottom − rail, column 12 | |
| 59 | GND | board 3: 13j (4081 pin 9, F) | board 3: bottom − rail, column 13 | |
| 60 | +5V | board 3: 17a (4081 pin 2, B) | board 3: top + rail, column 17 | |
| 61 | +5V | board 3: 18a (4081 pin 1, A) | board 3: top + rail, column 18 | |
| 62 | GND | board 3: 21j (4070 pin 8, E) | board 3: bottom − rail, column 21 | |
| 63 | GND | board 3: 22j (4070 pin 9, F) | board 3: bottom − rail, column 22 | |
| 64 | GND | board 3: 25j (4070 pin 12, G) | board 3: bottom − rail, column 25 | |
| 65 | GND | board 3: 26j (4070 pin 13, H) | board 3: bottom − rail, column 26 | |
| 66 | GND | board 3: 35j (4015 pin 14, RB) | board 3: bottom − rail, column 35 | |
| 67 | GND | board 3: 36j (4015 pin 15, DB) | board 3: bottom − rail, column 36 | |
| 68 | +5V | board 3: 37a (4015 pin 1, CLKB) | board 3: top + rail, column 37 | |
| 69 | GND | board 3: 43a (4511 S pin 5, LE) | board 3: top − rail, column 43 | |
| 70 | +5V | board 3: 44a (4511 S pin 4, BI) | board 3: top + rail, column 44 | |
| 71 | +5V | board 3: 45a (4511 S pin 3, LT) | board 3: top + rail, column 45 | |
| 72 | GND | board 3: 57j (display A + B pin 3, CC) | board 3: bottom − rail, column 57 | |
| 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: 55b | board 3: 51b | |
| RfS | 560 Ω | board 3: 56a | board 3: 52a | |
| RaS | 560 Ω | board 3: 58c | board 3: 53c | |
| ReS | 560 Ω | board 3: 55j | board 3: 51j | |
| RdS | 560 Ω | board 3: 56i | board 3: 52i | |
| RbS | 560 Ω | board 3: 59a | board 3: 63a | |
| RcS | 560 Ω | board 3: 58j | board 3: 62j | |
| Signal wires · 55 wires | ||||
| 73 | A.a | board 1: 24i (4511 A pin 13, a) | board 1: 15d (RaA) | |
| 74 | A.b | board 1: 23h (4511 A pin 12, b) | board 1: 16e (RbA) | |
| 75 | A.c | board 1: 22g (4511 A pin 11, c) | board 1: 15f (RcA) | |
| 76 | A.d | board 1: 21g (4511 A pin 10, d) | board 1: 14g (RdA) | |
| 77 | A.e | board 1: 20j (4511 A pin 9, e) | board 1: 4i (ReA) | |
| 78 | A.f (loose jumper) | board 1: 26h (4511 A pin 15, f) | board 1: 14d (RfA) | |
| 79 | A.g (loose jumper) | board 1: 25h (4511 A pin 14, g) | board 1: 4c (RgA) | |
| 80 | B.a | board 2: 22i (4511 B pin 13, a) | board 2: 13d (RaB) | |
| 81 | B.b | board 2: 21h (4511 B pin 12, b) | board 2: 14e (RbB) | |
| 82 | B.c | board 2: 20g (4511 B pin 11, c) | board 2: 13f (RcB) | |
| 83 | B.d | board 2: 19g (4511 B pin 10, d) | board 2: 12g (RdB) | |
| 84 | B.e | board 2: 18j (4511 B pin 9, e) | board 2: 2i (ReB) | |
| 85 | B.f | board 2: 24g (4511 B pin 15, f) | board 2: 12d (RfB) | |
| 86 | B.g (loose jumper) | board 2: 23h (4511 B pin 14, g) | board 2: 2c (RgB) | |
| 87 | S.a (loose jumper) | board 3: 44h (4511 S pin 13, a) | board 3: 53d (RaS) | |
| 88 | S.b (loose jumper) | board 3: 43h (4511 S pin 12, b) | board 3: 63c (RbS) | |
| 89 | S.c | board 3: 42j (4511 S pin 11, c) | board 3: 62i (RcS) | |
| 90 | S.d | board 3: 41g (4511 S pin 10, d) | board 3: 52f (RdS) | |
| 91 | S.e | board 3: 40g (4511 S pin 9, e) | board 3: 51g (ReS) | |
| 92 | S.f | board 3: 46h (4511 S pin 15, f) | board 3: 52d (RfS) | |
| 93 | S.g | board 3: 45i (4511 S pin 14, g) | board 3: 51c (RgS) | |
| 94 | CLK | board 1: 41d (4014 A pin 10, CLK) | board 2: 45g (4013 pin 3, CLK1) | |
| 95 | CLK | board 2: 45h (4013 pin 3, CLK1) | board 2: 33g (4014 B pin 10, CLK) | |
| 96 | CLK | board 2: 33h (4014 B pin 10, CLK) | board 3: 30g (4015 pin 9, CLKA) | |
| 97 | LOAD | board 1: 42c (4014 A pin 9, P/S) | board 2: 46g (4013 pin 4, R1) | |
| 98 | LOAD | board 2: 46h (4013 pin 4, R1) | board 2: 32h (4014 B pin 9, P/S) | |
| 99 | LOAD | board 2: 32g (4014 B pin 9, P/S) | board 3: 32d (4015 pin 6, RA) | |
| 100 | A→ (loose jumper) | board 1: 37h (4014 A pin 3, Q8) | board 3: 26c (4070 pin 2, B) | |
| 101 | A→ | board 3: 26b (4070 pin 2, B) | board 3: 13b (4081 pin 6, D) | |
| 102 | B→ | board 2: 37c (4014 B pin 3, Q8) | board 3: 27d (4070 pin 1, A) | |
| 103 | B→ | board 3: 27a (4070 pin 1, A) | board 3: 14a (4081 pin 5, C) | |
| 104 | P | board 3: 25d (4070 pin 3, J) | board 3: 22d (4070 pin 6, D) | |
| 105 | P | board 3: 22c (4070 pin 6, D) | board 3: 16g (4081 pin 12, G) | |
| 106 | S→ | board 3: 24c (4070 pin 4, K) | board 3: 31c (4015 pin 7, DA) | |
| 107 | G | board 3: 15d (4081 pin 4, K) | board 3: 9d (4071 pin 9, F) | |
| 108 | T | board 3: 15g (4081 pin 11, M) | board 3: 10c (4071 pin 8, E) | |
| 109 | C' (loose jumper) | board 3: 8c (4071 pin 10, L) | board 2: 47h (4013 pin 5, D1) | |
| 110 | C (loose jumper) | board 2: 43h (4013 pin 1, Q1) | board 3: 23b (4070 pin 5, C) | |
| 111 | C | board 3: 23c (4070 pin 5, C) | board 3: 17g (4081 pin 13, H) | |
| 112 | A0 | board 1: 35g (4014 A pin 1, P8) | board 1: 21b (4511 A pin 7, A) | |
| 113 | A1 | board 1: 36d (4014 A pin 15, P7) | board 1: 27d (4511 A pin 1, B) | |
| 114 | A2 | board 1: 37c (4014 A pin 14, P6) | board 1: 26c (4511 A pin 2, C) | |
| 115 | A3 | board 1: 38b (4014 A pin 13, P5) | board 1: 22b (4511 A pin 6, D) | |
| 116 | B0 | board 2: 39d (4014 B pin 1, P8) | board 2: 19d (4511 B pin 7, A) | |
| 117 | B0 | board 2: 39c (4014 B pin 1, P8) | board 1: 43b | |
| 118 | B1 | board 2: 38h (4014 B pin 15, P7) | board 2: 25b (4511 B pin 1, B) | |
| 119 | B1 | board 2: 38i (4014 B pin 15, P7) | board 1: 34b | |
| 120 | B2 | board 2: 37i (4014 B pin 14, P6) | board 2: 24a (4511 B pin 2, C) | |
| 121 | B2 | board 2: 37j (4014 B pin 14, P6) | board 1: 31e | |
| 122 | B3 (loose jumper) | board 2: 36h (4014 B pin 13, P5) | board 2: 20c (4511 B pin 6, D) | |
| 123 | B3 | board 2: 36j (4014 B pin 13, P5) | board 1: 28e | |
| 124 | Q1A | board 3: 33d (4015 pin 5, Q1A) | board 3: 42d (4511 S pin 6, D) | |
| 125 | Q2A | board 3: 34c (4015 pin 4, Q2A) | board 3: 46c (4511 S pin 2, C) | |
| 126 | Q3A | board 3: 35b (4015 pin 3, Q3A) | board 3: 47b (4511 S pin 1, B) | |
| 127 | Q4A | board 3: 31g (4015 pin 10, Q4A) | board 3: 41a (4511 S pin 7, A) | |
| Modules on male-female jumpers · 30 wires | ||||
| 128 | +5V | button A3, + | board 1: top + rail, column 4 | |
| 129 | A3 | button A3, OUT | board 1: 22a (4511 A pin 6, D) | |
| 130 | GND | button A3, − | board 1: top − rail, column 6 | |
| 131 | +5V | button A2, + | board 1: top + rail, column 9 | |
| 132 | A2 | button A2, OUT | board 1: 26a (4511 A pin 2, C) | |
| 133 | GND | button A2, − | board 1: top − rail, column 11 | |
| 134 | +5V | button A1, + | board 1: top + rail, column 14 | |
| 135 | A1 | button A1, OUT | board 1: 27a (4511 A pin 1, B) | |
| 136 | GND | button A1, − | board 1: top − rail, column 16 | |
| 137 | +5V | button A0, + | board 1: top + rail, column 19 | |
| 138 | A0 | button A0, OUT | board 1: 21a (4511 A pin 7, A) | |
| 139 | GND | button A0, − | board 1: top − rail, column 21 | |
| 140 | +5V | button B3, + | board 1: top + rail, column 26 | |
| 141 | B3 | button B3, OUT | board 1: 28a | |
| 142 | GND | button B3, − | board 1: top − rail, column 27 | |
| 143 | +5V | button B2, + | board 1: top + rail, column 30 | |
| 144 | B2 | button B2, OUT | board 1: 31a | |
| 145 | GND | button B2, − | board 1: top − rail, column 32 | |
| 146 | +5V | button B1, + | board 1: top + rail, column 34 | |
| 147 | B1 | button B1, OUT | board 1: 34a | |
| 148 | GND | button B1, − | board 1: top − rail, column 37 | |
| 149 | +5V | button B0, + | board 1: top + rail, column 40 | |
| 150 | B0 | button B0, OUT | board 1: 43a | |
| 151 | GND | button B0, − | board 1: top − rail, column 42 | |
| 152 | +5V | button LOAD, + | board 1: top + rail, column 46 | |
| 153 | LOAD | button LOAD, OUT | board 1: 42a (4014 A pin 9, P/S) | |
| 154 | GND | button LOAD, − | board 1: top − rail, column 48 | |
| 155 | GND | 555 clock module, − | board 1: top − rail, column 52 | |
| 156 | CLK | 555 clock module, OUT | board 1: 41a (4014 A pin 10, CLK) | |
| 157 | +5V | 555 clock module, + | board 1: top + rail, column 54 | |
Test it
- Test each board on its own first: the displays A and B must follow their buttons.
- Hold A2 and A0 (5) and B1 and B0 (3), press LOAD through one blink, and watch four more: the sum display shows 0, 0, 0 and then 8.
- Try 2 + 2, 4 + 5, 1 + 7: every one needs carries.
If it does not work: if the sum is right only without carries, as in 1 + 2, the carry does not come back: check C' to D1 and Q1 to the gates. If a sum goes wrong only when you press LOAD in the middle of an addition, RESET1 does not get LOAD. If the sum is always 0, check the stream wires from the two Q8s.
8SystemVerilog: the generic design
serial_adder_gates.sv is the design of P1 with the full adder written as gates, one line per gate, and
the carry as a flip-flop with an asynchronous reset, like the 4013. It is synthesizable.
// Exercise P2: the bit-serial adder of P1, with the 4032 replaced by a full // adder from gates and a D flip-flop for the carry. // // 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. At every // later edge each serializer moves one bit out, lowest first, then zeros; // the full adder adds the two bits and the carry, and the deserializer // takes the sum bit. load clears the carry flip-flop and the deserializer // at once, as RESET of the 4013 and of the 4015 do. 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_gates ( 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 output logic carry, // The carry flip-flop, like Q1 of the 4013 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 full adder of exercise M2 and a flip-flop for the carry wire p = serial_a ^ serial_b; // 4070 wire g = serial_a & serial_b; // 4081 wire t = p & carry; // 4081 assign serial_sum = p ^ carry; // 4070 always_ff @ (posedge clk or posedge load) if (load) carry <= 1'b0; else carry <= g | t; // 4071, into D1 of the 4013 //------------------------------------------------------------------------ // 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
// CD4013B: dual D flip-flop with set and reset. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. // // Each flip-flop stores D at the rising edge of its clock. SET and RESET // are active high and asynchronous: they act at once, without the clock. // With SET and RESET both high the chip sets both Q and ~Q high; the model // does the same while they are both high. Do not release them at the same // moment: which of the two wins is not defined. module cd4013 ( output logic q1, // Pin 1 output logic q1_n, // Pin 2: ~Q1 input logic clock1, // Pin 3 input logic reset1, // Pin 4: 1 = Q1 goes to 0 at once input logic d1, // Pin 5 input logic set1, // Pin 6: 1 = Q1 goes to 1 at once // Pin 7: VSS input logic set2, // Pin 8 input logic d2, // Pin 9 input logic reset2, // Pin 10 input logic clock2, // Pin 11 output logic q2_n, // Pin 12: ~Q2 output logic q2 // Pin 13 // Pin 14: VDD ); logic r1, r2; always_ff @ (posedge clock1 or posedge reset1 or posedge set1) if (reset1) r1 <= 1'b0; else if (set1) r1 <= 1'b1; else r1 <= d1; always_ff @ (posedge clock2 or posedge reset2 or posedge set2) if (reset2) r2 <= 1'b0; else if (set2) r2 <= 1'b1; else r2 <= d2; assign q1 = r1 | (set1 & reset1); assign q1_n = ~ r1 | (set1 & reset1); assign q2 = r2 | (set2 & reset2); assign q2_n = ~ r2 | (set2 & reset2); endmodule
// CD4070B: quad 2-input exclusive-OR (XOR) gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. // // The 4070, 4071 and 4081 have the same pinout: only the function of the // gates differs. module cd4070 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a ^ b output logic k, // Pin 4: k = c ^ d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e ^ f output logic m, // Pin 11: m = g ^ h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a ^ b; assign k = c ^ d; assign l = e ^ f; assign m = g ^ h; endmodule
// CD4081B: quad 2-input AND gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. // // The 4081, 4071 and 4070 have the same pinout: only the function of the // gates differs. module cd4081 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a & b output logic k, // Pin 4: k = c & d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e & f output logic m, // Pin 11: m = g & h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a & b; assign k = c & d; assign l = e & f; assign m = g & h; endmodule
// CD4071B: quad 2-input OR gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. module cd4071 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a | b output logic k, // Pin 4: k = c | d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e | f output logic m, // Pin 11: m = g | h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a | b; assign k = c | d; assign l = e | f; assign m = g | h; 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_gates_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as serial_adder_gates.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 P2, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as serial_adder_gates.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_p2.py, not this file. Synthesizable. module serial_adder_gates_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 carry, output logic [3:0] sum, output logic [6:0] abcdefg_a, output logic [6:0] abcdefg_b, output logic [6:0] abcdefg_sum ); // Connections between the chips that are not ports wire co; wire t; wire g; wire p; // 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_uff_q1_n; wire unused_uff_q2_n; wire unused_uff_q2; wire unused_uo_j; wire unused_uo_k; wire unused_uo_m; wire unused_ua_j; wire unused_ua_l; wire unused_ux_l; wire unused_ux_m; 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 ); //------------------------------------------------------------------------ // UFF: CD4013 dual D flip-flop with set and reset, pin 1 in column 43 of board 2 cd4013 i_uff ( .q1 ( carry ), // Pin 1 -> C .q1_n ( unused_uff_q1_n ), // Pin 2, unused output .clock1 ( clk ), // Pin 3 <- 555 clock module .reset1 ( load ), // Pin 4 <- button LOAD .d1 ( co ), // Pin 5 <- UO pin 10 (L) .set1 ( gnd ), // Pin 6 <- GND .set2 ( vdd ), // Pin 8 <- +5V, unused input .d2 ( vdd ), // Pin 9 <- +5V, unused input .reset2 ( vdd ), // Pin 10 <- +5V, unused input .clock2 ( vdd ), // Pin 11 <- +5V, unused input .q2_n ( unused_uff_q2_n ), // Pin 12, unused output .q2 ( unused_uff_q2 ) // Pin 13, unused output ); //------------------------------------------------------------------------ // UO: CD4071 quad 2-input OR, pin 1 in column 4 of board 3 cd4071 i_uo ( .a ( gnd ), // Pin 1 <- GND, unused input .b ( gnd ), // Pin 2 <- GND, unused input .j ( unused_uo_j ), // Pin 3, unused output .k ( unused_uo_k ), // Pin 4, unused output .c ( gnd ), // Pin 5 <- GND, unused input .d ( gnd ), // Pin 6 <- GND, unused input .e ( t ), // Pin 8 <- UA pin 11 (M) .f ( g ), // Pin 9 <- UA pin 4 (K) .l ( co ), // Pin 10 -> C' .m ( unused_uo_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // UA: CD4081 quad 2-input AND, pin 1 in column 12 of board 3 cd4081 i_ua ( .a ( vdd ), // Pin 1 <- +5V, unused input .b ( vdd ), // Pin 2 <- +5V, unused input .j ( unused_ua_j ), // Pin 3, unused output .k ( g ), // Pin 4 -> G .c ( serial_b ), // Pin 5 <- USB pin 3 (Q8) .d ( serial_a ), // Pin 6 <- USA pin 3 (Q8) .e ( gnd ), // Pin 8 <- GND, unused input .f ( gnd ), // Pin 9 <- GND, unused input .l ( unused_ua_l ), // Pin 10, unused output .m ( t ), // Pin 11 -> T .g ( p ), // Pin 12 <- UX pin 3 (J) .h ( carry ) // Pin 13 <- UFF pin 1 (Q1) ); //------------------------------------------------------------------------ // UX: CD4070 quad 2-input XOR, pin 1 in column 21 of board 3 cd4070 i_ux ( .a ( serial_b ), // Pin 1 <- USB pin 3 (Q8) .b ( serial_a ), // Pin 2 <- USA pin 3 (Q8) .j ( p ), // Pin 3 -> P .k ( serial_sum ), // Pin 4 -> S→ .c ( carry ), // Pin 5 <- UFF pin 1 (Q1) .d ( p ), // Pin 6 <- UX pin 3 (J) .e ( gnd ), // Pin 8 <- GND, unused input .f ( gnd ), // Pin 9 <- GND, unused input .l ( unused_ux_l ), // Pin 10, unused output .m ( unused_ux_m ), // Pin 11, unused output .g ( gnd ), // Pin 12 <- GND, unused input .h ( gnd ) // Pin 13 <- GND, unused input ); //------------------------------------------------------------------------ // U15: CD4015 dual 4-stage shift register, serial in, parallel out, pin 1 in column 30 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 <- UX pin 4 (K) .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 40 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 is the testbench of P1 with one more output to check, the carry, which it computes from the lower
bits of A and B. Like the testbenches of Basics-Graphics-Music, it drives and checks signals only at rising clock
edges. The output below is shortened.
tb.sv270 lines · show the codefile
// Testbench for exercise P2. // // Runs the generic design (serial_adder_gates.sv) and the chip-level design // (serial_adder_gates_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 carry, 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 carry, carry_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_gates i_generic ( .clk ( clk ), .load ( load ), .a ( a ), .b ( b ), .serial_a ( serial_a ), .serial_b ( serial_b ), .serial_sum ( serial_sum ), .carry ( carry ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); serial_adder_gates_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 ), .carry ( carry_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 // The carry into bit k: the carry out of the lower k bits. LOAD at 1 // clears the carry flip-flop at once, as RESET of the 4013. function automatic logic carry_into (input logic [3:0] x, input logic [3:0] y, input int i); logic [4:0] low; low = i <= 0 ? 5'd0 : i >= 5 ? 5' (x) + 5' (y) : 5' (x & 4' ((1 << i) - 1)) + 5' (y & 4' ((1 << i) - 1)); return i >= 1 && i <= 4 ? low [i] : 1'b0; endfunction wire carry_expected = ~ load & carry_into (a_loaded, b_loaded, k); wire serial_a_expected = bit_of (5' (a_loaded), k); wire serial_b_expected = bit_of (5' (b_loaded), k); wire [28:0] expected = { serial_a_expected, serial_b_expected, serial_a_expected ^ serial_b_expected ^ carry_expected, carry_expected, sum_expected, digit (a), digit (b), digit (sum_expected) }; wire [28:0] out = { serial_a, serial_b, serial_sum, carry, sum, abcdefg_a, abcdefg_b, abcdefg_sum }; wire [28:0] out_chips = { serial_a_chips, serial_b_chips, serial_sum_chips, carry_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 %b %s %s", cycle + 1, load, a, b, serial_a, serial_b, carry, 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> C 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 P2: 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_gates.sv serial_adder_gates_chips.sv cd4511.sv cd4014.sv cd4013.sv cd4071.sv cd4081.sv cd4070.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 P2: 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_gates.sv serial_adder_gates_chips.sv cd4511.sv cd4014.sv cd4013.sv cd4071.sv cd4081.sv cd4070.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 P2: 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_gates.sv serial_adder_gates_chips.sv cd4511.sv cd4014.sv cd4013.sv cd4071.sv cd4081.sv cd4070.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> C S> 4015 generic chips
1 0 5 3 1 1 0 0 0000 ok ok
2 0 5 3 0 1 1 0 0000 ok ok
3 0 5 3 1 0 1 0 0000 ok ok
4 0 5 3 0 0 1 1 0000 ok ok
5 0 5 3 0 0 0 0 1000 ok ok
6 0 5 3 0 0 0 0 0100 ok ok
7 1 9 8 0 0 0 0 0000 ok ok
8 0 9 8 1 0 0 1 0000 ok ok
9 0 9 8 0 0 0 0 1000 ok ok
10 0 9 8 0 0 0 0 0100 ok ok
11 0 9 8 1 1 0 0 0010 ok ok
12 0 9 8 0 0 1 1 0001 ok ok
13 0 9 8 0 0 0 0 1000 ok ok
14 1 15 15 0 0 0 0 0000 ok ok
15 0 15 15 1 1 0 0 0000 ok ok
16 0 15 15 1 1 1 1 0000 ok ok
17 0 15 15 1 1 1 1 1000 ok ok
18 0 15 15 1 1 1 1 1100 ok ok
19 0 15 15 0 0 1 1 1110 ok ok
20 0 15 15 0 0 0 0 1111 ok ok
21 1 0 0 0 0 0 0 0000 ok ok
22 0 0 0 0 0 0 0 0000 ok ok
23 0 0 0 0 0 0 0 0000 ok ok
24 0 0 0 0 0 0 0 0000 ok ok
25 0 0 0 0 0 0 0 0000 ok ok
26 0 0 0 0 0 0 0 0000 ok ok
27 0 0 0 0 0 0 0 0000 ok ok
28 1 7 2 0 0 0 0 0000 ok ok
…
105 0 5 4 0 0 0 0 1001 ok ok
106 0 5 4 0 0 0 0 0100 ok ok
tb.sv:256: $finish called at 1095000 (1ps)
PASS: 212 checks, no errors
To see that the testbench catches mistakes, break something and run it again: replace g | t with g in serial_adder_gates.sv, or connect RESET1 of the
4013 to GND instead of LOAD in serial_adder_gates_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_gates.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.
As in exercise P1: switches 3–0 are A, switches 7–4 are B, key 0 is LOAD. LEDs 3–0 show the deserializer, LEDs 6–4
the streams and LED 7 the carry. To run the chip-level design instead, add serial_adder_gates_chips.sv and
the seven chip models and change the module name in the instance.
bgm_lab/lab_top.sv114 lines · show the codefile
// Exercise P2 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_gates.sv. For the // chip-level design add serial_adder_gates_chips.sv and cd4014.sv, // cd4070.sv, cd4081.sv, cd4071.sv, cd4013.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, LED 7 the carry. The rightmost digit shows the deserializer as the // 4511 would. // LOAD is a synchronous load for the serializers and an asynchronous clear // for the carry and 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, carry; logic [3:0] sum; logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder_gates 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 ), .carry ( carry ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); assign led = w_led' ({ carry, 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_gates.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
As in exercise P1, with 3-bit numbers: keys 2–0 are A, keys 5–3 are B and key 7 is LOAD. LED 7 shows the carry.
bgm_hackathon/hackathon_top.sv97 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise P2 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_gates.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 an asynchronous clear // for the carry and 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, carry; logic [3:0] sum; logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder_gates 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 ), .carry ( carry ), .sum ( sum ), .abcdefg_a ( abcdefg_a ), .abcdefg_b ( abcdefg_b ), .abcdefg_sum ( abcdefg_sum ) ); assign led = { carry, 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.
As in exercise P1: DIP switches 0–3 are A, switches 4–7 are B, and LOAD comes from BIDIR pin 0. The display shows the deserializer; the carry goes to BIDIR pin 1, the streams and the deserializer to pins 2–7.
tiny_tapeout/project.v67 lines · show the codefile
/* * Exercise P2 on Tiny Tapeout: a bit-serial adder from gates and a * flip-flop for the carry. The 7-segment display of the demo board shows * the deserializer as a 4511 would. * * Add serial_adder_gates.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 an asynchronous clear // for the carry and 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, carry; wire [3:0] sum; wire [6:0] abcdefg_a, abcdefg_b, abcdefg_sum; serial_adder_gates 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 ), .carry ( carry ), .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, the streams of A and B and the carry on BIDIR pins // 1-7 assign uio_out = { sum, serial_b, serial_a, carry, 1'b0 }; assign uio_oe = 8'b1111_1110; // 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_gates
source_files:
- "project.v"
- "serial_adder_gates.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
- Compare. Count the wires of P1 and P2. What does one 4032 save? What does it cost when it is hard to find?
- Three adders. The 4032 has three adders and one clock. What could three serial adders do together?
- Subtraction. Put an XOR gate on the B stream, with a SUB button on its other input, and set the carry flip-flop to 1 with LOAD instead of resetting it. Why does this subtract?
- In SystemVerilog: write the adder for N-bit numbers with a counter that stops the deserializer after N clocks, so that the sum stays.
Files of this exercise
- serial_adder_gates.sv: the generic design (section 8)
- cd4511.sv, cd4014.sv, cd4013.sv, cd4071.sv, cd4081.sv, cd4070.sv and cd4015.sv: chip models (section 9)
- serial_adder_gates_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: “in the end the 4032 can be replaced by the full adder of the first group and a D flip-flop for the carry, a 4013; this shows what happens inside the 4032”. The chips are described in the Texas Instruments datasheets for the CD4014B, CD4070B, CD4081B, CD4071B, CD4013B, CD4015B and CD4511B.