How CPUs Work
How CPUs Work: Comprehensive Study Notes
Introduction: The Central Processing Unit
The Central Processing Unit (CPU) is the brain of every desktop computer, smartphone, gaming console, and laptop. It is built from billions of nanoscopic transistors connected by dozens of layers of wires. CPUs range from early models with a few thousand transistors to modern chips containing billions.
Historical vs. Modern Processors
Apple IIe (1983): Used the 6502 processor, built from 4,528 transistors, capable of ~430,000 calculations per second. It powered early computers and consoles like the NES, Commodore 64, and Atari.
MacBook Pro M1 (modern): Built from 16 billion transistors, capable of ~3 trillion calculations per second. Enables expansive 3D worlds and immersive graphics.
Despite 45 years of advancement, the underlying architectural principles remain remarkably similar. This shared "technological DNA" is not transistors or logic gates, but a fundamental operational design common to all processors — from the 6502 to the M1, and even GPUs, system-on-chips (SoCs), and AI chips.
Key Insight: The Oregon Trail from 50 years ago and modern AI algorithms run on processors with the same technological DNA, though modern chips are 10 billion times more powerful.
The Core Technological DNA: Fetch-Decode-Execute Cycle
The foundational operational principle of every processor is the Fetch-Decode-Execute cycle. This cycle repeats continuously from boot to shutdown, regulated by the system clock.
Fetch: Retrieve the next instruction from memory.
Decode: Interpret the instruction’s binary code and generate control signals.
Execute: Carry out the instruction using the Arithmetic Logic Unit (ALU) and other components.
Clock speed paces each step:
6502: 1 MHz (1 million ticks/second) → each step ~1 microsecond
M1: 3.2 GHz (3.2 billion ticks/second) → each step ~0.3 nanoseconds
Data width determines the size of numbers processed:
6502: 8-bit (registers and ALU handle 8 bits at a time)
M1: 64-bit (much larger numbers)
Pipelining (modern processors) allows multiple instructions to be in different stages simultaneously — while one instruction is being executed, the next is decoded, and the one after that is fetched.
Analogy: Understanding CPU Components
To visualize how the CPU works, imagine a workshop:
Analogy Component | Real CPU Component | Function |
|---|---|---|
Bookshelves | SSD / Hard Drive | Long-term, semi-permanent storage for programs and data |
Cart | DRAM (main memory) | Temporary, limited-capacity working memory |
Table | CPU core + Cache | Very limited space for immediate work; only one book open at a time (cache) |
Single sheet of paper | Registers | Stores values/numbers actively being used (e.g., general-purpose registers) |
Calculator | ALU (Arithmetic Logic Unit) | Performs arithmetic and logic operations on binary numbers |
Controller (robot) | Control Unit | Reads books (instructions), moves data between locations, operates the calculator |
Books | Programs (machine code) | Contain instructions (recipe steps) and data (addresses with values) |
Key Memory Hierarchy
SSD (Bookshelves): Large, permanent storage — holds all files and programs even when powered off.
DRAM (Cart): Temporary, much faster than SSD — holds currently running programs and data.
Cache (Table space): Very fast, very small — holds the most recently used instructions and data for quick access.
Registers (Paper): Fastest, smallest — holds a handful of values being actively computed.
Note: When the computer turns off, DRAM and cache are empty. On power-on, data is copied from SSD to DRAM to cache as needed.
Components in Detail
Registers
Registers are the fastest memory locations inside the CPU. Key types:
General-purpose registers (e.g., R0, R1, R2, R3): Hold values for arithmetic or logic operations.
Program Counter (PC): Also called Instruction Address Register or Instruction Pointer. Stores the address of the next instruction to fetch.
Current Instruction Register (CIR): Holds the fetched instruction during decode and execute.
Accumulator: The display/output of the ALU — where results of ALU operations are stored.
Arithmetic Logic Unit (ALU)
The ALU is a specialized calculator that works with binary (0 and 1). Its basic functions include:
Arithmetic: Add, subtract, multiply, increment (+1), decrement (-1)
Bit shifts: Moving bits left or right — in binary, shifting left multiplies by 2 (like adding a zero in decimal multiplies by 10).
Logic operations: AND, OR, XOR (exclusive OR) on two binary numbers, performed bitwise.
Example of AND:
Input1: 1100 Input2: 1010 Output: 1000 (bitwise AND)
Comparisons: Test if two numbers are equal, greater than, less than. Results set comparison flags (equal flag up, less-than flag down, etc.).
Instruction Decoder
A complex circuit that reads the binary instruction from the CIR and:
Interprets what the machine code means
Produces control signals to route data and activate the correct ALU functions
The instruction decoder is one of the most intricate parts of the CPU. A simplified example: an ADD instruction might specify "use ALU", "ADD function", and "take values from register 0 and register 1".
How Programs Execute: Instructions and Flow
A program consists of a sequence of instructions and data. Instructions are like recipe steps; data is like the ingredients.
Common Instruction Types
Load: Copy a value from a memory address into a register.
Store: Save the value in the accumulator (or a register) back to a memory address.
Increment/Decrement: Add 1 or subtract 1 from a register value using the ALU.
Jump: Set the Program Counter to a new value, skipping over instructions (like a goto).
Conditional Branch: Check a comparison flag (e.g., equal, greater than) and jump only if the condition is true — used for IF statements and loops.
Example: FOR Loop Execution
A loop that repeats 4 times involves these instructions:
Set i = 0 (store initial value)
Load i from memory into a register
Compare i with 4 using the ALU (sets flags)
Branch if greater or equal (BGE): if flag is set, jump to exit address (PC = 23)
Execute loop body instructions
Increment i (add 1)
Jump back to the load instruction (PC = 6)
The loop repeats until i ≥ 4, then exits.
Key Point: The Program Counter tracks execution order. Jump instructions alter the PC directly; conditional branches alter it only if a condition is true.
The Fetch-Decode-Execute Cycle — Full Explanation
Step 1: Fetch
The controller reads the Program Counter (PC).
It uses the PC value to locate the instruction in memory (from the "book").
The instruction is copied into the Current Instruction Register (CIR).
Simultaneously, the PC is incremented by 1 (or by the instruction length) to point to the next instruction.
Step 2: Decode
The binary code in the CIR is fed into the instruction decoder.
Complex logic gates (built from thousands of transistors) interpret the instruction and generate control signals.
Control signals determine: which ALU function to use, which registers to read/write, whether to access memory, etc.
Step 3: Execute
Control signals from the decoder and precise timing signals coordinate the execution.
Timing signals account for electrical propagation delays — ensuring signals stabilize before reading results.
Example: For an ADD instruction:
Route value from register 0 to ALU input A
Route value from register 1 to ALU input B
Activate ALU ADD function
After a timing delay, store the result into the accumulator
After Execute, the cycle repeats with the new PC value (which was incremented during Fetch, or jumped).
Optional Steps: Memory and Writeback
Some architectures include two more steps:
Memory: Transferring data between cache, DRAM, and SSD (e.g., loading data from DRAM into cache).
Writeback: Saving a computed value back from a register or accumulator into memory (cache or DRAM).
These can take many clock cycles — significantly longer than the basic fetch-decode-execute.
Instruction Set Architectures: RISC vs. CISC
Every processor family has a specific instruction set — the complete list of instructions it can execute.
RISC (Reduced Instruction Set Computer)
Example: ArmV8.4 (used in M1 and most smartphones)
354 instructions — each is simple, fixed-length, and executes in one clock cycle most of the time.
Simple instruction decoder — easier to pipeline and more energy efficient.
Used in all smartphones for their power efficiency.
Example: A simple Snake game written in C++ compiles to 676 assembly instructions for RISC.
CISC (Complex Instruction Set Computer)
Example: x86/x86-64 (used in Intel and AMD processors)
Thousands of instructions — some are complex, multi-step operations.
Complex instruction decoder — must handle variable-length instructions.
Execution time varies — some instructions take multiple clock cycles.
Allows fewer instructions for the same program (Snake compiles to 560 instructions for CISC).
Trade-off: RISC is simpler, faster on single instructions, and more power-efficient. CISC can pack more work into each instruction, potentially using less memory for code, but with more complex hardware.
The Role of the Compiler
A compiler is software that translates high-level code (e.g., C++, Python) into machine code. It:
Selects appropriate instructions from the target ISA (RISC or CISC).
Optimizes the sequence of instructions for speed or size.
Produces assembly language (semi-readable) which is then assembled into binary machine code.
Modern CPU Architecture (Apple M1 Example)
The M1 chip is a System-on-a-Chip (SoC) — it integrates multiple processors on one die:
4 high-performance CPU cores + 4 energy-efficient CPU cores
Graphics Processing Unit (GPU)
Neural Processing Unit (NPU) for AI and machine learning
Media Engine for video encode/decode
Cache memory (e.g., 64KB L1 data cache + 64KB L1 instruction cache per core)
Single Core Architecture — Expanded
Compared to the simple analogy, a modern core includes:
Pipelining: Queue of up to 8 instructions per clock cycle — fetch, decode, execute overlapped.
Branch Predictor: Specialized logic that guesses the outcome of conditional branches to keep the pipeline full (avoids stalls by predicting jump directions).
32 general-purpose registers (up from 4 in the simple model).
Separate ALU calculators: The single "calculator" is broken into 8 smaller specialized units — each handling specific operations (e.g., integer add, bit shift, comparison) to allow parallel execution.
Load/Store Unit: Dedicated to memory access, handling loads and stores separately from arithmetic.
Memory Cache Hierarchy
Cache levels (L1, L2, L3) provide increasingly larger but slower storage close to the CPU:
L1 (Level 1): ~64KB per core — fastest, split into instruction and data caches.
L2 (Level 2): Hundreds of KB to a few MB — shared between some cores.
L3 (Level 3): Several MB — shared across all cores, slower but still much faster than DRAM.
Why No Cache in Early CPUs?
The 6502 had no cache because 1980s DRAM was nearly as fast as the CPU core. Modern CPUs operate at GHz speeds, while DRAM is slower by orders of magnitude — making caches essential.
Alternative Architectures
ASICs (Application-Specific Integrated Circuits)
No fetch-decode-execute cycle — they perform fixed operations by flowing data through a hardwired pattern of logic gates.
Highly optimized for one task (e.g., Bitcoin mining) but completely inflexible.
Example: Specialized chips in Bitcoin miners.
FPGAs (Field-Programmable Gate Arrays)
Reconfigurable logic — can be programmed to implement any digital circuit after manufacturing.
Used in automotive computers, cameras, prototyping.
More flexible than ASICs but less optimized.
Quantum Computers
Completely different paradigm based on qubits and quantum circuits (not covered in detail here).
GPUs and SIMT
A GPU (Graphics Processing Unit) uses a different execution model:
Contains thousands of CUDA cores (each similar in complexity to the 6502 CPU).
SIMT (Single Instruction, Multiple Threads): One instruction is fetched and decoded once, then distributed to many cores. Each core executes that same instruction on different data simultaneously.
GPUs often use 32-bit ALUs and are optimized for parallel workloads (graphics, scientific computing, AI).
Historical Context: The 6502 Architecture
The Apple IIe's 6502 processor had:
No cache (DRAM as fast as the CPU)
Simple architecture: instruction decoder, ALU, program counter, current instruction register, a few general-purpose registers
56 instructions total
Clock speed: 1 MHz (later models faster)
Data width: 8-bit
Its internal diagram mapped closely to the simple analogy — a "table" even smaller than the modern one.
Summary
All processors — from the 6502 to the M1 — share the same fundamental operational DNA: the Fetch-Decode-Execute cycle. This cycle, regulated by the system clock, continuously fetches instructions from memory, decodes them into control signals, and executes them using the ALU and registers. Modern advancements like pipelining, branch prediction, larger caches, and higher clock speeds have multiplied performance billions of times, but the core principle remains unchanged. Understanding this cycle is essential to grasping how any computing device works, from a vintage game console to a cutting-edge AI server.

Ask anything
We've read this whole study set, so questions about it get the best answers.