Technical information
AN2216/D
18 MC9S12DP256 Software Development Using Metrowerkâs Codewarrior MOTOROLA
Linker Optimizations
for the BANKED
Memory Model
In the BANKED memory model all user defined functions are of type 'far' by
default. All functions are therefore called using the CALL instruction and
terminated with the RTC instruction. There is clearly a price to be paid for this
simplistic approach, as some functions which are called only by other functions
within the same page of memory could be called with a JSR/RTS instruction
sequence. Compared with a JSR/RTS sequence, a CALL/RTC sequence
typically requires one extra byte of code memory and 5 extra cycles to execute,
so the overhead is not great. In applications where this overhead cannot be
afforded, the default behaviour can be overriden. This can be done manually
by grouping functions together into specific segments each of which are
allocated to a single page in memory. Then all functions which are only called
by other functions within the same page can individually be declared with the
'near' type qualifier. Clearly this would be a difficult and laborious task even for
a relatively simple piece of code, but fortunately the Codewarrior linker is able
to perform this task automatically with the linker 'distribute' feature, activated
with the command line option, -Dist. This feature also minimises the amount of
unused memory in each of the pages. When using this feature, building an
application becomes a two-pass process.
The linker 'distribute feature' will distribute all functions in a single code
segment into paged memory sections. All functions which are to be optimized
must therefore be included in a single code segment. The default code
segment name DISTRIBUTE is expected by the linker, if a different name is to
be used then the linker must be informed of the name with the -DistSeg option.
With a distribution segment defined, the application is then compiled as normal.
In this first compiler pass, all functions are of type 'far' by default.
Before the application can be linked, the linker command file must be modified
so that the linker can be informed of which sections may use the near
inter-bank calling convention and which sections must used a far inter-bank
calling convention. This is done with the attributes IBCC_NEAR and
IBCC_FAR. An example of this is shown in Figure 19. Note also that the
DISTRIBUTE segment is placed with the DISTRIBUTE_INTO keyword.
SECTIONS
RAM = READ_WRITE 0x1000 TO 0x3FFF;
EEPROM = READ_ONLY 0x0400 TO 0x0FFF;
/* unbanked FLASH ROM */
ROM_4000 = READ_ONLY IBCC_NEAR 0x4000 TO 0x7FFF;
ROM_C000 = READ_ONLY IBCC_NEAR 0xC000 TO 0xFEFF;
/* banked FLASH ROM */
PAGE_30 = READ_ONLY IBCC_FAR 0x308000 TO 0x30BFFF;
PAGE_31 = READ_ONLY IBCC_FAR 0x318000 TO 0x31BFFF;
PAGE_32 = READ_ONLY IBCC_FAR 0x328000 TO 0x32BFFF;
PAGE_33 = READ_ONLY IBCC_FAR 0x338000 TO 0x33BFFF;
PAGE_34 = READ_ONLY IBCC_FAR 0x348000 TO 0x34BFFF;
PAGE_35 = READ_ONLY IBCC_FAR 0x358000 TO 0x35BFFF;
PAGE_36 = READ_ONLY IBCC_FAR 0x368000 TO 0x36BFFF;
PAGE_37 = READ_ONLY IBCC_FAR 0x378000 TO 0x37BFFF;