How to build a CPU from scratch
The exact build order, from one transistor to a processor that runs a program, all in your browser
You build a CPU from scratch by composing one simple part over and over: start with a single transistor (a voltage-controlled switch), wire switches into logic gates, gates into an adder and a memory cell, those into registers and an arithmetic unit, and finally add a control unit that steps a clock through fetch, decode, and execute. Each layer is small enough to build and test on its own, and every layer is just the one below it arranged differently, so no single step is hard. This guide walks the whole build order, and you can build every stage yourself in the browser with nothing to install.
Step 0: the one part you need, a transistor
Everything starts with the transistor: a switch you turn on and off with a voltage instead of a finger. There are two kinds, and you use both. An NMOS conducts when its control input (the gate) is high (
1); a PMOS conducts when its gate is low (0). That is the entire component. A CPU is millions of these switches, but you only ever work with a handful at a time, because each layer you build becomes a single reusable block for the next. See one switch turn on in the MOSFET simulator before you wire any up.The reason a switch is enough is that a computer only ever manipulates two values,
0 and 1, so every question a processor answers reduces to 'should current flow here or not'. Keeping that in mind, every step below is just switches deciding the path of current.Step 1: build logic gates from transistors
Pair an NMOS pull-down network with a complementary PMOS pull-up network and you get a CMOS logic gate whose output is always a clean
1 or 0. The smallest is the inverter (NOT), one transistor of each type. Add a second transistor to each network and you have the two workhorse gates: NAND (output 0 only when both inputs are 1) and NOR (output 1 only when both inputs are 0). Build and probe them from raw transistors in the logic gate simulator.You do not actually need every gate. NAND is universal: with enough NANDs you can build any other gate and therefore any logic function, which is why real chips lean on it. From NAND and NOT you get AND and OR (each is the inverting gate followed by an inverter), and the odd one out, XOR (output
1 when the inputs differ), from a small cluster of NANDs. Save each gate as a reusable part and you never have to think about its transistors again.Step 2: make the gates do arithmetic
The first useful thing gates compute is addition. Adding two bits, the sum is
A XOR B and the carry is A AND B: those two gates together are a half adder. Fold in a carry coming from the column to the right and you get the full adder, which is the cell you chain to add real numbers. Step through a binary sum, watching the carry ripple, in the interactive adder.Wire eight full adders in a row, each carry feeding the next, and you have an 8-bit adder that sums two bytes in one pass. With one trick, two's complement, the same adder also subtracts, so a single block covers both. That adder is the arithmetic core the rest of the machine feeds.
Step 3: build memory so the machine can remember
Everything so far forgets its answer the instant the inputs change. To remember, you use feedback: route an output back into an input and the circuit can hold a value. Cross-couple two NOR gates and you get an SR latch, the first one-bit memory. Add a control input for when it may change and you have a D latch; make it capture on a clock edge and you have a D flip-flop, the standard storage cell. Watch a bit get held through feedback in the memory simulator.
One flip-flop stores one bit. Line up eight, sharing a clock and a load line, and you have an 8-bit register: a byte the CPU can hold and update on command. Registers are the machine's short-term memory, where the values it is working on right now live.
Step 4: wire the datapath so bytes can move
Now you have registers and an adder that need to share wires. A bus is a bundle of wires many blocks read and write, but two outputs must never drive it at once, so shared outputs use tri-state buffers that can also let go of the wire entirely (the
Z state). To choose which source talks, a multiplexer picks one of several inputs, and a decoder turns a register's address into the single enable line that selects it.Bundle the adder with a couple of logic operations behind one function-select input and you have the ALU, the calculator at the center of the CPU: set its lines one way and it adds, another and it subtracts or ANDs, and it reports a zero flag the control logic will test. Registers, buses, muxes, a decoder, and the ALU wired together are the datapath, the plumbing every instruction flows through.
Step 5: add memory, instructions, and a control unit
A program needs more storage than a few registers. RAM is a grid of cells with an address bus to pick one and a data bus to read or write it, and a counter (a register that increments each clock) becomes the program counter that walks addresses in order. The bytes in RAM are machine code: each instruction is an opcode (LOAD, ADD, STORE, HALT) plus an operand, parked in the instruction register once fetched.
The last block is the conductor. The control unit asserts every load, enable, and function line in the right order, driven by the clock, running the fetch, decode, execute loop: fetch the instruction the counter points at, decode its opcode into control lines, execute by routing data through the datapath, then advance the counter. Put the datapath and control unit together and you have a working CPU. See the loop run one step at a time in the CPU simulator, then follow a real LOAD/ADD/STORE/HALT program cycle by cycle in the program trace.
You just built a computer
That is the whole build, and every rung is a small idea stacked on the one under it: switch, gate, adder, latch, register, bus, ALU, RAM, control. Nothing in the finished processor is smarter than a transistor deciding whether current flows; the intelligence is entirely in the arrangement, which is exactly why you can build the arrangement yourself.
The fastest way to actually do it is the free course, which hands you each block as a level and verifies your build against the real simulator before you move on, so you always know your NAND, your adder, and your register truly work before you compose them into a CPU. Or open the lab and start placing transistors right now.