r/programming • u/Mika412 • Jul 10 '22
I finally wrote my first ever emulator (GameBoy)
https://github.com/Mika412/NoobBoy182
u/passerbycmc Jul 10 '22
Good job, it's a lot of work but great way to learn both about emulation and stuff like SDL
79
u/Mika412 Jul 10 '22
Thank you. Yes, indeed. I've learned a ton, given I didn't have any previous experience
-156
Jul 10 '22
not bad for a first program ever
149
u/Mika412 Jul 10 '22
Just to be clear. I have plenty of coding experience. I just have little experience in C++ and didn't know anything about real hardware emulation.
33
u/aMAYESingNATHAN Jul 10 '22
I use quite a lot of C++ and always been interested in emulation. Where did you get started its always seemed a bit overwhelming to start.
Edit: ignore me just seen your reply to another comment.
20
Jul 10 '22
Also you can check out /r/EmuDev
9
u/Mika412 Jul 10 '22
Yes, this is probably one of the best resource there is. In the beginning I had so many issues with Tetris and thankfully, there always was a discussion on that issue.
→ More replies (2)7
u/gamestopcockLoopring Jul 10 '22
I've never written an emulator, but i like how much sense and organised your code is buddy.
Really like the instructions and how you replicate the architecture of w.e gb arch is, definitely inspiration to have a stab myself.
Good work!
4
u/Mika412 Jul 10 '22
Thank you! I hope that in the future I can learn even better practices and improve my skills.
→ More replies (2)7
u/Swag_Grenade Jul 11 '22
Yeah I mean I'd say you you'd have to be pretty damn talented to write a fully functional Game Boy emulator from scratch as the first thing you've ever written.
LMFAO at dude above you "the first thing you ever coded was a working hardware emulator not bad I guess" must be Elon's burner lol.
49
u/ucario Jul 10 '22
Very cool.
Did you consult https://ia903208.us.archive.org/9/items/GameBoyProgManVer1.1/GameBoyProgManVer1.1.pdf
Or get inspiration based on other open source repos?
I’ve always wanted to do this, but could never justify it time wise. I still want to do this…
49
u/Mika412 Jul 10 '22
No. I didn't know it existed... Could have saved so much time...
I initially read some Rust tutorials, before I started the project, but early on I decided that I didn't want to look at other's people code. Although, I had to break this rule 2 or 3 times, when I got stuck for more than a week..
45
u/ucario Jul 10 '22
There’s a balance between wanting the gratification of having figured it out yourself and knowing that you’re wasting time on an already solved problem.
You did great here, well done!
32
u/Mika412 Jul 10 '22
Thank you.
But it's always the case during the learning process. You almost always can find a solution to what you're doing, but you'll learn a lot more trying to solve it yourself.
But yeah, there should be a balance. There's no point in constantly reinventing the wheel.
4
21
u/Brilliant-Sky2969 Jul 10 '22
For the people that always wanted to write an emulator but don't want to start with a complicated thing, start with a CHIP-8 interpreter, it's doable in reasonable amount of time and you can go from there to implement a 8bit console.
10
u/irunArchbtw_1 Jul 10 '22 edited Jul 10 '22
May I ask what a chip-8 interpreter is? I tried looking it up, but I feel I may get a good idea if I ask, to have a decent starting point to proceed from.
EDIT: Nevermind, I think I found some good resources. Thanks for mentioning this, looks like a good beginner project for learning about emulation. One thing thats been difficult for me learning programming was in estimating project difficulty. For example, when I was learning 3D modeling many years ago, after learning the basic tools, picking projects is very simple, you just pick simple things model. Like you obviously dont want to start modeling a lamborghini on your first go lol, best to start with maybe a pocket knife to build your confidence and experience, and from there you're then able to pick more interesting stuff to work on. But picking programming projects, aside from the exercises in the books, and then learning basic data structures like linked lists, stacks & queues, etc. those early beginner to intermediate steps seem straight forward as well, such as going from a intro programming language book on your language of choice, learning some standard library stuff, and then going through a data structures book on that language, to me anyway, is my approach. But going beyond that, yeah was hard for me to estimate a good project appropriate for my given skill level.
8
u/Mika412 Jul 10 '22
Sorry that I didn't answer before. Great that you found out more about it. To be more clear, the goal is practically the same as with the GB, where you receive a set of CHIP-8 instructions and have to translate them to machine code. Usually you do it by re-implementing the instruction functionality in your desired language. The great thing about CHIP8 is that it has fewer opcodes (35) and is a very well documented "virtual" system.
As for the projects. I don't really care about the complexity. I just make sure I'm very interested in the project. The harder it is, the more opportunity you have to learn. With time, you'll fine tune your time estimation. Personally, I assume that it will take me 2.5 times of the initial estimation.
2
u/irunArchbtw_1 Jul 11 '22
Thanks man, and no worries. And very good job on your project. Yeah maybe I should just keep coding and not worry too much about what I dont understand yet, cant rush these things.
4
u/Mika412 Jul 10 '22
I totally agree. CHIP-8 is a lot more manageable and has more resources/guides.
17
u/crummy Jul 10 '22
this is an impressive achievement. I tried it a while back, thinking the Gameboy would be a nice simple place to start. after a very large amount of work I got a mangled Nintendo logo to slide down the screen. gave up not long after.
14
u/Mika412 Jul 10 '22
Ohh, I've been in that situation. I've almost quit the project a couple of times. But every time I got something to work, it motivated me even more to continue.
I remember when I got the logo to show up, it was so exciting. You should give another go
4
2
7
Jul 10 '22
Why do you refuse to touch sound?
27
u/Mika412 Jul 10 '22
Well, I don't find it that appealing, but someday I may try to implement it, just for the challenge.
Additionally, recently I've been having some issues with my ears, so for now I don't want to have to hear any high pitched sounds.
12
u/zeroone Jul 10 '22
I remember finding myself in the same situation. It is not a difficult feature to add. In fact, Game Boy Color is an easy addition too. Don't give up.
10
u/mxz3000 Jul 10 '22
Tbh, sound is probably the hardest thing to emulate, and thank god the blargg audio tests exist. Though they're quite difficult to pass and test LOTS of annoying edge cases that are a pain to handle. Though one benefit of sound is that it ensures that your emulator runs at the right speed, assuming you implement it in a way where you block emulation whilst waiting for the audio to play/sync up.
Would I do it again? Probably not, but it was satisfying to get it working after my CPU and GPU were pretty much sorted!
4
u/Keep_The_Peach Jul 11 '22
I thought the same thing at first while making mine but I learned way more taking care of the sound. Even simple things like handling volume or mixing different channels is actually fun
To be honest, it was also the hardest part for me
3
u/maxdickroom Jul 30 '22
Sound is definitely the hardest aspect of gb emulators. If you’re ever up for it you can try checking out my emulator’s APU code, I think it’s pretty easy to follow and understand.
https://github.com/bassicali/gem/tree/master/gem.core/Source/Core
13
u/wiriux Jul 10 '22
I hate people that can do stuff like OS and emulators. Therefore, I hate you OP and I hope a nasty bug appears breaking the whole emulator.
Jealousy over. That’s awesome dude! :D I’ll check out your GitHub!
9
u/Mika412 Jul 10 '22
Well, it's full of bugs. One more is not an issue. Thank you!
3
u/wiriux Jul 10 '22
It was just a joke! Awesome accomplishment. :)
2
1
6
Jul 10 '22
This level of dedication is pretty impressive. What do you do for work? I find it's impossible to take any side project seriously when I do it all day.
2
u/emperor000 Jul 11 '22
I'm the same way, except, what's worse, I'll still start something and "this will be the one I finish" until the next one comes along...
1
1
u/Mika412 Jul 11 '22
I work as a Research Engineer, leading a small research project in my company. It's pretty chill. But for the last couple of months I've been away from my wife, so I've had some extra time for side projects.
5
u/screwthat4u Jul 10 '22
Very cool, I like how your code is written too as so many emulators are complete maintenance nightmares
2
u/Mika412 Jul 10 '22
Thank you! Had to rewrite some parts multiple time until I got it the way I like.
4
u/Ochikobore Jul 10 '22
this is super cool, i envy your dedication to the project, how long did it take from start to finish in your estimation?
4
u/Mika412 Jul 10 '22
Thank you. Well, I technically started in September 2021, but then life happened, and I put it on hold for a while. I would say it took me around 3/4 months of casual work during the weekends. But it could have been faster if I was to be more dedicated.
4
u/sawkonmaicok Jul 10 '22
Is it accurate down to the clock cycle or just instruction accurate?
3
u/Mika412 Jul 10 '22
Instruction timings. I think the only thing off is the interrupt timings
6
u/zid Jul 10 '22
Basically everything will be 'off' if that's your timing granularity, you'll fail basically every mooneye test.
Try pinball deluxe at some point :)
It's definitely required to do a prototype or two like this though to get a feel for it before going full accuracy, hope you keep your interest up.
3
u/Mika412 Jul 10 '22
Yep, you're right. Just tried the tests and the game, and it doesn't look great. Guess there's still a lot more work to be done
3
u/immibis Jul 10 '22
Do GB games really require sub-instruction timing accuracy? As in, the order of CPU accesses to memory makes a difference of more than a couple of pixels?
3
u/Paoda Jul 11 '22
The vast majority of games will work fine with only instruction accuracy. There are a few that require a cycle accurate cpu, ppu or both but chances are you've never heard of them (two are pinball deluxe and prehistorik man).
1
u/Zouizoui Jul 19 '22
Can you explain the difference between instruction accuracy and clock cycle accuracy ?
3
u/SmashedTatoes Jul 11 '22
Great job, this is awesome. I tried doing the same in TypeScript last year but I only got to the point of having Tetris and Dr. Mario working. It takes a lot of reasearch and hard work to get it to where you have!
2
u/Mika412 Jul 11 '22
That's already a huge achievement! When I got to Tetris, I thought to end it there, but I really wanted to be able to play Pokemon Red. Although, yeah, there's a lot of nifty things that need to be done to get other games to work.
Would you mind sharing your project? I'm interested in taking a look.
2
u/SmashedTatoes Jul 11 '22
For sure, I've got it on GitHub here: https://github.com/AtomicJon/tsemu (the latest build is hosted on GitHub pages here: https://atomicjon.github.io/tsemu).
I planned on going back to it to try and get more games working but, as I too often do, got distracted with other side projects.
1
u/Mika412 Jul 12 '22
Amazing! Hope you continue working on it and are able to finish!
Great choice for using typescript and getting it to run on github pages. Unfortunately, for me it's not that easy to get it working in the browser, but I was planning on eventually to try using webassembly.
Also, you probably should reset the state every time you open a new rom
4
u/Ryba_PsiBlade Jul 10 '22
Doing something like this is truly special. I award you for learning and growing beyond that which others ask you to do for them.
2
2
u/ImVeryPogYes Jul 11 '22
ima use this now instead of vba
1
u/Mika412 Jul 11 '22
Please don't. This is just a hobby project of mine, with the goal of learning more about emulation. It's not meant to be used for anything else than a learning resource. VBA is a lot more stable and feature complete.
2
2
2
u/ml01 Jul 11 '22
congrats! it looks pretty neat!
i'm working on my nes emulator right now, just got the cpu working yesterday! your projects motivates me to push forward, can't wait to see something appearing on screen!
2
u/Mika412 Jul 12 '22
Thank you.
That's amazing! Hope you continue with the project till the end, and when you're ready, please do share!
3
5
96
u/SioBane Jul 10 '22
This is something I’ve been wanting to do for a long time. How did you get started and what resources did you use when starting out? I’d love to hear more about the process of this type of project. Seriously though, awesome job.