Skip to content

About

A CHIP-8 emulator written in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CHIP-8 Emulator

A CHIP-8 emulator written from scratch in C++17, using SDL2 for the window and rendering.

The IBM logo test ROM running in the emulator

What is CHIP-8?

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.

Status

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 stderr instead 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

  • DXYN turns 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.

Roadmap

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

Project structure

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

Building (Windows, MinGW)

Requirements:

  • A MinGW-w64 g++ with C++17 support
  • The SDL2 development libraries for MinGW (SDL2-devel-2.32.x-mingw.zip from the SDL releases)

Steps:

  1. Extract SDL2 and set the $SDL path at the top of build.ps1 to its x86_64-w64-mingw32 folder.
  2. Copy SDL2.dll from SDL's bin/ folder into build/.
  3. Put a ROM in roms/ (see below).
  4. Build and run:
./build.ps1

ROMs

ROMs 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.

References

What I learned

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

About

A CHIP-8 emulator written in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages