From 288d4f08b3cd3f2bb6febe976046af635bac28d7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 3 Nov 2020 22:42:59 +0100 Subject: [PATCH] implemented cc for integer sin and cos variants --- compiler/res/prog8lib/prog8_lib.asm | 153 +++++++++--------- .../c64/codegen/BuiltinFunctionsAsmGen.kt | 30 ++-- examples/test.p8 | 34 ++++ 3 files changed, 127 insertions(+), 90 deletions(-) diff --git a/compiler/res/prog8lib/prog8_lib.asm b/compiler/res/prog8lib/prog8_lib.asm index f8843213d..be75cfb9f 100644 --- a/compiler/res/prog8lib/prog8_lib.asm +++ b/compiler/res/prog8lib/prog8_lib.asm @@ -747,147 +747,142 @@ func_sqrt16 .proc func_sin8_into_A .proc - inx - ldy P8ESTACK_LO,x - lda func_sin8._sinecos8,y + tay + lda _sinecos8,y rts +_sinecos8 .char trunc(127.0 * sin(range(256+64) * rad(360.0/256.0))) .pend func_sin8u_into_A .proc - inx - ldy P8ESTACK_LO,x - lda func_sin8u._sinecos8u,y + tay + lda _sinecos8u,y rts +_sinecos8u .byte trunc(128.0 + 127.5 * sin(range(256+64) * rad(360.0/256.0))) .pend func_sin16_into_AY .proc - inx - ldy P8ESTACK_LO,x - lda func_sin16._sinecos8lo,y + tay + lda _sinecos8lo,y pha - lda func_sin16._sinecos8hi,y + lda _sinecos8hi,y tay pla rts +_ := trunc(32767.0 * sin(range(256+64) * rad(360.0/256.0))) +_sinecos8lo .byte <_ +_sinecos8hi .byte >_ .pend func_sin16u_into_AY .proc - inx - ldy P8ESTACK_LO,x - lda func_sin16u._sinecos8ulo,y + tay + lda _sinecos8ulo,y pha - lda func_sin16u._sinecos8uhi,y + lda _sinecos8uhi,y tay pla rts +_ := trunc(32768.0 + 32767.5 * sin(range(256+64) * rad(360.0/256.0))) +_sinecos8ulo .byte <_ +_sinecos8uhi .byte >_ .pend func_cos8_into_A .proc - inx - ldy P8ESTACK_LO,x - lda func_sin8._sinecos8+64,y + tay + lda func_sin8_into_A._sinecos8+64,y rts .pend func_cos8u_into_A .proc - inx - ldy P8ESTACK_LO,x - lda func_sin8u._sinecos8u+64,y + tay + lda func_sin8u_into_A._sinecos8u+64,y rts .pend func_cos16_into_AY .proc - inx - ldy P8ESTACK_LO,x - lda func_sin16._sinecos8lo+64,y + tay + lda func_sin16_into_AY._sinecos8lo+64,y pha - lda func_sin16._sinecos8hi+64,y + lda func_sin16_into_AY._sinecos8hi+64,y tay pla rts .pend func_cos16u_into_AY .proc - inx - ldy P8ESTACK_LO,x - lda func_sin16u._sinecos8ulo+64,y + tay + lda func_sin16u_into_AY._sinecos8ulo+64,y pha - lda func_sin16u._sinecos8uhi+64,y + lda func_sin16u_into_AY._sinecos8uhi+64,y tay pla rts .pend -func_sin8 .proc - ldy P8ESTACK_LO+1,x - lda _sinecos8,y - sta P8ESTACK_LO+1,x +func_sin8_cc .proc + tay + lda func_sin8_into_A._sinecos8,y + sta P8ESTACK_LO,x + dex rts -_sinecos8 .char trunc(127.0 * sin(range(256+64) * rad(360.0/256.0))) .pend -func_sin8u .proc - ldy P8ESTACK_LO+1,x +func_sin8u_cc .proc + tay lda _sinecos8u,y - sta P8ESTACK_LO+1,x + sta P8ESTACK_LO,x + dex rts -_sinecos8u .byte trunc(128.0 + 127.5 * sin(range(256+64) * rad(360.0/256.0))) .pend -func_sin16 .proc - ldy P8ESTACK_LO+1,x - lda _sinecos8lo,y +func_sin16_cc .proc + tay + lda func_sin16_into_AY._sinecos8lo,y sta P8ESTACK_LO+1,x - lda _sinecos8hi,y + lda func_sin16_into_AY._sinecos8hi,y sta P8ESTACK_HI+1,x rts -_ := trunc(32767.0 * sin(range(256+64) * rad(360.0/256.0))) -_sinecos8lo .byte <_ -_sinecos8hi .byte >_ .pend -func_sin16u .proc - ldy P8ESTACK_LO+1,x - lda _sinecos8ulo,y +func_sin16u_cc .proc + tay + lda func_sin16u_into_AY._sinecos8ulo,y sta P8ESTACK_LO+1,x - lda _sinecos8uhi,y - sta P8ESTACK_HI+1,x - rts - -_ := trunc(32768.0 + 32767.5 * sin(range(256+64) * rad(360.0/256.0))) -_sinecos8ulo .byte <_ -_sinecos8uhi .byte >_ - .pend - -func_cos8 .proc - ldy P8ESTACK_LO+1,x - lda func_sin8._sinecos8+64,y - sta P8ESTACK_LO+1,x - rts - .pend - -func_cos8u .proc - ldy P8ESTACK_LO+1,x - lda func_sin8u._sinecos8u+64,y - sta P8ESTACK_LO+1,x - rts - .pend - -func_cos16 .proc - ldy P8ESTACK_LO+1,x - lda func_sin16._sinecos8lo+64,y - sta P8ESTACK_LO+1,x - lda func_sin16._sinecos8hi+64,y + lda func_sin16u_into_AY._sinecos8uhi,y sta P8ESTACK_HI+1,x rts .pend -func_cos16u .proc - ldy P8ESTACK_LO+1,x - lda func_sin16u._sinecos8ulo+64,y +func_cos8_cc .proc + tay + lda func_sin8_into_A._sinecos8+64,y + sta P8ESTACK_LO,x + dex + rts + .pend + +func_cos8u_cc .proc + tay + lda func_sin8u_into_A._sinecos8u+64,y + sta P8ESTACK_LO,x + dex + rts + .pend + +func_cos16_cc .proc + tay + lda func_sin16_into_AY._sinecos8lo+64,y sta P8ESTACK_LO+1,x - lda func_sin16u._sinecos8uhi+64,y + lda func_sin16_into_AY._sinecos8hi+64,y + sta P8ESTACK_HI+1,x + rts + .pend + +func_cos16u_cc .proc + tay + lda func_sin16u_into_AY._sinecos8ulo+64,y + sta P8ESTACK_LO+1,x + lda func_sin16u_into_AY._sinecos8uhi+64,y sta P8ESTACK_HI+1,x rts .pend diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 7f4d992db..387083ed8 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -82,7 +82,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val "memcopy", "memset", "memsetw" -> funcMemSetCopy(fcall, func) "substr", "leftstr", "rightstr" -> { translateArguments(fcall.args, func) - asmgen.out(" jsr prog8_lib.func_${func.name}") + asmgen.out(" jsr prog8_lib.func_${func.name}_cc") // TODO } "exit" -> asmgen.out(" jmp prog8_lib.func_exit") else -> TODO("missing asmgen for builtin func ${func.name}") @@ -162,9 +162,9 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val private fun funcStrcmp(fcall: IFunctionCall, func: FSignature, resultToStack: Boolean) { translateArguments(fcall.args, func) if(resultToStack) - asmgen.out(" jsr prog8_lib.func_strcmp") + asmgen.out(" jsr prog8_lib.func_strcmp_cc") // TODO else - asmgen.out(" jsr prog8_lib.func_strcmp | inx") // result is also in register A + asmgen.out(" jsr prog8_lib.func_strcmp_into_A") // TODO } private fun funcSqrt16(fcall: IFunctionCall, func: FSignature, resultToStack: Boolean) { @@ -178,7 +178,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val private fun funcSinCosInt(fcall: IFunctionCall, func: FSignature, resultToStack: Boolean) { translateArguments(fcall.args, func) if(resultToStack) - asmgen.out(" jsr prog8_lib.func_${func.name}") + asmgen.out(" jsr prog8_lib.func_${func.name}_cc") // TODO else when(func.name) { "sin8", "sin8u", "cos8", "cos8u" -> asmgen.out(" jsr prog8_lib.func_${func.name}_into_A") @@ -194,33 +194,36 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val numElements = decl.arraysize!!.constIndex() when (decl.datatype) { DataType.ARRAY_UB, DataType.ARRAY_B -> { + // TODO cc asmgen.out(""" lda #<$varName ldy #>$varName sta P8ZP_SCRATCH_W1 sty P8ZP_SCRATCH_W1+1 lda #$numElements - jsr prog8_lib.reverse_b + jsr prog8_lib.reverse_b_cc """) } DataType.ARRAY_UW, DataType.ARRAY_W -> { + // TODO cc asmgen.out(""" lda #<$varName ldy #>$varName sta P8ZP_SCRATCH_W1 sty P8ZP_SCRATCH_W1+1 lda #$numElements - jsr prog8_lib.reverse_w + jsr prog8_lib.reverse_w_cc """) } DataType.ARRAY_F -> { + // TODO cc asmgen.out(""" lda #<$varName ldy #>$varName sta P8ZP_SCRATCH_W1 sty P8ZP_SCRATCH_W1+1 lda #$numElements - jsr prog8_lib.reverse_f + jsr prog8_lib.reverse_f_cc """) } else -> throw AssemblyError("weird type") @@ -236,6 +239,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val numElements = decl.arraysize!!.constIndex() when (decl.datatype) { DataType.ARRAY_UB, DataType.ARRAY_B -> { + // TODO cc asmgen.out(""" lda #<$varName ldy #>$varName @@ -244,9 +248,10 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val lda #$numElements sta P8ZP_SCRATCH_B1 """) - asmgen.out(if (decl.datatype == DataType.ARRAY_UB) " jsr prog8_lib.sort_ub" else " jsr prog8_lib.sort_b") + asmgen.out(if (decl.datatype == DataType.ARRAY_UB) " jsr prog8_lib.sort_ub_cc" else " jsr prog8_lib.sort_b_cc") } DataType.ARRAY_UW, DataType.ARRAY_W -> { + // TODO cc asmgen.out(""" lda #<$varName ldy #>$varName @@ -255,7 +260,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val lda #$numElements sta P8ZP_SCRATCH_B1 """) - asmgen.out(if (decl.datatype == DataType.ARRAY_UW) " jsr prog8_lib.sort_uw" else " jsr prog8_lib.sort_w") + asmgen.out(if (decl.datatype == DataType.ARRAY_UW) " jsr prog8_lib.sort_uw_cc" else " jsr prog8_lib.sort_w_cc") } DataType.ARRAY_F -> throw AssemblyError("sorting of floating point array is not supported") else -> throw AssemblyError("weird type") @@ -273,7 +278,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val is ArrayIndexedExpression -> { asmgen.translateExpression(what.arrayvar) asmgen.translateExpression(what.indexer) - asmgen.out(" jsr prog8_lib.ror2_array_ub") + asmgen.out(" jsr prog8_lib.ror2_array_ub_cc") // TODO cc } is DirectMemoryRead -> { if (what.addressExpression is NumericLiteralValue) { @@ -281,7 +286,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val asmgen.out(" lda ${number.toHex()} | lsr a | bcc + | ora #\$80 |+ | sta ${number.toHex()}") } else { asmgen.translateExpression(what.addressExpression) - asmgen.out(" jsr prog8_lib.ror2_mem_ub") + asmgen.out(" jsr prog8_lib.ror2_mem_ub_cc") // TODO cc } } is IdentifierReference -> { @@ -971,6 +976,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } private fun funcMkword(fcall: IFunctionCall, resultToStack: Boolean) { + // TODO cc if(resultToStack) { // trick: push the args in reverse order (lsb first, msb second) this saves some instructions asmgen.translateExpression(fcall.args[1]) @@ -986,6 +992,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } private fun funcMsb(fcall: IFunctionCall, resultToStack: Boolean) { + // TODO cc val arg = fcall.args.single() if (arg.inferType(program).typeOrElse(DataType.STRUCT) !in WordDatatypes) throw AssemblyError("msb required word argument") @@ -1006,6 +1013,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } private fun funcLsb(fcall: IFunctionCall, resultToStack: Boolean) { + // TODO cc val arg = fcall.args.single() if (arg.inferType(program).typeOrElse(DataType.STRUCT) !in WordDatatypes) throw AssemblyError("lsb required word argument") diff --git a/examples/test.p8 b/examples/test.p8 index ec1bed67d..df889bb61 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -11,6 +11,40 @@ main { sub start() { const uword ADDR = $0400 + ubyte ubb + ubyte zerobb + uword zeroww + + for ubb in 0 to 255 { + txt.print_uw(sin16u(ubb)+zerobb) + txt.chrout(' ') + } + txt.chrout('\n') + txt.chrout('\n') + for ubb in 0 to 255 { + txt.print_uw(cos16u(ubb)+zerobb) + txt.chrout(' ') + } + txt.chrout('\n') + txt.chrout('\n') + for ubb in 0 to 255 { + txt.print_w(sin16(ubb)+zerobb) + txt.chrout(' ') + } + txt.chrout('\n') + txt.chrout('\n') + for ubb in 0 to 255 { + txt.print_w(cos16(ubb)+zerobb) + txt.chrout(' ') + } + txt.chrout('\n') + txt.chrout('\n') + + testX() + + return + + byte zerob=0 ; word zerow=0 ; float zerof=0