From a40b3134f4c04c52751b5743d864a43725fdd055 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 22 Dec 2020 04:52:46 +0100 Subject: [PATCH] fix clobbering of A when restoring X or Y from stack --- .../src/prog8/compiler/target/c64/codegen/AsmGen.kt | 6 +++--- .../compiler/target/c64/codegen/FunctionCallAsmGen.kt | 5 +++-- examples/cx16/imageviewer/imageviewer.p8 | 2 ++ examples/tehtriz.p8 | 2 -- examples/test.p8 | 10 +++++++--- examples/textelite.p8 | 3 --- examples/turtle-gfx.p8 | 2 ++ 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 2b61ef9ad..2f7695a0d 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -584,16 +584,16 @@ internal class AsmGen(private val program: Program, CpuRegister.A -> out(" pla") CpuRegister.X -> { if (CompilationTarget.instance.machine.cpu == CpuType.CPU65c02) out(" plx") - else out(" pla | tax") + else out(" sta P8ZP_SCRATCH_REG | pla | tax | lda P8ZP_SCRATCH_REG") } CpuRegister.Y -> { if (CompilationTarget.instance.machine.cpu == CpuType.CPU65c02) out(" ply") - else out(" pla | tay") + else out(" sta P8ZP_SCRATCH_REG | pla | tay | lda P8ZP_SCRATCH_REG") } } } else { when (register) { - CpuRegister.A -> out(" sta _prog8_regsaveA") + CpuRegister.A -> out(" lda _prog8_regsaveA") CpuRegister.X -> out(" ldx _prog8_regsaveX") CpuRegister.Y -> out(" ldy _prog8_regsaveY") } diff --git a/compiler/src/prog8/compiler/target/c64/codegen/FunctionCallAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/FunctionCallAsmGen.kt index a67c50c5f..77bfaab9f 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/FunctionCallAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/FunctionCallAsmGen.kt @@ -25,8 +25,9 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg // does NOT output the code to deal with the result values! val sub = stmt.target.targetSubroutine(program.namespace) ?: throw AssemblyError("undefined subroutine ${stmt.target}") val saveX = sub.shouldSaveX() + val regSaveOnStack = sub.asmAddress==null // rom-routines don't require registers to be saved on stack, normal subroutines do because they can contain nested calls if(saveX) - asmgen.saveRegister(CpuRegister.X, (stmt as Node).definingSubroutine()!!, true) + asmgen.saveRegister(CpuRegister.X, (stmt as Node).definingSubroutine()!!, regSaveOnStack) val subName = asmgen.asmSymbolName(stmt.target) if(stmt.args.isNotEmpty()) { @@ -64,7 +65,7 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg } asmgen.out(" jsr $subName") if(saveX) - asmgen.restoreRegister(CpuRegister.X, true) + asmgen.restoreRegister(CpuRegister.X, regSaveOnStack) } private fun registerArgsViaStackEvaluation(stmt: IFunctionCall, sub: Subroutine) { diff --git a/examples/cx16/imageviewer/imageviewer.p8 b/examples/cx16/imageviewer/imageviewer.p8 index 027bd5472..51902b0c2 100644 --- a/examples/cx16/imageviewer/imageviewer.p8 +++ b/examples/cx16/imageviewer/imageviewer.p8 @@ -8,6 +8,8 @@ %import bmp_module ;; %import ci_module +; TODO WHY is this 400 bytes larger than a few hours ago? + main { sub start() { ; trick to check if we're running on sdcard or host system shared folder diff --git a/examples/tehtriz.p8 b/examples/tehtriz.p8 index 824eca03d..94481e21f 100644 --- a/examples/tehtriz.p8 +++ b/examples/tehtriz.p8 @@ -7,8 +7,6 @@ ; staged speed increase ; some simple sound effects -; TODO BROKEN: DOESN'T REGISTER KEYSTROKES ANYMORE (CAN'T START GAME) - %target c64 %import syslib diff --git a/examples/test.p8 b/examples/test.p8 index 081a53e53..f715c78a1 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -71,9 +71,13 @@ _y .byte 0 sub start () { - while c64.CHRIN() { - ; read the rest of the entry until the end - } + txt.print("status 8: ") + uword ss = diskio.status(8) + txt.print(ss) + txt.print("\nstatus 9: ") + ss = diskio.status(9) + txt.print(ss) + txt.chrout('\n') ; cx16.r0 = 65535 ; set_8_pixels_opaque_OLD(111,222,33) diff --git a/examples/textelite.p8 b/examples/textelite.p8 index 35f6acfe1..3c660e829 100644 --- a/examples/textelite.p8 +++ b/examples/textelite.p8 @@ -10,9 +10,6 @@ ; Note: this program is compatible with C64 and CX16. -; TODO BROKEN IN VICE WHEN LOADING WITHOUT DISK: PRINTS WEIRD CHARACTERS FOR IO-ERROR - - main { const ubyte numforLave = 7 ; Lave is 7th generated planet in galaxy one diff --git a/examples/turtle-gfx.p8 b/examples/turtle-gfx.p8 index 08a038069..b9a2ba60e 100644 --- a/examples/turtle-gfx.p8 +++ b/examples/turtle-gfx.p8 @@ -4,6 +4,8 @@ %import test_stack %zeropage floatsafe +; TODO WHY is this a couple of 100 bytes larger than a few hours ago? + main { sub start() {