Exercise M3 · mini-project · adder
2-bit adder from a half adder and a full adder
Two 2-bit numbers from four buttons, a half adder for bit 0, a full adder for bit 1 and the carry between them: the sum appears on three LEDs.
- M1: Half adder from XOR and AND
- M2: Full adder from basic gates
- M3: 2-bit adder from a half adder and a full adder
1What it is and why it matters
What it does. Buttons A1 A0 give the number A and B1 B0 the number B, each from 0 to 3. Bit 0 is added by a half adder (an XOR and an AND), whose carry C0 goes into a full adder for bit 1. LEDs 1–4 show A1 A0 B1 B0, LEDs 5–7 show S2 S1 S0, the sum from 0 to 6.
How it works. This is how a pupil adds on paper, column by column from the right, carrying into the next column. Each column after the first needs a full adder; the first needs only a half adder because nothing comes in. The carry ripples from bit 0 to bit 1: a ripple-carry adder, like the generic design of B8.
Where it is used. In every processor: the adder of the ALU, the incrementer of the program counter, the address calculation of loads and stores. They are all made of these cells, with faster ways to compute the carry when the numbers are wide.
Why learn it.
- This is where logic gates become arithmetic: the same XOR and AND of exercise B1, wired the right way, add numbers.
- The 4008 of B8 does the same inside; here you see what is in the black box.
- In SystemVerilog the whole circuit is
+; knowing what+costs in gates is part of designing hardware.
2The key elements: adding bits
The truth table
| A1 | A0 | B1 | B0 | S2 | S1 | S0 |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 1 | 0 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 1 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 0 |
Read the outputs as a binary number: it is always the sum of the inputs.
Waveforms
tb.sv in Icarus Verilog. The testbench sets the buttons right after a
rising edge of its clock (dotted lines) and checks the outputs at the next edge. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4071 | quad 2-input OR: the carry into bit 2 (one gate used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4081 | quad 2-input AND: three of four used | 1 | $0.55 | Elexp (elexp.com) |
| CD4070 | quad 2-input XOR: three of four used | 1 | $0.45 | Elexp (elexp.com) |
| Button modules | A1, A0, B1, B0; 4 from the pack of five | 4 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows the inputs and the result | 1 | $0.38 | aliexpress.us |
| Breadboard, 830 points | 1 | $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 | 30 wires on the board (or male-male Dupont jumpers) | 30 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 20 leads to the buttons and the LED module | 20 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; these chips are in the Comprehensive kit, and the 4070, 4081 and 4071 also in the Basics 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.
All three chips have the pinout of B1. Three gates of the 4070 and the 4081 and one of the 4071 are used. On the breadboard the 4071 sits turned around, notch on the right, so that its output faces the LED module.
4070: quad 2-input XOR
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | A0 |
| 2 | B | input | input of the gate with its output on pin 3 | B0 |
| 3 | J | output | J = A XOR B | S0 |
| 4 | K | output | K = C XOR 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 | A1 |
| 9 | F | input | input of the gate with its output on pin 10 | B1 |
| 10 | L | output | L = E XOR F | A1^B1 |
| 11 | M | output | M = G XOR H | S1 |
| 12 | G | input | input of the gate with its output on pin 11 | A1^B1 |
| 13 | H | input | input of the gate with its output on pin 11 | C0 |
| 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 | A0 |
| 2 | B | input | input of the gate with its output on pin 3 | B0 |
| 3 | J | output | J = A AND B | C0 |
| 4 | K | output | K = C AND D | A1&B1 |
| 5 | C | input | input of the gate with its output on pin 4 | A1 |
| 6 | D | input | input of the gate with its output on pin 4 | B1 |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | C0 |
| 9 | F | input | input of the gate with its output on pin 10 | A1^B1 |
| 10 | L | output | L = E AND F | C0&(A1^B1) |
| 11 | M | output | M = G AND 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 |
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 | A1&B1 |
| 2 | B | input | input of the gate with its output on pin 3 | C0&(A1^B1) |
| 3 | J | output | J = A OR B | S2 |
| 4 | K | output | K = C OR D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to +5V |
| 6 | D | input | input of the gate with its output on pin 4 | tied to +5V |
| 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 OR F | not connected |
| 11 | M | output | M = G OR 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 |
5Schematic 1: the gates
two_bit_adder.sv describes. A dot
means a connection; crossing lines without a dot are not connected. The chip under each gate is the one that holds it
on the breadboard. · SVG · PNG6Schematic 2: connections between the chips
Every connection, net by net. two_bit_adder_chips.sv in section 10 is written from the same list.
| Net | From | To | Wire |
|---|---|---|---|
| A1 | button A1, OUT | 4070 8 (E), 4081 5 (C), LED module, LED 1 | M-F, M-M |
| A0 | button A0, OUT | 4070 1 (A), 4081 1 (A), LED module, LED 2 | M-F, M-M |
| B1 | button B1, OUT | 4070 9 (F), 4081 6 (D), LED module, LED 3 | M-F, M-M |
| B0 | button B0, OUT | 4070 2 (B), 4081 2 (B), LED module, LED 4 | M-F, M-M |
| C0 | 4081 3 (J) | 4070 13 (H), 4081 8 (E) | M-M |
| A1^B1 | 4070 10 (L) | 4070 12 (G), 4081 9 (F) | M-M |
| A1&B1 | 4081 4 (K) | 4071 1 (A) | M-M |
| C0&(A1^B1) | 4081 10 (L) | 4071 2 (B) | M-M |
| S2 | 4071 3 (J) | LED module, LED 5 | M-F, M-M |
| S1 | 4070 11 (M) | LED module, LED 6 | M-F, M-M |
| S0 | 4070 3 (J) | LED module, LED 7 | M-F |
| +5V | +5V | 4071 14 (VDD), 4081 14 (VDD), 4070 14 (VDD), 4071 6 (D), 4071 5 (C), 4081 13 (H), 4081 12 (G) | |
| GND | GND | 4071 7 (VSS), 4081 7 (VSS), 4070 7 (VSS), 4071 8 (E), 4071 9 (F), 4071 12 (G), 4071 13 (H), 4070 5 (C), 4070 6 (D) | |
| not connected | 4071 4 (K), 4071 10 (L), 4071 11 (M), 4081 11 (M), 4070 4 (K) |
7Schematic 3: the breadboard
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.
- Insert the chips across the gap: the 4071 turned around, notch to the right, pin 1 in hole 15e; the 4081, notch to the left, pin 1 in hole 19f; the 4070, notch to the left, pin 1 in hole 28f. 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 tie-offs: the inputs of the unused gates to the nearest rail, top pins to +, bottom pins to −.
- The signal wires, in the order of the checklist. Tick each wire as you insert it.
- With the power off, the buttons above the board and the LED module below it.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 6 wires | ||||
| 1 | +5V | 15j (4071 pin 14, VDD) | bottom + rail, column 15 | |
| 2 | GND | 9a (4071 pin 7, VSS) | top − rail, column 9 | |
| 3 | +5V | 19a (4081 pin 14, VDD) | top + rail, column 19 | |
| 4 | GND | 25j (4081 pin 7, VSS) | bottom − rail, column 25 | |
| 5 | +5V | 28a (4070 pin 14, VDD) | top + rail, column 28 | |
| 6 | GND | 34j (4070 pin 7, VSS) | bottom − rail, column 34 | |
| Fixed inputs: CMOS inputs must not float · 10 wires | ||||
| 7 | GND | 9j (4071 pin 8, E) | bottom − rail, column 9 | |
| 8 | +5V | 10a (4071 pin 6, D) | top + rail, column 10 | |
| 9 | GND | 10j (4071 pin 9, F) | bottom − rail, column 10 | |
| 10 | +5V | 11a (4071 pin 5, C) | top + rail, column 11 | |
| 11 | GND | 13j (4071 pin 12, G) | bottom − rail, column 13 | |
| 12 | GND | 14j (4071 pin 13, H) | bottom − rail, column 14 | |
| 13 | +5V | 20a (4081 pin 13, H) | top + rail, column 20 | |
| 14 | +5V | 21a (4081 pin 12, G) | top + rail, column 21 | |
| 15 | GND | 32j (4070 pin 5, C) | bottom − rail, column 32 | |
| 16 | GND | 33j (4070 pin 6, D) | bottom − rail, column 33 | |
| Signal wires · 14 wires | ||||
| 17 | A1 | 34d (4070 pin 8, E) | 23g (4081 pin 5, C) | |
| 18 | A0 | 28g (4070 pin 1, A) | 19g (4081 pin 1, A) | |
| 19 | A0 | 28h (4070 pin 1, A) | 27b | |
| 20 | B1 | 33c (4070 pin 9, F) | 24h (4081 pin 6, D) | |
| 21 | B0 | 29i (4070 pin 2, B) | 20i (4081 pin 2, B) | |
| 22 | B0 | 29h (4070 pin 2, B) | 26e | |
| 23 | C0 | 21h (4081 pin 3, J) | 25c (4081 pin 8, E) | |
| 24 | C0 | 25d (4081 pin 8, E) | 29c (4070 pin 13, H) | |
| 25 | A1^B1 | 32d (4070 pin 10, L) | 30d (4070 pin 12, G) | |
| 26 | A1^B1 | 30c (4070 pin 12, G) | 24d (4081 pin 9, F) | |
| 27 | S1 | 31b (4070 pin 11, M) | 35f | |
| 28 | A1&B1 | 22g (4081 pin 4, K) | 15c (4071 pin 1, A) | |
| 29 | C0&(A1^B1) | 23d (4081 pin 10, L) | 14d (4071 pin 2, B) | |
| 30 | S2 | 13b (4071 pin 3, J) | 16f | |
| Modules on male-female jumpers · 20 wires | ||||
| 31 | +5V | button A1, + | top + rail, column 17 | |
| 32 | A1 | button A1, OUT | 34a (4070 pin 8, E) | |
| 33 | GND | button A1, − | top − rail, column 19 | |
| 34 | +5V | button A0, + | top + rail, column 22 | |
| 35 | A0 | button A0, OUT | 27a | |
| 36 | GND | button A0, − | top − rail, column 24 | |
| 37 | +5V | button B1, + | top + rail, column 27 | |
| 38 | B1 | button B1, OUT | 33a (4070 pin 9, F) | |
| 39 | GND | button B1, − | top − rail, column 29 | |
| 40 | +5V | button B0, + | top + rail, column 32 | |
| 41 | B0 | button B0, OUT | 26a | |
| 42 | GND | button B0, − | top − rail, column 34 | |
| 43 | GND | LED module, G | bottom − rail, column 24 | |
| 44 | A1 | LED module, LED 1 | 23j (4081 pin 5, C) | |
| 45 | A0 | LED module, LED 2 | 28j (4070 pin 1, A) | |
| 46 | B1 | LED module, LED 3 | 24j (4081 pin 6, D) | |
| 47 | B0 | LED module, LED 4 | 29j (4070 pin 2, B) | |
| 48 | S2 | LED module, LED 5 | 16j | |
| 49 | S1 | LED module, LED 6 | 35j | |
| 50 | S0 | LED module, LED 7 | 30j (4070 pin 3, J) | |
Test it
- No button: 000. A0 alone: 001. A0 and B0: 010, the carry went into bit 1.
- A1 A0 and B1 B0 all pressed: 3 + 3 = 6 = 110.
- A1 and B0: 2 + 1 = 3 = 011; there is no carry anywhere.
If it does not work: check each gate on its own: put a wire from its output to a free LED of the module and compare with the truth table of B1. A wrong sum with a right carry points to the XOR gates, and the other way round. If an LED flickers when you touch the wires, an input is floating.
8SystemVerilog: the generic design
two_bit_adder.sv describes schematic 1, one line per gate or pair of gates. It is synthesizable.
// Exercise M3: a 2-bit adder from a half adder and a full adder. // // Generic version that matches schematic 1, not tied to any chip. Bit 0 // is a half adder; its carry goes into a full adder for bit 1, whose carry // is bit 2 of the sum. Synthesizable. module two_bit_adder ( input logic a1, input logic a0, input logic b1, input logic b0, output logic s2, // Weight 4: the carry out of bit 1 output logic s1, // Weight 2 output logic s0 // Weight 1 ); // Bit 0: a half adder wire c0 = a0 & b0; assign s0 = a0 ^ b0; // Bit 1: a full adder with the carry from bit 0 wire p1 = a1 ^ b1; assign s1 = p1 ^ c0; assign s2 = a1 & b1 | p1 & c0; 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.
// 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
10SystemVerilog: the chip-level design
two_bit_adder_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as two_bit_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 M3, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as two_bit_adder.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_m123.py, not this file. Synthesizable. module two_bit_adder_chips ( input logic a1, input logic a0, input logic b1, input logic b0, output logic s2, output logic s1, output logic s0 ); // Connections between the chips that are not ports wire g1; wire t1; wire c0; wire p1; // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; wire vdd = 1'b1; // Outputs that go nowhere wire unused_u3_k; wire unused_u3_l; wire unused_u3_m; wire unused_u2_m; wire unused_u1_k; //------------------------------------------------------------------------ // U3: CD4071 quad 2-input OR, pin 1 in column 9 cd4071 i_u3 ( .a ( g1 ), // Pin 1 <- U2 pin 4 (K) .b ( t1 ), // Pin 2 <- U2 pin 10 (L) .j ( s2 ), // Pin 3 -> S2 .k ( unused_u3_k ), // Pin 4, unused output .c ( vdd ), // Pin 5 <- +5V, unused input .d ( vdd ), // Pin 6 <- +5V, unused input .e ( gnd ), // Pin 8 <- GND, unused input .f ( gnd ), // Pin 9 <- GND, unused input .l ( unused_u3_l ), // Pin 10, unused output .m ( unused_u3_m ), // Pin 11, unused output .g ( gnd ), // Pin 12 <- GND, unused input .h ( gnd ) // Pin 13 <- GND, unused input ); //------------------------------------------------------------------------ // U2: CD4081 quad 2-input AND, pin 1 in column 19 cd4081 i_u2 ( .a ( a0 ), // Pin 1 <- button A0 .b ( b0 ), // Pin 2 <- button B0 .j ( c0 ), // Pin 3 -> C0 .k ( g1 ), // Pin 4 -> A1&B1 .c ( a1 ), // Pin 5 <- button A1 .d ( b1 ), // Pin 6 <- button B1 .e ( c0 ), // Pin 8 <- U2 pin 3 (J) .f ( p1 ), // Pin 9 <- U1 pin 10 (L) .l ( t1 ), // Pin 10 -> C0&(A1^B1) .m ( unused_u2_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U1: CD4070 quad 2-input XOR, pin 1 in column 28 cd4070 i_u1 ( .a ( a0 ), // Pin 1 <- button A0 .b ( b0 ), // Pin 2 <- button B0 .j ( s0 ), // Pin 3 -> S0 .k ( unused_u1_k ), // Pin 4, unused output .c ( gnd ), // Pin 5 <- GND, unused input .d ( gnd ), // Pin 6 <- GND, unused input .e ( a1 ), // Pin 8 <- button A1 .f ( b1 ), // Pin 9 <- button B1 .l ( p1 ), // Pin 10 -> A1^B1 .m ( s1 ), // Pin 11 -> S1 .g ( p1 ), // Pin 12 <- U1 pin 10 (L) .h ( c0 ) // Pin 13 <- U2 pin 3 (J) ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with a reference model: the sum of the
inputs, computed with +. The circuit has no clock, but the testbench has one, like the testbenches of
Basics-Graphics-Music: it sets the inputs right after a rising edge and checks the outputs at the next one. It tries
all 16 combinations twice, counting up and in Gray code order, and prints PASS only if both designs match
every time and every combination was seen.
tb.sv169 lines · show the codefile
// Testbench for exercise M3. // // Runs the generic design (two_bit_adder.sv) and the chip-level design // (two_bit_adder_chips.sv) side by side and compares both with // the sum of A1 A0 and B1 B0 as a 3-bit number. Works with Icarus Verilog, Verilator (--binary) // and Questa. // // The circuit has no clock, but like the testbenches of Basics-Graphics-Music // this one drives and checks signals only at rising edges of its own clock: // it changes the inputs after one edge and checks the outputs at the next. // It tries all 16 input combinations twice: counting up, and then in // Gray code order, where one input changes at a time, as when a student // presses or releases one button. // Time units. The design files have none: Yosys does not accept timeunit // and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps // in its own testbench. This line sets the same units here and for the // design files compiled after it, so tb.sv comes first in the file list. `timescale 1ns / 1ps module tb; logic clk; logic a1; logic a0; logic b1; logic b0; wire s2, s2_chips; wire s1, s1_chips; wire s0, s0_chips; two_bit_adder i_generic ( .a1 ( a1 ), .a0 ( a0 ), .b1 ( b1 ), .b0 ( b0 ), .s2 ( s2 ), .s1 ( s1 ), .s0 ( s0 ) ); two_bit_adder_chips i_chips ( .a1 ( a1 ), .a0 ( a0 ), .b1 ( b1 ), .b0 ( b0 ), .s2 ( s2_chips ), .s1 ( s1_chips ), .s0 ( s0_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the sum of A1 A0 and B1 B0 as a 3-bit number. // Bits, from the left: s2, s1, s0. function automatic logic [2:0] expected ( input logic in_a1, input logic in_a0, input logic in_b1, input logic in_b0 ); logic [2:0] sum; sum = 3' ({ in_a1, in_a0 }) + 3' ({ in_b1, in_b0 }); return sum; // { s2, s1, s0 } endfunction wire [2:0] out = { s2, s1, s0 }; wire [2:0] out_chips = { s2_chips, s1_chips, s0_chips }; wire [2:0] out_model = expected (a1, a0, b1, b0); //------------------------------------------------------------------------ // Checking at every rising clock edge: the inputs still hold the values the // stimulus set at the previous edge, and the outputs have settled. // Initial values are fine in a testbench // verilator lint_off PROCASSINIT logic checking = 1'b0; // Set by the stimulus after the first inputs logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; logic [15:0] rows_seen = '0; // Which input combinations were checked always @ (posedge clk) if (checking & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (out === out_model ? 0 : 1) + (out_chips === out_model ? 0 : 1); rows_seen <= rows_seen | 16'b1 << { a1, a0, b1, b0 }; $display ("%5d %b %b %b %b %b %b %b %s %s", cycle + 1, a1, a0, b1, b0, s2, s1, s0, out === out_model ? "ok " : "WRONG ", out_chips === out_model ? "ok " : "WRONG"); end //------------------------------------------------------------------------ // Driving the inputs: all combinations, counting up, then in Gray code order initial begin $dumpfile ("tb.vcd"); `ifdef __ICARUS__ $dumpvars (0, tb); // Without a scope Icarus also tries to dump $unit `else $dumpvars; `endif $display ("cycle A1 A0 B1 B0 S2 S1 S0 generic chips"); checking <= '1; for (int i = 0; i < 16; i ++) begin { a1, a0, b1, b0 } <= 4' (i); @ (posedge clk); end for (int i = 0; i < 16; i ++) begin { a1, a0, b1, b0 } <= 4' (i ^ (i >> 1)); @ (posedge clk); end // Stop checking one clock edge before $finish, so that every // simulator reports the same number of checks done <= '1; @ (posedge clk); $finish; end //------------------------------------------------------------------------ final begin if (n_errors == 0 && & rows_seen) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, %0d of %0d input combinations seen", n_errors, n_checks, $countones (rows_seen), 16); 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 M3: 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 two_bit_adder.sv two_bit_adder_chips.sv cd4071.sv cd4081.sv cd4070.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 M3: 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 two_bit_adder.sv two_bit_adder_chips.sv cd4071.sv cd4081.sv cd4070.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 M3: 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 two_bit_adder.sv two_bit_adder_chips.sv cd4071.sv cd4081.sv cd4070.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 A1 A0 B1 B0 S2 S1 S0 generic chips
1 0 0 0 0 0 0 0 ok ok
2 0 0 0 1 0 0 1 ok ok
3 0 0 1 0 0 1 0 ok ok
4 0 0 1 1 0 1 1 ok ok
5 0 1 0 0 0 0 1 ok ok
6 0 1 0 1 0 1 0 ok ok
7 0 1 1 0 0 1 1 ok ok
8 0 1 1 1 1 0 0 ok ok
9 1 0 0 0 0 1 0 ok ok
10 1 0 0 1 0 1 1 ok ok
11 1 0 1 0 1 0 0 ok ok
12 1 0 1 1 1 0 1 ok ok
13 1 1 0 0 0 1 1 ok ok
14 1 1 0 1 1 0 0 ok ok
15 1 1 1 0 1 0 1 ok ok
16 1 1 1 1 1 1 0 ok ok
17 0 0 0 0 0 0 0 ok ok
18 0 0 0 1 0 0 1 ok ok
19 0 0 1 1 0 1 1 ok ok
20 0 0 1 0 0 1 0 ok ok
21 0 1 1 0 0 1 1 ok ok
22 0 1 1 1 1 0 0 ok ok
23 0 1 0 1 0 1 0 ok ok
24 0 1 0 0 0 0 1 ok ok
25 1 1 0 0 0 1 1 ok ok
26 1 1 0 1 1 0 0 ok ok
27 1 1 1 1 1 1 0 ok ok
28 1 1 1 0 1 0 1 ok ok
29 1 0 1 0 1 0 0 ok ok
30 1 0 1 1 1 0 1 ok ok
31 1 0 0 1 0 1 1 ok ok
32 1 0 0 0 0 1 0 ok ok
tb.sv:155: $finish called at 325000 (1ps)
PASS: 64 checks, no errors
To see that the testbench catches mistakes, break something and run it again: change wire c0 = a0 & b0; to wire c0 = 1'b0; in two_bit_adder.sv, or connect pin 13 of the 4070 to gnd instead of c0 in two_bit_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_01_and_or_not_xor_de_morganto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addtwo_bit_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.
Keys 0–3 are A1, A0, B1 and B0; LEDs 0–2 show S2, S1 and S0. To run the chip-level
design instead, add two_bit_adder_chips.sv and the chip models and change the module name in the instance.
bgm_lab/lab_top.sv100 lines · show the codefile
// Exercise M3 on an FPGA board with the Basics-Graphics-Music (BGM) labs: // https://github.com/yuri-panchul/basics-graphics-music // // Copy labs/1_basics/1_01_and_or_not_xor_de_morgan to a new directory, // replace its lab_top.sv with this file and add two_bit_adder.sv. For the // chip-level design add two_bit_adder_chips.sv and cd4070.sv, cd4081.sv, // cd4071.sv instead, and change the module name below. // // Keys: key [0] a1, key [1] a0, key [2] b1, key [3] b0. The LEDs: led [0] s2, // led [1] s1, led [2] s0. A board with fewer LEDs shows the first ones. `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 abcdefgh = '0; assign digit = '0; assign red = '0; assign green = '0; assign blue = '0; assign sound = '0; assign uart_tx = '1; //------------------------------------------------------------------------ logic s2; logic s1; logic s0; two_bit_adder i_two_bit_adder ( .a1 ( key [0] ), .a0 ( key [1] ), .b1 ( key [2] ), .b0 ( key [3] ), .s2 ( s2 ), .s1 ( s1 ), .s0 ( s0 ) ); assign led = w_led' ({ s0, s1, s2 }); 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/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addtwo_bit_adder.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 0–3 are the inputs. The screen shows the inputs in blue and the outputs in green.
bgm_hackathon/hackathon_top.sv88 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise M3 in the hackathon setup of Basics-Graphics-Music (BGM): // https://github.com/yuri-panchul/basics-graphics-music // // Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its // hackathon_top.sv with this file and add two_bit_adder.sv. // // Keys: key [0] a1, key [1] a0, key [2] b1, key [3] b0. The LEDs: led [0] s2, // led [1] s1, led [2] s0. The screen shows 7 lamps in a row: A1, A0, B1, B0, // S2, S1, S0; inputs in blue, outputs in green. 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 ); //------------------------------------------------------------------------ wire a1 = key [0]; wire a0 = key [1]; wire b1 = key [2]; wire b0 = key [3]; logic s2; logic s1; logic s0; two_bit_adder i_two_bit_adder ( .a1 ( a1 ), .a0 ( a0 ), .b1 ( b1 ), .b0 ( b0 ), .s2 ( s2 ), .s1 ( s1 ), .s0 ( s0 ) ); assign led = { 5'b0, s0, s1, s2 }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 7 square lamps on the 480 x 272 screen, 40 pixels wide, 64 apart: // the inputs in blue, the outputs in green. Bright when 1, dim when 0. wire [6:0] lamp = { s0, s1, s2, b0, b1, a0, a1 }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 7; i ++) if (x >= 9' (28 + i * 64) & x < 9' (68 + i * 64) & y >= 116 & y < 156) begin if (i < 4) blue = lamp [i] ? 31 : 6; else green = lamp [i] ? 63 : 12; end end endmodule
14On a chip: Tiny Tapeout
Tiny Tapeout makes real chips from student designs. Each design
is a module with the ports below; on the demo board ui_in comes from DIP switches and
uo_out drives a 7-segment display.
This design has no clock and no state: project.v connects the DIP switches to its inputs
and its outputs to the display. The project still gets a clock and a reset from the board; they stay unused.
Switches 0–3 are A1, A0, B1, B0; the segments a, b, … of the display show S2, S1, S0.
tiny_tapeout/project.v53 lines · show the codefile
/* * Exercise M3 on Tiny Tapeout: 2-bit adder from a half adder and a full adder, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add two_bit_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 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 ); // Switch 0 is a1, switch 1 is a0, switch 2 is b1, switch 3 is b0 wire s2; wire s1; wire s0; two_bit_adder i_two_bit_adder ( .a1 ( ui_in [0] ), .a0 ( ui_in [1] ), .b1 ( ui_in [2] ), .b0 ( ui_in [3] ), .s2 ( s2 ), .s1 ( s1 ), .s0 ( s0 ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. Each output lights one segment: // a (top) s2, b (top right) s1, c (bottom right) s0. assign uo_out = { 5'b0, s0, s1, s2 }; assign uio_out = 8'b0; assign uio_oe = 8'b0; // The circuit needs no clock or reset. // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, ui_in [7:4], uio_in, 1'b0 }; endmodule
In info.yaml list the files and use the module name as the top module:
project:
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_two_bit_adder
source_files:
- "project.v"
- "two_bit_adder.sv"
15Going further
Tasks for students
- Three bits. Add one more full adder for bit 2. How many chips does it take?
- The carry chain. Which input combination makes the carry travel the longest way? Why does that matter for a 32-bit adder?
- 8 bits. Exercise M4 cascades two 4008s, each a 4-bit adder like this one, extended.
- In SystemVerilog: replace the gates of
two_bit_adder.svwith one line that uses+. The testbench still passes.
Files of this exercise
- two_bit_adder.sv: the generic design (section 8)
- cd4071.sv, cd4081.sv and cd4070.sv: chip models (section 9)
- two_bit_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 comes from the Mini-projects list of “An improved way of teaching school kids digital electronics”. The chips are described in the Texas Instruments datasheets for the CD4070B, CD4081B, CD4071B.