Introduction
The 6502 CPU program has been a great inspiration for understanding the foundations of computer science. It’s fascinating how basic boolean functions and transistors can form such a complex and beautiful system. However, even the 6502 CPU, with its 150+ instructions, can be overwhelming for those trying to understand the fundamental principles of computing.
The Importance of Minimal Viable Products
When learning complex systems, it’s crucial to start with a minimal viable product (MVP) - understanding the most essential components that make a program run. This approach led me to explore foundational theories and historical concepts in computing.
Turing’s Influence
Alan Turing’s computational theory provides a perfect starting point. His concept of a universal computing machine demonstrates that any computable problem can be solved using a simple machine with:
- An infinite length tape
- A read/write head
- Basic operations (read, write, logic, and arithmetic)
- Position control
While we can’t implement an infinite tape, we can create a system that operates in a loop, simulating this fundamental concept.
Extension reading on Turing’s theory:
We have to compute the number, then the number should be computable. It seems the word “computable” is vague, so he gave a definition: A real number is computable if a mechanical procedure can print its digits one-by-one in finite time per digit. Replace “print a digit” with “return a value” and you have a computable (partial) function.
The probe can be seen as a finite-states machine, which react only on limited states to do some “computation”, which can be defined by limited and concrete steps.
Encode a TM’s state table as an integer (its ⟨code⟩).
- A Universal TM U takes (⟨M⟩, x) and simulates M running on input x. Legacy.
- “Program = data” → the von Neumann architecture, data can be control message(finite state) or information;
Figure 1: A simplified representation of a Turing Machine
Minimal Instruction Set Design
Based on these principles, a minimal CPU needs only four types of instructions:
- JMP (Jump) - For program flow control
- Logic - For basic boolean operations
- ADD/SUB - For arithmetic operations (multiplication and division can be simulated)
- HALT - To stop program execution
CPU Specification
After careful consideration and consultation, here’s the design for our minimal CPU:
Memory and Registers
- RAM: 64KB (65536 bytes) with 16-bit addressing
- Registers: 4 general-purpose 8-bit registers (R0-R3)
- Program Counter (PC): 16-bit register for instruction fetching
Instruction Set
| Opcode | Instruction | Description |
|---|---|---|
| 0x00 | HALT | Stops program execution |
| 0x01 | LOAD | Loads data from memory into register |
| 0x02 | STORE | Stores register value into memory |
| 0x03 | ADD | Adds two register values |
| 0x04 | SUB | Subtracts two register values |
| 0x05 | JNZ | Jump if register is not zero |
CPU Operation Cycle
The CPU follows a simple fetch-execute cycle:
- Fetch instruction from memory at PC
- Decode instruction
- Execute instruction
- Update PC
- Repeat until HALT
Look how cpu borrow the concept from Turing Machine:
| Component | Role | Modern analogue |
|---|---|---|
| Unbounded tape | Program + data store | RAM + disk |
| Read/write head | Pointer & ALU | CPU register |
| Finite state table | Control logic | Instruction set |
This minimal setup provides the foundation for basic logic and arithmetic operations, which can be extended to handle more complex tasks.
Implementation
CPU Header (cpu.h)
| |
Test Program (main.cpp)
| |
Conclusion
This minimal CPU implementation demonstrates the fundamental principles of computing while remaining accessible and understandable. It provides a foundation that can be extended to create more complex systems, making it an excellent learning tool for understanding computer architecture.