From 043df18daa11fbd04dc200197aa23f91590e899a Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 10 Mar 2023 23:29:34 +0100 Subject: [PATCH] set X to bottom part of eval stack in irq handler. fixes #94 --- compiler/res/prog8lib/c128/syslib.p8 | 18 ++++++------------ compiler/res/prog8lib/c64/syslib.p8 | 18 ++++++------------ compiler/res/prog8lib/cx16/syslib.p8 | 18 ++++++------------ docs/source/todo.rst | 1 - 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/compiler/res/prog8lib/c128/syslib.p8 b/compiler/res/prog8lib/c128/syslib.p8 index dea66ec72..140fcdbfd 100644 --- a/compiler/res/prog8lib/c128/syslib.p8 +++ b/compiler/res/prog8lib/c128/syslib.p8 @@ -340,8 +340,7 @@ _modified jsr $ffff ; modified _use_kernal .byte 0 _irq_handler_init - ; save all zp scratch registers and the X register as these might be clobbered by the irq routine - stx IRQ_X_REG + ; save all zp scratch registers as these might be clobbered by the irq routine lda P8ZP_SCRATCH_B1 sta IRQ_SCRATCH_ZPB1 lda P8ZP_SCRATCH_REG @@ -354,18 +353,15 @@ _irq_handler_init sta IRQ_SCRATCH_ZPWORD2 lda P8ZP_SCRATCH_W2+1 sta IRQ_SCRATCH_ZPWORD2+1 - ; stack protector; make sure we don't clobber the top of the evaluation stack - dex - dex - dex - dex - dex - dex + ; Set X to the bottom 32 bytes of the evaluation stack, to HOPEFULLY not clobber it. + ; This leaves 128-32=96 stack entries for the main program, and 32 stack entries for the IRQ handler. + ; We assume IRQ handlers don't contain complex expressions taking up more than that. + ldx #32 cld rts _irq_handler_end - ; restore all zp scratch registers and the X register + ; restore all zp scratch registers lda IRQ_SCRATCH_ZPB1 sta P8ZP_SCRATCH_B1 lda IRQ_SCRATCH_ZPREG @@ -378,10 +374,8 @@ _irq_handler_end sta P8ZP_SCRATCH_W2 lda IRQ_SCRATCH_ZPWORD2+1 sta P8ZP_SCRATCH_W2+1 - ldx IRQ_X_REG rts -IRQ_X_REG .byte 0 IRQ_SCRATCH_ZPB1 .byte 0 IRQ_SCRATCH_ZPREG .byte 0 IRQ_SCRATCH_ZPWORD1 .word 0 diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index f6b9ffcda..be926fb7f 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -384,8 +384,7 @@ _modified jsr $ffff ; modified _use_kernal .byte 0 _irq_handler_init - ; save all zp scratch registers and the X register as these might be clobbered by the irq routine - stx IRQ_X_REG + ; save all zp scratch registers as these might be clobbered by the irq routine lda P8ZP_SCRATCH_B1 sta IRQ_SCRATCH_ZPB1 lda P8ZP_SCRATCH_REG @@ -398,18 +397,15 @@ _irq_handler_init sta IRQ_SCRATCH_ZPWORD2 lda P8ZP_SCRATCH_W2+1 sta IRQ_SCRATCH_ZPWORD2+1 - ; stack protector; make sure we don't clobber the top of the evaluation stack - dex - dex - dex - dex - dex - dex + ; Set X to the bottom 32 bytes of the evaluation stack, to HOPEFULLY not clobber it. + ; This leaves 128-32=96 stack entries for the main program, and 32 stack entries for the IRQ handler. + ; We assume IRQ handlers don't contain complex expressions taking up more than that. + ldx #32 cld rts _irq_handler_end - ; restore all zp scratch registers and the X register + ; restore all zp scratch registers lda IRQ_SCRATCH_ZPB1 sta P8ZP_SCRATCH_B1 lda IRQ_SCRATCH_ZPREG @@ -422,10 +418,8 @@ _irq_handler_end sta P8ZP_SCRATCH_W2 lda IRQ_SCRATCH_ZPWORD2+1 sta P8ZP_SCRATCH_W2+1 - ldx IRQ_X_REG rts -IRQ_X_REG .byte 0 IRQ_SCRATCH_ZPB1 .byte 0 IRQ_SCRATCH_ZPREG .byte 0 IRQ_SCRATCH_ZPWORD1 .word 0 diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index c8c84b30d..8d636ce4d 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -729,8 +729,7 @@ _modified jsr $ffff ; modified _use_kernal .byte 0 _irq_handler_init - ; save all zp scratch registers and the X register as these might be clobbered by the irq routine - stx IRQ_X_REG + ; save all zp scratch registers as these might be clobbered by the irq routine lda P8ZP_SCRATCH_B1 sta IRQ_SCRATCH_ZPB1 lda P8ZP_SCRATCH_REG @@ -743,18 +742,15 @@ _irq_handler_init sta IRQ_SCRATCH_ZPWORD2 lda P8ZP_SCRATCH_W2+1 sta IRQ_SCRATCH_ZPWORD2+1 - ; stack protector; make sure we don't clobber the top of the evaluation stack - dex - dex - dex - dex - dex - dex + ; Set X to the bottom 32 bytes of the evaluation stack, to HOPEFULLY not clobber it. + ; This leaves 128-32=96 stack entries for the main program, and 32 stack entries for the IRQ handler. + ; We assume IRQ handlers don't contain complex expressions taking up more than that. + ldx #32 cld rts _irq_handler_end - ; restore all zp scratch registers and the X register + ; restore all zp scratch registers lda IRQ_SCRATCH_ZPB1 sta P8ZP_SCRATCH_B1 lda IRQ_SCRATCH_ZPREG @@ -767,10 +763,8 @@ _irq_handler_end sta P8ZP_SCRATCH_W2 lda IRQ_SCRATCH_ZPWORD2+1 sta P8ZP_SCRATCH_W2+1 - ldx IRQ_X_REG rts -IRQ_X_REG .byte 0 IRQ_SCRATCH_ZPB1 .byte 0 IRQ_SCRATCH_ZPREG .byte 0 IRQ_SCRATCH_ZPWORD1 .word 0 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index dab232dca..331132f1e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ -- fix Github issue with X register https://github.com/irmen/prog8/issues/94 - fix Github issue with array problems https://github.com/irmen/prog8/issues/99 - fix IR/VM: animals.p8 example is borked, it jumps straight to a suggestion and then somehow doesn't print the animal name correctly in the first question, and exits after 1 animal instead of looping this has happened after v8.7: caused by c21913a6 ir: keep order of children in block 22-11-2022