Note: If you haven’t read it yet, perhaps check out my post Things to know before starting Game Boy development first before starting this post.
Writing assembly code for the Game Boy isn’t as hard as it first may seem. Unsurprisingly, the CPU for the Game Boy is incredibly simple. Here are some basic things that you need to know before getting stuck into it.
1 – Devices are Memory-Mapped
Input and output are all fairly straight forward since they are memory-mapped. In short, this means that you interface with devices by reading/writing memory. Want to make a beep sound? Write a value to where the sound data is mapped. Want to read if a button is being pressed? Read a value from where the input data is mapped. The benefit of this is that once you have learnt how to read and write memory you don’t need to learn a different set of instructions for doing I/O operations.
2 – If you’re not familiar with what a Register is, you will be
For those of you who don’t know, Registers are generally the fastest memory available to a CPU and are stored within the processor itself. They are important because you can’t do operations directly on values that are in RAM or saved with your ROM. If you want to add two numbers together that are in RAM, then you will need to load them into registers and then do your operation.
On top of this there are also a few special registers, the most note-worthy of these is one that stores different flags about your last operation. Want to know if the last math operation overflowed? Want to know if the last math operation resulted in 0? Well flags for that are stored in this register.
3 – Interrupts are also important
Processors have an inbuilt mechanism for basic event-driven architecture called Interrupts. You can set flags so that when pre-specified events occur, the current CPU task is interrupted and code specific to that event is executed. There are 5 different interrupts available to you but you are probably going to dealing mostly with the V-Blank one.
4 – You may be the sheriff of this wild west, but there are rules you still need to follow
Just because you control the CPU, it doesn’t mean that you can do whatever you want. As an example, one of the restrictions you will need to work with is that you cannot write to the video memory while the LCD is writing to the screen. For the most part you will be updating the LCD during it’s V-Blank phase, which thanks to the V-Blank interrupt is pretty easy to manage.