Oct 4, 2026 · Updated Oct 11, 2026 · @Craig Taylor
This chapter is a reference to every keyword in BASIC 65: commands, functions, reserved variables and secondary keywords. It is meant to be consulted rather than read through. Chapter 4 shows how to use the language well, and Chapter 6 explains how BASIC 65 works internally.
The entries summarise the official BASIC 65 reference in the MEGA65 Book, which gives every keyword a full page or more with all parameters, remarks and examples (MEGA65 Book, Appendix B). Each entry here gives the page to turn to for the complete description. Most examples are taken from the Book's own entries, sometimes shortened; where the Book gives none, the example is written for this chapter.
| Part of an entry | Contents |
|---|---|
| Heading | The keyword, or a group of closely related keywords described together |
| Token line | How each keyword is stored in a program, as Chapter 6 describes, and the page of its full entry in the MEGA65 Book |
| Description | What it does, in brief |
| Format | Its syntax |
| Example | A short program fragment or direct-mode command showing it in use |
In the formats, square brackets [ ] mark optional parts,
and angle brackets with a slash, such as <ON / OFF>,
mark a choice of one. Where a format allows a list of optional arguments
in braces, as in RREG [{areg, xreg, yreg}], arguments can
be skipped with empty commas. Lower-case words stand for values the
programmer supplies.
Tokens come in four forms:
$80 to $FD, for
the keywords BASIC 65 shares with earlier Commodore BASICs and many of
its own.$FE, for most
extended statements, a scheme BASIC 65 inherits from the C128's BASIC
7.0, such as BANK ($FE $02).$CE, for most
added functions, such as POINTER ($CE $0A).$CB $A4) or SETBIT
($FE $2D $FE $4E, SET followed by BIT).Reserved variables such as DS, ER and TI have no token: they are stored as their names.
BASIC 65 recognises keywords anywhere in a line, even inside what was
meant as a variable name. A variable called SCORE contains
the keyword OR, and TOTAL contains
TO; both cause syntax errors. Short variable names avoid
the problem, and Chapter 6 explains why one-letter names are also the
fastest.
| Category | Keywords |
|---|---|
| Program flow | DO, LOOP, WHILE, UNTIL, EXIT, FOR, TO, STEP, NEXT, IF, THEN, ELSE, BEGIN, BEND, GOTO, GOSUB, FGOTO, FGOSUB, ON, RETURN, END, STOP, CONT, SLEEP, WAIT, VSYNC |
| Error handling and debugging | TRAP, RESUME, ER, EL, ERR$, HELP, TRON, TROFF |
| Program editing | AUTO, RENUMBER, DELETE, LIST, FIND, CHANGE, NEW, KEY, EDIT, HIGHLIGHT, MERGE, IMPORT |
| Variables and data | LET, DIM, CLR, DATA, READ, RESTORE, DEF FN, FN, POINTER, REM |
| Keyboard and screen input | INPUT, LINE INPUT, GET, GETKEY |
| Text output | PRINT, PRINT USING, USING, SPC, TAB, POS, CMD, CURSOR, RCURSOR, WINDOW, RWINDOW, SCNCLR, FONT, CHARDEF |
| Strings | ASC, CHR$, LEFT$, MID$,
RIGHT$, LEN, INSTR, STR$, VAL,
RPT$, HEX$, DEC, STRBIN$,
DECBIN |
| Numbers | ABS, INT, SGN, SQR, EXP, LOG, LOG10, LOG2, SIN, COS, TAN, ATN, MOD, RND, AND, OR, NOT, XOR |
| Memory and machine language | PEEK, POKE, WPEEK, WPOKE, SETBIT, CLRBIT, HASBIT, BANK, MEM, EDMA, DMA, SYS, RREG, USR, FRE, BLOAD, BSAVE, BVERIFY, BOOT, MONITOR |
| Disk and files | DIR, DIRECTORY, CATALOG, DLOAD, DSAVE, DVERIFY, LOAD, SAVE, VERIFY,
RUN, OPEN, CLOSE, DOPEN, DCLOSE, APPEND, PRINT#, INPUT#, LINE INPUT#,
GET#, RECORD, FREAD#, FWRITE#, COPY, CONCAT, RENAME, SCRATCH, ERASE,
LOCK, UNLOCK, BACKUP, COLLECT, FORMAT, HEADER, DCLEAR, DISK, MKDIR,
CHDIR, MOUNT, TYPE, SET, DS, DS$, ST, RDISK |
| Colour | COLOR, FOREGROUND, BACKGROUND, BORDER, PALETTE, RCOLOR, RPALETTE |
| Bitmap graphics | GRAPHIC, SCREEN, PEN, RPEN, DMODE, DPAT, DOT, LINE, BOX, CIRCLE, ELLIPSE, POLYGON, PAINT, CHAR, CUT, GCOPY, PASTE, PIXEL, VIEWPORT, RGRAPHIC, LOADIFF, SAVEIFF |
| Sprites | SPRITE, MOVSPR, SPRCOLOR, SPRSAV, COLLISION, BUMP, RSPRITE, RSPPOS, RSPCOLOR, RSPRSYS |
| Sound | SOUND, PLAY, TEMPO, ENVELOPE, FILTER, VOL, RPLAY |
| Controllers | JOY, POT, LPEN, MOUSE, RMOUSE |
| System | FAST, SPEED, RSPEED, GO64, FREEZER, INFO, DT$, TI,
TI$, T@&, C@&, OFF |
Some of BASIC 65's most useful features are not words at all, so they never appear in an alphabetical list. They are gathered here.
| Prefix | Meaning | Example | Book |
|---|---|---|---|
$ |
Hexadecimal constant | POKE $D020, 0 |
B-6 |
% |
Binary constant | DPAT 0, 4, %00110011, %00111100, %00001111, %11110000 |
B-6, B-67 |
Both work anywhere a number does, in programs and in direct mode:
PRINT $C000 prints 49152. The functions HEX$,
DEC, STRBIN$ and DECBIN convert
between these forms and strings at run time.
A variable's type is set by the symbol after its name (MEGA65 Book, p. B-7):
| Suffix | Type | Range | Example |
|---|---|---|---|
| none | Real (floating point) | about -1E37 to 1E37 | XY = 1/3 |
% |
Integer | -32768 to 32767 | I% = 5 |
& |
Byte | 0 to 255 | BY& = 23 |
$ |
String | 0 to 255 characters | AB$ = "TEXT" |
The same characters mean different things in front of a number and
after a name: %101 is a binary constant, while
A% is an integer variable. A single letter followed by its
type symbol, such as A, B% or C$,
is a fast variable: BASIC 65 finds it without searching.
| Array | Contents | Book |
|---|---|---|
T@&(column, row) |
Screen code of the character at that position | B-259 |
C@&(column, row) |
Colour code of the character at that position | B-39 |
These behave as predefined byte arrays mapped onto the text screen
and its colour memory. Reading one tells a program what is on the
screen; assigning to one changes it, with no PEEK, POKE or address
arithmetic: T@&(10,5)=1 : C@&(10,5)=2 puts a red A
at column 10, row 5.
| Variable | Contents |
|---|---|
DS, DS$ |
Status of the last disk operation, as a number and as text |
ER, EL |
Number and line of the last BASIC error, or -1 |
ST |
Status of the last I/O operation |
TI |
Microsecond-resolution timer |
TI$, DT$ |
Time and date from the real-time clock |
These names cannot be used for ordinary variables, and their values are set by BASIC. Each is described in the alphabetical list below.
| Symbol | Meaning |
|---|---|
? |
Abbreviation for PRINT; ? N is the same as
PRINT N, and LIST shows it as PRINT (MEGA65 Book, p.
9-13) |
# |
Marks a channel number, as in PRINT#,
INPUT#, GET# and DOPEN# |
↑ |
The power operator in expressions; at the start of a line, RUN |
← |
At the start of a line, SAVE |
/ |
At the start of a line, LOAD |
@ |
At the start of a line, DISK: send a command to the drive |
$ |
At the start of a line, DIR |
The last five shortcuts are meant for use with a directory listing:
type $ to list the disk, move the cursor to a program's
line, type ↑ over the start of it and press RETURN, and the
program loads and runs. Everything except the quoted file name on the
line is ignored (MEGA65 Book, p. 6-13).
Tokens $EB, $EC, $FD,
$FC, $ED · Book pp. B-78, B-162, B-282, B-272,
B-104
DO and LOOP enclose a block of statements that repeats. A WHILE or UNTIL condition can go after DO, tested before each pass, or after LOOP, tested after each pass; without one, the loop runs until EXIT leaves it.
Format:
DO [<UNTIL / WHILE> condition] ... [EXIT] ... LOOP [<UNTIL / WHILE> condition]
10 PW$ = "" : DO
20 GET A$ : PW$ = PW$ + A$
30 LOOP UNTIL LEN(PW$) > 7 OR A$ = CHR$(13)
Tokens $81, $A4, $A9,
$82 · Book pp. B-114, B-266, B-252, B-178
FOR starts a counted loop and NEXT ends it. The index runs from the start value to the end value, changing by STEP each time, or by 1 if STEP is omitted. TO is also used by many other commands, such as COPY and RENAME.
Format:
FOR index = start TO end [STEP step] ... NEXT [index]
10 FOR D = 0 TO 360 STEP 30
20 R = D * π / 180
30 PRINT D; R; SIN(R)
40 NEXT D
Tokens $8B, $A7, $D5 · Book
pp. B-134, B-263, B-96
IF tests a condition. When it is true, the statements after THEN run; otherwise the statements after ELSE run, if there is an ELSE. A line number after THEN acts as a GOTO.
Format:
IF expression THEN statements [: ELSE statements]. A colon
must come before ELSE.
10 INPUT "ENTER A NUMBER"; V
20 IF V < 0 THEN PRINT "NEGATIVE" : ELSE PRINT "NOT NEGATIVE"
Tokens $FE $18, $FE $19 · Book pp. B-27,
B-28
BEGIN and BEND bracket a block of statements, over several lines if needed, that runs as the THEN or ELSE part of an IF. They lift the old limit of one line per condition.
Format:
IF condition THEN BEGIN ... BEND [ELSE BEGIN ... BEND]
10 GET A$
20 IF A$ >= "A" AND A$ <= "Z" THEN BEGIN
30 PW$ = PW$ + A$
40 PRINT A$;
50 BEND
60 IF LEN(PW$) < 8 THEN 10
Token $89, or $CB $A4 as GO TO · Book p.
B-127
Continues the program at the given line number.
Format: GOTO line or GO TO line
10 PRINT "HELLO "; : GOTO 10
Tokens $8D, $8E · Book pp. B-126, B-214
GOSUB calls a subroutine at a line number; RETURN goes back to the statement after the GOSUB. RETURN also ends a COLLISION handler.
Format: GOSUB line ... RETURN
10 FOR I = 1 TO 3 : GOSUB 100 : NEXT
20 END
100 PRINT "CALL NUMBER"; I : RETURN
Tokens $FE $47, $FE $48 · Book pp. B-108,
B-107
Like GOTO and GOSUB, but the line number is calculated from an expression when the statement runs.
Format: FGOTO expression /
FGOSUB expression
10 INPUT "WHICH SUBROUTINE: 100, 200 OR 300"; LI
20 FGOSUB LI
Token $91 · Book p. B-181
Jumps to, or calls, one of a list of lines chosen by the value of an expression: 1 selects the first line, 2 the second, and so on. ON is also the secondary keyword in KEY ON and MOUSE ON.
Format: ON expression GOTO line [, line ...] or
ON expression GOSUB line [, line ...]
10 ON SGN(X) + 2 GOTO 100, 200, 300 : REM NEGATIVE, ZERO, POSITIVE
Token $80 · Book p. B-98
Ends the program and returns to the READY prompt.
Format: END
10 IF V < 0 THEN END
20 PRINT V
Token $90 · Book p. B-253
Stops the program with a BREAK message naming the line, so it can be resumed with CONT.
Format: STOP
10 IF V < 0 THEN STOP
20 PRINT SQR(V)
Token $9A · Book p. B-58
Resumes a program stopped by STOP, END or the RUN/STOP key, provided the program has not been edited in between.
Format: CONT
CONT
Token $FE $0B · Book p. B-242
Pauses for a number of seconds, which may be a fraction.
Format: SLEEP seconds
SLEEP 0.5 : REM WAIT HALF A SECOND
Token $92 · Book p. B-281
Pauses until a memory location shows a given bit pattern: the value read is XORed with the optional third value, then ANDed with the mask, and the wait ends when the result is not zero.
Format: WAIT address, andmask [, xormask]
10 BANK 128
20 WAIT $D611, 1 : REM WAIT FOR THE LEFT SHIFT KEY
Token $FE $54 · Book p. B-280
Waits until the video display reaches a raster line: 0-311 on PAL, 0-261 on NTSC. Useful for changing colours or graphics at a fixed point in the frame.
Format: VSYNC raster line
10 BORDER 3 : VSYNC 100
20 BORDER 7 : VSYNC 260
30 GOTO 10
Token $D7 · Book p. B-267
Sends any BASIC error to a handler at the given line instead of stopping the program. TRAP with no line turns the handler off.
Format: TRAP [line]
10 TRAP 100
20 FOR I = 1 TO 100 : PRINT EXP(I) : NEXT
30 END
100 PRINT ERR$(ER) : RESUME 30
Token $D6 · Book p. B-213
Ends a TRAP handler: RESUME retries the failing statement, RESUME NEXT continues after it, and RESUME with a line number continues there.
Format: RESUME [line / NEXT]
100 PRINT "ERROR"; ER; "IN LINE"; EL : RESUME NEXT
Reserved variables, no token · Book pp. B-100, B-93
ER holds the number of the most recent error and EL the line where it happened; both are -1 if there has been no error.
Format: ER, EL
100 PRINT "ERROR"; ER; "AT LINE"; EL
Token $D3 · Book p. B-103
Returns the text of an error message from its number, 1 to 44.
Format: ERR$(number)
100 PRINT ERR$(ER); " ERROR IN"; EL
Token $EA · Book p. B-131
After a program stops with an error, lists the line where it happened with the failing statement highlighted.
Format: HELP
HELP
Tokens $D8, $D9 · Book pp. B-269, B-268
TRON turns on trace mode, in which each line number is printed as it runs; TROFF turns it off.
Format: TRON / TROFF
10 TRON
20 FOR I = 80 TO 88 : PRINT I; EXP(I) : NEXT
30 TROFF
Token $DC · Book p. B-23
Turns automatic line numbering on while a program is typed in: after each line is entered, the next line number appears. AUTO with no step turns it off.
Format: AUTO [step]
AUTO 10 : REM NUMBER NEW LINES IN STEPS OF 10
Token $F8 · Book p. B-210
Renumbers program lines and updates every GOTO, GOSUB and other line reference to match. The arguments give the new first number, the increment and the range of lines to renumber.
Format: RENUMBER [C] [{new, inc, range}]
RENUMBER 100, 10 : REM START AT 100, STEPS OF 10
Token $F7 · Book p. B-69
Deletes a range of program lines. Given a file name, it deletes files on disk instead.
Format: DELETE [line range] or
DELETE filename [,D drive] [,U unit] [,R]
DELETE 240-350 : REM DELETE LINES 240 TO 350
Token $9B · Book p. B-153
Lists all or part of the program in memory, or a program file on disk without loading it. P pauses after each screenful.
Format: LIST [P] [line range] or
LIST [P] filename [,U unit]
LIST 500- : REM LIST FROM LINE 500 TO THE END
Token $FE $2B · Book p. B-110
Lists every line of the program that contains a string.
Format: FIND "string" [, line range]
FIND "HS"
FIND /PRINT/
With quotes, FIND searches plain text, which finds variable names but not keywords, because a keyword is stored as a token; another delimiter, as in the second line, finds the keyword (MEGA65 Book, FIND).
Token $FE $2C · Book p. B-42
Finds each occurrence of one string, throughout the program or within a range of lines, lists the line and asks whether to replace it: Y replaces it, * replaces this and every later match, RETURN stops, and any other key skips it.
Format: CHANGE "find" TO "replace" [, line range]
CHANGE "XX$" TO "UU$", 2000-2700
Token $A2 · Book p. B-177
Erases the program in memory and resets BASIC.
NEW RESTORE tries to recover a program erased by
mistake.
Format: NEW
NEW
Token $F9 · Book p. B-145
Manages the function key macros: KEY alone lists them, KEY with a number and string assigns one, and KEY ON and KEY OFF enable or disable them.
Format: KEY / KEY number, string /
KEY <ON / OFF>
KEY 2, "PRINT π" + CHR$(13) : REM F2 PRINTS PI
Tokens $FE $45, with $91 (ON) or
$FE $24 (OFF) · Book p. B-89
EDIT ON switches the editor to text mode, in which numbered lines are stored as plain text instead of being tokenised as BASIC, for writing data or text files; EDIT OFF returns to BASIC.
Format: EDIT <ON / OFF>
EDIT ON
Token $FE $3D · Book p. B-133
Sets the colours used by the editor's highlighting, such as for system messages, REM statements and keywords in listings.
Format: HIGHLIGHT colour [, mode]
HIGHLIGHT 7, 1
Token $E6 · Book p. B-166
Loads a program from disk and adds its lines to the program in memory.
Format: MERGE filename [,D drive] [,U unit]
DLOAD "MAIN PROGRAM"
MERGE "LIBRARY"
Token $DD · Book p. B-136
Loads a BASIC program written as plain text in a SEQ file, tokenising it as it loads; useful for programs written on another computer.
Format: IMPORT filename [,D drive] [,U unit]
IMPORT "MEGA TOOLS", U9
Token $88 · Book p. B-149
Assigns a value to a variable. The word LET is optional and only makes the line longer and slower.
Format: [LET] variable = expression
LET A = 5 : B = 6
Token $86 · Book p. B-70
Declares an array and its upper bounds; each dimension counts from 0,
so DIM A%(8) has nine elements. Arrays used without DIM
default to 11 elements.
Format: DIM name(limits) [, name(limits) ...]
10 DIM A%(8), XX(2, 3)
Token $9C · Book p. B-51
Clears all variables and arrays; CLR variable resets
just one, and CLR TI restarts the timer.
Format: CLR or CLR variable
10 A = 5 : CLR : PRINT A : REM PRINTS 0
Tokens $83, $87, $8C · Book
pp. B-63, B-205, B-212
DATA lines hold constants, READ copies them into variables in order, and RESTORE moves the reading position back to the first DATA line or to a given line.
Format: DATA constant [, constant ...] /
READ variable [, variable ...] /
RESTORE [line]
10 DATA 3, 1, 4, 1, 5, "MEGA65"
20 FOR I = 1 TO 5 : READ P : PRINT P : NEXT
30 READ N$ : PRINT N$
40 RESTORE
Tokens $96 $A5, $A5 · Book pp. B-68,
B-112
DEF FN defines a one-line function of one numeric argument; FN calls it.
Format: DEF FN name(variable) = expression /
FN name(expression)
10 DEF FN SD(X) = SIN(X * π / 180) : REM SINE IN DEGREES
20 PRINT FN SD(30)
Token $CE $0A · Book p. B-193
Returns the 32-bit address of a variable's value or an array element. For a string, it is the address of its descriptor. Chapter 6 shows how to use it.
Format: POINTER(variable)
10 BANK 0
20 H$ = "HELLO" : P = POINTER(H$)
30 PRINT "DESCRIPTOR AT $"; HEX$(P)
Token $8F · Book p. B-208
Starts a comment: BASIC ignores the rest of the line.
Format: REM text
20 N = 1000 : REM NUMBER OF ITEMS
Token $85 · Book p. B-138
Prints an optional prompt, followed by a question mark if the prompt ends with a semicolon (a comma leaves it out), then waits for the user to type values for one or more variables and press RETURN.
Format:
INPUT [prompt <, / ;>] variable [, variable ...]
10 INPUT "NAME, AGE"; NA$, AG%
Token $E5 $85 · Book p. B-151
Reads a whole line of typing into a string variable, including commas and quotation marks that INPUT would treat as separators.
Format:
LINE INPUT [prompt <, / ;>] stringvar [, stringvar ...]
10 LINE INPUT "ENTER A PHRASE: ", PH$
Token $A1 · Book p. B-122
Takes the next key from the keyboard queue without waiting; a string variable receives an empty string if no key is waiting.
Format: GET variable
10 DO : GET A$ : LOOP UNTIL A$ <> ""
Token $A1 $F9 · Book p. B-124
Like GET, but waits until a key is pressed.
Format: GETKEY variable
10 GETKEY A$
20 IF A$ = "W" THEN PRINT "NORTH"
Token $99 · Book p. B-198
Prints values to the screen or the current output channel. A
semicolon joins items directly, a comma moves to the next tab column,
and a trailing semicolon suppresses the new line. ? is an
abbreviation.
Format: PRINT arguments
10 FOR I = 1 TO 10 : PRINT I, I * I, SQR(I) : NEXT
Tokens $99 $FB (or $98 $FB with
#), $FB · Book pp. B-200, B-273
Prints values laid out by a format string, in which #
marks a digit position and . the decimal point, so columns
of figures line up.
Format: PRINT[# channel,] USING format; argument
10 PRINT USING "####.##"; 3.14159
Tokens $A6, $A3 · Book pp. B-244, B-260
Used inside PRINT: SPC moves the cursor right by a number of columns, and TAB moves it to a given column if it is not already past it.
Format: SPC(columns) / TAB(column)
10 PRINT "NAME"; TAB(20); "SCORE"
20 PRINT SPC(5); "INDENTED"
Token $B9 · Book p. B-196
Returns the cursor's column within the current window. The argument is ignored.
Format: POS(dummy)
10 IF POS(0) > 72 THEN PRINT
Token $9D · Book p. B-53
Redirects all screen output, including LIST, to an open channel such as a printer, until PRINT# to that channel restores it.
Format: CMD channel [, string]
OPEN 1, 4 : CMD 1 : LIST
PRINT#1 : CLOSE 1
Tokens $FE $41, $FE $42 · Book pp. B-61,
B-203
CURSOR moves the text cursor, and can also show, hide or restyle it. RCURSOR reads its position into two variables.
Format: CURSOR [<ON / OFF>,] column, row [, style]
/ RCURSOR colvar, rowvar
10 CURSOR ON, 20, 10 : PRINT "[HERE]";
20 RCURSOR X, Y : PRINT " COL:"; X; " ROW:"; Y
Tokens $FE $1A, $CE $09 · Book pp. B-283,
B-230
WINDOW confines text output to a rectangle of the screen, optionally clearing it. RWINDOW reads a property of the current window or screen, such as its width.
Format: WINDOW left, top, right, bottom [, clear] /
RWINDOW(n)
10 WINDOW 0, 12, 79, 24, 1 : REM LOWER HALF, CLEARED
20 PRINT RWINDOW(2) : REM SCREEN WIDTH
Token $E8 · Book p. B-233
Without an argument, clears the current text window; with a colour, fills the graphics screen with that colour.
Format: SCNCLR [colour]
SCNCLR
Token $FE $46 · Book p. B-113
Switches the character set: FONT A is PETSCII with extra ASCII
symbols such as {, | and _, FONT
B is similar with a serif font, and FONT C restores the standard
Commodore font.
Format: FONT <A / B / C>
FONT A
Token $E0 $96 · Book p. B-45
Redefines the 8 × 8 pattern of a character, given its screen code and eight bytes, one per row.
Format: CHARDEF index, bit-matrix
CHARDEF 1, $FF, $81, $81, $81, $81, $81, $81, $FF : REM 'A' BECOMES A BOX
CHR$Tokens $C6, $C7 · Book pp. B-21, B-47
ASC returns the PETSCII code of a string's first character;
CHR$ returns a one-character string from a code.
CHR$ is the usual way to put quotation marks and control
codes in strings.
Format: ASC(string) / CHR$(code)
10 PRINT ASC("MEGA") : REM 77
20 PRINT CHR$(34); "MEGA65"; CHR$(34) : REM "MEGA65" IN QUOTES
LEFT$, MID$,
RIGHT$Tokens $C8, $CA, $C9 · Book
pp. B-147, B-167, B-216
Return the first n characters, n characters from a position, or the
last n characters of a string. MID$ can also appear on the
left of an equals sign to replace part of a string variable.
Format: LEFT$(string, n) /
MID$(string, index, n) / RIGHT$(string, n)
10 A$ = "MEGA65"
20 PRINT LEFT$(A$, 4); " "; MID$(A$, 3, 2); " "; RIGHT$(A$, 2)
30 MID$(A$, 4, 1) = "@" : PRINT A$ : REM MEG@65
Token $C3 · Book p. B-148
Returns the number of characters in a string.
Format: LEN(string)
PRINT LEN("MEGA65") : REM 6
Token $D4 · Book p. B-141
Returns the position of one string inside another, or 0 if it is not there. An optional third argument gives the position to start searching from.
Format: INSTR(haystack, needle [, start])
I = INSTR("ABCDEF", "CD") : REM I = 3
STR$, VALTokens $C4, $C5 · Book pp. B-254, B-276
STR$ converts a number to a string as PRINT would show
it; VAL converts a string of digits back to a number.
Format: STR$(number) / VAL(string)
10 A$ = "PI IS" + STR$(π)
20 PRINT VAL("78E2") : REM 7800
RPT$Token $CE $14 · Book p. B-222
Returns a string repeated a number of times.
Format: RPT$(string, count)
PRINT RPT$("=", 30)
HEX$, DECTokens $D2, $D1 · Book pp. B-132, B-66
HEX$ converts a number to four or eight hexadecimal
digits; DEC converts a hexadecimal string to a number.
Format: HEX$(number) / DEC(string)
10 PRINT HEX$(1000) : REM 03E8
20 PRINT DEC("D000") : REM 53248
STRBIN$, DECBINTokens $CE $12, $CE $11 · Book pp. B-255,
B-67
STRBIN$ converts a number to a string of binary digits;
DECBIN converts such a string back to a number.
Format: STRBIN$(number) /
DECBIN(string)
10 PRINT STRBIN$(245)
20 PRINT DECBIN("11110101") : REM 245
Tokens $B6, $B4, $B5 · Book
pp. B-18, B-240, B-142
ABS gives the absolute value, SGN gives -1, 0 or 1 for a negative,
zero or positive number, and INT rounds down to a whole number, so
INT(-3.1) is -4.
Format: ABS(x) / SGN(x) /
INT(x)
PRINT ABS(-123); SGN(-7); INT(1.9); INT(-3.1) : REM 123 -1 1 -4
Tokens $BA, $BD, $BC,
$CE $08, $CE $16 · Book pp. B-250, B-105,
B-159, B-160, B-161
Square root, e raised to a power, and logarithms to base e, 10 and 2.
Format: SQR(x), EXP(x),
LOG(x), LOG10(x), LOG2(x)
PRINT SQR(2); EXP(1); LOG10(1000); LOG2(256)
Tokens $BF, $BE, $C0,
$C1 · Book pp. B-241, B-60, B-261, B-22
Trigonometric functions in radians. SIND, COSD and TAND take degrees instead; ATN returns an angle between -π/2 and π/2.
Format: SIN(radians) or SIND(degrees),
likewise COS and TAN; ATN(x)
PRINT SIN(π / 6); SIND(30); ATN(1) * 4
Token $CE $0B · Book p. B-169
Returns the remainder of a division.
Format: MOD(dividend, divisor)
FOR I = 0 TO 8 : PRINT MOD(I, 4); : NEXT : REM 0 1 2 3 0 1 2 3 0
Token $BB · Book p. B-218
Returns a pseudo-random number from 0 up to, but not including, 1. Multiply and use INT for whole numbers in a range.
Format: RND(type)
10 DEF FN DI(X) = INT(RND(0) * 6) + 1 : REM DIE ROLL
20 FOR I = 1 TO 10 : PRINT FN DI(0); : NEXT
Tokens $AF, $B0, $E9,
$A8 · Book pp. B-19, B-183, B-286, B-179
Bitwise operations on 16-bit values, which also combine conditions because true is -1 and false is 0. The Operators section below covers them in more detail.
Format: a AND b, a OR b,
a XOR b, NOT a
PRINT 1 AND 3; 1 OR 3; 6 XOR 5; NOT 3 : REM 1 3 3 -4
Tokens $C2, $97 · Book pp. B-187, B-194
PEEK reads a byte and POKE writes one or more bytes to consecutive
addresses. Addresses up to $FFFF are interpreted through
BANK; larger ones are flat 28-bit addresses.
Format: PEEK(address) /
POKE address, value [, value ...]
10 BANK 128
20 POKE $D020, 0 : PRINT PEEK($D020) AND 15
Tokens $CE $10, $FE $1D · Book pp. B-284,
B-285
Read or write 16-bit values, low byte first, which suits addresses and pointers.
Format: WPEEK(address) /
WPOKE address, word [, word ...]
10 BANK 128
20 WPOKE $02F8, $1800 : PRINT HEX$(WPEEK($02F8))
Tokens $FE $2D $FE $4E, $9C $FE $4E,
$CE $13 · Book pp. B-239, B-52, B-129
Set, clear or test a single bit of a memory location without disturbing the others. HASBIT returns -1 or 0.
Format: SETBIT address, bit /
CLRBIT address, bit / HASBIT(address, bit)
10 BANK 128
20 SETBIT $D031, 6 : PRINT HASBIT($D031, 6)
30 CLRBIT $D011, 4 : REM BLANK THE DISPLAY
Token $FE $02 · Book p. B-26
Selects which memory bank commands with 16-bit addresses use. Banks 0 to 5 are chip RAM, though banks 2 and 3 hold the ROM and are write-protected; bank 128, the default, is the system view with ROM and I/O.
Format: BANK number
BANK 0 : POKE $4000, 1 : BANK 128
Token $FE $23 · Book p. B-164
Reserves 8KB blocks of banks 4 and 5 so the graphics commands leave them alone. Each argument is a bit mask, one bit per block.
Format: MEM mask4, mask5
MEM 3, 0 : REM RESERVE $40000-$43FFF
Token $FE $21 · Book p. B-91
Uses the DMA controller to copy (command 0) or fill (command 3) memory very fast, anywhere in the 28-bit address space; adding 32 to the command copies backwards.
Format: EDMA command, length, source, target
10 EDMA 0, 80*25, 2048, $8000800 : REM COPY SCREEN TO ATTIC RAM
20 EDMA 3, 80*25, 32, 2048 : REM FILL SCREEN WITH SPACES
Token $FE $1F · Book p. B-76
The older form of EDMA, with addresses given as offset and bank pairs. It is obsolete; use EDMA.
Format:
DMA command [, length, source, source bank, target, target bank [, sub]]
DMA 3, 80*25, 32, 0, 2048, 0 : REM FILL SCREEN WITH SPACES
Token $9E · Book p. B-256
Calls a machine-language routine, which returns with RTS. Values for A, X, Y, Z and the status register can follow. SYS TO names its registers and adds a MAP offset (O) or keeps BASIC's interrupts running (I).
Format: SYS address [{, a, x, y, z, s}] /
SYS TO address [{,A a, X x, Y y, Z z, S s, I mode, O offset}]
10 POKE $1800, $18, $8A, $65, $06, $60 : REM CLC : TXA : ADC $06 : RTS
20 SYS $1800, 77, 11
Token $FE $09 · Book p. B-223
Copies the register values a SYS call returned into variables.
Format: RREG [{areg, xreg, yreg, zreg, sreg}]
30 RREG AC, X, Y, Z, S : PRINT AC
Token $B7 · Book p. B-275
Calls a machine-language function whose address is in the vector at
$02F8. The argument and result pass through BASIC's
floating-point accumulator.
Format: USR(expression)
10 BANK 128 : WPOKE $2F8, $7F33 : REM ROM NEGATE ROUTINE, AS IN THE BOOK
20 PRINT USR(-5)
Token $B8 · Book p. B-117
Returns free memory or other system information: FRE(0)
and FRE(1) give free bytes for program and variables,
FRE(4) and FRE(5) (from ROM 920415) which 8KB
blocks of banks 4 and 5 are in use, whether reserved by MEM, a graphics
screen or the system (MEGA65 ROM change log), and
FRE(-1) the ROM version.
Format: FRE(n)
PRINT FRE(0); "BYTES FREE FOR PROGRAM"; FRE(-1)
Tokens $FE $11, $FE $10,
$FE $28 · Book pp. B-29, B-35, B-38
Load, save or verify a block of memory as a file. Addresses above
$FFFF are flat 28-bit addresses that can reach attic RAM; R
reads or writes raw data with no load address.
Format:
BLOAD filename [,B bank] [,P address] [,R] [,D drive] [,U unit]
/
BSAVE filename, P start TO P end [,B bank] [,R] [,D drive] [,U unit]
/
BVERIFY filename [,P address] [,B bank] [,D drive] [,U unit]
10 BLOAD "SPRITES", P($40000), R
20 BSAVE "ML ROUTINES", B1, P($9000) TO P($A000)
Token $FE $1B · Book p. B-31
Loads a machine-language program and runs it at its load address.
With no arguments, runs AUTOBOOT.C65; BOOT SYS boots from a
disk's boot sector.
Format:
BOOT filename [,B bank] [,P address] [,D drive] [,U unit]
BOOT "GAME"
Token $FA · Book p. B-170
Starts the built-in machine-language monitor. X returns to BASIC.
Format: MONITOR
MONITOR
Most disk commands accept D for the drive number on
dual-drive units and U for the unit (device) number, which
defaults to 8. File names can be quoted strings or string expressions in
parentheses, such as (F$).
Tokens $EE (DIR), $EE $FE $29 (DIRECTORY),
$FE $0C · Book pp. B-71, B-40
List the files on a disk without disturbing the program in memory. A
pattern selects matching names; W lists in several columns, and P pauses
after each screenful. $ at the start of a line is a
shortcut.
Format:
DIR [pattern] [,W] [,P] [,R] [,D drive] [,U unit]
DIR "*.PRG", W, U9
Tokens $F0, $EF, $FE $14 ·
Book pp. B-74, B-86, B-88
Load, save or verify a BASIC program on disk, defaulting to unit 8.
An @ before the name in DSAVE replaces an existing
file.
Format: DLOAD filename [,D drive] [,U unit], likewise
DSAVE and DVERIFY
10 DSAVE "@MYPROG"
20 DVERIFY "MYPROG"
30 DLOAD "MYPROG", U9
Tokens $93, $94, $95 · Book
pp. B-154, B-231, B-277
The C64-style forms, which take a device number instead of U.
LOAD "*", 8, 1 loads to the address stored in the file.
Format: LOAD filename [, unit [, flag]] /
SAVE filename [, unit] /
VERIFY filename [, unit [, binflag]]
LOAD "MEGA TOOLS", 9
Token $8A · Book p. B-229
Runs the program in memory, optionally from a given line, or loads and runs a program from disk. Running clears variables.
Format: RUN [line] or
RUN filename [,D drive] [,U unit]
RUN "FLIGHTSIM"
Tokens $9F, $A0 · Book pp. B-182, B-50
OPEN connects a channel number to a device, with an optional secondary address and file name; CLOSE disconnects it. Always close disk files, or they are left as unclosed "splat" files.
Format: OPEN channel, device [, secondary [, filename]]
/ CLOSE channel
10 OPEN 2, 8, 2, "TEST,S,W"
20 PRINT#2, "TESTSTRING"
30 CLOSE 2
Tokens $FE $0D, $FE $0F,
$FE $0E · Book pp. B-79, B-65, B-20
The disk-specific way to open and close files. DOPEN opens for reading, or for writing with W, or as a relative file with L and a record length. APPEND opens an existing file for writing at its end. DCLOSE closes one channel, or every file on a unit.
Format:
DOPEN# channel, filename [,L reclen] [,W] [,D drive] [,U unit]
/ APPEND# channel, filename [,D drive] [,U unit] /
DCLOSE# channel or DCLOSE [U unit]
10 DOPEN#2, "SCORES", W
20 PRINT#2, "ALICE" "," 1200
30 DCLOSE#2
40 APPEND#2, "SCORES" : PRINT#2, "BOB" "," 900 : DCLOSE#2
The comma is written as a string: a comma between PRINT# items, as in PRINT, writes spaces to the next tab stop, not a comma, and INPUT# would then read the whole line as one value.
Tokens $98, $84, $E5 $84,
$A1 # · Book pp. B-199, B-139, B-152, B-123
The file versions of PRINT, INPUT, LINE INPUT and GET, working on an open channel.
Format: PRINT# channel, arguments /
INPUT# channel, variables /
LINE INPUT# channel, stringvars /
GET# channel, variables
10 DOPEN#2, "SCORES"
20 DO : INPUT#2, N$, S : PRINT N$, S : LOOP UNTIL ST
30 DCLOSE#2
Token $FE $12 · Book p. B-206
Moves to a record, and optionally a byte within it, in a relative file opened with DOPEN and L.
Format: RECORD# channel, record [, byte]
10 DOPEN#3, "INDEX", L40
20 RECORD#3, 25 : PRINT#3, "ENTRY 25"
30 DCLOSE#3
Tokens $FE $1C, $FE $1E · Book pp. B-118,
B-120
Read or write a block of bytes between an open file and memory at a 32-bit address, typically from POINTER.
Format: FREAD# channel, pointer, size /
FWRITE# channel, pointer, size
10 DIM B&(23) : DOPEN#2, "TEXT"
20 FREAD#2, POINTER(B&(0)), 24
30 DCLOSE#2
Tokens $F4, $FE $13 · Book pp. B-59,
B-57
COPY copies one file, or files matching a pattern, to another name or unit. CONCAT appends one file to the end of another.
Format:
COPY source [,D drive] [,U unit] TO [target] [,D drive] [,U unit]
/ CONCAT file [,D drive] TO target [,D drive] [,U unit]
10 COPY "CODES" TO "BACKUP"
20 CONCAT "NEW DATA" TO "ARCHIVE", U9
Token $F5 · Book p. B-209
Renames a file on disk.
Format: RENAME old TO new [,D drive] [,U unit]
RENAME "CODES" TO "BACKUP"
Tokens $F2, $FE $2A · Book pp. B-234,
B-102
Delete files from a disk. A pattern deletes all matching files. DELETE with a file name does the same.
Format: SCRATCH filename [,D drive] [,U unit] [,R],
likewise ERASE
SCRATCH "DRM", U9
Tokens $FE $50, $FE $4F · Book pp. B-158,
B-271
Protect a file from being changed or deleted, or remove the protection.
Format:
LOCK <filename / pattern> [,D drive] [,U unit],
likewise UNLOCK
LOCK "DRM", U9
Token $F6 · Book p. B-25
Copies a whole disk to another unit, or between the two drives of a dual-drive unit.
Format: BACKUP U source TO U target or
BACKUP D source TO D target [,U unit]
BACKUP U8 TO U9
Token $F3 · Book p. B-54
Tidies a disk: rebuilds its record of free space and removes files that were never properly closed.
Format: COLLECT [,D drive] [,U unit]
COLLECT U9
Tokens $FE $37, $F1 · Book pp. B-116,
B-130
Format a disk, erasing everything on it. With an I and a two-character ID, the format is complete; without one, it is a quick format.
Format: FORMAT diskname [,I id] [,D drive] [,U unit],
likewise HEADER
FORMAT "ADVENTURE", IDK
Token $FE $15 · Book p. B-64
Sends an initialise command to a drive, closing its open files.
Format: DCLEAR [,D drive] [,U unit]
DCLEAR U9
Token $FE $40 · Book p. B-73
Sends a command string directly to a drive. @ at the
start of a line is a shortcut.
Format: DISK command [,U unit]
DISK "I0" : REM INITIALISE DRIVE 0
Tokens $FE $51, $FE $4B · Book pp. B-168,
B-46
MKDIR creates a subdirectory, a partition of a given number of
tracks, on a floppy or D81 image, and moves into it. CHDIR changes the
current directory: "/" returns to the root of a disk, and
".." goes up a level only on the SD card (U12).
Format: MKDIR dirname, L size [,U unit] /
CHDIR dirname [,U unit]
10 MKDIR "SUBDIR", L5 : REM CREATE IT AND MOVE INTO IT
20 CHDIR "/" : REM BACK TO THE ROOT
Token $FE $49 · Book p. B-171
Mounts a D81 or D64 disk image from the SD card as unit 8, or unit 9 with U9. Without a file name, it gives the drives back to the physical floppy (from ROM 920414): unit 8 becomes the internal drive and unit 9 is unmounted unless a unit is given (MEGA65 ROM change log). MOUNT OFF unmounts.
Format: MOUNT filename [,U unit] /
MOUNT [U unit] / MOUNT OFF [U unit]
MOUNT "GAMES.D81"
Token $FE $27 · Book p. B-270
Prints a PETSCII text file from disk to the screen; P pauses after each screenful.
Format: TYPE [P] filename [,D drive] [,U unit]
TYPE "README"
Token $FE $2D · Book p. B-238
SET DEF changes the default disk unit, SET DISK changes a drive's unit number, and SET VERIFY turns automatic verification of saves on or off.
Format: SET DEF unit / SET DISK old TO new
/ SET VERIFY <ON / OFF>
SET DEF 11 : DIR
DS$, STReserved variables, no token · Book pp. B-84, B-85, B-251
DS and DS$ give the status of the last disk operation as
a number and as text; ST gives the status of the last I/O operation, and
is non-zero at the end of a file.
Format: DS, DS$, ST
10 DOPEN#1, "DATA"
20 IF DS <> 0 THEN PRINT DS$ : STOP
Token $CE $15 · Book p. B-204
Returns properties of the most recent disk action, such as the unit a program was loaded from.
Format: RDISK(property)
10 DU = RDISK(1)
20 BLOAD "MYRESOURCE", U(DU) : REM LOAD FROM THE SAME UNIT
Colours are palette entry numbers: 0 to 31 for COLOR and FOREGROUND, whose values 16 to 31 select the first 16 colours in bold, and 0 to 255 for BACKGROUND, BORDER and PEN. The first 16 are the C64 colours: 0 black, 1 white, 2 red, 3 cyan, 4 purple, 5 green, 6 blue, 7 yellow and so on.
Tokens $E7, $FE $39 · Book pp. B-56,
B-115
Set the text colour for everything printed afterwards. The two keywords do the same job.
Format: COLOR colour /
FOREGROUND colour
COLOR 2 : PRINT "THIS IS RED"
Tokens $FE $3B, $FE $3C · Book pp. B-24,
B-32
Set the colour of the screen background and of the border around it.
Format: BACKGROUND colour /
BORDER colour
BACKGROUND 6 : BORDER 14
Tokens $FE $34, $CE $0D · Book pp. B-185,
B-219
PALETTE sets the red, green and blue levels, 0 to 15, of a palette entry, either for a graphics screen or, with PALETTE COLOR, for the system palette used by text. PALETTE RESTORE puts the defaults back. RPALETTE reads one component back: 0 red, 1 green, 2 blue.
Format: PALETTE screen, colour, red, green, blue /
PALETTE COLOR colour, red, green, blue /
RPALETTE(screen, index, rgb)
10 PALETTE COLOR 9, 0, 0, 7 : REM ENTRY 9 BECOMES DARK BLUE
20 PALETTE RESTORE
Token $CD · Book p. B-202
Returns the current colour of a source such as the background, text or border.
Format: RCOLOR(source)
10 C = RCOLOR(3) : REM CURRENT BORDER COLOUR
HIGHLIGHT, which sets the editor's highlighting colours, is under Program Editing.
The graphics commands draw on a bitplane screen in banks 4 and 5, opened with SCREEN. Coordinates are in pixels from the top-left corner.
Tokens $FE $2E, $DE · Book pp. B-235,
B-128
SCREEN opens a graphics screen of a given width, height and depth in bitplanes, and SCREEN CLOSE returns to text. GRAPHIC CLR resets the whole graphics system.
Format: SCREEN [screen,] width, height, depth /
SCREEN CLOSE / GRAPHIC CLR
10 SCREEN 320, 200, 4 : REM 16 COLOURS
20 GETKEY A$ : SCREEN CLOSE
Tokens $FE $33, $D0 · Book pp. B-188,
B-220
PEN chooses the colour the drawing commands use; RPEN reads a pen's colour.
Format: PEN colour / RPEN(n)
PEN 2 : REM DRAW IN RED
Tokens $FE $35, $FE $36 · Book pp. B-77,
B-82
DMODE sets how drawing combines with what is already there, such as solid or complemented, and whether lines use the DPAT pattern (style 1). The stencil and thickness arguments are accepted but have no effect (MEGA65 ROM source, DMODE). DPAT sets a dash pattern for lines, used only when DMODE's style is 1.
Format: DMODE jam, complement, stencil, style, thick /
DPAT type [, number, pattern ...]
10 DPAT 0, 4, %00110011, %00111100, %00001111, %11110000
Tokens $FE $4C, $CE $0C · Book pp. B-81,
B-189
DOT plots a single pixel, in the pen colour or a given colour; PIXEL returns the colour of a pixel.
Format: DOT x, y [, colour] /
PIXEL(x, y)
10 SCREEN 320, 200, 4
20 FOR I = 1 TO 1000 : DOT RND(1) * 320, RND(1) * 200, RND(1) * 16 : NEXT
30 PRINT PIXEL(160, 100)
Token $E5 · Book p. B-150
Draws a line, or a connected series of lines through several points.
Format: LINE x1, y1 [, x2, y2 ...]
10 SCREEN 320, 200, 2 : PEN 1
20 LINE 25, 25, 295, 175, 295, 25, 25, 175
Tokens $E1, $E2, $FE $30,
$FE $2F · Book pp. B-33, B-48, B-94, B-195
Draw shapes, outlined or, with the solid flag, filled. BOX takes two opposite corners or four corner points; CIRCLE a centre and radius; ELLIPSE a centre and two radii; POLYGON a centre, radii and number of sides.
Format: BOX x0, y0, x2, y2 [, solid] /
CIRCLE xc, yc, radius [, flags, start, stop] /
ELLIPSE xc, yc, xr, yr [, flags, start, stop] /
POLYGON x, y, xrad, yrad, sides [...]
10 SCREEN 320, 200, 2 : PEN 1
20 BOX 10, 10, 100, 60, 1
30 CIRCLE 160, 100, 50
40 ELLIPSE 250, 100, 40, 20
50 POLYGON 160, 160, 30, 30, 6
Token $DF · Book p. B-184
Flood-fills an enclosed area, starting at a point, with the pen colour.
Format: PAINT x, y, mode [, border colour]
30 CIRCLE 160, 100, 50 : PAINT 160, 100, 0
Token $E0 · Book p. B-43
Draws text on a graphics screen, at a column given in characters and a row given in pixels, scaled by height and width.
Format:
CHAR column, row, height, width, direction, string [, charset address]
10 SCREEN 640, 400, 2
20 CHAR 28, 180, 4, 4, 2, "MEGA65", $29000
Tokens $E4, $FE $32, $E3 ·
Book pp. B-62, B-121, B-186
CUT copies a rectangle to a buffer and clears it; GCOPY copies it without clearing; PASTE draws the buffer at a new position.
Format: CUT x, y, width, height /
GCOPY x, y, width, height / PASTE x, y
10 SCREEN 320, 200, 2 : BOX 60, 60, 300, 180, 1
20 GCOPY 140, 80, 40, 40
30 PASTE 10, 10
Token $FE $31 · Book p. B-278
VIEWPORT DEF sets a clipping rectangle for drawing; VIEWPORT CLR fills it with the pen colour.
Format: VIEWPORT DEF x, y, width, height /
VIEWPORT CLR
10 VIEWPORT DEF 20, 30, 100, 120 : PEN 1 : VIEWPORT CLR
Token $CC · Book p. B-215
Returns a property of a graphics screen, such as whether it is open, or its width.
Format: RGRAPHIC(screen, parameter)
10 PRINT RGRAPHIC(0, 0) : REM 1 IF SCREEN 0 IS OPEN
Tokens $FE $43, $FE $44 · Book pp. B-156,
B-232
Load a picture in IFF format into graphics memory, or save the current screen as an IFF file.
Format: LOADIFF filename [,D drive] [,U unit] /
SAVEIFF filename [,D drive] [,U unit]
10 SAVEIFF "PICTURE"
20 LOADIFF "PICTURE"
Token $FE $07 · Book p. B-247
Turns a sprite on or off and sets its colour, priority, expansion and mode. SPRITE CLR resets all sprites, and SPRITE LOAD and SPRITE SAVE load and save sprite data.
Format:
SPRITE num [{, switch, colour, prio, expx, expy, mode}] /
SPRITE CLR / SPRITE LOAD filename /
SPRITE SAVE filename
10 SPRITE LOAD "SPRITEDATA"
20 SPRITE 1, 1, 2 : REM SPRITE 1 ON, RED
Token $FE $06 · Book p. B-175
Moves a sprite to a position, or sets it moving continuously at an
angle and speed, written angle#speed. BASIC's interrupt
keeps it moving while the program does other work.
Format: MOVSPR number, position
10 SPRITE 1, 1 : MOVSPR 1, 120, 0
20 MOVSPR 1, 0#5 : REM MOVE AT ANGLE 0, SPEED 5
Tokens $FE $08, $CE $07 · Book pp. B-246,
B-224
SPRCOLOR sets the two colours shared by all multicolour sprites; RSPCOLOR reads one back.
Format: SPRCOLOR [{mc1, mc2}] /
RSPCOLOR(n)
10 SPRCOLOR 4, 5
20 PRINT RSPCOLOR(1)
Token $FE $16 · Book p. B-249
Copies sprite data between sprites, or between a sprite and a string variable.
Format: SPRSAV source, destination
SPRSAV 1, 2 : REM COPY SPRITE 1'S SHAPE TO SPRITE 2
Tokens $FE $17, $CE $03 · Book pp. B-55,
B-37
COLLISION names a subroutine BASIC calls when sprites collide; BUMP returns a bitfield of the sprites involved, type 1 for sprite-to-sprite and type 2 for sprite-to-background.
Format: COLLISION type [, line] /
BUMP(type)
10 COLLISION 1, 70
...
70 S% = BUMP(1) : IF (S% AND 6) = 6 THEN PRINT "SPRITES 1 AND 2 HIT"
80 RETURN
Tokens $CE $06, $CE $05,
$CE $17 · Book pp. B-227, B-226, B-228
Read back sprite settings: RSPRITE a sprite's attributes, RSPPOS its position or speed, and RSPRSYS a system-wide sprite setting.
Format: RSPRITE(sprite, n) /
RSPPOS(sprite, n) / RSPRSYS(n)
10 XP = RSPPOS(1, 0) : YP = RSPPOS(1, 1)
20 PRINT "SPRITE 1 AT"; XP; YP
Chapter 13 covers these commands in detail.
Token $DA · Book p. B-243
Plays a sound effect on voices 1 to 6 of SIDs 2 and 4, with a SID frequency value and a duration in jiffies, and optional sweep, waveform and pulse width. SOUND CLR silences everything.
Format:
SOUND voice, freq, dur [{, dir, min, sweep, wave, pulse}] /
SOUND CLR
SOUND 3, 4000, 120, 2, 2000, 400, 1 : REM SWEEPING SAWTOOTH
Tokens $FE $04, $CE $0F · Book pp. B-190,
B-221
PLAY plays up to six strings of music notes in the background on SIDs 1 and 3. RPLAY tests whether a voice is still playing.
Format: PLAY [{string1, ... string6}] /
RPLAY(voice)
10 PLAY "O4ICDEFGABO5CR", "O2QCGEGCO1GCR"
20 IF RPLAY(1) OR RPLAY(2) THEN 20 : REM WAIT FOR THE END
Token $FE $05 · Book p. B-262
Sets the speed of PLAY; a whole note lasts 24 divided by the tempo, in seconds.
Format: TEMPO speed
TEMPO 24 : REM WHOLE NOTE = 1 SECOND
Token $FE $0A · Book p. B-99
Redefines one of the ten instrument slots PLAY uses.
Format:
ENVELOPE n [{, attack, decay, sustain, release, waveform, pw}]
ENVELOPE 9, 10, 5, 10, 5, 2, 4000
Token $FE $03 · Book p. B-109
Sets the SID filter for music: cutoff frequency, low-, band- and high-pass switches and resonance.
Format: FILTER sid [{, freq, lp, bp, hp, res}]
FILTER 1, 1024, 1, 0, 0, 10 : REM LOW PASS ON THE RIGHT MUSIC SID
Token $DB · Book p. B-279
Sets the volume, 0 to 15, of the right and left pairs of SIDs.
Format: VOL right, left
VOL 8, 8
Token $CF · Book p. B-143
Reads a joystick: the low four bits give the direction, 1 for up through 8 for up-left clockwise, and bit 7 is the fire button.
Format: JOY(port)
10 N = JOY(1)
20 IF N AND 128 THEN PRINT "FIRE!"
Token $CE $02 · Book p. B-197
Reads a paddle, 1 to 4. The low byte is the position, and values above 255 mean the button is pressed.
Format: POT(paddle)
10 X = POT(1) : B = X > 255 : V = X AND 255
Token $CE $04 · Book p. B-163
Reads the latched light pen X (0) or Y (1) position. The Book describes a light pen on port 1 with a CRT display, but the current core latches only touch input from the on-screen keyboard; see Chapter 16.
Format: LPEN(coordinate)
PRINT LPEN(0), LPEN(1)
Tokens $FE $3E, $FE $3F · Book pp. B-173,
B-217
MOUSE ON starts the mouse driver, with the pointer drawn by a sprite; RMOUSE reads the position and buttons, giving -1 if the mouse driver is off.
Format: MOUSE ON [{, port, sprite, hotspot, pos}] /
MOUSE OFF / RMOUSE x, y, buttons
10 MOUSE ON, 1, 1
20 RMOUSE XP, YP, BU : PRINT XP; YP; BU
Tokens $FE $26, $FE $25,
$CE $0E · Book pp. B-245, B-106, B-225
SPEED, or its synonym FAST, sets the CPU to 1, 3.5 or 40.5 MHz;
RSPEED reports the speed as 1, 3 or 40. Neither works after
POKE 0,65 has forced full speed.
Format: SPEED [speed] / FAST [speed] /
RSPEED(n)
10 SPEED 1 : PRINT RSPEED(0)
20 SPEED : PRINT RSPEED(0)
Token $CB $36 $34 · Book p. B-125
Switches to C64 mode, after asking for confirmation in direct mode. Any program in memory is lost.
Format: GO64
GO64
Token $FE $4A · Book p. B-119
Opens the Freezer menu, as holding RESTORE does.
Format: FREEZER
FREEZER
Token $FE $4D · Book p. B-137
Displays information about the running system, such as the video mode and memory use.
Format: INFO
INFO
TI$, DT$Reserved variables, no token · Book pp. B-264, B-265, B-87
TI is a timer with microsecond resolution, restarted with CLR TI;
TI$ and DT$ give the time and date from the
real-time clock.
Format: TI, TI$, DT$
10 CLR TI
20 FOR I% = 1 TO 10000 : NEXT
30 PRINT "TOOK"; TI; "SECONDS AT "; TI$; " ON "; DT$
Reserved arrays, no token · Book pp. B-259, B-39
Read or set the screen code and colour of the character at a column and row of the text screen, as described under Special Keywords and Symbols.
Format: T@&(column, row) /
C@&(column, row)
10 FOR C = 0 TO 63 : T@&(C, 0) = C : C@&(C, 0) = C AND 15 : NEXT
Token $FE $24 · Book p. B-180
Secondary keyword that turns a feature off, as in KEY OFF, MOUSE OFF and EDIT OFF.
Format: keyword OFF
MOUSE OFF
Operators are tokenised like keywords. The tables below follow the Book's operator reference (MEGA65 Book, pp. B-9 to B-12).
| Operator | Token | Meaning | Example |
|---|---|---|---|
↑ |
$AE |
Exponentiation | E = 2 ↑ 10 |
* |
$AC |
Multiplication | C = A * B |
/ |
$AD |
Division | D = B / 13 |
+ |
$AA |
Addition; positive sign; joins strings | A$ = B$ + ".PRG" |
- |
$AB |
Subtraction; negative sign | B = -42 |
<< |
$FE $52 |
Shift left | A = B << 2 |
>> |
$FE $53 |
Shift right | A = B >> 1 |
= |
$B2 |
Equal; also assignment | IF A = 42 THEN 100 |
<> |
$B3 $B1 |
Not equal | B <> 42 |
<, <= |
$B3, $B3 $B2 |
Less than, less than or equal | A < 42 |
>, >= |
$B1, $B1 $B2 |
Greater than, greater than or equal | B >= 42 |
| NOT, AND, OR, XOR | $A8, $AF, $B0,
$E9 |
Logical or bitwise operations | A = B AND $FF |
The shift operators are new in BASIC 65. The two-character comparisons are stored as their two single-character tokens.
A comparison evaluates to -1 when true and 0 when false. Because -1
has every bit set, NOT, AND, OR and XOR work the same way whether their
operands are comparisons or ordinary numbers:
IF X = 99 OR Y < 5 combines two conditions, while
A AND 20 masks bits. The values can also be used in
arithmetic: A = A - (B > 7) adds 1 to A when B is
greater than 7, and leaves it unchanged otherwise (MEGA65 Book, pp.
B-10, B-11).
The equals sign is both assignment and comparison, and BASIC tells
them apart by position. In A = B = 10, the first
= assigns and the second compares, so A becomes -1 or
0.
From highest to lowest:
↑+ and -* and /+ and -<< and >><, <=, >,
>=, =, <>Parentheses override the order: A * (A - B) * B performs
the subtraction first (MEGA65 Book, p. B-12).