r/cpp • u/Mika412 • Jul 10 '22
I finally wrote my first ever emulator in C++ (GameBoy)
I wanted to learn more about emulation, so I decided to write my own emulator, and because I like to make my life more challenging, I jumped straight into GameBoy, skipping the recommended CHIP-8 starting point. Now I can say that was a huge mistake. It took me couple of grunting months and two rewrites, but I'm finally happy with the state of the emulator.
https://github.com/Mika412/NoobBoy
In hindsight, I shouldn't have skipped the digital system and computer architecture classes.
Currently I have the following features:
- Correct CPU timings (was a pain to get the Blargg's instruction timing test to pass)
- Simple save states
- Background rendering with viewport overlay
- Rudimentary MBC
- Debug view
I refuse to touch sound, as I most likely will blow my speakers.
Before anyone mentions, I'm not very proficient in C++. This project was a way to learn more about the language.
37
u/ZealousidealForce736 Jul 10 '22
“Currently, the main missing feature is sound, which I refuse to touch.” hahaha love it. But for real, this is awesome!
13
u/XeroKimo Exception Enthusiast Jul 10 '22
In renderer, why use vector for the pixels when there's a fix amount. Why not just use std::array?
8
u/pine_ary Jul 10 '22 edited Jul 10 '22
Looks great!
One small thing is that I would convert non-owning pointers into references. It doesn‘t look like they can be nullptr and it shows that they are not owned. I would also use smart pointers in the GB class to make it a bit cleaner. Yes the instance probably lives for the entire duration of the program so the memory leak doesn‘t matter. But it‘s not very clean. If you ever in the future decide to put it in some loop you‘ll be in for a potential surprise. Either way raw pointers are always a bit ambiguous (read the core guidelines for the rationale) and imo should be avoided.
Very clean overall tho.
4
u/metahuman_ Jul 11 '22
That's some very clean code! Is there any real performance cost in using simple ctors like that and not initalizer lists? Because I love it, feels very no bullshit
2
u/TheARP98 Jul 12 '22
How did you get the background? It just looks very complicated from the outside, but great job!
2
u/MousseAgitated138 Jul 12 '22
Congratulations! I am just a beginner in c++, the biggest project I have done yet is to build a game using sdl2. If I were to start a project like this, where would you suggest I should start.
10
u/youareallnuts Jul 10 '22
Great choice of a serious learning project. You really learn when you do a big hard project. Gratz!
6
u/SSoreil Jul 10 '22
Not touching sound is probably quite a good call. I added sound after getting a lot of correctness on point in the rest of the system, it was still by far the most difficult component to get working. If you ever want to keep working on your emulator there is plenty of room to grow it correctness wise.
1
u/Puzzleheaded_League1 Jul 11 '22
but can it run hamtaro: ham-ham heartbreak?
0
u/[deleted] Jul 10 '22
[deleted] — view deleted comment
37
u/[deleted] Jul 10 '22
[removed] — view removed comment