A CHIP-8 emulator written from scratch in C++17, using SDL2 for the window and rendering.
CHIP-8 is a small virtual machine created in the 1970s to make simple video games easier to write. It has 4 KB of memory, sixteen 8-bit registers, a 64×32 monochrome display and about 35 instructions. Because it is so small, it is the classic first project for learning how emulators (and CPUs) work, before moving on to systems like the Game Boy or the NES.
The emulator currently runs the IBM logo test ROM.
Implemented
- Machine state: 4 KB memory, registers V0–VF, I, program counter, stack, timers, display and keypad
- ROM loading at
0x200, with a size check against the end of memory - The fetch-decode-execute cycle, one instruction per step
- Display rendering with SDL2 (64×32 pixels, scaled ×10 to a 640×320 window, about 60 frames per second)
- Unknown opcodes are reported on
stderrinstead of being silently ignored - Opcodes:
| Opcode | Meaning |
|---|---|
00E0 |
Clear the screen |
1NNN |
Jump to address NNN |
6XNN |
Set VX to NN |
7XNN |
Add NN to VX |
ANNN |
Set I to NNN |
DXYN |
Draw an N-row sprite at (VX, VY) — simplified, see below |
Known limitations
DXYNturns pixels on but does not XOR them yet, does not set the collision flag (VF), and does not clip sprites at the screen edges.- The ROM path is hard-coded (
roms/2-ibm-logo.ch8). - Only Windows (MinGW) is supported for now.
- Complete
DXYN: XOR drawing, collision flag, edge clipping - All remaining opcodes, validated one by one with the Timendus test suite
- Choose the ROM from the command line
- Keyboard input, 60 Hz timers and sound
- Run real games (Pong, Tetris, Space Invaders)
- Profiling and performance tuning
- Cross-platform build with CMake and continuous integration (GitHub Actions)
src/
├── chip8.hpp # the machine state (struct Chip8) and its constants
└── main.cpp # ROM loading, the step function (fetch-decode-execute), rendering and the main loop
build.ps1 # build-and-run script for Windows
Requirements:
- A MinGW-w64
g++with C++17 support - The SDL2 development libraries for MinGW (
SDL2-devel-2.32.x-mingw.zipfrom the SDL releases)
Steps:
- Extract SDL2 and set the
$SDLpath at the top ofbuild.ps1to itsx86_64-w64-mingw32folder. - Copy
SDL2.dllfrom SDL'sbin/folder intobuild/. - Put a ROM in
roms/(see below). - Build and run:
./build.ps1ROMs are not included in this repository.
The test ROMs come from the CHIP-8 test suite by Timendus.
Download 2-ibm-logo.ch8 from its bin/ folder and place it in a roms/ folder at the root of the project.
- Modelling hardware with types: fixed-width integers (
uint8_t,uint16_t) make the emulated registers overflow exactly like the real ones. - Bit manipulation: assembling 16-bit opcodes from two bytes, and decoding them with masks and shifts.
- Separating state from rendering: instructions only change an array of pixels; a separate function turns it into an image every frame, as in a game engine.
- C++ fundamentals coming from Python: references vs copies,
const, pointers, scope, and managing resources by hand (create, check, destroy in reverse order). - Debugging by elimination: forming hypotheses and ruling them out one by one, and reading a wrong image on screen to find the wrong formula.
- A professional Git workflow: small focused commits, feature branches, merges and resolving a real conflict.
