GBDK libraries documentation
PrevChapter 3. Accessing hardwareNext


Interrupts allow execution to jump to a different part of your code as soon as an external event occurs - for example the LCD entering the vertical blank period, serial data arriving or the timer reaching its end count. For an example see irq.c

Interrupts in GBDK are handled using the functions disable_interrupts(), enable_interrupts(), set_interrupts(UBYTE ier) and the interrupt service routine (ISR) linkers add_VBL, add_TIM, add_LCD, add_SIO and add_JOY which add interrupt handlers for the vertical blank, timer, LCD, serial and joypad interrupts respectively. The system supports up to eight ISRs per interrupt, executing the first one installed first.

As an example, this code installs an interrupt handler that increases count every time the timer runs out.

UWORD count;

void timer_isr(void)

int setup_isr(void)

Note that this assumes that the timer is setup elsewhere and the use of the global variable. All registers are pushed before the ISR is called.

As an interrupt can occur at any time, an ISR cannot take any arguments or return anything. Its only way of communicating with the greater program is through the global variable above. Note how interrupts have to be disabled before adding the timer ISR and that the set_interrupts call will disable any other interrupts. To use multiple interrupts, or the relevant IFLAGs together.

ISRs must be kept as small and short as possible and as at 2.0b13 cannot use any long longs or floating point variables as the code for them is not re-entrant. It is possible to write a ISR long enough so that the GB spends all of its time servicing interrupts and has no time spare for the main code.

Accessing hardwareUpMultiple banks