fix exit() now actually correctly setting the return code in A

also, moved some cleanup stuff such as CLRCHN from exit() to the cleanup routine that is always called.
finally, also call the cleanup routine when  %option no_sysinit is used
This commit is contained in:
Irmen de Jong 2024-01-04 00:30:20 +01:00
parent 242a3eec63
commit 19a2110ba2
8 changed files with 49 additions and 40 deletions

View File

@ -133,7 +133,6 @@ internal class ProgramAndVarsGen(
pha""") pha""")
} }
// make sure that on the cx16 and c64, basic rom is banked in again when we exit the program
when(compTarget.name) { when(compTarget.name) {
"cx16" -> { "cx16" -> {
if(options.floats) if(options.floats)
@ -143,17 +142,11 @@ internal class ProgramAndVarsGen(
} }
"c64" -> { "c64" -> {
asmgen.out(" jsr p8b_main.p8s_start | lda #31 | sta $01") asmgen.out(" jsr p8b_main.p8s_start | lda #31 | sta $01")
if(!options.noSysInit) asmgen.out(" jmp sys.cleanup_at_exit")
asmgen.out(" jmp sys.cleanup_at_exit")
else
asmgen.out(" rts")
} }
"c128" -> { "c128" -> {
asmgen.out(" jsr p8b_main.p8s_start | lda #0 | sta ${"$"}ff00") asmgen.out(" jsr p8b_main.p8s_start | lda #0 | sta ${"$"}ff00")
if(!options.noSysInit) asmgen.out(" jmp sys.cleanup_at_exit")
asmgen.out(" jmp sys.cleanup_at_exit")
else
asmgen.out(" rts")
} }
else -> asmgen.jmp("p8b_main.p8s_start") else -> asmgen.jmp("p8b_main.p8s_start")
} }

View File

@ -252,9 +252,9 @@ save_SCRATCH_ZPWORD2 .word 0
}} }}
} }
inline asmsub exit(ubyte returnvalue @A) { asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register ; -- immediately exit the program with a return code in the A register
; TODO ; TODO where to store A as exit code?
%asm {{ %asm {{
ldx prog8_lib.orig_stackpointer ldx prog8_lib.orig_stackpointer
txs txs

View File

@ -373,7 +373,13 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() { asmsub cleanup_at_exit() {
; executed when the main subroutine does rts ; executed when the main subroutine does rts
%asm {{ %asm {{
jmp sys.enable_runstop_and_charsetswitch lda #0
sta $ff00 ; default bank 15
jsr cbm.CLRCHN ; reset i/o channels
jsr enable_runstop_and_charsetswitch
_exitcode = *+1
lda #0 ; exit code possibly modified in exit()
rts
}} }}
} }
@ -754,16 +760,13 @@ _longcopy
}} }}
} }
inline asmsub exit(ubyte returnvalue @A) { asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register ; -- immediately exit the program with a return code in the A register
%asm {{ %asm {{
lda #0 sta cleanup_at_exit._exitcode
sta $ff00 ; default bank 15
jsr cbm.CLRCHN ; reset i/o channels
jsr sys.enable_runstop_and_charsetswitch
ldx prog8_lib.orig_stackpointer ldx prog8_lib.orig_stackpointer
txs txs
rts ; return to original caller jmp cleanup_at_exit
}} }}
} }

View File

@ -372,7 +372,13 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() { asmsub cleanup_at_exit() {
; executed when the main subroutine does rts ; executed when the main subroutine does rts
%asm {{ %asm {{
jmp sys.enable_runstop_and_charsetswitch lda #31
sta $01 ; bank the kernal in
jsr cbm.CLRCHN ; reset i/o channels
jsr enable_runstop_and_charsetswitch
_exitcode = *+1
lda #0 ; exit code possibly modified in exit()
rts
}} }}
} }
@ -752,16 +758,13 @@ _longcopy
}} }}
} }
inline asmsub exit(ubyte returnvalue @A) { asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register ; -- immediately exit the program with a return code in the A register
%asm {{ %asm {{
lda #31 sta cleanup_at_exit._exitcode
sta $01 ; bank the kernal in
jsr cbm.CLRCHN ; reset i/o channels
jsr sys.enable_runstop_and_charsetswitch
ldx prog8_lib.orig_stackpointer ldx prog8_lib.orig_stackpointer
txs txs
rts ; return to original caller jmp cleanup_at_exit
}} }}
} }

View File

@ -1239,6 +1239,9 @@ asmsub cleanup_at_exit() {
lda #4 lda #4
sta $01 ; rom bank 4 (basic) sta $01 ; rom bank 4 (basic)
stz $2d ; hack to reset machine code monitor bank to 0 stz $2d ; hack to reset machine code monitor bank to 0
jsr cbm.CLRCHN ; reset i/o channels
_exitcode = *+1
lda #0 ; exit code possibly modified in exit()
rts rts
}} }}
} }
@ -1602,10 +1605,10 @@ save_SCRATCH_ZPWORD2 .word 0
asmsub exit(ubyte returnvalue @A) { asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register ; -- immediately exit the program with a return code in the A register
%asm {{ %asm {{
jsr cbm.CLRCHN ; reset i/o channels sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer ldx prog8_lib.orig_stackpointer
txs txs
rts ; return to original caller jmp cleanup_at_exit
}} }}
} }

View File

@ -110,6 +110,8 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() { asmsub cleanup_at_exit() {
; executed when the main subroutine does rts ; executed when the main subroutine does rts
%asm {{ %asm {{
_exitcode = *+1
lda #0 ; exit code possibly modified in exit()
rts rts
}} }}
} }
@ -345,12 +347,13 @@ save_SCRATCH_ZPWORD2 .word 0
}} }}
} }
inline asmsub exit(ubyte returnvalue @A) { asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register ; -- immediately exit the program with a return code in the A register
%asm {{ %asm {{
sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer ldx prog8_lib.orig_stackpointer
txs txs
rts ; return to original caller jmp cleanup_at_exit
}} }}
} }

View File

@ -1,7 +1,7 @@
TODO TODO
==== ====
keep Bool alive longer until codegen? (don't replace by UBYTE so quickly?) [on branch 'booleans']: keep Bool alive longer until codegen? (don't replace by UBYTE so quickly?)
... ...

View File

@ -1,17 +1,21 @@
%import textio
%zeropage basicsafe %zeropage basicsafe
%option no_sysinit
main { main {
sub start() { sub start() {
sub1()
}
cx16.r0 = 500 sub sub1() {
if cx16.r0 in 127 to 5555 cx16.r0++
cx16.r0++ sub2()
}
sub sub2() {
cx16.r0 = 50 cx16.r0++
if cx16.r0 in 5555 downto 127 sub3()
cx16.r0++ }
sub sub3() {
cx16.r0++
sys.exit(42)
} }
} }