How a CPU Works
# How Computers Process Information: A Deep Dive into CPUs and Memory
## Introduction to the CPU
The Central Processing Unit (CPU), often referred to as the brain of a computer, is responsible for executing instructions that drive nearly every action your computer performs. Understanding how the CPU works provides insight into the inner workings of computers themselves. By removing the cover of the CPU and zooming in, we can see a complex network of wires carrying information around. This particular CPU is the 6502, famously used in iconic computers like the Apple II, Commodore 64, and the original Nintendo Entertainment System. A detailed simulation of this chip can be found online at [visual6502.org](https://visual6502.org).
## The Clock: TheheartbeatoftheCPU
Every CPU has a specific wire that turns on and off at a steady rate, keeping all components in sync. This wire is called the clock. In modern CPUs, this clock operates at several billion cycles per second, measured in gigahertz (giga meaning billion, hertz meaning cycles per second). While modern CPUs operate at lightning-fast speeds, the basic operations performed during each clock tick are surprisingly simple.
## Introducing the Scott CPU
The type of CPU we will examine in detail is called the Scott CPU. However, this chip does not physically exist; it is a conceptual design found in the book *But How Do It Know?* by John Scott. This book provides an excellent foundation for understanding how computers work without overwhelming technical jargon. If you're looking to deepen your knowledge of computer architecture, this resource is highly recommended.
## Exploring the Physical Components
When we flip the CPU over, we notice a series of pins that allow the CPU to interact with other components. These pins connect the CPU to the motherboard, which serves as the central hub for all components within the computer. The motherboard provides slots for RAM (Random Access Memory), which stores data being processed by the CPU.
## Understanding RAM
RAM consists of a list of addresses, each containing a piece of data. Normally, the CPU processes this data sequentially; however, it can access any address randomly when instructed to do so. When a program starts, the CPU requests an address from RAM to begin retrieving the program. This address is represented as a series of ones and zeros, corresponding to on and off wires.
The process involves three key steps:
1. The CPU sends an address to RAM.
2. It turns on the "enable" wire, prompting RAM to send back the data at that address.
3. The CPU processes this data before requesting the next piece of information.
If the CPU needs to save data, it sends an address along with new data and activates the "set" wire, instructing RAM to overwrite the existing data at that address.
## Decoding Data in RAM
The data stored in RAM is a mix of instructions, numbers, and addresses. Instructions guide the CPU on what operations to perform (e.g., load, add, store). Numbers are processed mathematically, while addresses direct the CPU to specific locations or external devices (e.g., printers or monitors). Letters are also stored as binary representations according to character codes, which define how each letter is displayed.
## The Instruction Set of the Scott CPU
The Scott CPU's instruction set includes fundamental commands like "load," "add," "store," and "compare." These instructions allow the CPU to manipulate data. For example:
- A load instruction retrieves a number from RAM.
- An add instruction combines two numbers.
- A store instruction saves the result back into RAM.
Conditional jumps are another critical feature of this instruction set. The "jump if" instruction evaluates conditions (e.g., whether two numbers are equal) before deciding to execute specific code paths. This capability is essential for creating programs with branching logic, such as a guessing game that provides feedback based on user input.
## Inside the CPU: Control Unit and Arithmetic Logic Unit
The CPU's control unit acts like a manager, interpreting instructions from RAM and directing other components to perform tasks. The Arithmetic Logic Unit (ALU) handles mathematical operations, receiving inputs from registers—temporary storage units within the CPU.
For instance:
- When processing an add instruction, the ALU receives two numbers from registers.
- It performs the addition and outputs the result.
- Depending on the operation, this output might be stored in another register or sent to RAM.
## Flags and Registers
The ALU also sets flags based on its operations. These flags provide critical information about the results of operations (e.g., whether two numbers are equal). The control unit uses these flags to make decisions, such as whether to jump to a new address in RAM.
Registers play multiple roles within the CPU:
- Temporary registers store intermediate values during calculations.
- Instruction and flags registers hold the current instruction and status information.
- Memory address and data registers interact with RAM and external devices.
## Bus System: Connecting Components
The bus is a group of wires connecting different components within the computer. While it allows for efficient communication, it can only carry one piece of data at a time. This limitation necessitates careful management when moving data between components like the ALU, registers, and RAM.
## External Devices and Port Addresses
Ports on the CPU allow interaction with external devices like monitors and keyboards. These ports have addresses used in "in" and "out" instructions to send or receive data. For example:
- An "out" instruction sends a message (e.g., "G") to the monitor.
- An "in" instruction retrieves user input from the keyboard.
## The Hard Drive: Permanent Storage
While RAM is essential for processing, it loses all data when power is turned off. To preserve information, computers use hard drives. These devices store data magnetically on spinning disks, which must be transferred to RAM before processing.
## Conclusion: From Basics to Modern CPUs
The Scott CPU provides a foundational understanding of computer architecture. While modern CPUs are far more complex, they still rely on the same principles—executing instructions, manipulating data, and communicating via buses. Resources like John Scott's book and online simulations offer valuable insights into these mechanisms.
By exploring the inner workings of the CPU and its interactions with RAM and external devices, we gain a deeper appreciation for the intricate dance of ones and zeros that powers our digital world.