Manual / Guide · 1983
Commodore VIC-20 Programmer's Reference Guide
'Using a light pen on the VIC': pin 6 / user port pin 7 and registers $9006/$9007.
- vic-20
- light pen
- vic chip
Manual / Guide · 1983
'Using a light pen on the VIC': pin 6 / user port pin 7 and registers $9006/$9007.
An all-purpose reference guide for first-time computerists as well as experienced programmers! z. commodore COMPUTER VM110 I I I I Reference A. Finkel N. Harris P. Higginbottom M. Tomczyk Published by Commodore Business Machines, Inc. and Howard W. Sams & Co., Inc. I FIRST EDITION Copyright = 1982 by Commodore Business Machines. Inc. ,.„__ [ All rights reserved. . No part of this publication may be reproduced, siored in a retrieval system, or transmitted in any form or by any means,electronic, mechanical, photocopying, recording or otherwise without the prior written permission ot Commodore Business Machines. Inc. : TABLE OF CONTENTS INTRODUCING THE PROGRAMMER'S REFERENCE GUIDE VIC 20 APPLICATIONS GUIDE v« 1 BASIC PROGRAMMING REFERENCE GUIDE VIC BASIC; The Language of the VIC Commands 5 Statements 14 I/O Statements BASIC Functions 35 40 Numbers and Variables 54 Operators 62 Logical Operators 68 PROGRAMMING TIPS 71 • How to Crunch BASIC Programs 79 • Working With Graphics 82 Character Memory 82 Programmable Characters High Resolution Graphics 82 88 Multi-Color Mode Graphics 92 Superexpander Cartridge 94 I 2 I • • • 3 1 3 Editing Programs Using the GET Statement Sound and Music 73 77 95 MACHINE LANGUAGE PROGRAMMING GUIDE 107 • System Overview 109 • Introduction to Machine Language 123 • Writing Your First Program 132 iii I Memory Maps 170 Useful Memory Locations 178 The KERNAL 182 KERNAL Power Up Activities 211 VIC Chips 212 6560 (Video Interface Chip) 212 6522 (Versatile Interface Adapter) 218 INPUT/OUTPUT GUIDE • 227 User Port 229 The Senaf Bus 234 Using the VIC Graphic Printer VIC Expansion Port 236 241 Game Controllers 246 Joystick 246 Paddfes 248 Light Pen 250 RS-232 Interlace Description APPENDICES 251 261 A, B. Abbreviations for BASIC Keywords Screen & Border Color Combinations C> Table of Musical Notes 263 265 266 D. Screen Display Codes 267 E. Screen Memory Maps 270 F. ASCII and CHRS Codes 272 G. Deriving Mathematical Functions 275 H. Error Messages 276 I. J. Converting Programs to VIC 20 BASIC Pinouts for Input/Output Devices 278 280 K. VIC Peripherals & Accessories 284 INDEX 285 SCHEMATIC 291 INTRODUCING... THE PROGRAMMER'S REFERENCE GUIDE! The Friendly Computer deserves a Friendly Reference Book. That's why we wrote the VIC 20 PROGRAMMER'S REFER ENCE GUIDE ... a book that gives you more information about your VIC 20 Personal Computer than any other source. This guide was compiled from the experience of Commodore's international programming staffs in more than half a dozen countries, and is designed to be used by first-time computerists as well as experienced programmers. To cover the areas VIC 20 programmers are most interested in, we divided the book into four sections: BASIC Programming, Machine Language Programming, Input/Output Interfacing and Programming Graphics & Sound. Here are just a few of the ways the VIC 20 Programmer's Reference Guide helps meet your programming needs: —Our complete "dictionary" includes not only BASIC com mands but also sample programs to show you how they work. —Need an introduction to Machine Level Programming? Our laymen's overview gets you started. —The exclusive Kernal helps assure the programs you write today won't be outdated tomorrow. —The VIC's Interface section lets you expand yourcomputer. . . from RS232 for telecommunications to joysticks, game paddles and lightpens. —You'll have fun learning about the VIC's graphic, sound and music capabilities . . . including the unique "multicolor" mode. —You'll discover POKEs you never knew about, and probably PEEK into some memory locations you never knew existed. There are lots of fascinating hours ahead of you. Let the Programmer's Reference Guide be your companion as you continue to explore your VIC 20 Personal Computer System. And ... if you find any errors in this book, please send us a postcard or letter in care of VIC PROGRAMMER'S REFERENCE GUIDE, VIC Product Marketing Group, Commodore Business Machines, inc., 681 Moore Road, King of Prussia, PA 19406+ We'd appreciate your assistance in helping us "debug" our reference guide for future printings. Enjoy your new reference guide . . . and happy programming! —Toe Authors i i i i v. [ ! I I ! I L VIC 20 APPLICATIONS GUIDE When you first considered buying a computer, the chances are you said something like, "I know computers are good things to have and it's nice thatthey're finally affordable, but. . .what can I do with one?" The great thing about a computer is that you can tailor the machine to do what you want it to—you can make it calculate your home budget, play arcade-style action games—you can even make it talk! And the best thing is, if your VIC 20 does only ONE of the things listed below, it's well worth the price you paid for it, Here then, is a list of applications for your VIC 20—in case you've asked yourself, "Yes, but what else can I do with it?" APPLICATION COMMENTS/REQUIRE MENTS ADVENTURE COMMODORE GAMES Adams Adventure games on car provides 5 Scott tridge, decoded to "talk'1 with the VOTRAX "Type N Talk11^. ADVERTISING & Hook the VIC to a television and put MERCHANDISING it in a store window with an animated message flashing and you've got a great point of purchase store dis play. ANIMATION The VIC is well-suited to screen animation . . , a special aid called THE PROGRAMMABLE CHARAC TER SET & GAMEGRAPHICS EDITOR is available from COM MODORE on tape cassette. BABYSITTING The VIC HOME BABYSITTER car tridge can keep your child occupied for hours and teach keyboard sym bols, special learning concepts and relationships. A "first" from COM MODORE. x BASIC The PROGRAMMING TEACH YOURSELF PROGRAM VIC owner's guide and the MING series of books and tapes are excellent starting points. A PRO GRAMMERS AID CARTRIDGE is i available from COMMODORE. BIORHYTHM COMMODORE'S CHARTING gram on tape has a special compati Biorhythm pro bility leature which lets you compare yourself to anyone else by simply typing in your birthdates. CHESS GAME SARGON II (on cartridge from COMMODORE) has been called the most powerful microcomputer chess program anywhere. COLLECTIONS COMMODORE will provide a car tridge which allows record their collections collectors i to (stamps, coins or other items) on tape or lloppy diskettes, and print out these lists on the VIC GRAPHIC PRINT ER. COMMUNICATION VICMODEM1-, VICNET[m and VICTERM^ are ail products which allow VIC owners to communicate by telephone with other computer owners, or telecomputing services like CompuServe1rn or The Source*1. COMPOSING The VIC's 3 tone generators cover 5 SONGS octaves and may be used to write i and record music. The best music writing accessory is the SUPEREXPANDER CARTRIDGE which lets you write music in note form and save it on tape or disk. DEXTERITY Hand-to-eye coordination and man ual dexterity are aided by several of COMMODORE'S VIC games . . . including the "Jupiter Lander night driving others. simulations, and among i ! EDUCATION The COMMODORE Educational Computing i tains Resource information on Book con educational uses of computers in general as welt as educational software lists for the VIC 20. Available through COM MODORE computer dealers. EXPENSE A CALENDAR/EXPENSE REC RECORDS ORD tape is offered by COMMO DORE, FOREIGN The VIC Programmable Character LANGUAGE Set Editor lets any user replace the VIC character set with user-defined foreign language characters. i FORMULA/FIGURES The VIC has the same powerful math routines built into its operating system as the COMMODORE PET/CBM microcomputers. Com plex formulas may be calculated quickly and easily either directly or under program control (see the OPERATORS section of the VIC user manual and/or the matching section in this book). GAMBLING COMMODORE provides several games which provide hours of gam bling fun without risking any money I . . . programs like VIC21 Casino Style Blackjack (on tape), SUPERSLOT (cartridge) and DRAW POKER {cartridge). GAMES Everything from space games on cartridge to Blackjack on tape, plus REAL ARCADE games adapted from the most popular coin-operat ed games in the world. GRAPHICS The SUPEREXPANDER CAR PLOTTING TRIDGE offers 3K memory expan sion, hi-resolution mulii-color graphics platting, easy function key definition, and musicwriting com mands ... all in one cartridge. I xi HOME INVENTORY The HOME INVENTORY tape in i COMMODORE'S HOME CALCU LATION SIXPACK provides a tow priced method for storing and up dating lists of belongings for insur ance purposes, business purposes, etc. INSTRUMENT The VIC has a serial port, RS-232 CONTROL port and IEEE-4888 adapter car tridge for use in a variety of special industrial applications. JOURNALS OR The VIC is excellent for making daily CREATIVE journal entries, using the VIC TYPE WRITING WRITER or VICWRITER. Informa tion can be stored on the ! VIC DATASSETTE tape recorder or VIC DISK DRIVE, and printed out on a VIC GRAPHIC PRINTER. LIGHTPEN Applications CONTROL specify items can use any commer using a lightpen to I cial llghtpen which fits the VIC game port connector makers , . . market at least lightpens two which work with the VIC. LOAN/MORTGAGE Try the LOAN/MORTGAGE CAL CALCULATION CULATOR from COMMODORE. MACHINE CODE COMMODORE'S PROGRAM PROGRAMMING MERS REFERENCE GUIDE in cludes a machine language section. The VICMONlm machine I language monitor cartridge is recommended. VfC machine may also language be written in programs assembly language on the PET/CBM using 1he COMMODORE Assembler Develop ment System. MATH PRACTICE Several software companies offer TOOL educational programs on tape for the VIC. COMMODORE'S first math practice program, called "SPACEMATH," is available on tape. Xii I NETWORKING & DISTRIBUTED PROCESSING Networking may be achieved by using the VIC as part of a telephone or RS-232 hookup, or by using a commercially available network system. PAYROLL & FORMS PRINTOUT The VIC can be programmed to handle a variety of entry-type busi ness applications. case letters Upper/tower combined with VIC "business form" graphics make it easy to design forms, which can be easily printed out on the VIC GRAPHIC PRINTER. PERSONAL BUDGET COMMODORE provides PERSON AL FINANCE programs on tape and on plug-in cartridge. PORTFOLIO ANALYSIS Business software which performs this function is available as printed programs in books available from most computer stores. This service is also available through telecom puting services. PRINT INFORMATION ON PAPER The VIC GRAPHIC PRINTER prints letters, numbers and graphics in high quality dot matrix format. RS232 PRINTERS including letter quality printers may also be used with the proper interfacing. An IEEE 488 INTERFACE cartridge may also allow IEEE printer use. RECIPES See the recipe program called "MIKE'S CHICKEN SOUP" in the VIC owner's manual, or check a computer book rack (most "practi cal" program books contain recipe programs). SIMULATIONS Computer simulations permit dan gerous or expensive experiments to be performed at minimum risk & expense. SPORTS DATA The Source'"1 and CompuServe"" both provide sports information. XIII STOCK QUOTES The VIC, a modem, and a subscrip tion to The Source" or Compu Serve1" can cost less than $500. TALKING VIC Connect the VIC to a voice synthe sizer such as the "Type N Talk"tm manufactured by VOTRAX INC. TERM PAPERS The VIC heips students research & REPORTS current library-type sources over the telephone . . . and compose, edit and print out their reports on the VIC and VIC GRAPHIC PRINTER . . . the same type of computer services which were previously available only through large institutions at a cost of many thousands of dollars. TERMINAL VIC accessories include an RS-232 MODEM modem interface (for use with RS232 modems} or the ultra low-priced VICMODEM1" TYPING PRACTICE i The reverse side of the VtC TYPE WRITER has a "TYPING TUTOR" program. WORDPROCESSING i i THE VIC TYPEWRITERS is avail able on WRITERS tape and cartridge wordprocessing the also power. VIC- provides Both work with the VIC GRAPHIC PRINTER. i xiv BASIC PROGRAMMING REFERENCE GUIDE • VIC BASIC: The Language of the VIC • Commands • Statements • I/O Statements • BASIC Functions • Numbers and Variables • Logical Operators I I I I I [ I I I I I 1 I VIC BASIC: THE LANGUAGE OF THE VIC The BASIC computing language is a powerful and easy-to-use means of communicating instructions to your VIC 20 Personal Computer. VIC BASIC is the same language used in the Commodore PET.'CBM line of microcomputers, and is nearly identical to the BASIC used in most other personal computers. Learning BASIC now can prepare you to move up to a more sophisticated computer in the future, and can also give you the foundation you need to learn other "higher level'1 computing languages. If you're a first-time computerist, you'll be pleased to know you can write your first BASIC program on the VIC within 15 minutes, using the VIC 20 PERSONAL COMPUTER GUIDE which comes with the machine. Additional self-teaching aids are available from Commodore as part of the TEACH YOURSELF PROGRAMMING SERIES, and classes offered by schools, computer centers and retail stores can give you a solid grounding in the fundamentals of BASIC within 4-6 hours. The VIC BASIC instructions which follow will provide a valuable reference as you learn to write BASIC, or as you put into practice the techniques you've already learned. Each entry in the listing explains how the instruction is used, with practical examples. Additional programming tips are included in a separate "BASIC PROGRAMMING TIPS" section. BASIC has approximately 60 words in its vocabulary and is surprisingly easy to learn. That doesn't mean you can't keep improving, however. Like any language, BASIC has its own ''idioms'1 and complexities which you can use to write increasingly sophisticated programs, VIC BASIC even has a sort ot "slang" in that you can abbreviate most of the commands by typing the first letter of the instruction and the SHIFTED second letter. Using abbreviated commands to write programs makes programming the VIC fast and convenient. (Note that if you LIST a program written in abbreviated form, the full-length commands are displayed to help you read your program.) In BASIC, all instructions are commonly referred to as 'commands," although technically the BASIC instruction set can be broken down into several areas . . . which is how we've grouped them in the following VIC BASIC "vocabulary11 guide. We've included separate sections on several types of BASIC instructions; Commands, Statements, Input/Output Statements, Numbers and Variables, and Operators. Functions, It should also be noted here that while we communicate with the VIC through the BASIC language, the VIC's "native" vocabulary is Machine Language which is based on a binary or hexadecimal numbering system. BASIC is really a translation of Machine Language into terms we humans can understand . . . which is why BASIC programs generally run slower than Machine Language programs, since BASIC programs have to be interpreted into i Machine Language before they run, while Machine Language programs run immediately. See "Introduction to Machine Lan guage Programming" for more information. Of course, you don't have to know BASIC to take advantage of the VIC's computing power. . . you don't even haveto know howto type. We like to say that with the VIC 20, the first thing you do is learn "computing" . . . not "programming." You don't have to bean auto mechanic to drive a car, and by the same token you don't have to be a programmer to "drive" your VIC 20. Still, knowing something about how your car works mechanically helps you maintain and use yourcarto best advantage. Likewise, knowing how the computer is programmed helps you get the most out of your VIC 20. In the future, being able to "speak" a computer language will give you a tremendous advantage over those who can't. . .not because you can write a computer program, but because you'll have a better understanding of what a computer is and does, and you'll be able to make better use of computing at school, on the job and at home. Learning BASIC . . .or at least how it works. . .will bring you close to the future and prepare you for the dramatic technological changes that are already occurring as part of the "Computer Revolution." TO AVOID CONFUSION, PLEASE NOTE: 0 = letter 0 as in OPEN 0 = = ZERO 1 = letter I as in INPUT 1 = = number ONE * - type the large asterisk key Format == all format entries shall be typed in on one line [ ] = information contained inside brackets in the Format lines is optional. WARM START —If you get into trouble or want to break out of a program while it's running, you can hold down the RUN/STOP key and hit the RESTORE key. This combination resets the VIC without losing your program. Now you can LIST or RUN your program again. [ COMMANDS BASIC commands tell the VIC to do something with ^program. For example, you "command" the VIC to list, run, stop, continue, save and load programs. BASIC commands may be used in direct mode (without line numbers) or as instructions in BASIC programs, tn direct mode, commands are executed as soon as the RETURN key is pressed. In a BASIC program, commands are included like any other instruction, and executed when you type RUN, The only command which may not be used in a program is CONT. VIC BASIC commands include the following: CONT LIST LOAD NEW RUN SAVE VERIFY CONT Format: Abbreviation: Screen Display: cont cBERlo cfi This command is used to re-start the execution of a program which has been stopped by either using the STOP key, a STOP statement, or an END statement within the program. The program will re-start at the exact place from whfch it left off. While the program is stopped, the user can inspect or change any variables or look at the program, CONT will not work if you have changed or added lines of the program (or even just moved the cursor to a program line and hit RETURN without changing anything), or if the program halted due 1o an error, or if you caused an error before typing to re-start the program. The message in this case is CANT CONTINUE ERROR. This is a handy tool when debugging a program. It lets you place STOP statements at strategic locations in the program, and examine variable vafues when the program stops. You can keep using STOP and CONT until you find what you're looking for. EXAMPLE: 10 PI = 0: C = --■ 1 20 PI = PI + 4/C - 4/(C + 2) 30 PRINT■ Dl PI 40 C - C + 4:GOTO 20 This program calculates the value of PI. RUN this program, and after a short while hit the ^9 key. You will see the display: BREAK IN 20 Type the command PRINT C to see how far the VIC has gotten. Then use CONT to resume from where the VIC left off. LIST Format: * Abbreviation: Screen Display: LIST [from line number] LKi;!!rTiJ I L -, - [to line number] The LIST command allows you to look at lines of the BASIC program currently in the VIC's memory. The VfC's powerful screen editor allows you to easily and quickly edit programs which you have LISTed. The LIST command can specify which program line numbers will be shown. If you type LIST and a single line number, only that line will be displayed. If the number is followed by a hyphen (-), all the lines from that number forward will be shown. If the number is preceded by the hyphen, all lines from the beginning to that line will be shown. You can LIST a range of line numbers by typing the two line numbers separated by a hyphen, in which case the lines from the first number to the second number are shown. If LIST0 is typed, the whole program will be LISTed and not just line 0. If the program length exceeds the length of the screen display, the first lines in the program will scroll off the screen during LISTing. To slow down the scrolling, hold down the p^ hi ifI key. To stop the program during a LIST, hit the EQ3 key. The LIST command may also be used as a statement within a BASIC program, but the program will stop as soon as the LIST is finished. "All format entries should be typed in one line EXAMPLES OF LIST COMMAND LIST LIST 0 LIST 100 LIST 100- LISTs whole program LISTs whole program Line 100 only Everything from 100 on LIST -100 Shows from start to 100 LIST 100-150 Starts at 100 and stops with 150 EXAMPLE OF LIST STATEMENT (Program Mode) 10 PRJNT'THIS IS LINE 10" 20 LIST 30 PRINT'THIS IS LINE 30" LOAD Format: Abbreviation: LOAD [■'filename", LJUffilo device, commandl Screen Display: L n The LOAD command transfers a program from cassette tape or disk into the VIC's memory, where it can be used or changed. LOAD FROM TAPE If the program to be LOADed is the first one on the tape, all you have to type is the word LOAD by itself. Unless the PLAY key was already down, the VIC answers with the message: PRESS PLAY ON TAPE. Once the recorder has been started, the VIC says: OK SEARCHING FOUND LOADING At this point, any program that had been in memory is lost, because the new one has begun to take its place. (If you use the ffflf Key to halt the LOAD there will likely be a spot in the program with garbage, just at the point where you stopped it.) Once the program has finished the LOAD, the VIC says: READY. When the program is not the first on the lape, or you're not sure that it is, the VIC can search for the program you want. Type LOAD I and the name of the program inside quote marks ("), or the name of a string variable containing the name of the program. The VIC will show any other programs or files that it sees on the tape with the message: r FOUND name The VIC will onfy LOAD the correct program, and will not LOAD a data file on that tape. LOAD FROM DESK In order to bring in a program from a device other than the tape, a device number is used. Folfowing the name of the program, type a comma (,) and the number (or variable containing the number). The cassette is device number 1. The disk drive is device numbers. See the manual of the device for its number. If the program with that name is not found on the device, a FILE NOT FOUND ERROR will result. This doesn't happen on tape, since the VIC has no way of sensing that there are no more programs on a tape. It is possible to put an end-of-tape marker on the tape, using the SAVE or OPEN statements, and if the VIC reads this marker while searching a tape, an error message appears. The VIC will automatically LOAD the program into the beginning of BASIC program memory, at location 4096 in a machine without an extra 3K of memory, or at 1024 with the extra 3K. For certain applications this may not be convenient. By following the device number with a comma and the command number 1, the VIC will be sure to LOAD the program in the same spot in memory from which it was SAVEd. LOAD can be used as a statement in a BASIC program. The program will be RUN as soon as LOADing is finished. Variables used in the first program will not be cleared as long as the new program is shorter in length than the older one. If a longer program is LOADed on a short one, the BASIC program lines will over-write the variables, and changing a variable will mess up the program. Note that using an asterisk can save loading time (see examples). EXAMPLES: LOAD LOAD "HELLO" Reads in the next program from tape. Searches tape until the program called LOAD AS HELLO is found, then it is LOADed. Uses the name in AS to search, 3 I I LOAD +1*"p 8 LOADs first program from disk. LOAD "AB*'\ 8 Loads first program beginning with AB. LOAD L1HELLO"r S Looks for a program on device 8 (disk drive}, LOAD "71,1 Looks for the first program on tape, and LOADs it into the same part of memory that it came from. 10 LOAD"NEXT'\ 8 Finds the program called NEXT on device 8, LOADs, then RUNs it. Because of possible problems with old tapes or misaligned recorders, it is possible that the program will not LOAD correctly. The VIC stores two copies of the program on the tape. If they don't match, the message LOAD ERROR is displayed. The program may LIST correctly, but probably won't. In any case, there is most likely some problem, some section of memory that is not right, and the program should be re-LOADed. It is wise to make an extra copy of any program, in case you run into this problem at some time. In the case o1 a very bad LOAD, some of the important memory locations inside the VIC may be changed. If you get weird results after a LOAD, like the VIC not understanding normal BASIC commands anymore, you'll have to turn the VIC off and then on again. NEW Format: Abbreviation: Screen Display: None None NEW NEW is used to tell the VIC to erase a current program from memory so a different program can be used. Unless the program is stored (on tape or disk), ft will be lost unless it is typed in again from the beginning. For this reason, you should BE CAREFUL when using this command! Not clearing out an old program before typing in a new one can result in a confusing mixing of the two programs. NEW can also be used as a statement within a program. When this instruction is executed, the program in memory Is erased, and the program stops. This is not a good programming technique, especially if you RUN the program and it erases itself while you're writing or debugging it. EXAMPLE: NEW 10 NEW Clears the program & variables. Performs the NEW operation and stops the program. RUN Format: Abbreviation: Screen Display: HUN [line number] rKUBMu RQ This command causes a BASIC program to begin operating. The command RUN by itself starts the program at the lowest numbered line. All variable values are cleared when this command is given. RUN followed by a number causes the program to start working from another line than the lowest numbered one. If that line number does not exist, the message UNDEF'D STATEMENT ERROR appears. RUN followed by a variable will first clear the value of that variable, and try to start the program at line 0. il it exists. RUN can also be used as a statement within a program. Keep in mind that all variables are cleared when this statement is executed. EXAMPLES: RUN Starts at the beginning RUN 100 Starts at line 100 RUN X Starts at line 0, or UNDEF'D STATEMENT ERROR if no line 0 SAVE Format: Abbreviation: Screen Display: SAVE ["filename", S^S"?prJ A S J5 device, command] The SAVE command stores a program currently in memory on tape or disk. The program being SAVEd is not affected and remains fn the VIC's memory after the save operation. Programs on tape are stored twice automatically, so the VIC can check for errors when LOADing the program back in. The command SAVE all by itself sends the program to the cassette deck without a name. When the command is given, the VIC wil! say: PRESS RECORD AND PLAY ON TAPE Holding down the RECORD button, press PLAY, and the VIC will say: OK SAVING 10 i and begin storing. The VIC cannot check the RECORD key: it can only sense that the tape is moving, so be sure to press RECORD, if PLAY was already pressed, no message appears. When the program has been SAVEd, the VIC will give the message: READY. The VIC has no way of searching for a blank spot on the tape, but just records wherever it is, erasing any information that may have been there. However, the VERIFY command can be used to find the end of the last program. SAVE can be followed by a program name in quotes or in a string variable. The VIC will then write the program name before the program on the tape, which lets the VIC find it more easily. The program can be SAVEd on a device other than the tape deck by giving a device number. To do this, put a comma after the program name, and then the number of the device. The cassette deck is device number 1, and the disk is number 8. (See examples.) It is possible to instruct the VIC to SAVE a program so it will not be moved in memory when LOADed. Using command number 1 after the device number will do this. This is useful when working with different memory configurations which may cause VIC memory locations to shift. To prevent a user from accidentally trying to read past the last information on the tape, the VIC can also write an end-of-tape marker after the program. To do this, follow the device number with a comma and command number 2. When the VIC finds this marker, it will stop and show the message DEVICE NOT PRESENT ERROR. Command number 3 is a combination of 1 and 2, telling the program on tape not to relocate and to put an end-of-tape marker after the program. SAVE can also be used as a statement within a BASIC program. When this statement is hit, the program will be SAVEd normally, with the usual prompts appearing on the screen. The program resumes normally after the SAVE. EXAMPLES SAVE SAVE "HELLO" SAVE AS SAVE ■1HELLO",8 SAVE "HELLO",! ,1 Stores program on tape without name Stores on tape with name HELLO Stores on tape with name in AS Stores on device number 8 (disk drive) Won't relocate HELLO upon re-LOADing 11 SAVE "HELLO",1,2 Puts an end-of-tape marker after the program. SAVE "HELLO",! ,3 Won't relocate & end-oMape marker 10 SAVE"HELLO" Saves the program, then goes on with the next program line. I VERIFY | Format: Abbreviation: Screen Display: VERIFY ''filename", v|h;!I;T|e V[ , device This command checks the program on tape or disk against the program in the VIC's memory. VERIFY is normally used right after a SAVE, to make sure the program was stored correctly on the tape ordisk. VERIFYing a program after it has been LOADed is useless, since the same incorrect program could be in both places. i When you tell the VIC to VERIFY, this message shows: PRESS PLAY ON TAPE Once the tape is moving, the VIC checks the program against memory, reading until the end of the program. If the copies match, the VIC says: OK READY. ' If there is a problem, you will see this message: . 7VERIFY ERROR In this case, you should immediately SAVE the program on a different tape or disk and try again. The format of the VERIFY command is similar to the LOAD command. A program name can be given either in quotes or a string variable, and the VIC will search for that program. If a comma and a device number follow the name, the VIC will look at the device designated. VERIFY is also used to position a tape just past the last program, so a new program can be added to the tape without over-writing an older one. Just VERIFY with the name of the last program there; the VIC searches, checks the program, and stops with a VERIFY ERROR. However, your program is still in memory, and now the tape is at a blank spot. You can SAVE without worry. 12 I EXAMPLES: VERIFY VERIFY "HELLO" VERIFY "HELLO",8 Checks the first program on tape. Searches for HELLO, then checks Looks on device a for the program. I 1 13 STATEMENTS I CLH DATA DEF FN DIM END FOR ... TO ... STEP GET GOSUB GOTO or GO TO IF . . THEN INPUT LET NEXT ON POKE PRINT READ REM RESTORE RETURN STOP SYS WAIT I CLR Format: Abbreviation: Screen Display: CLR C Bfffil L CG This statement clears out any variables that have been defined, un-DIMensions any arrays, and RESTORES the DATA pointer back to the beginning. This makes available all the RAM memory that the variables had used so that it can be used for something different. The RUN command automatically performs the CLR operation, asdoesLOADing a new program or doing a NEW. Don't confuse this statement with ihe ImII key, which clears the screen. 14 ! EXAMPLE: 100 A a 1014 110 CLR 120 PRINT A When RUN. this program PRINTs a zero on the screen. DATA Format: Abbreviation: Screen Display: DATA value dHJTHBa d V [■ value, . . . , value] The DATA statement holds information that will fill variables in a READ statement. Any type of information can be stored here, separated by commas. If a comma, space, or colon are to be used as data, they must be enclosed in quote marks ("). Two commas with nothing in between will be read as zero, or an empty string. Note: the information in brackets is optional. The line containing the DATA statement doesn't actually have to be executed while RUNning the program, so most programmers leave all the DATA statements at the end of the program, out of the way. EXAMPLE: 10 READ A : PRINT A 20 READ A, B, C : PRINT A; B; C 30 READ AS : PRINT A$ 40 FOR L = 1 to 5 : READ A : PRINT A; : NEXT 50 READ AS, A, B, B$ : PRINT AS, A. B, BS 60 END : REM YOU NEVER ACTUALLY HAVE TO HIT THE DATA STATEMENTS! 960 DATA 1 970 DATA 1,2,3 980 DATA ABC 990 DATA 2,3,5,7,11,"HELLO" -1.2445E-5, 69.7767. "A, B, C" DEF FN Format: Abbreviation: Screen Display: DU.F FN [name] D^fctlE D[ ! (variable) - formula 15 When a long mathematical formula is used several times in different lines of a program, program memory and typing time can be saved by using a defined function for the formula. The function is then used throughout the program in place of the lengthy formula. The name of the function will be the letters FN and any variable name you choose, one or two letters long. The DEF FN statement must be executed at least once for the program to use it, so this statement is normally placed at the beginning of the program. The function name is followed by a variable name inside parentheses. Next comes an equal sign, and then the formula. Here's an example of a simple formula definition, with an example of its use in the program. EXAMPLE 1: 10 DEF FNA(X) = 7 * X 20 PRINT FNA(1} 30 PRINT FNA(3) The result of line 20 is 7, and the result in line 30 is 21. EXAMPLE 2: I 10 DEF FNA(X) = INT(RND(1)*6) + 1 20 PRINT FNA(10) When the function in this example is used in the program, the value of the number in parentheses in line 20 doesn't have any effect on the result. This is because in line 10 the variable (X) in the parentheses doesn't appear in the formula on the right. The next example does use the variable name in the formula. EXAMPLE 3: 10 DEF FNA(X) = INT(RND(1)-X} + 1 20 PRINT FNA(10) In this case, the number in the parentheses in line 20 does affect the result. The number in the parentheses in line 20 is the largest random number that will be picked. The result of a defined formula must always be a number; there are no defined functions for string variables. 16 ] DIM Format; lull1 DIM variable ( number. Screen Display: Abbreviation: . . . D , number), [variable ( number, . . . , number), . ■■] This statement defines an array or matrix of variables, which allows you to use the variable name with a subscript. The subscript points to the element in the array being used. The lowest element number in an array is zerot and the highest is the number given in the DIM statement. If an array variable is used without a DIM statement to create it. it is automatically DIMensioned to 10 in each dimension. Let's suppose we wanted to keep track ot the score of a football game. There are 2 teams, and four quarters plus a possible overtime quarter in the game. We could use a matrix to hold the scores fn each quarter. Here is a program that asks you for the score of each team in each quarter: EXAMPLE: 100 DIM S(1,5) , T$(1) 110 INPUT "TEAM NAMES1' ; T$(0), T$(1) 120 FOR Q - 1 TO 5 130 FOR T - 0 TO 1 140 PRINT TS(T), "SCORE IN QUARTER" Q 150 INPUT S(T,Q) 160S(T,0) = S(T, 0) + S(Tr Q) 170 NEXTT,Q 180 PRINT CHRS(147) "SCOREBOARD" 190 PRINT "QUARTER11; 200 FOR Q = 1 TO 5 210 PRINT TAB(CT2 +9) O; 220 NEXT 230 PRINT TAB (15) "TOTAL" 240 FOR T = 0 TO 1 250 PRINT TS(T); 260 FOR Q = 1 TO 5 270 PRINT TAB (Q*2 +9) S(T. Q); 280 NEXT 290 PRINT TAB(15) S(T.O) 300 NEXT The element numbers in every dimension start at 0 and end at the number in the D!M statement. The number of elements created in any dimension is the maximum subscript number PLUS 1. The total number of elements is equal to the product of the number of elements in ah dimension multiplied together. There may be any number of dimensions and any number of elements in an array, limited only by the amount of RAM memory that is available to hold the variables. The array may be made up of normal numeric variablesI as shown above, or o1 strings or integer numbers. If the variables are to be other than normal numeric, simply use the S or % signs after the variable name to indicate string or integer variables. It's easy to calculate the amount oi memory that will be used up by an array: MEMORY USED - 5 bytes for variable name 2 bytes for each dimension 2 bytes/element for integer variables 5 bytes/element for normal numeric variables 3 bytes/element for string variables 1 byle for each character in each string element END Format; Abbreviation: Screen Display: END eRV^|n E0 i This statement will finish the program when RUNning and return complete control of the VIC to the person operating it. The CONT command can be used to resume execution of the program after the END statement was reached, because no variables or pointers are cleared. The END statement results in the message: READY. The difference between STOP and END statements is slight: the STOP statement displays the message: BREAK IN LINE XXX Neither STOP nor END is required to appear at any point in the program in VIC BASIC, because a program running out of lines to execute will END all by itself. i; FOR ... TO ... STEP . . . Format: Abbreviation: Screen Display: FOR variable = F^.'Lrffi|o Ff start TO limit [STEP increment] This is a special BASIC statement that lets you easily use a variable as a counter. You must specify certain parameters: the variable name, its starting value, the limit of the count, and how much to add during each cycle. Here is a simple BASIC program that counts from 1 to 10, PRINTing each number and ENDing when complete, and using no FOR statements: 100 L = 1 110 PRINT L 120 L - L -f- 1 130 IF L <- 10 THEN 110 140 END Using the FOR statement, here is the same program; 100 FOR L - 1 TO 10 110 PRINT L 120 NEXT L 130 END As you can seer the program is shorter and easier to understand using the FOR statement. Here is a closer look at the parameters, to see how everything works. The variable can be any numeric variable name except an array variable. When the program reaches a FOR statement, variable is set to the value of start. The program proceeds with the statements, until a statement containing the word NEXT is reached. At that point, increment is added to variabEe's value. The STEP is optional, and if there is no STEP shown increment is assumed to be 4-1, After increment has been added to variable, the vaJue of variable is compared to limit. It the limit has not been exceeded the program continues with the line after the FOR statement. If the limit has been passed, the line to be executed is the line following the NEXT statement. Note: if the STEP value is positive, variable will exceed limit when its value is greater than limit, and if the STEP value is negative, the variable must be less than limit to end the count. The loop will always be executed at least once, re gardless of the values in "start" and Lliimit". 19 EXAMPLE: I 100 FOR L - 100 TO 0 STEP -1 100 FOR L = PI TO 6*PI STEP .01 100 FOR AA = 3 TO 3 I GET Format: Abbreviation: Screen Display: GET variable gI:^;;" |e G £j This statement lets you inpul one character at a time from the keyboard. Whatever character was hit goes into the variable. If no key was p