2023-06-25 19:35:30 +00:00
===============
Library modules
===============
2020-10-15 19:30:03 +00:00
The compiler provides several "built-in" library modules with useful subroutine and variables.
Some of these may be specific for a certain compilation target, or work slightly different,
but some effort is put into making them available across compilation targets.
This means that as long as your program is only using the subroutines from these
libraries and not using hardware- and/or system dependent code, and isn't hardcoding certain
assumptions like the screen size, the exact same source program can
be compiled for multiple different target platforms. Many of the example programs that come
with Prog8 are written like this.
You can `` %import `` and use these modules explicitly, but the compiler may also import one or more
of these library modules automatically as required.
2021-12-30 22:57:54 +00:00
.. note ::
For full details on what is available in the libraries, please study their source code here:
https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib
2020-12-21 17:28:10 +00:00
2020-10-17 18:35:36 +00:00
.. caution ::
2020-10-15 19:30:03 +00:00
The resulting compiled binary program *only works on the target machine it was compiled for* .
You must recompile the program for every target you want to run it on.
2024-04-12 19:56:25 +00:00
.. note ::
Several algorithms and math routines in Prog8's assembly library files are adapted from
code publicly available on https://www.codebase64.org/
2020-10-15 19:30:03 +00:00
2024-04-10 17:58:15 +00:00
Low-fi variable and subroutine definitions in all available library modules
---------------------------------------------------------------------------
These are auto generated and contain no documentation, but provide a view into what's available.
Grouped per compilation target.
* `atari <_static/symboldumps/skeletons-atari.txt> `_
* `c64 <_static/symboldumps/skeletons-c64.txt> `_
* `c128 <_static/symboldumps/skeletons-c128.txt> `_
* `cx16 <_static/symboldumps/skeletons-cx16.txt> `_
* `pet32 <_static/symboldumps/skeletons-pet32.txt> `_
* `virtual <_static/symboldumps/skeletons-virtual.txt> `_
2020-10-15 19:30:03 +00:00
syslib
------
The "system library" for your target machine. It contains many system-specific definitions such
2022-10-29 12:10:11 +00:00
as ROM/Kernal subroutine definitions, memory location constants, and utility subroutines.
2020-10-15 19:30:03 +00:00
2022-04-02 17:10:18 +00:00
2020-10-15 19:30:03 +00:00
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.
2023-08-12 21:24:41 +00:00
This module is usually imported automatically and can provide definitions in the `` sys `` , `` cbm `` , `` c64 `` , `` cx16 `` , `` c128 `` , `` atari `` blocks
2024-02-21 20:29:17 +00:00
depending on the chosen compilation target. Read the `sys lib source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib> `_ for the correct compilation target to see exactly what is there.
2023-01-25 23:37:30 +00:00
2021-01-08 00:05:26 +00:00
sys (part of syslib)
--------------------
`` target ``
2021-01-07 22:36:28 +00:00
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:
2023-06-03 17:14:45 +00:00
- 8 = Atari 8 bits
- 16 = Commander X16
- 64 = Commodore 64
- 128 = Commodore 128
- 255 = Virtual machine
2021-01-07 22:36:28 +00:00
2023-04-29 20:11:26 +00:00
`` exit (returncode) ``
2021-01-08 15:20:56 +00:00
Immediately stops the program and exits it, with the returncode in the A register.
Note: custom interrupt handlers remain active unless manually cleared first!
2024-03-08 22:35:15 +00:00
`` exit2 (resultA, resultX, resultY) ``
Immediately stops the program and exits it, with the result values in the A, X and Y registers.
Note: custom interrupt handlers remain active unless manually cleared first!
`` exit3 (resultA, resultX, resultY, carry) ``
Immediately stops the program and exits it, with the result values in the A, X and Y registers, and the carry flag in the status register.
Note: custom interrupt handlers remain active unless manually cleared first!
2023-04-29 20:11:26 +00:00
`` memcopy (from, to, numbytes) ``
2021-01-08 00:05:26 +00:00
Efficiently copy a number of bytes from a memory location to another.
2022-03-02 22:31:07 +00:00
*Warning:* can only copy *non-overlapping* memory areas correctly!
2021-01-08 00:05:26 +00:00
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.
The most efficient will often be to write a specialized copy routine in assembly yourself!
2023-04-29 20:11:26 +00:00
`` memset (address, numbytes, bytevalue) ``
2021-01-08 00:05:26 +00:00
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!
Note that for clearing the screen, very fast specialized subroutines are
available in the `` textio `` and `` graphics `` library modules.
2023-04-29 20:11:26 +00:00
`` memsetw (address, numwords, wordvalue) ``
2021-01-08 00:05:26 +00:00
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!
2021-01-07 22:36:28 +00:00
2023-04-29 20:11:26 +00:00
`` read_flags () -> ubyte ``
2021-01-08 15:20:56 +00:00
Returns the current value of the CPU status register.
2023-04-29 20:11:26 +00:00
`` set_carry () ``
2021-01-08 15:20:56 +00:00
Sets the CPU status register Carry flag.
2023-04-29 20:11:26 +00:00
`` clear_carry () ``
2021-01-08 15:20:56 +00:00
Clears the CPU status register Carry flag.
2023-04-29 20:11:26 +00:00
`` set_irqd () ``
2021-01-08 15:20:56 +00:00
Sets the CPU status register Interrupt Disable flag.
2023-04-29 20:11:26 +00:00
`` clear_irqd () ``
2021-01-08 15:20:56 +00:00
Clears the CPU status register Interrupt Disable flag.
2023-05-22 19:13:20 +00:00
`` irqsafe_set_irqd () ``
Sets the CPU status register Interrupt Disable flag, in a way that is safe to be used inside a IRQ handler.
Pair with `` irqsafe_clear_irqd() `` .
`` irqsafe_clear_irqd () ``
Clears the CPU status register Interrupt Disable flag, in a way that is safe to be used inside a IRQ handler.
Pair with `` irqsafe_set_irqd() `` . Inside an IRQ handler this makes sure it doesn't inadvertently
clear the irqd status bit, and it can still be used inside normal code as well (where it *does* clear
the irqd status bit if it was cleared before entering).
2023-04-29 20:11:26 +00:00
`` progend () ``
2024-01-22 17:57:25 +00:00
Returns the last address of the program in memory + 1. This means: the memory address directly after all the program code and variables,
including the uninitialized ones ("BSS" variables) and the uninitialized memory blocks reserved by the `memory()` function.
2021-01-08 15:20:56 +00:00
Can be used to load dynamic data after the program, instead of hardcoding something.
2023-04-29 20:11:26 +00:00
`` wait (uword jiffies) ``
2021-04-01 16:31:33 +00:00
wait approximately the given number of jiffies (1/60th seconds)
2023-01-20 02:10:41 +00:00
Note: the regular system irq handler has run for this to work as it depends on the system jiffy clock.
If this is is not possible (for instance because your program is running its own irq handler logic *and* no longer calls
the kernal's handler routine), you'll have to write your own wait routine instead.
2021-04-01 16:31:33 +00:00
2023-04-29 20:11:26 +00:00
`` waitvsync () ``
2021-04-01 16:31:33 +00:00
busy wait till the next vsync has occurred (approximately), without depending on custom irq handling.
2021-04-01 16:53:16 +00:00
can be used to avoid screen flicker/tearing when updating screen contents.
2021-04-01 16:31:33 +00:00
note: a more accurate way to wait for vsync is to set up a vsync irq handler instead.
2023-01-20 02:10:41 +00:00
note for cx16: the regular system irq handler has to run for this to work (this is not required on C64 and C128)
2021-04-01 16:31:33 +00:00
2023-04-29 20:11:26 +00:00
`` waitrastborder () `` (c64/c128 targets only)
2021-04-01 16:53:16 +00:00
busy wait till the raster position has reached the bottom screen border (approximately)
can be used to avoid screen flicker/tearing when updating screen contents.
note: a more accurate way to do this is by using a raster irq handler instead.
2023-04-29 20:11:26 +00:00
`` reset_system () ``
2022-10-29 12:12:10 +00:00
Soft-reset the system back to initial power-on BASIC prompt.
2021-04-01 16:31:33 +00:00
(called automatically by Prog8 when the main subroutine returns and the program is not using basicsafe zeropage option)
2023-11-06 22:55:49 +00:00
`` disable_caseswitch() `` and `` enable_caseswitch() ``
Disable or enable the ability to switch character set case using a keyboard combination.
2023-11-21 20:31:50 +00:00
`` save_prog8_internals() `` and `` restore_prog8_internals() ``
Normally not used in user code, the compiler utilizes these for the internal interrupt logic.
It stores and restores the values of the internal prog8 variables.
This allows other code to run that might clobber these values temporarily.
2023-12-26 13:47:31 +00:00
`` push (value) ``
pushes a byte value on the CPU hardware stack. Low-level function that should normally not be used.
`` pushw (value) ``
pushes a 16-bit word value on the CPU hardware stack. Low-level function that should normally not be used.
`` pop () ``
pops a byte value off the CPU hardware stack and returns it.
Low-level function that should normally not be used.
`` popw () ``
pops a 16-bit word value off the CPU hardware stack and returns it.
Low-level function that should normally not be used.
2021-01-07 22:36:28 +00:00
2020-10-15 19:30:03 +00:00
conv
----
Routines to convert strings to numbers or vice versa.
- numbers to strings, in various formats (binary, hex, decimal)
2021-03-06 12:18:04 +00:00
- strings in decimal, hex and binary format into numbers (bytes, words)
2020-10-15 21:36:03 +00:00
2024-02-21 20:29:17 +00:00
Read the `conv source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/conv.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there.
2020-10-15 19:30:03 +00:00
textio (txt.*)
--------------
This will probably be the most used library module. It contains a whole lot of routines
dealing with text-based input and output (to the screen). Such as
2024-02-21 16:04:39 +00:00
- printing strings, numbers and booleans
2020-10-15 19:30:03 +00:00
- reading text input from the user via the keyboard
- filling or clearing the screen and colors
- scrolling the text on the screen
- placing individual characters on the screen
2024-02-07 21:36:43 +00:00
- convert petscii to screencode characters
2020-10-15 19:30:03 +00:00
2023-11-06 22:18:24 +00:00
All routines work with Screencode character encoding, except `print` , `chrout` and `input_chars` ,
these work with PETSCII encoding instead.
2024-02-21 20:29:17 +00:00
Read the `textio source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/textio.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there. (Note: slight variations for different compiler targets)
2020-10-15 19:30:03 +00:00
diskio
------
Provides several routines that deal with disk drive I/O, such as:
2020-12-30 22:34:00 +00:00
- list files on disk, optionally filtering by a simple pattern with ? and *
2020-12-08 00:34:08 +00:00
- show disk directory as-is
2020-10-15 19:30:03 +00:00
- display disk drive status
- load and save data from and to the disk
- delete and rename files on the disk
2022-08-01 20:58:03 +00:00
- send arbitrary CbmDos command to disk drive
2020-10-15 19:30:03 +00:00
2023-05-02 01:11:09 +00:00
Commander X16 additions:
2023-05-19 20:06:49 +00:00
Headerless load and save routines are available (load_raw, save_raw).
2022-10-29 12:10:11 +00:00
On the Commander X16 it tries to use that machine's fast Kernal loading routines if possible.
2023-05-02 01:11:09 +00:00
Routines to directly load data into video ram are also present (vload and vload_raw).
Also contains a helper function to calculate the file size of a loaded file (although that is truncated
to 16 bits, 64Kb)
2023-11-11 13:26:40 +00:00
Als contains routines for operating on subdirectories (chdir, mkdir, rmdir), to relabel the disk,
and to seek in open files.
2022-06-01 22:37:18 +00:00
2024-02-21 20:29:17 +00:00
Read the `diskio source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/diskio.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there. (Note: slight variations for different compiler targets)
2023-10-21 20:25:38 +00:00
.. note ::
If you are using the X16 emulator with HostFS, and are experiencing weird behavior with these
routines, please first try again with an SD-card image instead of HostFs.
It is possible that there are still small differences between HostFS and actual CBM DOS in the X16 emulator.
2023-11-19 17:59:03 +00:00
.. note ::
You can set the active disk drive number so it supports multiple drives,
but it does not support multiple open files at the same time.
2023-11-12 19:40:17 +00:00
.. attention ::
Error handling is peculiar on CBM dos systems (C64, C128, cx16, PET). Read the
descriptions for the various methods in this library for details and tips.
2020-10-15 19:30:03 +00:00
2021-01-07 19:01:11 +00:00
string
------
Provides string manipulation routines.
2023-04-29 20:11:26 +00:00
`` length (str) -> ubyte length ``
2021-01-07 19:01:11 +00:00
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.
2022-10-28 21:39:15 +00:00
Don't confuse this with `` len `` and `` sizeof `` !
2021-01-07 19:01:11 +00:00
2023-04-29 20:11:26 +00:00
`` left (source, length, target) ``
2021-01-07 19:01:11 +00:00
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).
2023-04-29 20:11:26 +00:00
`` right (source, length, target) ``
2021-01-07 19:01:11 +00:00
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).
2023-04-29 20:11:26 +00:00
`` slice (source, start, length, target) ``
2021-01-07 19:01:11 +00:00
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).
2023-04-29 20:11:26 +00:00
`` find (string, char) -> ubyte index + carry bit ``
2022-01-24 20:37:04 +00:00
Locates the first position of the given character in the string, returns carry bit set if found
2022-07-31 09:58:27 +00:00
and the index in the string. Or 0+carry bit clear if the character was not found.
2023-12-11 19:50:56 +00:00
You can consider this a safer way of checking if a character occurs
in a string than using an `in` containment check - because the find routine
properly stops at the first 0-byte string terminator it encounters.
Simply call this and only act on the carry status with `` if_cc `` for example.
Much like the difference between len(str) and length(str).
2021-01-07 19:01:11 +00:00
2024-01-05 19:46:26 +00:00
`` contains (string, char) -> bool ``
Just returns true if the character is in the given string, or false if it's not.
For string literals, you can use a containment check expression instead: `` char in "hello world" `` .
2023-04-29 20:11:26 +00:00
`` compare (string1, string2) -> ubyte result ``
2022-10-28 21:39:15 +00:00
Returns -1, 0 or 1 depending on whether string1 sorts before, equal or after string2.
Note that you can also directly compare strings and string values with each other
2021-01-07 19:01:11 +00:00
using `` == `` , `` < `` etcetera (it will use string.compare for you under water automatically).
2023-06-21 22:20:30 +00:00
This even works when dealing with uword (pointer) variables when comparing them to a string type.
2021-01-07 19:01:11 +00:00
2023-04-29 20:11:26 +00:00
`` copy (from, to) -> ubyte length ``
2021-01-07 19:01:11 +00:00
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.
2023-11-10 21:27:07 +00:00
`` append (string, suffix) -> ubyte length ``
Appends the suffix string to the other string (make sure the memory buffer is large enough!)
Returns the length of the combined string.
2023-04-29 20:11:26 +00:00
`` lower (string) ``
2022-10-29 12:07:04 +00:00
Lowercases the PETSCII-string in place.
2021-01-10 14:22:21 +00:00
2023-04-29 20:11:26 +00:00
`` upper (string) ``
2022-10-29 12:07:04 +00:00
Uppercases the PETSCII-string in place.
2021-01-10 14:22:21 +00:00
2023-04-29 20:11:26 +00:00
`` lowerchar (char) ``
2023-12-05 22:52:43 +00:00
Returns lowercased PETSCII character.
2022-09-25 18:20:38 +00:00
2023-04-29 20:11:26 +00:00
`` upperchar (char) ``
2023-12-05 22:52:43 +00:00
Returns uppercased PETSCII character.
2022-09-25 18:20:38 +00:00
2024-02-07 01:09:08 +00:00
`` strip (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace and other non-visible characters at the edges of the string. (destructive)
2024-02-07 01:09:08 +00:00
`` rstrip (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace and other non-visible characters at the end of the string. (destructive)
2024-02-07 01:09:08 +00:00
`` lstrip (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace and other non-visible characters at the start of the string. (destructive)
`` lstripped (string) -> str ``
Returns pointer to first non-whitespace and non-visible character at the start of the string (non-destructive lstrip)
2024-02-07 01:09:08 +00:00
`` trim (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace characters at the edges of the string. (destructive)
2024-02-07 01:09:08 +00:00
`` rtrim (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace characters at the end of the string. (destructive)
2024-02-07 01:09:08 +00:00
`` ltrim (string) ``
2024-02-07 22:06:01 +00:00
Gets rid of whitespace characters at the start of the string. (destructive)
`` ltrimmed (string) -> str ``
Returns pointer to first non-whitespace character at the start of the string (non-destructive ltrim)
2024-02-07 01:09:08 +00:00
2023-12-05 22:52:43 +00:00
`` isdigit (char) ``
Returns boolean if the character is a numerical digit 0-9
`` islower (char) `` , `` isupper (char) `` , `` isletter (char) ``
2023-12-12 23:28:34 +00:00
Returns true if the character is a shifted-PETSCII lowercase letter, uppercase letter, or any letter, respectively.
`` isspace (char) ``
Returns true if the PETSCII character is a whitespace (tab, space, return, and shifted versions)
`` isprint (char) ``
Returns true if the PETSCII character is a "printable" character (space or any visible symbol)
2023-12-05 21:50:20 +00:00
2023-04-29 20:11:26 +00:00
`` startswith (string, prefix) -> bool ``
2022-07-20 22:38:30 +00:00
Returns true if string starts with prefix, otherwise false
2023-04-29 20:11:26 +00:00
`` endswith (string, suffix) -> bool ``
2022-07-20 22:38:30 +00:00
Returns true if string ends with suffix, otherwise false
2024-02-04 22:18:11 +00:00
`` pattern_match (string, pattern) -> bool `` (not on Virtual target)
Returns true if the string matches the pattern, false if not.
2022-07-20 22:38:30 +00:00
'?' in the pattern matches any one character. '*' in the pattern matches any substring.
2023-11-26 21:07:05 +00:00
`` hash (string) -> ubyte ``
Returns a simple 8 bit hash value for the given string.
2023-11-27 00:09:42 +00:00
The formula is: hash(-1)=179; clear carry; hash(i) = ROL hash(i-1) XOR string[i]
(where ROL is the cpu ROL instruction)
On the English word list in /usr/share/dict/words it seems to have a pretty even distribution.
2023-11-26 21:07:05 +00:00
2021-01-07 19:01:11 +00:00
2020-10-15 19:30:03 +00:00
floats
------
2023-04-28 16:18:41 +00:00
.. note ::
Floating point support is only available on c64, cx16 and virtual targets for now.
2022-10-29 12:10:11 +00:00
Provides definitions for the ROM/Kernal subroutines and utility routines dealing with floating
2023-04-29 20:11:26 +00:00
point variables. This includes `` print_f `` , the routine used to print floating point numbers.
2022-04-21 22:45:54 +00:00
2023-12-05 00:34:41 +00:00
`` π `` and `` PI ``
float const for the number Pi, 3.141592653589793...
`` TWOPI ``
float const for the number 2 times Pi
2023-04-29 20:11:26 +00:00
`` atan (x) ``
2022-04-21 22:45:54 +00:00
Arctangent.
2023-04-29 20:11:26 +00:00
`` ceil (x) ``
2022-04-21 22:45:54 +00:00
Rounds the floating point up to an integer towards positive infinity.
2023-04-29 20:11:26 +00:00
`` cos (x) ``
2022-04-21 22:45:54 +00:00
Cosine.
2023-04-29 20:11:26 +00:00
`` deg (x) ``
2022-04-21 22:45:54 +00:00
Radians to degrees.
2023-04-29 20:11:26 +00:00
`` floor (x) ``
2022-04-21 22:45:54 +00:00
Rounds the floating point down to an integer towards minus infinity.
2023-04-29 20:11:26 +00:00
`` ln (x) ``
2022-04-21 22:45:54 +00:00
Natural logarithm (base e).
2023-04-29 20:11:26 +00:00
`` log2 (x) ``
2022-04-21 22:45:54 +00:00
Base 2 logarithm.
2023-04-29 20:11:26 +00:00
`` minf (x, y) ``
2023-04-29 15:52:15 +00:00
returns the smallest of x and y.
2023-04-29 20:11:26 +00:00
`` maxf (x, y) ``
2023-04-29 15:52:15 +00:00
returns the largest of x and y.
2023-05-17 21:03:59 +00:00
`` clampf (value, minimum, maximum) ``
returns the value restricted to the given minimum and maximum.
2023-12-29 02:12:44 +00:00
`` print (x) ``
2023-12-07 20:25:03 +00:00
Prints the floating point number x as a string.
There's no leading whitespace (unlike cbm BASIC when printing a floating point number)
2023-12-29 02:12:44 +00:00
`` tostr (x) ``
2023-12-07 20:25:03 +00:00
Converts the floating point number x to a string (returns address of the string buffer)
There's no leading whitespace.
2023-04-29 20:11:26 +00:00
`` rad (x) ``
2022-04-21 22:45:54 +00:00
Degrees to radians.
2023-04-29 20:11:26 +00:00
`` round (x) ``
2022-04-21 22:45:54 +00:00
Rounds the floating point to the closest integer.
2023-04-29 20:11:26 +00:00
`` sin (x) ``
2022-04-21 22:45:54 +00:00
Sine.
2023-04-29 20:11:26 +00:00
`` tan (x) ``
2022-04-21 22:45:54 +00:00
Tangent.
2023-12-29 02:12:44 +00:00
`` rnd () ``
2022-10-22 14:35:57 +00:00
returns the next random float between 0.0 and 1.0 from the Pseudo RNG sequence.
2023-12-29 02:12:44 +00:00
`` rndseed (seed) ``
2022-11-06 21:41:23 +00:00
Sets a new seed for the float pseudo-RNG sequence. Use a negative non-zero number as seed value.
2020-10-15 19:30:03 +00:00
2023-12-29 02:12:44 +00:00
`` parse (stringvalue) ``
2023-08-16 12:47:20 +00:00
Parses the string value as floating point number.
Warning: this routine may stop working on the Commander X16 when a new ROM version is released,
because it uses an internal BASIC routine. Then it will require a fix.
2023-06-15 22:12:52 +00:00
2020-10-15 19:30:03 +00:00
graphics
--------
2023-06-18 00:31:45 +00:00
Bitmap graphics routines:
2020-10-15 19:30:03 +00:00
- clearing the screen
2020-12-30 17:02:46 +00:00
- drawing individual pixels
- drawing lines, rectangles, filled rectangles, circles, discs
2020-10-15 19:30:03 +00:00
2023-09-05 17:48:22 +00:00
This library is available both on the C64 and the cx16.
2020-12-26 12:38:14 +00:00
It uses the ROM based graphics routines on the latter, and it is a very small library because of that.
2024-03-05 21:56:02 +00:00
On the cx16 there's also the `` gfx2 `` library if you want more features and different screen modes. See below for that one.
2023-06-18 00:31:45 +00:00
2024-02-21 20:29:17 +00:00
Read the `graphics source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/c64/graphics.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there. (Note: slight variations for different compiler targets)
2020-12-26 12:38:14 +00:00
2022-01-03 21:44:27 +00:00
math
----
2022-10-28 21:40:36 +00:00
Low-level integer math routines (which you usually don't have to bother with directly, but they are used by the compiler internally).
2022-10-22 14:35:57 +00:00
Pseudo-Random number generators (byte and word).
Various 8-bit integer trig functions that use lookup tables to quickly calculate sine and cosines.
Usually a custom lookup table is the way to go if your application needs these,
but perhaps the provided ones can be of service too.
2023-08-31 22:42:15 +00:00
`` log2 (ubyte v) ``
Returns the 2-Log of the byte value v.
`` log2w (uword v) ``
Returns the 2-Log of the word value v.
2022-10-22 14:35:57 +00:00
2023-04-29 20:11:26 +00:00
`` rnd () ``
2022-10-22 14:35:57 +00:00
Returns next random byte 0-255 from the pseudo-RNG sequence.
2023-04-29 20:11:26 +00:00
`` rndw () ``
2022-10-22 14:35:57 +00:00
Returns next random word 0-65535 from the pseudo-RNG sequence.
2022-01-03 21:44:27 +00:00
2024-02-10 21:16:13 +00:00
`` randrange (ubyte n) -> ubyte ``
Returns random byte uniformly distributed from 0 to n-1 (compensates for divisibility bias)
`` randrangew (uword n) -> uword ``
Returns random word uniformly distributed from 0 to n-1 (compensates for divisibility bias)
2023-04-29 20:11:26 +00:00
`` rndseed (uword seed1, uword seed2) ``
2022-10-22 14:35:57 +00:00
Sets a new seed for the pseudo-RNG sequence (both rnd and rndw). The seed consists of two words.
2022-10-22 15:54:24 +00:00
Do not use zeros for the seed!
2022-04-13 18:43:07 +00:00
2023-04-29 20:11:26 +00:00
`` sin8u (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit ubyte sine of angle 0..255, result is in range 0..255
2023-04-29 20:11:26 +00:00
`` sin8 (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit byte sine of angle 0..255, result is in range -127..127
2023-04-29 20:11:26 +00:00
`` sinr8u (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit ubyte sine of angle 0..179 (each is a 2 degree step), result is in range 0..255
Angles 180..255 will yield a garbage result!
2023-04-29 20:11:26 +00:00
`` sinr8 (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit byte sine of angle 0..179 (each is a 2 degree step), result is in range -127..127
Angles 180..255 will yield a garbage result!
2023-04-29 20:11:26 +00:00
`` cos8u (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit ubyte cosine of angle 0..255, result is in range 0..255
2023-04-29 20:11:26 +00:00
`` cos8 (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit byte cosine of angle 0..255, result is in range -127..127
2023-04-29 20:11:26 +00:00
`` cosr8u (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit ubyte cosine of angle 0..179 (each is a 2 degree step), result is in range 0..255
Angles 180..255 will yield a garbage result!
2023-04-29 20:11:26 +00:00
`` cosr8 (x) ``
2022-04-13 18:43:07 +00:00
Fast 8-bit byte cosine of angle 0..179 (each is a 2 degree step), result is in range -127..127
Angles 180..255 will yield a garbage result!
2023-06-18 11:05:36 +00:00
`` atan2 (ubyte x1, ubyte y1, ubyte x2, ubyte y2) ``
2023-06-16 22:43:23 +00:00
Fast arctan routine that uses more memory because of large lookup tables.
Calculate the angle, in a 256-degree circle, between two points in the positive quadrant.
2023-06-17 13:36:53 +00:00
`` direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) ``
2023-06-16 22:43:23 +00:00
From a pair of positive coordinates, calculate discrete direction between 0 and 23.
2023-06-17 13:36:53 +00:00
This is a heavily optimized routine (small and fast).
`` direction_sc (byte x1, byte y1, byte x2, byte y2) ``
From a pair of signed coordinates around the origin, calculate discrete direction between 0 and 23.
This is a heavily optimized routine (small and fast).
2023-06-16 22:43:23 +00:00
2023-06-17 13:36:53 +00:00
`` direction_qd (ubyte quadrant, ubyte xdelta, ubyte ydelta) ``
2023-06-16 22:43:23 +00:00
If you already know the quadrant and x/y deltas, calculate discrete direction between 0 and 23.
2023-06-17 13:36:53 +00:00
This is a heavily optimized routine (small and fast).
2023-06-16 22:43:23 +00:00
2023-10-27 20:36:43 +00:00
`` diff (ubyte b1, ubyte b2) -> ubyte ``
Returns the absolute difference, or distance, between the two byte values.
(This routine is more efficient than doing a compare and a subtract separately, or using abs)
`` diffw (uword w1, uword w2) -> uword ``
Returns the absolute difference, or distance, between the two word values.
(This routine is more efficient than doing a compare and a subtract separately, or using abs)
2023-06-16 22:43:23 +00:00
2023-11-06 20:55:58 +00:00
`` mul16_last_upper () -> uword ``
Fetches the upper 16 bits of the previous 16*16 bit multiplication.
To avoid corrupting the result, it is best performed immediately after the multiplication.
2023-11-06 22:18:24 +00:00
Note: It is only for the regular 6502 cpu multiplication routine.
It does not work for the verafx multiplication routines on the Commander X16!
These have a different way to obtain the upper 16 bits of the result: just read cx16.r0.
2023-11-06 20:55:58 +00:00
2023-12-29 06:00:14 +00:00
`` crc16 (uword data, uword length) -> uword ``
Returns a CRC-16 (XMODEM) checksum over the given data buffer.
Note: on the Commander X16, there is a CRC-16 routine in the kernal: cx16.memory_crc().
2024-03-15 23:45:25 +00:00
That one is faster, but yields different results. It is unclear to me what flavour of crc it is calculating.
`` crc16_start() / crc16_update(ubyte value) / crc16_end() -> uword ``
"streaming" crc16 calculation routines, when the data doesn't fit in a single buffer.
Tracks the crc16 checksum in cx16.r15! If your code uses that, it must save/restore it before calling this routine!
Call the start() routine first, feed it bytes with the update() routine, finalize with calling the end() routine which returns the crc16 value.
2023-12-29 06:00:14 +00:00
`` crc32 (uword data, uword length) ``
Calculates a CRC-32 (POSIX) checksum over the given data buffer.
2024-03-15 23:45:25 +00:00
The 32 bits result is stored in cx16.r14 (low word) and cx16.r15 (high word).
2023-12-29 06:00:14 +00:00
2024-03-15 23:45:25 +00:00
`` crc32_start() / crc32_update(ubyte value) / crc32_end() ``
"streaming" crc32 calculation routines, when the data doesn't fit in a single buffer.
Tracks the crc32 checksum in cx16.r14 and cx16.r15! If your code uses these, it must save/restore them before calling this routine!
Call the start() routine first, feed it bytes with the update() routine, finalize with calling the end() routine.
The 32 bits result is stored in cx16.r14 (low word) and cx16.r15 (high word).
2022-01-03 21:44:27 +00:00
cx16logo
--------
2022-04-02 17:10:18 +00:00
Just a fun module that contains the Commander X16 logo in PETSCII graphics
and allows you to print it anywhere on the screen.
2022-01-03 21:44:27 +00:00
2023-06-18 00:31:45 +00:00
`` logo () ``
prints the logo at the current cursor position
`` logo_at (column, row) ``
printss the logo at the given position
2022-01-03 21:44:27 +00:00
prog8_lib
---------
2022-10-28 21:40:36 +00:00
Low-level language support. You should not normally have to bother with this directly.
2022-04-02 17:10:18 +00:00
The compiler needs it for various built-in system routines.
2022-01-03 21:44:27 +00:00
2023-06-24 19:04:47 +00:00
cx16
----
This is available on *all targets* , it is always imported as part of syslib.
2024-02-04 10:35:09 +00:00
On the Commander X16 this module contains a *whole bunch* of things specific to that machine.
It's way too much to include here, you have to study the
2024-02-21 20:29:17 +00:00
`syslib source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/syslib.p8> `_
2024-02-04 10:35:09 +00:00
to see what is there.
2023-06-24 19:04:47 +00:00
On the other targets, it only contains the definition of the 16 memory mapped virtual registers
2024-02-04 10:35:09 +00:00
(cx16.r0 - cx16.r15) and the following utility routines:
2023-06-24 19:04:47 +00:00
`` save_virtual_registers() ``
save the values of all 16 virtual registers r0 - r15 in a buffer. Might be useful in an IRQ handler to avoid clobbering them.
`` restore_virtual_registers() ``
restore the values of all 16 virtual registers r0 - r15 from the buffer. Might be useful in an IRQ handler to avoid clobbering them.
2024-02-04 10:35:09 +00:00
`` cpu_is_65816() ``
Returns true if the CPU in the computer is a 65816, false otherwise (6502 cpu).
2024-03-15 00:04:51 +00:00
`` reset_system () ``
Soft-reset the system back to initial power-on BASIC prompt. (same as the routine in sys)
`` poweroff_system () ``
Powers down the computer.
`` set_led_brightness (ubyte brightness) ``
Sets the brightness of the activity led on the computer.
2024-02-04 10:35:09 +00:00
2023-11-24 20:26:34 +00:00
bmx (cx16 only)
----------------
Routines to load and save "BMX" files, the CommanderX16 bitmap file format.
Specification available here: https://cx16forum.com/forum/viewtopic.php?t=6945
2023-11-30 21:12:13 +00:00
Only *uncompressed* bitmaps are supported in this library for now.
2023-11-24 20:26:34 +00:00
The routines are designed to be fast and bulk load/save the data directly into or from vram,
without the need to buffer something in main memory.
2023-11-30 21:12:13 +00:00
For details about what routines are available, have a look at
2024-02-21 20:29:17 +00:00
the `bmx source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/bmx.p8> `_ .
2023-11-30 21:12:13 +00:00
There's also the "showbmx" example to look at.
2023-11-24 20:26:34 +00:00
2023-06-24 19:04:47 +00:00
2023-10-02 19:57:42 +00:00
emudbg (cx16 only)
-------------------
X16Emu Emulator debug routines, for Cx16 only.
Allows you to interface with the emulator's debug routines/registers.
There's stuff like `` is_emulator `` to detect if running in the emulator,
and `` console_write `` to write a (iso) string to the emulator's console (stdout) etc.
2024-02-21 20:29:17 +00:00
Read the `emudbg source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/emudbg.p8> `_
2023-10-02 19:57:42 +00:00
to see what's in there.
Information about the exposed debug registers is in the `emulator's documentation <https://github.com/X16Community/x16-emulator#debug-io-registers> `_ .
2023-10-23 22:16:25 +00:00
monogfx (cx16 and virtual)
---------------------------
2023-10-04 20:32:13 +00:00
Full-screen lores or hires monochrome bitmap graphics routines, available on the Cx16 machine only.
Same interface as gfx2, but is optimized for monochrome (1 bpp) screens.
- lores 320*240 or hires 640* 480 bitmap mode, monochrome
- clearing screen, switching screen mode, also back to text mode
- drawing and reading individual pixels
- drawing lines, rectangles, filled rectangles, circles, discs
- flood fill
- drawing text inside the bitmap
2024-03-03 21:35:54 +00:00
- can draw using a stipple pattern (alternate black/white pixels) and in invert mode (toggle pixels)
2023-10-04 20:32:13 +00:00
2024-02-21 20:29:17 +00:00
Read the `monogfx source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/monogfx.p8> `_
2023-10-04 20:32:13 +00:00
to see what's in there.
2023-06-24 19:04:47 +00:00
2020-12-26 12:38:14 +00:00
gfx2 (cx16 only)
-----------------
Full-screen multicolor bitmap graphics routines, available on the Cx16 machine only.
2023-10-04 20:32:13 +00:00
Same interface as monogfx, but for color screens. For 1 bpp monochrome screens, use monogfx.
2020-12-26 12:38:14 +00:00
2023-10-04 20:32:13 +00:00
- multiple full-screen bitmap color resolutions
- clearing screen, switching screen mode, also back to text mode
2022-11-06 12:40:55 +00:00
- drawing and reading individual pixels
2020-12-27 22:57:13 +00:00
- drawing lines, rectangles, filled rectangles, circles, discs
2023-05-22 22:27:42 +00:00
- flood fill
2020-12-26 12:38:14 +00:00
- drawing text inside the bitmap
2024-02-21 20:29:17 +00:00
Read the `gfx2 source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/gfx2.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there.
2020-12-26 12:38:14 +00:00
palette (cx16 only)
--------------------
Available for the Cx16 target. Various routines to set the display color palette.
2022-10-28 21:45:09 +00:00
There are also a few better looking Commodore 64 color palettes available here,
2020-12-26 12:38:14 +00:00
because the Commander X16's default colors for this (the first 16 colors) are too saturated
2022-10-29 12:12:10 +00:00
and are quite different than how they looked on a VIC-II chip in a C64.
2020-12-26 12:38:14 +00:00
2024-02-21 20:29:17 +00:00
Read the `palette source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/palette.p8> `_
2023-06-18 00:31:45 +00:00
to see what's in there.
2020-10-15 19:30:03 +00:00
2022-07-02 15:08:19 +00:00
psg (cx16 only)
----------------
Available for the Cx16 target.
Contains a simple abstraction for the Vera's PSG (programmable sound generator) to play simple waveforms.
It includes an interrupt routine to handle simple Attack/Release envelopes as well.
See the examples/cx16/bdmusic.p8 program for ideas how to use it.
2023-06-18 00:31:45 +00:00
2024-02-21 20:29:17 +00:00
Read the `psg source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/psg.p8> `_
2023-09-01 14:56:46 +00:00
to see what's in there.
2023-04-23 23:23:03 +00:00
2023-09-01 14:56:46 +00:00
sprites (cx16 only)
--------------------
Available for the Cx16 target. Simple routines to manipulate sprites.
They're not written for high performance, but for simplicity.
That's why they control one sprite at a time. The exception is the `` pos_batch `` routine,
which is quite efficient to update sprite positions of multiple sprites in one go.
See the examples/cx16/sprites/dragon.p8 and dragons.p8 programs for ideas how to use it.
2024-02-21 20:29:17 +00:00
Read the `sprites source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/sprites.p8> `_
2023-09-01 14:56:46 +00:00
to see what's in there.
2023-09-24 21:00:40 +00:00
verafx (cx16 only)
-------------------
Available for the Cx16 target.
Experimental routines that use the new Vera FX logic (hopefully coming in the Vera in new X16 boards,
the emulators already support it).
2023-10-10 18:26:16 +00:00
`` available ``
Returns true if Vera FX is available, false if not (that would be an older Vera chip)
2023-10-01 23:34:56 +00:00
`` mult `` , `` muls ``
2023-10-10 18:26:16 +00:00
The hardware 16*16 multiplier is exposed via `` mult `` and `` muls `` routines (unsigned and signed respectively).
2023-10-01 23:34:56 +00:00
They are about 4 to 5 times faster as the default 6502 cpu routine for word multiplication.
But they depend on some Vera manipulation and 4 bytes in vram just below the PSG registers for storage.
2023-10-05 19:52:48 +00:00
Note: there is a block level %option "verafxmuls" that automatically replaces all word multiplications in that block
by calls to verafx.muls/mult, but be careful with it because it may interfere with other Vera operations or IRQs.
2023-10-01 23:34:56 +00:00
2023-11-06 20:55:58 +00:00
Note: the lower 16 bits of the 32 bits result is returned as the normal subroutine's returnvalue,
2023-12-04 21:18:37 +00:00
but the upper 16 bits is returned in cx16.r0 so you can still access those separately.
2023-11-06 20:55:58 +00:00
2023-10-01 23:34:56 +00:00
`` clear ``
2023-10-10 18:26:16 +00:00
Very quickly clear a piece of vram to a given byte value (it writes 4 bytes at a time).
2023-10-01 23:34:56 +00:00
The routine is around 3 times faster as a regular unrolled loop to clear vram.
2023-09-24 21:00:40 +00:00
2023-12-22 16:52:43 +00:00
`` copy ``
Very quickly copy a portion of the video memory to somewhere else in vram (4 bytes at a time)
Sometimes this is also called "blitting".
2023-12-22 21:24:11 +00:00
This routine is about 50% faster as a regular byte-by-byte copy.
2023-12-22 16:52:43 +00:00
2023-10-02 23:47:52 +00:00
`` transparency ``
Enable or disable transparent writes (color 0 will be transparent if enabled).
2024-02-21 20:29:17 +00:00
Read the `verafx source code <https://github.com/irmen/prog8/tree/master/compiler/res/prog8lib/cx16/verafx.p8> `_
2023-09-24 21:00:40 +00:00
to see what's in there.
2024-04-10 17:58:15 +00:00