The goal of this project is to build a functional Instruction Set Architecture (ISA) Emulator — a software‑based virtual CPU that mimics real hardware execution.
The emulator:
- Reads raw binary instructions
- Interprets them using a predefined instruction set
- Modifies internal CPU state (registers and RAM) accordingly
This simulates how a physical processor fetches, decodes, and executes machine code.
A CPU is fundamentally a high‑speed calculator that executes a sequence of stored instructions. Every CPU is built around three core components:
-
Control Unit
Fetches instructions from memory and decodes them into control signals the CPU can act upon. -
Arithmetic Logic Unit (ALU)
Performs arithmetic and logical operations such as addition, subtraction, and bitwise logic. -
Registers
Small, ultra‑fast memory inside the CPU used for temporary data storage. Registers are accessed far faster than RAM.
-
Word Size: 8‑bit
All data values are constrained to the range 0–255. -
Registers:
Four general‑purpose registers: A, B, C, D. These serve as the CPU’s internal working memory. -
Program Counter (PC):
A special register that stores the memory address of the next instruction to be executed. -
Memory (RAM):
256 bytes of addressable memory used to store both program instructions and data. -
Overflow Behavior:
Arithmetic uses modular arithmetic to simulate hardware overflow (e.g.,255 + 1 = 0).
The emulator follows the standard CPU execution loop:
Fetch → Decode → Execute
-
Fetch
Read the byte located at the address stored in the Program Counter. -
Decode
Interpret the fetched byte by matching it against the instruction table. -
Execute
Perform the operation associated with the instruction (math, data movement, or control flow). -
Step
Increment the Program Counter to prepare for the next instruction.
Although the CPU operates on binary values, each opcode is mapped to a human‑readable mnemonic.
| Hex Code | Mnemonic | Description |
|---|---|---|
0x01 |
LOAD_VAL | Load the next byte in memory into Register A |
0x02 |
ADD | Add the value in Register B to Register A |
0x03 |
HALT | Stop execution and halt the CPU |
- Python 3.x
No external libraries required.
Programs are written as a list of bytes. The following example computes 10 + 5:
# 0x01 = LOAD_VAL into Register A
# 10 = Value to load
# 0x02 = ADD (adds Register B to A)
# 0x03 = HALT (stop execution)
program = [0x01, 10, 0x02, 0x03]- Clone the repository
- Run the script:
python emulator.py
- The emulator outputs the register state at each clock cycle (loop iteration), allowing step‑by‑step inspection of execution.