Chapter 7 of 8
Read the return ticket an interrupt leaves
A subroutine call saves the address where its caller can resume. An interrupt can arrive while several registers hold half-finished work. How much more does the CPU have to remember? Trigger the event yourself with SWI and compare the stack before and after it. You control when the experiment starts, and you can finish it without waiting for an external device.
Follow the vector to a RAM stub
Use a fresh Disk BASIC practice session, with the MC6809 or an HD6309 in its normal emulation mode. Save softirq.asm. Before running it, open the machine debugger and inspect these mapped bytes:
| Address | Bytes on the practice machine | What uses them |
|---|---|---|
| $FFFA | FE FA | The CPU reads this 16-bit SWI vector and sets PC to $FEFA. |
| $FEFA | 16 02 09 | Opcode $16 is LBRA. The displacement is relative to $FEFD, the PC after the instruction. Adding $0209 wraps to $0106. |
| $0106 | 00 00 00 | Disk BASIC's three-byte RAM stub on this machine. The example saves all three bytes before installing its handler. |
If your vector chain differs, leave it alone and use the supplied observations or return to the practice profile. The CPU vector gives an address. The relay at that address is executable code. The stub at the relay destination will become another instruction, an extended JMP to our own handler. These are three distinct things you can identify in memory.
The program saves its caller’s CC, D and X, masks IRQ and FIRQ, and installs the temporary JMP. The masks hold off those hardware interrupt classes during the experiment; executing SWI still triggers a software interrupt. Keep the guest in this program through cleanup. If you abandon the experiment before restoration, cold-restart the disposable profile before using it for anything else.
Catch the frame before the handler touches it
Set source breakpoints at trigger, handler and returned, then Run. Read the addresses from the listing after any edit. At trigger the supplied 6809 build has PC=$3F26, A=$4F, B=$52 and S=$7F30. The values in A and B spell the first two characters of our station code. Continue to handler and look at S before executing INC. It now reads $7F24, twelve bytes below its value at SWI.
Switch the machine debugger to Mapped CPU memory and look up your handler’s S. If you use a different BASIC stack, look at the address in the S register rather than copying $7F24. In increasing address order the test guest showed:
| At S + offset | Saved register | Bytes in this run |
|---|---|---|
| 0 | CC | D0 |
| 1, 2, 3 | A, B, DP | 4F 52 00 |
| 4–5 | X | 3F 37 |
| 6–7 | Y | A0 0E |
| 8–9 | U | AB EE |
| 10–11 | PC | 3F 27 |
Multibyte registers have their high byte first. Locate $4F and $52 as a check on your orientation. The final two bytes deserve a closer look. SWI at $3F26 is one byte long; $3F27 is the instruction immediately after it. The processor stacked a return PC that will let the program carry on. E, bit 7 of the stacked CC, is set. For the 6809 this indicates a full frame.
lda #$4f ; known bytes to locate in the stack frame
ldb #$52
trigger swi ; CPU stacks state and follows the SWI vector
returned lda oldop,pcr ; breakpoint to compare S after RTI
sta hook
ldx oldaddr,pcr
stx hook+1
restored puls cc,d,x,pc ; restore flags, D and X, return to BASIC
handler inc visits,pcr ; memory survives the return
rti ; reload the stacked context, including PC
Let RTI rebuild the context
Continue to returned. The handler increments a byte named visits and executes RTI. Inspect visits using its listing address ($3F3F in the supplied build). It is now 1. Compare registers with the trigger stop: A=$4F, B=$52 and S=$7F30 are back, and PC is the saved $3F27. The memory side effect survives the return. The stack frame has been consumed; old bytes might still be visible below S until some later call reuses that memory.
Set a final source stop at restored, then Continue. Read $0106–$0108 again. The original three bytes are back. Continue once more and enter PRINT 2+3 at the BASIC prompt. An RTS here in place of the handler’s RTI would take the top two bytes as an address and leave the rest of the saved context on the stack. You can explain that failure from the table without trying it.
Apply the observation to other interrupts
IRQ and FIRQ are signals a device can assert. A real interrupt handler also has to determine which device needs service and acknowledge it according to that device’s rules. SWI gives us a repeatable stack experiment without those timing or device interactions.
For the MC6809, IRQ normally saves the full register frame. FIRQ normally saves just CC and PC, with the stacked E bit clear. Code in a fast handler has to preserve other registers it uses. RTI reads the stacked state to determine what to pull. The HD6309 can run in emulation or native mode; full-frame stacking in native mode also includes E and F, and the 6309 provides a mode control affecting FIRQ stacking. Recheck the mode before applying any fixed frame diagram. The twelve bytes shown here were measured on both supported processors in the startup configuration. For full-frame layouts and mode controls see Darren Atkinson, Motorola 6809 and Hitachi 6309 Programmer’s Reference (2009), parts I–II.