Game boy emulation
Nicolas Montanaro nicolas.moe
Game boy emulation Nicolas Montanaro nicolas.moe Emulation - - PowerPoint PPT Presentation
Game boy emulation Nicolas Montanaro nicolas.moe Emulation Overview hardware or software that enables one computer system (called the host) to behave like another computer system (called the guest) game boy Architecture Split up
Nicolas Montanaro nicolas.moe
“hardware or software that enables
to behave like another computer system (called the guest)”
software components
test individually, “wire” together
00000100: 00c3 5001 ceed 6666 cc0d 000b 0373 0083 ..P...ff.....s.. 00000110: 000c 000d 0008 111f 8889 000e dccc 6ee6 ..............n. 00000120: dddd d999 bbbb 6763 6e0e eccc dddc 999f ......gcn....... 00000130: bbb9 333e 5445 5452 4953 0000 0000 0000 ..3>TETRIS...... 00000140: 0000 0000 0000 0000 0000 0001 000b 89b5 ................
“I’ve been downloading them 100% legally for years!”
Zilog Z80 / Intel 8080
registers, 2 16- bit
clock speed
instructions, 255 CB-prefixed
type Registers struct { a byte // Accumulator // Flags // ZNHC 0000 // Z = zero, N = subtract, H = half carry, C = carry f byte b byte c byte d byte e byte h byte l byte sp []byte // Stack pointer PC []byte // Program counter }
Also contains methods to modify flags
type Instruction struct { Mnemonic string // Number of T cycles instruction takes to execute // Divide by 4 to get number of M cycles TCycles uint16 NumOperands uint16 Executor func() int // Executes appropriate function }
representation as a flat byte array
correct location, begin fetch-decode- dispatch loop
memory dealing with video & I/O
Direct memory access
Abstracted memory
Executors CPU MMU Registers I/O LCD Timer
each 8x8 pixels
“background”
1:BG 2:Window 3:Sprites 4:View
Tile: Image: .33333.. .33333.. -> 01111100 -> 7Ch 22...22. 01111100 -> 7Ch 11...11. 22...22. -> 00000000 -> 00h
33...33. color numbers 11...11. -> 11000110 -> C6h 22...22. 00000000 -> 00h 11...11. 2222222. -> 00000000 -> 00h ........ 11111110 -> FEh 33...33. -> 11000110 -> C6h 11000110 -> C6h 22...22. -> 00000000 -> 00h 11000110 -> C6h 11...11. -> 11000110 -> C6h 00000000 -> 00h ........ -> 00000000 -> 00h 00000000 -> 00h
ASCII from: https://fms.komkon.org/GameBoy/Tech/Software.html
Sprite OAM: $FE00-$FEA0
Y X ID Attributes
0->Palette # (CGB) 1->Palette # (CGB) 2->Palette # (CGB) 3->VRAM bank (CGB) 4->Palette # (DMG) 5->X flip 6->Y flip 7->Above BG
Rendering pipeline
UpdateLCD() DrawFrame()
PopulateTiles() (BG only)
GenerateBGImage(bg)
PopulateTiles() (window) PlaceView(img)
GenerateBGImage(win)
Complete frame
RenderSprites() (boring stuff)
if window is enabled
Image: http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Input
Hardware selects which column it wants input from Last button pressed updated 60 times per second cols = [0x0F, 0x0F] Values altered by reading inputs Selected column = index 0 or 1 Return value
as PRNG
triggers timer interrupt when
counter overflows,
generate PRNG in divider
& 1 set speed, bit 2 sets timer on/off
interrupt not entirely correct
1 color palette
aren’t implemented, get cut off
programming, computer organization/ architecture
time to write
especially when someone else wrote the tests
creative programming