prog8/docs/source/syntaxreference.rst

714 lines
28 KiB
ReStructuredText
Raw Normal View History

2018-08-06 01:35:43 +00:00
.. _syntaxreference:
================
Syntax Reference
================
Module file
-----------
2018-09-15 14:35:15 +00:00
This is a file with the ``.p8`` suffix, containing *directives* and *code blocks*, described below.
2018-08-06 01:35:43 +00:00
The file is a text file wich can also contain:
2018-08-06 23:23:34 +00:00
Lines, whitespace, indentation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Line endings are significant because *only one* declaration, statement or other instruction can occur on every line.
Other whitespace and line indentation is arbitrary and ignored by the compiler.
You can use tabs or spaces as you wish.
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Source code comments
^^^^^^^^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Everything after a semicolon ``;`` is a comment and is ignored.
If the whole line is just a comment, it will be copied into the resulting assembly source code.
This makes it easier to understand and relate the generated code. Examples::
2018-08-06 01:35:43 +00:00
counter = 42 ; set the initial value to 42
2018-08-06 23:23:34 +00:00
; next is the code that...
2018-08-06 01:35:43 +00:00
.. _directives:
Directives
-----------
2020-09-09 20:49:32 +00:00
.. data:: %target <target>
Level: module.
Global setting, selects a compilation target from within the source file.
The default compilation target is "c64" which targets the Commodore-64 machine.
You can also omit this and use the ``-target`` command line option.
2018-08-06 01:35:43 +00:00
.. data:: %output <type>
Level: module.
Global setting, selects program output type. Default is ``prg``.
- type ``raw`` : no header at all, just the raw machine code data
- type ``prg`` : C64 program (with load address header)
.. data:: %launcher <type>
Level: module.
Global setting, selects the program launcher stub to use.
Only relevant when using the ``prg`` output type. Defaults to ``basic``.
- type ``basic`` : add a tiny C64 BASIC program, whith a SYS statement calling into the machine code
- type ``none`` : no launcher logic is added at all
2018-08-16 21:10:28 +00:00
.. data:: %zeropage <style>
2018-08-06 01:35:43 +00:00
Level: module.
Global setting, select ZeroPage handling style. Defaults to ``kernalsafe``.
- style ``kernalsafe`` -- use the part of the ZP that is 'free' or only used by BASIC routines,
and don't change anything else. This allows full use of KERNAL ROM routines (but not BASIC routines),
including default IRQs during normal system operation.
When the program exits, a system reset is performed (because BASIC will be in a corrupt state).
- style ``floatsafe`` -- like the previous one but also reserves the addresses that
are required to perform floating point operations (from the BASIC kernel). No clean exit is possible.
- style ``basicsafe`` -- the most restricted mode; only use the handful 'free' addresses in the ZP, and don't
touch change anything else. This allows full use of BASIC and KERNAL ROM routines including default IRQs
during normal system operation.
When the program exits, it simply returns to the BASIC ready prompt.
- style ``full`` -- claim the whole ZP for variables for the program, overwriting everything,
except the few addresses mentioned above that are used by the system's IRQ routine.
Even though the default IRQ routine is still active, it is impossible to use most BASIC and KERNAL ROM routines.
This includes many floating point operations and several utility routines that do I/O, such as ``print_string``.
As with ``kernalsafe``, it is not possible to cleanly exit the program, other than to reset the machine.
This option makes programs smaller and faster because even more variables can
be stored in the ZP (which allows for more efficient assembly code).
- style ``dontuse`` -- don't use *any* location in the zeropage.
Also read :ref:`zeropage`.
2018-08-06 01:35:43 +00:00
2018-10-08 22:01:53 +00:00
.. data:: %zpreserved <fromaddress>,<toaddress>
Level: module.
Global setting, can occur multiple times. It allows you to reserve or 'block' a part of the zeropage so
that it will not be used by the compiler.
2018-08-06 01:35:43 +00:00
.. data:: %address <address>
Level: module.
Global setting, set the program's start memory address
- default for ``raw`` output is ``$c000``
- default for ``prg`` output is ``$0801``
- cannot be changed if you select ``prg`` with a ``basic`` launcher;
2018-10-13 23:55:39 +00:00
then it is always ``$081e`` (immediately after the BASIC program), and the BASIC program itself is always at ``$0801``.
2018-08-06 01:35:43 +00:00
This is because the C64 expects BASIC programs to start at this address.
.. data:: %import <name>
Level: module, block.
This reads and compiles the named module source file as part of your current program.
Symbols from the imported module become available in your code,
without a module or filename prefix.
You can import modules one at a time, and importing a module more than once has no effect.
2018-08-13 08:51:05 +00:00
.. data:: %option <option> [, <option> ...]
2018-08-06 01:35:43 +00:00
2019-01-01 20:47:19 +00:00
Level: module, block.
2018-08-13 08:51:05 +00:00
Sets special compiler options.
2019-01-01 20:47:19 +00:00
For a module option, only the ``enable_floats`` option is recognised, which will tell the compiler
2018-08-13 08:51:05 +00:00
to deal with floating point numbers (by using various subroutines from the Commodore-64 kernal).
Otherwise, floating point support is not enabled.
2019-01-08 01:25:22 +00:00
When used in a block with the ``force_output`` option, it will force the block to be outputted
in the final program. Can be useful to make sure some
data is generated that would otherwise be discarded because it's not referenced (such as sprite data).
2018-08-06 01:35:43 +00:00
.. data:: %asmbinary "<filename>" [, <offset>[, <length>]]
Level: block.
This directive can only be used inside a block.
2018-09-15 14:21:05 +00:00
The assembler will include the file as binary bytes at this point, prog8 will not process this at all.
2018-08-06 01:35:43 +00:00
The optional offset and length can be used to select a particular piece of the file.
2019-04-16 23:33:07 +00:00
The file is located relative to the current working directory!
2018-08-06 01:35:43 +00:00
.. data:: %asminclude "<filename>", "scopelabel"
2018-08-06 01:35:43 +00:00
Level: block.
This directive can only be used inside a block.
The assembler will include the file as raw assembly source text at this point,
2018-09-15 14:21:05 +00:00
prog8 will not process this at all, with one exception: the labels.
The scopelabel argument will be used as a prefix to access the labels from the included source code,
2018-08-06 01:35:43 +00:00
otherwise you would risk symbol redefinitions or duplications.
If you know what you are doing you can leave it as an empty string to not have a scope prefix.
The compiler first looks for the file relative to the same directory as the module containing this statement is in,
if the file can't be found there it is searched relative to the current directory.
2018-08-06 01:35:43 +00:00
.. data:: %breakpoint
Level: block, subroutine.
Defines a debugging breakpoint at this location. See :ref:`debugging`
2018-08-10 21:56:30 +00:00
.. data:: %asm {{ ... }}
2018-08-06 01:35:43 +00:00
Level: block, subroutine.
Declares that there is *inline assembly code* in the lines enclosed by the curly braces.
This code will be written as-is into the generated output file.
2018-09-15 14:21:05 +00:00
The assembler syntax used should be for the 3rd party cross assembler tool that Prog8 uses.
2018-08-10 21:56:30 +00:00
Note that the start and end markers are both *double curly braces* to minimize the chance
that the inline assembly itself contains either of those. If it does contain a ``}}``,
the parsing of the inline assembler block will end prematurely and cause compilation errors.
2018-08-06 01:35:43 +00:00
Identifiers
-----------
Naming things in Prog8 is done via valid *identifiers*. They start with a letter,
2018-08-08 23:54:43 +00:00
and after that, a combination of letters, numbers, or underscores. Examples of valid identifiers::
2018-08-06 01:35:43 +00:00
a
A
monkey
COUNTER
Better_Name_2
something_strange__
2018-08-06 01:35:43 +00:00
Code blocks
-----------
A named block of actual program code. Itefines a *scope* (also known as 'namespace') and
2020-03-14 14:19:03 +00:00
can only contain *directives*, *variable declarations*, *subroutines* or *inline assembly*::
2018-08-06 01:35:43 +00:00
2019-07-29 21:11:13 +00:00
<blockname> [<address>] {
2018-08-06 01:35:43 +00:00
<directives>
<variables>
<subroutines>
2020-03-14 14:19:03 +00:00
<inline asm>
2018-08-06 01:35:43 +00:00
}
2018-08-10 21:56:30 +00:00
The <blockname> must be a valid identifier.
The <address> is optional. If specified it must be a valid memory address such as ``$c000``.
It's used to tell the compiler to put the block at a certain position in memory.
2018-08-06 01:35:43 +00:00
Also read :ref:`blocks`. Here is an example of a code block, to be loaded at ``$c000``::
2019-07-29 21:11:13 +00:00
main $c000 {
2018-08-06 01:35:43 +00:00
; this is code inside the block...
}
2018-08-06 23:23:34 +00:00
Labels
------
To label a position in your code where you can jump to from another place, you use a label::
nice_place:
; code ...
It's just an identifier followed by a colon ``:``. It's allowed to put the next statement on
the same line, after the label.
2018-08-06 01:35:43 +00:00
Variables and value literals
----------------------------
The data that the code works on is stored in variables. Variable names have to be valid identifiers.
Values in the source code are written using *value literals*. In the table of the supported
data types below you can see how they should be written.
2018-08-14 14:29:08 +00:00
2018-08-06 01:35:43 +00:00
Variable declarations
^^^^^^^^^^^^^^^^^^^^^
2018-08-06 23:23:34 +00:00
Variables should be declared with their exact type and size so the compiler can allocate storage
2019-07-15 00:26:51 +00:00
for them. You can give them an initial value as well. That value can be a simple literal value,
or an expression. If you don't provide an intial value yourself, zero will be used.
You can add a ``@zp`` zeropage-tag, to tell the compiler to prioritize it
2019-02-27 23:13:59 +00:00
when selecting variables to be put into zeropage.
The syntax is::
2018-08-06 23:23:34 +00:00
2019-02-27 23:13:59 +00:00
<datatype> [ @zp ] <variable name> [ = <initial value> ]
2018-08-06 23:23:34 +00:00
Various examples::
2018-09-11 22:51:48 +00:00
word thing = 0
byte counter = len([1, 2, 3]) * 20
byte age = 2018 - 1974
float wallet = 55.25
str name = "my name is Irmen"
str name = @"my name is Irmen" ; string with alternative byte encoding
2019-04-11 19:41:46 +00:00
uword address = &counter
byte[] values = [11, 22, 33, 44, 55]
byte[5] values ; array of 5 bytes, initially set to zero
2018-09-11 22:51:48 +00:00
byte[5] values = 255 ; initialize with five 255 bytes
2018-08-06 23:23:34 +00:00
2019-02-27 23:13:59 +00:00
word @zp zpword = 9999 ; prioritize this when selecting vars for zeropage storage
Color rgb = {1,255,0} ; a struct variable with initial values
2018-08-06 01:35:43 +00:00
Data types
^^^^^^^^^^
2018-09-15 14:21:05 +00:00
Prog8 supports the following data types:
2018-08-06 01:35:43 +00:00
=============== ======================= ================= =========================================
type identifier type storage size example var declaration and literal value
=============== ======================= ================= =========================================
2018-10-12 16:01:40 +00:00
``byte`` signed byte 1 byte = 8 bits ``byte myvar = -22``
``ubyte`` unsigned byte 1 byte = 8 bits ``ubyte myvar = $8f``, ``ubyte c = 'a'``, ``ubyte c2 = @'a'``
2018-08-06 01:35:43 +00:00
-- boolean 1 byte = 8 bits ``byte myvar = true`` or ``byte myvar == false``
The true and false are actually just aliases
for the byte values 1 and 0.
2018-10-12 16:01:40 +00:00
``word`` signed word 2 bytes = 16 bits ``word myvar = -12345``
``uword`` unsigned word 2 bytes = 16 bits ``uword myvar = $8fee``
2018-08-06 01:35:43 +00:00
``float`` floating-point 5 bytes = 40 bits ``float myvar = 1.2345``
stored in 5-byte cbm MFLPT format
``byte[x]`` signed byte array x bytes ``byte[4] myvar``
``ubyte[x]`` unsigned byte array x bytes ``ubyte[4] myvar``
``word[x]`` signed word array 2*x bytes ``word[4] myvar``
``uword[x]`` unsigned word array 2*x bytes ``uword[4] myvar``
``float[x]`` floating-point array 5*x bytes ``float[4] myvar``
``byte[]`` signed byte array depends on value ``byte[] myvar = [1, 2, 3, 4]``
``ubyte[]`` unsigned byte array depends on value ``ubyte[] myvar = [1, 2, 3, 4]``
``word[]`` signed word array depends on value ``word[] myvar = [1, 2, 3, 4]``
``uword[]`` unsigned word array depends on value ``uword[] myvar = [1, 2, 3, 4]``
``float[]`` floating-point array depends on value ``float[] myvar = [1.1, 2.2, 3.3, 4.4]``
2018-08-06 01:35:43 +00:00
``str`` string (petscii) varies ``str myvar = "hello."``
implicitly terminated by a 0-byte
=============== ======================= ================= =========================================
**arrays:** you can split an array initializer list over several lines if you want. When an initialization
value is given, the array size in the declaration can be omitted.
2018-08-06 01:35:43 +00:00
**hexadecimal numbers:** you can use a dollar prefix to write hexadecimal numbers: ``$20ac``
**binary numbers:** you can use a percent prefix to write binary numbers: ``%10010011``
Note that ``%`` is also the remainder operator so be careful: if you want to take the remainder
of something with an operand starting with 1 or 0, you'll have to add a space in between.
**character values:** you can use a single character in quotes like this ``'a'`` for the Petscii byte value of that character.
**``byte`` versus ``word`` values:**
2018-10-12 16:01:40 +00:00
- When an integer value ranges from 0..255 the compiler sees it as a ``ubyte``. For -128..127 it's a ``byte``.
- When an integer value ranges from 256..65535 the compiler sees it as a ``uword``. For -32768..32767 it's a ``word``.
- When a hex number has 3 or 4 digits, for example ``$0004``, it is seen as a ``word`` otherwise as a ``byte``.
- When a binary number has 9 to 16 digits, for example ``%1100110011``, it is seen as a ``word`` otherwise as a ``byte``.
- You can force a byte value into a word value by adding the ``.w`` datatype suffix to the number: ``$2a.w`` is equivalent to ``$002a``.
Data type conversion
^^^^^^^^^^^^^^^^^^^^
Many type conversions are possible by just writing ``as <type>`` at the end of an expression,
for example ``ubyte ub = floatvalue as ubyte`` will convert the floating point value to an unsigned byte.
2018-08-08 23:54:43 +00:00
Memory mapped variables
^^^^^^^^^^^^^^^^^^^^^^^
The ``&`` (address-of operator) used in front of a data type keyword, indicates that no storage
should be allocated by the compiler. Instead, the (mandatory) value assigned to the variable
2018-08-08 23:54:43 +00:00
should be the *memory address* where the value is located::
&byte BORDERCOLOR = $d020
&ubyte[5*40] top5screenrows = $0400 ; works for array as well
2018-08-08 23:54:43 +00:00
Direct access to memory locations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Instead of defining a memory mapped name for a specific memory location, you can also
2018-12-31 03:48:26 +00:00
directly access the memory. Enclose a numeric expression or literal with ``@(...)`` to do that::
color = @($d020) ; set the variable 'color' to the current c64 screen border color ("peek(53280)")
2018-12-31 03:48:26 +00:00
@($d020) = 0 ; set the c64 screen border to black ("poke 53280,0")
@(vic+$20) = 6 ; a dynamic expression to 'calculate' the address
2018-08-08 23:54:43 +00:00
Constants
^^^^^^^^^
All variables can be assigned new values unless you use the ``const`` keyword.
The initial value must be known at compile time (it must be a compile time constant expression).
2018-10-07 00:05:26 +00:00
This is only valid for the simple numeric types (byte, word, float)::
2018-08-08 23:54:43 +00:00
const byte max_age = 99
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Reserved names
^^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
The following names are reserved, they have a special meaning::
2018-08-06 01:35:43 +00:00
true false ; boolean values 1 and 0
2018-08-06 01:35:43 +00:00
Range expression
^^^^^^^^^^^^^^^^
2020-03-11 19:47:42 +00:00
A special value is the *range expression* which represents a range of numbers or characters,
from the starting value to (and including) the ending value::
<start> to <end> [ step <step> ]
<start> downto <end> [ step <step> ]
You an provide a step value if you need something else than the default increment which is one (or,
in case of downto, a decrement of one). Because a step of minus one is so common you can just use
the downto variant to avoid having to specify the step as well.
If used in the place of a literal value, it expands into the actual array of values::
byte[] array = 100 to 199 ; initialize array with [100, 101, ..., 198, 199]
2018-10-01 20:23:16 +00:00
Array indexing
^^^^^^^^^^^^^^
2018-10-30 19:29:03 +00:00
Strings and arrays are a sequence of values. You can access the individual values by indexing.
Syntax is familiar with brackets: ``arrayvar[x]`` ::
2018-10-01 20:23:16 +00:00
array[2] ; the third byte in the array (index is 0-based)
string[4] ; the fifth character (=byte) in the string
2019-07-12 17:01:36 +00:00
Struct
^^^^^^
A *struct* has to be defined to specify what its member variables are.
There are one or more members::
struct <structname> {
<vardecl>
[ <vardecl> ...]
}
You can only use numerical variables as member of a struct, so strings and arrays
and other structs can not be part of a struct. Vice versa, a struct can not occur in an array.
After defining a struct you can use the name of the struct as a data type to declare variables with.
Struct variables can be assigned a struct literal value (also in their declaration as initial value)::
Color rgb = {255, 100, 0} ; curly braces instead of brackets
2018-08-06 23:23:34 +00:00
Operators
---------
2018-08-06 01:35:43 +00:00
arithmetic: ``+`` ``-`` ``*`` ``/`` ``**`` ``%``
2018-09-21 22:33:25 +00:00
``+``, ``-``, ``*``, ``/`` are the familiar arithmetic operations.
``/`` is division (will result in integer division when using on integer operands, and a floating point division when at least one of the operands is a float)
``**`` is the power operator: ``3 ** 5`` is equal to 3*3*3*3*3 and is 243. (it only works on floating point variables)
``%`` is the remainder operator: ``25 % 7`` is 4. Be careful: without a space, %10 will be parsed as the binary number 2.
Remainder is only supported on integer operands (not floats).
2018-08-06 01:35:43 +00:00
bitwise arithmetic: ``&`` ``|`` ``^`` ``~`` ``<<`` ``>>``
2019-01-08 01:25:22 +00:00
``&`` is bitwise and, ``|`` is bitwise or, ``^`` is bitwise xor, ``~`` is bitwise invert (this one is an unary operator)
``<<`` is bitwise left shift and ``>>`` is bitwise right shift (both will not change the datatype of the value)
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
assignment: ``=``
Sets the target on the LHS (left hand side) of the operator to the value of the expression on the RHS (right hand side).
Note that an assignment sometimes is not possible or supported.
2018-08-06 01:35:43 +00:00
augmented assignment: ``+=`` ``-=`` ``*=`` ``/=`` ``**=`` ``&=`` ``|=`` ``^=`` ``<<=`` ``>>=``
This is syntactic sugar; ``aa += xx`` is equivalent to ``aa = aa + xx``
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
postfix increment and decrement: ``++`` ``--``
Syntactic sugar; ``aa++`` is equivalent to ``aa = aa + 1``, and ``aa--`` is equivalent to ``aa = aa - 1``.
2018-08-10 21:56:30 +00:00
Because these operations are so common, we have these short forms.
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
comparison: ``!=`` ``<`` ``>`` ``<=`` ``>=``
Equality, Inequality, Less-than, Greater-than, Less-or-Equal-than, Greater-or-Equal-than comparisons.
The result is a 'boolean' value 'true' or 'false' (which in reality is just a byte value of 1 or 0).
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
logical: ``not`` ``and`` ``or`` ``xor``
These operators are the usual logical operations that are part of a logical expression to reason
about truths (boolean values). The result of such an expression is a 'boolean' value 'true' or 'false'
(which in reality is just a byte value of 1 or 0).
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
range creation: ``to``
Creates a range of values from the LHS value to the RHS value, inclusive.
These are mainly used in for loops to set the loop range. Example::
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
0 to 7 ; range of values 0, 1, 2, 3, 4, 5, 6, 7 (constant)
2018-08-06 01:35:43 +00:00
aa = 5
aa = 10
aa to xx ; range of 5, 6, 7, 8, 9, 10
2018-08-06 01:35:43 +00:00
byte[] array = 10 to 13 ; sets the array to [1, 2, 3, 4]
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
for i in 0 to 127 {
; i loops 0, 1, 2, ... 127
}
2018-08-06 01:35:43 +00:00
2019-04-11 19:41:46 +00:00
address of: ``&``
This is a prefix operator that can be applied to a string or array variable or literal value.
It results in the memory address (UWORD) of that string or array in memory: ``uword a = &stringvar``
Sometimes the compiler silently inserts this operator to make it easier for instance
to pass strings or arrays as subroutine call arguments.
This operator can also be used as a prefix to a variable's data type keyword to indicate that
it is a memory mapped variable (for instance: ``&ubyte screencolor = $d021``)
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
precedence grouping in expressions, or subroutine parameter list: ``(`` *expression* ``)``
Parentheses are used to group parts of an expression to change the order of evaluation.
(the subexpression inside the parentheses will be evaluated first):
``(4 + 8) * 2`` is 24 instead of 20.
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
Parentheses are also used in a subroutine call, they follow the name of the subroutine and contain
the list of arguments to pass to the subroutine: ``big_function(1, 99)``
2018-08-06 01:35:43 +00:00
2018-08-10 21:56:30 +00:00
Subroutine / function calls
---------------------------
2018-08-06 23:23:34 +00:00
You call a subroutine like this::
[ void / result = ] subroutinename_or_address ( [argument...] )
2018-08-06 23:23:34 +00:00
; example:
resultvariable = subroutine(arg1, arg2, arg3)
void noresultvaluesub(arg)
2018-08-06 23:23:34 +00:00
Arguments are separated by commas. The argument list can also be empty if the subroutine
takes no parameters. If the subroutine returns a value, usually you assign it to a variable.
If you're not interested in the return value, prefix the function call with the ``void`` keyword.
Otherwise the compiler will warn you about discarding the result of the call.
Normal subroutines can only return zero or one return values.
2020-03-10 22:09:31 +00:00
However, the special ``asmsub`` routines (implemented in assembly code) or ``romsub`` routines
(referencing a routine in kernel ROM) can return more than one return value.
For example a status in the carry bit and a number in A, or a 16-bit value in A/Y registers.
2019-07-10 07:33:19 +00:00
It is not possible to process the results of a call to these kind of routines
directly from the language, because only single value assignments are possible.
You can still call the subroutine and not store the results.
But if you want to do something with the values it returns, you'll have to write
a small block of custom inline assembly that does the call and stores the values
appropriately. Don't forget to save/restore the registers if required.
2018-08-06 23:23:34 +00:00
Subroutine definitions
----------------------
2018-08-06 01:35:43 +00:00
The syntax is::
2018-10-08 20:35:39 +00:00
sub <identifier> ( [parameters] ) [ -> returntype ] {
2018-08-06 01:35:43 +00:00
... statements ...
}
2018-08-06 23:23:34 +00:00
; example:
sub triple_something (word amount) -> word {
2018-08-06 23:23:34 +00:00
return X * 3
}
The open curly brace must immediately follow the subroutine result specification on the same line,
and can have nothing following it. The close curly brace must be on its own line as well.
The parameters is a (possibly empty) comma separated list of "<datatype> <parametername>" pairs specifying the input parameters.
2018-10-08 20:35:39 +00:00
The return type has to be specified if the subroutine returns a value.
2018-08-06 23:23:34 +00:00
2020-03-10 22:09:31 +00:00
Assembly / ROM subroutines
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Subroutines implemented in ROM are usually defined by compiler library files, with the following syntax::
romsub $FFD5 = LOAD(ubyte verify @ A, uword address @ XY) -> clobbers() -> ubyte @Pc, ubyte @ A, ubyte @ X, ubyte @ Y
This defines the ``LOAD`` subroutine at ROM memory address $FFD5, taking arguments in all three registers A, X and Y,
and returning stuff in several registers as well. The ``clobbers`` clause is used to signify to the compiler
what CPU registers are clobbered by the call instead of being unchanged or returning a meaningful result value.
User subroutines in the program source code that are implemented purely in assembly and which have an assembly calling convention (i.e.
the parameters are strictly passed via cpu registers), are defined with ``asmsub`` like this::
2020-03-10 22:09:31 +00:00
asmsub clear_screenchars (ubyte char @ A) clobbers(Y) {
2020-03-10 22:09:31 +00:00
%asm {{
ldy #0
_loop sta c64.Screen,y
sta c64.Screen+$0100,y
sta c64.Screen+$0200,y
sta c64.Screen+$02e8,y
iny
bne _loop
rts
}}
2020-03-10 22:09:31 +00:00
}
the statement body of such a subroutine should consist of just an inline assembly block.
2018-08-06 23:23:34 +00:00
Expressions
-----------
Expressions calculate a value and can be used almost everywhere a value is expected.
They consist of values, variables, operators, function calls, type casts, direct memory reads,
and can be combined into other expressions.
Long expressions can be split over multiple lines by inserting a line break before or after an operator::
num_hours * 3600
+ num_minutes * 60
+ num_seconds
2018-08-06 23:23:34 +00:00
Loops
-----
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
for loop
^^^^^^^^
2018-09-16 01:00:32 +00:00
The loop variable must be a byte or word variable,
2019-08-18 12:14:14 +00:00
and must be defined first in the local scope of the for loop.
2018-09-16 01:00:32 +00:00
The expression that you loop over can be anything that supports iteration (such as ranges like ``0 to 100``,
2018-10-01 23:52:08 +00:00
array variables and strings) *except* floating-point arrays (because a floating-point loop variable is not supported).
2018-08-14 12:33:36 +00:00
2018-09-23 00:04:45 +00:00
You can use a single statement, or a statement block like in the example below::
2018-08-06 01:35:43 +00:00
for <loopvar> in <expression> [ step <amount> ] {
2018-08-06 23:23:34 +00:00
; do something...
2018-08-10 21:56:30 +00:00
break ; break out of the loop
2018-08-06 23:23:34 +00:00
}
2018-08-06 01:35:43 +00:00
2019-08-18 12:14:14 +00:00
For example, this is a for loop using a byte variable ``i``, defined before, to loop over a certain range of numbers::
ubyte i
...
for i in 20 to 155 {
; do something
}
2019-08-18 12:14:14 +00:00
And this is a loop over the values of the array ``fibonacci_numbers``::
2019-08-18 12:14:14 +00:00
uword[] fibonacci_numbers = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
2019-08-18 12:14:14 +00:00
uword number
for number in fibonacci_numbers {
; do something with number
}
2018-08-06 01:35:43 +00:00
2019-02-27 23:13:59 +00:00
2018-08-06 23:23:34 +00:00
while loop
^^^^^^^^^^
2018-08-14 12:33:36 +00:00
2018-09-23 00:04:45 +00:00
As long as the condition is true (1), repeat the given statement(s).
You can use a single statement, or a statement block like in the example below::
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
while <condition> {
; do something...
2018-08-10 21:56:30 +00:00
break ; break out of the loop
2018-08-06 23:23:34 +00:00
}
2018-08-06 01:35:43 +00:00
do-until loop
^^^^^^^^^^^^^
2018-08-14 12:33:36 +00:00
2018-09-23 00:04:45 +00:00
Until the given condition is true (1), repeat the given statement(s).
You can use a single statement, or a statement block like in the example below::
2018-08-06 23:23:34 +00:00
do {
2018-08-06 23:23:34 +00:00
; do something...
2018-08-10 21:56:30 +00:00
break ; break out of the loop
2018-08-06 23:23:34 +00:00
} until <condition>
2018-08-06 01:35:43 +00:00
repeat loop
^^^^^^^^^^^
2020-03-14 17:11:04 +00:00
When you're only interested in repeating something a given number of times.
It's a short hand for a for loop without an explicit loop variable::
2020-03-14 17:11:04 +00:00
repeat 15 {
; do something...
break ; you can break out of the loop
2020-03-14 17:11:04 +00:00
}
If you omit the iteration count, it simply loops forever.
You can still ``break`` out of such a loop if you want though.
2020-03-14 17:11:04 +00:00
2018-08-10 21:56:30 +00:00
Conditional Execution and Jumps
-------------------------------
2018-08-06 01:35:43 +00:00
2018-09-21 22:33:25 +00:00
Unconditional jump
2018-08-10 21:56:30 +00:00
^^^^^^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-08-13 08:51:05 +00:00
To jump to another part of the program, you use a ``goto`` statement with an addres or the name
of a label or subroutine::
2018-08-10 21:56:30 +00:00
goto $c000 ; address
goto name ; label or subroutine
2018-08-06 23:23:34 +00:00
2018-08-13 08:51:05 +00:00
Notice that this is a valid way to end a subroutine (you can either ``return`` from it, or jump
to another piece of code that eventually returns).
2018-08-14 14:29:08 +00:00
Conditional execution
2018-08-10 21:56:30 +00:00
^^^^^^^^^^^^^^^^^^^^^
2018-08-06 23:23:34 +00:00
2018-08-14 12:33:36 +00:00
With the 'if' / 'else' statement you can execute code depending on the value of a condition::
2018-09-23 00:04:45 +00:00
if <expression> <statements> [else <statements> ]
2018-08-13 08:51:05 +00:00
2018-09-02 16:32:48 +00:00
where <statements> can be just a single statement for instance just a ``goto``, or it can be a block such as this::
2018-08-14 00:22:59 +00:00
2018-09-23 00:04:45 +00:00
if <expression> {
2018-08-14 00:22:59 +00:00
<statements>
2018-08-14 12:33:36 +00:00
} else {
2018-08-14 00:22:59 +00:00
<alternative statements>
2018-08-14 12:33:36 +00:00
}
2018-09-02 16:32:48 +00:00
**Special status register branch form:**
There is a special form of the if-statement that immediately translates into one of the 6502's branching instructions.
It is almost the same as the regular if-statement but it lacks a contional expression part, because the if-statement
itself defines on what status register bit it should branch on::
if_XX <statements> [else <statements> ]
where <statements> can be just a single statement for instance just a ``goto``, or it can be a block such as this::
if_XX {
<statements>
} else {
<alternative statements>
}
The XX corresponds to one of the eigth branching instructions so the possibilities are:
2018-09-21 22:33:25 +00:00
``if_cs``, ``if_cc``, ``if_eq``, ``if_ne``, ``if_pl``, ``if_mi``, ``if_vs`` and ``if_vc``.
It can also be one of the four aliases that are easier to read: ``if_z``, ``if_nz``, ``if_pos`` and ``if_neg``.
2019-07-08 22:02:38 +00:00
2019-07-09 22:45:53 +00:00
when statement ('jump table')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The structure of a when statement is like this::
when <expression> {
<value(s)> -> <statement(s)>
<value(s)> -> <statement(s)>
...
[ else -> <statement(s)> ]
}
The when-*value* can be any expression but the choice values have to evaluate to
compile-time constant integers (bytes or words).
The else part is optional.
Choices can result in a single statement or a block of multiple statements in which
case you have to use { } to enclose them::
when value {
4 -> c64scr.print("four")
5 -> c64scr.print("five")
10,20,30 -> {
c64scr.print("ten or twenty or thirty")
2019-07-09 06:42:38 +00:00
}
2019-07-09 22:45:53 +00:00
else -> c64scr.print("don't know")
}