gammpei's blog

RSS feed My GitHub

How to write a Game Boy emulator – Part 1: Loading the bios

Posted on 2017-02-04

This post is part of a blog series about writing a Game Boy emulator.

Many consoles have been released in the Game Boy line throughout the years:

In this tutorial, we're only going to consider the original black-and-white Game Boy. The one released in 1989. This model is called the Dot Matrix Game Boy or DMG for short. It's called like that because of the inscription "DOT MATRIX WITH STEREO SOUND" above the screen. From this point on, when I say "Game Boy", what I really mean is the DMG.

The Dot Matrix Game Boy (DMG).

The bios is a program that is stored in the Game Boy itself. It's always executed first thing on powerup. It's also called the bootstrap rom or boot rom.

The bios has a slightly different behavior whether you start the Game Boy with a cartridge or not. If no cartridge is inserted, a black bar is scrolled on the screen, and if a cartridge is inserted, the Nintendo logo is scrolled on the screen.

Game Boy startup with no cartridge.
Game Boy startup with a cartridge.

From our point of view, the bios does two things:

  1. It scrolls a black bar® or the Nintendo® logo to the center of the screen.
  2. It plays a sound.

The data for the Nintendo logo is actually stored in the cartridge itself. That's why you see a black bar if there is no cartridge. The bios reads the cartridge, and if it does not contain an exact copy of the Nintendo logo, the bios locks up. It's a way for Nintendo to control who releases games for the Game Boy: if you don't include the logo, your game won't work, and if you include it without their permission, it's a copyright/trademark violation.

The bios also does a partial checksum of the cartridge and locks up if it's not happy with the result.

I will describe these things in more detail later. For now, we need to get the bios. The DMG bios is a 256-byte file with these hashes:

Hashes for the DMG bios as seen in 7-Zip.

The Game Boy bios is a copyrighted program though, so how do we get it? Can you legally extract it from your own Game Boy? Can you legally download it if you already own a Game Boy? These questions probably have different answers depending on your jurisdiction, and frankly I'd rather not give any legal advice. It sounds like a cop-out and it is.

That being said, you don't strictly need the bios to write your emulator. Game Boy emulators out there don't ask you for the bios because they just skip its execution. For pedagogical purposes though, I think it's best to start by emulating the bios because it's the fastest way to get things displayed on the screen. Otherwise you need to mess with interrupts and timing.

Once we have the bios, we are going to:

  1. Read the bios file.
  2. Put it in a byte array.
  3. And check its SHA-256 hash.

That's it for now.

When I say to read the bios and put it in an array, I mean at runtime. Some people hardcode the bios in the source code of their emulator, but I would actually strongly advise against doing that (for legal reasons).

See the code/commit on GitHub.