EMUKING

How Game Boy emulation actually works

An emulator is not a video player and it is not a port. It is a small model of real hardware, rebuilt in software, running the original game code instruction by instruction.

When you load a game into EmuKing, nothing about that game has been converted or rewritten. The file is the same machine code that shipped on the cartridge in 1996. What changed is the machine reading it. Instead of a Sharp processor on a circuit board, the instructions are carried out by a program in your browser that behaves the way that processor did.

That distinction explains most of what people find surprising about emulators, including why some run poorly, why timing bugs are so common, and why accuracy is harder than speed.

The processor

The Game Boy uses a chip usually called the LR35902, running at about 4.19 MHz. It is close to a Z80 and close to an Intel 8080, but it is not quite either one. It has its own instruction set with some instructions the others lack and some notable omissions.

Emulating it means writing a loop that does what silicon did:

  1. Read the byte at the program counter. That byte is an opcode.
  2. Work out which instruction it represents.
  3. Carry out that instruction, updating registers, flags and memory.
  4. Add the number of cycles the real chip would have spent doing it.
  5. Move the program counter on and repeat.

Step four is the one that separates a working emulator from a convincing one. Every instruction on real hardware takes a known number of clock cycles, and the rest of the console is keeping time against that same clock. If your CPU model runs instructions correctly but reports the wrong cycle counts, everything else drifts out of step with it.

The picture processor

The Game Boy does not draw a frame all at once. Its picture processing unit, the PPU, builds the image one horizontal line at a time, exactly like the scanning beam in a television.

A full frame is 154 scanlines. Only 144 of them are visible; the remaining 10 are the vertical blanking period, the pause after the last visible line. Each line takes 456 clock cycles, so a frame is 70,224 cycles. At roughly 4.19 MHz that lands very close to 59.7 frames per second.

Within each visible line the PPU moves through modes: reading which tiles to draw, reading the pixel data for them, then drawing. While it is busy, parts of video memory are off limits to the CPU.

Games exploit this relentlessly. The vertical blank is the one safe window to move large amounts of data, so it is where most games do their heavy lifting. Many effects depend on changing a register partway down the screen, between one scanline and the next: split status bars, wavy water, screen shake, parallax backgrounds. None of that works unless the emulator advances the PPU in step with the CPU rather than drawing whole frames in one go.

This is why "it runs at 60 frames per second" says very little about an emulator. The question is whether it is doing the right thing within each frame. EmuKing advances the PPU cycle by cycle alongside the CPU, which is what makes mid frame effects appear correctly.

Cartridges were not just storage

The Game Boy can only address 32 KB of cartridge space at once. Plenty of games are far larger than that, so cartridges shipped with a chip that swapped different parts of the ROM into view on request. These are memory bank controllers, and an emulator has to model them too.

ControllerRoughly what it added
MBC1Up to 2 MB of ROM, with switchable save RAM
MBC2Smaller, with a little built in memory for saves
MBC3Larger games, plus a real time clock that kept running with the console off
MBC5Bigger still, and reliable at Color double speed

The real time clock in MBC3 is the reason the day and night cycle in some Game Boy Color games keeps moving while the console sits in a drawer. There is a battery in the cartridge powering a clock chip. An emulator has to decide what "now" means for that clock, and reproduce the odd behaviour of the registers games read it through.

Sound

The audio hardware has four channels: two pulse wave generators, one channel that plays a short waveform you upload yourself, and a noise channel used for percussion and explosions. There is no sample playback in the modern sense. Music is written as instructions to those four channels, updated many times a second.

Emulating audio means generating those waveforms at your sound card's sample rate while keeping them locked to the emulated clock. If the two drift apart you get clicks, or audio that gradually runs ahead of the picture.

What Game Boy Color changed

The Color model kept the same processor but added a switch that doubles its speed, more memory, colour palettes instead of four shades of grey, and a faster way of copying data into video memory during horizontal blanking. Games can flip the speed switch while running, so the emulator has to change how it counts time on the fly.

How you can tell an emulator is accurate

The community settled on test ROMs: small programs that put the hardware through precise sequences and report a pass or fail. Blargg's test suite is the best known. Its cpu_instrs collection works through every processor instruction and checks the results, including the flag bits that most homemade emulators get subtly wrong.

They matter because they fail loudly. A game with a timing bug might just look slightly wrong in one room, and you would never trace it back. A test ROM tells you exactly which instruction is broken.

EmuKing's Game Boy and Game Boy Color core passes all eleven of the cpu_instrs tests.