GBDK libraries documentation
PrevNext

Chapter 3. Accessing hardware

Table of Contents
Memory
Interrupts
Multiple banks
Functions in RAM

Memory

There are two main ways of accessing the memory of the GB directly. The first which I recommend is using casting, where the second is by declaring an address as external and defining it at link time. Both are best illustrated by example. Suppose that you want to turn on sound by writing 8fh to SND_STAT (FF26h). The code using casts is

#define SND_STAT    (UBYTE *)0xFF26U

void sound_on(void)
{
    *SND_STAT = 0x8FU;
}
				
while the code using late linking is
extern UBYTE snd_stat;

void sound_on(void)
{
    snd_stat = 0x8FU;
}
				
where snd_stat is defined at link time by adding -gsnd_stat=0xff26 to the linker arguments.

The most comonly used hardware registers are pre-defined in hardware.h using the late linking method. This code

#include <hardware.h>

void sound_on(void)
{
    NR52_REG = 0x8fU; /* 'NR52_REG' maps to '_reg_0x26' or SND_STAT */
}
				
achieves the same as the other two examples.


PrevHomeNext
Other Interrupts