Bankswitching Application Note ============================== Bankswitching a.k.a. code banking is a technique to increase the code space above the 64k limit of the 8051. Hardware -------- FFFF +--------+--------+--------+ | | | | | bank 1 | bank 2 | bank 3 | | | | | 8000 +--------+--------+--------+ | | | common | | | 0000 +--------+ SiLabs C8051F120 example Usually it uses some sfr (a port or internal) to select a bank and put that in the banked area of the memory map. Because the selected bank usually becomes active immediately you cannot jump or call directly from one bank to another and need to use a so-called trampoline in the common area. For SDCC an example trampoline is in crtbank.asm and you may need to change it to your 8051 derivative or schematic. The presented code is written for the C8051F120. When calling a banked function SDCC will put the LSB of the function's address in r0, the MSB in r1 and the bank in r2 and then call this trampoline __sdcc_banked_call. The current selected bank is saved on the stack, the new bank is selected and an indirect jump is made. When the banked function returns it jumps to __sdcc_banked_ret which restores the previous bank and returns to the caller. Software -------- When writing banked software using SDCC you need to use some special keywords and options. You also need to take over a bit of work from the linker. To create a function that can be called from another bank it requires the keyword banked. The caller must see this in the prototype of the callee and the callee needs it for a proper return. Called functions within the same bank as the caller do not need the banked keyword nor do functions in the common area. Beware: SDCC does not know or check if functions are in the same bank. This is your responsibility. Normally all functions you write end up in the segment CSEG. If you want a function explicitly to reside in the common area put it in segment HOME. This applies for instance to interrupt service routines as they should not be banked. Functions that need to be in a switched bank must be put in a named segment. The name can be mostly anything upto eight characters (f.e. BANK1). To do this you either use --codeseg BANK1 on the command line when compiling or #pragma codeseg BANK1 at the top of the C source file. The segment name always applies to the whole source file and generated object so functions for different banks need to be defined in different source files. When linking your objects you need to tell the linker where to put your segments. To do this you use the following command line option to SDCC: -Wl-b BANK1=0x018000. This sets the virtual start address of this segment. It sets the banknumber to 0x01 and maps the bank to 0x8000 and up. The linker will not check for overflows, again this is your responsibility.