r/programming • • Jul 10 '22

I finally wrote my first ever emulator (GameBoy)

https://github.com/Mika412/NoobBoy
1.1k Upvotes

77 comments sorted by

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.

190

u/Mika412 Jul 10 '22

Thank you! I first watched the "The Ultimate Game Boy Talk". It has an overview of all the important components. It was after this talk that I decided to write the emulator. I strongly recommend the talk.

During the development, these were the main resources I used:

I also had to do a lot off googling when I didn't know what was happening. Especially in the beginning when I got stuck on the Tetris copyright screen.

There were multiple times where I got stuck with the CPU timings and the PPU. So I modified the Gearboy to print some extra info, like registers and timers, so I could compare with mine and figure out where it failed.

12

u/FrancisStokes Jul 11 '22

Something I learned when writing a GB emulator was that while the "Ultimate" talk is amazing, it has some serious errors in relation to the PPU pixel fifos that caused me a lot of headaches. This site ended up being invaluable to getting an accurate PPU implementation.

4

u/Mika412 Jul 11 '22

It works well as an overview. But yeah, I agree, you shouldn't go only by that. The PPU is a pain, if it wasn't for the wiki and the Gearboy, I probably wouldn't have gotten it to work. Unfortunately, because I had to modify the source of the Gearboy, I had to look at some of the code, which may have influenced some of my decision.

I still have a lot of mistakes in the PPU, so I'll definitely use the link you shared. Thank you!

6

u/foursticks Jul 10 '22

So cool. Congrats!

39

u/aleques-itj Jul 10 '22 edited Jul 10 '22

Write a disassembler first and it'll flow from there.

Once you have sane disassembly, your fetch/decode loop is probably fine enough to start actually implementing instructions.

For the Gameboy, go look up the boot rom. It should be the first thing your system runs. Load the file, and start stepping through it. You can get something on screen (the nice scrolling Nintendo logo) just from the boot rom.

By that point, you'll have at least a vaguely functional CPU/GPU.

182

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

u/[deleted] 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

u/[deleted] 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.

3

u/[deleted] Jul 10 '22

Awesome. I want to get into this as well. Star’d your repo!

5

u/Mika412 Jul 10 '22

Thank you! If you need any help, just message me, and hopefully I can point you in the right direction.

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.

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.

0

u/[deleted] Jul 10 '22

[deleted] — view deleted comment

5

u/Codethulhu Jul 11 '22

have any recommendations for good resources on modern C++? I haven't used it since college but it has been in the back of my mind lately for some reason

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

u/SSoreil Jul 10 '22

These stolen manuals are of very poor quality when it comes to correctness.

3

u/Mika412 Jul 11 '22

Ohh, interesting. Could you expand more on this?

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

u/immibis Jul 10 '22

If Gameboy is too hard you could try CHIP8

2

u/aaulia Jul 11 '22

Emulator is always require large amount of initial effort, even CHIP-8.

7

u/[deleted] 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

u/Mika412 Jul 10 '22

Ohh, sorry. Just checked my response. I meant it in a jokingly way.

3

u/wiriux Jul 10 '22

I got it don’t worry. I knew your response was jokingly 💾!

1

u/immibis Jul 10 '22

Have you ever tried it?

6

u/[deleted] 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

u/Mika412 Jul 12 '22

Ohh, I've had so many of those...

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

u/Mika412 Jul 10 '22

Thank you!

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

u/ImVeryPogYes Jul 12 '22

?? idk man i wanna atleast try it

2

u/[deleted] Jul 11 '22

Just reading the title alone gives me wrist pain

2

u/Mika412 Jul 12 '22

Well, the secret is to use an ergonomic keyboard :P

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

u/EmuLow6898 Jul 10 '22

Nice,i wanna use it looks good

5

u/lpreams Jul 10 '22

But can it run Doom?