Interrupts are used for infrequent or irregular events which must be acted upon by the processor. The use of interrupts saves having to look periodically to see whether the event has occurred or not. This is known as polling and can tie the micro up for a large proportion of the time, especially if the event is a rare one, like power failure. Another reason for using interrupts is to achieve a fast response to external events. If an event has just been polled and found inactive, and then occurs just after polling, then it will have to wait until the next time the event is polled.
Clearly, there is a risk that the registers used by the interrupted program will be overwritten by the interrupt routine. This is avoided by the processor, which automatically stacks up the machine registers when servicing an interrupt and unstacks them again when a return from interrupt instruction (RTI) is executed. Some processors allow multiple interrupts, which means that one interrupt service routine may itself be interrupted by another ISR. In this case integrity of the registers is still assured as they will again be stacked, preserving the work being done by the first interrupt routine. This is known as nesting interrupts. Whether or not an interrupt is serviced at all may be decided by the user, who may disable and enable interrupts, so that they may be ignored if they are not relevant.
Interrupt routines may also need to use locations in memory for use as scratch-pad locations. In this case use of the stack will guarantee non-corruption of vital location, as space will then be allocated and deallocated as the interrupt is serviced.
Finally, the cause of an interrupt MUST be cleared (if necessary) by the micro before the RTI instruction is executed, or else the processor will be immediately interrupted again, and you get into a deadlock situation
+--------------+ FFFF,FFFE | /RESET | Starting address on reset +--------------+ FFFD,FFFC | /NMI | Non-maskable interrupt routine +--------------+ FFFB,FFFA | SWI | Software Interrupt no. 1 +--------------+ FFF9,FFF8 | /IRQ | Interrupt request +--------------+ FFF7.FFF6 | /FIRQ | Fast interrupt request +--------------+ FFF5,FFF4 | SWI2 | Software interrupt no. 2 +--------------+ FFF3,FFF2 | SWI3 | Software interrupt no. 3 +--------------+An order of priority is imposed - the interrupts at the top of the list (/RESET, /NMI) set the flag bits of the other interrupts to stop them interrupting during the interrupt service routine. The former values of these bits are restored when the flag register is pulled off the stack on return from the interrupt. Further interrupts are then free to occur thereafter.
The software interrupts (SWI. SWI2, SWI3) allow interrupt facilities to be reached explicitly from a program. By invoking one of these instructions. a program may transfer control to a subroutine in the same way that control is transferred on an interrupt, including the stacking of registers etc.
Most of the interrupts cause all the registers to be stacked. An exception is the fast interrupt (/FIRQ). For applications where quick response is required, FIRQ is useful as it only stacks the flag register (and of course the program counter!). The user then has responsibility for stacking the registers that must be preserved during the course of the interrupt routine; not all may be in use. A record is kept in the entire flag (E) in the flag register that only the flag register and PC have been saved, by clearing E. For all other interrupts, E is set to "1" to indicate that the entire register set has been stacked. E is then referred to during RTI to determine how many registers are pulled off the stack at the end of the routine. As the flag register is always stacked, this is referred to after pulling to see whether only the PC is to be pulled or the complete set of registers and THEN the PC.