From bba4f8450324012a8e4e1ba18b31f96992a31840 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 22 Dec 2020 06:13:14 +0100 Subject: [PATCH] added target() function --- compiler/src/prog8/functions/BuiltinFunctions.kt | 15 +++++++++++++++ docs/source/programming.rst | 7 +++++++ docs/source/todo.rst | 1 - examples/test.p8 | 3 +-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 8a7d0622f..967496b6e 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -6,6 +6,9 @@ import prog8.ast.expressions.* import prog8.ast.statements.StructDecl import prog8.ast.statements.VarDecl import prog8.compiler.CompilerException +import prog8.compiler.target.C64Target +import prog8.compiler.target.CompilationTarget +import prog8.compiler.target.Cx16Target import kotlin.math.* @@ -142,6 +145,7 @@ private val functionSignatures: List = listOf( FSignature("clear_irqd" , false, emptyList(), null), FSignature("read_flags" , false, emptyList(), DataType.UBYTE), FSignature("progend" , true, emptyList(), DataType.UWORD), + FSignature("target" , true, emptyList(), DataType.UBYTE, ::builtinTarget), FSignature("swap" , false, listOf(FParam("first", NumericDatatypes), FParam("second", NumericDatatypes)), null), FSignature("memcopy" , false, listOf( FParam("from", IterableDatatypes + DataType.UWORD), @@ -480,6 +484,17 @@ private fun builtinSgn(args: List, position: Position, program: Prog return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position) } +private fun builtinTarget(args: List, position: Position, program: Program): NumericLiteralValue { + if (args.isNotEmpty()) + throw SyntaxError("target requires no arguments", position) + val target = when(CompilationTarget.instance) { + is C64Target -> 64 + is Cx16Target -> 16 + else -> throw CompilerException("unrecognised compilation target") + } + return NumericLiteralValue(DataType.UBYTE, target, position) +} + private fun numericLiteral(value: Number, position: Position): NumericLiteralValue { val floatNum=value.toDouble() val tweakedValue: Number = diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 67348966b..c935d7928 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -912,6 +912,13 @@ set_irqd() / clear_irqd() swap(x, y) Swap the values of numerical variables (or memory locations) x and y in a fast way. +target() + Returns byte value designating the target machine that the program was compiled for. + The following return values are currently defined: + + - 16 = compiled for CommanderX16 with 65C02 CPU + - 64 = compiled for Commodore-64 with 6502/6510 CPU + progend() Returns the last address of the program in memory + 1. Can be used to load dynamic data after the program, instead of hardcoding something. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index c1d1d5a66..4f1e0fe43 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- add target() function that returns 16=cx16, 64=C64 - Cx16 target: support full-screen 640x480 and 320x240 graphics? That requires our own custom graphics routines though to draw lines. - hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine) - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' diff --git a/examples/test.p8 b/examples/test.p8 index 2ceb1fe8c..d6c4f9e94 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -83,8 +83,7 @@ _y .byte 0 } sub start () { - ubyte ss = withasm(derp(1), 33,66) - txt.print_ub(ss) + txt.print_ub(target()) txt.chrout('\n') ; cx16.r0 = 65535