Interrupts
MC9S08GB/GT Data Sheet, Rev. 2.3
Freescale Semiconductor
63
WhentheCPUreceivesaqualifiedinterruptrequest,itcompletesthecurrentinstructionbeforeresponding
to the interrupt. The interrupt sequence follows the same cycle-by-cycle sequence as the SWI instruction
and consists of:
Saving the CPU registers on the stack
Setting the I bit in the CCR to mask further interrupts
Fetching the interrupt vector for the highest-priority interrupt that is currently pending
Filling the instruction queue with the first three bytes of program information starting from the
address fetched from the interrupt vector locations
WhiletheCPUisrespondingtotheinterrupt,theIbitisautomaticallysettoavoidthepossibilityofanother
interrupt interrupting the ISR itself (this is called nesting of interrupts). Normally, the I bit is restored to 0
when the CCR is restored from the value stacked on entry to the ISR. In rare cases, the I bit may be cleared
inside an ISR (after clearing the status flag that generated the interrupt) so that other interrupts can be
servicedwithoutwaitingforthefirstserviceroutinetofinish.Thispracticeisnotrecommendedforanyone
otherthanthemostexperiencedprogrammersbecauseitcanleadtosubtleprogramerrorsthataredifficult
to debug.
The interrupt service routine ends with a return-from-interrupt (RTI) instruction which restores the CCR,
A, X, and PC registers to their pre-interrupt values by reading the previously saved information off the
stack.
NOTE
For compatibility with the M68HC08, the H register is not automatically
savedandrestored.ItisgoodprogrammingpracticetopushHontothestack
at the start of the interrupt service routine (ISR) and restore itjust before the
RTI that is used to return from the ISR.
When two or more interrupts are pending when the I bit is cleared, the highest priority source is serviced
first (see
Table 5-1
).
5.5.1
Interrupt Stack Frame
Figure 5-1
shows the contents and organization of a stack frame. Before the interrupt, the stack pointer
(SP) points at the next available byte location on the stack. The current values of CPU registers are stored
onthestackstartingwiththelow-orderbyteoftheprogramcounter(PCL)andendingwiththeCCR.After
stacking, the SP points at the next available location on the stack which is the address that is one less than
the address where the CCR was saved. The PC value that is stacked is the address of the instruction in the
main program that would have executed next if the interrupt had not occurred.