********* Welcome to Project 64! The goal of Project 64 is to preserve Commodore 64 related documents in electronic text format that might otherwise cease to exist with the rapid advancement of computer technology and declining interest in 8- bit computers on the part of the general population. If you would like to help by converting C64 related hardcopy documents to electronic texts please contact the manager of Project 64, Cris Berneburg, at <74171.2136@compuserve.com>. Extensive efforts were made to preserve the contents of the original document. However, certain portions, such as diagrams, program listings, and indexes may have been either altered or sacrificed due to the limitations of plain vanilla text. Diagrams may have been eliminated where ASCII-art was not feasible. Program listings may be missing display codes where substitutions were not possible. Tables of contents and indexes may have been changed from page number references to section number references. Please accept our apologies for these limitations, alterations, and possible omissions. The author(s) of the original document and members of Project 64 make no representations about the accuracy or suitability of this material for any purpose. This etext is provided "as-is". Please refer to the warantee of the original document, if any, that may included in this etext. No other warantees, express or implied, are made to you as to the etext or any medium it may be on. Neither the author(s) nor the members of Project 64 will assume liability for damages either from the direct or indirect use of this etext or from the distribution of or modification to this etext. ********* The Project 64 etext of the Commodore 64 Programmer's Reference Guide, first edition. Converted to etext by Ville Muikkula. Some errors in the original document were corrected in this etext. C64PRG10.TXT, June 1996, etext #46 ********* ~ I would like to thank the following persons for their valuable help: Jouko Valta for the memory maps on pages 310-334. Marko Makela for the combined table of memory maps on pages 264-266. Cris Berneburg for proof reading. Kimmo Hamalainen for proof reading. There was a lot of work, but finally, after five weeks of correcting OCR-errors and formatting the text to readable format, it is ready. I hope that this massive project shows to the C= community that it is in a fact possible for one man to convert a 500 page book to ASCII text. One just have to be dedicated, believe that it can be done and have the PATIENCE for it... and lots of free time. So, who's going to etext Inside Commodore DOS? If you find errors in the text, please report them so that they can be fixed. There should not be many, though... There are some pictures missing on pages 132,157,162-163,195,364-365, 377-378,380-381,404,406-407,416-417,421,459,476-477 and 481. Also the schematics of C-64 are not available. I apologize for the possible inconvenience this might cause. Ville Muikkula or . ********* Note: To extract the ascii text basic programs all at once from this etext use "tok64" by Cris Berneburg <74171.2136@compuserve.com>. ********* Windows 95 MS-DOS Edit is the ideal program for reading this etext. Just check that ANSI.SYS is loaded in CONFIG.SYS and issue the command: mode con lines=50 Now a whole page fits nicely on the screen and you can use Page Up/Page Down keys to flip pages. Just be sure that the ~ characters are always on the last line of the screen. ********* ~ COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE Published by Commodore Business Machines, Inc. and Howard W. Sams & Co., Inc. i ~ FIRST EDITION FOURTH PRINTING-1983 Copyright (C) 1982 by Commodore Business Machines, Inc. All rights reserved. This manual is copyrighted and contains proprietary information. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photo- copying, recording, or otherwise, without the prior written permission of COMMODORE BUSINESS MACHINES, Inc. ii ~ TABLE OF CONTENTS INTRODUCTION ....................................................... ix o What's Included? .............................................. x o How to Use This Reference Guide ............................... xi o Commodore 64 Applications Guide ............................... xii o Commodore Information Network ................................. xvii 1. BASIC PROGRAMMING RULES ......................................... 1 o Introduction .................................................. 2 o Screen Display Codes (BASIC Character Set) .................... 2 The Operating System (OS) ................................... 2 o Programming Numbers and Variables ............................. 4 Integer, Floating-Point and String Constants ................ 4 Integer, Floating-Point and String Variables ................ 7 Integer, Floating-Point and String Arrays ................... 8 o Expressions and Operators ..................................... 9 Arithmetic Expressions ...................................... 10 Arithmetic Operations ....................................... 10 Relational Operators ........................................ 12 Logical Operators ........................................... 13 Hierarchy of Operations ..................................... 15 String Operations ........................................... 16 String Expressions .......................................... 17 o Programming Techniques ........................................ 18 Data Conversions ............................................ 18 Using the INPUT Statement ................................... 18 Using the GET Statement ..................................... 22 How to Crunch BASIC Programs ................................ 24 2. BASIC LANGUAGE VOCABULARY ....................................... 29 o Introduction .................................................. 30 o BASIC Keywords, Abbreviations, and Function Types ............. 31 o Description of BASIC Keywords (Alphabetical) .................. 35 o The Commodore 64 Keyboard and Features ........................ 93 o Screen Editor ................................................. 94 iii ~ 3. PROGRAMMING GRAPHICS ON THE COMMODORE 64 .................................................... 99 o Graphics Overview ............................................. 100 Character Display Modes ..................................... 100 Bit Map Modes ............................................... 100 Sprites ..................................................... 100 o Graphics locations ............................................ 101 Video Bank Selection ........................................ 101 Screen Memory ............................................... 102 Color Memory ................................................ 103 Character Memory ............................................ 103 o Standard Character Mode ....................................... 107 Character Definitions ....................................... 107 o Programmable Characters ....................................... 108 o Multi-Color Mode Graphics ..................................... 115 Multi-Color Mode Bit ........................................ 115 o Extended Background Color Mode ................................ 120 o Bit Mapped Graphics ........................................... 121 Standard High-Resolution Bit Map Mode ....................... 122 How It Works ................................................ 122 o Multi-Color Bit Map Mode ...................................... 127 o Smooth Scrolling .............................................. 128 o Sprites ....................................................... 131 Defining a Sprite ........................................... 131 Sprite Pointers ............................................. 133 Turning Sprites On .......................................... 134 Turning Sprites Off ......................................... 135 Colors ...................................................... 135 Multi-Color Mode ............................................ 135 Setting a Sprite to Multi-Color Mode ........................ 136 Expanded Sprites ............................................ 136 Sprite Positioning .......................................... 137 Sprite Positioning Summary .................................. 143 Sprite Display Priorities ................................... 144 Collision Detects ........................................... 144 o Other Graphics Features ....................................... 150 Screen Blanking ............................................. 150 Raster Register ............................................. 150 Interrupt Status Register ................................... 151 Suggested Screen and Character Color Combinations ........... 152 iv ~ o Programming Sprites-Another Look .............................. 153 Making Sprites in BASIC-A Short Program ..................... 153 Crunching Your Sprite Programs .............................. 156 Positioning Sprites on the Screen ........................... 157 Sprite Priorities ........................................... 161 Drawing a Sprite ............................................ 162 Creating a Sprite ... Step by Step .......................... 163 Moving Your Sprite on the Screen ............................ 165 Vertical Scrolling .......................................... 166 The Dancing Mouse-A Sprite Program Example .................. 166 Easy Spritemaking Chart ..................................... 176 Spritemaking Notes .......................................... 177 4. PROGRAMMING SOUND AND MUSIC ON YOUR COMMODORE 64 ............................................ 183 o Introduction .................................................. 184 Volume Control .............................................. 186 Frequencies of Sound Waves .................................. 186 o Using Multiple Voices ......................................... 187 Controlling Multiple Voices ................................. 191 o Changing Waveforms ............................................ 192 Understanding Waveforms ..................................... 194 o The Envelope Generator ........................................ 196 o Filtering ..................................................... 199 o Advanced Techniques ........................................... 202 o Synchronization and Ring Modulation ........................... 207 5. BASIC TO MACHINE LANGUAGE ....................................... 209 o What is Machine Language? ..................................... 210 What Does Machine Code Look Like? ........................... 211 Simple Memory Map of the Commodore 64 ....................... 212 The Registers Inside the 6510 Microprocessor ................ 213 o How Do You Write Machine Language Programs? ................... 214 64MON ....................................................... 215 o Hexadecimal Notation .......................................... 215 Your First Machine Language Instruction ..................... 218 Writing Your First Program .................................. 220 o Addressing Modes .............................................. 221 Zero Page ................................................... 221 The Stack ................................................... 222 v ~ o Indexing ...................................................... 223 Indirect Indexed ............................................ 223 Indexed Indirect ............................................ 224 Branches and Testing ........................................ 226 o Subroutines ................................................... 228 o Useful Tips for the Beginner .................................. 229 o Approaching a Large Task ...................................... 230 o MCS6510 Microprocessor Instruction Set- Alphabetic Sequence ........................................... 232 Instruction Addressing Modes and Related Execution Times ................................... 254 o Memory Management on the Commodore 64 ......................... 260 o The KERNAL .................................................... 268 o KERNAL Power-Up Activities .................................... 269 How to Use the KERNAL ....................................... 270 User Callable KERNAL Routines ............................... 272 Error Codes ................................................. 306 o Using Machine Language From BASIC ............................. 307 Where to Put Machine Language Routines ...................... 309 How to Enter Machine language ............................... 309 o Commodore 64 Memory Map ....................................... 310 Commodore 64 Input/Output Assignments ....................... 320 6. INPUT/OUTPUT GUIDE .............................................. 335 o Introduction .................................................. 336 o Output to the TV .............................................. 336 o Output to Other Devices ....................................... 337 Output to Printer ........................................... 338 Output to Modem ............................................. 339 Working With Cassette Tape .................................. 340 Data Storage on Floppy Diskettes ............................ 342 o The Game Ports ................................................ 343 Paddles ..................................................... 346 Light Pen ................................................... 348 o RS-232 Interface Description .................................. 348 General Outline .............................................. 348 Opening an RS-232 Channel .................................... 349 Getting Data From an RS-232 Channel .......................... 352 Sending Data to an RS-232 Channel ............................ 353 Closing an RS-232 Data Channel ............................... 354 Sample BASIC Programs ........................................ 356 vi ~ Receiver/Transmitter Buffer Base Location Pointers ........... 357 Zero-Page Memory Locations and Usage for RS-232 System Interface ................................ 358 Nonzero-Page Memory Locations and Usage for RS-232 System Interface ................................ 358 o The User Port ................................................. 359 Port Pin Description ........................................ 359 o The Serial Bus ................................................ 362 Serial Bus Pinouts .......................................... 363 o The Expansion Port ............................................ 366 o Z-80 Microprocessor Cartridge ................................. 368 Using Commodore CP/M (R) .................................... 369 Running Commodore CP/M (R) .................................. 369 APPENDICES ......................................................... 373 A. Abbreviations for BASIC Keywords ............................ 374 B. Screen Display Codes ........................................ 376 C. ASCII and CHR$ Codes ........................................ 379 D. Screen and Color Memory Maps ................................ 382 E. Music Note Values ........................................... 384 F. Bibliography ................................................ 388 G. VIC Chip Register Map ....................................... 391 H. Deriving Mathematical Functions ............................. 394 I. Pinouts for Input/Output Devices ............................ 395 J. Converting Standard BASIC Programs to Commodore 64 BASIC ........................................ 398 K. Error Messages .............................................. 400 L. 6510 Microprocessor Chip Specifications ..................... 402 M. 6526 Complex Interface Adapter (CIA) Chip Specifications ....................................... 419 N. 6566/6567 (VIC-II) Chip Specifications ...................... 436 0. 6581 Sound Interface Device (SID) Chip Specifications ....... 457 P. Glossary .................................................... 482 INDEX .............................................................. 483 COMMODORE 64 QUICK REFERENCE CARD .................................. 487 SCHEMATIC DIAGRAM OF THE COMMODORE 64 .............................. 491 vii ~~ INTRODUCTION The COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE has been developed as a working tool and reference source for those of you who want to maximize your use of the built-in capabilities of your COMMODORE 64. This manual contains the information you need for your programs, from the simplest example all the way to the most complex. The PROGRAMMER'S REFERENCE GUIDE is designed so that everyone from the beginning BASIC programmer to the professional experienced in 6502 machine language can get information to develop his or her own creative programs. At the same time this book shows you how clever your COMMODORE 64 really is. This REFERENCE GUIDE is not designed to teach the BASIC programming language or the 6502 machine language. There is, however, an extensive glossary of terms and a "semi-tutorial" approach to many of the sections in the book. If you don't already have a working knowledge of BASIC and how to use it to program, we suggest that you study the COMMODORE 64 USER'S GUIDE that came with your computer. The USER'S GUIDE gives you an easy to read introduction to the BASIC programming language. If you still have difficulty understanding how to use BASIC then turn to the back of this book (or Appendix N in the USER'S GUIDE) and check out the Bibliography. The COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE is just that; a reference. Like most reference books, your ability to apply the information creatively really depends on how much knowledge you have about the subject. In other words if you are a novice programmer you will not be able to use all the facts and figures in this book until you expand your current programming knowledge. ix ~ What you can do with this book is to find a considerable amount of valuable programming reference information written in easy to read, plain English with the programmer's jargon explained. On the other hand the programming professional will find all the information needed to use the capabilities of the COMMODORE 64 effectively. WHAT'S INCLUDED? o Our complete "BASIC dictionary" includes Commodore BASIC language commands, statements and functions listed in alphabetical order. We've created a "quick list" which contains all the words and their abbreviations. This is followed by a section containing a more detailed definition of each word along with sample BASIC programs to illustrate how they work. o If you need an introduction to using machine language with BASIC programs our layman's overview will get you started. o A powerful feature of all Commodore computers is called the KERNAL. It helps insure that the programs you write today can also be used on your Commodore computer of tomorrow. o The Input/Output Programming section gives you the opportunity to use your computer to the limit. It describes how to hook-up and use everything from lightpens and joysticks to disk drives, printers, and telecommunication devices called modems. o You can explore the world of SPRITES, programmable characters, and high resolution graphics for the most detailed and advanced animated pictures in the microcomputer industry. o You can also enter the world of music synthesis and create your own songs and sound effects with the best built-in synthesizer available in any personal computer. o If you're an experienced programmer, the soft load language section gives you information about the COMMODORE 64's ability to run CP/M* and high level languages. This is in addition to BASIC. Think of your COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE as a useful tool to help you and you will enjoy the -hours of programming ahead of you. ----------- * CP/M is a registered trademark of Digital Research, Inc. x INTRODUCTION ~ HOW TO USE THIS REFERENCE GUIDE Throughout this manual certain conventional notations are used to de- scribe the syntax (programming sentence structure) of BASIC commands or statements and to show both the required and optional parts of each BASIC keyword. The rules to use for interpreting statement syntax are as follows: 1. BASIC keywords are shown in capital letters. They must appear where shown in the statement, entered and spelled exactly as shown. 2. Items shown within quotation marks (" ") indicate variable data which you must put in. Both the quotation marks and the data inside the quotes must appear where shown in each statement. 3. Items inside the square brackets ([ ]) indicate an optional state- ment parameter. A parameter is a limitation or additional qualifier for your statements. If you use an optional parameter you must supply the data for that optional parameter. In addition, ellipses (...) show that an optional item can be repeated as many times as a programming line allows. 4. If an item in the square brackets ([ ]) is UNDERLINED, that means that you MUST use those certain characters in the optional para- meters, and they also have to be spelled exactly as shown. 5. Items inside angle brackets (< >) indicate variable data which you provide. While the slash (/) indicates that you must make a choice between two mutually exclusive options. EXAMPLE OF SYNTAX FORMAT: OPEN ,[,
],[":][,]" EXAMPLES OF ACTUAL STATEMENTS: 10 OPEN 2,8,6,"0:STOCK FOLIO,S,W" 20 OPEN 1,1,2,"CHECKBOOK" 30 OPEN 3,4 When you actually apply the syntax conventions in a practical situa- tion, the sequence of parameters in your statements might not be exactly the same as the sequence shown in syntax examples. The examples are not meant to show every possible sequence. They are intended to present all required and optional parameters. INTRODUCTION xi ~ Programming examples in this book are shown with blanks separating words and operators for the sake of readability. Normally though, BASIC doesn't require blanks between words unless leaving them out would give you an ambiguous or incorrect syntax. Shown below are some examples and descriptions of the symbols used for various statement parameters in the following chapters. The list is not meant to show every possibility, but to give you a better understanding as to how syntax examples are presented. SYMBOL EXAMPLE DESCRIPTION 50 A logical file number 4 A hardware device number
15 A serial bus secondary device address number 0 A physical disk drive number "TEST.DATA" The name of a data or program file "ABCDEFG" Literal data supplied by the programmer X145 Any BASIC data variable name or constant AB$ Use of a string type variable required 12345 Use of a numeric type variable required 1000 An actual program line number 1.5E4 An integer or floating-point variable COMMODORE 64 APPLICATIONS GUIDE When you first thought about buying a computer you probably asked yourself, "Now that I can afford to buy a computer, what can I do with it once I get one?" The great thing about your COMMODORE 64 is that you can make it do what YOU want it to do! You can make it calculate and keep track of home and business budget needs. You can use it for word processing. You can make it play arcade-style action games. You can make it sing. You can even create your own animated cartoons, and more. The best part of owning a COMMODORE 64 is that even if it did only one of the things listed below it would be well worth the price you paid for it. But the 64 is a complete computer and it does do EVERYTHING listed and then some! xii INTRODUCTION ~ By the way, in addition to everything here you can pick up a lot of other creative and practical ideas by signing up with a local Commodore Users' Club, subscribing to the COMMODORE and POWER/PLAY magazines, and joining the COMMODORE INFORMATION NETWORK on CompuServe(TM) APPLICATION COMMENTS/REQUIREMENTS ACTION PACKED You can get real Bally Midway arcade games GAMES like Omega Race, Gorf and Wizard of War, as well as "play and learn" games like Math Teacher 1, Home Babysitter and Commodore Artist. ADVERTISING & Hook your COMMODORE 64 to a TV, put it in MERCHANDISING a store window with a flashing, animated, and musical message and you've got a great point of purchase store display. ANIMATION Commodore's Sprite Graphics allow you to create real cartoons with 8 different levels so that shapes can move in front of or behind each other. BABYSITTING The COMMODORE 64 HOME BABYSITTER cartridge can keep your youngest child occupied for hours and teach alphabet/ keyboard recognition at the same time. It also teaches special learning concepts and relationships. BASIC PROGRAMMING Your COMMODORE 64 USER'S GUIDE and the TEACH YOURSELF PROGRAMMING series of books and tapes offer an excellent starting point. BUSINESS The COMMODORE 64 offers the "Easy" series SPREADSHEET of business aids including the most powerful word processor and largest spreadsheet available for any personal computer. COMMUNICATION Enter the fascinating world of computer "net- working." If you hook a VICMODEM to your COMMODORE 64 you can communicate with other computer owners all around the world. INTRODUCTION xiii ~ Not only that, if you join the COMMODORE INFORMATION NETWORK on CompuServe(TM) you can get the latest news and updates on all Commodore products, financial information, shop at home services, you can even play games with the friends you make through the information systems you join. COMPOSING SONGS The COMMODORE 64 is equipped with the most sophisticated built-in music synthesizer available on any computer. It has three com- pletely programmable voices, nine full music octaves, and four controllable waveforms. Look for Commodore Music Cartridges and Commodore Music books to help you create or reproduce all kinds of music and sound effects. CP/M* Commodore offers a CP/M* add-on and access to software through an easy-to-load cartridge. DEXTERITY TRAINING Hand/Eye coordination and manual dexterity are aided by several Commodore games... including "Jupiter lander" and night driving simulation. EDUCATION While working with a computer is an education in itself, The COMMODORE Educational Resource Book contains general information on the educational uses of computers. We also have a variety of learning cartridges designed to teach everything from music to math and art to astronomy. FOREIGN LANGUAGE The COMMODORE 64 programmable character set lets you replace the standard character set with user defined foreign language characters. GRAPHICS AND ART In addition to the Sprite Graphics mentioned above, the COMMODORE 64 offers high-resolution, multi-color graphics plotting, programmable ----------- * CP/M is a Registered trademark of Digital Research, Inc. xiv INTRODUCTION ~ characters, and combinations of all the different graphics and character display modes. INSTRUMENT Your COMMODORE 64 has a serial port, RS-232 port CONTROL and a user port for use with a variety of special industrial applications. An IEEE/488 cartridge is also available as an optional extra. JOURNALS AND The COMMODORE 64 will soon offer an exceptional CREATIVE WRITING wordprocessing system that matches or exceeds the qualities and flexibilities of most "high- priced" wordprocessors available. Of course you can save the information on either a 1541 Disk Drive or a Datassette TM recorder and have it printed out using a VIC-PRINTER or PLOTTER. LIGHTPEN CONTROL Applications requiring the use of a lightpen can be performed by any lightpen that will fit the COMMODORE 64 game port connector. MACHINE CODE Your COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE PROGRAMMING includes a machine language section, as well as a BASIC to machine code interface section. There's even a bibliography available for more in-depth study. PAYROLL & FORMS The COMMODORE 64 can be programmed to handle PRINTOUT a variety of entry-type business applications. Upper/lower case letters combined with C64 "business form" graphics make it easy for you to design forms which can then be printed on your printer. PRINTING The COMMODORE 64 interfaces with a variety of dot matrix and letter quality printers as well as plotters. RECIPES You can store your favorite recipes on your COMMODORE 64 and its disk or cassette storage unit, and end the need for messy recipe cards that often get lost when you need them most. INTRODUCTION xv ~ SIMULATIONS Computer simulations let you conduct dangerous or expensive experiments at minimum risk and cost. SPORTS DATA The Source (TM) and CompuServe (TM) both offer sports information which you can get using your COMMODORE 64 and a VICMODEM. STOCK QUOTES With a VICMODEM and a subscription to any of the appropriate network services, your COMMODORE 64 becomes your own private stock ticker. These are just a few of the many applications for you and your COMMODORE 64. As you can see, for work or play, at home, in school or the office, your COMMODORE 64 gives you a practical solution for just about any need. Commodore wants you to know that our support for users only STARTS with your purchase of a Commodore computer. That's why we've created two publications with Commodore information from around the world, and a "two-way" computer information network with valuable input for users in the U.S. and Canada from coast to coast. In addition, we wholeheartedly encourage and support the growth of Commodore Users' Clubs around the world. They are an excellent source of information for every Commodore computer owner from the beginner to the most advanced. The magazines and network, which are more fully described below, have the most up-to-date information about how to get involved with the Users' Club in your area. Finally, your local Commodore dealer is a useful source of Commodore support and information. POWER/PLAY The Home Computer Magazine When it comes to entertainment, learning at home and practical home applications, POWER/PLAY is THE prime source of information for Com- modore home users. Find out where your nearest user clubs are and what they're doing, learn about software, games, programming techniques, telecommunications, and new products. POWER/PLAY is your personal connection to other Commodore users, outside software and hardware developers, and to Commodore itself. Published quarterly. Only $10.00 for a year of home computing excitement. xvi INTRODUCTION ~ COMMODORE The Microcomputer Magazine Widely read by educators, businessmen and students, as well as home computerists, COMMODORE Magazine is our main vehicle for sharing exclusive information on the more technical use of Commodore systems. Regular departments cover business, science and education, programming tips, "excerpts from a technical notebook," and many other features of interest to anyone who uses or is thinking about purchasing Commodore equipment for business, scientific or educational applications. COMMODORE is the ideal complement to POWER/PLAY. Published bimonthly. Subscription price: $15.00 per year. AND FOR EVEN MORE INFORMATION... ...DIAL UP OUR PAPERLESS USER MAGAZINE COMMODORE INFORMATION NETWORK The magazine of the future is here. To supplement and enhance your subscription to POWER/PLAY and COMMODORE magazines, the COMMODORE INFORMATION NETWORK - our "paperless magazine" - is available now over the telephone using your Commodore computer and modem. Join our computer club, get help with a computing problem, "talk" to other Commodore friends, or get up-to-the-minute information on new products, software and educational resources. Soon you will even be able to save yourself the trouble of typing in the program listings you find in POWER/PLAY or COMMODORE by downloading direct from the Information Network (a new user service planned for early 1983). The best part is that most of the answers are there before you even ask the questions. (How's that for service?) To call our electronic magazine you need only a modem and a sub- scription to CompuServe TM, one of the nation's largest telecommunica- tions networks. (To make it easy for you Commodore includes a FREE year's subscription to CompuServe TM in each VICMODEM package.) Just dial your local number for the CompuServe (TM) data bank and connect your phone to the modem. When the CompuServe (TM) video text appears on your screen type G CBM on your computer keyboard. When the COMMODORE INFORMATION NETWORK'S table of contents, or "menu," appears on your screen choose from one of our sixteen departments, make yourself comfortable, and enjoy the paperless magazine other magazines are writing about. INTRODUCTION xvii ~ For more information, visit your Commodore dealer or contact Com- puserve(TM) customer service at 800-848-8990 (in Ohio, 614-457-8600). COMMODORE INFORMATION NETWORK +-----------------------------------+-----------------------------------+ | Main Menu Description | Commodore Dealers | | Direct Access Codes | Educational Resources | | Special Commands | User Groups | | User Questions | Descriptions | | Public Bulletin Board | Questions and Answers | | Magazines and Newsletters | Software Tips | | Products Announced | Technical Tips | | Commodore News Direct | Directory Descriptions | +-----------------------------------+-----------------------------------+ xviii INTRODUCTION ~ CHAPTER 1 BASIC PROGRAMMING RULES o Introduction o Screen Display Codes (BASIC Character Set) o Programming Numbers and variables o Expressions and Operators o Programming Techniques 1 ~ INTRODUCTION This chapter talks about how BASIC stores and manipulates data. The topics include: 1) A brief mention of the operating system components and functions as well as the character set used in the Commodore 64. 2) The formation of constants and variables. What types of variables there are. And how constants and variables are stored in memory. 3) The rules for arithmetic calculations, relationship tests, string handling, and logical operations. Also included are the rules for forming expressions, and the data conversions necessary when you're using BASIC with mixed data types. SCREEN DISPLAY CODES (BASIC CHARACTER SET) THE OPERATING SYSTEM (OS) The Operating System is contained in the Read Only Memory (ROM) chips and is a combination of three separate, but interrelated, program modules. 1) The BASIC Interpreter 2) The KERNAL 3) The Screen Editor 1) The BASIC Interpreter is responsible for analysing BASIC statement syntax and for performing the required calculations and/or data manipulation. The BASIC Interpreter has a vocabulary of 65 "keywords" which have special meanings. The upper and lower case alphabet and the digits 0-9 are used to make both keywords and variable names. Certain punctuation characters and special symbols also have meanings for the Interpreter. Table 1-1 lists the special characters and their uses. 2) The KERNAL handles most of the interrupt level processing in the system (for details on interrupt level processing, see Chapter 5). The KERNAL also does the actual input and output of data. 3) The Screen Editor controls the output to the video screen (tele- vision set) and the editing of BASIC program text. In addition, the Screen Editor intercepts keyboard input so that it can decide whether the characters put in should be acted upon immediately, or passed on to the BASIC Interpreter. 2 BASIC PROGRAMMING RULES ~ Table 1 - 1. CBM BASIC Character Set +-------------+---------------------------------------------------------+ | CHARACTER | NAME and DESCRIPTION | +-------------+---------------------------------------------------------+ | | BLANK - separates keywords and variable names | | ; | SEMI-COLON - used in variable lists to format output | | = | EQUAL SIGN - value assignment and relationship testing | | + | PLUS SIGN - arithmetic addition or string concatenation | | | (concatenation: linking together in a chain) | | - | MINUS SIGN - arithmetic subtraction, unary minus | | * | ASTERISK - arithmetic multiplication | | / | SLASH - arithmetic division | | ^ | UP ARROW - arithmetic exponentiation | | ( | LEFT PARENTHESIS - expression evaluation and functions | | ) | RIGHT PARENTHESIS - expression evaluation and functions | | % | PERCENT - declares variable name as an integer | | # | NUMBER - comes before logical file number in input/ | | | output statements | | $ | DOLLAR SIGN - declares variable name as a string | | , | COMMA - used in variable lists to format output; also | | | separates command parameters | | . | PERIOD - decimal point in floating point constants | | " | QUOTATION MARK - encloses string constants | | : | COLON - separates multiple BASIC statements in a line | | ? | QUESTION MARK - abbreviation for the keyword PRINT | | < | LESS THAN - used in relationship tests | | > | GREATER THAN - used in relationship tests | | {pi} | PI - the numeric constant 3.141592654 | +-------------+---------------------------------------------------------+ The Operating System gives you two modes of BASIC operation: 1) DIRECT Mode 2) PROGRAM Mode 1) When you're using the DIRECT mode, BASIC statements don't have line numbers in front of the statement. They are executed whenever the key is pressed. 2) The PROGRAM mode is the one you use for running programs. BASIC PROGRAMMING RULES 3 ~ When using the PROGRAM mode, all of your BASIC statements must have line numbers in front of them. You can have more than one BASIC statement in a line of your program, but the number of statements is limited by the fact that you can only put 80 characters on a logical screen line. This means that if you are going to go over the 80 character limit you have to put the entire BASIC statement that doesn't fit on a new line with a new line number. Always type NEW and hit before starting a new program. The Commodore 64 has two complete character sets that you can use either from the keyboard or in your programs. In SET 1, the upper case alphabet and the numbers 0-9 are available without pressing the key. If you hold down the key while typing, the graphics characters on the RIGHT side of the front of the keys are used. If you hold down the key while typing, the graphics characters on the LEFT side of the front of the key are used. Holding down the key while typing any character that doesn't have graphic symbols on the front of the key gives you the symbol on the top most part of the key. In SET 2, the lower case alphabet and the numbers 0-9 are available without pressing the key. The upper case alphabet is available when you hold down the key while typing. Again, the graphic symbols on the LEFT side of the front of the keys are displayed by press- ing the key, while the symbols on the top most part of any key without graphics characters are selected when you hold down the key while typing. To switch from one character set to the other press the and the keys together. PROGRAMMING NUMBERS AND VARIABLES INTEGER, FLOATING-POINT AND STRING CONSTANTS Constants are the data values that you put in your BASIC statements. BASIC uses these values to represent data during statement execution. CBM BASIC can recognize and manipulate three types of constants: 1) INTEGER NUMBERS 2) FLOATING-POINT NUMBERS 3) STRINGS 4 BASIC PROGRAMMING RULES ~ Integer constants are whole numbers (numbers without decimal points). Integer constants must be between -32768 and +32767. Integer constants do not have decimal points or commas between digits. If the plus (+) sign is left out, the constant is assumed to be a positive number. Zeros coming before a constant are ignored and shouldn't be used since they waste memory and slow down your program. However, they won't cause an error. Integers are stored in memory as two-byte binary numbers. Some examples of integer constants are: -12 8765 -32768 +44 0 -32767 +-----------------------------------------------------------------------+ | NOTE: Do NOT put commas inside any number. For example, always type | | 32,000 as 32000. If you put a comma in the middle of a number you | | will get the BASIC error message ?SYNTAX ERROR. | +-----------------------------------------------------------------------+ Floating-point constants are positive or negative numbers and can contain fractions. Fractional parts of a number may be shown using a decimal point. Once again remember that commas are NOT used between numbers. If the plus sign (+) is left off the front of a number, the Commodore 64 assumes that the number is positive. If you leave off the decimal point the computer will assume that it follows the last digit of the number. And as with integers, zeros that come before a constant are ignored. Floating-point constants can be used in two ways: 1) SIMPLE NUMBER 2) SCIENTIFIC NOTATION Floating-point constants will show you up to nine digits on your screen. These digits can represent values between -999999999. and +999999999. If you enter more than nine digits the number will be rounded based on the tenth digit. if the tenth digit is greater than or equal to 5 the number will be rounded upward. Less than 5 the number be rounded downward. This could be important to the final totals of some numbers you may want to work with. Floating-point numbers are stored (using five bytes of memory) and are manipulated in calculations with ten places of accuracy. However, BASIC PROGRAMMING RULES 5 ~ the numbers are rounded to nine digits when results are printed. Some examples of simple floating-point numbers are: 1.23 .7777777 -.998877 -333. +3.1459 .01 Numbers smaller than .01 or larger than 999999999. will be printed in scientific notation. In scientific notation a floating-point constant is made up of three parts: 1) THE MANTISSA 2) THE LETTER E 3) THE EXPONENT The mantissa is a simple floating-point number. The letter E is used to tell you that you're seeing the number in exponential form. In other words E represents * 10 (eg., 3E3 = 3*10^3 = 3000). And the exponent is what multiplication power of 10 the number is raised to. Both the mantissa and the exponent are signed (+ or -) numbers. The exponent's range is from -39 to +38 and it indicates the number of places that the actual decimal point in the mantissa would be moved to the left (-) or right (+) if the value of the constant were represented as a simple number. There is a limit to the size of floating-point numbers that BASIC can handle, even in scientific notation: the largest number is +1.70141183E+38 and calculations which would result in a larger number will display the BASIC error message ?OVERFLOW ERROR. The smallest floating-point number is +2.93873588E-39 and calculations which result in a smaller value give you zero as an answer and NO error message. Some examples of floating-point numbers in scientific notation (and their decimal values) are: 235.988E-3 (.235988) 2359E6 (2359000000.) -7.09E-12 (-.00000000000709) -3.14159E+5 (-314159.) String constants are groups of alphanumeric information like letters, numbers and symbols. When you enter a string from the keyboard, it can have any length up to the space available in an 80-character line 6 BASIC PROGRAMMING RULES ~ (that is, any character spaces NOT taken up by the line number and other required parts of the statement). A string constant can contain blanks, letters, numbers, punctuation and color or cursor control characters in any combination. You can even put commas between numbers. The only character which cannot be included in a string is the double quote mark ("). This is because the double quote mark is used to define the beginning and end of the string. A string can also have a null value-which means that it can contain no character data. You can leave the ending quote mark off of a string if it's the last item on a line or if it's followed by a colon (:). Some examples of string constants are: "" ( a null string) "HELLO" "$25,000.00" "NUMBER OF EMPLOYEES" +-----------------------------------------------------------------------+ | NOTE: Us CHR$(34) to include quotes (") in strings. | +-----------------------------------------------------------------------+ INTEGER, FLOATING-POINT AND STRING VARIABLES Variables are names that represent data values used in your BASIC statements. The value represented by a variable can be assigned by setting it equal to a constant, or it can be the result of calculations in the program. Variable data, like constants, can be integers, floating- point numbers, or strings. If you refer to a variable name in a program before a value has been assigned, the BASIC Interpreter will auto- matically create the variable with a value of zero if it's an integer or floating-point number. Or it will create a variable with a null value if you're using strings. Variable names can be any length but only the first two characters are considered significant in CBM BASIC. This means that all names used for variables must NOT have the same first two characters. Variable names may NOT be the same as BASIC keywords and they may NOT contain keywords in the middle of variable names. Keywords include all BASIC commands, state- ments, function names and logical operator names. If you accidentally use a keyword in the middle of a variable name, the BASIC error message ?SYNTAX ERROR will show up on your screen. The characters used to form variable names are the alphabet and the numbers 0-9. The first character of the name must be a letter. Data BASIC PROGRAMMING RULES 7 ~ type declaration characters (%) and ($) can be used as the last char- acter of the name. The percent sign declares the variable to be an integer and the dollar sign ($) declares a string variable. If no type declaration character is used the Interpreter will assume that the vari- able is a floating-point. Some examples of variable names, value as- signments and data types are: A$="GROSS SALES" (string variable) MTH$="JAN"+A$ (string variable) K%=5 (integer variable) CNT%=CNT%+1 (integer variable) FP=12.5 (floating-point variable) SUM=FP*CNT% (floating-point variable) INTEGER, FLOATING-POINT AND STRING ARRAYS An array is a table (or list) of associated data items referred to by a single variable name. In other words, an array is a sequence of related variables. A table of numbers can be seen as an array, for example. The individual numbers within the table become "elements" of the array. Arrays are a useful shorthand way of describing a large number of related variables. Take a table of numbers for instance. Let's say that the table has 10 rows of numbers with 20 numbers in each row. That makes total of 200 numbers in the table. Without a single array name to call on you would have to assign a unique name to each value in the table. But because you can use arrays you only need one name for the array and all the elements in the array are identified by their individual locations within the array. Array names can be integers, floating-points or string data types and all elements in the array have the same data type as the array name. Arrays can have a single dimension (as in a simple list) or they can have multiple dimensions (imagine a grid marked in rows and columns or a Rubik's Cube(R)). Each element of an array is uniquely identified and re- ferred to by a subscript (or index variable) following the array name, enclosed within parentheses ( ). The maximum number of dimensions an array can have in theory is 255 and the number of elements in each dimension is limited to 32767. But for practical purposes array sizes are limited by the memory space available to hold their data and/or the 80 character logical screen line. If an array has only one dimension and its subscript value will never 8 BASIC PROGRAMMING RULES ~ exceed 1 0 (1 I items: 0 thru 1 0) then the array will be created by the Interpreter and filled with zeros (or nulls if string type) the first time any element of the array is referred to, otherwise the BASIC DIM statement must be used to define the shape and size of the array. The amount of memory required to store an array can be determined as follows: 5 bytes for the array name + 2 bytes for each dimension of the array + 2 bytes per element for integers OR + 5 bytes per element for floating-point OR + 3 bytes per element for strings AND + 1 byte per character in each string element Subscripts can be integer constants, variables, or an arithmetic ex- pression which gives an integer result. Separate subscripts, with com- mas between them, are required for each dimension of an array. Sub- scripts can have values from zero up to the number of elements in the respective dimensions of the array. Values outside that range will cause the BASIC error message ?BAD SUBSCRIPT. Some examples of array names, value assignments and data types are: A$(0)="GROSS SALES" (string array) MTH$(K%)="JAN" (string array) G2%(X)=5 (integer array) CNT%(G2%(X))=CNT%(1)-2 (integer array) FP(12*K%)=24.8 (floating-point array) SUM(CNT%(1))=FP^K% (floating-point array) A(5)=0 (sets the 5th element in the 1 dimensional array called "A" equal to 0) B(5,6)=0 (sets the element in row position 5 and column position 6 in the 2 dimensional array called "B" equal to 0) C(1,2,3)=0 (sets the element in row position 1, column position 2, and depth position 3 in the 3 dimensional array called "C" equal to 0) EXPRESSIONS AND OPERATORS Expressions are formed using constants, variables and/or arrays. An expression can be a single constant, simple variable, or an array vari- BASIC PROGRAMMING RULES 9 ~ able of any type. It can also be a combination of constants and variables with arithmetic, relational or logical operators designed to produce a ingle value. How operators work is explained below. Expressions can be separated into two classes: 1) ARITHMETIC 2) STRING Expressions are normally thought of as having two or more data items called operands. Each operand is separated by a single operator to produce the desired result. This is usually done by assigning the value of the expression to a variable name. All of the examples of constants and variables that you've seen so for, were also examples of expressions. An operator is a special symbol the BASIC Interpreter in your Commodore 64 recognizes as representing an operation to be performed on the variables or constant data. One or more operators, combined with one or more variables and/or constants form an expression. Arithmetic, relational and logical operators are recognized by Commodore 64 BASIC. ARITHMETIC EXPRESSIONS Arithmetic expressions, when solved, will give an integer or floating- point value. The arithmetic operators (+, -, *, /, ^) are used to perform addition, subtraction, multiplication, division and exponentiation opera- tions respectively. ARITHMETIC OPERATIONS An arithmetic operator defines an arithmetic operation which is per- formed on the two operands on either side of the operator. Arithmetic operations are performed using floating-point numbers. Integers are converted to floating-point numbers before an arithmetic operation is performed. The result is converted back to an integer if it is assigned to an integer variable name. ADDITION (+): The plus sign (+) specifies that the operand on the right is added to the operand on the left. 10 BASIC PROGRAMMING RULES ~ EXAMPLES: 2+2 A+B+C X%+1 BR+10E-2 SUBTRACTION (-): The minus sign (-) specifies that the operand on the right is subtracted from the operand on the left. EXAMPLES: 4-1 100-64 A-B 55-142 The minus can also be used as a unary minus. That means that it is the minus sign in front of a negative number. This is equal to subtracting the number from zero (0). EXAMPLES: -5 -9E4 -B 4-(-2) same as 4+2 MULTIPLICATION (*): An asterisk (*) specifies that the operand on the left is multiplied by the operand on the right. EXAMPLES: 100*2 50*0 A*X1 R%*14 DIVISION (/): The slash (/) specifies that the operand on the left is divided by the operand on the right. EXAMPLES: 10/2 6400/4 A/B 4E2/XR BASIC PROGRAMMING RULES 11 ~ EXPONENTIATION The up arrow (^) specifies that the operand on the left is raised to the power specified by the operand on the right (the exponent). If the operand on the right is a 2, the number on the left is squared; if the exponent is a 3, the number on the left is cubed, etc. The exponent can be any number so long as the result of the operation gives a valid floating-point number. EXAMPLES: 2^2 Equivalent to: 2*2 3^3 Equivalent to: 3*3*3 4^4 Equivalent to: 4*4*4*4 AB^CD 3^-2 Equivalent to: 1/3*1/3 RELATIONAL OPERATORS The relational operators (<, =, >, <=, >=, <>) are primarily used to compare the values of two operands, but they also produce an arith- metic result. The relational operators and the logical operators (AND, OR, and NOT), when used in comparisons, actually produce an arithmetic true/false evaluation of an expression. If the relationship stated in the expression is true the result is assigned an integer value of - 1 and if it's false a value of 0 is assigned. These are the relational operators: < LESS THAN = EQUAL TO > GREATER THAN <= LESS THAN OR EQUAL TO >= GREATER THAN OR EQUAL TO <> NOT EQUAL TO EXAMPLES: 1 =5-4 result true (-1) 14>66 result false (0) 15>=15 result true (-1) Relational operators can be used to compare strings. For comparison purposes, the letters of the alphabet have the order A CC$ Numeric data items can only be compared (or assigned) to other numeric items. The same is true when comparing strings, otherwise the BASIC error message ?TYPE MISMATCH will occur. Numeric operands are compared by first converting the values of either or both operands from integer to floating-point form, as necessary. Then the relationship of the floating- point values is evaluated to give a true/false result. At the end of all comparisons, you get an integer no matter what data type the operand is (even if both are strings). Because of this, a comparison of two operands can be used as an operand in performing calculations. The result will be - 1 or 0 and can be used as anything but a divisor, since division by zero is illegal. LOGICAL OPERATORS The logical operators (AND, OR, NOT) can be used to modify the meanings of the relational operators or to produce an arithmetic result. Logical operators can produce results other than -1 and 0, though any nonzero result is considered true when testing for a true/false condition. The logical operators (sometimes called Boolean operators) can also be used to perform logic operations on individual binary digits (bits) in two operands. But when you're using the NOT operator, the operation is performed only on the single operand to the right. The operands must be in the integer range of values (-32768 to +32767) (floating-point numbers are converted to integers) and logical operations give an integer result. Logical operations are performed bit-by-corresponding-bit on the two operands. The logical AND produces a bit result of 1 only if both operand bits are 1. The logical OR produces a bit result of I if either operand bit is 1. The logical NOT is the opposite value of each bit as a single operand. In other words, it's really saying, "if it's NOT 1 then it is 0. If it's NOT 0 then it is 1." The exclusive OR (XOR) doesn't have a logical operator but it is per- formed as part of the WAIT statement. Exclusive OR means that if the bits of two operands are equal then the result is 0 otherwise the result is 1. Logical operations are defined by groups of statements which, taken together, constitute a Boolean "truth table" as shown in Table 1-2. BASIC PROGRAMMING RULES 13 ~ Table 1-2. Boolean Truth Table +-----------------------------------------------------------------------+ | The AND operation results in a 1 only if both bits are 1: | | | | 1 AND 1 = 1 | | 0 AND 1 = 0 | | 1 AND 0 = 0 | | 0 AND 0 = 0 | | | | The OR operation results in a 1 if either bit is 1: | | | | 1 OR 1 = 1 | | 0 OR 1 = 1 | | 0 OR 0 = 1 | | 0 OR 0 = 0 | | | | The NOT operation logically complements each bit: | | | | NOT 1 = 0 | | NOT 0 = 1 | | | | The exclusive OR (XOR) is part of the WAIT statement! | | | | 1 XOR 1 = 0 | | 1 XOR 0 = 1 | | 0 XOR 1 = 1 | | 0 XOR 0 = 0 | +-----------------------------------------------------------------------+ The logical operators AND, OR and NOT specify a Boolean arithmetic operation to be performed on the two operand expressions on either side of the operator. In the case of NOT, ONLY the operand on the RIGHT is considered. Logical operations (or Boolean arithmetic) aren't performed until all arithmetic and relational operations in an expression have been completed. EXAMPLES: IF A=100 AND B=100 THEN 10 (if both A and B have a value of 100 then the result is true) A=96 AND 32: PRINT A (A = 32) 14 BASIC PROGRAMMING RULES ~ IF A=100 OR B=100 THEN 20 (if A or B is 100 then the result is true) A=64 OR 32: PRINT A (A = 96) IF NOT X=Y the result is true) X= NOT 96 (result is -97 (two's complement)) HIERARCHY OF OPERATIONS All expressions perform the different types of operations according to a fixed hierarchy. In other words, certain operations are performed be- fore other operations. The normal order of operations can be modified by enclosing two or more operands within parentheses ( ), creating a "subexpression." The parts of an expression enclosed in parentheses will be reduced to a single value before working on parts outside the par- entheses. When you use parentheses in expressions, they must be paired so that you always have an equal number of left and right parentheses. Otherwise, the BASIC error message ?SYNTAX ERROR will appear. Expressions which have operands inside parentheses may themselves be enclosed in parentheses, forming complex expressions of multiple levels. This is called nesting. Parentheses can be nested in expressions to a maximum depth of ten levels-ten matching sets of parentheses. The inner-most expression has its operations performed first. Some examples of expressions are: A+B C^(D+E)/2 ((X-C^(D+E)/2)*10)+1 GG$>HH$ JJ$+"MORE" K%=1 AND M<>X K%=2 OR (A=B AND M = < | Relational Operations | A <= B | | | | | | NOT | Logical NOT | NOT K% | | | (Integer Two's Complement) | | | | | | | AND | Logical AND | JK AND 128 | | | | | | OR | Logical OR | PQ OR 15 | +---------------+---------------------------------+---------------------+ STRING OPERATIONS Strings are compared using the same relational operators (=, <>, <=, >=, <, >) that are used for comparing numbers. String compari- sons are mode by taking one character at a time (left-to-right) from each string and evaluating each character code position from the PET/ CBM character set. If the character codes are the same, the characters are equal. If the character codes differ, the character with the lower code number is lower in the character set. The comparison stops when 16 BASIC PROGRAMMING RULES ~ the end of either string is reached. All other things being equal, the shorter string is considered less than the longer string. Leading or trailing blanks ARE significant. Regardless of the data types, at the end of all comparisons you get an integer result. This is true even if both operands are strings. Because of this a comparison of two string operands can be used as an operand in performing calculations. The result will be - 1 or 0 (true or false) and can be used as anything but a divisor since division by zero is illegal. STRING EXPRESSIONS Expressions are treated as if an implied "<>0" follows them. This means that if an expression is true then the next BASIC statements on. the same program line are executed. If the expression is false the rest of the line is ignored and the next line in the program is executed. Just as with numbers, you can also perform operations on string vari- ables. The only string arithmetic operator recognized by CBM BASIC is the plus sign (+) which is used to perform concatenation of strings. When strings are concatenated, the string on the right of the plus sign is appended to the string on the left, forming a third string as a result. The result can be printed immediately, used in a comparison, or assigned to a variable name. If a string data item is compared with (or set equal to) a numeric item, or vice-versa, the BASIC error message ?TYPE MISMATCH will occur. Some examples of string expressions and concatenation are: 10 A$="FILE": B$="NAME" 20 NAM$=A$+B$ (gives the string: FILENAME) 30 RES$="NEW "+A$+B$ (gives the string: NEW FILENAME) ^ | +-----------------+ +-------+ Note space here.| +-----------------+ BASIC PROGRAMMING RULES 17 ~ PROGRAMMING TECHNIQUES DATA CONVERSIONS When necessary, the CBM BASIC Interpreter will convert a numeric data item from an integer to floating-point. or vice-versa, according to the following rules: o All arithmetic and relational operations are performed in floating point format. Integers are converted to floating-point form for evaluation of the expression, and the result is converted back to integer. logical operations convert their operands to integers an return an integer result. o If a numeric variable name of one type is set equal to a numeric data item of a different type, the number will be converted and stored as the data type declared in the variable name. o When a floating-point value is converted to an integer, the frac- tional portion is truncated (eliminated) and the integer result is less than or equal to the floating-point value. If the result is outside the range of +32767 thru -32768, the BASIC error message ?ILLEGAL QUANTITY will occur. USING THE INPUT STATEMENT Now that you know what variables are, let's take that information an put it together with the INPUT statement for some practical program- ming applications. In our first example, you can think of a variable as a "storage com- partment" where the Commodore 64 stores the user's response to your prompt question. To write a program which asks the user to type in a name, you might assign the variable N$ to the name typed in. Now every time you PRINT N$ in your program, the Commodore 64 will automatically PRINT the name that the user typed in. Type the word NEW on your Commodore 64. Hit the key and try this example: 10 PRINT"YOUR NAME": INPUT N$ 20 PRINT"HELLO",N$ 18 BASIC PROGRAMMING RULES ~ In this example you used N to remind yourself that this variable stands for "NAME". The dollar sign ($) is used to tell the computer that you're using a string variable. It is important to differentiate between the two types of variables: 1) NUMERIC 2) STRING You probably remember from the earlier sections that numeric vari- ables are used to store number values such as 1, 100, 4000, etc. A numeric variable can be a single letter (A), any two letters (AB), a letter and a number (AI), or two letters and a number (AB1). You can save memory space by using shorter variables. Another helpful hint is to use letters and numbers for different categories in the same program (AI, A2, A3). Also, if you want whole numbers for an answer instead of numbers with decimal points, all you have to do is put a percent sign (%) at the end of your variable name (AB%, AI%, etc.) Now let's look at a few examples that use different types of variables and expressions with the INPUT statement. 10 PRINT"ENTER A NUMBER": INPUT A 20 PRINT A 10 PRINT"ENTER A WORD": INPUT A$ 20 PRINT A$ 10 PRINT"ENTER A NUMBER": INPUT A 20 PRINT A "TIMES 5 EQUALS" A*5 +-----------------------------------------------------------------------+ | NOTE: Example 3 shows that MESSAGES or PROMPTS are inside the | | quotation marks (" ") while the variables are outside. Notice, too, | | that in line 20 the variable A was printed first, then the message | | "TIMES 5 EQUALS", and then the calculation, multiply variable A by 5 | | (A*5). | +-----------------------------------------------------------------------+ Calculations are important in most programs. You have a choice of using "actual numbers" or variables when doing calculations, but if you're working with numbers supplied by a user you must use numeric variables. Begin by asking the user to type in two numbers like this: 10 PRINT"TYPE 2 NUMBERS": INPUT A: INPUT B BASIC PROGRAMMING RULES 19 ~ INCOME/EXPENSE BUDGET EXAMPLE start tok64 page20.prg 5 print"{clear}" 10 print"monthly income":input in 20 print 30 print"expense category 1":input e1$ 40 print"expense amount":input e1 50 print 60 print"expense category 2":input e2$ 70 print"expense amount":input e2 80 print 90 print"expense category 3":input e3$ 100 print"expense amount":input e3 110 print"{clear}" 120 e=e1+e2+e3 130 ep=e/in 140 print"monthly income: $"in 150 print"total expenses: $"e 160 print"balance equals: $"in-e 170 print 180 print e1$"="(e1/e)*100"% of total expenses" 190 print e2$"="(e2/e)*100"% of total expenses" 200 print e3$"="(e3/e)*100"% of total expenses" 210 print 220 print"your expenses="ep*100"% of your total income" 230 forx=1to5000:next:print 240 print"repeat? (y or n)":input y$:if y$="y"then 5 250 print"{clear}":end stop tok64 +-----------------------------------------------------------------------+ | NOTE:IN can NOT = 0, and E1, E2, E3 can NOT all be 0 at the same time.| +-----------------------------------------------------------------------+ 20 BASIC PROGRAMMING RULES ~ LINE-BY-LINE EXPLANATION OF INCOME/EXPENSE BUDGET EXAMPLE +-----------+-----------------------------------------------------------+ | Line(s) | Description | +-----------+-----------------------------------------------------------+ | 5 | Clears the screen. | | 10 | PRINT/INPUT statement. | | 20 | Inserts blank line. | | 30 | Expense Category 1 = E1$. | | 40 | Expense Amount = E1. | | 50 | Inserts blank line. | | 60 | Expense Category 2 = E2. | | 70 | Expense Amount 2 = E2. | | 80 | Inserts blank line. | | 90 | Expense Category 3 = E3. | | 100 | Expense Amount 3 = E3. | | 110 | Clears the screen. | | 120 | Add Expense Amounts = E. | | 130 | Calculate Expense/income%. | | 140 | Display Income. | | 150 | Display Total Expenses. | | 160 | Display Incomes - Expenses. | | 170 | Inserts blank line. | | 180-200 | lines 180-200 calculate % each expense | | | amount is of total expenses. | | 210 | Inserts blank line. | | 220 | Display E/IN %. | | 230 | Time delay loop. | +-----------+-----------------------------------------------------------+ Now multiply those two numbers together to create a new variable C as shown in line 20 below: 20 C=A*B To PRINT the result as a message type 30 PRINT A "TIMES" B "EQUALS" C Enter these 3 lines and RUN the program. Notice that the messages are inside the quotes while the variables are not. BASIC PROGRAMMING RULES 21 ~ Now let's say that you wanted a dollar sign ($) in front of the number represented by variable C. The $ must be PRINTed inside quotes and in front of variable C. To add the $ to your program hit the and keys. Now type in line 40 as follows: 40 PRINT"$" C Now hit , type RUN and hit again. The dollar sign goes in quotes because the variable C only represents a number and can't contain a $. If the number represented by C was 100 then the Commodore 64 screen would display $ 100. But, if you tried to PRINT $C without using the quotes, you would get a ?SYNTAX ERROR message. One last tip about $$$: You can create a variable that represents a dollar sign which you can then substitute for the $ when you want to use it with numeric variables. For example: 10 Z$="$" Now whenever you need a dollar sign you can use the string variable Z$. Try this: 10 Z$="$": INPUT A 20 PRINT Z$A line 10 defines the $ as a string variable called Z$, and then INPUTs a number called A. line 20 PRINTs Z$ ($) next to A (number). You'll probably find that it's easier to assign certain characters, like dollar signs, to a string variable than to type "$" every time you want to calculate dollars or other items which require "" like %. USING THE GET STATEMENT Most simple programs use the INPUT statement to get data from the person operating the computer. When you're dealing with more complex needs, like protection from typing errors, the GET statement gives you more flexibility and your program more "intelligence." This section shows you how to use the GET statement to add some special screen editing features to your programs. 22 BASIC PROGRAMMING RULES ~ The Commodore 64 has a keyboard buffer that holds up to 10 characters. This means that if the computer is busy doing some operation and it's not reading the keyboard, you can still type in up to 10 characters, which will be used as soon as the Commodore 64 finishes what it was doing. To demonstrate this, type in this program on your Commodore 64: NEW 10 TI$="000000" 20 IF TI$ < "000015" THEN 20 Now type RUN, hit and while the program is RUNning type in the word HELLO. Notice that nothing happened for about IS seconds when the program started. Only then did the message HELLO appear on the screen. Imagine standing in line for a movie. The first person in the line is the first to get a ticket and leave the line. The last person in line is last for a ticket. The GET statement acts like a ticket taker. First it looks to see if there are any characters "in line." In other words have any keys been typed. If the answer is yes then that character gets placed in the appropriate variable. If no key was pressed then an empty value is assigned to a variable, At this point it's important to note that if you try to put more than 10 characters into the buffer at one time, all those over the 10th character will be lost. Since the GET statement will keep going even when no character is typed, it is often necessary to put the GET statement into a loop so that it will have to wait until someone hits a key or until a character is received through your program. Below is the recommended form for the GET statement. Type NEW to erase your previous program. 10 GET A$: IF A$ ="" THEN 10 Notice that there is NO SPACE between the quote marks("") on this line. This indicates an empty value and sends the program back to the GET statement in a continuous loop until someone hits a key on the computer. Once a key is hit the program will continue with the line following line 10. Add this line to your program: 100 PRINT A$;: GOTO 10 BASIC PROGRAMMING RULES 23 ~ Now RUN the program. Notice that no cursor appears on the screen, but any character you type will be printed in the screen. This 2-line program can be turned into part of a screen editor program as shown below. There are many things you can do with a screen editor. You can have a flashing cursor. You can keep certain keys like from accidentally erasing the whole screen. You might even want to be able to use your function keys to represent whole words or phrases. And speaking of function keys, the following program lines give each function key a special purpose. Remember this is only the beginning of a program that you can customize for your needs. 20 IF A$ = CHR$(133) THEN POKE 53280,8: GOTO 10 30 IF A$ = CHR$(134) THEN POKE 53281,4: GOTO 10 40 IF A$ = CHR$(135) THEN A$="DEAR SIR:"+CHR$(13) 50 IF A$ = CHR$(136) THEN A$="SINCERELY,"+CHR$(13) The CHR$ numbers in parentheses come from the CHR$ code chart in Appendix C. The chart lists a different number for each character. The four function keys are set up to perform the tasks represented by the instructions that follow the word THEN in each line. By changing the CHR$ number inside each set of parentheses you can designate different keys. Different instructions would be performed if you changed the information after the THEN statement. HOW TO CRUNCH BASIC PROGRAMS You can pack more instructions - and power - into your BASIC programs by making each program as short as possible. This process of shortening programs is called "crunching." Crunching programs lets you squeeze the maximum possible number of instructions into your program. It also helps you reduce the size of programs which might not otherwise run in a given size; and if you're writing a program which requires the input of data such as inventory items, numbers or text, a short program will leave more memory space free to hold data. ABBREVIATING KEYWORDS A list of keyword abbreviations is given in Appendix A. This is helpful when you program because you can actually crowd more information on each line using abbreviations. The most frequently used abbreviation is 24 BASIC PROGRAMMING RULES ~ the question mark (?) which is the BASIC abbreviation for the PRINT command. However, if you LIST a program that has abbreviations, the Commodore 64 will automatically print out the listing with the full- length keywords. If any program line exceeds 80 characters (2 lines on the screen) with the keywords unabbreviated, and you want to change it, you will have to re-enter that line with the abbreviations before saving the program. SAVEing a program incorporates the keywords without inflating any lines because BASIC keywords are tokenized by the Commodore 64. Usually, abbreviations are added after a program is written and it isn't going to be LISTed any more before SAVEing. SHORTENING PROGRAM LINE NUMBERS Most programmers start their programs at line 100 and number each fine at intervals of 10 (i.e., 100, 110, 120). This allows extra lines of instruction to be added (111, 112, etc.) as the program is developed. One means of crunching the program after it is completed is to change the fine numbers to the lowest numbers possible (i.e., 1, 2, 3) because longer line numbers take more memory than shorter numbers when referenced by GOTO and GOSUB statements. For instance, the number 100 uses 3 bytes of memory (one for each number) while the number I uses only 1 byte. PUTTING MULTIPLE INSTRUCTIONS ON EACH LINE You can put more than one instruction on each numbered line in your program by separating them by a colon. The only limitation is that all the instructions on each line, including colons, should not exceed the standard 80-character line length. Here is an example of two programs, before and after crunching: BEFORE CRUNCHING: AFTER CRUNCHING: 10 PRINT"HELLO..."; 10 PRINT "HELLO...";:FORT=1TO500:NEXT: 20 FOR T=1 TO 500: NEXT PRINT"HELLO, AGAIN...":GOTO10 30 PRINT"HELLO, AGAIN..." 40 GOTO 10 REMOVING REM STATEMENTS REM statements are helpful in reminding yourself-or showing other programmers - what a particular section of a program is doing. However, when the program is completed and ready to use, you probably BASIC PROGRAMMING RULES 25 ~ won't need those REM statements anymore and you can save quite a bit of space by removing the REM statements. If you plan to revise or study the program structure in the future, it's a good idea to keep a copy on file with the REM statements intact. USING VARIABLES If a number, word or sentence is used repeatedly in your program it's usually best to define those long words or numbers with a one or two letter variable. Numbers can be defined as single letters. Words and sentences can be defined as string variables using a letter and dollar sign. Here's one example: BEFORE CRUNCHING: AFTER CRUNCHING: 10 POKE 54296,15 10 V=54296:F=54273 20 POKE 54276,33 20 POKEV,15:POKE54276,33 30 POKE 54273,10 30 POKEF,10:POKEF,40:POKEF,70 40 POKE 54273,40 40 POKEV,0 50 POKE 54273,70 60 POKE 54296,0 USING READ AND DATA STATEMENTS Large amounts of data can be typed in as one piece of data at a time, over and over again ... or you can print the instructional part of the program ONCE and print all the data to be handled in a long running list called the DATA statement. This is especially good for crowding large lists of numbers into a program. USING ARRAYS AND MATRICES Arrays and matrices are similar to DATA statements in that long amounts of data can be handled as a list, with the data handling portion of the program drawing from that list, in sequence. Arrays differ in that the list can be multi-dimensional ELIMINATING SPACES One of the easiest ways to reduce the size of your program is to eliminate all the spaces. Although we often include spaces in sample 26 BASIC PROGRAMMING RULES ~ programs to provide clarity, you actually don't need any spaces in your program and will save space if you eliminate them. USING GOSUB ROUTINES If you use a particular line or instruction over and over, it might be wise to GOSUB to the line from several places in your program, rather than write the whole line or instruction every time you use it. USING TAB AND SPC Instead of PRINTing several cursor commands to position a character on the screen, it is often more economical to use the TAB and SPC in- structions to position words or characters on the screen. BASIC PROGRAMMING RULES 27 ~~ CHAPTER 2 BASIC LANGUAGE VOCABULARY o Introduction o BASIC Keywords, Abbreviations, and Function Types o Description of BASIC Keywords (Alphabetical) o The Commodore 64 Keyboard and Features o Screen Editor ~ INTRODUCTION This chapter explains CBM BASIC Language keywords. First we give you an easy to read list of keywords, their abbreviations and what each letter looks like on the screen. Then we explain how the syntax and operation of each keyword works in detail, and examples are shown to give you an idea as to how to use them in your programs. As a convenience, Commodore 64 BASIC allows you to abbreviate most keywords. Abbreviations are entered by typing enough letters of the keyword to distinguish it from all other keywords, with the last letter or graphics entered holding down the key. Abbreviations do NOT save any memory when they're used in programs, because all keywords are reduced to single-character "tokens" by the BASIC Interpreter. When a program containing abbreviations is listed, all keywords appear in their fully spelled form. You can use abbreviations to put more statements onto a program line even if they won't fit onto the 80-character logical screen line. The Screen Editor works on an 80- character line. This means that if you use abbreviations on any line that goes over 80 characters, you will NOT be able to edit that line when LISTed. Instead, what you'll have to do is (1) retype the entire line including all abbreviations, or (2) break the single line of code into two lines, each with its own line number, etc. A complete list of keywords, abbreviations, and their appearance on the screen is presented in Table 2-1. They are followed by an alphabetical description of all the statements, commands, and functions available on your Commodore 64. This chapter also explains the BASIC functions built into the BASIC Language Interpreter. Built-in functions can be used in direct mode statements or in any program, without having to define the function further. This is NOT the case with user-defined functions. The results of built-in BASIC functions can be used as immediate output or they can be assigned to a variable name of an appropriate type. There are two types of BASIC functions: 1) NUMERIC 2) STRING Arguments of built-in functions are always enclosed in parentheses (). The parentheses always come directly after the function keyword and NO SPACES between the last letter of the keyword and the left parenthesis (. 30 BASIC LANGUAGE VOCABULARY ~ The type of argument needed is generally decided by the data type in the result. Functions which return a string value as their result are identified by having a dollar sign ($) as the last character of the keyword. In some cases string functions contain one or more numeric argument. Numeric functions will convert between integer and floating- point format as needed. In the descriptions that follow, the data type of the value returned is shown with each function name. The types of argu- ments are also given with the statement format. Table 2-1. COMMODORE 64 BASIC KEYWORDS +-----------+----------------------+----------------+-------------------+ | COMMAND | ABBREVIATION | SCREEN | FUNCTION TYPE | +-----------+----------------------+----------------+-------------------+ | | | | | | ABS | A | | NUMERIC | | | | | | | AND | A | | | | | | | | | ASC | A | | NUMERIC | | | | | | | ATN | A | | NUMERIC | | | | | | | CHR$ | C | | STRING | | | | | | | CLOSE | CL | | | | | | | | | CLR | C | | | | | | | | | CMD | C | | | | | | | | | CONT | C | | | | | | | | | COS | none | COS | NUMERIC | | | | | | | DATA | D | | | | | | | | | DEF | D | | | | | | | | | DIM | D | | | BASIC LANGUAGE VOCABULARY 31 ~ +-----------+----------------------+----------------+-------------------+ | COMMAND | ABBREVIATION | SCREEN | FUNCTION TYPE | +-----------+----------------------+----------------+-------------------+ | | | | | | END | E | | | | | | | | | EXP | E | | NUMERIC | | | | | | | FN | none | FN | | | | | | | | FOR | F | | | | | | | | | FRE | F | | NUMERIC | | | | | | | GET# | none | GET# | | | | | | | | GOSUB | GO | | | | | | | | | GOTO | G | | | | | | | | | IF | none | IF | | | | | | | | INPUT | none | INPUT | | | | | | | | INPUT# | I | | | | | | | | | INT | none | INT | NUMERIC | | | | | | | LEFT$ | LE | | STRING | | | | | | | LEN | none | LEN | NUMERIC | | | | | | | LET | L | | | | | | | | | LIST | L | | | | | | | | | LOAD | L | | | | | | | | | LOG | none | LOG | NUMERIC | 32 BASIC LANGUAGE VOCABULARY ~ +-----------+----------------------+----------------+-------------------+ | COMMAND | ABBREVIATION | SCREEN | FUNCTION TYPE | +-----------+----------------------+----------------+-------------------+ | | | | | | MID$ | M | | STRING | | | | | | | NEW | none | NEW | | | | | | | | NEXT | N | | | | | | | | | NOT | N | | | | | | | | | ON | none | ON | | | | | | | | OPEN | O | | | | | | | | | OR | none | OR | | | | | | | | PEEK | P | | NUMERIC | | | | | | | POKE | P | | | | | | | | | POS | none | POS | NUMERIC | | | | | | | PRINT | ? | ? | | | | | | | | PRINT# | P | | | | | | | | | READ | R | | | | | | | | | REM | none | REM | | | | | | | | RESTORE| RE | | | | | | | | | RETURN | RE | | | | | | | | | RIGHT$ | R | | STRING | | | | | | | RND | R | | NUMERIC | | | | | | | RUN | R | | | BASIC LANGUAGE VOCABULARY 33 ~ | | | | | | SAVE | S | | | | | | | | | SGN | S | | NUMERIC | | | | | | | SIN | S | | NUMERIC | | | | | | | SPC( | S | | SPECIAL | | | | | | | SQR | S | | NUMERIC | | | | | | | STATUS | ST | ST | NUMERIC | | | | | | | STEP | ST | | | | | | | | | STOP | S | | | | | | | | | STR$ | ST | | STRING | | | | | | | SYS | S | | | | | | | | | TAB( | T | | SPECIAL | | | | | | | TAN | none | TAN | NUMERIC | | | | | | | THEN | T | | | | | | | | | TIME | TI | TI | NUMERIC | | | | | | | TIME$ | TI$ | TI$ | STRING | | | | | | | TO | none | TO | | | | | | | | USR | U | | NUMERIC | | | | | | | VAL | V | | NUMERIC | | | | | | | VERIFY | V | | | | | | | | | WAIT | W | | | +-----------+----------------------+----------------+-------------------+ 34 BASIC LANGUAGE VOCABULARY ~ DESCRIPTION OF BASIC KEYWORDS ABS TYPE: Function-Numeric FORMAT: ABS() Action: Returns the absolute value of the number, which is its value without any signs. The absolute value of a negative number is that number multiplied by -1. EXAMPLES of ABS Function: 10 X = ABS (Y) 10 PRINT ABS (X*J) 10 IF X = ABS (X) THEN PRINT"POSITIVE" AND TYPE: Operator FORMAT: AND Action: AND is used in Boolean operations to test bits. it is also used in operations to check the truth of both operands. In Boolean algebra, the result of an AND operation is 1 only if both numbers being ANDed are 1. The result is 0 if either or both is 0 (false). EXAMPLES of 1-Bit AND operation: 0 1 0 1 AND 0 AND 0 AND 1 AND 1 ------ ----- ----- ----- 0 0 0 1 The Commodore 64 performs the AND operation on numbers in the range from -32768 to +32767. Any fractional values are not used, and numbers beyond the range will cause an ?ILLEGAL QUANTITY error message. When BASIC LANGUAGE VOCABULARY 35 ~ converted to binary format, the range allowed yields 16 bits for each number. Corresponding bits are ANDed together, forming a 16-bit result in the same range. EXAMPLES of 16-Bit AND Operation: 17 AND 194 -------- 0000000000010001 AND 0000000011000010 -------------------------- (BINARY) 0000000000000000 -------------------------- (DECIMAL) 0 32007 AND 28761 ---------- 0111110100000111 AND 0111000001011001 -------------------------- (BINARY) 0111000000000001 -------------------------- (DECIMAL) 28673 -241 AND 15359 ---------- 1111111100001111 AND 0011101111111111 -------------------------- (BINARY) 0011101100001111 -------------------------- (DECIMAL) 15119 36 BASIC LANGUAGE VOCABULARY ~ When evaluating a number for truth or falsehood, the computer assumes the number is true as long as its value isn't 0. When evaluating a comparison, it assigns a value of -I if the result is true, while false has a value of 0. In binary format, -1 is all 1's and 0 is all 0's. Therefore, when ANDing true/false evaluations, the result will be true if any bits in the result are true. EXAMPLES of Using AND with True/False Evaluations: 50 IF X=7 AND W=3 THEN GOTO 10: REM ONLY TRUE IF BOTH X=7 AND W=3 ARE TRUE 60 IF A AND Q=7 THEN GOTO 10: REM TRUE IF A IS NON-ZERO AND Q=7 IS TRUE ASC TYPE: Function-Numeric FORMAT: ASC() Action: ASC will return a number from 0 to 255 which corresponds to the Commodore ASCII value of the first character in the string. The table of Commodore ASCII values is shown in Appendix C. EXAMPLES OF ASC Function: 10 PRINT ASC("Z") 20 X = ASC("ZEBRA") 30 J = ASC(J$) If there are no characters in the string, an ?ILLEGAL QUANTITY error results. In the third example above, if J$="", the ASC function will not work. The GET and GET# statement read a CHR$(0) as a null string. To eliminate this problem, you should add a CHR$(0) to the end of the string as shown below. EXAMPLE of ASC Function Avoiding ILLEGAL QUANTITY ERROR: 30 J = ASC(J$ + CHR$(0)) BASIC LANGUAGE VOCABULARY 37 ~ ATN TYPE: Function-Numeric FORMAT: ATN() Action: This mathematical function returns the arctangent of the number. The result is the angle (in radians) whose tangent is the number given. The result is always in the range -pi/2 to +pi/2. EXAMPLES of ATN Function: 10 PRINT ATN(0) 20 X = ATN(J)*180/ {pi} : REM CONVERT TO DEGREES CHR$ TYPE: Function-String FORMAT: CHR$ () Action: This function converts a Commodore ASCII code to its character equivalent. See Appendix C for a list of characters and their codes. The number must have a value between 0 and 255, or an ?ILLEGAL QUANTITY error message results. EXAMPLES of CHR$ Function: 10 PRINT CHR$(65) : REM 65 = UPPER CASE A 20 A$=CHR$(13) : REM 13 = RETURN KEY 50 A=ASC(A$) : A$ = CHR$(A) : REM CONVERTS TO C64 ASCII CODE AND BACK 38 BASIC LANGUAGE VOCABULARY ~ CLOSE TYPE: I/O Statement FORMAT: CLOSE Action: This statement shuts off any data file or channel to a device. The file number is the same as when the file or device was OPENed (see OPEN statement and the section on INPUT/OUTPUT programming). When working with storage devices like cassette tape and disks, the CLOSE operation stores any incomplete buffers to the device. When this is not performed, the file will be incomplete on the tape and unreadable on the disk. The CLOSE operation isn't as necessary with other devices, but it does free up memory for other files. See your external device manual for more details. EXAMPLES of CLOSE Statement: 10 CLOSE 1 20 CLOSE X 30 CLOSE 9*(1+J) CLR TYPE: Statement FORMAT: CLR Action: This statement makes available RAM memory that had been used but is no longer needed. Any BASIC program in memory is untouched, but all variables, arrays, GOSUB addresses, FOR...NEXT loops, user-defined functions, and files are erased from memory, and their space is mode available to new variables, etc. BASIC LANGUAGE VOCABULARY 39 ~ In the case of files to the disk and cassette tape, they are not properly CLOSED by the CLR statement. The information about the files is lost to the computer, including any incomplete buffers. The disk drive will still think the file is OPEN. See the CLOSE statement for more information on this. EXAMPLE of CLR Statement: 10 X=25 20 CLR 30 PRINT X RUN 0 READY CMD TYPE: I/O Statement FORMAT: [,string] Action: This statement switches the primary- output device from the TV screen to the file specified. This file could be on disk, tape, printer, or an I/O device like the modem. The file number must be specified in a prior OPEN statement. The string, when specified, is sent to the file. This is handy for titling printouts, etc. When this command is in effect, any PRINT statements and LIST commands will not display on the screen, but will send the text in the same format to the file. To re-direct the output back to the screen, the PRINT# command should send a blank line to the CMD device before CLOSEing, so it will stop expecting data (called "un-listening" the device). 40 BASIC LANGUAGE VOCABULARY ~ Any system error (like ?SYNTAX ERROR) will cause output to return to the screen. Devices aren't un-listened by this, so you should send a blank line after an error condition. (See your printer or disk manual for more details.) EXAMPLES of CMD Statement: OPEN 4,4: CMD 4,"TITLE" : LIST: REM LISTS PROGRAM ON PRINTER PRINT#4: CLOSE 4: REM UN-LISTENS AND CLOSES PRINTER 10 OPEN 1,1,1,"TEST" : REM CREATE SEQ FILE 20 CMD 1 : REM OUTPUT TO TAPE FILE, NOT SCREEN 30 FOR L = 1 TO 100 40 PRINT L: REM PUTS NUMBER IN TAPE BUFFER 50 NEXT 60 PRINT#1 : REM UNLISTEN 70 CLOSE 1 : REM WRITE UNFINISHED BUFFER, PROPERLY FINISH CONT TYPE: Command FORMAT: CONT Action: This command re-starts the execution of a program which was halted by a STOP or END statement or the key being pressed. The program will re-start at the exact place from which it left off. While the program is stopped, the user can inspect or change any variables or look at the program. When debugging or examining a program, STOP statements can be placed at strategic locations to allow examination of variables and to check the flow of the program. The error message CAN'T CONTINUE will result from editing the program (even just hitting with the cursor on an unchanged line), or if the program halted due to an error, or if you caused an error before typing CONT to re-start the program. EXAMPLE of CONT Command: 10 PI=0:C=1 20 PI=PI+4/C-4/(C+2) 30 PRINT PI 40 C=C+4:GOTO 20 BASIC LANGUAGE VOCABULARY 41 ~ This program calculates the value of PI. RUN this program, and after a short while hit the key. You will see the display: +----------------------------------+ BREAK IN 20 | NOTE: Might be different number. | +----------------------------------+ Type the command PRINT C to see how far the Commodore 64 has gotten. Then use CONT to resume from where the Commodore 64 left off. COS TYPE: Function FORMAT: COS () Action: This mathematical function calculates the cosine of the number, where the number is an angle in radians. EXAMPLES of COS Function: 10 PRINT COS(0) 20 X = COS(Y* {pi} /180) : REM CONVERT DEGREES TO RADIANS DATA TYPE: Statement FORMAT: DATA Action: DATA statements store information within a program. The program uses the information by means of the READ statement, which pulls successive constants from the DATA statements. The DATA statements don't have to be executed by the program, they only have to be present. Therefore, they are usually placed at the end of the program. All data statements in a program are treated as a continuous list. Data is READ from left to right, from the lowest numbered line to the highest. If the READ statement encounters data that doesn't fit the type requested (if it needs a number and finds a string) an error message occurs. 42 BASIC LANGUAGE VOCABULARY ~ Any characters can be included as data, but if certain ones are used the data item must be enclosed by quote marks (" "). These include punctuation like comma (,), colon (:), blank spaces, and shifted letters, graphics, and cursor control characters. EXAMPLES of DATA Statement: 10 DATA 1,10,5,8 20 DATA JOHN,PAUL,GEORGE,RINGO 30 DATA "DEAR MARY, HOW ARE YOU, LOVE, BILL" 40 DATA -1.7E-9, 3.33 DEF FN TYPE: Statement FORMAT: DEF FN ( ) = Action: This sets up a user-defined function that can be used later in the program. The function can consist of any mathematical formula. User- defined functions save space in programs where a long formula is used in several places. The formula need only be specified once, in the definition statement, and then it is abbreviated as a function name. It must be executed once, but any subsequent executions are ignored. The function name is the letters FN followed by any variable name. This can be 1 or 2 characters, the first being a letter and the second a letter or digit. EXAMPLES of DEF FN Statement: 10 DEF FN A(X)=X+7 20 DEF FN AA(X)=Y*Z 30 DEF FN A9(Q) = INT(RND(1)*Q+1) The function is called later in the program by using the function name with a variable in parentheses. This function name is used like any other variable, and its value is automatically calculated, BASIC LANGUAGE VOCABULARY 43 ~ EXAMPLES of FN Use: 40 PRINT FN A(9) 50 R=FN AA(9) 60 G=G+FN A9(10) In line 50 above, the number 9 inside the parentheses does not affect the outcome of the function, because the function definition in line 20 doesn't use the variable in the parentheses. The result is Y times Z, regardless of the value of X. In the other two functions, the value in parentheses does affect the result. DIM TYPE: Statement FORMAT: DIM ( )[ ( )...] Action: This statement defines an array or matrix of variables. This allows you to use the variable name with a subscript. The subscript points to the element being used. The lowest element number in an array is zero, and the highest is the number given in the DIM statement, which has a maximum of 32767. The DIM statement must be executed once and only once for each array. A REDIM'D ARRAY error occurs if this line is re-executed. Therefore, most programs perform all DIM operations at the very beginning. There may be any number of dimensions and 255 subscripts in an array, limited only by the amount of RAM memory which is available to hold the variables. The array may be mode up of normal numeric variables, as shown above, or of strings or integer numbers. If the variables are other than normal numeric, use the $ or % signs after the variable name to indicate string or integer variables, 44 BASIC LANGUAGE VOCABULARY ~ If an array referenced in a program was never DiMensioned, it is automatically dimensioned to 11 elements in each dimension used in the first reference. EXAMPLES of DIM Statement: 10 DIM A(100) 20 DIM Z (5,7), Y(3,4,5) 30 DIM Y7%(Q) 40 DIM PH$(1000) 50 F(4)=9 : REM AUTOMATICALLY PERFORMS DIM F(10) EXAMPLE of FOOTBALL SCORE-KEEPING Using DIM: 10 DIM S(1,5), T$(1) 20 INPUT"TEAM NAMES"; T$(0), T$(1) 30 FOR Q=1 TO 5: FOR T=0 TO 1 40 PRINT T$(T),"SCORE IN QUARTER" Q 50 INPUT S(T,Q): S(T,0)= S(T,0)+ S(T,Q) 60 NEXT T,Q 70 PRINT CHR$(147) "SCOREBOARD" 80 PRINT "QUARTER" 90 FOR Q= 1 TO 5 100 PRINT TAB(Q*2+9) Q; 110 NEXT: PRINT TAB(15) "TOTAL" 120 FOR T=0 TO 1: PRINT T$(T); 130 FOR Q= 1 TO 5 140 PRINT TAB(Q*2+9) S(T,Q); 150 NEXT: PRINT TAB(15) S(T,0) 160 NEXT CALCULATING MEMORY USED BY DIM: 5 bytes for the array 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 byte for each character in each string element BASIC LANGUAGE VOCABULARY 45 ~ END TYPE: Statement FORMAT: END Action: This finishes a program's execution and displays the READY message, returning control to the person operating the computer. There may be any number of END statements within a program. While it is not necessary to include any END statements at all, it is recommended that a program does conclude with one, rather than just running out of lines. The END statement is similar to the STOP statement. The only difference is that STOP causes the computer to display the message BREAK IN LINE XX and END just displays READY. Both statements allow the computer to resume execution by typing the CONT command. EXAMPLES of END Statement: 10 PRINT"DO YOU REALLY WANT TO RUN THIS PROGRAM" 20 INPUT A$ 30 IF A$ = "NO" THEN END 40 REM REST OF PROGRAM . . . 999 END EXP TYPE: Function-Numeric FORMAT: EXP ( ) Action: This mathematical function calculates the constant e (2.71828183) raised to the power of the number given. A value greater than 88.0296919 causes an ?OVERFLOW error to occur. EXAMPLES of EXP Function: 10 PRINT EXP (1) 20 X = Y*EXP (Z*Q) 46 BASIC LANGUAGE VOCABULARY ~ FN TYPE: Function-Numeric FORMAT: FN ( ) Action: This function references the previously DEFined formula spec- ified by name. The number is substituted into its place (if any) and the formula is calculated. The result will be a numeric value. This function can be used in direct mode, as long as the statement DEFining it has been executed. If an FN is executed before the DEF statement which defines it, an UNDEF'D FUNCTION error occurs. EXAMPLES of FN (User-Defined) Function: PRINT FN A(Q) 1100 J = FN J(7)+ FN J(9) 9990 IF FN B7 (1+1)= 6 THEN END FOR ... TO ... [STEP ... TYPE: Statement FORMAT: FOR = TO [ STEP ] Action: This is a special BASIC statement that lets you easily use a variable as a counter. You must specify certain parameters: the floating-point 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 = 1 + 1 130 IF L <= 10 THEN 110 140 END BASIC LANGUAGE VOCABULARY 47 ~ 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 see, the program is shorter and easier to understand using the FOR statement. When the FOR statement is executed, several operations take place. The value is placed in the being used in the counter. In the example above, a I is placed in L. When the NEXT statement is reached, the value is added to the . If a STEP was not included, the is set to + 1. The first time the program above hits line 120, 1 is added to L, so the new value of L is 2. Now the value in the is compared to the . If the has not been reached yet, the program G0es TO the line after the original FOR statement. In this case, the value of 2 in L is less than the limit of 10, so it GOes TO line 110. Eventually, the value of is exceeded by the . At that time, the loop is concluded and the program continues with the line following the NEXT statement. In our example, the value of L reaches 11, which exceeds the limit of 10, and the program goes on with line 130. When the value of is positive, the must exceed the , and when it is negative it must become less than the . +---------------------------------------------+ | NOTE: A loop always executes at least once. | +---------------------------------------------+ EXAMPLES of FOR...TO...STEP...Statement: 100 FOR L = 100 TO 0 STEP -1 100 FOR L = PI TO 6* {pi} STEP .01 100 FOR AA = 3 TO 3 48 BASIC LANGUAGE VOCABULARY ~ FRE TYPE: Function FORMAT: FRE ( ) Action: This function tells you how much RAM is available for your program and its variables. If a program tries to use more space than is available, the OUT OF MEMORY error results. The number in parentheses can have any value, and it is not used in the calculation. +-----------------------------------------------------------------------+ | NOTE: If the result of FRE is negative, add 65536 to the FRE number | | get the number of bytes available in memory. | +-----------------------------------------------------------------------+ EXAMPLES of FRE Function: PRINT FRE(0) 10 X = (FRE(K)-1000)/7 950 IF FRE(0)< 100 THEN PRINT "NOT ENOUGH ROOM" +-----------------------------------------------------------------------+ | NOTE: The following always tells you the current available RAM: | | PRINT FRE(0) - (FRE(0) < 0)* 65536 | +-----------------------------------------------------------------------+ GET TYPE: Statement FORMAT: GET Action: This statement reads each key typed by the user. As the user is typing, the characters are stored in the Commodore 64's keyboard buffer. Up to 10 characters are stored here, and any keys struck after the 10th are lost. Reading one of the characters with the GET statement makes room for another character. If the GET statement specifies numeric data, and the user types a key other than a number, the message ?SYNTAX ERROR appears. To be safe, read the keys as strings and convert them to numbers later. BASIC LANGUAGE VOCABULARY 49 ~ The GET statement can be used to avoid some of the limitations of the INPUT statement. For more on this, see the section on Using the GET Statement in the Programming Techniques section. EXAMPLES of GET Statement: 10 GET A$: IF A$ ="" THEN 10: REM LOOPS IN 10 UNTIL ANY KEY HIT 20 GET A$, B$, C$, D$, E$: REM READS 5 KEYS 30 GET A, A$ GET# TYPE: I/O Statement FORMAT: GET# , Action: This statement reads characters one-at-a-time from the device or file specified. It works the same as the GET statement, except that the data comes from a different place than the keyboard. If no character is received, the variable is set to an empty string (equal to "") or to 0 for numeric variables. Characters used to separate data in files, like the comma (,) or key code (ASC code of 13), are received like any other character. When used with device #3 (TV screen), this statement will read char- acters one by one from the screen. Each use of GET# moves the cursor 1 position to the right. The character at the end of the logical line is changed to a CHR$ (13), the key code. EXAMPLES of GET# Statement: 5 GET#1, A$ 10 OPEN 1,3: GET#1, Z7$ 20 GET#1, A, B, C$, D$ 50 BASIC LANGUAGE VOCABULARY ~ GOSUB TYPE: Statement FORMAT: GOSUB Action: This is a specialized form of the GOTO statement, with one important difference: GOSUB remembers where it came from. When the RETURN statement (different from the key on the keyboard) is reached in the program, the program jumps back to the statement immediately following the original GOSUB statement. The major use of a subroutine (GOSUB really means GO to a SUBroutine) is when a small section of program is used by different sections of the program. By using subroutines rather than repeating the same lines over and over at different places in the program, you can save lots of program space. In this way, GOSUB is similar to DEF FN. DEF FN lets you save space when using a formula, while GOSUB saves space when using a several- line routine. Here is an inefficient program that doesn't use GOSUB: 100 PRINT "THIS PROGRAM PRINTS" 110 FOR L = 1 TO 500:NEXT 120 PRINT "SLOWLY ON THE SCREEN" 130 FOR L = 1 TO 500:NEXT 140 PRINT "USING A SIMPLE LOOP" 150 FOR L = 1 TO 500:NEXT 160 PRINT "AS A TIME DELAY." 170 FOR L = 1 TO 500:NEXT Here is the same program using GOSUB: 100 PRINT "THIS PROGRAM PRINTS" 110 GOSUB 200 120 PRINT "SLOWLY ON THE SCREEN" 130 GOSUB 200 140 PRINT "USING A SIMPLE LOOP" 150 GOSUB 200 160 PRINT "AS A TIME DELAY." 170 GOSUB 200 180 END 200 FOR L = 1 TO 500 NEXT 210 RETURN BASIC LANGUAGE VOCABULARY 51 ~ Each time the program executes a GOSUB, the line number and position in the program line are saved in a special area called the "stack," which takes up 256 bytes of your memory. This limits the amount of data that can be stored in the stack. Therefore, the number of subroutine return addresses that can be stored is limited, and care should be taken to make sure every GOSUB hits the corresponding RETURN, or else you'll run out of memory even though you have plenty of bytes free. GOTO TYPE: Statement FORMAT :GOTO or GO TO Action: This statement allows the BASIC program to execute lines out of numerical order. The word GOTO followed by a number will make the program jump to the line with that number. GOTO NOT followed by a number equals GOTO 0. It must have the line number after the word GOTO. It is possible to create loops with GOTO that will never end. The simplest example of this is a line that GOes TO itself, like 10 GOTO 10. These loops can be stopped using the key on the keyboard. EXAMPLES of GOTO Statement: GOTO 100 10 GO TO 50 20 GOTO 999 IF...THEN... TYPE: Statement FORMAT: IF THEN IF GOTO IF THEN Action: This is the statement that gives BASIC most of its "intelli- gence," the ability to evaluate conditions and take different actions de- pending on the outcome. 52 BASIC LANGUAGE VOCABULARY ~ The word IF is followed by an expression, which can include variables, strings, numbers, comparisons, and logical operators. The word THEN appears on the same line and is followed by either a line number or one or more BASIC statements. When the expression is false, everything after the word THEN on that line is ignored, and execution continues with the next line number in the program. A true result makes the program either branch to the line number after the word THEN or execute whatever other BASIC statements are found on that line. EXAMPLE of IF...GOTO...Statement: 100 INPUT "TYPE A NUMBER"; N 110 IF N <= 0 GOTO 200 120 PRINT "SQUARE ROOT=" SQR(N) 130 GOTO 100 200 PRINT "NUMBER MUST BE >0" 210 GOTO 100 This program prints out the square root of any positive number. The IF statement here is used to validate the result of the INPUT. When the result of N <= 0 is true, the program skips to line 200, and when the result is false the next line to be executed is 120. Note that THEN GOTO is not needed with IF...THEN, as in line 110 where GOTO 200 actually means THEN GOTO 200. EXAMPLE OF IF...THEN...Statement: 100 FOR L = 1 TO 100 110 IF RND(1) < .5 THEN X=X+1: GOTO 130 120 Y=Y+1 130 NEXT L 140 PRINT "HEADS=" X 150 PRINT "TAILS= " Y The IF in line 110 tests a random number to see if it is less than .5. When the result is true, the whole series of statements following the word THEN are executed: first X is incremented by 1, then the program skips to line 130. When the result is false, the program drops to the next statement, line 120. BASIC LANGUAGE VOCABULARY 53 ~ INPUT TYPE: Statement FORMAT: INPUT [ "" ; ] Action: This is a statement that lets the person RUNning the program "feed" information into the computer. When executed, this statement PRINTs a question mark (?) on the screen, and positions the cursor 1 space to the right of the question mark. Now the computer waits, cursor blinking, for the operator to type in the answer and press the key. The word INPUT may be followed by any text contained in quote marks (""). This text is PRINTed on the screen, followed by the question mark. After the text comes a semicolon (;) and the name of one or more variables separated by commas. This variable is where the computer stores the information that the operator types. The variable can be any legal variable name, and you can have several different variable names, each for a different input. EXAMPLES of INPUT Statement: 100 INPUT A 110 INPUT B, C, D 120 INPUT "PROMPT"; E When this program RUNs, the question mark appears to prompt the operator that the Commodore 64 is expecting an input for line 100. Any number typed in goes into A, for later use in the program. If the answer typed was not a number, the ?REDO FROM START message appears, which means that a string was received when a number was expected. If the operator just hits without typing anything, the vari- able's value doesn't change. Now the next question mark, for line 110, appears. If we type only one number and hit the , Commodore 64 will now display 2 question marks (??), which means that more input is required. You can 54 BASIC LANGUAGE VOCABULARY ~ just type as many inputs as you need separated by commas, which prevents the double question mark from appearing. If you type more data than the INPUT statement requested, the ?EXTRA IGNORED message appears, which means that the extra items you typed were not put into any variables. Line 120 displays the word PROMPT before the question mark appears. The semicolon is required between the prompt and any list of variables. The INPUT statement can never be used outside a program. The Commodore 64 needs space for a buffer for the INPUT variables, the same space that is used for commands. INPUT# TYPE: I/O Statement FORMAT: INPUT# , Action: This is usually the fastest and easiest way to retrieve data stored in a file on disk or tape. The data is in the form of whole vari- ables of up to 80 characters in length, as opposed to the one-at-a-time method of GET#. First, the file must have been OPENed, then INPUT# can fill the variables. The INPUT# command assumes a variable is finished when it reads a RETURN code (CHR$ (13)), a comma (,), semicolon (;), or colon (:). Quote marks can be used to enclose these characters when writing if they are needed (see PRINT# statement). If the variable type used is numeric, and non-numeric characters are received, a BAD DATA error results. INPUT# can read strings up to 80 characters long, beyond which a STRING TOO LONG error results. When used with device #3 (the screen), this statement will read an entire logical line and move the cursor down to the next line. EXAMPLES of INPUT# Statement: 10 INPUT#1,A 20 INPUT#2,A$,B$ BASIC LANGUAGE VOCABULARY 55 ~ INT TYPE: Integer Function FORMAT: INT () Action: Returns the integer value of the expression. If the expression is positive, the fractional part is left off. If the expression is negative, any fraction causes the next lower integer to be returned. EXAMPLES of INT Function: 120 PRINT INT(99.4343), INT(-12.34) 99 -13 LEFT$ TYPE: String Function FORMAT: LEFT$ (, ) Action: Returns a string comprised of the leftmost char- acters of the . The integer argument value must be in the range 0 to 255. If the integer is greater than the length of the string, the entire string will be returned. If an value of zero is used, then a null string (of zero length) is returned. EXAMPLES of LEFT$ Function: 10 A$ = "COMMODORE COMPUTERS" 20 B$ = LEFT$(A$,9): PRINT B$ RUN COMMODORE 56 BASIC LANGUAGE VOCABULARY ~ LEN TYPE: Integer Function Format: LEN () Action: Returns the number of characters in the string expression. Non-printed characters and blanks are counted. EXAMPLE of LEN Function: CC$ = "COMMODORE COMPUTER": PRINT LEN(CC$) 18 LET TYPE: Statement FORMAT: [LET] = Action: The LET statement can be used to assign a value to a variable. But the word LET is optional and therefore most advanced programmers leave LET out because it's always understood and wastes valuable memory. The equal sign (=) alone is sufficient when assigning the value of an expression to a variable name. EXAMPLES of LET Statement: 10 LET D= 12 (This is the same as D = 12) 20 LET E$ = "ABC" 30 F$ = "WORDS" 40 SUM$= E$ + F$ (SUM$ would equal ABCWORDS) BASIC LANGUAGE VOCABULARY 57 ~ LIST TYPE: Command FORMAT: LIST [[]-[]] Action: The LIST command allows you to look at lines of the BASIC program currently in the memory of your Commodore 64. This lets you use your computer's powerful screen editor, to edit programs which you've LISTed both quickly and easily. The LIST system command displays all or part of the program that is currently in memory on the default output device. The LIST will normally be directed to the screen and the CMD statement can be used to switch output to an external device such as a printer or a disk. The LIST com- mand can appear in a program, but BASIC always returns to the system READY message after a LIST is executed. When you bring the program LIST onto the screen, the "scrolling" of the display from the bottom of the screen to the top can be slowed by holding down the ConTRoL key. LIST is aborted by typing the key. If no line-numbers are given the entire program is listed. If only the first-line number is specified, and followed by a hyphen (-), that line and all higher-numbered lines are listed. If only the last line-number is specified, and it is preceded by a hyphen, then all lines from the beginning of the program through that line are listed. If both numbers are specified, the entire range, including the line-numbers LISTed, is displayed. EXAMPLES of LIST Command: LIST (Lists the program currently in memory.) LIST 500 (Lists line 500 only.) LIST 150- (Lists all lines from 150 to the end.) LIST -1000 (Lists all lines from the lowest through 1000.) LIST 150-1000 (Lists lines 150 through 1000, inclusive.) 10 PRINT "THIS IS LINE 10" 20 LIST (LIST used in Program Mode) 30 PRINT "THIS IS LINE 30" 58 BASIC LANGUAGE VOCABULARY ~ LOAD TYPE: Command FORMAT: LOAD[""][,][,
] Action: The LOAD statement reads the contents of a program file from tape or disk into memory. That way you can use the information LOADed or change the information in some way. The device number is optional, but when it is left out the computer will automatically default to 1, the cassette unit. The disk unit is normally device number 8. The LOAD closes all open files and, if it is used in direct mode, it performs a CLR (clear) before reading the program. If LOAD is executed from within a program, the program is RUN. This means that you can use LOAD to "chain" several programs together. None of the variables are cleared during a chain operation. If you are using file-name pattern matching, the first file which matches the pattern is loaded. The asterisk in quotes by itself ("*") causes the first file-name in the disk directory to be loaded. if the filename used does not exist or if it is not a program file, the BASIC error message ?FILE NOT FOUND occurs. When LOADing programs from tape, the can be left out, and the next program file on the tape will be read. The Commodore 64 will blank the screen to the border color after the PLAY key is pressed. When the program is found, the screen clears to the background color and the "FOUND" message is displayed. When the key, key, key, or is pressed, the file will be loaded. Programs will LOAD starting at memory location 2048 unless a secondary
of 1 is used. If you use the secondary address of 1 this will cause the program to LOAD to the memory location from which it was saved. BASIC LANGUAGE VOCABULARY 59 ~ EXAMPLES of LOAD Command: LOAD (Reads the next program on tape) LOAD A$ (Uses the name in A$ to search) LOAD"*",8 (LOADs first program from disk) LOAD"",1,1 (Looks for the first program on tape, and LOADs it into the same part of memory that it came from) LOAD"STAR TREK" (LOAD a file from tape) PRESS PLAY ON TAPE FOUND STAR TREK LOADING READY. LOAD"FUN",8 (LOAD a file from disk) SEARCHING FOR FUN LOADING READY. LOAD"GAME ONE",8,1 (LOAD a file to the specific SEARCHING FOR GAME ONE memory location from which the LOADING program was saved on the disk) READY. 60 BASIC LANGUAGE VOCABULARY ~ LOG TYPE: Floating-Point Function FORMAT: LOG() Action: Returns the natural logarithm (log to the base of e) of the argument. If the value of the argument is zero or negative the BASIC error message ?ILLEGAL QUANTITY will occur. EXAMPLES of LOG Function: 25 PRINT LOG(45/7) 1.86075234 10 NUM=LOG(ARG)/LOG(10) (Calculates the LOG of ARG to the base 10) MID$ TYPE: String Function FORMAT: MID$(,[,]) Action: The MID$ function returns a sub-string which is taken from within a larger argument. The starting position of the sub- string is defined by the argument and the length of the sub-string by the argument. Both of the numeric arguments can have values ranging from 0 to 255. If the value is greater than the length of the , or if the value is zero, then MID$ gives a null string value. If the argument is left out, then the computer will assume that a length of the rest of the string is to be used. And if the source string has fewer characters than , from the starting position to the end of the string argument, then the whole rest of the string is used. EXAMPLE of MID$ Function: 10 A$="GOOD" 20 B$="MORNING EVENING AFTERNOON" 30 PRINT A$ + MID$(B$,8,8) GOOD EVENING BASIC LANGUAGE VOCABULARY 61 ~ NEW TYPE: Command FORMAT: NEW Action: The NEW command is used to delete the program currently in memory and clear all variables. Before typing in a new program, NEW should be used in direct mode to clear memory. NEW can also be used in a program, but you should be aware of the fact that it will erase everything that has gone before and is still in the computer's memory. This can be particularly troublesome when you're trying to debug your program. +-----------------------------------------------------------------------+ | BE CAREFUL: Not clearing out an old program before typing a new one | | can result in a confusing mix of the two programs. | +-----------------------------------------------------------------------+ EXAMPLES of NEW Command: NEW (Clears the program and all variables) 10 NEW (Performs a NEW operation and STOPs the program.) NEXT TYPE: Statement FORMAT: NEXT[][,]... Action: The NEXT statement is used with FOR to establish the end of a FOR...NEXT loop. The NEXT need not be physically the last statement in the loop, but it is always the last statement executed in a loop. The is the loop index's variable name used with FOR to start the loop. A single NEXT can stop several nested loops when it is followed by each FOR's variable name(s). To do this each name must appear in the order of inner-most nested loop first, to outer-most nested loop last. When using a single NEXT to increment and stop several variable names, each variable name must be separated by commas. Loops can be nested to 9 levels. If the counter variable(s) are omitted, the counter associated with the FOR of the current level (of the nested loops) is incremented. 62 BASIC LANGUAGE VOCABULARY ~ When the NEXT is reached, the counter value is incremented by 1 or by an optional STEP value. It is then tested against an end-value to see if it's time to stop the loop. A loop will be stopped when a NEXT is found which has its counter value greater than the end-value. EXAMPLES of NEXT Statement: 10 FOR J=1 TO 5: FOR K=10 TO 20: FOR N=5 TO -5 STEP - 1 20 NEXT N,K,J (Stopping Nested Loops) 10 FOR L=1 TO 100 20 FOR M=1 TO 10 30 NEXT M 400 NEXT L (Note how the loops do NOT cross each other) 10 FOR A=1 TO 10 20 FOR B=1 TO 20 30 NEXT 40 NEXT (Notice that no variable names are needed) NOT TYPE: Logical Operator FORMAT: NOT Action: The NOT logical operator "complements" the value of each bit in its single operand, producing an integer "twos-complement" result. In other words, the NOT is really saying, "if it isn't. When working with a floating-point number, the operands are converted to integers and any fractions are lost. The NOT operator can also be used in a comparison to reverse the true/false value which was the result of a relationship test and therefore it will reverse the meaning of the comparison. In the first example below, if the "twos-complement" of "AA" is equal to "BB" and if "BB" is NOT equal to "CC" then the expression is true. BASIC LANGUAGE VOCABULARY 63 ~ EXAMPLES of NOT Operator: 10 IF NOT AA = BB AND NOT(BB = CC) THEN... NN% = NOT 96: PRINT NN% -97 +-----------------------------------------------------------------------+ | NOTE: TO find the value of NOT use the expression X=(-(X+1)). (The | | two's complement of any integer is the bit complement plus one.) | +-----------------------------------------------------------------------+ ON TYPE: Statement FORMAT: ON GOTO / GOSUB [,]... Action: The ON statement is used to GOTO one of several given line- numbers, depending upon the value of a variable. The value of the variables can range from zero through the number of lines given. if the value is a non-integer, the fractional portion is left off. For example, if the variable value is 3, ON will GOTO the third line-number in the list. If the value of the variable is negative, the BASIC error message ?ILLEGAL QUANTITY occurs. If the number is zero, or greater than the number of items in the list, the program just "ignores" the statement and continues with the statement following the ON statement. ON is really an underused variant of the IF...THEN...statement. Instead of using a whole lot of IF statements each of which sends the program to 1 specific line, 1 ON statement can replace a list of IF statements. When you look at the first example you should notice that the 1 ON statement replaces 4 IF...THEN... statements. EXAMPLES of ON Statement: ON -(A=7)-2*(A=3)-3*(A<3)-4*(A>7)GOTO 400,900,1000,100 ON X GOTO 100,130,180,220 ON X+3 GOSUB 9000,20,9000 100 ON NUM GOTO 150,300,320,390 500 ON SUM/2 + 1 GOSUB 50,80,20 64 BASIC LANGUAGE VOCABULARY ~ OPEN TYPE: I/O Statement FORMAT: OPEN ,[][,
] [," [,] [,]"] Action: This statement OPENs a channel for input and/or output to a peripheral device. However, you may NOT need all those parts for every OPEN statement. Some OPEN statements require only 2 codes: 1) LOGICAL FILE NUMBER 2) DEVICE NUMBER The is the logical file number, which relates the OPEN, CLOSE, CMD, GET#, INPUT#, and PRINT# statements to each other and associates them with the file-name and the piece of equipment being used. The logical file number can range from 1 to 255 and you can assign it any number you want in that range. +-----------------------------------------------------------------------+ | NOTE: File numbers over 128 were really designed for other uses so | | it's good practice to use only numbers below 127 for file numbers. | +-----------------------------------------------------------------------+ Each peripheral device (printer, disk drive, cassette) in the system has its own number which it answers to. The number is used with OPEN to specify on which device the data file exists. Peripherals like cassette decks, disk drives or printers also answer to several secondary addresses. Think of these as codes which tell each device what operation to perform. The device logical file number is used with every GET#, INPUT#, and PRINT#. If the number is left out the computer will automatically assume that you want your information to be sent to and received from the Datassette(TM), which is device number 1. The file-name can also be left out, but later on in your program, you can NOT call the file by name if you have not already given it one. When you are storing files on cas- sette tape, the computer will assume that the secondary
is zero (0) if you omit the secondary address (a READ operation). BASIC LANGUAGE VOCABULARY 65 ~ A secondary address value of one (1) OPENs cassette tape files for writing. A secondary address value of two (2) causes an end-of-tape marker to be written when the file is later closed. The end-of-tape marker prevents accidentally reading past the end of data which results in the BASIC error message ?DEVICE NOT PRESENT. For disk files, the secondary addresses 2 thru 14 are available for data-files, but other numbers have special meanings in DOS commands. You must use a secondary address when using your disk drive(s). (See your disk drive manual for DOS command details.) The is a string of 1-16 characters and is optional for cassette or printer files. If the file is left out the type of file will automatically default to the Program file unless the is given. Sequential files are OPENed for reading =R unless you specify that files should be OPENed for writing =W is specified. A file can be used to OPEN an existing Relative file. Use REL for with Relative files. Relative and Sequential files are for disk only. If you try to access a file before it is OPENed the BASIC error message ?FILE NOT OPEN will occur. If you try to OPEN a file for reading which does not exist the BASIC error message ?FILE NOT FOUND will occur. If a file is OPENed to disk for writing and the file-name already exists, the DOS error message FILE EXISTS occurs. There is no check of this type available for tape files, so be sure that the tape is properly positioned or you might accidentally write over some data that had previously been SAVED. If a file is OPENed that is already OPEN, the BASIC error message FILE OPEN occurs. (See Printer Manual for further details.) 66 BASIC LANGUAGE VOCABULARY ~ EXAMPLES of OPEN Statements: 10 OPEN 2,8,4,"DISK-OUTPUT,SEQ,W" (Opens sequential file on disk) 10 OPEN 1,1,2,"TAPE-WRITE" (Write End-of-File on Close) 10 OPEN 50,0 (Keyboard input) 10 OPEN 12,3 (Screen output) 10 OPEN 130,4 (Printer output) 10 OPEN 1,1,0,"NAME" (Read from cassette) 10 OPEN 1,1,1,"NAME" (Write to cassette) 10 OPEN 1,2,0,CHR$(10) (open channel to RS-232 device) 10 OPEN 1,4,0,"STRING" (Send upper case/graphics to the printer) 10 OPEN 1,4,7,"STRING" (Send upper/lower case to printer) 10 OPEN 1,5,7,"STRING" (Send upper/lower case to printer with device # 5) 10 OPEN 1,8,15,"COMMAND" (Send a command to disk) BASIC LANGUAGE VOCABULARY 67 ~ OR TYPE: Logical Operator FORMAT: OR Action: Just as the relational operators can be used to make decisions regarding program flow, logical operators can connect two or more re- lations and return a true or false value which can then be used in a decision. When used in calculations, the logical OR gives you a bit result of I if the corresponding bit of either or both operands is 1. This will produce an integer as a result depending on the values of the operands. When used in comparisons the logical OR operator is also used to link two expressions into a single compound expression. If either of the expressions are true, the combined expression value is true (-1). In the first example below if AA is equal to BB OR if XX is 20, the expression is true. Logical operators work by converting their operands to 16-bit, signed, two's complement integers in the range of -32768 to +32767. If the operands are not in the range an error message results. Each bit of the result is determined by the corresponding bits in the two operands. EXAMPLES of OR Operator: 100 IF (AA=BB) OR (XX=20) THEN... 230 KK%=64 OR 32: PRINT KK% (You typed this with a bit value of 1000000 for 64 and 100000 for 32) 96 (The computer responded with bit value 1100000. 1100000=96.) 68 BASIC LANGUAGE VOCABULARY ~ PEEK TYPE: Integer Function FORMAT: PEEK() Action: Returns an integer in the range of 0 to 255, which is read from a memory location. The expression is a memory location which must be in the range of 0 to 65535. If it isn't then the BASIC error message ?ILLEGAL QUANTITY occurs. EXAMPLES of PEEK Function: 10 PRINT PEEK(53280) AND 15 (Returns value of screen border color) 5 A%=PEEK(45)+PEEK(46)*256 (Returns address of BASIC variable table) POKE TYPE: Statement FORMAT: POKE , Action: The POKE statement is used to write a one-byte (8-bits) binary value into a given memory location or input/output register. The is an arithmetic expression which must equal a value in the range of 0 to 65535. The is an expression which can be reduced to an integer value of 0 to 255. If either value is out of its respective range, the BASIC error message ?ILLEGAL QUANTITY occurs. The POKE statement and PEEK statement (which is a built-in function that looks at a memory location) are useful for data storage, controlling graphics displays or sound generation, loading assembly language sub- routines, and passing arguments and results to and from assembly language subroutines. In addition, Operating System parameters can be examined using PEEK statements or changed and manipulated using POKE statements. A complete memory map of useful locations is given in Appendix G. BASIC LANGUAGE VOCABULARY 69 ~ EXAMPLES of POKE Statement: POKE 1024, 1 (Puts an "A" at position 1 on the screen) POKE 2040, PTR (Updates Sprite #0 data pointer) 10 POKE RED,32 20 POKE 36879,8 2050 POKE A,B POS TYPE: Integer Function FORMAT: POS () Action: Tells you the current cursor position which, of course, is in the range of 0 (leftmost character) though position 79 on an 80-character logical screen line. Since the Commodore 64 has a 40-column screen, any position from 40 through 79 will refer to the second screen line. The dummy argument is ignored. EXAMPLE of POS Function: 1000 IF POS(0)>38 THEN PRINT CHR$(13) PRINT TYPE: Statement FORMAT: PRINT [][<,/;>]... Action: The PRINT statement is normally used to write data items to the screen. However, the CMD statement may be used to re-direct that output to any other device in the system. The in the output-list are expressions of any type. If no output-list is present, a blank line is printed. The position of each printed item is determined by the punctuation used to separate items in the output-list. The punctuation characters that you can use are blanks, commas, or semicolons. The 80-character logical screen line is divided into 8 print zones of 10 spaces each. In the list of expressions, a comma causes the next value to be printed at the beginning of the next zone. A semicolon causes the next value to be printed immediately following the previous value. However, there are two exceptions to this rule: 70 BASIC LANGUAGE VOCABULARY ~ 1) Numeric items are followed by an added space. 2) Positive numbers have a space preceding them. When you use blanks or no punctuation between string constants or variable names it has the same effect as a semicolon. However, blanks between a string and a numeric item or between two numeric items will stop output without printing the second item. If a comma or a semicolon is at the end of the output-list, the next PRINT statement begins printing on the same line, and spaced accord- ingly. If no punctuation finishes the list, a carriage-return and a line- feed are printed at the end of the data. The next PRINT statement will begin on the next line. If your output is directed to the screen and the data printed is longer than 40 columns, the output is continued on the next screen line. There is no statement in BASIC with more variety than the PRINT statement. There are so many symbols, functions, and parameters associated with this statement that it might almost be considered as a language of its own within BASIC; a language specially designed for writing on the screen. EXAMPLES of PRINT Statement: 1) 5 X = 5 10 PRINT -5*X,X-5,X+5,X^5 -25 0 10 3125 2) 5 X=9 10 PRINT X;"SQUARED IS";X*X;"AND"; 20 PRINT X "CUBED IS" X^3 9 SQUARED IS 81 AND 9 CUBED IS 729 3) 90 AA$="ALPHA":BB$="BAKER":CC$="CHARLIE":DD$="DOG":EE$="ECHO" 100 PRINT AA$BB$;CC$ DD$,EE$ ALPHABAKERCHARLIEDOG ECHO BASIC LANGUAGE VOCABULARY 71 ~ Quote Mode Once the quote mark is typed, the cursor controls stop operating and start displaying reversed characters which actually stand for the cursor control you are hitting. This allows you to program these cursor controls, because once the text inside the quotes is PRINTed they perform their functions. The key is the only cursor control not affected by "quote mode." 1. Cursor Movement The cursor controls which can be "programmed" in quote mode are: KEY APPEARS AS If you wanted the word HELLO to PRINT diagonally from the upper left corner of the screen, you would type: PRINT"HELLO" 2. Reverse Characters Holding down the key and hitting <9> will cause to appear inside the quotes. This will make all characters start printing in reverse video (like a negative of a picture). To end the reverse printing hit , or else PRINT a (CHR$(13)). (Just ending the PRINT statement without a semicolon or comma will take care of this.) 3.Color Controls Holding down the key or key with any of the 8 color keys will make a special reversed character appear in the quotes. When the character is PRINTed, then the color change will occur. 72 BASIC LANGUAGE VOCABULARY ~ KEY COLOR APPEARS AS Black White Red Cyan Purple Green Blue Yellow Orange Brown Light Red Grey 1 Grey 2 Light Green Light Blue Grey 3 If you wanted to PRINT the word HELLO in cyan and the word THERE in white, type: PRINT "HELLO THERE" 4. Insert Mode The spaces created by using the key have some of the same characteristics as quote mode. The cursor controls and color controls show up as reversed characters. The only difference is in the and , which performs its normal function even in quote mode, now BASIC LANGUAGE VOCABULARY 73 ~ creates the . And , which created a special character in quote mode, inserts spaces normally. Because of this, it is possible to create a PRINT statement containing DELetes, which cannot be PRINTed in quote mode. Here is an example of how this is done: 10 PRINT"HELLO"P" When the above line is RUN, the word displayed will be HELP, because the last two letters are deleted and the P is put in their place. +-----------------------------------------------------------------------+ | WARNING: The DELetes will work when LISTing as well as PRINTing, so | | editing a line with these characters will be difficult. | +-----------------------------------------------------------------------+ The "insert mode" condition is ended when the (or ) key is hit, or when as many characters have been typed as spaces were inserted. 5. Other Special Characters There are some other characters that can be PRINTed for special functions, although they are not easily available from the keyboard. In order to get these into quotes, you must leave empty spaces for them in the line, hit or , and go back to the spaces with the cursor controls. Now you must hit , to start typing reversed characters, and type the keys shown below: Function Type Appears As switch to lower case switch to upper case disable case-switching keys enable case-switching keys 74 BASIC LANGUAGE VOCABULARY ~ The will work in the LISTing as well as PRINTing, so editing will be almost impossible if this character is used. The LISTing will also look very strange. PRINT# TYPE: I/O Statement FORMAT: PRINT#[][<,/;>]... Actions: The PRINT# statement is used to write data items to a logical file. It must use the same number used to OPEN the file. Output goes to the device-number used in the OPEN statement. The expressions in the output-list can be of any type. The punctuation characters between items are the same as with the PRINT statement and they can be used in the same ways. The effects of punctuation are different in two significant respects. When PRINT# is used with tape files, the comma, instead of spacing by print zones, has the same effect as a semicolon. Therefore, whether blanks, commas, semicolons or no punctuation characters are used between data items, the effect on spacing is the same. The data items are written as a continuous stream of characters. Numeric items are followed by a space and, if positive, are preceded by a space. If no punctuation finishes the list, a carriage-return and a line-feed are written at the end of the data. If a comma or semicolon terminates the output-list, the carriage-return and line-feed are suppressed. Re- gardless of the punctuation, the next PRINT# statement begins output in the next available character position. The line-feed will act as a stop when using the INPUT# statement, leaving an empty variable when the next INPUT# is executed. The line-feed can be suppressed or compensated for as shown in the examples below. The easiest way to write more than one variable to a file on tape or disk is to set a string variable to CHR$(13), and use that string in be- tween all the other variables when writing the file. BASIC LANGUAGE VOCABULARY 75 ~ EXAMPLES of PRINT# Statement: 1) 10 OPEN 1,1,1,"TAPE FILE" 20 R$=CHR$(13) (By Changing the CHR$(13) to 30 PRINT#1,1;R$;2;R$;3;R$;4;R$;5 CHR$(44) you put a "," between 40 PRINT#1,6 each variable. CHR$(59) would 50 PRINT# 1,7 put a ";" between each variable.) 2) 10 CO$=CHR$(44):CR$=CHR$(13) 20 PRINT#1,"AAA"CO$"BBB", AAA,BBB CCCDDDEEE "CCC";"DDD";"EEE"CR$ (carriage return) "FFF"CR$; FFF(carriage return) 30 INPUT#1,A$,BCDE$,F$ 3) 5 CR$=CHR$(13) 10 PRINT#2,"AAA";CR$;"BBB" (10 blanks) AAA 20 PRINT#2,"CCC"; BBB (10 blanks)CCC 30 INPUT#2,A$,B$,DUMMY$,C$ READ TYPE: Statement FORMAT: READ [,]... Action: The READ statement is used to fill variable names from con- stants in DATA statements. The data actually read must agree with the variable types specified or the BASIC error message ?SYNTAX ERROR will result.(*) Variables in the DATA input-list must be separated by commas. A single READ statement can access one or more DATA statements, which will be accessed in order (see DATA), or several READ statements can access the same DATA statement. If more READ statements are executed than the number of elements in DATA statements(s) in the program, the 76 BASIC LANGUAGE VOCABULARY ~ BASIC error message ?OUT OF DATA is printed. If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements will continue reading at the next data element. (See RESTORE.) +-----------------------------------------------------------------------+ | *NOTE: The ?SYNTAX ERROR will appear with the line number from the | | DATA statement, NOT the READ statement. | +-----------------------------------------------------------------------+ EXAMPLES of READ Statement: 110 READ A,B,C$ 120 DATA 1,2,HELLO 100 FOR X=1 TO 10: READ A(X):NEXT 200 DATA 3.08, 5.19, 3.12, 3.98, 4.24 210 DATA 5.08, 5.55, 4.00, 3.16, 3.37 (Fills array items (line 1) in order of constants shown (line 5)) 1 READ CITY$,STATE$,ZIP 5 DATA DENVER,COLORADO, 80211 REM TYPE: Statement FORMAT: REM [] Action:The REM statement makes your programs more easily understood when LISTed. It's a reminder to yourself to tell you what you had in mind when you were writing each section of the program. For instance, you might want to remember what a variable is used for, or some other useful information. The REMark can be any text, word, or character including the colon (:) or BASIC keywords. The REM statement and anything following it on the same line-number are ignored by BASIC, but REMarks are printed exactly as entered when the program is listed. A REM statement can be referred to by a GOTO or GOSUB statement, and the execution of the program will continue with the next higher program line having executable statements. BASIC LANGUAGE VOCABULARY 77 ~ EXAMPLES of REM Statement: 10 REM CALCULATE AVERAGE VELOCITY 20 FOR X= 1 TO 20 :REM LOOP FOR TWENTY VALUES 30 SUM=SUM + VEL(X): NEXT 40 AVG=SUM/20 RESTORE TYPE: Statement FORMAT: RESTORE Action: BASIC maintains an internal pointer to the next DATA constant to be READ. This pointer can be reset to the first DATA constant in a program using the RESTORE statement. The RESTORE statement can be used anywhere in the program to begin re-READing DATA. EXAMPLES of RESTORE Statement: 100 FOR X=1 TO 10: READ A(X): NEXT 200 RESTORE 300 FOR Y=1 TO 10: READ B(Y): NEXT 4000 DATA 3.08, 5.19, 3.12, 3.98, 4.24 4100 DATA 5.08, 5.55, 4.00, 3.16, 3.37 (Fills the two arrays with identical data) 10 DATA 1,2,3,4 20 DATA 5,6,7,8 30 FOR L= 1 TO 8 40 READ A: PRINT A 50 NEXT 60 RESTORE 70 FOR L= 1 TO 8 80 READ A: PRINT A 90 NEXT 78 BASIC LANGUAGE VOCABULARY ~ RETURN TYPE: Statement FORMAT: RETURN Action: The RETURN statement is used to exit from a subroutine called for by a GOSUB statement. RETURN restarts the rest of your program at the next executable statement following the GOSUB. If you are nesting subroutines, each GOSUB must be paired with at least one RETURN statement. A subroutine can contain any number of RETURN statements, but the first one encountered will exit the subroutine. EXAMPLE of RETURN Statement: 10 PRINT"THIS IS THE PROGRAM" 20 GOSUB 1000 30 PRINT"PROGRAM CONTINUES" 40 GOSUB 1000 50 PRINT"MORE PROGRAM" 60 END 1000 PRINT"THIS IS THE GOSUB":RETURN RIGHT$ TYPE: String Function FORMAT: RIGHT$ (,) Action: The RIGHT$ function returns a sub-string taken from the right- most end of the argument. The length of the sub-string is defined by the argument which can be any integer in the range of 0 to 255. If the value of the numeric expression is zero, then a null string ("") is returned. If the value you give in the argument is greater than the length of the then the entire string is returned. EXAMPLE of RIGHT$ Function: 10 MSG$="COMMODORE COMPUTERS" 20 PRINT RIGHT$(MSG$,9) RUN COMPUTERS BASIC LANGUAGE VOCABULARY 79 ~ RND TYPE: Floating-Point Function FORMAT: RND () Action: RND creates a floating-point random from 0.0 to 1.0. The computer generates a sequence of random numbers by performing cal- culations on a starting number, which in computer jargon is called a seed. The RND function is seeded on system power-up. The argument is a dummy, except for its sign (positive, zero, or negative). If the argument is positive, the same "pseudorandom" sequence of numbers is returned, starting from a given seed value. Dif- ferent number sequences will result from different seeds, but any se- quence is repeatable by starting from the same seed number. Having a known sequence of "random" numbers is useful in testing programs. If you choose a argument of zero, then RND generates a number directly from a free-running hardware clock (the system "jiffy clock"). Negative arguments cause the RND function to be re-seeded with each function call. EXAMPLES of RND Function: 220 PRINT INT(RND(0)*50) (Return random integers 0-49) 100 X=INT(RND(1)*6)+INT(RND(1)*6)+2 (Simulates 2 dice) 100 X=INT(RND(1)*1000)+1 (Random integers from 1-1000) 100 X=INT(RND(1)*150)+100 (Random numbers from 100-249) 100 X=RND(1)*(U-L)+L (Random numbers between upper (U) and lower (L) limits) 80 BASIC LANGUAGE VOCABULARY ~ RUN TYPE: Command FORMAT: RUN [] Action: The system command RUN is used to start the program currently in memory. The RUN command causes an implied CLR operation to be performed before starting the program. You can avoid the CLeaRing operation by using CONT or GOTO to restart a program instead of RUN. If a is specified, your program will start on that line. Otherwise, the RUN command starts at first line of the program. The RUN command can also be used within a program. If the you specify doesn't exist, the BASIC error message UNDEF'D STATEMENT occurs. A RUNning program stops and BASIC returns to direct mode when an END or STOP statement is reached, when the last line of the program is finished, or when a BASIC error occurs during execution. EXAMPLES of RUN Command: RUN (Starts at first line of program) RUN 500 (Starts at line-number 500) RUN X (Starts at line X, or UNDEF'D STATEMENT ERROR if there is no line X) SAVE TYPE: Command FORMAT: SAVE [""][,][,
] Action: The SAVE command is used to store the program that is cur- rently in memory onto a tape or diskette file. The program being SAVED is only affected by the command while the SAVE is happening. The program remains in the current computer memory even after the SAVE operation is completed until you put something else there by using another command. The file type will be "prg" (program). If the is left out, then the C64 will automatically assume that you want the program saved on cassette, device number 1. If the is an <8>, then the program is written onto disk. The SAVE statement can be used BASIC LANGUAGE VOCABULARY 81 ~ be used in your programs and execution will continue with the next statement after the SAVE is completed. Programs on tape are automatically stored twice, so that your Com- modore 64 can check for errors when LOADing the program back in. When saving programs to tape, the and secondary
are optional. But following a SAVE with a program name in quotes ("") or by a string variable (---$) helps your Commodore 64 find each program more easily. If the file-name is left out it can NOT be LOADed by name later on. A secondary address of I will tell the KERNAL to LOAD the tape at a later time, with the program currently in memory instead of the normal 2048 location. A secondary address of 2 will cause an end-of-tape marker to follow the program. A secondary address of 3 combines both functions. When saving programs onto a disk, the must be present. EXAMPLES of SAVE Command. SAVE (Write to tape without a name) SAVE"ALPHA",1 (Store on tape as file-name "alpha") SAVE"ALPHA",1,2 (Store "alpha" with end-of-tape marker) SAVE"FUN.DISK",8 (SAVES on disk (device 8 is the disk)) SAVE A$ (Store on tape with the name A$) 10 SAVE"HI" (SAVEs program and then move to next program line) SAVE"ME",1,3 (Stores at same memory location and puts an end-of-tope marker on) 82 BASIC LANGUAGE VOCABULARY ~ SGN TYPE: Integer Function FORMAT: SGN () Action: SGN gives you an integer value depending upon the sign of the argument. If the argument is positive the result is 1, if zero the result is also 0, if negative the result is -1. EXAMPLE of SGN Function: 90 ON SGN(DV)+2 GOTO 100, 200, 300 (jump to 100 if DV=negative, 200 if DV=0, 300 if DV=positive) SIN TYPE: Floating-Point Function FORMAT: SIN () Action: SIN gives you the sine of the argument, in radians. The value of COS(X) is equal to SIN(x+3.14159265/2). EXAMPLE of SIN Function: 235 AA=SIN(1.5):PRINT AA .997494987 SPC TYPE: String Function FORMAT: SPC () Action: The SPC function is used to control the formatting of data, as either an output to the screen or into a logical file. The number of SPaCes given by the argument are printed, starting at the first available position. For screen or tape files the value of the argument is in the range of 0 to 255 and for disk files up to 254. For printer files, an automatic carriage-return and line-feed will be performed by the printer if a SPaCe is printed in the last character position of a line. No SPaCes are printed on the following line. BASIC LANGUAGE VOCABULARY 83 ~ EXAMPLE of SPC Function: 10 PRINT"RIGHT "; "HERE &"; 20 PRINT SPC(5)"OVER" SPC(14)"THERE" RUN RIGHT HERE & OVER THERE SQR TYPE: Floating-Point Function FORMAT: SQR () Action: SQR gives you the value of the SQuare Root of the argument. The value of the argument must not be negative, or the BASIC error message ?ILLEGAL QUANTITY will happen. EXAMPLE of SQR Function: FOR J = 2 TO 5: PRINT J*S, SQR(J*5): NEXT 10 3.16227766 15 3.87298335 20 4.47213595 25 5 READY STATUS TYPE: Integer Function FORMAT: STATUS Action: Returns a completion STATUS for the last input/output operation which was performed on an open file. The STATUS can be read from any peripheral device. The STATUS (or simply ST) keyword is a system defined 84 BASIC LANGUAGE VOCABULARY ~ variable-name into which the KERNAL puts the STATUS of I/O operations. A table of STATUS code values for tape, printer, disk and RS-232 file operations is shown below: +---------+------------+---------------+------------+-------------------+ | ST Bit | ST Numeric | Cassette | Serial | Tape Verify | | Position| Value | Read | Bus R/W | + Load | +---------+------------+---------------+------------+-------------------+ | 0 | 1 | | time out | | | | | | write | | +---------+------------+---------------+------------+-------------------+ | 1 | 2 | | time out | | | | | | read | | +---------+------------+---------------+------------+-------------------+ | 2 | 4 | short block | | short block | +---------+------------+---------------+------------+-------------------+ | 3 | 8 | long block | | long block | +---------+------------+---------------+------------+-------------------+ | 4 | 16 | unrecoverable | | any mismatch | | | | read error | | | +---------+------------+---------------+------------+-------------------+ | 5 | 32 | checksum | | checksum | | | | error | | error | +---------+------------+---------------+------------+-------------------+ | 6 | 64 | end of file | EOI | | +---------+------------+---------------+------------+-------------------+ | 7 | -128 | end of tape | device not | end of tape | | | | | present | | +---------+------------+---------------+------------+-------------------+ EXAMPLES of STATUS Function: 10 OPEN 1,4:OPEN 2,8,4,"MASTER FILE,SEQ,W" 20 GOSUB 100:REM CHECK STATUS 30 INPUT#2,A$,B,C 40 IF STATUS AND 64 THEN 80:REM HANDLE END-OF-FILE 50 GOSUB 100:REM CHECK STATUS 60 PRINT#1,A$,B;C 70 GOTO 20 80 CLOSE1:CLOSE2 90 GOSUB 100:END 100 IF ST > 0 THEN 9000:REM HANDLE FILE I/O ERROR 110 RETURN BASIC LANGUAGE VOCABULARY 85 ~ STEP TYPE: Statement FORMAT: [STEP ] Action: The optional STEP keyword follows the expression in a FOR statement. It defines an increment value for the loop counter variable. Any value can be used as the STEP increment. Of course, a STEP value of zero will loop forever. If the STEP keyword is left out, the increment value will be + 1. When the NEXT statement in a FOR loop is reached, the STEP increment happens. Then the counter is tested against the end-value to see if the loop is finished. (See FOR statement for more information.) +-----------------------------------------------------------------------+ | NOTE: The STEP value can NOT be changed once it's in the loop. | +-----------------------------------------------------------------------+ EXAMPLES of STEP Statement: 25 FOR XX=2 TO 20 STEP 2 (Loop repeats 10 times) 35 FOR ZZ=0 TO -20 STEP -2 (Loop repeats 11 times) STOP TYPE: Statement FORMAT: STOP Action: The STOP statement is used to halt execution of the current program and return to direct mode. Typing the key on the keyboard has the same effect as a STOP statement. The BASIC error message ?BREAK IN LINE nnnnn is displayed on the screen, followed by READY. The "nnnnn" is the line-number where the STOP occurs. Any open files remain open and all variables are preserved and can be examined. The program can be restarted by using CONT or GOTO statements. EXAMPLES of STOP Statement: 10 INPUT#1,AA,BB,CC 20 IF AA=BB AND BB=CC THEN STOP 30 STOP (If the variable AA is -1 and BB is equal to CC then:) BREAK IN LINE 20 BREAK IN LINE 30 (For any other data values) 86 BASIC LANGUAGE VOCABULARY ~ STR$ TYPE: String Function FORMAT: STR$ () Action: STR$ gives you the STRing representation of the numeric value of the argument. When the STR$ value is converted to each variable represented in the argument, any number shown is followed by a space and, if it's positive, it is also preceded by a space. EXAMPLE of STR$ Function: 100 FLT = 1.5E4: ALPHA$ = STR$(FLT) 110 PRINT FLT, ALPHA$ 15000 15000 SYS TYPE: Statement FORMAT: SYS Action: This is the most common way to mix a BASIC program with a machine language program. The machine language program begins at the location given in the SYS statement. The system command SYS is used in either direct or program mode to transfer control of the microprocessor to an existing machine language program in memory. The memory-location given is by numeric expression and can be anywhere in memory, RAM or ROM. When you're using the SYS statement you must end that section of machine language code with an RTS (ReTurn from Subroutine) instruction so that when the machine language program is finished, the BASIC execution will resume with the statement following the SYS command. EXAMPLES of SYS Statement: SYS 64738 (Jump to System Cold Start in ROM) 10 POKE 4400,96:SYS 4400 (Goes to machine code location 4400 and returns immediately) BASIC LANGUAGE VOCABULARY 87 ~ TAB TYPE: String Function FORMAT: TAB () Action: The TAB function moves the cursor to a relative SPC move position on the screen given by the argument, starting with the left-most position of the current line. The value of the argument can range from 0 to 255. The TAB function should only be used with the PRINT statement, since it has no effect if used with PRINT# to a logical file. EXAMPLE of TAB Function: 100 PRINT"NAME" TAB(25) "AMOUNT": PRINT 110 INPUT#1, NAM$, AMT$ 120 PRINT NAM$ TAB(25) AMT$ NAME AMOUNT G.T. JONES 25. TAN TYPE: Floating-Point Function FORMAT: TAN () Action: Returns the tangent of the value of the expression in radians. If the TAN function overflows, the BASIC error message ?DIVISION BY ZERO is displayed. EXAMPLE of TAN Function: 10 XX=.785398163: YY=TAN(XX):PRINT YY 1 88 BASIC LANGUAGE VOCABULARY ~ TIME TYPE: Numeric Function FORMAT: TI Action: The TI function reads the interval Timer. This type of "clock" is called a "jiffy clock." The "jiffy clock" value is set at zero (initialized) when you power-up the system. This 1/60 second interval timer is turned off during tape I/O. EXAMPLE of TI Function: 10 PRINT TI/60 "SECONDS SINCE POWER UP" TIME$ TYPE: String Function FORMAT: TI$ Action: The TI$ timer looks and works like a real clock as long as your system is powered-on. The hardware interval timer (or jiffy clock) is read and used to update the value of TI$, which will give you a TIme $tring of six characters in hours, minutes and seconds. The TI$ timer can also be assigned an arbitrary starting point similar to the way you set your wristwatch. The value of TI$ is not accurate after tape I/O. EXAMPLE of TI$ Function: 1 TI$ = "000000": FOR J=1 TO 10000: NEXT: PRINT TI$ 000011 BASIC LANGUAGE VOCABULARY 89 ~ USR TYPE: Floating-Point Function FORMAT: USR () Action: The USR function jumps to a User callable machine language SubRoutine which has its starting address pointed to by the contents of memory locations 785-786. The starting address is established before calling the USR function by using POKE statements to set up locations 785-786. Unless POKE statements are used, locations 785-786 will give you an ?ILLEGAL QUANTITY error message. The value of the argument is stored in the floating-point accumulator starting at location 97, for access by the Assembler code, and the result of the USR function is the value which ends up there when the subroutine returns to BASIC. EXAMPLES of USR Function: 10 B=T*SIN(Y) 20 C=USR(B/2) 30 D=USR(B/3) VAL TYPE: Numeric Function FORMAT: VAL () Action: Returns a numeric VALue representing the data in the argument. If the first non-blank character of the string is not a plus sign (+), minus sign (-), or a digit the VALue returned is zero. String conversion is finished when the end of the string or any non-digit character is found (except decimal point or exponential e). EXAMPLE of VAL Function: 10 INPUT#1, NAM$, ZIP$ 20 IF VAL(ZIP$) < 19400 OR VAL(ZIP$) > 96699 THEN PRINT NAM$ TAB(25) "GREATER PHILADELPHIA" 90 BASIC LANGUAGE VOCABULARY ~ VERIFY TYPE: Command FORMAT: VERIFY [""][,] Action: The VERIFY command is used, in direct or program mode, to compare the contents of a BASIC program file on tape or disk with the program currently in memory. VERIFY is normally used right after a SAVE, to make sure that the program was stored correctly on tape or disk. If the number is left out, the program is assumed to be on the Datassette(TM) which is device number 1. For tape files, if the is left out, the next program found on the tape will be com- pared. For disk files (device number 8), the file-name must be present. If any differences in program text are found, the BASIC error message ?VERIFY ERROR is displayed. A program name can be given either in quotes or as a string variable. VERIFY is also used to position a tape just past the last program, so that a new program can be added to the tape without accidentally writing over another program. EXAMPLES of VERIFY Command: VERIFY (Checks 1st program on tape) PRESS PLAY ON TAPE OK SEARCHING FOUND VERIFYING 9000 SAVE "ME",8: 9010 VERIFY "ME",8 (Looks at device 8 for the program) BASIC LANGUAGE VOCABULARY 91 ~ WAIT TYPE: Statement FORMAT: WAIT ,[,] Action: The WAIT statement causes program execution to be suspended until a given memory address recognizes a specified bit pattern. In other words WAIT can be used to halt the program until some external event has occurred. This is done by monitoring the status of bits in the input/ output registers, The data items used with WAIT can be any numeric expressions, but they will be converted to integer values. For most programmers, this statement should never be used. It causes the program to halt until a specific memory location's bits change in a specific way. This is used for certain I/O operations and almost nothing else. The WAIT statement takes the value in the memory location and performs a logical AND operation with the value in mask-1. If there is a mask-2 in the statement, the result of the first operation is exclusive-ORed with mask-2. In other words mask-1 "filters out" any bits that you don't want to test. Where the bit is 0 in mask-1, the corresponding bit in the result will always be 0. The mask-2 value flips any bits, so that you can test for an off condition as well as an on condition, Any bits being tested for a 0 should have a I in the corresponding position in mask-2. If corresponding bits of the and operands differ, the exclusive-OR operation gives a bit result of 1. If corresponding bits get the same result the bit is 0. It is possible to enter an infinite pause with the WAIT statement, in which case the and keys can be used to recover. Hold down the key and then press . The first example below WAITs until a key is pressed on the tape unit to continue with the program. The second example will WAIT until a sprite collides with the screen background. EXAMPLES of WAIT Statement: WAIT 1,32,32 WAIT 53273,6,6 WAIT 36868,144,16 (144 & 16 are masks. 144=10010000 in binary and 16=10000 in binary. The WAIT statement will halt the program until the 128 bit is on or until the 16 bit is off) 92 BASIC LANGUAGE VOCABULARY ~ THE COMMODORE 64 KEYBOARD AND FEATURES The Operating System has a ton-character keyboard "buffer" that is used to hold incoming keystrokes until they can be processed. This buffer, or queue, holds keystrokes in the order in which they occur so that the first one put into the queue is the first one processed. For example, if a second keystroke occurs before the first can be processed, the second character Is stored in the buffer, while processing of the first character continues. After the program has finished with the first character, the keyboard buffer is examined for more data, and the second keystroke processed. Without this buffer, rapid keyboard input would occasionally drop characters. In other words, the keyboard buffer allows you to "type-ahead" of the system, which means it can anticipate responses to INPUT prompts or GET statements. As you type on the keys their character values are lined up, single-file (queued) into the buffer to wait for processing in the order the keys were struck. This type-ahead feature can give you an occasional problem where an accidental keystroke causes a program to fetch an incorrect character from the buffer. Normally, incorrect keystrokes present no problem, since they can be corrected by the CuRSoR-Left or DELete keys and then retyping the character, and the corrections will be processed before a following carriage-return. However, if you press the key, no corrective action is possible, since all characters in the buffer up to and including the carriage-return will be processed before any cor- rections. This situation can be avoided by using a loop to empty the keyboard buffer before reading an intended response: 10 GET JUNK$: IF JUNK$ <>"" THEN 10: REM EMPTY THE KEYBOARD BUFFER In addition to GET and INPUT, the keyboard can also be read using PEEK to fetch from memory location 197 ($00C5) the integer value of the key currently being pressed. If no key Is being held when the PEEK is executed, a value of 64 is returned, The numeric keyboard values, keyboard symbols and character equivalents (CHR$) are shown in Ap- pendix C. The following example loops until a key is pressed then con- verts the integer to a character value. 10 AA=PEEK(197): IF AA=64 THEN 10 20 BB$=CHR$(AA) BASIC LANGUAGE VOCABULARY 93 ~ The keyboard is treated as a set of switches organized into a matrix of 8 columns by 8 rows. The keyboard matrix is scanned for key switch- closures by the KERNAL using the CIA #l 1/0 chip (MOS 6526 Complex Interface Adapter). Two CIA registers are used to perform the scan: register #0 at location 56320 ($DC00) for keyboard columns and register #l at location 56321 ($DC01) for keyboard rows. Bits 0-7 of memory location 56320 correspond to the columns 0-7. Bits 0-7 of memory location 56321 correspond to rows 0-7. By writing column values in sequence, then reading row values, the KERNAL decodes the switch closures into the CHR$ (N) value of the key pressed. Eight columns by eight rows yields 64 possible values. However, if you first strike the , or keys or hold down the key and type a second character, additional values are generated. This is because the KERNAL decodes these keys separately and "remembers" when one of the control keys was pressed. The result of the keyboard scan is then placed in location 197. Characters can also be written directly to the keyboard buffer at lo- cations 631-640 using a POKE statement. These characters will be processed when the POKE is used to set a character count into location 198. These facts can be used to cause a series of direct-mode commands to be executed automatically by printing the statements onto the screen, putting carriage-returns into the buffer, and then setting the character count. In the example below, the program will LIST itself to the printer and then resume execution. 10 PRINT CHR$(147)"PRINT#1: CLOSE 1: GOTO 50" 20 POKE 631119: POKE 632,13: POKE 633,13: POKE 198,3 30 OPEN 114: CMD1: LIST 40 END 50 REM PROGRAM RE-STARTS HERE SCREEN EDITOR The SCREEN EDITOR provides you with powerful and convenient facilities for editing program text. Once a section of a program is listed to the screen, the cursor keys and other special keys are used to move around the screen so that you can make any appropriate changes. After making all the changes you want to a specific line-number of text, hitting the key anywhere on the line, causes the SCREEN EDITOR to read the entire 80-character logical screen line. 94 BASIC LANGUAGE VOCABULARY ~ The text is then passed to the Interpreter to be tokenized and stored in the program. The edited line replaces the old version of that line in memory. An additional copy of any line of text can be created simply by changing the line-number and pressing . If you use keyword abbreviations which cause a program line to exceed 80 characters, the excess characters will be lost when that line is edited, because the EDITOR will read only two physical screen lines. This is also why using INPUT for more than a total of 80 characters is not possible. Thus, for all practical purposes, the length of a line of BASIC text is limited to 80 characters as displayed on the screen. Under certain conditions the SCREEN EDITOR treats the cursor control keys differently from their normal mode of handling. If the CuRSoR is positioned to the right of an odd number of double-quote marks (") the EDITOR operates in what is known as the QUOTE-MODE. In quote mode data characters are entered normally but the cursor controls no longer move the CuRSoR, instead reversed characters are displayed which actually stand for the cursor control being entered. The same is true of the color control keys. This allows you to include cursor and color controls inside string data items in programs. You will find that this is a very important and powerful feature. That's because when the text inside the quotes is printed to the screen it performs the cursor positioning and color control functions automatically as part of the string. An example of using cursor controls in strings is: You type --> 10 PRINT"A(R)(R)B(L)(L)(L)C(R)(R)D": REM(R)=CRSR RIGHT, (L)=CRSR LEFT Computer prints --> AC BD The key is the only cursor control NOT affected by quote mode. Therefore, if an error is made while keying in quote mode, the key can't be used to back up and strike over the error - even the key produces a reverse video character. Instead, finish entering the line, and then, after hitting the key, you can edit the line normally. Another alternative, if no further cursor- controls are needed in the string, is to press the and keys which will cancel QUOTE MODE. The cursor control keys that you can use in strings are shown in Table 2-2. BASIC LANGUAGE VOCABULARY 95 ~ Table 2-2. Cursor Control Characters in QUOTE MODE ------------------------------------------------------------------------- Control Key Appearance ------------------------------------------------------------------------- CRSR up CRSR down CRSR left CRSR right CLR HOME INST ------------------------------------------------------------------------- When you are NOT in quote mode, holding down the key and then pressing the INSerT key shifts data to the right of the cursor to open up space between two characters for entering data between them. The Editor then begins operating in INSERT MODE until all of the space opened up is filled. The cursor controls and color controls again show as reversed char- acters in insert mode. The only difference occurs on the DELete and INSerT key. The instead of operating normally as in the quote mode, now creates the reversed . The key, which created a reverse character in quote mode, inserts spaces normally. This means that a PRINT statement can be created, containing DELetes, which can't be done in quote mode. The insert mode is cancelled by pressing the , and , or and keys. Or you can cancel the insert mode by filling all the inserted spaces. An example of using DEL characters in strings is: 10 PRINT"HELLO"P" (Keystroke sequence shown above, appearance when listed below) 10 PRINT"HELP" When the example is RUN, the word displayed will be HELP, because the letters LO are deleted before the P is printed. The DELete character in strings will work with LIST as well as PRINT. You can use this to "hide" part or all of a line of text using this technique. However, trying to edit a line with these characters will be difficult if not impossible. 96 BASIC LANGUAGE VOCABULARY ~ There are some other characters that can be printed for special func- tions, although they are not easily available from the keyboard. In order to get these into quotes, you must leave empty spaces for them in the line, press , and go back to edit the line. Now you hold down the (ConTRoL) key and type (ReVerSe-ON) to start typing reversed characters. Type the keys as shown below: Key Function Key Entered Appearance Shifted RETURN Switch to upper/lower case Switch to upper/graphics Holding down the key and hitting causes a carriage- return and line-feed on the screen but does not end the string. This works with LIST as well as PRINT, so editing will be almost impossible if this character is used. When output is switched to the printer via the CMD statement, the reverse "N" character shifts the printer into its upper-lower case character set and the "N" shifts the printer into the upper-case/graphics character set. Reverse video characters can be included in strings by holding down the ConTRoL key and pressing ReVerSe , causing a reversed R to appear inside the quotes. This will make all characters print in reverse video (like a negative of a photograph). To end the reverse printing, press and (ReVerSe OFF) by holding down the key and typing the key, which prints a reverse R. Numeric data can be printed in reverse video by first printing a CHR$(18). Printing a CHR$(146) or a carriage-return will cancel reverse video output. BASIC LANGUAGE VOCABULARY 97 ~~ CHAPTER 3 PROGRAMMING GRAPHICS ON THE COMMODORE 64 o Graphics Overview o Graphics Locations o Standard Character Mode o Programmable Characters o Multi-Color Mode Graphics o Extended Background Color Mode o Bit Mapped Graphics o Multi-Color Bit Map Mode o Smooth Scrolling o Sprites o Other Graphics Features o Programming Sprites - Another Look 99 ~ GRAPHICS OVERVIEW All of the graphics abilities of the Commodore 64 come from the 6567 Video Interface Chip (also known as the VIC-II chip). This chip gives a variety of graphics modes, including a 40 column by 25 line text display, a 320 by 200 dot high resolution display, and SPRITES, small movable objects which make writing games simple. And if this weren't enough, many of the graphics modes can be mixed on the same screen. It is possible, for example, to define the top half of the screen to be in high resolution mode, while the bottom half is in text mode. And SPRITES will combine with anything! More on sprites later. First the other graphics modes. The VIC-II chip has the following graphics display modes: A) CHARACTER DISPLAY MODES 1) Standard Character Mode a)ROM characters b)RAM programmable characters 2) Multi-Color Character Mode a)ROM characters b)RAM programmable characters 3) Extended Background Color Mode a)ROM characters b)RAM programmable characters B) BIT MAP MODES 1) Standard Bit Map Mode 2) Multi-Color Bit Map Mode C) SPRITES 1) Standard Sprites 2) Multi-Color Sprites 100 PROGRAMMING GRAPHICS ~ GRAPHICS LOCATIONS Some general information first. There are 1000 possible locations on the Commodore 64 screen. Normally, the screen starts at location 1024 ($0400 in HEXadecimal notation) and goes to location 2023. Each of these locations is 8 bits wide. This means that it can hold any integer number from 0 to 255. Connected with screen memory is a group of 1000 locations called COLOR MEMORY or COLOR RAM. These start at location 55296 ($D800 in HEX) and go up to 56295. Each of the color RAM locations is 4 bits wide, which means that it can hold any integer number from 0 to 15. Since there are 16 possible colors that the Commodore 64 can use, this works out well. In addition, there are 256 different characters that can be displayed at any time. For normal screen display, each of the 1000 locations in screen memory contains a code number which tells the VIC-II chip which character to display at that screen location. The various graphics modes are selected by the 47 CONTROL registers in the VIC-II chip. Many of the graphics functions can be controlled by POKEing the correct value into one of the registers. The VIC-II chip is located starting at 53248 ($D000 in HEX) through 53294 ($D02E in HEX). VIDEO BANK SELECTION The VIC-II chip can access ("see") 16K of memory at a time. Since there is 64K of memory in the Commodore 64, you want to be able to have the VIC-II chip see all of it. There is a way. There are 4 possible BANKS (or sections) of 16K of memory. All that is needed is some means of controlling which 16K bank the VIC-II chip looks at. In that way, the chip can "see" the entire 64K of memory. The BANK SELECT bits that allow you access to all the different sections of memory are located in the 6526 COMPLEX INTERFACE ADAPTER CHIP #2 (CIA #2). The POKE and PEEK BASIC statements (or their machine language versions) are used to select a bank, by controlling bits 0 and 1 of PORT A of CIA#2 (location 56576 (or $DD00 HEX)). These 2 bits must be set to outputs by setting bits 0 and 1 of location 56578 ($DD02,HEX) to change banks. The following example shows this: POKE 56578,PEEK(56578)OR 3: REM MAKE SURE BITS 0 AND 1 ARE OUTPUTS POKE 56576,(PEEK(56576)AND 252)OR A: REM CHANGE BANKS "A" should have one of the following values: PROGRAMMING GRAPHICS 101 ~ +-------+------+-------+----------+-------------------------------------+ | VALUE | BITS | BANK | STARTING | VIC-II CHIP RANGE | | OF A | | | LOCATION | | +-------+------+-------+----------+-------------------------------------+ | 0 | 00 | 3 | 49152 | ($C000-$FFFF)* | | 1 | 01 | 2 | 32768 | ($8000-$BFFF) | | 2 | 10 | 1 | 16384 | ($4000-$7FFF)* | | 3 | 11 | 0 | 0 | ($0000-$3FFF) (DEFAULT VALUE) | +-------+------+-------+----------+-------------------------------------+ This 16K bank concept is part of everything that the VIC-II chip does. You should always be aware of which bank the VIC-II chip is pointing at, since this will affect where character data patterns come from, where the screen is, where sprites come from, etc. When you turn on the power of your Commodore 64, bits 0 and 1 of location 56576 are automatically set to BANK 0 ($0000-$3FFF) for all display information. +-----------------------------------------------------------------------+ | *NOTE: The Commodore 64 character set is not available to the VIC-II | | chip in BANKS 1 and 3. (See character memory section.) | +-----------------------------------------------------------------------+ SCREEN MEMORY The location of screen memory can be changed easily by a POKE to control register 53272 ($D018 HEX). However, this register is also used to control which character set is used, so be careful to avoid disturbing that part of the control register. The UPPER 4 bits control the location of screen memory. To move the screen, the following statement should be used: POKE53272,(PEEK(53272)AND15)OR A 102 PROGRAMMING GRAPHICS ~ Where "A" has one of the following values: +---------+------------+-----------------------------+ | | | LOCATION* | | A | BITS +---------+-------------------+ | | | DECIMAL | HEX | +---------+------------+---------+-------------------+ | 0 | 0000XXXX | 0 | $0000 | | 16 | 0001XXXX | 1024 | $0400 (DEFAULT) | | 32 | 0010XXXX | 2048 | $0800 | | 48 | 0011XXXX | 3072 | $0C00 | | 64 | 0100XXXX | 4096 | $1000 | | 80 | 0101XXXX | 5120 | $1400 | | 96 | 0110XXXX | 6144 | $1800 | | 112 | 0111XXXX | 7168 | $1C00 | | 128 | 1000XXXX | 8192 | $2000 | | 144 | 1001XXXX | 9216 | $2400 | | 160 | 1010XXXX | 10240 | $2800 | | 176 | 1011XXXX | 11264 | $2C00 | | 192 | 1100XXXX | 12288 | $3000 | | 208 | 1101XXXX | 13312 | $3400 | | 224 | 1110XXXX | 14336 | $3800 | | 240 | 1111XXXX | 15360 | $3C00 | +---------+------------+---------+-------------------+ +-----------------------------------------------------------------------+ | * Remember that the BANK ADDRESS of the VIC-II chip must be added in. | | You must also tell the KERNAL'S screen editor where the screen is as | | follows: POKE 648, page (where page = address/256, e.g., 1024/256= 4, | | so POKE 648,4). | +-----------------------------------------------------------------------+ COLOR MEMORY Color memory can NOT move. It is always located at locations 55296 ($D800) through 56295 ($DBE7). Screen memory (the 1000 locations starting at 1024) and color memory are used differently in the different graphics modes. A picture created in one mode will often look completely different when displayed in another graphics mode. CHARACTER MEMORY Exactly where the VIC-II gets it character information is important to graphic programming. Normally, the chip gets the shapes of the characters PROGRAMMING GRAPHICS 103 ~ you want to be displayed from the CHARACTER GENERATOR ROM. In this chip are stored the patterns which make up the various letters, numbers, punctuation symbols, and the other things that you see on the keyboard. One of the features of the Commodore 64 is the ability to use patterns located in RAM memory. These RAM patterns are created by you, and that means that you can have an almost infinite set of symbols for games, business applications, etc. A normal character set contains 256 characters in which each character is defined by 8 bytes of data. Since each character takes up 8 bytes this means that a full character set is 256*8=2K bytes of memory. Since the VIC-II chip looks at 16K of memory at a time, there are 8 possible locations for a complete character set. Naturally, you are free to use less than a full character set. However, it must still start at one of the 8 possible starting locations. The location of character memory is controlled by 3 bits of the VIC-II control register located at 53272 ($D018 in HEX notation). Bits 3,2, and 1 control where the characters' set is located in 2K blocks. Bit 0 is ig- nored. Remember that this is the same register that determines where screen memory is located so avoid disturbing the screen memory bits. To change the location of character memory, the following BASIC statement can be used: POKE 53272,(PEEK(53272)AND240)OR A Where A is one of the following values: +-----+----------+------------------------------------------------------+ |VALUE| | LOCATION OF CHARACTER MEMORY* | | of A| BITS +-------+----------------------------------------------+ | | |DECIMAL| HEX | +-----+----------+-------+----------------------------------------------+ | 0 | XXXX000X | 0 | $0000-$07FF | | 2 | XXXX001X | 2048 | $0800-$0FFF | | 4 | XXXX010X | 4096 | $1000-$17FF ROM IMAGE in BANK 0 & 2 (default)| | 6 | XXXX011X | 6144 | $1800-$1FFF ROM IMAGE in BANK 0 & 2 | | 8 | XXXX100X | 8192 | $2000-$27FF | | 10 | XXXX101X | 10240 | $2800-$2FFF | | 12 | XXXX110X | 12288 | $3000-$37FF | | 14 | XXXX111X | 14336 | $3800-$3FFF | +-----+----------+-------+----------------------------------------------+ +-----------------------------------------------------------------------+ | * Remember to add in the BANK address. | +-----------------------------------------------------------------------+ 104 PROGRAMMING GRAPHICS ~ The ROM IMAGE in the above table refers to the character generator ROM. It appears in place of RAM at the above locations in bank 0. it also appears in the corresponding RAM at locations 36864-40959 ($9000-$9FFF) in bank 2. Since the VIC-II chip can only access 16K of memory at a time, the ROM character patterns appear in the 16K block of memory the VIC-II chip looks at. Therefore, the system was designed to make the VIC-II chip think that the ROM characters are at 4096-8191 ($1000-$1FFF) when your data is in bank 0, and 36864-40959 ($9000-$9FFF) when your data is in bank 2, even though the character ROM is actually at location 53248-57343 ($D000-$DFFF). This imaging only applies to character data as seen by the VIC-II chip. It can be used for programs, other data, etc., just like any other RAM memory. +-----------------------------------------------------------------------+ | NOTE: If these ROM images got in the way of your own graphics, then | | set the BANK SELECT BITS to one of the BANKS without the images | | (BANKS 1 or 3). The ROM patterns won't be there. | +-----------------------------------------------------------------------+ The location and contents of the character set in ROM are as follows: +-----+-------------------+-----------+---------------------------------+ | | ADDRESS | VIC-II | | |BLOCK+-------+-----------+ IMAGE | CONTENTS | | |DECIMAL| HEX | | | +-----+-------+-----------+-----------+---------------------------------+ | 0 | 53248 | D000-D1FF | 1000-11FF | Upper case characters | | | 53760 | D200-D3FF | 1200-13FF | Graphics characters | | | 54272 | D400-D5FF | 1400-15FF | Reversed upper case characters | | | 54784 | D600-D7FF | 1600-17FF | Reversed graphics characters | | | | | | | | 1 | 55296 | D800-D9FF | 1800-19FF | Lower case characters | | | 55808 | DA00-DBFF | 1A00-1BFF | Upper case & graphics characters| | | 56320 | DC00-DDFF | 1C00-1DFF | Reversed lower case characters | | | 56832 | DE00-DFFF | 1E00-1FFF | Reversed upper case & | | | | | | graphics characters | +-----+-------+-----------+-----------+---------------------------------+ Sharp-eyed readers will have just noticed something. The locations occupied by the character ROM are the same as the ones occupied by the VIC-II chip control registers. This is possible because they don't occupy the same locations at the same time. When the VIC-II chip needs to access PROGRAMMING GRAPHICS 105 ~ character data the ROM is switched in. It becomes an image in the 16K bank of memory that the VIC-II chip is looking at. Otherwise, the area is occupied by the I/O control registers, and the character ROM is only available to the VIC-II chip. However, you may need to get to the character ROM if you are going to use programmable characters and want to copy some of the character ROM for some of your character definitions. In this case you must switch out the I/O register, switch in the character ROM, and do your copying. When you're finished, you must switch the 1/0 registers back in again. During the copying process (when I/O is switched out) no interrupts can be allowed to take place. This is because the I/O registers are needed to service the interrupts. If you forget and perform an interrupt, really strange things happen. The keyboard should not be read during the copying process. To turn off the keyboard and other normal interrupts that occur with your Commodore 64, the following POKE should be used: POKE 56334,PEEK(56334)AND254 (TURNS INTERRUPTS OFF) After you are finished getting characters from the character ROM, and are ready to continue with your program, you must turn the keyboard scan back on by the following POKE: POKE 56334,PEEK(56334)OR1 (TURNS INTERRUPTS ON) The following POKE will switch out 1/0 and switch the CHARACTER ROM in: POKE 1,PEEK(1)AND251 The character ROM is now in the locations from 53248-57343 ($D000- $DFFF). To switch I/O back into $D000 for normal operation use the following POKE: POKE 1,PEEK(1)OR 4 106 PROGRAMMING GRAPHICS ~ STANDARD CHARACTER MODE Standard character mode is the mode the Commodore 64 is in when you first turn it on. It is the mode you will generally program in. Characters can be taken from ROM or from RAM, but normally they are taken from ROM. When you want special graphics characters for a program, all you have to do is define the new character shapes in RAM, and tell the VIC-II chip to get its character information from there instead of the character ROM. This is covered in more detail in the next section. In order to display characters on the screen in color, the VIC-II chip accesses the screen memory to determine the character code for that location on the screen. At the same time, it accesses the color memory to determine what color you want for the character displayed. The character code is translated by the VIC-II into the starting address of the 8-byte block holding your character pattern. The 8-byte block is located in character memory. The translation isn't too complicated, but a number of items are com- bined to generate the desired address. First the character code you use to POKE screen memory is multiplied by 8. Next add the start of char- acter memory (see CHARACTER MEMORY section). Then the Bank Select Bits are taken into account by adding in the base address (see VIDEO BANK SELECTION section). Below is a simple formula to illustrate what happens: CHARACTER ADDRESS = SCREEN CODE*8+(CHARACTER SET*2048)+(BANK*16384) CHARACTER DEFINITIONS Each character is formed in an 8 by 8 grid of dots, where each dot may be either on or off. The Commodore 64 character images are stored in the Character Generator ROM chip. The characters are stored as a set of 8 bytes for each character, with each byte representing the dot pattern of a row in the character, and each bit representing a dot. A zero bit means that dot is off, and a one bit means the dot is on. The character memory in ROM begins at location 53248 (when the I/O is switched off). The first 8 bytes from location 53248 ($D000) to 53255 ($D007) contain the pattern for the @ sign, which has a character code value of zero in the screen memory. The next 8 bytes, from location PROGRAMMING GRAPHICS 107 ~ 53256 ($D008) to 53263 ($D00F), contain the information for forming the letter A. IMAGE BINARY PEEK ** 00011000 24 **** 00111100 60 ** ** 01100110 102 ****** 01111110 126 ** ** 01100110 102 ** ** 01100110 102 ** ** 01100110 102 00000000 0 Each complete character set takes up 2K (2048 bits) of memory, 8 bytes per character and 256 characters. Since there are two character sets, one for upper case and graphics and the other with upper and lower case, the character generator ROM takes up a total of 4K locations. PROGRAMMABLE CHARACTERS Since the characters are stored in ROM, it would seem that there is no way to change them for customizing characters. However, the memory location that tells the VIC-II chip where to find the characters is a programmable register which can be changed to point to many sections of memory. By changing the character memory pointer to point to RAM, the character set may be programmed for any need. If you want your character set to be located in RAM, there are a few VERY IMPORTANT things to take into account when you decide to actually program your own character sets. In addition, there are two other important points you must know to create your own special characters: 1) It is an all or nothing process. Generally, if you use your own character set by telling the VIC-II chip to get the character information from the area you have prepared in RAM, the standard Commodore 64 characters are unavailable to you. To solve this, you must copy any letters, numbers, or standard Commodore 64 graphics you intend to use into your own character memory in RAM. You can pick and choose, take only the ones you want, and don't even have to keep them in order! 108 PROGRAMMING GRAPHICS ~ 2) Your character set takes memory space away from your BASIC program. Of course, with 38K available for a BASIC program, most applications won't have problems. +-----------------------------------------------------------------------+ | WARNING: You must be careful to protect the character set from being | | overwritten by your BASIC program, which also uses the RAM. | +-----------------------------------------------------------------------+ There are two locations in the Commodore 64 to start your character set that should NOT be used with BASIC: location 0 and location 2048. The first should not be used because the system stores important data on page 0. The second can't be used because that is where your BASIC program starts! However, there are 6 other starting positions for your custom character set. The best place to put your character set for use with BASIC while experimenting is beginning at 12288 ($3000 in HEX). This is done by POKEing the low 4 bits of location 53272 with 12. Try the POKE now, like this: POKE 53272,(PEEK(53272)AND240)+12 Immediately, all the letters on the screen turn to garbage, This is because there are no characters set up at location 12288 right now... only random bytes. Set the Commodore 64 back to normal by hitting the key and then the key. Now let's begin creating graphics characters. To protect your char- acter set from BASIC, you should reduce the amount of memory BASIC thinks it has. The amount of memory in your computer stays the same... it's just that you've told BASIC not to use some of it. Type: PRINT FRE(0)-(SGN(FRE(0))<0)*65535 The number displayed is the amount of memory space left unused. Now type the following: POKE 52148:POKE56,48:CLR Now type: PRINT FRE(0)-(SGN(FRE(0))<0)*65535 PROGRAMMING GRAPHICS 109 ~ See the change? BASIC now thinks it has less memory to work with. The memory you just claimed from BASIC is where you are going to put your character set, safe from actions of BASIC. The next step is to put your characters into RAM. When you begin, there is random data beginning at 12288 ($3000 HEX). You must put character patterns in RAM (in the same style as the ones in ROM) for the VIC-II chip to use. The following program moves 64 characters from ROM to your character set RAM: start tok64 page110.prg 5 printchr$(142) :rem switch to upper case 10 poke52,48:poke 56,48:clr :rem reserve memory for characters 20 poke56334,peek(56334)and254 :rem turn off keyscan interrupt timer 30 poke1,peek(1)and251 :rem switch in character 40 fori=0to511:pokei+12288,peek(i+53248):next 50 poke1,peek(1)or4 :rem switch in i/o 60 poke56334,peek(56334)or1 :rem restart keyscan interrupt timer 70 end stop tok64 Now POKE location 53272 with (PEEK(53272)AND240)+12. Nothing happens, right? Well, almost nothing. The Commodore 64 is now getting it's character information from your RAM, instead of from ROM. But since we copied the characters from ROM exactly, no difference can be seen... yet. You can easily change the characters now. Clear the screen and type an @ sign. Move the cursor down a couple of lines, then type: FOR I=12288 TO 12288+7:POKE 1,255-PEEK(I):NEXT You just created a reversed @ sign! +-----------------------------------------------------------------------+ | TIP: Reversed characters are just characters with their bit patterns | | in character memory reversed. | +-----------------------------------------------------------------------+ Now move the cursor up to the program again and hit again to re-reverse the character (bring it back to normal). By looking at the table of screen display codes, you can figure out where in RAM each character is. Just remember that each character takes eight memory locations to store. Here's a few examples just to get you started: 110 PROGRAMMING GRAPHICS ~ +-----------+--------------+--------------------------------------------+ | CHARACTER | DISPLAY CODE | CURRENT STARTING LOCATION IN RAM | +-----------+--------------+--------------------------------------------+ | @ | 0 | 1228 | | A | 1 | 12296 | | ! | 33 | 12552 | | > | 62 | 12784 | +-----------+--------------+--------------------------------------------+ Remember that we only took the first 64 characters. Something else will have to be done if you want one of the other characters. What if you wanted character number 154, a reversed Z? Well, you could make it yourself, by reversing a Z, or you could copy the set of reversed characters from the ROM, or just take the one character you want from ROM and replace one of the characters you have in RAM that you don't need. Suppose you decide that you won't need the > sign. Let's replace the > sign with the reversed Z. Type this: FOR I=0 TO 7:POKE 12784+I,255-PEEK(I+12496):NEXT Now type a > sign. It comes up as a reversed Z. No matter how many times you type the >, it comes out as a reversed Z. (This change is really an illusion. Though the > sign looks like a reversed Z, it still acts like a > in a program. Try something that needs a > sign. It will still work fine, only it will look strange.) A quick review: You can now copy characters from ROM into RAM. You can even pick and choose only the ones you want. There's only one step left in programmable characters (the best step!)... making your own characters. Remember how characters are stored in ROM? Each character is stored as a group of eight bytes. The bit patterns of the bytes directly control the character. If you arrange 8 bytes, one on top of another, and write out each byte as eight binary digits, it forms an eight by eight matrix, looking like the characters. When a bit is a one, there is a dot at that location. When a bit is a zero, there is a space at that location. When creating your own characters, you set up the same kind of table in memory. Type NEW and then type this program: 10 FOR I=12448 TO 12455: READ A:POKE I,A:NEXT 20 DATA 60, 66, 165, 129, 165, 153, 66, 60 PROGRAMMING GRAPHICS 111 ~ Now type RUN. The program will replace the letter T with a smile face character. Type a few T's to see the face. Each of the numbers in the DATA statement in line 20 is a row in the smile face character. The matrix for the face looks like this: 76543210 BINARY DECIMAL +--------+ ROW 0 | **** | 00111100 60 1 | * * | 01000010 66 2 |* * * *| 10100101 165 3 |* *| 10000001 129 4 |* * * *| 10100101 165 5 |* ** *| 10011001 153 6 | * * | 01000010 66 ROW 7 | **** | 00111100 60 +--------+ 7 6 5 4 3 2 1 0 +-+-+-+-+-+-+-+-+ 0 | | | | | | | | | +-+-+-+-+-+-+-+-+ 1 | | | | | | | | | +-+-+-+-+-+-+-+-+ 2 | | | | | | | | | +-+-+-+-+-+-+-+-+ 3 | | | | | | | | | +-+-+-+-+-+-+-+-+ 4 | | | | | | | | | +-+-+-+-+-+-+-+-+ 5 | | | | | | | | | +-+-+-+-+-+-+-+-+ 6 | | | | | | | | | +-+-+-+-+-+-+-+-+ 7 | | | | | | | | | +-+-+-+-+-+-+-+-+ Figure 3-1. Programmable Character Worksheet. 112 PROGRAMMING GRAPHICS ~ The Programmable Character Worksheet (Figure 3-1) will help you design your own characters. There is an 8 by 8 matrix on the sheet, with row numbers, and numbers at the top of each column. (if you view each row as a binary word, the numbers are the value of that bit position. Each is a power of 2. The leftmost bit is equal to 128 or 2 to the 7th power, the next is equal to 64 or 2 to the 6th, and so on, until you reach the rightmost bit (bit 0) which is equal to 1 or 2 to the 0 power.) Place an X on the matrix at every location where you want a dot to be in your character. When your character is ready you can create the DATA statement for your character. Begin with the first row. Wherever you placed an X, take the number at the top of the column (the power-of-2 number, as explained above) and write it down. When you have the numbers for every column of the first row, add them together. \Mite this number down, next to the row. This is the number that you will put into the DATA statement to draw this row. Do the same thing with all of the other rows (1-7). When you are finished you should have 8 numbers between 0 and 255. If any of your numbers are not within range, recheck your addition. The numbers must be in this range to be correct! If you have less than 8 numbers, you missed a row. It's OK if some are 0. The 0 rows are just as important as the other numbers. Replace the numbers in the DATA statement in line 20 with the numbers you just calculated, and RUN the program. Then type a T. Every time you type it, you'll see your own character! If you don't like the way the character turned out, just change the numbers in the DATA statement and re-RUN the program until you are happy with your character. That's all there is to it! +-----------------------------------------------------------------------+ | HINT: For best results, always make any vertical lines in your | | characters at least 2 dots (bits) wide. This helps prevent CHROMA | | noise (color distortion) on your characters when they are displayed | | on a TV screen. | +-----------------------------------------------------------------------+ PROGRAMMING GRAPHICS 113 ~ Here is an example of a program using standard programmable characters: start tok64 page114.prg 10 rem * example 1 * 20 rem creating programmable characters 31 poke 56334,peek(56334)and254: rem turn off kb 32 poke 1,peek(1)and251: rem turn off i/o 35 for i=0to63: rem character range to be copied 36 for j=0to7: rem copy all 8 bytes per character 37 poke 12288+I*8+j,peek(53248+i*8+j): rem copy a byte 38 next j:next i: rem goto next byte or character 39 poke 1,peek(1)or4:poke 56334,peek(56334)or1: rem turn on i/O and kb 40 poke 53272,(peek(53272)and240)+12: rem set char pointer to mem. 12288 60 for char=60to63: rem program characters 60 thru 63 80 for byte=0to7: rem do all 8 bytes of a character 100 read number: rem read in 1/8th of character data 120 poke 12288+(8*char)+byte,number: rem store the data in memory 140 next byte:next char: rem also could be next byte, char 150 print chr$(147)tab(255)chr$(60); 155 print chr$(61)tab(55)chr$(62)chr$(63) 160 rem line 150 puts the newly defined characters on the screen 170 get a$: rem wait for user to press a key 180 if a$=""then goto170: rem if no keys were pressed, try again! 190 poke 53272,21: rem return to normal characters 200 data 4,6,7,5,7,7,3,3: rem data for character 60 210 data 32,96,224,160,224,224,192,192: rem data for character 61 220 data 7,7,7,31,31,95,143,127: rem data for character 62 230 data 224,224,224,248,248,248,240,224: rem data for character 63 240 end stop tok64 114 PROGRAMMING GRAPHICS ~ MULTI-COLOR MODE GRAPHICS Standard high-resolution graphics give you control of very small dots on the screen. Each dot in character memory can have 2 possible values, 1 for on and 0 for off. When a dot is off, the color of the screen is used in the space reserved for that dot. If the dot is on, the dot is colored with the character color you have chosen for that screen posi- tion. When you're using standard high-resolution graphics, all the dots within each 8X8 character can either have background color or foreground color. In some ways this limits the color resolution within that space. For example, problems may occur when two different colored lines cross. Multi-color mode gives you a solution to this problem. Each dot in multi-color mode can be one of 4 colors: screen color (background color register #0), the color in background register #1, the color in back- ground color register #2, or character color. The only sacrifice is in the horizontal resolution, because each multi-color mode dot is twice as wide as a high-resolution dot. This minimal loss of resolution is more than compensated for by the extra abilities of multi-color mode. MULTI-COLOR MODE BIT To turn on multi-color character mode, set bit 4 of the VIC-II control register at 53270 ($D016) to a 1 by using the following POKE: POKE 53270,PEEK(53270)OR 16 To turn off multi-color character mode, set bit 4 of location 53270 to a 0 by the following POKE: POKE 53270,PEEK(53270)AND 239 Multi-color mode is set on or off for each space on the screen, so that multi-color graphics can be mixed with high-resolution (hi-res) graphics. This is controlled by bit 3 in color memory. Color memory begins at location 55296 ($D800 in HEX). If the number in color memory is less than 8 (0-7) the corresponding space on the video screen will be standard hi-res, in the color (0-7) you've chosen. If the number located in color memory is greater or equal to 8 (from 8 to 15), then that space will be displayed in multi-color mode. PROGRAMMING GRAPHICS 115 ~ By POKEing a number into color memory, you can change the color of the character in that position on the screen. POKEing a number from 0 to 7 gives the normal character colors. POKEing a number between 8 and 15 puts the space into multi-color mode. In other words, turning BIT 3 ON in color memory, sets MULTI-COLOR MODE. Turning BIT 3 OFF in color memory, sets the normal, HIGH-RESOLUTION mode. Once multi-color mode is set in a space, the bits in the character determine which colors are displayed for the dots. For example, here is a picture of the letter A, and its bit pattern: IMAGE BIT PATTERN ** 00011000 **** 00111100 ** ** 01100110 ****** 01111110 ** ** 01100110 ** ** 01100110 ** ** 01100110 00000000 In normal or high-resolution mode, the screen color is displayed everywhere there is a 0 bit, and the character color is displayed where the bit is a 1. Multi-color mode uses the bits in pairs, like so: IMAGE BIT PATTERN AABB 00011000 CCCC 00111100 AABBAABB 01100110 AACCCCBB 01111110 AABBAABB 01100110 AABBAABB 01100110 AABBAABB 01100110 00000000 In the image area above, the spaces marked AA are drawn in the background #1 color, the spaces marked BB use the background #2 color, and the spaces marked CC use the character color. The bit pairs determine this, according to the following chart: 116 PROGRAMMING GRAPHICS ~ +----------+--------------------------------------+---------------------+ | BIT PAIR | COLOR REGISTER | LOCATION | +----------+--------------------------------------+---------------------+ | 00 | Background #0 color (screen color) | 53281 ($D021) | | 01 | Background #l color | 53282 ($D022) | | 10 | Background #2 color | 53283 ($D023) | | 11 | Color specified by the | color RAM | | | lower 3 bits in color memory | | +----------+--------------------------------------+---------------------+ Type NEW and then type this demonstration program: start tok64 page117.prg 100 poke 53281,1: rem set background color #0 to white 110 poke 53282,3: rem set background color #1 to cyan 120 poke 53282,8: rem set background color #2 to orange 130 poke 53270,peek(53270)or16: rem turn on multicolor mode 140 c=13*4096+8*256: rem set c to point to color memory 150 printchr$(147)"aaaaaaaaaa" 160 forl=0to9 170 pokec+l,8: rem use multi black 180 next stop tok64 The screen color is white, the character color is black, one color register is cyan (greenish blue), the other is orange. You're not really putting color codes in the space for character color, you're actually using references to the registers associated with those colors. This conserves memory, since 2 bits can be used to pick 16 colors (background) or 8 colors (character). This also makes some neat tricks possible. Simply changing one of the indirect registers will change every dot drawn in that color. Therefore everything drawn in the screen and background PROGRAMMING GRAPHICS 117 ~ colors can be changed on the whole screen instantly. Here is an example of changing background color register #1: start tok64 page118.prg 100 poke53270,peek(53270)or16: rem turn on multicolor mode 110 print chr$(147)chr$(18); 120 print"{orange*2}";: rem type c= & 1 for orange or multicolor black bg 130 forl=1to22:printchr$(65);:next 135 fort=1to500:next 140 print"{blue*2}";: rem type ctrl & 7 for blue color change 145 fort=1to500:next 150 print"{black}hit a key" 160 get a$:if a$=""then160 170 x=int(rnd(1)*16) 180 poke 53282,x 190 goto 160 stop tok64 By using the key and the COLOR keys the characters can be changed to any color, including multi-color characters. For example, type this command: POKE 53270,PEEK(53270)OR 16:PRINT"";: rem lt.red/ multi-color red The word READY and anything else you type will be displayed in multi- color mode. Another color control can set you back to regular text. 118 PROGRAMMING GRAPHICS ~ Here is an example of a program using multi-color programmable characters: start tok64 page119.prg 10 rem * example 2 * 20 rem creating multi color programmable characters 31 poke 56334,peek(56334)and254:poke1,peek(1)and251 35 fori=0to63:rem character range to be copied from rom 36 forj=0to7:rem copy all 8 bytes per character 37 poke 12288+i*8+j,peek(53248+i*8+j):rem copy a byte 38 next j,i:rem goto next byte or character 39 poke 1,peek(1)or4:poke 56334,peek(56334)or1:rem turn on i/o and kb 40 poke 53272,(peek(53272)and240)+12:rem set char pointer to mem. 12288 50 poke 53270,peek(53270)or16 51 poke 53281,0:rem set background color #0 to black 52 poke 53282,2:rem set background color #1 to red 53 poke 53283,7:rem set background color #2 to yellow 60 for char=60to63:rem program characters 60 thru 63 80 for byte=0to7:rem do all 8 bytes of a character 100 read number:rem read 1/8th of the character data 120 poke 12288+(8*char)+byte,number:rem store the data in memory 140 next byte,char 150 print"{clear}"tab(255)chr$(60)chr$(61)tab(55)chr$(62)chr$(63) 160 rem line 150 puts the newly defined characters on the screen 170 get a$:rem wait for user to press a key 180 if a$=""then170:rem if no keys were pressed, try again 190 poke53272,21:poke53270,peek(53270)and239:rem return to normal chars 200 data129,37,21,29,93,85,85,85: rem data for character 60 210 data66,72,84,116,117,85,85,85: rem data for character 61 220 data87,87,85,21,8,8,40,0: rem data for character 62 230 data213,213,85,84,32,32,40,0: rem data for character 63 240 end stop tok64 PROGRAMMING GRAPHICS 119 ~ EXTENDED BACKGROUND COLOR MODE Extended background color mode gives you control over the background color of each individual character, as well as over the foreground color. For example, in this mode you could display a blue character with a yellow background on a white screen. There are 4 registers available for extended background color mode. Each of the registers can be set to any of the 16 colors. Color memory is used to hold the foreground color in extended back- ground mode. It is used the same as in standard character mode. Extended character mode places a limit on the number of different characters you can display, however. When extended color mode is on, only the first 64 characters in the character ROM (or the first 64 characters in your programmable character set) can be used. This is because two of the bits of the character code are used to select the background color. It might work something like this: The character code (the number you would POKE to the screen) of the letter "A" is a 1. When extended color mode is on, if you POKED a 1 to the screen, an "A" would appear. If you POKED a 65 to the screen normally, you would expect the character with character code (CHR$) 129 to appear, which is a reversed "A." This does NOT happen in extended color mode. Instead you get the same unreversed "A" as before, but on a different background color. The following chart gives the codes: +------------------------+---------------------------+ | CHARACTER CODE | BACKGROUND COLOR REGISTER | +------------------------+---------------------------+ | RANGE BIT 7 BIT 6 | NUMBER ADDRESS | +------------------------+---------------------------+ | 0- 63 0 0 | 0 53281 ($D021) | | 64-127 0 1 | 1 53282 ($D022) | | 128-191 1 0 | 2 53283 ($D023) | | 192-255 1 1 | 3 53284 ($D024) | +------------------------+---------------------------+ Extended color mode is turned ON by setting bit 6 of the VIC-II regis- ter to a 1 at location 53265 ($D011 in HEX). The following POKE does it: POKE 53265,PEEK(53265)OR 64 120 PROGRAMMING GRAPHICS ~ Extended color mode is turned OFF by setting bit 6 of the VIC-II regis- ter to a 0 at location 53265 ($D011). The following statement will do this: POKE 53265,PEEK(53265)AND 191 BIT MAPPED GRAPHICS When writing games, plotting charts for business applications, or other types of programs, sooner or later you get to the point where you want high-resolution displays. The Commodore 64 has been designed to do just that: high resolution is available through bit mapping of the screen. Bit mapping is the method in which each possible dot (pixel) of resolution on the screen is assigned its own bit (location) in memory. If that memory bit is a one, the dot it is assigned to is on. If the bit is set to zero, the dot is off. High-resolution graphic design has a couple of drawbacks, which is why it is not used all the time. First of all, it takes lots of memory to bit map the entire screen. This is because every pixel must have a memory bit to control it. You are going to need one bit of memory for each pixel (or one byte for 8 pixels). Since each character is 8 by 8, and there are 40 lines with 25 characters in each line, the resolution is 320 pixels (dots) by 200 pixels for the whole screen. That gives you 64000 separate dots, each of which requires a bit in memory. In other words, 8000 bytes of memory are needed to map the whole screen. Generally, high-resolution operations are made of many short, simple, repetitive routines. Unfortunately, this kind of thing is usually rather slow if you are trying to write high-resolution routines in BASIC. How- ever, short, simple, repetitive routines are exactly what machine lan- guage does best. The solution is to either write your programs entirely in machine language, or call machine language, high-resolution sub- routines from your BASIC program using the SYS command from BASIC. That way you get both the ease of writing in BASIC, and the speed of machine language for graphics. The VSP cartridge is also available to add high- resolution commands to COMMODORE 64 BASIC. All of the examples given in this section will be in BASIC to make them clear. Now to the technical details. BIT MAPPING is one of the most popular graphics techniques in the computer world. It is used to create highly detailed pictures. Basically, when the Commodore 64 goes into bit map mode, it directly displays an PROGRAMMING GRAPHICS 121 ~ 8K section of memory on the TV screen. When in bit map mode, you can directly control whether an individual dot on the screen is on or off. There are two types of bit mapping available on the Commodore 64. They are: 1) Standard (high-resolution) bit mapped mode (320-dot by 200-dot resolution) 2) Multi-color bit mapped mode (160-dot by 200-dot resolution) Each is very similar to the character type it is named for: standard has greater resolution, but fewer color selections. On the other hand, multi-color bit mapping trades horizontal resolution for a greater number of colors in an 8-dot by 8-dot square. STANDARD HIGH-RESOLUTION BIT MAP MODE Standard bit map mode gives you a 320 horizontal dot by 200 vertical dot resolution, with a choice of 2 colors in each 8-dot by 8-dot section. Bit map mode is selected (turned ON) by setting bit 5 of the VIC-II control register to a 1 at location 53265 ($D011 in HEX). The following POKE will do this: POKE 53265,PEEK(53265)OR 32 Bit map mode is turned OFF by setting bit 5 of the VIC-II control register to 0 at location 53265 ($D011), like this: POKE 53265,PEEK(53265)AND 223 Before we get into the details of the bit map mode, there is one more issue to tackle, and that is where to locate the bit map area. HOW IT WORKS If you remember the PROGRAMMABLE CHARACTERS section you will recall that you were able to set the bit pattern of a character stored in RAM to almost anything you wanted. If at the same time you change the character that is displayed on the screen, you would be able to change a single dot, and watch it happen. This is the basis of bit-mapping. The entire 122 PROGRAMMING GRAPHICS ~ screen is filled with programmable characters, and you make your changes directly into the memory that the programmable characters get their patterns from. Each of the locations in screen memory that were used to control what character was displayed, are now used for color information. For example, instead of POKEing a I in location 1024 to make an "A" appear in the top left hand corner of the screen, location 1024 now controls the colors of the bits in that top left space. Colors of squares in bit map mode do not come from color memory, as they do in the character modes. Instead, colors are taken from screen memory. The upper 4 bits of screen memory become the color of any bit that is set to 1 in the 8 by 8 area controlled by that screen memory location. The lower 4 bits become the color of any bit that is set to a 0. EXAMPLE: Type the following: 5 BASE=2*4096:POKE53272,PEEK(53272)OR8:REM PUT BIT MAP AT 8192 10 POKE53265,PEEK(53265)OR32:REM ENTER BIT MAP MODE Now RUN the program. Garbage appears on the screen, right? Just like the normal screen mode, you have to clear the HIGH-RESOLUTION (HI-RES) screen before you use it. Unfortunately, printing a CLR won't work in this case. Instead you have to clear out the section of memory that you're using for your programmable characters. Hit the and keys, then add the following lines to your program to clear the HI-RES screen: 20 FORI=BASETOBASE+7999:POKEI,0:NEXT:REM CLEAR BIT 30 FORI=1024TO2023:POKEI,3:NEXT:REM SET COLOR TO CYAN AND BLACK Now RUN the program again. You should see the screen clearing, then the greenish blue color, cyan, should cover the whole screen. What we want to do now is to turn the dots on and off on the HI-RES screen. PROGRAMMING GRAPHICS 123 ~ To SET a dot (turn a dot ON) or UNSET a dot (turn a dot OFF) you must know how to find the correct bit in the character memory that you have to set to a 1. In other words, you have to find the character you need to change, the row of the character, and which bit of the row that you have to change. You need a formula to calculate this. We will use X and Y to stand for the horizontal and vertical positions of a dot, The dot where X=0 and Y=0 is at the upper-left of the display. Dots to the right have higher X values, and the dots toward the bottom have higher Y values. The best way to use bit mapping is to arrange the bit map display something like this: 0. . . . . . . . . . . . . . . . . .X. . . . . . . . . . . . . . . . .319 . . . . . . . . Y . . . . . . . . . 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Each dot will have an X and a Y coordinate. With this format it is easy to control any dot on the screen. 124 PROGRAMMING GRAPHICS ~ However, what you actually have is something like this: ----- BYTE 0 BYTE 8 BYTE 16 BYTE 24 ..................... BYTE 312 BYTE 1 BYTE 9 . . BYTE 313 BYTE 2 BYTE 10 . . BYTE 314 BYTE 3 BYTE 11 . . BYTE 315 BYTE 4 BYTE 12 . . BYTE 316 BYTE 5 BYTE 13 . . BYTE 317 BYTE 6 BYTE 14 . . BYTE 318 ----- BYTE 7 BYTE 15 . . BYTE 319 ----- BYTE 320 BYTE 328 BYTE 336 BYTE 344....................... BYTE 632 BYTE 321 BYTE 329 . . BYTE 633 BYTE 322 BYTE 330 . . BYTE 634 BYTE 323 BYTE 331 . . BYTE 635 BYTE 324 BYTE 332 . . BYTE 636 BYTE 325 BYTE 333 . . BYTE 637 BYTE 326 BYTE 334 . . BYTE 638 ----- BYTE 327 BYTE 335 . . BYTE 639 The programmable characters which make up the bit map are arranged in 25 rows of 40 columns each. While this is a good method of organization for text, it makes bit mapping somewhat difficult. (There is a good reason for this method. See the section on MIXED MODES.) The following formula will make it easier to control a dot on the bit map screen: The start of the display memory area is known as the BASE, The row number (from 0 to 24) of your dot is: ROW = INT(Y/8) (There are 320 bytes per line.) The character position on that line (from 0 to 39) is: CHAR = INT(X/8) (There are 8 bytes per character.) The line of that character position (from 0 to 7) is: LINE = Y AND 7 PROGRAMMING GRAPHICS 125 ~ The bit of that byte is: BIT = 7-(X AND 7) Now we put these formulas together. The byte in which character memory dot (X,Y) is located is calculated by: BYTE = BASE + ROW*320+ CHAR*8 + LINE To turn on any bit on the grid with coordinates (X,Y), use this line: POKE BYTE, PEEK(BYTE) OR 2^BIT Let's add these calculations to the program. In the following example, the COMMODORE 64 will plot a sine curve: 50 FORX=0TO319STEP.5:REM WAVE WILL FILL THE SCREEN 60 Y=INT(90+80*SIN(X/10)) 70 CH=INT(X/8) 80 RO=INT(Y/8) 85 LN=YAND7 90 BY=BASE+RO*320+8*CH+LN 100 BI=7-(XAND7) 110 POKEBY,PEEK(BY)OR(2^BI) 120 NEXTX 125 POKE1024,16 130 GOTO130 The calculation in line 60 will change the values for the sine function from a range of +1 to -1 to a range of 10 to 170. Lines 70 to 100 calculate the character, row, byte, and bit being affected, using the formulae as shown above. Line 125 signals the program is finished by changing the color of the top left corner of the screen. Line 130 freezes the program by putting it into an infinite loop. When you have finished looking at the display, just hold down and hit . 126 PROGRAMMING GRAPHICS ~ As a further example, you can modify the sine curve program to display a semicircle. Here are the lines to type to make the changes: 50 FORX=0TO160:REM DO HALF THE SCREEN 55 Y1=100+SQR(160*X-X*X) 56 Y2=100-SQR(160*X-X*X) 60 FORY=Y1TOY2STEPY1-Y2 70 CH=INT(X/() 80 RO=INT(Y/X) 85 LNYAND7 90 BY=BASE+RO*320+8*CH+LN 100 BI=7-(XAND7) 110 POKEBY,PEEK(BY)OR(2^BI) 114 NEXT This will create a semicircle in the HI-RES area of the screen. +-----------------------------------------------------------------------+ | WARNING: BASIC variables can overlay your high-resolution screen. If | | you need more memory space you must move the bottom of BASIC above the| | high-resolution screen area. Or, you must move your high-resolution | | screen area. This problem will NOT occur in machine language. It ONLY | | happens when you're writing programs in BASIC. | +-----------------------------------------------------------------------+ MULTI-COLOR BET MAP MODE Like multi-color mode characters, multi-color bit map mode allows you to display up to four different colors in each 8 by 8 section of bit map. And as in multi-character mode, there is a sacrifice of horizontal resolution (from 320 dots to 160 dots). Multi-color bit map mode uses an 8K section of memory for the bit map. You select your colors for multi-color bit map mode from (1) the background color register 0, (the screen background color), (2) the video matrix (the upper 4 bits give one possible color, the lower 4 bits an- other), and (3) color memory. Multi-color bit mapped mode is turned ON by setting bit 5 of 53265 ($D011) and bit 4 at location 53270 ($D016) to a 1. The following POKE does this: POKE 53265,PEEK(53625)OR 32: POKE 53270,PEEK(53270)OR 16 PROGRAMMING GRAPHICS 127 ~ Multi-color bit mapped mode is turned OFF by setting bit 5 of 53265 ($D011) and bit 4 at location 53270 ($D016) to a 0. The following POKE does this: POKE 53265,PEEK(53265)AND 223: POKE 53270,PEEK(53270)AND 239 As in standard (HI-RES) bit mapped mode, there is a one to one cor- respondence between the 8K section of memory being used for the display, and what is shown on the screen. However, the horizontal dots are two bits wide. Each 2 bits in the display memory area form a dot, which can have one of 4 colors. BITS COLOR INFORMATION COMES FROM 00 Background color #0 (screen color) 01 Upper 4 bits of screen memory 10 Lower 4 bits of screen memory 11 Color nybble (nybble = 1/2 byte = 4 bits) SMOOTH SCROLLING The VIC-II chip supports smooth scrolling in both the horizontal and vertical directions. Smooth scrolling is a one pixel movement of the entire screen in one direction. It can move either UP, or down, or left, or right. It is used to move new information smoothly onto the screen, while smoothly removing characters from the other side. While the VIC-II chip does much of the task for you, the actual scroll- ing must be done by a machine language program. The VIC-II chip features the ability to place the video screen in any of 8 horizontal positions, and 8 vertical positions. Positioning is controlled by the VIC-II scrolling registers. The VIC-II chip also has a 38 column mode, and a 24 row mode. the smaller screen sizes are used to give you a place for your new data to scroll on from. The following are the steps for SMOOTH SCROLLING: 128 PROGRAMMING GRAPHICS ~ 1) Shrink the screen (the border will expand). 2) Set the scrolling register to maximum (or minimum value depending upon the direction of your scroll). 3) Place the new data on the proper (covered) portion of the screen. 4) Increment (or decrement) the scrolling register until it reaches the maximum (or minimum) value. 5) At this point, use your machine language routine to shift the entire screen one entire character in the direction of the scroll. 6) Go back to step 2. To go into 38 column mode, bit 3 of location 53270 ($D016) must be set to a 0. The following POKE does this: POKE 53270,PEEK(53270)AND 247 To return to 40 column mode, set bit 3 of location 53270 ($D016) to a 1.The following POKE does this: POKE 53270,PEEK(53270)OR 8 To go into 24 row mode, bit 3 of location 53265 ($D011) must be set to a 0. The following POKE will do this: POKE 53265,PEEK(53265)AND 247 To return to 25 row mode, set bit 3 of location 53265 ($D011) to a 1. The following POKE does this: POKE 53265,PEEK(53265)OR 8 When scrolling in the X direction, it is necessary to place the VIC-II chip into 38 column mode. This gives new data a place to scroll from. When scrolling LEFT, the new data should be placed on the right. When scrolling RIGHT the new data should be placed on the left. Please note that there are still 40 columns to screen memory, but only 38 are visible. When scrolling in the Y direction, it is necessary to place the VIC-II chip into 24 row mode. When scrolling UP, place the new data in the LAST row. When scrolling DOWN, place the new data on the FIRST row. Unlike X scrolling, where there are covered areas on each side of the screen, there is only one covered area in Y scrolling. When the Y scrolling PROGRAMMING GRAPHICS 129 ~ register is set to 0, the first line is covered, ready for new data. When the Y scrolling register is set to 7 the last row is covered. For scrolling in the X direction, the scroll register is located in bits 2 to 0 of the VIC-II control register at location 53270 ($D016 in HEX). As always, it is important to affect only those bits. The following POKE does this: POKE 53270,(PEEK(53270)AND 248)+X where X is the X position of the screen from 0 to 7. For scrolling in the Y direction, the scroll register is located in bits 2 to 0 of the VIC-II control register at location 53265 ($D011 in HEX). As always, it is important to affect only those bits. The following POKE does this: POKE 53265,(PEEK(53265)AND 248)+Y where Y is the Y position of the screen from 0 to 7. To scroll text onto the screen from the bottom, you would step the low- order 3 bits of location 53265 from 0-7, put more data on the covered line at the bottom of the screen, and then repeat the process. To scroll characters onto the screen from left to right, you would step the low- order 3 bits of location 53270 from 0 to 7, print or POKE another column of new data into column 0 of the screen, then repeat the process. If you step the scroll bits by -1, your text will move in the opposite direction. EXAMPLE: Text scrolling onto the bottom of the screen: start tok64 page130.prg 10 poke53265,peek(53265)and247 :rem go into 24 row mode 20 printchr$(147) :rem clear the screen 30 forx=1to24:printchr$(17);:next :rem move the cursor to the bottom 40 poke53265,(peek(53265)and248)+7:print :rem position for 1st scroll 50 print" hello"; 60 forp=6to0step-1 70 poke53265,(peek(53265)and248)+p 80 forx=1to50:next :rem delay loop 90 next:goto40 stop tok64 130 PROGRAMMING GRAPHICS ~ SPRITES A SPRITE is a special type of user definable character which can be displayed anywhere on the screen. Sprites are maintained directly by the VIC-II chip. And all you have to do is tell a sprite "what to look like," "what color to be," and "where to appear." The VIC-II chip will do the rest! Sprites can be any of the 16 colors available. Sprites can be used with ANY of the other graphics modes, bit mapped, character, multi-color, etc., and they'll keep their shape in all of them. The sprite carries its own color definition, its own mode (HI-RES or multi-colored), and its own shape. Up to 8 sprites at a time can be maintained by the VIC-II chip auto- matically. More sprites can be displayed using RASTER INTERRUPT techniques. The features of SPRITES include: 1) 24 horizontal dot by 21 vertical dot size. 2) Individual color control for each sprite. 3) Sprite multi-color mode. 4) Magnification (2x) in horizontal, vertical, or both directions. 5) Selectable sprite to background priority. 6) Fixed sprite to sprite priorities. 7) Sprite to sprite collision detection. 8) Sprite to background collision detection. These special sprite abilities make it simple to program many arcade style games. Because the sprites are maintained by hardware, it is even possible to write a good quality game in BASIC! There are 8 sprites supported directly by the VIC-II chip. They are numbered from 0 to 7. Each of the sprites has it own definition location, position registers and color register, and has its own bits for enable and collision detection. DEFINING A SPRITE Sprites are defined like programmable characters are defined. However, since the size of the sprite is larger, more bytes are needed. A sprite is 24 by 21 dots, or 504 dots. This works out to 63 bytes (504/8 bits) PROGRAMMING GRAPHICS 131 ~ [THE PICTURE IS MISSING!] Figure 3-2. Sprite Definition Block. 132 PROGRAMMING GRAPHICS ~ needed to define a sprite. The 63 bytes are arranged in 21 rows of 3 bytes each. A sprite definition looks like this. BYTE 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 BYTE 5 BYTE 6 BYTE 7 BYTE 8 .. .. .. .. .. .. .. .. .. BYTE 60 BYTE 61 BYTE 62 Another way to view how a sprite is created is to take a look at the sprite definition block on the bit level. It would look something like Figure 3-2. In a standard (HI-RES) sprite, each bit set to I is displayed in that sprite's foreground color. Each bit set to 0 is transparent and will display whatever data is behind it. This is similar to a standard character. Multi-color sprites are similar to multi-color characters. Horizontal resolution is traded for extra color resolution. The resolution of the sprite becomes 12 horizontal dots by 21 vertical dots. Each dot in the sprite becomes twice as wide, but the number of colors displayable in the sprite is increased to 4. SPRITE POINTERS Even though each sprite takes only 63 bytes to define, one more byte is needed as a place holder at the end of each sprite. Each sprite, then, takes up 64 bytes. This makes it easy to calculate where in memory your sprite definition is, since 64 bytes is an even number and in binary it's an even power. Each of the 8 sprites has a byte associated with it called the SPRITE POINTER. The sprite pointers control where each sprite definition is lo- cated in memory. These 8 bytes are always located as the lost 8 bytes of the 1K chunk of screen memory. Normally, on the Commodore 64, this means they begin at location 2040 ($07F8 in HEX). However, if you move the screen, the location of your sprite pointers will also move. Each sprite pointer can hold a number from 0 to 255. This number points to the definition for that sprite. Since each sprite definition takes 64 bytes, that means that the pointer can "see" anywhere in the 16K block of memory that the VIC-II chip can access (since 256*64=16K). PROGRAMMING GRAPHICS 133 ~ If sprite pointer #0, at location 2040, contains the number 14, for example, this means that sprite 0 will be displayed using the 64 bytes beginning at location 14*64 = 896 which is in the cassette buffer. The following formula makes this clear: LOCATION = (BANK * 16384) + (SPRITE POINTER VALUE * 64) Where BANK is the 16K segment of memory that the VIC-II chip is looking at and is from 0 to 3. The above formula gives the start of the 64 bytes of the sprite definition block. When the VIC-II chip is looking at BANK 0 or BANK 2, there is a ROM IMAGE of the character set present in certain locations, as mentioned before. Sprite definitions can NOT be placed there. If for some reason you need more than 128 different sprite definitions, you should use one of the banks without the ROM IMAGE, 1 or 3. TURNING SPRITES ON The VIC-II control register at location 53269 ($D015 in HEX) is known as the SPRITE ENABLE register. Each of the sprites has a bit in this register which controls whether that sprite is ON or OFF. The register looks like this: $D015 7 6 5 4 3 2 1 0 To turn on sprite 1, for example, it is necessary to turn that bit to a 1. The following POKE does this: POKE 53269.PEEK(53269)OR 2 A more general statement would be the following: POKE 53269,PEEK(53269)OR (2^SN) where SN is the sprite number, from 0 to 7. +-----------------------------------------------------------------------+ | NOTE: A sprite must be turned ON before it can be seen. | +-----------------------------------------------------------------------+ 134 PROGRAMMING GRAPHICS ~ TURNING SPRITES OFF A sprite is turned off by setting its bit in the VIC-II control register at 53269 ($D015 in HEX) to a 0. The following POKE will do this: POKE 53269,PEEK(53269)AND(255-2^SN) where SN is the sprite number from 0 to 7. COLORS A sprite can be any of the 16 colors generated by the VIC-II chip. Each of the sprites has its own sprite color register. These are the memory locations of the color registers: ADDRESS | DESCRIPTION --------------------------+---------------------------------------------- 53287 ($D027) | SPRITE 0 COLOR REGISTER 53288 ($D028) | SPRITE 1 COLOR REGISTER 53289 ($D029) | SPRITE 2 COLOR REGISTER 53290 ($D02A) | SPRITE 3 COLOR REGISTER 53291 ($D02B) | SPRITE 4 COLOR REGISTER 53292 ($D02C) | SPRITE 5 COLOR REGISTER 53293 ($D02D) | SPRITE 6 COLOR REGISTER 53294 ($D02E) | SPRITE 7 COLOR REGISTER All dots in the sprite will be displayed in the color contained in the sprite color register. The rest of the sprite will be transparent, and will show whatever is behind the sprite. MULTI-COLOR MODE Multi-color mode allows you to have up to 4 different colors in each sprite. However, just like other multi-color modes, horizontal resolution is cut in half. In other words, when you're working with sprite multi- color mode (like in multi-color character mode), instead of 24 dots across the sprite, there are 12 pairs of dots. Each pair of dots is called a BIT PAIR. Think of each bit pair (pair of dots) as a single dot in your overall sprite when it comes to choosing colors for the dots in your sprites. The table below gives you the bit pair values needed to PROGRAMMING GRAPHICS 135 ~ turn ON each of the four colors you've chosen for your sprite: BIT PAIR DESCRIPTION ------------------------------------------------------------------------- 00 TRANSPARENT, SCREEN COLOR 01 SPRITE MULTI-COLOR REGISTER #0 (53285) ($D025) 10 SPRITE COLOR REGISTER 11 SPRITE MULTI-COLOR REGISTER #I (53286) ($D026) +-----------------------------------------------------------------------+ | NOTE: The sprite foreground color is a 10. The character foreground | | is a 11. | +-----------------------------------------------------------------------+ SETTING A SPRITE TO MULTI-COLOR MODE To switch a sprite into multi-color mode you must turn ON the VIC-II control register at location 53276 ($D01C). The following POKE does this: POKE 53276,PEEK(53276)OR(2^SN) where SN is the sprite number (0 to 7). To switch a sprite out of multi-color mode you must turn OFF the VIC-II control register at location 53276 ($D01C). The following POKE does this: POKE 53276,PEEK(53276)AND(255-2^SN) where SN is the sprite number (0 to 7). EXPANDED SPRITES The VIC-II chip has the ability to expand a sprite in the vertical direction, the horizontal direction, or both at once. When expanded, each dot in the sprite is twice as wide or twice as tall. Resolution doesn't actually increase... the sprite just gets bigger. To expand a sprite in the horizontal direction, the corresponding bit in the VIC-II control register at location 53277 ($D01D in HEX) must be turned ON (set to a 1). The following POKE expands a sprite in the X direction: POKE 53277,PEEK(53277)OR(2^SN) where SN is the sprite number from 0 to 7. 136 PROGRAMMING GRAPHICS ~ To unexpand a sprite in the horizontal direction, the corresponding bit in the VIC-II control register at location 53277 ($D01D in HEX) must be turned OFF (set to a 0). The following POKE "unexpands" a sprite in the X direction: POKE 53277,PEEK(53277)AND (255-2^SN) where SN is the sprite number from 0 to 7. To expand a sprite in the vertical direction, the corresponding bit in the VIC-II control register at location 53271 ($D017 in HEX) must be turned ON (set to a 1). The following POKE expands a sprite in the Y direction: POKE 53271,PEEK(53271)OR(2^SN) where SN is the sprite number from 0 to 7. To unexpand a sprite in the vertical direction, the corresponding bit in the VIC-II control register at location 53271 ($D017 in HEX) must be turned OFF (set to a 0). The following POKE "unexpands" a sprite in the Y direction: POKE 53271,PEEK(53271)AND (255-2^SN) where SN is the sprite number from 0 to 7. SPRITE POSITIONING Once you've made a sprite you want to be able to move it around the screen. To do this, your Commodore 64 uses three positioning registers: 1) SPRITE X POSITION REGISTER 2) SPRITE Y POSITION REGISTER 3) MOST SIGNIFICANT BIT X POSITION REGISTER Each sprite has an X position register, a Y position register, and a bit in the X most significant bit register. This lets you position your sprites very accurately. You can place your sprite in 512 possible X positions and 256 possible Y positions. The X and Y position registers work together, in pairs, as a team. The locations of the X and Y registers appear in the memory map as follows: First is the X register for sprite 0, then the Y register for sprite 0. PROGRAMMING GRAPHICS 137 ~ Next comes the X register for sprite 1, the Y register for sprite 1, and so on. After all 16 X and Y registers comes the most significant bit in the X position (X MSB) located in its own register. The chart below lists the locations of each sprite position register. You use the locations at their appropriate time through POKE statements: +-------------------+---------------------------------------------------+ | LOCATION | | +---------+---------+ DESCRIPTION | | DECIMAL | HEX | | +---------+---------+---------------------------------------------------+ | 53248 | ($D000) | SPRITE 0 X POSITION REGISTER | | 53249 | ($D001) | SPRITE 0 Y POSITION REGISTER | | 53250 | ($D002) | SPRITE 1 X POSITION REGISTER | | 53251 | ($D003) | SPRITE 1 Y POSITION REGISTER | | 53252 | ($D004) | SPRITE 2 X POSITION REGISTER | | 53253 | ($D005) | SPRITE 2 Y POSITION REGISTER | | 53254 | ($D006) | SPRITE 3 X POSITION REGISTER | | 53255 | ($D007) | SPRITE 3 Y POSITION REGISTER | | 53256 | ($D008) | SPRITE 4 X POSITION REGISTER | | 53257 | ($D009) | SPRITE 4 Y POSITION REGISTER | | 53258 | ($D00A) | SPRITE 5 X POSITION REGISTER | | 53259 | ($D00B) | SPRITE 5 Y POSITION REGISTER | | 53260 | ($D00C) | SPRITE 6 X POSITION REGISTER | | 53261 | ($D00D) | SPRITE 6 Y POSITION REGISTER | | 53262 | ($D00E) | SPRITE 7 X POSITION REGISTER | | 53263 | ($D00F) | SPRITE 7 Y POSITION REGISTER | | 53264 | ($D010) | SPRITE X MSB REGISTER | +---------+---------+---------------------------------------------------+ The position of a sprite is calculated from the TOP LEFT corner of the 24 dot by 21 dot area that your sprite can be designed in. It does NOT matter how many or how few dots you use to make up a sprite. Even if only one dot is used as a sprite, and you happen to want it in the middle of the screen, you must still calculate the exact positioning by starting at the top left corner location. VERTICAL POSITIONING Setting up positions in the horizontal direction is a little more difficult than vertical positioning, so we'll discuss vertical (Y) positioning first. 138 PROGRAMMING GRAPHICS ~ There are 200 different dot positions that can be individually pro- grammed onto your TV screen in the Y direction. The sprite Y position registers can handle numbers up to 255. This means that you have more than enough register locations to handle moving a sprite up and down. You also want to be able to smoothly move a sprite on and off the screen. More than 200 values are needed for this. The first on-screen value from the top of the screen, and in the Y direction for an unexpanded sprite is 30. For a sprite expanded in the Y direction it would be 9. (Since each dot is twice as tall, this makes a certain amount of sense, as the initial position is STILL calculated from the top left corner of the sprite.) The first Y value in which a sprite (expanded or not) is fully on the screen (all 21 possible lines displayed) is 50. The last Y value in which an unexpanded sprite is fully on the screen is 229. The last Y value in which an expanded sprite is fully on the screen is 208. The first Y value in which a sprite is fully off the screen is 250. EXAMPLE: start tok64 page139.prg 10 print"{clear}" :rem clear screen 20 poke 2040,13 :rem get sprite 0 data from block 13 30 fori=0to62:poke832+i,129:next :rem poke sprite data into block 13 40 v=53248 :rem set beginning of video chip 50 pokev+21,1 :rem enable sprite 0 60 pokev+39,1 :rem set sprite 0 color 70 pokev+1,100 :rem set sprite 0 y position 80 pokev+16,0:pokev,100 :rem set sprite 0 x position stop tok64 HORIZONTAL POSITIONING Positioning in the horizontal direction is more complicated because there are more than, 256 positions. This means that an extra bit, or 9th bit is used to control the X position. By adding the extra bit when necessary a sprite now has 512 possible positions in the left/right, X, direction. This makes more possible combinations than can be seen on the visible part of the screen. Each sprite can have a position from 0 to 511. However, only those values between 24 and 343 are visible on the screen. If the X position of a sprite is greater than 255 (on the right side of the screen), the bit in the X MOST SIGNIFICANT BIT POSITION PROGRAMMING GRAPHICS 139 ~ 0 ($00) 24 ($18) 296 ($128) 344 ($158) | | | | | | +----+ 8 ($08) | | | 29 ($1D) | +--+ | | | | | | | | | | | 50 ($32) +--+-------------------------------------+----+----+ 50 ($32) | | | | | | | | | | | | +--+--+ | | | | | | | | +----+----+ | | | VISIBLE VIEWING AREA | | | | | | | | | | | | | | | | NTSC* | | 40 COLUMNS | | 25 ROWS | 208 ($D0) +----+----+ | | | | | | | | +--+--+ 299 ($E5) | | | | | | | | | | | | 250 ($FA) +----+----+----------------------------------+--+--+ 250 ($FA) | | | | | | | | | | | | +--+ | | | +----+ | | 488 ($1E8) | 320 ($140) 344 ($158) 24 ($18) *North American television transmission standards for your home TV. 140 PROGRAMMING GRAPHICS ~ 7 ($07) 31 ($1F) 287 ($11F) 335 ($14F) | | | | | | +----+ 12 ($0C) | | | 33 ($21) | +--+ | | | | | | | | | | | 54 ($36) +--+-------------------------------------+----+----+ 54 ($36) | | | | | | | | | | | | +--+--+ | | | | | | | | +----+----+ | | | VISIBLE VIEWING AREA | | | | | | | | | | | | | | | | NTSC* | | 38 COLUMNS | | 24 ROWS | 204 ($CC) +----+----+ | | | | | | | | +--+--+ 225 ($E1) | | | | | | | | | | | | 246 ($F6) +----+----+----------------------------------+--+--+ 246 ($F6) | | | | | | | | | | | | +--+ | | | +----+ | | 480 ($1E0) | 311 ($137) 335 ($14F) 31 ($1F) *North American television transmission standards for your home TV. PROGRAMMING GRAPHICS 141 ~ register must be set to a 1 (turned ON). If the X position of a sprite is less than 256 (on the left side of the screen), then the X MSB of that sprite must be 0 (turned OFF). Bits 0 to 7 of the X MSB register correspond to sprites 0 to 7, respectively. The following program moves a sprite across the screen: EXAMPLE: start tok64 p142_1.prg 10 print"{clear}" 20 poke2040,13 30 fori=0to62:poke832+i,129:next 40 v=53248 50 pokev+21,1 60 pokev+39,1 70 pokev+1,100 80 forj=0to347 90 hx=int(j/256):lx=j-256*hx 100 pokev,lx:pokev+16,hx:next stop tok64 When moving expanded sprites onto the left side of the screen in the X direction, you have to start the sprite OFF SCREEN on the RIGHT SIDE. This is because an expanded sprite is larger than the amount of space available on the left side of the screen. EXAMPLE: start tok64 p142_2.prg 10 print"{clear}" 20 poke2040,13 30 fori=0to62:poke832+i,129:next 40 v=53248 50 pokev+21,1 60 pokev+39,1:pokev+23,1:pokev+29,1 70 pokev+1,100 80 j=488 90 hx=int(j/256):lx=j-256*hx 100 pokev,lx:pokev+16,hx 110 j=j+1:ifj>511thenj=0 120 ifj>488orj<348goto90 stop tok64 142 PROGRAMMING GRAPHICS ~ The charts in Figure 3-3 explain sprite positioning. By using these values, you can position each sprite anywhere. By moving the sprite a single dot position at a time, very smooth movement is easy to achieve. SPRITE POSITIONING SUMMARY Unexpanded sprites are at least partially visible in the 40 column, by 25 row mode within the following parameters: 1 < X < 343 30 < Y < 249 In the 38 column mode, the X parameters change to she following: 8 <= X <= 334 In the 24 row mode, the Y parameters change to the following: 34 <= Y <= 245 Expanded sprites are at least partially visible in the 40 column, by 25 row mode within the following parameters: 489 >= X <= 343 9 >= Y <= 249 In the 38 column mode, the X parameters change to the following: 496 >= X <= 334 In the 24 row mode, the Y parameters change to the following: 13 <= Y <= 245 PROGRAMMING GRAPHICS 143 ~ SPRITE DISPLAY PRIORITIES Sprites have the ability to cross each other's paths, as well as cross in front of, or behind other objects on the screen. This can give you a truly three dimensional effect for games. Sprite to sprite priority is fixed. That means that sprite 0 has the highest priority, sprite 1 has the next priority, and so on, until we get to sprite 7, which has the lowest priority. In other words, if sprite 1 and sprite 6 are positioned so that they cross each other, sprite 1 will be in front of sprite 6. So when you're planning which sprites will appear to be in the fore- ground of the picture, they must be assigned lower sprite numbers than those sprites you want to put towards the back of the scene. Those sprites will be given higher sprite numbers, +-----------------------------------------------------------------------+ | NOTE: A "window" effect is possible. If a sprite with higher priority | | has "holes" in it (areas where the dots are not set to 1 and thus | | turned ON), the sprite with the lower priority will show through. This| | also happens with sprite and background data. | +-----------------------------------------------------------------------+ Sprite to background priority is controllable by the SPRITE-BACK- GROUND priority register located at 53275 ($D01B). Each sprite has a bit in this register. If that bit is 0, that sprite has a higher priority than the background on the screen. In other words, the sprite appears in front of background data. If that bit is a 1, that sprite has a lower priority than the background. Then the sprite appears behind the back- ground data. COLLISION DETECTS One of the more interesting aspects of the VIC-II chip is its collision detection abilities. Collisions can be detected between sprites, or be- tween sprites and background data. A collision occurs when a non-zero part of a sprite overlaps a non-zero portion of another sprite or char- acters on the screen. 144 PROGRAMMING GRAPHICS ~ SPRITE TO SPRITE COLLISIONS Sprite to sprite collisions are recognized by the computer, or flagged, in the sprite to sprite collision register at location 53278 ($D01E in HEX) in the VIC-II chip control register. Each sprite has a bit in this register. If that bit is a 1, then that sprite is involved in a collision. The bits in this register will remain set until read (PEEKed). Once read, the register is automatically cleared, so it is a good idea to save the value in a variable until you are finished with it. +-----------------------------------------------------------------------+ | NOTE: Collisions can take place even when the sprites are off screen. | +-----------------------------------------------------------------------+ SPRITE TO DATA COLLISIONS Sprite to data collisions are detected in the sprite to data collision register at location 53279 ($D01F in HEX) of the VIC-II chip control register. Each sprite has a bit in this register. If that bit is a 1 , then that sprite is involved in a collision. The bits in this register remain set until read (PEEKed). Once read, the register is automatically cleared, so it is a good idea to save the value in a variable until you are finished with it. +-----------------------------------------------------------------------+ | NOTE: MULTI-COLOR data 01 is considered transparent for collisions, | | even though it shows up on the screen. When setting up a background | | screen, it is a good idea to make everything that should not cause a | | collision 01 in multi-color mode. | +-----------------------------------------------------------------------+ PROGRAMMING GRAPHICS 145 ~ start tok64 page146.prg 10 rem sprite example 1... the hot air balloon 30 vic=13*4096:rem this is where the vic registers begin 35 pokevic+21,1:rem enable sprite 0 36 pokevic+33,14:rem set background color to light blue 37 pokevic+23,1:rem expand sprite 0 in y 38 pokevic+29,1:rem expand sprite 0 in x 40 poke2040,192:rem set sprite 0's pointer 180 pokevic+0,100:rem set sprite 0's x position 190 pokevic+1,100:rem set sprite 0's y position 220 pokevic+39,1:rem set sprite 0's color 250 fory=0to63:rem byte counter with sprite loop 300 reada:rem read in a byte 310 poke192*64+y,a:rem store the data in sprite area 320 nexty:rem close loop 330 dx=1:dy=1 340 x=peek(vic):rem look at sprite 0's x position 350 y=peek(vic+1):rem look at sprite 0's y position 360 ify=50ory=208thendy=-dy:rem if y is on the edge of the... 370 rem screen, then reverse delta y 380 ifx=24and(peek(vic+16)and1)=0thendx=-dx:rem if sprite is touching... 390 rem the left edge(x=24 and the msb for sprite 0 is 0), reverse it 400 ifx=40and(peek(vic+16)and1)=1thendx=-dx:rem if sprite is touching... 410 rem the right edge (x=40 and the msb for sprite 0 is 1), reverse it 420 ifx=255anddx=1thenx=-1:side=1 430 rem switch to other side of the screen 440 ifx=0anddx=-1thenx=256:side=0 450 rem switch to other side of the screen 460 x=x+dx:rem add delta x to x 470 x=xand255:rem make sure x is in allowed range 480 y=y+dy:rem add delta y to y 485 pokevic+16,side 490 pokevic,x:rem put new x value into sprite 0's x position 510 pokevic+1,y:rem put new y value into sprite 0's y position 530 goto340 600 rem ***** sprite data ***** 610 data0,127,0,1,255,192,3,255,224,3,231,224 620 data7,217,240,7,223,240,7,217,240,3,231,224 630 data3,255,224,3,255,224,2,255,160,1,127,64 640 data1,62,64,0,156,128,0,156,128,0,73,0,0,73,0 650 data0,62,0,0,62,0,0,62,0,0,28,0,0 stop tok64 146 PROGRAMMING GRAPHICS ~ start tok64 page147.prg 10 rem sprite example 2... 20 rem the hot air balloon again 30 vic=13*4096:rem this is where the vic registers begin 35 pokevic+21,63:rem enable sprites 0 thru 5 36 pokevic+33,14:rem set background color to light blue 37 pokevic+23,3:rem expand sprites 0 and 1 in y 38 pokevic+29,3:rem expand sprites 0 and 1 in x 40 poke2040,192:rem set sprite 0's pointer 50 poke2041,193:rem set sprite 1's pointer 60 poke2042,192:rem set sprite 2's pointer 70 poke2043,193:rem set sprite 3's pointer 80 poke2044,192:rem set sprite 4's pointer 90 poke2045,193:rem set sprite 5's pointer 100 pokevic+4,30:rem set sprite 2's x position 110 pokevic+5,58:rem set sprite 2's y position 120 pokevic+6,65:rem set sprite 3's x position 130 pokevic+7,58:rem set sprite 3's y position 140 pokevic+8,100:rem set sprite 4's x position 150 pokevic+9,58:rem set sprite 4's y position 160 pokevic+10,100:rem set sprite 5's x position 170 pokevic+11,58:rem set sprite 5's y position 175 print"{white}{clear}"tab(15)"this is two hires sprites"; 176 printtab(55)"on top of each other" 180 pokevic+0,100:rem set sprite 0's x position 190 pokevic+1,100:rem set sprite 0's y position 200 pokevic+2,100:rem set sprite 1's x position 210 pokevic+3,100:rem set sprite 1's y position 220 pokevic+39,1:rem set sprite 0's color 230 pokevic+41,1:rem set sprite 2's color 240 pokevic+43,1:rem set sprite 4's color 250 pokevic+40,6:rem set sprite 1's color 260 pokevic+42,6:rem set sprite 3's color 270 pokevic+44,6:rem set sprite 5's color 280 forx=192to193:rem the start of the loop that defines the sprites 290 fory=0to63:rem byte counter with sprite loop 300 reada:rem read in a byte 310 pokex*64+y,a:rem store the data in sprite area 320 nexty,x:rem close loops 330 dx=1:dy=1 340 x=peek(vic):rem look at sprite 0's x position 350 ify=50ory=208thendy=-dy:rem if y is on the edge of the... 370 rem screen, then reverse delta y 380 ifx=24and(peek(vic+16)and1)=0thendx=-dx:rem if sprite is... 390 rem touching the left edge, then reverse it 400 ifx=40and(peek(vic+16)and1)=1thendx=-dx:rem if sprite is... 410 rem touching the right edge, then reverse it 420 ifx=255anddx=1thenx=-1:side=3 430 rem switch to other side of the screen 440 ifx=0anddx=-1thenx=256:side=0 450 rem switch to other side of the screen 460 x=x+dx:rem add delta x to x 470 x=xand255:rem make sure x is in allowed range 480 y=y+dy:rem add delta y to y 485 pokevic+16,side 490 pokevic,x:rem put new x value into sprite 0's x position 500 pokevic+2,x:rem put new x value into sprite 1's x position 510 pokevic+1,y:rem put new y value into sprite 0's y position 520 pokevic+3,y:rem put new y value into sprite 1's y position 530 goto340 600 rem ***** sprite data ***** 610 data0,255,0,3,153,192,7,24,224,7,56,224,14,126,112,14,126,112,14,126 620 data112,6,126,96,7,56,224,7,56,224,1,56,128,0,153,0,0,90,0,0,56,0 630 data0,56,0,0,0,0,0,0,0,0,126,0,0,42,0,0,84,0,0,40,0,0 640 data0,0,0,0,102,0,0,231,0,0,195,0,1,129,128,1,129,128,1,129,128 650 data1,129,128,0,195,0,0,195,0,4,195,32,2,102,64,2,36,64,1,0,128 660 data1,0,128,0,153,0,0,153,0,0,0,0,0,84,0,0,42,0,0,20,0,0 stop tok64 start tok64 page148.prg 10 rem sprite example 3... 20 rem the hot air gorf 30 vic=53248:rem this is where the vic registers begin 35 pokevic+21,1:rem enable sprite 0 36 pokevic+33,14:rem set background color to light blue 37 pokevic+23,1:rem expand sprite 0 in y 38 pokevic+29,1:rem expand sprite 0 in x 40 poke2040,192:rem set sprite 0's pointer 50 pokevic+28,1:rem turn on multicolor 60 pokevic+37,7:rem set multicolor 0 70 pokevic+38,4:rem set multicolor 1 180 pokevic+0,100:rem set sprite 0's x position 190 pokevic+1,100:rem set sprite 0's y position 220 pokevic+39,2:rem set sprite 0's color 290 fory=0to63:rem byte counter with sprite loop 300 reada:rem read in a byte 310 poke12288+y,a:rem store the data in sprite area 320 next y:rem close loop 330 dx=1:dy=1 340 x=peek(vic):rem look at sprite 0's x position 350 y=peek(vic+1):rem look at sprite 0's y position 360 ify=50ory=208then dy=-dy:rem if y is on the edge of the... 370 rem screen, then reverse delta y 380 ifx=24and(peek(vic+16)and1)=0thendx=-dx:rem if sprite is... 390 rem touching the left edge, then reverse it 400 ifx=40and(peek(vic+16)and1)=1thendx=-dx:rem if sprite is... 410 rem touching the right edge, then reverse it 420 ifx=255anddx=1thenx=-1:side=1 430 rem switch to other side of the screen 440 ifx=0anddx=-1thenx=256:side=0 450 rem switch to other side of the screen 460 x=x+dx:rem add delta x to x 470 x=xand255:rem make sure that x is in allowed range 480 y=y+dy:rem add delta y to y 485 pokevic+16,side 490 pokevic,x:rem put new x value into sprite 0's x position 510 pokevic+1,y:rem put new y value into sprite 0's y position 520 geta$:rem get a key from the keyboard 521 ifa$="m"thenpokevic+28,1:rem user selected multicolor 522 ifa$="h"thenpokevic+28,0:rem user selected high resolution 530 goto340 600 rem ***** sprite data ***** 610 data64,0,1,16,170,4,6,170,144,10,170,160,42,170,168,41,105,104,169 620 data235,106,169,235,106,169,235,106,170,170,170,170,170,170,170,170 630 data170,170,170,170,166,170,154,169,85,106,170,85,170,42,170,168,10 640 data170,160,1,0,64,1,0,64,5,0,80,0 stop tok64 PROGRAMMING GRAPHICS 149 ~ OTHER GRAPHICS FEATURES SCREEN BLANKING Bit 4 of the VIC-II control register controls the screen blanking func- tion. It is found in the control register at location 53265 ($D011). When it is turned ON (in other words, set to a 1) the screen is normal. When bit 4 is set to 0 (turned OFF), the entire screen changes to border color. The following POKE blanks the screen. No data is lost, it just isn't displayed. POKE 53265,PEEK(53265)AND 239 To bring back the screen. use the POKE shown below: POKE 53265,PEEK(53265)OR 16 +-----------------------------------------------------------------------+ | NOTE: Turning off the screen will speed up the processor slightly. | | This means that program RUNning is also sped up. | +-----------------------------------------------------------------------+ RASTER REGISTER The raster register is found in the VIC-II chip at location 53266 ($D012). The raster register is a dual purpose register. When you read this register it returns the lower 8 bits of the current raster position. The raster position of the most significant bit is in register location 53265 ($D011). You use the raster register to set up timing changes in your display so that you can get rid of screen flicker. The changes on your screen should be mode when the raster is not in the visible display area, which is when your dot positions fall between 51 and 251. When the raster register is written to (including the MSB) the number written to is saved for use with the raster compare function. When the actual raster value becomes the same as the number written to the raster register, a bit in the VIC-II chip interrupt register 53273 ($D019) is turned ON by setting it to 1. +-----------------------------------------------------------------------+ | NOTE: If the proper interrupt bit is enabled (turned on), an interrupt| | (IRQ) will occur. | +-----------------------------------------------------------------------+ 150 PROGRAMMING GRAPHICS ~ INTERRUPT STATUS REGISTER The interrupt status register shows the current status of any interrupt source. The current status of bit 2 of the interrupt register will be a 1 when two sprites hit each other. The same is true, in a corresponding 1 to 1 relationship, for bits 0-3 listed in the chart below. Bit 7 is also set with a 1, whenever an interrupt occurs. The interrupt status register is located at 53273 ($D019) and is as follows: LATCH BIT# DESCRIPTION ------------------------------------------------------------------------- IRST 0 Set when current raster count = stored raster count IMDC 1 Set by SPRITE-DATA collision (1st one only, until reset) IMMC 2 Set by SPRITE-SPRITE collision (1st one only, until reset) ILP 3 Set by negative transition of light pen (1 per frame) IRQ 7 Set by latch set and enabled ------------------------------------------------------------------------- Once an interrupt bit has been set, it's "latched" in and must be cleared by writing a 1 to that bit in the interrupt register when you're ready to handle it. This allows selective interrupt handling, without having to store the other interrupt bits. The INTERRUPT ENABLE REGISTER is located at 53274 ($D01A). It has the same format as the interrupt status register. Unless the corresponding bit in the interrupt enable register is set to a 1, no interrupt from that source will take place. The interrupt status register can still be polled for information, but no interrupts will be generated. To enable an interrupt request the corresponding interrupt enable bit (as shown in the chart above) must be set to a 1. This powerful interrupt structure lets you use split screen modes. For instance you can have half of the screen bit mapped, half text, more than 8 sprites at a time, etc. The secret is to use interrupts properly. For example, if you want the top half of the screen to be bit mapped and the bottom to be text, just set the raster compare register (as explained previously) for halfway down the screen. When the interrupt occurs, tell the VIC-II chip to get characters from ROM, then set the raster compare register to interrupt at the top of the screen. When the interrupt occurs at the top of the screen, tell the VIC-II chip to get characters from RAM (bit map mode). You can also display more than 8 sprites in the same way. Unfortunately BASIC isn't fast enough to do this very well. So if you want to start using display interrupts, you should work in machine language. PROGRAMMING GRAPHICS 151 ~ SUGGESTED SCREEN AND CHARACTER COLOR COMBINATIONS Color TV sets are limited in their ability to place certain colors next to each other on the same line. Certain combinations of screen and char- acter colors produce blurred images. This chart shows which color com- binations to avoid, and which work especially well together. CHARACTER COLOR 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 0| x| o| x| o| o| /| x| o| o| x| o| o| o| o| o| o| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 1| o| x| o| x| o| o| o| x| /| o| /| o| o| x| o| o| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 2| x| o| x| x| /| x| x| o| o| x| o| x| x| x| x| /| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 3| o| x| x| x| x| /| o| x| x| x| x| /| x| x| /| x| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 4| o| /| x| x| x| x| x| x| x| x| x| x| x| x| x| /| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 5| o| /| x| /| x| x| x| x| x| x| x| /| x| o| x| /| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ SCREEN 6| /| o| x| o| x| x| x| x| x| x| x| x| x| /| o| o| COLOR +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 7| o| x| o| x| x| x| /| x| /| o| /| o| o| x| x| x| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 8| /| o| o| x| x| x| x| o| x| o| x| x| x| x| x| /| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 9| x| o| x| x| x| x| x| o| o| x| o| x| x| x| x| o| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 10| /| /| o| x| x| x| x| /| x| o| x| x| x| x| x| /| o = EXCELLENT +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 11| o| o| x| /| x| x| x| o| x| x| x| x| o| o| /| o| / = FAIR +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 12| o| o| /| x| x| x| /| x| x| /| x| o| x| x| x| o| x = POOR +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 13| o| x| x| x| x| o| /| x| x| x| x| o| x| x| x| x| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 14| o| o| x| o| x| x| o| x| x| x| x| /| x| x| x| /| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 15| o| o| o| x| /| /| o| x| x| /| /| o| o| x| /| x| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 152 PROGRAMMING GRAPHICS ~ PROGRAMMING SPRITES - ANOTHER LOOK For those of you having trouble with graphics, this section has been designed as a more elementary tutorial approach to sprites. MAKING SPRITES IN BASIC - A SHORT PROGRAM There are at least three different BASIC programming techniques which let you create graphic images and cartoon animations on the Commodore 64. You can use the computer's built-in graphics character set (see Page 376). You can program your own characters (see Page 108) or... best of all... you can use the computer's built-in "sprite graphics. To illustrate how easy it is, here's one of the shortest spritemaking programs you can write in BASIC: start tok64 page153.prg 10 print"{clear}" 20 poke2040,13 30 fors=832to832+62:pokes,255:next 40 v=53248 50 pokev+21,1 60 pokev+39,1 70 pokev,24 80 pokev+1,100 stop tok64 This program includes the key "ingredients" you need to create any sprite. The POKE numbers come from the SPRITEMAKING CHART on Page 176. This program defines the first sprite... sprite 0... as a solid white square on the screen. Here's a line-by-line explanation of the program: LINE 10 clears the screen. LINE 20 sets the "sprite pointer" to where the Commodore 64 will read its sprite data from. Sprite 0 is set at 2040, sprite 1 at 2041, sprite 2 at 2042, and so on up to sprite 7 at 2047. You can set all 8 sprite pointers to 13 by using this line in place of line 20: 20 FOR SP=2040TO2047:POKE SP,13:NEXT SP LINE 30 puts the first sprite (sprite 0) into 63 bytes of the Commodore 64's RAM memory starting at location 832 (each sprite requires 63 bytes PROGRAMMING GRAPHICS 153 ~ of memory). The first sprite (sprite 0) is "addressed" at memory locations 832 to 894. LINE 40 sets the variable "V" equal to 53248, the starting address of the VIDEO CHIP. This entry lets us use the form (V+number) for sprite settings. 're using the form (V+number) when POKEing sprite settings because this format conserves memory and lets us work with smaller numbers. For example, in line 50 we typed POKE V+21. This is the same as typing POKE 53248+21 or POKE 53269... but V+21 requires less space than 53269, and is easier to remember. LINE 50 enables or "turns on" sprite 0. There are 8 sprites, numbered from 0 to 7. To turn on an individual sprite, or a combination of sprites, all you have to do is POKE V+21 followed by a number from 0 (turn all sprites off) to 255 (turn all 8 sprites on). You can turn on one or more sprites by POKEing the following numbers: +------+------+------+------+------+------+------+------+------+-------+ |ALL ON|SPRT 0|SPRT 1|SPRT 2|SPRT 3|SPRT 4|SPRT 5|SPRT 6|SPRT 7|ALL OFF| | 255 | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 0 | +------+------+------+------+------+------+------+------+------+-------+ POKE V+21,1 turns on sprite 0. POKE V+21,128 turns on sprite 7. You can also turn on combinations of sprites. For example, POKE V+21,129 turns on both sprite 0 and sprite 7 by adding the two "turn on" numbers (1+128) together. (See SPRITEMAKING CHART, Page 176.) LINE 60 sets the COLOR of sprite 0. There are 16 possible sprite colors, numbered from 0 (black) to 15 (grey). Each sprite requires a different POKE to set its color, from V+39 to V+46. POKE V+39,1 colors sprite 0 white. POKE V+46,15 colors sprite 7 grey. (See the SPRITEMAKING CHART for more information.) When you create a sprite, as you just did, the sprite will STAY IN MEMORY until you POKE it off, redefine it, or turn off your computer. This lets you change the color, position and even shape of the sprite in DIRECT or IMMEDIATE mode, which is useful for editing purposes. As an example, RUN the program above, then type this line in DIRECT mode (without a line number) and hit the key: POKE V+39,8 The sprite on the screen is now ORANGE. Try POKEing some other numbers from 0 to 15 to see the other sprite colors. Because you did this in 154 PROGRAMMING GRAPHICS ~ DIRECT mode, if you RUN your program the sprite will return to its origi- nal color (white). LINE 70, determines the HORIZONTAL or "X" POSITION of the sprite on the screen. This number represents the location of the UPPER LEFT CORNER of the sprite. The farthest left horizontal (X) position which you can see on your television screen is position number 24, although you can move the sprite OFF THE SCREEN to position number 0. LINE 80 determines the VERTICAL or "Y" POSITION of the sprite. In this program, we placed the sprite at X (horizontal) position 24, and Y (vertical) position 100. To try another location, type this POKE in DIRECT mode and hit : POKE V,24:POKE V+1,50 This places the sprite at the upper left corner of the screen. To move the sprite to the lower left corner, type this: POKE V,24:POKE V+1,229 Each number from 832 to 895 in our sprite 0 address represents one block of 8 pixels, with three 8-pixel blocks in each horizontal row of the sprite. The loop in line 80 tells the computer to POKE 832,255 which makes the first 8 pixels solid . . . then POKE 833,255 to make the second 8 pixels solid, and so on to location 894 which is the last group of 8 pixels in the bottom right corner of the sprite. To better see how this works, try typing the following in DIRECT r-node, and notice that the second group of 8 pixels is erased: POKE 833,0 (to put it back type POKE 833,255 or RUN your program) The following line, which you can add to your program. erases the blocks in the MIDDLE of the sprite you created: 90 FOR A=836 TO 891 STEP 3:POKE A,O:NEXT A Remember, the pixels that make up the sprite are grouped in blocks of eight. This line erases the 5th group of eight pixels (block 836) and every third block up to block 890. Try POKEing any of the other numbers from 832 to 894 with either a 255 to make them solid or 0 to make them blank. PROGRAMMING GRAPHICS 155 ~ +-----------------------------------------------------------------------+ | CRUNCHING YOUR SPRITE PROGRAMS | | | | Here's a helpful "crunching" tip: The program described above is | | already short, but it can be made even shorter by "crunching" it | | smaller. In our example we list the key sprite settings on separate | | program lines so you can see what's happening in the program. In | | actual practice, a good programmer would probably write this program | | as a TWO LINE PROGRAM... by "crunching" it as follows: | | | | 10 PRINTCHR$(147):V=53248:POKEV+21,1:POKE2040.13:POKEV+39,1 | | 20 FORS=832TO894:POKES,255:NEXT:POKEV,24:POKEV+1,100 | | | | For more tips on how to crunch your programs so they fit in less | | memory and run more efficiently, see the "crunching guide" on Page 24.| +-----------------------------------------------------------------------+ TV SCREEN +---------------------------------------------------+ | ^ | | | | |<-------+---- X POSITION = HORIZONTAL ------------>| | | | | | | | | | | | | | | | | | +-+ | | | | | | | | +-+ | | | / | | | / | | | / | | | / | +-------------------------------/-------------------+ / A sprite located here must have both its X-position (horizontal) and Y-position (vertical) set so it can be displayed on the screen. Figure 3-4. The display screen is divided into a grid of X and Y coor- dinates. 156 PROGRAMMING GRAPHICS ~ POSITIONING SPRITES ON THE SCREEN The entire display screen is divided into a grid of X and Y coordi- nates, like a graph. The X COORDINATE is the HORIZONTAL position across the screen and the Y COORDINATE is the VERTICAL position up and down (see Figure 3-4). To position any sprite on the screen, you must POKE TWO SETTINGS... the X position and the Y position... these tell the computer where to display the UPPER LEFT HAND CORNER of the sprite. Remember that a sprite consists of 504 individual pixels, 24 across by 21 down... so if you POKE a sprite onto the upper left corner of your screen, the sprite will be displayed as a graphic image 24 pixels ACROSS and 21 pixels DOWN starting at the X-Y position you defined. The sprite will be displayed based on the upper left corner of the entire sprite, even if you define the sprite using only a small part of the 24X21-pixel sprite area. To understand how X-Y positioning works, study the following diagram (Figure 3-5), which shows the X and Y numbers in relation to your display screen. Note that the GREY AREA in the diagram shows your television viewing area... the white area represents positions which are OFF your viewing screen... [THE PICTURE IS MISSING!] PROGRAMMING GRAPHICS 157 ~ To display a sprite in a given location, You must POKE the X and Y settings for each SPRITE... remembering that every sprite has its own unique X POKE and Y POKE. The X and Y settings for ail 8 sprites are shown here: POKE THESE VALUES TO SET X-Y SPRITE POSITIONS +------+-------+-------+-------+-------+-------+-------+-------+--------+ | |SPRT 0 |SPRT 1 |SPRT 2 |SPRT 3 |SPRT 4 |SPRT 5 |SPRT 6 |SPRT 7 | +------+-------+-------+-------+-------+-------+-------+-------+--------+ |SET X |V,X |V+2,X |V+4,X |V+6,X |V+8,X |V+10,X |V+12,X |V+14,X | |SET Y |V+1,Y |V+3,Y |V+5,Y |V+7,Y |V+9,Y |V+11,Y |V+13,Y |V+15,Y | |RIGHTX|V+16,1 |V+16,2 |V+16,4 |V+16,8 |V+16,16|V+16,32|V+16,64|V+16,128| +------+-------+-------+-------+-------+-------+-------+-------+--------+ POKEING AN X POSITION: The possible values of X are 0 to 255, counting from left to right. Values 0 to 23 place all or part of the sprite OUT OF THE VIEWING AREA off the left side of the screen... values 24 to 255 place the sprite IN THE VIEWING AREA up to the 255th position (see next paragraph for settings beyond the 255th X position). To place the sprite at one of these positions, just type the X-POSITION POKE for the sprite you're using. For example, to POKE sprite I at the farthest left X position IN THE VIEWING AREA, type: POKE V+2,24. X VALUES BEYOND THE 255TH POSITION: To get beyond the 255th position across the screen, you need to make a SECOND POKE using the numbers in the "RIGHT X" row of the chart (Figure 3-5). Normally, the horizontal (X) numbering would continue past the 255th position to 256, 257, etc., but because registers only contain 8 bits we must use a "second register" to access the RIGHT SIDE of the screen and start our X numbering over again at 0. So to get beyond X position 255, you must POKE V+16 and a number (depending on the sprite). This gives you 65 additional X positions (renumbered from 0 to 65) in the viewing area on the RIGHT side of the viewing screen. (You can actually POKE the right side X value as high as 255, which takes you off the right edge of the viewing screen.) POKEING A Y POSITION: The possible values of Y are 0 to 255, counting from top to bottom. Values 0 to 49 place all or part of the sprite OUT OF THE VIEWING AREA off the TOP of the screen. Values 50 to 229 place the sprite IN THE VIEWING AREA. Values 230 to 255 place all or part of the sprite OUT OF THE VIEWING AREA off the BOTTOM of the screen. 158 PROGRAMMING GRAPHICS ~ Let's see how this X-Y positioning works, using sprite 1. Type this program: start tok64 page159.prg 10 print"{clear}":v=53248:pokev+21,2:poke2041,13 20 fors=832to895:pokes,255:next:pokev+40,7 30 pokev+2,24 40 pokev+3,50 stop tok64 This simple program establishes sprite 1 as a solid box and positions it at the upper left corner of the screen. Now change line 40 to read: 40 POKE V+3,229 This moves the sprite to the bottom left corner of the screen. Now let's test the RIGHT X LIMIT of the sprite. Change line 30 as shown: 30 POKE V+2,255 This moves the sprite to the RIGHT but reaches the RIGHT X LIMIT, which is 255. At this point, the "most significant bit" in register 16 must be SET. In other words, you must type POKE V+ 16 and the number shown in the "RIGHT X" column in the X-Y POKE CHART above to RESTART the X position counter at the 256th pixel/position on the screen. Change line 30 as follows: 30 POKE V+16,PEEK(V+16)OR 2:POKE V+2,0 POKE V+16,2 sets the most significant bit of the X position for sprite 1 and restarts it at the 256th pixel/position on the screen. POKE V+2,0 displays the sprite at the NEW POSITION ZERO, which is now reset to the 256th pixel. To get back to the left side of the screen, you must reset the most significant bit of the X position counter to 0 by typing (for sprite 1): POKE V+16, PEEK(V+16)AND 253 TO SUMMARIZE how the X positioning works... POKE the X POSITION for any sprite with a number from 0 to 255. To access a position beyond the 255th position/pixel across the screen, you must use an additional POKE (V+16) which sets the most significant bit of the X position and start counting from 0 again at the 256th pixel across the screen. PROGRAMMING GRAPHICS 159 ~ This POKE starts the X numbering over again from 0 at the 256th position (Example: POKE V+16,PEEK(V+16)OR 1 and POKE V,1 must be included to place sprite 0 at the 257th pixel across the screen.) To get back to the left side X positions you have to TURN OFF the control setting by typing POKE V+16,PEEK(V+16)AND 254. POSITIONING MULTIPLE SPRITES ON THE SCREEN Here's a program which defines THREE DIFFERENT SPRITES (0, 1 and 2) in different colors and places them in different positions on the screen: start tok64 page160.prg 10 print"{clear}":v=53248:fors=832to895:pokes,255:next 20 form=2040to2042:pokem,13:next 30 pokev+21,7 40 pokev+39,1:pokev+40,7:pokev+41,8 50 pokev,24:pokev+1,50 60 pokev+2,12:pokev+3,229 70 pokev+4,255:pokev+5,50 stop tok64 For convenience, all 3 sprites have been defined as solid squares, getting their data from the same place. The important lesson here is how the 3 sprites are positioned. The white sprite 0 is at the top lefthand corner. The yellow sprite 1 is at the bottom lefthand corner but HALF the sprite is OFF THE SCREEN (remember, 24 is the leftmost X position in the viewing area... an X position less than 24 puts all or part of the sprite off the screen and we used an X position 12 here which put the sprite halfway off the screen). Finally, the orange sprite 2 is at the RIGHT X LIMIT (position 255)... but what if you want to display a sprite in the area to the RIGHT of X position 255? DISPLAYING A SPRITE BEYOND THE 255TH X-POSITION Displaying a sprite beyond the 255th X position requires a special POKE which SETS the most significant bit of the X position and starts over at the 256th pixel position across the screen. Here's how it works... First, you POKE V+16 with the number for the sprite you're using (check the "RIGHT X" row in the X-Y chart... we'll use sprite 0). Now we assign an X position, keeping in mind that the X counter starts over from 0 at the 256th position on the screen. Change line 50 to read as follows: 50 POKE V+16,1:POKE V,24:POKE V+1,75 160 PROGRAMMING GRAPHICS ~ This line POKEs V+ 16 with the number required to "open up" the right side of the screen... the new X position 24 for sprite 0 now begins 24 pixels to the RIGHT of position 255. To check the right edge of the screen, change line 60 to: 60 POKE V+16,1:POKE V,65:POKE V+1,75 Some experimentation with the settings in the sprite chart will give you the settings you need to position and move sprites on the left and right sides of the screen. The section on "moving sprites" will also increase your understanding of how sprite positioning works. SPRITE PRIORITIES You can actually make different sprites seem to move IN FRONT OF or BEHIND each other on the screen. This incredible three dimensional illu- sion is achieved by the built-in SPRITE PRIORITIES which determine which sprites have priority over the others when 2 or more sprites OVERLAP on the screen. The rule is "first come, first served" which means lower-numbered sprites AUTOMATICALLY have priority over higher-numbered sprites. For example, if you display sprite 0 and sprite 1 so they overlap on the screen, sprite 0 will appear to be IN FRONT OF sprite 1. Actually, sprite 0 always supersedes all the other sprites because it's the lowest num- bered sprite. In comparison, sprite 1 has priority over sprites 2-7; sprite 2 has priority over sprites 3-7, etc. Sprite 7 (the last sprite) has LESS PRIORITY than any of the other sprites, and will always appear to be displayed "BEHIND" any other sprites which overlap its position. To illustrate how priorities work, change lines 50, 60, and 70 in the program above to the following: 50 POKEV,24:POKEV+1,50:POKEV+16,0 60 POKEV+2,34:POKEV+3,60 70 POKEV+4,44:POKEV+5,70 You should see a white sprite on top of a yellow sprite on top of an orange sprite. Of course, now that you see how priorities work, you can also MOVE SPRITES and take advantage of these priorities in your ani- mation. PROGRAMMING GRAPHICS 161 ~ DRAWING A SPRITE Drawing a Commodore sprite is like coloring the empty spaces in a coloring book. Every sprite consists of tiny dots called pixels. To draw a sprite, all you have to do is "color in" some of the pixels. Look at the spritemaking grid in Figure 3-6. This is what a blank sprite looks like: [THE PICTURE IS MISSING!] Figure 3-6. Spritemaking grid. Each little "square" represents one pixel in the sprite. There are 24 pixels across and 21 pixels up and down, or 504 pixels in the entire sprite. To make the sprite look like something, you have to color in these pixels using a special PROGRAM... but how can you control over 500 individual pixels? That's where computer programming can help you. In- stead of typing 504 separate numbers, you only have to type 63 numbers for each sprite. Here's how it works... 162 PROGRAMMING GRAPHICS ~ CREATING A SPRITE... STEP BY STEP To make this as easy as possible for you, we've put together this simple step by step guide to help you draw your own sprites. STEP 1: Write the spritemaking program shown here ON A PIECE OF PAPER... note that line 100 starts a special DATA section of your program which will contain the 63 numbers you need to create your sprite. [THE PICTURE IS MISSING!] STEP 2: Color in the pixels on the spritemaking grid on Page 162 (or use a piece of graph paper... remember, a sprite has 24 squares across and 21 squares down). We suggest you use a pencil and draw lightly so you can reuse this grid. You can create any image you like, but for our example we'll draw a simple box. STEP 3: Look at the first EIGHT pixels. Each column of pixels has a number (128, 64, 32, 16, 8, 4, 2, 1). The special type of addition we are going to show you is a type of BINARY ARITHMETIC which is used by most computers PROGRAMMING GRAPHICS 163 ~ as a special way of counting. Here's a close-up view of the first eight pixels in the top left hand corner of the sprite: |128| 64| 32| 16| 8| 4| 2| 1| +---+---+---+---+---+---+---+---+ |@@@|@@@|@@@|@@@|@@@|@@@|@@@|@@@| |@@@|@@@|@@@|@@@|@@@|@@@|@@@|@@@| +---+---+---+---+---+---+---+---+ STEP 4: Add up the numbers of the SOLID pixels. This first group of eight pixels is completely solid, so the total number is 255. STEP 5: Enter that number as the FIRST DATA STATEMENT in line 100 of the Spritemaking Program below. Enter 255 for the second and third groups of eight. STEP 6: Look at the FIRST EIGHT PIXELS IN THE SECOND ROW of the sprite. Add up the values of the solid pixels. Since only one of these pixels is solid, the total value is 128. Enter this as the first DATA number in line 101. |128| 64| 32| 16| 8| 4| 2| 1| +---+---+---+---+---+---+---+---+ |@@@| | | | | | | | |@@@| | | | | | | | +---+---+---+---+---+---+---+---+ STEP 7: Add up the values of the next group of eight pixels (which is 0 because they're all BLANK) and enter in line 101. Now move to the next group of pixels and repeat the process for each GROUP OF EIGHT PIXELS (there are 3 groups across each row, and 21 rows). This will give you a total of 63 numbers. Each number represents ONE group of 8 pixels, and 63 groups of eight equals 504 total individual pixels. Perhaps a better way of looking at the program is like this... each line in the program represents ONE ROW in the sprite. Each of the 3 numbers in each row represents ONE GROUP OF EIGHT PIXELS. And each number tells the computer which pixels to make SOLID and which pixels to leave blank. 164 PROGRAMMING GRAPHICS ~ STEP 8: CRUNCH YOUR PROGRAM INTO A SMALLER SPACE BY RUNNING TOGETHER ALL THE DATA STATEMENTS, AS SHOWN IN THE SAMPLE PROGRAM BELOW. Note that we asked you to write your sprite program on a piece of paper. We did this for a good reason. The DATA STATEMENT LINES 100-120 in the program in STEP 1 are only there to help you see which numbers relate to which groups of pixels in your sprite. Your final program should be "crunched" like this: start tok64 page165.prg 10 print"{clear}":poke53280,5:poke53281,6 20 v=53248:pokev+34,3 30 poke 53269,4:poke2042,13 40 forn=0to62:readq:poke832+n,q:next 100 data255,255,255,128,0,1,128,0,1,128,0,1,144,0,1,144,0,1,144,0,1,144,0 101 data1,144,0,1,144,0,1,144,0,1,144,0,1,144,0,1,144,0,1,128,0,1,128,0,1 102 data128,0,1,128,0,1,128,0,1,128,0,1,255,255,255 200 x=200:y=100:poke53252,x:poke53253,y stop tok64 MOVING YOUR SPRITE ON THE SCREEN Now that you've created your sprite, let's do some interesting things with it. To move your sprite smoothly across the screen, add these two lines to your program: 50 POKE V+5,100:FOR X=24TO255:POKE V+4,X:NEXT:POKE V+16,4 55 FOR X=0TO65:POKE V+4,X:NEXT X:POKE V+16,0:GOTO 50 LINE 50 POKEs the Y POSITION at 100 (try 50 or 229 instead for variety). Then it sets up a FOR... NEXT loop which POKEs the sprite into X position 0 to X position 255, in order. When it reaches the 255th position, it POKEs the RIGHT X POSITION (POKE V+16,4) which is required to cross to the right side of the screen. LINE 55 has a FOR... NEXT loop which continues to POKE the sprite in the last 65 positions on the screen. Note that the X value was reset to zero but because you used the RIGHT X setting (POKE V+16,2) X starts over on the right side of the screen. This line keeps going back to itself (GOTO 50). If you just want the sprite to move ONCE across the screen and disappear, then take out GOTO50. PROGRAMMING GRAPHICS 165 ~ Here's a line which moves the sprite BACK AND FORTH: 50 POKE V+5,100:FOR X=24TO255:POKE V+4,X:NEXT:POKE V+16,4: FOR X=0TO65: POKE V+4,X: NEXT X 55 FOR X=65TO0 STEP-1:POKE V+4,X:NEXT:POKE V+16,0: FOR X=255TO24 STEP-1: POKE V+4,X:NEXT 60 GOTO 50 Do you see how these programs work? This program is the same as the previous one, except when it reaches the end of the right side of the screen, it REVERSES ITSELF and goes back in the other direction. That is what the STEP-1 accomplishes... it tells the program to POKE the sprite into X values from 65 to 0 on the right side of the screen, then from 255 to 0 on the left side of the screen, STEPping backwards minus-1 position at a time. VERTICAL SCROLLING This type of sprite movement is called "scrolling." To scroll your sprite up or down in the Y position, you only have to use ONE LINE. ERASE LINES 50 and 55 by typing the line numbers by themselves and hitting like this: 50 60 Now enter LINE 50 again as follows: 50 POKE V+4,24:FOR Y=0TO255:POKE V+5,Y:NEXT THE DANCING MOUSE-A SPRITE PROGRAM EXAMPLE Sometimes the techniques described in a programmer's reference manual are difficult to understand, so we've put together a fun sprite program called "Michael's Dancing Mouse." This program uses three different sprites in a cute animation with sound effects-and to help you understand how it works we've included an explanation of EACH COMMAND so you can see exactly how the program is constructed: 166 PROGRAMMING GRAPHICS ~ start tok64 page167.prg 5 s=54272:pokes+24,15:pokes,220:pokes+1,68:pokes+5,15:pokes+6,215 10 pokes+7,120:pokes+8,100:pokes+12,15:pokes+13,215 15 print"{clear}":v=53248:pokev+21,1 20 fors1=12288to12350:readq1:pokes1,q1:next 25 fors2=12352to12414:readq2:pokes2,q2:next 30 fors3=12416to12478:readq3:pokes3,q3:next 35 pokev+39,15:pokev+1,68 40 printtab(160)"{white}i am the dancing mouse!{light blue}" 45 p=192 50 forx=0to347step3 55 rx=int(x/256):lx=x-rx*256 60 pokev,lx:pokev+16,rx 70 ifp=192thengosub200 75 ifp=193thengosub300 80 poke2040,p:fort=1to60:next 85 p=p+1:ifp>194thenp=192 90 next 95 end 100 data30,0,120,63,0,252,127,129,254,127,129,254,127,189,254,127,255,254 101 data63,255,252,31,187,248,3,187,192,1,255,128,3,189,192,1,231,128,1, 102 data255,0,31,255,0,0,124,0,0,254,0,1,199,32,3,131,224,7,1,192,1,192,0 103 data3,192,0,30,0,120,63,0,252,127,129,254,127,129,254,127,189,254,127 104 data255,254,63,255,252,31,221,248,3,221,192,1,255,128,3,255,192,1,195 105 data128,1,231,3,31,255,255,0,124,0,0,254,0,1,199,0,7,1,128,7,0,204,1 106 data128,124,7,128,5630,0,120,63,0,252,127,129,254,127,129,254,127,189 107 data254,127,255,25463,255,252,31,221,248,3,221,192,1,255,134,3,189 108 data204,1,199,152,1,255,48,1,255,224,1,252,0,3,254,0 109 data7,14,0,204,14,0,248,56,0,112,112,0,0,60,0,-1 200 pokes+4,129:pokes+4,128:return 300 pokes+11,129:pokes+11,128:return stop tok64 PROGRAMMING GRAPHICS 167 ~ LINE 5: S=54272 Sets the variable 5 equal to 54272, which is the beginning memory location of the SOUND CHIP. From now on, instead of poking a direct memory location, we will POKE S plus a value. POKES+24,15 Same as POKE 54296,15 which sets VOLUME to highest level. POKES,220 Same as POKE 54272,220 which sets Low Fre- quency in Voice 1 for a note which approximates high C in Octave 6. POKES+1,68 Same as POKE 54273,68 which sets High Fre- quency in Voice I for a note which approximates high C in Octave 6. POKES+5,15 Same as POKE 54277,15 which sets Attack/Decay for Voice 1 and in this case consists of the maximum DECAY level with no attack, which pro- duces the "echo" effect. POKES+6,215 Same as POKE 54278,215 which sets Sustain/Re- lease for Voice 1 (215 represents a combination of sustain and release values). LINE 10: POKES+7,120 Same as POKE 54279,120 which sets the Low Fre- quency for Voice 2. POKES+8,100 Same as POKE 54280,100 which sets the High Frequency for Voice 2. POKES+12,15 Same as POKE 54284,15 which sets Attack/Decay for Voice 2 to same level as Voice 1 above. POKES+13,215 Same as POKE 54285,215 which sets Sustain/Re- lease for Voice 2 to same level as Voice 1 above. LINE 15: PRINT"" Clears the screen when the program begins. V=53248 Defines the variable "V" as the starting location of the VIC chip which controls sprites. From now on we will define sprite locations as V plus a value. POKEV+21,1 Turns on (enables) sprite number 1. 168 PROGRAMMING GRAPHICS ~ LINE 20: FORS1=12288 We are going to use ONE SPRITE (sprite 0) in this TO 12350 animation, but we are going to use THREE sets of sprite data to define three separate shapes. To get our animation, we will switch the POINTERS for sprite 0 to the three places in memory where we have stored the data which defines our three different shapes. The same sprite will be rede- fined rapidly over and over again as 3 different shapes to produce the dancing mouse animation. You can define dozens of sprite shapes in DATA STATEMENTS, and rotate those shapes through one or more sprites. So you see, you don't have to limit one sprite to one shape or vice-versa. One sprite can have many different shapes, simply by changing the POINTER SETTING FOR THAT SPRITE to different places in memory where the sprite data for different shapes is stored. This line means we have put the DATA for "sprite shape 1" at memory locations 12288 to 12350. READ Q1 Reads 63 numbers in order from the DATA state- ments which begin at line 100. Q1 is an arbitrary variable name. It could just as easily be A, Z1 or another numeric variable. POKES1,Q1 Pokes the first number from the DATA statements (the first "Q1" is 30) into the first memory location (the first memory location is 12288). This is the same as POKE12288,30. NEXT This tells the computer to look BETWEEN the FOR and NEXT parts of the loop and perform those in-between commands (READQ1 and POKES1,Q1 using the NEXT numbers in order). In other words, the NEXT statement makes the computer READ the NEXT Q1 from the DATA STATEMENTS, which is 0, and also increments S1 by 1 to the next value, which is 12289. The result is POKE12289,0... the NEXT command makes the loop keep going back until the last values in the series, which are POKE 12350,0. PROGRAMMING GRAPHICS 169 ~ LINE 25: FORS2=12352 The second shape of sprite zero is defined by the TO 12414 DATA which is located at locations 12352 to 12414. NOTE that location 12351 is SKIPPED... this is the 64th location which is used in the definition of the first sprite group but does not contain any of the sprite data numbers. Just remember when defining sprites in consecutive locations that you will use 64 locations, but only POKE sprite data into the first 63 locations. READQ2 Reads the 63 numbers which follow the numbers we used for the first sprite shape. This READ simply looks for the very next number in the DATA area and starts reading 63 numbers, one at a time. POKES2,Q2 Pokes the data (Q2) into the memory locations (S2) for our second sprite shape, which begins at location 12352. NEXT Same use as line 20 above. LINE 30: FORS3=12416 The third shape of sprite zero is defined by the TO 12478 DATA to be located at locations 12416 to 12478. READQ3 Reads last 63 numbers in order as Q3. POKES3,Q3 Pokes those numbers into locations 12416 to 12478. NEXT Same as lines 20 and 25. LINE 35: POKEV+39,15 Sets color for sprite 0 to light grey. POKEV+1,68 Sets the upper right hand corner of the sprite square to vertical (Y) position 68. For the sake of comparison, position 50 is the top lefthand corner Y position on the viewing screen. 170 PROGRAMMING GRAPHICS ~ LINE 40: PRINTTAB(160) Tabs 160 spaces from the top lefthand CHARACTER SPACE on the screen, which is the same as 4 rows beneath the clear command... this starts your PRINT message on the 6th line down on the screen. "{white} Hold down the key and press the key marked at the same time. If you do this inside quotation marks, a "reversed E" will appear. This sets the color to everything PRINTed from then on to WHITE. I AM THE This is a simple PRINT statement. DANCING MOUSE! {light blue} This sets the color back to light blue when the PRINT statement ends. Holding down and <7> a at the same time inside quotation marks causes a "reversed diamond symbol" to appear. LINE 45: P=192 Sets the variable P equal to 192. This number 192 is the pointer you must use, in this case to "point" sprite 0 to the memory locations that begin at location 12288. Changing this pointer to the locations of the other two sprite shapes is the secret of using one sprite to create an animation that is actually three different shapes. LINE 50: FORX=0TO347 Steps the movement of your sprite 3 X positions at STEP3 a time (to provide fast movement) from position 0 to position 347. PROGRAMMING GRAPHICS 171 ~ LINE 55: RX=INT(X/256) RX is the integer of X/256 which means that RX is rounded off to 0 when X is less than 256, and RX becomes 1 when X reaches position 256. We will use RX in a moment to POKE V+16 with a 0 or 1 to turn on the "RIGHT SIDE" of the screen. LX=X-RX*256 When the sprite is at X position 0, the formula looks like this: LX = 0 - (0 times 256) or 0. When the sprite is at X position 1 the formula looks like this: LX = 1 - (0 times 256) or 1. When the sprite is at X position 256 the formula looks like this: LX = 256 - (1 times 256) or 0 which resets X back to 0 which must be done when you start over on the RIGHT SIDE of the screen (POKEV+16,1). LINE 60: POKEV,LX You POKE V by itself with a value to set the Hori- zontal (X) Position of sprite 0 on the screen. (See SPRITEMAKING CHART on Page 176). As shown above, the value of LX, which is the horizontal position of the sprite, changes from 0 to 255 and when it reaches 255 it automatically resets back to zero because of the LX equation set up in line 55. POKEV+16,RX POKEV+16 always turns on the "right side" of the screen beyond position 256, and resets the horizontal positioning coordinates to zero. RX is either a 0 or a 1 based on the position of the sprite as determined by the RX formula in line 55. LINE 70: IFP=192THEN If the sprite pointer is set to 192 (the first GOSUB200 sprite shape) the waveform control for the first sound effect is set to 129 and 128 per line 200. 172 PROGRAMMING GRAPHICS ~ LINE 75: IFP=193THEN If the sprite pointer is set to 193 (the second GOSUB300 sprite shape) the waveform control for the second sound effect (Voice 2) is set to 129 and 128 per line 300. LINE 80: POKE2040,P Sets the SPRITE POINTER to location 192 (remember P=192 in line 45? Here's where we use the P). FORT=1TO60: A simple time delay loop which sets the speed at NEXT which the mouse dances. (Try a faster or slower speed by increasing/decreasing the number 60.) LINE 85: P=P+1 Now we increase the value of the pointer by adding 1 to the original value of P. IFP>194THEN We only want to point the sprite to 3 memory lo- P=192 cations. 192 points to locations 12288 to 12350, 193 points to locations 12352 to 12414, and 194 points to locations 12416 to 12478. This line tells the computer to reset P back to 192 as soon as P becomes 195 so P never really becomes 195. P is 192, 193, 194 and then resets back to 192 and the pointer winds up pointing consecutively to the three sprite shapes in the three 64-byte groups of memory locations containing the DATA. PROGRAMMING GRAPHICS 173 ~ LINE 90: NEXTX After the sprite has become one of the 3 different shapes defined by the DATA, only then is it allowed to move across the screen. It will jump 3 X positions at a time (instead of scrolling smoothly one position at a time, which is also possible). STEPping 3 positions at a time makes the mouse "dance" faster across the screen. NEXT X matches the FOR... X position loop in line 50. LINE 95 END ENDs the program, which occurs when the sprite moves off the screen. LINES 100-109 DATA The sprite shapes are read from the data numbers, in order. First the 63 numbers which comprise sprite shape 1 are read, then the 63 numbers for sprite shape 2, and then sprite shape 3. This data is permanently read into the 3 memory locations and after it is read into these locations, all the program has to do is point sprite 0 at the 3 memory locations and the sprite automatically takes the shape of the data in those locations. We are pointing the sprite at 3 locations one at a time which produces the "animation" effect. If you want to see how these numbers affect each sprite, try changing the first 3 numbers in LINE 100 to 255, 255, 255. See the section on defining sprite shapes for more information. 174 PROGRAMMING GRAPHICS ~ LINE 200: POKES+4,129 Waveform control set to 129 turns on the sound effect. POKES+4,128 Waveform control set to 128 turns off the sound effect. RETURN Sends program back to end of line 70 after waveform control settings are changed, to resume program. LINE 300: POKES+11,129 Waveform control set to 129 turns on the sound effect. POKES+11,128 Waveform control set to 128 turns off the sound effect. RETURN Sends program back to end of line 75 to resume. PROGRAMMING GRAPHICS 175 ~ EASY SPRITEMAKING CHART +----------+------+------+------+------+-------+-------+-------+--------+ | |SPRT 0|SPRT 1|SPRT 2|SPRT 3|SPRT 4 |SPRT 5 |SPRT 6 | SPRT 7 | +----------+------+------+------+------+-------+-------+-------+--------+ |Turn on |V+21,1|V+21,2|V+21,4|V+21,8|V+21,16|V+21,32|V+21,64|V+21,128| +----------+------+------+------+------+-------+-------+-------+--------+ |Put in mem| 2040,| 2041,| 2042,| 2043,| 2044, | 2045, | 2046, | 2047, | |set point.| 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | +----------+------+------+------+------+-------+-------+-------+--------+ |Locations | 12288| 12352| 12416| 12480| 12544 | 12608 | 12672 | 12736 | |for Sprite| to | to | to | to | to | to | to | to | |Pixel | 12350| 12414| 12478| 12542| 12606 | 12670 | 12734 | 12798 | +----------+------+------+------+------+-------+-------+-------+--------+ |Color |V+39,C|V+40,C|V+41,C|V+42,C|V+43,C |V+44,C |V+45,C |V+46,C | +----------+------+------+------+------+-------+-------+-------+--------+ |Set LEFT X| V+0,X| V+2,X| V+4,X| V+6,X| V+8,X |V+10,X |V+12,X |V+14,X | +----------+------+------+------+------+-------+-------+-------+--------+ |Set RIGHT |V+16,1|V+16,2|V+16,4|V+16,8|V+16,16|V+16,32|V+16,64|V+16,128| |X position| V+0,X| V+2,X| V+4,X| V+6,X| V+8,X |V+10,X |V+12,X |V+14,X | +----------+------+------+------+------+-------+-------+-------+--------+ |Set Y pos.| V+1,Y| V+3,Y| V+5,Y| V+7,Y| V+9,Y |V+11,Y |V+13,Y |V+15,Y | +----------+------+------+------+------+-------+-------+-------+--------+ |Exp. Horiz|V+29,1|V+29,2|V+29,4|V+29,8|V+29,16|V+29,32|V+29,64|V+29,128| +----------+------+------+------+------+-------+-------+-------+--------+ |Exp. Vert.|V+23,1|V+23,2|V+23,4|V+23,8|V+23,16|V+23,32|V+23,64|V+23,128| +----------+------+------+------+------+-------+-------+-------+--------+ |Multi-Col.|V+28,1|V+28,2|V+28,4|V+28,8|V+28,16|V+28,32|V+28,64|V+28,128| +----------+------+------+------+------+-------+-------+-------+--------+ |M-Color 1 |V+37,C|V+37,C|V+37,C|V+37,C|V+37,C |V+37,C |V+37,C |V+37,C | +----------+------+------+------+------+-------+-------+-------+--------+ |M-Color 2 |V+38,C|V+38,C|V+38,C|V+38,C|V+38,C |V+38,C |V+38,C |V+38,C | +----------+------+------+------+------+-------+-------+-------+--------+ |Priority | The rule is that lower numbered sprites always have display| |of sprites| priority over higher numbered sprites. For example, sprite | | | 0 has priority over ALL other sprites, sprite 7 has last | | | priority. This means lower numbered sprites always appear | | | to move IN FRONT OF or ON TOP OF higher numbered sprites. | +----------+------------------------------------------------------------+ |S-S Collis| V+30 IF PEEK(V+30)ANDX=X THEN [action] | +----------+------------------------------------------------------------+ |S-B Collis| V+31 IF PEEK(V+31)ANDX=X THEN [action] | +----------+------------------------------------------------------------+ 176 PROGRAMMING GRAPHICS ~ SPRITEMAKING NOTES Alternative Sprite Memory Pointers and Memory Locations Using Cassette Buffer +---------------+-------+-------+-------+-------------------------------+ | Put in Memory |SPRT 0 |SPRT 1 |SPRT 2 | If you're using 1 to 3 sprites| | (Set pointers)|2040,13|2041,14|2042,15| you can use these memory | +---------------+-------+-------+-------+ locations in the cassette | | Sprite Pixel | 832 | 896 | 960 | buffer (832 to 1023) but for | | Locations for | to 894| to 958|to 1022| more than 3 sprites we suggest| | Blocks 13-15 | | | | using locations from 12288 to | +---------------+-------+-------+-------+ 12798 (see chart). | TURNING ON SPRITES: +-------------------------------+ You can turn on any individual sprite by using POKE V+21 and the number from the chart... BUT... turning on just ONE sprite will turn OFF any others. To turn on TWO OR MORE sprites, ADD TOGETHER the numbers of the sprites you want to turn on (Example: POKE V+21, 6 turns on sprites 1 and 2). Here is a method you can use to turn one sprite off and on without affecting any of the others (useful for animation). EXAMPLE: To turn off just sprite 0 type: POKE V+21,PEEK V+21AND(255-1). Change the number 1 in (255-1) to 1,2,4,8,16,32,64, or 128 (for sprites 0-7). To re-enable the sprite and not affect the other sprites currently turned on, POKE V+21, PEEK(V+21)OR 1 and change the OR 1 to OR 2 (sprite 2), OR 4 (sprite 3), etc. X POSITION VALUES BEYOND 255: X positions run from 0 to 255... and then START OVER from 0 to 255. To put a sprite beyond X position 255 on the far right side of the screen, you must first POKE V+ 16 as shown, THEN POKE a new X valve from 0 to 63, which will place the sprite in one of the X positions at the right side of the screen. To get back to positions 0-255, POKE V+16,0 and POKE in an X value from 0 to 255. Y POSITION VALUES: Y positions run from 0 to 255, including 0 to 49 off the TOP of the viewing area, 50 to 229 IN the,viewing area, and 230 to 255 off the BOTTOM of the viewing area. PROGRAMMING GRAPHICS 177 ~ SPRITE COLORS: To make sprite 0 WHITE, type: POKE V+39,1 (use COLOR POKE SETTING shown in chart, and INDIVIDUAL COLOR CODES shown below): 0-BLACK 4-PURPLE 8-ORANGE 12-MED. GREY 1-WHITE 5-GREEN 9-BROWN 13-LT. GREEN 2-RED 6-BLUE 10-LT. RED 14-LT. BLUE 3-CYAN 7-YELLOW 11-DARK GREY 15-LT. GREY MEMORY LOCATION: You must "reserve" a separate 64-BYTE BLOCK of numbers in the computer's memory for each sprite of which 63 BYTES will be used for sprite data. The memory settings shown below are recommended for the "sprite pointer" settings in the chart above. Each sprite will be unique and you'll have to define it as you wish. To make all sprites exactly the same, point the sprites you want to look the same to the same register for sprites. DIFFERENT SPRITE POINTER SETTINGS: These sprite pointer settings are RECOMMENDATIONS ONLY. Caution: you can set your sprite pointers anywhere in RAM memory but if you set them too "low" in memory a long BASIC program may overwrite your sprite data, or vice versa. To protect an especially LONG BASIC PROGRAM from overwriting sprite data, you may want to set the sprites at a higher area of memory (for example, 2040,192 for sprite 0 at locations 12288 to 12350... 2041,193 at locations 12352 to 12414 for sprite 1 and so on... by adjusting the memory locations from which sprites get their "data," you can define as many as 64 different sprites plus a sizable BASIC program. To do this, define several sprite "shapes" in your DATA statements and then redefine a particular sprite by changing the "pointer" so the sprite you are using is "pointed" at different areas of memory containing different sprite picture data. See the "Dancing Mouse" to see how this works. If you want two or more sprites to have THE SAME SHAPE (you can still change position and color of each sprite), use the same sprite pointer and memory location for the sprites you want to match (for example, you can point sprites 0 and 1 to the same location by using POKE 2040,192 and POKE 2041, 192). 178 PROGRAMMING GRAPHICS ~ PRIORITY: Priority means one sprite will appear to move "in front of" or "behind" another sprite on the display screen. Sprites with more priority always appear to move "in front of" or "on top of" sprites with less priority. The rule is that lower numbered sprites have priority over higher numbered sprites. Sprite 0 has priority over all other sprites. Sprite 7 has no priority in relation to the other sprites. Sprite 1 has priority over sprites 2-7, etc. If you put two sprites in the some position, the sprite with the higher priority will appear IN FRONT OF the sprite with the lower priority. The sprite with lower priority will either be obscured, or will "show through" (from "behind") the sprite with higher priority. USING MULTI-COLOR: You can create multi-colored sprites although using multi-color mode requires that you use PAIRS of pixels instead of individual pixels in your sprite picture (in other words each colored "dot" or "block" in the sprite will consist of two pixels side by side). You have 4 colors to choose from: Sprite Color (chart,above), Multi-Color 1, Multi-Color 2 and "Background Color" (background is achieved by using zero settings which let the background color "show through"). Consider one horizontal 8-pixel block in a sprite picture. The color of each PAIR of pixels is determined according to whether the left, right, or both pixels are solid, like this: +-+-+ | | | BACKGROUND (Making BOTH PIXELS BLANK (zero) lets the +-+-+ INNER SCREEN COLOR (background)show through.) +-+-+ | |@| MULTI-COLOR 1 (Making the RIGHT PIXEL SOLID in a pair of pixels +-+-+ sets BOTH PIXELS to Multi-Color 1.) +-+-+ |@| | SPRITE COLOR (Making the LEFT PIXEL SOLID in a pair of pixels +-+-+ sets BOTH PIXELS to Sprite Color.) +-+-+ |@|@| MULTI-COLOR 2 (Making BOTH PIXELS SOLID in a pair of pixels +-+-+ sets BOTH PIXELS to Multi-Color 2.) PROGRAMMING GRAPHICS 179 ~ Look at the horizontal 8-pixel row shown below. This block sets the first two pixels to background color, the second two pixels to Multi-Color 1, the third two pixels to Sprite Color and the fourth two pixels to Multi- Color 2. The color of each PAIR of pixels depends on which bits in each pair are solid and which are blank, according to the illustration above. After you determine which colors you want in each pair of pixels, the next step is to add the values of the solid pixels in the 8-pixel block, and POKE that number into the proper memory location. For example, if the 8-pixel row shown below is the first block in a sprite which begins at memory location 832, the value of the solid pixels is 16+8+2+1 27, so you would POKE 832,27. |128| 64| 32| 16| 8| 4| 2| 1| 16+8+2+1 = 27 +---+---+---+---+---+---+---+---+ | | | |@@@|@@@| |@@@|@@@| | | | |@@@|@@@| |@@@|@@@| +---+---+---+---+---+---+---+---+ LOOKS LIKE THIS IN SPRITE +-------+-------+-------+-------+ |BACKGR.|MULTI- |SPRITE |MULTI- | | COLOR |COLOR 1| COLOR |COLOR 2| +-------+-------+-------+-------+ COLLISION: You can detect whether a sprite has collided with another sprite by using this line: IF PEEK(V+30)ANDX=XTHEN [insert action here]. This line checks to see if a particular sprite has collided with ANY OTHER SPRITE, where X equals 1 for sprite 0, 2 for sprite 1, 4 for sprite 2, 8 for sprite 3, 16 for sprite 4, 32 for sprite 5, 64 for sprite 6, and 128 for sprite 7. To check to see if the sprite has collided with a "BACKGROUND CHARACTER" use this line: IF PEEK(V+31)ANDX=XTHEN [insert action here]. 180 PROGRAMMING GRAPHICS ~ USING GRAPHIC CHARACTERS IN DATA STATEMENTS The following program allows you to create a sprite using blanks and solid circles in DATA statements. The sprite and the numbers POKED into the sprite data registers are displayed. start tok64 page181.prg 10 print"{clear}":fori=0to63:poke832+i,0:next 20 gosub60000 999 end 60000 data" QQQQQQQ " 60001 data" QQQQQQQQQQQ " 60002 data" QQQQQQQQQQQQQ " 60003 data" QQQQQ QQQQQ " 60004 data" QQQQQ QQQ QQQQ " 60005 data" QQQQQ QQQ QQQQQ " 60006 data" QQQQQ QQQ QQQQ " 60007 data" QQQQQ QQQQQ " 60008 data" QQQQQQQQQQQQQ " 60009 data" QQQQQQQQQQQQQ " 60010 data" Q QQQQQQQQQ Q " 60011 data" Q QQQQQQQ Q " 60012 data" Q QQQQQ Q " 60013 data" Q QQQ Q " 60014 data" Q QQQ Q " 60015 data" Q Q Q " 60016 data" Q Q Q " 60017 data" QQQQQ " 60018 data" QQQQQ " 60019 data" QQQQQ " 60020 data" QQQ " 60100 v=53248:pokev,200:pokev+1,100:pokev+21,1:pokev+39,14:poke2040,13 60105 pokev+23,1:pokev+29,1 60110 fori=0to20:reada$:fork=0to2:t=0:forj=0to7:b=0 60140 ifmid$(a$,j+k*8+1,1)="Q"thenb=1 60150 t=t+b*2^(7-j):next:printt;:poke832+i*3+k,t:next:print:next 60200 return stop tok64 PROGRAMMING GRAPHICS 181 ~~ CHAPTER 4 PROGRAMMING SOUND AND MUSIC ON YOUR COMMODORE 64 o Introduction Volume Control Frequencies of Sound Waves o Using Multiple Voices o Changing Waveforms o The Envelope Generator o Filtering o Advanced Techniques o Synchronization and Ring Modulation 183 ~ INTRODUCTION Your Commodore computer is equipped with one of the most sophisticated electronic music synthesizers available on any computer. It comes complete with three voices, totally addressable, ATTACK/DECAY/SUSTAIN/ RELEASE (ADSR), filtering, modulation, and "white noise." All of these capabilities are directly available for you through a few easy to use BASIC and/or assembly language statements and functions. This means that you can make very complex sounds and songs using programs that are relatively simple to design. This section of your Programmer's Reference Guide has been created to help you explore all the capabilities of the 6581 "SID" chip, the sound and music synthesizer inside your Commodore computer. We'll explain both the theory behind musical ideas and the practical aspects of turning those ideas into real finished songs on your Commodore computer. You need not be an experienced programmer nor a music expert to achieve exciting results from the music synthesizer. This section is full of programming examples with complete explanations to get you started. You get to the sound generator by POKEing into specified memory locations. A full list of the locations used is provided in Appendix O. We will go through each concept, step by step. By the end you should be able to create an almost infinite variety of sounds, and be ready to perform experiments with sound on your own. Each section of this chapter begins by giving you an example and a full line-by-line description of each program, which will show you how to use the characteristic being discussed. The technical explanation is for you to read whenever you are curious about what is actually going on. The workhorse of your sound programs is the POKE statement. POKE sets the indicated memory location (MEM) equal to a specified value (NUM). POKE MEM,NUM The memory locations (MEM) used for music synthesis start at 54272 ($D400) in the Commodore 64. The memory locations 54272 to 54296 inclusive are the POKE locations you need to remember when you're using the 6581 (SID) chip register map. Another way to use the locations above is to remember only location 54272 and then add a number from 0 through 24 to it. By doing this you can POKE all the locations from 54272 to 54296 that you need from the SID chip. The numbers (NUM) that you use in your POKE statement must be between 0 and 255, inclusive. 184 PROGRAMMING SOUND AND MUSIC ~ When you've had a little more practice with making music, then you can get a little more involved, by using the PEEK function. PEEK is a function that is equal to the value currently in the indicated memory location. X=PEEK(MEM) The value of the variable X is set equal to the current contents of memory location MEM. Of course, your programs include other BASIC commands, but for a full explanation of them, refer to the BASIC Statements section of this manual. Let's jump right in and try a simple program using only one of the three voices. Computer ready? Type NEW, then type in this program, and save it on your Commodore DATASSETTE(TM) or disk. Then, RUN it. EXAMPLE PROGRAM 1: start tok64 page185.prg 5 s=54272 10 forl=stos+24:pokel,0:next:rem clear sound chip 20 pokes+5,9:pokes+6,0 30 pokes+24,15 :rem set volume to maximum 40 readhf,lf,dr 50 ifhf<0thenend 60 pokes+1,hf:pokes,lf 70 pokes+4,33 80 fort=1todr:next 90 pokes+4,32:fort=1to50:next 100 goto40 110 data25,177,250,28,214,250 120 data25,177,250,25,177,250 130 data25,177,125,28,214,125 140 data32,94,750,25,177,250 150 data28,214,250,19,63,250 160 data19,63,250,19,63,250 170 data21,154,63,24,63,63 180 data25,177,250,24,63,125 190 data19,63,250,-1,-1,-1 stop tok64 Here's a line-by-line description of the program you've just typed in. Refer to it whenever you feel the need to investigate parts of the pro- gram that you don't understand completely. PROGRAMMING SOUND AND MUSIC 185 ~ LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 1: +--------+--------------------------------------------------------------+ | Line(s)| Description | +--------+--------------------------------------------------------------+ | 5 | Set S to start of sound chip. | | 10 | Clear all sound chip registers. | | 20 | Set Attack/Decay for voice 1 (A=O,D=9). | | | Set Sustain/Release for voice 1 (S=O,R=O), | | 30 | Set volume at maximum. | | 40 | Read high frequency, low frequency, duration of note. | | 50 | When high frequency less than zero, song is over. | | 60 | Poke high and low frequency of voice 1. | | 70 | Gate sawtooth waveform for voice 1. | | 80 | Timing loop for duration of note. | | 90 | Release sawtooth waveform for voice 1. | | 100 | Return for next note. | | 110-180| Data for song: high frequency, low frequency, duration | | | (number of counts) for each note. | | 190 | Last note of song and negative Is signaling end of song. | +--------+--------------------------------------------------------------+ VOLUME CONTROL Chip register 24 contains the overall volume control. The volume can be set anywhere between 0 and 15. The other four bits are used for purposes we'll get into later. For now it is enough to know volume is 0 to 15. Look at line 30 to see how it's set in Example Program 1. FREQUENCIES OF SOUND WAVES Sound is created by the movement of air in waves. Think of throwing a stone into a pool and seeing the waves radiate outward. When similar waves are created in air, we hear it. If we measure the time between one peak of a wave and the next, we find the number of seconds for one cycle of the wave (n = number of seconds). The reciprocal of this number (1/n) gives you the cycles per second. Cycles per second are more commonly known as the frequency. The highness or lowness of a sound (pitch) is determined by the frequency of the sound waves produced. The sound generator in your Commodore computer uses two locations to determine the frequency. Appendix E gives you the frequency values you need to reproduce a full eight octaves of musical notes. To create a 186 PROGRAMMING SOUND AND MUSIC ~ frequency other than the ones listed in the note table use "Fout" (fre- quency output) and the following formula to represent the frequency (Fn) of the sound you want to create. Remember that each note requires both a high and a low frequency number. Fn = Fout/.06097 Once you've figured out what Fn is for your "new" note the next step is to create the high and low frequency values for that note. To do this you must first round off Fn so that any numbers to the right of the decimal point are left off. You are now left with an integer value. Now you can set the high frequency location (Fhi) by using the formula Fhi=INT(Fn/256) and the low frequency location (Flo) should be Flo=Fn-(256*Fhi). At this point you have already played with one voice of your computer. If you wanted to stop here you could find a copy of your favorite tune and become the maestro conducting your own computer orchestra in your "at home" concert hall. USING MULTIPLE VOICES Your Commodore computer has three independently controlled voices (oscillators). Our first example program used only one of them. later on, you'll learn how to change the quality of the sound made by the voices. But right now, let's get all three voices singing. This example program shows you one way to translate sheet music for your computer orchestra. Try typing it in, and then SAVE it on your DATASSETTE(TM) or disk. Don't forget to type NEW before typing in this program. EXAMPLE PROGRAM 2: start tok64 page187.prg 10 s=54272:forl=stos+24:pokel,0:next 20 dimh(2,200),l(2,200),c(2,200) 30 dimfq(11) 40 v(0)=17:v(1)=65:v(2)=33 50 pokes+10,8:pokes+22,128:pokes+23,244 60 fori=0to11:readfq(i):next 100 fork=0to2 110 i=0 120 readnm 130 ifnm=0then250 140 wa=v(k):wb=wa-1:ifnm<0thennm=-nm:wa=0:wb=0 150 dr%nm/128:oc%=(nm-128*dr%)/16 160 nt=nm-128*dr%-16*oc% 170 fr=fq(nt) 180 ifoc%=7then200 190 forj=6tooc%step-1:fr=fr/2:next 200 hf%=fr/256:lf%=fr-256*hf% 210 ifdr%=1thenh(k,i)=hf%:l(k,i)=lf%:c(k,i)=wa:i=i+1:goto120 220 forj=1todr%-1:h(k,i)=hf%:l(k,i)=lf%:c(k,i)=wa:i=i+1:next 230 h(k,i)=hf%:l(k,i)=lf%:c(k,i)=wb 240 i=i+1:goto120 250 ifi>imthenim=i 260 next 500 pokes+5,0:pokes+6,240 510 pokes+12,85:pokes+13,133 520 pokes+19,10:pokes+20,197 530 pokes+24,31 540 fori=0toim 550 pokes,l(0,i):pokes+7,l(1,i):pokes+14,l(2,i) 560 pokes+1,h(0,i):pokes+8,h(1,i):pokes+15,h(2,i) 570 pokes+4,c(0,i):pokes+11,c(1,i):pokes+18,c(2,i) 580 fort=1to80:next:next 590 fort=1to200:next:pokes+24,0 600 data34334,36376,38539,40830 610 data43258,45830,48556,51443 620 data54502,57743,61176,64814 1000 data594,594,594,596,596,1618,587,592,587.585,331,336 1010 data1097,583,585,585,585,587,587,1609,585,331,337,594,594,593 1020 data1618,594,596,594,592,587,1616,587,585,331,336,841,327 1999 data1607,0 2000 data583,585,583,583,327,329,1611,583,585,578,578,578 2010 data196,198,583,326,578,326,327,329,327,329,326,578,583 2020 data1606,582,322,324,582,587,329,327,1606,583,327,329,587,331,329 2999 data329,328,1609,578,834,324,322,327,585,1602,0 3000 data567,566,567,304,306,308,310,1591,567,311,310,567 3010 data306,304,299,308,304,171,176,306,291,551,306,308 3020 data310,308,310,306,295,297,299,304,1586,562,567,310,315,311 3030 data308,313,297,1586,567,560,311,309,308,309,306,308 3999 data1577,299,295,306,310,311,304,562,546,1575,0 stop tok64 188 PROGRAMMING SOUND AND MUSIC ~ Here is a line,-by-line explanation of Example Program 2. For now, we are interested in how the three voices are controlled. LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 2: +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 10 | Set S equal to start of sound chip and clear all sound | | | chip registers. | | 20 | Dimension arrays to contain activity of song, 1/16th of a | | | measure per location. | | 30 | Dimension array to contain base frequency for each note. | | 40 | Store waveform control byte for each voice. | | 50 | Set high pulse width for voice 2. | | | Set high frequency for filter cutoff. | | | Set resonance for filter and filter voice 3. | | 60 | Read in base frequency for each note. | | 100 | Begin decoding loop for each voice. | | 110 | Initialize pointer to activity array. | | 120 | Read coded note. | | 130 | If coded note is zero, then next voice. | | 140 | Set waveform controls to proper voice. | | | If silence, set waveform controls to 0. | | 150 | Decode duration and octave. | | 160 | Decode note. | | 170 | Get base frequency for this note. | | 180 | If highest octave, skip division loop. | | 190 | Divide base frequency by 2 appropriate number of times. | | 200 | Get high and low frequency bytes. | | 210 | If sixteenth note, set activity array: high frequency, low | | | frequency, and waveform control (voice on). | | 220 | For all but last beat of note, set activity array: high | | | frequency, low frequency, waveform control (voice on). | | 230 | For last beat of note, set activity array: high frequency, | | | low frequency, waveform control (voice off). | | 240 | Increment pointer to activity array. Get next note. | | 250 | If longer than before, reset number of activities. | | 260 | Go back for next voice. | | 500 | Set Attack/Decay for voice 1 (A=0, D=0). | | | Set Sustain/Release for voice 1 (S=15, R=0). | PROGRAMMING SOUND AND MUSIC 189 ~ +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 510 | Set Attack/Decay for voice 2 (A=5, D=5). | | | Set Sustain/Release for voice 2 (S=8, R=5). | | 520 | Set Attack/Decay for voice 3 (A=O, D=10). | | | Set Sustain/Release for voice 3 (S=12, R=5). | | 530 | Set volume 15, low-pass filtering. | | 540 | Start loop for every 1/16th of a measure. | | 550 | POKE low frequency from activity array for all voices. | | 560 | POKE high frequency from activity array for all voices. | | 570 | POKE waveform control from activity array for all voices. | | 580 | Timing loop for 1/16th of a measure and back for next | | | 1/16th measure. | | 590 | Pause, then turn off volume. | | 600-620 | Base frequency data. | |1000-1999| Voice 1 data. | |2000-2999| Voice 2 data. | |3000-3999| Voice 3 data. | +-----------------------------------------------------------------------+ The values used in the data statements were found by using the note table in Appendix E and the chart below: +-----------------+------------+ | NOTE TYPE | DURATION | +-----------------+------------+ | 1/16 | 128 | | 1/8 | 256 | | DOTTED 1/8 | 384 | | 1/4 | 512 | | 1/4+1/16 | 640 | | DOTTED 1/4 | 768 | | 1/2 | 1024 | | 1/2+1/16 | 1152 | | 1/2+1/8 | 1280 | | DOTTED 1/2 | 1536 | | WHOLE | 2048 | +-----------------+------------+ 190 PROGRAMMING SOUND AND MUSIC ~ The note number from the note table is added to the duration above. Then each note can be entered using only one number which is decoded by your program. This is only one method of coding note values. You may be able to come up with one with which you are more comfortable. The formula used here for encoding a note is as follows: 1) The duration (number of 1/16ths of a measure) is multiplied by 8. 2) The result of step 1 is added to the octave you've chosen (0-7). 3) The result of step 2 is then multiplied by 16. 4) Add your note choice (0-11) to the result of the operation in step 3. In other words: ((((D*8)+O)*16)+N) Where D = duration, O = octave, and N = note A silence is obtained by using the negative of the duration number (number of 1/16ths of a measure * 128). CONTROLLING MULTIPLE VOICES Once you have gotten used to using more than one voice, you will find that the timing of the three voices needs to be coordinated. This is ac- complished in this program by: 1) Divide each musical measure into 16 parts. 2) Store the events that occur in each 1/16th measure interval in three separate arrays. The high and low frequency bytes are calculated by dividing the fre- quencies of the highest octave by two (lines 180 and 190). The waveform control byte is a start signal for beginning a note or continuing a note that is already playing. It is a stop signal to end a note. The waveform choice is made once for each voice in line 40. Again, this is only one way to control multiple voices. You may come up with your own methods. However, you should now be able to take any piece of sheet music and figure out the notes for all three voices. PROGRAMMING SOUND AND MUSIC 191 ~ CHANGING WAVEFORMS The tonal quality of a sound is called the timbre. The timbre of a sound is determined primarily by its "waveform." If you remember the example of throwing a pebble into the water you know that the waves ripple evenly across the pond. These waves almost look like the first sound wave we're going to talk about, the sinusoidal wave, or sine wave for short (shown below). + + + + + + / \ / \ ./.......\......./.......\. \ / + + + To make what we're talking about a bit more practical, let's go back to the first example program to investigate different waveforms. The reason for this is that you can hear the changes more easily using only one voice. LOAD the first music program that you typed in earlier, from your DATASSETTE(TM) or disk, and RUN it again. That program is using the sawtooth waveform (shown here) + + + /| /| /| / | / | / | / | / | / | ./...|.../...|.../...|..... | / | / | / | / | / | / |/ |/ |/ + + + from the 6581 SID chip's sound generating device. Try changing the note start number in line 70 from 33 to 17 and the note stop number in line 90 from 32 to 16. Your program should now look like this: 192 PROGRAMMING SOUND AND MUSIC ~ EXAMPLE PROGRAM 3 (EXAMPLE 1 MODIFIED): start tok64 page193.prg 5 s=54272 10 forl=stos+24:pokel,0:next 20 pokes+5,9:pokes+6,0 30 pokes+24,15 40 readhf,lf,dr 50 ifhf<0thenend 60 pokes+1,hf:pokes,lf 70 pokes+4,17 80 fort=1todr:next 90 pokes+4,16:fort=1to50:next 100 goto40 110 data25,177,250,28,214,250 120 data25,177,250,25,177,250 130 data25,177,125,28,214,125 140 data32,94,750,25,177,250 150 data28,214,250,19,63,250 160 data19,63,250,19,63,250 170 data21,154,63,24,63,63 180 data25,177,250,24,63,125 190 data19,63,250,-1,-1,-1 stop tok64 Now RUN the program. Notice how the sound quality is different, less twangy, more hollow. That's because we changed the sawtooth waveform into a triangular waveform (shown left). The third musical waveform is called a variable pulse wave (shown right). + + +----+ +----+ +----+ | / \ / \ | | | | | | | / \ / \ | | | | | | | / \ / \ | | | | | | | ./.......\......./.......\. .|....|..|....|..|....|..|. \ / | | | | | | | \ / | | | | | | | \ / | | | | | | | + | +--+ +--+ +--+ <--> PULSE WIDTH PROGRAMMING SOUND AND MUSIC 193 ~ It is a rectangular wave and you determine the length of the pulse cycle by defining the proportion of the wave which will be high. This is accomplished for voice 1 by using registers 2 and 3: Register 2 is the low byte of the pulse width (Lpw = 0 through 255). Register 3 is the high 4 bits (Hpw = 0 through 15). Together these registers specify a 12-bit number for your pulse width, which you can determine by using the following formula: PWn = Hpw*256 + Lpw The pulse width is determined by the following equation: PWout = (PWn/40.95) % When PWn has a value of 2048, it will give you a square wave. That means that register 2 (Lpw) = 0 and register 3 (Hpw) = 8. Now try adding this line to your program: 15 POKES+3,8:POKES+2,0 Then change the start number in line 70 to 65 and the stop number in fine 90 to 64, and RUN the program. Now change the high pulse width (register 3 in line 15) from an 8 to a 1. Notice how dramatic the difference in sound quality is? The last waveform available to you is white noise (shown here). . . . . . . . . . . . . ........................... . . . . . . . . . . . It is used mostly for sound effects and such. To hear how it sounds, try changing the start number in line 70 to 129 and the stop number in line 90 to 128. UNDERSTANDING WAVEFORMS When a note is played, it consists of a sine wave oscillating at the fundamental frequency and the harmonics of that wave. 194 PROGRAMMING SOUND AND MUSIC ~ The fundamental frequency defines the overall pitch of the note. Harmonics are sine waves having frequencies which are integer multiples of the fundamental frequency. A sound wave is the fundamental frequency and all of the harmonics it takes to make up that sound. [THE PICTURE IS MISSING!] In musical theory let's say that the fundamental frequency is harmonic number 1. The second harmonic has a frequency twice the fundamental frequency, the third harmonic is three times the fundamental frequency, and so on. The amounts of each harmonic present in a note give it its timbre. An acoustic instrument, like a guitar or a violin, has a very compli- cated harmonic structure. In fact, the harmonic structure may vary as a single note is played. You have already played with the waveforms available in your Commodore music synthesizer. Now let's talk about how the harmonics work with the triangular, sawtooth, and rectangular waves. A triangular wave contains only odd harmonics. The amount of each harmonic present is proportional to the reciprocal of the square of the harmonic number. In other words harmonic number 3 is 1/9 quieter than harmonic number 1, because the harmonic 3 squared is 9 (3 X 3) and the reciprocal of 9 is 1/9. As you can see, there is a similarity in shape of a triangular wave to a sine wave oscillating at the fundamental frequency. Sawtooth waves contain all the harmonics. The amount of each harmonic present is proportional to the reciprocal of the harmonic number. For example, harmonic number 2 is 1/2 as loud as harmonic number 1. The square wave contains odd harmonics in proportion to the reciprocal of the harmonic number. Other rectangular waves have varying harmonic content. By changing the pulse width, the timbre of the sound of a rectangular wave can be varied tremendously. PROGRAMMING SOUND AND MUSIC 195 ~ By choosing carefully the waveform used, you can start with a harmonic structure that looks somewhat like the sound you want. To refine the sound, you can add another aspect of sound quality available on your Commodore 64 called filtering, which we'll discuss later in this section. THE ENVELOPE GENERATOR The volume of a musical tone changes from the moment you first hear it, all the way through until it dies out and you can't hear it anymore. When a note is first struck, it rises from zero volume to its peak volume. The rate at which this happens is called the ATTACK. Then, it fails from the peak to some middle-ranged volume. The rate at which the fall of the note occurs is called the DECAY. The mid-ranged volume itself is called the SUSTAIN level. And finally, when the note stops playing, it fails from the SUSTAIN level to zero volume. The rate at which it fails is called the RELEASE. Here is a sketch of the four phases of a note: + / \ / \ / \ SUSTAIN LEVEL . ./. . . .+--------+ / \ / \ / \ | | | | | | A | D | S | R | Each of the items mentioned above give certain qualities and restric- tions to a note. The bounds are called parameters. The parameters ATTACK/DECAY/SUSTAIN/RELEASE and collectively called ADSR, can be controlled by your use of another set of locations in the sound generator chip. LOAD your first example program again. RUN it again and remember how it sounds. Then, changing line 20 so the program is like this: 196 PROGRAMMING SOUND AND MUSIC ~ EXAMPLE PRO6RAM 4 (EXAMPLE 1 MODIFIED): start tok64 page197.prg 5 s=54272 10 forl=stos+24:pokel,0:next 20 pokes+5,88:pokes+6,195 30 pokes+24,15 40 readhf,lf,dr 50 ifhf<0thenend 60 pokes+1,hf:pokes,lf 70 pokes+4,33 80 fort=1todr:next 90 pokes+4,32:fort=1to50:next 100 goto40 110 data25,177,250,28,214,250 120 data25,177,250,25,177,250 130 data25,177,125,28,214,125 140 data32,94,750,25,177,250 150 data28,214,250,19,63,250 160 data19,63,250,19,63,250 170 data21,154,63,24,63,63 180 data25,177,250,24,63,125 190 data19,63,250,-1,-1,-1 stop tok64 Registers 5 and 6 define the ADSR for voice 1. The ATTACK is the high nybble of register 5. Nybble is half a byte, in other words the lower 4 or higher 4 on/off locations (bits) in each register. DECAY is the low nybble. You can pick any number 0 through 15 for ATTACK, multiply it by 16 and add to any number 0 through 15 for DECAY. The values that correspond to these numbers are listed below. SUSTAIN level is the high nybble of register 6. It can be 0 through 15. It defines the proportion of the peak volume that the SUSTAIN level will be. RELEASE rate is the low nybble of register 6. PROGRAMMING SOUND AND MUSIC 197 ~ Here are the meanings of the values for ATTACK, DECAY, and RELEASE: +-----+------------------------+--------------------------------+ |VALUE|ATTACK RATE (TIME/CYCLE)| DECAY/RELEASE RATE (TIME/CYCLE)| +-----+------------------------+--------------------------------+ | 0 | 2 ms | 6 ms | | 1 | 8 ms | 24 ms | | 2 | 16 ms | 48 ms | | 3 | 24 ms | 72 ms | | 4 | 38 ms | 114 ms | | 5 | 56 ms | 168 ms | | 6 | 68 ms | 204 ms | | 7 | 80 ms | 240 ms | | 8 | 100 ms | 300 ms | | 9 | 250 ms | 750 ms | | 10 | 500 ms | 1.5 s | | 11 | 800 ms | 2.4 s | | 12 | 1 s | 3 s | | 13 | 3 s | 9 s | | 14 | 5 s | 15 s | | 15 | 8 s | 24 s | +-----+------------------------+--------------------------------+ Here are a few sample settings to try in your example program. Try these and a few of your own. The variety of sounds you can produce is astounding! For a violin type sound, try changing line 20 to read: 20 POKES+5,88:POKES+6,89:REM A=5;D=8;S=5;R=9 Change the waveform to triangle and get a xylophone type sound by using these lines: 20 POKES+5,9:POKES+6,9:REM A=0;D=9;S=O;R=9 70 POKES+4,17 90 POKES+4,16:FORT=1TO50:NEXT 198 PROGRAMMING SOUND AND MUSIC ~ Change the waveform to square and try a piano type sound with these lines: 15 POKES+3,8:POKES+2,0 20 POKES+5,9:POKES+6,0: REM A=0;D=9;S=0;R=0 70 POKES+4,65 90 POKES+4,64:FORT=1TO50:NEXT The most exciting sounds are those unique to the music synthesizer itself, ones that do not attempt to mimic acoustic instruments. For example try: 20 POKES+5,144:POKES+6,243:REM A=9;D=O; S=15;R=3 FILTERING The harmonic content of a waveform can be changed by using a filter. The SID chip is equipped with three types of filtering. They can be used separately or in combination with one another. Let's go back to the sample program you've been using to play with a simple example that uses a filter. There are several filter controls to set. You add line 15 in the program to set the cutoff frequency of the filter. The cutoff frequency is the reference point for the filter. You SET the high and low frequency cutoff points in registers 21 and 22. To turn ON the filter for voice 1, POKE register 23. Next change line 30 to show that a high-pass filter will be used (see the SID register map). PROGRAMMING SOUND AND MUSIC 199 ~ EXAMPLE PROGRAM 5 (EXAMPLE 1 MODIFIED): start tok64 page200.prg 5 s=54272 10 forl=stos+24:pokel,0:next 15 pokes+22,128:pokes+21,0:pokes+23,1 20 pokes+5,9:pokes+6,0 30 pokes+24,79 40 readhf,lf,dr 50 ifhf<0thenend 60 pokes+1,hf:pokes,lf 70 pokes+4,33 80 fort=1todr:next 90 pokes+4,32:fort=1to50:next 100 goto40 110 data25,177,250,28,214,250 120 data25,177,250,25,177,250 130 data25,177,125,28,214,125 140 data32,94,750,25,177,250 150 data28,214,250,19,63,250 160 data19,63,250,19,63,250 170 data21,154,63,24,63,63 180 data25,177,250,24,63,125 190 data19,63,250,-1,-1,-1 stop tok64 Try RUNning the program now. Notice the lower tones have had their volume cut down. It makes the overall quality of the note sound tinny. This is because you are using a high-pass filter which attenuates (cuts down the level of) frequencies below the specified cutoff frequency. There are three types of filters in your Commodore computer's SID chip. We have been using the high-pass filter. It will pass all the frequencies at or above the cutoff, while attenuating the frequencies below the cutoff. | AMOUNT | +----- PASSED | / | / | / +------|------- FREQUENCY 200 PROGRAMMING SOUND AND MUSIC ~ The SID chip also has a low-pass filter. As its name implies, this filter will pass the frequencies below cutoff and attenuate those above. | AMOUNT | -----+ PASSED | \ | \ | \ +------|------- FREQUENCY Finally, the chip is equipped with a bandpass filter, which passes a narrow band of frequencies around the cutoff, and attenuates all others. | AMOUNT | + PASSED | / \ | / \ | / \ +------|------- FREQUENCY The high- and low-pass filters can be combined to form a notch reject filter which passes frequencies away from the cutoff while attenuating at the cutoff frequency. | AMOUNT | --+ +--- PASSED | \ / | \ / | + +------|------- FREQUENCY PROGRAMMING SOUND AND MUSIC 201 ~ Register 24 determines which type filter you want to use. This is in addition to register 24's function as the overall volume control. Bit 6 controls the high-pass filter (0 = off, 1 = on), bit 5 is the bandpass filter, and bit 4 is the low-pass filter. The low 3 bits of the cutoff frequency are determined by register 21 (Lcf) (Lcf = 0 through 7). While the 8 bits of the high cutoff frequency are determined by register 22 (Hcf) (Hcf = 0 through 255). Through careful use of filtering, you can change the harmonic structure of any waveform to get just the sound you want. In addition, changing the filtering of a sound as it goes through the ADSR phases of its life can produce interesting effects. ADVANCED TECHNIQUES The SID chip's parameters can be changed dynamically during a note or sound to create many interesting and fun effects. In order to make this easy to do, digitized outputs from oscillator three and envelope generator three are available for you in registers 27 and 28, respec- tively. The output of oscillator 3 (register 27) is directly related to the waveform selected. If you choose the sawtooth waveform of oscillator 3, this register will present a series of numbers incremented (increased step by step) from 0 to 255 at a rate determined by the frequency of oscillator 3. If you choose the triangle waveform, the output will incre- ment from 0 up to 255, then decrement (decrease step by step) back down to 0. If you choose the pulse wave, the output will jump back-and-forth between 0 and 255. Finally, choosing the noise waveform will give you a series of random numbers. When oscillator 3 is used for modulation, you usually do NOT want to hear its output. Setting bit 7 of register 24 turns the audio output of voice 3 off. Register 27 always reflects the changing output of the oscillator and is not affected in any way by the envelope (ADSR) generator. 202 PROGRAMMING SOUND AND MUSIC ~ Register 25 gives you access to the output of the envelope generator of oscillator 3. It functions in much the same fashion that the output of oscillator 3 does. The oscillator must be turned on to produce any output from this register. Vibrato (a rapid variation in frequency) can be achieved by adding the output of oscillator 3 to the frequency of another oscillator. Example Program 6 illustrates this idea. EXAMPLE PROGRAM 6: start tok64 page203.prg 10 s=54272 20 forl=0to24:pokes+l,0:next 30 pokes+3,8 40 pokes+5,41:pokes+6,89 50 pokes+14,117 60 pokes+18,16 70 pokes+24,143 80 readfr,dr 90 iffr=0thenend 100 pokes+4,65 110 fort=1todr*2 120 fq=fr+peek(s+27)/2 130 hf=int(fq/256):lf=lqand255 140 pokes+0,lf:pokes+1,hf 150 next 160 pokes+4,64 170 goto80 500 data4817,2,5103,2,5407,2 510 data8583,4,5407,2,8583,4 520 data5407,4,8583,12,9634,2 530 data10207,2,10814,2,8583,2 540 data9634,4,10814,2,8583,2 550 data8583,12 560 data0,0 stop tok64 PROGRAMMING SOUND AND MUSIC 203 ~ LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 6: +----------+------------------------------------------------------------+ | Lines(s) | Description | +----------+------------------------------------------------------------+ | 10 | Set S to beginning of sound chip. | | 20 | Clear all sound chip locations. | | 30 | Set high pulse width for voice 1. | | 40 | Set Attack/Decay for voice 1 (A=2, D=9). | | | Set Sustain/Release for voice 1 (S=5, R=9). | | 50 | Set low frequency for voice 3. | | 60 | Set triangle waveform for voice 3. | | 70 | Set volume 15, turn off audio output of voice 3. | | 80 | Read frequency and duration of note. | | 90 | If frequency equals zero, stop. | | 100 | POKE start pulse waveform control voice 1. | | 110 | Start timing loop for duration. | | 120 | Get new frequency using oscillator 3 output. | | 130 | Get high and low frequency. | | 140 | POKE high and low frequency for voice 1. | | 150 | End of timing loop. | | 160 | POKE stop pulse waveform control voice 1. | | 170 | Go back for next note. | | 500-550 | Frequencies and durations for song, | | 560 | Zeros signal end of song. | +----------+------------------------------------------------------------+ A wide variety of sound effects can also be achieved using dynamic effects. For example, the following siren program dynamically changes the frequency output of oscillator 1 when it's based on the output of oscillator 3's triangular wave: 204 PROGRAMMING SOUND AND MUSIC ~ EXAMPLE PROGRAM 7: start tok64 page205.prg 10 s=54272 20 forl=0to24:pokes+l,0:next 30 pokes+14,5 40 pokes+18,16 50 pokes+3,1 60 pokes+24,143 70 pokes+6,240 80 pokes+4,65 90 fr=5389 100 fort=1to200 110 fq=fr+peek(s+27)*3.5 120 hf=int(fq/256):lf=fq-hf*256 130 pokes+0,lf:pokes+1,hf 140 next 150 pokes+24,0 stop tok64 LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 7: +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 10 | Set S to start of sound chip. | | 20 | Clear sound chip registers. | | 30 | Set low frequency of voice 3. | | 40 | Set triangular waveform voice 3. | | 50 | Set high pulse width for voice 1. | | 60 | Set volume 15, turn off audio output of voice 3. | | 70 | Set Sustain/Release for voice I (S=15, R=0). | | 80 | POKE start pulse waveform control voice 1. | | 90 | Set lowest frequency for siren. | | 100 | Begin timing loop. | | 110 | Get new frequency using output of oscillator 3. | | 120 | Get high and low frequencies. | | 130 | POKE high.and low frequencies for voice 1. | | 140 | End timing loop. | | 150 | Turn off volume. | +---------+-------------------------------------------------------------+ PROGRAMMING SOUND AND MUSIC 205 ~ The noise waveform can be used to provide a wide range of sound effects. This example mimics a hand clap using a filtered noise waveform: EXAMPLE PROGRAM 8: start tok64 page206.prg 10 s=54272 20 forl=0to24:pokes+l,0:next 30 pokes+0,240:pokes+1,33 40 pokes+5,8 50 pokes+22,104 60 pokes+23,1 70 pokes+24,79 80 forn=1to15 90 pokes+4,129 100 fort=1to250:next:pokes+4,128 110 fort=1to30:next:next 120 pokes+24,0 stop tok64 LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 8: +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 10 | Set S to start of sound chip. | | 20 | Clear all sound chip registers. | | 30 | Set high and low frequencies for voice 1. | | 40 | Set Attack/Decay for voice I (A=0, D=8). | | 50 | Set high cutoff frequency for filter. | | 60 | Turn on filter for voice 1. | | 70 | Set volume 15, high-pass filter. | | 80 | Count 15 claps. | | 90 | Set start noise waveform control. | | 100 | Wait, then set stop noise waveform control. | | 110 | Wait, then start next clap- | | 120 | Turn off volume. | +---------+-------------------------------------------------------------+ 206 PROGRAMMING SOUND AND MUSIC ~ SYNCHRONIZATION AND RING MODULATION The 6581 SID chip lets you create more complex harmonic structures through synchronization or ring modulation of two voices. The process of synchronization is basically a logical ANDing of two wave forms. When either is zero, the output is zero. The following example uses this process to create an imitation of a mosquito: EXAMPLE PROGRAM 9: start tok64 page207.prg 10 s=54272 20 forl=0to24:pokes+l,0:next 30 pokes+1,100 40 pokes+5,219 50 pokes+15,28 60 pokes+24,15 70 pokes+4,19 80 fort=1to5000:next 90 pokes+4,18 100 fort=1to1000:next:pokes+24,0 stop tok64 LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 9: +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 10 | Set S to start of sound chip. | | 20 | Clear sound chip registers. | | 30 | Set high frequency voice 1. | | 40 | Set Attack/Decay for voice 1 (A=13, D=11). | | 50 | Set high frequency voice 3. | | 60 | Set volume 15. | | 70 | Set start triangle, sync waveform control for voice 1. | | 80 | Timing loop. | | 90 | Set stop triangle, sync waveform control for voice 1. | | 100 | Wait, then turn off volume. | +-----------------------------------------------------------------------+ The synchronization feature is enabled (turned on) in line 70, where bits 0, 1, and 4 of register 4 are set. Bit 1 enables the syncing function between voice 1 and voice 3. Bits 0 and 4 have their usual functions of gating voice 1 and setting the triangular waveform. PROGRAMMING SOUND AND MUSIC 207 ~ Ring modulation (accomplished for voice 1 by setting bit 3 of register 4 in line 70 of the program below) replaces the triangular output of oscillator I with a "ring modulated" combination of oscillators 1 and 3. This produces non-harmonic overtone structures for use in mimicking bell or gong sounds. This program produces a clock chime imitation: EXAMPLE PROGRAM 10: start tok64 page208.prg 10 s=54272 20 forl=0to24:pokes+l,0:next 30 pokes+1,130 40 pokes+5,9 50 pokes+15,30 60 pokes+24,15 70 forl=1to12:pokes+4,21 80 fort=1to1000:next:pokes+4,20 90 fort=1to1000:next:next stop tok64 LINE-BY-LINE EXPLANATION OF EXAMPLE PROGRAM 10: +---------+-------------------------------------------------------------+ | Line(s) | Description | +---------+-------------------------------------------------------------+ | 10 | Set S to start of sound chip. | | 20 | Clear sound chip registers. | | 30 | Set high frequency for voice 1. | | 40 | Set Attack/Decay for voice 1 (A=0, D=9). | | 50 | Set high frequency for voice 3. | | 60 | Set volume 15. | | 70 | Count number of clings, set start triangle, ring mod | | | waveform control voice 1. | | 80 | Timing loop, set stop triangle, ring mod. | | 90 | Timing loop, next ding. | +---------+-------------------------------------------------------------+ The effects available through the use of the parameters of your Commodore 64's SID chip are numerous and varied. Only through ex- perimentation on your own will you fully appreciate the capabilities of your machine. The examples in this section of the Programmer's Reference Guide merely scratch the surface. Watch for the book MAKING MUSIC ON YOUR COMMODORE COMPUTER for everything from simple fun and games to professional-type musical instruction. 208 PROGRAMMING SOUND AND MUSIC ~ CHAPTER 5 BASIC TO MACHINE LANGUAGE o What Is Machine Language? o How Do You Write Machine Language Programs? o Hexadecimal Notation o Addressing Modes o Indexing o Subroutines o Useful Tips for the Beginner o Approaching a Large Task o MCS6510 Microprocessor Instruction Set o Memory Management on the Commodore 64 o The KERNAL o KERNAL Power-Up Activities o Using Machine Language From BASIC o Commodore 64 Memory Map 209 ~ WHAT IS MACHINE LANGUAGE? At the heart of every microcomputer, is a central microprocessor. It's a very special microchip which is the "brain" of the computer. The Commodore 64 is no exception. Every microprocessor understands its own language of instructions. These instructions are called machine language instructions. To put it more precisely, machine language is the ONLY programming language that your Commodore 64 understands. It is the NATIVE language of the machine. If machine language is the only language that the Commodore 64 understands, then how does it understand the CBM BASIC programming language? CBM BASIC is NOT the machine language of the Commodore 64. What, then, makes the Commodore 64 understand CBM BASIC instructions like PRINT and GOTO? To answer this question, you must first see what happens inside your Commodore 64. Apart from the microprocessor which is the brain of the Commodore 64, there is a machine language program which is stored in a special type of memory so that it can't be changed. And, more impor- tantly, it does not disappear when the Commodore 64 is turned off, unlike a program that you may have written. This machine language program is called the OPERATING SYSTEM of the Commodore 64. Your Commodore 64 knows what to do when it's turned on because its OPERATING SYSTEM (program) is automatically "RUN." 210 BASIC TO MACHINE LANGUAGE ~ The OPERATING SYSTEM is in charge of "organizing" all the memory in your machine for various tasks. It also looks at what characters you type on the keyboard and puts them onto the screen, plus a whole number of other functions. The OPERATING SYSTEM can be thought of as the "intelligence and personality" of the Commodore 64 (or any computer for that matter). So when you turn on your Commodore 64, the OPERATING SYSTEM takes control of your machine, and after it has done its housework, it then says: READY. The OPERATING SYSTEM of the Commodore 64 then allows you to type on the keyboard, and use the built-in SCREEN EDITOR on the Commodore 64. The SCREEN EDITOR allows you to move the cursor, DELete, INSert, etc., and is, in fact, only one part of the operating system that is built in for your convenience. All of the commands that are available in CBM BASIC are simply recognized by another huge machine language program built into your Commodore 64. This huge program "RUNS" the appropriate piece of machine language depending on which CBM BASIC command is being executed. This program is called the BASIC INTERPRETER, because it interprets each command, one by one, unless it encounters a command it does not understand, and then the familiar message appears: ?SYNTAX ERROR READY. WHAT DOES MACHINE CODE LOOK LIKE? You should be familiar with the PEEK and POKE commands in the CBM BASIC language for changing memory locations. You've probably used them for graphics on the screen, and for sound effects. Each memory location has its own number which identifies it. This number is known as the "address" of a memory location. If you imagine the memory in the Commodore 64 as a street of buildings, then the number on each door is, of course, the address. Now let's look at which parts of the street are used for what purposes. BASIC TO MACHINE LANGUAGE 211 ~ SIMPLE MEMORY MAP OF THE COMMODORE 64 +-------------+---------------------------------------------------------+ | ADDRESS | DESCRIPTION | +-------------+---------------------------------------------------------+ | | | | 0 & 1 | -6510 Registers. | | | | | 2 | -Start of memory. | | | | | 2-1023 | -Memory used by the operating system. | | | | | 1024-2039 | -Screen memory. | | | | | 2040-2047 | -SPRITE pointers. | | | | | 2048-40959 | -This is YOUR memory. This is where your BASIC or | | | machine language programs, or both, are stored. | | | | | 40960-49151 | -8K CBM BASIC Interpreter. | | | | | 49152-53247 | -Special programs RAM area. | | | | | 53248-53294 | -VIC-II. | | | | | 54272-55295 | -SID Registers. | | | | | 55296-56296 | -Color RAM. | | | | | 56320-57343 | -I/O Registers. (6526's) | | | | | 57344-65535 | -8K CBM KERNAL Operating System. | | | | +-------------+---------------------------------------------------------+ 212 BASIC TO MACHINE LANGUAGE ~ If you don't understand what the description of each part of memory means right now, this will become clear from other parts of this manual. Machine language programs consist of instructions which may or may not have operands (parameters) associated with them. Each instruction takes up one memory location, and any operand is contained in one or two locations following the instruction. In your BASIC programs, words like PRINT and GOTO do, in fact, only take up one memory location, rather than one for each character of the word. The contents of the location that represents a particular BASIC keyword is called a token. In machine language, there are different tokens for different instructions, which also take up just one byte (mem- ory location=byte). Machine language instructions are very simple. Therefore, each indi- vidual instruction cannot achieve a great deal. Machine language in- structions either change the contents of a memory location, or change one of the internal registers (special storage locations) inside the micro- processor. The internal registers form the very basis of machine lan- guage. THE REGISTERS INSIDE THE 6510 MICROPROCESSOR THE ACCUMULATOR This is THE most important register in the microprocessor. Various ma- chine language instructions allow you to copy the contents of a memory location into the accumulator, copy the contents of the accumulator into a memory location, modify the contents of the accumulator or some other register directly, without affecting any memory. And the accumulator is the only register that has instructions for performing math. THE X INDEX REGISTER This is a very important register. There are instructions for nearly all of the transformations you can make to the accumulator. But there are other instructions for things that only the X register can do. Various machine language instructions allow you to copy the contents of a memory location into the X register, copy the contents of the X register into a memory location, and modify the contents of the X, or some other register directly. BASIC TO MACHINE LANGUAGE 213 ~ THE Y INDEX REGISTER This is a very important register. There are instructions for nearly all of the transformations you can make to the accumulator, and the X register. But there are other instructions for things that only the Y register can do. Various machine language instructions allow you to copy the contents of a memory location into the Y register, copy the contents of the Y register into a memory location, and modify the contents of the Y, or some other register directly. THE STATUS REGISTER This register consists of eight "flags" (a flag = something that indi- cates whether something has, or has not occurred). THE PROGRAM COUNTER This contains the address of the current machine language instruction being executed. Since the operating system is always "RUN"ning in the Commodore 64 (or, for that matter, any computer), the program counter is always changing. It could only be stopped by halting the microprocessor in some way. THE STACK POINTER This register contains the location of the first empty place on the stack. The stack is used for temporary storage by machine language pro- grams, and by the computer. THE INPUT/OUTPUT PORT This register appears at memory locations 0 (for the DATA DIRECTION REGISTER) and 1 (for the actual PORT). It is an 8-bit input/output port. On the Commodore 64 this register is used for memory management, to allow the chip to control more than 64K of RAM and ROM memory. The details of these registers are not given here. They are explained as the principles needed to explain them are explained. HOW DO YOU WRITE MACHINE LANGUAGE PROGRAMS? Since machine language programs reside in memory, and there is no facility in your Commodore 64 for writing and editing machine language 214 BASIC TO MACHINE LANGUAGE ~ programs, you must use either a program to do this, or write for yourself a BASIC program that "allows" you to write machine language. The most common methods used to write machine language programs are assembler programs. These packages allow you to write machine language instructions in a standardized mnemonic format, which makes the machine language program a lot more readable than a stream of numbers! Let's review: A program that allows you to write machine language programs in mnemonic format is called an assembler. Incidentally, a program that displays a machine language program in mnemonic format is called a disassembler. Available for your Commodore 64 is a machine language monitor cartridge (with assembler/disassembler, etc.) made by Commodore: 64MON The 64MON cartridge available from your local dealer, is a program that allows you to escape from the world of CBM BASIC, into the land of machine language. It can display the contents of the internal registers in the 6510 microprocessor, and it allows you to display portions of mem- ory, and change them on the screen, using the screen editor. It also has a built-in assembler and disassembler, as well as many other features that allow you to write and edit machine language programs easily. You don't HAVE to use an assembler to write machine language, but the task is considerably easier with it. If you wish to write machine language programs, it is strongly suggested that you purchase an assembler of some sort. Without an assembler you will probably have to "POKE" the machine language program into memory, which is totally unadvisable. This manual will give its examples in the format that 64MON uses, from now on. Nearly all assembler formats are the same, therefore the machine language examples shown will almost certainly be compatible with any assembler. But before explaining any of the other features of 64MON, the hexadecimal numbering system must be explained. HEXADECIMAL NOTATION Hexadecimal notation is used by most machine language programmers when they talk about a number or address in a machine language program. Some assemblers let you refer to addresses and numbers in decimal (base 10), binary (base 2), or even octal (base 8) as well as hexadecimal BASIC TO MACHINE LANGUAGE 215 ~ (base 16) (or just "hex" as most people say). These assemblers do the conversions for you. Hexadecimal probably seems a little hard to grasp at first, but like most things, it won't take long to master with practice. By looking at decimal (base 10) numbers, you can see that each digit fails somewhere in the range between zero and a number equal to the base less one (e.g., 9). THIS IS TRUE OF ALL NUMBER BASES. Binary (base 2) numbers have digits ranging from zero to one (which is one less than the base). Similarly, hexadecimal numbers should have digits ranging from zero to fifteen, but we do not have any single digit figures for the numbers ten to fifteen, so the first six letters of the alphabet are used instead: +---------+-------------+----------+ | DECIMAL | HEXADECIMAL | BINARY | +---------+-------------+----------+ | 0 | 0 | 00000000 | | 1 | 1 | 00000001 | | 2 | 2 | 00000010 | | 3 | 3 | 00000011 | | 4 | 4 | 00000100 | | 5 | 5 | 00000101 | | 6 | 6 | 00000110 | | 7 | 7 | 00000111 | | 8 | 8 | 00001000 | | 9 | 9 | 00001001 | | 10 | A | 00001010 | | 11 | B | 00001011 | | 12 | C | 00001100 | | 13 | D | 00001101 | | 14 | E | 00001110 | | 15 | F | 00001111 | | 16 | 10 | 00010000 | +---------+-------------+----------+ 216 BASIC TO MACHINE LANGUAGE ~ Let's look at it another way; here's an example of how a base 10 (decimal number) is constructed: Base raised by increasing powers:... 10^3 10^2 10^1 10^0 --------------------- Equals:.............. 1000 100 10 1 --------------------- Consider 4569 (base 10) 4 5 6 9 = (4*1000)+(5*100)+(6*10)+9 Now look at an example of how a base 16 (hexadecimal number) is constructed: Base raised by increasing powers:... 16^3 16^2 16^1 16^0 --------------------- Equals:.............. 4096 256 16 1 --------------------- Consider 11D9 (base 16) 1 1 D 9 = 1*4096+1*256+13*16+9 Therefore, 4569 (base 10) = 11D9 (base 16) The range for addressable memory locations is 0-65535 (as was stated earlier). This range is therefore 0-FFFF in hexadecimal notation. Usually hexadecimal numbers are prefixed with a dollar sign ($). This is to distinguish them from decimal numbers. Let's look at some "hex" numbers, using 64MON, by displaying the contents of some memory by typing: SYS 8*4096 (or SYS 12*4096) B* PC SR AC XR YR SP .;0401 32 04 5E 00 F6 (these may be different) Then if you type in: .M 0000 0020 (and press ). you will see rows of 9 hex numbers. The first 4-digit number is the ad- dress of the first byte of memory being shown in that row, and the other eight numbers are the actual contents of the memory locations beginning at that start address. BASIC TO MACHINE LANGUAGE 217 ~ You should really try to learn to "think" in hexadecimal. It's not too difficult, because you don't have to think about converting it back into decimal. For example, if you said that a particular value is stored at $14ED instead of 5357, it shouldn't make any difference. YOUR FIRST MACHINE LANGUAGE INSTRUCTION LDA - LOAD THE ACCUMULATOR In 6510 assembly language, mnemonics are always three characters. LDA represents "load accumulator with...", and what the accumulator should be loaded with is decided by the parameter(s) associated with that instruction. The assembler knows which token is represented by each mnemonic, and when it "assembles" an instruction, it simply puts into memory (at whatever address has been specified), the token, and what parameters, are given. Some assemblers give error messages, or warnings when you try to assemble something that either the assembler, or the 6510 microprocessor, cannot do. If you put a "#" symbol in front of the parameter associated with the instruction, this means that you want the register specified in the instruction to be loaded with the "value" after the "#". For example: LDA #$05 <----[ $=HEX ] This instruction will put $05 (decimal 5) into the accumulator register. The assembler will put into the specified address for this instruction, $A9 (which is the token for this particular instruction, in this mode), and it will put $05 into the next location after the location containing the instruction ($A9). If the parameter to be used by an instruction has "#" before it; i.e., the parameter is a "value," rather than the contents of a memory loca- tion, or another register, the instruction is said to be in the "immediate" mode. To put this into perspective, let's compare this with another mode: If you want to put the contents of memory location $102E into the accumulator, you're using the "absolute" mode of instruction: LDA $102E The assembler can distinguish between the two different modes because the latter does not have a "#" before the parameter. The 6510 microprocessor 218 BASIC TO MACHINE LANGUAGE ~ can distinguish between the immediate mode, and the absolute mode of the LDA instruction, because they have slightly different tokens. LDA (immediate) has $A9 as its token, and LDA (absolute), has $AD as its token. The mnemonic representing an instruction usually implies what it does. For instance, if we consider another instruction, LDX, what do you think this does? If you said "load the X register with...", go to the top of the class. If you didn't, then don't worry, learning machine language does take patience, and cannot be learned in a day. The various internal registers can be thought of as special memory locations, because they too can hold one byte of information. It is not necessary for us to explain the binary numbering system (base 2) since it follows the same rules as outlined for hexadecimal and decimal outlined previously, but one "bit" is one binary digit and eight bits make up one byte! This means that the maximum number that can be contained in a byte is the largest number that an eight digit binary number can be. This number is 11111111 (binary), which equals $FF (hexadecimal), which equals 255 (decimal). You have probably wondered why only numbers from zero to 255 could be put into a memory location. If you try POKE 7680,260 (which is a BASIC statement that "says": "Put the number two hundred and sixty, into memory location seven thousand, six hundred and eighty", the BASIC interpreter knows that only numbers 0 - 255 can be put in a memory location, and your Commodore 64 will reply with: ?ILLEGAL QUANTITY ERROR READY. If the limit of one byte is $FF (hex), how is the address parameter in the absolute instruction "LDA $102E" expressed in memory? It's expressed in two bytes (it won't fit into one, of course). The lower (rightmost) two digits of the hexadecimal address form the "low byte" of the address, and the upper (leftmost) two digits form the "high byte." The 6510 requires any address to be specified with its low byte first, and then the high byte. This means that the instruction "LDA $102E" is represented in memory by the three consecutive values: $AD, $2E, $10 Now all you need to know is one more instruction and then you can write your first program. That instruction is BRK. For a full explanation of BASIC TO MACHINE LANGUAGE 219 ~ this I instruction, refer to M.O.S. 6502 Programming Manual. But right now, you can think of it as the END instruction in machine language. If we write a program with 64MON and put the BRK instruction at the end, then when the program is executed, it will return to 64MON when it is finished. This might not happen if there is a mistake in your program, or the BRK instruction is never reached (just like an END statement in BASIC may never get executed). This means that if the Commodore 64 didn't have a STOP key, you wouldn't be able to abort your BASIC programs! WRITING YOUR FIRST PROGRAM If you've used the POKE statement in BASIC to put characters onto the screen, you're aware that the character codes for POKEing are different from CBM ASCII character values. For example, if you enter: PRINT ASC("A") (and press ) the Commodore 64 will respond with: 65 READY. However, to put an "A" onto the screen by POKEing, the code is 1, enter: to clear the screen POKE 1024,1:POKE 55296,14 (and (1024 is the start of screen memory) The "P" in the POKE statement should now be an "A." Now let's try this in machine language. Type the following in 64MON: (Your cursor should be flashing alongside a "." right now.) .A 1400 LDA#$01 (and press ) 220 BASIC TO MACHINE LANGUAGE ~ The Commodore 64 will prompt you with: .A 1400 A9 01 LDA #$01 .A 1402 Type: .A 1402 STA $0400 (The STA instruction stores the contents of the accumulator in a specified memory location.) The Commodore 64 will prompt you with: .A 1405 Now type in: .A 1405 LDA #$0E .A 1407 STA $D800 .A 140A BRK Clear the screen, and type: G 1400 The G should turn into an "A" if you've done everything correctly. You have now written your first machine language program. Its purpose is to store one character ("A") at the first location in the screen memory. Having achieved this, we must now explore some of the other instructions, and principles. ADDRESSING MODES ZERO PAGE As shown earlier, absolute addresses are expressed in terms of a high and a low order byte. The high order byte is often referred to as the page of memory. For example, the address $1637 is in page $16 (22), and $0277 is in page $02 (2). There is, however, a special mode of addressing known as zero page addressing and is, as the name implies, associated BASIC TO MACHINE LANGUAGE 221 ~ with the addressing of memory locations in page zero. These addresses, therefore, ALWAYS have a high order byte of zero. The zero page mode of addressing only expects one byte to describe the address, rather than two when using an absolute address. The zero page addressing mode tells the microprocessor to assume that the high order address is zero. Therefore zero page addressing can reference memory locations whose addresses are between $0000 and $00FF. This may not seem too important at the moment, but you'll need the principles of zero page addressing soon. THE STACK The 6510 microprocessor has what is known as a stack. This is used by both the programmer and the microprocessor to temporarily remember things, and to remember, for example, an order of events. The GOSUB statement in BASIC, which allows the programmer to call a subroutine, must remember where it is being called from, so that when the RETURN statement is executed in the subroutine, the BASIC interpreter "knows" where to go back to continue executing. When a GOSUB statement is encountered in a program by the BASIC interpreter, the BASIC interpreter "pushes" its current position onto the stack before going to do the subroutine, and when a RETURN is executed, the interpreter "pulls" off the stack the information that tells it where it was before the subroutine call was made. The interpreter uses instructions like PHA, which pushes the contents of the accumulator onto the stack, and PLA (the reverse) which pulls a value off the stack and into the accumulator. The status register can also be pushed and pulled with the PHP and PLP, respectively. The stack is 256 bytes long, and is located in page one of memory. It is therefore from $01 00 to $01 FF. It is organized backwards in memory. In other words, the first position in the stack is at $01 FF, and the last is at $0100. Another register in the 651 0 microprocessor is called the stack pointer, and it always points to the next available location in the stack. When something is pushed onto the stack, it is placed where the stack pointer points to, and the stack pointer is moved down to the next position (decremented). When something is pulled off the stack, the stack pointer is incremented, and the byte pointed to by the stack pointer is placed into the specified register. 222 BASIC TO MACHINE LANGUAGE ~ Up to this point, we have covered immediate, zero page, and absolute mode instructions. We have also covered, but have not really talked about, the "implied" mode. The implied mode means that information is implied by an instruction itself. In other words, what registers, flags, and memory the instruction is referring to. The examples we have seen are PHA, PLA, PHP, and PLP, which refer to stack processing and the accumulator and status registers, respectively. +-----------------------------------------------------------------------+ | NOTE: The X register will be referred to as X from now on, and | | similarly A (accumulator), Y (Y index register), S (stack pointer), | | and P (processor status). | +-----------------------------------------------------------------------+ INDEXING Indexing plays an extremely important part in the running of the 6510 microprocessor. It can be defined as "creating an actual address from a base address plus the contents of either the X or Y index registers." For example, if X contains $05, and the microprocessor executes an LDA instruction in the "absolute X indexed mode" with base address (e.g., $9000), then the actual location that is loaded into the A register is $9000 + $05 = $9005. The mnemonic format of an absolute indexed instruction is the same as an absolute instruction except a ",X" or ",Y" denoting the index is added to the address. EXAMPLE: LDA $9000,X There are absolute indexed, zero page indexed, indirect indexed, and indexed indirect modes of addressing available on the 6510 microprocessor. INDIRECT INDEXED This only allows usage of the Y register as the index. The actual ad- dress can only be in zero page, and the mode of instruction is called indirect because the zero page address specified in the instruction con- tains the low byte of the actual address, and the next byte to it contains the high order byte. BASIC TO MACHINE LANGUAGE 223 ~ EXAMPLE: Let us suppose that location $02 contains $45, and location $03 con- tains $1E. If the instruction to load the accumulator in the indirect indexed mode is executed and the specified zero page address is $02, then the actual address will be: Low order = contents of $02 High order = contents of $03 Y register = $00 Thus the actual address = $1E45 + Y = $1E45. The title of this mode does in fact imply an indirect principle, although this may be difficult to grasp at first sight. Let's look at it another way: "I am going to deliver this letter to the post office at address $02, MEMORY ST., and the address on the letter is $05 houses past $1600, MEMORY street." This is equivalent to the code: LDA #$00 - load low order actual base address STA $02 - set the low byte of the indirect address LDA #$16 - load high order indirect address STA $03 - set the high byte of the indirect address LDY #$05 - set the indirect index (Y) LDA ($02),Y - load indirectly indexed by Y INDEXED INDIRECT Indexed indirect only allows usage of the X register as the index. This is the some as indirect indexed, except it is the zero page address of the pointer that is indexed, rather than the actual base address. Therefore, the actual base address IS the actual address because the index has already been used for the indirect. Index indirect would also be used if a table of indirect pointers were located in zero page memory, and the X register could then specify which indirect pointer to use. 224 BASIC TO MACHINE LANGUAGE ~ EXAMPLE: Let us suppose that location $02 contains $45, and location $03 con- tains $10. If the instruction to load the accumulator in the indexed indirect mode is executed and the specified zero page address is $02, then the actual address will be: Low order = contents of ($02+X) High order = contents of ($03+X) X register = $00 Thus the actual pointer is in = $02 + X = $02. Therefore, the actual address is the indirect address contained in $02 which is again $1045. The title of this mode does in fact imply the principle, although it may be difficult to grasp at first sight. Look at it this way: "I am going to deliver this letter to the fourth post office at address $01,MEMORY ST., and the address on the letter will then be delivered to $1600, MEMORY street." This is equivalent to the code: LDA #$00 - load low order actual base address STA $06 - set the low byte of the indirect address LDA #$16 - load high order indirect address STA $07 - set the high byte of the indirect address LDX #$05 - set the indirect index (X) LDA ($02,X) - load indirectly indexed by X +-----------------------------------------------------------------------+ | NOTE: Of the two indirect methods of addressing, the first (indirect | | indexed) is far more widely used. | +-----------------------------------------------------------------------+ BASIC TO MACHINE LANGUAGE 225 ~ BRANCHES AND TESTING Another very important principle in machine language is the ability to test, and detect certain conditions, in a similar fashion to the "IF... THEN, IF... GOTO" structure in CBM BASIC. The various flags in the status register are affected by different in- structions in different ways. For example, there is a flag that is set when an instruction has caused a zero result, and is reset when a result is not zero. The instruction: LDA #$00 will cause the zero result flag to be set, because the instruction has resulted in the accumulator containing a zero. There are a set of instructions that will, given a particular condition, branch to another part of the program. An example of a branch instruction is BEQ, which means Branch if result EQual to zero. The branch instructions branch if the condition is true, and if not, the program continues onto the next instruction, as if nothing had occurred. The branch instructions branch not by the result of the previous instructions), but by internally examining the status register. As was just mentioned, there is a zero result flag in the status register. The BEQ instruction branches if the zero result flag (known as Z) is set. Every branch instruction has an opposite branch instruction. The BEQ instruction has an opposite instruction BNE, which means Branch on result Not Equal to zero (i.e., Z not set). The index registers have a number of associated instructions which modify their contents. For example, the INX instruction INcrements the X index register. If the X register contained $FF before it was incremented (the maximum number the X register can contain), it will "wrap around" back to zero. If you wanted a program to continue to do something until you had performed the increment of the X index that pushed it around to zero, you could use the BNE instruction to continue "looping" around, until X became zero. The reverse of INX, is DEX, which is DEcrement the X index register. If the X index register is zero, DEX wraps around to $FF. Similarly, there are INY and DEY for the Y index register. 226 BASIC TO MACHINE LANGUAGE ~ But what if a program didn't want to wait until X or Y had reached (or not reached) zero? Well there are comparison instructions, CPX and CPY, which allow the machine language programmer to test the index registers with specific values, or even the contents of memory locations. If you wanted to see if the X register contained $40, you would use the instruction: CPX #$40 - compare X with the "value" $40. BEQ - branch to somewhere else in the (some other program, if this condition is "true." part of the program) The compare, and branch instructions play a major part in any machine language program. The operand specified in a branch instruction when using 64MON is the address of the part of the program that the branch goes to when the proper conditions are met. However, the operand is only an offset, which gets you from where the program currently is to the address specified. This offset is just one byte, and therefore the range that a branch instruction can branch to is limited. It can branch from 128 bytes back- ward, to 127 bytes forward. +-----------------------------------------------------------------------+ | NOTE: This is a total range of 255 bytes which is, of course, the | | maximum range of values one byte can contain. | +-----------------------------------------------------------------------+ 64MON will tell you if you "branch out of range" by refusing to "as- semble" that particular instruction. But don't worry about that now be- cause it's unlikely that you will have such branches for quite a while. The branch is a "quick" instruction by machine language standards because of the "offset" principle as opposed to an absolute address. 64MON allows you to type in an absolute address, and it calculates the correct offset. This is just one of the "comforts" of using an assembler. +-----------------------------------------------------------------------+ | NOTE: It is NOT possible to cover every single branch instruction. For| | further information, refer to the Bibliography section in Appendix F. | +-----------------------------------------------------------------------+ BASIC TO MACHINE LANGUAGE 227 ~ SUBROUTINES In machine language (in the same way as using BASIC), you can call subroutines. The instruction to call a subroutine is JSR (Jump to Sub- Routine), followed by the specified absolute address. Incorporated in the operating system, there is a machine language subroutine that will PRINT a character to the screen. The CBM ASCII code of the character should be in the accumulator before calling the subroutine. The address of this subroutine is $FFD2. Therefore, to print "Hi" to the screen, the following program should be entered: .A 1400 LDA #$48 - load the CBM ASCII code of "H" .A 1402 JSR $FFD2 - print it .A 1405 LDA #$49 - load the CBM ASCII code of "I" .A 1407 JSR $FFD2 - print that too .A 140A LDA #$0D - print a carriage return as well .A 140C JSR $FFD2 .A 140F BRK - return to 64MON .G 1400 - will print "HI" and return to 64MON The "PRINT a character" routine we have just used is part of the KERNAL jump table. The instruction similar to GOTO in BASIC is JMP, which means JUMP to the specified absolute address. The KERNAL is a long list of "standardized" subroutines that control ALL input and output of the Commodore 64. Each entry in the KERNAL JMPs to a subroutine in the operating system. This "jump table" is found between memory locations $FF84 to $FFF5 in the operating system. A full explanation of the KERNAL is available in the "KERNAL Reference Section" of this manual. However, certain routines are used here to show how easy and effective the KERNAL is. Let's now use the new principles you've just learned in another pro- gram. It will help you to put the instructions into context: 228 BASIC TO MACHINE LANGUAGE ~ This program. will display the alphabet using a KERNAL routine. The only new instruction introduced here is TXA Transfer the contents of the X index register, into the Accumulator. .A 1400 LDX #$41 - X = CBM ASCII of "A" .A 1402 TXA - A = X .A 1403 JSR $FFD2 - print character .A 1406 INX - bump count .A 1407 CPX #$5B - have we gone past "Z"? .A 1409 BNE $1402 - no, go back and do more .A 140B BRK - yes, return to 64MON To see the Commodore 64 print the alphabet, type the familiar command: .G 1400 The comments that are beside the program, explain the program flow and logic. If you are writing a program, write it on paper first, and then test it in small parts if possible. USEFUL TIPS FOR THE BEGINNER One of the best ways to learn machine language is to look at other peoples' machine language programs. These are published all the time in magazines and newsletters. Look at them even if the article is for a different computer, which also uses the 6510 (or 6502) microprocessor. You should make sure that you thoroughly understand the code that you look at. This will require perseveres I ce, especially when you see a new technique that you have never come across before. This can be infuriat- ing, but if patience prevails, you will be the victor. Having looked at other machine language programs, you MUST write your own. These may be utilities for your BASIC programs, or they may be an all machine language program. BASIC TO MACHINE LANGUAGE 229 ~ You should also use the utilities that are available, either IN your computer, or in a program, that aid you in writing, editing, or tracking down errors in a machine language program. An example would be the KERNAL, which allows you to check the keyboard, print text, control peripheral devices like disk drives, printers, modems, etc., manage memory and the screen. It is extremely powerful and it is advised strongly that it is used (refer to KERNAL section, Page 268). Advantages of writing programs in machine language: 1. Speed - Machine language is hundreds, and in some cases thousands of times faster than a high level language such as BASIC. 2. Tightness - A machine language program can be made totally "watertight," i.e., the user can be made to do ONLY what the program allows, and no more. With a high level language, you are relying on the user not "crashing" the BASIC interpreter by entering, for example, a zero which later causes a: ?DIVISION BY ZERO ERROR IN LINE 830 READY. In essence, the computer can only be maximized by the machine language programmer. APPROACHING A LARGE TASK When approaching a large task in machine language, a certain amount of subconscious thought has usually taken place. You think about how certain processes are carried out in machine language. When the task is started, it is usually a good idea to write it out on paper. Use block diagrams of memory usage, functional modules of code required, and a program flow. Let's say that you wanted to write a roulette game in machine language. You could outline it something like this: 230 BASIC TO MACHINE LANGUAGE ~ o Display title o Ask if player requires instructions o YES - display them-Go to START o NO - Go to START o START Initialize everything o MAIN display roulette table o Take in bets o Spin wheel o Slow wheel to stop o Check bets with result o Inform player o Player any money left? o YES - Go to MAIN o NO - Inform user!, and go to START This is the main outline. As each module is approached, you can break it down further. If you look at a large indigestable problem as something that can be broken down into small enough pieces to be eaten, then you'll be able to approach something that seems impossible, and have it all fall into place. This process only improves with practice, so KEEP TRYING. BASIC TO MACHINE LANGUAGE 231 ~ +------------------------------------------------------------------------ | | MCS6510 MICROPROCESSOR INSTRUCTION SET - ALPHABETIC SEQUENCE | +------------------------------------------------------------------------ | | ADC Add Memory to Accumulator with Carry | AND "AND" Memory with Accumulator | ASL Shift Left One Bit (Memory or Accumulator) | | BCC Branch on Carry Clear | BCS Branch on Carry Set | BEQ Branch on Result Zero | BIT Test Bits in Memory with Accumulator | BMI Branch on Result Minus | BNE Branch on Result not Zero | BPL Branch on Result Plus | BRK Force Break | BVC Branch on Overflow Clear | BVS Branch on Overflow Set | | CLC Clear Carry Flag | CLD Clear Decimal Mode | CLI Clear interrupt Disable Bit | CLV Clear Overflow Flag | CMP Compare Memory and Accumulator | CPX Compare Memory and Index X | CPY Compare Memory and Index Y | | DEC Decrement Memory by One | DEX Decrement Index X by One | DEY Decrement Index Y by One | | EOR "Exclusive-Or" Memory with Accumulator | | INC Increment Memory by One | INX Increment Index X by One | INY Increment Index Y by One | | JMP Jump to New Location | +------------------------------------------------------------------------ 232 BASIC TO MACHINE LANGUAGE ~ ------------------------------------------------------------------------+ | MCS6510 MICROPROCESSOR INSTRUCTION SET - ALPHABETIC SEQUENCE | | ------------------------------------------------------------------------+ | JSR Jump to New Location Saving Return Address | | LDA Load Accumulator with Memory | LDX Load Index X with Memory | LDY Load Index Y with Memory | LSR Shift Right One Bit (Memory or Accumulator) | | NOP No Operation | | ORA "OR" Memory with Accumulator | | PHA Push Accumulator on Stack | PHP Push Processor Status on Stack | PLA Pull Accumulator from Stack | PLP Pull Processor Status from Stack | | ROL Rotate One Bit Left (Memory or Accumulator) | ROR Rotate One Bit Right (Memory or Accumulator) | RTI Return from Interrupt | RTS Return from Subroutine | | SBC Subtract Memory from Accumulator with Borrow | SEC Set Carry Flag | SED Set Decimal Mode | SEI Set Interrupt Disable Status | STA Store Accumulator in Memory | STX Store Index X in Memory | STY Store Index Y in Memory | | TAX Transfer Accumulator to Index X | TAY Transfer Accumulator to Index Y | TSX Transfer Stack Pointer to Index X | TXA Transfer Index X to Accumulator | TXS Transfer Index X to Stack Pointer | TYA Transfer Index Y to Accumulator | ------------------------------------------------------------------------+ BASIC TO MACHINE LANGUAGE 233 ~ The following notation applies to this summary: A Accumulator EOR Logical Exclusive Or X, Y Index Registers fromS Transfer from Stack M Memory toS Transfer to Stack P Processor Status Register -> Transfer to S Stack Pointer <- Transfer from / Change V Logical OR _ No Change PC Program Counter + Add PCH Program Counter High /\ Logical AND PCL Program Counter Low - Subtract OPER OPERAND # IMMEDIATE ADDRESSING MODE Note: At the top of each table is located in parentheses a reference number (Ref: XX) which directs the user to that Section in the MCS6500 Microcomputer Family Programming Manual in which the instruction is defined and discussed. 234 BASIC TO MACHINE LANGUAGE ~ ADC Add memory to accumulator with carry ADC Operation: A + M + C -> A, C N Z C I D V / / / _ _ / (Ref: 2.2.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | ADC #Oper | 69 | 2 | 2 | | Zero Page | ADC Oper | 65 | 2 | 3 | | Zero Page,X | ADC Oper,X | 75 | 2 | 4 | | Absolute | ADC Oper | 60 | 3 | 4 | | Absolute,X | ADC Oper,X | 70 | 3 | 4* | | Absolute,Y | ADC Oper,Y | 79 | 3 | 4* | | (Indirect,X) | ADC (Oper,X) | 61 | 2 | 6 | | (Indirect),Y | ADC (Oper),Y | 71 | 2 | 5* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if page boundary is crossed. AND "AND" memory with accumulator AND Operation: A /\ M -> A N Z C I D V / / _ _ _ _ (Ref: 2.2.3.0) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | AND #Oper | 29 | 2 | 2 | | Zero Page | AND Oper | 25 | 2 | 3 | | Zero Page,X | AND Oper,X | 35 | 2 | 4 | | Absolute | AND Oper | 2D | 3 | 4 | | Absolute,X | AND Oper,X | 3D | 3 | 4* | | Absolute,Y | AND Oper,Y | 39 | 3 | 4* | | (Indirect,X) | AND (Oper,X) | 21 | 2 | 6 | | (Indirect,Y) | AND (Oper),Y | 31 | 2 | 5 | +----------------+-----------------------+---------+---------+----------+ * Add 1 if page boundary is crossed. BASIC TO MACHINE LANGUAGE 235 ~ ASL ASL Shift Left One Bit (Memory or Accumulator) ASL +-+-+-+-+-+-+-+-+ Operation: C <- |7|6|5|4|3|2|1|0| <- 0 +-+-+-+-+-+-+-+-+ N Z C I D V / / / _ _ _ (Ref: 10.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Accumulator | ASL A | 0A | 1 | 2 | | Zero Page | ASL Oper | 06 | 2 | 5 | | Zero Page,X | ASL Oper,X | 16 | 2 | 6 | | Absolute | ASL Oper | 0E | 3 | 6 | | Absolute, X | ASL Oper,X | 1E | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ BCC BCC Branch on Carry Clear BCC N Z C I D V Operation: Branch on C = 0 _ _ _ _ _ _ (Ref: 4.1.1.3) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BCC Oper | 90 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to different page. BCS BCS Branch on carry set BCS Operation: Branch on C = 1 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.4) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BCS Oper | B0 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to next page. 236 BASIC TO MACHINE LANGUAGE ~ BEQ BEQ Branch on result zero BEQ N Z C I D V Operation: Branch on Z = 1 _ _ _ _ _ _ (Ref: 4.1.1.5) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BEQ Oper | F0 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to next page. BIT BIT Test bits in memory with accumulator BIT Operation: A /\ M, M7 -> N, M6 -> V Bit 6 and 7 are transferred to the status register. N Z C I D V If the result of A /\ M is zero then Z = 1, otherwise M7/ _ _ _ M6 Z = 0 (Ref: 4.2.1.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | BIT Oper | 24 | 2 | 3 | | Absolute | BIT Oper | 2C | 3 | 4 | +----------------+-----------------------+---------+---------+----------+ BMI BMI Branch on result minus BMI Operation: Branch on N = 1 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BMI Oper | 30 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 1 if branch occurs to different page. BASIC TO MACHINE LANGUAGE 237 ~ BNE BNE Branch on result not zero BNE Operation: Branch on Z = 0 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.6) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BMI Oper | D0 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to different page. BPL BPL Branch on result plus BPL Operation: Branch on N = 0 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BPL Oper | 10 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to different page. BRK BRK Force Break BRK Operation: Forced Interrupt PC + 2 toS P toS N Z C I D V _ _ _ 1 _ _ (Ref: 9.11) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | BRK | 00 | 1 | 7 | +----------------+-----------------------+---------+---------+----------+ 1. A BRK command cannot be masked by setting I. 238 BASIC TO MACHINE LANGUAGE ~ BVC BVC Branch on overflow clear BVC Operation: Branch on V = 0 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.8) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BVC Oper | 50 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to different page. BVS BVS Branch on overflow set BVS Operation: Branch on V = 1 N Z C I D V _ _ _ _ _ _ (Ref: 4.1.1.7) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Relative | BVS Oper | 70 | 2 | 2* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if branch occurs to same page. * Add 2 if branch occurs to different page. CLC CLC Clear carry flag CLC Operation: 0 -> C N Z C I D V _ _ 0 _ _ _ (Ref: 3.0.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | CLC | 18 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 239 ~ CLD CLD Clear decimal mode CLD Operation: 0 -> D N A C I D V _ _ _ _ 0 _ (Ref: 3.3.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | CLD | D8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ CLI CLI Clear interrupt disable bit CLI Operation: 0 -> I N Z C I D V _ _ _ 0 _ _ (Ref: 3.2.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | CLI | 58 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ CLV CLV Clear overflow flag CLV Operation: 0 -> V N Z C I D V _ _ _ _ _ 0 (Ref: 3.6.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | CLV | B8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ 240 BASIC TO MACHINE LANGUAGE ~ CMP CMP Compare memory and accumulator CMP Operation: A - M N Z C I D V / / / _ _ _ (Ref: 4.2.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | CMP #Oper | C9 | 2 | 2 | | Zero Page | CMP Oper | C5 | 2 | 3 | | Zero Page,X | CMP Oper,X | D5 | 2 | 4 | | Absolute | CMP Oper | CD | 3 | 4 | | Absolute,X | CMP Oper,X | DD | 3 | 4* | | Absolute,Y | CMP Oper,Y | D9 | 3 | 4* | | (Indirect,X) | CMP (Oper,X) | C1 | 2 | 6 | | (Indirect),Y | CMP (Oper),Y | D1 | 2 | 5* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if page boundary is crossed. CPX CPX Compare Memory and Index X CPX N Z C I D V Operation: X - M / / / _ _ _ (Ref: 7.8) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | CPX *Oper | E0 | 2 | 2 | | Zero Page | CPX Oper | E4 | 2 | 3 | | Absolute | CPX Oper | EC | 3 | 4 | +----------------+-----------------------+---------+---------+----------+ CPY CPY Compare memory and index Y CPY N Z C I D V Operation: Y - M / / / _ _ _ (Ref: 7.9) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | CPY *Oper | C0 | 2 | 2 | | Zero Page | CPY Oper | C4 | 2 | 3 | | Absolute | CPY Oper | CC | 3 | 4 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 241 ~ DEC DEC Decrement memory by one DEC Operation: M - 1 -> M N Z C I D V / / _ _ _ _ (Ref: 10.7) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | DEC Oper | C6 | 2 | 5 | | Zero Page,X | DEC Oper,X | D6 | 2 | 6 | | Absolute | DEC Oper | CE | 3 | 6 | | Absolute,X | DEC Oper,X | DE | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ DEX DEX Decrement index X by one DEX Operation: X - 1 -> X N Z C I D V / / _ _ _ _ (Ref: 7.6) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | DEX | CA | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ DEY DEY Decrement index Y by one DEY Operation: X - 1 -> Y N Z C I D V / / _ _ _ _ (Ref: 7.7) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | DEY | 88 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ 242 BASIC TO MACHINE LANGUAGE ~ EOR EOR "Exclusive-Or" memory with accumulator EOR Operation: A EOR M -> A N Z C I D V / / _ _ _ _ (Ref: 2.2.3.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | EOR #Oper | 49 | 2 | 2 | | Zero Page | EOR Oper | 45 | 2 | 3 | | Zero Page,X | EOR Oper,X | 55 | 2 | 4 | | Absolute | EOR Oper | 40 | 3 | 4 | | Absolute,X | EOR Oper,X | 50 | 3 | 4* | | Absolute,Y | EOR Oper,Y | 59 | 3 | 4* | | (Indirect,X) | EOR (Oper,X) | 41 | 2 | 6 | | (Indirect),Y | EOR (Oper),Y | 51 | 2 | 5* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if page boundary is crossed. INC INC Increment memory by one INC N Z C I D V Operation: M + 1 -> M / / _ _ _ _ (Ref: 10.6) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | INC Oper | E6 | 2 | 5 | | Zero Page,X | INC Oper,X | F6 | 2 | 6 | | Absolute | INC Oper | EE | 3 | 6 | | Absolute,X | INC Oper,X | FE | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ INX INX Increment Index X by one INX N Z C I D V Operation: X + 1 -> X / / _ _ _ _ (Ref: 7.4) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | INX | E8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 243 ~ INY INY Increment Index Y by one INY Operation: X + 1 -> X N Z C I D V / / _ _ _ _ (Ref: 7.5) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | INY | C8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ JMP JMP Jump to new location JMP Operation: (PC + 1) -> PCL N Z C I D V (PC + 2) -> PCH (Ref: 4.0.2) _ _ _ _ _ _ (Ref: 9.8.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Absolute | JMP Oper | 4C | 3 | 3 | | Indirect | JMP (Oper) | 6C | 3 | 5 | +----------------+-----------------------+---------+---------+----------+ JSR JSR Jump to new location saving return address JSR Operation: PC + 2 toS, (PC + 1) -> PCL N Z C I D V (PC + 2) -> PCH _ _ _ _ _ _ (Ref: 8.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Absolute | JSR Oper | 20 | 3 | 6 | +----------------+-----------------------+---------+---------+----------+ 244 BASIC TO MACHINE LANGUAGE ~ LDA LDA Load accumulator with memory LDA Operation: M -> A N Z C I D V / / _ _ _ _ (Ref: 2.1.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | LDA #Oper | A9 | 2 | 2 | | Zero Page | LDA Oper | A5 | 2 | 3 | | Zero Page,X | LDA Oper,X | B5 | 2 | 4 | | Absolute | LDA Oper | AD | 3 | 4 | | Absolute,X | LDA Oper,X | BD | 3 | 4* | | Absolute,Y | LDA Oper,Y | B9 | 3 | 4* | | (Indirect,X) | LDA (Oper,X) | A1 | 2 | 6 | | (Indirect),Y | LDA (Oper),Y | B1 | 2 | 5* | +----------------+-----------------------+---------+---------+----------+ * Add 1 if page boundary is crossed. LDX LDX Load index X with memory LDX Operation: M -> X N Z C I D V / / _ _ _ _ (Ref: 7.0) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | LDX #Oper | A2 | 2 | 2 | | Zero Page | LDX Oper | A6 | 2 | 3 | | Zero Page,Y | LDX Oper,Y | B6 | 2 | 4 | | Absolute | LDX Oper | AE | 3 | 4 | | Absolute,Y | LDX Oper,Y | BE | 3 | 4* | +----------------+-----------------------+---------+---------+----------+ * Add 1 when page boundary is crossed. BASIC TO MACHINE LANGUAGE 245 ~ LDY LDY Load index Y with memory LDY N Z C I D V Operation: M -> Y / / _ _ _ _ (Ref: 7.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | LDY #Oper | A0 | 2 | 2 | | Zero Page | LDY Oper | A4 | 2 | 3 | | Zero Page,X | LDY Oper,X | B4 | 2 | 4 | | Absolute | LDY Oper | AC | 3 | 4 | | Absolute,X | LDY Oper,X | BC | 3 | 4* | +----------------+-----------------------+---------+---------+----------+ * Add 1 when page boundary is crossed. LSR LSR Shift right one bit (memory or accumulator) LSR +-+-+-+-+-+-+-+-+ Operation: 0 -> |7|6|5|4|3|2|1|0| -> C N Z C I D V +-+-+-+-+-+-+-+-+ 0 / / _ _ _ (Ref: 10.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Accumulator | LSR A | 4A | 1 | 2 | | Zero Page | LSR Oper | 46 | 2 | 5 | | Zero Page,X | LSR Oper,X | 56 | 2 | 6 | | Absolute | LSR Oper | 4E | 3 | 6 | | Absolute,X | LSR Oper,X | 5E | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ NOP NOP No operation NOP N Z C I D V Operation: No Operation (2 cycles) _ _ _ _ _ _ +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | NOP | EA | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ 246 BASIC TO MACHINE LANGUAGE ~ ORA ORA "OR" memory with accumulator ORA Operation: A V M -> A N Z C I D V / / _ _ _ _ (Ref: 2.2.3.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | ORA #Oper | 09 | 2 | 2 | | Zero Page | ORA Oper | 05 | 2 | 3 | | Zero Page,X | ORA Oper,X | 15 | 2 | 4 | | Absolute | ORA Oper | 0D | 3 | 4 | | Absolute,X | ORA Oper,X | 10 | 3 | 4* | | Absolute,Y | ORA Oper,Y | 19 | 3 | 4* | | (Indirect,X) | ORA (Oper,X) | 01 | 2 | 6 | | (Indirect),Y | ORA (Oper),Y | 11 | 2 | 5 | +----------------+-----------------------+---------+---------+----------+ * Add 1 on page crossing PHA PHA Push accumulator on stack PHA Operation: A toS N Z C I D V _ _ _ _ _ _ (Ref: 8.5) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | PHA | 48 | 1 | 3 | +----------------+-----------------------+---------+---------+----------+ PHP PHP Push processor status on stack PHP Operation: P toS N Z C I D V _ _ _ _ _ _ (Ref: 8.11) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | PHP | 08 | 1 | 3 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 247 ~ PLA PLA Pull accumulator from stack PLA Operation: A fromS N Z C I D V _ _ _ _ _ _ (Ref: 8.6) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | PLA | 68 | 1 | 4 | +----------------+-----------------------+---------+---------+----------+ PLP PLP Pull processor status from stack PLA Operation: P fromS N Z C I D V From Stack (Ref: 8.12) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | PLP | 28 | 1 | 4 | +----------------+-----------------------+---------+---------+----------+ ROL ROL Rotate one bit left (memory or accumulator) ROL +------------------------------+ | M or A | | +-+-+-+-+-+-+-+-+ +-+ | Operation: +-< |7|6|5|4|3|2|1|0| <- |C| <-+ N Z C I D V +-+-+-+-+-+-+-+-+ +-+ / / / _ _ _ (Ref: 10.3) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Accumulator | ROL A | 2A | 1 | 2 | | Zero Page | ROL Oper | 26 | 2 | 5 | | Zero Page,X | ROL Oper,X | 36 | 2 | 6 | | Absolute | ROL Oper | 2E | 3 | 6 | | Absolute,X | ROL Oper,X | 3E | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ 248 BASIC TO MACHINE LANGUAGE ~ ROR ROR Rotate one bit right (memory or accumulator) ROR +------------------------------+ | | | +-+ +-+-+-+-+-+-+-+-+ | Operation: +-> |C| -> |7|6|5|4|3|2|1|0| >-+ N Z C I D V +-+ +-+-+-+-+-+-+-+-+ / / / _ _ _ (Ref: 10.4) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Accumulator | ROR A | 6A | 1 | 2 | | Zero Page | ROR Oper | 66 | 2 | 5 | | Zero Page,X | ROR Oper,X | 76 | 2 | 6 | | Absolute | ROR Oper | 6E | 3 | 6 | | Absolute,X | ROR Oper,X | 7E | 3 | 7 | +----------------+-----------------------+---------+---------+----------+ Note: ROR instruction is available on MCS650X microprocessors after June, 1976. RTI RTI Return from interrupt RTI N Z C I D V Operation: P fromS PC fromS From Stack (Ref: 9.6) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | RTI | 4D | 1 | 6 | +----------------+-----------------------+---------+---------+----------+ RTS RTS Return from subroutine RTS N Z C I D V Operation: PC fromS, PC + 1 -> PC _ _ _ _ _ _ (Ref: 8.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | RTS | 60 | 1 | 6 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 249 ~ SBC SBC Subtract memory from accumulator with borrow SBC - Operation: A - M - C -> A N Z C I D V - / / / _ _ / Note:C = Borrow (Ref: 2.2.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Immediate | SBC #Oper | E9 | 2 | 2 | | Zero Page | SBC Oper | E5 | 2 | 3 | | Zero Page,X | SBC Oper,X | F5 | 2 | 4 | | Absolute | SBC Oper | ED | 3 | 4 | | Absolute,X | SBC Oper,X | FD | 3 | 4* | | Absolute,Y | SBC Oper,Y | F9 | 3 | 4* | | (Indirect,X) | SBC (Oper,X) | E1 | 2 | 6 | | (Indirect),Y | SBC (Oper),Y | F1 | 2 | 5 | +----------------+-----------------------+---------+---------+----------+ * Add 1 when page boundary is crossed. SEC SEC Set carry flag SEC Operation: 1 -> C N Z C I D V _ _ 1 _ _ _ (Ref: 3.0.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | SEC | 38 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ SED SED Set decimal mode SED N Z C I D V Operation: 1 -> D _ _ _ _ 1 _ (Ref: 3.3.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | SED | F8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ 250 BASIC TO MACHINE LANGUAGE ~ SEI SEI Set interrupt disable status SED N Z C I D V Operation: 1 -> I _ _ _ 1 _ _ (Ref: 3.2.1) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | SEI | 78 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ STA STA Store accumulator in memory STA Operation: A -> M N Z C I D V _ _ _ _ _ _ (Ref: 2.1.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | STA Oper | 85 | 2 | 3 | | Zero Page,X | STA Oper,X | 95 | 2 | 4 | | Absolute | STA Oper | 80 | 3 | 4 | | Absolute,X | STA Oper,X | 90 | 3 | 5 | | Absolute,Y | STA Oper, Y | 99 | 3 | 5 | | (Indirect,X) | STA (Oper,X) | 81 | 2 | 6 | | (Indirect),Y | STA (Oper),Y | 91 | 2 | 6 | +----------------+-----------------------+---------+---------+----------+ STX STX Store index X in memory STX Operation: X -> M N Z C I D V _ _ _ _ _ _ (Ref: 7.2) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | STX Oper | 86 | 2 | 3 | | Zero Page,Y | STX Oper,Y | 96 | 2 | 4 | | Absolute | STX Oper | 8E | 3 | 4 | +----------------+-----------------------+---------+---------+----------+ BASIC TO MACHINE LANGUAGE 251 ~ STY STY Store index Y in memory STY Operation: Y -> M N Z C I D V _ _ _ _ _ _ (Ref: 7.3) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Zero Page | STY Oper | 84 | 2 | 3 | | Zero Page,X | STY Oper,X | 94 | 2 | 4 | | Absolute | STY Oper | 8C | 3 | 4 | +----------------+-----------------------+---------+---------+----------+ TAX TAX Transfer accumulator to index X TAX Operation: A -> X N Z C I D V / / _ _ _ _ (Ref: 7.11) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | TAX | AA | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ TAY TAY Transfer accumulator to index Y TAY Operation: A -> Y N Z C I D V / / _ _ _ _ (Ref: 7.13) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | TAY | A8 | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ 252 BASIC TO MACHINE LANGUAGE ~ TSX TSX Transfer stack pointer to index X TSX Operation: S -> X N Z C I D V / / _ _ _ _ (Ref: 8.9) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | TSX | BA | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ TXA TXA Transfer index X to accumulator TXA N Z C I D V Operation: X -> A / / _ _ _ _ (Ref: 7.12) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles| +----------------+-----------------------+---------+---------+----------+ | Implied | TXA | 8A | 1 | 2 | +----------------+-----------------------+---------+---------+----------+ TXS TXS Transfer index X to stack pointer TXS N Z C I D V Operation: X -> S _ _ _ _ _ _ (Ref: 8.8) +----------------+-----------------------+---------+---------+----------+ | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles|