C++ Chip8 Emulator Update
Hey, its been a while. I wanted to give a quick update on this project. Since my initial release, I've made several improvements and added new features to enhance the overall experience.
The Issues
Per my previous post the emulator was having significant timing issues. Even to the point that the system was essentially unplayable. After some analysis and profiling using Valgrind's callgrind tool, I identified several key areas that were causing performance bottlenecks:
- Both the debugger interface and chip8 interface were being rendered at the emulation loop frequency, rather than at a fixed 60hz rate.
- There was no text or texture caching, leading to the entire GUI being redrawn every frame, which is particularly expensive when using TTF fonts instead of bitmap fonts.
- All of the timers and inputs were coupled to the game loop rather than real time, causing inconsistencies in timing and responsiveness.
The fixes
The first issue was a very simple fix but took an embarrassingly long to identify. However by using valgrind and slowly profiling the various calls made I was able to identify the root cause and fix it fairly quickly, instantly boosting performance by nearly 2x.
The second issue took a bit longer to resolve as I had to implement a caching system for both text and textures. My younger brother who has a background in computer graphics helped me understand some of the best practices when it came to caching in SDL2. By storing rendered text and textures in memory and only redrawing them when necessary, I was able to significantly reduce the rendering overhead. This change alone resulted in a substantial performance improvement in its own right.
The third issue required decoupling the timers and input handling from the main emulation loop. By implementing a separate timing mechanism that operates independently of the game loop, I was able to ensure that timers and inputs are processed in real time. This change didn't result in a direct increase to performance, but rather greatly improved the overall responsiveness and accuracy of the emulator. It additionally resolved the issues regarding user inputs being dropped which was preventing games from being playable.
Updated demo
Future work
At the point im happy enough with the performance of the emulator for now. For the time being I plan to shift my focus to finishing the core functionality of the system. Primarily in regards to audio emulation as well as adding support for more opcodes and features such as Super Chip8 extensions. Once these changes have been implemented I plan on adding a launcher interface using imgui to allow for easier rom selection and configuration, with additional settings like color pallets and input mapping/controller support.Unfortunately due to time constraints I likely wont be able to work on this project much over the next couple months due to my work on FSAE and classes, but I do plan to return to it in the near future.
References that helped me (and maybe you!)
All code for this project is available on my GitHub if you're interested!
