###
Interrupt service routines

The PIC18 devices use two kind of interrupts: low priority interrupts, and high priority interrupts. The corresponding Interrupt Service Routines (ISR) respectively start at addresses `0x18` and `0x8`. cpik automatically put a branch instruction at these addresses, so the code of the ISRs can be located anywhere in memory. Two empty interrupt service routines are provided by the run-time library14.

- `hi_pri_ISR` for high priority interrupts,
- `lo_pri_ISR` for low priority interrupts.

A user who plan to use interrupts must provide a specific interrupt service routine that can be written in C, using the (non-standard) `__interrupt__` keyword as following:

    __interrupt__ void hi_pri_ISR()
    {
      /* interrupt service code */
    }

The used-defined routine will shadow the default one, because user libraries are scanned before `rtl.slb`.

The role of the `__interrupt__` keyword is to insure that

- `W, BSR, FSR1, FSR2, STATUS`, registers are properly saved and restored on the data stack15.
- `retfie 0` is used as return instruction instead of `return 0` 16.

The body of an ISR routine can be written in pure assembly language, using the `__asm__` directive. In this case, all previously mentioned registers can be freely altered, as long as `FSR0` (the software stack pointer) is not altered when the ISR exits.

When the interrupt code is written in C (or mix of C and asm code), registers used by the run-time library and user code will be saved if a proper pragma `saved_regs` has be seen by the compiler before the source code of the ISR.

A standard `saved_regs` pragma is provided in the `<interrupt.h>` header, so, generally, nothing special must be done if `interrupt.h` is included. However, I recommend to verify that the registers specified by the `saved_regs` pragma match the registers that are actually used in the ISR.

See section 9.4.5 about the `saved_regs` pragma for details.

#### Footnotes

... library 14
  The run time library is the file `/usr/share/cpik/<version>/lib/rtl.slb`
... stack 15
  `FSR0` is not saved because it is the stack pointer itself.
... `return 0` 16
  The `reftie 1` instruction is not used because it is explicitly mentioned as bogus by errata documents from Microchip.