5.9.4.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, will be saved in code generated by the compiler. This is the context save or context switch code.

See 5.6 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 WREG, STATUS, BSR and FSRx registers in hardware (using special shadow registers) and hence these registers do not need to be saved by software. The registers that might need to be saved by software are the BTEMP registers(3), compiler temporary locations that act like registers.

Other Mid-range PIC processors only save the entire PC (excluding the PCLATH register) when an interrupt occurs. The WREG, STATUS, FSR and PCLATH registers and any BTEMP registers must be saved by code produced by the compiler, if required.

By default, the PIC18 high-priority interrupt function will utilize its internal shadow register to save the W, STATUS and BSR registers. For the low priority PIC18 interrupts, or when the shadow registers cannot be used, all registers that have been used by the interrupt code will be saved by software.

If the PIC18 device has the Vectored Interrupt Controller module, it additionally saves the FSRx, PCLATHx and PRODx registers to shadow registers. All other used registers are saved by software. Separate shadow registers are available for low- and high-priority interrupts.

Note that for some older devices, the compiler will not use the shadow registers 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 4.6.1.10 Errata Option).

The compiler determines exactly which registers and objects are used by an interrupt function, or any of the functions that it calls and saves these appropriately.

Assembly code placed in-line within the interrupt function is not scanned for register usage. Thus, if you include in-line assembly code into an interrupt function (or functions called by the interrupt function), you may have to add extra assembly code to save and restore any registers used.

If the software stack is in use, the context switch code will also initialize the stack pointer register so it is accessing the area of the stack reserved for the interrupt. See 5.4.2.2.1 Object Size Limits for more information on the software stack.

1

These registers are memory locations allocated by the compiler, but are treated like registers for code generation purposes. They are typically used when generating reentrant code.