GBDK 2020 Docs
4.3.0
API Documentation for GBDK 2020
|
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 the irq.c sample project.
Interrupts in GBDK are handled using the functions disable_interrupts(), enable_interrupts(), set_interrupts(uint8_t ier) and the interrupt service routine (ISR) linkers add_VBL(), add_TIM, add_low_priority_TIM, add_LCD, add_SIO and add_JOY which add interrupt handlers for the vertical blank, timer, LCD, serial link and joypad interrupts respectively.
Since an interrupt can occur at any time an Interrupt Service Request (ISR) cannot take any arguments or return anything. Its only way of communicating with the greater program is through the global variables. When interacting with those shared ISR global variables from main code outside the interrupt, it is a good idea to wrap them in a critical {}
section in case the interrupt occurs and modifies the variable while it is being used.
Interrupts should be disabled before adding ISRs. To use multiple interrupts, logical OR the relevant IFLAGs together.
ISRs should be kept as small and short as possible, do not write an ISR so long that the Game Boy hardware spends all of its time servicing interrupts and has no time spare for the main code.
For more detail on the Game Boy interrupts consider reading about them in the Pandocs.
The GameBoy hardware can generate 5 types of interrupts. Custom Interrupt Service Routines (ISRs) can be added in addition to the built-in ones available in GBDK.
lcd_isr_wobble
tim
comm
It is possible to install your own interrupt handlers (in C or in assembly) for any of these interrupts. Up to 4 chained handlers may be added, with the last added being called last. If the remove_VBL() function is to be called, only three may be added for VBL.
Interrupt handlers are called in sequence. To install a new interrupt handler, do the following:
__critical { ... }
section, install your interrupt handling routines using the add_XXX() function, where XXX is the interrupt that you want to handle.See the irq
example project for additional details for a complete example.
If you want to use your own Interrupt Dispatcher instead of the GBDK chained dispatcher (for improved performance), then don't call the add_...()
function for the respective interrupt and its dispatcher won't be installed.
Then, ISR_VECTOR() or ISR_NESTED_VECTOR() can be used to install a custom ISR handler.
By default when an Interrupt handler completes and is ready to exit it will check STAT_REG and only return at the BEGINNING of either LCD Mode 0 or Mode 1. This helps prevent graphical glitches caused when an ISR interrupts a graphics operation in one mode but returns in a different mode for which that graphics operation is not allowed.
You can change this behavior using nowait_int_handler() which does not check STAT_REG before returning. Also see wait_int_handler().
For implementation details on the NES console in GBDK, see the NES entry in Supported Consoles & Cross Compiling
GBDK sets up a Shadow OAM which gets copied automatically to the hardware OAM by the default V-Blank ISR. The Shadow OAM allows updating sprites without worrying about whether it is safe to write to them or not based on the hardware LCD mode.
By default for the Game Boy GBDK assigns:
0x8800
0x9800
0x9C00
8x8
modeIncluding stdio.h and using functions such as printf() will use a large number of the background tiles for font characters. If stdio.h is not included then that space will be available for use with other tiles instead.
drawing_vbl
and drawing_lcd
). Changing the mode to (mode(M_TEXT_OUT);
) will cause them to be de-installed. These handlers are used to change the tile data source at start-of-frame and mid-frame so that 384 background tiles can be used instead of the typical 256.There are certain times during each video frame when memory and registers relating to graphics are "busy" and should not be read or written to (otherwise there may be corrupt or dropped data). GBDK handles this automatically for most graphics related API calls. It also ensures that ISR handlers return in such a way that if they interrupted a graphics access then it will only resume when access is allowed.
The ISR return behavior can be turned off using the nowait_int_handler.
For more details see the related Pandocs section: https://gbdev.io/pandocs/Accessing_VRAM_and_OAM.html
For programs that would benefit from compression GBDK includes the gbcompress utility and companion API functions.
In addition to the built-in compression unapack is another option:
Another way to save space is using 1 bit-per-pixel (bpp) tile pattern data instead of 2-bpp or 4-bpp data. This can reduce the ROM size for groups of tiles which only require two shades of color.
Use of 1-bpp tile pattern data may be combined with the compression described above to save even more space, however that approach requires using an intermediary RAM buffer before the tile pattern data can be written to VRAM with the set_*_1bpp_data() functions.
See the ram_function
example project included with GBDK which demonstrates copying functions to RAM and HIRAM.
Warning!
Copying of functions is generally not safe since they may contain jumps to absolute addresses that will not be converted to match the new location.
It is possible to copy functions to RAM and HIRAM (using the memcpy() and hiramcpy() functions), and execute them from C. Ensure you have enough free space in RAM or HIRAM for copying a function.
There are basically two ways for calling a function located in RAM, HIRAM, or ROM:
The second approach is slightly more efficient. Both approaches are demonstrated in the ram_function.c
example.
The following is primarily oriented toward the Game Boy and related clones (sm83 devices), other targets such as sms/gg may vary.
You can mix C and assembly (ASM) in two ways as described below.
NAKED
keyword may be used to indicate that the funtion setup and return should have no handling done by the compiler, and will instead be handled entirely by user code.PRESERVES_REGS
keyword may be used as additional hinting for the compiler. For example PRESERVES_REGS(b, c)
. By default it is assumed by the compiler that no registers are preserved.Example:
__asm__("nop");
Another Example:
void some_c_function() { // Optionally do something __asm (ASM code goes here) __endasm; }
It is possible to assemble and link files written in ASM alongside files written in C.
i
will be called _i
in assembly..globl
directive.Here is an example of how to mix assembly with C:
main.c
uint16_t add(uint16_t, uint16_t); main() { uint16_t i; i = add(1, 3); }
add.s
.globl _add .area _CODE _add: ; uint16_t add(uint16_t First, uint16_t Second) ; ; In this particular example there is no use and modification of the stack ; No need to save and restore registers ; ; For calling convention __sdcccall(1) ; - first 16 bit param is passed in DE ; - second 16 bit param is passed in BC ; Load Second Parameter ("Second") into HL ld l, c ld h, b ; Add Parameters "Second" + "First" add hl, de ; Return result in BC ld c, l ld b, h ret ; 16 bit values are returned in BC
Data from binary files can be included in C source files as a const array using the INCBIN() macro.
See the incbin
example project for a demo of how to use it.
somevar[n] = {x}
will NOT get initialized with value x
. This may change when the SDCC RLE initializer is fixed. Use memset for now if you need it.-Wf-ba*
will force that variable to be in WRAM instead.0xA5
to a variable in bank 0
and assigned to address 0xA000
using the AT() directive:// Workaround for initializing variable in SRAM // (MBC RAM and Bank needs to get enabled during GSINIT loading) static uint8_t AT(0x0000) __rRAMG = 0x0a; // Enable SRAM static uint8_t AT(0x4000) __rRAMB = 0x00; // Set SRAM bank 0 // Now SRAM is enabled so the variable can get initialized uint8_t AT(0xA000) initialized_sram_var = 0xA5u;