Bankswitching (a.k.a. code banking) is a technique to increase the code space above the 64k limit of the 8051.
8000-FFFF | bank1 | bank2 | bank3 |
0000-7FFF | common | ||
SiLabs C8051F120 example |
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 (e.g.
BANK1). To do this you either use --codeseg BANK1 (See 3.2.9)
on the command line when compiling or #pragma codeseg BANK1 (See
3.19) 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=0x18000 (See 3.2.3). 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.