Skip to content

Sam-Of-The-Arth/ISAEmulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

8‑Bit ISA Emulator (Virtual CPU)


Project Overview

Objective

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.


Understanding CPUs

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.


Architecture

  • 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).


CPU Execution Cycle

The emulator follows the standard CPU execution loop:

Fetch → Decode → Execute

  1. Fetch
    Read the byte located at the address stored in the Program Counter.

  2. Decode
    Interpret the fetched byte by matching it against the instruction table.

  3. Execute
    Perform the operation associated with the instruction (math, data movement, or control flow).

  4. Step
    Increment the Program Counter to prepare for the next instruction.


Instruction Set

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

Usage

Requirements

  • Python 3.x
    No external libraries required.

Writing a Program

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]

Running the Emulator

  1. Clone the repository
  2. Run the script:
    python emulator.py
  3. The emulator outputs the register state at each clock cycle (loop iteration), allowing step‑by‑step inspection of execution.

About

Lightweight 8-bit ISA emulator built in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages