Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 191
5.9.3
Context Switching
5.9.3.1
CONTEXT SAVING ON INTERRUPTS
Some registers are automatically saved by the hardware when an interrupt occurs. Any 
registers or compiler temporary objects used by the interrupt function, other than those 
saved by the hardware, must be saved in code generated by the compiler. This is the 
context save, or context switch code.
SeSection 5.7 “Register Usage” for the registers that must be saved and restored 
either by hardware or software when an interrupt occurs.
Enhanced mid-range PIC devices save the W, STATUS, BSR and FSRx registers in 
hardware (using special shadow registers) and hence these registers do not need to 
be saved by software. The only register that may need to be saved is BTEMP1
1
, a com-
piler temporary location that acts like a pseudo register. This makes interrupt functions 
on Enhanced mid-range PIC devices very fast and efficient.
Other mid-range PIC processors only save the entire PC (excluding the PCLATH reg-
ister) when an interrupt occurs. The W, STATUS, FSR and PCLATH registers and the 
BTEMP1 pseudo register must be saved by code produced by the compiler, if required.
By default, the PIC18 high-priority interrupt function will utilize its internal shadow reg-
ister to save the W, STATUS and BSR registers. All other used registers are saved in 
software. Note that for some older devices, the compiler will not use the shadow regis-
ters if compiling for the MPLAB ICD debugger, as the debugger itself utilizes these 
shadow registers. Some errata workarounds also prevent the use of the shadow 
registers see Section 4.8.27 “--ERRATA: Specify Errata Workarounds”.
For the low priority PIC18 interrupts, or when the shadow registers cannot be used, all 
registers that has been used by the interrupt code will be saved by software.
The compiler determines exactly which registers and objects are used by an interrupt 
function, or any of the functions that it calls (based on the call graph generated by the 
compiler), and saves these appropriately.
Assembly code placed inline within the interrupt function is not scanned for register 
usage. Thus, if you include inline assembly code into an interrupt function, you may 
have to add extra assembly code to save and restore any registers or locations used. 
The same is true for any assembly routines called by the interrupt code.
If the W register is to be saved by the compiler, it may be stored to memory reserved 
in the common RAM. If the device for which the code is written does not have common 
memory, a byte is reserved in all RAM banks for the storage location for W register.
Most registers to be saved are allocated memory in the interrupt function’s auto area. 
They can be treated like any other auto variable and use the same assembly symbols. 
On mid-range devices, the W register is stored in BTEMP0, a pseudo register.
5.9.3.2
CONTEXT RESTORATION
Any objects saved by software are automatically restored by software before the inter-
rupt function returns. The order of restoration is the reverse to that used when context 
is saved.
1. The BTEMP register is a memory location allocated by the compiler, but it is treated like a register for
code generation purposes. It is not used by all devices.