Learn & build · Electronics · Chapter 12
From gates to a computer
Chapter 4's MOSFET is a switch worked by a voltage. Two of them make a gate that answers yes/no questions about its inputs. A few gates add numbers; two gates looped together remember a bit; a row of those counts; and counters, adders and memory, wired together, are a computer. This chapter climbs that ladder one rung at a time.
1. Two transistors make a NOT
A CMOS inverter: a P-type MOSFET from the supply to the output, an N-type from the output to ground, both gates tied to the input. Input high: the N one turns on, the P one off, so the output is pulled to 0. Input low: the reverse. Exactly one of them is on at any time, so (apart from the instant of switching) no current flows from supply to ground. That's why a phone's billions of transistors don't melt it.
2. Gates and their truth tables
Every gate is completely described by a table: each combination of inputs, and the output. Pick a gate, predict its column by clicking the outputs, then check. (That's how you really learn them.)
Which gate outputs 1 exactly when its two inputs are different?
- OR
- NAND
- XOR
3. NAND can build everything
A surprising fact: with enough NAND gates you can build every other gate, and so a whole computer. NOT is a NAND with its inputs tied together. AND is a NAND followed by a NOT. OR is a NAND of the two inputs, each NOT-ed first (that's De Morgan's law: "not (not A and not B)" means "A or B"). Written in Boolean algebra's shorthand, where a bar over something means NOT, a dot means AND and a plus means OR:
\[ \overline{A} = \overline{A \cdot A}, \qquad A \cdot B = \overline{\overline{A \cdot B}}, \qquad A + B = \overline{\overline{A} \cdot \overline{B}} \]
How many NAND gates make an OR gate this way?
4. Adding numbers
In binary each place is worth twice the one to its right: 1, 2, 4, 8. Adding works like school addition: add a column, write the low bit, carry the high bit into the next column. One column is a full adder: sum = A XOR B XOR carry-in, carry-out = majority of the three. Chain four of them and you have a 4-bit adder. Click the bits.
🎯 Challenge: make every one of the four carries light up at once.
What decimal number is binary 1101?
Where it breaks: the carry has to ripple
The adder above looks instant, but a real gate isn't: its output changes a moment after its inputs, because its transistors have to charge the next gate's input, a small capacitor (chapter 2). Call one full adder's carry delay \(d\). The fourth column can't know its carry-in until the third has finished, which waits for the second, and so on: the carry ripples along the chain.
- 4 bits: the last sum is ready after about \(4d\).
- 64 bits, like a modern CPU's registers: about \(64d\), sixteen times longer, and the clock can't tick until it's done.
Your challenge above, every carry lit at once, is exactly this worst case. Real processors use carry-lookahead adders, which work out the carries for a group of columns side by side with extra gates, so the delay grows far more slowly than the number of bits.
5. A circuit that remembers
Loop two NAND gates into each other and something new happens: the circuit holds its state after you let go. Press Set and Q becomes 1 and stays 1; press Reset and it becomes 0 and stays. That's one bit of memory, an SR latch. Add a gate so it only listens when a clock ticks, and it's a flip-flop, the building block of registers and counters.
Why does the latch keep its value when both buttons are released?
Gates combine 1s and 0s by fixed rules; loop a gate's output back into its input and the circuit remembers.
6. Counting
A flip-flop wired to flip on every clock tick divides the frequency by two. Feed its output to a second one, which flips half as often, and so on. Read the outputs together and they count in binary. Chapter 9's watch divided 32,768 Hz by 2 fifteen times exactly this way.
7. A computer, very small
Put it together: a program counter (a counter) points at a line of memory; the line is an instruction; the instruction tells the ALU (adders and gates) what to do with the registers (flip-flops); then the counter moves on, or jumps. That loop, fetch, decode, execute, is every processor. Here's a toy one with two registers and five instructions, running a program that multiplies 6 by 7 by adding 6 seven times.
The program has no "multiply" instruction. How does it get 42?
- The ALU multiplies
- A loop: add 6 to A, count B down by 1, jump back while B isn't 0
- It looks it up in memory
Try gates in the circuit lab (Insert circuit → "Logic gate with two switches"). Next: microcontrollers, a whole computer like this one on a chip with its own memory and pins, and how it talks to other chips.
Previous: Light and sensors · All chapters: Learn & build.
Last updated