Skip to content

A fully functional Chip-8 emulator built from scratch in C++.

License

Notifications You must be signed in to change notification settings

eko-071/chip8-emulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chip-8 Emulator

A fully-functional Chip-8 emulator built from scratch in C++ with SDL2 graphics and audio support.

Features

  • All 35 Chip-8 opcodes implemented
  • 64x32 pixel display with SDL2 rendering
  • Keyboard input support
  • Sound effects (beep tone)
  • 60 FPS rendering

Architecture

Chip-8 is a virtual machine from the 1970s designed to make programming video games easier on early microcomputers.

System Specifications

  • Memory: 4 KB (4096 bytes)
    • 0x000-0x1FF: Reserved for interpreter and fonts
    • 0x200-0xFFF: Program/ROM space
  • Registers:
    • 16 8-bit general-purpose registers (V0-VF)
    • VF doubles as a flag register for arithmetic operations
    • 16-bit index register (I)
    • 16-bit program counter (PC)
    • 8-bit stack pointer (SP)
  • Display: 64×32 pixels, monochrome
  • Timers:
    • Delay timer (counts down at 60 Hz)
    • Sound timer (beeps when > 0, counts down at 60 Hz)
  • Stack: 16 levels for subroutine calls
  • Keypad: 16-key hexadecimal input

Dependencies

  • SDL2 library

Installation

Arch Linux:

sudo pacman -S sdl2

Ubuntu/Debian:

sudo apt-get install libsdl2-dev

macOS:

brew install sdl2

Building

  1. Clone the repo
git clone https://github.com/eko-071/chip8-emulator.git
  1. Make it
make

Usage

./chip8 <path-to-rom-file>

Example:

./chip8 roms/PONG.ch8

Keyboard Mapping

The original Chip-8 keypad is mapped to keyboard keys:

Chip-8 Keypad:          QWERTY Keyboard:
┌─┬─┬─┬─┐               ┌─┬─┬─┬─┐
│1│2│3│C│               │1│2│3│4│
├─┼─┼─┼─┤               ├─┼─┼─┼─┤
│4│5│6│D│               │Q│W│E│R│
├─┼─┼─┼─┤      =        ├─┼─┼─┼─┤
│7│8│9│E│               │A│S│D│F│
├─┼─┼─┼─┤               ├─┼─┼─┼─┤
│A│0│B│F│               │Z│X│C│V│
└─┴─┴─┴─┘               └─┴─┴─┴─┘

Controls:

  • ESC - Quit emulator
  • Keyboard keys as mapped above

Game-Specific Controls

PONG:

  • Left paddle: 1 (up), Q (down)
  • Right paddle: 4 (up), R (down)

TETRIS:

  • Q - Rotate
  • W - Drop
  • E - Move right
  • A - Move left

Project Structure

.
├── LICENSE
├── Makefile
├── README.md
├── roms # Some ROM files (.ch8)
│   ├── Blinky.ch8
│   ├── Pong.ch8
│   └── Tetris.ch8
└── src
    ├── chip8.cpp # Core emulator logic and opcodes implementation
    ├── chip8.h # Chip-8 class declaration
    └── main.cpp # SDL2 setup, rendering, input handling and stuff

Implementation Details

Instruction Set

The emulator implements all 35 Chip-8 instructions, including:

  • Arithmetic: ADD, SUB, AND, OR, XOR, shift operations
  • Graphics: Draw sprites with XOR mode, collision detection
  • Flow control: Jump, call/return subroutines, conditional skips
  • Memory: Load/store registers, BCD conversion
  • Timers: Delay and sound timer operations
  • Input: Key press detection (blocking and non-blocking)

Display

Graphics are rendered using SDL2:

  • Each Chip-8 pixel is scaled 10× for visibility (640×320 window)
  • XOR-based sprite drawing for collision detection
  • 60 FPS rendering

Audio

Simple square wave generation at 440 Hz (musical note A) plays when sound_timer > 0.

Resources

About

A fully functional Chip-8 emulator built from scratch in C++.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published