Exercise B1 · basics · logic gates
AND, OR, XOR and NOT gates
Two buttons, four chips and one row of LEDs. Press A, B, both or neither, and the LEDs show the result of an AND, an OR, an XOR and two NOT gates at once: each press is one row of the truth table.
1What it is and why it matters
What it does. Button A and button B each give a 0 or a 1: 0 while the button is up, 1 while it is pressed. Both signals go to one gate of each of four chips. The 8-LED module shows the two inputs on LEDs 1 and 2 and the five results on LEDs 3–7: A AND B, A OR B, A XOR B, NOT A and NOT B. This is the first exercise of the Basics list in “An improved way of teaching school kids digital electronics”: “Demo 4081 (AND), 4071 (OR), 4069 (NOT), 4070 (XOR)”; lesson 1.1 of the Habr article uses the same chips.
How it works. A gate computes one output from its inputs, and only from its present inputs: it has no memory and no clock. Its whole behaviour fits in a truth table with one row for each combination of the inputs; two inputs give four rows. The LEDs on this board show all five tables side by side.
Where it is used. Everywhere. Every digital chip, from a calculator to a graphics processor, is built from gates and flip-flops. An adder is XOR and AND gates (exercise B9); a multiplexer picks one of several inputs with AND and OR; a comparator checks two numbers bit by bit with XOR; the next-state logic of every state machine is gates. When a synthesis tool turns SystemVerilog into a circuit, it produces a network of gates like these, only millions of them.
Why learn it.
- The operators
&,|,^and~of SystemVerilog are exactly these gates. After this exercise each operator has a chip and an LED behind it. - Reading a truth table and a datasheet is the first skill of digital design, and these four chips are the simplest datasheets there are.
- You meet a rule of CMOS chips that matters in every later exercise: unused inputs must be connected.
- Three of the chips have the same pinout. Swap a 4081 for a 4071 or a 4070 and the same wires make a different function: the pins are the interface, the function is inside.
2The key elements: four kinds of gate
AND
| A | B | Y = A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Y = 1 only when A and B are both 1. In SystemVerilog: a & b.
OR
| A | B | Y = A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Y = 1 when A or B or both are 1. In SystemVerilog: a | b.
XOR, exclusive OR
| A | B | Y = A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Y = 1 when A and B are different. In SystemVerilog: a ^ b.
NOT, inverter
| A | Y = NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
Y is the opposite of A. The small circle means NOT. In SystemVerilog: ~ a.
All five results for each row
This is what LEDs 1–7 show, one row per combination of the buttons. The bar over a letter, as in A, is the usual way to write NOT A.
| A | B | A&B | A|B | A^B | ~A | ~B |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Two things to notice. XOR is OR without its last row: it is 0 when both inputs are 1. And NOT A AND NOT B is 1
only in the first row, where A OR B is 0: that is one of De Morgan's laws, ~ (a | b) == ~ a & ~ b,
which exercise B2 builds with these chips.
Waveforms
tb.sv in Icarus Verilog. The testbench sets A and B right after
a rising edge of its clock (dotted lines) and checks the outputs at the next edge. The outputs change together with
the inputs: gates have no clock. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4081 | quad 2-input AND: A AND B (one gate used) | 1 | $0.55 | Elexp (elexp.com) |
| CD4071 | quad 2-input OR: A OR B (one gate used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4070 | quad 2-input XOR: A XOR B (one gate used) | 1 | $0.45 | Elexp (elexp.com) |
| CD4069 | hex inverter: NOT A and NOT B (two of six used) | 1 | $0.30 | Elexp (elexp.com) |
| Button modules | A and B; two modules from the pack of five | 2 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows A, B and the five results | 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 | 36 wires on the board (or pre-cut wires) | 36 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 14 leads to the buttons and the LED module | 14 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; all of these parts are in the Basics kit. The board wires are male-male Dupont jumpers from the Basics kit; pre-cut wires lie flatter and match the drawing better.
4The chips, pin by pin
All four 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, and it can also make the chip draw extra current. That is why every unused gate input here is wired to +5V or GND, and why there are more tie-off wires than signal wires. Outputs of unused gates stay open. The drawings show each chip from above, the way it sits on the breadboard: notch on the left, pin 1 at the bottom left, pins numbered counter-clockwise. Blue arrows are inputs, orange arrows are outputs.
4081: quad 2-input AND
Pin 3 = pin 1 AND pin 2 makes A AND B. The 4081, 4071 and 4070 have the same pinout: gate J has inputs on pins 1 and 2 and its output on pin 3, gate K on pins 5, 6 and 4, gate L on 8, 9 and 10, gate M on 12, 13 and 11. Only the function inside differs.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | A |
| 2 | B | input | input of the gate with its output on pin 3 | B |
| 3 | J | output | J = A AND B | A&B |
| 4 | K | output | K = C AND D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 9 | F | input | input of the gate with its output on pin 10 | tied to +5V |
| 10 | L | output | L = E AND F | not connected |
| 11 | M | output | M = G AND H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4071: quad 2-input OR
Pin 3 = pin 1 OR pin 2 makes A OR B. The other three gates are not used; their six inputs go to the rails.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | A |
| 2 | B | input | input of the gate with its output on pin 3 | B |
| 3 | J | output | J = A OR B | A|B |
| 4 | K | output | K = C OR D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 9 | F | input | input of the gate with its output on pin 10 | tied to +5V |
| 10 | L | output | L = E OR F | not connected |
| 11 | M | output | M = G OR H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4070: quad 2-input XOR
Pin 3 = pin 1 XOR pin 2 makes A XOR B.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | A |
| 2 | B | input | input of the gate with its output on pin 3 | B |
| 3 | J | output | J = A XOR B | A^B |
| 4 | K | output | K = C XOR D | not connected |
| 5 | C | input | input of the gate with its output on pin 4 | tied to GND |
| 6 | D | input | input of the gate with its output on pin 4 | tied to GND |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 9 | F | input | input of the gate with its output on pin 10 | tied to +5V |
| 10 | L | output | L = E XOR F | not connected |
| 11 | M | output | M = G XOR H | not connected |
| 12 | G | input | input of the gate with its output on pin 11 | tied to +5V |
| 13 | H | input | input of the gate with its output on pin 11 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4069: hex inverter
Pin 2 = NOT pin 1 makes NOT A, pin 4 = NOT pin 3 makes NOT B. The 4069 has a different pinout from the other three: an inverter has only one input. The four unused inverters have their inputs on pins 5, 9, 11 and 13.
Polarity. On all four chips 1 means +5 V and 0 means 0 V, and an LED lights for 1. The buttons give 1 while pressed.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 2 | A |
| 2 | G | output | G = NOT A | A |
| 3 | B | input | input of the gate with its output on pin 4 | B |
| 4 | H | output | H = NOT B | B |
| 5 | C | input | input of the gate with its output on pin 6 | tied to GND |
| 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 +5V |
| 10 | K | output | K = NOT E | not connected |
| 11 | E | input | input of the gate with its output on pin 10 | tied to +5V |
| 12 | L | output | L = NOT F | not connected |
| 13 | F | input | input of the gate with its output on pin 12 | tied to +5V |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the gates
basic_gates.sv describes. A
dot means a connection; crossing lines without a dot are not connected. · SVG · PNGFive gates read the same two signals. The A line feeds the AND, OR and XOR gates and one NOT gate; the B line feeds the same three gates and the other NOT gate. On the breadboard each line is one net: the button, a wire from chip to chip and a lead to the LED module.
6Schematic 2: connections between the chips
Every connection of the circuit, net by net. basic_gates_chips.sv in section 10 is written from the
same list.
| Net | From | To | Wire |
|---|---|---|---|
| A | button A, OUT | 4081 1 (A), 4071 1 (A), 4070 1 (A), 4069 1 (A), LED module, LED 1 | M-F, M-M |
| B | button B, OUT | 4081 2 (B), 4071 2 (B), 4070 2 (B), 4069 3 (B), LED module, LED 2 | M-F, M-M |
| A&B | 4081 3 (J) | LED module, LED 3 | M-F |
| A|B | 4071 3 (J) | LED module, LED 4 | M-F |
| A^B | 4070 3 (J) | LED module, LED 5 | M-F |
| A | 4069 2 (G) | LED module, LED 6 | M-F |
| B | 4069 4 (H) | LED module, LED 7 | M-F |
| +5V | +5V | 4081 14 (VDD), 4071 14 (VDD), 4070 14 (VDD), 4069 14 (VDD), 4081 13 (H), 4081 12 (G), 4081 9 (F), 4081 8 (E), 4071 13 (H), 4071 12 (G), 4071 9 (F), 4071 8 (E), 4070 13 (H), 4070 12 (G), 4070 9 (F), 4070 8 (E), 4069 13 (F), 4069 11 (E), 4069 9 (D) | |
| GND | GND | 4081 7 (VSS), 4071 7 (VSS), 4070 7 (VSS), 4069 7 (VSS), 4081 5 (C), 4081 6 (D), 4071 5 (C), 4071 6 (D), 4070 5 (C), 4070 6 (D), 4069 5 (C) | |
| not connected | 4081 4 (K), 4081 10 (L), 4081 11 (M), 4071 4 (K), 4071 10 (L), 4071 11 (M), 4070 4 (K), 4070 10 (L), 4070 11 (M), 4069 6 (I), 4069 8 (J), 4069 10 (K), 4069 12 (L) |
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, notch to the left: the 4081 with pin 1 in hole 10f, the 4071 in 19f, the 4070 in 28f, the 4069 in 37f. Read the numbers on the chips: the 4081, 4071 and 4070 look the same.
- Power: for each chip a red wire from pin 14 to the top + rail and a black wire from pin 7 to the bottom − rail.
- The tie-offs: every unused input to the nearest rail, top pins to +, bottom pins to −. It is the longest part of the job; tick each wire in the checklist.
- The six signal wires: line A from pin 1 of the 4081 through pin 1 of the 4071 and the 4070 to pin 1 of the 4069, line B from pin 2 to pin 2 to pin 2 to pin 3 of the 4069.
- With the power off, the modules: the buttons and the LED module below the board. Connect the − of each button and G of the LED module first.
- Turn the power on.
Wiring checklist
| # | Net | From | To | |
|---|---|---|---|---|
| Power (red to +, black to −) · 8 wires | ||||
| 1 | +5V | 10a (4081 pin 14, VDD) | top + rail, column 10 | |
| 2 | GND | 16j (4081 pin 7, VSS) | bottom − rail, column 16 | |
| 3 | +5V | 19a (4071 pin 14, VDD) | top + rail, column 19 | |
| 4 | GND | 25j (4071 pin 7, VSS) | bottom − rail, column 25 | |
| 5 | +5V | 28a (4070 pin 14, VDD) | top + rail, column 28 | |
| 6 | GND | 34j (4070 pin 7, VSS) | bottom − rail, column 34 | |
| 7 | +5V | 37a (4069 pin 14, VDD) | top + rail, column 37 | |
| 8 | GND | 43j (4069 pin 7, VSS) | bottom − rail, column 43 | |
| Fixed inputs: CMOS inputs must not float · 22 wires | ||||
| 9 | +5V | 11a (4081 pin 13, H) | top + rail, column 11 | |
| 10 | +5V | 12a (4081 pin 12, G) | top + rail, column 12 | |
| 11 | GND | 14j (4081 pin 5, C) | bottom − rail, column 14 | |
| 12 | +5V | 15a (4081 pin 9, F) | top + rail, column 15 | |
| 13 | GND | 15j (4081 pin 6, D) | bottom − rail, column 15 | |
| 14 | +5V | 16a (4081 pin 8, E) | top + rail, column 16 | |
| 15 | +5V | 20a (4071 pin 13, H) | top + rail, column 20 | |
| 16 | +5V | 21a (4071 pin 12, G) | top + rail, column 21 | |
| 17 | GND | 23j (4071 pin 5, C) | bottom − rail, column 23 | |
| 18 | +5V | 24a (4071 pin 9, F) | top + rail, column 24 | |
| 19 | GND | 24j (4071 pin 6, D) | bottom − rail, column 24 | |
| 20 | +5V | 25a (4071 pin 8, E) | top + rail, column 25 | |
| 21 | +5V | 29a (4070 pin 13, H) | top + rail, column 29 | |
| 22 | +5V | 30a (4070 pin 12, G) | top + rail, column 30 | |
| 23 | GND | 32j (4070 pin 5, C) | bottom − rail, column 32 | |
| 24 | +5V | 33a (4070 pin 9, F) | top + rail, column 33 | |
| 25 | GND | 33j (4070 pin 6, D) | bottom − rail, column 33 | |
| 26 | +5V | 34a (4070 pin 8, E) | top + rail, column 34 | |
| 27 | +5V | 38a (4069 pin 13, F) | top + rail, column 38 | |
| 28 | +5V | 40a (4069 pin 11, E) | top + rail, column 40 | |
| 29 | GND | 41j (4069 pin 5, C) | bottom − rail, column 41 | |
| 30 | +5V | 42a (4069 pin 9, D) | top + rail, column 42 | |
| Signal wires · 6 wires | ||||
| 31 | A | 10g (4081 pin 1, A) | 19g (4071 pin 1, A) | |
| 32 | A | 19h (4071 pin 1, A) | 28g (4070 pin 1, A) | |
| 33 | A | 28h (4070 pin 1, A) | 37g (4069 pin 1, A) | |
| 34 | B | 11g (4081 pin 2, B) | 20g (4071 pin 2, B) | |
| 35 | B | 20h (4071 pin 2, B) | 29g (4070 pin 2, B) | |
| 36 | B | 29h (4070 pin 2, B) | 39g (4069 pin 3, B) | |
| Modules on male-female jumpers · 14 wires | ||||
| 37 | +5V | button A, + | bottom + rail, column 6 | |
| 38 | A | button A, OUT | 10j (4081 pin 1, A) | |
| 39 | GND | button A, − | bottom − rail, column 8 | |
| 40 | +5V | button B, + | bottom + rail, column 12 | |
| 41 | B | button B, OUT | 11j (4081 pin 2, B) | |
| 42 | GND | button B, − | bottom − rail, column 13 | |
| 43 | GND | LED module, G | bottom − rail, column 27 | |
| 44 | A | LED module, LED 1 | 28j (4070 pin 1, A) | |
| 45 | B | LED module, LED 2 | 29j (4070 pin 2, B) | |
| 46 | A&B | LED module, LED 3 | 12j (4081 pin 3, J) | |
| 47 | A|B | LED module, LED 4 | 21j (4071 pin 3, J) | |
| 48 | A^B | LED module, LED 5 | 30j (4070 pin 3, J) | |
| 49 | A | LED module, LED 6 | 38j (4069 pin 2, G) | |
| 50 | B | LED module, LED 7 | 40j (4069 pin 4, H) | |
Test it
- With no button pressed: LEDs 6 and 7 (NOT A, NOT B) are lit, all others dark.
- Press A: LED 1 lights, LED 6 goes dark, and the OR and XOR LEDs (4 and 5) light.
- Press both: AND (LED 3) and OR light, XOR goes dark. That is the row where XOR differs from OR.
- Write down the four rows and compare them with the table in section 2.
If it does not work: an LED that never lights usually means a lead in the wrong hole; an LED that is always on, an input of its gate tied to +5V by mistake. If several LEDs flicker when you touch the wires, an input is floating: look for a missing tie-off. If a chip gets warm, turn the power off at once: a chip inserted the wrong way round or a short between + and − are the usual causes.
8SystemVerilog: the generic design
basic_gates.sv describes schematic 1: five assign statements, one per gate. It is
synthesizable: an FPGA tool turns it into a few lookup tables, an ASIC flow into five standard cells.
// Exercise B1: the basic gates AND, OR, XOR and NOT. // // Generic version that matches schematic 1: five gates, not tied to any // chip. Synthesizable. module basic_gates ( input logic a, input logic b, output logic a_and_b, // 1 when both inputs are 1 output logic a_or_b, // 1 when at least one input is 1 output logic a_xor_b, // 1 when the inputs differ output logic not_a, // 1 when a is 0 output logic not_b // 1 when b is 0 ); assign a_and_b = a & b; assign a_or_b = a | b; assign a_xor_b = a ^ b; assign not_a = ~ a; assign not_b = ~ b; endmodule
An assign is not a step of a program: it is a wire that always carries the value of its
expression, like the output pin of a gate.
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. All four models are synthesizable. The models leave out the power pins: in SystemVerilog a wire simply has the value 0 or 1.
// CD4081B: quad 2-input AND gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. // // The 4081, 4071 and 4070 have the same pinout: only the function of the // gates differs. module cd4081 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a & b output logic k, // Pin 4: k = c & d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e & f output logic m, // Pin 11: m = g & h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a & b; assign k = c & d; assign l = e & f; assign m = g & h; endmodule
// CD4071B: quad 2-input OR gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. module cd4071 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a | b output logic k, // Pin 4: k = c | d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e | f output logic m, // Pin 11: m = g | h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a | b; assign k = c | d; assign l = e | f; assign m = g | h; endmodule
// CD4070B: quad 2-input exclusive-OR (XOR) gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out. Synthesizable. // // The 4070, 4071 and 4081 have the same pinout: only the function of the // gates differs. module cd4070 ( input logic a, // Pin 1 input logic b, // Pin 2 output logic j, // Pin 3: j = a ^ b output logic k, // Pin 4: k = c ^ d input logic c, // Pin 5 input logic d, // Pin 6 // Pin 7: VSS input logic e, // Pin 8 input logic f, // Pin 9 output logic l, // Pin 10: l = e ^ f output logic m, // Pin 11: m = g ^ h input logic g, // Pin 12 input logic h // Pin 13 // Pin 14: VDD ); assign j = a ^ b; assign k = c ^ d; assign l = e ^ f; assign m = g ^ h; endmodule
// 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
basic_gates_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect the
chips, with the same ports as basic_gates.sv. Unused inputs are tied to vdd or
gnd as on the breadboard, and unused outputs go to wires named unused_…. The file is
generated from the breadboard wire list after a pin-level check of that list, so the code, the drawings and the
checklist cannot disagree.
// Exercise B1, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as basic_gates.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_b1.py, not this file. Synthesizable. module basic_gates_chips ( input logic a, input logic b, output logic a_and_b, output logic a_or_b, output logic a_xor_b, output logic not_a, output logic not_b ); // The power rails of the breadboard, for tied-off inputs wire gnd = 1'b0; wire vdd = 1'b1; // Outputs that go nowhere wire unused_u1_k; wire unused_u1_l; wire unused_u1_m; wire unused_u2_k; wire unused_u2_l; wire unused_u2_m; wire unused_u3_k; wire unused_u3_l; wire unused_u3_m; wire unused_u4_i; wire unused_u4_j; wire unused_u4_k; wire unused_u4_l; //------------------------------------------------------------------------ // U1: CD4081 quad 2-input AND, pin 1 in column 10 cd4081 i_u1 ( .a ( a ), // Pin 1 <- button A .b ( b ), // Pin 2 <- button B .j ( a_and_b ), // Pin 3 -> A&B .k ( unused_u1_k ), // Pin 4, unused output .c ( gnd ), // Pin 5 <- GND, unused input .d ( gnd ), // Pin 6 <- GND, unused input .e ( vdd ), // Pin 8 <- +5V, unused input .f ( vdd ), // Pin 9 <- +5V, unused input .l ( unused_u1_l ), // Pin 10, unused output .m ( unused_u1_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U2: CD4071 quad 2-input OR, pin 1 in column 19 cd4071 i_u2 ( .a ( a ), // Pin 1 <- button A .b ( b ), // Pin 2 <- button B .j ( a_or_b ), // Pin 3 -> A|B .k ( unused_u2_k ), // Pin 4, unused output .c ( gnd ), // Pin 5 <- GND, unused input .d ( gnd ), // Pin 6 <- GND, unused input .e ( vdd ), // Pin 8 <- +5V, unused input .f ( vdd ), // Pin 9 <- +5V, unused input .l ( unused_u2_l ), // Pin 10, unused output .m ( unused_u2_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U3: CD4070 quad 2-input XOR, pin 1 in column 28 cd4070 i_u3 ( .a ( a ), // Pin 1 <- button A .b ( b ), // Pin 2 <- button B .j ( a_xor_b ), // Pin 3 -> A^B .k ( unused_u3_k ), // Pin 4, unused output .c ( gnd ), // Pin 5 <- GND, unused input .d ( gnd ), // Pin 6 <- GND, unused input .e ( vdd ), // Pin 8 <- +5V, unused input .f ( vdd ), // Pin 9 <- +5V, unused input .l ( unused_u3_l ), // Pin 10, unused output .m ( unused_u3_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U4: CD4069 hex inverter (six NOT gates), pin 1 in column 37 cd4069 i_u4 ( .a ( a ), // Pin 1 <- button A .g ( not_a ), // Pin 2 -> ~A .b ( b ), // Pin 3 <- button B .h ( not_b ), // Pin 4 -> ~B .c ( gnd ), // Pin 5 <- GND, unused input .i ( unused_u4_i ), // Pin 6, unused output .j ( unused_u4_j ), // Pin 8, unused output .d ( vdd ), // Pin 9 <- +5V, unused input .k ( unused_u4_k ), // Pin 10, unused output .e ( vdd ), // Pin 11 <- +5V, unused input .l ( unused_u4_l ), // Pin 12, unused output .f ( vdd ) // Pin 13 <- +5V, unused input ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with the truth tables, written out row by
row. The gates have 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 outputs at the next rising edge. It goes through the four rows twice,
in two different orders, and prints PASS only if both designs match every time and every row was
seen.
tb.sv154 lines · show the codefile
// Testbench for exercise B1. // // Runs the generic design (basic_gates.sv) and the chip-level design // (basic_gates_chips.sv) side by side and compares both with the truth // tables of the gates. Works with Icarus Verilog, Verilator (--binary) // and Questa. // // The gates have 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 A and B after one edge and checks the outputs at the next. // 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 a, b; wire a_and_b, a_and_b_chips; wire a_or_b, a_or_b_chips; wire a_xor_b, a_xor_b_chips; wire not_a, not_a_chips; wire not_b, not_b_chips; basic_gates i_generic ( .a ( a ), .b ( b ), .a_and_b ( a_and_b ), .a_or_b ( a_or_b ), .a_xor_b ( a_xor_b ), .not_a ( not_a ), .not_b ( not_b ) ); basic_gates_chips i_chips ( .a ( a ), .b ( b ), .a_and_b ( a_and_b_chips ), .a_or_b ( a_or_b_chips ), .a_xor_b ( a_xor_b_chips ), .not_a ( not_a_chips ), .not_b ( not_b_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the truth tables, written out row by row. // Bits: AND, OR, XOR, NOT A, NOT B. function automatic logic [4:0] expected (input logic in_a, input logic in_b); case ({ in_a, in_b }) 2'b00: return 5'b000_11; 2'b01: return 5'b011_10; 2'b10: return 5'b011_01; 2'b11: return 5'b110_00; default: return 5'bxxx_xx; endcase endfunction wire [4:0] out = { a_and_b, a_or_b, a_xor_b, not_a, not_b }; wire [4:0] out_chips = { a_and_b_chips, a_or_b_chips, a_xor_b_chips, not_a_chips, not_b_chips }; //------------------------------------------------------------------------ // Checking at every rising clock edge: A and B 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 [3:0] rows_seen = '0; // Which rows of the truth table were checked always @ (posedge clk) if (checking & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (out === expected (a, b) ? 0 : 1) + (out_chips === expected (a, b) ? 0 : 1); rows_seen <= rows_seen | 4'b1 << { a, b }; $display ("%5d %b %b %b %b %b %b %b %s %s", cycle + 1, a, b, a_and_b, a_or_b, a_xor_b, not_a, not_b, out === expected (a, b) ? "ok " : "WRONG ", out_chips === expected (a, b) ? "ok " : "WRONG"); end //------------------------------------------------------------------------ // Driving A and B: every row of the truth table, twice, in two orders wire [15:0] order = 16'b00_01_10_11_11_10_00_01; // { A, B } for each step 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 AND OR XOR NOT A NOT B generic chips"); checking <= '1; for (int i = 0; i < 8; i ++) begin { a, b } <= order [15 - 2 * i -: 2]; @ (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 == 4'b1111) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, truth table rows seen %b", n_errors, n_checks, rows_seen); 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 B1: 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 basic_gates.sv basic_gates_chips.sv cd4081.sv cd4071.sv cd4070.sv cd4069.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 B1: 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 basic_gates.sv basic_gates_chips.sv cd4081.sv cd4071.sv cd4070.sv cd4069.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 B1: 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 basic_gates.sv basic_gates_chips.sv cd4081.sv cd4071.sv cd4070.sv cd4069.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 AND OR XOR NOT A NOT B generic chips
1 0 0 0 0 0 1 1 ok ok
2 0 1 0 1 1 1 0 ok ok
3 1 0 0 1 1 0 1 ok ok
4 1 1 1 1 0 0 0 ok ok
5 1 1 1 1 0 0 0 ok ok
6 1 0 0 1 1 0 1 ok ok
7 0 0 0 0 0 1 1 ok ok
8 0 1 0 1 1 1 0 ok ok
tb.sv:141: $finish called at 85000 (1ps)
PASS: 16 checks, no errors
To see that the testbench catches mistakes, break something and run it again: change a ^ b to
a | b in basic_gates.sv, or swap the connections of two outputs in
basic_gates_chips.sv. The script reports FAIL and the trace marks the rows
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, reset, keys, LEDs, a 7-segment display. The first BGM lab, 1_01_and_or_not_xor_de_morgan,
is about the same gates.
- Copy
labs/1_basics/1_01_and_or_not_xor_de_morganto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addbasic_gates.sv. The BGM scripts compile every.svfile of the directory excepttb.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash; the first run asks which board you have.
Keys 0 and 1 are A and B; the LEDs show A AND B, A OR B, A XOR B, NOT A and NOT B. To run the chip-level design
instead, add basic_gates_chips.sv and the four chip models and change the module name in the
instance.
bgm_lab/lab_top.sv99 lines · show the codefile
// Exercise B1 on an FPGA board with the Basics-Graphics-Music (BGM) labs: // https://github.com/yuri-panchul/basics-graphics-music // // Copy labs/1_basics/1_01_and_or_not_xor_de_morgan to a new directory, // replace its lab_top.sv with this file and add basic_gates.sv. For the // chip-level design add basic_gates_chips.sv and cd4081.sv, cd4071.sv, // cd4070.sv, cd4069.sv instead, and change the module name below. // // Keys 0 and 1 are the buttons A and B. The LEDs show the results: // led [0] A AND B, led [1] A OR B, led [2] A XOR B, led [3] NOT A, // led [4] NOT B. 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 a_and_b, a_or_b, a_xor_b, not_a, not_b; basic_gates i_basic_gates ( .a ( key [0] ), .b ( key [1] ), .a_and_b ( a_and_b ), .a_or_b ( a_or_b ), .a_xor_b ( a_xor_b ), .not_a ( not_a ), .not_b ( not_b ) ); assign led = w_led' ({ not_b, not_a, a_xor_b, a_or_b, a_and_b }); 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 a directory such as
hackathon/problems/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addbasic_gates.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 0 and 1 are A and B. The screen shows seven square lamps in a row, A and B in blue and the five results in green, like the LED module on the breadboard.
bgm_hackathon/hackathon_top.sv85 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise B1 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 basic_gates.sv. // // Keys 0 and 1 are A and B. The LEDs show the results: led [0] A AND B, // led [1] A OR B, led [2] A XOR B, led [3] NOT A, led [4] NOT B. // The screen shows seven lamps in a row: A, B and the five results. 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 a = key [0]; wire b = key [1]; logic a_and_b, a_or_b, a_xor_b, not_a, not_b; basic_gates i_basic_gates ( .a ( a ), .b ( b ), .a_and_b ( a_and_b ), .a_or_b ( a_or_b ), .a_xor_b ( a_xor_b ), .not_a ( not_a ), .not_b ( not_b ) ); assign led = { 3'b0, not_b, not_a, a_xor_b, a_or_b, a_and_b }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // Seven square lamps on the 480 x 272 screen, 40 pixels wide, 24 apart: // A and B in blue, the results in green. Bright when 1, dim when 0. wire [6:0] lamp = { not_b, not_a, a_xor_b, a_or_b, a_and_b, b, a }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 7; i ++) if (x >= 9' (30 + i * 64) & x < 9' (70 + i * 64) & y >= 116 & y < 156) begin if (i < 2) 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.
Switches 0 and 1 are A and B. The display has no row of LEDs, so each result lights one segment: a (top) is A
AND B, b (top right) A OR B, c (bottom right) A XOR B, d (bottom) NOT A, e (bottom left) NOT B. The gates need no
clock: clk and rst_n are listed as unused.
tiny_tapeout/project.v52 lines · show the codefile
/* * Exercise B1 on Tiny Tapeout: the basic gates, with the DIP switches as * inputs and the 7-segment display of the demo board as five lamps. * * Add basic_gates.sv to source_files in info.yaml next to project.v. * Rename the module to tt_um_<your GitHub name>_<project name> and use the * same name as top_module in info.yaml. */ `default_nettype none module tt_um_example ( input wire [7:0] ui_in, // DIP switches, INPUT Pmod, RP2350 GPIO17-24 output wire [7:0] uo_out, // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40 input wire [7:0] uio_in, // BIDIR Pmod, RP2350 GPIO25-32 output wire [7:0] uio_out, // (same pins) output wire [7:0] uio_oe, // 1 = your design drives the pin input wire ena, // set by the on-chip project mux, not a board pin input wire clk, // RP2350 GPIO16 input wire rst_n // RP2350 GPIO14 ); // Switch 0 is A, switch 1 is B wire a_and_b, a_or_b, a_xor_b, not_a, not_b; basic_gates i_basic_gates ( .a ( ui_in [0] ), .b ( ui_in [1] ), .a_and_b ( a_and_b ), .a_or_b ( a_or_b ), .a_xor_b ( a_xor_b ), .not_a ( not_a ), .not_b ( not_b ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. Each result lights one segment: // a (top) A AND B, b (top right) A OR B, c (bottom right) A XOR B, // d (bottom) NOT A, e (bottom left) NOT B. assign uo_out = { 3'b0, not_b, not_a, a_xor_b, a_or_b, a_and_b }; assign uio_out = 8'b0; assign uio_oe = 8'b0; // The gates need no clock or reset. // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, ui_in [7:2], uio_in, 1'b0 }; endmodule
In info.yaml list both files and use the module name as the top module:
project:
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_basic_gates
source_files:
- "project.v"
- "basic_gates.sv"
15Going further
Tasks for students
- Swap the chips. Turn the power off and swap the 4081 and the 4071. Which LEDs change their meaning? Why does the circuit still work?
- XOR from other gates. Build A XOR B from AND, OR and NOT only: (A OR B) AND NOT (A AND B). Use the free gates of the 4081, 4071 and 4069 and compare the result with the 4070 on two LEDs.
- De Morgan's laws (exercise B2): show that NOT (A AND B) equals NOT A OR NOT B, and NOT (A OR B) equals NOT A AND NOT B, on two LEDs each.
- A gate from its datasheet (exercise B3): take a 4001 (NOR) or a 4011 (NAND), find its pinout, describe the function in words and write the truth table.
- In SystemVerilog: add an output
a_nand_btobasic_gates.svand its expected values to the testbench. The testbench fails until both are right.
Files of this exercise
- basic_gates.sv: the generic design (section 8)
- cd4081.sv, cd4071.sv, cd4070.sv, cd4069.sv: chip models (section 9)
- basic_gates_chips.sv: the chip-level design (section 10)
- tb.sv and 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 three schematics, the pinouts and the waveforms;
the Russian versions end in
_ru
Sources
The exercise is the first of the Basics list in “An improved way of teaching school kids digital electronics using venerable small-scale integration ICs”; lesson 1.1 of the Habr article about the electronics course describes the class with these chips, 5 V LEDs and buttons with built-in pull-down resistors. The chips are described in the Texas Instruments datasheets for the CD4081B, CD4071B, CD4070B and CD4069UB.