diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index d67fbf49b..4a35aa795 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -3107,6 +3107,17 @@ $repeatLabel""") return "prog8_label_asm_${generatedLabelSequenceNumber}_$postfix" } + fun assignConstFloatToPointerAY(number: PtNumber) { + val floatConst = allocator.getFloatAsmConst(number.number) + out(""" + pha + lda #<$floatConst + sta P8ZP_SCRATCH_W1 + lda #>$floatConst + sta P8ZP_SCRATCH_W1+1 + pla + jsr floats.copy_float""") + } } /** diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 1ce45d592..113720bad 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -767,18 +767,38 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, } private fun funcPokeF(fcall: PtBuiltinFunctionCall) { - val tempvar = asmgen.getTempVarName(DataType.FLOAT) - asmgen.assignExpressionTo(fcall.args[1], - AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, DataType.FLOAT, fcall.definingISub(), fcall.position, tempvar, null, null, null, null)) - asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) - asmgen.out(""" - pha - lda #<$tempvar - sta P8ZP_SCRATCH_W1 - lda #>$tempvar - sta P8ZP_SCRATCH_W1+1 - pla - jsr floats.copy_float""") + when(val number = fcall.args[1]) { + is PtIdentifier -> { + val varName = asmgen.asmVariableName(number) + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.out(""" + pha + lda #<$varName + sta P8ZP_SCRATCH_W1 + lda #>$varName + sta P8ZP_SCRATCH_W1+1 + pla + jsr floats.copy_float""") + } + is PtNumber -> { + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.assignConstFloatToPointerAY(number) + } + else -> { + val tempvar = asmgen.getTempVarName(DataType.FLOAT) + asmgen.assignExpressionTo(fcall.args[1], + AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, DataType.FLOAT, fcall.definingISub(), fcall.position, tempvar, null, null, null, null)) + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.out(""" + pha + lda #<$tempvar + sta P8ZP_SCRATCH_W1 + lda #>$tempvar + sta P8ZP_SCRATCH_W1+1 + pla + jsr floats.copy_float""") + } + } } private fun funcPokeW(fcall: PtBuiltinFunctionCall) { diff --git a/compiler/res/prog8lib/cx16/floats.p8 b/compiler/res/prog8lib/cx16/floats.p8 index 142ce1016..3b3f72552 100644 --- a/compiler/res/prog8lib/cx16/floats.p8 +++ b/compiler/res/prog8lib/cx16/floats.p8 @@ -28,7 +28,8 @@ romsub $fe00 = AYINT() clobbers(A,X,Y) ; fac1-> signed word in 100-101 romsub $fe03 = GIVAYF(ubyte lo @ Y, ubyte hi @ A) clobbers(A,X,Y) romsub $fe06 = FOUT() clobbers(X) -> uword @ AY ; fac1 -> string, address returned in AY -; romsub $fe09 = VAL_1() clobbers(A,X,Y) ; convert ASCII string to floating point [not yet implemented!!!] see parse_f() instead +; romsub $fe09 = VAL_1() clobbers(A,X,Y) ; convert ASCII string to floating point [not yet implemented!!!] see parse_f() instead. +; once it is supported it works like this: call with "index" zp var ($a9/$aa) contains pointer to string, A = length of string. Float returned in FAC1. ; GETADR: fac1 -> unsigned word in Y/A (might throw ILLEGAL QUANTITY) (result also in $14/15) ; (tip: use GETADRAY to get A/Y output; lo/hi switched to normal little endian order) diff --git a/examples/test.p8 b/examples/test.p8 index 2b70fcc33..7f79d09d2 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,18 +1,18 @@ %import textio +%import floats +%zeropage basicsafe main { sub start() { - uword size1 = sizeof(22222) - txt.print_uw(size1) + poke($4000,123) + txt.print_ub(peek($4000)) txt.nl() - uword size2 = sizeof(2.2) - txt.print_uw(size2) + pokew($4002, 55555) + txt.print_uw(peekw($4002)) txt.nl() - cx16.r0 = sizeof(22222) - txt.print_uw(cx16.r0) - txt.nl() - cx16.r0 = sizeof(2.2) - txt.print_uw(cx16.r0) + float value=123.45678 + pokef($4004, value) + floats.print_f(peekf($4004)) txt.nl() } }