Color BASIC


Color BASIC is the implementation of Microsoft BASIC that is included in the ROM of the Tandy/Radio Shack TRS-80 Color Computers manufactured between 1980 and 1991. BASIC is a high level language with simple syntax that makes it easy for novices to write simple programs. Color BASIC is interpreted, that is, decoded as it is run. Interpreted BASIC is simple to edit and debug but execution is significantly slower than for programs written in assembly language or typical compiled languages of the time.

Background

The nucleus of Color BASIC was Microsoft BASIC-69 which Tandy licensed from Microsoft. Color BASIC 1.0 was released with the original 4k TRS-80 Color Computer in 1980. It resides on 8k bytes of ROM, and is responsible for all 'housekeeping' duties on the system. This includes hardware initialization, memory management, interrupt processing, etc. Like most implementations of BASIC, each line of code starts with a line number and consists of one or more statements with variables and operators. 16k of memory is required for the next level of BASIC, Extended Color BASIC. Extended BASIC is required for the floppy disk controller, which then gives you Disk Extended Color BASIC. Emulators of the Color Computers running this interpreter and the others are available for modern computers, some of which require a "snapshot" file of the physical machine.

Variables

Color BASIC understands one type of numeric variable and string variables. Variable names in Color BASIC have the first two characters significant. The first character of the variable name must be a letter. The second can be either a letter or number. String variables are indicated by adding a dollar sign after the variable name.

Examples


A=5
A$="THIS IS A STRING"
K7=10
BB$="HELLO!"
ZZ=1.54

Numeric variables have only one type, a binary floating point implementation. Each numeric variable uses 5 bytes of memory and can be in the range from -1E+38 up to 1E+37.
Unlike most implementations of Microsoft BASIC, Color BASIC requires the user to reserve space for string variables via the CLEAR statement.
Multidimensional arrays are also supported with both numeric and string variables. In the case of an array, the element address is enclosed with a parenthesis:

A=1
A$="BOOKS"

Multiple dimensions are separated by commas

A=4
A$="XYZ123"

Operators and Symbols

Color BASIC provides several operators for both mathematic and, to a lesser extent, string operations.
Parenthesis are used to override mathematical order of operation
For testing, the following operators are used:
Other symbols used in BASIC:

Key

num indicates a numeric expression is required. This can be a fixed number, a variable, or other operation or function that returns a numeric quantity.
str indicates a string expression is required. This can be a static string value, a string variable, or other function or expression that returns a string of characters.
device number indicates a device. By default, device 0 is assumed. In Color BASIC, device #-1 and #-2 are available to the programmer.

Edit mode

If you make a mistake typing in a line, you can either retype it from scratch.. or you can EDIT it.
When in EDIT mode, you get a reprint of the line, and a second copy that you SPACEbar across chars. You cannot use arrow keys. backspace takes you left, but does not actually erase it in the buffer. 'i' puts you in insert mode. pressing return gets you out of it. 'c' changes one char, 'd' deletes one char. 'x' takes you to end of line, allowing you to e'x'tend it. 'l' redraws the line.

Functions

;ABS: returns the absolute value of num
;ASC: returns the ASCII code of the first character in str
;CHR$: returns a single string character with the ASCII code num
;EOF: returns 0 if the file has data, or -1 if at the end of the file
;INKEY$: returns a character if a key on the keyboard has been pressed, or null if nothing is pressed
;INT: returns the integer portion of num
;INSTR: searches for the first string, in the target str. startpos is optional.
;JOYSTK: returns the position of the joystick axis with a value from 0 to 63
;LEFT$: returns the first num characters of string str
;LEN: returns the length of string str
;MEM: returns the available free memory in bytes
;MID$: returns a sub-string of string str beginning at position start num and length num characters long. Can also reassign by adding ="newvalue"
;PEEK: returns the value of the memory location num
;POINT: returns the color of the semigraphics dot at position x numm and y num
;RIGHT$: returns the end portion of string str beginning at character position num
;RND: returns a random number between 1 and num
;SGN: returns the sign of a number num, 1 if positive, -1 if negative, 0 if 0
;SIN: returns the sine of num in radians
;STR$: returns a string of the number num
;USR: calls a machine language subroutine whose address is stored in memory locations 275 and 276. num is passed to the routine, and a return value is assigned when the routine is done

Commands

;AUDIO : Connects or disconnects cassette audio from the TV sound
;CLEAR variable space: reserves memory for string variables, and optionally, a machine language program
;CLOAD : loads BASIC program from cassette. If no name is specified, the next program is loaded
;CLOADM : loads machine language program from cassette. If no name is specified, the next program is loaded
;CLOSE : closes a device
;CLS: clears the screen. An optional color num can be specified
;CONT: continues a program after pressing BREAK or a STOP statement
;CSAVE : saves a BASIC program to cassette with optional name
;DATA var,var,var...: stores data in a BASIC program for retrieval with the READ command
;DIM variable is assumed. An optional prompt can be printed on the screen for the input statement
;LIST - : lists line of your program. Either start or end can be omitted, or if both are omitted, the entire program will be listed
;LLIST - : works like LIST, but outputs to the printer
;MOTOR : turns the cassette motor on or off
;NEW: erases contents of memory
;ON GOSUB line 1, line 2,... line n: evaluates expression num and calls the numth subroutine listed
;ON GOTO line 1, line 2,... line n: evaluates expression num and jumps to the numth line listed
;OPEN "",device number: opens a device for communication
;POKE memory address, data: writes data into memory address
;PRINT device number,expression: prints data to device specified. If omitted, #0 is assumed
;PRINT @ expression: works like PRINT, but prints at the location specified
;READ variable: reads the next variable from the BASIC program embedded with DATA statements
;RENUM num: renumbers each line of the program at multiples of num
;RESET: sets the semigraphics pixel at location x and y to black
;RESTORE: resets the READ pointer back to the first DATA statement
;RETURN: returns from a subroutine
;RUN num: runs the BASIC program, optionally, at the line number specified
;SET: sets the semigraphics pixel at location x y to color
;SKIPF : skips over BASIC programs on tape until the program name specified is found
;SOUND tone,duration: sounds a tone with frequency and duration
;STOP: causes the program to stop executing
;TAB: tabs to the column specified
;VAL: returns the numeric value of a string that contains a number in string form

Control flow

;GOSUB ': calls the subroutine at the line number specified
;GOTO
': jumps to the program's line number specified
;IF ' THEN ' : performs conditional test. If the test is true THEN commands are executed, otherwise other commands are executed. If the no ELSE is specified, and the test is false, the next line of the program will be run
FOR ' = ' TO '
...
NEXT
'

Error Messages

;/0: division by zero
;AO: file specified is already open
;BS: bad subscript. subscript is out of DIM range
;CN: can't continue
;DD: attempt to redimension an array
;DN: invalid device number
;DS: direct statement error
;FC: illegal function call: function contains a value that is out of range
;FD: bad file data: attempt to read a number into a string value, etc.
;FM: bad file mode, attempt to INPUT data to a file open for OUTPUT, etc.
;ID: illegal direct: the specified command can only be run in a program
;IE: input past end of file. See EOF
;IO: input/output error
;LS: long string: strings can only have 255 characters
;NF: NEXT without FOR
;NO: file not open
;OD: out of data: attempt to read beyond the last DATA in the program
;OM: out of memory
;OS: out of string space: see CLEAR
;OV: overflow: the number is out of range
;RG: RETURN without GOSUB
;SN: syntax error
;ST: string operation too complex
;TM: type mismatch
;UL: attempt to GOTO or GOSUB to a line that doesn't exist

Documented ROM subroutines

There are a few subroutines available for machine language programs in the Color BASIC ROM that are available for general purpose programming.
;POLCAT: address : polls keyboard for a character
;CHROUT: address : outputs a character to screen or device
;CSRDON: address : starts cassette and prepares for reading
;BLKIN: address : reads a block from cassette
;BLKOUT: address : writes a block to cassette
;JOYIN: address : reads joystick values