After my last week end project I got even deeper into emulation. I needed a new adventure. So I asked myself “What’s the next system I could play with?” and “What do I want to play with?”. Unlike Chip8 this had to be something I was emotionally attached to, something that played a real role in my childhood. Something I can look back at with admiration and feel proud recreating.
There is one and only one answer to all these questions. The mighty Game Boy. I spent countless hours playing with this guy, I loved it. At the same time is more challenging without being super complex. It’s the right balance between the two.
So I went scavenging for documentation. Luckily the Internet is full of informations about this wonderful piece of machinery. This gave me fuel, so I started my journey. I can no longer say that this is my first emulator, however it is for sure my first complex emulator. It has a PPU (Pixel Processing Unit, the ancestor of modern graphic cards) with backgrounds, window and sprites, an APU (Audio Processing Unit), memory bank switching, interrupts, timers and a whole lot of scary stuff to deal with. It is perfect.
How does Game Boy work?
In order to write a good Game Boy emulator I had to become familiar with the Game Boy hardware, very familiar. There are many useful resources on the Game Boy internals, one of the most helpful is the famous and very detailed Ultimate Game Boy Talk by Michael Steil.
This is a great introduction and a great source of detailed informations especially on the PPU and sound.
Another essential resource is the GameBoy CPU Manual by Marc Rawer, this is not just a CPU manual. In addition to describing in detail every opcode as a good CPU manual should, this document provides a lot of informations about the PPU, tiles, sprites, sound, I/O registers, timing and so on. I say it’s a good idea to start working on a Game Boy emulator only after this manual is in your system.
The third resource is patience, it might take some time to digest all the informations about Game Boy internals. Especially if you never wrote an emulator before. You are going to have to grasp concepts like H-BLANK, V-BLANK, Palettes, Bitplanes etc. Don’t worry, it’s all way easier than it sounds, just stick at it. And keep reading, I’ll go through and explain each of these bad boys while coding my StrangeBoy emulator.
A good plan is important
“But where do I even start building this?” and “So many things to do before I can even see something on the screen!”. Questions like these were crossing my mind, once I decided I was going to do it. I took some time to better understand how hard it would be, and where to start from. I didn’t want to end up with an unfinished project. The risk is very real, I am working full time and only have some evenings and weekends for hobby projects. Especially big projects like this tend to end up in my “One day I’ll finish it!” mental box.
So I set the bar lower, I started focusing on something that I could see working after a few days of coding. “If I can see something work” I thought “I will be happy and perhaps move on“. Boy was I right. I needed a reason to keep going, and seeing stuff happening on the screen, getting the feeling that at least something is working properly, is a very good source of motivation, it kept me going, coding, more and more just to see more stuff happening. After enough stuff happened I knew I could actually make this.
So I mentally wrote down my first three milestones:
- Display the initial scrolling Nintendo Logo.
- Display the Tetris menu.
- Play Tetris.
Breaking this monster down into small milestone kept me focused. I knew where I was going, I wanted to see this.
Tetris is probably the best game to start with. It’s a very small rom, it does not use bank switching. All you need to do is load the ROM and start executing it. At the same time Tetris is very well documented, you can find the ROM disassembled and commented, this makes things easier during debugging. So in the spirit of keeping things simple I focused on Tetris.
For my previous project, Chip8 and SuperCHIP emulator, I used Scala. That was not the best choice. I like Scala, it’s an amazingly powerful language, but after working on my first emulator I realised that it is just not the right choice for this kind of project. I explained some of the issues I had in this article. So for the Game Boy emulator I choose the good old C++. C++ is fast, has classes, pointers and a powerful standard library. I needed just that.
Ready to code
After watching the Ultimate Game Boy Talk a couple of times, reading the whole Game Boy CPU manual and doing some extra research online I was ready to start creating my Game Boy emulator. I’m going to publish my progress in the next articles, trying to break things down as much as possible. I though it would have been nice to shared my journey. Stay tuned!