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.

In plain words: in digital circuits a voltage near the supply means 1 and near 0 V means 0, and nothing in between counts. Gates are tiny circuits that combine 1s and 0s by fixed rules. Everything a computer does is built from a handful of such rules, repeated billions of times.

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.

← click the input

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?

eXclusive OR: one or the other, not both. It's also binary addition of one bit without the carry: 0+0=0, 0+1=1, 1+1=0 (carry 1).

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?

Two to invert A and B, one to NAND the results: 3.

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.

AB

🎯 Challenge: make every one of the four carries light up at once.

What decimal number is binary 1101?

\(8 + 4 + 0 + 1 = 13\).
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.

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?

Each gate's output is the other's input. With Q = 1, the lower gate sees 1 and outputs Q̄ = 0, which keeps the upper gate's output at 1: they hold each other up. The loop is stable in two states, and whichever one you last pushed it into is where it stays. That feedback (chapter 9's idea, with a decision instead of an oscillation) is what memory is.

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?

Seven trips round the loop. Early computers really did multiply this way (or with shift-and-add, which is faster); modern CPUs have multiplier circuits built from many adders.

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