Emulation Closet: Gameboy

posted by Jabo Blog
I spent a large part of my emulation life building a NES emulator and playing games, however the original Gameboy has always been a favorite handheld for me. Through the years I've been asked if I would build emulators for other consoles, but the time and dedication to building a polished emulator makes it difficult to do more. Recently I decided to go through some of my old projects and remembered that I had written a relatively functional Gameboy emulator, come with me on a journey to rediscover this project.



Original Gameboy (DMG-01, 1989)



Technical Details

On the surface it has a tiny 160x144 screen, a limited palette of green colors, and lacks that thunderous triangle audio channel that NES enjoys. However as I looked past the crude graphics I found many interesting features coming from the NES:


Sharp LR35902 CPU
A very accessible instruction set, inspired by the Z80, where source and destination registers are available with many opcodes, not to mention 16-bit registers for memory addressing. While you might be impressed with the 4 MHz clock rating, most of the opcodes are double the clock cycles of related processors such as the 6502. Here is an example snippet of code:

ld b, a add a, a ; *2 add a, b ; *3 add a, a ; *6
Allows you to multiply the accumulator by 6 since the Gameboy had no multiply instruction.
Easier Memory Map
I definitely preferred dealing with the memory map in the Gameboy compared to the NES. It's nice to see a smaller screen reduced the entire memory map to fit into a 16-bit space, nicely done gentlemen. Here is a simplistic view of the address space:

0150-7FFF Cartridge ROM 8000-9FFF Video RAM A000-BFFF Cartridge RAM (if available) C000-DFFF Internal RAM FE00-FE9F Sprite (OAM) FF00-FF7F I/O Registers
The Gameboy included 8Kb RAM, where the NES had a smaller 2Kb RAM.
PPU Windowing
Coming from the NES this was a really cool feature I came to appreciate. On the NES it's relatively annoying to provide a sub-menu or status menu area due to the fact that these are often stored separately from your main window, which is scrolling. Windowing provides the ability to overlay these layers natively so the game does not have to manipulate registers with pixel-precision in order to snap in a status area, or update the screen to paint a quick sub-menu.


Megaman II uses windowing in order to provide a status area on the bottom with player health.

This technique is more modern and far easier to program.
Scanline IRQ via LCDY
Being able to trigger an IRQ on a specific scanline allowed games to create special effects. While most consoles had this feature, it was a native feature to the Gameboy not implemented in cartridge hardware like the NES.


World Heroes 2 Jet triggers an IRQ to produce a drip effect for it's logo rendering.

Techniques like this were a common way of providing graphical effects since animations may cost additional storage space.

Also worth a mention is the Timer IRQ capabilities in the Gameboy, which I can only assume was used to update music in some games, or for more advanced graphical effects.
4-bit Waveform Audio
Having the ability to stream 4-bit PCM is quite an improvement from the NES. Waves are commonly used for digitized speech or to recreate the sound of real instruments, every retrogamer remembers the first time they heard something like this.

While there aren't any games I felt necessary to single out here, there is a great YouTube video with examples, and an article that tracked more games if you are interested in exploring.
Stereo Audio Mixer
While I did not have the opportunity to emulate audio on the Gameboy platform, I found it compelling that there is a register NR51 that allows the programmer to specify how each audio channel is mixed as stereo audio. Unfortunately the Gameboy included a single speaker, I can only assume there was not much motivation for developers to take advantage of this.

Looking back clearly the Gameboy benefited by being released in 1989, well after the NES in 1983. If you have played many games on the platform it may not seem like Gameboy games were much, if at all better despite some of these technical improvements.

Emulation


Somewhere along my emulation adventures, around 2003, I got interested in Gameboy emulation. The platform has striking similarities to the NES in terms audio and graphics, so I can only assume I wanted to learn more about it. I never set out to create a fully featured emulator, there were plenty of those. Looking back on it almost 20 years later I was surprised by a few things about this project. First like many programmers I was slightly shaken by the code I wrote so long ago, but more importantly with a few hours of updating the DirectX code I was actually able to get some games running. From a technical perspective it uses an interpreter for the CPU and a scan-line renderer for the PPU, a pretty classical emulation setup.

I thought it would be fun to dig up the past a little bit and walk through some of my favorite games and see if the old code still works before I put it back in the closet for another lengthy amount of time.

Game Library


Perhaps the best part of the Gameboy was that the platform had longevity and enjoyed a vast library of games. In addition to offering games that appeared on the NES, the Gameboy often bridged a gap between the NES and SNES with more modern console ports such as Street Fighter 2, Mortal Kombat 2, King of the Fighters, and World Heroes Jet.



This combined with incredible exclusive titles such as Donkey Kong Land, Metroid 2, and Legend of Zelda Link's Awakening made the platform a viable alternative to playing games on a television.


As with any library of games, not every game was great, especially since anyone creating Gameboy titles started off with serious visual challenges, and a tighter budget for development. As a gamer the novelty of it being handheld often made it easier to overlook some serious flaws in games.

Final Thoughts

It's been a long time since I looked at not only the Gameboy platform, but also an emulator that I created so long ago. I'm not going to lie, this was a huge trip back in time for nostalgia, I really enjoyed getting back into this. I don't think I will be picking this project up any time soon, it's a very raw basic emulator that lacks polish and audio emulation. Not to mention that the games have not held up well over time.

I probably have other projects laying around I should go through.
Comments 3

sirpooplol 1607604675000 #1

anyway I could try out your old gameboy emulator, I'm really interested what old emulator are like from the past

Jabo 1616017916000 #2

anyway I could try out your old gameboy emulator, I'm really interested what old emulator are like from the past

If it wasn't in such a terrible state I would be tempted to upload it, I am flattered you would spend time looking at it, for now though it's best it remains in an archive
REPLY updated 1616088976000

Bobcat15 1625463223000 #3

It's fascinating to learn about these gaming systems and how they work. That's what's so cool about emulation. We get to learn more about the games and the hardware that gave us awesome memories. I hope you'll continue to write more about what you've discovered while adventuring in emulator engineering.

Add comment



Please be respectful and post comments relevant to the article.