; SOFTIRQ.ASM -- install, call and remove a BASIC RAM SWI trampoline.
; Fresh CoCo 3 Disk BASIC, MC6809 or HD6309 in emulation mode only.
; From a debugger stop, Continue through cleanup before returning to BASIC.
hook        equ     $0106           ; three-byte RAM stub reached via $FFFA
            org     $3f00
start       pshs    cc,d,x          ; preserve the caller's flags, D and X
            orcc    #$50            ; keep IRQ & FIRQ out during this experiment
            lda     hook
            ldx     hook+1
            sta     oldop,pcr
            stx     oldaddr,pcr
            lda     #$7e            ; extended JMP opcode
            leax    handler,pcr
            stx     hook+1
            sta     hook            ; JMP handler now complete
            clr     visits,pcr
            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
oldop       fcb     0
oldaddr     fdb     0
visits      fcb     0
            end     start
