Exercise B3c · basics · logic gates
Explore a gate chip: the dual 4-input family
Four chips with two 4-input gates each, all with one pinout. Buttons A, B, C and D drive one gate of each, and the LEDs show the inputs and NOR, NAND, OR and AND side by side: all eight LEDs of the module are in use.
- B3a: 4001 NOR, 4011 NAND, 4077 XNOR
- B3b: 4023 NAND, 4025 NOR, 4073 AND, 4075 OR
- B3c: 4002 NOR, 4012 NAND, 4072 OR, 4082 AND
- B3d: 4068 AND/NAND, 4078 OR/NOR
1What it is and why it matters
What it does. Buttons A, B, C and D drive one gate of each chip of the family: 4002 (NOR), 4012 (NAND), 4072 (OR), 4082 (AND). The LED module shows the 4 inputs on LEDs 1–4 and the 4 outputs on LEDs 5–8, so each press of the buttons is one row of 4 truth tables at once. The exercise comes from lesson 1.1 of the Habr article: each student gets one chip and its datasheet, describes the function in words and writes the truth table. This page puts the whole family on one board; a student with one chip builds only its part, with the same wires.
How it works. Chips of one family have the same pinout: the dual 4-input gates: two gates of four inputs each. Gate J has its inputs on pins 2–5 and its output on pin 1, gate K its inputs on pins 9–12 and its output on pin 13. Pins 6 and 8 are not connected inside: they need no wire. Only the gate inside differs. The datasheet says which gate, and a few presses on the board confirm it.
Where it is used. Every digital circuit is made of such gates. Wide AND and OR gates recognise one combination of many signals: an address decoder, the “all bits zero” flag of an ALU, the state of a state machine. Inverting gates, NAND and NOR, are the simplest in CMOS, and libraries of standard cells for chip design are full of them.
Why learn it.
- Reading a datasheet is the first skill of a hardware engineer. A gate datasheet is the simplest one there is: a pinout, a function and a table of voltages and delays.
- Describing the function in words (“1 only when all inputs are 0”) is the first step to writing it in a hardware description language.
- Chips with the same pinout can trade places on the board: the wiring stays, the function changes.
2The key elements: the gates of the family
NOR
NAND
OR
AND
A small circle on the output, a bubble, means NOT: NAND is NOT AND, NOR is NOT OR, XNOR is NOT XOR.
The functions in words
| Chip | Gate | In words | In SystemVerilog |
|---|---|---|---|
| 4002 | NOR | 1 only when all inputs are 0 | ~| { a, b, c, d } |
| 4012 | NAND | 0 only when all inputs are 1 | ~& { a, b, c, d } |
| 4072 | OR | 0 only when all inputs are 0 | | { a, b, c, d } |
| 4082 | AND | 1 only when all inputs are 1 | & { a, b, c, d } |
The truth tables
Every row of the board: the inputs and all 4 outputs.
| A | B | C | D | NOR | NAND | OR | AND |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
Waveforms
tb.sv in Icarus Verilog. The testbench sets the inputs right after a
rising edge of its clock (dotted lines) and checks the outputs at the next edge. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4072 | dual 4-input OR (one gate used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4002 | dual 4-input NOR (one gate used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4012 | dual 4-input NAND (one gate used) | 1 | $0.30 | Elexp (elexp.com) |
| CD4082 | dual 4-input AND (one gate used) | 1 | $0.35 | Elexp (elexp.com) |
| Button modules | A, B, C, D; 4 from the pack of five | 4 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows the inputs and every output | 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 | 21 leads to the buttons and the LED module | 21 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; these chips are in the Comprehensive kit. A class can share the chips: each student needs only one.
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.
4002: dual 4-input NOR
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | J | output | J = NOT (A OR B OR C OR D) | NOR |
| 2 | A | input | input of the gate with its output on pin 1 | C |
| 3 | B | input | input of the gate with its output on pin 1 | A |
| 4 | C | input | input of the gate with its output on pin 1 | B |
| 5 | D | input | input of the gate with its output on pin 1 | D |
| 6 | NC | — | not connected inside the chip | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | NC | — | not connected inside the chip | not connected |
| 9 | E | input | input of the gate with its output on pin 13 | tied to +5V |
| 10 | F | input | input of the gate with its output on pin 13 | tied to +5V |
| 11 | G | input | input of the gate with its output on pin 13 | tied to +5V |
| 12 | H | input | input of the gate with its output on pin 13 | tied to +5V |
| 13 | K | output | K = NOT (E OR F OR G OR H) | not connected |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4012: dual 4-input NAND
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | J | output | J = NOT (A AND B AND C AND D) | NAND |
| 2 | A | input | input of the gate with its output on pin 1 | A |
| 3 | B | input | input of the gate with its output on pin 1 | D |
| 4 | C | input | input of the gate with its output on pin 1 | B |
| 5 | D | input | input of the gate with its output on pin 1 | C |
| 6 | NC | — | not connected inside the chip | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | NC | — | not connected inside the chip | not connected |
| 9 | E | input | input of the gate with its output on pin 13 | tied to +5V |
| 10 | F | input | input of the gate with its output on pin 13 | tied to +5V |
| 11 | G | input | input of the gate with its output on pin 13 | tied to +5V |
| 12 | H | input | input of the gate with its output on pin 13 | tied to +5V |
| 13 | K | output | K = NOT (E AND F AND G AND H) | not connected |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4072: dual 4-input OR
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | J | output | J = A OR B OR C OR D | not connected |
| 2 | A | input | input of the gate with its output on pin 1 | tied to +5V |
| 3 | B | input | input of the gate with its output on pin 1 | tied to +5V |
| 4 | C | input | input of the gate with its output on pin 1 | tied to +5V |
| 5 | D | input | input of the gate with its output on pin 1 | tied to +5V |
| 6 | NC | — | not connected inside the chip | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | NC | — | not connected inside the chip | not connected |
| 9 | E | input | input of the gate with its output on pin 13 | B |
| 10 | F | input | input of the gate with its output on pin 13 | D |
| 11 | G | input | input of the gate with its output on pin 13 | A |
| 12 | H | input | input of the gate with its output on pin 13 | C |
| 13 | K | output | K = E OR F OR G OR H | OR |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
4082: dual 4-input AND
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | J | output | J = A AND B AND C AND D | not connected |
| 2 | A | input | input of the gate with its output on pin 1 | tied to +5V |
| 3 | B | input | input of the gate with its output on pin 1 | tied to +5V |
| 4 | C | input | input of the gate with its output on pin 1 | tied to +5V |
| 5 | D | input | input of the gate with its output on pin 1 | tied to +5V |
| 6 | NC | — | not connected inside the chip | not connected |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | NC | — | not connected inside the chip | not connected |
| 9 | E | input | input of the gate with its output on pin 13 | A |
| 10 | F | input | input of the gate with its output on pin 13 | D |
| 11 | G | input | input of the gate with its output on pin 13 | B |
| 12 | H | input | input of the gate with its output on pin 13 | C |
| 13 | K | output | K = E AND F AND G AND H | AND |
| 14 | VDD | power | supply, +5 V (3 to 18 V allowed) | +5V |
5Schematic 1: the gates
gates_4_input.sv describes. A dot
means a connection; crossing lines without a dot are not connected. The chip under each gate is the one that holds
it on the breadboard. · SVG · PNGEvery input line goes to one input of each gate and to an LED.
6Schematic 2: connections between the chips
Every connection of the circuit, net by net. gates_4_input_chips.sv in section 10 is written from the same
list.
| Net | From | To | Wire |
|---|---|---|---|
| A | button A, OUT | 4002 3 (B), 4012 2 (A), 4072 11 (G), 4082 9 (E), LED module, LED 1 | M-F, M-M |
| B | button B, OUT | 4002 4 (C), 4012 4 (C), 4072 9 (E), 4082 11 (G), LED module, LED 2 | M-F, M-M |
| C | button C, OUT | 4002 2 (A), 4012 5 (D), 4072 12 (H), 4082 12 (H), LED module, LED 3 | M-F, M-M |
| D | button D, OUT | 4002 5 (D), 4012 3 (B), 4072 10 (F), 4082 10 (F), LED module, LED 4 | M-F, M-M |
| NOR | 4002 1 (J) | LED module, LED 5 | M-F |
| NAND | 4012 1 (J) | LED module, LED 6 | M-F |
| OR | 4072 13 (K) | LED module, LED 7 | M-F |
| AND | 4082 13 (K) | LED module, LED 8 | M-F |
| +5V | +5V | 4072 14 (VDD), 4002 14 (VDD), 4012 14 (VDD), 4082 14 (VDD), 4072 5 (D), 4072 4 (C), 4072 3 (B), 4072 2 (A), 4002 12 (H), 4002 11 (G), 4002 10 (F), 4002 9 (E), 4012 12 (H), 4012 11 (G), 4012 10 (F), 4012 9 (E), 4082 5 (D), 4082 4 (C), 4082 3 (B), 4082 2 (A) | |
| GND | GND | 4072 7 (VSS), 4002 7 (VSS), 4012 7 (VSS), 4082 7 (VSS) | |
| not connected | 4072 1 (J), 4072 6 (NC), 4072 8 (NC), 4002 6 (NC), 4002 8 (NC), 4002 13 (K), 4012 6 (NC), 4012 8 (NC), 4012 13 (K), 4082 1 (J), 4082 6 (NC), 4082 8 (NC) |
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 4072 turned around, notch to the right, pin 1 in hole 13e; the 4002, notch to the left, pin 1 in hole 17f; the 4012, notch to the left, pin 1 in hole 25f; the 4082 turned around, notch to the right, pin 1 in hole 39e. Read the numbers on the chips: many look alike. A chip turned around has pin 1 at the top right: it puts the pins this circuit needs next to its neighbours.
- Power: for each chip a red wire from its VDD pin (14, or 16 on a 16-pin chip) to the + rail and a black wire from its VSS pin (7 or 8) to the − rail, each to the rail on its own side of the gap. On a chip turned around VDD is on the bottom row and VSS on the top row. Make sure no wire touches + and − at the same time.
- The tie-offs: the inputs of the unused gates to the nearest rail, top pins to +, bottom pins to −.
- The signal wires, in the order of the checklist: each input line from chip to chip. Tick each wire as you insert it.
- 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 | 13j (4072 pin 14, VDD) | bottom + rail, column 13 | |
| 2 | GND | 7a (4072 pin 7, VSS) | top − rail, column 7 | |
| 3 | +5V | 17a (4002 pin 14, VDD) | top + rail, column 17 | |
| 4 | GND | 23j (4002 pin 7, VSS) | bottom − rail, column 23 | |
| 5 | +5V | 25a (4012 pin 14, VDD) | top + rail, column 25 | |
| 6 | GND | 31j (4012 pin 7, VSS) | bottom − rail, column 31 | |
| 7 | +5V | 39j (4082 pin 14, VDD) | bottom + rail, column 39 | |
| 8 | GND | 33a (4082 pin 7, VSS) | top − rail, column 33 | |
| Fixed inputs: CMOS inputs must not float · 16 wires | ||||
| 9 | +5V | 9a (4072 pin 5, D) | top + rail, column 9 | |
| 10 | +5V | 10a (4072 pin 4, C) | top + rail, column 10 | |
| 11 | +5V | 11a (4072 pin 3, B) | top + rail, column 11 | |
| 12 | +5V | 12a (4072 pin 2, A) | top + rail, column 12 | |
| 13 | +5V | 19a (4002 pin 12, H) | top + rail, column 19 | |
| 14 | +5V | 20a (4002 pin 11, G) | top + rail, column 20 | |
| 15 | +5V | 21a (4002 pin 10, F) | top + rail, column 21 | |
| 16 | +5V | 22a (4002 pin 9, E) | top + rail, column 22 | |
| 17 | +5V | 27a (4012 pin 12, H) | top + rail, column 27 | |
| 18 | +5V | 28a (4012 pin 11, G) | top + rail, column 28 | |
| 19 | +5V | 29a (4012 pin 10, F) | top + rail, column 29 | |
| 20 | +5V | 30a (4012 pin 9, E) | top + rail, column 30 | |
| 21 | +5V | 35a (4082 pin 5, D) | top + rail, column 35 | |
| 22 | +5V | 36a (4082 pin 4, C) | top + rail, column 36 | |
| 23 | +5V | 37a (4082 pin 3, B) | top + rail, column 37 | |
| 24 | +5V | 38a (4082 pin 2, A) | top + rail, column 38 | |
| Signal wires · 12 wires | ||||
| 25 | A | 19g (4002 pin 3, B) | 26g (4012 pin 2, A) | |
| 26 | A | 26h (4012 pin 2, A) | 34g (4082 pin 9, E) | |
| 27 | A | 19h (4002 pin 3, B) | 10g (4072 pin 11, G) | |
| 28 | B | 20h (4002 pin 4, C) | 28h (4012 pin 4, C) | |
| 29 | B | 28g (4012 pin 4, C) | 36g (4082 pin 11, G) | |
| 30 | B | 20i (4002 pin 4, C) | 8h (4072 pin 9, E) | |
| 31 | C | 18g (4002 pin 2, A) | 11g (4072 pin 12, H) | |
| 32 | C | 18j (4002 pin 2, A) | 29i (4012 pin 5, D) | |
| 33 | C | 29h (4012 pin 5, D) | 37h (4082 pin 12, H) | |
| 34 | D | 21g (4002 pin 5, D) | 27g (4012 pin 3, B) | |
| 35 | D | 27i (4012 pin 3, B) | 35h (4082 pin 10, F) | |
| 36 | D | 21i (4002 pin 5, D) | 9i (4072 pin 10, F) | |
| Modules on male-female jumpers · 21 wires | ||||
| 37 | +5V | button A, + | bottom + rail, column 19 | |
| 38 | A | button A, OUT | 19j (4002 pin 3, B) | |
| 39 | GND | button A, − | bottom − rail, column 21 | |
| 40 | +5V | button B, + | bottom + rail, column 24 | |
| 41 | B | button B, OUT | 28j (4012 pin 4, C) | |
| 42 | GND | button B, − | bottom − rail, column 26 | |
| 43 | +5V | button C, + | bottom + rail, column 29 | |
| 44 | C | button C, OUT | 29j (4012 pin 5, D) | |
| 45 | GND | button C, − | bottom − rail, column 30 | |
| 46 | +5V | button D, + | bottom + rail, column 34 | |
| 47 | D | button D, OUT | 35j (4082 pin 10, F) | |
| 48 | GND | button D, − | bottom − rail, column 36 | |
| 49 | GND | LED module, G | bottom − rail, column 39 | |
| 50 | A | LED module, LED 1 | 34j (4082 pin 9, E) | |
| 51 | B | LED module, LED 2 | 36j (4082 pin 11, G) | |
| 52 | C | LED module, LED 3 | 37j (4082 pin 12, H) | |
| 53 | D | LED module, LED 4 | 35i (4082 pin 10, F) | |
| 54 | NOR | LED module, LED 5 | 17j (4002 pin 1, J) | |
| 55 | NAND | LED module, LED 6 | 25j (4012 pin 1, J) | |
| 56 | OR | LED module, LED 7 | 12j (4072 pin 13, K) | |
| 57 | AND | LED module, LED 8 | 38j (4082 pin 13, K) | |
Test it
- No button pressed: NOR and NAND are lit.
- Press one, two or three buttons, in any combination: NAND and OR are lit, NOR and AND dark.
- Press all four: AND lights and NAND goes dark.
- Of the 16 rows, only the first and the last make NOR or AND light. That is why wide AND and OR gates are used to recognise one particular combination.
If it does not work: when one output is wrong and the others are right, the inputs are fine: check the wires of that chip, and its power. An LED that flickers when you touch the wires has a floating input: look for a missing tie-off. If a chip gets warm, turn the power off at once: it is probably inserted the wrong way round.
8SystemVerilog: the generic design
gates_4_input.sv describes schematic 1: one assign statement per gate. It is synthesizable.
// Exercise B3c: the dual 4-input gate chips 4002 (NOR), 4012 (NAND), // 4072 (OR) and 4082 (AND), one gate of each. // // Generic version that matches schematic 1: four gates on the same four // inputs, not tied to any chip. Synthesizable. module gates_4_input ( input logic a, input logic b, input logic c, input logic d, output logic y_nor, // 1 only when all four inputs are 0 output logic y_nand, // 0 only when all four inputs are 1 output logic y_or, // 0 only when all four inputs are 0 output logic y_and // 1 only when all four inputs are 1 ); // The reduction operators & and | combine all bits of a vector: // & { a, b, c, d } is a & b & c & d assign y_nor = ~| { a, b, c, d }; assign y_nand = ~& { a, b, c, d }; assign y_or = | { a, b, c, d }; assign y_and = & { a, b, c, d }; endmodule
The reduction operators & and | in front of a vector combine all its bits: & { a, b, c, d } is a & b & c & d, and ~& its NAND.
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.
// CD4002B: dual 4-input NOR gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out, and so are the unconnected pins 6 and 8. // Synthesizable. module cd4002 ( output logic j, // Pin 1: j = ~ (a | b | c | d) input logic a, // Pin 2 input logic b, // Pin 3 input logic c, // Pin 4 input logic d, // Pin 5 // Pin 6: not connected // Pin 7: VSS // Pin 8: not connected input logic e, // Pin 9 input logic f, // Pin 10 input logic g, // Pin 11 input logic h, // Pin 12 output logic k // Pin 13: k = ~ (e | f | g | h) // Pin 14: VDD ); assign j = ~ (a | b | c | d); assign k = ~ (e | f | g | h); endmodule
// CD4012B: dual 4-input NAND gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out, and so are the unconnected pins 6 and 8. // Synthesizable. module cd4012 ( output logic j, // Pin 1: j = ~ (a & b & c & d) input logic a, // Pin 2 input logic b, // Pin 3 input logic c, // Pin 4 input logic d, // Pin 5 // Pin 6: not connected // Pin 7: VSS // Pin 8: not connected input logic e, // Pin 9 input logic f, // Pin 10 input logic g, // Pin 11 input logic h, // Pin 12 output logic k // Pin 13: k = ~ (e & f & g & h) // Pin 14: VDD ); assign j = ~ (a & b & c & d); assign k = ~ (e & f & g & h); endmodule
// CD4072B: dual 4-input OR gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out, and so are the unconnected pins 6 and 8. // Synthesizable. module cd4072 ( output logic j, // Pin 1: j = a | b | c | d input logic a, // Pin 2 input logic b, // Pin 3 input logic c, // Pin 4 input logic d, // Pin 5 // Pin 6: not connected // Pin 7: VSS // Pin 8: not connected input logic e, // Pin 9 input logic f, // Pin 10 input logic g, // Pin 11 input logic h, // Pin 12 output logic k // Pin 13: k = e | f | g | h // Pin 14: VDD ); assign j = a | b | c | d; assign k = e | f | g | h; endmodule
// CD4082B: dual 4-input AND gate. // // Ports in pin order, named as in the datasheet. The power pins, VSS (7) // and VDD (14), are left out, and so are the unconnected pins 6 and 8. // Synthesizable. module cd4082 ( output logic j, // Pin 1: j = a & b & c & d input logic a, // Pin 2 input logic b, // Pin 3 input logic c, // Pin 4 input logic d, // Pin 5 // Pin 6: not connected // Pin 7: VSS // Pin 8: not connected input logic e, // Pin 9 input logic f, // Pin 10 input logic g, // Pin 11 input logic h, // Pin 12 output logic k // Pin 13: k = e & f & g & h // Pin 14: VDD ); assign j = a & b & c & d; assign k = e & f & g & h; endmodule
10SystemVerilog: the chip-level design
gates_4_input_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as gates_4_input.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 B3c, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as gates_4_input.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_b3.py, not this file. Synthesizable. module gates_4_input_chips ( input logic a, input logic b, input logic c, input logic d, output logic y_nor, output logic y_nand, output logic y_or, output logic y_and ); // The power rails of the breadboard, for tied-off inputs wire vdd = 1'b1; // Outputs that go nowhere wire unused_u3_j; wire unused_u1_k; wire unused_u2_k; wire unused_u4_j; //------------------------------------------------------------------------ // U3: CD4072 dual 4-input OR, pin 1 in column 7 cd4072 i_u3 ( .j ( unused_u3_j ), // Pin 1, unused output .a ( vdd ), // Pin 2 <- +5V, unused input .b ( vdd ), // Pin 3 <- +5V, unused input .c ( vdd ), // Pin 4 <- +5V, unused input .d ( vdd ), // Pin 5 <- +5V, unused input .e ( b ), // Pin 9 <- button B .f ( d ), // Pin 10 <- button D .g ( a ), // Pin 11 <- button A .h ( c ), // Pin 12 <- button C .k ( y_or ) // Pin 13 -> OR ); //------------------------------------------------------------------------ // U1: CD4002 dual 4-input NOR, pin 1 in column 17 cd4002 i_u1 ( .j ( y_nor ), // Pin 1 -> NOR .a ( c ), // Pin 2 <- button C .b ( a ), // Pin 3 <- button A .c ( b ), // Pin 4 <- button B .d ( d ), // Pin 5 <- button D .e ( vdd ), // Pin 9 <- +5V, unused input .f ( vdd ), // Pin 10 <- +5V, unused input .g ( vdd ), // Pin 11 <- +5V, unused input .h ( vdd ), // Pin 12 <- +5V, unused input .k ( unused_u1_k ) // Pin 13, unused output ); //------------------------------------------------------------------------ // U2: CD4012 dual 4-input NAND, pin 1 in column 25 cd4012 i_u2 ( .j ( y_nand ), // Pin 1 -> NAND .a ( a ), // Pin 2 <- button A .b ( d ), // Pin 3 <- button D .c ( b ), // Pin 4 <- button B .d ( c ), // Pin 5 <- button C .e ( vdd ), // Pin 9 <- +5V, unused input .f ( vdd ), // Pin 10 <- +5V, unused input .g ( vdd ), // Pin 11 <- +5V, unused input .h ( vdd ), // Pin 12 <- +5V, unused input .k ( unused_u2_k ) // Pin 13, unused output ); //------------------------------------------------------------------------ // U4: CD4082 dual 4-input AND, pin 1 in column 33 cd4082 i_u4 ( .j ( unused_u4_j ), // Pin 1, unused output .a ( vdd ), // Pin 2 <- +5V, unused input .b ( vdd ), // Pin 3 <- +5V, unused input .c ( vdd ), // Pin 4 <- +5V, unused input .d ( vdd ), // Pin 5 <- +5V, unused input .e ( a ), // Pin 9 <- button A .f ( d ), // Pin 10 <- button D .g ( b ), // Pin 11 <- button B .h ( c ), // Pin 12 <- button C .k ( y_and ) // Pin 13 -> AND ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares them with the truth tables of the gates,
written out row by row. The circuit has no clock, but the testbench has one, like the testbenches of
Basics-Graphics-Music: it sets the inputs right after a rising edge and checks the outputs at the next one. It goes
through all 16 rows twice, counting up and in Gray code order, and prints PASS only if both designs
match every time and every row was seen.
tb.sv180 lines · show the codefile
// Testbench for exercise B3c. // // Runs the generic design (gates_4_input.sv) and the chip-level design // (gates_4_input_chips.sv) side by side and compares both with // the truth tables of the gates, written out row by row. Works with Icarus Verilog, Verilator (--binary) // and Questa. // // The circuit has no clock, but like the testbenches of Basics-Graphics-Music // this one drives and checks signals only at rising edges of its own clock: // it changes the inputs after one edge and checks the outputs at the next. // It tries all 16 input combinations twice: counting up, and then in // Gray code order, where one input changes at a time, as when a student // presses or releases one button. // Time units. The design files have none: Yosys does not accept timeunit // and timeprecision, and the Tiny Tapeout template sets `timescale 1ns / 1ps // in its own testbench. This line sets the same units here and for the // design files compiled after it, so tb.sv comes first in the file list. `timescale 1ns / 1ps module tb; logic clk; logic a; logic b; logic c; logic d; wire y_nor, y_nor_chips; wire y_nand, y_nand_chips; wire y_or, y_or_chips; wire y_and, y_and_chips; gates_4_input i_generic ( .a ( a ), .b ( b ), .c ( c ), .d ( d ), .y_nor ( y_nor ), .y_nand ( y_nand ), .y_or ( y_or ), .y_and ( y_and ) ); gates_4_input_chips i_chips ( .a ( a ), .b ( b ), .c ( c ), .d ( d ), .y_nor ( y_nor_chips ), .y_nand ( y_nand_chips ), .y_or ( y_or_chips ), .y_and ( y_and_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the truth tables of the gates, written out row by row. // Bits, from the left: y_nor, y_nand, y_or, y_and. function automatic logic [3:0] expected (input logic in_a, input logic in_b, input logic in_c, input logic in_d); case ({ in_a, in_b, in_c, in_d }) 4'b0000: return 4'b1100; 4'b0001: return 4'b0110; 4'b0010: return 4'b0110; 4'b0011: return 4'b0110; 4'b0100: return 4'b0110; 4'b0101: return 4'b0110; 4'b0110: return 4'b0110; 4'b0111: return 4'b0110; 4'b1000: return 4'b0110; 4'b1001: return 4'b0110; 4'b1010: return 4'b0110; 4'b1011: return 4'b0110; 4'b1100: return 4'b0110; 4'b1101: return 4'b0110; 4'b1110: return 4'b0110; 4'b1111: return 4'b0011; default: return 4'bxxxx; endcase endfunction wire [3:0] out = { y_nor, y_nand, y_or, y_and }; wire [3:0] out_chips = { y_nor_chips, y_nand_chips, y_or_chips, y_and_chips }; wire [3:0] out_model = expected (a, b, c, d); //------------------------------------------------------------------------ // Checking at every rising clock edge: the inputs still hold the values the // stimulus set at the previous edge, and the outputs have settled. // Initial values are fine in a testbench // verilator lint_off PROCASSINIT logic checking = 1'b0; // Set by the stimulus after the first inputs logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; logic [15:0] rows_seen = '0; // Which input combinations were checked always @ (posedge clk) if (checking & ~ done) begin cycle <= cycle + 1; n_checks <= n_checks + 2; n_errors <= n_errors + (out === out_model ? 0 : 1) + (out_chips === out_model ? 0 : 1); rows_seen <= rows_seen | 16'b1 << { a, b, c, d }; $display ("%5d %b %b %b %b %b %b %b %b %s %s", cycle + 1, a, b, c, d, y_nor, y_nand, y_or, y_and, 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 C D NOR NAND OR AND generic chips"); checking <= '1; for (int i = 0; i < 16; i ++) begin { a, b, c, d } <= 4' (i); @ (posedge clk); end for (int i = 0; i < 16; i ++) begin { a, b, c, d } <= 4' (i ^ (i >> 1)); @ (posedge clk); end // Stop checking one clock edge before $finish, so that every // simulator reports the same number of checks done <= '1; @ (posedge clk); $finish; end //------------------------------------------------------------------------ final begin if (n_errors == 0 && & rows_seen) $display ("PASS: %0d checks, no errors", n_checks); else $display ("FAIL: %0d errors in %0d checks, %0d of %0d input combinations seen", n_errors, n_checks, $countones (rows_seen), 16); end endmodule
Each script compiles the files, runs the simulation, writes a log and the waveform
tb.vcd, and exits with an error unless the test passes. Run them from any directory:
./run_icarus.bash, ./run_verilator.bash or ./run_questa.bash;
./clean.bash removes what they make. The waveform opens in GTKWave or Surfer.
run_icarus.bash22 lines · show the codefile
#!/usr/bin/env bash # Exercise B3c: 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 gates_4_input.sv gates_4_input_chips.sv cd4072.sv cd4002.sv cd4012.sv cd4082.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 B3c: 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 gates_4_input.sv gates_4_input_chips.sv cd4072.sv cd4002.sv cd4012.sv cd4082.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 B3c: 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 gates_4_input.sv gates_4_input_chips.sv cd4072.sv cd4002.sv cd4012.sv cd4082.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 C D NOR NAND OR AND generic chips
1 0 0 0 0 1 1 0 0 ok ok
2 0 0 0 1 0 1 1 0 ok ok
3 0 0 1 0 0 1 1 0 ok ok
4 0 0 1 1 0 1 1 0 ok ok
5 0 1 0 0 0 1 1 0 ok ok
6 0 1 0 1 0 1 1 0 ok ok
7 0 1 1 0 0 1 1 0 ok ok
8 0 1 1 1 0 1 1 0 ok ok
9 1 0 0 0 0 1 1 0 ok ok
10 1 0 0 1 0 1 1 0 ok ok
11 1 0 1 0 0 1 1 0 ok ok
12 1 0 1 1 0 1 1 0 ok ok
13 1 1 0 0 0 1 1 0 ok ok
14 1 1 0 1 0 1 1 0 ok ok
15 1 1 1 0 0 1 1 0 ok ok
16 1 1 1 1 0 0 1 1 ok ok
17 0 0 0 0 1 1 0 0 ok ok
18 0 0 0 1 0 1 1 0 ok ok
19 0 0 1 1 0 1 1 0 ok ok
20 0 0 1 0 0 1 1 0 ok ok
21 0 1 1 0 0 1 1 0 ok ok
22 0 1 1 1 0 1 1 0 ok ok
23 0 1 0 1 0 1 1 0 ok ok
24 0 1 0 0 0 1 1 0 ok ok
25 1 1 0 0 0 1 1 0 ok ok
26 1 1 0 1 0 1 1 0 ok ok
27 1 1 1 1 0 0 1 1 ok ok
28 1 1 1 0 0 1 1 0 ok ok
29 1 0 1 0 0 1 1 0 ok ok
30 1 0 1 1 0 1 1 0 ok ok
31 1 0 0 1 0 1 1 0 ok ok
32 1 0 0 0 0 1 1 0 ok ok
tb.sv:166: $finish called at 325000 (1ps)
PASS: 64 checks, no errors
To see that the testbench catches mistakes, break something and run it again: change ~& { a, b, c, d } to ~& { a, b, c } in gates_4_input.sv. The script
reports FAIL and the trace shows the lines marked WRONG.
12On an FPGA board: lab_top in BGM
The Basics-Graphics-Music (BGM) repository runs the same labs on dozens
of FPGA boards. Each lab has a lab_top module with the same ports on every board: clock, a 1 Hz
slow_clk, reset, keys, LEDs, a 7-segment display.
- Copy
labs/1_basics/1_01_and_or_not_xor_de_morganto a new directory. - Replace its
lab_top.svwithbgm_lab/lab_top.svfrom this exercise and addgates_4_input.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, 1, 2 and 3 are A, B, C and D; LEDs 0–3 show NOR, NAND, OR and AND. To run the
chip-level design instead, add gates_4_input_chips.sv and the chip models and change the module name in the
instance.
bgm_lab/lab_top.sv103 lines · show the codefile
// Exercise B3c 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 gates_4_input.sv. For the // chip-level design add gates_4_input_chips.sv and cd4002.sv, cd4012.sv, // cd4072.sv, cd4082.sv instead, and change the module name below. // // Keys: key [0] a, key [1] b, key [2] c, key [3] d. The LEDs: led [0] y_nor, // led [1] y_nand, led [2] y_or, led [3] y_and. 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 y_nor; logic y_nand; logic y_or; logic y_and; gates_4_input i_gates_4_input ( .a ( key [0] ), .b ( key [1] ), .c ( key [2] ), .d ( key [3] ), .y_nor ( y_nor ), .y_nand ( y_nand ), .y_or ( y_or ), .y_and ( y_and ) ); assign led = w_led' ({ y_and, y_or, y_nand, y_nor }); 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 addgates_4_input.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 0–3 are the inputs. The screen shows 8 square lamps in a row: the inputs in blue and the outputs in green, like the LED module on the breadboard.
bgm_hackathon/hackathon_top.sv90 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise B3c 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 gates_4_input.sv. // // Keys: key [0] a, key [1] b, key [2] c, key [3] d. The LEDs: led [0] y_nor, // led [1] y_nand, led [2] y_or, led [3] y_and. The screen shows 8 lamps in a // row: A, B, C, D, NOR, NAND, OR, AND; inputs in blue, outputs in green. module hackathon_top ( input logic clock, input logic slow_clock, input logic reset, input logic [7:0] key, output logic [7:0] led, // A dynamic seven-segment display output logic [7:0] abcdefgh, output logic [7:0] digit, // LCD screen interface input logic [8:0] x, input logic [8:0] y, output logic [4:0] red, output logic [5:0] green, output logic [4:0] blue, inout logic [3:0] gpio ); //------------------------------------------------------------------------ wire a = key [0]; wire b = key [1]; wire c = key [2]; wire d = key [3]; logic y_nor; logic y_nand; logic y_or; logic y_and; gates_4_input i_gates_4_input ( .a ( a ), .b ( b ), .c ( c ), .d ( d ), .y_nor ( y_nor ), .y_nand ( y_nand ), .y_or ( y_or ), .y_and ( y_and ) ); assign led = { 4'b0, y_and, y_or, y_nand, y_nor }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 8 square lamps on the 480 x 272 screen, 40 pixels wide, 57 apart: // the inputs in blue, the outputs in green. Bright when 1, dim when 0. wire [7:0] lamp = { y_and, y_or, y_nand, y_nor, d, c, b, a }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 8; i ++) if (x >= 9' (20 + i * 57) & x < 9' (60 + i * 57) & y >= 116 & y < 156) begin if (i < 4) blue = lamp [i] ? 31 : 6; else green = lamp [i] ? 63 : 12; end end endmodule
14On a chip: Tiny Tapeout
Tiny Tapeout makes real chips from student designs. Each design
is a module with the ports below; on the demo board ui_in comes from DIP switches and
uo_out drives a 7-segment display.
This design has no clock and no state: project.v connects the DIP switches to its inputs
and its outputs to the display. The project still gets a clock and a reset from the board; they stay unused.
Switches 0–3 are the inputs; segments a, b, c and d of the display show NOR, NAND, OR and AND.
tiny_tapeout/project.v56 lines · show the codefile
/* * Exercise B3c on Tiny Tapeout: the dual 4-input family, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add gates_4_input.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, switch 2 is c, switch 3 is d wire y_nor; wire y_nand; wire y_or; wire y_and; gates_4_input i_gates_4_input ( .a ( ui_in [0] ), .b ( ui_in [1] ), .c ( ui_in [2] ), .d ( ui_in [3] ), .y_nor ( y_nor ), .y_nand ( y_nand ), .y_or ( y_or ), .y_and ( y_and ) ); // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g, // uo_out [7] is the dot. Each output lights one segment: // a (top) y_nor, b (top right) y_nand, c (bottom right) y_or, d (bottom) // y_and. assign uo_out = { 4'b0, y_and, y_or, y_nand, y_nor }; assign uio_out = 8'b0; assign uio_oe = 8'b0; // The circuit needs no clock or reset. // List all unused inputs to prevent warnings wire _unused = & { ena, clk, rst_n, ui_in [7:4], uio_in, 1'b0 }; endmodule
In info.yaml list the files and use the module name as the top module:
project:
top_module: "tt_um_example" # rename to tt_um_<your GitHub name>_gates_4_input
source_files:
- "project.v"
- "gates_4_input.sv"
15Going further
Tasks for students
- Your chip in words. Take one chip of the family and its datasheet, find the pinout and the function, and write the function in words and as a truth table before you build anything. Then check it on the board.
- Recognise a code. Make an LED light only for A = 1, B = 0, C = 1, D = 0, with one 4082 gate and two inverters of a 4069.
- Three inputs from four. Use a 4072 gate as a 3-input OR: where does the fourth input go?
- Eight inputs. Combine the two gates of a 4082 and one gate of a 4081 into an 8-input AND, and compare with the 4068 of exercise B3d.
- In SystemVerilog: add an output to
gates_4_input.svfor another gate of the family and its expected values to the testbench. The testbench fails until both are right.
Files of this exercise
- gates_4_input.sv: the generic design (section 8)
- cd4072.sv, cd4002.sv, cd4012.sv and cd4082.sv: chip models (section 9)
- gates_4_input_chips.sv: the chip-level design (section 10)
- tb.sv, run_icarus.bash, run_verilator.bash, run_questa.bash, clean.bash (section 11)
- bgm_lab/lab_top.sv, bgm_hackathon/hackathon_top.sv, tiny_tapeout/project.v (sections 12–14)
- Drawings as SVG and PNG in images/: the schematics, the pinouts and the waveforms; the Russian versions end in
_ru
Sources
Lesson 1.1 of the Habr article about the electronics course describes the datasheet exercise with these chips; the LSSK components sheet lists them. The chips are described in the Texas Instruments datasheets for the CD4002B, CD4012B, CD4072B and CD4082B.