diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 00993b310..5e015b6a7 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -127,9 +127,27 @@ internal class ProgramAndVarsGen( "cx16" -> { if(options.floats) asmgen.out(" lda #4 | sta $01") // to use floats, make sure Basic rom is banked in - asmgen.out(" jsr main.start | lda #4 | sta $01 | rts") + asmgen.out(" jsr main.start | lda #4 | sta $01") + if(!options.noSysInit) + asmgen.out(" jmp ${compTarget.name}.cleanup_at_exit") + else + asmgen.out(" rts") + } + "c64" -> { + asmgen.out(" jsr main.start | lda #31 | sta $01") + if(!options.noSysInit) + asmgen.out(" jmp ${compTarget.name}.cleanup_at_exit") + else + asmgen.out(" rts") + } + "c128" -> { + asmgen.out(" jsr main.start") + // TODO c128: how to bank basic+kernal back in? + if(!options.noSysInit) + asmgen.out(" jmp ${compTarget.name}.cleanup_at_exit") + else + asmgen.out(" rts") } - "c64" -> asmgen.out(" jsr main.start | lda #31 | sta $01 | rts") else -> asmgen.jmp("main.start") } } diff --git a/compiler/res/prog8lib/c128/syslib.p8 b/compiler/res/prog8lib/c128/syslib.p8 index 87647ef72..be742a32e 100644 --- a/compiler/res/prog8lib/c128/syslib.p8 +++ b/compiler/res/prog8lib/c128/syslib.p8 @@ -274,6 +274,16 @@ asmsub disable_runstop_and_charsetswitch() clobbers(A) { }} } +asmsub enable_runstop_and_charsetswitch() clobbers(A) { + %asm {{ + lda #0 + sta 247 ; enable charset switching + lda #110 + sta 808 ; enable run/stop key + rts + }} +} + asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { %asm {{ sta _modified+1 @@ -481,6 +491,13 @@ asmsub init_system_phase2() { }} } +asmsub cleanup_at_exit() { + ; executed when the main subroutine does rts + %asm {{ + jmp c64.enable_runstop_and_charsetswitch + }} +} + asmsub disable_basic() clobbers(A) { %asm {{ lda $0a04 ; disable BASIC shadow registers @@ -681,7 +698,10 @@ _longcopy inline asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ + ;lda #14 + ;sta $01 ; bank the kernal in TODO c128 how to do this? jsr c64.CLRCHN ; reset i/o channels + jsr c64.enable_runstop_and_charsetswitch ldx prog8_lib.orig_stackpointer txs rts ; return to original caller diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index 44d32c4f6..9f0a675e8 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -300,6 +300,13 @@ asmsub init_system_phase2() { }} } +asmsub cleanup_at_exit() { + ; executed when the main subroutine does rts + %asm {{ + jmp c64.enable_runstop_and_charsetswitch + }} +} + asmsub disable_runstop_and_charsetswitch() clobbers(A) { %asm {{ lda #$80 @@ -310,6 +317,16 @@ asmsub disable_runstop_and_charsetswitch() clobbers(A) { }} } +asmsub enable_runstop_and_charsetswitch() clobbers(A) { + %asm {{ + lda #0 + sta 657 ; enable charset switching + lda #237 + sta 808 ; enable run/stop key + rts + }} +} + asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { %asm {{ sta _modified+1 @@ -646,7 +663,10 @@ _longcopy inline asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ + lda #31 + sta $01 ; bank the kernal in jsr c64.CLRCHN ; reset i/o channels + jsr c64.enable_runstop_and_charsetswitch ldx prog8_lib.orig_stackpointer txs rts ; return to original caller diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 4f1349cf9..9af6fedf2 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -641,6 +641,14 @@ asmsub init_system_phase2() { }} } +asmsub cleanup_at_exit() { + ; executed when the main subroutine does rts + ; just an rts here, nothing special to clean up on cx16 + %asm {{ + rts + }} +} + asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { %asm {{ sta _modified+1 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 8a5f6dfdd..899061144 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- c64 target: after exit, switching charset case is still disabled. Don't disable this by default in startup? - vm: check array type in PtAssignTarget - vm: animals example game breaks after adding first new animal... - vm: add more instructions operating directly on memory instead of only registers? (translate assignment self-assigns) diff --git a/examples/tehtriz.p8 b/examples/tehtriz.p8 index 050b50e16..b975feb80 100644 --- a/examples/tehtriz.p8 +++ b/examples/tehtriz.p8 @@ -32,7 +32,6 @@ main { sub start() { - c64.disable_runstop_and_charsetswitch() ;@(650) = 128 ; set all keys to repeat sound.init() newGame() diff --git a/examples/test.p8 b/examples/test.p8 index 5b4753573..18faab313 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -31,7 +31,9 @@ main { txt.print(name) txt.nl() - name=other + uword otherptr = &other + 2 + name = otherptr + txt.print(name) txt.nl() txt.nl()