Easy Programming with the TI-99/4A Easy Programming with the TI-99/4A Richard Guenette and James Vogel Birkhiiuser Boston -Basel - Stuttgart Library of Congress Cataloging in Publication Data Guenette, Richard. Easy programming with the TI-99/4A Bibliography: p. Includes index. 1. TI 99/4A (Computer)-Programming. 2. Basic (Computer program language) m. I. Vogel, James, 1952- II. Title. Title: Easy programming with the T.I.-99/4A. QA76.8.TI33G83 1983 001.64'2 83-15714 ISBN-13: 978-1-4615-9815-2 e-ISBN -13: 978-1-4615-9813-8 DOl: 10.1007/978-1-4615-9813-8 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without prior permission of the copyright owner. © Birkhiiuser Boston, Inc., 1984 ABCDEFGHIJ ISBN-13: 978-1-4615-9815-2 Contents Foreword ...................................................................... v Chapter 1 Microcomputers in Brief. ........................................... .. Of Bits and Bytes ................................................ . Storing Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 1 Elementary Computer Architecture. . .. . . . . . .. . . . . . .. .. .. .. . . . . . . . . ... 2 Computer Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 4 Chapter 2 Cas~ette Deck Notes ................................................. 5 Cassette Decks .................................................... 5 Cassette Interface Cable .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 5 Loading and Saving Programs ...................................... , 6 Chapter 3 Keyboard Guide. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 8 Enter Key ........................................................ 8 Shift and Alpha Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 9 Special Symbols .................................................. 10 Mathematical Operators ........................................... 10 Control Keys .................................... " ............... 10 Function Keys.. . . .. . .. . .. . .. .. . .. .. .. . .. . . .. . .. .. . . .. .. . . .. . .. ... 11 Automatic Repetition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 12 Using the Screen Editor .......................... , ......... , ... '" 12 Chapter 4 Introducing TI BASIC .............................................. 15 Commands, Functions, and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 16 Immediate Mode ............................................... 16 Using the PRINT Command ..................................... 17 CALL Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . • . . . . . . . . . . . . .. 17 Command Mode Calculator ...................................... 19 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 19 Functions ....................................................... , 20 Math Functions ................................................ 20 TAB Function .................................................. 21 Statement and Program Lines .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 22 GOTO .......................................................... 23 Chapter 5 Tools for Building Programs ......................................... 26 Line Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 26 RESEQUENCE .................................................. 28 Editing Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 30 LIST .......................................................... 30 Edit Mode .... '.' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 30 Starting and Stopping Programs .................................... 32 Diamond Track ................................................ 33 RUN ........................... '" ........................... 34 CONtinue ..................................................... 34 BREAK and UNBREAK ........................................ 34 END and STOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 36 REMark Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 36 Chapter 6 Working with Numbers .............................................. 37 Order of Arithmetic Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 38 Relational Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 39 Chapter 7 Variables .......................................................... 40 Numeric Variables ................................................ 40 Naming Numeric Variables ......................................... 42 String Variables .................................................. 43 The Role of Variables in Programming .............................. 43 Chapter 8 Using PRINT ...................................................... 45 The PRINT System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 45 Quotation Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 45 Print Separators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 47 The TAB Function ............................................... 49 The Sick Rose. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 50 Chapter 9 Branching Statements ............................................... 52 The Unconditional Branchers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 53 GOTO ........................................................ 53 GOSUBroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 54 Conditional Branching Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 56 ON-GOTO .................................................... 56 ON-GOSUB ................................................... 58 IF-THEN-ELSE ................................................ 59 Chapter 10 Data Anyone? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 62 INPUT .......................................................... 62 The READ/DATA Statements ...................................... 65 Setting Up a Data Bank. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 67 Checking Variable Status .......................................... 68 Multiple-Variable READ/DATA Statements .......................... 69 RESTORE ....................................................... 70 Using Counters To Manipulate Data ................................ 73 Using Data Flags ................................................. 74 Chapter 11 The FOR-NEXT Loop .............................................. 77 Entering Data with FOR-NEXT Loops .............................. 78 Defining Loops With STEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 79 Nested Loops .................................................... 81 Chapter 12 Debugging Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 85 Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 86 TRACE and UNTRACE .......................................... 87 PRINT Debug ................................................... 88 Chapter 13 Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 90 INTeger ......................................................... 90 RANDOMIZE and the Random Number Function - (RND(X» ......... 90 Other Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 93 ABS(X) ....................................................... 93 ATN(X) ....................................................... 93 COS(X) ....................................................... 93 SIN(X) ........................................................ 94 TAN(X) ....................................................... 94 EXP(X) ....................................................... 94 LOG(X) ....................................................... 94 SQR(X) ....................................................... 94 SGN(X) ....................................................... 94 User-Defined Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 94 Chapter 14 Computer Sound and Music. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 98 Tpe Sound Chip. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 98 CALLing Sound. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 100 Noise Settings ................................................... 100 Negative Duration Values ......................................... 101 Programming a Song for One Voice ............................... 102 RESTORE for Repeats ........................................... 104 A Song for Three Voices ......................................... 105 Sound Effects ................................................... 108 Chapter 15 BASIC Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 110 The Screen ..................................................... 110 BASIC Graphics Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. III CALL CHAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. III CALL CLEAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 113 CALL HCHAR and CALL VCHAR. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 113 CALL COLOR ............................................... 115 CALL SCREEN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 117 The RANDOM Character Generator ............................... 119 Combining Characters in Space: White Knight. . . . . . . . . . . . . . . . . . . . . .. 123 Combining Characters in Time: Running Man. . . . . . . . . . . . . . . . . . . . . .. 124 Chapter 16 Interacting with Your Computer: Keyboard and Joystick ................ 126 CALL KEYboard ................................................ 126 Key-Unit ...................................................... 127 Return Variable ............................................... 127 Status Variable ................................................ 128 Mazemaker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 130 CALL JOySTick ................................................ 133 Joystick Mazemaker ........................................... 134 Chapter 17 Arrays........................................................... 136 Subscripted Variables and Simple Arrays ............................ 137 Using READ/DATA To Load Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 139 OPTION BASE 1 and the DIM Statements ......................... 139 Two-Dimensional Arrays ......................................... 142 Chapter 18 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 147 ASCII VaIue-ASC .............................................. 147 Character-CHR$ ............................................... 148 Value-VAL .................................................... 150 String Number-STR$ ........................................... 151 Length-LEN ................................................... 152 Position-POS .................................................. 152 String Segment-SEG$ ........................................... 154 Chapter 19 More Graphics .................................................... 156 FOR-NEXT Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 156 CALLing COLOR ............................................... 157 Strings and String Functions ...................................... 160 Using PRINT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 160 Using SEGment and LENgth String Functions ..................... 161 Printing Text with HCHAR ..................................... 162 Using Arrays in Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 163 Chapter 20 Live Time on the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 167 Links in a Chain ........................................ , ....... 171 Chapter 21 Your Home Computer as a Terminal ................................. 176 Chapter 22 Syste~ Options. . . . .. .. . . . .. . . . .. . . . . . . . . . . . . . .. . . . . . . . . . . . . .. . . . .. 179 Extended BASIC ................................................ 179 Speech Synthesizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 179 Peripheral Expansion Box ........................ '. ................ 180 RS232 Card ................................................... 180 Disk Drive Controller Card ..................................... 180 32K Memory Expansion Card ................................... 180 P-Code Card. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 180 Disk Drives ..................................................... 181 Printers ..•..................................................... 181 Telephone Modems .............................................. 182 The Fully Configured System. . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . . . . ... 182 Alternatives to the Peripheral Expansion Box System ................. 182 Word Processing ................................................ 182 Microsoft Multiplan™ ............................................ 182 UCSD p-System™ .... , ........................................... 183 LOGO ......................................................... 183 Machine Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 183 FORTH ........................................................ 184 Voice Recognition ............................................. 184 Winchester Hard Disk .......................................... 184 Chapter 23 Resource List ..................................................... 185 Foreword Welcome to the world of computer programming. Your Texas Instruments TI-99/4A is a real 16-bit microcomputer, complete with sound and color graphics. Making these features do what you want may seem impossible at fIrst, but don't worry; there's nothing mysterious about computer programming. Learning to program computers simply means learning a new language-in this case, TI BASIC (for Beginner's All purpose Symbolic Instruction Code), a version of the most popular language used on today's microcomputers. This book will teach you, step by step, how to tell your machine what you wish it to do-in other words, how to program it. But that's not all. You'll also find chapters on general microcomputer principles, cassette deck use, the TI-99/4A as a terminal for much larger systems, options for expansion, and a list of resources for getting the most out of your home computer. With the 99/4A you have access to a large library of programs, or software, already written by someone else. Without knowing anything about programming, you can use this software to play games, learn math, or store addresses. But at some point you'll want to make your personal computer really personal. You might want to create your own video game or compose a tune. And that's when you'll want to learn programming. Learning to program is like learning to ride a bicycle: you must over come the fear of falling. Once you do, you move right along. Once you overcome the fear of indecipherable computerese, you'll move right along in programming, too. And, as with bicycle riding, you learn by doing. To get the most out of this book, you'll need your computer, a TV set (preferably color), a cassette tape recorder and connecting cable, your User's Reference Guide, and the confidence that you can do it. Remember: computers, personal or otherwise, are machines. They are tools, extensions of the people who use them. Your TI-99/4A is an ex tension of you, and the programs you write are extensions of your own imagination. So let's begin. Easy Programming with the TI-99/4A 1 Microcomputers in Brief This chapter introduces microcomputers: their structure, methods of storing information, and languages. You'll find this information useful but not absolutely necessary for understanding how to program your TI-99/4A. If you're eager to grapple with the machine on your desk, skip right ahead. OF BITS AND BYTES On the most basic level, computers process information using numbers having only one digit. These numbers are called binary numbers; each digit is called a bit (short for "binary digit''). The value of a single bit can either be 1 or O. To the computer, a 1 means an electrical "on"; a 0 means "off." All computer operations are ultimately manipulations of these binary numbers. The basic building blocks, bits, are in turn assembled to form bytes. A byte is a series of eight bits. Each byte is equivalent to a letter or number, a keyboard symbol, or single character space in a page of text. The word ''byte,'' for instance, would occupy four bytes. The byte representing the letter "A" is composed of the bits 0100000 1. Every time you press the letter "A" the computer translates it into this series of bits. STORING INFORMATION Computers store information in different kinds of memory. One kind is called random access memory, or RAM. RAM is like a blank sheet of paper that the computer allows you to write instructions, or programs, on. There are two types of RAM, static and dynamic. Static RAM, found 1