Chip8 Emulator
- 2 Devlogs
- 22 Total hours
An emulator/interpreter of the Chip8 programming language using C++ and SDL3
An emulator/interpreter of the Chip8 programming language using C++ and SDL3
I am proud to write that I have finished my emulator and it is now fully public on GitHub and has a downloadable binary for MacOS.
Since the last devlog I have:
Out of all of this, the biggest challenge (although it was minor, it was very annoying) was figuring out a way to map the colour picker in Dear ImGui to the actual colours drawn in SDL3. The frame buffer expected a 32 bit integer representing the RGBA hex code of the colour, while ImGui wanted a float array with each colour being from 0.0-1.0. I tried to use methods like ImGui::ColorConvertFloat4ToU32, but it ended up all using types native to ImGui that SDL does not support, so I ended up using a standard float array and then writing a simple function to multiply the values by 255 and shift the bits to turn it in to the value SDL expects. Here’s the code: (I know I could’ve only had the bit shifting logic once but why have DRY code when it can be wet 🤣)
int Config::getColor(bool fg){
if(fg){
return static_cast<int>(fgColor[0] * 255) << 24 | static_cast<int>(fgColor[1] * 255) << 16 | static_cast<int>(fgColor[2] * 255) << 8 | 0xFF;
}
return static_cast<int>(bgColor[0] * 255) << 24 | static_cast<int>(bgColor[1] * 255) << 16 | static_cast<int>(bgColor[2] * 255) << 8 | 0xFF;
}
Attached is a picture of the emulator running a ROM that is essentially a backport of the chrome dino game. This was a really fun project and a perfect stepping stone to eventually making a Gameboy emulator!!
I’ve implemented all of the Chip8 opcodes in C++ and the emulator/interpreter is able to execute them all correctly. I used various test files/ROMs to verify this and debug it along the way. I chose SDL3 to render the project because of the easy support for an array of ints that represent pixel values/colors. It also uses SDL3 for playing sound (using the sound timer) and getting input using the hexadecimal keyboard. I’m now going to work on adding configurable settings (such as background and foreground color) using Dear IMGUI and then porting the project to the web with Emscripten