BASIC Reference

A.C. Wright 6502 BIOS
Commands & Statements
Core Statements
CommandSyntaxEffect
PRINTPRINT [item [sep item …]]Output to console. ;=no sep (trailing ; suppresses CRLF), ,=14-col zone. Bare PRINT=CRLF only
INPUTINPUT ["prompt"{;|,}] var [,var…]Read value(s) from user. ?REDO FROM START on bad numeric input; ?EXTRA IGNORED if too many
LET[LET] var = exprAssign expression to variable. LET keyword is optional
GOTOGOTO linenumJump unconditionally to line
GOSUBGOSUB linenumPush position and jump. ≥20 levels always, ~27 for a simple one; beyond → OUT OF MEMORY
RETURNRETURNPop GOSUB stack and resume after calling GOSUB
IFIF expr THEN stmt [ELSE stmt]Execute THEN if expr non-zero, else ELSE branch. THEN/ELSE linenum = GOTO linenum
FORFOR var=init TO limit [STEP n]Counted loop. Default step 1. Up to 8 nested. Limit tested at NEXT, so the body always runs once
NEXTNEXT [var [,var…]]Increment loop var and branch back to matching FOR if condition holds
REMREM [text]Comment — rest of line ignored
ENDENDStop execution and return to OK. Variables preserved
STOPSTOPStop and print BREAK IN nnnn. Resume with CONT
CONTCONTContinue after STOP, END or Ctrl+C (immediate mode only); FOR and GOSUB frames survive. CAN’T CONTINUE after an error, NEW, CLR or RUN
ONON expr GOTO l1,l2…Evaluate expr, branch to nth target. Also ON … GOSUB. Out-of-range silently continues
DATADATA v1,v2,…Inline data (numeric or string literals) for READ. Skipped during normal execution
READREAD var [,var…]Read next value(s) from DATA. OUT OF DATA if exhausted
RESTORERESTOREReset DATA pointer to start of program
LISTLISTPrint program in detokenized form. Ctrl+C interrupts
RUNRUN [linenum]Clear variables and run program, optionally from linenum
NEWNEWErase program and clear variables
CLRCLRClear variables and arrays; reset stacks. Program is kept
DIMDIM var(size) [,var(size)…]Dimension a 1-D array (numeric or string). Valid indices 0..size. REDIM'D ARRAY if already done
DEF FNDEF FN A(X) = exprDefine single-argument numeric user function. Call with FN A(value)
POKEPOKE addr, valueWrite byte value to memory address addr
BRKBRKDrop into machine-code monitor. Return to BASIC with X
Storage & System
CommandEffect
SYS <addr>Call machine-code routine at addr; RTS returns to BASIC
LOAD "name"Load named file from CompactFlash to $0800
SAVE "name"Save current program to CompactFlash
LOADReceive program via XModem on serial port
SAVETransmit current program via XModem
DIRList current disk's directory (prints DISK n header)
DEL "name"Delete named file from current disk
DISK <n>Select CF disk bank 0–255. Each disk = 1MB (2048 sectors); 256 disks = 256MB total. Resets to 0 on boot
BLOAD <addr>,"name"Load file's raw bytes from current disk to memory address addr
BSAVE <addr>,<len>,"name"Save len bytes from address addr to a named file on current disk
FORMATErase current disk's file directory (prompts ERASE DISK n? Y/N)
BANK <n>Select 1KB RAM bank n at $8000–$83FE
MEMPrint free bytes, HW_PRESENT (hex), and current DISK n
Video & Display
CommandEffect
CLSClear screen; reset cursor to (0,0)
LOCATE <row>,<col>Move cursor to row 0–23, col 0–39
COLOR <fg>,<bg>Set TMS9918 text foreground/background colours (0–15 each)
Sound
CommandEffect
SOUND <v>,<f>,<d>Play tone on voice 1–3 at freq Hz for dur centiseconds, then silence (Kernal SidPlayNote numbers them 0–2)
VOL <n>Set SID master volume (0–15)
Timing & I/O
CommandEffect
PAUSE <n>Pause for n centiseconds (~10 ms each)
WAIT <addr>,<mask>Spin until (addr) AND mask is non-zero; Ctrl+C aborts
Time & Date
CommandEffect
TIMEPrint current RTC time as HH:MM:SS
DATEPrint current RTC date as CCYY-MM-DD
SETTIME <hh>,<mm>,<ss>Set the RTC time
SETDATE <cc>,<yy>,<mm>,<dd>Set the RTC date
NVRAM <addr>,<val>Write byte to RTC NVRAM at address 0–255

BASIC Reference

A.C. Wright 6502 BIOS
Functions & Expressions
Functions & Expressions
FunctionReturns
ABS(x)Absolute value of x
SGN(x)Sign of x: 1, 0, or −1
INT(x)Floor — largest integer ≤ x
SQR(x)Square root of x (error if negative)
EXP(x)e raised to x
LOG(x)Natural logarithm (error if x ≤ 0)
SIN(x) / COS(x) / TAN(x)Trig functions, radians
ATN(x)Arctangent, radians
RND(x)Float [0,1) for x>0; repeats last for x=0; reseeds for x<0
PEEK(addr)Byte value at memory address addr
FRE(x)Free bytes between top of var space and string heap (arg ignored)
POS(x)Current print column (arg ignored)
LEN(s$)String length
VAL(s$)Parse s$ as number; returns 0 if not numeric
ASC(s$)ASCII code of first character of s$
CHR$(n)One-character string with ASCII code n
STR$(n)Numeric value n formatted as a string
LEFT$(s$,n)First n characters of s$
RIGHT$(s$,n)Last n characters of s$
MID$(s$,start[,len])Substring from 1-based index start, optional length len
TAB(n)In PRINT: advance cursor to column n (no-op if already past)
SPC(n)In PRINT: emit n spaces
INKEYNon-blocking key: ASCII code or 0. No parentheses
JOY(1) / JOY(2)Joystick bitmask, bits R L D U Y X B A (7→0). Active low — a held button reads 0, an untouched stick reads $FF. Test with IF (JOY(1) AND 16) = 0
NVRAM(addr)Read byte from RTC NVRAM; returns 0 if RTC absent
HEX(n)In PRINT: output n as $xxxx hex; in expressions: returns n unchanged
MIN(a,b) / MAX(a,b)Smaller / larger of a and b
FN A(value)Call user function defined with DEF FN A(X)
var(index)Array element access. Array must be DIM-med first
Operators
OperatorDescription
+   −   *   /Standard arithmetic
^Exponentiation
+ (strings)String concatenation
=   <>   <   >   <=   >=Comparisons — work on numbers and strings; return −1 (true) or 0 (false)
AND   OR   NOTBitwise logical on integer parts of operands
Operator Precedence (High → Low)
LevelOperators
Power^
Unary− (negate)   +
Multiplicative*   /
Additive+   −
Relational=   <>   <   >   <=   >=
Logical NOTNOT
Logical ANDAND
Logical OROR
Graceful Degradation
Hardware AbsentBASIC Behavior
CompactFlashLOAD, SAVE, DIR, DEL, BLOAD, BSAVE, FORMAT print NO DEVICE
SerialXModem LOAD/SAVE return error
GPIO/VIAJOY() returns $FF — every line released, as an untouched stick reads. PAUSE falls back to a calibrated software loop
SIDSOUND, VOL silently return
VideoCLS, LOCATE, COLOR silently skip; console auto-switches to serial
RTCTIME, DATE, SETTIME, SETDATE, NVRAM (write) print NO DEVICE; NVRAM() read returns 0
A screen and a speaker have nothing to report back, so those statements skip silently; the ones that move data (CompactFlash, RTC) raise NO DEVICE, because there the program asked for something it did not get. Either way arguments are parsed and range-checked first — LOCATE 24,0 and VOL 16 are ILLEGAL QUANTITY even on a machine with neither card, so a program is wrong or right everywhere.
Notes
Variables:  A–Z single-letter numeric (5-byte float, ~±1.7×10³⁸, 6 sig. digits).  A$–Z$ strings.  Arrays: DIM var(size) — indices 0..size, 1-D only.
Boolean:  −1 = true, 0 = false.  Multiple statements per line separated by :.
Stack Limits:  GOSUB and FOR frames share the 6502 stack with the interpreter: at least 20 GOSUB levels always, ~27 for a simple subroutine · FOR/NEXT 8 nested loops. Exceeding either → OUT OF MEMORY.
Memory Layout:  Programs grow up from $0800. Vars follow. String heap grows down from $8000. MEM reports free bytes.
XModem:  LOAD/SAVE without filename uses XModem (128-byte blocks, checksum). System prints XMODEM RX/TX READY and waits ~60 s for terminal to begin transfer.
Sign Convention:  Positive numbers print with a leading space; negative with .
Program Files:  .bas/.prg both just mean the bytes at $0800; the extension is cosmetic. A .prg is a 10 SYS 2060 stub with machine code attached. LOAD/SAVE round-trip it intact — don't edit its BASIC line, since that shifts the attached code.
HW_PRESENT Flags  ($030D)
BitMaskCard / Device
0$01RAM card low (IO 1)
1$02RAM card high (IO 2)
2$04RTC DS1511Y (IO 3)
3$08CompactFlash (IO 4)
4$10Serial R65C51 (IO 5)
5$20GPIO / VIA 65C22 (IO 6)
6$40SID / ARMSID (IO 7)
7$80Video TMS9918 (IO 8)