How to make a (GameBoy) emulator?
Petar Veličković
University of CambridgeC�������� C����� S����� S�����

13 July 2016

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

What is emulation?

Emulation

I Emulation is a computer science concept corresponding toimitation of one computer system on another.
I Contrast with simulation, which is just an imitation of aparticular feature/functionality (e.g. of a video game).
I Emulation can be performed in both hardware and so�ware;in this talk, we will focus only on so�ware emulators.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

What is emulation?

Emulator vs simulator

PSX emulator

Pac-man simulator

An emulator executes the original code (ROM) wri�en for the console,whereas the simulator represents an entirely new game.
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

History and specs of GameBoy

GameBoy

I The GameBoy is a video gaming console released by Nintendoin 1989.
I First notable handheld console, in spite of technologicallysuperior competition:

Together with its successor (Game Boy Color), sold over 119
million units (by far the most popular handheld console prior
to the release of the Nintendo DS).
I The Tetris cartridge alone sold 35 million copies!
I

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

History and specs of GameBoy

Console exterior

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

History and specs of GameBoy

Under the hood

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Overview of the GameBoy emulator

Today’s task
Cartridge

GameBoy⇡⇡insert1100 0011 0000 1100
0000 0010 0000 0000
0000 0000 0000 0000

input

0000 0010 1111 1111
1111 1111 1111 1111

ROM (Game code)
How to make a (GameBoy) emulator?

Emulator
Petar Veličković

Introduction

Essential emulator

Emulator extensions

Overview of the GameBoy emulator

Modular approach

I We will adopt a modular approach in our implementation – theemulator will consist of (nearly) independent
implementations of all of its subsystems (⇠ modules)!

I Why do this?

Debugging becomes much easier because the e�ects ofprogrammer errors are local to a subsystem.
I The modules may be reusable in other emulator projects – thehardware is standardised.
I Amenable to object-oriented programming.
I

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Overview of the GameBoy emulator

Overview of the emulator

Processor

Memorycontroller

MemoryROM
RAM

Cartridge
readerInterrupts

InputGPUSprites

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Processor

Processor

I Usually the most involved and complex module in the emulator

implementation (which is why programmers usually tend touse pre-made CPU implementations).
I Several approaches are possible—we will focus on writing aninterpreter, which “follows” the game code and simply
simulates every instruction it encounters using the local CPU.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Interpreter emulator

IFDCEX

Instruction Fetch

DecodeExecuteI Interpreters usually simulate the fetch-decode-execute cycle,

present in most contemporary processors:

IF Instruction Fetch – fetch the current instruction;
DC Decode – determine the type of the instruction;
EX Execute – execute the current instruction, updating the state of
the processor, memory, etc.

I This way, we can only emulate systems whose processors areseveral orders of magnitude slower than the native one.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

ProcessorInstruction FetchIFInstruction Fetch

I Where is our program???
I With GameBoy, and most contemporary systems, both thecode and data are stored in the same memory (⇠ von Neumannarchitecture).
I=) We need methods for reading from and writing tomemory, if we’d like to be able to fetch the current instruction.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Memory interface

I The GameBoy does all of its computations on the level of asingle byte; therefore, it makes sense to expose two methods:
ReadByte(address);
WriteByte(address, value);to the CPU.

I For now, the processor only needs to know that these functions

exist—we will consider their actual implementations later.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

DecodeDC
Decode

I A�er fetching the current byte of the program, we need todetermine the instruction that it represents (and hence
whether we need to read additional parameters from memory).
I This requires us to be well acquainted with the instruction set

architecture (ISA) of GameBoy’s processor (which is very
similar to the Zilog Z80 ISA).

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

GameBoy arhitecture
I Since we have read a single byte of our program as aninstruction, this allows us to consider 28 = 256 distinct

instructions.
I It is necessary to implement an enormous branching(switch–case is an appropriate construct to use), which will
select the instruction to execute based on this byte’s value.
I This is easily the most cumbersome part of theimplementation. . .

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulatorEmulator extensions

Processor

ExecuteEX
ExecuteI Each instruction may a�ect the processor’s internal state (⇠registers), as well as the state of memory.
I Furthermore, every instruction requires a certain(predetermined) number of CPU ticks/cycles (will be very
important later!).
I The previously mentioned WriteByte method may be used fore�ects on memory.
I In order to implement all these instructions, we need toimplement and maintain the processor’s internal state as well.
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Processor state
The CPU’s current state is fully specified by the following registers:
PC Program Counter — the memory address of the next instruction
to be fetched;
SP Stack Pointer — the memory address of the top of the stack(omi�ed from this talk);
F Flags;
A-E,H,L General purpose registers.
I We will also maintain the number of elapsed ticks since thestart of execution.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

ProcessorFlags
I Flags are logical (true/false) variables that allow theprocessor to determine the e�ects of the previously executedinstruction.
I With GameBoy, the flags register looks like this:765430ZF OF HCF CFI The aforementioned flags are:

ZF Zero Flag — set if the result of the operation is zero;
OF Operation Flag — set if the operation was subtraction;
HCF Half-Carry Flag — set if there was an overflow in the lower halfof the result (omi�ed);
CF Carry Flag — set if there was an overflow in the result.
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

CPU state, cont’d...

1125

6

73

PC

SPA

B

C

T

F

F

F

15732

ZF

OF

HCF

CFticks

CPU
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Example: Sub(A, 5)
Memory6

121 214 5

0 255 4...

910 11 12 13 14

1125

673

PC

SPA

B

C

T

F

F

F

15732

ZF

OF

HCF

CFticks...

Address...

CPU
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Example: Sub(A, 5)Memory
6121 214 5

0 255 4...

910 11 12 13 141125

673

PC

SPA

B

C

T

F

F

F

15732

ZF

OF

HCF

CFticks...

...

Address

Sub(A, n)CPU
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Example: Sub(A, 5)
Memory6121 214 5

0 255 4...

910 11 12 13 141225

673

PC

SPA

B

C

T

F

F

F

15732

ZF

OF

HCF

CFticks...

...

Address

Sub(A, 5)CPU
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Processor

Example: Sub(A, 5)
Memory6121 214 5

0 255 4...

910 11 12 13 141325

173

PC

SPA

B

CF

T

F

F

15739

ZF

OF

HCF

CFticks...

...

Address

Sub(A, 5)CPU
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

MemoryMemoryMemorycontroller

MemoryROM
RAM

I It is necessary to correctly implement the ReadByte andWriteByte methods: we need to emulate the memory
controller, i.e. the memory management unit (MMU).

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

MemoryNaïve implementation
I PC has 16 bits, and therefore the GameBoy memory system
supports 216 = 65536 addresses.
I The naïve implementation is very simple. . .
int memo [65536];
public int ReadByte ( int address )3 {4return memo [ address ];5 }6 public void WriteByte ( int address , int value )7 {8memo [ address ] = value ;9 }12I Can you think of some reasons why this is not so easy?

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

MemoryGotchas

I Generally, logical address 6= physical address:

Not all writes should succeed (ROM. . . );Not all addresses represent main memory (e.g. a special address
allows the CPU to read keypad inputs. . . );I Two di�erent addresses could be mapped to the same physicallocation.
I ...
II

I With GameBoy, all of the above hold!

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPUGPUGPUI So far, we have managed to design an emulator that can, stepby step, execute any GameBoy program.
I Now we are interested in seeing the results of our work on agraphical output.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPUGameBoy graphics
I GameBoy’s graphical output is an LCD with a 160 ⇥ 144 pixelresolution =) we can maintain a colour matrix of such size,and display it in our emulator.

I The values in this matrix have the following format:

0xAARRGGBB (Alpha–Red–Green–Blue)I GameBoy supports four shades of gray, so the matrix may

contain only these four values:

0xFF000000 (BLACK)
0xFF555555 (DARK_GRAY)
I 0xFFAAAAAA (LIGHT_GRAY)
I 0xFFFFFFFF (WHITE)I
I

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPURendering a single frame (⇠ CRT)
Line 1
HBlankLine 2
HBlankLine 3
HBlank
VBlankHBlankLine 143
HBlankLine 144
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPU

When do we draw a new frame?
I Every stage in the previous figure takes a specific amount oftime:Phase
Line (background)Line (sprites)
Horizontal blank
Single line
Vertical blank
Entire frameTicks
172
80204456
4560
70224

I How could we keep track of when to trigger each phase?

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPUTiling system
I GameBoy doesn’t have enough memory to keep track of theentire matrix of pixels; instead, we use a tiling system.
I VRAM contains “tiles” of size 8 ⇥ 8, and the background isrepresented as a 32 ⇥ 32 matrix of pointers to the tiles.

I Observe that this gives us a total resolution of 256 ⇥ 256, yet

GameBoy’s LCD is 160 ⇥ 144!
I

Two special-purpose registers, Scroll-X and Scroll-Y,
contain the coordinates of the point on the background that will
be located in the upper-le� corner of the LCD.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

GPU

Tiling system, cont’dTile 1

1..
.

2..
.

2..
.

Tile 33 ...
.. . .
.
.

Background map

How to make a (GameBoy) emulator?

Tile 2

Background

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Input

Input

Input

I With the GPU implemented, the emulator is able to display

graphical output, but the user/player still has no control overthe game.
I Next step: emulating user input.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Input

Input
I Input is processed using a special-purpose JOYP register, which

the processor may access at address 65280. The wires leading
to that register are connected in the following way:

#START"SELECT!

AB

765
4
3
2
10

JOYP

I The processor first writes 0x10 or 0x20 to the register, so it can

activate one of two columns; then, a�er several ticks, it canread the least significant 4 bits in order to infer which bu�ons
were pressed.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Sprites

Sprites

Sprites

I With the input processed, we can play some games – but weusually have to play blindly (there is no way the player can tellwhat his/her position is).
I This issue (and many others) is solved by using sprites – tiles

which may be drawn and moved independent of thebackground.

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Sprites

Sprites: additional details
I Sprites use exactly the same type of 8 ⇥ 8 tiles as thebackground.

I Specific parameters, maintained for every sprite in the OAM:

(x, y) coordinates of the upper-le� corner;
priority: is it in the foreground or behind the background;I flip: horizontal or vertical;
I size: all sprites can be resized to 8 ⇥ 16 at the same time.
II

I The frame rendering algorithm: first draw the background,then infer (based on colours and sprite priority) whether todraw the sprite over the background.
Sprite above background
Sprite below background

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Interrupts

Interrupts
Interrupts
I With sprite support, some simpler games work exactly asexpected.
I Most games require a very important detail: notifying theprocessor when it may render the next frame.
I The most convenient time for changing the frame is duringVBlank, because during the line change, the GPU doesn’t
access either VRAM nor OAM.
I The best (in terms of resource management and requirements)
method for notifying the processor about an important event
such as entering VBlank is using interrupts.
How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Interrupts

Fetch-decode-execute cycle, revisitedIFDCEX

Instruction Fetch

DecodeExecuteIRQInterrupt Request
I A�er every executed instruction, the CPU needs to check

whether an interrupt was raised; if an interrupt is detected, theCPU:
Remembers its current state;
Jumps (sets the PC) to the address of the interrupt handler.
I Executes the interrupt handler code, which ends with a special
RETI (Return From Interrupt) instruction, which restores state.
II

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Interrupts

Interrupts: GameBoy specifics
I The processor has access to the Interrupt Enable (IE) registers,

through which it can specify which interrupts (out of 5 in total)
it is willing to handle at this point.
I

If multiple interrupts are detected, the one with the highest
priority is handled first.

I The processor also has an Interrupt Master Enable (IME) switch,which allows it to completely deactivate interrupt handling(this is done during e.g. processing an interrupt, because weshould not handle two of them at the same time).

How to make a (GameBoy) emulator?

Petar Veličković

Introduction

Essential emulator

Emulator extensions

Interrupts

GameBoy interrupts

Priority01
2
3
4

How to make a (GameBoy) emulator?

InterruptVBlank
LCD Status
TimerSerial
JoypadAdress of the handler
0x00400x00480x00500x00580x0060Petar Veličković

Introduction

Essential emulator

Emulator extensions

Interrupts

We’re done!
I Even with only implementing the VBlank interrupt, ouremulator is capable of running Tetris. :)I All details about GameBoy’s functionalities, as well as theGame Boy Color console, may be found on the following(extremely useful) reference page:
http://problemkaputt.de/pandocs.htmI For any further queries, feel free to contact me:

pv273@cam.ac.uk
I Thank you!

How to make a (GameBoy) emulator?

Petar Veličković