2018-08-06 23:23:34 +00:00
.. _programstructure:
2018-08-06 01:35:43 +00:00
2018-09-15 14:21:05 +00:00
====================
Programming in Prog8
====================
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
This chapter describes a high level overview of the elements that make up a program.
Details about the syntax can be found in the :ref: `syntaxreference` chapter.
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Elements of a program
---------------------
Program
2020-03-14 14:19:03 +00:00
Consists of one or more *modules* .
2018-08-06 23:23:34 +00:00
Module
2020-03-14 14:19:03 +00:00
A file on disk with the `` .p8 `` suffix. It can contain *directives* and *code blocks* .
Whitespace and indentation in the source code are arbitrary and can be mixed tabs or spaces.
A module file can *import* other modules, including *library modules* .
2018-08-06 23:23:34 +00:00
Comments
2020-03-14 14:19:03 +00:00
Everything after a semicolon `` ; `` is a comment and is ignored by the compiler.
If the whole line is just a comment, this line will be copied into the resulting assembly source code for reference.
2018-08-06 23:23:34 +00:00
Directive
2020-03-14 14:19:03 +00:00
These are special instructions for the compiler, to change how it processes the code
and what kind of program it creates. A directive is on its own line in the file, and
starts with `` % `` , optionally followed by some arguments.
2018-08-06 23:23:34 +00:00
Code block
2020-03-14 14:19:03 +00:00
A block of actual program code. It has a starting address in memory,
and defines a *scope* (also known as 'namespace').
It contains variables and subroutines.
More details about this below: :ref: `blocks` .
2018-08-06 23:23:34 +00:00
Variable declarations
2020-03-14 14:19:03 +00:00
The data that the code works on is stored in variables ('named values that can change').
The compiler allocates the required memory for them.
There is *no dynamic memory allocation* . The storage size of all variables
is fixed and is determined at compile time.
Variable declarations tend to appear at the top of the code block that uses them.
They define the name and type of the variable, and its initial value.
Prog8 supports a small list of data types, including special 'memory mapped' types
that don't allocate storage but instead point to a fixed location in the address space.
2018-08-06 23:23:34 +00:00
Code
2020-03-14 14:19:03 +00:00
These are the instructions that make up the program's logic.
Code can only occur inside a subroutine.
There are different kinds of instructions ('statements' is a better name) such as:
2018-08-06 23:23:34 +00:00
2020-03-14 14:19:03 +00:00
- value assignment
2020-07-25 14:25:02 +00:00
- looping (for, while, do-until, repeat, unconditional jumps)
2020-03-14 14:19:03 +00:00
- conditional execution (if - then - else, when, and conditional jumps)
- subroutine calls
- label definition
2018-08-06 23:23:34 +00:00
Subroutine
2018-09-15 00:12:37 +00:00
Defines a piece of code that can be called by its name from different locations in your code.
2018-10-08 20:35:39 +00:00
It accepts parameters and can return a value (optional).
2018-09-15 00:12:37 +00:00
It can define its own variables, and it is even possible to define subroutines nested inside other subroutines.
Their contents is scoped accordingly.
2020-03-14 14:19:03 +00:00
Nested subroutines can access the variables from outer scopes.
This removes the need and overhead to pass everything via parameters.
2018-08-06 23:23:34 +00:00
Label
2018-09-15 00:12:37 +00:00
This is a named position in your code where you can jump to from another place.
You can jump to it with a jump statement elsewhere. It is also possible to use a
subroutine call to a label (but without parameters and return value).
2018-09-19 23:13:21 +00:00
Labels can only be defined in a block or in another subroutine, so you can't define a label
inside a loop statement block for instance.
2018-08-06 23:23:34 +00:00
Scope
Also known as 'namespace', this is a named box around the symbols defined in it.
This prevents name collisions (or 'namespace pollution'), because the name of the scope
is needed as prefix to be able to access the symbols in it.
Anything *inside* the scope can refer to symbols in the same scope without using a prefix.
2018-09-15 14:21:05 +00:00
There are three scopes in Prog8:
2018-08-06 23:23:34 +00:00
- global (no prefix)
- code block
- subroutine
Modules are *not* a scope! Everything defined in a module is merged into the global scope.
.. _blocks:
Blocks, Scopes, and accessing Symbols
-------------------------------------
2020-03-14 14:19:03 +00:00
**Blocks** are the top level separate pieces of code and data of your program. They have a
starting address in memory and will be combined together into a single output program.
They can only contain *directives* , *variable declarations* , *subroutines* and *inline assembly code* .
Your actual program code can only exist inside these subroutines.
(except the occasional inline assembly)
2018-08-06 23:23:34 +00:00
2020-03-14 14:19:03 +00:00
Here's an example::
2018-08-06 23:23:34 +00:00
2020-03-14 14:19:03 +00:00
main $c000 {
; this is code inside the block...
}
2018-08-06 23:23:34 +00:00
The name of a block must be unique in your entire program.
2020-03-14 14:19:03 +00:00
Be careful when importing other modules; blocks in your own code cannot have
2018-08-06 23:23:34 +00:00
the same name as a block defined in an imported module or library.
2018-09-07 22:07:25 +00:00
If you omit both the name and address, the entire block is *ignored* by the compiler (and a warning is displayed).
2018-08-06 23:23:34 +00:00
This is a way to quickly "comment out" a piece of code that is unfinshed or may contain errors that you
want to work on later, because the contents of the ignored block are not fully parsed either.
The address can be used to place a block at a specific location in memory.
Usually it is omitted, and the compiler will automatically choose the location (usually immediately after
the previous block in memory).
2020-03-14 14:19:03 +00:00
It must be >= `` $0200 `` (because `` $00 `` --`` $ff `` is the ZP and `` $100 `` --`` $1ff `` is the cpu stack).
2018-08-06 23:23:34 +00:00
2018-10-06 15:21:34 +00:00
.. _scopes:
**Scopes**
2018-10-02 23:11:28 +00:00
.. sidebar ::
Scoped access to symbols / "dotted names"
Every symbol is 'public' and can be accessed from elsewhere given its full "dotted name".
So, accessing a variable `` counter `` defined in subroutine `` worker `` in block `` main `` ,
can be done from anywhere by using `` main.worker.counter `` .
2018-10-06 15:21:34 +00:00
*Symbols* are names defined in a certain *scope* . Inside the same scope, you can refer
to them by their 'short' name directly. If the symbol is not found in the same scope,
the enclosing scope is searched for it, and so on, until the symbol is found.
2018-08-06 23:23:34 +00:00
2019-08-04 13:33:00 +00:00
Scopes are created using either of these two statements:
2018-08-06 23:23:34 +00:00
2018-10-06 15:21:34 +00:00
- blocks (top-level named scope)
2019-08-04 13:33:00 +00:00
- subroutines (nested named scope)
2020-03-23 00:24:41 +00:00
.. important ::
Unlike most other programming languages, a new scope is *not* created inside
2020-07-25 14:25:02 +00:00
for, while, repeat, and do-until statements, the if statement, and the branching conditionals.
2020-03-23 00:24:41 +00:00
These all share the same scope from the subroutine they're defined in.
You can define variables in these blocks, but these will be treated as if they
were defined in the subroutine instead.
This can seem a bit restrictive because you have to think harder about what variables you
want to use inside the subroutine, to avoid clashes.
But this decision was made for a good reason: memory in prog8's
2020-03-14 14:19:03 +00:00
target systems is usually very limited and it would be a waste to allocate a lot of variables.
The prog8 compiler is not yet advanced enough to be able to share or overlap
2020-03-23 00:24:41 +00:00
variables intelligently. So for now that is something you have to think about yourself.
2018-08-06 23:23:34 +00:00
Program Start and Entry Point
-----------------------------
Your program must have a single entry point where code execution begins.
The compiler expects a `` start `` subroutine in the `` main `` block for this,
taking no parameters and having no return value.
2018-09-28 19:28:16 +00:00
2018-08-06 23:23:34 +00:00
As any subroutine, it has to end with a `` return `` statement (or a `` goto `` call)::
2020-03-14 14:19:03 +00:00
main {
sub start () {
; program entrypoint code here
return
}
}
2018-08-06 23:23:34 +00:00
2018-09-28 19:28:16 +00:00
2018-08-06 23:23:34 +00:00
The `` main `` module is always relocated to the start of your programs
address space, and the `` start `` subroutine (the entrypoint) will be on the
first address. This will also be the address that the BASIC loader program (if generated)
calls with the SYS statement.
2018-09-28 19:28:16 +00:00
2018-08-08 23:54:43 +00:00
Variables and values
--------------------
2018-08-06 23:23:34 +00:00
2018-08-08 23:54:43 +00:00
Variables are named values that can change during the execution of the program.
2020-03-23 00:24:41 +00:00
They can be defined inside any scope (blocks, subroutines etc.) See :ref: `Scopes <scopes>` .
2018-09-18 21:14:32 +00:00
When declaring a numeric variable it is possible to specify the initial value, if you don't want it to be zero.
For other data types it is required to specify that initial value it should get.
2018-08-08 23:54:43 +00:00
Values will usually be part of an expression or assignment statement::
2018-08-06 23:23:34 +00:00
2018-10-22 22:52:51 +00:00
12345 ; integer number
$aa43 ; hex integer number
2018-12-10 23:09:37 +00:00
%100101 ; binary integer number (% is also remainder operator so be careful)
2018-10-22 22:52:51 +00:00
-33.456e52 ; floating point number
2020-03-10 23:32:50 +00:00
"Hi, I am a string" ; text string, encoded with compiler target default encoding
'a' ; byte value (ubyte) for the letter a
@"Alternate" ; text string, encoded with alternate encoding
@'a' ; byte value of the letter a, using alternate encoding
2018-08-06 23:23:34 +00:00
2018-10-22 22:52:51 +00:00
byte counter = 42 ; variable of size 8 bits, with initial value 42
2018-08-06 01:35:43 +00:00
2018-09-11 22:51:48 +00:00
2019-02-27 23:13:59 +00:00
*zeropage tag:*
If you add the `` @zp `` tag to the variable declaration, the compiler will prioritize this variable
when selecting variables to put into zero page. If there are enough free locations in the zeropage,
it will then try to fill it with as much other variables as possible (before they will be put in regular memory pages).
Example::
byte @zp zeropageCounter = 42
2019-01-26 16:32:26 +00:00
Integers
^^^^^^^^
Integers are 8 or 16 bit numbers and can be written in normal decimal notation,
in hexadecimal and in binary notation.
A single character in single quotes such as `` 'a' `` is translated into a byte integer,
which is the Petscii value for that character.
Unsigned integers are in the range 0-255 for unsigned byte types, and 0-65535 for unsigned word types.
The signed integers integers are in the range -128..127 for bytes,
and -32768..32767 for words.
Floating point numbers
^^^^^^^^^^^^^^^^^^^^^^
Floats are stored in the 5-byte 'MFLPT' format that is used on CBM machines,
and currently all floating point operations are specific to the Commodore-64.
This is because routines in the C-64 BASIC and KERNAL ROMs are used for that.
So floating point operations will only work if the C-64 BASIC ROM (and KERNAL ROM)
are banked in.
2020-09-21 16:21:24 +00:00
Also your code needs to import the `` floats `` library to enable floating point support
2019-01-26 16:32:26 +00:00
in the compiler, and to gain access to the floating point routines.
(this library contains the directive to enable floating points, you don't have
to worry about this yourself)
The largest 5-byte MFLPT float that can be stored is: **1.7014118345e+38** (negative: **-1.7014118345e+38** )
Arrays
^^^^^^
2020-09-28 22:03:47 +00:00
Array types are also supported. They can be made of bytes, words or floats, strings, and other arrays
(although the usefulness of the latter is very limited for now)::
2018-09-11 22:51:48 +00:00
2019-07-15 21:05:04 +00:00
byte[10] array ; array of 10 bytes, initially set to 0
2019-04-15 23:19:51 +00:00
byte[] array = [1, 2, 3, 4] ; initialize the array, size taken from value
byte[99] array = 255 ; initialize array with 99 times 255 [255, 255, 255, 255, ...]
byte[] array = 100 to 199 ; initialize array with [100, 101, ..., 198, 199]
2020-09-28 00:23:36 +00:00
str[] names = ["ally", "pete"] ; array of string pointers/addresses (equivalent to uword)
2020-09-28 22:03:47 +00:00
uword[] others = [names, array] ; array of pointers/addresses to other arrays
2018-10-01 20:23:16 +00:00
value = array[3] ; the fourth value in the array (index is 0-based)
char = string[4] ; the fifth character (=byte) in the string
2018-08-14 14:29:08 +00:00
2018-10-22 22:52:51 +00:00
.. note ::
2018-10-27 20:16:18 +00:00
Right now, the array should be small enough to be indexable by a single byte index.
This means byte arrays should be <= 256 elements, word arrays <= 128 elements, and float
2019-01-09 21:28:04 +00:00
arrays <= 51 elements.
2018-10-22 22:52:51 +00:00
2018-12-31 00:52:18 +00:00
You can split an array initializer list over several lines if you want.
2018-08-06 01:35:43 +00:00
2018-08-13 08:51:05 +00:00
Note that the various keywords for the data type and variable type (`` byte `` , `` word `` , `` const `` , etc.)
2018-10-22 22:52:51 +00:00
can't be used as *identifiers* elsewhere. You can't make a variable, block or subroutine with the name `` byte ``
2018-08-13 08:51:05 +00:00
for instance.
2020-10-17 18:35:36 +00:00
2020-10-10 02:30:28 +00:00
It's possible to assign a new array to another array, this will overwrite all elements in the original
array with those in the value array. The number and types of elements have to match.
For large arrays this is a slow operation because every element is copied over. It should probably be avoided.
2020-08-18 12:47:52 +00:00
**Arrays at a specific memory location:**
Using the memory-mapped syntax it is possible to define an array to be located at a specific memory location.
For instance to reference the first 5 rows of the Commodore 64's screen matrix as an array, you can define::
&ubyte[5*40] top5screenrows = $0400
This way you can set the second character on the second row from the top like this::
top5screenrows[41] = '!'
2018-09-06 19:13:49 +00:00
2019-01-26 16:32:26 +00:00
Strings
^^^^^^^
2018-09-04 21:37:21 +00:00
2019-01-26 16:32:26 +00:00
Strings are a sequence of characters enclosed in `` " `` quotes. The length is limited to 255 characters.
They're stored and treated much the same as a byte array,
but they have some special properties because they are considered to be *text* .
2020-03-10 23:32:50 +00:00
Strings in your source code files will be encoded (translated from ASCII/UTF-8) into bytes via the
default encoding that is used on the target platform. For the C-64, this is CBM PETSCII.
Alternate-encoding strings (prefixed with `` @ `` ) will be encoded via the alternate encoding for the
platform (if defined). For the C-64, that is SCREEN CODES (also known as POKE codes).
This @-prefix can also be used for character byte values.
2018-09-02 09:54:42 +00:00
2019-07-16 21:34:43 +00:00
You can concatenate two string literals using '+' (not very useful though) or repeat
2020-06-05 21:19:06 +00:00
a string literal a given number of times using '*'. You can also assign a new string
value to another string. No bounds check is done so be sure the destination string is
2020-10-03 13:11:09 +00:00
large enough to contain the new value (it is overwritten in memory)::
2019-07-16 21:34:43 +00:00
str string1 = "first part" + "second part"
str string2 = "hello!" * 10
2020-06-05 21:19:06 +00:00
string1 = string2
string1 = "new value"
2019-07-16 21:34:43 +00:00
2020-10-03 13:11:09 +00:00
There are several 'escape sequences' to help you put special characters into strings, such
as newlines, quote characters themselves, and so on. The ones used most often are
`` \\ `` , `` \" `` , `` \n `` , `` \r `` . For a detailed description of all of them and what they mean,
read the syntax reference on strings.
2020-10-17 18:35:36 +00:00
.. hint ::
2020-12-03 17:39:32 +00:00
Strings/arrays and uwords (=memory address) can often be interchanged.
2020-09-28 00:23:36 +00:00
An array of strings is actually an array of uwords where every element is the memory
address of the string. You can pass a memory address to assembly functions
that require a string as an argument.
2020-12-03 17:39:32 +00:00
For regular assignments you still need to use an explicit `` & `` (address-of) to take
the address of the string or array.
2020-09-28 00:23:36 +00:00
2019-01-26 16:32:26 +00:00
.. caution ::
2020-12-03 17:39:32 +00:00
It's probably best to avoid changing the contents in strings and treat them as static.
This includes changing certain letters by index, or by assigning a new value, or by
2020-06-05 21:19:06 +00:00
modifying the string via other means for example `` substr `` function and its cousins.
2020-12-03 17:39:32 +00:00
This is because the changes persist in memory. If your program exits and is restarted
(without reloading it from disk), it will then start working with the modified strings
instead of the original ones!
The same is true for arrays! So be careful to (re)initialize them if needed.
2018-09-02 09:54:42 +00:00
2018-08-13 08:51:05 +00:00
2019-07-12 17:01:36 +00:00
Structs
^^^^^^^
A struct is a group of one or more other variables.
This allows you to reuse the definition and manipulate it as a whole.
Individual variables in the struct are accessed as you would expect, just
use a scoped name to refer to them: `` structvariable.membername `` .
Structs are a bit limited in Prog8: 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.
Also, it is not possible to use a struct itself inside an array.
Structs are mainly syntactic sugar for repeated groups of vardecls
2019-07-15 21:05:04 +00:00
and assignments that belong together. However,
*they are layed out in sequence in memory as the members are defined*
which may be usefulif you want to pass pointers around.
2019-07-12 17:01:36 +00:00
To create a variable of a struct type you need to define the struct itself,
and then create a variable with it::
struct Color {
ubyte red
ubyte green
ubyte blue
}
2020-09-28 22:03:47 +00:00
Color rgb = [255,122,0] ; note that struct initializer value is same as an array
2019-07-12 17:01:36 +00:00
Color another ; the init value is optional, like arrays
another = rgb ; assign all of the values of rgb to another
another.blue = 255 ; set a single member
2018-08-08 23:54:43 +00:00
Special types: const and memory-mapped
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When using `` const `` , the value of the 'variable' can no longer be changed.
You'll have to specify the initial value expression. This value is then used
by the compiler everywhere you refer to the constant (and no storage is allocated
2018-10-07 00:05:26 +00:00
for the constant itself). This is only valid for the simple numeric types (byte, word, float).
2018-08-08 23:54:43 +00:00
2019-04-12 20:00:32 +00:00
When using `` & `` (the address-of operator but now applied to a datatype), the variable will point to specific location in memory,
2018-08-08 23:54:43 +00:00
rather than being newly allocated. The initial value (mandatory) must be a valid
memory address. Reading the variable will read the given data type from the
address you specified, and setting the varible will directly modify that memory location(s)::
const byte max_age = 2000 - 1974 ; max_age will be the constant value 26
2019-04-12 20:00:32 +00:00
&word SCREENCOLORS = $d020 ; a 16-bit word at the addres $d020-$d021
2018-08-08 23:54:43 +00:00
2018-12-30 20:40:27 +00:00
Converting types into other types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sometimes you need an unsigned word where you have an unsigned byte, or you need some other type conversion.
Many type conversions are possible by just writing `` as <type> `` at the end of an expression::
uword uw = $ea31
ubyte ub = uw as ubyte ; ub will be $31, identical to lsb(uw)
float f = uw as float ; f will be 59953, but this conversion can be omitted in this case
word w = uw as word ; w will be -5583 (simply reinterpret $ea31 as 2-complement negative number)
f = 56.777
ub = f as ubyte ; ub will be 56
Sometimes it is a straight 'type cast' where the value is simply interpreted as being of the other type,
sometimes an actual value conversion is done to convert it into the targe type.
Try to avoid type conversions as much as possible.
2018-08-06 23:23:34 +00:00
Initial values across multiple runs of the program
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2018-10-06 22:21:13 +00:00
When declaring values with an initial value, this value will be set into the variable each time
the program reaches the declaration again. This can be in loops, multiple subroutine calls,
2020-03-21 11:51:32 +00:00
or even multiple invocations of the entire program.
2020-03-22 14:12:26 +00:00
If you omit the initial value, zero will be used instead.
2019-01-08 01:25:22 +00:00
2019-07-15 21:05:04 +00:00
This only works for simple types, *and not for string variables and arrays* .
It is assumed these are left unchanged by the program; they are not re-initialized on
a second run.
If you do modify them in-place, you should take care yourself that they work as
expected when the program is restarted.
(This is an optimization choice to avoid having to store two copies of every string and array)
2018-08-06 23:23:34 +00:00
Loops
-----
2020-07-24 20:57:19 +00:00
The *for* -loop is used to let a variable iterate over a range of values. Iteration is done in steps of 1, but you can change this.
2019-08-18 01:16:23 +00:00
The loop variable must be declared as byte or word earlier so you can reuse it for multiple occasions.
2018-10-06 20:54:03 +00:00
Iterating with a floating point variable is not supported. If you want to loop over a floating-point array, use a loop with an integer index variable instead.
2018-09-18 21:14:32 +00:00
2018-08-06 23:23:34 +00:00
The *while* -loop is used to repeat a piece of code while a certain condition is still true.
2020-07-25 14:25:02 +00:00
The *do--until* loop is used to repeat a piece of code until a certain condition is true.
The *repeat* loop is used as a short notation of a for loop where the loop variable doesn't matter and you're only interested in the number of iterations.
(without iteration count specified it simply loops forever).
2020-03-14 17:11:04 +00:00
2018-08-14 12:33:36 +00:00
You can also create loops by using the `` goto `` statement, but this should usually be avoided.
2018-08-06 23:23:34 +00:00
2020-08-17 16:42:02 +00:00
Breaking out of a loop prematurely is possible with the `` break `` statement.
2018-09-19 23:13:21 +00:00
.. attention ::
2020-07-24 20:57:19 +00:00
The value of the loop variable after executing the loop *is undefined* . Don't use it immediately
2018-09-19 23:13:21 +00:00
after the loop without first assigning a new value to it!
(this is an optimization issue to avoid having to deal with mostly useless post-loop logic to adjust the loop variable's value)
2018-08-06 23:23:34 +00:00
2019-07-09 06:42:38 +00:00
Conditional Execution
---------------------
if statements
^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-08-06 23:23:34 +00:00
Conditional execution means that the flow of execution changes based on certiain conditions,
2018-08-14 00:22:59 +00:00
rather than having fixed gotos or subroutine calls::
2018-08-06 23:23:34 +00:00
2020-07-24 20:57:19 +00:00
if aa>4 goto overflow
2018-08-06 23:23:34 +00:00
2020-07-24 20:57:19 +00:00
if xx==3 yy = 4
if xx==3 yy = 4 else aa = 2
2018-08-14 00:22:59 +00:00
2020-07-24 20:57:19 +00:00
if xx==5 {
yy = 99
2018-08-14 00:22:59 +00:00
} else {
2020-07-24 20:57:19 +00:00
aa = 3
2018-08-14 00:22:59 +00:00
}
2018-08-06 23:23:34 +00:00
2018-09-23 00:04:45 +00:00
Conditional jumps (`` if condition goto label `` ) are compiled using 6502's branching instructions (such as `` bne `` and `` bcc `` ) so
2018-08-06 23:23:34 +00:00
the rather strict limit on how *far* it can jump applies. The compiler itself can't figure this
out unfortunately, so it is entirely possible to create code that cannot be assembled successfully.
2019-01-26 22:28:28 +00:00
Thankfully the `` 64tass `` assembler that is used has the option to automatically
convert such branches to their opposite + a normal jmp. This is slower and takes up more space
and you will get warning printed if this happens. You may then want to restructure your branches (place target labels closer to the branch,
or reduce code complexity).
2018-08-06 23:23:34 +00:00
2018-09-02 16:32:48 +00:00
There is a special form of the if-statement that immediately translates into one of the 6502's branching instructions.
This allows you to write a conditional jump or block execution directly acting on the current values of the CPU's status register bits.
2018-09-21 22:33:25 +00:00
The eight branching instructions of the CPU each have an if-equivalent (and there are some easier to understand aliases):
====================== =====================
condition meaning
====================== =====================
`` if_cs `` if carry status is set
`` if_cc `` if carry status is clear
`` if_vs `` if overflow status is set
`` if_vc `` if overflow status is clear
`` if_eq `` / `` if_z `` if result is equal to zero
`` if_ne `` / `` if_nz `` if result is not equal to zero
`` if_pl `` / `` if_pos `` if result is 'plus' (>= zero)
`` if_mi `` / `` if_neg `` if result is 'minus' (< zero)
====================== =====================
2018-09-02 16:32:48 +00:00
So `` if_cc goto target `` will directly translate into the single CPU instruction `` BCC target `` .
2020-12-22 02:35:00 +00:00
.. caution ::
These special `` if_XX `` branching statements are only useful in certain specific situations where you are *certain*
that the status register (still) contains the correct status bits.
This is not always the case after a fuction call or other operations!
If in doubt, check the generated assembly code!
2018-09-15 21:56:56 +00:00
.. note ::
For now, the symbols used or declared in the statement block(s) are shared with
the same scope the if statement itself is in.
Maybe in the future this will be a separate nested scope, but for now, that is
only possible when defining a subroutine.
2019-07-09 22:45:53 +00:00
when statement ('jump table')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Instead of writing a bunch of sequential if-elseif statements, it is more readable to
use a `` when `` statement. (It will also result in greatly improved assembly code generation)
Use a `` when `` statement if you have a set of fixed choices that each should result in a certain
action. It is possible to combine several choices to result in the same action::
when value {
4 -> c64scr.print("four")
5 -> c64scr.print("five")
10,20,30 -> {
c64scr.print("ten or twenty or thirty")
}
else -> c64scr.print("don't know")
}
The when-*value* can be any expression but the choice values have to evaluate to
compile-time constant integers (bytes or words). They also have to be the same
datatype as the when-value, otherwise no efficient comparison can be done.
2019-07-08 22:02:38 +00:00
2018-08-06 23:23:34 +00:00
Assignments
-----------
Assignment statements assign a single value to a target variable or memory location.
2020-07-24 20:57:19 +00:00
Augmented assignments (such as `` aa += xx `` ) are also available, but these are just shorthands
for normal assignments (`` aa = aa + xx `` ).
2018-08-06 23:23:34 +00:00
2020-07-24 20:57:19 +00:00
Only variables of type byte, word and float can be assigned a new value.
2018-09-27 21:21:04 +00:00
It's not possible to set a new value to string or array variables etc, because they get allocated
2020-07-24 20:57:19 +00:00
a fixed amount of memory which will not change. (You *can* change the value of elements in a string or array though).
2018-09-27 21:21:04 +00:00
2018-09-24 20:34:12 +00:00
.. attention ::
**Data type conversion (in assignments):**
2020-07-24 20:57:19 +00:00
When assigning a value with a 'smaller' datatype to variable with a 'larger' datatype,
2018-09-24 20:34:12 +00:00
the value will be automatically converted to the target datatype: byte --> word --> float.
So assigning a byte to a word variable, or a word to a floating point variable, is fine.
The reverse is *not* true: it is *not* possible to assign a value of a 'larger' datatype to
a variable of a smaller datatype without an explicit conversion. Otherwise you'll get an error telling you
that there is a loss of precision. You can use builtin functions such as `` round `` and `` lsb `` to convert
2018-09-25 17:52:59 +00:00
to a smaller datatype, or revert to integer arithmetic.
2018-08-06 23:23:34 +00:00
2018-12-30 20:40:27 +00:00
Direct access to memory locations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Normally memory locations are accessed by a *memory mapped* name, such as `` c64.BGCOL0 `` that is defined
as the memory mapped address $d021.
If you want to access a memory location directly (by using the address itself), without defining
2018-12-31 03:48:26 +00:00
a memory mapped location, you can do so by enclosing the address in `` @(...) `` ::
2018-12-30 20:40:27 +00:00
2020-07-24 20:57:19 +00:00
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 ; you can also use expressions to 'calculate' the address
2018-12-30 20:40:27 +00:00
2018-08-06 23:23:34 +00:00
Expressions
-----------
2019-01-26 16:32:26 +00:00
Expressions tell the program to *calculate* something. They consist of
values, variables, operators such as `` + `` and `` - `` , function calls, type casts, or other expressions.
Here is an example that calculates to number of seconds in a certain time period::
num_hours * 3600 + num_minutes * 60 + num_seconds
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-09-03 21:19:25 +00:00
In most places where a number or other value is expected, you can use just the number, or a constant expression.
2018-09-24 20:34:12 +00:00
If possible, the expression is parsed and evaluated by the compiler itself at compile time, and the (constant) resulting value is used in its place.
Expressions that cannot be compile-time evaluated will result in code that calculates them at runtime.
2018-09-03 21:19:25 +00:00
Expressions can contain procedure and function calls.
There are various built-in functions such as sin(), cos(), min(), max() that can be used in expressions (see :ref: `builtinfunctions` ).
You can also reference idendifiers defined elsewhere in your code.
2018-09-24 20:34:12 +00:00
.. attention ::
2019-02-25 00:08:10 +00:00
**Floating points used in expressions:**
2018-09-25 17:52:59 +00:00
When a floating point value is used in a calculation, the result will be a floating point, and byte or word values
will be automatically converted into floats in this case. The compiler will issue a warning though when this happens, because floating
point calculations are very slow and possibly unintended!
2018-12-20 22:28:03 +00:00
Calculations with integer variables will not result in floating point values.
if you divide two integer variables say 32500 and 99 the result will be the integer floor
2018-09-24 20:34:12 +00:00
division (328) rather than the floating point result (328.2828282828283). If you need the full precision,
2018-12-20 22:28:03 +00:00
you'll have to make sure at least the first operand is a floating point. You can do this by
using a floating point value or variable, or use a type cast.
When the compiler can calculate the result during compile-time, it will try to avoid loss
of precision though and gives an error if you may be losing a floating point result.
2018-12-20 01:52:33 +00:00
2018-08-06 23:23:34 +00:00
2018-08-07 20:49:01 +00:00
Arithmetic and Logical expressions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Arithmetic expressions are expressions that calculate a numeric result (integer or floating point).
Many common arithmetic operators can be used and follow the regular precedence rules.
2018-09-24 20:34:12 +00:00
Logical expressions are expressions that calculate a boolean result: true or false
(which in reality are just a 1 or 0 integer value).
2018-08-06 23:23:34 +00:00
2018-08-07 20:49:01 +00:00
You can use parentheses to group parts of an expresion to change the precedence.
Usually the normal precedence rules apply (`` * `` goes before `` + `` etc.) but subexpressions
within parentheses will be evaluated first. So `` (4 + 8) * 2 `` is 24 and not 20,
and `` (true or false) and false `` is false instead of true.
2018-08-06 23:23:34 +00:00
2019-02-25 00:08:10 +00:00
.. attention ::
2020-09-19 21:23:32 +00:00
**calculations keep their datatype even if the target variable is larger:**
2019-02-25 00:08:10 +00:00
When you do calculations on a BYTE type, the result will remain a BYTE.
When you do calculations on a WORD type, the result will remain a WORD.
For instance::
byte b = 44
word w = b*55 ; the result will be 116! (even though the target variable is a word)
2020-09-19 21:23:32 +00:00
w *= 999 ; the result will be -15188 (the multiplication stays within a word, but overflows)
2019-02-25 00:08:10 +00:00
2020-09-19 21:23:32 +00:00
*The compiler does NOT warn about this!* It's doing this for
2019-02-25 00:08:10 +00:00
performance reasons - so you won't get sudden 16 bit (or even float)
calculations where you needed only simple fast byte arithmetic.
If you do need the extended resulting value, cast at least one of the
2020-09-19 21:23:32 +00:00
operands explicitly to the larger datatype. For example::
2019-02-25 00:08:10 +00:00
byte b = 44
2020-09-19 21:23:32 +00:00
w = (b as word)*55
w = b*(55 as word)
2019-02-25 00:08:10 +00:00
2018-08-06 23:23:34 +00:00
Subroutines
-----------
Defining a subroutine
^^^^^^^^^^^^^^^^^^^^^
Subroutines are parts of the code that can be repeatedly invoked using a subroutine call from elsewhere.
2018-10-08 20:35:39 +00:00
Their definition, using the `` sub `` statement, includes the specification of the required parameters and return value.
2018-09-15 00:12:37 +00:00
Subroutines can be defined in a Block, but also nested inside another subroutine. Everything is scoped accordingly.
2018-08-06 23:23:34 +00:00
Calling a subroutine
^^^^^^^^^^^^^^^^^^^^
2018-08-06 01:35:43 +00:00
2018-09-30 16:49:58 +00:00
The arguments in parentheses after the function name, should match the parameters in the subroutine definition.
2020-02-09 00:21:23 +00:00
If you want to ignore a return value of a subroutine, you should prefix the call with the `` void `` keyword.
Otherwise the compiler will issue a warning about discarding a result value.
2018-08-13 23:15:11 +00:00
2018-09-03 21:19:25 +00:00
.. caution ::
2019-01-31 22:46:58 +00:00
Note that due to the way parameters are processed by the compiler,
subroutines are *non-reentrant* . This means you cannot create recursive calls.
2018-09-03 21:19:25 +00:00
If you do need a recursive algorithm, you'll have to hand code it in embedded assembly for now,
or rewrite it into an iterative algorithm.
2019-03-06 21:11:16 +00:00
Also, subroutines used in the main program should not be used from an IRQ handler. This is because
the subroutine may be interrupted, and will then call itself from the IRQ handler. Results are
then undefined because the variables will get overwritten.
2018-09-03 21:19:25 +00:00
.. _builtinfunctions:
2018-08-13 23:15:11 +00:00
Built-in Functions
------------------
2018-09-02 09:54:42 +00:00
There's a set of predefined functions in the language. These are fixed and can't be redefined in user code.
You can use them in expressions and the compiler will evaluate them at compile-time if possible.
2020-10-17 00:14:19 +00:00
Math
^^^^
2019-01-04 21:11:46 +00:00
2020-10-17 00:14:19 +00:00
abs(x)
Absolute value.
2019-01-06 21:22:12 +00:00
2020-10-17 00:14:19 +00:00
atan(x)
Arctangent.
2019-01-06 21:22:12 +00:00
2020-10-17 00:14:19 +00:00
ceil(x)
Rounds the floating point up to an integer towards positive infinity.
2019-01-04 21:11:46 +00:00
2020-10-17 00:14:19 +00:00
cos(x)
Cosine. (floating point version)
2019-01-06 21:22:12 +00:00
cos8u(x)
Fast 8-bit ubyte cosine of angle 0..255, result is in range 0..255
2019-01-04 21:11:46 +00:00
cos8(x)
2019-01-06 21:22:12 +00:00
Fast 8-bit byte cosine of angle 0..255, result is in range -127..127
cos16u(x)
Fast 16-bit uword cosine of angle 0..255, result is in range 0..65535
2019-01-04 21:11:46 +00:00
cos16(x)
2019-08-17 23:39:48 +00:00
Fast 16-bit word cosine of angle 0..255, result is in range -32767..32767
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
deg(x)
Radians to degrees.
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
floor (x)
Rounds the floating point down to an integer towards minus infinity.
2018-08-13 23:15:11 +00:00
2018-09-17 22:31:11 +00:00
ln(x)
2019-08-17 23:39:48 +00:00
Natural logarithm (base e).
2018-09-17 22:31:11 +00:00
log2(x)
Base 2 logarithm.
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
rad(x)
Degrees to radians.
round(x)
Rounds the floating point to the closest integer.
sin(x)
Sine. (floating point version)
sgn(x)
Get the sign of the value. Result is -1, 0 or 1 (negative, zero, positive).
sin8u(x)
Fast 8-bit ubyte sine of angle 0..255, result is in range 0..255
sin8(x)
Fast 8-bit byte sine of angle 0..255, result is in range -127..127
sin16u(x)
Fast 16-bit uword sine of angle 0..255, result is in range 0..65535
sin16(x)
Fast 16-bit word sine of angle 0..255, result is in range -32767..32767
2019-03-16 18:25:47 +00:00
sqrt16(w)
2019-08-17 23:39:48 +00:00
16 bit unsigned integer Square root. Result is unsigned byte.
2019-03-16 18:25:47 +00:00
2018-09-02 09:54:42 +00:00
sqrt(x)
2019-08-17 23:39:48 +00:00
Floating point Square root.
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
tan(x)
Tangent.
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
Array operations
^^^^^^^^^^^^^^^^
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
any(x)
1 ('true') if any of the values in the array value x is 'true' (not zero), else 0 ('false')
2018-08-13 23:15:11 +00:00
2020-10-17 00:14:19 +00:00
all(x)
1 ('true') if all of the values in the array value x are 'true' (not zero), else 0 ('false')
len(x)
Number of values in the array value x, or the number of characters in a string (excluding the size or 0-byte).
Note: this can be different from the number of *bytes* in memory if the datatype isn't a byte. See sizeof().
Note: lengths of strings and arrays are determined at compile-time! If your program modifies the actual
length of the string during execution, the value of len(string) may no longer be correct!
(use strlen function if you want to dynamically determine the length)
2018-09-02 09:54:42 +00:00
2018-09-04 21:37:21 +00:00
max(x)
2019-08-17 23:39:48 +00:00
Maximum of the values in the array value x
2018-09-04 21:37:21 +00:00
min(x)
2019-08-17 23:39:48 +00:00
Minimum of the values in the array value x
2018-09-04 21:37:21 +00:00
2020-10-17 00:14:19 +00:00
reverse(array)
Reverse the values in the array (in-place).
Can be used after sort() to sort an array in descending order.
2018-09-04 21:37:21 +00:00
sum(x)
2019-08-17 23:39:48 +00:00
Sum of the values in the array value x
sort(array)
Sort the array in ascending order (in-place)
2020-10-16 16:11:25 +00:00
Supported are arrays of bytes or word values.
Sorting a floating-point array is not supported right now, as a general sorting routine for this will
be extremely slow. Either build one yourself or find another solution that doesn't require sorting.
Finally, note that sorting an array with strings in it will not do what you might think;
it considers the array as just an array of integer words and sorts the string *pointers* accordingly.
Sorting strings alphabetically has to be programmed yourself if you need it.
2019-08-17 23:39:48 +00:00
2018-09-04 21:37:21 +00:00
2020-10-17 00:14:19 +00:00
Strings and memory blocks
^^^^^^^^^^^^^^^^^^^^^^^^^
memcopy(from, to, numbytes)
2020-11-01 06:36:40 +00:00
Efficiently copy a number of bytes from a memory location to another.
2020-10-17 00:14:19 +00:00
NOTE: 'to' must NOT overlap with 'from', unless it is *before* 'from'.
Because this function imposes some overhead to handle the parameters,
it is only faster if the number of bytes is larger than a certain threshold.
Compare the generated code to see if it was beneficial or not.
2020-11-01 06:36:40 +00:00
The most efficient will often be to write a specialized copy routine in assembly yourself!
2019-03-15 22:10:26 +00:00
2020-10-17 00:14:19 +00:00
memset(address, numbytes, bytevalue)
Efficiently set a part of memory to the given (u)byte value.
But the most efficient will always be to write a specialized fill routine in assembly yourself!
2020-10-22 21:41:16 +00:00
Note that for clearing the screen, very fast specialized subroutines are
available in the `` textio `` and `` graphics `` library modules.
2020-10-17 00:14:19 +00:00
memsetw(address, numwords, wordvalue)
Efficiently set a part of memory to the given (u)word value.
But the most efficient will always be to write a specialized fill routine in assembly yourself!
leftstr(source, target, length)
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.
2020-11-05 21:27:50 +00:00
Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
2020-10-17 00:14:19 +00:00
Modifies in-place, doesn't return a value (so can't be used in an expression).
rightstr(source, target, length)
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.
2020-11-05 21:27:50 +00:00
Also, you have to make sure yourself that length is smaller or equal to the length of the source string.
2020-10-17 00:14:19 +00:00
Modifies in-place, doesn't return a value (so can't be used in an expression).
2020-08-20 11:50:28 +00:00
2019-03-15 22:10:26 +00:00
strlen(str)
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.
2020-10-17 00:14:19 +00:00
Don't confuse this with `` len `` and `` sizeof ``
strcmp(string1, string2)
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 strcmp for you under water automatically).
substr(source, target, start, length)
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.
2020-11-05 21:27:50 +00:00
Also, you have to make sure yourself that start and length are within bounds of the strings.
2020-10-17 00:14:19 +00:00
Modifies in-place, doesn't return a value (so can't be used in an expression).
2020-12-14 16:20:16 +00:00
strcopy(from, to)
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.
2020-10-17 00:14:19 +00:00
Miscellaneous
^^^^^^^^^^^^^
exit(returncode)
Immediately stops the program and exits it, with the returncode in the A register.
Note: custom interrupt handlers remain active unless manually cleared first!
2018-09-04 21:37:21 +00:00
2018-09-06 19:13:49 +00:00
lsb(x)
2018-12-20 01:52:33 +00:00
Get the least significant byte of the word x. Equivalent to the cast "x as ubyte".
2018-09-06 19:13:49 +00:00
msb(x)
Get the most significant byte of the word x.
2020-08-22 19:08:29 +00:00
mkword(msb, lsb)
Efficiently create a word value from two bytes (the msb and the lsb). Avoids multiplication and shifting.
So mkword($80, $22) results in $8022.
2019-01-09 00:03:33 +00:00
2020-12-15 21:25:06 +00:00
.. note ::
The arguments to the mkword() function are in 'natural' order that is first the msb then the lsb.
Don't get confused by how the system actually stores this 16-bit word value in memory (which is
in little-endian format, so lsb first then msb)
2018-09-15 16:43:23 +00:00
rnd()
returns a pseudo-random byte from 0..255
rndw()
returns a pseudo-random word from 0..65535
rndf()
returns a pseudo-random float between 0.0 and 1.0
2018-09-04 21:37:21 +00:00
rol(x)
2018-09-02 09:54:42 +00:00
Rotate the bits in x (byte or word) one position to the left.
This uses the CPU's rotate semantics: bit 0 will be set to the current value of the Carry flag,
while the highest bit will become the new Carry flag value.
(essentially, it is a 9-bit or 17-bit rotation)
Modifies in-place, doesn't return a value (so can't be used in an expression).
2018-09-04 21:37:21 +00:00
rol2(x)
2019-08-21 22:34:17 +00:00
Like `` rol `` but now as 8-bit or 16-bit rotation.
2018-09-02 09:54:42 +00:00
It uses some extra logic to not consider the carry flag as extra rotation bit.
Modifies in-place, doesn't return a value (so can't be used in an expression).
2018-09-04 21:37:21 +00:00
ror(x)
2018-09-02 09:54:42 +00:00
Rotate the bits in x (byte or word) one position to the right.
This uses the CPU's rotate semantics: the highest bit will be set to the current value of the Carry flag,
while bit 0 will become the new Carry flag value.
(essentially, it is a 9-bit or 17-bit rotation)
Modifies in-place, doesn't return a value (so can't be used in an expression).
2018-09-04 21:37:21 +00:00
ror2(x)
2019-08-21 22:34:17 +00:00
Like `` ror `` but now as 8-bit or 16-bit rotation.
2018-09-02 09:54:42 +00:00
It uses some extra logic to not consider the carry flag as extra rotation bit.
Modifies in-place, doesn't return a value (so can't be used in an expression).
2018-10-14 20:28:16 +00:00
rsave()
Saves the CPU registers and the status flags.
You can now more or less 'safely' use the registers directly, until you
restore them again so the generated code can carry on normally.
2018-12-09 18:48:59 +00:00
Note: it's not needed to rsave() before an asm subroutine that clobbers the X register
(which is used as the internal evaluation stack pointer).
The compiler will take care of this situation automatically.
2020-12-22 12:29:16 +00:00
Note: the 16 bit 'virtual' registers of the Commander X16 are not saved.
2018-10-14 20:28:16 +00:00
rrestore()
Restores the CPU registers and the status flags from previously saved values.
2020-12-22 12:29:16 +00:00
Note: the 16 bit 'virtual' registers of the Commander X16 are not restored.
2019-03-05 22:10:00 +00:00
read_flags()
Returns the current value of the CPU status register.
2019-03-06 21:11:16 +00:00
2020-10-17 00:14:19 +00:00
sizeof(name)
Number of bytes that the object 'name' occupies in memory. This is a constant determined by the data type of
the object. For instance, for a variable of type uword, the sizeof is 2.
For an 10 element array of floats, it is 50 (on the C-64, where a float is 5 bytes).
Note: usually you will be interested in the number of elements in an array, use len() for that.
set_carry() / clear_carry()
Set (or clear) the CPU status register Carry flag. No result value.
(translated into `` SEC `` or `` CLC `` cpu instruction)
2020-03-13 01:05:15 +00:00
2020-10-17 00:14:19 +00:00
set_irqd() / clear_irqd()
Set (or clear) the CPU status register Interrupt Disable flag. No result value.
(translated into `` SEI `` or `` CLI `` cpu instruction)
swap(x, y)
Swap the values of numerical variables (or memory locations) x and y in a fast way.
2019-03-06 21:11:16 +00:00
2020-12-22 05:13:14 +00:00
target()
Returns byte value designating the target machine that the program was compiled for.
2020-12-22 12:29:16 +00:00
Notice that this is a compile-time constant value and is not determined on the
system when the program is running.
2020-12-22 05:13:14 +00:00
The following return values are currently defined:
- 16 = compiled for CommanderX16 with 65C02 CPU
- 64 = compiled for Commodore-64 with 6502/6510 CPU
2020-12-10 22:30:58 +00:00
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.
2019-03-06 21:11:16 +00:00
Library routines
----------------
There are many routines available in the compiler libraries.
Some are used internally by the compiler as well.
There's too many to list here, just have a look through the source code
of the library modules to see what's there.
(They can be found in the compiler/res directory)
The example programs also use a small set of the library routines, you can study
their source code to see how they might be used.