Exercise BB10 · building block · ALU
40181 ALU: an adder and subtractor
The 40181 is a 4-bit arithmetic logic unit, the heart of a 1970s processor: four select inputs choose one of 32 functions of two numbers. One button and one inverter switch it between A plus B and A minus B, and the LEDs show the result and the carry.
1What it is and why it matters
What it does. Buttons A3–A0 and B3–B0 give two numbers from 0 to 15. While SUB is released, LEDs 1–4 show F = A + B; while SUB is pressed, F = A − B. LED 5 is the carry out Cn+4 and LED 6 the output A=B.
How it works. The function select inputs S3–S0 and the mode input M choose what the 40181 computes. M is at GND, so it does arithmetic. SUB drives S1 and S2 directly and S0, S3 and the carry input Cn through an inverter. With SUB released, S = 1001 and Cn = 1: F = A plus B. With SUB pressed, S = 0110 and Cn = 0: the chip adds A, the inverted B and a carry of 1, which is A minus B in two's complement. Cn and Cn+4 are active low when the data are read as active high, as here: Cn = 0 means “add one more”, and Cn+4 = 0 means that the result has a fifth bit.
Where it is used. Every processor has an ALU; the bits of an instruction choose its function, as S3–S0 and M do here. The 74181, the TTL version of this chip, was the arithmetic unit of many minicomputers of the 1970s, among them several models of the PDP-11 and the VAX-11/780.
Why learn it.
- It shows how a processor subtracts: by adding the inverted number and one, with the same adder.
- It shows that a few control bits can turn one circuit into many: the select inputs are an opcode.
- It shows carries, borrows and signed numbers in two's complement on real LEDs.
2The key elements: subtraction is addition
| Example | A | B | SUB | F (LEDs 1-4) | Cn+4 (LED 5) | A=B (LED 6) | Answer |
|---|---|---|---|---|---|---|---|
| 5 + 3 | 0101 | 0011 | 0 | 1000 = 8 | 1 | 0 | 8 |
| 9 + 8 | 1001 | 1000 | 0 | 0001 = 1 | 0 | 0 | 17 = 16 + 1 |
| 15 + 1 | 1111 | 0001 | 0 | 0000 = 0 | 0 | 0 | 16 = 16 + 0 |
| 7 + 8 | 0111 | 1000 | 0 | 1111 = 15 | 1 | 1 | 15 |
| 5 − 3 | 0101 | 0011 | 1 | 0010 = 2 | 0 | 0 | 2 |
| 3 − 5 | 0011 | 0101 | 1 | 1110 = 14 | 1 | 0 | −2 = F − 16 |
| 6 − 6 | 0110 | 0110 | 1 | 0000 = 0 | 0 | 0 | 0 |
| 2 − 3 | 0010 | 0011 | 1 | 1111 = 15 | 1 | 1 | −1 = F − 16 |
In addition, Cn+4 goes to 0 when the sum is 16 or more: the fifth bit of the sum is the carry, shown inverted. In subtraction it is the other way round: Cn+4 is 0 while A ≥ B and 1 when A < B, and then F is a negative number in two's complement: 1110 is −2, 1111 is −1. A=B is 1 when all four bits of F are 1. With the carry input at 0, as here during subtraction, that means A − B = −1, not A = B; section 15 shows how to make it a real comparator.
Waveforms
tb.sv in Icarus Verilog: A = 9, B from 4 to 11, each first added,
then subtracted. 9 + 6 = 15 lights A=B, 9 + 7 = 16 turns Cn+4 to 0, and 9 − 10 = −1 shows F = 15 with Cn+4 at 1. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4069 | hex inverter: NOT SUB for S0, S3 and Cn (one of six used) | 1 | $0.30 | Elexp (elexp.com) |
| CD40181 | 4-bit ALU, used for A plus B and A minus B | 1 | $19.78 | electronicplus.com |
| Button modules | A3-A0, B3-B0 and SUB; the first pack of five | 5 | $0.66 / pack | aliexpress.us |
| Button modules | 4 more from the second pack | 4 | $2.03 / pack | aliexpress.us |
| 8-LED module | F on LEDs 1-4, Cn+4 on LED 5, A=B on LED 6 | 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 | 20 wires on the board (or pre-cut wires) | 20 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 34 leads to the modules | 34 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet. The 40181 is out of production, hard to find and expensive; it is an optional part of the Comprehensive kit. The datasheet calls the Motorola MC14581B and the 74181 similar types.
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.
40181: 4-bit ALU
A3–A0 are pins 19, 21, 23 and 2; B3–B0 are pins 18, 20, 22 and 1. S3–S0 are pins 3–6, Cn pin 7 and M pin 8. F0–F2 are pins 9–11 and F3 is pin 13. A=B is pin 14 and the carry out Cn+4 pin 16. P and G (pins 15 and 17) tell a look-ahead carry chip, the 40182, how to join several ALUs into a wider one; they are not used here.
A wide chip. The rows of pins are 0.6 inch apart. On the breadboard the pins go into rows d and h, and the body covers rows e, f and g.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | B0 | input | number B, bit 0 | B0 |
| 2 | A0 | input | number A, bit 0 | A0 |
| 3 | S3 | input | function select, bit 3 | SUB |
| 4 | S2 | input | function select, bit 2 | SUB |
| 5 | S1 | input | function select, bit 1 | SUB |
| 6 | S0 | input | function select, bit 0 | SUB |
| 7 | Cn | input | carry in, active low: 0 adds one more | SUB |
| 8 | M | input | mode: 0 arithmetic, 1 logic (no carries) | GND |
| 9 | F0 | output | result, bit 0 | F0 |
| 10 | F1 | output | result, bit 1 | F1 |
| 11 | F2 | output | result, bit 2 | F2 |
| 12 | VSS | power | ground, 0 V | GND |
| 13 | F3 | output | result, bit 3 | F3 |
| 14 | A=B | output | 1 when F3-F0 are all 1: A = B in subtraction without carry in | A=B |
| 15 | P | output | carry propagate of the 4 bits, active low, for a look-ahead carry chip | not connected |
| 16 | Cn+4 | output | carry out, active low: 0 when the sum has a fifth bit | C4 |
| 17 | G | output | carry generate of the 4 bits, active low, for a look-ahead carry chip | not connected |
| 18 | B3 | input | number B, bit 3 | B3 |
| 19 | A3 | input | number A, bit 3 | A3 |
| 20 | B2 | input | number B, bit 2 | B2 |
| 21 | A2 | input | number A, bit 2 | A2 |
| 22 | B1 | input | number B, bit 1 | B1 |
| 23 | A1 | input | number A, bit 1 | A1 |
| 24 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4069: hex inverter
One inverter, pins 1 → 2, makes NOT SUB for S0, S3 and Cn. The inputs of the other five are tied to the rails.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 2 | SUB |
| 2 | G | output | G = NOT A | SUB |
| 3 | B | input | input of the gate with its output on pin 4 | tied to +5V |
| 4 | H | output | H = NOT B | not connected |
| 5 | C | input | input of the gate with its output on pin 6 | tied to +5V |
| 6 | I | output | I = NOT C | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | J | output | J = NOT D | not connected |
| 9 | D | input | input of the gate with its output on pin 8 | tied to GND |
| 10 | K | output | K = NOT E | not connected |
| 11 | E | input | input of the gate with its output on pin 10 | tied to GND |
| 12 | L | output | L = NOT F | not connected |
| 13 | F | input | input of the gate with its output on pin 12 | tied to GND |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the ALU and its controls
add_sub.sv describes. · SVG · PNG6Schematic 2: connections between the chips
| Net | From | To | Wire |
|---|---|---|---|
| A3 | button A3, OUT | 40181 19 (A3) | M-F |
| A2 | button A2, OUT | 40181 21 (A2) | M-F |
| A1 | button A1, OUT | 40181 23 (A1) | M-F |
| A0 | button A0, OUT | 40181 2 (A0) | M-F, M-M |
| B3 | button B3, OUT | 40181 18 (B3) | M-F |
| B2 | button B2, OUT | 40181 20 (B2) | M-F |
| B1 | button B1, OUT | 40181 22 (B1) | M-F |
| B0 | button B0, OUT | 40181 1 (B0) | M-F, M-M |
| SUB | button SUB, OUT | 40181 5 (S1), 40181 4 (S2), 4069 1 (A) | M-F, M-M |
| SUB | 4069 2 (G) | 40181 6 (S0), 40181 3 (S3), 40181 7 (Cn) | M-M |
| F3 | 40181 13 (F3) | LED module, LED 1 | M-F, M-M |
| F2 | 40181 11 (F2) | LED module, LED 2 | M-F |
| F1 | 40181 10 (F1) | LED module, LED 3 | M-F |
| F0 | 40181 9 (F0) | LED module, LED 4 | M-F |
| C4 | 40181 16 (C4) | LED module, LED 5 | M-F, M-M |
| A=B | 40181 14 (A=B) | LED module, LED 6 | M-F, M-M |
| +5V | +5V | 4069 14 (VDD), 40181 24 (VDD), 4069 5 (C), 4069 3 (B) | |
| GND | GND | 4069 7 (VSS), 40181 12 (VSS), 4069 9 (D), 4069 11 (E), 4069 13 (F), 40181 8 (M) | |
| not connected | 4069 4 (H), 4069 6 (I), 4069 8 (J), 4069 10 (K), 4069 12 (L), 40181 15 (P), 40181 17 (G) |
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 4069 turned around, notch to the right, pin 1 in hole 16e; the 40181, notch to the left, pin 1 in hole 21h. 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. The 40181 is a wide chip, 0.6 inch between the rows of pins: the pins go into rows d and h, and the body covers rows e, f and g, so each pin has three free holes above it or two below it.
- 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 the 40181 VDD is pin 24 and VSS pin 12. 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 fixed inputs: M of the 40181 to GND, and the inputs of the five unused inverters to the rails.
- The control wires: SUB to S1, S2 and the inverter input; the inverter output to S0, S3 and Cn. Tick each wire as you insert it.
- With the power off, the modules: SUB, A3–A0 and B3–B0 above the board, the LED module below it.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 4 wires | ||||
| 1 | +5V | 16j (4069 pin 14, VDD) | bottom + rail, column 16 | |
| 2 | GND | 10a (4069 pin 7, VSS) | top − rail, column 10 | |
| 3 | +5V | 21a (40181 pin 24, VDD) | top + rail, column 21 | |
| 4 | GND | 32j (40181 pin 12, VSS) | bottom − rail, column 32 | |
| Fixed inputs: CMOS inputs must not float · 6 wires | ||||
| 5 | GND | 11j (4069 pin 9, D) | bottom − rail, column 11 | |
| 6 | +5V | 12a (4069 pin 5, C) | top + rail, column 12 | |
| 7 | GND | 13j (4069 pin 11, E) | bottom − rail, column 13 | |
| 8 | +5V | 14a (4069 pin 3, B) | top + rail, column 14 | |
| 9 | GND | 15j (4069 pin 13, F) | bottom − rail, column 15 | |
| 10 | GND | 28j (40181 pin 8, M) | bottom − rail, column 28 | |
| Signal wires · 10 wires | ||||
| 11 | SUB | 25i (40181 pin 5, S1) | 24i (40181 pin 4, S2) | |
| 12 | SUB | 24j (40181 pin 4, S2) | 16d (4069 pin 1, A) | |
| 13 | SUB | 15c (4069 pin 2, G) | 23j (40181 pin 3, S3) | |
| 14 | SUB | 23i (40181 pin 3, S3) | 26j (40181 pin 6, S0) | |
| 15 | SUB | 26i (40181 pin 6, S0) | 27i (40181 pin 7, Cn) | |
| 16 | C4 | 29b (40181 pin 16, C4) | 34f | |
| 17 | A=B | 31c (40181 pin 14, A=B) | 35f | |
| 18 | A0 | 22i (40181 pin 2, A0) | 20e | |
| 19 | B0 | 21i (40181 pin 1, B0) | 19e | |
| 20 | F3 | 32c (40181 pin 13, F3) | 33f | |
| Modules on male-female jumpers · 34 wires | ||||
| 21 | +5V | button SUB, + | top + rail, column 5 | |
| 22 | SUB | button SUB, OUT | 16a (4069 pin 1, A) | |
| 23 | GND | button SUB, − | top − rail, column 7 | |
| 24 | +5V | button A3, + | top + rail, column 11 | |
| 25 | A3 | button A3, OUT | 26a (40181 pin 19, A3) | |
| 26 | GND | button A3, − | top − rail, column 13 | |
| 27 | +5V | button A2, + | top + rail, column 16 | |
| 28 | A2 | button A2, OUT | 24a (40181 pin 21, A2) | |
| 29 | GND | button A2, − | top − rail, column 18 | |
| 30 | +5V | button A1, + | top + rail, column 20 | |
| 31 | A1 | button A1, OUT | 22a (40181 pin 23, A1) | |
| 32 | GND | button A1, − | top − rail, column 23 | |
| 33 | +5V | button A0, + | top + rail, column 26 | |
| 34 | A0 | button A0, OUT | 20a | |
| 35 | GND | button A0, − | top − rail, column 28 | |
| 36 | +5V | button B3, + | top + rail, column 32 | |
| 37 | B3 | button B3, OUT | 27a (40181 pin 18, B3) | |
| 38 | GND | button B3, − | top − rail, column 34 | |
| 39 | +5V | button B2, + | top + rail, column 37 | |
| 40 | B2 | button B2, OUT | 25a (40181 pin 20, B2) | |
| 41 | GND | button B2, − | top − rail, column 39 | |
| 42 | +5V | button B1, + | top + rail, column 42 | |
| 43 | B1 | button B1, OUT | 23a (40181 pin 22, B1) | |
| 44 | GND | button B1, − | top − rail, column 44 | |
| 45 | +5V | button B0, + | top + rail, column 47 | |
| 46 | B0 | button B0, OUT | 19a | |
| 47 | GND | button B0, − | top − rail, column 49 | |
| 48 | GND | LED module, G | bottom − rail, column 26 | |
| 49 | F3 | LED module, LED 1 | 33j | |
| 50 | F2 | LED module, LED 2 | 31j (40181 pin 11, F2) | |
| 51 | F1 | LED module, LED 3 | 30j (40181 pin 10, F1) | |
| 52 | F0 | LED module, LED 4 | 29j (40181 pin 9, F0) | |
| 53 | C4 | LED module, LED 5 | 34j | |
| 54 | A=B | LED module, LED 6 | 35j | |
Test it
- Hold A2 and A0 (5) and B1 and B0 (3): LEDs 1–4 show 1000, the number 8, and LED 5 is lit (no carry).
- Press SUB as well: 0010, the number 2.
- Swap the numbers, A = 3 and B = 5, with SUB: 1110, which is −2, and LED 5 lit, because A < B.
- Without SUB, A = 9 and B = 8: 0001 and LED 5 dark: 9 + 8 = 17 = 16 + 1.
- A = 7 and B = 8 without SUB: 1111 and LED 6 lit.
If it does not work: if a subtraction gives one less than it should, Cn does not get NOT SUB. If an addition gives one more, Cn is at GND. If the LEDs show something that is not a sum, such as A XOR B, M is not at GND. If only some bits are wrong, check the order of the A and B wires: on the 40181 they take turns.
8SystemVerilog: the generic design
add_sub.sv does what the wiring does: when sub is 1, it inverts b and adds 1
through the carry. It is synthesizable.
// Exercise BB10: a 4-bit adder and subtractor, as built from the 40181 ALU // with sub driving its function select and carry inputs. // // Generic version that matches schematic 1, not tied to any chip. With // sub = 0, f = a + b; with sub = 1, f = a - b, computed as a + ~b + 1, the // way the ALU does it. carry_n is the carry out of the 40181, active low: // in addition it is 0 when the sum does not fit into 4 bits; in // subtraction it is 0 when a >= b and 1 when a < b, that is, when f is // negative. all_ones is the A=B output of the 40181: 1 when f is 1111. // Synthesizable. module add_sub ( input logic [3:0] a, input logic [3:0] b, input logic sub, output logic [3:0] f, output logic carry_n, output logic all_ones ); wire [3:0] b_in = sub ? ~ b : b; wire [4:0] total = { 1'b0, a } + { 1'b0, b_in } + { 4'b0, sub }; assign f = total [3:0]; assign carry_n = ~ total [4]; assign all_ones = & f; 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.
The 40181 model describes all 32 functions with two signals per bit, as the chip does inside. It was checked against the function table of the datasheet for every A, B and carry.
// CD40181B: 4-bit arithmetic logic unit (ALU), the CMOS version of the // 74181. // // Ports in pin order, named as in the datasheet. The power pins, VSS (12) // and VDD (24), are left out. // // The model uses the active-high reading of the datasheet: A, B and F are // numbers with 1 = high. Then the carry in Cn and the carry out Cn+4 are // active low: Cn = 0 adds one more, Cn+4 = 0 means that the result has a // fifth bit. M = 1 selects one of 16 logic functions of A and B, bit by // bit; M = 0 one of 16 arithmetic functions, chosen by S3-S0. For // example, S = 1001 gives A plus B, and S = 0110 with Cn = 0 gives A minus // B. A=B is 1 when F3-F0 are all 1, which in subtraction with Cn = 1, // F = A minus B minus 1, means that A equals B. P and G, active low, say // whether the four bits propagate or generate a carry; a look-ahead carry // chip such as the 40182 uses them to join several ALUs. // // Inside, each bit makes two signals from A, B and S3-S0, here x and y. // The arithmetic result is x plus y plus the carry in; the logic result is // NOT (x XOR y). The model computes the same, without the gates of the // chip. Synthesizable. module cd40181 ( input logic b0, // Pin 1 input logic a0, // Pin 2 input logic s3, // Pin 3 input logic s2, // Pin 4 input logic s1, // Pin 5 input logic s0, // Pin 6 input logic cn, // Pin 7: carry in, 0 = add one more input logic m, // Pin 8: 1 = logic, 0 = arithmetic output logic f0, // Pin 9 output logic f1, // Pin 10 output logic f2, // Pin 11 // Pin 12: VSS output logic f3, // Pin 13 output logic a_eq_b, // Pin 14: 1 = F is 1111 output logic p, // Pin 15: carry propagate, active low output logic cn4, // Pin 16: carry out, 0 = a fifth bit output logic g, // Pin 17: carry generate, active low input logic b3, // Pin 18 input logic a3, // Pin 19 input logic b2, // Pin 20 input logic a2, // Pin 21 input logic b1, // Pin 22 input logic a1 // Pin 23 // Pin 24: VDD ); wire [3:0] a = { a3, a2, a1, a0 }; wire [3:0] b = { b3, b2, b1, b0 }; wire [3:0] x = a | (b & { 4 { s0 } }) | (~ b & { 4 { s1 } }); wire [3:0] y = (a & ~ b & { 4 { s2 } }) | (a & b & { 4 { s3 } }); wire [4:0] sum = { 1'b0, x } + { 1'b0, y } + { 4'b0, ~ cn }; wire [3:0] f = m ? ~ (x ^ y) : sum [3:0]; assign { f3, f2, f1, f0 } = f; assign a_eq_b = & f; assign cn4 = ~ sum [4]; assign p = ~ (& x); assign g = ~ (y [3] | x [3] & y [2] | x [3] & x [2] & y [1] | x [3] & x [2] & x [1] & y [0]); endmodule
// CD4069UB: hex inverter (six NOT gates). // // Ports in pin order, named as in the datasheet: inputs A-F, outputs G-L. // The power pins, VSS (7) and VDD (14), are left out. Synthesizable. module cd4069 ( input logic a, // Pin 1 output logic g, // Pin 2: g = ~ a input logic b, // Pin 3 output logic h, // Pin 4: h = ~ b input logic c, // Pin 5 output logic i, // Pin 6: i = ~ c // Pin 7: VSS output logic j, // Pin 8: j = ~ d input logic d, // Pin 9 output logic k, // Pin 10: k = ~ e input logic e, // Pin 11 output logic l, // Pin 12: l = ~ f input logic f // Pin 13 // Pin 14: VDD ); assign g = ~ a; assign h = ~ b; assign i = ~ c; assign j = ~ d; assign k = ~ e; assign l = ~ f; endmodule
10SystemVerilog: the chip-level design
add_sub_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as add_sub.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 BB10, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as add_sub.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_bb10.py, not this file. Synthesizable. module add_sub_chips ( input logic [3:0] a, input logic [3:0] b, input logic sub, output logic [3:0] f, output logic carry_n, output logic all_ones ); // Connections between the chips that are not ports wire nsub; // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; wire vdd = 1'b1; // Outputs that go nowhere wire unused_u2_h; wire unused_u2_i; wire unused_u2_j; wire unused_u2_k; wire unused_u2_l; wire unused_u1_p; wire unused_u1_g; //------------------------------------------------------------------------ // U2: CD4069 hex inverter (six NOT gates), pin 1 in column 10 cd4069 i_u2 ( .a ( sub ), // Pin 1 <- button SUB .g ( nsub ), // Pin 2 -> ~SUB .b ( vdd ), // Pin 3 <- +5V, unused input .h ( unused_u2_h ), // Pin 4, unused output .c ( vdd ), // Pin 5 <- +5V, unused input .i ( unused_u2_i ), // Pin 6, unused output .j ( unused_u2_j ), // Pin 8, unused output .d ( gnd ), // Pin 9 <- GND, unused input .k ( unused_u2_k ), // Pin 10, unused output .e ( gnd ), // Pin 11 <- GND, unused input .l ( unused_u2_l ), // Pin 12, unused output .f ( gnd ) // Pin 13 <- GND, unused input ); //------------------------------------------------------------------------ // U1: CD40181 4-bit arithmetic logic unit (ALU): 16 arithmetic and 16 logic functions, pin 1 in column 21 cd40181 i_u1 ( .b0 ( b [0] ), // Pin 1 <- button B0 .a0 ( a [0] ), // Pin 2 <- button A0 .s3 ( nsub ), // Pin 3 <- U2 pin 2 (G) .s2 ( sub ), // Pin 4 <- button SUB .s1 ( sub ), // Pin 5 <- button SUB .s0 ( nsub ), // Pin 6 <- U2 pin 2 (G) .cn ( nsub ), // Pin 7 <- U2 pin 2 (G) .m ( gnd ), // Pin 8 <- GND .f0 ( f [0] ), // Pin 9 -> F0 .f1 ( f [1] ), // Pin 10 -> F1 .f2 ( f [2] ), // Pin 11 -> F2 .f3 ( f [3] ), // Pin 13 -> F3 .a_eq_b ( all_ones ), // Pin 14 -> A=B .p ( unused_u1_p ), // Pin 15, unused output .cn4 ( carry_n ), // Pin 16 -> C4 .g ( unused_u1_g ), // Pin 17, unused output .b3 ( b [3] ), // Pin 18 <- button B3 .a3 ( a [3] ), // Pin 19 <- button A3 .b2 ( b [2] ), // Pin 20 <- button B2 .a2 ( a [2] ), // Pin 21 <- button A2 .b1 ( b [1] ), // Pin 22 <- button B1 .a1 ( a [1] ) // Pin 23 <- button A1 ); endmodule
11Testbench and simulation scripts
tb.sv tries all 512 combinations of A, B and SUB, first counting up, then in Gray code order, where one
input bit changes at a time. It compares both designs with integer arithmetic. The output below is shortened.
tb.sv165 lines · show the codefile
// Testbench for exercise BB10. // // Runs the generic design (add_sub.sv) and the chip-level design // (add_sub_chips.sv) side by side and compares both with // integer addition and subtraction. 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 512 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; logic sub; wire [3:0] f, f_chips; wire carry_n, carry_n_chips; wire all_ones, all_ones_chips; add_sub i_generic ( .a ( a ), .b ( b ), .sub ( sub ), .f ( f ), .carry_n ( carry_n ), .all_ones ( all_ones ) ); add_sub_chips i_chips ( .a ( a ), .b ( b ), .sub ( sub ), .f ( f_chips ), .carry_n ( carry_n_chips ), .all_ones ( all_ones_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: integer addition and subtraction. // Bits, from the left: f, carry_n, all_ones. function automatic logic [5:0] expected ( input logic [3:0] in_a, input logic [3:0] in_b, input logic in_sub ); int x = int' (in_a); int y = int' (in_b); int r = in_sub ? x - y + 16 : x + y; return { 4' (r), r < 16, 4' (r) == 4'hf }; endfunction wire [5:0] out = { f, carry_n, all_ones }; wire [5:0] out_chips = { f_chips, carry_n_chips, all_ones_chips }; wire [5:0] out_model = expected (a, b, sub); //------------------------------------------------------------------------ // 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 [511: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 | 512'b1 << { a, b, sub }; $display ("%5d %b %b %b %b %b %b %s %s", cycle + 1, a, b, sub, f, carry_n, all_ones, 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 SUB F Cn+4 A=B generic chips"); checking <= '1; for (int i = 0; i < 512; i ++) begin { a, b, sub } <= 9' (i); @ (posedge clk); end for (int i = 0; i < 512; i ++) begin { a, b, sub } <= 9' (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), 512); 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 BB10: 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 add_sub.sv add_sub_chips.sv cd4069.sv cd40181.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 BB10: 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 add_sub.sv add_sub_chips.sv cd4069.sv cd40181.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 BB10: 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 add_sub.sv add_sub_chips.sv cd4069.sv cd40181.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 SUB F Cn+4 A=B generic chips
1 0000 0000 0 0000 1 0 ok ok
2 0000 0000 1 0000 0 0 ok ok
3 0000 0001 0 0001 1 0 ok ok
4 0000 0001 1 1111 1 1 ok ok
5 0000 0010 0 0010 1 0 ok ok
6 0000 0010 1 1110 1 0 ok ok
7 0000 0011 0 0011 1 0 ok ok
8 0000 0011 1 1101 1 0 ok ok
9 0000 0100 0 0100 1 0 ok ok
10 0000 0100 1 1100 1 0 ok ok
11 0000 0101 0 0101 1 0 ok ok
12 0000 0101 1 1011 1 0 ok ok
13 0000 0110 0 0110 1 0 ok ok
14 0000 0110 1 1010 1 0 ok ok
15 0000 0111 0 0111 1 0 ok ok
16 0000 0111 1 1001 1 0 ok ok
17 0000 1000 0 1000 1 0 ok ok
18 0000 1000 1 1000 1 0 ok ok
19 0000 1001 0 1001 1 0 ok ok
20 0000 1001 1 0111 1 0 ok ok
21 0000 1010 0 1010 1 0 ok ok
22 0000 1010 1 0110 1 0 ok ok
23 0000 1011 0 1011 1 0 ok ok
24 0000 1011 1 0101 1 0 ok ok
25 0000 1100 0 1100 1 0 ok ok
26 0000 1100 1 0100 1 0 ok ok
27 0000 1101 0 1101 1 0 ok ok
28 0000 1101 1 0011 1 0 ok ok
29 0000 1110 0 1110 1 0 ok ok
30 0000 1110 1 0010 1 0 ok ok
31 0000 1111 0 1111 1 1 ok ok
32 0000 1111 1 0001 1 0 ok ok
33 0001 0000 0 0001 1 0 ok ok
34 0001 0000 1 0001 0 0 ok ok
35 0001 0001 0 0010 1 0 ok ok
36 0001 0001 1 0000 0 0 ok ok
37 0001 0010 0 0011 1 0 ok ok
38 0001 0010 1 1111 1 1 ok ok
39 0001 0011 0 0100 1 0 ok ok
40 0001 0011 1 1110 1 0 ok ok
41 0001 0100 0 0101 1 0 ok ok
42 0001 0100 1 1101 1 0 ok ok
43 0001 0101 0 0110 1 0 ok ok
44 0001 0101 1 1100 1 0 ok ok
45 0001 0110 0 0111 1 0 ok ok
46 0001 0110 1 1011 1 0 ok ok
47 0001 0111 0 1000 1 0 ok ok
48 0001 0111 1 1010 1 0 ok ok
49 0001 1000 0 1001 1 0 ok ok
50 0001 1000 1 1001 1 0 ok ok
51 0001 1001 0 1010 1 0 ok ok
52 0001 1001 1 1000 1 0 ok ok
53 0001 1010 0 1011 1 0 ok ok
54 0001 1010 1 0111 1 0 ok ok
55 0001 1011 0 1100 1 0 ok ok
56 0001 1011 1 0110 1 0 ok ok
57 0001 1100 0 1101 1 0 ok ok
58 0001 1100 1 0101 1 0 ok ok
59 0001 1101 0 1110 1 0 ok ok
60 0001 1101 1 0100 1 0 ok ok
61 0001 1110 0 1111 1 1 ok ok
62 0001 1110 1 0011 1 0 ok ok
63 0001 1111 0 0000 0 0 ok ok
64 0001 1111 1 0010 1 0 ok ok
65 0010 0000 0 0010 1 0 ok ok
66 0010 0000 1 0010 0 0 ok ok
67 0010 0001 0 0011 1 0 ok ok
68 0010 0001 1 0001 0 0 ok ok
69 0010 0010 0 0100 1 0 ok ok
70 0010 0010 1 0000 0 0 ok ok
71 0010 0011 0 0101 1 0 ok ok
72 0010 0011 1 1111 1 1 ok ok
73 0010 0100 0 0110 1 0 ok ok
74 0010 0100 1 1110 1 0 ok ok
75 0010 0101 0 0111 1 0 ok ok
76 0010 0101 1 1101 1 0 ok ok
77 0010 0110 0 1000 1 0 ok ok
78 0010 0110 1 1100 1 0 ok ok
79 0010 0111 0 1001 1 0 ok ok
80 0010 0111 1 1011 1 0 ok ok
81 0010 1000 0 1010 1 0 ok ok
82 0010 1000 1 1010 1 0 ok ok
83 0010 1001 0 1011 1 0 ok ok
84 0010 1001 1 1001 1 0 ok ok
85 0010 1010 0 1100 1 0 ok ok
86 0010 1010 1 1000 1 0 ok ok
87 0010 1011 0 1101 1 0 ok ok
88 0010 1011 1 0111 1 0 ok ok
89 0010 1100 0 1110 1 0 ok ok
90 0010 1100 1 0110 1 0 ok ok
91 0010 1101 0 1111 1 1 ok ok
92 0010 1101 1 0101 1 0 ok ok
93 0010 1110 0 0000 0 0 ok ok
94 0010 1110 1 0100 1 0 ok ok
95 0010 1111 0 0001 0 0 ok ok
96 0010 1111 1 0011 1 0 ok ok
97 0011 0000 0 0011 1 0 ok ok
98 0011 0000 1 0011 0 0 ok ok
99 0011 0001 0 0100 1 0 ok ok
100 0011 0001 1 0010 0 0 ok ok
101 0011 0010 0 0101 1 0 ok ok
102 0011 0010 1 0001 0 0 ok ok
103 0011 0011 0 0110 1 0 ok ok
104 0011 0011 1 0000 0 0 ok ok
105 0011 0100 0 0111 1 0 ok ok
106 0011 0100 1 1111 1 1 ok ok
107 0011 0101 0 1000 1 0 ok ok
108 0011 0101 1 1110 1 0 ok ok
109 0011 0110 0 1001 1 0 ok ok
110 0011 0110 1 1101 1 0 ok ok
111 0011 0111 0 1010 1 0 ok ok
112 0011 0111 1 1100 1 0 ok ok
113 0011 1000 0 1011 1 0 ok ok
114 0011 1000 1 1011 1 0 ok ok
115 0011 1001 0 1100 1 0 ok ok
116 0011 1001 1 1010 1 0 ok ok
117 0011 1010 0 1101 1 0 ok ok
118 0011 1010 1 1001 1 0 ok ok
119 0011 1011 0 1110 1 0 ok ok
120 0011 1011 1 1000 1 0 ok ok
121 0011 1100 0 1111 1 1 ok ok
122 0011 1100 1 0111 1 0 ok ok
123 0011 1101 0 0000 0 0 ok ok
124 0011 1101 1 0110 1 0 ok ok
125 0011 1110 0 0001 0 0 ok ok
126 0011 1110 1 0101 1 0 ok ok
127 0011 1111 0 0010 0 0 ok ok
128 0011 1111 1 0100 1 0 ok ok
129 0100 0000 0 0100 1 0 ok ok
130 0100 0000 1 0100 0 0 ok ok
131 0100 0001 0 0101 1 0 ok ok
132 0100 0001 1 0011 0 0 ok ok
133 0100 0010 0 0110 1 0 ok ok
134 0100 0010 1 0010 0 0 ok ok
135 0100 0011 0 0111 1 0 ok ok
136 0100 0011 1 0001 0 0 ok ok
137 0100 0100 0 1000 1 0 ok ok
138 0100 0100 1 0000 0 0 ok ok
139 0100 0101 0 1001 1 0 ok ok
140 0100 0101 1 1111 1 1 ok ok
141 0100 0110 0 1010 1 0 ok ok
142 0100 0110 1 1110 1 0 ok ok
143 0100 0111 0 1011 1 0 ok ok
144 0100 0111 1 1101 1 0 ok ok
145 0100 1000 0 1100 1 0 ok ok
146 0100 1000 1 1100 1 0 ok ok
147 0100 1001 0 1101 1 0 ok ok
148 0100 1001 1 1011 1 0 ok ok
149 0100 1010 0 1110 1 0 ok ok
150 0100 1010 1 1010 1 0 ok ok
151 0100 1011 0 1111 1 1 ok ok
152 0100 1011 1 1001 1 0 ok ok
153 0100 1100 0 0000 0 0 ok ok
154 0100 1100 1 1000 1 0 ok ok
155 0100 1101 0 0001 0 0 ok ok
156 0100 1101 1 0111 1 0 ok ok
157 0100 1110 0 0010 0 0 ok ok
158 0100 1110 1 0110 1 0 ok ok
159 0100 1111 0 0011 0 0 ok ok
160 0100 1111 1 0101 1 0 ok ok
161 0101 0000 0 0101 1 0 ok ok
162 0101 0000 1 0101 0 0 ok ok
163 0101 0001 0 0110 1 0 ok ok
164 0101 0001 1 0100 0 0 ok ok
165 0101 0010 0 0111 1 0 ok ok
166 0101 0010 1 0011 0 0 ok ok
167 0101 0011 0 1000 1 0 ok ok
168 0101 0011 1 0010 0 0 ok ok
169 0101 0100 0 1001 1 0 ok ok
170 0101 0100 1 0001 0 0 ok ok
171 0101 0101 0 1010 1 0 ok ok
172 0101 0101 1 0000 0 0 ok ok
173 0101 0110 0 1011 1 0 ok ok
174 0101 0110 1 1111 1 1 ok ok
175 0101 0111 0 1100 1 0 ok ok
176 0101 0111 1 1110 1 0 ok ok
177 0101 1000 0 1101 1 0 ok ok
178 0101 1000 1 1101 1 0 ok ok
179 0101 1001 0 1110 1 0 ok ok
180 0101 1001 1 1100 1 0 ok ok
181 0101 1010 0 1111 1 1 ok ok
182 0101 1010 1 1011 1 0 ok ok
183 0101 1011 0 0000 0 0 ok ok
184 0101 1011 1 1010 1 0 ok ok
185 0101 1100 0 0001 0 0 ok ok
186 0101 1100 1 1001 1 0 ok ok
187 0101 1101 0 0010 0 0 ok ok
188 0101 1101 1 1000 1 0 ok ok
189 0101 1110 0 0011 0 0 ok ok
190 0101 1110 1 0111 1 0 ok ok
191 0101 1111 0 0100 0 0 ok ok
192 0101 1111 1 0110 1 0 ok ok
193 0110 0000 0 0110 1 0 ok ok
194 0110 0000 1 0110 0 0 ok ok
195 0110 0001 0 0111 1 0 ok ok
196 0110 0001 1 0101 0 0 ok ok
197 0110 0010 0 1000 1 0 ok ok
198 0110 0010 1 0100 0 0 ok ok
199 0110 0011 0 1001 1 0 ok ok
200 0110 0011 1 0011 0 0 ok ok
201 0110 0100 0 1010 1 0 ok ok
202 0110 0100 1 0010 0 0 ok ok
203 0110 0101 0 1011 1 0 ok ok
204 0110 0101 1 0001 0 0 ok ok
205 0110 0110 0 1100 1 0 ok ok
206 0110 0110 1 0000 0 0 ok ok
207 0110 0111 0 1101 1 0 ok ok
208 0110 0111 1 1111 1 1 ok ok
209 0110 1000 0 1110 1 0 ok ok
210 0110 1000 1 1110 1 0 ok ok
211 0110 1001 0 1111 1 1 ok ok
212 0110 1001 1 1101 1 0 ok ok
213 0110 1010 0 0000 0 0 ok ok
214 0110 1010 1 1100 1 0 ok ok
215 0110 1011 0 0001 0 0 ok ok
216 0110 1011 1 1011 1 0 ok ok
217 0110 1100 0 0010 0 0 ok ok
218 0110 1100 1 1010 1 0 ok ok
219 0110 1101 0 0011 0 0 ok ok
220 0110 1101 1 1001 1 0 ok ok
221 0110 1110 0 0100 0 0 ok ok
222 0110 1110 1 1000 1 0 ok ok
223 0110 1111 0 0101 0 0 ok ok
224 0110 1111 1 0111 1 0 ok ok
225 0111 0000 0 0111 1 0 ok ok
226 0111 0000 1 0111 0 0 ok ok
227 0111 0001 0 1000 1 0 ok ok
228 0111 0001 1 0110 0 0 ok ok
229 0111 0010 0 1001 1 0 ok ok
230 0111 0010 1 0101 0 0 ok ok
231 0111 0011 0 1010 1 0 ok ok
232 0111 0011 1 0100 0 0 ok ok
233 0111 0100 0 1011 1 0 ok ok
234 0111 0100 1 0011 0 0 ok ok
235 0111 0101 0 1100 1 0 ok ok
236 0111 0101 1 0010 0 0 ok ok
237 0111 0110 0 1101 1 0 ok ok
238 0111 0110 1 0001 0 0 ok ok
239 0111 0111 0 1110 1 0 ok ok
240 0111 0111 1 0000 0 0 ok ok
241 0111 1000 0 1111 1 1 ok ok
242 0111 1000 1 1111 1 1 ok ok
243 0111 1001 0 0000 0 0 ok ok
244 0111 1001 1 1110 1 0 ok ok
245 0111 1010 0 0001 0 0 ok ok
246 0111 1010 1 1101 1 0 ok ok
247 0111 1011 0 0010 0 0 ok ok
248 0111 1011 1 1100 1 0 ok ok
249 0111 1100 0 0011 0 0 ok ok
250 0111 1100 1 1011 1 0 ok ok
251 0111 1101 0 0100 0 0 ok ok
252 0111 1101 1 1010 1 0 ok ok
253 0111 1110 0 0101 0 0 ok ok
254 0111 1110 1 1001 1 0 ok ok
255 0111 1111 0 0110 0 0 ok ok
256 0111 1111 1 1000 1 0 ok ok
257 1000 0000 0 1000 1 0 ok ok
258 1000 0000 1 1000 0 0 ok ok
259 1000 0001 0 1001 1 0 ok ok
260 1000 0001 1 0111 0 0 ok ok
261 1000 0010 0 1010 1 0 ok ok
262 1000 0010 1 0110 0 0 ok ok
263 1000 0011 0 1011 1 0 ok ok
264 1000 0011 1 0101 0 0 ok ok
265 1000 0100 0 1100 1 0 ok ok
266 1000 0100 1 0100 0 0 ok ok
267 1000 0101 0 1101 1 0 ok ok
268 1000 0101 1 0011 0 0 ok ok
269 1000 0110 0 1110 1 0 ok ok
270 1000 0110 1 0010 0 0 ok ok
271 1000 0111 0 1111 1 1 ok ok
272 1000 0111 1 0001 0 0 ok ok
273 1000 1000 0 0000 0 0 ok ok
274 1000 1000 1 0000 0 0 ok ok
275 1000 1001 0 0001 0 0 ok ok
276 1000 1001 1 1111 1 1 ok ok
277 1000 1010 0 0010 0 0 ok ok
278 1000 1010 1 1110 1 0 ok ok
279 1000 1011 0 0011 0 0 ok ok
280 1000 1011 1 1101 1 0 ok ok
281 1000 1100 0 0100 0 0 ok ok
282 1000 1100 1 1100 1 0 ok ok
283 1000 1101 0 0101 0 0 ok ok
284 1000 1101 1 1011 1 0 ok ok
285 1000 1110 0 0110 0 0 ok ok
286 1000 1110 1 1010 1 0 ok ok
287 1000 1111 0 0111 0 0 ok ok
288 1000 1111 1 1001 1 0 ok ok
289 1001 0000 0 1001 1 0 ok ok
290 1001 0000 1 1001 0 0 ok ok
291 1001 0001 0 1010 1 0 ok ok
292 1001 0001 1 1000 0 0 ok ok
293 1001 0010 0 1011 1 0 ok ok
294 1001 0010 1 0111 0 0 ok ok
295 1001 0011 0 1100 1 0 ok ok
296 1001 0011 1 0110 0 0 ok ok
297 1001 0100 0 1101 1 0 ok ok
298 1001 0100 1 0101 0 0 ok ok
299 1001 0101 0 1110 1 0 ok ok
300 1001 0101 1 0100 0 0 ok ok
301 1001 0110 0 1111 1 1 ok ok
302 1001 0110 1 0011 0 0 ok ok
303 1001 0111 0 0000 0 0 ok ok
304 1001 0111 1 0010 0 0 ok ok
305 1001 1000 0 0001 0 0 ok ok
306 1001 1000 1 0001 0 0 ok ok
307 1001 1001 0 0010 0 0 ok ok
308 1001 1001 1 0000 0 0 ok ok
309 1001 1010 0 0011 0 0 ok ok
310 1001 1010 1 1111 1 1 ok ok
311 1001 1011 0 0100 0 0 ok ok
312 1001 1011 1 1110 1 0 ok ok
313 1001 1100 0 0101 0 0 ok ok
314 1001 1100 1 1101 1 0 ok ok
315 1001 1101 0 0110 0 0 ok ok
316 1001 1101 1 1100 1 0 ok ok
317 1001 1110 0 0111 0 0 ok ok
318 1001 1110 1 1011 1 0 ok ok
319 1001 1111 0 1000 0 0 ok ok
320 1001 1111 1 1010 1 0 ok ok
321 1010 0000 0 1010 1 0 ok ok
322 1010 0000 1 1010 0 0 ok ok
323 1010 0001 0 1011 1 0 ok ok
324 1010 0001 1 1001 0 0 ok ok
325 1010 0010 0 1100 1 0 ok ok
326 1010 0010 1 1000 0 0 ok ok
327 1010 0011 0 1101 1 0 ok ok
328 1010 0011 1 0111 0 0 ok ok
329 1010 0100 0 1110 1 0 ok ok
330 1010 0100 1 0110 0 0 ok ok
331 1010 0101 0 1111 1 1 ok ok
332 1010 0101 1 0101 0 0 ok ok
333 1010 0110 0 0000 0 0 ok ok
334 1010 0110 1 0100 0 0 ok ok
335 1010 0111 0 0001 0 0 ok ok
336 1010 0111 1 0011 0 0 ok ok
337 1010 1000 0 0010 0 0 ok ok
338 1010 1000 1 0010 0 0 ok ok
339 1010 1001 0 0011 0 0 ok ok
340 1010 1001 1 0001 0 0 ok ok
341 1010 1010 0 0100 0 0 ok ok
342 1010 1010 1 0000 0 0 ok ok
343 1010 1011 0 0101 0 0 ok ok
344 1010 1011 1 1111 1 1 ok ok
345 1010 1100 0 0110 0 0 ok ok
346 1010 1100 1 1110 1 0 ok ok
347 1010 1101 0 0111 0 0 ok ok
348 1010 1101 1 1101 1 0 ok ok
349 1010 1110 0 1000 0 0 ok ok
350 1010 1110 1 1100 1 0 ok ok
351 1010 1111 0 1001 0 0 ok ok
352 1010 1111 1 1011 1 0 ok ok
353 1011 0000 0 1011 1 0 ok ok
354 1011 0000 1 1011 0 0 ok ok
355 1011 0001 0 1100 1 0 ok ok
356 1011 0001 1 1010 0 0 ok ok
357 1011 0010 0 1101 1 0 ok ok
358 1011 0010 1 1001 0 0 ok ok
359 1011 0011 0 1110 1 0 ok ok
360 1011 0011 1 1000 0 0 ok ok
361 1011 0100 0 1111 1 1 ok ok
362 1011 0100 1 0111 0 0 ok ok
363 1011 0101 0 0000 0 0 ok ok
364 1011 0101 1 0110 0 0 ok ok
365 1011 0110 0 0001 0 0 ok ok
366 1011 0110 1 0101 0 0 ok ok
367 1011 0111 0 0010 0 0 ok ok
368 1011 0111 1 0100 0 0 ok ok
369 1011 1000 0 0011 0 0 ok ok
370 1011 1000 1 0011 0 0 ok ok
371 1011 1001 0 0100 0 0 ok ok
372 1011 1001 1 0010 0 0 ok ok
373 1011 1010 0 0101 0 0 ok ok
374 1011 1010 1 0001 0 0 ok ok
375 1011 1011 0 0110 0 0 ok ok
376 1011 1011 1 0000 0 0 ok ok
377 1011 1100 0 0111 0 0 ok ok
378 1011 1100 1 1111 1 1 ok ok
379 1011 1101 0 1000 0 0 ok ok
380 1011 1101 1 1110 1 0 ok ok
381 1011 1110 0 1001 0 0 ok ok
382 1011 1110 1 1101 1 0 ok ok
383 1011 1111 0 1010 0 0 ok ok
384 1011 1111 1 1100 1 0 ok ok
385 1100 0000 0 1100 1 0 ok ok
386 1100 0000 1 1100 0 0 ok ok
387 1100 0001 0 1101 1 0 ok ok
388 1100 0001 1 1011 0 0 ok ok
389 1100 0010 0 1110 1 0 ok ok
390 1100 0010 1 1010 0 0 ok ok
391 1100 0011 0 1111 1 1 ok ok
392 1100 0011 1 1001 0 0 ok ok
393 1100 0100 0 0000 0 0 ok ok
394 1100 0100 1 1000 0 0 ok ok
395 1100 0101 0 0001 0 0 ok ok
396 1100 0101 1 0111 0 0 ok ok
397 1100 0110 0 0010 0 0 ok ok
398 1100 0110 1 0110 0 0 ok ok
399 1100 0111 0 0011 0 0 ok ok
400 1100 0111 1 0101 0 0 ok ok
401 1100 1000 0 0100 0 0 ok ok
402 1100 1000 1 0100 0 0 ok ok
403 1100 1001 0 0101 0 0 ok ok
404 1100 1001 1 0011 0 0 ok ok
405 1100 1010 0 0110 0 0 ok ok
406 1100 1010 1 0010 0 0 ok ok
407 1100 1011 0 0111 0 0 ok ok
408 1100 1011 1 0001 0 0 ok ok
409 1100 1100 0 1000 0 0 ok ok
410 1100 1100 1 0000 0 0 ok ok
411 1100 1101 0 1001 0 0 ok ok
412 1100 1101 1 1111 1 1 ok ok
413 1100 1110 0 1010 0 0 ok ok
414 1100 1110 1 1110 1 0 ok ok
415 1100 1111 0 1011 0 0 ok ok
416 1100 1111 1 1101 1 0 ok ok
417 1101 0000 0 1101 1 0 ok ok
418 1101 0000 1 1101 0 0 ok ok
419 1101 0001 0 1110 1 0 ok ok
420 1101 0001 1 1100 0 0 ok ok
421 1101 0010 0 1111 1 1 ok ok
422 1101 0010 1 1011 0 0 ok ok
423 1101 0011 0 0000 0 0 ok ok
424 1101 0011 1 1010 0 0 ok ok
425 1101 0100 0 0001 0 0 ok ok
426 1101 0100 1 1001 0 0 ok ok
427 1101 0101 0 0010 0 0 ok ok
428 1101 0101 1 1000 0 0 ok ok
429 1101 0110 0 0011 0 0 ok ok
430 1101 0110 1 0111 0 0 ok ok
431 1101 0111 0 0100 0 0 ok ok
432 1101 0111 1 0110 0 0 ok ok
433 1101 1000 0 0101 0 0 ok ok
434 1101 1000 1 0101 0 0 ok ok
435 1101 1001 0 0110 0 0 ok ok
436 1101 1001 1 0100 0 0 ok ok
437 1101 1010 0 0111 0 0 ok ok
438 1101 1010 1 0011 0 0 ok ok
439 1101 1011 0 1000 0 0 ok ok
440 1101 1011 1 0010 0 0 ok ok
441 1101 1100 0 1001 0 0 ok ok
442 1101 1100 1 0001 0 0 ok ok
443 1101 1101 0 1010 0 0 ok ok
444 1101 1101 1 0000 0 0 ok ok
445 1101 1110 0 1011 0 0 ok ok
446 1101 1110 1 1111 1 1 ok ok
447 1101 1111 0 1100 0 0 ok ok
448 1101 1111 1 1110 1 0 ok ok
449 1110 0000 0 1110 1 0 ok ok
450 1110 0000 1 1110 0 0 ok ok
451 1110 0001 0 1111 1 1 ok ok
452 1110 0001 1 1101 0 0 ok ok
453 1110 0010 0 0000 0 0 ok ok
454 1110 0010 1 1100 0 0 ok ok
455 1110 0011 0 0001 0 0 ok ok
456 1110 0011 1 1011 0 0 ok ok
457 1110 0100 0 0010 0 0 ok ok
458 1110 0100 1 1010 0 0 ok ok
459 1110 0101 0 0011 0 0 ok ok
460 1110 0101 1 1001 0 0 ok ok
461 1110 0110 0 0100 0 0 ok ok
462 1110 0110 1 1000 0 0 ok ok
463 1110 0111 0 0101 0 0 ok ok
464 1110 0111 1 0111 0 0 ok ok
465 1110 1000 0 0110 0 0 ok ok
466 1110 1000 1 0110 0 0 ok ok
467 1110 1001 0 0111 0 0 ok ok
468 1110 1001 1 0101 0 0 ok ok
469 1110 1010 0 1000 0 0 ok ok
470 1110 1010 1 0100 0 0 ok ok
471 1110 1011 0 1001 0 0 ok ok
472 1110 1011 1 0011 0 0 ok ok
473 1110 1100 0 1010 0 0 ok ok
474 1110 1100 1 0010 0 0 ok ok
475 1110 1101 0 1011 0 0 ok ok
476 1110 1101 1 0001 0 0 ok ok
477 1110 1110 0 1100 0 0 ok ok
478 1110 1110 1 0000 0 0 ok ok
479 1110 1111 0 1101 0 0 ok ok
480 1110 1111 1 1111 1 1 ok ok
481 1111 0000 0 1111 1 1 ok ok
482 1111 0000 1 1111 0 1 ok ok
483 1111 0001 0 0000 0 0 ok ok
484 1111 0001 1 1110 0 0 ok ok
485 1111 0010 0 0001 0 0 ok ok
486 1111 0010 1 1101 0 0 ok ok
487 1111 0011 0 0010 0 0 ok ok
488 1111 0011 1 1100 0 0 ok ok
489 1111 0100 0 0011 0 0 ok ok
490 1111 0100 1 1011 0 0 ok ok
491 1111 0101 0 0100 0 0 ok ok
492 1111 0101 1 1010 0 0 ok ok
493 1111 0110 0 0101 0 0 ok ok
494 1111 0110 1 1001 0 0 ok ok
495 1111 0111 0 0110 0 0 ok ok
496 1111 0111 1 1000 0 0 ok ok
497 1111 1000 0 0111 0 0 ok ok
498 1111 1000 1 0111 0 0 ok ok
499 1111 1001 0 1000 0 0 ok ok
500 1111 1001 1 0110 0 0 ok ok
501 1111 1010 0 1001 0 0 ok ok
502 1111 1010 1 0101 0 0 ok ok
503 1111 1011 0 1010 0 0 ok ok
504 1111 1011 1 0100 0 0 ok ok
505 1111 1100 0 1011 0 0 ok ok
506 1111 1100 1 0011 0 0 ok ok
507 1111 1101 0 1100 0 0 ok ok
508 1111 1101 1 0010 0 0 ok ok
509 1111 1110 0 1101 0 0 ok ok
510 1111 1110 1 0001 0 0 ok ok
511 1111 1111 0 1110 0 0 ok ok
512 1111 1111 1 0000 0 0 ok ok
513 0000 0000 0 0000 1 0 ok ok
514 0000 0000 1 0000 0 0 ok ok
515 0000 0001 1 1111 1 1 ok ok
516 0000 0001 0 0001 1 0 ok ok
517 0000 0011 0 0011 1 0 ok ok
518 0000 0011 1 1101 1 0 ok ok
519 0000 0010 1 1110 1 0 ok ok
520 0000 0010 0 0010 1 0 ok ok
521 0000 0110 0 0110 1 0 ok ok
522 0000 0110 1 1010 1 0 ok ok
523 0000 0111 1 1001 1 0 ok ok
524 0000 0111 0 0111 1 0 ok ok
525 0000 0101 0 0101 1 0 ok ok
526 0000 0101 1 1011 1 0 ok ok
527 0000 0100 1 1100 1 0 ok ok
528 0000 0100 0 0100 1 0 ok ok
529 0000 1100 0 1100 1 0 ok ok
530 0000 1100 1 0100 1 0 ok ok
531 0000 1101 1 0011 1 0 ok ok
532 0000 1101 0 1101 1 0 ok ok
533 0000 1111 0 1111 1 1 ok ok
534 0000 1111 1 0001 1 0 ok ok
535 0000 1110 1 0010 1 0 ok ok
536 0000 1110 0 1110 1 0 ok ok
537 0000 1010 0 1010 1 0 ok ok
538 0000 1010 1 0110 1 0 ok ok
539 0000 1011 1 0101 1 0 ok ok
540 0000 1011 0 1011 1 0 ok ok
541 0000 1001 0 1001 1 0 ok ok
542 0000 1001 1 0111 1 0 ok ok
543 0000 1000 1 1000 1 0 ok ok
544 0000 1000 0 1000 1 0 ok ok
545 0001 1000 0 1001 1 0 ok ok
546 0001 1000 1 1001 1 0 ok ok
547 0001 1001 1 1000 1 0 ok ok
548 0001 1001 0 1010 1 0 ok ok
549 0001 1011 0 1100 1 0 ok ok
550 0001 1011 1 0110 1 0 ok ok
551 0001 1010 1 0111 1 0 ok ok
552 0001 1010 0 1011 1 0 ok ok
553 0001 1110 0 1111 1 1 ok ok
554 0001 1110 1 0011 1 0 ok ok
555 0001 1111 1 0010 1 0 ok ok
556 0001 1111 0 0000 0 0 ok ok
557 0001 1101 0 1110 1 0 ok ok
558 0001 1101 1 0100 1 0 ok ok
559 0001 1100 1 0101 1 0 ok ok
560 0001 1100 0 1101 1 0 ok ok
561 0001 0100 0 0101 1 0 ok ok
562 0001 0100 1 1101 1 0 ok ok
563 0001 0101 1 1100 1 0 ok ok
564 0001 0101 0 0110 1 0 ok ok
565 0001 0111 0 1000 1 0 ok ok
566 0001 0111 1 1010 1 0 ok ok
567 0001 0110 1 1011 1 0 ok ok
568 0001 0110 0 0111 1 0 ok ok
569 0001 0010 0 0011 1 0 ok ok
570 0001 0010 1 1111 1 1 ok ok
571 0001 0011 1 1110 1 0 ok ok
572 0001 0011 0 0100 1 0 ok ok
573 0001 0001 0 0010 1 0 ok ok
574 0001 0001 1 0000 0 0 ok ok
575 0001 0000 1 0001 0 0 ok ok
576 0001 0000 0 0001 1 0 ok ok
577 0011 0000 0 0011 1 0 ok ok
578 0011 0000 1 0011 0 0 ok ok
579 0011 0001 1 0010 0 0 ok ok
580 0011 0001 0 0100 1 0 ok ok
581 0011 0011 0 0110 1 0 ok ok
582 0011 0011 1 0000 0 0 ok ok
583 0011 0010 1 0001 0 0 ok ok
584 0011 0010 0 0101 1 0 ok ok
585 0011 0110 0 1001 1 0 ok ok
586 0011 0110 1 1101 1 0 ok ok
587 0011 0111 1 1100 1 0 ok ok
588 0011 0111 0 1010 1 0 ok ok
589 0011 0101 0 1000 1 0 ok ok
590 0011 0101 1 1110 1 0 ok ok
591 0011 0100 1 1111 1 1 ok ok
592 0011 0100 0 0111 1 0 ok ok
593 0011 1100 0 1111 1 1 ok ok
594 0011 1100 1 0111 1 0 ok ok
595 0011 1101 1 0110 1 0 ok ok
596 0011 1101 0 0000 0 0 ok ok
597 0011 1111 0 0010 0 0 ok ok
598 0011 1111 1 0100 1 0 ok ok
599 0011 1110 1 0101 1 0 ok ok
600 0011 1110 0 0001 0 0 ok ok
601 0011 1010 0 1101 1 0 ok ok
602 0011 1010 1 1001 1 0 ok ok
603 0011 1011 1 1000 1 0 ok ok
604 0011 1011 0 1110 1 0 ok ok
605 0011 1001 0 1100 1 0 ok ok
606 0011 1001 1 1010 1 0 ok ok
607 0011 1000 1 1011 1 0 ok ok
608 0011 1000 0 1011 1 0 ok ok
609 0010 1000 0 1010 1 0 ok ok
610 0010 1000 1 1010 1 0 ok ok
611 0010 1001 1 1001 1 0 ok ok
612 0010 1001 0 1011 1 0 ok ok
613 0010 1011 0 1101 1 0 ok ok
614 0010 1011 1 0111 1 0 ok ok
615 0010 1010 1 1000 1 0 ok ok
616 0010 1010 0 1100 1 0 ok ok
617 0010 1110 0 0000 0 0 ok ok
618 0010 1110 1 0100 1 0 ok ok
619 0010 1111 1 0011 1 0 ok ok
620 0010 1111 0 0001 0 0 ok ok
621 0010 1101 0 1111 1 1 ok ok
622 0010 1101 1 0101 1 0 ok ok
623 0010 1100 1 0110 1 0 ok ok
624 0010 1100 0 1110 1 0 ok ok
625 0010 0100 0 0110 1 0 ok ok
626 0010 0100 1 1110 1 0 ok ok
627 0010 0101 1 1101 1 0 ok ok
628 0010 0101 0 0111 1 0 ok ok
629 0010 0111 0 1001 1 0 ok ok
630 0010 0111 1 1011 1 0 ok ok
631 0010 0110 1 1100 1 0 ok ok
632 0010 0110 0 1000 1 0 ok ok
633 0010 0010 0 0100 1 0 ok ok
634 0010 0010 1 0000 0 0 ok ok
635 0010 0011 1 1111 1 1 ok ok
636 0010 0011 0 0101 1 0 ok ok
637 0010 0001 0 0011 1 0 ok ok
638 0010 0001 1 0001 0 0 ok ok
639 0010 0000 1 0010 0 0 ok ok
640 0010 0000 0 0010 1 0 ok ok
641 0110 0000 0 0110 1 0 ok ok
642 0110 0000 1 0110 0 0 ok ok
643 0110 0001 1 0101 0 0 ok ok
644 0110 0001 0 0111 1 0 ok ok
645 0110 0011 0 1001 1 0 ok ok
646 0110 0011 1 0011 0 0 ok ok
647 0110 0010 1 0100 0 0 ok ok
648 0110 0010 0 1000 1 0 ok ok
649 0110 0110 0 1100 1 0 ok ok
650 0110 0110 1 0000 0 0 ok ok
651 0110 0111 1 1111 1 1 ok ok
652 0110 0111 0 1101 1 0 ok ok
653 0110 0101 0 1011 1 0 ok ok
654 0110 0101 1 0001 0 0 ok ok
655 0110 0100 1 0010 0 0 ok ok
656 0110 0100 0 1010 1 0 ok ok
657 0110 1100 0 0010 0 0 ok ok
658 0110 1100 1 1010 1 0 ok ok
659 0110 1101 1 1001 1 0 ok ok
660 0110 1101 0 0011 0 0 ok ok
661 0110 1111 0 0101 0 0 ok ok
662 0110 1111 1 0111 1 0 ok ok
663 0110 1110 1 1000 1 0 ok ok
664 0110 1110 0 0100 0 0 ok ok
665 0110 1010 0 0000 0 0 ok ok
666 0110 1010 1 1100 1 0 ok ok
667 0110 1011 1 1011 1 0 ok ok
668 0110 1011 0 0001 0 0 ok ok
669 0110 1001 0 1111 1 1 ok ok
670 0110 1001 1 1101 1 0 ok ok
671 0110 1000 1 1110 1 0 ok ok
672 0110 1000 0 1110 1 0 ok ok
673 0111 1000 0 1111 1 1 ok ok
674 0111 1000 1 1111 1 1 ok ok
675 0111 1001 1 1110 1 0 ok ok
676 0111 1001 0 0000 0 0 ok ok
677 0111 1011 0 0010 0 0 ok ok
678 0111 1011 1 1100 1 0 ok ok
679 0111 1010 1 1101 1 0 ok ok
680 0111 1010 0 0001 0 0 ok ok
681 0111 1110 0 0101 0 0 ok ok
682 0111 1110 1 1001 1 0 ok ok
683 0111 1111 1 1000 1 0 ok ok
684 0111 1111 0 0110 0 0 ok ok
685 0111 1101 0 0100 0 0 ok ok
686 0111 1101 1 1010 1 0 ok ok
687 0111 1100 1 1011 1 0 ok ok
688 0111 1100 0 0011 0 0 ok ok
689 0111 0100 0 1011 1 0 ok ok
690 0111 0100 1 0011 0 0 ok ok
691 0111 0101 1 0010 0 0 ok ok
692 0111 0101 0 1100 1 0 ok ok
693 0111 0111 0 1110 1 0 ok ok
694 0111 0111 1 0000 0 0 ok ok
695 0111 0110 1 0001 0 0 ok ok
696 0111 0110 0 1101 1 0 ok ok
697 0111 0010 0 1001 1 0 ok ok
698 0111 0010 1 0101 0 0 ok ok
699 0111 0011 1 0100 0 0 ok ok
700 0111 0011 0 1010 1 0 ok ok
701 0111 0001 0 1000 1 0 ok ok
702 0111 0001 1 0110 0 0 ok ok
703 0111 0000 1 0111 0 0 ok ok
704 0111 0000 0 0111 1 0 ok ok
705 0101 0000 0 0101 1 0 ok ok
706 0101 0000 1 0101 0 0 ok ok
707 0101 0001 1 0100 0 0 ok ok
708 0101 0001 0 0110 1 0 ok ok
709 0101 0011 0 1000 1 0 ok ok
710 0101 0011 1 0010 0 0 ok ok
711 0101 0010 1 0011 0 0 ok ok
712 0101 0010 0 0111 1 0 ok ok
713 0101 0110 0 1011 1 0 ok ok
714 0101 0110 1 1111 1 1 ok ok
715 0101 0111 1 1110 1 0 ok ok
716 0101 0111 0 1100 1 0 ok ok
717 0101 0101 0 1010 1 0 ok ok
718 0101 0101 1 0000 0 0 ok ok
719 0101 0100 1 0001 0 0 ok ok
720 0101 0100 0 1001 1 0 ok ok
721 0101 1100 0 0001 0 0 ok ok
722 0101 1100 1 1001 1 0 ok ok
723 0101 1101 1 1000 1 0 ok ok
724 0101 1101 0 0010 0 0 ok ok
725 0101 1111 0 0100 0 0 ok ok
726 0101 1111 1 0110 1 0 ok ok
727 0101 1110 1 0111 1 0 ok ok
728 0101 1110 0 0011 0 0 ok ok
729 0101 1010 0 1111 1 1 ok ok
730 0101 1010 1 1011 1 0 ok ok
731 0101 1011 1 1010 1 0 ok ok
732 0101 1011 0 0000 0 0 ok ok
733 0101 1001 0 1110 1 0 ok ok
734 0101 1001 1 1100 1 0 ok ok
735 0101 1000 1 1101 1 0 ok ok
736 0101 1000 0 1101 1 0 ok ok
737 0100 1000 0 1100 1 0 ok ok
738 0100 1000 1 1100 1 0 ok ok
739 0100 1001 1 1011 1 0 ok ok
740 0100 1001 0 1101 1 0 ok ok
741 0100 1011 0 1111 1 1 ok ok
742 0100 1011 1 1001 1 0 ok ok
743 0100 1010 1 1010 1 0 ok ok
744 0100 1010 0 1110 1 0 ok ok
745 0100 1110 0 0010 0 0 ok ok
746 0100 1110 1 0110 1 0 ok ok
747 0100 1111 1 0101 1 0 ok ok
748 0100 1111 0 0011 0 0 ok ok
749 0100 1101 0 0001 0 0 ok ok
750 0100 1101 1 0111 1 0 ok ok
751 0100 1100 1 1000 1 0 ok ok
752 0100 1100 0 0000 0 0 ok ok
753 0100 0100 0 1000 1 0 ok ok
754 0100 0100 1 0000 0 0 ok ok
755 0100 0101 1 1111 1 1 ok ok
756 0100 0101 0 1001 1 0 ok ok
757 0100 0111 0 1011 1 0 ok ok
758 0100 0111 1 1101 1 0 ok ok
759 0100 0110 1 1110 1 0 ok ok
760 0100 0110 0 1010 1 0 ok ok
761 0100 0010 0 0110 1 0 ok ok
762 0100 0010 1 0010 0 0 ok ok
763 0100 0011 1 0001 0 0 ok ok
764 0100 0011 0 0111 1 0 ok ok
765 0100 0001 0 0101 1 0 ok ok
766 0100 0001 1 0011 0 0 ok ok
767 0100 0000 1 0100 0 0 ok ok
768 0100 0000 0 0100 1 0 ok ok
769 1100 0000 0 1100 1 0 ok ok
770 1100 0000 1 1100 0 0 ok ok
771 1100 0001 1 1011 0 0 ok ok
772 1100 0001 0 1101 1 0 ok ok
773 1100 0011 0 1111 1 1 ok ok
774 1100 0011 1 1001 0 0 ok ok
775 1100 0010 1 1010 0 0 ok ok
776 1100 0010 0 1110 1 0 ok ok
777 1100 0110 0 0010 0 0 ok ok
778 1100 0110 1 0110 0 0 ok ok
779 1100 0111 1 0101 0 0 ok ok
780 1100 0111 0 0011 0 0 ok ok
781 1100 0101 0 0001 0 0 ok ok
782 1100 0101 1 0111 0 0 ok ok
783 1100 0100 1 1000 0 0 ok ok
784 1100 0100 0 0000 0 0 ok ok
785 1100 1100 0 1000 0 0 ok ok
786 1100 1100 1 0000 0 0 ok ok
787 1100 1101 1 1111 1 1 ok ok
788 1100 1101 0 1001 0 0 ok ok
789 1100 1111 0 1011 0 0 ok ok
790 1100 1111 1 1101 1 0 ok ok
791 1100 1110 1 1110 1 0 ok ok
792 1100 1110 0 1010 0 0 ok ok
793 1100 1010 0 0110 0 0 ok ok
794 1100 1010 1 0010 0 0 ok ok
795 1100 1011 1 0001 0 0 ok ok
796 1100 1011 0 0111 0 0 ok ok
797 1100 1001 0 0101 0 0 ok ok
798 1100 1001 1 0011 0 0 ok ok
799 1100 1000 1 0100 0 0 ok ok
800 1100 1000 0 0100 0 0 ok ok
801 1101 1000 0 0101 0 0 ok ok
802 1101 1000 1 0101 0 0 ok ok
803 1101 1001 1 0100 0 0 ok ok
804 1101 1001 0 0110 0 0 ok ok
805 1101 1011 0 1000 0 0 ok ok
806 1101 1011 1 0010 0 0 ok ok
807 1101 1010 1 0011 0 0 ok ok
808 1101 1010 0 0111 0 0 ok ok
809 1101 1110 0 1011 0 0 ok ok
810 1101 1110 1 1111 1 1 ok ok
811 1101 1111 1 1110 1 0 ok ok
812 1101 1111 0 1100 0 0 ok ok
813 1101 1101 0 1010 0 0 ok ok
814 1101 1101 1 0000 0 0 ok ok
815 1101 1100 1 0001 0 0 ok ok
816 1101 1100 0 1001 0 0 ok ok
817 1101 0100 0 0001 0 0 ok ok
818 1101 0100 1 1001 0 0 ok ok
819 1101 0101 1 1000 0 0 ok ok
820 1101 0101 0 0010 0 0 ok ok
821 1101 0111 0 0100 0 0 ok ok
822 1101 0111 1 0110 0 0 ok ok
823 1101 0110 1 0111 0 0 ok ok
824 1101 0110 0 0011 0 0 ok ok
825 1101 0010 0 1111 1 1 ok ok
826 1101 0010 1 1011 0 0 ok ok
827 1101 0011 1 1010 0 0 ok ok
828 1101 0011 0 0000 0 0 ok ok
829 1101 0001 0 1110 1 0 ok ok
830 1101 0001 1 1100 0 0 ok ok
831 1101 0000 1 1101 0 0 ok ok
832 1101 0000 0 1101 1 0 ok ok
833 1111 0000 0 1111 1 1 ok ok
834 1111 0000 1 1111 0 1 ok ok
835 1111 0001 1 1110 0 0 ok ok
836 1111 0001 0 0000 0 0 ok ok
837 1111 0011 0 0010 0 0 ok ok
838 1111 0011 1 1100 0 0 ok ok
839 1111 0010 1 1101 0 0 ok ok
840 1111 0010 0 0001 0 0 ok ok
841 1111 0110 0 0101 0 0 ok ok
842 1111 0110 1 1001 0 0 ok ok
843 1111 0111 1 1000 0 0 ok ok
844 1111 0111 0 0110 0 0 ok ok
845 1111 0101 0 0100 0 0 ok ok
846 1111 0101 1 1010 0 0 ok ok
847 1111 0100 1 1011 0 0 ok ok
848 1111 0100 0 0011 0 0 ok ok
849 1111 1100 0 1011 0 0 ok ok
850 1111 1100 1 0011 0 0 ok ok
851 1111 1101 1 0010 0 0 ok ok
852 1111 1101 0 1100 0 0 ok ok
853 1111 1111 0 1110 0 0 ok ok
854 1111 1111 1 0000 0 0 ok ok
855 1111 1110 1 0001 0 0 ok ok
856 1111 1110 0 1101 0 0 ok ok
857 1111 1010 0 1001 0 0 ok ok
858 1111 1010 1 0101 0 0 ok ok
859 1111 1011 1 0100 0 0 ok ok
860 1111 1011 0 1010 0 0 ok ok
861 1111 1001 0 1000 0 0 ok ok
862 1111 1001 1 0110 0 0 ok ok
863 1111 1000 1 0111 0 0 ok ok
864 1111 1000 0 0111 0 0 ok ok
865 1110 1000 0 0110 0 0 ok ok
866 1110 1000 1 0110 0 0 ok ok
867 1110 1001 1 0101 0 0 ok ok
868 1110 1001 0 0111 0 0 ok ok
869 1110 1011 0 1001 0 0 ok ok
870 1110 1011 1 0011 0 0 ok ok
871 1110 1010 1 0100 0 0 ok ok
872 1110 1010 0 1000 0 0 ok ok
873 1110 1110 0 1100 0 0 ok ok
874 1110 1110 1 0000 0 0 ok ok
875 1110 1111 1 1111 1 1 ok ok
876 1110 1111 0 1101 0 0 ok ok
877 1110 1101 0 1011 0 0 ok ok
878 1110 1101 1 0001 0 0 ok ok
879 1110 1100 1 0010 0 0 ok ok
880 1110 1100 0 1010 0 0 ok ok
881 1110 0100 0 0010 0 0 ok ok
882 1110 0100 1 1010 0 0 ok ok
883 1110 0101 1 1001 0 0 ok ok
884 1110 0101 0 0011 0 0 ok ok
885 1110 0111 0 0101 0 0 ok ok
886 1110 0111 1 0111 0 0 ok ok
887 1110 0110 1 1000 0 0 ok ok
888 1110 0110 0 0100 0 0 ok ok
889 1110 0010 0 0000 0 0 ok ok
890 1110 0010 1 1100 0 0 ok ok
891 1110 0011 1 1011 0 0 ok ok
892 1110 0011 0 0001 0 0 ok ok
893 1110 0001 0 1111 1 1 ok ok
894 1110 0001 1 1101 0 0 ok ok
895 1110 0000 1 1110 0 0 ok ok
896 1110 0000 0 1110 1 0 ok ok
897 1010 0000 0 1010 1 0 ok ok
898 1010 0000 1 1010 0 0 ok ok
899 1010 0001 1 1001 0 0 ok ok
900 1010 0001 0 1011 1 0 ok ok
901 1010 0011 0 1101 1 0 ok ok
902 1010 0011 1 0111 0 0 ok ok
903 1010 0010 1 1000 0 0 ok ok
904 1010 0010 0 1100 1 0 ok ok
905 1010 0110 0 0000 0 0 ok ok
906 1010 0110 1 0100 0 0 ok ok
907 1010 0111 1 0011 0 0 ok ok
908 1010 0111 0 0001 0 0 ok ok
909 1010 0101 0 1111 1 1 ok ok
910 1010 0101 1 0101 0 0 ok ok
911 1010 0100 1 0110 0 0 ok ok
912 1010 0100 0 1110 1 0 ok ok
913 1010 1100 0 0110 0 0 ok ok
914 1010 1100 1 1110 1 0 ok ok
915 1010 1101 1 1101 1 0 ok ok
916 1010 1101 0 0111 0 0 ok ok
917 1010 1111 0 1001 0 0 ok ok
918 1010 1111 1 1011 1 0 ok ok
919 1010 1110 1 1100 1 0 ok ok
920 1010 1110 0 1000 0 0 ok ok
921 1010 1010 0 0100 0 0 ok ok
922 1010 1010 1 0000 0 0 ok ok
923 1010 1011 1 1111 1 1 ok ok
924 1010 1011 0 0101 0 0 ok ok
925 1010 1001 0 0011 0 0 ok ok
926 1010 1001 1 0001 0 0 ok ok
927 1010 1000 1 0010 0 0 ok ok
928 1010 1000 0 0010 0 0 ok ok
929 1011 1000 0 0011 0 0 ok ok
930 1011 1000 1 0011 0 0 ok ok
931 1011 1001 1 0010 0 0 ok ok
932 1011 1001 0 0100 0 0 ok ok
933 1011 1011 0 0110 0 0 ok ok
934 1011 1011 1 0000 0 0 ok ok
935 1011 1010 1 0001 0 0 ok ok
936 1011 1010 0 0101 0 0 ok ok
937 1011 1110 0 1001 0 0 ok ok
938 1011 1110 1 1101 1 0 ok ok
939 1011 1111 1 1100 1 0 ok ok
940 1011 1111 0 1010 0 0 ok ok
941 1011 1101 0 1000 0 0 ok ok
942 1011 1101 1 1110 1 0 ok ok
943 1011 1100 1 1111 1 1 ok ok
944 1011 1100 0 0111 0 0 ok ok
945 1011 0100 0 1111 1 1 ok ok
946 1011 0100 1 0111 0 0 ok ok
947 1011 0101 1 0110 0 0 ok ok
948 1011 0101 0 0000 0 0 ok ok
949 1011 0111 0 0010 0 0 ok ok
950 1011 0111 1 0100 0 0 ok ok
951 1011 0110 1 0101 0 0 ok ok
952 1011 0110 0 0001 0 0 ok ok
953 1011 0010 0 1101 1 0 ok ok
954 1011 0010 1 1001 0 0 ok ok
955 1011 0011 1 1000 0 0 ok ok
956 1011 0011 0 1110 1 0 ok ok
957 1011 0001 0 1100 1 0 ok ok
958 1011 0001 1 1010 0 0 ok ok
959 1011 0000 1 1011 0 0 ok ok
960 1011 0000 0 1011 1 0 ok ok
961 1001 0000 0 1001 1 0 ok ok
962 1001 0000 1 1001 0 0 ok ok
963 1001 0001 1 1000 0 0 ok ok
964 1001 0001 0 1010 1 0 ok ok
965 1001 0011 0 1100 1 0 ok ok
966 1001 0011 1 0110 0 0 ok ok
967 1001 0010 1 0111 0 0 ok ok
968 1001 0010 0 1011 1 0 ok ok
969 1001 0110 0 1111 1 1 ok ok
970 1001 0110 1 0011 0 0 ok ok
971 1001 0111 1 0010 0 0 ok ok
972 1001 0111 0 0000 0 0 ok ok
973 1001 0101 0 1110 1 0 ok ok
974 1001 0101 1 0100 0 0 ok ok
975 1001 0100 1 0101 0 0 ok ok
976 1001 0100 0 1101 1 0 ok ok
977 1001 1100 0 0101 0 0 ok ok
978 1001 1100 1 1101 1 0 ok ok
979 1001 1101 1 1100 1 0 ok ok
980 1001 1101 0 0110 0 0 ok ok
981 1001 1111 0 1000 0 0 ok ok
982 1001 1111 1 1010 1 0 ok ok
983 1001 1110 1 1011 1 0 ok ok
984 1001 1110 0 0111 0 0 ok ok
985 1001 1010 0 0011 0 0 ok ok
986 1001 1010 1 1111 1 1 ok ok
987 1001 1011 1 1110 1 0 ok ok
988 1001 1011 0 0100 0 0 ok ok
989 1001 1001 0 0010 0 0 ok ok
990 1001 1001 1 0000 0 0 ok ok
991 1001 1000 1 0001 0 0 ok ok
992 1001 1000 0 0001 0 0 ok ok
993 1000 1000 0 0000 0 0 ok ok
994 1000 1000 1 0000 0 0 ok ok
995 1000 1001 1 1111 1 1 ok ok
996 1000 1001 0 0001 0 0 ok ok
997 1000 1011 0 0011 0 0 ok ok
998 1000 1011 1 1101 1 0 ok ok
999 1000 1010 1 1110 1 0 ok ok
1000 1000 1010 0 0010 0 0 ok ok
1001 1000 1110 0 0110 0 0 ok ok
1002 1000 1110 1 1010 1 0 ok ok
1003 1000 1111 1 1001 1 0 ok ok
1004 1000 1111 0 0111 0 0 ok ok
1005 1000 1101 0 0101 0 0 ok ok
1006 1000 1101 1 1011 1 0 ok ok
1007 1000 1100 1 1100 1 0 ok ok
1008 1000 1100 0 0100 0 0 ok ok
1009 1000 0100 0 1100 1 0 ok ok
1010 1000 0100 1 0100 0 0 ok ok
1011 1000 0101 1 0011 0 0 ok ok
1012 1000 0101 0 1101 1 0 ok ok
1013 1000 0111 0 1111 1 1 ok ok
1014 1000 0111 1 0001 0 0 ok ok
1015 1000 0110 1 0010 0 0 ok ok
1016 1000 0110 0 1110 1 0 ok ok
1017 1000 0010 0 1010 1 0 ok ok
1018 1000 0010 1 0110 0 0 ok ok
1019 1000 0011 1 0101 0 0 ok ok
1020 1000 0011 0 1011 1 0 ok ok
1021 1000 0001 0 1001 1 0 ok ok
1022 1000 0001 1 0111 0 0 ok ok
1023 1000 0000 1 1000 0 0 ok ok
1024 1000 0000 0 1000 1 0 ok ok
tb.sv:151: $finish called at 10245000 (1ps)
PASS: 2048 checks, no errors
To see that the testbench catches mistakes, break something and run it again: delete + { 4'b0, sub } in add_sub.sv, or connect Cn to SUB instead of NOT SUB in
add_sub_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 addadd_sub.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 and switches 7–4 are B; hold key 0 to subtract. LEDs 3–0 show F, LED 4 Cn+4 and LED 5 A=B. To
run the chip-level design instead, add add_sub_chips.sv, cd40181.sv and
cd4069.sv and change the module name in the instance.
bgm_lab/lab_top.sv100 lines · show the codefile
// Exercise BB10 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 add_sub.sv. For the chip-level design add // add_sub_chips.sv and cd4069.sv, cd40181.sv instead, and change the module // name below. // // Switches: sw [3:0] a, sw [7:4] b; key 0 is SUB: hold it to subtract. // The LEDs: led [3:0] f, led [4] carry_n, led [5] all_ones. 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] f; logic carry_n; logic all_ones; add_sub i_add_sub ( .a ( sw [3:0] ), .b ( sw [7:4] ), .sub ( key [0] ), .f ( f ), .carry_n ( carry_n ), .all_ones ( all_ones ) ); assign led = w_led' ({ all_ones, carry_n, f }); 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 addadd_sub.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 3–0 are A and keys 7–4 are B. No key is left for SUB, so two copies of the design run side by side, one adding and one subtracting. LEDs 3–0 show A + B and LEDs 7–4 A − B. The screen shows A and B in blue on the top row, then F, Cn+4 and A=B of the sum in green, and of the difference in orange.
bgm_hackathon/hackathon_top.sv109 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise BB10 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 add_sub.sv. // // Keys: key [3:0] a, key [7:4] b. All eight keys are taken, so there is no // key for SUB: two copies of the design run side by side, one adding and // one subtracting. The LEDs: led [3:0] a + b, led [7:4] a - b. The screen // shows three rows of lamps, bit 3 on the left: A and B in blue; then F, // Cn+4 and A=B of the sum in green; then the same for the difference in // orange. 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] f_add, f_sub; logic carry_n_add, carry_n_sub; logic all_ones_add, all_ones_sub; add_sub i_add ( .a ( a ), .b ( b ), .sub ( 1'b0 ), .f ( f_add ), .carry_n ( carry_n_add ), .all_ones ( all_ones_add ) ); add_sub i_sub ( .a ( a ), .b ( b ), .sub ( 1'b1 ), .f ( f_sub ), .carry_n ( carry_n_sub ), .all_ones ( all_ones_sub ) ); assign led = { f_sub, f_add }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // Square lamps on the 480 x 272 screen, 30 pixels wide, 40 apart, bit 3 // on the left. Top row: A, a gap, B. Middle row: F, a gap, Cn+4 and // A=B of the sum. Bottom row: the same for the difference. wire [8:0] row_in = { a, 1'b0, b }; wire [8:0] row_add = { f_add, 1'b0, carry_n_add, all_ones_add, 2'b0 }; wire [8:0] row_sub = { f_sub, 1'b0, carry_n_sub, all_ones_sub, 2'b0 }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 9; i ++) if (x >= 9' (60 + i * 40) & x < 9' (90 + i * 40)) begin if (y >= 50 & y < 80 & i != 4) blue = row_in [8 - i] ? 31 : 6; if (y >= 120 & y < 150 & i < 7 & i != 4) green = row_add [8 - i] ? 63 : 12; if (y >= 190 & y < 220 & i < 7 & i != 4) begin red = row_sub [8 - i] ? 31 : 6; green = row_sub [8 - i] ? 32 : 6; end 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.
DIP switches 0–3 are A and switches 4–7 are B. All eight are taken, so SUB comes from BIDIR pin 0: connect it to VCC to subtract. The display shows F as a hexadecimal digit, and its dot is Cn+4. F, Cn+4 and A=B also go to BIDIR pins 2–7.
tiny_tapeout/project.v78 lines · show the codefile
/* * Exercise BB10 on Tiny Tapeout: a 4-bit adder and subtractor, as built * from the 40181 ALU. The 7-segment display of the demo board shows the * result as a hexadecimal digit. * * Add add_sub.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-3 are A and switches 4-7 are B. All eight are taken, // so SUB comes from BIDIR pin 0: connect it to VCC to subtract. The // circuit has no clock and no reset. wire [3:0] f; wire carry_n, all_ones; add_sub i_add_sub ( .a ( ui_in [3:0] ), .b ( ui_in [7:4] ), .sub ( uio_in [0] ), .f ( f ), .carry_n ( carry_n ), .all_ones ( all_ones ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. F as a hexadecimal digit: 0-9, then A, b, C, // d, E, F. The dot is Cn+4: dark when the sum has a fifth bit, lit in // a subtraction when A < B. reg [6:0] gfedcba; always @(*) case (f) 4'h0: gfedcba = 7'b0111111; 4'h1: gfedcba = 7'b0000110; 4'h2: gfedcba = 7'b1011011; 4'h3: gfedcba = 7'b1001111; 4'h4: gfedcba = 7'b1100110; 4'h5: gfedcba = 7'b1101101; 4'h6: gfedcba = 7'b1111101; 4'h7: gfedcba = 7'b0000111; 4'h8: gfedcba = 7'b1111111; 4'h9: gfedcba = 7'b1101111; 4'ha: gfedcba = 7'b1110111; 4'hb: gfedcba = 7'b1111100; 4'hc: gfedcba = 7'b0111001; 4'hd: gfedcba = 7'b1011110; 4'he: gfedcba = 7'b1111001; 4'hf: gfedcba = 7'b1110001; endcase assign uo_out = { carry_n, gfedcba }; // F, Cn+4 and A=B on BIDIR pins 2-7; pin 0 is the SUB input assign uio_out = { f, carry_n, all_ones, 2'b0 }; assign uio_oe = 8'b1111_1100; // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, uio_in [7:1], 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>_add_sub
source_files:
- "project.v"
- "add_sub.sv"
15Going further
Tasks for students
- A real comparator. Connect Cn to +5V instead of NOT SUB. Now subtraction gives A − B − 1, A=B lights exactly when A = B, and Cn+4 tells A > B from A ≤ B. Compare with the 4063 of exercise BB3.
- Logic. Move M to +5V. With SUB released, S = 1001: what function of A and B do the LEDs show? With SUB pressed?
- All functions. Replace the SUB wiring by four wires for S3–S0 and one for M, and fill in the table of the datasheet yourself: A plus 1, A minus 1, A plus A (a shift to the left), …
- Eight bits. Two 40181s make an 8-bit ALU: Cn+4 of the low one goes to Cn of the high one.
- In SystemVerilog: write an ALU with an opcode input that chooses among add, subtract, AND, OR and XOR, as in a processor, and add a zero flag and a negative flag.
Files of this exercise
- add_sub.sv: the generic design (section 8)
- cd4069.sv and cd40181.sv: chip models (section 9)
- add_sub_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
Sources
The exercise is in the Building blocks list of “An improved way of teaching school kids digital electronics”, in lesson 1.6 of the Habr article about the electronics course, which calls the chip rare, and in picture 11 of “Teaching children electronics is due for a major redesign”, which shows the ALU demo Svetlana Mosyagina built: one button switches it between addition and subtraction, as here. The chips are described in the Intersil CD40181BMS and Texas Instruments CD4069UB datasheets.