This is a simple CPU emulator written in Go.
It supports basic operations like LOAD, ADD, SUB, STORE, JUMP, and HALT.
At a high level, a CPU performs operations through the fetch-decode-execute cycle. This is the core mechanism behind most CPUs:
- Fetch: The CPU fetches an instruction from memory.
- Decode: It decodes the instruction to understand what operation it needs to perform.
- Execute: It performs the operation, such as an arithmetic operation, moving data, or jumping to another part of the program.
Registers and memory play a key role in this process:
- Registers: Small, fast storage locations inside the CPU that hold data and addresses.
- Memory: The larger, slower storage for the programβs data (RAM).
In a real CPU, operations like ADD, SUB, LOAD, JUMP, and HALT are often controlled by the Control Unit (CU) and Arithmetic Logic Unit (ALU).
- main.goβ Entry point of the application
- cpu_emulator/β Contains CPU logic (structs, instructions, memory management)
- Simple CPU with 4 registers and 256 bytes of memory
- Basic instruction set:
- LOAD
- ADD
- SUB
- STORE
- JUMP
- HALT
 
- Program Counter (pc) and Running State (running) management
- Easy to extend with more instructions!
First, clone the repo:
git clone https://github.com/manoj2994/tiny_cpu_emulator
cd <repo>
go run main.goThis project is licensed under the MIT License β see the LICENSE file for details.