GBDK libraries documentation
PrevChapter 3. Accessing hardwareNext

Multiple banks

The GB supports ROMS of up to 1.5MB and up to 32kB of RAM by using a bank switching method. Bank 0 of the ROM is always located in the region 0000h - 3FFFh and cannot be swapped but any other bank can be swapped into the high ROM region between 4000h to 7FFFh. Unfortunately GBDK doesn't support programs bigger than 32kB at the moment which is partly due to lcc assuming a flat address space. However you can manually access the other banks using the switch_rom_bank(UBYTE) and switch_ram_bank functions. See banks.c for an example.

The ROM and RAM bank that the code should exist in is specified at compile time using the -Wf-box and -Wf-bax compiler options where x is the bank number between 1 and 31. Note that this means that you cant switch banks within one code file but multiple files can exist in the same bank. If neither the -bo or -ba options are given then the default _CODE and _BSS segments are used. Dont forget that local variables are allocated on the stack inside _BSS.

For example, suppose the code:

int silly_fun( int a )
    printf("%i times %i is ", a, a, a*a+1 );
    return 0;
was compiled with the -Wf-bo1 compiler option then the code would be stored in segment _CODE_1. To call this function from your main routine you would use:
int main(void)
    switch_rom_bank( 1 );  /* Select bank 1 with segment _CODE_1 */
	silly_fun(5);      /* Prints "5 times 5 is 26" */

	return 0;
Note that you obviousally cannot do a switch_rom_bank call from inside any segment but _CODE as otherwise you'd switch yourself out. Note also that all global routines like printf must fit within the limited 16k of bank 0.

When linking all the object files together the number of banks used should be specified with the -Wl-yox and -Wl-yax flags and the MCB type with the -Wl-ytx flag. The current supported values for x in -Wl-ytx are:

1 : ROM+MBC1
5 : ROM+MBC2

InterruptsUpFunctions in RAM