From cd49c5f88d4fafd55e4e4b8e62d51018885604e8 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 28 Sep 2024 20:27:58 +0200 Subject: [PATCH] cx16: set rom bank to 0 at startup (for faster kernal API calls) cx16: callfar() with constant address generates shorter asm --- .../codegen/cpu6502/BuiltinFunctionsAsmGen.kt | 29 +++++++++++++------ compiler/res/prog8lib/cx16/syslib.p8 | 4 ++- docs/source/todo.rst | 3 -- examples/test.p8 | 12 ++++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index d0294ffb3..3ebbc797d 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -284,15 +284,26 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, if(asmgen.options.compTarget.name != "cx16") throw AssemblyError("callfar only works on cx16 target at this time") - asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.A) // bank - asmgen.out(" sta (++)+0") - asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY) // jump address - asmgen.out(" sta (+)+0 | sty (+)+1") - asmgen.assignExpressionToRegister(fcall.args[2], RegisterOrPair.AY) // uword argument - asmgen.out(""" - jsr cx16.JSRFAR -+ .word 0 -+ .byte 0""") + val constBank = fcall.args[0].asConstInteger() + val constAddress = fcall.args[1].asConstInteger() + if(constBank!=null && constAddress!=null) { + asmgen.assignExpressionToRegister(fcall.args[2], RegisterOrPair.AY) // uword argument + asmgen.out(""" + jsr cx16.JSRFAR + .word ${constAddress.toHex()} + .byte $constBank""") + } else { + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.A) // bank + asmgen.out(" sta (++)+0") + asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY) // jump address + asmgen.out(" sta (+)+0 | sty (+)+1") + asmgen.assignExpressionToRegister(fcall.args[2], RegisterOrPair.AY) // uword argument + asmgen.out(""" + jsr cx16.JSRFAR ++ .word 0 ++ .byte 0""") + } + // note that by convention the values in A+Y registers are now the return value of the call. if(resultRegister!=null) { assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister, false, fcall.position, null, asmgen), RegisterOrPair.AY) diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index eb4ab799d..6f31f0588 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -483,7 +483,7 @@ romsub $C006 = x16edit_loadfile_options(ubyte firstbank @X, ubyte lastbank @Y, s uword filenameLengthAndOptions @R1, uword tabstopAndWordwrap @R2, uword disknumberAndColors @R3, uword headerAndStatusColors @R4) clobbers(A,X,Y) -; Audio (rom bank 10 - you have to activate this bank manually first! Or use callfar/JSRFAR.) +; Audio (rom bank 10 - you have to activate this bank manually first! Or use the stubs in the audio module. Or use callfar/JSRFAR manually.) romsub $C09F = audio_init() clobbers(A,X,Y) -> bool @Pc ; (re)initialize both vera PSG and YM audio chips romsub $C000 = bas_fmfreq(ubyte channel @A, uword freq @XY, bool noretrigger @Pc) clobbers(A,X,Y) -> bool @Pc romsub $C003 = bas_fmnote(ubyte channel @A, ubyte note @X, ubyte fracsemitone @Y, bool noretrigger @Pc) clobbers(A,X,Y) -> bool @Pc @@ -1397,6 +1397,7 @@ asmsub init_system() { lda #PROG8_VARSHIGH_RAMBANK sta $00 ; select ram bank lda #0 + sta $01 ; set ROM bank to kernal bank to speed up kernal calls tax tay cli @@ -1413,6 +1414,7 @@ asmsub init_system_phase2() { sta restore_irq._orig_irqvec+1 lda #PROG8_VARSHIGH_RAMBANK sta $00 ; select ram bank + stz $01 ; set ROM bank to kernal bank to speed up kernal calls cli cld clc diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 4dedc3e69..4de567291 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,9 +1,6 @@ TODO ==== -Commit "tweak program start initialization and fix cleanup at exit for atari and pet compiler targets #04cb684f" causing programs to crash (assem, rockrunner) - - Improve register load order in subroutine call args assignments: in certain situations, the "wrong" order of evaluation of function call arguments is done which results in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!) diff --git a/examples/test.p8 b/examples/test.p8 index 2ee9e1074..ac4155400 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,12 +3,12 @@ %option no_sysinit main { - ubyte @shared @nozp value1 = 99 - ubyte @shared @requirezp value2 = 42 sub start() { - txt.print_ub(value1) - txt.nl() - txt.print_ub(value2) - txt.nl() + uword @shared address = $C09F + ubyte @shared bank = 10 + uword @shared argument = $1234 + + void callfar(bank, address, argument) + void callfar(10, $C09F, argument) } }