Code and Cake

How to write your own Game Boy emulator


Writing a Game Boy emulator is a fun project! Especially if you grew up playing Game Boy games, it’s the right mix of nostalgia, challenge, and fun. There’s a ton of wonderful resources out there about the guts of the Game Boy, but fewer about how to actually program a Game Boy emulator. Where do you start? What order should you implement things? What should you ignore? All your questions will be answered here!

What is an emulator anyway?

An emulator is a program that pretends to be the hardware of a different kind of computer. So a Game Boy emulator is a program that pretends to be Game Boy hardware.

Practically speaking, a Game Boy emulator:

  • reads a ROM file.
  • interprets the binary data in that ROM file as CPU instructions.
  • reads the Game Boy’s graphics and audio systems and turns it into something the host computer can output.
  • sends button input from the host computer to the emulated system.

Starting out

So you’ve decided to make a Game Boy emulator. You’ve got a blank project in your favorite language. What next?

You should probably get a ROM file to test with! Traditionally, Tetris is very simple and the game a lot of people use to test their emulators with at first. But if you don’t have a copy of Tetris just lying around, there’s a ton of cool and free homebrew Game Boy games you can download these days.

Once you have your ROM, you’ll wanna read it in and start executing those bytes as CPU instructions. I think this blog post is a great introduction on how to do that.

This is kind of a long process. There are a lot of CPU instructions in the Game Boy. It’s a little boring, honestly, but stick with it! If it helps, you won’t need all of the instructions right away. A fun way to do this is to run your test ROM and just implement the instructions as it gets stuck on one.

Finding bugs

Once you have a somewhat fleshed out CPU, you’ll wanna start throwing tests at it. In the Game Boy emulation world, there are a lot of ways to test your emulator out there, but I think SingleStepTests is the best option. If you can make your emulator hook into those JSON files, you’ll have a 500,000 test suite for your emulator for free.

Graphics!

Once your CPU is mostly working, you can start on graphics. There’s two main ways to write your graphics system: a more simple line-based renderer, or a more accurate pixel FIFO renderer. I’d recommend starting off with a line based renderer! It’s good enough for 99% of games. Another blog post in the same series as earlier is a pretty good resource to do that. If you’re set on the more accurate option, this blog post is pretty detailed.

The Game Boy PPU has a lot of options to tweak, but not all games use everything.

Finding more bugs

Like the CPU, there are many ways to test your PPU, but this is my favorite option.

Input 🎮

Comparatively, this part is pretty easy! Read up about input on Pan Docs and hook it up your windowing framework of choice.

A working emulator?!

Putting everything together, you should now be able to play most games! Congratulations, you’ve made a Game Boy emulator! Your next steps are audio or CGB support.