Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 57 ยท Toward a Processor

From Gates to a Tiny CPU

See how registers, an ALU and a control unit form a CPU. Learn the program counter, instruction register and fetch-decode-execute cycle with an 8-instruction toy CPU.

A processor can look like magic: billions of operations per second, running software that no one wired by hand. But a CPU is built from the same parts you have already met. Registers store values, an ALU calculates, multiplexers route data, and a state machine decides what happens next. Put those pieces together and add a memory that holds both the program and its data, and you have a computer.

This lesson designs the smallest useful CPU you can still call a CPU. It has 32 bytes of memory, one working register and just eight instructions. You will meet the program counter, the instruction register and the control unit, follow the fetch-decode-execute cycle, and run a real program in a Python model of the machine.

This is a teaching design, not a copy of any real processor. Real CPUs have more registers, wider instructions, pipelines and caches, but they all do the same three things in a loop: fetch an instruction, decode it, execute it.

The parts of a CPU

The datapath and its controller

Program counter Holds the address of the next instruction.
Instruction register Holds the instruction being run.
ALU and accumulator Calculates and keeps the working value.
Control unit Turns an instruction into control signals.

Registers store, the ALU calculates, the control unit decides.

1 / 2

Here is what each part does in our design:

  • Memory has 32 locations of 8 bits each. Addresses are therefore 5 bits (0 to 31). Instructions and data live in the same memory.
  • The program counter (PC) is a 5-bit register. After each fetch it moves to the next address, unless a jump changes it.
  • The instruction register (IR) captures the byte just fetched, so the instruction stays stable while it executes.
  • The accumulator (ACC) is the one general register. Arithmetic uses it as the first operand, and results go back into it.
  • The zero flag (Z) remembers whether the last load or arithmetic result was zero, so a later instruction can branch on it.
  • The control unit looks at the instruction's opcode and sets all the control lines: which register loads, whether memory is read or written, which ALU operation runs.

The instruction set

Every instruction is one byte. The top 3 bits are the opcode, and the low 5 bits are an operand, usually a memory address.

TEXT
bit:   7 6 5 | 4 3 2 1 0
       opcode | operand (address)

Three bits allow exactly eight opcodes, and we use all of them:

Opcode Name What it does
000 LDA addr ACC = memory[addr]
001 STA addr memory[addr] = ACC
010 ADD addr ACC = ACC + memory[addr]
011 SUB addr ACC = ACC - memory[addr]
100 JMP addr PC = addr
101 JZ addr if Z = 1 then PC = addr
110 OUT send ACC to the output
111 HLT stop the clock

The words in the second column are assembly language: names humans can read. An assembler turns each line into the matching byte. The first program does that translation in both directions.

Python Assembling and disassembling instructions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The shift op << 5 slides the opcode into the top three bits, and the mask & 0b11111 keeps the low five. In hardware the same split is free: the top three wires of the IR go to the control unit and the bottom five go to the address bus.

The control unit

The control unit is a combinational circuit. Its input is the 3-bit opcode and its outputs are the control signals that steer the datapath. You can build it from a decoder plus a few OR gates, or simply as a small ROM with 8 rows. The table below is that ROM.

Python The control unit as a lookup table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The JZ row has pc = Z: the PC loads only if the zero flag is 1. This single wire is where a program gains the ability to decide something. Without conditional jumps, a machine could only run straight through its instructions. With them, it can loop and make choices.

Fetch, decode, execute

The CPU repeats three steps, one after another, driven by the clock:

  1. Fetch. Put the PC on the address bus, read the byte from memory into the IR, and add 1 to the PC.
  2. Decode. The control unit reads the opcode in the IR and raises the right control signals.
  3. Execute. The datapath does the work: a memory read, an ALU operation, a register load, or a PC change.

In a real implementation each step takes one or more clock cycles. A simple state machine (the same kind you can design with the techniques from Finite State Machines) counts through the steps. In the Python model below, one step() call performs a whole instruction.

Python Running a short program, one instruction at a time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the PC printed after each line is already the next address, because fetch increments it before the instruction runs. The program loads 7, adds 5, stores 12 into memory address 12, sends it to the output, and halts. The program and its data share the same memory. This is the stored-program idea (often called the von Neumann architecture): code is just numbers in memory, and a program can even be changed like data.

A program with a loop

A CPU becomes interesting when it repeats work. This program adds the numbers 5, 4, 3, 2, 1. It keeps a counter at address 20, a running sum at address 21, and the constant 1 at address 22.

TEXT
 0  LDA 21     ; ACC = sum
 1  ADD 20     ; ACC = sum + counter
 2  STA 21     ; sum = ACC
 3  LDA 20     ; ACC = counter
 4  SUB 22     ; ACC = counter - 1   (sets Z when it reaches 0)
 5  STA 20     ; counter = ACC
 6  JZ 8       ; counter == 0? leave the loop
 7  JMP 0      ; otherwise repeat
 8  LDA 21     ; ACC = sum
 9  OUT        ; show it
10  HLT

The machine has no ADD 1 instruction with a constant value, so the 1 is kept in memory at address 22, and SUB 22 reads it from there. This is a common limitation of very small CPUs. The STA instruction does not change the zero flag, so at instruction 6 the flag still describes the result of the SUB at instruction 4.

Python Summing 5+4+3+2+1 with a loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The loop body has 8 instructions and runs 5 times, but the last pass skips the JMP and takes the exit. Together with the three instructions after the loop, that comes to 42 executed instructions. The safety counter in the Python loop is a good habit: a bug in the program, such as a missing exit, would otherwise spin forever, just as it would on the real machine.

What real CPUs add

Our toy CPU does everything a computer needs in principle, but it is slow and cramped. Real processors extend the same design:

  • More registers, so the program does not constantly go back to memory.
  • Wider instructions and addresses, usually with several operands per instruction.
  • Subroutine calls with a stack, so code can be reused.
  • Interrupts, which let hardware events pause the program.
  • Pipelining: fetch the next instruction while still executing the current one.
  • Caches, small fast memories that hide the slowness of main memory.

Each of these is another application of the building blocks from this course: registers, multiplexers, adders, decoders and state machines.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the program counter hold?

    The PC points at the next instruction in memory. The fetch step reads from it and then increments it, unless a jump overwrites it.
  2. 2. In this toy CPU, how is an instruction byte divided?

    Three opcode bits give eight instructions, and five operand bits address 32 memory locations.
  3. 3. What is the job of the control unit?

    The control unit is a decoder-like circuit. It reads the opcode and raises the signals that load registers, read or write memory and select the ALU operation.
  4. 4. Which instruction gives the CPU the ability to make decisions?

    A conditional jump loads the PC only when the flag is set. That is what allows loops and branches instead of straight-line execution.
  5. 5. Why does the PC already point to the next instruction while the current one executes?

    During fetch the instruction is copied to the IR and the PC is incremented immediately, so the execute step works from the IR while the PC is already advanced.

Summary

  • A CPU is registers, an ALU, memory and a control unit, all built from gates and flip-flops.
  • The program counter selects the next instruction, the instruction register holds the current one, and the accumulator holds the working value.
  • Our toy CPU has 8-bit instructions: a 3-bit opcode and a 5-bit address, which gives eight instructions and 32 bytes of memory.
  • The control unit is a lookup table from opcode to control signals, and a conditional jump gives the machine decisions and loops.
  • Every instruction goes through fetch, decode and execute, and program and data share the same memory.
  • Real processors keep this core idea and add registers, pipelines, caches and interrupts.

Next: Project: Seven-Segment Display Decoder.