fix stack crash in cx16.push_vera_context()

This commit is contained in:
Irmen de Jong 2022-07-13 23:23:37 +02:00
parent 2d600da8b6
commit 4bc65e9ef7
5 changed files with 31 additions and 21 deletions

View File

@ -735,49 +735,53 @@ IRQ_SCRATCH_ZPWORD2 .word 0
}} }}
} }
inline asmsub push_vera_context() clobbers(A) { asmsub push_vera_context() clobbers(A) {
; -- use this at the start of your IRQ handler if it uses Vera registers, to save the state ; -- use this at the start of your IRQ handler if it uses Vera registers, to save the state
%asm {{ %asm {{
; note cannot store this on cpu hardware stack because this gets called as a subroutine
lda cx16.VERA_ADDR_L lda cx16.VERA_ADDR_L
pha sta _vera_storage
lda cx16.VERA_ADDR_M lda cx16.VERA_ADDR_M
pha sta _vera_storage+1
lda cx16.VERA_ADDR_H lda cx16.VERA_ADDR_H
pha sta _vera_storage+2
lda cx16.VERA_CTRL lda cx16.VERA_CTRL
pha sta _vera_storage+3
eor #1 eor #1
sta cx16.VERA_CTRL sta cx16.VERA_CTRL
lda cx16.VERA_ADDR_L lda cx16.VERA_ADDR_L
pha sta _vera_storage+4
lda cx16.VERA_ADDR_M lda cx16.VERA_ADDR_M
pha sta _vera_storage+5
lda cx16.VERA_ADDR_H lda cx16.VERA_ADDR_H
pha sta _vera_storage+6
lda cx16.VERA_CTRL lda cx16.VERA_CTRL
pha sta _vera_storage+7
rts
_vera_storage: .byte 0,0,0,0,0,0,0,0
}} }}
} }
inline asmsub pop_vera_context() clobbers(A) { asmsub pop_vera_context() clobbers(A) {
; -- use this at the end of your IRQ handler if it uses Vera registers, to restore the state ; -- use this at the end of your IRQ handler if it uses Vera registers, to restore the state
%asm {{ %asm {{
pla lda cx16.push_vera_context._vera_storage+7
sta cx16.VERA_CTRL sta cx16.VERA_CTRL
pla lda cx16.push_vera_context._vera_storage+6
sta cx16.VERA_ADDR_H sta cx16.VERA_ADDR_H
pla lda cx16.push_vera_context._vera_storage+5
sta cx16.VERA_ADDR_M sta cx16.VERA_ADDR_M
pla lda cx16.push_vera_context._vera_storage+4
sta cx16.VERA_ADDR_L sta cx16.VERA_ADDR_L
pla lda cx16.push_vera_context._vera_storage+3
sta cx16.VERA_CTRL sta cx16.VERA_CTRL
pla lda cx16.push_vera_context._vera_storage+2
sta cx16.VERA_ADDR_H sta cx16.VERA_ADDR_H
pla lda cx16.push_vera_context._vera_storage+1
sta cx16.VERA_ADDR_M sta cx16.VERA_ADDR_M
pla lda cx16.push_vera_context._vera_storage+0
sta cx16.VERA_ADDR_L sta cx16.VERA_ADDR_L
rts
}} }}
} }

View File

@ -1 +1 @@
8.4-dev 8.3.1

View File

@ -706,7 +706,7 @@ main {
} }
""" """
val result = compileText(C64Target(), false, text, writeAssembly = true)!! val result = compileText(C64Target(), false, text, writeAssembly = true)!!
result.program.entrypoint.statements.size shouldBe 13 result.program.entrypoint.statements.size shouldBe 15
} }
test("invalid typecasts of numbers") { test("invalid typecasts of numbers") {

View File

@ -495,7 +495,9 @@ Breaking out of a loop prematurely is possible with the ``break`` statement.
languages if this is *not* the case! A for loop from ubyte 10 to ubyte 2, for example, will iterate through languages if this is *not* the case! A for loop from ubyte 10 to ubyte 2, for example, will iterate through
all values 10, 11, 12, 13, .... 254, 255, 0 (wrapped), 1, 2. In other languages the entire loop will all values 10, 11, 12, 13, .... 254, 255, 0 (wrapped), 1, 2. In other languages the entire loop will
be skipped in such cases. But prog8 omits the overhead of an extra loop range check and/or branch for every for loop be skipped in such cases. But prog8 omits the overhead of an extra loop range check and/or branch for every for loop
by assuming the normal ranges. because the most common case is that it is not needed.
You should add an explicit range check yourself if the ending value can be less than the start value and
a full wrap-around loop is not what you want!
Conditional Execution Conditional Execution

View File

@ -3,6 +3,10 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- cx16: sys.reset_system() doesn't silence the vera psg (kernel bug in reset vector?) use reset vera bit?
- see if we can let for loops skip the loop if end<start, without adding a lot of code size/duplicating the loop condition
this is documented behiavor to now loop around but it's too easy to forget about
... ...