move target() builtin to sys.target constant

This commit is contained in:
Irmen de Jong 2021-01-07 23:36:28 +01:00
parent 87cee7a0fd
commit 025dde264a
7 changed files with 26 additions and 33 deletions

View File

@ -474,6 +474,9 @@ _raster_irq_handler
sys {
; ------- lowlevel system routines --------
const ubyte target = 64 ; compilation target specifier. 64 = C64, 16 = CommanderX16.
asmsub reset_system() {
; Soft-reset the system back to Basic prompt.
%asm {{

View File

@ -473,6 +473,9 @@ asmsub init_system() {
sys {
; ------- lowlevel system routines --------
const ubyte target = 16 ; compilation target specifier. 64 = C64, 16 = CommanderX16.
asmsub reset_system() {
; Soft-reset the system back to Basic prompt.
%asm {{

View File

@ -6,9 +6,6 @@ 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.*
@ -146,7 +143,6 @@ private val functionSignatures: List<FSignature> = listOf(
FSignature("read_flags" , true, emptyList(), DataType.UBYTE),
FSignature("progend" , true, emptyList(), DataType.UWORD),
FSignature("memory" , true, listOf(FParam("name", setOf(DataType.STR)), FParam("size", setOf(DataType.UWORD))), 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),
@ -452,17 +448,6 @@ private fun builtinSgn(args: List<Expression>, position: Position, program: Prog
return NumericLiteralValue(DataType.BYTE, constval.number.toDouble().sign.toInt().toShort(), position)
}
private fun builtinTarget(args: List<Expression>, 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 =

View File

@ -34,6 +34,17 @@ as ROM/kernal subroutine definitions, memory location constants, and utility sub
Many of these definitions overlap for the C64 and Commander X16 targets so it is still possible
to write programs that work on both targets without modifications.
``sys.target``
A constant ubyte value designating the target machine that the program is compiled for.
Notice that this is a compile-time constant value and is not determined on the
system when the program is running.
The following return values are currently defined:
- 16 = compiled for CommanderX16 with 65C02 CPU
- 64 = compiled for Commodore-64 with 6502/6510 CPU
conv
----
Routines to convert strings to numbers or vice versa.
@ -69,40 +80,40 @@ string
------
Provides string manipulation routines.
length(str) -> ubyte length
``length(str) -> ubyte length``
Number of bytes in the string. This value is determined during runtime and counts upto
the first terminating 0 byte in the string, regardless of the size of the string during compilation time.
Don't confuse this with ``len`` and ``sizeof``
left(source, length, target)
``left(source, length, target)``
Copies the left side of the source string of the given length to target string.
It is assumed the target string buffer is large enough to contain the result.
Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
Modifies in-place, doesn't return a value (so can't be used in an expression).
right(source, length, target)
``right(source, length, target)``
Copies the right side of the source string of the given length to target string.
It is assumed the target string buffer is large enough to contain the result.
Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
Modifies in-place, doesn't return a value (so can't be used in an expression).
slice(source, start, length, target)
``slice(source, start, length, target)``
Copies a segment from the source string, starting at the given index,
and of the given length to target string.
It is assumed the target string buffer is large enough to contain the result.
Also, you have to make sure yourself that start and length are within bounds of the strings.
Modifies in-place, doesn't return a value (so can't be used in an expression).
find(string, char) -> uword address
``find(string, char) -> uword address``
Locates the first position of the given character in the string, returns the string starting
with this character or $0000 if the character is not found.
compare(string1, string2) -> ubyte result
``compare(string1, string2) -> ubyte result``
Returns -1, 0 or 1 depeding on wether string1 sorts before, equal or after string2.
Note that you can also directly compare strings and string values with eachother
using ``==``, ``<`` etcetera (it will use string.compare for you under water automatically).
copy(from, to) -> ubyte length
``copy(from, to) -> ubyte length``
Copy a string to another, overwriting that one. Returns the length of the string that was copied.
Often you don't have to call this explicitly and can just write ``string1 = string2``
but this function is useful if you're dealing with addresses for instance.

View File

@ -888,15 +888,6 @@ 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.
Notice that this is a compile-time constant value and is not determined on the
system when the program is running.
The following return values are currently defined:
- 16 = compiled for CommanderX16 with 65C02 CPU
- 64 = compiled for Commodore-64 with 6502/6510 CPU
memory(name, size)
Statically allocates a fixed portion of memory of the given size in bytes, and returns its address.
Slabs are considered identical if their name and size are the same.

View File

@ -2,8 +2,7 @@
TODO
====
- move all str* builtin functions to a strings library module, mem* to the sys module. update docs.
- move target() builtin to sys.target constant
- move all mem* builtins to the sys module. update docs.
- use (zp) addressing mode on 65c02 specific code rather than ldy#0 / lda (zp),y
- optimize pointer access code @(pointer)? use a subroutine? macro? 65c02 vs 6502?
- allow byte return type with single register for asmsubs, for instance string.compare

View File

@ -7,6 +7,7 @@
main {
sub start() {
txt.print_ub(sys.target)
}
sub start2 () {