CHAPTER TWO : TI BASIC
In this section we will look at the language in your console.
There are a number of general books now available on the BASIC
language, and one or two of these may help you if you experience
difficulty in handling the language. Many evening classes are also
Do not try to take in all the information in one reading, but go
back and read it again a few times.
A computer works as a large number of switches, which are either
on or off. Each 'switch' is described as a BIT. In order to pass
information more quickly, the computer looks at more than one BIT
at a time. The TI99/4A uses a 16 bit processor: it is able to look
at 16 bits at a time. For most purposes however, the computer
looks at 'words' composed of 8 bits. These words are called BYTES.
A BYTE is a binary number composed of eight numbers, which may be
0 or 1. In digital representation the BYTE has a maximum value of
255 (Binary 11111111).
The computer stores its commands (reserved words) as one byte,
rather than a collection of letters. It can only identify the
command words if you follow the rules regarding the characters
permitted in front of and following command words. In general, you
may only use a space, arithmetic operator, or ENTER, but there are
exceptions which you will see in the program listings in the
manual and in the books of TI programs now available.
For discussion of the error tracing commands (Trace,
Untrace,Break) see the section on How to use TI Basic.
The command LIST is used to list the contents of a program. Used
on its own,it will list the program on the screen. You may use
CLEAR (FCTN and 4) to halt the LIST. To start again at say line 400,
you type in LIST 400- the hyphen indicating 'to the end', or LIST
LIST can also be used in many other ways. These are described in
the section on modules and peripherals.
LET is optional, but uses up one byte of memory every time
you use it. It is better not to.
LET A=2 is the same as
END is also optional, but in this case it is good practice
to use it. By adding END to your program, you may be certain when
you list it in the future, that you have the complete program, and
not a 'working copy'.
Try to use a separate INPUT for each variable. It IS possible to
input more than one variable eg by using INPUT A,B but this
requires the program user to input two numbers separated by a
The TI form of input, INPUT "HOW MANY?":N uses a colon separator
(:), most other Basics use a semi colon (;).
COLOUR AND SOUND
The TI99/4A allows you to set the screen, and the foreground and background colours of the characters, to any of fifteen colours, plus transparent. The transparent colour allows the screen colour to show behind a character.
CALL SCREEN is used to set the screen colour, and
CALL COLOR is used to set the character colours.
Note that COLOR is spelt the American way.
100 FOR N=1 to 8
110 CALL HCHAR(N,1,24+N*8,32)
120 NEXT N
130 FOR N=1 TO 16
140 CALL SCREEN(N)
150 FOR DELAY=1 TO 300
160 NEXT DELAY
170 CALL COLOR(N,N,1)
180 NEXT N
In BASIC, characters are referred to by standard codes referred
to as ASCII CODES. The ASCII code for the capital letter A is 65
for instance.These codes may simply be referred to as CHARACTER
TI BASIC allows you to define characters with the ASCII codes 32
to 159, and for colour purposes these are divided into sixteen
sets. You may define different colours for each set, but all of
the characters in that set must be the same colour.
Characters are defined with CALL CHAR(CODE,STRING$),where
STRING$ is a string or string variable made up of HEXADECIMAL
characters (0 to 9 plus A to F).
As each character occupies a grid of 8 x 8 dots, it can be
defined by splitting it down the middle to form 16 rows of 4 dots.
Each possible combination of ON and OFF dots in a row of four
can be defined in terms of one of the sixteen hexadecimal
A character with one dot ON in the top right corner is defined
The definition is by row, first the left side then the right.
Each row of 4 dots can be considered a row of binary switches.
The right switch is 1, the next 2, the next 4 and the leftmost
switch 8. When a dot is ON, add its value to the others which are
ON in the same row. You will obtain a unique number, from 0 to 15.
From this decimal number you change to a single hexadecimal
digit (hexadecimal numbers have a 'base' of 16). The number 15 for
instance is hexadecimal F.
You may purchase the EXTENDED BASIC module at some future
date. As TI BASIC programs run faster in EXTENDED BASIC, it is
worth noting that that language has only 14 character sets.
Therefore,if you use characters coded 144 to 159 in your TI
BASIC programs, the programs will not run in Extended Basic.
There are several versions of the console around, and there have
been slight changes in the relative shades of the colours used. If
you purchase a program and the colours seem awful, it was probably
written on a 99/4 or an NTSC 99/4A...you should be able to change
the colours to something more suitable, and it will give you good
programming exercise looking for the lines to amend.
If you wish to place a single character on the screen, use the
CALL HCHAR command, it is slightly faster than CALL VCHAR.
Sound is produced with CALL SOUND(TIME,F1,V1,F2,V2,F3,V3,N,V4)
where TIME is in milliseconds, F1,F2 and F3 are FREQUENCY in Hertz
(cycles per second) and V1,V2 and V3 are volume (0 loudest, 30
quietist). N is a noise generator which provides some sound
effects.[N & V4 WILL NOT PRODUCE TI EFFECT IN TI EMULATOR]
A CALL SOUND may use only one frequency if you wish: the second
and third frequencies are optional and may be omitted. The Noise
is also optional.
A CALL SOUND will occupy the computer for about 50 milliseconds,
and then, even if the sound is still continuing, it will proceed
with the next instruction. If it comes to another CALL SOUND, the
computer will wait until the first has finished, unless the second
CALL SOUND has a negative time, in which case the first CALL SOUND
will immediately be terminated and the second CALL SOUND begin.
TI state that you can only have tones down to 110 Hz on your
computer, but that is not quite the case:
100 INPUT A
110 IF A<37 THEN 100
120 CALL SOUND(2000,200,30,2
130 CALL SOUND(500,200,30)
140 GOTO 100
[THIS WILL NOT WORK IN TI EMULATOR WHICH HAS ONLY 3 VOICES]
[AT WORST USING NOISE WILL PRODUCE A RACKET]
It would appear that the console can at least appear to go well
below 110Hz. Try an input of say 50 or 60. If you find the sound
interesting, try changing the -4 to -8.
Keep in mind that the computer takes about 40 milliseconds to
process a CALL SOUND command.It is not possible to use this
command to change the TYPE of sound produced.You cannot produce a
piano or harpsichord sound for instance. =====
Please note that when using the joysticks, the alphalock key
must be in the up position. If alphalock is down, the computer
will not be able to sense when the joystick is pushed upwards.
Using Call Key(3...) does not affect joystick operation, but you
should not use CALL JOYST(3...) as this may prevent correct
operation of the joystick.
ATN is a trigonometrical function which you may not need to use
often, but in TI BASIC it may be used to obtain an accurate value
for the mathematical constant PI:
This and the other trigonometric functions provided work in
radians. You may convert radians to degrees by using ATN:
DEGREES=RADIANS*180/4*ATN(1), or more simply
INTEGER is a numeric function you will use quite often. It
removes the fraction from a number, so that 2.3 for instance
becomes 2. It is frequently used with the RND function, and can
also be used to round decimal numbers.
For example, if B is a decimal number to 13 places, and you wish
to print only the first two places, you could use:
If you wanted the last decimal to be 'rounded', the alternative
SQR is used to obtain the SQuaRe root of a number:
Many computers will not equate SQR(4)=2, or fail on some other
comparison, due to internal rounding of numbers. Your 99/4A will
equate all ten squares up to 100. Try it on a friends computer.
(webnote: this was written in 1983)
You may not need to use this very often, but it is an indication
of the numeracy of the 99/4A. Try:
100 FOR I=1 TO 100
120 IF I=A*A THEN 130 ELSE 140
130 PRINT I;"PASSED THE TEST
140 PRINT "NEXT VALUE OF I"
150 NEXT I
NOTE that 1,2,4,9,16,25,36,49,64,81 and 100 pass the test OK.
The failures are due to internal rounding, which still exists, but
it is not quite so marked on the 99/4A as on other computers .
If your program needs to make a comparison such as this, use the
INT function to remove any (unprinted) fraction.
In the program above for instance,there is some improvement by
using INT(A*A) in line 120.
There is a bigger improvement using
which will remove from A any invisible fraction.
The POS function is rarely used on other computers, but enables
you to program very concisely on your 99/4a.
In the following example,CALL KEY is used to detect whether keys
A B C or D are pressed, and control is passed accordingly.
First,without using POS:
100 CALL KEY(0,K,S)
110 IF K=65 THEN 200
120 IF K=66 THEN 250
130 IF K=67 THEN 300
140 IF K=68 THEN 350
150 GOTO 400
In this case, the keys have adjacent ASCII codes, and it would be
possible to use:
110 IF S=0 THEN 400
120 IF (K<65)+(K>68) then 400
130 ON K-64 GOTOo 200,250,300
omitting 140 & 150.
However, in many games you may wish to test for keys which are
well spread, such as AKESDXQP. The POS function can then offer
the solution. Still using ABCD:
100 CALL KEY(0,K,S)
110 IF S=0 THEN 400
120 ON POS("ABCD",CHR$(K),1)
+1 GOTO 400,200,250,300,350
If the key pressed is not in the string used in POS, then the
expression has a value of zero, so one is added to enable us to
use ON..GOTO,and the first transfer occurs if an unwanted key is
pressed. In this case 'only' three lines have been saved, but if
you wish to use more valid keys, you still only need to use three
lines. This can be very useful in a program.
Although you may use a string up to 255 characters long, the POS
function is unreliable for strings longer than 127 characters.
SEG$ is used when you wish to print a SEGment of a string, or
remove a part of a string.
TI BASIC uses only one command to segment strings, SEG$. Other
computers use LEFT$, RIGHT$, and MID$, but you only really need
It is used for instance in this DISPLAY AT routine taken from
THE TEXAS PROGRAM BOOK.
110 REM PRINT AT X,Y,M$
120 REM ROUTINE
140 FOR J=1 TO LEN(P$)
150 IF Y<32 THEN 180
180 IF X<24 THEN 200
210 CALL HCHAR(X,Y,CH)
230 NEXT J
(Set X and Y to the start position of your word, placed in M$.
Then GOSUB this routine, and remember to add RETURN at the end
to go back to the place in your program you left).
VAL is intended to make a number contained in a string available
as a number, for use in mathematical operations. It changes "2"
into 2, and may be used NO=VAL("2"). It is the opposite of STR$,
used to change a number into a string:
The TI VAL function will change a string such as "2" to the
numeric variable 2.
For instance: A=VAL("123")
This is of great importance when memory space is short, as a
string variable representing "2" uses less memory than a numeric
variable representing 2. This is explained in the section on
Please note that the TI VAL will only work if the string
contains numbers only. It will not function for numeric
expressions such as "2*3+8" nor if alphabetical letters are used
such as "12 APPLES".
-It is better NOT to use a proportional font to print listings!
This TI BASIC program simulates two puzzles
The screen is used as a memory device, with
CALL GCHAR used to find out what is in a
particular position, and then the information
is manipulated and new characters displayed.
The program can be speeded up by using a 6x6
array to hold the information and using that
instead of GCHAR. This program will work:
can you make it work better?
100 REM SQUARES S SHAW 1981
4X4 IS BEST
120 DEF RAN(X)=CNT(X*RND)+1
130 GOSUB 2200
140 GOSUB 2100
150 GOSUB 1340
160 GOSUB 980
170 FOR C=8 TO 6+2*V STEP 2
180 FOR C2=5 TO 2*H+3 STEP 2
190 CALL GCHAR(C2,C,C4)
220 IF C4=32 THEN 260
230 NEXT C2
240 NEXT C
260 IF SCR=0 THEN 300
280 IF SCR=2 THEN 360
300 CALL KEY(0,A,B)
310 IF A=ASC("P")THEN 130
320 CALL HCHAR(15,22,63)
330 CALL HCHAR(15,22,32)
340 IF B<1 THEN 260
360 IF (H=4)*(KEY>5)THEN 260
370 ON KEY GOTO 260,390,450,
390 REM ^
400 IF POSR=5 THEN 260
410 CALL GCHAR(POSR-2,POSV,C3)
420 CALL HCHAR(POSR-2,POSV,32)
430 CALL HCHAR(POSR,POSV,C3)
440 GOTO 170
450 REM DOWN
460 IF POSR=15 THEN 260
470 IF (H=4)*(POSR=11)THEN 260
480 CALL GCHAR(POSR+2,POSV,C3)
490 CALL HCHAR(POSR+2,POSV,32)
500 CALL HCHAR(POSR,POSV,C3)
510 GOTO 170
520 IF H=6 THEN 580
530 IF POSV=8 THEN 260
540 CALL GCHAR(POSR,POSV-2,C3)
550 CALL HCHAR(POSR,POSV-2,32)
560 CALL HCHAR(POSR,POSV,C3)
570 GOTO 170
600 FOR C=8 TO 18
610 CALL GCHAR(C5,C,C2)
630 NEXT C
650 FOR C=8 TO 18
660 CALL HCHAR(C5,C,ASC(SEG$
670 NEXT C
680 GOTO 170
690 IF H=6 THEN 750
700 IF POSV=14 THEN 260
710 CALL GCHAR(POSR,POSV+2,C3)
720 CALL HCHAR(POSR,POSV+2,32)
730 CALL HCHAR(POSR,POSV,C3)
740 GOTO 170
770 FOR C=8 TO 18
780 CALL GCHAR(C5,C,C2)
800 NEXT C
820 FOR C=8 TO 18
830 CALL HCHAR(C5,C,ASC(SEG$
840 NEXT C
850 GOTO 170
880 GOTO 600
910 GOTO 770
940 GOTO 600
970 GOTO 770
980 T$="PRESS APPROPRIATE KE
1010 GOSUB 2260
1020 T$="MOVE BLANK SQUARE"
1050 GOSUB 2260
1060 IF V<5 THEN 1110
1070 T$="OR SLIDE ROWS "
1100 GOSUB 2260
1110 T$="1. UP 2. DOWN"
1140 GOSUB 2260
1150 IF V>5 THEN 1210
1160 T$="3. LEFT 4. RIGHT"
1190 GOSUB 2260
1210 T$="3.TOP< 4.TOP> 5.2
1240 GOSUB 2260
1250 T$="7.3< 8.3> 9.4
1280 GOSUB 2260
1290 T$="Q.5< W.5> E.6
1320 GOSUB 2260
1340 CALL CLEAR
1350 REM DRAW
1360 FOR C=7 TO 7+2*V STEP 2
1370 FOR C2=4 TO 4+2*H STEP 2
1380 CALL HCHAR(C2,C,98)
1390 NEXT C2
1400 NEXT C
1410 FOR C=8 TO 6+2*V STEP 2
1420 FOR C2=4 TO 4+2*H STEP 2
1430 CALL HCHAR(C2,C,96)
1440 NEXT C2
1450 NEXT C
1460 FOR C=7 TO 7+2*V STEP 2
1470 FOR C2=5 TO 3+2*H STEP 2
1480 CALL HCHAR(C2,C,97)
1490 NEXT C2
1500 NEXT C
1510 IF V<5 THEN 1740
1520 FOR C=8 TO 18 STEP 2
1530 FOR C2=5 TO 15 STEP 2
1540 CALL HCHAR(C2,C,45+C/2)
1550 NEXT C2
1560 NEXT C
1570 CALL HCHAR(11,14,32)
1590 T$=" WAIT-RANDOMISING"
1620 GOSUB 2260
1630 FOR Y=1 TO 32
1660 GOSUB 170
1690 GOSUB 170
1700 GOTO 1710
1710 NEXT Y
1760 FOR C=5 TO 11 STEP 2
1770 FOR C2=8 TO 14 STEP 2
1780 CALL HCHAR(C,C2,C3,1)
1800 NEXT C2
1810 NEXT C
1820 CALL HCHAR(11,14,32)
1840 T$=" WAIT-RANDOMIZING"
1870 GOSUB 2260
1880 FOR Y=1 TO 55
1910 GOSUB 170
1920 NEXT Y
1950 PRINT "SQUARES"::"STEPH
EN SHAW 1981":"PRESS '1' OR
1960 PRINT "1. 6X6 PROBLEM":
"2. 4X4 PROBLEM"
1970 PRINT "FOLLOW DIRECTION
S AT BOTTOM OF SCREEN AFTER
DIAGRAM":"HAS BEEN DRAWN"
1980 CALL KEY(0,A,B)
1990 IF A=49 THEN 2050
2000 IF A=50 THEN 2010 ELSE 1980
2030 CALL CLEAR
2070 CALL CLEAR
2090 CALL CLEAR
2100 PRINT "SQUARES 1981"
::"STEPHEN SHAW STOCKPORT":"
THE OBJECT IS TO RESTORE "
2110 PRINT "THE ORIGINAL PAT
TERN OF ":"SQUARES,WHICH TH
E COMPUTER":"HAS SCRAMBLED"
2120 PRINT "USING THE COMMAN
DS AVAILABLE.":"IN BOTH PUZZ
LES THE BLANK":"MOVES UP OR
2130 PRINT "IN THE 6X6 PUZZL
E":"IT CANNOT MOVE > OR < BU
T": "THE WHOLE ROW SLIDES "
2140 PRINT "WATCH AS THE COM
PUTER":"SCRAMBLES THE ORIGIN
AL":"PATTERN TO SEE HOW IT":
2150 PRINT "(BEING RANDOM YO
U MAY END":"UP BACK AT THE S
2160 PRINT "PRESS KEY P TO P
LAY AGAIN":"WHEN YOU HAVE CO
2170 INPUT "PRESS ENTER":T$
2180 CALL CLEAR
2190 GOTO 1950
2200 CALL CLEAR
2210 CALL CHAR(96,"000000FF")
2220 CALL CHAR(97,"101010101
2230 CALL CHAR(98,"101010FF1
2240 CALL SCREEN(12)
2260 FOR G=1 TO LEN(T$)
2270 CALL HCHAR(R,VR+G,ASC(S
2280 NEXT G
2320 GOTO 600
2350 GOTO 770
2380 GOTO 600
2410 GOTO 770
2440 GOTO 600
2470 GOTO 770
THE INFORMATION IN THIS BOOK IS FREE. It may be copied, ditributed and/or modified under the conditions set down i the Design Science License published by Michael Stutz at http://dsl.org/copyleft/dsl.txt