Exercise M4 · mini-project · adder
8-bit adder from two cascaded 4008s
One 4008 adds two 4-bit numbers. Connect its carry out to the carry in of a second 4008, and the two add 8-bit numbers: the low chip adds bits 3–0, the high chip bits 7–4 and the carry that comes from below. This is how wide adders are built from narrow ones.
1What it is and why it matters
What it does. Eight buttons A7–A0 give the number A, eight wires B7–B0 to the rails give the number B; they start at 00101101, the number 45. The LED module shows the 8-bit sum, S7 on LED 1 and S0 on LED 8, and a red LED shows the ninth bit, the carry out C8.
How it works. The low 4008 adds A3–A0 and B3–B0 with its carry in at GND. Its carry out C4 says that the low half overflowed, and it goes into the carry in of the high 4008, which adds A7–A4, B7–B4 and C4. The high carry out is the ninth bit of the sum.
Where it is used. Every wide adder is built from narrow pieces. Inside the 4008 the carry goes across the four bits at once (a parallel carry), but between the two chips it ripples: the high chip waits for the low one. In a processor, look-ahead carry circuits, like the 40182 for the 40181 of exercise BB10, cut this wait.
Why learn it.
- It shows what the carry in and carry out of a chip are for.
- It practises binary numbers up to 255 and the ninth bit.
- It prepares the bit-serial adder of P1, where the carry goes from one clock to the next instead of from one chip to the next.
2The key elements: the carry between the chips
| Example | A | B | C4 | S7 … S0 (LEDs 1-8) | C8 (red LED) | A + B |
|---|---|---|---|---|---|---|
| 100 + 45 | 01100100 | 00101101 | 1 | 10010001 | 0 | 145 |
| 200 + 45 | 11001000 | 00101101 | 1 | 11110101 | 0 | 245 |
| 15 + 1 | 00001111 | 00000001 | 1 | 00010000 | 0 | 16 |
| 255 + 1 | 11111111 | 00000001 | 1 | 00000000 | 1 | 256 |
| 128 + 128 | 10000000 | 10000000 | 0 | 00000000 | 1 | 256 |
| 85 + 170 | 01010101 | 10101010 | 0 | 11111111 | 0 | 255 |
C4 is 1 when the low halves add up to 16 or more: 15 + 1 makes 0000 in the low half and passes 1 to the high half. 255 + 1 overflows both: the sum is 0 and the red LED lights.
Waveforms
tb.sv in Icarus Verilog: the six examples of the table, then the
start of the run through all 65536 pairs. C4 is the wire between the two chips. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4008 | 4-bit full adders: bits 3-0 and bits 7-4 | 2 | $1.00 | leeselectronic.com |
| Button modules | A7-A0; the first pack of five | 5 | $0.66 / pack | aliexpress.us |
| Button modules | 3 more from the second pack | 3 | $2.03 / pack | aliexpress.us |
| 8-LED module | the sum S7-S0 | 1 | $0.38 | aliexpress.us |
| 5 V LED, red | the carry out C8, plugged into the board | 1 | $0.45 | elexp.com |
| 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 | 16 wires on the board (or male-male Dupont jumpers) | 16 | $11.95 / pack | jameco.com |
| Male-female Dupont jumpers | 33 leads to the buttons and the LED module | 33 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; the 4008 is in the Basics kit, and this exercise needs two of them.
4The chip, 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.
4008: 4-bit full adder
As in exercise B8. A1–A4 are pins 7, 5, 3, 1; B1–B4 are pins 6, 4, 2, 15; CARRY IN is pin 9; S1–S4 are pins 10–13 and CARRY OUT pin 14. In the low chip CARRY IN is at GND and CARRY OUT goes to CARRY IN of the high chip.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A4 | input | number A, bit 3 (weight 8) | A3 |
| 2 | B3 | input | number B, bit 2 (weight 4) | B2 (+5V) |
| 3 | A3 | input | number A, bit 2 (weight 4) | A2 |
| 4 | B2 | input | number B, bit 1 (weight 2) | B1 (GND) |
| 5 | A2 | input | number A, bit 1 (weight 2) | A1 |
| 6 | B1 | input | number B, bit 0 (weight 1) | B0 (+5V) |
| 7 | A1 | input | number A, bit 0 (weight 1) | A0 |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | CIN | input | carry in, added to A + B | GND |
| 10 | S1 | output | sum, bit 0 (weight 1) | S0 |
| 11 | S2 | output | sum, bit 1 (weight 2) | S1 |
| 12 | S3 | output | sum, bit 2 (weight 4) | S2 |
| 13 | S4 | output | sum, bit 3 (weight 8) | S3 |
| 14 | COUT | output | carry out, the fifth bit of the sum (weight 16) | C4 |
| 15 | B4 | input | number B, bit 3 (weight 8) | B3 (+5V) |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: two adders in a cascade
adder8.sv describes. · SVG · PNG6Schematic 2: connections between the chips
| Net | From | To | Wire |
|---|---|---|---|
| A7 | button A7, OUT | 4008 1 (A4) | M-F |
| A6 | button A6, OUT | 4008 3 (A3) | M-F |
| A5 | button A5, OUT | 4008 5 (A2) | M-F |
| A4 | button A4, OUT | 4008 7 (A1) | M-F |
| A3 | button A3, OUT | 4008 1 (A4) | M-F |
| A2 | button A2, OUT | 4008 3 (A3) | M-F |
| A1 | button A1, OUT | 4008 5 (A2) | M-F |
| A0 | button A0, OUT | 4008 7 (A1) | M-F |
| B7 | B7 wire: GND | 4008 15 (B4) | M-M |
| B6 | B6 wire: GND | 4008 2 (B3) | M-M |
| B5 | B5 wire: +5V | 4008 4 (B2) | M-M |
| B4 | B4 wire: GND | 4008 6 (B1) | M-M |
| B3 | B3 wire: +5V | 4008 15 (B4) | M-M |
| B2 | B2 wire: +5V | 4008 2 (B3) | M-M |
| B1 | B1 wire: GND | 4008 4 (B2) | M-M |
| B0 | B0 wire: +5V | 4008 6 (B1) | M-M |
| C4 | 4008 14 (COUT) | 4008 9 (CIN) | M-M |
| S7 | 4008 13 (S4) | LED module, LED 1 | M-F |
| S6 | 4008 12 (S3) | LED module, LED 2 | M-F |
| S5 | 4008 11 (S2) | LED module, LED 3 | M-F |
| S4 | 4008 10 (S1) | LED module, LED 4 | M-F |
| S3 | 4008 13 (S4) | LED module, LED 5 | M-F |
| S2 | 4008 12 (S3) | LED module, LED 6 | M-F |
| S1 | 4008 11 (S2) | LED module, LED 7 | M-F |
| S0 | 4008 10 (S1) | LED module, LED 8 | M-F |
| C8 | 4008 14 (COUT) | 5 V LED, + leg | M-M |
| +5V | +5V | 4008 16 (VDD), 4008 16 (VDD) | |
| GND | GND | 4008 8 (VSS), 4008 8 (VSS), 5 V LED, − leg, 4008 9 (CIN) |
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 4008 turned around, notch to the right, pin 1 in hole 15e; the 4008 turned around, notch to the right, pin 1 in hole 25e. 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 red 5 V LED: long leg into the hole shown, short leg to GND as in the checklist. It has its resistor inside.
- CARRY IN of the low 4008 to GND, and the cascade wire from CARRY OUT of the low 4008 to CARRY IN of the high one.
- The eight B wires to the rails, as in the checklist.
- With the power off, the modules: A7–A0 above the board, the LED module below it. Tick each wire as you insert it.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 5 wires | ||||
| 1 | +5V | 15j (4008 pin 16, VDD) | bottom + rail, column 15 | |
| 2 | GND | 8a (4008 pin 8, VSS) | top − rail, column 8 | |
| 3 | +5V | 25j (4008 pin 16, VDD) | bottom + rail, column 25 | |
| 4 | GND | 18a (4008 pin 8, VSS) | top − rail, column 18 | |
| 13 | GND | 29j (5 V LED, − leg) | bottom − rail, column 29 | |
| Fixed inputs: CMOS inputs must not float · 1 wire | ||||
| 14 | GND | 8j (4008 pin 9, CIN) | bottom − rail, column 8 | |
| Mode wires: move them between + and − to change the mode · 8 wires | ||||
| 5 | B0 | 10a (4008 pin 6, B1) | top + rail, column 10 | |
| 6 | B1 | 12a (4008 pin 4, B2) | top − rail, column 12 | |
| 7 | B2 | 14a (4008 pin 2, B3) | top + rail, column 14 | |
| 8 | B3 | 14j (4008 pin 15, B4) | bottom + rail, column 14 | |
| 9 | B4 | 20a (4008 pin 6, B1) | top − rail, column 20 | |
| 10 | B5 | 22a (4008 pin 4, B2) | top + rail, column 22 | |
| 11 | B6 | 24a (4008 pin 2, B3) | top − rail, column 24 | |
| 12 | B7 | 24j (4008 pin 15, B4) | bottom − rail, column 24 | |
| Signal wires · 2 wires | ||||
| 15 | C4 | 13g (4008 pin 14, COUT) | 18g (4008 pin 9, CIN) | |
| 16 | C8 | 23g (4008 pin 14, COUT) | 28g (5 V LED, + leg) | |
| Modules on male-female jumpers · 33 wires | ||||
| 17 | +5V | button A7, + | top + rail, column 5 | |
| 18 | A7 | button A7, OUT | 25a (4008 pin 1, A4) | |
| 19 | GND | button A7, − | top − rail, column 7 | |
| 20 | +5V | button A6, + | top + rail, column 9 | |
| 21 | A6 | button A6, OUT | 23a (4008 pin 3, A3) | |
| 22 | GND | button A6, − | top − rail, column 11 | |
| 23 | +5V | button A5, + | top + rail, column 15 | |
| 24 | A5 | button A5, OUT | 21a (4008 pin 5, A2) | |
| 25 | GND | button A5, − | top − rail, column 17 | |
| 26 | +5V | button A4, + | top + rail, column 20 | |
| 27 | A4 | button A4, OUT | 19a (4008 pin 7, A1) | |
| 28 | GND | button A4, − | top − rail, column 22 | |
| 29 | +5V | button A3, + | top + rail, column 25 | |
| 30 | A3 | button A3, OUT | 15a (4008 pin 1, A4) | |
| 31 | GND | button A3, − | top − rail, column 27 | |
| 32 | +5V | button A2, + | top + rail, column 30 | |
| 33 | A2 | button A2, OUT | 13a (4008 pin 3, A3) | |
| 34 | GND | button A2, − | top − rail, column 32 | |
| 35 | +5V | button A1, + | top + rail, column 35 | |
| 36 | A1 | button A1, OUT | 11a (4008 pin 5, A2) | |
| 37 | GND | button A1, − | top − rail, column 37 | |
| 38 | +5V | button A0, + | top + rail, column 40 | |
| 39 | A0 | button A0, OUT | 9a (4008 pin 7, A1) | |
| 40 | GND | button A0, − | top − rail, column 42 | |
| 41 | GND | LED module, G | bottom − rail, column 12 | |
| 42 | S7 | LED module, LED 1 | 22j (4008 pin 13, S4) | |
| 43 | S6 | LED module, LED 2 | 21j (4008 pin 12, S3) | |
| 44 | S5 | LED module, LED 3 | 20j (4008 pin 11, S2) | |
| 45 | S4 | LED module, LED 4 | 19j (4008 pin 10, S1) | |
| 46 | S3 | LED module, LED 5 | 12j (4008 pin 13, S4) | |
| 47 | S2 | LED module, LED 6 | 11j (4008 pin 12, S3) | |
| 48 | S1 | LED module, LED 7 | 10j (4008 pin 11, S2) | |
| 49 | S0 | LED module, LED 8 | 9j (4008 pin 10, S1) | |
Test it
- With no button pressed the LEDs show B, 00101101.
- Press A0 (1): 00101110, 46.
- Press A2 and A1 together (6): 00110011, 51. In the low half 1101 + 0110 makes 10011, so C4 carries a 1 into the high half.
- Move all B wires to +5V (255) and press A0: all LEDs dark, the red LED lit: 256.
If it does not work: if the high half never gets the carry, as when 45 + 6 shows 00100011, check the cascade wire from pin 14 of the low chip to pin 9 of the high chip. If the sum is one too big, CARRY IN of the low chip is not at GND. If single bits are wrong, compare the A and B wires with the pins: A and B take turns on the 4008.
8SystemVerilog: the generic design
adder8.sv adds the two halves separately and passes the carry, to match the two chips. The one line
{ carry, sum } = a + b would give the same result, and synthesis would choose the adder. It is
synthesizable.
// Exercise M4: an 8-bit adder made of two 4-bit adders, the carry out of // the low one into the carry in of the high one, like two cascaded 4008s. // // Generic version that matches schematic 1, not tied to any chip. carry is // the ninth bit of the sum: 1 when a + b > 255. Synthesizable. module adder8 ( input logic [7:0] a, input logic [7:0] b, output logic [7:0] sum, output logic carry ); // The low adder adds bits 3-0 with no carry in; its carry out C4 goes // into the high adder, which adds bits 7-4 wire [4:0] low = 5' (a [3:0]) + 5' (b [3:0]); wire [4:0] high = 5' (a [7:4]) + 5' (b [7:4]) + 5' (low [4]); assign sum = { high [3:0], low [3:0] }; assign carry = high [4]; endmodule
9SystemVerilog model of the chip
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.
// CD4008B: 4-bit full adder with a fast carry output. // // Ports in pin order, named as in the datasheet: A1-A4 and B1-B4 are the // two numbers (A1 and B1 the lowest bits), S1-S4 the sum. The power pins, // VSS (8) and VDD (16), are left out. Synthesizable. // // Inside are four full adders. The chip computes CARRY OUT with extra // look-ahead logic, so it does not wait for the carry to ripple through all // four stages; the result is the same. module cd4008 ( input logic a4, // Pin 1 input logic b3, // Pin 2 input logic a3, // Pin 3 input logic b2, // Pin 4 input logic a2, // Pin 5 input logic b1, // Pin 6 input logic a1, // Pin 7 // Pin 8: VSS input logic carry_in, // Pin 9 output logic s1, // Pin 10 output logic s2, // Pin 11 output logic s3, // Pin 12 output logic s4, // Pin 13 output logic carry_out, // Pin 14 input logic b4 // Pin 15 // Pin 16: VDD ); assign { carry_out, s4, s3, s2, s1 } = { 1'b0, a4, a3, a2, a1 } + { 1'b0, b4, b3, b2, b1 } + { 4'b0, carry_in }; endmodule
10SystemVerilog: the chip-level design
adder8_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as adder8.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 M4, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as adder8.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_m4.py, not this file. Synthesizable. module adder8_chips ( input logic [7:0] a, input logic [7:0] b, output logic [7:0] sum, output logic carry ); // Connections between the chips that are not ports wire c4; // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; //------------------------------------------------------------------------ // U1: CD4008 4-bit full adder, pin 1 in column 8 cd4008 i_u1 ( .a4 ( a [3] ), // Pin 1 <- button A3 .b3 ( b [2] ), // Pin 2 <- B2 wire, at +5V on the breadboard .a3 ( a [2] ), // Pin 3 <- button A2 .b2 ( b [1] ), // Pin 4 <- B1 wire, at GND on the breadboard .a2 ( a [1] ), // Pin 5 <- button A1 .b1 ( b [0] ), // Pin 6 <- B0 wire, at +5V on the breadboard .a1 ( a [0] ), // Pin 7 <- button A0 .carry_in ( gnd ), // Pin 9 <- GND .s1 ( sum [0] ), // Pin 10 -> S0 .s2 ( sum [1] ), // Pin 11 -> S1 .s3 ( sum [2] ), // Pin 12 -> S2 .s4 ( sum [3] ), // Pin 13 -> S3 .carry_out ( c4 ), // Pin 14 -> C4 .b4 ( b [3] ) // Pin 15 <- B3 wire, at +5V on the breadboard ); //------------------------------------------------------------------------ // U2: CD4008 4-bit full adder, pin 1 in column 18 cd4008 i_u2 ( .a4 ( a [7] ), // Pin 1 <- button A7 .b3 ( b [6] ), // Pin 2 <- B6 wire, at GND on the breadboard .a3 ( a [6] ), // Pin 3 <- button A6 .b2 ( b [5] ), // Pin 4 <- B5 wire, at +5V on the breadboard .a2 ( a [5] ), // Pin 5 <- button A5 .b1 ( b [4] ), // Pin 6 <- B4 wire, at GND on the breadboard .a1 ( a [4] ), // Pin 7 <- button A4 .carry_in ( c4 ), // Pin 9 <- U1 pin 14 (COUT) .s1 ( sum [4] ), // Pin 10 -> S4 .s2 ( sum [5] ), // Pin 11 -> S5 .s3 ( sum [6] ), // Pin 12 -> S6 .s4 ( sum [7] ), // Pin 13 -> S7 .carry_out ( carry ), // Pin 14 -> C8 .b4 ( b [7] ) // Pin 15 <- B7 wire, at GND on the breadboard ); endmodule
11Testbench and simulation scripts
tb.sv adds the examples of section 2 and then every pair of 8-bit numbers, 65536 in all, and compares
both designs with integer addition. Like the testbenches of Basics-Graphics-Music, it changes the inputs and checks the
outputs only at rising clock edges. It prints only the examples and any error, and dumps the waveforms only at the
start, so that the files stay small.
tb.sv150 lines · show the codefile
// Testbench for exercise M4. // // Drives the generic design (adder8.sv) and the chip-level design // (adder8_chips.sv) with the same inputs and compares both with integer // addition. Works with Icarus Verilog, Verilator (--binary) and Questa. // // Like the testbenches of Basics-Graphics-Music, it changes the inputs and // checks the outputs only at rising clock edges. It adds the examples of // section 2 and then every pair of 8-bit numbers, 65536 in all; it prints // only the examples and any error, and dumps the waveforms only at the // start. // 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 [7:0] a, b; wire [7:0] sum, sum_chips; wire carry, carry_chips; adder8 i_generic ( .a ( a ), .b ( b ), .sum ( sum ), .carry ( carry ) ); adder8_chips i_chips ( .a ( a ), .b ( b ), .sum ( sum_chips ), .carry ( carry_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: integer addition, the ninth bit is the carry wire [8:0] expected = 9' (a) + 9' (b); wire ok_generic = { carry, sum } === expected; wire ok_chips = { carry_chips, sum_chips } === expected; //------------------------------------------------------------------------ // 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 printing = 1'b1; // Cleared by the stimulus after the examples logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; always @ (posedge clk) if (checking & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (ok_generic ? 0 : 1) + (ok_chips ? 0 : 1); if (printing | ~ ok_generic | ~ ok_chips) $display ("%5d %3d + %3d %b %b %3d %s %s", cycle + 1, a, b, carry, sum, expected, ok_generic ? "ok " : "WRONG ", ok_chips ? "ok" : "WRONG"); end //------------------------------------------------------------------------ // Driving the inputs initial begin $dumpfile ("tb.vcd"); `ifdef __ICARUS__ $dumpvars (0, tb); // Without a scope Icarus also tries to dump $unit `else $dumpvars; `endif $display ("cycle A B C8 S7..S0 A+B generic chips"); checking <= '1; // The examples of section 2 { a, b } <= { 8'd100, 8'd45 }; @ (posedge clk); { a, b } <= { 8'd200, 8'd45 }; @ (posedge clk); { a, b } <= { 8'd15, 8'd1 }; @ (posedge clk); { a, b } <= { 8'd255, 8'd1 }; @ (posedge clk); { a, b } <= { 8'd128, 8'd128 }; @ (posedge clk); { a, b } <= { 8'd85, 8'd170 }; @ (posedge clk); // Every pair, counting up: the low nibbles change fastest, so the // carry between the two adders flips often printing <= '0; for (int i = 0; i < 65536; i ++) begin { b, a } <= 16' (i); @ (posedge clk); if (i == 14) $dumpoff; 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_checks == 2 * (6 + 65536)) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks", n_errors, n_checks); 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 M4: 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 adder8.sv adder8_chips.sv cd4008.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 M4: 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 adder8.sv adder8_chips.sv cd4008.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 M4: 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 adder8.sv adder8_chips.sv cd4008.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 A B C8 S7..S0 A+B generic chips
1 100 + 45 0 10010001 145 ok ok
2 200 + 45 0 11110101 245 ok ok
3 15 + 1 0 00010000 16 ok ok
4 255 + 1 1 00000000 256 ok ok
5 128 + 128 1 00000000 256 ok ok
6 85 + 170 0 11111111 255 ok ok
tb.sv:137: $finish called at 655425000 (1ps)
PASS: 131084 checks, no errors
To see that the testbench catches mistakes, break something and run it again: replace 5' (low [4]) with 5'd0 in adder8.sv, or connect CARRY IN of
the high 4008 to GND instead of C4 in adder8_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_06_binary_counterto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addadder8.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.
Switches 7–0 are A. B is fixed in the code, as the wires on the breadboard: change it to try other numbers. LEDs 7–0
show the sum, and the dot of the rightmost digit is the carry out. To run the chip-level design instead, add
adder8_chips.sv and cd4008.sv and change the module name in the instance.
bgm_lab/lab_top.sv99 lines · show the codefile
// Exercise M4 on an FPGA board with the Basics-Graphics-Music (BGM) labs: // https://github.com/yuri-panchul/basics-graphics-music // // Copy labs/1_basics/1_06_binary_counter to a new directory, replace its // lab_top.sv with this file and add adder8.sv. For the chip-level design // add adder8_chips.sv and cd4008.sv instead, and change the module name // below. // // Switches 7-0 are A. B is fixed in the code, as the wires to the rails // on the breadboard: change B below to try other numbers. LEDs 7-0 show // the sum, and the dot of the rightmost digit is the carry out C8. `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; //------------------------------------------------------------------------ localparam [7:0] B = 8'd45; // 0010 1101, as the wires on the breadboard logic [7:0] sum; logic carry; adder8 i_adder8 ( .a ( sw [7:0] ), .b ( B ), .sum ( sum ), .carry ( carry ) ); assign led = w_led' (sum); assign abcdefgh = { 7'b0, carry }; 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/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addadder8.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 7–0 are A, B is fixed in the code. The screen shows three rows of eight lamps: A, B and the sum, with the carry left of the sum.
bgm_hackathon/hackathon_top.sv93 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise M4 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 adder8.sv. // // Keys 7-0 are A. B is fixed in the code, as the wires to the rails on the // breadboard. The LEDs show the sum, the dot of the rightmost digit the // carry. The screen shows three rows of eight lamps, bit 7 on the left: // A in blue, B in orange and the sum in green, with the carry left of it. 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 ); //------------------------------------------------------------------------ localparam [7:0] B = 8'd45; // 0010 1101, as the wires on the breadboard logic [7:0] sum; logic carry; adder8 i_adder8 ( .a ( key ), .b ( B ), .sum ( sum ), .carry ( carry ) ); assign led = sum; assign abcdefgh = { 7'b0, carry }; assign digit = 8'b0000_0001; //------------------------------------------------------------------------ // Lamps 30 pixels wide, 44 apart, from x = 90: rows at y = 60 (A), // 120 (B) and 180 (the sum); the carry at x = 46 in the row of the sum always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 8; i ++) if (x >= 9' (90 + i * 44) & x < 9' (120 + i * 44)) begin if (y >= 60 & y < 90) begin green = key [7 - i] ? 40 : 8; blue = key [7 - i] ? 31 : 6; end if (y >= 120 & y < 150) begin red = B [7 - i] ? 31 : 6; green = B [7 - i] ? 32 : 6; end if (y >= 180 & y < 210) green = sum [7 - i] ? 63 : 12; end if (x >= 46 & x < 76 & y >= 180 & y < 210) green = carry ? 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.
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.
DIP switches 0–7 are A and BIDIR pins 0–7 are B. The sum lights the segments of the display, bit k on segment k and bit 7 on the dot. All pins are taken, so the carry out has no pin.
tiny_tapeout/project.v48 lines · show the codefile
/* * Exercise M4 on Tiny Tapeout: an 8-bit adder made of two 4-bit adders. * * Add adder8.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 ); // DIP switches 0-7 are A, BIDIR pins 0-7 are B. The circuit has no // clock and no reset. wire [7:0] sum; wire carry; adder8 i_adder8 ( .a ( ui_in ), .b ( uio_in ), .sum ( sum ), .carry ( carry ) ); // The sum on the 7-segment display and the OUTPUT Pmod: bit k lights // segment k, bit 7 the dot. All 24 pins are taken, so the carry out has // no pin: a + b > 255 shows as a small sum. assign uo_out = sum; assign uio_out = 8'b0; assign uio_oe = 8'b0; // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, carry, 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>_adder8
source_files:
- "project.v"
- "adder8.sv"
15Going further
Tasks for students
- Twelve bits. How would a third 4008 connect? How many buttons and wires would a 12-bit adder need?
- Signed numbers. Read A, B and the sum as numbers from −128 to 127. When is the sum wrong? (Look at the carry into bit 7 and out of it.)
- Subtraction. With 4070 XOR gates on B and a 1 at CARRY IN of the low chip, the adder computes A − B. Why? (Exercise BB10 does it with the 40181.)
- In SystemVerilog: write a 32-bit adder from eight 4-bit adders with a
generateloop, and compare its delay after synthesis with the one-line version.
Files of this exercise
- adder8.sv: the generic design (section 8)
- cd4008.sv: chip models (section 9)
- adder8_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 lesson 1.4 of the Habr article about the electronics course, where the second group builds a cascade of two 4008s to add 8-bit numbers. The chip is described in the Texas Instruments datasheet for the CD4008B.