Skip to content

Computer Architecture

Build a simple CPU from logic gates: a countdown computer

You can build a working computer from about a dozen gates and four flip-flops. This one has a 2-bit program counter, a decoder for four instructions, a 2-bit accumulator and a ZERO flag, and it runs a fixed program, LDI 3, DEC, JNZ 1, HALT: it counts 3, 2, 1, 0 and stops. Change one number and the same hardware runs a different number of laps.

The parts

  • ADDR, the program counter: two flip-flops that step through addresses 0 to 3.
  • The decoder: one line per address. Because the program is fixed, the ROM and the decoder fold into one, and each address's line is that instruction's control line.
  • ACC, the accumulator: two flip-flops wired as a loadable down counter.
  • ZERO: a NOR of the accumulator's two bits.
  • DONE: lit at HALT.

Step 1: decode

LDI = Q0 NOR Q1 · DEC = Q0 AND NOT Q1 · JNZ = NOT Q0 AND Q1 · HALT = NOT Q0 NOR NOT Q1

LDI's address is 00, so a NOR of both address bits spots it. HALT's is 11, so a NOR of both Q̄ pins does.

Step 2: the accumulator

It's a countdown counter with DEC as its GO and LDI as its LOAD. A tick with LDI lit loads 3, a tick with DEC lit takes one away, and every other tick it holds. This time ZERO doesn't stop it: the program does.

Step 3: close the loop

After address 2 the counter would go on to 3. It should jump back to 1 unless ZERO is set, so the 2s bit of the next address is 1 only when the loop falls through: one AND of JNZ and ZERO. HALT points back at its own address, so once there the machine stays at 3 with DONE lit.

Live experiment

Run the countdown computer

The finished machine: tick it through LDI 3, DEC, JNZ 1, HALT and watch ADDR loop 1, 2, 1, 2 while ACC counts 3, 2, 1, 0, then stop at HALT with DONE lit. Pick LDI 1, 2 or 3: the same hardware runs a different number of laps.

Try it yourself: free lesson

“Project: Countdown computer” is the project that ends Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.

Tick by tick

tick 1: LDI, ACC = 3 · ticks 2 to 7: DEC, JNZ three times, ACC = 2, 1, 0 · then HALT

LDI takes one tick and each lap of DEC and JNZ takes two, so three laps end on tick 7, when the last JNZ sees ZERO and falls through. Load 2 instead and DEC runs twice; load 1 and DONE lights after three ticks.

What makes it a computer

The same hardware runs a different program when the data changes: LDI 2 runs two laps without a single wire moving. Real processors add a ROM so the whole program can change, wider registers, an ALU with many operations and more flags, but the cycle is the same: decode the instruction at the program counter, change some registers, and choose the next address.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Project: Countdown computer” is the project that ends Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3JNZ 1 at address 2 jumps when…
    Show the answer

    Answer: The counter is at 2 and ZERO is 0. JUMP = address 2 AND NOT ZERO.

    • Why not “The counter is at 1”? The instruction lives at address 2, so it can only act when the counter is there.
    • Why not “Every tick”? It's a conditional jump: on most ticks the counter isn't at 2, and even there it needs ZERO at 0.
    • Why not “ZERO is 1”? JNZ means jump if not zero. With ZERO at 1 it falls through to the next address.
  2. Question 2 of 3Writing to register B leaves register A…
    Show the answer

    Answer: Holding its bit. A's enable is 0, so its picker feeds its own Q back.

    • Why not “Unknown”? A's value is known: with its enable at 0, it keeps exactly what it had.
    • Why not “Copied from B”? No path runs from B to A. A takes either DATA or its own Q.
    • Why not “Cleared”? Nothing clears A. Its enable is 0, so it reloads its own bit.
  3. Question 3 of 3Counting down from 2 (10), the 2s bit flips because…
    Show the answer

    Answer: The 1s bit was 0, so it has to borrow. Down-counting flips the 2s bit when the 1s bit is 0.

    • Why not “The 1s bit was 1”? When the 1s bit is 1, taking one away just clears it: 11 becomes 10, and the 2s bit stays.
    • Why not “ZERO is lit”? ZERO is lit only at 0. At 2 it's off, and the borrow comes from the 1s bit being 0.
    • Why not “LOAD is on”? LOAD puts a fixed number in. Counting down from 2 is an ordinary tick with LOAD off.
Keep practising: free lesson

“Project: Countdown computer” is the project that ends Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 20 before it first.

Questions people ask

What are the basic parts of a CPU?

A program counter that holds the address of the next instruction, a decoder that turns each instruction into control signals, registers such as an accumulator to hold numbers, an ALU to calculate, and flags that let jumps make decisions. This build has a tiny version of each.

Can you build a CPU from logic gates?

Yes. Every processor is made of gates and flip-flops, and hobbyists build working 8-bit computers from 74-series logic chips on breadboards. A 2-bit machine like this one needs four flip-flops and about a dozen gates.

What is an accumulator?

A register that holds the running result. Instructions load it, add to it or count it down, and the answer stays there for the next instruction. Early processors such as the 6502 were built around one.

Every topic