BASIC Reference

A.C. Wright 6502 BIOS
BASIC V2.0 · BIOS v1.5
The shape of a program
Two modes. Type a line with a number in front and it is stored; type one without and it runs there and then. RUN runs what is stored, LIST shows it, NEW throws it away. The prompt is OK.
Names. Any length, letters and digits — but only the first two characters count, so COUNT and COURSE are the same variable. A name containing a keyword will not parse: SCORE has OR in it.
Numbers. Five-byte floats, about ±1.7 × 1038, nine significant digits. A positive number prints with a leading space where the minus sign would go.
Control and flow
KeywordSyntaxWhat it does
LET[LET] var = exprGive a variable a value. LET is optional and almost nobody types it.
PRINTPRINT [item] [;|, item ...]Show numbers and text. ; joins items tightly, , moves to the next 14-column zone, and a trailing ; or , holds the cursor on the line.
INPUTINPUT ["prompt";] var [, var ...]Stop and wait for the person at the keyboard to type something. Only works inside a program.
GOTOGOTO linenumJump to a line and carry on from there.
GOSUBGOSUB linenumJump to a subroutine, remembering where you came from. RETURN comes back.
RETURNRETURNGo back to the line after the GOSUB that called this subroutine.
ONON expr GOTO l1 [,l2 ...] | ON expr GOSUB l1 [,l2 ...]Pick a destination by number: 1 takes the first line in the list, 2 the second. Out of range falls through to the next line.
IFIF expr THEN stmt|linenum [ELSE stmt|linenum]Do something only when a test holds. THEN 100 is short for THEN GOTO 100.
THENIF expr THEN stmt|linenumThe part of IF that says what to do when the test holds.
ELSEIF expr THEN stmt ELSE stmtWhat to do when the test does not hold.
FORFOR var = start TO limit [STEP step]Count from one number to another, running everything up to NEXT each time through.
TOFOR var = start TO limitThe part of FOR that says where to count to.
STEPFOR var = start TO limit STEP stepHow much to add each time through. Negative counts down.
NEXTNEXT [var]End of a loop: add the step and go around again. One variable only — NEXT J, I is not accepted, so close each loop with its own NEXT.
DEFDEF FN name(var) = exprDefine your own one-line function.
FNFN name(expr)Call a function you defined with DEF.
ENDENDStop the program and return to the prompt. Variables keep their values.
STOPSTOPPause the program and say where it stopped. CONT picks it up again.
CONTCONTCarry on from a STOP or a stopped program, at the line after the one it stopped on. Editing any line first makes it impossible.
REMREM [text]A remark. Everything after REM on the line is for people, not the machine.
The program itself
KeywordSyntaxWhat it does
RUNRUN [linenum]Run the program from the top, or from a line number if you give one. Clears the variables first.
LISTLISTShow the program you have. It takes no line number — LIST lists the lot.
NEWNEWThrow the program away and clear the variables. There is no undo.
CLRCLRClear the variables, arrays and strings but keep the program.
DIMDIM var(size) [, var(size) ...]Make an array. One dimension only, and you get elements 0 to size — so DIM S(9) is ten of them.
DATADATA item [, item ...]A list of values baked into the program for READ to work through. Skipped when the program runs past it.
READREAD var [, var ...]Take the next value from the DATA lists and put it in a variable.
RESTORERESTOREGo back to the first DATA item, so you can read the list again.
Screen, sound and sticks
KeywordSyntaxWhat it does
CLSCLSClear the screen and put the cursor at the top left.
LOCATELOCATE row, colPut the cursor somewhere. Rows are 0 to 23 down the screen, columns 0 to 39 across.
COLORCOLOR fg, bgSet the text and background colors, each 0 to 15, for everything printed from now on.
SOUNDSOUND voice, freq, durPlay a note: voice 1 to 3, freq in hertz, dur in hundredths of a second. The program waits for the note to finish.
VOLVOL nSet the overall volume, 0 to 15.
INKEYINKEYThe code of a key being pressed right now, or 0 if none. Does not wait — put it in a loop.
JOYJOY(n)Read joystick 1 or 2. The bits are active low — a bit that reads 0 is a direction being pushed, so test with = 0, not with <> 0.
WAITWAIT addr, maskHold the program until reading addr and ANDing it with mask gives something other than zero. Nothing else happens while it waits.
PAUSEPAUSE nDo nothing for n hundredths of a second.
Files on the card
KeywordSyntaxWhat it does
LOADLOAD "name" | LOADRead a program back off the card, replacing whatever is in memory. Bare LOAD receives one over the serial port by XModem.
SAVESAVE "name" | SAVEWrite the program to the card. Bare SAVE sends it over the serial port instead, by XModem.
DIRDIRList the files on the current disk, with their sizes in bytes.
DELDEL "name"Delete a file from the current disk.
DISKDISK nChoose which of the card's 256 one-megabyte disks to work with.
FORMATFORMATErase the current disk and start it fresh. Asks first.
BLOADBLOAD addr, "name"Read a file of raw bytes straight back into memory.
BSAVEBSAVE addr, len, "name"Write raw bytes straight from memory to a file — a high score, a screen, a level.
Clock and lasting memory
KeywordSyntaxWhat it does
TIMETIMEPrint the time of day from the battery-backed clock.
DATEDATEPrint today's date from the battery-backed clock.
SETTIMESETTIME hh, mm, ssSet the clock. It keeps running with the power off.
SETDATESETDATE cc, yy, mm, ddSet the date: century, year within it, month, day. 2026 is 20, 26.
NVRAMNVRAM addr, value | NVRAM(addr)256 bytes of memory on the clock card that survive a power cut. A statement to write one, a function to read one.

BASIC Reference

A.C. Wright 6502 BIOS
BASIC V2.0 · BIOS v1.5
Reaching the machine
KeywordSyntaxWhat it does
PEEKPEEK(addr)Read one byte of memory, 0 to 255.
POKEPOKE addr, valueWrite one byte of memory. value is 0 to 255.
SYSSYS addrCall machine code at an address and come back when it returns.
BANKBANK nChoose which bank of extra RAM appears in the window at 32768.
MEMMEMPrint free memory, which cards the machine found, and the current disk. A statement, not a function — PRINT MEM is a syntax error.
FREFRE(x)How many bytes are left for variables, arrays and strings. The argument is ignored.
BRKBRKDrop into the machine-code Monitor. X at the Monitor's . prompt comes back to BASIC.
Functions
KeywordSyntaxWhat it does
TABTAB(n)Inside PRINT, move to column n of the line.
SPCSPC(n)Inside PRINT, print n spaces from wherever the cursor is.
NOTNOT exprFlips every bit, which turns true (-1) into false (0) — but NOT 1 is -2, not 0, so use it on comparisons rather than on counts.
ANDa AND bTrue when both sides are true — and, underneath, a bit-by-bit AND of two whole numbers.
ORa OR bTrue when either side is true — and, underneath, a bit-by-bit OR.
SGNSGN(x)-1 for a negative number, 0 for zero, 1 for a positive one.
INTINT(x)Round down to a whole number — down towards minus infinity, so INT(-3.7) is -4.
ABSABS(x)The size of a number, ignoring its sign.
POSPOS(x)Which column the cursor is on. The argument is ignored; give it 0.
SQRSQR(x)Square root.
RNDRND(x)A random number from 0 up to (but not including) 1. A negative argument reseeds from that number, so the same seed replays the same sequence.
LOGLOG(x)Natural logarithm.
EXPEXP(x)e raised to the power x.
COSCOS(x)Cosine of an angle in radians.
SINSIN(x)Sine of an angle in radians.
TANTAN(x)Tangent of an angle in radians.
ATNATN(x)Arctangent, in radians. ATN(1) * 4 is a serviceable π.
LENLEN(s$)How many characters are in a string.
STR$STR$(n)A number turned into a string — keeping the leading space that PRINT puts where a minus sign would go.
VALVAL(s$)A string turned into a number. Reads as far as it can and gives 0 if that is nowhere.
ASCASC(s$)The code of the first character of a string.
CHR$CHR$(n)The character with code n.
LEFT$LEFT$(s$, n)The first n characters of a string.
RIGHT$RIGHT$(s$, n)The last n characters of a string.
MID$MID$(s$, start [, len])Characters from the middle of a string, counting from 1. Leave len out to take the rest.
HEXHEX(n)A number written in hexadecimal, always four digits with a $ in front. n is 0 to 65535.
MINMIN(a, b)The smaller of two numbers.
MAXMAX(a, b)The larger of two numbers.
Operator precedence
RankLevelOperators
1Power^
2Unary- +
3Multiplicative* /
4Additive+ -
5Relational= <> < > <= >=
6Logical NOTNOT
7Logical ANDAND
8Logical OROR
Truth is −1. A comparison gives -1 for true and 0 for false, and AND, OR and NOT work bitwise on the integer part — which is exactly why the joystick test is IF (JOY(1) AND 16) = 0.
Three things that catch everyone
The loop body always runs once. FOR tests its limit at the NEXT, so FOR N = 1 TO 0 still runs the body one time.
One NEXT per loop. NEXT J, I is not accepted, whatever else you may have read — it fails mid-loop with ?NEXT WITHOUT FOR ERROR. Close each loop with its own NEXT.
The joystick reads backwards. A direction being held reads 0, not 1. An untouched stick is 255.
Every error message
The machine printsBecause
?SYNTAX ERRORBASIC could not parse the line — a typo, a missing bracket, or a keyword buried in a name
?OVERFLOW ERRORA result too big for a five-byte float
?OUT OF MEMORY ERRORProgram, variables and the GOSUB / FOR stack together filled the space
?UNDEF'D STATEMENT ERRORGOTO or GOSUB naming a line number that is not in the program
?BAD SUBSCRIPT ERRORAn array index outside 0 to the size given to DIM
?REDIM'D ARRAY ERRORDIM used twice on the same array
?DIVISION BY ZERO ERRORA divide with zero on the bottom
?ILLEGAL DIRECT ERRORA statement that only works inside a program, typed at the prompt
?TYPE MISMATCH ERRORA string where a number belongs, or the other way around
?STRING TOO LONG ERRORA string over 255 characters
?FORMULA TOO COMPLEX ERRORAn expression nested deeper than the evaluator can hold
?ILLEGAL QUANTITY ERRORA number outside what the statement accepts — voice 0, volume 16, color 16
?RETURN WITHOUT GOSUB ERRORA RETURN reached without a GOSUB to come back from
?NEXT WITHOUT FOR ERRORA NEXT with no FOR open — or a comma list after it, which is not accepted
?OUT OF DATA ERRORREAD ran past the last DATA item. RESTORE goes back to the start
?NO DEVICE ERRORThe statement needs a card this machine has not got
?CAN'T CONTINUE ERRORCONT after an error, NEW, CLR or RUN — there is nothing to resume
Getting out of trouble
KeyDoes
EscStops a running program. This is the one to remember.
Ctrl + CThe same thing, for people used to typing it
CONTCarries on from where STOP or Esc left off
Reset buttonWarm start — your program and variables survive