Exercise B2 · basics · logic gates
De Morgan's laws
NOT (A AND B) is the same as NOT A OR NOT B, and NOT (A OR B) is the same as NOT A AND NOT B. Here each side of both laws is built from its own gates, and the LEDs show both sides next to each other: whatever you press, the two LEDs of a pair agree.
1What it is and why it matters
What it does. Buttons A and B drive four small circuits. The first is an AND gate followed by a NOT gate: NOT (A AND B), a NAND. The second inverts A and B first and then ORs them: NOT A OR NOT B. The third and fourth do the same with OR and AND swapped. LEDs 1 and 2 of the LED module show A and B, LEDs 3 and 4 the first pair, LEDs 5 and 6 the second pair. The laws say that the two LEDs of a pair are always equal, and the board lets you check all four combinations of the buttons in a few seconds.
How it works. NOT (A AND B) is 0 only when both inputs are 1. NOT A OR NOT B is 0 only when NOT A and NOT B are both 0, which is again when both inputs are 1: the same function, built differently. In words: “not both” is the same as “at least one is not”, and “neither” is the same as “not this one and not that one”. Lesson 1.1 of the Habr article uses these laws in the first class: half of the students get a 4081, half a 4071, everybody a 4069, and each pair compares its LEDs.
Where it is used.
- In every CMOS chip. The simplest CMOS gates invert: a NAND or a NOR is smaller and faster than an AND or an OR, which is a NAND or a NOR with an inverter behind it. Synthesis tools use De Morgan's laws all the time to turn AND/OR logic into NAND/NOR cells.
- Active-low signals. Many signals are true when they are 0:
rst_n, chip selects, the ~CARRY OUT of the 4029. “Reset if button 1 or button 2 says reset”, with active-low buttons, is an AND of the two signals. - Conditions in code.
if (!(ready && valid))is the same asif (!ready || !valid); the second form is often easier to read, and in a code review you will see both. - State machines. The next-state equations of the FSM exercises (P5, P6) are simplified with these laws, so that they fit the gates that are on the board.
Why learn it. Two gates in a row are a new function, and two different circuits can be the same function. Proving it with LEDs, row by row, is exactly what a testbench does later in this exercise, and what equivalence checking tools do for whole chips.
2The key elements: the two laws
AND, then NOT
A NAND gate is an AND with a small circle, a bubble, on its output: the circle means NOT.
NOT, then OR
The same function drawn as an OR gate with bubbles on its inputs: NOT A OR NOT B.
OR, then NOT
A NOR gate: OR with a bubble on the output.
NOT, then AND
The same function as an AND gate with bubbles on its inputs: NOT A AND NOT B.
The truth tables
The first law, ~(A & B) == ~A | ~B, and the second law, ~(A | B) == ~A & ~B, in the notation of
SystemVerilog: ~ is NOT, & is AND, | is OR.
| A | B | ~(A & B) | ~A | ~B |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| A | B | ~(A | B) | ~A & ~B |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 |
Every signal of the board, one row per combination of the buttons:
| A | B | ~A | ~B | A & B | ~(A & B) | ~A | ~B | A | B | ~(A | B) | ~A & ~B |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
A rule to remember them by, called bubble pushing: push a bubble through a gate and the gate changes from AND to OR or back, and the bubble appears on every input. That is how a designer reads a NAND gate as “OR of the inverted inputs” without writing a table.
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. In every column the two bars of
a pair are equal. · SVG · PNG3Parts
| Part | In this circuit | Qty | Price | Supplier |
|---|---|---|---|---|
| CD4071 | quad 2-input OR: A OR B, NOT A OR NOT B (two of four used) | 1 | $0.25 | Elexp (elexp.com) |
| CD4069 | hex inverter: NOT A, NOT B and the two inverted results (four of six used) | 1 | $0.30 | Elexp (elexp.com) |
| CD4081 | quad 2-input AND: A AND B, NOT A AND NOT B (two of four used) | 1 | $0.55 | Elexp (elexp.com) |
| Button modules | A and B; 2 from the pack of five | 2 | $0.66 / pack | aliexpress.us |
| 8-LED module | shows A, B and both sides of both laws | 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 | 27 wires on the board (or pre-cut wires) | 27 | $1.30 / pack | aliexpress.us |
| Male-female Dupont jumpers | 13 leads to the buttons and the LED module | 13 | $1.30 / pack | aliexpress.us |
Prices from the LSSK components sheet; all of these parts are in the Basics kit. Pre-cut wires lie flatter than Dupont jumpers and match the drawing better.
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.
Here two gates of the 4081, two of the 4071 and four of the six inverters of the 4069 are used; the inputs of the others are tied to the rails.
4071: quad 2-input OR
Gate L (pins 8, 9 → 10) makes A OR B, gate K (pins 5, 6 → 4) makes NOT A OR NOT B. The 4071 and the 4081 have the same pinout, so in a class the two chips can trade places.
| Pin | Name | Direction | Function | In this circuit |
|---|---|---|---|---|
| 1 | A | input | input of the gate with its output on pin 3 | tied to GND |
| 2 | B | input | input of the gate with its output on pin 3 | tied to GND |
| 3 | J | output | J = A OR B | not connected |
| 4 | K | output | K = C OR D | A|B |
| 5 | C | input | input of the gate with its output on pin 4 | B |
| 6 | D | input | input of the gate with its output on pin 4 | A |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | E | input | input of the gate with its output on pin 10 | A |
| 9 | F | input | input of the gate with its output on pin 10 | B |
| 10 | L | output | L = E OR F | A|B |
| 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 |
4069: hex inverter
Pins 1 → 2 and 3 → 4 make NOT A and NOT B; pins 5 → 6 invert A AND B into the NAND, and pins 9 → 8 invert A OR B into the NOR. The inverters with inputs on pins 11 and 13 are not used.
| 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 | A&B |
| 6 | I | output | I = NOT C | A&B |
| 7 | VSS | power | ground, 0 V | GND |
| 8 | J | output | J = NOT D | A|B |
| 9 | D | input | input of the gate with its output on pin 8 | A|B |
| 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 |
4081: quad 2-input AND
Gate J (pins 1, 2 → 3) makes A AND B, gate K (pins 5, 6 → 4) makes NOT A AND NOT B.
Polarity. On all three 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 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 | A&B |
| 5 | C | input | input of the gate with its output on pin 4 | A |
| 6 | D | input | input of the gate with its output on pin 4 | B |
| 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 |
5Schematic 1: the gates
de_morgan.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 · PNGThe two inverters of A and B in the first column feed both right-hand sides: NOT A OR NOT B and NOT A AND NOT B. The left-hand sides are a gate and an inverter each. On the right, the LEDs: the two of each pair sit next to each other on the LED module.
6Schematic 2: connections between the chips
Every connection of the circuit, net by net. de_morgan_chips.sv in section 10 is written from the
same list.
| Net | From | To | Wire |
|---|---|---|---|
| A | button A, OUT | 4081 1 (A), 4071 8 (E), 4069 1 (A), LED module, LED 1 | M-F, M-M |
| B | button B, OUT | 4081 2 (B), 4071 9 (F), 4069 3 (B), LED module, LED 2 | M-F, M-M |
| A | 4069 2 (G) | 4081 5 (C), 4071 6 (D) | M-M |
| B | 4069 4 (H) | 4081 6 (D), 4071 5 (C) | M-M |
| A&B | 4081 3 (J) | 4069 5 (C) | M-M |
| A|B | 4071 10 (L) | 4069 9 (D) | M-M |
| A&B | 4069 6 (I) | LED module, LED 3 | M-F |
| A|B | 4071 4 (K) | LED module, LED 4 | M-F |
| A|B | 4069 8 (J) | LED module, LED 5 | M-F, M-M |
| A&B | 4081 4 (K) | LED module, LED 6 | M-F |
| +5V | +5V | 4071 14 (VDD), 4069 14 (VDD), 4081 14 (VDD), 4071 13 (H), 4071 12 (G), 4069 13 (F), 4069 11 (E), 4081 13 (H), 4081 12 (G), 4081 9 (F), 4081 8 (E) | |
| GND | GND | 4071 7 (VSS), 4069 7 (VSS), 4081 7 (VSS), 4071 1 (A), 4071 2 (B) | |
| not connected | 4071 3 (J), 4071 11 (M), 4069 10 (K), 4069 12 (L), 4081 10 (L), 4081 11 (M) |
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 4071 with pin 1 in hole 10f, the 4069 in 19f, the 4081 in 27f. The 4071 and the 4081 look alike: read the numbers.
- 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: 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: A and B to all three chips, NOT A and NOT B from the 4069 to the 4071 and the 4081, A AND B and A OR B into the 4069. 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 −) · 6 wires | ||||
| 1 | +5V | 10a (4071 pin 14, VDD) | top + rail, column 10 | |
| 2 | GND | 16j (4071 pin 7, VSS) | bottom − rail, column 16 | |
| 3 | +5V | 19a (4069 pin 14, VDD) | top + rail, column 19 | |
| 4 | GND | 25j (4069 pin 7, VSS) | bottom − rail, column 25 | |
| 5 | +5V | 27a (4081 pin 14, VDD) | top + rail, column 27 | |
| 6 | GND | 33j (4081 pin 7, VSS) | bottom − rail, column 33 | |
| Fixed inputs: CMOS inputs must not float · 10 wires | ||||
| 7 | GND | 10j (4071 pin 1, A) | bottom − rail, column 10 | |
| 8 | +5V | 11a (4071 pin 13, H) | top + rail, column 11 | |
| 9 | GND | 11j (4071 pin 2, B) | bottom − rail, column 11 | |
| 10 | +5V | 12a (4071 pin 12, G) | top + rail, column 12 | |
| 11 | +5V | 20a (4069 pin 13, F) | top + rail, column 20 | |
| 12 | +5V | 22a (4069 pin 11, E) | top + rail, column 22 | |
| 13 | +5V | 28a (4081 pin 13, H) | top + rail, column 28 | |
| 14 | +5V | 29a (4081 pin 12, G) | top + rail, column 29 | |
| 15 | +5V | 32a (4081 pin 9, F) | top + rail, column 32 | |
| 16 | +5V | 33a (4081 pin 8, E) | top + rail, column 33 | |
| Signal wires · 11 wires | ||||
| 17 | A | 27h (4081 pin 1, A) | 19h (4069 pin 1, A) | |
| 18 | A | 19i (4069 pin 1, A) | 16b (4071 pin 8, E) | |
| 19 | B | 28i (4081 pin 2, B) | 21i (4069 pin 3, B) | |
| 20 | B | 21h (4069 pin 3, B) | 15c (4071 pin 9, F) | |
| 21 | A | 20g (4069 pin 2, G) | 15g (4071 pin 6, D) | |
| 22 | A | 20h (4069 pin 2, G) | 31g (4081 pin 5, C) | |
| 23 | B | 22g (4069 pin 4, H) | 14g (4071 pin 5, C) | |
| 24 | B | 22h (4069 pin 4, H) | 32g (4081 pin 6, D) | |
| 25 | A&B | 29j (4081 pin 3, J) | 23j (4069 pin 5, C) | |
| 26 | A|B | 14d (4071 pin 10, L) | 24d (4069 pin 9, D) | |
| 27 | A|B | 25d (4069 pin 8, J) | 26f | |
| Modules on male-female jumpers · 13 wires | ||||
| 28 | +5V | button A, + | bottom + rail, column 17 | |
| 29 | A | button A, OUT | 19j (4069 pin 1, A) | |
| 30 | GND | button A, − | bottom − rail, column 19 | |
| 31 | +5V | button B, + | bottom + rail, column 22 | |
| 32 | B | button B, OUT | 21j (4069 pin 3, B) | |
| 33 | GND | button B, − | bottom − rail, column 24 | |
| 34 | GND | LED module, G | bottom − rail, column 27 | |
| 35 | A | LED module, LED 1 | 27j (4081 pin 1, A) | |
| 36 | B | LED module, LED 2 | 28j (4081 pin 2, B) | |
| 37 | A&B | LED module, LED 3 | 24j (4069 pin 6, I) | |
| 38 | A|B | LED module, LED 4 | 13j (4071 pin 4, K) | |
| 39 | A|B | LED module, LED 5 | 26j | |
| 40 | A&B | LED module, LED 6 | 30j (4081 pin 4, K) | |
Test it
- With no button pressed, LEDs 3–6 are all lit: NOT (0 AND 0) and NOT (0 OR 0) are both 1.
- Press A or B alone: LEDs 3 and 4 stay lit, LEDs 5 and 6 go dark together.
- Press both: all four go dark. LEDs 3 and 4 change only in this row, LEDs 5 and 6 only in the first.
- If the two LEDs of a pair ever differ, a wire is wrong: the laws do not have exceptions.
If it does not work: when one LED of a pair is wrong, follow its own path in schematic 1, gate by gate, and check each output with a wire to an LED of the module. An LED that flickers when you touch the wires has a floating input somewhere on its path: 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
de_morgan.sv describes schematic 1: two wires for the inverted inputs and four assign
statements, one for each side of each law. It is synthesizable.
// Exercise B2: De Morgan's laws. // // Generic version that matches schematic 1: both sides of // // ~ (a & b) == ~ a | ~ b // ~ (a | b) == ~ a & ~ b // // each built from its own gates, so that the two outputs of a pair can // be compared. Synthesizable. module de_morgan ( input logic a, input logic b, output logic not_a_and_b, // ~ (a & b): an AND gate, then NOT output logic not_a_or_not_b, // ~ a | ~ b: two NOT gates, then OR output logic not_a_or_b, // ~ (a | b): an OR gate, then NOT output logic not_a_and_not_b // ~ a & ~ b: two NOT gates, then AND ); wire not_a = ~ a; wire not_b = ~ b; assign not_a_and_b = ~ (a & b); assign not_a_or_not_b = not_a | not_b; assign not_a_or_b = ~ (a | b); assign not_a_and_not_b = not_a & not_b; endmodule
A synthesis tool knows De Morgan's laws, so it notices that not_a_and_b and
not_a_or_not_b are the same function and builds it once. On the breadboard nobody merges them, which
is the point of the exercise.
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.
// 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
// 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
// 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
10SystemVerilog: the chip-level design
de_morgan_chips.sv connects the chip models exactly as schematic 2 and the breadboard connect
the chips, with the same ports as de_morgan.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 B2, chip-level version: the chips and wires of schematic 2 and // of the breadboard. Same ports as de_morgan.sv. // // Generated by tools/bb_lib.py from the checked breadboard wire list; // change the layout in tools/bb_b2.py, not this file. Synthesizable. module de_morgan_chips ( input logic a, input logic b, output logic not_a_and_b, output logic not_a_or_not_b, output logic not_a_or_b, output logic not_a_and_not_b ); // Connections between the chips that are not ports wire nb; wire na; wire a_or_b; wire a_and_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_u2_j; wire unused_u2_m; wire unused_u3_k; wire unused_u3_l; wire unused_u1_l; wire unused_u1_m; //------------------------------------------------------------------------ // U2: CD4071 quad 2-input OR, pin 1 in column 10 cd4071 i_u2 ( .a ( gnd ), // Pin 1 <- GND, unused input .b ( gnd ), // Pin 2 <- GND, unused input .j ( unused_u2_j ), // Pin 3, unused output .k ( not_a_or_not_b ), // Pin 4 -> ~A|~B .c ( nb ), // Pin 5 <- U3 pin 4 (H) .d ( na ), // Pin 6 <- U3 pin 2 (G) .e ( a ), // Pin 8 <- button A .f ( b ), // Pin 9 <- button B .l ( a_or_b ), // Pin 10 -> A|B .m ( unused_u2_m ), // Pin 11, unused output .g ( vdd ), // Pin 12 <- +5V, unused input .h ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U3: CD4069 hex inverter (six NOT gates), pin 1 in column 19 cd4069 i_u3 ( .a ( a ), // Pin 1 <- button A .g ( na ), // Pin 2 -> ~A .b ( b ), // Pin 3 <- button B .h ( nb ), // Pin 4 -> ~B .c ( a_and_b ), // Pin 5 <- U1 pin 3 (J) .i ( not_a_and_b ), // Pin 6 -> ~(A&B) .j ( not_a_or_b ), // Pin 8 -> ~(A|B) .d ( a_or_b ), // Pin 9 <- U2 pin 10 (L) .k ( unused_u3_k ), // Pin 10, unused output .e ( vdd ), // Pin 11 <- +5V, unused input .l ( unused_u3_l ), // Pin 12, unused output .f ( vdd ) // Pin 13 <- +5V, unused input ); //------------------------------------------------------------------------ // U1: CD4081 quad 2-input AND, pin 1 in column 27 cd4081 i_u1 ( .a ( a ), // Pin 1 <- button A .b ( b ), // Pin 2 <- button B .j ( a_and_b ), // Pin 3 -> A&B .k ( not_a_and_not_b ), // Pin 4 -> ~A&~B .c ( na ), // Pin 5 <- U3 pin 2 (G) .d ( nb ), // Pin 6 <- U3 pin 4 (H) .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 ); endmodule
11Testbench and simulation scripts
tb.sv runs both designs side by side and compares their four outputs with a reference model: the
truth tables of NAND and NOR, written out row by row. The model has one value per pair, so a design passes only if
the two outputs of each pair are equal, as the laws say. The circuit has no clock, but the testbench has one, like
the testbenches of Basics-Graphics-Music: it sets A and B right after a rising edge and checks the outputs at the
next one. It goes through the four 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.sv166 lines · show the codefile
// Testbench for exercise B2. // // Runs the generic design (de_morgan.sv) and the chip-level design // (de_morgan_chips.sv) side by side and compares both with // the truth table of NAND and NOR, 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 4 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; wire not_a_and_b, not_a_and_b_chips; wire not_a_or_not_b, not_a_or_not_b_chips; wire not_a_or_b, not_a_or_b_chips; wire not_a_and_not_b, not_a_and_not_b_chips; de_morgan i_generic ( .a ( a ), .b ( b ), .not_a_and_b ( not_a_and_b ), .not_a_or_not_b ( not_a_or_not_b ), .not_a_or_b ( not_a_or_b ), .not_a_and_not_b ( not_a_and_not_b ) ); de_morgan_chips i_chips ( .a ( a ), .b ( b ), .not_a_and_b ( not_a_and_b_chips ), .not_a_or_not_b ( not_a_or_not_b_chips ), .not_a_or_b ( not_a_or_b_chips ), .not_a_and_not_b ( not_a_and_not_b_chips ) ); //------------------------------------------------------------------------ // Clock with a period of 10 ns initial begin clk = '0; forever # 5 clk = ~ clk; end //------------------------------------------------------------------------ // Reference model: the truth table of NAND and NOR, written out row by row. // Bits, from the left: not_a_and_b, not_a_or_not_b, not_a_or_b, not_a_and_not_b. function automatic logic [3:0] expected (input logic in_a, input logic in_b); // Both laws say that the two outputs of a pair are equal, so the // model gives each pair one value: NAND for the first, NOR for the second case ({ in_a, in_b }) 2'b00: return 4'b11_11; 2'b01: return 4'b11_00; 2'b10: return 4'b11_00; 2'b11: return 4'b00_00; default: return 4'bxx_xx; endcase endfunction wire [3:0] out = { not_a_and_b, not_a_or_not_b, not_a_or_b, not_a_and_not_b }; wire [3:0] out_chips = { not_a_and_b_chips, not_a_or_not_b_chips, not_a_or_b_chips, not_a_and_not_b_chips }; wire [3:0] out_model = expected (a, b); //------------------------------------------------------------------------ // Checking at every rising clock edge: the inputs still hold the values the // stimulus set at the previous edge, and the outputs have settled. // Initial values are fine in a testbench // verilator lint_off PROCASSINIT logic checking = 1'b0; // Set by the stimulus after the first inputs logic done = 1'b0; // Set by the stimulus before $finish int cycle = 0; int n_checks = 0; int n_errors = 0; logic [3: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 | 4'b1 << { a, b }; $display ("%5d %b %b %b %b %b %b %s %s", cycle + 1, a, b, not_a_and_b, not_a_or_not_b, not_a_or_b, not_a_and_not_b, 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 ~(A&B) ~A|~B ~(A|B) ~A&~B generic chips"); checking <= '1; for (int i = 0; i < 4; i ++) begin { a, b } <= 2' (i); @ (posedge clk); end for (int i = 0; i < 4; i ++) begin { a, b } <= 2' (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), 4); 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 B2: 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 de_morgan.sv de_morgan_chips.sv cd4071.sv cd4069.sv cd4081.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 B2: 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 de_morgan.sv de_morgan_chips.sv cd4071.sv cd4069.sv cd4081.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 B2: 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 de_morgan.sv de_morgan_chips.sv cd4071.sv cd4069.sv cd4081.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 ~(A&B) ~A|~B ~(A|B) ~A&~B generic chips
1 0 0 1 1 1 1 ok ok
2 0 1 1 1 0 0 ok ok
3 1 0 1 1 0 0 ok ok
4 1 1 0 0 0 0 ok ok
5 0 0 1 1 1 1 ok ok
6 0 1 1 1 0 0 ok ok
7 1 1 0 0 0 0 ok ok
8 1 0 1 1 0 0 ok ok
tb.sv:152: $finish called at 85000 (1ps)
PASS: 16 checks, no errors
To see that the testbench catches mistakes, break something and run it again: change not_a | not_b to not_a & not_b in de_morgan.sv, or swap
the outputs on pins 6 and 8 of the 4069 in de_morgan_chips.sv. The script
reports FAIL and the trace shows the lines marked WRONG.
Swapping the wires of NOT A and NOT B, on the other hand, is not a mistake: both laws treat A and B alike, and the testbench rightly passes.
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 addde_morgan.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.
The first BGM lab has the De Morgan exercise too. Keys 0 and 1 are A and B; LEDs 0 and 1 show the first pair,
LEDs 2 and 3 the second. To run the chip-level design instead, add de_morgan_chips.sv and the three
chip models and change the module name in the instance.
bgm_lab/lab_top.sv101 lines · show the codefile
// Exercise B2 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 de_morgan.sv. For the // chip-level design add de_morgan_chips.sv and cd4071.sv, cd4069.sv, // cd4081.sv instead, and change the module name below. // // Keys: key [0] a, key [1] b. The LEDs: led [0] not_a_and_b, led [1] // not_a_or_not_b, led [2] not_a_or_b, led [3] not_a_and_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 not_a_and_b; logic not_a_or_not_b; logic not_a_or_b; logic not_a_and_not_b; de_morgan i_de_morgan ( .a ( key [0] ), .b ( key [1] ), .not_a_and_b ( not_a_and_b ), .not_a_or_not_b ( not_a_or_not_b ), .not_a_or_b ( not_a_or_b ), .not_a_and_not_b ( not_a_and_not_b ) ); assign led = w_led' ({ not_a_and_not_b, not_a_or_b, not_a_or_not_b, not_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
hackathon/problems/1_gates_and_muxes. - Replace its
hackathon_top.svwithbgm_hackathon/hackathon_top.svand addde_morgan.sv. - Run
./03_synthesize_for_fpga.bashand./04_configure_fpga.bash.
Keys 0 and 1 are A and B. The screen shows six square lamps in a row: A and B in blue, then the two pairs in green, like LEDs 1–6 of the module on the breadboard.
bgm_hackathon/hackathon_top.sv87 lines · show the codefile
// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon // This module uses few parameterization and relaxed typing rules // Exercise B2 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 de_morgan.sv. // // Keys: key [0] a, key [1] b. The LEDs: led [0] not_a_and_b, led [1] // not_a_or_not_b, led [2] not_a_or_b, led [3] not_a_and_not_b. The screen // shows 6 lamps in a row: A, B, ~(A&B), ~A|~B, ~(A|B), ~A&~B; 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]; logic not_a_and_b; logic not_a_or_not_b; logic not_a_or_b; logic not_a_and_not_b; de_morgan i_de_morgan ( .a ( a ), .b ( b ), .not_a_and_b ( not_a_and_b ), .not_a_or_not_b ( not_a_or_not_b ), .not_a_or_b ( not_a_or_b ), .not_a_and_not_b ( not_a_and_not_b ) ); assign led = { 4'b0, not_a_and_not_b, not_a_or_b, not_a_or_not_b, not_a_and_b }; assign abcdefgh = '0; assign digit = '0; //------------------------------------------------------------------------ // 6 square lamps on the 480 x 272 screen, 40 pixels wide, 64 apart: // the inputs in blue, the outputs in green. Bright when 1, dim when 0. wire [5:0] lamp = { not_a_and_not_b, not_a_or_b, not_a_or_not_b, not_a_and_b, b, a }; always_comb begin red = 0; green = 0; blue = 0; for (int i = 0; i < 6; i ++) if (x >= 9' (60 + i * 64) & x < 9' (100 + 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.
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 and 1 are A and B. Segments a and b (top, top right) show the first pair, c and d (bottom right, bottom) the second: the pairs light up and go dark together.
tiny_tapeout/project.v54 lines · show the codefile
/* * Exercise B2 on Tiny Tapeout: De Morgan's laws, with the DIP switches as * inputs and the 7-segment display of the demo board as lamps. * * Add de_morgan.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 not_a_and_b; wire not_a_or_not_b; wire not_a_or_b; wire not_a_and_not_b; de_morgan i_de_morgan ( .a ( ui_in [0] ), .b ( ui_in [1] ), .not_a_and_b ( not_a_and_b ), .not_a_or_not_b ( not_a_or_not_b ), .not_a_or_b ( not_a_or_b ), .not_a_and_not_b ( not_a_and_not_b ) ); // 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) not_a_and_b, b (top right) not_a_or_not_b, c (bottom right) // not_a_or_b, d (bottom) not_a_and_not_b. assign uo_out = { 4'b0, not_a_and_not_b, not_a_or_b, not_a_or_not_b, not_a_and_b }; 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:2], 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>_de_morgan
source_files:
- "project.v"
- "de_morgan.sv"
15Going further
Tasks for students
- Three inputs. Write the laws for three inputs: NOT (A AND B AND C) = ? Check your answer with a 4023 (triple 3-input NAND) on one side and a 4069 and a 4075 (triple 3-input OR) on the other.
- NAND only. Build NOT, AND and OR from the four NAND gates of a 4011 only. Hint: a NAND with both inputs tied together is a NOT; for OR, use De Morgan's law.
- NOR only. The same with the four NOR gates of a 4001.
- Active-low buttons. Suppose the buttons gave 0 while pressed. Which gate lights an LED when at least one button is pressed? Which one when both are?
- In SystemVerilog: add an output
not_a_and_not_b_wrong = ~ a & b, a typical mistake, and see which rows of the testbench catch it.
Files of this exercise
- de_morgan.sv: the generic design (section 8)
- cd4071.sv, cd4069.sv and cd4081.sv: chip models (section 9)
- de_morgan_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 class with the 4081, the 4071 and the 4069 and why the laws matter for the FSM lessons. The chips are described in the Texas Instruments datasheets for the CD4081B, CD4071B and CD4069UB.