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""")
}
// make sure that on the cx16 and c64, basic rom is banked in again when we exit the program
when(compTarget.name) {
"cx16" -> {
if(options.floats)
@ -143,17 +142,11 @@ internal class ProgramAndVarsGen(
}
"c64" -> {
asmgen.out(" jsr p8b_main.p8s_start | lda #31 | sta $01")
if(!options.noSysInit)
asmgen.out(" jmp sys.cleanup_at_exit")
else
asmgen.out(" rts")
asmgen.out(" jmp sys.cleanup_at_exit")
}
"c128" -> {
asmgen.out(" jsr p8b_main.p8s_start | lda #0 | sta ${"$"}ff00")
if(!options.noSysInit)
asmgen.out(" jmp sys.cleanup_at_exit")
else
asmgen.out(" rts")
asmgen.out(" jmp sys.cleanup_at_exit")
}
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
; TODO
; TODO where to store A as exit code?
%asm {{
ldx prog8_lib.orig_stackpointer
txs

View File

@ -373,7 +373,13 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() {
; executed when the main subroutine does rts
%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
%asm {{
lda #0
sta $ff00 ; default bank 15
jsr cbm.CLRCHN ; reset i/o channels
jsr sys.enable_runstop_and_charsetswitch
sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer
txs
rts ; return to original caller
jmp cleanup_at_exit
}}
}

View File

@ -372,7 +372,13 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() {
; executed when the main subroutine does rts
%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
%asm {{
lda #31
sta $01 ; bank the kernal in
jsr cbm.CLRCHN ; reset i/o channels
jsr sys.enable_runstop_and_charsetswitch
sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer
txs
rts ; return to original caller
jmp cleanup_at_exit
}}
}

View File

@ -1239,6 +1239,9 @@ asmsub cleanup_at_exit() {
lda #4
sta $01 ; rom bank 4 (basic)
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
}}
}
@ -1602,10 +1605,10 @@ save_SCRATCH_ZPWORD2 .word 0
asmsub exit(ubyte returnvalue @A) {
; -- immediately exit the program with a return code in the A register
%asm {{
jsr cbm.CLRCHN ; reset i/o channels
sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer
txs
rts ; return to original caller
jmp cleanup_at_exit
}}
}

View File

@ -110,6 +110,8 @@ asmsub init_system_phase2() {
asmsub cleanup_at_exit() {
; executed when the main subroutine does rts
%asm {{
_exitcode = *+1
lda #0 ; exit code possibly modified in exit()
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
%asm {{
sta cleanup_at_exit._exitcode
ldx prog8_lib.orig_stackpointer
txs
rts ; return to original caller
jmp cleanup_at_exit
}}
}

View File

@ -1,7 +1,7 @@
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
%option no_sysinit
main {
sub start() {
sub1()
}
cx16.r0 = 500
if cx16.r0 in 127 to 5555
cx16.r0++
cx16.r0 = 50
if cx16.r0 in 5555 downto 127
cx16.r0++
sub sub1() {
cx16.r0++
sub2()
}
sub sub2() {
cx16.r0++
sub3()
}
sub sub3() {
cx16.r0++
sys.exit(42)
}
}