Skip to content

AkhileshManda/Emulator-101

Repository files navigation

CHIP-8 Emulator in Rust

A simple CHIP-8 emulator implementation using Rust and SDL2 for graphics and input handling.

Overview

This project implements a CHIP-8 emulator with:

  • 4KB of memory
  • 64x32 monochrome display
  • 16 general-purpose registers
  • 16-key hexadecimal keypad
  • Two timers (delay and sound)

Architecture

The project is split into two main components:

1. Emulator Core (chip8_core)

The core emulator handles:

  • CPU emulation
  • Memory management
  • Display buffer
  • Input processing
  • Timer management

2. Frontend (desktop)

Handles:

  • Window creation and rendering
  • Input processing
  • Timing control
  • Display scaling (15x)

Low Level Design

Below is the detailed class diagram showing the relationship between Frontend and Emulator components:

Low Level Design

How It Works

Key Components

Memory Layout

0x000-0x1FF: Reserved for interpreter (includes font data)
0x200-0xFFF: Program ROM and work RAM

Display

  • Resolution: 64x32 pixels
  • Monochrome (black and white)
  • Scaled 15x in the window (960x480)

Input

Original CHIP-8 Keypad mapping to modern keyboard:

1 2 3 4     →    1 2 3 C
Q W E R     →    4 5 6 D
A S D F     →    7 8 9 E
Z X C V     →    A 0 B F

Execution Flow

The emulator runs in a continuous loop at 60 FPS, with each frame consisting of:

  1. Input Processing

    • Detect key presses/releases
    • Map modern keys to CHIP-8 keys
    • Update emulator key states
  2. CPU Execution

    • Execute 10 instructions per frame
    • Fetch opcode from memory
    • Decode and execute instruction
    • Update system state
  3. Timer Updates

    • Update delay timer
    • Update sound timer
    • Handle sound when timer > 0
  4. Display Update

    • Clear screen
    • Draw active pixels
    • Scale display output
    • Present frame

Detailed Key Press Flow

Below is a sequence diagram showing what happens when a key is pressed (e.g., 'W' key):

Key Press Flow

Usage

  1. Build the project:
cargo build --release
  1. Run a ROM:
cargo run path/to/rom

Controls

  • Use the keyboard mapping shown above
  • ESC to quit
  • Each key maps to its corresponding CHIP-8 key

Technical Details

Emulator Features

  • Full CHIP-8 instruction set support
  • Accurate timing (60 Hz)
  • Sound support (beep when sound timer > 0)
  • Configurable CPU speed (10 instructions per frame)

Display Rendering

  • Uses SDL2 for graphics
  • Each CHIP-8 pixel is rendered as a 15x15 rectangle
  • Black background with white pixels
  • VSync enabled for smooth rendering

Implementation Notes

The emulator follows the original CHIP-8 specifications:

  • Instructions are 2 bytes long
  • Programs start at memory address 0x200
  • Built-in font set for hexadecimal digits
  • Stack limited to 16 levels
  • No floating-point operations

Dependencies

  • Rust
  • SDL2
  • rand (for random number generation)

Building

Requires:

  1. Rust toolchain
  2. SDL2 development libraries
  3. Cargo package manager

Future Improvements

  • Audio implementation
  • Configuration file support
  • Save state functionality
  • Debug mode
  • Custom key mapping support
  • Performance monitoring

About

A repo to understand how the CHIP8 Emulator works

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages