From 0916b943da5c0cc0365cd97045c658dffc81d26c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 9 Apr 2025 22:13:52 +0200 Subject: [PATCH] sys.exit(), exit2(), exit3() now romable --- compiler/res/prog8lib/c128/syslib.p8 | 26 ++++--- compiler/res/prog8lib/c64/syslib.p8 | 26 ++++--- compiler/res/prog8lib/conv.p8 | 2 - compiler/res/prog8lib/cx16/syslib.p8 | 28 +++---- compiler/res/prog8lib/pet32/syslib.p8 | 26 ++++--- .../customtarget/libraries/atari/syslib.p8 | 24 +++--- .../customtarget/libraries/neo6502/syslib.p8 | 44 ++++++----- .../customtarget/libraries/tinyc64/syslib.p8 | 73 ++++++++++++++++--- .../customtarget/libraries/tinycx16/syslib.p8 | 73 ++++++++++++++++--- .../customtarget/libraries/tinypet/syslib.p8 | 73 ++++++++++++++++--- examples/test.p8 | 12 +-- 11 files changed, 289 insertions(+), 118 deletions(-) diff --git a/compiler/res/prog8lib/c128/syslib.p8 b/compiler/res/prog8lib/c128/syslib.p8 index a7053fc0e..c589d1e17 100644 --- a/compiler/res/prog8lib/c128/syslib.p8 +++ b/compiler/res/prog8lib/c128/syslib.p8 @@ -445,7 +445,6 @@ sys { } asmsub save_prog8_internals() { - ; TODO: Romable %asm {{ lda P8ZP_SCRATCH_B1 sta save_SCRATCH_ZPB1 @@ -896,7 +895,7 @@ _no_msb_size sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta p8_sys_startup.cleanup_at_exit._exitcodeCarry + sta p8_sys_startup.cleanup_at_exit._exitcarry stx p8_sys_startup.cleanup_at_exit._exitcodeX sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer @@ -1071,7 +1070,6 @@ cx16 { &byte r15sH = $1bff asmsub save_virtual_registers() clobbers(A,Y) { - ; TODO: Romable %asm {{ ldy #31 - lda cx16.r0,y @@ -1148,22 +1146,26 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ lda #0 sta $ff00 ; default bank 15 jsr cbm.CLRCHN ; reset i/o channels jsr enable_runstop_and_charsetswitch -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index e87a07814..20cf98f97 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -461,7 +461,6 @@ sys { } asmsub save_prog8_internals() { - ; TODO: Romable %asm {{ lda P8ZP_SCRATCH_B1 sta save_SCRATCH_ZPB1 @@ -914,7 +913,7 @@ _no_msb_size sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta p8_sys_startup.cleanup_at_exit._exitcodeCarry + sta p8_sys_startup.cleanup_at_exit._exitcarry stx p8_sys_startup.cleanup_at_exit._exitcodeX sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer @@ -1089,7 +1088,6 @@ cx16 { &byte r15sH = $cfff asmsub save_virtual_registers() clobbers(A,Y) { - ; TODO: Romable %asm {{ ldy #31 - lda cx16.r0,y @@ -1177,7 +1175,6 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ lda #%00101111 sta $00 @@ -1189,16 +1186,21 @@ asmsub cleanup_at_exit() { jsr cbm.MEMTOP ; adjust MEMTOP down again jsr cbm.CLRCHN ; reset i/o channels jsr enable_runstop_and_charsetswitch -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } diff --git a/compiler/res/prog8lib/conv.p8 b/compiler/res/prog8lib/conv.p8 index 4af9db7d8..9f31e3032 100644 --- a/compiler/res/prog8lib/conv.p8 +++ b/compiler/res/prog8lib/conv.p8 @@ -352,7 +352,6 @@ asmsub str2word(str string @AY) -> word @AY { ; the number may be preceded by a + or - sign but may NOT contain spaces ; (any non-digit character will terminate the number string that is parsed) ; result in AY, number of characters processed also remains in cx16.r15 if you want to use it!! (0 = error) - ; TODO: Romable %asm {{ _result = P8ZP_SCRATCH_W1 sta P8ZP_SCRATCH_W2 @@ -542,7 +541,6 @@ asmsub internal_uword2decimal (uword value @AY) -> ubyte @Y, ubyte @A, ubyte @ ; output in internal_uword2decimal.decTenThousands, decThousands, decHundreds, decTens, decOnes ; (these are terminated by a zero byte so they can be easily printed) ; also returns Y = 100's, A = 10's, X = 1's - ; TODO: Romable %asm {{ diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index ed267c5f7..6be969ac7 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -1046,7 +1046,6 @@ asmsub vpoke_mask(ubyte bank @A, uword address @R0, ubyte mask @X, ubyte value @ } asmsub save_virtual_registers() clobbers(A,Y) { - ; TODO: Romable %asm {{ ldy #31 - lda cx16.r0,y @@ -1077,7 +1076,6 @@ asmsub restore_virtual_registers() clobbers(A,Y) { asmsub save_vera_context() clobbers(A) { ; -- use this at the start of your IRQ handler if it uses Vera registers, to save the state - ; TODO: Romable %asm {{ ; note cannot store this on cpu hardware stack because this gets called as a subroutine lda cx16.VERA_ADDR_L @@ -1526,7 +1524,6 @@ _vector .word ? } asmsub restore_irq() clobbers(A) { - ; TODO: Romable %asm {{ sei lda _orig_irqvec @@ -1863,7 +1860,6 @@ _no_msb_size } asmsub save_prog8_internals() { - ; TODO: Romable %asm {{ lda P8ZP_SCRATCH_B1 sta save_SCRATCH_ZPB1 @@ -1934,7 +1930,7 @@ save_SCRATCH_ZPWORD2 .word ? sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta p8_sys_startup.cleanup_at_exit._exitcodeCarry + sta p8_sys_startup.cleanup_at_exit._exitcarry stx p8_sys_startup.cleanup_at_exit._exitcodeX sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer @@ -2064,7 +2060,6 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ lda #1 sta $00 ; ram bank 1 @@ -2073,16 +2068,21 @@ asmsub cleanup_at_exit() { jsr cbm.CLRCHN ; reset i/o channels lda #9 jsr cbm.CHROUT ; enable charset switch -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } diff --git a/compiler/res/prog8lib/pet32/syslib.p8 b/compiler/res/prog8lib/pet32/syslib.p8 index 34c784cb6..2aab9fc31 100644 --- a/compiler/res/prog8lib/pet32/syslib.p8 +++ b/compiler/res/prog8lib/pet32/syslib.p8 @@ -360,7 +360,6 @@ _no_msb_size } asmsub save_prog8_internals() { - ; TODO: Romable %asm {{ lda P8ZP_SCRATCH_B1 sta save_SCRATCH_ZPB1 @@ -431,7 +430,7 @@ save_SCRATCH_ZPWORD2 .word ? sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta p8_sys_startup.cleanup_at_exit._exitcodeCarry + sta p8_sys_startup.cleanup_at_exit._exitcarry stx p8_sys_startup.cleanup_at_exit._exitcodeX sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer @@ -606,7 +605,6 @@ cx16 { &byte r15sH = $7fff asmsub save_virtual_registers() clobbers(A,Y) { - ; TODO: Romable %asm {{ ldy #31 - lda cx16.r0,y @@ -671,18 +669,22 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } diff --git a/examples/customtarget/libraries/atari/syslib.p8 b/examples/customtarget/libraries/atari/syslib.p8 index 35fa64a17..7c0590440 100644 --- a/examples/customtarget/libraries/atari/syslib.p8 +++ b/examples/customtarget/libraries/atari/syslib.p8 @@ -330,7 +330,7 @@ save_SCRATCH_ZPWORD2 .word 0 sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta p8_sys_startup.cleanup_at_exit._exitcodeCarry + sta p8_sys_startup.cleanup_at_exit._exitcarry stx p8_sys_startup.cleanup_at_exit._exitcodeX sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer @@ -563,18 +563,22 @@ p8_sys_startup { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } } diff --git a/examples/customtarget/libraries/neo6502/syslib.p8 b/examples/customtarget/libraries/neo6502/syslib.p8 index 51ba3ce63..61bdbefb0 100644 --- a/examples/customtarget/libraries/neo6502/syslib.p8 +++ b/examples/customtarget/libraries/neo6502/syslib.p8 @@ -238,37 +238,37 @@ save_SCRATCH_ZPWORD2 .word 0 asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ - sta cleanup_at_exit._exitcode + sta p8_sys_startup.cleanup_at_exit._exitcode ldx prog8_lib.orig_stackpointer txs - jmp cleanup_at_exit + jmp p8_sys_startup.cleanup_at_exit }} } asmsub exit2(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y) { ; -- immediately exit the program with result values in the A, X and Y registers. %asm {{ - sta cleanup_at_exit._exitcode - stx cleanup_at_exit._exitcodeX - sty cleanup_at_exit._exitcodeY + sta p8_sys_startup.cleanup_at_exit._exitcode + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer txs - jmp cleanup_at_exit + jmp p8_sys_startup.cleanup_at_exit }} } asmsub exit3(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y, bool carry @Pc) { ; -- immediately exit the program with result values in the A, X and Y registers, and the Carry flag in the status register. %asm {{ - sta cleanup_at_exit._exitcode + sta p8_sys_startup.cleanup_at_exit._exitcode lda #0 rol a - sta cleanup_at_exit._exitcodeCarry - stx cleanup_at_exit._exitcodeX - sty cleanup_at_exit._exitcodeY + sta p8_sys_startup.cleanup_at_exit._exitcarry + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY ldx prog8_lib.orig_stackpointer txs - jmp cleanup_at_exit + jmp p8_sys_startup.cleanup_at_exit }} } @@ -493,18 +493,22 @@ p8_sys_startup { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } diff --git a/examples/customtarget/libraries/tinyc64/syslib.p8 b/examples/customtarget/libraries/tinyc64/syslib.p8 index 2c1d5ea18..ce5a1803a 100644 --- a/examples/customtarget/libraries/tinyc64/syslib.p8 +++ b/examples/customtarget/libraries/tinyc64/syslib.p8 @@ -7,6 +7,20 @@ sys { ; these push/pop routines are always required by the compiler: + inline asmsub progend() -> uword @AY { + %asm {{ + lda #prog8_program_end + }} + } + + inline asmsub progstart() -> uword @AY { + %asm {{ + lda #prog8_program_start + }} + } + inline asmsub push(ubyte value @A) { %asm {{ pha @@ -44,6 +58,43 @@ sys { jmp ($fffc) ; reset vector }} } + + asmsub exit(ubyte returnvalue @A) { + ; -- immediately exit the program with a return code in the A register + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit2(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y) { + ; -- immediately exit the program with result values in the A, X and Y registers. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit3(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y, bool carry @Pc) { + ; -- immediately exit the program with result values in the A, X and Y registers, and the Carry flag in the status register. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + lda #0 + rol a + sta p8_sys_startup.cleanup_at_exit._exitcarry + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } } @@ -64,18 +115,22 @@ p8_sys_startup { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } } diff --git a/examples/customtarget/libraries/tinycx16/syslib.p8 b/examples/customtarget/libraries/tinycx16/syslib.p8 index 34a88679e..10fff8636 100644 --- a/examples/customtarget/libraries/tinycx16/syslib.p8 +++ b/examples/customtarget/libraries/tinycx16/syslib.p8 @@ -7,6 +7,20 @@ sys { ; these push/pop routines are always required by the compiler: + inline asmsub progend() -> uword @AY { + %asm {{ + lda #prog8_program_end + }} + } + + inline asmsub progstart() -> uword @AY { + %asm {{ + lda #prog8_program_start + }} + } + inline asmsub push(ubyte value @A) { %asm {{ pha @@ -48,6 +62,42 @@ sys { }} } + asmsub exit(ubyte returnvalue @A) { + ; -- immediately exit the program with a return code in the A register + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit2(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y) { + ; -- immediately exit the program with result values in the A, X and Y registers. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit3(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y, bool carry @Pc) { + ; -- immediately exit the program with result values in the A, X and Y registers, and the Carry flag in the status register. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + lda #0 + rol a + sta p8_sys_startup.cleanup_at_exit._exitcarry + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } } @@ -68,18 +118,23 @@ p8_sys_startup { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! + }} } } diff --git a/examples/customtarget/libraries/tinypet/syslib.p8 b/examples/customtarget/libraries/tinypet/syslib.p8 index 5a697c2d4..92660c592 100644 --- a/examples/customtarget/libraries/tinypet/syslib.p8 +++ b/examples/customtarget/libraries/tinypet/syslib.p8 @@ -7,6 +7,20 @@ sys { ; these push/pop routines are always required by the compiler: + inline asmsub progend() -> uword @AY { + %asm {{ + lda #prog8_program_end + }} + } + + inline asmsub progstart() -> uword @AY { + %asm {{ + lda #prog8_program_start + }} + } + inline asmsub push(ubyte value @A) { %asm {{ pha @@ -42,6 +56,43 @@ sys { jmp ($fffc) ; reset vector }} } + + asmsub exit(ubyte returnvalue @A) { + ; -- immediately exit the program with a return code in the A register + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit2(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y) { + ; -- immediately exit the program with result values in the A, X and Y registers. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } + + asmsub exit3(ubyte resulta @A, ubyte resultx @X, ubyte resulty @Y, bool carry @Pc) { + ; -- immediately exit the program with result values in the A, X and Y registers, and the Carry flag in the status register. + %asm {{ + sta p8_sys_startup.cleanup_at_exit._exitcode + lda #0 + rol a + sta p8_sys_startup.cleanup_at_exit._exitcarry + stx p8_sys_startup.cleanup_at_exit._exitcodeX + sty p8_sys_startup.cleanup_at_exit._exitcodeY + ldx prog8_lib.orig_stackpointer + txs + jmp p8_sys_startup.cleanup_at_exit + }} + } } @@ -62,18 +113,22 @@ p8_sys_startup { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts - ; TODO: Romable %asm {{ -_exitcodeCarry = *+1 - lda #0 + lda _exitcarry lsr a -_exitcode = *+1 - lda #0 ; exit code possibly modified in exit() -_exitcodeX = *+1 - ldx #0 -_exitcodeY = *+1 - ldy #0 + lda _exitcode + ldx _exitcodeX + ldy _exitcodeY rts + + .section BSS +_exitcarry .byte ? +_exitcode .byte ? +_exitcodeX .byte ? +_exitcodeY .byte ? + .send BSS + + ; !notreached! }} } } diff --git a/examples/test.p8 b/examples/test.p8 index 16b127db3..81d140eb5 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,14 +4,8 @@ main { sub start() { - str name = "irmen!" - txt.print(name) - txt.nl() - sys.memset(&name, len(name), 'a') - txt.print(name) - txt.nl() - sys.memsetw(&name, len(name)/2, $4041) - txt.print(name) - txt.nl() + sys.exit3(100,101,102,true) + sys.exit2(100,101,102) + sys.exit(100) } }