FlappyBoy: Making a simple Game Boy game
Building a small Game Boy game in assembly
Introduction
After watching this great talk called āThe Ultimate Game Boy Talkā given by Michael Steil I felt inspired to investigate and play a bit more with the Game Boy I had laying around.
Iāve previously implemented the same game using GBDK, which is a great tool, but I did feel like I was cheating since I was programming in C and I wasnāt really exploring the consoleās features and limitations, specially becuause GBDK provides a rich and easy to use library. It felt like killing a fly with a missile shot by a drone.
Iām not going to go into fine details of the Game Boy internals. The talk I mentioned before explains everything very well.
Weapons of Choice
Since I wanted to move away from GBDK that meant that my only option was to find an assembler and linker that would allow me to generate a ROM that could run on device. There is a nice list of tools that can be found here. I chose RGBDS which is a well documented toolchain for assembling my program. I also needed a way of getting the ROM to play on my Game Boy and a tool for testing and debugging. For the first thing I went with the EverDrive GB, itās not a cheap solution but itās worth the money. You can easily load your ROMs using a micro SD card. For the emulator I decided to go with BGB. Without a doubt this has to be one of the best emulators with a debugger out there. Just look at it.
It allows you to set code and data breakpoints, explore memory, load debug symbols, visualize VRAM and the registers and so much more.
For transforming my PNG assets to valid byte data I used this nice online tool called Game Boy Tile Data Generator. Itās very straight forward.
Buildingā¦
After a while it gets a bit tedious to the execute the assembler and linker by hand. So I made this helpful and kind of generic Makefile for building my project.
My project was composed by two directories, ā./include/ā and ā./src/ā. Very C of my part. Anything that is on the ā./src/ā directory is assembled and linked into a GB file.
Programming the Game Boy
First I needed to read and understand the instruction set that the Game Boy CPU uses. Itās very similar to the Z80, but not really. I used this great manual (http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf) that compiles a great amount of information about the device, going from the register set, memory map, serial I/O, interrupts, etc. This was the document I had open all the time while programming.
I started coding the game with the idea I didnāt wanted to build a ālibraryā or an āengineā. The game is very simple and didnāt wanted to over-engineer by adding unnecessary extra code. I did how ever add simple helper subroutines that made my life easier, for example for loading big chunks of data into VRAM, clearing the MAP memory area or resetting the gameboy registers.
Here is the subroutine I used to copy assets (tile and map data) into VRAM. Itās basically a memcpy written for the game boyās CPU.
Of course this isnāt the fastest memcpy ever but it does the work.
Another subroutine I used a lot was for checking the button input.
What I do here is read the register that are used to map the current button input and store the values on memory. First I save the previous value into IO_PXX_OLD and then store the fresh input value into IO_PXX. This allows me to not only check when a button is pressed down but also when itās hit. The last one is useful when you want to do an action only when the button was initially pressed.
For example here is how I handle the ājumpingā action of the player character.
I first check if the button A was pressed on IO_P15, and then check if this value is different from IO_P15_OLD, which contains the last input update. If they are both different I continue with the jumping simulation.
Screen Effects
This is probably on what I spent most of the time working and what I am most proud of. The menu wave effect, parallax scrolling, fading the screen and even the blinking āPress Aā on the menu.
Here you can see all of them in a single animation:
Starting with the easiest. Fading is simply done by modifying the background color palette. There is a register that can be found at address $FF47. This register is known as BGP or background palette. The palette is described in 4 sets of 2 bit values. The darkest shade is 11 and the lightest is 00. 11100100 would be considered a ādefault paletteā.
What I do is simply modify this value in this order for fade in:
For fade out is the same sequence but reversed.
The wave, parallax and blinking text use the same technique in different ways.
I made the wave effect by modifying the register SCX (Scroll X) with a different offset on HBLANK. This happens for every āscanlineā of the LCD. The wave offsets was pregenerated and I just stored it in memory as a lookup table. To make it move I simply increment a pointer that references the wave lookup table.
This is a visualization of what happens:
For the parallax scrolling on the game I use a similar technique. What I do different, is that I wait for the LCY (LCD vertical line) register to hit specific values to increment the SCX register.
Something like this:
For the blinking effect I do the same thing as the parallax but instead of modifying the SCX register I flip the BGP register between %11100100 and %00000000.
The Game
Here you can play the game online
Here you can look at the source code
This is a small video of the game running on a Game Boy Color.
I am done with this game. It was a nice learning experience pic.twitter.com/ZVtbEPckoJ
— Felipe Alfonso (@bitnenfer) January 21, 2017
Conclusion
This was a great learning experience. Iāll definitely look into making more demos for the Game Boy. One thing I regret not exploring was the sound. I mostly focused my efforts into learning about how the Game Boy displayed pixels on screen. Iāll surely keep in my TODO list looking how to play sound and music in the console.