当前位置:
文档之家› Freescale 高级S12系列单片机编程
Freescale 高级S12系列单片机编程
TM
10
How to change default behavior locally
Remember that any memory model allows accesses to banked code or data, It is only the default behavior that changes ! The keywords __far and __near specify the calling convention for functions. Far function calls can “cross” pages. Near function calls must stay in the same page. How to override the calling convention of a function ? using keywords __near and __far ! In the SMALL memory model, functions are near, data is near. In the BANKED memory model functions are far, data is near. In the LARGE memory model functions and data are far by default.
TM
8
Banked Memory Model
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
TM
12
Overriding default access to variables
__near and __far keywords can also be applied to variable declarations : char __near variable; A variable declared __near is considered by the compiler to be allocated in the zero page (first 256 bytes of the memory space, address range 0x00 – 0xFF) Accessing variables in the zero page generates less code and executes faster since the address used to access them is only one byte long.
TM
6
Comparison between memory models
Let’s compare the assembly instructions generated by the compiler with the following simple code:
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
A call to a banked function is more expensive both in codesize and execution time.
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
Advanced C programming for 16-bit MCUs
Austin, Texas 25-29 September, 2006
TM
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
1
Introduction
What is this training about ?
TM
Agenda
Memory
Models functions calls by hand
Optimizing Running Running
routines in RAM a function from the Stack
TM
3
Memory Models
Models! Where?
TM
What is a memory model ?
Memory Models
A memory model is a set of rules that changes the default behavior of the compiler. It tells the compiler what instructions to generate when accessing data and calling functions. The Compiler for the 16 bits MCU’s supports three different memory models.
TM
ቤተ መጻሕፍቲ ባይዱ13
__near and __far applied to variables or data
char __far variable; A variable declared __far is considered by the compiler to be allocated in a banked memory segment an has to be accessed through a page switch routine. (Like in the large memory model) In what scenarios would we have banked data ? The only banked internal memory resource is Flash (read-only). Therefore if using only internal resources, only constant data (read-only data) can be paged. The other scenario is where we have external RAM accessed through a paged area.
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
SMALL ( code and data fit in 64Kb )
Data and functions are accessed by default with 16 bit addresses.
BANKED (code in banked memory)
Data is also accessed with 16 bit addresses, functions are called using banked calls
TM
9
Large Memory Model
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. Freescale Semiconductor, Inc. 2006.
TM
5
Memory Models
Differences when calling banked and non-banked functions
If a function is in banked memory, it has to be called differently than a function in non–banked memory. In particular: