Chip-8
2020-09-12
The Internet recommends the Chip-8 as a first emulation target because of its small instruction set, simple graphics and sound, and ease of getting something working. I tried this using Rust for my first project that was larger than an Advent of Code problem.
I decided to try implementing the emulator without looking at other similar projects to see if I could figure it out. I worked from Cowgod's Technical Reference which describes how the interpreter should work and lists the CPU instruction set. I also used the Wikipedia article as a reference when something didn't make sense.
It was very convenient being able to use Rust pattern matching to destructure the 2-byte instructions and match against different parts of them: the most significant bits determine the instruction type and the least significant usually determine the registers to operate upon.
I also decided to go test first by writing the unit tests for the instructions before implementing each one. Rust made this easy and pretty simple to run in CI as a GitHub Action.
To begin with I wanted to use the recently released Bevy game engine for the graphics and main loop, but this brought with it a lot more dependencies than I really needed for this project. In the end I migrated my existing code to use SDL2 instead which was more lightweight and appropriate for this kind of project.
You can find the source code on GitHub.