Exercise B8 · basics · adder
4-bit adder with buttons and LEDs
A 4008 adds two 4-bit numbers. Eight buttons give the numbers bit by bit, and five LEDs show the sum in binary. The adder is a black box here: the exercise is about binary numbers and about reading a datasheet, not about what is inside the chip.
1What it is and why it matters
What it does. Buttons A4, A3, A2 and A1 give the bits of the first number A, buttons B4–B1 the bits of the second number B; a pressed button is a 1. The 4008 adds them and puts out a 5-bit result: the carry, CARRY OUT, and the four sum bits S4–S1. LEDs 1–5 of the LED module show the carry and S4 … S1 from left to right, so the row of LEDs reads like a binary number. Lesson 1.2 of the Habr article starts the adders this way: enter A and B, read the sum, and practise binary numbers.
How it works. Each bit of a binary number is worth twice the bit to its right: 1, 2, 4, 8, 16. The adder adds column by column like a pupil adding decimal numbers on paper, and when a column gives 2 or 3 it writes 0 or 1 and carries 1 into the next column. The carry out of the last column is the fifth bit of the sum.
Where it is used. Every processor has adders: to add numbers, to compute the address of the next instruction, to compare (A − B is an addition of A and the inverted B). The arithmetic-logic unit of the RISC-V processor at the end of the course is built around an adder like this one, only 32 bits wide.
Why learn it.
- Binary numbers stop being abstract when you press them in and see the sum light up.
- The 4008 is the first “building block” of the course: a chip that does a whole job. Later exercises build the same adder from gates (M1–M3), cascade two 4008s into 8 bits (M4) and add bit by bit, serially (P1, P2).
- In SystemVerilog this whole chip is one operator,
+. Knowing what it costs in hardware is the difference between writing code and designing hardware.
2The key elements: binary numbers and the adder
Bits and their weights
A 4-bit number has bits worth 8, 4, 2 and 1. A = 13 is 8 + 4 + 1, so A4, A3 and A1 are pressed and A2 is not:
| Bit | A4 | A3 | A2 | A1 |
|---|---|---|---|---|
| Weight | 8 | 4 | 2 | 1 |
| A = 13 | 1 | 1 | 0 | 1 |
Some sums
The 5-bit result is COUT S4 S3 S2 S1. With CARRY OUT on the left the five LEDs read as one number from 0 to 30.
| A | A4 … A1 | B | B4 … B1 | LEDs 1–5: COUT S4 … S1 | A + B |
|---|---|---|---|---|---|
| 3 | 0011 | 4 | 0100 | 00111 | 7 |
| 5 | 0101 | 5 | 0101 | 01010 | 10 |
| 7 | 0111 | 1 | 0001 | 01000 | 8 |
| 9 | 1001 | 6 | 0110 | 01111 | 15 |
| 13 | 1101 | 2 | 0010 | 01111 | 15 |
| 15 | 1111 | 1 | 0001 | 10000 | 16 |
| 15 | 1111 | 15 | 1111 | 11110 | 30 |
Waveforms
tb.sv in Icarus Verilog: 17 of its 512 steps, A = 15 and B
counting up, with the numbers in decimal. From B = 1 on the sum does not fit into four bits, and CARRY is 1. The testbench sets A and B right after a rising edge of its clock (dotted lines)
and checks the sum at the next edge. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4008 | 4-bit full adder | 1 | $1.00 | leeselectronic.com |
| Button modules | A1-A4 and B1-B4; 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 | shows the sum and the carry | 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 |
| Male-male Dupont jumpers | 4 wires on the board (or pre-cut wires) | 4 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 30 leads to the buttons and the LED module | 30 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; all of these parts are in the Basics kit. The eight buttons are the two packs of five.
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
The pins of A and B take turns along the bottom row: A4 on pin 1, B3, A3, B2, A2, B1, A1 on pin 7. B4 is the odd one out, on pin 15 of the top row. The sum S1–S4 comes out on pins 10–13 and CARRY OUT on pin 14, next to each other on the top row; CARRY IN is pin 9. Here CARRY IN is tied to GND: nothing comes in from a lower adder.
Polarity. All signals are active high: 1 is +5 V. A1, B1 and S1 are the lowest bits.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A4 | input | number A, bit 3 (weight 8) | A4 |
| 2 | B3 | input | number B, bit 2 (weight 4) | B3 |
| 3 | A3 | input | number A, bit 2 (weight 4) | A3 |
| 4 | B2 | input | number B, bit 1 (weight 2) | B2 |
| 5 | A2 | input | number A, bit 1 (weight 2) | A2 |
| 6 | B1 | input | number B, bit 0 (weight 1) | B1 |
| 7 | A1 | input | number A, bit 0 (weight 1) | A1 |
| 8 | VSS | power | ground, 0 V | GND |
| 9 | CIN | input | carry in, added to A + B | GND |
| 10 | S1 | output | sum, bit 0 (weight 1) | S1 |
| 11 | S2 | output | sum, bit 1 (weight 2) | S2 |
| 12 | S3 | output | sum, bit 2 (weight 4) | S3 |
| 13 | S4 | output | sum, bit 3 (weight 8) | S4 |
| 14 | COUT | output | carry out, the fifth bit of the sum (weight 16) | CO |
| 15 | B4 | input | number B, bit 3 (weight 8) | B4 |
| 16 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the adder as a block
adder_leds.sv describes,
only that the generic design spells out the four full adders inside the block. CARRY IN is tied to GND. · SVG · PNGExercises M1–M3 open the block: a half adder is an XOR and an AND gate, a full adder two of them and an OR.
6Schematic 2: connections
| Net | From | To | Wire |
|---|---|---|---|
| A4 | button A4, OUT | 4008 1 (A4) | M-F |
| A3 | button A3, OUT | 4008 3 (A3) | M-F |
| A2 | button A2, OUT | 4008 5 (A2) | M-F |
| A1 | button A1, OUT | 4008 7 (A1) | M-F |
| B4 | button B4, OUT | 4008 15 (B4) | M-F, M-M |
| B3 | button B3, OUT | 4008 2 (B3) | M-F |
| B2 | button B2, OUT | 4008 4 (B2) | M-F |
| B1 | button B1, OUT | 4008 6 (B1) | M-F |
| CO | 4008 14 (COUT) | LED module, LED 1 | M-F |
| S4 | 4008 13 (S4) | LED module, LED 2 | M-F |
| S3 | 4008 12 (S3) | LED module, LED 3 | M-F |
| S2 | 4008 11 (S2) | LED module, LED 4 | M-F |
| S1 | 4008 10 (S1) | LED module, LED 5 | M-F |
| +5V | +5V | 4008 16 (VDD) | |
| GND | GND | 4008 8 (VSS), 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 4008 across the gap, notch to the left, pin 1 in hole 22f.
- Power: a red wire from pin 16 (column 22, top half) to the top + rail and a black wire from pin 8 (column 29, bottom half) to the bottom − rail. Then CARRY IN, pin 9 (column 29, top half), to the top − rail.
- One signal wire: B4 is on the top row, its button below the board, so a wire brings B4 from column 23 (top half) down to column 21 (bottom half), where the button's lead goes.
- With the power off, the buttons: + to the + rail, − to the − rail and OUT to the A or B pin of the chip. Put them in the order A4, A3, A2, A1, B4, B3, B2, B1 from left to right, so that each number reads like on paper.
- The LED module above the board: G to the top − rail, LED 1 to CARRY OUT (pin 14), LEDs 2–5 to S4, S3, S2 and S1 (pins 13, 12, 11, 10).
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 2 wires | ||||
| 1 | +5V | 22a (4008 pin 16, VDD) | top + rail, column 22 | |
| 2 | GND | 29j (4008 pin 8, VSS) | bottom − rail, column 29 | |
| Fixed inputs: CMOS inputs must not float · 1 wire | ||||
| 3 | GND | 29a (4008 pin 9, CIN) | top − rail, column 29 | |
| Signal wires · 1 wire | ||||
| 4 | B4 | 23d (4008 pin 15, B4) | 21f | |
| Modules on male-female jumpers · 30 wires | ||||
| 5 | +5V | button A4, + | bottom + rail, column 5 | |
| 6 | A4 | button A4, OUT | 22j (4008 pin 1, A4) | |
| 7 | GND | button A4, − | bottom − rail, column 7 | |
| 8 | +5V | button A3, + | bottom + rail, column 10 | |
| 9 | A3 | button A3, OUT | 24j (4008 pin 3, A3) | |
| 10 | GND | button A3, − | bottom − rail, column 12 | |
| 11 | +5V | button A2, + | bottom + rail, column 15 | |
| 12 | A2 | button A2, OUT | 26j (4008 pin 5, A2) | |
| 13 | GND | button A2, − | bottom − rail, column 17 | |
| 14 | +5V | button A1, + | bottom + rail, column 20 | |
| 15 | A1 | button A1, OUT | 28j (4008 pin 7, A1) | |
| 16 | GND | button A1, − | bottom − rail, column 22 | |
| 17 | +5V | button B4, + | bottom + rail, column 26 | |
| 18 | B4 | button B4, OUT | 21j | |
| 19 | GND | button B4, − | bottom − rail, column 28 | |
| 20 | +5V | button B3, + | bottom + rail, column 31 | |
| 21 | B3 | button B3, OUT | 23j (4008 pin 2, B3) | |
| 22 | GND | button B3, − | bottom − rail, column 33 | |
| 23 | +5V | button B2, + | bottom + rail, column 36 | |
| 24 | B2 | button B2, OUT | 25j (4008 pin 4, B2) | |
| 25 | GND | button B2, − | bottom − rail, column 38 | |
| 26 | +5V | button B1, + | bottom + rail, column 41 | |
| 27 | B1 | button B1, OUT | 27j (4008 pin 6, B1) | |
| 28 | GND | button B1, − | bottom − rail, column 43 | |
| 29 | GND | LED module, G | top − rail, column 24 | |
| 30 | CO | LED module, LED 1 | 24a (4008 pin 14, COUT) | |
| 31 | S4 | LED module, LED 2 | 25a (4008 pin 13, S4) | |
| 32 | S3 | LED module, LED 3 | 26a (4008 pin 12, S3) | |
| 33 | S2 | LED module, LED 4 | 27a (4008 pin 11, S2) | |
| 34 | S1 | LED module, LED 5 | 28a (4008 pin 10, S1) | |
Test it
- No button pressed: 0 + 0 = 0, all LEDs dark.
- Press A1 alone: LED 5 (S1) lights. Press B1 too: 1 + 1 = 2, so S1 goes dark and S2 lights: the carry moved one column to the left.
- Press all eight: 15 + 15 = 30 = 11110, LEDs 1–4 lit and LED 5 dark.
- Pick two numbers, work out the sum on paper in binary, then check it on the LEDs.
If it does not work: if one sum bit is always wrong, check the lead of that LED: S1–S4 are on neighbouring pins and easy to swap. If one button changes nothing, check its OUT lead and its + lead. If the sum is one too high, CARRY IN is not at GND. If the LEDs flicker when you touch the wires, an input is floating.
8SystemVerilog: the generic design
adder_leds.sv describes the adder as four full adders in a chain, a ripple-carry adder: each
adds one bit of A, one bit of B and the carry from the bit below. It is synthesizable.
// Exercise B8: a 4-bit adder, its inputs from buttons and its outputs on // LEDs. // // Generic version that matches schematic 1: four full adders in a chain // (a ripple-carry adder), not tied to any chip. Synthesizable. module adder_leds ( input logic [3:0] a, // The first number, from buttons A4-A1 input logic [3:0] b, // The second number, from buttons B4-B1 output logic [3:0] sum, // a + b without the carry, on LEDs 4-1 output logic carry // The fifth bit of the sum: 1 when a + b > 15 ); // Each full adder adds one bit of a, one bit of b and the carry from // the bit below, and passes its own carry to the bit above logic [4:0] c; assign c [0] = 1'b0; // The carry into bit 0, like CARRY IN of the 4008 tied to GND for (genvar i = 0; i < 4; i ++) begin : full_adder assign sum [i] = a [i] ^ b [i] ^ c [i]; assign c [i + 1] = a [i] & b [i] | (a [i] ^ b [i]) & c [i]; end assign carry = c [4]; endmodule
The loop with genvar is not a loop that runs: it repeats the two assign statements for
each bit, four copies of the same circuit. In a real design you would write assign { carry, sum } = a +
b; and let synthesis choose the adder; the testbench uses exactly that as its reference.
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
adder_leds_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as adder_leds.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 B8, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as adder_leds.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_b8.py, not this file. Synthesizable. module adder_leds_chips ( input logic [3:0] a, input logic [3:0] b, output logic [3:0] sum, output logic carry ); // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; //------------------------------------------------------------------------ // U1: CD4008 4-bit full adder, pin 1 in column 22 cd4008 i_u1 ( .a4 ( a [3] ), // Pin 1 <- button A4 .b3 ( b [2] ), // Pin 2 <- button B3 .a3 ( a [2] ), // Pin 3 <- button A3 .b2 ( b [1] ), // Pin 4 <- button B2 .a2 ( a [1] ), // Pin 5 <- button A2 .b1 ( b [0] ), // Pin 6 <- button B1 .a1 ( a [0] ), // Pin 7 <- button A1 .carry_in ( gnd ), // Pin 9 <- GND .s1 ( sum [0] ), // Pin 10 -> S1 .s2 ( sum [1] ), // Pin 11 -> S2 .s3 ( sum [2] ), // Pin 12 -> S3 .s4 ( sum [3] ), // Pin 13 -> S4 .carry_out ( carry ), // Pin 14 -> CO .b4 ( b [3] ) // Pin 15 <- button B4 ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with the sum of two integers,
a + b. The circuit has no clock, but the testbench has one, like the testbenches of
Basics-Graphics-Music: it sets A and B right after a rising edge and checks the sum at the next one. It tries all 256
pairs twice, counting up and in Gray code order, and prints PASS only if both designs match every time
and every pair was seen.
tb.sv152 lines · show the codefile
// Testbench for exercise B8. // // Runs the generic design (adder_leds.sv) and the chip-level design // (adder_leds_chips.sv) side by side and compares both with // the sum of two integers, a + b. 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 256 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 [3:0] a; logic [3:0] b; wire [3:0] sum, sum_chips; wire carry, carry_chips; adder_leds i_generic ( .a ( a ), .b ( b ), .sum ( sum ), .carry ( carry ) ); adder_leds_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: the sum of two integers, a + b. // Bits, from the left: sum, carry. function automatic logic [4:0] expected (input logic [3:0] in_a, input logic [3:0] in_b); int s = int' (in_a) + int' (in_b); return { 4' (s), s > 15 }; endfunction wire [4:0] out = { sum, carry }; wire [4:0] out_chips = { sum_chips, carry_chips }; wire [4:0] out_model = expected (a, b); //------------------------------------------------------------------------ // 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 [255: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 | 256'b1 << { a, b }; $display ("%5d %b %b %b %b %s %s", cycle + 1, a, b, sum, carry, 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 A B SUM CARRY generic chips"); checking <= '1; for (int i = 0; i < 256; i ++) begin { a, b } <= 8' (i); @ (posedge clk); end for (int i = 0; i < 256; i ++) begin { a, b } <= 8' (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), 256); 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 B8: run the testbench with Icarus Verilog 12 or later. # The output goes to icarus.log and the waveform to tb.vcd, # for GTKWave or Surfer. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv adder_leds.sv adder_leds_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 B8: run the testbench with Verilator 5. --binary builds # a simulator with the testbench's delays; it needs a C++20 compiler. # The output goes to verilator.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv adder_leds.sv adder_leds_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 B8: run the testbench with Questa, for example the free # Questa Starter Edition that comes with Quartus Prime Lite. # The output goes to questa.log and the waveform to tb.vcd. set -Eeuo pipefail cd "$(dirname "$0")" files=(tb.sv adder_leds.sv adder_leds_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 SUM CARRY generic chips
1 0000 0000 0000 0 ok ok
2 0000 0001 0001 0 ok ok
3 0000 0010 0010 0 ok ok
4 0000 0011 0011 0 ok ok
5 0000 0100 0100 0 ok ok
6 0000 0101 0101 0 ok ok
7 0000 0110 0110 0 ok ok
8 0000 0111 0111 0 ok ok
9 0000 1000 1000 0 ok ok
10 0000 1001 1001 0 ok ok
11 0000 1010 1010 0 ok ok
12 0000 1011 1011 0 ok ok
13 0000 1100 1100 0 ok ok
14 0000 1101 1101 0 ok ok
15 0000 1110 1110 0 ok ok
16 0000 1111 1111 0 ok ok
17 0001 0000 0001 0 ok ok
18 0001 0001 0010 0 ok ok
19 0001 0010 0011 0 ok ok
20 0001 0011 0100 0 ok ok
21 0001 0100 0101 0 ok ok
22 0001 0101 0110 0 ok ok
23 0001 0110 0111 0 ok ok
24 0001 0111 1000 0 ok ok
25 0001 1000 1001 0 ok ok
26 0001 1001 1010 0 ok ok
27 0001 1010 1011 0 ok ok
28 0001 1011 1100 0 ok ok
29 0001 1100 1101 0 ok ok
30 0001 1101 1110 0 ok ok
31 0001 1110 1111 0 ok ok
32 0001 1111 0000 1 ok ok
33 0010 0000 0010 0 ok ok
34 0010 0001 0011 0 ok ok
35 0010 0010 0100 0 ok ok
36 0010 0011 0101 0 ok ok
37 0010 0100 0110 0 ok ok
38 0010 0101 0111 0 ok ok
39 0010 0110 1000 0 ok ok
40 0010 0111 1001 0 ok ok
41 0010 1000 1010 0 ok ok
42 0010 1001 1011 0 ok ok
43 0010 1010 1100 0 ok ok
44 0010 1011 1101 0 ok ok
45 0010 1100 1110 0 ok ok
46 0010 1101 1111 0 ok ok
47 0010 1110 0000 1 ok ok
48 0010 1111 0001 1 ok ok
49 0011 0000 0011 0 ok ok
50 0011 0001 0100 0 ok ok
51 0011 0010 0101 0 ok ok
52 0011 0011 0110 0 ok ok
53 0011 0100 0111 0 ok ok
54 0011 0101 1000 0 ok ok
55 0011 0110 1001 0 ok ok
56 0011 0111 1010 0 ok ok
57 0011 1000 1011 0 ok ok
58 0011 1001 1100 0 ok ok
59 0011 1010 1101 0 ok ok
60 0011 1011 1110 0 ok ok
61 0011 1100 1111 0 ok ok
62 0011 1101 0000 1 ok ok
63 0011 1110 0001 1 ok ok
64 0011 1111 0010 1 ok ok
65 0100 0000 0100 0 ok ok
66 0100 0001 0101 0 ok ok
67 0100 0010 0110 0 ok ok
68 0100 0011 0111 0 ok ok
69 0100 0100 1000 0 ok ok
70 0100 0101 1001 0 ok ok
71 0100 0110 1010 0 ok ok
72 0100 0111 1011 0 ok ok
73 0100 1000 1100 0 ok ok
74 0100 1001 1101 0 ok ok
75 0100 1010 1110 0 ok ok
76 0100 1011 1111 0 ok ok
77 0100 1100 0000 1 ok ok
78 0100 1101 0001 1 ok ok
79 0100 1110 0010 1 ok ok
80 0100 1111 0011 1 ok ok
81 0101 0000 0101 0 ok ok
82 0101 0001 0110 0 ok ok
83 0101 0010 0111 0 ok ok
84 0101 0011 1000 0 ok ok
85 0101 0100 1001 0 ok ok
86 0101 0101 1010 0 ok ok
87 0101 0110 1011 0 ok ok
88 0101 0111 1100 0 ok ok
89 0101 1000 1101 0 ok ok
90 0101 1001 1110 0 ok ok
91 0101 1010 1111 0 ok ok
92 0101 1011 0000 1 ok ok
93 0101 1100 0001 1 ok ok
94 0101 1101 0010 1 ok ok
95 0101 1110 0011 1 ok ok
96 0101 1111 0100 1 ok ok
97 0110 0000 0110 0 ok ok
98 0110 0001 0111 0 ok ok
99 0110 0010 1000 0 ok ok
100 0110 0011 1001 0 ok ok
101 0110 0100 1010 0 ok ok
102 0110 0101 1011 0 ok ok
103 0110 0110 1100 0 ok ok
104 0110 0111 1101 0 ok ok
105 0110 1000 1110 0 ok ok
106 0110 1001 1111 0 ok ok
107 0110 1010 0000 1 ok ok
108 0110 1011 0001 1 ok ok
109 0110 1100 0010 1 ok ok
110 0110 1101 0011 1 ok ok
111 0110 1110 0100 1 ok ok
112 0110 1111 0101 1 ok ok
113 0111 0000 0111 0 ok ok
114 0111 0001 1000 0 ok ok
115 0111 0010 1001 0 ok ok
116 0111 0011 1010 0 ok ok
117 0111 0100 1011 0 ok ok
118 0111 0101 1100 0 ok ok
119 0111 0110 1101 0 ok ok
120 0111 0111 1110 0 ok ok
121 0111 1000 1111 0 ok ok
122 0111 1001 0000 1 ok ok
123 0111 1010 0001 1 ok ok
124 0111 1011 0010 1 ok ok
125 0111 1100 0011 1 ok ok
126 0111 1101 0100 1 ok ok
127 0111 1110 0101 1 ok ok
128 0111 1111 0110 1 ok ok
129 1000 0000 1000 0 ok ok
130 1000 0001 1001 0 ok ok
131 1000 0010 1010 0 ok ok
132 1000 0011 1011 0 ok ok
133 1000 0100 1100 0 ok ok
134 1000 0101 1101 0 ok ok
135 1000 0110 1110 0 ok ok
136 1000 0111 1111 0 ok ok
137 1000 1000 0000 1 ok ok
138 1000 1001 0001 1 ok ok
139 1000 1010 0010 1 ok ok
140 1000 1011 0011 1 ok ok
141 1000 1100 0100 1 ok ok
142 1000 1101 0101 1 ok ok
143 1000 1110 0110 1 ok ok
144 1000 1111 0111 1 ok ok
145 1001 0000 1001 0 ok ok
146 1001 0001 1010 0 ok ok
147 1001 0010 1011 0 ok ok
148 1001 0011 1100 0 ok ok
149 1001 0100 1101 0 ok ok
150 1001 0101 1110 0 ok ok
151 1001 0110 1111 0 ok ok
152 1001 0111 0000 1 ok ok
153 1001 1000 0001 1 ok ok
154 1001 1001 0010 1 ok ok
155 1001 1010 0011 1 ok ok
156 1001 1011 0100 1 ok ok
157 1001 1100 0101 1 ok ok
158 1001 1101 0110 1 ok ok
159 1001 1110 0111 1 ok ok
160 1001 1111 1000 1 ok ok
161 1010 0000 1010 0 ok ok
162 1010 0001 1011 0 ok ok
163 1010 0010 1100 0 ok ok
164 1010 0011 1101 0 ok ok
165 1010 0100 1110 0 ok ok
166 1010 0101 1111 0 ok ok
167 1010 0110 0000 1 ok ok
168 1010 0111 0001 1 ok ok
169 1010 1000 0010 1 ok ok
170 1010 1001 0011 1 ok ok
171 1010 1010 0100 1 ok ok
172 1010 1011 0101 1 ok ok
173 1010 1100 0110 1 ok ok
174 1010 1101 0111 1 ok ok
175 1010 1110 1000 1 ok ok
176 1010 1111 1001 1 ok ok
177 1011 0000 1011 0 ok ok
178 1011 0001 1100 0 ok ok
179 1011 0010 1101 0 ok ok
180 1011 0011 1110 0 ok ok
181 1011 0100 1111 0 ok ok
182 1011 0101 0000 1 ok ok
183 1011 0110 0001 1 ok ok
184 1011 0111 0010 1 ok ok
185 1011 1000 0011 1 ok ok
186 1011 1001 0100 1 ok ok
187 1011 1010 0101 1 ok ok
188 1011 1011 0110 1 ok ok
189 1011 1100 0111 1 ok ok
190 1011 1101 1000 1 ok ok
191 1011 1110 1001 1 ok ok
192 1011 1111 1010 1 ok ok
193 1100 0000 1100 0 ok ok
194 1100 0001 1101 0 ok ok
195 1100 0010 1110 0 ok ok
196 1100 0011 1111 0 ok ok
197 1100 0100 0000 1 ok ok
198 1100 0101 0001 1 ok ok
199 1100 0110 0010 1 ok ok
200 1100 0111 0011 1 ok ok
201 1100 1000 0100 1 ok ok
202 1100 1001 0101 1 ok ok
203 1100 1010 0110 1 ok ok
204 1100 1011 0111 1 ok ok
205 1100 1100 1000 1 ok ok
206 1100 1101 1001 1 ok ok
207 1100 1110 1010 1 ok ok
208 1100 1111 1011 1 ok ok
209 1101 0000 1101 0 ok ok
210 1101 0001 1110 0 ok ok
211 1101 0010 1111 0 ok ok
212 1101 0011 0000 1 ok ok
213 1101 0100 0001 1 ok ok
214 1101 0101 0010 1 ok ok
215 1101 0110 0011 1 ok ok
216 1101 0111 0100 1 ok ok
217 1101 1000 0101 1 ok ok
218 1101 1001 0110 1 ok ok
219 1101 1010 0111 1 ok ok
220 1101 1011 1000 1 ok ok
221 1101 1100 1001 1 ok ok
222 1101 1101 1010 1 ok ok
223 1101 1110 1011 1 ok ok
224 1101 1111 1100 1 ok ok
225 1110 0000 1110 0 ok ok
226 1110 0001 1111 0 ok ok
227 1110 0010 0000 1 ok ok
228 1110 0011 0001 1 ok ok
229 1110 0100 0010 1 ok ok
230 1110 0101 0011 1 ok ok
231 1110 0110 0100 1 ok ok
232 1110 0111 0101 1 ok ok
233 1110 1000 0110 1 ok ok
234 1110 1001 0111 1 ok ok
235 1110 1010 1000 1 ok ok
236 1110 1011 1001 1 ok ok
237 1110 1100 1010 1 ok ok
238 1110 1101 1011 1 ok ok
239 1110 1110 1100 1 ok ok
240 1110 1111 1101 1 ok ok
241 1111 0000 1111 0 ok ok
242 1111 0001 0000 1 ok ok
243 1111 0010 0001 1 ok ok
244 1111 0011 0010 1 ok ok
245 1111 0100 0011 1 ok ok
246 1111 0101 0100 1 ok ok
247 1111 0110 0101 1 ok ok
248 1111 0111 0110 1 ok ok
249 1111 1000 0111 1 ok ok
250 1111 1001 1000 1 ok ok
251 1111 1010 1001 1 ok ok
252 1111 1011 1010 1 ok ok
253 1111 1100 1011 1 ok ok
254 1111 1101 1100 1 ok ok
255 1111 1110 1101 1 ok ok
256 1111 1111 1110 1 ok ok
257 0000 0000 0000 0 ok ok
258 0000 0001 0001 0 ok ok
259 0000 0011 0011 0 ok ok
260 0000 0010 0010 0 ok ok
261 0000 0110 0110 0 ok ok
262 0000 0111 0111 0 ok ok
263 0000 0101 0101 0 ok ok
264 0000 0100 0100 0 ok ok
265 0000 1100 1100 0 ok ok
266 0000 1101 1101 0 ok ok
267 0000 1111 1111 0 ok ok
268 0000 1110 1110 0 ok ok
269 0000 1010 1010 0 ok ok
270 0000 1011 1011 0 ok ok
271 0000 1001 1001 0 ok ok
272 0000 1000 1000 0 ok ok
273 0001 1000 1001 0 ok ok
274 0001 1001 1010 0 ok ok
275 0001 1011 1100 0 ok ok
276 0001 1010 1011 0 ok ok
277 0001 1110 1111 0 ok ok
278 0001 1111 0000 1 ok ok
279 0001 1101 1110 0 ok ok
280 0001 1100 1101 0 ok ok
281 0001 0100 0101 0 ok ok
282 0001 0101 0110 0 ok ok
283 0001 0111 1000 0 ok ok
284 0001 0110 0111 0 ok ok
285 0001 0010 0011 0 ok ok
286 0001 0011 0100 0 ok ok
287 0001 0001 0010 0 ok ok
288 0001 0000 0001 0 ok ok
289 0011 0000 0011 0 ok ok
290 0011 0001 0100 0 ok ok
291 0011 0011 0110 0 ok ok
292 0011 0010 0101 0 ok ok
293 0011 0110 1001 0 ok ok
294 0011 0111 1010 0 ok ok
295 0011 0101 1000 0 ok ok
296 0011 0100 0111 0 ok ok
297 0011 1100 1111 0 ok ok
298 0011 1101 0000 1 ok ok
299 0011 1111 0010 1 ok ok
300 0011 1110 0001 1 ok ok
301 0011 1010 1101 0 ok ok
302 0011 1011 1110 0 ok ok
303 0011 1001 1100 0 ok ok
304 0011 1000 1011 0 ok ok
305 0010 1000 1010 0 ok ok
306 0010 1001 1011 0 ok ok
307 0010 1011 1101 0 ok ok
308 0010 1010 1100 0 ok ok
309 0010 1110 0000 1 ok ok
310 0010 1111 0001 1 ok ok
311 0010 1101 1111 0 ok ok
312 0010 1100 1110 0 ok ok
313 0010 0100 0110 0 ok ok
314 0010 0101 0111 0 ok ok
315 0010 0111 1001 0 ok ok
316 0010 0110 1000 0 ok ok
317 0010 0010 0100 0 ok ok
318 0010 0011 0101 0 ok ok
319 0010 0001 0011 0 ok ok
320 0010 0000 0010 0 ok ok
321 0110 0000 0110 0 ok ok
322 0110 0001 0111 0 ok ok
323 0110 0011 1001 0 ok ok
324 0110 0010 1000 0 ok ok
325 0110 0110 1100 0 ok ok
326 0110 0111 1101 0 ok ok
327 0110 0101 1011 0 ok ok
328 0110 0100 1010 0 ok ok
329 0110 1100 0010 1 ok ok
330 0110 1101 0011 1 ok ok
331 0110 1111 0101 1 ok ok
332 0110 1110 0100 1 ok ok
333 0110 1010 0000 1 ok ok
334 0110 1011 0001 1 ok ok
335 0110 1001 1111 0 ok ok
336 0110 1000 1110 0 ok ok
337 0111 1000 1111 0 ok ok
338 0111 1001 0000 1 ok ok
339 0111 1011 0010 1 ok ok
340 0111 1010 0001 1 ok ok
341 0111 1110 0101 1 ok ok
342 0111 1111 0110 1 ok ok
343 0111 1101 0100 1 ok ok
344 0111 1100 0011 1 ok ok
345 0111 0100 1011 0 ok ok
346 0111 0101 1100 0 ok ok
347 0111 0111 1110 0 ok ok
348 0111 0110 1101 0 ok ok
349 0111 0010 1001 0 ok ok
350 0111 0011 1010 0 ok ok
351 0111 0001 1000 0 ok ok
352 0111 0000 0111 0 ok ok
353 0101 0000 0101 0 ok ok
354 0101 0001 0110 0 ok ok
355 0101 0011 1000 0 ok ok
356 0101 0010 0111 0 ok ok
357 0101 0110 1011 0 ok ok
358 0101 0111 1100 0 ok ok
359 0101 0101 1010 0 ok ok
360 0101 0100 1001 0 ok ok
361 0101 1100 0001 1 ok ok
362 0101 1101 0010 1 ok ok
363 0101 1111 0100 1 ok ok
364 0101 1110 0011 1 ok ok
365 0101 1010 1111 0 ok ok
366 0101 1011 0000 1 ok ok
367 0101 1001 1110 0 ok ok
368 0101 1000 1101 0 ok ok
369 0100 1000 1100 0 ok ok
370 0100 1001 1101 0 ok ok
371 0100 1011 1111 0 ok ok
372 0100 1010 1110 0 ok ok
373 0100 1110 0010 1 ok ok
374 0100 1111 0011 1 ok ok
375 0100 1101 0001 1 ok ok
376 0100 1100 0000 1 ok ok
377 0100 0100 1000 0 ok ok
378 0100 0101 1001 0 ok ok
379 0100 0111 1011 0 ok ok
380 0100 0110 1010 0 ok ok
381 0100 0010 0110 0 ok ok
382 0100 0011 0111 0 ok ok
383 0100 0001 0101 0 ok ok
384 0100 0000 0100 0 ok ok
385 1100 0000 1100 0 ok ok
386 1100 0001 1101 0 ok ok
387 1100 0011 1111 0 ok ok
388 1100 0010 1110 0 ok ok
389 1100 0110 0010 1 ok ok
390 1100 0111 0011 1 ok ok
391 1100 0101 0001 1 ok ok
392 1100 0100 0000 1 ok ok
393 1100 1100 1000 1 ok ok
394 1100 1101 1001 1 ok ok
395 1100 1111 1011 1 ok ok
396 1100 1110 1010 1 ok ok
397 1100 1010 0110 1 ok ok
398 1100 1011 0111 1 ok ok
399 1100 1001 0101 1 ok ok
400 1100 1000 0100 1 ok ok
401 1101 1000 0101 1 ok ok
402 1101 1001 0110 1 ok ok
403 1101 1011 1000 1 ok ok
404 1101 1010 0111 1 ok ok
405 1101 1110 1011 1 ok ok
406 1101 1111 1100 1 ok ok
407 1101 1101 1010 1 ok ok
408 1101 1100 1001 1 ok ok
409 1101 0100 0001 1 ok ok
410 1101 0101 0010 1 ok ok
411 1101 0111 0100 1 ok ok
412 1101 0110 0011 1 ok ok
413 1101 0010 1111 0 ok ok
414 1101 0011 0000 1 ok ok
415 1101 0001 1110 0 ok ok
416 1101 0000 1101 0 ok ok
417 1111 0000 1111 0 ok ok
418 1111 0001 0000 1 ok ok
419 1111 0011 0010 1 ok ok
420 1111 0010 0001 1 ok ok
421 1111 0110 0101 1 ok ok
422 1111 0111 0110 1 ok ok
423 1111 0101 0100 1 ok ok
424 1111 0100 0011 1 ok ok
425 1111 1100 1011 1 ok ok
426 1111 1101 1100 1 ok ok
427 1111 1111 1110 1 ok ok
428 1111 1110 1101 1 ok ok
429 1111 1010 1001 1 ok ok
430 1111 1011 1010 1 ok ok
431 1111 1001 1000 1 ok ok
432 1111 1000 0111 1 ok ok
433 1110 1000 0110 1 ok ok
434 1110 1001 0111 1 ok ok
435 1110 1011 1001 1 ok ok
436 1110 1010 1000 1 ok ok
437 1110 1110 1100 1 ok ok
438 1110 1111 1101 1 ok ok
439 1110 1101 1011 1 ok ok
440 1110 1100 1010 1 ok ok
441 1110 0100 0010 1 ok ok
442 1110 0101 0011 1 ok ok
443 1110 0111 0101 1 ok ok
444 1110 0110 0100 1 ok ok
445 1110 0010 0000 1 ok ok
446 1110 0011 0001 1 ok ok
447 1110 0001 1111 0 ok ok
448 1110 0000 1110 0 ok ok
449 1010 0000 1010 0 ok ok
450 1010 0001 1011 0 ok ok
451 1010 0011 1101 0 ok ok
452 1010 0010 1100 0 ok ok
453 1010 0110 0000 1 ok ok
454 1010 0111 0001 1 ok ok
455 1010 0101 1111 0 ok ok
456 1010 0100 1110 0 ok ok
457 1010 1100 0110 1 ok ok
458 1010 1101 0111 1 ok ok
459 1010 1111 1001 1 ok ok
460 1010 1110 1000 1 ok ok
461 1010 1010 0100 1 ok ok
462 1010 1011 0101 1 ok ok
463 1010 1001 0011 1 ok ok
464 1010 1000 0010 1 ok ok
465 1011 1000 0011 1 ok ok
466 1011 1001 0100 1 ok ok
467 1011 1011 0110 1 ok ok
468 1011 1010 0101 1 ok ok
469 1011 1110 1001 1 ok ok
470 1011 1111 1010 1 ok ok
471 1011 1101 1000 1 ok ok
472 1011 1100 0111 1 ok ok
473 1011 0100 1111 0 ok ok
474 1011 0101 0000 1 ok ok
475 1011 0111 0010 1 ok ok
476 1011 0110 0001 1 ok ok
477 1011 0010 1101 0 ok ok
478 1011 0011 1110 0 ok ok
479 1011 0001 1100 0 ok ok
480 1011 0000 1011 0 ok ok
481 1001 0000 1001 0 ok ok
482 1001 0001 1010 0 ok ok
483 1001 0011 1100 0 ok ok
484 1001 0010 1011 0 ok ok
485 1001 0110 1111 0 ok ok
486 1001 0111 0000 1 ok ok
487 1001 0101 1110 0 ok ok
488 1001 0100 1101 0 ok ok
489 1001 1100 0101 1 ok ok
490 1001 1101 0110 1 ok ok
491 1001 1111 1000 1 ok ok
492 1001 1110 0111 1 ok ok
493 1001 1010 0011 1 ok ok
494 1001 1011 0100 1 ok ok
495 1001 1001 0010 1 ok ok
496 1001 1000 0001 1 ok ok
497 1000 1000 0000 1 ok ok
498 1000 1001 0001 1 ok ok
499 1000 1011 0011 1 ok ok
500 1000 1010 0010 1 ok ok
501 1000 1110 0110 1 ok ok
502 1000 1111 0111 1 ok ok
503 1000 1101 0101 1 ok ok
504 1000 1100 0100 1 ok ok
505 1000 0100 1100 0 ok ok
506 1000 0101 1101 0 ok ok
507 1000 0111 1111 0 ok ok
508 1000 0110 1110 0 ok ok
509 1000 0010 1010 0 ok ok
510 1000 0011 1011 0 ok ok
511 1000 0001 1001 0 ok ok
512 1000 0000 1000 0 ok ok
tb.sv:138: $finish called at 5125000 (1ps)
PASS: 1024 checks, no errors
To see that the testbench catches mistakes, break something and run it again: change c [0] = 1'b0 to c [0] = 1'b1 in adder_leds.sv, or swap the
connections of S1 and S2 in adder_leds_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 addadder_leds.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 3–0 are A, switches 7–4 are B; on a board without switches use a lab with keys. LEDs 3–0 show the sum
and LED 4 the carry. To run the chip-level design instead, add adder_leds_chips.sv and
cd4008.sv and change the module name in the instance.
bgm_lab/lab_top.sv96 lines · show the codefile
// Exercise B8 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 adder_leds.sv. For the chip-level design // add adder_leds_chips.sv and cd4008.sv instead, and change the module name // below. // // Switches: sw [3:0] a, sw [7:4] b. The LEDs: led [3:0] sum, led [4] carry. 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 [3:0] sum; logic carry; adder_leds i_adder_leds ( .a ( sw [3:0] ), .b ( sw [7:4] ), .sum ( sum ), .carry ( carry ) ); assign led = w_led' ({ carry, sum }); 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 addadder_leds.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 3–0 are A, keys 7–4 are B. The screen shows 13 lamps: the eight input bits in blue, then the sum and the carry in green.
bgm_hackathon/hackathon_top.sv82 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise B8 in the hackathon setup of Basics-Graphics-Music (BGM): // https://github.com/yuri-panchul/basics-graphics-music // // Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its // hackathon_top.sv with this file and add adder_leds.sv. // // Keys: key [3:0] a, key [7:4] b. The LEDs: led [3:0] sum, led [4] carry. The // screen shows 13 lamps in a row: A1, A2, A3, A4, B1, B2, B3, B4, S1, S2, S3, // S4, CO; 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 [3:0] a = key [3:0]; wire [3:0] b = key [7:4]; logic [3:0] sum; logic carry; adder_leds i_adder_leds ( .a ( a ), .b ( b ), .sum ( sum ), .carry ( carry ) ); assign led = { 3'b0, carry, sum }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 13 square lamps on the 480 x 272 screen, 23 pixels wide, 35 apart: // the inputs in blue, the outputs in green. Bright when 1, dim when 0. wire [12:0] lamp = { carry, sum, b, a }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 13; i ++) if (x >= 9' (18 + i * 35) & x < 9' (41 + i * 35) & y >= 125 & y < 148) begin if (i < 8) 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 3–0 are A and switches 7–4 are B. Segments a–d of the display show S1–S4, segment e the carry: not digits, just five lamps.
tiny_tapeout/project.v50 lines · show the codefile
/* * Exercise B8 on Tiny Tapeout: a 4-bit adder, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add adder_leds.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 ); // Switches 3-0 are a, switches 7-4 are b wire [3:0] sum; wire carry; adder_leds i_adder_leds ( .a ( ui_in [3:0] ), .b ( ui_in [7:4] ), .sum ( sum ), .carry ( carry ) ); // 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) sum [0], b (top right) sum [1], c (bottom right) sum [2], d // (bottom) sum [3], e (bottom left) carry. assign uo_out = { 3'b0, carry, sum }; 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, 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>_adder_leds
source_files:
- "project.v"
- "adder_leds.sv"
15Going further
Tasks for students
- CARRY IN. Move the wire on pin 9 from the − rail to the + rail. What changes, and why is the largest result now 31?
- Subtraction. A − B = A + (NOT B) + 1. With a 4069 inverting B and CARRY IN at +5V, the 4008 subtracts. What does the carry mean then?
- Decimal display. Exercise B9 adds three 4511 decoders and three displays to show A, B and the sum as digits.
- 8 bits. Exercise M4 connects CARRY OUT of one 4008 to CARRY IN of a second one.
- In SystemVerilog: replace the loop in
adder_leds.svwithassign { carry, sum } = a + b;. The testbench still passes: the same function, written differently.
Files of this exercise
- adder_leds.sv: the generic design (section 8)
- cd4008.sv: chip models (section 9)
- adder_leds_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
Lesson 1.2 of the Habr article about the electronics course describes the 4008 with buttons or wires into +5V and GND and the LED array, and the Basics list of “An improved way of teaching school kids digital electronics” names the 4008. The chip is described in the Texas Instruments datasheet for the CD4008B.