Color BASIC
Encyclopedia
Color BASIC is the implementation of Microsoft BASIC
that is included in the ROM
of the Tandy/Radio Shack TRS-80 Color Computer
s ("CoCos") manufactured between 1980 and 1991. BASIC
(Beginners All-purpose Symbolic Instruction Code) is a high level language with simple syntax that makes it easy for novices to write simple programs. BASIC is interpreted, that is, decoded as it is run. Because of this, it is simple to edit and debug but performance is significantly lower than a compiled language such as C
or assembly language
.
Numeric variables have only one type, a binary floating point implementation. Each numeric variable consumes 5 bytes of memory and can be in the range from -1E+38 up to 1E+37
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:
Multiple dimensions are separated by commas
+ can be used to concatenate strings or for mathematical addition
- is used for subtraction
* is used for multiplication
/ is used for division
Parenthesis are used to override mathematical order of operation
AND is used for logical 'and' operations
OR is used for logical 'or' operations
NOT is used for logical 'not' operations
For testing, the following operators are used:
is equal to
> is greater than
< is less than
>= is greater than or equal to (also => is acceptable)
<= is less than or equal to (also =< is acceptable)
<> is not equal to (also >< is acceptable)
Other symbols used in BASIC:
" " indicates string data is a constant (static)
: separates multiple commands on a single program line
A semicolon, when encountered in a PRINT function, will cause the output to remain on the same line
A comma, when encountered in a PRINT function, will tab to the next column
str indicates a string expression is required. This can be a static string value (in quotes), a string variable, or other function or expression that returns a string of characters.
device number indicates a device. By default, device 0 (screen and keyboard) is assumed. In Color BASIC, device #-1 (cassette) and #-2 (printer) are available to the programmer.
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.
of num
ASC(str) returns the ASCII
code of the first character in str
CHR$(num) returns a single string character with the ASCII code num
EOF(device number) 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(num) returns the integer portion of num
INSTR(startpos,search str,target str) searches for the first string, in the target str. startpos is optional.
JOYSTK(num) returns the position of the joystick axis (0-3) with a value from 0 to 63
LEFT$(str,num) returns the first ("left") num characters of string str
LEN(str) returns the length (in characters) of string str
MEM returns the available free memory in bytes
MID$(str,start num,length num) returns a sub-string of string str beginning at position start num and length num characters long. Can also reassign by adding ="newvalue"
PEEK(num) returns the value of the memory location num (0-65535)
POINT(x num,y num) returns the color of the semigraphics dot at position x numm (0-63) and y num (0-31)
RIGHT$(str,position num) returns the end ("right") portion of string str beginning at character position num
RND(number) returns a random number (integer) between 1 and num
SGN(num) returns the sign of a number num, 1 if positive, -1 if negative, 0 if 0
SIN(num) returns the sine of num in radians
STR$(num) returns a string of the number num
USR(num) 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
CLEAR variable space[,highest memory location] reserves memory for string variables, and optionally, a machine language program
CLOAD ["name"] loads BASIC program from cassette. If no name is specified, the next program is loaded
CLOADM ["name"] loads machine language program from cassette. If no name is specified, the next program is loaded
CLOSE [device number] closes a device (in Color BASIC this can only be #-1, the cassette)
CLS(num) clears the screen. An optional color num (0-8) can be specified
CONT continues a program after pressing BREAK or a STOP statement
CSAVE ["name"] 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(dimension[,dimension 2,...] dimensions an array and reserves memory space for it
END indicates the end of a BASIC program
EXEC [memory address] executes the machine language program at memory address. If none specified, the execute address of the program loaded off tape is used
FOR
{num} = {number} TO {number} [STEP {number}]
... NEXT (num) creates a loop where the numeric variable (num) runs from start number to end number in increments of number (STEP). If step is omitted, 1 is assumed
GOSUB
line number
} calls the subroutine at the line number specified
GOTO
{line number} jumps to the program's line number specified
IF {test} THEN {command(s)} [ELSE {command(s)}] performs conditional test. If the test is true THEN commands are executed, otherwise (ELSE) other commands are executed. If the no ELSE is specified, and the test is false, the next line of the program will be run
INPUT [device number] [{prompt text};] variable [,variable 2, variable n] Waits for input from device number. If not specified, device 0 (keyboard) is assumed. An optional prompt can be printed on the screen for the input statement
LIST [starting line] - [ending line] lists line(s) of your program. Either start or end can be omitted, or if both are omitted, the entire program will be listed
LLIST [starting line] - [ending line] works like LIST, but outputs to the printer
MOTOR [ON|OFF] turns the cassette motor on or off
NEW erases contents of memory (program and variable)
ON {num} GOSUB line 1, line 2, ... line n evaluates expression num and calls the numth subroutine listed
ON (num) GOTO line 1, line 2, ... line n evaluates expression num and jumps to the numth line listed
OPEN "[I|O]",device number[,"filename"] opens a device for communication
POKE memory address, data writes data (0-255) into memory address (0-65535)
PRINT device number,expression prints data to device specified. If omitted, #0 (screen) is assumed
PRINT @{screen position} expression works like PRINT, but prints at the location specified (0-511)
READ variable[,variable,...] reads the next variable(s) from the BASIC program embedded with DATA statements
RENUM num renumbers each line of the program at multiples of num
RESET(x,y) sets the semigraphics pixel at location x (0-63) and y (0-31) 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(x,y,color) sets the semigraphics pixel at location x (0-63) y (0-31) to color (0-8)
SKIPF ["filename"] skips over BASIC programs on tape until the program name specified is found
SOUND tone,duration sounds a tone with frequency (1-255) and duration (1-255)
STOP causes the program to stop executing
TAB(column) tabs to the column specified (used with PRINT)
VAL(str) returns the numeric value of a string that contains a number in string form
AO file specified is already open
BS bad subscript. subscript is out of DIM range
CN can't continue (see CONT command)
DD attempt to redimension an array
DN invalid device number
DS direct statement error (program has no line numbers)
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 (A$=3, A="CAT")
UL attempt to GOTO or GOSUB to a line that doesn't exist
POLCAT, address [$A000]: polls keyboard for a character
CHROUT, address [$A002]: outputs a character to screen or device
CSRDON, address [$A004]: starts cassette and prepares for reading
BLKIN, address [$A006]: reads a block from cassette
BLKOUT, address [$A008]: writes a block to cassette
JOYIN, address [$A00A]: reads joystick values
Microsoft BASIC
Microsoft BASIC was the foundation product of the Microsoft company. It first appeared in 1975 as Altair BASIC, which was the first BASIC, and the first high level programming language available for the MITS Altair 8800 hobbyist microcomputer....
that is included in the ROM
Read-only memory
Read-only memory is a class of storage medium used in computers and other electronic devices. Data stored in ROM cannot be modified, or can be modified only slowly or with difficulty, so it is mainly used to distribute firmware .In its strictest sense, ROM refers only...
of the Tandy/Radio Shack TRS-80 Color Computer
TRS-80 Color Computer
The Radio Shack TRS-80 Color Computer was a home computer launched in 1980. It was one of the earliest of the first generation of computers marketed for home use in English-speaking markets...
s ("CoCos") manufactured between 1980 and 1991. BASIC
BASIC
BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....
(Beginners All-purpose Symbolic Instruction Code) is a high level language with simple syntax that makes it easy for novices to write simple programs. BASIC is interpreted, that is, decoded as it is run. Because of this, it is simple to edit and debug but performance is significantly lower than a compiled language such as C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
or assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
.
Background
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, 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. ("ECB") Extended BASIC is required for the floppy disk controller, which then gives you Disk Extended Color BASIC. ("DECB")Russian roulette
a color BASIC program from the original Color BASIC manual gives the commands for a Russian roulette gameVariables
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
Numeric variables have only one type, a binary floating point implementation. Each numeric variable consumes 5 bytes of memory and can be in the range from -1E+38 up to 1E+37
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:
Multiple dimensions are separated by commas
Operators and Symbols
Color BASIC provides several operators for both mathematic and, to a lesser extent, string operations.+ can be used to concatenate strings or for mathematical addition
- is used for subtraction
/ is used for division
Parenthesis are used to override mathematical order of operation
AND is used for logical 'and' operations
OR is used for logical 'or' operations
NOT is used for logical 'not' operations
For testing, the following operators are used:
is equal to
> is greater than
< is less than
>= is greater than or equal to (also => is acceptable)
<= is less than or equal to (also =< is acceptable)
<> is not equal to (also >< is acceptable)
Other symbols used in BASIC:
" " indicates string data is a constant (static)
: separates multiple commands on a single program line
A semicolon, when encountered in a PRINT function, will cause the output to remain on the same line
A comma, when encountered in a PRINT function, will tab to the next column
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 (in quotes), a string variable, or other function or expression that returns a string of characters.
device number indicates a device. By default, device 0 (screen and keyboard) is assumed. In Color BASIC, device #-1 (cassette) and #-2 (printer) are available to the programmer.
Edit mode
If you make a mistake typing in a line, you can either retype it from scratch (or DEL it).. 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(num) returns the absolute valueAbsolute value
In mathematics, the absolute value |a| of a real number a is the numerical value of a without regard to its sign. So, for example, the absolute value of 3 is 3, and the absolute value of -3 is also 3...
of num
ASC(str) returns the ASCII
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...
code of the first character in str
CHR$(num) returns a single string character with the ASCII code num
EOF(device number) 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(num) returns the integer portion of num
INSTR(startpos,search str,target str) searches for the first string, in the target str. startpos is optional.
JOYSTK(num) returns the position of the joystick axis (0-3) with a value from 0 to 63
LEFT$(str,num) returns the first ("left") num characters of string str
LEN(str) returns the length (in characters) of string str
MEM returns the available free memory in bytes
MID$(str,start num,length num) returns a sub-string of string str beginning at position start num and length num characters long. Can also reassign by adding ="newvalue"
PEEK(num) returns the value of the memory location num (0-65535)
POINT(x num,y num) returns the color of the semigraphics dot at position x numm (0-63) and y num (0-31)
RIGHT$(str,position num) returns the end ("right") portion of string str beginning at character position num
RND(number) returns a random number (integer) between 1 and num
SGN(num) returns the sign of a number num, 1 if positive, -1 if negative, 0 if 0
SIN(num) returns the sine of num in radians
STR$(num) returns a string of the number num
USR(num) 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 [ON|OFF] Connects or disconnects cassette audio from the TV soundCLEAR variable space[,highest memory location] reserves memory for string variables, and optionally, a machine language program
CLOAD ["name"] loads BASIC program from cassette. If no name is specified, the next program is loaded
CLOADM ["name"] loads machine language program from cassette. If no name is specified, the next program is loaded
CLOSE [device number] closes a device (in Color BASIC this can only be #-1, the cassette)
CLS(num) clears the screen. An optional color num (0-8) can be specified
CONT continues a program after pressing BREAK or a STOP statement
CSAVE ["name"] 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(dimension[,dimension 2,...] dimensions an array and reserves memory space for it
END indicates the end of a BASIC program
EXEC [memory address] executes the machine language program at memory address. If none specified, the execute address of the program loaded off tape is used
FOR
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....
{num} = {number} TO {number} [STEP {number}]
... NEXT (num) creates a loop where the numeric variable (num) runs from start number to end number in increments of number (STEP). If step is omitted, 1 is assumed
GOSUB
GOSUB
GOSUB is a command in many versions of the BASIC computer programming language. A GOSUB statement jumps to a line elsewhere in the program. That line and the following lines up to a RETURN are used as a simple kind of a subroutine without parameters or local variables.The GOSUB command may be used...
line number
Line number
In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line.In the C...
} calls the subroutine at the line number specified
GOTO
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...
{line number} jumps to the program's line number specified
IF {test} THEN {command(s)} [ELSE {command(s)}] performs conditional test. If the test is true THEN commands are executed, otherwise (ELSE) other commands are executed. If the no ELSE is specified, and the test is false, the next line of the program will be run
INPUT [device number] [{prompt text};] variable [,variable 2, variable n] Waits for input from device number. If not specified, device 0 (keyboard) is assumed. An optional prompt can be printed on the screen for the input statement
LIST [starting line] - [ending line] lists line(s) of your program. Either start or end can be omitted, or if both are omitted, the entire program will be listed
LLIST [starting line] - [ending line] works like LIST, but outputs to the printer
MOTOR [ON|OFF] turns the cassette motor on or off
NEW erases contents of memory (program and variable)
ON {num} GOSUB line 1, line 2, ... line n evaluates expression num and calls the numth subroutine listed
ON (num) GOTO line 1, line 2, ... line n evaluates expression num and jumps to the numth line listed
OPEN "[I|O]",device number[,"filename"] opens a device for communication
POKE memory address, data writes data (0-255) into memory address (0-65535)
PRINT device number,expression prints data to device specified. If omitted, #0 (screen) is assumed
PRINT @{screen position} expression works like PRINT, but prints at the location specified (0-511)
READ variable[,variable,...] reads the next variable(s) from the BASIC program embedded with DATA statements
RENUM num renumbers each line of the program at multiples of num
RESET(x,y) sets the semigraphics pixel at location x (0-63) and y (0-31) 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(x,y,color) sets the semigraphics pixel at location x (0-63) y (0-31) to color (0-8)
SKIPF ["filename"] skips over BASIC programs on tape until the program name specified is found
SOUND tone,duration sounds a tone with frequency (1-255) and duration (1-255)
STOP causes the program to stop executing
TAB(column) tabs to the column specified (used with PRINT)
VAL(str) returns the numeric value of a string that contains a number in string form
Error Messages
/0 division by zeroAO file specified is already open
BS bad subscript. subscript is out of DIM range
CN can't continue (see CONT command)
DD attempt to redimension an array
DN invalid device number
DS direct statement error (program has no line numbers)
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 (A$=3, A="CAT")
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 [$A000]: polls keyboard for a character
CHROUT, address [$A002]: outputs a character to screen or device
CSRDON, address [$A004]: starts cassette and prepares for reading
BLKIN, address [$A006]: reads a block from cassette
BLKOUT, address [$A008]: writes a block to cassette
JOYIN, address [$A00A]: reads joystick values