From 4bc65e9ef71014868205c6dd88f98923cc973117 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 13 Jul 2022 23:23:37 +0200 Subject: [PATCH] fix stack crash in cx16.push_vera_context() --- compiler/res/prog8lib/cx16/syslib.p8 | 40 +++++++++++++++------------- compiler/res/version.txt | 2 +- compiler/test/TestTypecasts.kt | 2 +- docs/source/programming.rst | 4 ++- docs/source/todo.rst | 4 +++ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 516c75fc4..77666e337 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -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 %asm {{ + ; note cannot store this on cpu hardware stack because this gets called as a subroutine lda cx16.VERA_ADDR_L - pha + sta _vera_storage lda cx16.VERA_ADDR_M - pha + sta _vera_storage+1 lda cx16.VERA_ADDR_H - pha + sta _vera_storage+2 lda cx16.VERA_CTRL - pha + sta _vera_storage+3 eor #1 sta cx16.VERA_CTRL lda cx16.VERA_ADDR_L - pha + sta _vera_storage+4 lda cx16.VERA_ADDR_M - pha + sta _vera_storage+5 lda cx16.VERA_ADDR_H - pha + sta _vera_storage+6 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 %asm {{ - pla + lda cx16.push_vera_context._vera_storage+7 sta cx16.VERA_CTRL - pla + lda cx16.push_vera_context._vera_storage+6 sta cx16.VERA_ADDR_H - pla + lda cx16.push_vera_context._vera_storage+5 sta cx16.VERA_ADDR_M - pla + lda cx16.push_vera_context._vera_storage+4 sta cx16.VERA_ADDR_L - pla + lda cx16.push_vera_context._vera_storage+3 sta cx16.VERA_CTRL - pla + lda cx16.push_vera_context._vera_storage+2 sta cx16.VERA_ADDR_H - pla + lda cx16.push_vera_context._vera_storage+1 sta cx16.VERA_ADDR_M - pla + lda cx16.push_vera_context._vera_storage+0 sta cx16.VERA_ADDR_L + rts }} } diff --git a/compiler/res/version.txt b/compiler/res/version.txt index 47ea0f6ff..56b6be4eb 100644 --- a/compiler/res/version.txt +++ b/compiler/res/version.txt @@ -1 +1 @@ -8.4-dev +8.3.1 diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index dcf31c2eb..8b03bf8a5 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -706,7 +706,7 @@ main { } """ 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") { diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 02aa21143..e9b5f7c3f 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -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 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 - 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 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 692aa7b4b..05e37031f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,6 +3,10 @@ TODO 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