12.07.2015 Views

williams-et-al-1983-apple-ii-computer-graphics

williams-et-al-1983-apple-ii-computer-graphics

williams-et-al-1983-apple-ii-computer-graphics

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPLE IICOMPUTERGRAPHICSKen WilliamsBob KernaghanLisa Kernaghanillustrations byGregory Paul SteffenRobert J. Brady Co.A Prentice-H<strong>al</strong>l Publishing andCommunications CampanyBowie, MD 20715


Executive Editor: David T. CulverwellProduction Editor/Text Designer: Michael J. RogersArt Director: Bernard VervinTypes<strong>et</strong>ting: Creative Communications CorporationCockeysville, MDTypefaces: Eurostile (display); Optima (text)Printed by: R.R. Donnelley & Sons CompanyHarrisonburg, VACover Design: Don SellersIndexer: Leah KramerApple 11 Computer GraphicsCopyright© <strong>1983</strong> by Robert J. Brady Company.All rights reserved. No part of this publication may be reproduced ortransmitted in any form or by any means, electronic or mechanic<strong>al</strong>, includingphotocopying and recording, or by any information storage andr<strong>et</strong>riev<strong>al</strong> system, without permission in writing from the publisher. Forinformation, address Robert J. Brady Co., Bowie, Maryland 20715Library of Congress Cat<strong>al</strong>oging in Publication DataWilliams, Ken, 1938-Apple 11 <strong>computer</strong> <strong>graphics</strong>.Includes index.1. Apple II (Computer)-Programming. 2. Computer<strong>graphics</strong>. I. Kernaghan, Bob. 11. Kernaghan, Lisa.111. Steffen, Gregory Paul. IV. Title. V. Title:Apple 2 <strong>computer</strong> <strong>graphics</strong>. VI. Title: Apple Two<strong>computer</strong> <strong>graphics</strong>.QA76.8.A662W54 <strong>1983</strong> 001.64' 43 83-3871ISBN 0-89303-315-4Prentice-H<strong>al</strong>l Internation<strong>al</strong>, Inc., LondonPrentice-H<strong>al</strong>l Canada, Inc., Scarborough, OntarioPrentice-H<strong>al</strong>l of Austr<strong>al</strong>ia, Pty., Ltd., SydneyPrentice-H<strong>al</strong>l of India Private Limited, New DelhiPrentice-H<strong>al</strong>l of Japan, Inc., TokyoPrentice-H<strong>al</strong>l of Southeast Asia Pte. Ltd., SingaporeWhiteh<strong>al</strong>l Books, Limited, P<strong>et</strong>one, New Ze<strong>al</strong>andEditora Prentice-H<strong>al</strong>l Do Brasil LTDA., Rio de JaneiroPrinted in the United States of America83 84 85 86 87 88 89 90 91 92 93 10 9 8 7 6 5 4 3 2 1


Contents1 Introduction 12 Computer Physiology 33 System Monitor-Memory Tricks 134 APPLESOFT Extensions 215 Graphics Modes and Soft Switches 316 Text and Low-Res 397 Preserving Your Pictures 538 Hi-Res Graphics 659 Hi-Res Color 8110 Shaping Up 9911 Graphs and Charts 11912 Byte-Move Shapes 13513 Advanced Moves 14714 Collision Course 157Appendix 1: Decim<strong>al</strong>, Hex, and Bina._ry 165Appendix 2: Character Codes: ASCII vs APPLE 175Appendix 3: Memory Maps 177Glossary 181Index 186<strong>ii</strong>i


ForewordThe introduction of the Apple II <strong>computer</strong> marked a revolution in <strong>computer</strong>s.For the first time there was a fully fu nction<strong>al</strong> computing machineavailable which was priced affordably for home users. In addition to itspowerfu l data processing capabil ities, APPLESOFT had the first s<strong>et</strong> ofBASIC extensions which <strong>al</strong>lowed the programmer true power and conveniencewhen working with <strong>graphics</strong>. The six-color high resolution modewith the HCOLOR and HPLOT commands were "state of the art," and theSHAPE construct and associated DRAW, XDRAW, SCALE, and ROTatefunctions were ahead of their time.When I acquired my fi rst Apple, the Low-Res "Little Brick Out" wasconsidered a very good game, and Bob Bishop's "Applevision" demo was,and still is, awe-inspiring. But with liter<strong>al</strong>ly millions of people seeing andusing the Apple, advances in style and technique came rapidly as programmersand "hackers" accepted and surmounted one ch<strong>al</strong>lenge after anotherin their attempt to "do it b<strong>et</strong>ter."The manu<strong>al</strong>s state that only six colors are available on the High-Resscreen, but Sierra On-Line soon developed over 100 colors. It was notsupposed to be possible to put text on the High-Res screen, but doing so isnow commonplace. The power, ingenuity, and persistence of the humanmind has extended the capabil ities of the Apple further than anyone hadimagined possible. Like most of the current software companies, SierraOn-Line was born in long and solitary hours of developing newer, faster,and b<strong>et</strong>ter m<strong>et</strong>hods of producing <strong>graphics</strong> on the Apple. In writing thisbook, it is my hope to introduce you to those advanced techniques such asByte-Move, animation, and collision d<strong>et</strong>ection, as well as the standardApple <strong>graphics</strong>.With the aid of my co-authors, who are profession<strong>al</strong> teachers in additionto being experts in the field of home <strong>computer</strong>s, I have tried to presentthose ideas in a manner which is clear and understandable, and whichrelies only on a knowledge of BASIC. I hope you find this book enjoyabl<strong>et</strong>o read as well as usefu l in your own program development.Ken WilliamsThe author and publisher of this book have used their best efforts inpreparing this book and the programs contained in it. These effortsinclude the development, research, and testing of the theories and programsto d<strong>et</strong>ermine their effectiveness. The author and publisher makeno warranty of any kind, expressed or implied, with regard to these programsor the documentation contained in this book. The author andpublisher sh<strong>al</strong>l not be liable in any event for incident<strong>al</strong> or consequenti<strong>al</strong>damages m connection with, or arising out of, the furnishing, performance,or use of these programs.iv


1IntroductionThe level of <strong>graphics</strong> for the Apple II has risen considerably since the earlydays when Little Brick Out was considered a major accomplishment. Theindustry standards now virtu<strong>al</strong>ly require that good <strong>graphics</strong> be multicoloredand high resolution, and that the animation be smooth, clean, andfast, and collision d<strong>et</strong>ection quick and accu rate.If you are at <strong>al</strong>l fam iliar with the remarkable <strong>graphics</strong> effects on themark<strong>et</strong> today, you have probably become curious about the m<strong>et</strong>hods usedto generate them. This text will d<strong>et</strong>ail those aspects of Apple II <strong>graphics</strong>which are currently in common use; particularly with regard to Hi-Res,Low-Res, and Text Mode <strong>graphics</strong>, color generation, animation, and collisiond<strong>et</strong>ection. All subjects are presented for use by the APPLESOFT programmer,but have extensions for the assembly and machine languageprogrammers.In using this text, it is necessary to have the following equipment handyand ready to use:Apple 11, Apple II +, or Apple lie <strong>computer</strong>Color Monitor or TVOne or more disk drives and disksorA tape recorder and tapesLots of paperA pri nter is helpful for <strong>graphics</strong>, but not necessary.This book is not written for the beginning BASIC programmer; it is necessarythat the user of this book have some knowledge of BASIC program-1


2 APPLE II COMPUTER GRAPHICSming. The user should be familiar with the following commands: GR,COLOR, PLOT, HLIN, VLIN, SCRN, HGR, HGR2, HCOLO R, HPLOT,DRAW, and XDRAW. If you are unfam iliar with APPLESOFT BASIC orthese commands, please review the APPLESOFT Reference Manu<strong>al</strong> beforecontinuing.Before starting our journey into the world of <strong>graphics</strong>, it is prudent toarm ourselves with some relatively technic<strong>al</strong>, often frustrating, but quiteessenti<strong>al</strong> materi<strong>al</strong>. We assume you have a rudimentary knowledge ofAPPLESOFT BASIC and your Apple 11 system. So, l<strong>et</strong> us begin with adiscussion of the binary, decim<strong>al</strong>, and hexadecim<strong>al</strong> systems of numeration.The time has come, the w<strong>al</strong>rus saidTo speak of many things.Of bits and bytes and peeks and pokes . ..


4 APPLE II COMPUTER GRAPHICSmachine's level means learning som<strong>et</strong>hing about how the <strong>computer</strong> storesand processes data.Digressing to Digit<strong>al</strong>All of today's micro<strong>computer</strong>s use two-state digit<strong>al</strong> circuitry. In relation to<strong>computer</strong>s, digit<strong>al</strong> circuitry means, in very loose terms, that <strong>al</strong>l informationis represented within the <strong>computer</strong> as a discre<strong>et</strong> state of a circuit-usu<strong>al</strong>lyeither off or on. The power light on your Apple is a good example; thelight is either off or on, and by looking at the light, you can infer the stateof your machine.There are an<strong>al</strong>og <strong>computer</strong>s whose basis for processing is not the presenceor absence of voltage, but instead the amount of voltage present in acircuit. However, it is operation<strong>al</strong>ly much easier to test for the presence orabsence of voltage in a circuit than it is to d<strong>et</strong>ermine the precise amount ofvoltage. You can imagine the complexity and accu racy required of a <strong>computer</strong>circuit to correctly and reliably represent a number such as 36,741by an<strong>al</strong>yzing only the amount of the voltage. For that reason the memoryof your <strong>computer</strong> is digit<strong>al</strong>, and consists of thousands of two-state microcircuits,where each circuit can be s<strong>et</strong> either on or off. Since each circuitmaintains or "remembers" the on or off state to which it was s<strong>et</strong>, thesecircuits can be used to store <strong>al</strong>l of the info rmation which the machine issupposed to remember.RAM and ROMYour Apple II has two types of memory: RAM and ROM. ROM stands forRead Only Memory. The information contained in ROM is essenti<strong>al</strong>ly permanentand un<strong>al</strong>terable. You may read the information from ROM, butyou cannot write to it or change it in any way (that is why it is termed"Read Only"). Your version of BASIC is stored in ROM, as are most of theother instructions which make your system operate. That is what makesthe Apple II "intelligent" since its operati ng system is "on board" or"ROM resident."RAM is short for Random Access Memory; RAM is the working memoryfor your Apple. Wh<strong>et</strong>her you are writing, editing, or running a program,that program resides in RAM, as do <strong>al</strong>l of the variables and other data youuse. RAM is of great interest in <strong>graphics</strong> because the video output is simplydisplaying data from a secton of the RAM. The use of <strong>graphics</strong> then becomesmostly a matter of putting the correct data into the proper place inmemory.To make it easier to visu<strong>al</strong>ize, memory within the <strong>computer</strong> may b<strong>et</strong>hought of as a great many switches, each of--,which is either on or off atany given time. Those memory switches are commonly c<strong>al</strong>led "bits," and


CHAPTER 2-COMPUTER PHYSIOLOGY 5ONE BITTWO CHOICES2 1TWO BITSFIRST BIT SECOND BITONONOFFONOFFONOFFOFFFOUR CHOICES2 2THREE BITSFIRST BIT SECOND BIT THIRD BITONONONONONONOFFOFFOFFOFFONOFFOFFONONOFFOFFOFFONOFFONOFFONOFFEIGHT CHOICES23Figure 2-1. Bit combinations.once each RAM bit is s<strong>et</strong> on or off, it remains in that state until <strong>al</strong>tered by acommand, or until the <strong>computer</strong> is turned off. (At which time the v<strong>al</strong>ues inRAM are lost.) All of the <strong>computer</strong>' s impressive capabilities rely on thelowly bit. One bit is quite a long way from making a <strong>computer</strong>, just as onegrain of sand is a long way from being a beach. A bit, like a grain of sand,has little meaning individu<strong>al</strong>ly, but when grouped with thousands of other·bits, tog<strong>et</strong>her they are able to take on meaning and fo rm.Making It Happen with Ones and ZerosAs stated earlier, each bit will take on only one of two states, on or off. Inorder to facilitate discussion, if a bit is on, it is said to have a v<strong>al</strong>ue of 1; abit which is off has a v<strong>al</strong>ue of 0. Since each bit may only be 1 or 0, each bitconveys a very limited amount of information. In the example of your


6 APPLE II COMPUTER GRAPHICS<strong>computer</strong>'s power light, you know only that the power is on or off, andnothing more. Two bits in a group combine in one of four (22) combi nations:(/)(/),


CHAPTER 2-COMPUTER PHYSIOLOGY 7ber is c<strong>al</strong>led the "address" of that byte. A memory address is much like thenumber on a post office box- one can d<strong>et</strong>ermine what is in any byte ofmemory by going to the box with the proper address and looking inside.The number of boxes available depends upon two things: The number ofboxes inst<strong>al</strong>led (memory size), and the maximum number of digits availablein the address. If you were limited to use only three decim<strong>al</strong> digits inan add ress, there would be a maximum of H'P (HM'l©) boxes-addressed©©© through 999. The processor within the Apple <strong>al</strong>lows for two bytes(sixteen bits) in the address of any location in memory, and that provides amaximum of 216 addresses. If you have wondered why you can use amaximum of 64K bytes of memory within an Apple II, consider that eachmemory location has to have an address, and that 216, using a littlearithm<strong>et</strong>ic and a good c<strong>al</strong>culator, equ<strong>al</strong>s 65,536-the true maximum forthe number of bytes the 65©2 processor can address. Many larger <strong>computer</strong>suse three or more bytes for addressing; a three-byte address contains24 bits, so by <strong>al</strong>lowing three bytes in the addresses, a <strong>computer</strong> canaddress 224 memory locations: over SIXTEEN MILLION bytes of memory!Binary BlindnessYour <strong>computer</strong> is very happy to work in binary. The early machines did soexclusively, but most people fi nd it trying-looking at binary numbers <strong>al</strong>lday makes your eyes cross! To demonstrate, compare the two columns ofbinary numbers below, and try to d<strong>et</strong>ermine where they differ.1©©1©1 11 1 ©©1 ©1 11©11Zl1 1Zl1 1©llllllllll©lllllllIll 1 Ill 1 Ill 11 Illlllllllll 1 lllllll Ill110 11©1© 11©1 11Zl11Zlllllll 1 llllll 111 lllllll llllll 1 11111 Ill 1 1 llllll 111llllll 1 llllllIll 1 lllllllllllllll 1 Ill 1 lllllllllllllll 111111Zl11Zllll11111Zl1©1ZlIs it any wonder that there were very few programmers in the days when<strong>computer</strong>s spoke only binary?Hex ta the RescueThe hexadecim<strong>al</strong> or hex system of numeration, base sixteen, supplies awelcome solution to the problem of "binary blindness." Hex is advantageousbecause it provides a convenient way to turn the binary ones and


8 APPLE II COMPUTER GRAPHICSzeros into som<strong>et</strong>hing more easily communicated to the human brain, somost modern <strong>computer</strong>s like to speak to people using hex.Hexadecim<strong>al</strong>, which uses sixteen digits, is used to represent the 256different combinations available with an eight-bit byte. It is conceivable torepresent the v<strong>al</strong>ue of a byte using a single base 256 digit; however, mostpeople would fi nd it very difficult to remember 256 different digits. There,::fore, in the interest of simplicity, instead of one base 256 digit representingthe v<strong>al</strong>ue of a byte, two hex digits are used. Why not use decim<strong>al</strong> which is ,..simpler y<strong>et</strong>? Do not forg<strong>et</strong> that we are de<strong>al</strong>ing with a group of bits. The tendecim<strong>al</strong> digits are too many to represent three bits and too few for fourbits, as three bits make for eight combinations, and four bits make forsixteen. The fact that sixteen combinations are available to four bits, andthat hexadecim<strong>al</strong> uses exactly sixteen digits, is very important. This is thereason that many programmers count like they have sixteen fi ngers!BIT BIT BIT BIT1 2 3 4HEXADECIMAL BINARYDECIMALOFF OFF OFF OFFOFF OFF OFF ON0 0 0 o I o1 0 0 0 101OFF OFF ONOFF2 0 0 1 02OFF OFF ONON3 0 0 1 13OFF ONOFF OFF4 0 1 0 04OFF ONOFF ON5 0 1 0 15OFF ON ON OFFOFF ON ON ON6 0 1 1 07 0 1 1 167ONOFF OFF OFF8I 1 0 0 08ONONOFF OFF ONOFF ON OFF9 1 0 0 1A 1 0 1 o I910ONOFF ONONB 1 0 1 111ONONOFF OFFc 1 1 0 012ONONOFF ONDI 1 1 0 113ONON ON OFFEI 1 1 1 014ONON ON ONF! 1 1 1 I 115Figure 2-2. A binary/hex chart.


CHAPTER 2-COMPUTER PHYSIOLOGY 9Taking a Sm<strong>al</strong>ler ByteThe next problem is to represent each of the 256 diffe rent v<strong>al</strong>ues for onebyte using only the sixteen hex digits-the standard (/) through 9, plus thel<strong>et</strong>ters A, B, C, D, E, and F; where A stands fo r ten, B for eleven, and soforth through F which stands for fifteen.One byte is represented by two hex digits; each digit represents four ofthe eight bits in that byte. As modern <strong>computer</strong> whimsy would dictate,each of the two h<strong>al</strong>f-bytes is c<strong>al</strong>led a nibble (som<strong>et</strong>imes nybble) . A binarynibble corresponds to a hex digit as shown in Figure 2-2.Suppose that you were looking at a byte with <strong>al</strong>l eight bits turned on.Separated into the two nibbles, it would look like 1111 1111 and wouldcorrespond to hexadecim<strong>al</strong> FF, since each nibble (1 111) corresponds to F.The binary byte 1


10 APPLE II COMPUTER GRAPHICSthe system your Apple understands. There are, however, a few instanceswhere you must convert hex to decim<strong>al</strong>, such as for use in an APPLESOFTPOKE or CALL statement. When you do have to perform the conversionyou may use the following <strong>al</strong>gorithm:Reading the hex number from right to left, the decim<strong>al</strong> v<strong>al</strong>ue of theentire number is found by taking the decim<strong>al</strong> v<strong>al</strong>ue of the fi rst hex digit(nibble), plus 16 times the decim<strong>al</strong> v<strong>al</strong>ue of the second digit, plus 256(162) times the decim<strong>al</strong> v<strong>al</strong>ue of the third digit, plus 4096 (163) times thedecim<strong>al</strong> v<strong>al</strong>ue of the fourth digit, and so on.Example 1$Clll57 = 7 + 16*5 + 256*© + 4096*11= 7 + Bill + Ill + 45056= 45143Example 2$10 = © + 1*16= 16Micro<strong>computer</strong>s rarely de<strong>al</strong> intern<strong>al</strong>ly with numbers having more than fourhex digits, but the <strong>al</strong>gorithm does carry on if necessary. If you convert thelargest possible two byte hex number, $FFFF, to decim<strong>al</strong> you should g<strong>et</strong>65,536. Since your micro uses two byte addresses, you can again see whythat number is the largest in the Apple's world.Although conversion m<strong>et</strong>hods have been discussed only briefly, the precedingdiscussion is sufficient to serve our purposes for now. Appendix 1contains a more thorough treatment of hexadecim<strong>al</strong>, decim<strong>al</strong>, and binarynumber systems. There are <strong>al</strong>so a number of c<strong>al</strong>culators presently on themark<strong>et</strong> which are designed for <strong>computer</strong> programmers; they have fu nctionsto do <strong>al</strong>l of the conversions fo r you at the press of a button.VocabularyAddressBinaryBitByteDecim<strong>al</strong>HexHexadecim<strong>al</strong>MemoryNibbleRAMROMROM Resident


CHAPTER 2-COMPUTER PHYSIOLOGY 11Exercises1. Convert the following binary numbers to hex:a. !1©1 1 ©1©©b. !©1 ©1 1111c. !©1 ©1 ©1 1©2. Convert the following to binary:a. $7Cb. $FFc. $ASd. $DB3. Convert the following to decim<strong>al</strong>:a. $7C e. $7CFFb. $FF f. $DBASc. $AS g. !1©1 1 ©©©© (Hint: convert tod. $DB h. !©111 ©1©1 hex fi rst.)4. Convert $3F and $S7 to binary and to decim<strong>al</strong> numer<strong>al</strong>s.S. Convert ! 1 ©©1 ©1 1 © and !©1 11 1 ©1 1 to hexadecim<strong>al</strong>.



3System Monitor­Memory TricksObjectivesAfter reading Chapter 3 you should be able to:• Enter and leave the Monitor.• Examine RAM and ROM.• Change the contents of RAM.• Move blocks of memory.• Use a memory map to locate reserved and free memory.In essence, your <strong>computer</strong> is simply a pile of very fancy circuitry, andthose circuits are designed to respond to a certain s<strong>et</strong> of very, very rudimentaryinstructions. Although BASIC will respond to high level commandssuch as NEW and PRINT, those are commands of your language,and not your processor. It is the function of the BASIC interpr<strong>et</strong>er totranslate those statements down to the elementary ones and zeros thatyour processor requires to operate; the machine takes over from there.The capabil ities of BASIC are good as far as they go, but there are applicationsto <strong>graphics</strong> which req uire that the programmer de<strong>al</strong> directly withmemory.Memory is where everything happens, and though you can reach memoryusing BASIC's PEEK and POKE statements, there is a faster, morepowerful, and som<strong>et</strong>imes more convenient m<strong>et</strong>hod . Supplied in the ROMof every Apple is the system Monitor, with a capit<strong>al</strong> M, which is very13


14 APPLE II COMPUTER GRAPHICSdifferent from your video monitor. The Monitor is actu<strong>al</strong>ly another <strong>computer</strong> language and is designed to l<strong>et</strong> you, the user, communicate directlywith system memory. As related to <strong>graphics</strong>, you will use Monitor toinspect, mod ify, or move the contents of the RAM. You will <strong>al</strong>so learn touse Monitor to enter short machine language programs which can beaccessed from your BASIC programs to manipulate <strong>graphics</strong>. But beforeyou can learn about the Monitor, you have to find it.Monitor, I PresumeIf you have worked much with your Apple you have noticed that, whenyou are t<strong>al</strong>king to APPLESOFT, the <strong>computer</strong> displays a ] on the left edgeof the screen; you g<strong>et</strong> a > when you work with INTEGER BASIC. Thoseare prompt characters, and their purpose is to remind you which languageyou are de<strong>al</strong>ing with. The prompt character fo r Monitor is *. If you have aregular Apple II, you see the * prompt every time you turn the system on.When you power up an Apple II + or Apple lie, they will immediatelyaccess the disk and load the disk operati ng system (DOS). After DOS hasbeen loaded, the screen will display either the > or ] prompt, and thenyou can reach the Monitor by typing:CALL-151 When you want to r<strong>et</strong>urn to BASIC from Monitor, type:< CTRL-C> If you have DOS up, an <strong>al</strong>ternate m<strong>et</strong>hod of r<strong>et</strong>u rning to BASIC is to type:3Dl3G Examining MemoryYou can use Monitor to examine or <strong>al</strong>ter any byte of RAM; you can <strong>al</strong>soexamine ROM, though you cannot change it. Enter the Monitor, then type:33 < CR>The display should respond with the contents of address $33:131333- AALocation $33 is where Apple stores the current prompt character, and $AAis Apple's numeric code fo r the *. Notice that you did not preface the 33with a dollar sign, even though you meant hexadecim<strong>al</strong> 33; Monitor <strong>al</strong>waysspeaks in hex and assumes that you will do the same.You may look at more than one byte of memory at a time by specifyingthe beginning and ending addresses of the range of memory you wish tosee. Still, from Monitor type:


CHAPTER 3-SYSTEM MONITOR-MEMORY TRICKS 15q .ClF< CR>Do not use spaces to separate the two elements of the command. The<strong>computer</strong> will then display som<strong>et</strong>hing similar to:c- xx xx xx xx xx xx xx xxC8- xx xx xx xx xx xx xx xxCl- xx xx xx xx xx xx xx xxC18- xx xx xx xx xx xx xx xxThe numbers in the left hand column index the memory locations displayedin the first column of each line. Each of the xx's will be somehexadecim<strong>al</strong> byte, though the precise v<strong>al</strong>ue of each byte will vary. Shownin the fi rst row will be the contents of memory locations CThe colon is the Monitor command to change the v<strong>al</strong>ues of memorystarting at the given location (C), and the FF's are the v<strong>al</strong>ues to whichyou wish to change. The spaces b<strong>et</strong>ween each of the bytes are necessaryfor the command to execute properly. (From here on out, we will assum<strong>et</strong>hat you will press the r<strong>et</strong>urn key when required, so we will stop putting after each entry.)To verify that the memory has been changed, enter:C . C7and this time you will see:C- FF FF FF FF FF FF FF FFThis indicates that the eight bytes beginning at C


16 APPLE II COMPUTER GRAPHICSDOS vector area ($JC!3-$JFF), you have an excellent chance of creatinghavoc, so be very carefu l. The memory map in Figure 3-1 shows whichareas of memory are used for what.Memory Map ol a 48K Apple IIFunctionAPPLE MONITORAPPLESOITRESERVEDl!O DECODEDOSUNUSEDHI· RES PAGE 2HI· RES PAGE IUNUSEDTEXT /LO· RES PAGE 2TEXT /LO· RES PAGE IDOS VECTORSUNUSEDTEXT INPUT BUFFER6502 STACKZERO PAGEAddress$FSOO·SFFFF$EOOO·$F7FFSDOOO·SDFFFSCOOO·$CFFF$9600·$BFFF$6000·$95FF$4000·$5FFF$2000·$3FFF$COO·$! FFF$800·$BFF$400·$7FF$3CO·S3FF$300·$3BF$200·$2FF$100·$1FFSO·$FFFigure 3-1. A memory map.In a 48K system, RAM extends from location $0, at the "bottom" ofmemory, through location $BFFF. The memory above $BFFF ($C000through $FFFF) is ROM. (Unless you own a 16K RAM expansion, in whichcase the entire range is RAM.) The reference manu<strong>al</strong>s for your systemcontain sever<strong>al</strong> memory maps which are usefu l in d<strong>et</strong>ermining which areasare used by the system, BASIC, and/or DOS.Write Fram MemoryYou can place characters directly on the Text Screen by inserting thenumeric code for the character into the area of memory which is displayed(Text, page 1, is the area of memory displayed). The memory map showsthat the memory area fo r page 1 of -Cext lies b<strong>et</strong>ween $400 and $7FF. Toclear the screen from Monitor, type: Locations $6D!3 through $6D7 are within Te)#JIScreen Memory, so l<strong>et</strong>'s plac<strong>et</strong>he Apple code fo r "B" in those locations by typing:6D!3 : C2 C2 C2 C2 C2 C2 C2 C2As soon as you press the r<strong>et</strong>urn key, eight "B's" will appear near thebottom of your screen.


CHAPTER 3-SYSTEM MONITOR-MEMORY TRICKS 17Next, change the same locations to $A0, and the "B's" will disappear.As you have probably guessed, $C2 is the Apple code for "B", and $A0 isthe code fo r a blank. You could clear the entire screen by placing $A0 inevery location from $400 through $7FF, but entering 960 copies of $A0 isnot only tedious, it can be avoided by using the Monitor MOVE commandwhich we will discuss next.Making Memory MoveThe Monitor MOVE command l<strong>et</strong>s you move the contents of one range ofmemory to a diffe rent range. For example, you could use the MOVEcommand to take the contents of the eight bytes which begin at location /$C00 and place those v<strong>al</strong>ues into the eight bytes which begin at $000.You would have to tell Monitor sever<strong>al</strong> things: that you wish to movememory, which chunk of memory is to be moved (C9JLC9J7}, and whereyou wish to put the v<strong>al</strong>ues that you are moving (D9J9J}.First, display the contents of $C9J9J through $C9J7 just to see what is there.Enter:C9J9J . C9J7then move those v<strong>al</strong>ues to $000 by typing:D9J9J < C9J9J . C9J7MDo not use spaces to separate the diffe rent parts of the command. Thiscommand tells Monitor that the memory locations beginning at $D9J9J are toreceive ( $D9J9J2 $C9Jl ----> $D9Jl3 $C9J2 ----> $D9J24 $C9J3 __ ...;._> $D9J35 $C9J4 ----> $D9J46 $C9J5 ----> $D9J57 $C9J6 ----> $D9J68 $C9J7 ----> $D9J7Figure 3-2.


18 APPLE II COMPUTER GRAPHICSThe post office box model does have a weakness; the information in theorigin<strong>al</strong> locations is not actu<strong>al</strong>ly taken out of the old boxes and moved tothe new locations, but rather a copy is made and then stored at the newaddresses. Hence the same v<strong>al</strong>ues actu<strong>al</strong>ly reside in both memory ranges,and the v<strong>al</strong>ues which were origin<strong>al</strong>ly in the destination addresses are destroyedwhen using the MOVE command.Now it is time to learn to do som<strong>et</strong>hing quasi-useful with the MOVEcommand: clear the text screen (memory locations $4-$7FF). Begin byplacing the Apple code for a blank, $A, into $3FF, by typing:3FF : ANext enter4 < 3FF . 7FEMand the entire screen will clear. You executed a MOVE command wher<strong>et</strong>he source range of memory ($3FF. 7FE) overlapped with the destinationrange ($4 . 7FF) . This can produce some startling results and warrants acloser look.The Monitor MOVE command works on one byte at a time and proceedsin ascending order. Therefore, the fi rst step copied the contents of$3FF (an A) into $4' the second step\copied the contents of $4 (now<strong>al</strong>so A) into $41, and so on as shown in Figure 3-3.STEP CONTENTS SHIFTED___ jt ________ J _______________ lQ. ___1 $3FF ----> $42 $4 ----> $413 $41 ----> $424 $42 ----> $435 $43 ----> $44958 $7FC ----> $7FD959 $7FD ----> $7FE96 $7FE ----> $7FFFigure 3-3.Though this process may seem a bit complex at fi rst blush, with a littlepractice it becomes both routine and usefu l.The memory map in Figure 3-1 shows that location $3FF is within anarea of memory used by DOS. To avoid any possibil ity of clobbering DOS,it is a good idea to replace the v<strong>al</strong>ue that was origin<strong>al</strong>ly there, so use whatyou have learned so far to put an $FF back in address $3FF.


CHAPTER 3-SYSTEM MONITOR-MEMORY TRICKS 19Other Monitor TricksThere are other Monitor commands, such as the command you use whenyou r<strong>et</strong>urn to BASIC by typing 3D0G . The 300 is the address marking thebeginning of a DOS routine, and the G says GO. However, the v<strong>al</strong>ue ofthose other commands to the BASIC-oriented <strong>graphics</strong> programmer is margin<strong>al</strong>,so we will not discuss them further here. If you are interested, referto Chapter 3 of the APPLE II Reference Manu<strong>al</strong> for more d<strong>et</strong>ail.VocabularyBufferMemory MapMonitorMOVEPrompt CharacterExercises1. Use the Monitor to examine the contents of locations $96YJYJ through$96FYJ.2. Examine memory locations $AA6YJ and $AA61 using a single Monitorcommand.3. Examine location $A4 without displaying the contents of any otherlocation.4. Load $FF' s into locations $CYJ5 and $CY,6.5. Move the contents of $4YJYJ through $7FF to the block of memorywhich begins at $61YJYJ.6. Load $YJYJ into each location from $2YJYJYJ through $J FFF.


4APP LESO FTExtensionsObjectivesAfter reading Chapter 4 you should be able to:• Use the PEEK statement to d<strong>et</strong>ermine the v<strong>al</strong>ue in any given memorylocation.• Use POKE to place given v<strong>al</strong>ues into given memory locations.• Use CALL and USR to execute a given machine level routine in memory.APPLESOFT BASIC contains a number of additions or extensions to thestandard s<strong>et</strong> of BASIC commands. Among those extensions are sever<strong>al</strong>commands relating to <strong>graphics</strong>, such as GR, HGR, HLIN, and PLOT. Althoughthese commands eQJbody the ease and simplicity of the BASIClanguage, they <strong>al</strong>so share its drawbacks-slow execution and limited accessto system memory. There is another s<strong>et</strong> of command extensions, thePEEK, POKE, CALL, and USR statements, which <strong>al</strong>low the APPLESOFTprogrammer a good de<strong>al</strong> of access to memory and machine-level operations;this in turn permits more control and faster execution of <strong>graphics</strong>effects.Take a PeekThe PEEK command does very closely what its name implies; it PEEKs atany given memory location to see what v<strong>al</strong>ue is stored there. PEEK interro-


22 APPLE II COMPUTER GRAPHICSgates memory from within a BASIC program, without having to enter theMonitor. The command PRINT PEEK (37) , for example, will print the contentsof the thirty-seventh location in memory. PEEK is a decim<strong>al</strong>-orientedcommand, so in the previous example both the address (37), and the v<strong>al</strong>ueprinted are in decim<strong>al</strong> form.Another application of the statement involves PEEKi ng to see if a key hasbeen pressed. This is usefu l when the <strong>computer</strong> is required to processcontinuously and still look for a sign<strong>al</strong> to interrupt, such as a change ofdata. Many of the <strong>computer</strong>-based arcade games provide examples of thissince they must continuously animate the figures on the screen, but stillcheck for keyboard, game paddle, and/or joystick input in order to receiv<strong>et</strong>he sign<strong>al</strong> to fi re, change the player's direction, and so on.To illustrate use of the PEEK statement, key in the program given inListing 4-1 . When you have fin ished keying in the program, run it. Whatdo you see? You should see the l<strong>et</strong>ter "L" being continuously printed onthe screen. Press another key. "*ie character which was pressed will nowbe pri nted until y<strong>et</strong> another key is pressed, and so on. Program executionmay be stopped by pressing the res<strong>et</strong> key.10 REM DEM0NSTRATE PEEK20 REM30 P$ = "L"40 REM50 REM TOP OF LOOP60 PRINT P$;70 P = PEEK ( 49152) : REM SEE IF KEY PRESSED80 IF P > 127 THEN PRINT CHR$(7);:P$ = CHR$ (P-128): POKE -16368, 090 GOTO 50Listing 4-1.The following line-by-line explanation should clarify the example and itsuse of the PEEK command.30 P$ = "L"Line 30 assigns P$ an initi<strong>al</strong> v<strong>al</strong>ue of L.60 PRINT P$;Line 60 prints the v<strong>al</strong>ue of P$. The semicolon suppresses the usu<strong>al</strong>ly automaticline feed so that the printing will continue across the screen.70 P = PEEK ( 49152) : REM SEE IF KEY PRESSEDLine 70 assigns P the v<strong>al</strong>ue in location 49152. Location 49152 has twofunctions: the first is to sign<strong>al</strong> when a key has been pressed, and thesecond is to temporari ly hold the numeric code for that key. When a key ispressed, the left-most bit in byte 49152 is turned on, so the byte has a


CHAPTER 4-APPLESOFT EXTENSIONS 23v<strong>al</strong>ue of 128 (or greater if any other bit in byte 49152 happens to be on).Also, the v<strong>al</strong>ue of the key pressed is stored in the other seven bits of byte49152. If the v<strong>al</strong>ue in location 49152 is greater than 127, then a key hasbeen pressed.8!i) IF P > 127 THEN PRINT CHR$(7) ; :P$ = CHR$ (P-128) : POKE 49168, !i)If a key is pressed, then printing CHR$ (7) will sound the bell, and P$ isreassigned to be the character pressed. CHR$ (P-128) translates the numericcode for the character into the actu<strong>al</strong> character. The POKE readies thekeyboard fo r the next input by changing the v<strong>al</strong>ue of location 49152 to av<strong>al</strong>ue less than 128.9!il GOTO 5!ilThis line begins the loop again.PDKE1n AlongThe POKE command <strong>al</strong>so states its own fu nction. It l<strong>et</strong>s you POKE a v<strong>al</strong>ueinto memory from within BASIC, without using the Monitor. The effect ofPOKE 37, 12 is to place the v<strong>al</strong>ue 12 into memory location 37. As with thePEEK statement, the arguments (37 and 12) are decim<strong>al</strong>-form numer<strong>al</strong>s. Asit happens, the contents of location 37 d<strong>et</strong>ermine the vertic<strong>al</strong> position ofthe cursor, so POKE 37,12 is equ iv<strong>al</strong>ent to VTAB (12).The program given in Listing 4-2 demonstrates a use fo r POKE whichhas direct application to <strong>graphics</strong>. Type in Listing 4-2 and run it. You willsee the Hi-Res screen fi ll with dots and lines, <strong>al</strong>l of this occurring withoutthe use of the HPLOT or HLINE commands. The program POKEs randomv<strong>al</strong>ues directly into locations within the Hi-Res screen memory area, sothat the dots corresponding to the "on" bits of the byte will appear on theHi-Res display. You will have to press res<strong>et</strong> to stop the program. In laterchapters you will c<strong>al</strong>culate some speci<strong>al</strong> v<strong>al</strong>ues, and create pictures on theHi-Res screen using this m<strong>et</strong>hod . ---... .,l!i) REM DEMONSTRATE POKE2!il REMJ!il HGR4!i) REM5!il REM TOP OF LOOP6!i) 1% = (RND ( l) * 8192) + 81927!il V% = (RND ( l) * 256)8!il POKE 1%, V%9!il GOTO 5!ilListing 4-2.The following line-by-line explanation should clarify the example.J!il HGR


24 APPLE II COMPUTER GRAPHICSs<strong>et</strong>s the Hi-Res <strong>graphics</strong> mode and clears the screen.6(il 1% = (RND ( l) * 8192) + 8192generates a random location b<strong>et</strong>ween 8192 ($2©©©) and 16383 ($3FFF),inclusive. This area of memory is reserved fo r page 1 of Hi-Res <strong>graphics</strong>.7(il V% = (RND ( l) * 256)generates a v<strong>al</strong>ue b<strong>et</strong>ween © and 255, inclusive.8(il POKE 1%, V%POKEs the v<strong>al</strong>ue into the memory location within screen memory to mak<strong>et</strong>he dots appear.9(il GOTO 5(ilbegins the loop again.C<strong>al</strong>l for HelpThe CALL statement is much like the APPLESOFT GOSUB, except that thesubroutine being CALLed is a machine language routine in memory insteadof a BASIC routine within your program. At the end of the subroutine,the machine code equ iv<strong>al</strong>ent of a RE:fURN statement r<strong>et</strong>urns to theinstruction which fo llowed the CALL in your-program. The use of machinelevel routines is helpful because they <strong>al</strong>low you more control over the<strong>computer</strong>, and are much faster than corresponding BASIC routines.Smooth animation virtu<strong>al</strong>ly requires the extra speed afforded by machinelevel subroutines. On the negative side, the workings of a routine writtenin machine code are usu<strong>al</strong>ly rather obscu re; and further, any errors inusing that routine can lead to any number of unpredictable and bizarreresults.The simplest use of the CALL statement is to CALL one of the routineswithin the Apple operating system. CALL -936 clears the text screen andmoves the cursor to the upper left corner-the same effect as the HOMEstatement. When one uses the HOME command, APPLESOFT essenti<strong>al</strong>lyissues a CALL to that same location. There are many other usable routineslisted in the APPLESOFT manu<strong>al</strong>, see pages 129-1 3©, and the APPLE IIREFERENCE MAN UAL, see pages 61-64.Another way to use CALL is to have your BASIC program POKE a machinelanguage program into memory, then invoke that routine using theCALL statement. Type in the program from Listing 4-3 by way of anexample. You will see the screen will fi ll with the inverse @ character. Thefirst portion of the program POKEs a subroutine into memory using a datatable, and then the subroutine is CALLed. The subroutine itself is of no re<strong>al</strong>v<strong>al</strong>ue because <strong>al</strong>l it does is fill the screen with inverse @ characters, but itdoes demonstrate the use of CALL.


CHAPTER 4-APPLESOFT EXTENSIONS 251 REM DEMONSTRATE CALL2 REM3 REM POKE THE MACHINE4 REM LEVEL ROUTINE5 REM INTO MEMORY6 REM7 FOR L = 768 TO 828 READ V9 POKE L , V1 NEXT L11 REM12 REM POKES COMPLETE13 REM14 PRINT "GET READY"15 FOR I = 1 TO 1 : NEXT I16 CALL 76817 VTAB (24) : PRINT "ALL DONE" :GOTO 2518 REM19 REM DATA TO POKE2 REM21 DATA 169, , 133, 254, 169, 7, 133,255, 16, 25522 DATA 162,4,32, 88,252, 165, 161, 145, 25423 DATA 198, 254, 28,252, 136, 28,247, 22, 24, 5, 198, 255, 7624 DATA 17,3,9625 ENDListing 4-3.To aid your understanding, each line is explained below.7 FOR L = 768 TO 828 READ V9 POKE L , V1 NEXT LThis loop reads the v<strong>al</strong>ues from the data table and POKEs them into memorylocations 768 through 8


26 APPLE II COMPUTER GRAPHICS17 VTAB (24) : PRINT "ALL DONE" :GOTO 2t\i't\fter the subroutine is compl<strong>et</strong>e, this line will print an end of job message:ind then jump to the END statement.21 DATA 169,,lj3 ,254,169,7,133,255,16,25522 DATA 162,4,32,88,252, 165,161,145, 25423 DATA 198, 254, 28,252,136,28,247, 22,24,5,198,255,7624 DATA 17,3,96Lines 210-240 constitute the data table of v<strong>al</strong>ues to be placed into memoryto fo rm the subroutine. They are READ by line 80 and POKEd by line 90.Using the technique presented above, any machine language programmay be POKEd into memory, and then executed from BASIC using theCALL statement.USRThe USR statement is similar to the CALL statement, in that it causes theexecution of a machine level routine in memory. Where CALL simplyexecutes the indicated routine, USR <strong>al</strong>lows the BASIC program some controlover the machine level routine by handing up to two bytes of informationto the subroutine before commencing execution. Further, when theroutine is fi nished, it passes a v<strong>al</strong>ue back to the BASIC program.The USR statement must be used within a larger BASIC statement, fo rexample, X = USR(8) or PRINT USR(247). The v<strong>al</strong>ue given within theparentheses is the decim<strong>al</strong> v<strong>al</strong>ue of data passed to the subroutine, andafter the subroutine is executed, USR( ) represents the v<strong>al</strong>ue which is givenback to the BASIC routine. Suppose that the two examples cited above usea subroutine which doubles any v<strong>al</strong>ue it is given. Then the statement X =USR(8) would assign X the v<strong>al</strong>ue 16, and PRINT USR(247) would print 494.Listing 4-4 is an <strong>al</strong>teration of Listing 4-3. Key in Listing 4-4 and run theprogram. You will see the messageWHAT CHARACTER DO YOU WISH TO PRINT?appear on the screen. Press any character key and then the RETURN key.After a short wait, the screen will fill with the character that you havechosen. When the screen has filled, a number is printed in the upper leftcorner of the screen; the "ALL DONE" message is printed near the bottom.Next, the messageANOTHER CHARACTER? (Y/N)will appear, giving you a chance to try another character.


CHAPTER 4-APPLESOFT EXTENSIONS 2710 REM DEMONSTRATE USR20 REM30 REM POKE THE MACHINE40 REM LEVEL ROUTINE50 REM INTO MEMORY60 REM65 POKE 10 , 76 : POKE 11, 0: POKE 12,370 FOR L = 768 TO 80580 READ V90 POKE L, V100 NEXT L110 REM120 REM POKES COMPLETE130 REM134 PRINT "WHAT CHARACTER DO YOUWISH TO PRINT? 11136 GET C$ : C = ASC ( C$) + 128140 PRINT "GET READY"150 FOR I = 1 TO 1000 : NEXT I160 PRINT USR(C)170 VTAB (22) : PRINT "ALL DONE"172 PRINT "ANOTHER CHARACTER? ( Y/N) ": GET R$174 IF R$ = "Y" GOTO 134176 IF R$ = "N" GOTO 250178 PRINT CHR$ (7) : GOTO 172180 REM190 REM DATA TO POKE200 REM205 DATA 32,12,225210 DATA 169, 0, 133 , 254, 169, 7, 133,255,160,255220 DATA 162,4,32,88,252, 165,161,145, 254230 DATA 198, 254,208,252,136,208, 247,202,240,5,198,255,76240 DATA 20,3,96250 END"''Listing 4-4.The character which you select is passed to the subroutine via the USRstatement. One caution: Pressing the "P" key while holding down theCTRL and SHIFT keys will generate a syntax error due to the constraintsof the ASC function used in line 136. The number that appears at the topof the screen is caused by the statement "PRINT USR (C)'', which actu-


28 APPLE II COMPUTER GRAPHICS<strong>al</strong>ly prints the v<strong>al</strong>ue r<strong>et</strong>urned by the subroutine. The subroutine in thisexample r<strong>et</strong>urns only garbage.The following <strong>al</strong>terations are those which have been made to Listing4-3:Lines changed: 1 (/), 7(/), 16(/), 17(/), 24(/)Lines added : 65, 134, 136, 172, 174,1 76, 1 78,2(/)5These differences are explained below.65 POKE 1, 76: POKE 11, : POKE 12, 3Line 65 places information into memory which is needed by the USRcommand. When BASIC encounters USR, it automatic<strong>al</strong>ly jumps to location1 (/), and from there to the location of the subroutine which is to beused. The particular v<strong>al</strong>ues POKEd here direct the <strong>computer</strong> to jump tolocation $3 'where lies the subroutine. Location 11 contains the secondbyte of the subroutine's addre\s, in this case $ 'and location 12 containsthe fi rst byte-$3. If the subroutine begins in a different location, say$C25, the v<strong>al</strong>ues 37 ($25 = 2*16 + 5) and 192 ($Cf/) = 12*16 + (/))would be POKEd into locations 11 and 12, respectively.7 FOR L =768 TO 85The loop goes from 768 to 8(/)5 instead of to 8(/)2 because of the three extradata v<strong>al</strong>ues which have been added in line 2(/)5. The effect of those threeextra v<strong>al</strong>ues is to accept the character code which is passed by the USRcommand.134 PRINT "WHAT CHARACTER DO YOUWISH TO PRINT? 11136 GET C$:C = ASC (C$) + 128These lines GET the character that the user wishes to pri nt and convert it toits Apple code v<strong>al</strong>ue.16 PRINT USR ( C)This line passes the selected character code to the subroutine and thenexecutes it. When the subroutine is fi nished, this statement <strong>al</strong>so prints thev<strong>al</strong>ue which is r<strong>et</strong>urned by the subroutine. In this case the v<strong>al</strong>ue printed ismeaningless and it will not be printed if line 16(/) is changed to read 16X =USR(C).17 VTAB (22) : PRINT "ALL DONE"The VTAB has been changed to 22 to <strong>al</strong>low program line 172 to print onscreen line 23, and the GOTO has been removed since the program nolonger ends at this point.172 PRINT "ANOTHER CHARACTER? (Y/N) 11 : GET R$


Introducing -Disk<strong>et</strong>te to Accompany Apple II Computer GraphicsKen Williams Bob Kernaghan Lisa KernaghanYo u can g<strong>et</strong> started in g ra phics right away, because <strong>al</strong>l theprogra m keyboarding has <strong>al</strong>ready been done for you!Th is companion software disk<strong>et</strong>te offers yo u faster and easier access to Apple 11 (and I l e!) <strong>computer</strong> <strong>graphics</strong>.J ust look at what you' ll enjoy:• All the programs (more than 25) listed in the book. Including generating shapes ...three types of graphs .full-spectru m color design and animation ... and more! Before you know it, you'll even be able to create yourown . simple <strong>computer</strong> games.• 20 to 25 v<strong>al</strong>uable hours saved! It would probable take more than one compl<strong>et</strong>e 24-hour day just to enter theprograms <strong>al</strong>one.With the disk<strong>et</strong>te, you could be creating graphic designs within minutes!• Eliminates <strong>al</strong>l those frustrating and mind-numbing keyboarding errors. Leaves your mind clear and your fingersnimble to tackle the excitement of design!Here's how to order your copy ofDisk<strong>et</strong>te to Accompany Apple II Computer Graphics.Enclose a check or money order fo r $1 2 .50 plus loc<strong>al</strong> s<strong>al</strong>es tax, slip in this handy order envelope - and mail!No postage needed. Or charge it to yo ur VISA or MasterCard . Just compl<strong>et</strong>e the information below.ORDER TODA Y!DYES! I want to begin gra phics right away! Please rush me Disk<strong>et</strong>te to AccompanyApple II Computer Graphics/031 89·0. I have enclosed payment of $1 2.50 plus loc<strong>al</strong>s<strong>al</strong>es tax.Name __________________AddressCharge my Credit Card InsteadD VISAD MasterCardCity __ State __ Z ip ___Account NumberExpiration DateSignature as it appears on CardBradyRobert J . Brady Co. • Bowie, MD 2071 5 ;A Prentice-H<strong>al</strong>l Publis hing and Communications Companylocations 462© through 4677.32, 88, 252, 169, 217, 32, 240, 253, 169, 207, 32, 240, 253 , 169,213 , 32, 240, 253, 169, 160, 32, 240, 253, 169, 196, 32, 240, 253,169, 201, 32, 240, 253, 169, 196, 32, 240, 253, 169, 160, 32, 240,253, 169, 201, 32, 240, 253, 169, 212 , 32, 240, 253, 169, 161, 32,240, 2533. Alter the program written in Exercise 2 so that it places the machinecode v<strong>al</strong>ues into memory and then executes them as a subroutine.


It took Michaelangelo 1460 days to paint theceiling of the Sistine Chapel.But it'll take you less than one to begin creatingyour own Apple-generated <strong>graphics</strong>!How? See over for d<strong>et</strong>ails . ..111111NO POSTAGENECESSARYIF MAILEDIN THEUNITED STATESBUSIN ESS REPLY MAILFIRST CLASS PERMIT N0. 1976 BOWI E, MDPOSTAGE WILL BE PA ID BY ADDRESSEERobert J. Brady Co.A Prentice-H<strong>al</strong>l CompanyBowie, Maryland 20715· · · - -· · · · ·oX =USR(C).·- . . .. .. ... "v' uc ,._,, .. ,lcu 11 1111 e 1 011J 1::. cnangea IO reaa lb!J17 VTAB (22) : PRINT "ALL DONE"The VT B has been changed to 22 to <strong>al</strong>low program line 172 to print onscreen line 23, and the GOTO has been removed since the program nolonger ends at this point.172 PRINT 11 ANOTHER CHARACTER? ( Y/N) ": GET R$


CHAPTER 4-APPLESOFT EXTENSIONS 29174 IF R$ = "Y" GOTO 134176 IF R$ = "N" GOTO 25(J178 PRINT CHR$ ( 7) : GOTO 172These lines inquire wh<strong>et</strong>her the user wants to try another character; if yesthen GOTO line 134 to restart, if no then GOTO the END statement, and ifneither then beep the bell and try for another response.2(J5 DATA 32,12,225The three new data v<strong>al</strong>ues discussed with line 7©.24(J DATA 2(J ,3,96The fi rst piece of data i this line is different than in Listing 4-3.The subroutine which prints the characters for Listings 4-3 and 4-4 isonly slightly faster than the BASIC program in Listing 4-1, which <strong>al</strong>so fi llsthe screen with a selected character. However, it is worth noting that themachine level subroutine used has been slowed down by a factor ofapproximately 125 so that the process will take a noticeable amount oftime. To help demonstrate the speed of execution fo r machine level routines,replace each of the fi rst four data v<strong>al</strong>ues in line 23(/J with the v<strong>al</strong>ue234, to disable the delay fu nction, and then run the modified program.VocabularyCALLPEEKPOKEUSRExercises1. Write a BASIC program to d<strong>et</strong>ermine the v<strong>al</strong>ues at memory locations1©2©, 1©21, 1©22, and 1©23.2. Write a BASIC program to poke the following v<strong>al</strong>ues into memorylocations 462(/J through 4677.32, 88, 252, 169, 217, 32, 24(J , 253, 169, 2(J7, 32, 24(J , 253, 169,213, 32, 24(J , 253, 169, 16(J , 32, 24(J , 253, 169, 196, 32, 24(J , 253,169, 2(Jl, 32, 24(J , 253, 169, 196, 32, 24(J , 253 , 169, 16(J , 32, 24(J ,253, 169, 2(Jl, 32, 24(J , 253, 169, 212 , 32, 24(J , 253, 169, 161, 32,24(J , 2533. Alter the program written in Exercise 2 so that it places the machinecode v<strong>al</strong>ues into memory and then executes them as a subroutine.


30 APPLE II COMPUTER GRAPHICS4. Write a BASIC program which plots sever<strong>al</strong> blocks or lines on theLow-Res screen, and then clears that screen by CALLing the ROMroutine located at $F832.5. Write a BASIC program which prints, on line 5 of the text screen, amessage of your choice in the inverse mode, and then pri nts, onscreen line 10, a second message in the norm<strong>al</strong> mode. Do not use theVTAB, INVERSE, or NORMAL commands, but instead use the POKEand CALL commands. The VTAB location is discussed in the POKEsection of this chapter, and the ROM routines to s<strong>et</strong> the INVERSE andNORMAL modes are located at $FE80 and $FE84, respectively.)


5Graphics Modes andSoft SwitchesObjectivesAfter reading Chapter 5 you should be able to:• S<strong>et</strong> any of the <strong>graphics</strong> modes and their variations by using the softswitches.Your Apple <strong>computer</strong> contains a chunk of hardware which is responsiblefor looking at portions of memory and translating what it fi nds there into avideo display. Within limits, you may d<strong>et</strong>ermine which area of memory isselected, and how the data there is interpr<strong>et</strong>ed . Data in memory may beinterpr<strong>et</strong>ed for video display in three diffe rent ways: as Text, Low-Res (LowResolution) <strong>graphics</strong>, or Hi-Res (High Resolution) <strong>graphics</strong>. Those threemodes, their ten variations, and the m<strong>et</strong>hod fo r selecting each will bediscussed in this chapter.Text MadeThe Text screen of an Apple II or Apple II+ comes from the factory able todisplay 24 lines of text on the screen with 4rand <strong>al</strong>low for the display of lower case l<strong>et</strong>ters, more than 24 lines per31


32 ·. APPLE II COMPUTER GRAPHICSscreen, and more than 4


CHAPTER 5-CRAPHIC MODES AND SOFT SWITCHES 33For those of you whose <strong>computer</strong>s store APPLESOFT in RAM, Hi-Res<strong>graphics</strong>, page 2 is not available for use. It may, however, be displayed ifyou would like to see a "picture" of BASIC.Sa Switches and YouThe BASIC commands used to s<strong>et</strong> the different display modes are suitablefor many applications, but they do have their drawbacks. Besides thenorm<strong>al</strong> problem BASIC statements have of being slow to execute, BASIChas no direct way for you to see fu ll page <strong>graphics</strong> on page one, mixed<strong>graphics</strong> on page two, or to switch back and forth b<strong>et</strong>ween the text and<strong>graphics</strong> screens without clobbering the <strong>graphics</strong>. The solution lies in s<strong>et</strong>tingthe proper <strong>graphics</strong> mode you rself instead of through BASIC, and thatis where soft switches play their part.The four switches that control the output mode are c<strong>al</strong>led soft becaus<strong>et</strong>hey are not physic<strong>al</strong> switches, but rather they are features of the operatingsystem. It is convenient to think of each soft switch as being a two-positiontoggle switch, as shown in Figure 5-1.DANGER! SOFT SWITCHES! HANDLE WITH CARE!GRAPHICS FULL SCREEN PAGE 1 LO-RESSELF$C050 $C052 $C054 $C056DESTRUCT(49232) (49234) (49236) (49238)u n u u nTEXT MIXED SCREEN PAG E 2 HI-RES$C051 $C053 $C055 $C057 RUN(49233) (49235) (49237) (49239)NUMBERS IN PARENTHESES ARE DECIMALFigure 5-1. Toggle switches.Each switch may be toggled in one of two diffe rent directions by referencingthe associated memory location.• Switch 1 d<strong>et</strong>ermines wh<strong>et</strong>her the screen will display in the Text modeor one of the <strong>graphics</strong> modes.• Switch 2 chooses b<strong>et</strong>ween the full screen <strong>graphics</strong> mode or mixedscreen <strong>graphics</strong> with four lines of text at the bottom. If switch 1 is s<strong>et</strong>to Text, then switch 2 has no visible effect.• Switch 3 selects which page will be displayed. Each display modeactu<strong>al</strong>ly has two entirely different screens which may be used.


34 APPLE II COMPUTER GRAPHICS• Switch 4 selects b<strong>et</strong>ween the Low"Res and Hi-Res modes. Switch 4 hasno visible effect unless switc h 1 is s<strong>et</strong> to display <strong>graphics</strong>.The APPLE lie has sever<strong>al</strong> addition<strong>al</strong> display-oriented soft switches.Those switches are discussed later in this chapter under the heading "Forlle's Only."Toggling So SwitchesDismembering your <strong>computer</strong> to look fo r these switches will not help youlocate them; remember, they are not a physic<strong>al</strong> switch, but only exist inthe system software. So how do you flip a switch that does not exist? Whenthe <strong>computer</strong> is turned on, the operating system automatic<strong>al</strong>ly togglesswitch one and switch three to display the primary page of text, but fromthere it is up to you.Each switch may be fli pped in the desired direction by referring to theassociated address. To s<strong>et</strong> switch 1 from BASIC to display <strong>graphics</strong>, youcould PEEK at the v<strong>al</strong>ue which is in location 49232, or POKE a new v<strong>al</strong>uein there. Neither the v<strong>al</strong>ue POKEd nor the the v<strong>al</strong>ue PEEKed is of anyconsequence; POKE 49232,0, X = PEEK (49232), PRINT PEEK (49232), andPOKE 49232, 175 <strong>al</strong>l s<strong>et</strong> switch 1 to <strong>graphics</strong> mode. It is the mere act ofreferring to that location which s<strong>et</strong>s the switch. From Monitor you can s<strong>et</strong>the <strong>graphics</strong> switch by typing C050 , so that Monitor willdisplay the v<strong>al</strong>ue in location C050, or type C050:0 to place a zero in thatlocation. Again, it is only the reference to the location which is important.Try s<strong>et</strong>ting the switches to view the fi rst page of Hi-Res <strong>graphics</strong> withoutthe four lines of text at the bottom and without clearing the screen. Referto Figure 5-2 fo r the associated addresses for each switch. Your sequenceof commands should look som<strong>et</strong>hing like this. From the BASIC prompt ( >or ])type:POKE 49234, (S<strong>et</strong> fu ll screen)POKE 49236, (S<strong>et</strong> page 1)POKE 49239, (S<strong>et</strong> Hi-Res mode)POKE 49232, (S<strong>et</strong> <strong>graphics</strong>)The order is not important, but if you s<strong>et</strong> the <strong>graphics</strong> mode fi rst (POKE49232,0), then the remainder of the POKEs will not be visible to you sinc<strong>et</strong>he text is no longer being displayed.To s<strong>et</strong> the same display sequence from Monitor, type:C52: C54: C57: C5:


CHAPTER 5-GRAPHIC MODES AND SOFT SWITCHES 35Switching to text page 1 may be accomplished by using only two of thesoft switc hes. You would type:From BASICPOKE 49236, 0POKE 49233 , 0From MonitorC056 : 0C051: 0The first line s<strong>et</strong>s switch 3 to display the primary page (page 1), and thesecond s<strong>et</strong>s switch 1 to display text. The s<strong>et</strong>tings of switches two and fourdo not matter since they affect only the <strong>graphics</strong> modes.A summary of the available video display modes appears in Figure 5-J.Practice s<strong>et</strong>ting each mode and its variations by the use of the softswitches; when you feel comfortable, proceed to the next chapter.Switch 1 $C050 ( 49232) Graphics$C051 ( 49233) TextSwitch 2Switch 3Switch 4$C052 (49234)$C053 (49235)$C054 (49236 )$C055 (49237)$C056 (49238 )$C057 (48239)Full ScreenSplit ScreenPage 1Page 2Low-ResHi-ResFigure 5-2. A summary of so switch addresses.Text (<strong>al</strong>ways full page)Page 1Page 2Low-ResPage 1MixedFull pagePage 2MixedFull pageHi-ResPage 1MixedFull pagePage 2MixedFull pageFigure 5-3. Summary of available modes.


36 APPLE II COMPUTER GRAPHICSFor I/e 's OnlyThe APPLE lie uses three addition<strong>al</strong> soft switches to help control the display.These switches are referred to by the names 80COL, 80STO RE, andALTCHARSET.80COL d<strong>et</strong>ermi nes wh<strong>et</strong>her the text is displayed with the norm<strong>al</strong> 40columns per line or with 80 columns per line. (You must have an 80column card inst<strong>al</strong>led to display 80 columns per line!) To s<strong>et</strong> the 40column mode, POKE a v<strong>al</strong>ue (any v<strong>al</strong>ue) into 491 64 ($C00C). To enabl<strong>et</strong>he 80 column mode, POKE location 49165 ($C00D).80STORE d<strong>et</strong>ermines wh<strong>et</strong>her addresses referencing memory locationswithin the first page of text (1024 to 2047) actu<strong>al</strong>ly refer to memory on themain memory boara, or memory on the auxil iary 80 column board. Doyou have the feeling that you missed som<strong>et</strong>hing? L<strong>et</strong>'s back up a bit.Allowing an 80 column display doubles the number of characters whichare displayed on one page of text. This requires twice the amount ofmemory. The Apple lie designers had to fi nd an addition<strong>al</strong> 1024 bytes ofmemory somewhere, without disturbing the existing memory ranges usedin the APPLE II +. The solution was to "clone" Page 1 of Text memory andput the duplicate memor/ on the 80 column board. But now there are twopost office boxes with the address 1024, two boxes addressed 1025, and soon through 2047. When your c " omputer "postman" is told to deliver av<strong>al</strong>ue to one of these addresses (for example, address 1777), he or shemust know which of the two boxes having that address is to receive thev<strong>al</strong>ue. The 80STORE switch acts as a mini ZIP code; it specifies which ofthe two banks of memory, main or auxiliary, is intended . POKEing anyv<strong>al</strong>ue into 49152 ($C000) will route <strong>al</strong>l addresses to memory on the mainboard, and POKEing into 49153 ($C©©1) routes addresses from 1 ©24through 2©47 to the memory on the 8© column board .ALTCHARSET switches the character generator (the thing that takes thenumeric code for an A and makes it appear as A on the on the screen)b<strong>et</strong>ween the standard character s<strong>et</strong> (American characters) and some onboard,<strong>al</strong>ternate character s<strong>et</strong>. For, though it may come as a surprise to8©COL8©STOREALTCHARSET$C!iJ!iJC ( 49164)$C!iJ!iJD ( 49165)$C!il!il!il ( 49152)$C!il!ill ( 4915 3)$C!iJ!iJE ( 49166)$C!iJ!iJF ( 49167)4© Columns8© ColumnsStore in main memoryStore in auxiliary memoryStandard character s<strong>et</strong>Alternate character s<strong>et</strong>Figure 5-4. Summary of Apple lie so switches.


CHAPTER 5-CRAPHIC MODES AND SOFT SWITCHES 37some Americans, not every country's <strong>al</strong>phab<strong>et</strong> consists of our 26 ABCs.This switch, however, does little for the BASIC <strong>graphics</strong> programmer, sowe will not be discussing it fu rther.In addition to the added switches, the Apple lie <strong>al</strong>lows the programmerto d<strong>et</strong>ermine the s<strong>et</strong>ting of any of the seven switches by PEEKi ng thecontents of a related location. This way you can discover if the machine iss<strong>et</strong> to Page 1 or Page 2, Text or Graphics, Hi-Res or Low-Res, and so forth,without actu<strong>al</strong>ly POKEing any of the associated soft switches. For morediscussion of these test locations, please refer to your APPLE lie manu<strong>al</strong>.VocabularyASCIIFull screen <strong>graphics</strong>Hi-Res modeLow-Res modeMixed screen <strong>graphics</strong> ... _,Primary pageSecondary pageSoft switchExercisesS<strong>et</strong> the given display mode once using BASIC, and again using Monitor.1. Text mode, page 2.2. Full screen Low-Res mode, page 1.3. Mixed screen Hi-Res mode, page 2.4. Full screen Hi-Res mode, page 1.


396Text and Low-ResObjectivesAfter reading Chapter 6 you should be able to:• Convert any pair of Low-Res colors into the corresponding v<strong>al</strong>ue, bothdecim<strong>al</strong> and hex.• Place any l<strong>et</strong>ter or pair of blocks at any given location on the Text orLow-Res screens, and do so using both Monitor and the POKE statement.• Use the Low-Res editor to develop figures.• Save the Low-Res screen using BSAVE or the Monitor WRITE command.Although many programmers have an aversion to <strong>graphics</strong> in the Low­Res mode, a number of respectable programs have employed Low-Reswith success. The "Little Brick Out" (on your DOS 3.3 system master) and"Lemonade" games are good examples of what can be done with Low-Resand a little imagination. We will demonstrate how the Text andLow-Res modes work, and how you may easily create and display Text andLow-Res figures.L<strong>et</strong>'s begin our exploration of Low-Res <strong>graphics</strong> by examining the Textmode a bit closer.TextTo start with, type in Listing 6-1, which is designed to fill the screen with


40 APPLE II COMPUTER GRAPHICSthe l<strong>et</strong>ter "A" without using a PRINT statement. It is similar in effect toListing 4-3, but uses no machine language subroutine.10 HOME20 FOR I = 1024 TO 204 730 POKE I ,193 : REM 193 IS "A"40 FOR J = 1 TO 30 :REM DELAY50 NEXT J60 NEXT I70 CALL 65338: REM BEEP SPEAKER80 GOTO 80Listing 6-1.After clearing the screen, the program proceeds to POKE the numericcode for "A" into every memory location from 1024 through 2047 ($400-$7FF), which is the range displayed on the Text/Low-Res screen. You willsee the screen fi ll with "A's"; the speaker will beep when the screen is fu ll.In order to regain control of your machine you will have to press.Try replacing the number 30 in line 40 with a diffe rent v<strong>al</strong>ue. The higherthe number you select, the slower the screen will fi ll. Also try changing thev<strong>al</strong>ue POKEd in line 30; fo r example, using a 1 instead of 193 will fi ll thescreen with inverse "A's".Notice that the screen fi lls in three separate pieces, even though theprogram fi lls memory sequenti<strong>al</strong>ly. The fi rst line of A's appears at the top ofthe screen, the second appears <strong>al</strong>most a third of the way down the screen,and the third line appears roughly a third below the second. The fourthline fi lls under the fi rst, the fifth under the second, and so on until thescreen is fu ll. This three-part fi lling sequence is not just coincidence, but ismore of a curse which haunts anyone de<strong>al</strong>ing with <strong>graphics</strong>. It makesd<strong>et</strong>ermining the address of any location on the screen rather difficult. Thereason fo r the peculiar design of screen memory is tied up with the electronics,though in simple terms it was easier to wire the <strong>computer</strong> tog<strong>et</strong>herthat way. As you can see, however, locating the proper address to match adesired position on the screen could turn into a problem.This problem is usu<strong>al</strong>ly solved by the use of a memory map such as theone in Figure 6-1 .Finding Your WayTo use the map, fi rst locate the position that you want on the map, say thesecond row down and the third box from the right. The address of thatspace on the text screen is found by adding the row and column numberswhich are found to the left of the chosen box and directly above it, in this


CHAPTER 6-TEXT AND LOW-RES 41exam ple 1152 + 37 = 1189. The hex address is $48© + $25 = $4A5.(Remember: $8 + $2 = $A.)Example 1Use POKE to write "ACROSS" horizont<strong>al</strong>ly on the text screen, beginningin the third row and twenty-fi rst col umn. - - - - - - - - - - - - - - - - - - - - -s oo w N - OO w - s N - Ss - oo w s w s N N w s oo NS N OO S N S N OO S N S N OO S N 10II12131415161718192021222324252627282930313233343536373839$00$0 1$02$03$04$05$06$07$08$09$0A$0Bsoc$00$0E$0F$10Si l$12$13$14$15$16$17$18$19SIASIBSICSIDSIESIFS20S2 1S22S23S24S25$26$27Figure 6-1. Text/Low-Res memory map.(Used with permission of Apple Computer Corporation)


42 APPLE II COMPUTER GRAPHICSSolutionRow three, column twenty-one corresponds to the decim<strong>al</strong> address13


CHAPTER 6-TEXT AND LOW-RES 43Lost and FoundWhile running the program (Listing 6-1 ), you may have <strong>al</strong>so noted thatafter each line drawn on the bottom third of the screen, there was a slightpause before the fi lling continued on the upper third. That happens becaus<strong>et</strong>here are eight bytes of "phantom storage" following each line inthe lower portion. Those bytes exist in memory, but are not displayed onthe screen. To illustrate, c<strong>al</strong>culate the address of the right-most box in thesecond row from the bottom of Figure 6-1 . You should fi nd it to be 1872+ 39, or 1,91 1. The memory that fo llows that is the eighth line from th<strong>et</strong>op which commences with location 1, 920, so locations 1, 912 through1,919 are "lost." Lost, but not forgotten, for Apple has put that wastedmemory to use within DOS to remember which drive was most recentlyaccessed. As a matter of fact, the next time that you use your disk afterrunning this program, you will notice that your disk makes that fearsomegrunting noise as it rec<strong>al</strong>ibrates.There are other games to play with Text, such as saving the compl<strong>et</strong>escreen, but since Text is so similar to Low-Res, we might as well discussthose tricks under the more colorfu l guise of ....Low-Res GraphicsAlthough Low-Res <strong>graphics</strong> are not as popular as they once were, they stillhave their uses. Low-Res may be the proper option if your major concernsare simplicity, speed, color, and memory conservation; and if the blockynature of the mode is not a debil itating obstacle to you.Low-Res <strong>graphics</strong> use the same area of memory and the same memorymap as Text, so use of the two modes is similar. Alter the program inListing 6-1 by changing line 10 to read:1 GR : REM SET GRAPHICS DISPLAYRun the modified program. The <strong>graphics</strong> screen will be fi lled with pairs oflittle boxes-a magenta box on top of a green box. (The precise colorsmay vary with individu<strong>al</strong> TV s<strong>et</strong>s.) The text window at the bottom of thescreen will sti ll fi ll with "A's."Add a line to the program:15 X = PEEK ( 49234)and run it again. Line 15 s<strong>et</strong>s the soft switch to display fu ll screen <strong>graphics</strong>,so you will see the pairs of blocks <strong>al</strong>l the way down the screen with no textwindow.How1d Yau Do That?Line 30 POKEs the v<strong>al</strong>ue 193 ($C1) into every address in Low-Res memory.


44 APPLE II COMPUTER GRAPHICSEach byte of Low-Res memory d<strong>et</strong>ermines two colored blocks. Refe r to theLow-Res color chart in Figure 6-2, and you will find that $C is the Low-Rescode for the light green block, while $1 is the code fo r the magenta block.In Low-Res, the bottom block is d<strong>et</strong>ermined by the left-hand nibble, andthe top block is d<strong>et</strong>ermined by the right-hand nibble. Using only a nibbl<strong>et</strong>o d<strong>et</strong>ermine each block leads to some inconvenience because memorycan be controlled one byte at a time, and no less. This means that c<strong>al</strong>culatingthe correct combination of two nibbles fo r the proper two boxes isoften confusing.$0 (0) BLACK$1 (1) MAGENTA$2 (2) DARK BLUE$3 (3) PURPLE$4 (4) DARK GREEN$5 (5) GREY$6 (6) MEDIUM BLUE$7 (7) LIGHT BLUE$8 (8) BROWN$9 (9) ORANGE$A (10) GREY$B (1 1) PINK$C (12) GREEN$D (1 3) YELLOW$E (14) AQUA$F (15) WHITEFigure 6-2. Low-Res color codes.Example 3Suppose that you want to place a grey block over a yellow block atlocation 1028. First, you must c<strong>al</strong>culate the v<strong>al</strong>ue that will give you thedesired combination. Again referring to Figure 6-2, yellow is color number$D and grey is number $5. Arrange the digits as $D5 so that grey willbe on top, and convert $D5 to decim<strong>al</strong> 213. From BASIC type:GRPOKE 1(328,213The grey and yellow pair of blocks will appear near the upper left cornerof the screen. You can use Monitor to achieve the same end by using thehex equ iv<strong>al</strong>ent of the numbers. Still from BASIC, type:GRCALL - 15 14(J4: D5Experiment with Low-Res by plotti ng various combinations of coloredblocks in diffe rent locations.Creating Low-Res PicturesCreating a drawing in either Low-Res or Hi-Res requires a good de<strong>al</strong> of


CHAPTER 6-TEXT AND LOW-RES 45preliminary planning. We will take you through the process of creating aLow-Res <strong>apple</strong> (what else?!).The first step is to make a sk<strong>et</strong>ch of the figure, in this case an <strong>apple</strong>.Those of you that claim no artistic abil ity can fi nd patterns to tracechildren'scolori ng books and cross-stitch or embroidery patterns makegood sources of simple figures. Figure 6-3 represents the preliminarysk<strong>et</strong>ch of the <strong>apple</strong>.Figure 6-3. An <strong>apple</strong>.After your sk<strong>et</strong>ch is compl<strong>et</strong>ed to your satisfaction, the outline must beblocked to fit with the Low-Res bricks as in Figure 6-4. Notice that the leafwas omitted in the blocked figure; it proved impossible to block in arecognizable manner. It is b<strong>et</strong>ter to identify that type of problem befo reyou begin working with the <strong>computer</strong>!Blocking is not as simple as it might be since the Low-Res blocks are notsquare. The height of each brick is approximately two-thirds its length, andthat distortion must be accounted fo r. Perhaps the simplest technique fo rproportioning the blocks is to use very fi ne graph paper-perhaps tensquares to the inch-and defi ne each block to be three squares long andtwo squares high. Place your sk<strong>et</strong>ch over the graph paper and go over theoutline again while applying fi rm pressure with the pen. This will leave animpression of your outline on the blocking paper, and then you maysquare up the outline as in Figure 6-4.


..46 APPLE II COMPUTER GRAPHICS[ b ..(_·-- -·t--t-t--t--+-+-1t--t--t-+-···;···+ ·! ···+ + + ·; ·+· !·-+-+-+ +- +-, +-+-+-t-,_,._._,c-; -1 -··· t- + - 1 t -+ ··lI · + ·· ! + ··! t-+-t-t- ····'--1-+-+J.+-+-+-+- +·+ ·+ ·! · l ·· +··· i···+··-·I· ·+ -+-+-+; -++-++ _,._,_-.-.+-+-• +1+•+•++++-+ t-+-i t-+- +-+-+-+•-++ + + +±+ +++ - r+-+-f+-•H I•-t ·-······i + --f··- · ·+··· +-··+ ·++ ++-+ -+-+-i; --t-t- r--t-1-t- - --+++-···1-·····f···+· +- ·· + + +++ __,_f.,..!-+-· +-+-+···+···-·•··-·+···-!···-+ + ·+-+·+-i·-··+-t ++-+--+-+-+-+-+-+-! t-J:>-i I ·-]' J!--' J. -H t t-· t-++ +· + - ·l· · + -···!-- +-+-+1 +-+-+-:r+-+- 1 JIFigure 6-4. Apple block diagram.Next, decide which of the Low-Res colors you wish to use in your <strong>apple</strong>and indicate them on your block drawing. With <strong>al</strong>l of that done, you arenow ready to begin putting the picture onto the screen. There are twom<strong>et</strong>hods of accomplishing this. The fi rst technique is to make use of thePLOT, HLIN, and VLIN statements and place those within your program;the second involves using a Low-Res editor program (described later in this<strong>apple</strong> directly onto the screen, and then record thecontents of Low-Res memory..chapter) To plot theHatching a PlatThe brute-force m<strong>et</strong>hod uses the PLOT, HLIN, and VLIN commands; simplysit down with the blocked figure and develop a list similar to Listing6-2 which creates the <strong>apple</strong> by drawing an obnoxious number of horizont<strong>al</strong>lines.1 REM PLOT APPLE


CHAPTER 6-TEXT AND LOW-RES 472 REM3 GR4 COLOR = 45 PLOT 2, 16 VLIN 11, 14 AT 217 COLOR = 128 HLIN 17, 19 AT 139 HLIN 24,26 AT 131 HLIN 16,2 AT 1411 HLIN 23,27 AT 1412 HLIN 15,27 AT 1513 COLOR = 1314 HLIN 15,26 AT 16150 HLIN 15,25 AT 1716 HLIN 14,25 AT 1817 COLOR = 918 HLIN 14,25 AT 1919 HLIN 14 ,25 AT 22 HLIN 14 ,26 AT 2121 COLOR = 122 HLIN 14 ,26 AT 2223 HLIN 14 ,27 AT 2324 HLIN 14 ,27 AT 2425 COLOR = 3 ·26 HLIN 15 ,26 AT 2527 HLIN 16,25 AT 2628 HLIN 16,25 AT 2729 COLOR = 63 HLIN 17,24 AT 2831 HLIN 17,24 AT 2932 HLIN 18, 19 AT 333 HLIN 22,23 AT 3Listing 6-2. Apple drawn using VLIN and HLIN.The PLOT was used for the top, VLIN for the remainder of the stem, andsince the colors in the <strong>apple</strong> run horizont<strong>al</strong>ly, HLIN was used fo r the restof the figure. Which of the commands you use most will depend solely onthe figure that you want to draw. This technique has obvious drawbacks ifthe picture is composed of many isolated blocks and few horizont<strong>al</strong> orvertic<strong>al</strong> lines. In any event, it is quite tedious for the programmer; anaspect only parti<strong>al</strong>ly b<strong>al</strong>anced by the efficiency with which the program isexecuted.A variation on this technique is to place an HLIN statement within a loopand read the individu<strong>al</strong> numbers from a data table as shown in Listing 6-3.1 GR2 COLOR = 4


48 APPLE II COMPUTER GRAPHICS3 PLOT 2, 14 VLIN 11, 14 AT 215 COLOR = 126 FOR I = 1 TO 57 READ Xl , X2, Y8 HLIN Xl, X2 AT Y9 NEXT I1 COLOR = 1311 FOR I = 1 TO 335 DATA 17 , 19, 13 ,24 , 26, 13 , 16,2, 14, 23 , 27, 14, 15 ,27 , 1536 DATA 15, 26, 16, 15, 25, 17, . ..Listing 6-3.Although this is easier for the programmer, the advantage of speedyexecution has been sacrificed. Unless the program is to be run only a fewtimes, it is usu<strong>al</strong>ly b<strong>et</strong>ter fo r the programmer to expend the extra effort inorder to enhance the efficiency of the execution.De<strong>al</strong>ing with MemoryThe basic scheme behind this second technique and its variations is todraw your figure on the screen initi<strong>al</strong>ly, and then save the contents of Low­Res screen memory. Then, when you wish to use that figure in a program,you need only replace the contents of memory with the data <strong>al</strong>readysaved.The fi rst step is to draw your picture on the screen. This may be doneusing PLOT, HLIN, and VLIN as in the previous discussion, or by using aneditor. An editor is a program which simplifies creating, <strong>al</strong>tering, andsaving either text or <strong>graphics</strong>. A sample Low-Res editor is provided inListing 6-4. The editor will l<strong>et</strong> you plot and erase blocks in any of the Low­Res colors anywhere on the screen, except in the bottom four lines of text.1 HOME : GR2 PF = :X = 2 :Y = 23 GOSUB 234 C = PEEK ( 49152) : IF C > 127 THENC$ = CHR$ (C-128) : POKE -16368, :GOTO 65 PLOT X, Y: COLOR = : PLOT X, Y:COLOR = CN : GOTO 4


CHAPTER 6-TEXT AND LOW-RES 4960 REM70 IF PF THEN PLOT X ,Y:PF = 080 IF C$ = "J" AND X > 0 THEN X X-1:GOTO 19090 IF C$ = "I" AND Y > 0 THEN Y Y-1:GOTO 190100 IF C$ = "K" AND X < 39 THEN X X + 1:GOTO 190110 IF C$ = "M" AND Y < 39 THEN Y Y + 1:GOTO 190120 IF C$ = "U" AND X > 0 AND Y > 0 THENX = X-l :Y = Y-1: GOTO 190130 IF C$ = "0" AND X < 39 AND Y > 0 THENX = X + l :Y = Y-1: GOTO 190140 IF C$ = " , " AND X < 39 AND Y < 39 THENX = X + 1: Y = Y + 1: GOTO 190150 IF C$ = "N" AND X > 0 AND Y < 39 THENX = X-l :Y = Y + 1: GOTO 190160 IF C$ "P" THEN PF = 1: GOTO 190170 IF C$ = "Q" THEN GOTO 280180 IF C$ = "C" THEN GOSUB 230190 GOTO 40 : REM BACK TO TOP OF LOOP200 REM210 REM SET COLOR220 REM230 VTAB (24) : INPUT "NUMBER FOR NEWCOLOR:?";CN$240 CN = VAL (CN$ ) : IF CN < 0 OR CN > 15THEN PRINT CHR$ (7) : GOTO 230250 PRINT : PRINT : PRINT : PRINT"COLOR = "; CN260 COLOR = CN270 RETURN28i;l REM290 REM300 PRINT "QUIT? (Y/N) "; : GET R$310 IF R$ < > "Y" GOTO 40320 ENDListing 6-4. Low-Res editor.This editor is not elaborate, but it does give you the fu nctions necessaryto plot and edit pictures on the Low-Res screen, and it is easily modified tosuit your individu<strong>al</strong> requirements.When you run the editor, you will fi rst be asked to enter the number ofthe color to be plotted. The <strong>computer</strong> will ask:NUMBER FOR NEW COLOR :?


50 APPLE II COMPUTER GRAPHICSType the decim<strong>al</strong> number fo r the color you want (see Figure 6-2} and thenpress r<strong>et</strong>u rn. You will then see a flashing cursor in the center of the screen.The cursor may be moved anywhere on the screen by using the keysshown below.uI()\ I\ /\ /\!/J----* .. ··· -·- ·+:/!\/ \I \/ \Figure 6-5. Cursor movement.The I, M, J, and K keys move the cursor up, down, left, and right; the U,0, N, and , keys move the cursor diagon<strong>al</strong>ly as indicated . Try it!!The editor <strong>al</strong>so uses the P, C, and Q keys. To plot a point, press P andthen move the cursor out of the way. Press C and you will be given theopportun ity to s<strong>et</strong> the color as you did at the beginning of the program.Press Q, and you will be askedQUIT? (Y/N)Press Y if you wish to quit and N if you do not.Any color block may be erased by simply covering the offending blockwith the cursor. That makes erasing very simple-som<strong>et</strong>imes too sim ple,but you will g<strong>et</strong> used to it.Use the editor to copy the <strong>apple</strong> from Figure6-4.Saving FiguresNow that you have a picture on the screen, you probably would like to beable to save it. Assuming that you have a disk drive, type


CHAPTER 6-TEXT ANO LOW-RES 51BSAVE APPLE, A$400,L$400This statement causes the <strong>computer</strong> to save $400 bytes of memory beginning with location $400 . The A param<strong>et</strong>er indicates the starting address,and the L param<strong>et</strong>er gives the length of the memory range to be BSA VE .The Low-Res screen memory begins at $400 and is $400 bytes long. If youdo not care to work in hex, you may typeBSAVE APPLE, A1024, L1024and do the same thing since $400 equ<strong>al</strong>s 1024 .With the screen safe ly saved on the disk, you may rec<strong>al</strong>l it at any time.To demonstrate, type:BLOAD APPLEand the <strong>apple</strong> will reappear on the screen. The A and L param<strong>et</strong>ers may beused with BLOAD, but they are not necessary.You may BLOAD the picture from within a BASIC program by inserting <strong>al</strong>ine in the program similar to line 15 below. Load the Low-Res editor andtype the fo llowing:15 PRINT CHR$ (4) "BLOAD APPLE"When you run the editor it will now load the <strong>apple</strong> picture automatic<strong>al</strong>ly.This is handy if you wish to re-edit an existing picture.If you do not have a disk drive, you can save the screen memory oncass<strong>et</strong>te tape by using the Monitor. Type:CALL -151400 .7FFWto tell Monitor to write (hence the W) the range of memory from $400through $7FF onto the tape. To insure an accu rate record ing, it is a wiseidea to put the tape on "record" and l<strong>et</strong> it run fo r a few seconds b<strong>et</strong>weenpressing the W key and the r<strong>et</strong>urn key. After the Low-Res screen is savedon the tape, you can rewind the tape and load it from Monitor by typing400 .7FFRThe format is the same as the Write command, except the Wis replaced bythe R or READ command.If you have saved and loaded the screen as suggested, you may havenoticed that when the screen is loaded, it brings with it some garbage inthe text window. The BSAVE command saves the whole screen, includingwhatever text was in the text window at the time. That may be circumventedby writing a short BASIC program to fi rst clear the text window andthen save the screen. That program is left to the reader as an end of thechapter exercise.·


52 APPLE II COMPUTER GRAPHICSVocabularyBLOADBSAVEEditorHIMEMHLINPLOT.VLINExercises1. Convert the following Low-Res color pairs into the hex v<strong>al</strong>uenecessary to plot the first over the second.a. dark blue/green d. orange/purpleb. green/light blue e. brown/blackc. aqua/pink f. black/brown2. Convert the color pairs in Exercise 1 to their decim<strong>al</strong> v<strong>al</strong>ues.3. Use the POKE statement to plot the following color pairs at the givenlocation. The location is given by its row and column on the screenand memory map (see Figure 6-1).a. white/green at row 5, column 2©b. orange/black at row 19, column 1 ©c. aqua/pink at row 1 ©, column 124. Use Monitor to perform the plotting discussed in Exercise 3 above.5. Use the POKE command to place the message "HI-SCORE" in thebottom right corner of the Low-Res text window.6. Write a .BASIC program to BSAVE the Low-Res screen after clearingthe text window.7. Design and plot a picture of a spaceship on the Low-Res screen.8. Save the figure plotted in Exercise 7 using BSA VE (or the MonitorWrite command).


7Preserving YourPicturesObjectivesAfter reading Chapter 7 you should be able to:• Save the Low-Res screen using BSAVE, print a list of v<strong>al</strong>ues, write thelist of v<strong>al</strong>ues to a text file on disk, or create a text fi le of DATAstatements on disk.• Replace a figure on the Low-Res screen by using BLOAD, read the textfile of v<strong>al</strong>ues from disk and POKE them into screen memory, POKEv<strong>al</strong>ues from DATA statements-either keyed or EXECed nto the program,or load the screen v<strong>al</strong>ues into a safe memory area and thencopy those v<strong>al</strong>ues to Low-Res screen memory.In Chapter 6 you discovered how to save the Low-Res screen usingBSAVE or the Monitor WRITE command. Both m<strong>et</strong>hods were simple, butthey both have the same drawback-they often save a great de<strong>al</strong> of emptyspace. Since disk space is a precious commod ity, we will devote thischapter to discussing <strong>al</strong>ternate m<strong>et</strong>hods for saving your pictures.Scanning MemoryOne <strong>al</strong>ternate m<strong>et</strong>hod of saving your picture is to record which bytes ofLow-Res memory are turned "on," and what v<strong>al</strong>ues they contain. In the<strong>apple</strong> picture in Chapter 6, only about one of every ten bytes contains anon-zero v<strong>al</strong>ue. That is, nine-tenths of the screen is blank, and we areinterested only in the non-blank portions-those that are displaying a53


54 APPLE II COMPUTER GRAPHICScolor. The following program searches through Low-Res memory andpri nts a list giving the location of every non-zero byte and its address. Theprogram presumes that your printer is in slot 1, but that may be changedby <strong>al</strong>teri ng the v<strong>al</strong>ue assigned to SL in line 4(/J. If you do not have a pri nter,s<strong>et</strong> SL equ<strong>al</strong> to Ill.10 REM PRINT LOW-RES VALUES20 REM30 RE_M40 SL = 1: REM SL PRINTER SLOT #50 PR# SL60 REM70 REM80 FOR I = 1 TO 2090 READ S: REM STARTING ADDRESS OFSCREEN LINE100 FOR L = S TO S + 39110 IF PEEK (L) < > 0 THEN PRINT L ;II ' II ; PEEK (L) : COUNT = COUNT + 1120 NEXT L130 NEXT I140 PRINT "COUNT = II ; COUNT ; "EOJ"150 PR# 0160 END170 REM180 REM DATA TABLE190 REM200 DATA 1024, 1152, 1280 , 1408,1536210 DATA 1664,1792 , 1920 , 1064, 1192220 DATA 1320 ,1448,1576,1704,1832230 DATA 1960 ,1104,1232,1360 ,1488Listing 7-1. Low-Res scan #1.The program uses a data table to straighten out the convolutions of screenmemory. Each piece of data represents the starting address for a line onthe screen (see Figure 6-1), and the 4(/) bytes beginning with each of thoseaddresses are scanned by the FOR-NEXT loop in lines 1(/J(/J through 12(/J.This m<strong>et</strong>hod scans the screen from top to bottom, and without scanningthe memory which is not displayed. Line 11(/J pri nts the location (L) andconte nts (PEEK L) of any non-zero byte, and increments a counter. Onlythe top 2(/) lines are scanned, so none of the v<strong>al</strong>ues in the text window arepri nted. After the scanning is compl<strong>et</strong>e, line 14(/J prints the number of "on"bytes, and an end of job message. Scanning the <strong>apple</strong> will give you a list ofclose to 1 (/)9 addresses and v<strong>al</strong>ues, depending on where you placed your<strong>apple</strong> on the screen.Try the program out by loading it into memory and then typing:


CHAPTER 7-PRESERVING YOUR PICTURES 55GRBLOAD APPLERUNThis will (1) s<strong>et</strong> the <strong>graphics</strong> screen, (2) load the picture of the <strong>apple</strong> backo nto the screen from disk, and (3) run the program in memory (hopefu llyListing 7-1 ). The list of addresses and v<strong>al</strong>ues will appear on your printer. Ifit does not, check your program against Listing 7-1 and make sure thatyou have the printer slot assigned correctly in line 40.What ta Do with DataThe list of addresses and v<strong>al</strong>ues printed by the Low-Res scan #1 may bewritten into a program as a data table and POKEd back into memory usinga BASIC subroutine similar to:l(il(il(il FOR I = 1 TO l(il71(il1(il READ L,V: POKE L ,V1(il2(il NEXT Il(il3(il RETURN1(il4(il DATA 1684,4,1685,64,1809,192,Listing 7-2.The data v<strong>al</strong>ues in line 1040 are those generated from Listing 7-1 . Thism<strong>et</strong>hod has the advantage of not using up disk space, but it has probablyoccurred to you that entering <strong>al</strong>l of those data v<strong>al</strong>ues by hand is veryinefficient, not to mention boring and prone to error. We have two <strong>al</strong>ternativesfor those of you with a disk drive.The Data FileInstead of printing the addresses and v<strong>al</strong>ues on paper, you can write themto a text file on the disk. Load Low-Res Scan #1 (Listing 7-1) and then typ<strong>et</strong>he following lines to modify it.1(il REM WRITE DATA FILE35 D$ = CHR$ (13) + CHR$ (4)4(ll INPUT "FILENAME :";FL$45 PRINT D$ "OPEN" FL$5(il PRINT D$ "DELETE" FL$55 PRINT D$ "OPEN" FL$6(il PRINT D$ "WRITE" FL$1l(il IF PEEK (L) < > ill THEN PRINT ,L :PRINT PEEK (L)14(il PRINT "* "15(il PRINT D$ "CLOSE" FL$155 PRINT CHR$ (7) : REM BEEP SPEAKERListing 7-3. Low-Res Scan #2 (modifications only].


56 APPLE II COMPUTER GRAPHICSThis mod ification will create a data fi le on the disk which contains thesame list of addresses and v<strong>al</strong>ues that you printed out with Low-Res Scan#1 . Line 40 requires that you give the data fi le a name, and line 140 printsan "*" to mark the end of the fi le. To test the program, load it intomemory and type:GRBLOAD APPLERUNSo that your filename is the same as the one used in the next example,enter APPLE.OTA when the <strong>computer</strong> requests a fi lename. After that, thedisk will spin intermittently as the screen memory is being scanned andsaved; the speaker will beep to sign<strong>al</strong> the end of the run.The data may be read from the fi le APPLE. OT A and POK Ed into theproper locations by using a subroutine such as:1000 GR1010 D$ = CHR$ (13) + CHR$ (4)1020 FL$ = "APPLE .DTA"1030 PRINT D$ "OPEN" FL$1040 PRINT D$ "READ" FL$1050 INPUT L$1060 IF L$ = "* " GOTO 1110 : REM THE* .MARKS THE END OF THE FILE1070 L = VAL (L$)1080 INPUT V1090 POKE L ,V1100 GOTO 10501110 REM1120 PRINT D$ "CLOSE" FL$1130 RETURNListing 7-4.To test this subroutine, enter it and these extra lines.10 REM YOUR PROGRAM20 GOSUB 10003 0 ENDWhen you run the program, the <strong>graphics</strong> screen will be s<strong>et</strong> and cleared,then the disk will spin, and the <strong>apple</strong> will appear. If your data fi le is notc<strong>al</strong>led APPLE.OTA, change line 1030 to assign FL$ the proper name.This m<strong>et</strong>hod is nice because you do not have to key in the largeamounts of data, but unfortunately disk access is very slow and delays theexecution of the program which uses the <strong>graphics</strong>. Moreover, this procedurenot only requires that the user have a disk drive, but <strong>al</strong>so opens aPandora's box of problems such as having the disk in a different drive or


CHAPTER 7-PRESERVING YOUR PICTURES 57slot. To remedy those problems we present a slightly more complex technique.Using EXECWhat you will do here is create a text fi le of DATA statements on diskwhich may be EXECed into any program that you like. Each record in th<strong>et</strong>ext fi le will contain a line number followed by the DATA statement which,in turn, is followed by ten pairs of entries which are the locations andv<strong>al</strong>ues of the bytes used in screen memory. The last DATA statement maycontain less than the 2© entries the others have. The line numbers for theDATA statements will begin at 1©©©© and increment by two; the s<strong>et</strong> ofv<strong>al</strong>ues in the DATA statements will be the same as those printed with Low­Res Scan #1 .Load the program from Listing 7-1 once again, then enter the fo llowingstatements to mod ify it.10 REM CREATE EXEC FILE35 D$ = CHR$ (13) + CHR$ (4)40 INPUT "FILENAME :";FL$45 PRINT D$ "OPEN" FL$50 PRINT D$ "DELETE" FL$55 PRINT D$ "OPEN" FL$60 PRINT D$ "WRITE" FL$70 LN% = 10000 :Cl = 1104 IF PEEK (L) = 0 GOTO 120106 COUNT COUNT + 1108 IF Cl = 1 THEN PRINT LN%; "DATA" ;110 IF Cl > 1 THEN PRINT II ' II ;112 PRINT L ;", "; PEEK (L);:Cl = Cl + 1114 IF Cl > 10 THEN Cl = l :LN% = LN% + 2:PRINT140 PRINT D$ "CLOSE" FL$15YJ PRINT CHR$ (7) ; "COUNT =" ;COUNT ; "EOJ"Listing 7-5. Low-Res Scan #3 [modifications only].Line 4© requires that you enter a name for the fi le being created. Line 7©s<strong>et</strong>s the line numbers to begin at 1©©©© (LN% = 10000), and line 114increments each line number by 2 (LN% = LN% + 2).With this routine in memory, create the text fi le for the <strong>apple</strong> figure bytyping:GRBLOAD APPLERUNWhen asked for a fi lename, enter APPLE .EXC. The disk will behave very


58 APPLE II COMPUTER GRAPHICSmuch like it did when you created the data fi le using the previous m<strong>et</strong>hod .When the program is finished, the speaker will beep and the <strong>computer</strong> willpri nt the tot<strong>al</strong> number of bytes that it saved -write this number downsomeplace. APPLE.EXC is a text fi le which contains a number of DATAstatements, compl<strong>et</strong>e with line numbers. Those DATA statements may beeasily appended to your programs. To demonstrate, type:NEWlYJ GR2Y, COUNT = xxx3Y' FOR I = 1 TO COUNT4Y, READ L,V: POKE L ,V5YJ NEXT IlYJYJ ENDEXEC APPLE .EXCThe v<strong>al</strong>ue assigned to COUNT in line 2(/) should be the number you wrotedown after running Listing 7-5, for that is the number of data bytes in the<strong>apple</strong> figure.When the disk stops spinning, type:LISTand you will see the program lines you entered above plus sever<strong>al</strong> DATAstatements beginning at line 1(/Jf/Jf/Jf/J.When you are ready, type:RUNand the <strong>computer</strong> will draw the <strong>apple</strong> on the Low-Res screen.Since the DATA lines are now part of the code, the program does notneed to access the disk for the locations and v<strong>al</strong>ues. This eliminates thedifficulties inherent in using disk data fi les.We have <strong>al</strong>ready stored the data for a figure in two places-the disk andthe program code. There is y<strong>et</strong> another place to store that info rmation, andthat is within the <strong>computer</strong> itself.Moving MemoryWhat we will do is store the entire <strong>apple</strong> screen in memory somewhereother than the Low-Res screen memory area. Then when you wish todisplay the figure, you need only copy the information from the storagearea to Low-Res memory. We will perform the copying fu nction using aBASIC routine, and then <strong>al</strong>so do it using a machine language subroutine.The fi rst task is to load the <strong>apple</strong> figure into a safe, unused memoryrange. We will use the top $4(/)(/) bytes of user memory, from $92(/)(/) to$95FF (37376-38399). The addresses given here are fo r a 48K Apple II +,


CHAPTER 7-PRESERVING YOU R PICTURES 59and will need to be adjusted if you have less memory in your m_ac hine.You may d<strong>et</strong>ermine the highest usable memory address by referring to theApple DOS 3.3 manu<strong>al</strong>, page 142, or by booting your system and typing:PRINT PEEK(115) +256*PEEK(116)If your <strong>computer</strong> is an Apple II with INTEGER BASIC in ROM, you wouldtype:PRINT PEEK(76) +256*PEEK(77)The number you g<strong>et</strong> as a result is the address of the "top" of availablememory. To fit your Low-Res screen data in below that location, subtract1024 ($400) to g<strong>et</strong> the starting address of your storage area. Multiplescreens may be stored one below the other by <strong>al</strong>lowing 1024 bytes fo reach screen.Loading the <strong>apple</strong> figure at $9200 is easily accomplished by:orBLOAD APPLE , A$9200BLOAD APPLE, A37376If you have the <strong>apple</strong> figure stored on tape, enter Monitor and type:9200 .95FFRAfter the <strong>apple</strong> has been loaded in the proper location, you need to res<strong>et</strong>HIMEM so that your BASIC program does not overwrite your data andreduce your picture to <strong>apple</strong>sauce! From BASIC type:HIMEM :37375To g<strong>et</strong> a preview of the speed of moving memory, enter Monitor and type:400 < 9200 . 95FFMand, thanks to the Monitor MOVE command, you will see the <strong>apple</strong> ... in aflash!This entire procedure was accomplished in your <strong>computer</strong>'s immediateexecution mode, but now we need a program to do the same thi ngs.Listing 7-6 is designed to do just that from APPLESOFT.5 REM MOVE THE APPLE6 REM USING POKE7 REM10 HIMEM : 3737520 D$ = CHR$ (13) + CHR$ (4)30 PRINT D$; "BLOAD APPLE, A37376"40 SOURCE = 3737650 GR60 FOR I = 0 TO 1023


60 APPLE II COMPUTER GRAPHICS70 POKE 1024 + I, PEEK (SOURCE + I)80 NEXT I90 ENDListing 7-6. BASIC memory move.Line 10 s<strong>et</strong>s HIMEM down below the area needed to store our <strong>apple</strong>data in order to create a safe area of memory. Line 30 loads the data intothat protected area, and line 4© s<strong>et</strong>s the variable SOURCE equ<strong>al</strong> to theaddress of the fi rst byte of data. After the <strong>graphics</strong> mode is s<strong>et</strong>, lines 6©through 80 read the data from the source area and POKE it into the screenmemory area. When I equ<strong>al</strong>s


CHAPTER 7- PRESERVING YOUR PICTURES 61The POKEs in line 45 are very important because they provide the subroutinewith the starting address of the data area. If the data area begins ata different location, then you will have to adjust these POKEs. The way toc<strong>al</strong>culate the v<strong>al</strong>ues is to write down the starting address in hex ($9200),separate the two bytes so that it looks like 92 00, and convert each byte todecim<strong>al</strong>: $92 = 9* 16 + 2 = 146, $00 = 0. Those are the two v<strong>al</strong>ues tobe POKEd into 252 and 253, but they must be POKEd in the correct orderor strange things will happen. Apple wants you to give it the right-hand(low order) byte fi rst and then the left-hand (high order) byte-a conspiracyknown as Lo-byte/Hi-byte-so the 0 is placed in 252 and the 146 in253.Example 1Suppose that the subroutine in Listing 7-7 has <strong>al</strong>ready been POKEdinto memory, and that you want to display a picture which is stored at$8E©C.$8E = 8*16 + 14 142$C = *16 + 12 12so you wouldPOKE 252,12: POKE 253, 142CALL 768A fu rther note of caution: The starting address must be POKEd just prior toCALLing the subroutine. APPLESOFT uses location 253 fo r scratch padmemory, so after the CALL is executed BASIC clobbers whatever v<strong>al</strong>ue youhad in there.Sound Assembly1 BuglerIf you speak assembly language, Listing 7-8 may be of interest. It is theassembly listing of the subroutine used in Listing 7-7.SOURCEL EPZ $FCSOURCEH EPZ $FDDES TL EPZ $FEDES TH EPZ $FF; INITIALIZATION3- A9 32- 85 FE34- A835- A9 4LDASTATAYLDA#DES TL


62 APPLE II COMPUTER GRAPHICS37- 85 FF STA DES TH39- AATAX;BEGIN LDA/STA LOOP3A- Bl FC LOOPTOP LDA (SOURCEL) ,Y3C- 91 FE STA (DESTL) ,Y3E- C8INY3F- D F9 BNE LOOPTOP311- CA DEX312- F 7 BEQ STOP314- E6 FD INC SOURCEH316- E6 FF INC DES TH318- 4C AJMP LOOPTOP31B- 6 STOP RTSEND3Listing 7-8. Assembly language subroutine.In essence, this routine takes $4 bytes of memory from the source rangeand copies it into the Low-Res page 1 area.Wrapping UpBy now yow have probably learned more than you ever wanted to knowabout Low-Res <strong>graphics</strong>, but do not despair, as many of the same conceptswill apply in Hi-Res <strong>graphics</strong> as wel l. We have shown you how to controlthe individu<strong>al</strong> units of the Text and Low-Res screens, and how to create,save, and rec<strong>al</strong>l pictures on the Graphics screen. Each of the varioustechniques discussed has its peculiar advantages and disadvantages-someare faster than others, some use more memory, some require disks, and soforth. It is up to you, the programmer, to choose the m<strong>et</strong>hod best suited toyour application.VocabularyBLOADBSAVEEditorEXECHIMEMLo-byte/Hi-bytePLOT


CHAPTER 7-PRESERVING YOUR PICTURES 63Exercises1. Design and plot a picture of a spaceship on the Low-Res screen.2. Save the figure plotted in Exercise 1 using:a. The m<strong>et</strong>hod of Low-Res scan #2 (Listing 7-2).b. The m<strong>et</strong>hod of Low-Res scan #3 (Listing 7-3).3. Draw the figure saved in Exercise 2 by:a. Using BLOAD (or Monitor's READ command).b. Writing a BASIC program to input and plot the data generated inExercise 2a.c. EXECing the DATA statements generated in Exercise 2b into aBASIC program.d. Loading the figure into a safe area of memory and using thememory move given in Listing 7-6.


8Hi-Res GraphicsObjectivesAfter reading Chapter 8 you should be able to:• C<strong>al</strong>culate the address for any byte on the Hi-Res screen.• C<strong>al</strong>culate the v<strong>al</strong>ue needed to produce a give n dot pattern in a byte,and use both BASIC and Monitor to place that v<strong>al</strong>ue in Hi-Res screenmemory.• Design, digitize, and display simple figures in Hi-Res.A large percentage of the commerci<strong>al</strong> software written fo r persona I <strong>computer</strong>stoday uses Hi-Res <strong>graphics</strong>. Games, graphing packages, draftingdesign programs, sever<strong>al</strong> word processors, and even some of the <strong>al</strong>ternatelanguages (for example, PILOT and LOGO) use the Hi-Res <strong>graphics</strong> screenfor video output. Reflecting the pre-eminence of Hi-Res, the remainder ofthis book will be <strong>al</strong>most entirely devoted to various m<strong>et</strong>hods of controllingthis mode of output.Hi-Res gains an advantage over Low-Res because it l<strong>et</strong>s you have individu<strong>al</strong>control of each dot on the screen; there are 280 such dots horizont<strong>al</strong>lyacross the screen and up to 192 dots vertic<strong>al</strong>ly. The appearance of animage on the video screen is greatly affected by the degree of resolution(the number of dots per inch) in which it is drawn. In Chapter 6 you sawhow the figure of the <strong>apple</strong> suffered when it was blocked out for Low-Reswhich has only four "dots" per inch. The Hi-Res dots are seven timessm<strong>al</strong>ler than the Low-Res blocks, so the Hi-Res mode produces figures thatlook much b<strong>et</strong>ter; but as with any improvement, there is a price to pay.65


66 APPLE II COMPUTER GRAPHICSHi-Res adds another level to the addressing difficulty experienced withthe non-contiguous Low-Res mode, and requires four times the memoryfor display. Add to that a reduced number of colors readily available to theprogrammer, plus the limitation of not being able to put <strong>al</strong>l of the colors atany one location on the screen, and you may begin to wish you had nevereven heard of Hi-Res.Fortunately, there are sever<strong>al</strong> Hi-Res <strong>graphics</strong> editors on the mark<strong>et</strong>which take most of the work and frustration out of creating Hi-Res figures.If you do not have one of these editors, we suggest that you acquire onebefore attempting any serious endeavor. A good Hi-Res editor will takecare of the multitude of grubby d<strong>et</strong>ails involved with Hi-Res <strong>graphics</strong>. Itwill l<strong>et</strong> you draw lines, fi ll regions with color, and turn individu<strong>al</strong> screendots on and off. It will <strong>al</strong>so l<strong>et</strong> you save Hi-Res figures in shape tables (seeChapter 1 ©), and even save the entire screen. Y<strong>et</strong>, even with an editor youmust still understand the ins and outs of Hi-Res <strong>graphics</strong>.In the following chapters we intend to show you a number of ways totame the Hi-Res beast. In this chapter we will concentrate on mapping theHi-Res maze, gaining control of individu<strong>al</strong> dots, and creating simple figuresin black and white. Color, animation, and other nifty topics will be dis-. cussed in later chapters.Hi-Res MemoryThere are two storage locations for Hi-Res <strong>graphics</strong>. The primary page,page 1, is located from 8192 th rough 16383 ($2©©©-$3 FFF), and the secondarypage, page 2, runs from 16384 through 24575 ($4©©©-$5FFF). Mostof our work will be done with page 1, but the techniq ues and programsdiscussed will work identic<strong>al</strong>ly on page 2 by simply adding 8192 ($2©©©) tothe page 1 addresses.Perhaps we should begin with a program designed to turn on every Hi­Res dot, one at a time and sequenti<strong>al</strong>ly. We will start with location 8192and turn on the first bit in that byte, then turn it off and turn on the secondbit in the same byte. Then the second bit will go off and the third will goon, continuing to the eighth bit, which is left on when we begin with thenext byte (location 8193) and do the same thing with it, and so forththroughout the eritire screen. Even though we want to turn only one bit onor off at a time, we must POKE the entire byte, since bits do not have anaddress and hence cannot be referenced. For each byte we must POKEeight v<strong>al</strong>ues, ,one after another, and each v<strong>al</strong>ue will have only one bit on.The following lines of BASIC code will generate the eight v<strong>al</strong>ues required.FOR J = 1 TO 8x = 2"(8 - J)NEXT J


CHAPTER 8-Hl-RES GRAPHICS 67When J equ<strong>al</strong>s 1, X equ<strong>al</strong>s l7 equ<strong>al</strong>s 128, J equ<strong>al</strong>s 2 gives X equ<strong>al</strong>s26 equ<strong>al</strong>s 64, and so on as indicated in Figure 8-1 .J 2A (8-J) x BINARY HEX-------- -----------1 27 128 1000 0000 $802 26 64 0100 0000 $403 25 32 0010 0000 $204 24 16 0001 0000 $105 23 8 0000 1000 $086 22 4 0000 0100 $047 21 2 0000 0010 $028 20 1 0000 0001 $01Figure 8-1. Powers of two.As promised, each v<strong>al</strong>ue has only one bit turned on, and notice <strong>al</strong>so that,as the index "J" goes from one to eight, the "on" bit in the byte goes fromleft to right. The fo llowing program uses this <strong>al</strong>gorithm to successivelyPOKE each v<strong>al</strong>ue into a byte of Hi-Res memory.10 REM TURN ON20 REM HI-RES DOTS30 REM40 HGR : REM TURN HI-RES ON50 X = PEEK ( 49234) : REM SWITCH TOFULL PAGE60 REM70 FOR I = 8192 TO 1638380 REM BEGINNING AND END OF HI-RES90 FOR J = 1 TO 8100 X = 2 A ( 8-J) : REM TURN ON BIT110 POKE I,X : REM TURN ON DOT120 FOR L = 1 TO 2: REM DELAY130 NEXT L140 NEXT J150 NEXT I160 CALL 65338: REM BEEP170 ENDListing 8-1. Turn on Hi-Res dots.From our discussion so far, you should expect a single dot to appear in th<strong>et</strong>op left corner, and rapidly be replaced by one a little further to the right.That dot should <strong>al</strong>so be quickly replaced by a third y<strong>et</strong> a little further right,and so on until the eighth dot, which remains on. The ninth dot shouldthen begin just to the right of the still lit eighth dot, only to be replaced bythe tenth still slightly more to the right, and so on; it should give the


68 APPLE II COMPUTER GRAPHICSimpression of a dot travelling steadily to the right while leaving everyeighth dot on.Run the program and if the screen behaves in an unexpectedborderingon bizarre-manner, then the program is probably working perfectly.Seeing Is BelievingWhat you should re<strong>al</strong>ly see is a dot that appears close to the upper lefthand corner of the screen, and then travels rapidly to the LEFT. Just as thefirst dot bumps into the edge of the screen, a second dot appears; it <strong>al</strong>somoves to the left and stops where the fi rst dot began. A third dot appearsand moves left, and then a fourth and fifth and so on until the entire screenfills with a grid of little dots, <strong>al</strong>l about a quarter-inch apart. (It requiresapproximately five minutes to reach this point.) The routine continues withthe next s<strong>et</strong> of dots appearing immediately beneath the first s<strong>et</strong>. If you l<strong>et</strong>the process continue long enough, the screen will fi n<strong>al</strong>ly display fortyvertic<strong>al</strong> lines of dots.The display is mildly amusing, but after the novelty wears off you mightre<strong>al</strong>ize that the "on" dots moved from right to left while the "on" bits inthe byte moved from left to right. Apple Hi-Res displays the bits of a bytein the reverse order, so the right hand bit turns on the left hand dot of abyte, and the left hand bit turns on the right hand dot-er, sort of. You see,there is a little more to it.Slow down the display process by changing the delay v<strong>al</strong>ue in line 12


C..: HAPTER 8-Hl-RES GRAPHICS 69Going DottyEach bit pattern creates a corresponding dot pattern on the screen. Sinc<strong>et</strong>here are seven bits displayed from any byte, there are 128 possible combinations,and though we do not propose to show you every combi nation,Figure 8-2 lists which dots are turned on by which bits, and which onesmay be handy for reference. You can see again that no dot corresponds tothe left-hand bit.l(J(JiJ(JiJ(JiJOFF OFF OFF OFF OFF OFF OFF(JliJ(J(JiJ(JiJOFF OFF OFF OFF OFF OFF ONiJiJ l(J(JiJ(JiJ OFF OFF OFF OFF OFF ON OFFiJiJiJ l(J(J(J(J OFF OFF OFF OFF ON OFF OFFiJ(J(J(Jl(J(JiJ OFF OFF OFF ON OFF OFF OFFiJ(JiJ(J(JliJ(J OFF OFF ON OFF OFF OFF OFFiJ(JiJ(J(JiJ l(J OFF ON OF OFF OFF OFF OFFiJ(JiJ(JiJ(J(Jl ON OFF OFF OFF OFF OFF OFFFigure 8-2. Bit correspondence.To turn on a dot, you must <strong>al</strong>so know the address of the byte whichcontrols it. Refer to the Hi-Res memory map in Figure 8-3.The boxes shown in the map are very similar to the ones used in theText/Low-Res map in Chapter 6, and the address of each box is found inthe same manner-add the addresses given on the map for the row andcolumn of the box. Within each box there are eight rows of seven dotsstacked vertic<strong>al</strong>ly as shown in the ins<strong>et</strong>, and each row of dots in the boxcontributes an addition<strong>al</strong> number c<strong>al</strong>led the position address. To turn on adot, you must fi rst fi nd the address of the box and the row of dots withinthat box which contains the dot you are looking for. If you are not confusedby now, then you must have worked with this befo re! Perhaps wecan clear things up by use of an example.Example 1Suppose that you wish to display the seventeenth dot from the left in th<strong>et</strong>hird row of dots.SolutionThe third row of dots is within the fi rst row of boxes, and since each boxdisplays seven (not eight) dots across, the seventeenth dot is in the thirdbox across as shown in Figure 8-4. Now that you have located the dot,you need to add three numbers to g<strong>et</strong> its address: the add ress for thebox's row, column, and the byte's position within the box.


n each box·1 1 0 $0000I I 1024 $0400I 2048 $0800I 3072 S0C00I 4096 $1000I 5120 $1400I6144 $1800I I 7168 $1C00r-""('"'\0c:-l""C"l:::c?=itl'l....;00CT0"c.$2000 8192$2080 83200 $2 100 8448CT;;$2 180 8576c. $2200 8704c. $2280 8832f$2300 8960$2380 9088 $2028 8232 $20A8 8360$2 128 8488CT$21A8 8616$2228 87443•$22A8 8872CT$2328 90000$23A8 9128J" 0$2050 8272c.-r$2000 8400$2150 85285·$2 1D0 8656CT$2250 8784$2200 8912$2350 9040$2300 9168!::i::!5:;3:a;;EiE:S - N M • Vl ...O f'- OO O"I S - N M . Vl \O f'- OO O"I S - N M Vl '° f'- OO O"IIS - N M ""'" Vl '° r- 00 °" - - - - - - - - - - N N N N N N N N N N M M ... M M M M M ... ...Figure 8-3. Hi-Res memory map.(Used with permission of Apple Computer Corporation]


CHAPTER 8-Hl-RES GRAPHICS 71Row AddressColumn AddressPosition Address81922248Fin<strong>al</strong> Address1242$282Thus, you at last arrive at the address, but now you need to c<strong>al</strong>culate thev<strong>al</strong>ue to put into it. To do that, again refer to Figure 8-4.The top line of the figure shows the fi rst three bytes of screen memory,taking into account that only seven bits of each byte are actu<strong>al</strong>ly displayed.Dot number seventeen is the third dot from the left in its box; soto turn it on, you would turn on the third bit from the RIGHT in the byteas shown in the bit pattern on the second line. Remember, the bitpattern is the reverse of the dot pattern.Dot #1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21I I [X] I· . •BYTE # 2 3l0000 0 1 0 0l$4 = 4Figure 8-4. Display conversion.The left-hand bit has been shown in the second line as a zero (off) . Thebinary number in the second line is converted to hex and decim<strong>al</strong> formin the third line, and the decim<strong>al</strong> v<strong>al</strong>ue is the one we are about to POKE.After <strong>al</strong>l that work, you can turn on the desired dot using the fo llowingcommands from BASIC.HGRPOKE 1242,4You could use Monitor by typingHGRCALL -151282 : 4(s<strong>et</strong> <strong>graphics</strong>)(turn on the dot)Example 2Turn on dots 57, 58, and 59 in line number 116 of the Hi-Res screen.


72 APPLE II COMPUTER GRAPHICSSolutionIf you count eight lines per box, the fourteenth row of boxes ends withthe 112th line of dots (8* 14= 112), so the 116th line is the fourth line inthe fifteenth box. Therefore, by using the memory map in Figure 8-3,the row address is 9000, and the position in the box address is 3072.Dots 57, 58, and 59 of that line are the second, third, and fourth dots inthe eighth box, which has column address 7 ($7), so the fin<strong>al</strong> address is:9({l({l({lJ(J72712({l79$2328$(JC({l({l$ 7$2F2FThe display fo r that byte would look like:-XXX ---where - represents a dot which is off, and X a dot which is on.Reversing the order and supplying the "missing" left zero wouldgive you the bit pattern: 00001 110, which is equ<strong>al</strong> to 14 ($0E).Now that you have d<strong>et</strong>ermi ned the address (12079 or $2F2F) andthe v<strong>al</strong>ue (14 or $0E), type:HGRPOKE 12({l79, 14orHGRCALL -1512F2F : EThis is not so bad after you g<strong>et</strong> used to it, is it? Perhaps onemore exampleExample 3Turn on the dots 27 and 30 in line 16 of the Hi-Res screen.SolutionLine 16 is the last byte in the second row of boxes, so the row addressis 8320 ($2©8©) and the position address is 7168 ($1 C00). Dots 27 and 3©are in separate boxes-the fo urth and fifth columns-so they will havedifferent column addresses, and we will have to POKE each byte separately.


CHAPTER 8-Hl-RES GRAPHICS 73The fourth column address is 3 ($3), so the address of the fi rst byte weare after is 8320 + 7168 + 3 = 15491 ($2080 + $1C00 + $3 =$3C83), and the address of the second byte is 15492 ($3C83). The dotpatterns fo r the fourth and fifth byte look like:-----X - - X -----so the bit patterns would be:010 0000 and 000 0010and when you supply the left hand zero you g<strong>et</strong>:0010 0000$20$ 2= 32 2and 0000 0010Hence, to plot the dots, type:HGRPOKE 15491,32POKE 15492,2If you were to supply ones for the left-hand bits you would g<strong>et</strong>:so:1010 0000 and$A0160HGRPOKE 15491,160POKE 15492,1301000 0010$82130Notice that the same dots were turned on with both s<strong>et</strong>s of statements,but that they were diffe rent colors-not very impressive unlessyou have . a color display, but it does illustrate how the eighthbit controls . color in Hi-Res.Monitor may be used to turn on the same dots, and <strong>al</strong>so to clearthe screen without the use of the HGR statement.The Hi-Res screen may be cleared by moving zeros into everybyte, so type:CALL -1512000 : 02001 < 2000 . 3FFEM3C83 :60 03The fi rst command invokes Monitor, the next two clear thescreen (see Chapter 3), and the last places the proper byte in eachof the two addresses.


74 APPLE II COMPUTER GRAPHICSFigure 8-5. Alien critter.[From The Artist by Warren Schwader, used with permission.]Whew! That seems like a lot of work just for a few dots!! As it happens,working up an entire figure is not much harder. However, your preliminaryplanning is extremely important when designing a figure in Hi-Resl<strong>et</strong>'sput tog<strong>et</strong>her an <strong>al</strong>ien critter as an example. Figure 8-5 shows a sample<strong>al</strong>ien <strong>al</strong>ready drawn on graph paper. For simplicity, it is only seven dotswide and eight rows high so that it will fit within a single box of thememory map.The fi rst step is to c<strong>al</strong>culate the v<strong>al</strong>ue of each byte composing the <strong>al</strong>ien.Figure 8-6 shows the dot patterns, the required bit patterns, and the correspondinghex and decim<strong>al</strong> v<strong>al</strong>ues.DOT BIT HEX DECPATTERN PATTERN VALUE VALUE-------------------------------------·---------XXX-- !il!il!ill ll!il!il $1C 28-XXXXX- !il!illl lll!il $3D 61X--X--X !ill!il!il l!il!ill $49 73X--X--X !ill!il!il l!il!ill $49 73-XXXXX- !il!illl lll!il $3D 61--XXX-- !il!il!ill ll!il!il $1C 28-X---X- !il!ill!il !il!ill!il $22 34X-----X 0100 0001 $41 65Figure 8-6. Alien translation.The extra zero has been placed to the left of each bit pattern to supplythe "missing" eighth bit. C<strong>al</strong>culating the v<strong>al</strong>ues was not difficult at <strong>al</strong>l.Now you need to c<strong>al</strong>culate the addresses.


CHAPTER 8-Hl-RES GRAPHICS 75The row and column addresses depend on which box you will use toplot the <strong>al</strong>ien, but the address for the position in the box is fixed by therow's position in the figure. The position address for row one is © ($©), forrow two it is 1©24 ($4©©), fo r row three it is 2©48 ($8©©), and so forth, asshown in the Hi-Res memory map (see Figure 8-3). The following programtakes advantage of those facts as it plots the <strong>al</strong>ien in any box you chooseon the mixed mode Hi-Res screen.1 REM ALIEN PLOT2 REM3 REM INITIALIZATION4 DIM RA% (2)5 FOR I = 1 TO 2 : READ RA%(I) :NEXT I6 REM7 REM INPUT ROW AND COLUMN8 HGR : HOME : VTAB (24)9 INPUT "ROW NUMBER? (1-2) ";R$1 R% = VAL (R$)11 IF .t:t/0 < 1 OR R% > 2 THEN PRINTCHR$ (7) : GOTO 912 INPUT "COLUMN NUMBER? (1-4)";C$13 C% = VAL (C$)14 IF C% < 1 OR C% > 4 THEN PRINTCHR$ (7) : GOTO 1215 BA% = RA% (R%) + C%-116 POKE BA%,2817 POKE BA% + 124, 6218 POKE BA% + 248, 7319 POKE BA% + 372, 732 POKE BA% + 496, 6221 POKE BA% + 512, 2822 POKE BA% + 6144, 3423 POKE BA% + 7168,6524 REM25 PRINT "PLOT ANOTHER? (Y/N) ": GET A$26 IF A$ < > "N" GOTO 927 TEXT : HOME28 END29 DATA 8192,832, 8448,8576, 874,8832,896,988,8232, 8363 DATA 8488,8616,8744,8872,9'9128, 8272,84,8528, 8656Listing 8-2. Alien plot.Lines 4© through 6© dimension and initi<strong>al</strong>ize an array containing th<strong>et</strong>wenty row addresses. Line 8© s<strong>et</strong>s the mixed Hi-Res <strong>graphics</strong> mode with


76 APPLE II COMPUTER GRAPHICSthe cursor at the bottom of the text window. Lines 9© through 15© acceptyour input for the row and column numbers and, after checking to insur<strong>et</strong>hey are v<strong>al</strong>id, converts them to a base address for the box ind icated. Line15© c<strong>al</strong>culates the base address by adding the row address for the box[RA%(R%)] to the column address (which happens to be one less than thecolumn number). Having added the row and column addresses for thebox, it only remains to add the address for the position in the box for eachbyte of the <strong>al</strong>ien (how do <strong>al</strong>iens taste?). The fi n<strong>al</strong> address is c<strong>al</strong>culated andthe proper v<strong>al</strong>ue POKEd into it in lines 16© through 23©, lines 25© and 26©give you a chance to plot another one, and then lines 27© and 28© wrapup the program. The data in lines 29© and 3©© are the row addresses readin line 5©. The plotting speed may be enhanced somewhat by c<strong>al</strong>culating<strong>al</strong>l of the fi n<strong>al</strong> addresses before starting to POKE any of them.Example 4Write a simple BASIC program to plot the cannon given in Figure 8-7.Figure 8-7. Hi-Res cannon.(From Cannonb<strong>al</strong>l Blitz by Olaf Lubek, with permission.]


CHAPTER 8-Hl-RES GRAPHICS 77SolutionThe fi rst task is to digitize the cannon. Notice that it is fourteen dots wideand ten bytes high, so it will require four boxes to display. The figure has<strong>al</strong>ready been divided into four quadrants. Figures 8-8A-D show the digitizingof the corresponding quadrant.DOT BIT HEX DECPATTERN PATTERN VALUE VALUE---------------------------------------------X-X-X-X 101 0101 $55 85X-X-X-X 101 0101 $55 85X-X-X-X 101 0101 $55 85X-X-X-X 101 0101 $55 85------- 000 0000 $00 0-----X- 010 0000 $20 32----XX- 011 0000 $30 48---XXXX 111 1000 $78 120Figure 8-8A.DOT BIT HEX DECPATTERN PATTERN VALUE VALUE-----------------------------------------------X-X-X- 010 1010 $2A 42-X-X-XX 110 1010 $6A 106-X-X-XX 110 1010 $6A 106-X-X-X- 010 1010 $2A 42--X-X-- 001 0100 $14 . 20X-X-X-- 001 0101 $15 21X-X-XX- 011 0101 $35 53---XXXX 111 1000 $78 120Figure 8-88.DOT BIT HEX DECPATTERN PATTERN VALUE VALUE-------------------------------------------------XXXX----XX-111 1000 $78011 0000 $30Figure 8-8C.12048DOTPATTERNBITPATTERNHEXVALUEDECVALUE--XXXX----XX-$78$3012048Figure 8-80.


78 APPLE II COMPUTER GRAPHICSThe following program will POKE into memory the v<strong>al</strong>ues we justgenerated. We will plot the cannon in the upper left corner of thescreen.10 REM POKE CANNON20 REM THE HARD WAY !30 REM40 HGR50 REM QUADRANT A60 POKE 8192,85 : POKE 9216,85:POKE 10240 ,85: POKE 11264,8570 POKE 12288, 0 : POKE 13312,32 :POKE 14336,48: POKE 15360 ,12080 REM QUADRANT B90 POKE 8193,42: POKE 9217,106:POKE 10241,106: POKE 11265,42100 POKE 12289,20 : POKE 13313,21:POKE 14337,53: POKE 15361,120110 REM QUADRANT C120 POKE 8320 ,120 : POKE 9344,48130 REM QUADRANT D140 POKE 8321,120 : POKE 9345,48150 ENDListing 8-3. Cannon poker.Listing 8-3 makes no pr<strong>et</strong>ense at cleverness, but it does perform aspromised .By now you have seen how the individu<strong>al</strong> bits on the Hi-Res screen arecontrotled, and how you can wade through the memory map to fi nd thebyte of your dreams. Using those ideas, you have developed the data forand plotted some simple figures on the screen. Not bad fo r a day's work,eh?VocabularyColumn AddressDigitizingPosition AddressRow AddressExercises1. Place the given dot pattern where indicated on the Hi-Res screen.a. -XX -X -X in row 10, column 20 of boxes, and fi rst byte of the box.


CHAPTER 8-Hl-RES GRAPHICS 79b. XX-XX-- in the fifth line and eleventh column of dots.c. X-----X in the fourteenth line and 27th column of dots_2. Write a BASIC program which l<strong>et</strong>s the user plot the cannon fromExample 3 in any group of four boxes on the Hi-Res screen.3. Digitize and display the butterfly figure given below.%cP--_...__Figure 8-9. A butterfly.


81gHi-Res ColorObjectivesAfter reading Chapter 9 you should be able to:• Design and display a Hi-Res color figure.• When given the location of a dot on the screen and a desired color,d<strong>et</strong>ermine if that color can be displayed at that location, and if so,c<strong>al</strong>culate the byte of memory which contains that dot and POKE the·appropriate v<strong>al</strong>ue into that location.• Use dithering to generate colors other than the six basic Hi-Resshades.Good color is virtu<strong>al</strong>ly a requirement for qu<strong>al</strong>ity Hi-Res products; mostsoftware publishers will not touch a Hi-Res program unless its color effectsare clear, clean, and smooth. Color enhances the display by distinguishingthe diffe rent elements of a picture or graph, by adding life to a screen, andby making the output more attractive to the eye of the viewer. YourApple's hardware is designed to produce six colors, including black andwhite and, by a software process known as dithering, can be made todisplay upwards of twenty addition<strong>al</strong> hues.G<strong>et</strong>ti ng color on the Hi-Res screen is simple-<strong>al</strong>most too simple sinceany time you plot a single dot, it is <strong>al</strong>ways in color. The trick is to developthe right color in the right place. By now you should be familiar with theway Hi-Res memory is laid out, and how to use the memory map to fi ndyour way around. You <strong>al</strong>so need some facility in translating desired dotpatterns into their corresponding bit patterns, and then into their hex ordecim<strong>al</strong> v<strong>al</strong>ues. Another necessary item is either a color display, or a veryvivid imagination.


82 APPLE II COMPUTER GRAPHICSA Bit of ColorIn Chapter 8, we pointed out that the left-most bit in a Hi-Res byte was notdisplayed, and <strong>al</strong>luded to its fu nction in color control. That bit is c<strong>al</strong>led thecolor bit of the byte, and its s<strong>et</strong>ting d<strong>et</strong>ermi nes which group of colors theremaining seven bits will display. If the color bit of a byte is off (zero), theremaining seven bits will display colors from group 1: BLACK1, GREEN,VIOLET, or WHITE1 . If the color bit is on (one), the byte will display group2 colors: BLACK2, ORANGE, BLUE, or WHITE2. Those colors will varydepending on the TV or monitor used, but you can usu<strong>al</strong>ly adjust the tinton your screen so the proper colors are displayed. We will r<strong>et</strong>urn to thecolor bits and color groups; meanwhile, run the following program as anexperiment.10 HGR20 HCOLOR = 330 HPLOT 1,0 TO 1,10040 GET P$: REM PAUSE50 HCOLOR = 460 FOR I = 0 TO 10070 HPLOT 0 , I80 NEXT IListing 9-1 . Vertic<strong>al</strong> line.According to the Apple manu<strong>al</strong>s, HCOLOR = 3 is WHITE1 (the whitebelonging to color group 1); hence, lines 10 through 30 should plot avertic<strong>al</strong> white line in column 1. But when you run the program you areinstead gre<strong>et</strong>ed by a vertic<strong>al</strong> green line! That is because you are plotting asingle dot in each row, and any isolated dot <strong>al</strong>ways results in a color. Butthere is more. Line 40 h<strong>al</strong>ts the program until you press a key, line 50 s<strong>et</strong>sthe color to BLACK2, which is the black belongi ng to color group 2. Lines60, 70, and 80 plot a vertic<strong>al</strong> line of black dots in column 0. Since column0 was <strong>al</strong>ready black, you would not expect anything to change, and y<strong>et</strong>the green line slowly changes to orange after you press a key.The reason fo r the color change has to do with the color bit. The dots incolumn 0 and column 1 are controlled by the same byte of Hi-Res memory,and plotting WHITE1 turned on the column 1 bit of each byte and leftthe rest of the bits off, including the color bit. Plotti ng column 0 in BLACK2turned off the column 0 bits, which were off anyway, but <strong>al</strong>so turned onthe color bit of each byte since BLACK2 is a group 2 color. When the colorbit of each byte was turned on, it caused the entire byte to display group 2colors; thus, the green in column 1, which is a group 1 color, changed tothe corresponding group 2 color, orange. If column 0 had been plotted inB LACK1 instead of BLACK2, the color bit of each byte would have remainedoff and no color change would have occu rred.


CHAPTER 9-Hl-RES COLOR 83L<strong>et</strong>'s run a program which illustrates y<strong>et</strong> another characteristic of Hi-Res·color.10 HGR20 HCOLOR = 230 FOR I = 1 TO 10040 HPLOT I,050 NEXT I60 REM70 REM NOW TURN HALF OFF80 HCOLOR = 090 FOR I = 2 TO 100 STEP 2100 HPLOT I , 0110 NEXT I120 ENDListing 9-2. Horizont<strong>al</strong> line.An examination of the listing would lead one to believe that the programwill plot 100 consecutive horizont<strong>al</strong> blue dots, and then turn <strong>al</strong>l the evendots off. However, when the program is run, the blue line is drawn, andthen erased compl<strong>et</strong>ely! How can that be when you plot 100 dots and onlyerase 50? If you care to insert a delay loop and sign<strong>al</strong> in the program35 FOR J = 1 TO 500 : NEXT J: PRINT CHR$ (7) and count the dots as they areplotted, you will fi nd that only 50 dots are plotted in the fi rst place. Blue isonly available in even numbered columns (0,2,4, ... ), and so when theprogram attempts to plot blue on the odd numbered dots, it has no effectat <strong>al</strong>l. Viol<strong>et</strong> is <strong>al</strong>so available on only even dots, while odd dots maydisplay either green or orange.To summarize what you have seen so far, if a dot is plotted in an evennumbered column, it will be either viol<strong>et</strong> or blue, and if a dot is in an oddcolumn, it will be green or orange. The color bit selects which of theavailable colors each dot will display: if the color bit is off (group 1 ), evendots display viol<strong>et</strong> and odd dots display green; if the color bit is on (group2) then even dots are blue and odd dots are orange. The left-most columnof dots on the screen is counted as column zero, and columns 1 through279 follow sequenti<strong>al</strong>ly across the screen.Black is plotted by turning a dot off, and white is plotted by turning ontwo adjacent dots in the same row. L<strong>et</strong>'s experiment using the POKEstatement. Type:HGRPOKE 8192,1POKE 8192,2POKE 8192,3In the upper left corner of the Hi-Res screen you will display a single viol<strong>et</strong>dot, then a lone green dot, and fin<strong>al</strong>ly a white dot. How did the white dot


84 -APPLE II COMPUTER GRAPHICSg<strong>et</strong> there? The chart below takes the v<strong>al</strong>ues that you POKEd and shows thecorresponding bit patterns and effects.VALUEBIT PATTERN DOT(S) ON1 0000 0001 Row 02 0000 0010 Row 13 0000 0011 Row 0 and Row 1Figure 9-1. Bit patterns and effects.When the dots were turned on singly, they displayed their respectivecolors, but when they were both on they displayed white. Any "on" dotnext to another "on" dot will display white. Plotti ng one dot under orover another has no effect on the color.Haw Do Yau Like Them Apples?To practice with Hi-Res colors, we will plot the <strong>apple</strong> given in Figure9-2. The figure has <strong>al</strong>ready been sectioned into the nine boxes requiredto display it, and Figures 9-2A through 9-'-21 show the conversion fromdot patterns to decim<strong>al</strong> v<strong>al</strong>ues for each of the sections.Ac•• •••••••F•G H IFigure 9-2. Hi-Res <strong>apple</strong>.Except for the stem, which will be white, the grid for the <strong>apple</strong> is composedof dots in every second column. That pattern is necessary since


CHAPTER 9-Hl-RES COLOR 85each Hi-Res color is available on only odd or only even dots, and further,if two adjacent dots are turned on, they will both display white.Colors from both groups are used as indicated in Figure 9-2.DOTBITPATTERN PATTERN HEX DEC-------------------------------------------------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ----X-X (Jl(Jl iJ(J(JiJ $5(J 8(J--X-X-X (Jl(Jl (Jl(J(J $54 84--X-X-X (Jl(Jl (Jl(J(J $54 84-X-X-X- (J(Jl(J l(Jl(J $2A 42Figure 9-2A.DOTBITPATTERN PATTERN HEX DEC-------------------------------------------XX----- iJ(JiJ(J (J(J 11 $iJ3 3-XX---- iJ(JiJ(J (Jll(J $(J6 6--XX--- iJ iJ iJ iJ 11 iJ iJ $(JC 12--XX--- iJ iJ iJ iJ 11 iJ iJ $(JC 12--XX--- iJ(J(JiJ ll(J(J $(JC 12-X-X-X- (J(Jl(J l(Jl(J $2A 42-X-X-X- (J(Jl(J l(Jl(J $2A 42X-X-X-X (Jl(Jl (Jl(Jl $5585Figure 9-28.DOTBITPATTERN PATTERN HEX DEC-------------------------------------------------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJ------- (JiJ(JiJ (J(J (J(J $(J(J iJ------- iJ iJ iJ iJ iJ iJ iJ iJ $(J(J iJX-X---- iJ(J(JiJ (Jl(Jl $(J5 5X-X-X-- (J(J(Jl (Jl(Jl $15 21X-X---- iJ(J(JiJ (Jl(Jl $(J5 5-X----- iJ(J(JiJ (J(Jl(J $!32 2Figure 9-2C.


86 APPLE II COMPUTER GRAPHICSDOTBITPATTERN PATTERN HEX DEC- -------------- -------- - - -- --------- --------X-X-X- 0010 1010 $2A 42-X-X-X- 0010 1010 $2A 42X-X-X-X 1101 0101 $D5 213X-X-X-X 1101 0101 $D5 2 13X-X-X-X 1101 0101 $D5 213-X-X-X- 1010 1010 $AA 170-X-X-X- 1010 1010 $AA 170-X-X-X- 1010 1010 $AA 170Figure 9-20.DOTBITPATTERN PATTERN HEX DEC-- - ---------- ---- - ----- --------- - - ---------X-X-X-X 0101 0101 $55 85X-X-X-X 0101 010185-X-X-X- 1010 1010 $AA 170-X-X-X- 1010 1010 $AA 170-X-X-X- 1010 1010 $AA 170X-X-X-X 1101 0101 $D5 2 13X-X-X-X 1101 0101 $D5 2 13X-X-X-X 1101 0101 $D5 2 13$55Figure 9-2E.DOTBITPATTERN PATTERN HEX DEC- ---------------------------- ---------------X----- 0000 0010 $02 2-X----- 0000 0010 $02 2X-X---- 1000 0101 $85 133X-X---- 1000 0101 $85 133X-X-X-- 1001 0101 $95 149-X-X--- 1000 1010 $8A 13 8-X-X--- 1000 1010 $8A 138-X----- 1000 0010 $82 130Figure 9-2F.


CHAPTER 9-Hl-RES COLOR 87DOTPATTERNBITPATTERNHEXDEC--X-X-X--X-X-X----X-X------X-- - --- --- - - - - -- -------- --- - -131131 1311313131131 1311313131131 131313131311313 13131313131313·13 13 13131313131313 1313131313131313 1313131313131313 13131313$54 84$54 84$513 813$413 64$1313 13$1313 1313$1313 13$1313Figure 9-2G.DOTPATTERNBITPATTERNHEXDEC---- ------------ --- ------ - - --- - ------- -- - ---X-X-X- 1313113 113113 $2A 42-X-X-X- 1313113 113113 $2A 42-X-X-X- 1313113 113113 $2A 42-X-X--- 13131313 113113 $13A 113-------13131313 13131313 $1313 13-- - - --- 13131313 13131313 $1313 13-- - - ---13131313 13131313 $1313 13-- -----13131313 13131313 $1313 13Figure 9-2H.DOTBITPATTERN PATTERN HEX DEC--------- - -------------- - ----- -------- - ----X-X---- 13131313 131131 $135 5X------ 13131313 1313131 $131 1----- --13131313 13131313 ' $1313 13-------13131313 13131313 $1313 13---- ---13131313 13131313 $1313 13- - - ---- 13131313 13131313 $1313 13- - --- - -13131313 13131313 $1313 13- ------ 13131313 13131313 $1313 13Figure 9-21.


88 APPLE II COMPUTER GRAPHICSIn Figures 9-20 through 9-2F, some of the color bits are turned on inorder to select group 2 colors.Now that you have digitized the figure, you need a program such asListing 9-3 below to place those v<strong>al</strong>ues in Hi-Res screen memory. Theprogram l<strong>et</strong>s you select the position of the <strong>apple</strong> on the screen, withinlimits. You will be asked fo r the row and column number fo r the upper leftcorner of the <strong>apple</strong> (Figure 9-2A); the rest of the <strong>apple</strong> follows from there.1!{l REM COLOR APPLE2!{l REM3!l' REM4!{l REM INITIALIZE5!{l REM6!{l OFST% = 1!{l24 : DIM SA%(2!{l)7!{l FOR I = 1 TO 2!{l : READ SA% ( I) : NEXT8!{l HGR : VTAB (23)9!{l REM1!{l!{l REM GET ROW & COL #'S11!{l REM12!{l INPUT "BOX ROW #? (1-18) II ;R$13!{l R% = VAL (R$) : IF R% < 1 ORR% > 18 THEN PRINT CHR$ (7) :GOTO 12!{l14!{l INPUT "BOX COL. #? (1-38)";C$15!{l C% = VAL ( C$) : IF C% < 1 ORC% > 38 THEN PRINT CHR$ ( 7) :GOTO 14!{l16!{l REM17!{l REM PLOT APPLE18!{l REM19!{l FOR K = !{l TO 22!{l!{l FOR J = !{l TO 221!{l BA% = SA% (R% + K) + (C%-1) + J22 FOR I = 1 TO 823!{l READ V% : POKE BA% + (I-1) *OFST%,V%24!{l NEXT I,J, K25!{l END37!{l REM38!{l REM ADDR . TABLE39!{l REM4!{l!{l DATA 8192,832!{l,8448,8576,87!{l4,8832,896!{l,9!{l88, 8232,836!{l, 848841!{l DATA 8616,8744,8872,9!{l!{l!{l,9128,8272,84!{l!{l,8528, 865648!{l REM49!{l REMAPPLE TABLE


CHAPTER 9-Hl-RES COLOR 895 REM51 DATA '',8,84,84,4252 DATA J ,6,12,12,12,42,42,8553 DATA ''''5,21,5,254 DATA 42,42,21J,21J ,21J ,17, 17, 1755 DATA 85, 85, 17,17,17,21J ,21J ,21J56 DATA 2,2,1JJ ,1JJ,149,1J 8,1J8,1J57 DATA 84,84,8,64,,' '58 DATA 42,42,42,1,'''59 DATA 5,1,'''''Listing 9-3. POKE color <strong>apple</strong>.Line 6


90 APPLE II COMPUTER GRAPHICSblue as they are in an even column (zero) and a group 2 byte. When boxD is plotted in the second column of boxes from the left, those same dotsdisplay orange since they are now in an odd column of dots (seven). Thatpattern <strong>al</strong>ternates across the width of the screen.Another weakness of the program is that you are constrained to <strong>al</strong>waysselect the <strong>apple</strong>'s position in increments of a fu ll box.' That is, you may notchoose to draw the <strong>apple</strong> starting three and one-h<strong>al</strong>f boxes into the screen,but instead you must <strong>al</strong>ways use a whole number of boxes. There ar<strong>et</strong>echniques which l<strong>et</strong> you POKE the figure beginning with any of the 280columns and 160 rows on the screen, but they will not be pursued here, asthe results are b<strong>et</strong>ter accomplished using the m<strong>et</strong>hods discussed in thechapters covering Shapes and Byte-Move Graphics.Everything You Know Is F<strong>al</strong>seIn this section we appear to contradict a portion of what we have just toldyou. We have said (repeatedly) that there are 280 positions across the Hi­Res screen, but now we attempt to convince you that there are only 40positions, and <strong>al</strong>so that there are 560 positions, and som<strong>et</strong>imes 140 positions.ARRGH!! Hopefu lly, by temporari ly confusing the issue, we willclarify it. The purpose is to provide you with sever<strong>al</strong> clear and accu ratemodels fo r use ·with the Hi-Res screen- not physic<strong>al</strong> models, but ratherconceptu<strong>al</strong> ones which will help structure your thinking and simplify yourtask when designing Hi-Res output. Each model is based on a diffe rentnumber of positions across the screen, but they do use the same numberof positions down the screen: 160 rows of dots (1 92 fo r full screen).Why 4(1?The explanation of the 40 position model is easiest since you have <strong>al</strong>readyde<strong>al</strong>t with it, perhaps unknowingly. To process any dot, you must unavoidablyde<strong>al</strong> with the byte which contains that dot, and there are only 40bytes across each line of the screen. When you developed and plotted theHi-Res <strong>apple</strong>, your design was developed in one-byte increments andplotted with the consideration of having only 40 boxes or bytes across thescreen in which to draw the figure.What About 28(1?The 280-position model is still v<strong>al</strong>id. Each of the 40 bytes displays sevendots, and that yields 280 dots across the screen. The 280-dot concept isusefu l when you are designing your figures, but when you begin to digitiz<strong>et</strong>he data, you natur<strong>al</strong>ly adopt the 40-position model, perhaps without re<strong>al</strong>izingit. Refer to the section where you worked with the Hi-Res <strong>apple</strong>, and


CHAPTER 9-Hl-RES COLOR 91notice how the 28©-dot model led into the 4©-byte model with no troubleat <strong>al</strong>l when you began to digitize the figure.Only 1411When working with colors on the Hi-Res screen, most game programmersconsider the width of the screen to be composed of only 14© availabledots. Clearly, it requires two adjacent dots to display white, and if youwould display a line of green dots, then b<strong>et</strong>ween each green dot there is ablack one, so you can see that it takes two dots to display a color <strong>al</strong>so. Ifyou look back at Figure 9-2, the picture of the Hi-Res <strong>apple</strong>, you will se<strong>et</strong>hat only every second dot was used for the colored portions. Since a unitof color requires two dots, you may reasonably interpr<strong>et</strong> the screen to<strong>al</strong>low only 14© units of color across.Further, when you plotted the <strong>apple</strong> in different positions, the colorschanged depending on where it was plotted. In order to maintain theorigin<strong>al</strong> colors, any dot that was origin<strong>al</strong>ly even must <strong>al</strong>ways be plotted asan even dot, and the same idea fo llows for the odd dots. To accomplishthat, the figure must be moved in multiples of two dots at a time, so for thisreason <strong>al</strong>so, there are only 14© possible positions across the screen.Would Yau Believe 5611?Despite what the Apple documentation says, there are actu<strong>al</strong>ly 56© dotsavailable across the Hi-Res screen. By now you probably think we ar<strong>et</strong>ot<strong>al</strong>ly bonkers, so we will prove our point by demonstration. First, enterthe Monitor, and then the Hi-Res mode, by typing:CALL -151C5C57Next, clear the decks fo r action by typing:2 : 21< 2 . JFFEMIf this seems obscu re to you, perhaps another reading of the Monitor andSoft Switch chapters is needed.- Figure 9-3 shows the hex address of the fi rst byte for each of the fi rst 14screen lines, and the dot and bit patterns for 14 v<strong>al</strong>ues to be placed inthose locations.


92 APPLE II COMPUTER GRAPHICSHEX HEX BIT DOT....h.PP3§§ ___ Y8------8IIfiN ______ EIIBN __2!il!il!il !ill !il!il!il!il !il!il!ill24!il!il 81 l!il!il!il !il!il!ill28!il!il !il2 !il!il!il!il !il!ill!il2C!il!il 82 l!il!il!il !il!ill!ilJ!il!il!il !il4 !il!il!il!il !ill!il!ilJ4!il!il 84 l!il!il!il !ill!il!ilJ 8!il!il !il8 !il!il!il!il l!il!il!ilJ C!il!il 88 l!il!il!il l!il!il!il2!iJ8!il l!il !il!il!ill !il!il!il!il248!il 9!il l!il!ill !il!il!il!il288!iJ 2!il !il!ill!il !il!il!il!il2C8!il A!il . l!ill!il !il!il!il!ilJ !il8!il 4!il !ill!il!il !il!il!il!ilJ48!il C!iJ ll!il!il !il!il!il!ilFigure 9-3.X------X-------X------X-------X------X-------X------X-------X------X-------X------X-------X------XPlace the v<strong>al</strong>ues in memory by typing2!il!iJ!iJ : !ill24!iJ!il : 8128!iJ!iJ : !il2J48!iJ : C!ilLooking at the dot patterns, the expected result is seven pairs of dots, withone dot of the pair directly above the other.However, when you enter the v<strong>al</strong>ues, what you g<strong>et</strong> is a diagon<strong>al</strong> line;the pairs of dots are not stacked as we expected, even though the samedot was turned on in each byte of the pair. This phenomenon relies on thefact that the fi rst byte in each pair was in color group one, and the secondbyte was from group two, as d<strong>et</strong>ermi ned by the color bit on the left side ofthe byte.One byte of screen memory actu<strong>al</strong>ly controls fou rteen dots on thescreen, and the two s<strong>et</strong>s of seven dots are interleaved as shown in Figure9-4.DOT # 1 2 3 4 5 6 7l•l•l•l•l•l•l•l•l •l•l•l•l•l • I• GROUP 1i GROUP 2Figure 9-4. Double dot picture.


CHAPTER 9-Hl-RES COLOR 93One s<strong>et</strong> of seven dots may be displayed by s<strong>et</strong>ting the color bit to displaygroup 1, and the other seven are displayed by s<strong>et</strong>ting the color bit to group2. For example, change the group two v<strong>al</strong>ue in location $24(/)(/), which ispresently 1(/)(/)(/) (/)(/)(/)1, to the equ iv<strong>al</strong>ent group one v<strong>al</strong>ue, (/)(/)(/)(/) (/)(/)(/)1, andthen back again by typing:2400 : 012400 : 81You will see the dot in line two move slightly to the left, and then back tothe right even though in both cases it was the left-most dot of the groupbeing displayed.The point is that there are actu<strong>al</strong>ly 56(/) dots across the Hi-Res screen,though only h<strong>al</strong>f the dots in any byte are available at any one time. Thefact that the group 2 dots are slightly to the right of the group 1 dots maybe exploited to draw a smoother line than possible with just one colorgroup, as we did in the example, or to smooth the outli nes of figures onthe screen in order to improve their looks.How1s That Again ?The result of <strong>al</strong>l this discussion is that there are 56(/) dots across the Hi-Resscreen, 28(/) of which are potenti<strong>al</strong>ly displayable. There are 14(/) pairs ofdots available when generating color, and <strong>al</strong>l the data for a line on thescreen must fit into 4(/) bytes. You use the units and model which are mostconvenient for your immed iate purpose.Hither, Dither, and YonAccord ing to the Apple documentation, there are only six colors availablein Hi-Res mode, including black and white. Since Apple built the <strong>computer</strong>one would expect them to be correct, and they are-sort of. Thereare programs available in Hi-Res which use many more than six colors,and that seems to contradict the reference manu<strong>al</strong>s.As you have seen for yourself, the dots on the Hi-Res screen are trulycapable of producing only black, white, viol<strong>et</strong>, blue, green, and orange;but when you add to that a good measure of programmer imagination andcreativity, it is possible to artifici<strong>al</strong>ly produce extra hues by a process c<strong>al</strong>led"dithering."Run the following program.10 REM DITHER DEMO20 REM30 DLAY = 200 : REM DELAY VALUE40 SZ = 50: REM SIZE


94 APPLE II COMPUTER GRAPHICS50 HGR60 FOR Cl = 1 TO 770 FOH C2 = 1 TO 780 FOR Y = 0 TO SZ STEP 290 HCOLOR = Cl100 HPLOT 0 ,Y TO SZ,Y110 HCOLOR = C2120 HPLOT 0, Y + 1 TO SZ , Y + 1130 NEXT Y140 FOR I = 1 TO DLAY : NEXT I150 NEXT C2160 NEXT Cl170 ENDListing 9-4. Dithering.You should see a rectangle in the upper left corner of the screen whichcontinu<strong>al</strong>ly changes color, and in the process takes on some shades otherthan the basic six. You may change the speed by <strong>al</strong>tering the v<strong>al</strong>ue ofOLAY in line 30, and the size of the rectangle is d<strong>et</strong>ermi ned by the v<strong>al</strong>ueof SZ in line 40.All the program does is plot pairs of horizont<strong>al</strong> lines in two <strong>al</strong>ternatingcolors (C1 and C2), then change one or both colors and do it again. As theroutine cycles, you can see the basic colors-green, blue, viol<strong>et</strong>, orange,black, and white-but <strong>al</strong>so some other colors such as light green and hotpink. The extra colors are generated by two colors blurring tog<strong>et</strong>her toform a mixture, for instance white and green blurring to form light green.There are a great many variations used to produce other shades. Forexample, you might mix the colors orange and black, by <strong>al</strong>ternating themin a checkerboard pattern to produce dark orange, or perhaps by drawinga pure orange line fo llowed by a line which <strong>al</strong>ternates orange and black.The possibil ities are limited only by your imagination.There are a few considerations, however. For instance, if you tried to<strong>al</strong>ternate green and viol<strong>et</strong> dots across the screen, as in the following shortprogram, you would run into severe problems.10 HGR20 Cl = 2: C2 = 130 FOR X = 0 TO 278 STEP 240 HCOLOR = Cl : HPLOT X , Y: REM VIOLET50 GOSUB 10060 HCOLOR = C2 : HPLOT X + 1, Y: REM GREEN70 GOSUB 10080 NEXT X90 END100 FOR I 1 TO 200 : NEXT I: RETURNListing 9-5.


CHAPTER 9-Hl-RES COLOR 95Thanks to the delay loop in line 1©©, you will be able to see <strong>al</strong>l theaction when the program runs. The fi rst dot appears in its proper viol<strong>et</strong>,but then the next dot comes on- not as the expected green, but as white,and the first dot turns white with it. The remaining dots <strong>al</strong>so display white,regardless of the color s<strong>et</strong> in the program. Remember, any time two consecutivedots in a row are on, they display white, so if you wish to <strong>al</strong>ternatecolors across a line, you must leave one or more black dots b<strong>et</strong>weeneach color and the next (see Figure 9-5).m 1 m 1 m 1 mFigure 9-5. Alternating colors.Even if you leave black dots b<strong>et</strong>ween colored dots, you are still notcompl<strong>et</strong>ely safe. The next listing ostensibly plots viol<strong>et</strong> in col umn ©, blue incolumn 2, viol<strong>et</strong> in column 4, blue again in column 6, and so on acrossthe line.l HGR2 Cl = 2:C2 = 63 FOR X = TO 276 STEP 44 HCOLOR = Cl : HP LOT X , Y: REM VIOLET5 GOSUB l6 HCOLOR = C2 : HP LOT X + 2, Y: REM BLUE7 GOSUB l8 NEXT X9 ENDl FOR I = 1 TO 2 : NEXT I : RETURNListing 9-6.When the program runs, a viol<strong>et</strong> (group 1) dot will appear in column ©,then a blue dot will appear in column 2. Since blue is in group 2, theentire byte will become group 2 and the viol<strong>et</strong> dot will change to blue<strong>al</strong>so. Next, a viol<strong>et</strong> dot will be plotted in column 4, turning the previoustwo dots viol<strong>et</strong>, and then a blue dot will be plotted in column 6 to turneverything blue again. Column 8 is in the next byte of memory so whenthe viol<strong>et</strong> dot is plotted there, the previous four dots are unaffected.Column 1 © displays blue, and then column 12 displays viol<strong>et</strong> so that th<strong>et</strong>hree dots in that byte are <strong>al</strong>l viol<strong>et</strong>. Column 14 begins the third byte, and,as it displays blue, the previous three dots remain viol<strong>et</strong>. The fi n<strong>al</strong> patternis a line which has <strong>al</strong>ternating viol<strong>et</strong> and blue segments.This demonstrates a propensity of Hi-Res <strong>graphics</strong> c<strong>al</strong>led "clashing."Clashing is the inadvertent switching of a dot from one color group to theother and occu rs when you attempt to plot colors from opposite colorgroups within the same byte-the <strong>computer</strong> simply cannot and will not doit-so the attempt turns the first colors plotted into colors from the secondgroup plotted. You cannot mix green with orange or blue in the samebyte, nor can you put orange or blue in the same byte as viol<strong>et</strong>. Clashing is


96 APPLE II COMPUTER GRAPHICSone of the facts of Hi-Res life, and it is virtu<strong>al</strong>ly impossible to avoid compl<strong>et</strong>ely,though carefu l planning of your figures will help you to minimizeit.Wrapping It UpBy their very nature, the topics discussed in this chapter are perhaps themost confusing in the book. The color bit controls the colors available tothe rest of the byte, and the color of an individu<strong>al</strong> dot is d<strong>et</strong>ermined bywh<strong>et</strong>her it is in an odd or even column of the screen, and by the s<strong>et</strong>ting ofthe color bit. To display a given color, you must fi rst d<strong>et</strong>ermine wh<strong>et</strong>her itis in color group 1 or 2, and then wh<strong>et</strong>her it is available on an even dot oran odd dot. There is no way to display two diffe rent color groups in thesame byte at <strong>al</strong>l, and no way to display two colored dots next to eachother without both dots turning to white.We have seen that, despite what the manu<strong>al</strong>s say, there are 56


CHAPTER 9-Hl-RES COLOR 97Exercises1. Use the POKE statement to display the following colors at the givenlocation on the screen. It is assumed that column (/) is the left-m ostcolumn of dots.a. Viol<strong>et</strong> in the tenth row and twenti<strong>et</strong>h column of dots.b. Orange in row ten, column 71 .c. Blue in row nin<strong>et</strong>y, column 1(/)4.d. Green in row 16(/J, column 279.2. Write a BASIC program to cycle through each of the eight v<strong>al</strong>ues forHCOLOR and plot a line of that color, followed by a line which <strong>al</strong>ternatesthat color with black, and creates a rectangle containing 15 pairsof such lines. The routine needs to account for the fact that green andorange may only be plotted on odd columns, and viol<strong>et</strong> and blue onlyon even.3. Take the butterfly designed in Exercise 3 of Chapter 8 and display it incolor.


991 111ObjectivesAfter reading Chapter 1(/) you should be able to (if fo rced):• Draw a vector diagram fo r a figure.• List and encode the vectors from the diagram.• Group the vector codes into bytes.• Develop a shape table from one or more shapes.• Save and recover a shape table from disk or tape.• Use the shape commands to plot, sc<strong>al</strong>e, and rotate a shape.In this chapter you will investigate the properties of Apple shapes andshape tables. The term "shape" is used here in a very specific sense. Fromthe programmer's point of view, a shape could be <strong>al</strong>most any figure-aspaceship, frog, <strong>al</strong>ien, and so on-but to the <strong>computer</strong> a shape is a s<strong>et</strong> ofhighly formatted data which l<strong>et</strong>s the machine produce a display on thescreen.Though using a shape can cost you extra time at the outs<strong>et</strong>, in practice itmakes drawing and animating figures much faster and simpler than th<strong>et</strong>echniq ues we have discussed in the previous two chapters. Shapes may<strong>al</strong>so be used to simulate text on the Hi-Res screen.After you digitize a: figure to create a shape, that shape can be plotted onthe screen as a unit so that you no longer need to concern yourself withc<strong>al</strong>culating the necessary addresses, nor the v<strong>al</strong>ues to POKE. APPLESOFTtakes care of <strong>al</strong>l those grubby little d<strong>et</strong>ails, and <strong>al</strong>so plots the shape fasterthan you could if you used the READ and POKE technique discussed inearlier chapters.


100 APPLE II COMPUTER GRAPHICSWe will show you how to digitize a figure into a shape, put the shapeinto a shape table, and how to save the results of your labor. From there,you will use APPLESOFT to draw, erase, rotate, and sc<strong>al</strong>e your shape. Youwill see very quickly how tedious and time-consuming shapes can be ifyou do them you rself. Fortunately, for your sanity, any good Hi-Res <strong>graphics</strong>editor will contain a provision for making shapes from the figures youdevelop on the screen, so that the machine does <strong>al</strong>l the meni<strong>al</strong> c<strong>al</strong>culationsinstead of you. We highly recommend using a Hi-Res editor to makeshapes.Ta the Vector Belong the SpoilsTo draw a shape, the Apple II uses a series of vectors which tell it wh<strong>et</strong>heror not to plot the current point; and which direction to move to find thenext point. Each vector is visu<strong>al</strong>ly represented by a little arrow with a doton one end. (See Figure HD-1 B.) ·We will begin with a very simple figure inorder to illustrate the technique and rules for digitizing a shape. FigureHll-1 A is the block diagram of a figure which uses only three dots, andFigure 10-1 B shows the six vectors used to turn them on .•i6. 0--; _._•ABFigure 19-1.


CHAPTER 10-SHAPING UP 101A closed dot at the beginning of a vector ind icates that the point is to beplotted, and an open dot indicates that the point is to be left <strong>al</strong>one(skipped), and the arrow points the direction to the next point. It is vit<strong>al</strong> toremember that the plotting or skipping comes before the moving. We willuse "P" to represent a dot which is to be plotted, and "S" to representone which is skipped. Also, "U" means that we move up, "R" right, "D"down, and "L" left. It requires both a plot/skip and a move command torepresent each vector. For example, SD represents a vector which skipsthe current point and moves down. With that in mind l<strong>et</strong>'s attempt tomake sense out of Figure 1©-1 B.Starting at the beginning, the bottom block, we will trace the path of thevectors to its terminus. The fi rst vector ind icates that the point is to beplotted, and then we are to move up (PU). The second vector leaves thedot <strong>al</strong>one and moves right (SR). The third vector plots and moves up (PU),while the fourth, fifth, and sixth skip and move left (SL), skip and move up(SU), and plot and move right (PR), respectively. You end up with this listof vectors:PUSRPUSLSUPRThe last vector plots before it moves, and the direction of movement isunimportant. Right was chosen arbitrarily-the important characteristic ofthat vector was the plot to turn on the last point.The <strong>computer</strong> generates the shape on the screen by following this seriesof instructions: plot-up, skip-right, plot-up, skip-left, skip-up, and plot-right.You need to encode those vectors for the machine using binary codes fo reach instruction as shown in Figure 1©-2.Plot 1Skip lilUp lillilRight !illDown lfl"lLeft 11Figure 10-2. Vector codes.To encode the first vector (PU), you would use 1 for Plot and (/)(/) for Upto g<strong>et</strong> 1 ©©. The second vector (SR) yields ©©1 , and the th ird (PU) gives you1 (/)(/) again. Take a piece of paper and write down the six vectors and theirrelated codes. You should end up with:PU lfl"llilSR : lillill


102 APPLE II COMPUTER GRAPHICSPUSLSUPR1!011llTo communicate those v<strong>al</strong>ues to the <strong>computer</strong>, you need to put themtog<strong>et</strong>her to form "byte size" pieces. For the purpose of encoding thevectors, each byte is divided into three sections, A, B, and C, as shown inFigure 10-3.c B ABIT # .__ 5__.4-'-3_.I l_2_.__1_.__o___.Figure 11-3. Byte divisions.Begin by placing the fi rst vector code in section A, the second in B, andthe third (if possible) in C. It is NOT permissible to break a code in themiddle and carry the remainder over to the next byte; you have no doubtnoticed that section C contains only two bits, so it cannot contain a compl<strong>et</strong>ecode. The logic<strong>al</strong> conclusion is that section C is unusable, and youmay <strong>al</strong>ways leave it blank (00) . However, in certain cases section C can beused; the left hand bit of any code in section C is assumed to be 0, andonly the two bits on the right (the direction bits) are entered. For example,10 000 101 would cause the <strong>computer</strong> to execute PR (101), SU (000), andthen SD (10).Any vector placed in section C must be a "skip" vector; the third vectorin our shape is a plot vector so it will not fit in section C. Therefore, it mustbe placed in section A of the next byte. When plotting the shape, the<strong>computer</strong> will ignore the two zeros at the left end of a byte. In fact, anyzero section of a byte is ignored if <strong>al</strong>l of the following sections of that byteare zero <strong>al</strong>so. For example, with 00 000 01 1, the <strong>computer</strong> will skip andmove left (01 1 ), then ignore the rest of the byte, and begin the followingbyte. It fo llows, then, that section C can never execute an up vector (00),since it would be ignored as in -00 111 000 where the <strong>computer</strong> wouldskip-up (000), plot left (1 11 ), and theri ignore section C. In that case, theskip-up vector would have to be placed in section A of the next byte.Refer to Figure 10-4 as we group the vectors for our shape.C B A C B A HEXBYTE # 1SR PU2341-----1SLPLPUSUFigure 18-4.00000000001011111000100100011000oc1C3B00


CHAPTER 10-SHAPING UP 103In the first byte, section A contains the PU code, section B the code forSR, and section C is s<strong>et</strong> to (/)(/) to be ignored. The next PU code is in sectionA of byte number 2, followed by the SL in section B, and (/)(/) in C, which isagain ignored. The SU is in section A of byte number 3, followed by PRand then (/)(/). Byte number four is fi lled with zeros and marks the end ofthe shape. Any shape is ended by the fi rst zero byte encountered; therefore,if you intend to put the skip-up code ((/)(/)(/)) in sections A and B,section C MUST NOT BE ZEROS!Following these rules, Figure 1 (/)-4 shows the entire shape encoded infour bytes. The shape may not be displayed until it is entered in a shap<strong>et</strong>able, but before we do that, l<strong>et</strong>'s digitize one more shape fo r practice.UFO TimeIf you did not believe in flying saucers before, you will soon, as you areabout to create one. Figure 1 (/)-SA contains the block diagram of thesaucer you will create and Figure 1(/)-SB contains its vector diagram.ABFigure 1 Cll-5. Saucer.It is necessary to plot each of the 23 dots shown in Figure 1 (/)-SA, butthe order in which they are traced is not important. However, changingthe pattern of the vectors will <strong>al</strong>so <strong>al</strong>ter the v<strong>al</strong>ues used in the shape, so forthe sake of consistency we suggest that you use the vectors shown inFigure 1 (/)-SB. Follow the vectors and write down a list of their functions asyou go. Your list will look like this:PR, PR, PR, SR, SD , PL, PL, PL, PL,PL, PL, PD, PR, SR, SR, PR, SR, SR,PD, PL, PL, PL, PL, PL, PL, PD, SR,SR, PR, PR, PRGo back and double check your list; accu racy is tedious but imperative!!The next step is to encode the instructions as bytes. We have done thisin Figure 1 (/)-6. To be on the safe side, the encoding is done in a series ofsm<strong>al</strong>l steps, each of which is easily checked. The extra care taken now will


104 APPLE II COMPUTER GRAPHICShelp minimize errors which can cost you hours of time and frustration, andwill reduce the urge to physic<strong>al</strong>ly abuse your <strong>computer</strong>.C B AC B AHEXDECIMAL-- PR PRSD SR PR-- PL PL00 101 10110 001 10100 111 1112D8D3F4514163-- PL PL-- PL PLSR PR PD00 111 11100 111 11101 101 1i03F3F6E6363110SR PR SR-- PD SR-- PL PL01 101 00100 110 00100 111 11169313F1054963-- PL PL-- PL PLSR SR PD00 111 11100 111 11101 001 1103F3F4E636378-- PR PR-- -- PR00 101 10100 000 10100 000 0004550Figure 11-6.First, the instructions are inserted into the bytes by sections, and then eachinstruction is converted to its binary code. After that, the resulting byte isconverted to hex and decim<strong>al</strong>. You now have digitized the saucer, but aswith the first shape, you must insert it in a shape table in order to displayit.S<strong>et</strong>ting the TableA shape table is a collection of the digitized data for one or more shapes,up to a maximum of 255, plus some extra info rmation at the beginningwhich is needed by the machine. The fi rst thing your Apple needs to knowis the number of shapes contained in the table, and the next thing that itwill ask is where each of the <strong>al</strong>leged shapes is located within the table. Wewill use Monitor to s<strong>et</strong> up a shape table containing our two shapes atmemory location $3


CHAPTER 10-SHAPING UP 105( (J(J) has no meaning, but it must be there to hold the place. The next twobytes ( (J6 (J(J) are used by the system to locate the first shape in the table,and the pair following ( (JA (J(J) locate the second shape. (Remember thatyour Apple eats addresses in lo-byte/hi-byte form.) The next byte ( (JC) isthe beginning of the first shape. Each shape in a table is located by a pairof bytes, and the fi rst shape begins directly following the last pair of locatingbytes.Continue to enter the shape table by typing:=- 'J(J8: J(J (J(J 2D 8D JF JF JF 6E69 31 JF JF JF 4E 2D (J5 (J(JThe second byte of this line ( (J(J) is the end of your first shape, and thesecond shape fo llows immediately, beginning with 2D and ending withanother (J(J .Finding Your ShapesIf you put your fi nger at the beginning of the table (v<strong>al</strong>ue (J2) and thencount in, you will find that the fi rst shape begins with the sixth byte of th<strong>et</strong>able as we promised when we entered (J6 (J(J for the fi rst pair of locationbytes. (The (J2 counts as the zeroth byte of the table.) The location bytesgive the location of the corresponding shc,i.pe RELATIVE TO THE BEGIN­NING OF THE TABLE. A v<strong>al</strong>ue used to specify a location relative to somefixed address is c<strong>al</strong>led an offs<strong>et</strong>. The second pair of location bytes is (JA (J(J,and since $A equ<strong>al</strong>s 1 © that claims that the second shape begins with th<strong>et</strong>enth byte of the table. When you key in a table on your own, you mayiniti<strong>al</strong>ly put zeros in the location bytes for each shape, and after the rest ofthe table is entered and the locations are fixed, go back to the locationbytes and enter the correct offs<strong>et</strong>s.Shapes tend to be long (and drawn out?), and it is not unusu<strong>al</strong> to have <strong>al</strong>arge number as an offs<strong>et</strong>, such as 2©85. To enter that v<strong>al</strong>ue in the locationbytes, fi rst turn it into a hex number, 2©85 equ<strong>al</strong>s $©825. Divide thisnumber into lo-byte ($25) and hi-byte ($©8). The fi rst byte for the offs<strong>et</strong>would be $25, and the second $©8. (Remember lo-byte/hi-byte.)Using DrawIf you have worked with your own shapes, at this point you would havespent a great many hours in designing, digitizing, and entering the data (amarvelous form of self punishment!). You are fi n<strong>al</strong>ly ready to do one ofthree things: (1), display the shapes; (2), forg<strong>et</strong> the whole thing or (3),assign the project to someone you d<strong>et</strong>est! We will select option one.If you have been following <strong>al</strong>ong, you presently have the shape tableentered at $3©©. Prior to drawing the shapes, you need to inform yourApple as to the table's whereabouts by placing the starting address of th<strong>et</strong>able at memory locations 232 and 233 ($E8 and $E9). APPLESOFT <strong>al</strong>ways


106 APPLE II COMPUTER GRAPHICSlooks in those locations to find the starting address of the current shap<strong>et</strong>able, and if you fo rg<strong>et</strong> to load the proper address, there is no telling whatis going to pop up on the display. Since you are still in Monitor, typeE8 : 00 03This will put the address $0300 (remember lo-byte/hi-byte) in the properlocation. Fin<strong>al</strong>ly, key in and run the following program.1 REM SHAPE DEMO20 REM30 ROT = 0: REM SET ROTATION TO 040 s = 150 HGR : HCOLOR = 360 FOR I = 70 TO 190 STEP 3070 SCALE = S: REM SET SCALE TO S80 DRAW 1 AT I,3090 DRAW 2 AT I,100100 S = S + 1 : REM INCREMENT S110 NEXT I120 ENDListing 19-1.Line 30 s<strong>et</strong>s the rotation of the figure to 0 (no rotation) so it will bedrawn as it was designed, and line 50 s<strong>et</strong>s the Hi-Res <strong>graphics</strong> mode andthen s<strong>et</strong>s HCOLOR to 3 (white1). Line 70 s<strong>et</strong>s the sc<strong>al</strong>ing factor to thepresent v<strong>al</strong>ue of S; the sc<strong>al</strong>ing factor will increment from one through five.At SCALE = 1, the figure is the same size as it was designed, and at SCALE= 5, it is five times the origin<strong>al</strong>. The SCALE may be s<strong>et</strong> to any numberfrom zero to 255, but, as you will see, increasing the SCALE tends togreatly distort the shape. If you wish, you may s<strong>et</strong> SCALE = 0, but zerocorresponds to 256, and the shape will be enlarged beyond recognition.Line 80 DRAWs shape number 1 beginning at the coordinates given by1,30 and line 90 DRAWs shape 2 at I, 100. The coordinates denote theplacement of the point of origin for the shape-the place where the chainof vectors began.If everything was entered as specified, the program will draw five copiesof both shapes, and each copy will increase in size.Saving Shape TablesIf you have a disk drive, you may save the table by using BSAVE to sav<strong>et</strong>he range of memory which contains it. Remember to save the fi n<strong>al</strong> 00with the rest of the table, otherwise you will g<strong>et</strong> some unpredictable results


CHAPTER 10-SHAPING UP 107as the <strong>computer</strong> draws off the end of your shape and on into memorygarbage. To save the table currently in memory, type:orBSAVE TABLE1,A$3, L$19BSAVE TABLE1, A768 , L25The only difference b<strong>et</strong>ween the two is that the first gives the address (A)and length (L) in hex, and the second gives them in decim<strong>al</strong>. If you add thefollowing lines to Listing 10-1, they will automatic<strong>al</strong>ly load the table andinform the Apple of its location when the program is run.22 PRINT CHR$(4); "BLOAD TABLEl"24 REM CHR$ (4) IS CTRL-D26 POKE 232,: POKE 233 ,328 REM $ INTO $E8, $3 INTO $E9A longer shape table may not fit at $3(/)(/) since only the range $3©©-$3BF isavailable; a tot<strong>al</strong> of 192 bytes. (See the memory map in Chapter 3.) Longtables are often placed up at the top of available memory, and thenHIMEM s<strong>et</strong> underneath them so they will not be overwritten by the variablesof your APPLESOFT program. For a 48K machine, DOS s<strong>et</strong>s HIMEM to$96©© (384©©), so if your shape table was $1 (]')(]') (256) bytes long you wou IdBLOAD [tablename] ,A$95HIMEM : 38144or, in decim<strong>al</strong>,BLOAD [tablename] , A38144HIMEM : 38144A more thorough discussion of the use of BLOAD and HIMEM can befound in Chapter 7 where we covered saving the Low-Res screen.If you wish to save the table on tape, you must enter the Monitor anduse a variation of the Write command to save the memory range containingthe table. You must know the hex v<strong>al</strong>ues fo r the fi rst address, the lastaddress, and the length of the table. In the table that contains the saucer,this would be $3©©, $318, and $19, respectively. First, you put the lengthof the table in locations (]') and 1 by typing:: 19 (Remember lo-byte/hi-byte)then write the information onto the tape using the Monitor "W" . Writebytes zero and one onto the tape (they contain the length of the table),and then immediately write the table itself. To do <strong>al</strong>l this in one instruction,you could type:.lW 3 .318W


108 APPLE II COMPUTER GRAPHICSRemember to l<strong>et</strong> the tape run for a moment on the record s<strong>et</strong>ting b<strong>et</strong>weentyping the second "W" and pressing the r<strong>et</strong>urn key.You may use the Monitor READ command to reload the table, butAPPLESOFT has a much b<strong>et</strong>ter m<strong>et</strong>hod using the SHLOAD command.SHLOAD loads the shape table from tape and stores it just below HIMEM.HIMEM is s<strong>et</strong> below the table to protect it, and the starting address of th<strong>et</strong>able is placed in $E8 and $E9. The beauty of this process is that <strong>al</strong>l thed<strong>et</strong>ails are taken care of automatic<strong>al</strong>ly. The only command you give isSHLOAD, and, unlike the Monitor command, SHLOAD may be used fromwithin a BASIC program. For our example, rewind your shape table tape,push the play button, typeSHLOADand everything is done for you. TA DA!!The table may <strong>al</strong>so be loaded directly as part of the program by using thePOKE statement. Listing 10-2 POKEs the data for our shape table, asks youto specify the sc<strong>al</strong>ing factor (1-255), and then draws the saucer on thescreen with ten different rotation v<strong>al</strong>ues.Learning by ROTROT = Ill causes the shape to be oriented in the same way as it wasdefined-no rotation. When ROT = 16, DRAW will show the shape rotated90 degrees clockwise from the origin<strong>al</strong>, ROT = 32 rotates 180 degrees, andROT = 48 rotates it 270 degrees clockwise (90 degrees counter-clockwise).ROT = 64 is back where it started, and so fo rth in increments of 16 <strong>al</strong>l theway through 256.The ROTation param<strong>et</strong>er is parti<strong>al</strong>ly dependent on the s<strong>et</strong>ting of SCALE.According to the APPLESOFT manu<strong>al</strong>, at SCALE = 1 , only the four rotationv<strong>al</strong>ues [Ill, 16,32,48] are recognized. However, as you can see by usingListing 10-2, the v<strong>al</strong>ues [8,24,40,56] <strong>al</strong>so work at SCALE = 1 . At SCALE = 2,sixteen rotations are available, with [4, 12,2©,28,36,44,52,60] being the addition<strong>al</strong>v<strong>al</strong>ues. The larger the sc<strong>al</strong>e, the more rotations available, up to apoint. If you s<strong>et</strong> ROT to a v<strong>al</strong>ue it does not recognize, it will rotate theshape to a v<strong>al</strong>ue it does recognize.Hopefu lly, this program will demonstrate and clarify <strong>al</strong>l of those rulesconcerning ROT. Please run the program and watch the v<strong>al</strong>ues of ROT atthe bottom of the screen, and the effects they have on the figure. You willhave to press res<strong>et</strong> to end the program.1 REM DEMONSTRATE2 REM POKING TABLE3 REM4 REM POKE ADDRESS5 REM OF TABLE


6 POKE 232,: POKE 233 ,37 REM8 REM READ AND POKE9 FOR L = 768 TO 7891 READ V: POKE L ,V11 NEXT L12 REM13 REM INITIALIZE14 REM15 HOME : VTAB 2116 HGR17 HCOLOR = 318 INPUT "SCALE? "; S$: SCALE =VAL (S$)19 R = 2 REM21 REM BEGIN LOOP22 REM TO PLOT SHAPES23 REM24 FOR I = 1 TO 244 STEP 2625 ROT = R26 DRAW 2 AT I,127 REM PRINT ROT VALUES28 PRINT II " ;R;" " ;29 R = R + 2 : REM INCREMENT ROT VALUE3 NEXT I31 PRINT32 PRINT "ROTATION VALUES"33 INPUT "PRESS RETURN TOCONTINUE" ; R$: GOTO 1534 END35 REM36 REM DATA TABLE37 REM38 DATA 2,,6,,1,,12,28,48,,45,141,63,63,63,ll,15, 49,63,63,63,78,45,5,Listing 11-2.CHAPTER 10-SHAPING UP 109An Apple a Day ...The following section creates a shape from our perenni<strong>al</strong> <strong>apple</strong>, and demonstrateshow to place the point of origin anyplace you like in a shape.Only the fi rst sever<strong>al</strong> vectors are digitized, then Listing 1 lll-3 gives you theentire shape as a shape table in order to spare you the trouble. For (relative)simplicity, the shape is drawn by plotting every dot instead of every


110 APPLE II COMPUTER GRAPHICSsecond dot, so you would expect to <strong>al</strong>ways have a white <strong>apple</strong> (rememberthat consecutive dots display white!), but you will soon see differently.Figure 1©-7A gives the block diagram for the shape, and 1©-7B shows thebeginning of the vector diagram.A. r:::::m:::::· ::::: :::: :::: ::::: .:::: ::::: ::::: : :::: ::::: ::::: :::: ::::: ::::: ::::· :::::. :::: ::::: ::::: ::::: ::::=::::: :::: ::::=::::: ::::: ::::: ::::: :::: ::::: ::::::::::::::: .:::: ::::· ::::: ::::: :::::::::: :::: ::::· ::::: :::: :::::::::: .::::·:::: ::::· ::::: : :::: ::::· ::::: .:::: ::::: ::::: ::::: ::::: ::::: :::: ::::: ::::::::::.:::: ::::=::::: ::::: ::::: ::::: .:::: ::::· ::::: .:::: ::::: ::::: :::: ::::: »$$ $ $ $ 2 2 $ $ $ * 0* 0* 0*** * * $ $ $ $ $$ $$ $2 * $* $ 2$::::: .:::: ::::: ::::: ::::: ::::· :::::.:::: ::::· ::::: ::::: ::::: ::::: .:::: ::::: ::::::::::::::: :::: :::::::::: :::::::::: .:::: :=:: :::::.:::: :=::: :::::::::: :::::::::· ::::: ::::: :::: ::::: ::::: ::::: ::::: ::::: ::::: ::::: .:::: ::::·::::: ::::: ::::: ::::: ::::: ::::· ::::: ::::: :::::rl[B.rn0•o .. o•0• f--I--o .. o•010f-I-•0•o .. o•0•o .. o•0••Figure 19-7. Apple figures.The vectors plot one point within the <strong>apple</strong>, and then, without plotting,move to the top of the stem. A figure is <strong>al</strong>ways ROTated around its origin,and by placing the point of origin as we have, the <strong>apple</strong> will rotate aboutits center instead of the top. The only reason to plot the origin is to showits location, and it could just as well have been skipped. The fi rst twentyvectors are:PU SU SL SU SU SL SU SU SL SUSU SL SU SU SL SU SU SR PR PDFigure 1©-8 gives you the fi rst portion of the digitization.C B AC B AHEXDECIMALSL SU PUSL SU SUSL SU SUSL SU SUSL SU SUSR SU SU-- PD PR-- PD PR11 111 11 11 11 1 11 11 11 11C4cccc43535196192192192192645353Figure 19-8.


CHAPTER 10-SHAPING UP 111Sections A and B contain the code to move up (00 ) fo r the first six bytes,arid section C contains the code to move left (11) for the fi rst five, whil<strong>et</strong>he sixth causes a move to the right. It may seem pr<strong>et</strong>ty silly to move to theleft in number 5 and then immediately right in number 6 instead of movingstraight up in both of them. Remember, however, that if number 5 wereonly to move up, it would be <strong>al</strong>l zeros- 00 000 000-and would erroneouslysign<strong>al</strong> the end of the shape, so the sm<strong>al</strong>l deception is necessary. Theremainder of the shape is digitized in the same manner as the saucer, soinstead of belaboring the point, just enter Monitor and type the shape tablegiven in Listing 1©-3.300 : 01 00 04 00 C4 C0 C0 C0308: C0 40 35 35 35 35 2B 4D310 : 09 89 3F 3F FF FF 3B FF318 : 2A 2D 2D 2D 2D 2D 2D 2D320 : lE 3F 3F 3F 3F 3F 3F 3F328: 17 2D 2D 2D 2D 2D 2D 2D330: 3E 3F 3F 3F 3F 3F 3F 37338 : 2D 2D 2D 2D 2D 2D 2D 15340 : 3F 3F 3F 3F 3F 3F 3F 3F348 : 2E 2D 2D 2D 2D 2D 2D 2D350 : 2D 15 3F 3F 3F 3F 3F 3F358 : 3F 3F 3F 0E 2D 2D 2D 2D360 : 2D 2D 2D 2D 3E 3F 3F 3F368 : 3F 3F 3F 3F 37 2D 2D 2D370 : 2D 2D 2D 2D 35 3F 3F 3F378 : 3F 3F 3F 3F 2E 2D 2D 2D380 : 2D 2D F5 3B 3F 3F 3F 4E388 : 2D 2D 05 00Listing 1 Cll-3. Apple shape table.The fi rst byte declares that there is one shape in the table, and thesecond byte is unused. The third and fourth bytes give you the offs<strong>et</strong> forthe fi rst (and only) shape: 4 (remember lo-byte/hi-byte), and the fo llowingbyte is the beginning of the shape.Save the table (if you have a disk) by typingBSAVE APPLESHAPE,A$300,L$8CTo see what you have accomplished, typePOKE 232, 0 :POKE 233 ,3HGR:SCALE = l:ROT = 0DRAW 1 AT 100,100The first line tells the system where to find the table, the second is obvious,and the third draws the shape from that table on the Hi-Res screen. If youmanaged to g<strong>et</strong> <strong>al</strong>l of the numbers keyed in correctly, you will see a little


112 APPLE II COMPUTER GRAPHICSwhite <strong>apple</strong> in the center of your screen. If this is not so, enter Monitor andlist the memory range for the table by typing3f3f3.38Fand check your numbers against the ones in Listing 1 ©-3.If the <strong>apple</strong> did turn out correctly, l<strong>et</strong>'s label it using a second shap<strong>et</strong>able. From within Monitor, place the new table at memory location $4©©©by typing4f3f3f3 : f34 f3f3 f3A f3f3 21 f3f3 2E f3f34f3f38 : 36 i3i3 24 24 24 24 24 f3C4f31f3 : f3C 2D 6D 15 36 36 3E 3F4f318 : 3F f36 49 f39 36 36 36 364f32f3 : f3f3 24 24 2C 2D 35 36 364f328 : 3F 3F 36 36 36 f3f3 24 244f33f3: 24 24 24 24 24 f3f3 24 244f338: 2C 2D 35 3E 3F 96 2D 2D4f34f3 : f3f3Listing 19-4. Apple text (courtesy of Dean Lewis].This table contains four shapes representing the l<strong>et</strong>ters A, P, L, and E. Sav<strong>et</strong>his table (again, only if you have a disk) by typingBSAVE APPLE TEXT, A$4f3f3f3, L$41We can use APPLE TEXT to simulate putting a little text on the Hi-Resscreen. There is a continuing debate about wh<strong>et</strong>her you can put true texton a <strong>graphics</strong> screen, other than that in the text window at the bottom.The Apple manu<strong>al</strong>s say absolutely not, and they ought to know-it's theirmachine. However, Bob Bishop says "yes," and he ought to know-he'sdone it. The technique is very complex, difficult to put into practice, andimpossible for the BASIC programmer, so it will not be covered in this text.We refer interested readers to the October 1982 issue of SOFT ALK magazine,pages 54 through 63.The BASIC programmer can do <strong>al</strong>most as well by designing shapes torepresent any or <strong>al</strong>l of the l<strong>et</strong>ters, as we have done with APPLE TEXT. Sol<strong>et</strong>'s put text on the Hi-Res screen by typingBLOAD APPLESHAPEBLOAD APPLE TEXTHGR : SCALE = 1: ROT f3POKE 232,f3:POKE 233 ,3DRAW 1 AT lf3f3, lf3f3POKE 232,f3:POKE 233 ,64DRAW 1 AT 115 , 12f3


CHAPTE R 10-SHAPING UP 113DRAW 2 AT 130 ,120DRAW 2 AT 140 ,120DRAW 3 AT 150 ,120DRAW 4 AT 156, 120The fourth line puts the address of the <strong>apple</strong> shape table in locations 232and 233, then the fifth line DRAWs from that table. Line number 6 thenplaces the address of the APPLE TEXT table in the proper locations so thatthe remaining commands DRAW from it. Location $4©©© is $©© and $4© inlo-byte/hi-byte fo rm, and $4© = 64.You can see how simple it is to put "text" on the Hi-Res screen, and <strong>al</strong>soto DRAW from two or more shape tables which are stored in memory atthe same time.Since things seem to be going so smoothly, l<strong>et</strong>' s make them a bit morecomplex. Key in and run the program in Listing 1©-5. To save time, wesuggest that you dispense with the 29 REM statements.10 REM BACKGROUND DEMO20 REM30 REM40 REM INITIALIZE50 PRINT CHR$(4) ; "BLOAD APPLESHAPE"60 SCALE = 170 POKE 232,0 : POKE 233 ,380 FOR I = 0 TO 7: READ CLR$(I) : NEXT I90 VTAB 24100 REM110 REM120 REM BEGIN LOOPS TO130 REM VARY BACKGROUND140 REM AND SHAPE COLORS150 REM160 FOR Cl = 1 TO 7170 FOR C2 = 0 TO 6180 IF Cl = C2 GOTO 210190 PRINT CLR$(C2) ;" OVER " ;CLR$ (C1)200 GOSUB 280210 NEXT C2,C1220 END230 REM240 DATA BLACK1, GREEN,VIOLET,WHITE1,BLACK2, 0RANGE ,BLUE , WHITE2250 REM260 REM DRAWING ROUTINE270 REM280 HGR290 FLAG 0 : REM FIRST PASS


114 APPLE II COMPUTER GRAPHICS300 REM310 REM DRAW BACKGROUND320 HCOLOR = Cl330 FOR Y = 10 TO 160340 HPLOT 10 , Y TO 180 ,Y350 NEXT Y360 REM370 REM DRAW SHAPE380 REM390 HCOLOR = C2400 R = 0 : REM ROTATION VALUE410 REM420 REM BEGIN LOOPS TO DRAW430 REM REPETITIONS OF SHAPE440 , REM450 FOR Y = _ 20 TO 100 STEP 80460 FOR X = 20 TO 220 STEP 50470 ROT = R: REM SET NEW ROTATION480 DRAW 1 AT X ,Y490 R = R + 2500 FOR I = 1 TO 200 : NEXT I: REM DELAY510 NEXT X , Y520 REM530 REM540 REM FLAG = 0 IF FIRST PASS550 REM FLAG = 1 IF SECOND PASS560 REM IF FIRST PASS , PLOT570 REM AGAIN IN BLACKl580 IF NOT FLAG THEN FLAG 1:HCOLOR = 0 : GOTO 400590 RETURNListing 11-5. Pandora's <strong>apple</strong>.The program is titled "Pandora's Apple" because it displays a wholeherd of unexpected and som<strong>et</strong>imes uns<strong>et</strong>tling results. The function of theprogram is to draw the <strong>apple</strong> shape in various colors and rotations (C/J, 2, 4,. .. , 18) superimposed on a background of a diffe rent color, and then"erase" the shape by drawing over it in black1 . The background does notcompl<strong>et</strong>ely fi ll the screen so that you may <strong>al</strong>so see the shape plotted onthe black1 background. The techniques used in the program are standardand we will not mess with them since the results are the important topic.Haw Do Yau Like Them Apples ?The program begins by painting a green field, and then DRAWing theshape over it in black1 -nothing unusu<strong>al</strong> so far. The colors used are


CHAPTER 10-SHAPING UP 115printed at the bottom of the screen so you can keep up with the action.The next pass DRAWs viol<strong>et</strong> on green, and around the border of the <strong>apple</strong>you should notice white spots (mold?) caused by the viol<strong>et</strong> dots of the<strong>apple</strong> plotting next to the green dots of the background. Remember, anytimeyou plot two adjacent dots, they display as white.When black2 is plotted over the green, you see the effect of clashing in<strong>al</strong>l its radiant glory. Any time that a group 2 dot is plotted within a group 1byte, the entire byte becomes group 2. Hence, some of the green changesto its group 2 counterpart, orange. As the program progresses you will seesever<strong>al</strong> more examples, both of white edges and of clashing (Oh goody!).When the program stops, change line 6(/J to read:6 SCALE = 2Run the program again and watch the fun! When it plots black1 over thegreen, it starts by only turning every other line black, and when it g<strong>et</strong>s tothe lower left corner of the green field, it only plots a few dots! The reasonfor this, and later shennanigans, is that at SCALE = 2, the <strong>apple</strong> is beingplotted on every second line only. By coincidence, it happens to be plottingon only the odd columns, and green does not exist on those lines.At SCALE = 1, the shape data causes the <strong>computer</strong> to plot across onerow of the <strong>apple</strong>, and then execute a plot-down to drop to the followingrow. At SCALE = 2, the machine executes every vector twice, includingthe PD vector, so it drops two lines b<strong>et</strong>ween plotting each row.When viol<strong>et</strong> <strong>apple</strong>s are plotted over the green field, the ones rotated 9(/Jdegrees (in the lower right corner of the green field) plot as white (Afarmer's nightmare!). The <strong>apple</strong> is plotting on the odd columns causing theviol<strong>et</strong> in those columns to be turned on while the field is made up of greenin the even columns. Since you then have both the odd and even columnson, the result is white! For a similar reason, plotting rotated white1 <strong>apple</strong>son a black background produces only viol<strong>et</strong> <strong>apple</strong>s since turning on onlythe odd columns produces viol<strong>et</strong> but no green.When the program tries to DRAW rotated green <strong>apple</strong>s on a group 1background, nothing is plotted, since the position of the <strong>apple</strong> causes it toattempt to plot green on odd columns, and green exists only on evencolumns. Plotting green <strong>apple</strong>s on a group 2 field will cause portions ofthat field to change to the corresponding group 1 color-another instanceof clashing. The program will run through <strong>al</strong>l the colors, and similarstrange combinations will occur. For fu n, you might try s<strong>et</strong>ting the sc<strong>al</strong>e tothree and watching what that does to the innocent <strong>apple</strong>.XDRA WYou saw in the last program that using DRAW to plot the shape overthe background in one color, and erasing it by using black, <strong>al</strong>ways left the


116 APPLE II COMPUTER GRAPHICScolored background mangled. You could erase it using the color of thefield, but that wou ld mess up the black section of the background. TheXDRAW command eliminates that problem. With Listing 1©-5 in memory,type171819 PRINT CLR$ (Cl)21 NEXT Cl3948 XDRAW 1 AT X ,YRun the modified program and notice the differences. Line 48© changesthe DRAW to XDRAW-, and the other statements eliminate C2 which representsthe color in which the <strong>apple</strong> was DRAWn. Where DRAW attempts toplot the <strong>apple</strong> in the specified color, XDRAW ignores HCOLOR and plotsthe shapes in colors which depend entirely on the background. XDRAWautomatic<strong>al</strong>ly plots the shape in the complement of the background color.Black and white are complements, as are blue and green, orange andviol<strong>et</strong>. XDRAWing on the green field will give you a blue <strong>apple</strong>, and anyportion which overlaps into the black will be white. The usefu l aspect ofXDRAW is that when it XDRAWs the figure the second time, it erasescleanly and compl<strong>et</strong>ely, regard less of the background. This attribute is veryhandy when animating-a subject we will cover in later chapters.RecappingWe have shown you how to design and digitize shapes, and at the sam<strong>et</strong>ime we have hopefu lly shown you the v<strong>al</strong>ue of a good Hi-Res editor. Ifyou are planning to do any serious work in Hi-Res, an editor is a necessarytool. We have <strong>al</strong>so demonstrated how to use BSAVE or the Monitor "W"to save the table, and BLOAD or SHLOAD to recover it. You have <strong>al</strong>sobeen introduced to the commands used to plot, rotate, and sc<strong>al</strong>e theshape. The initi<strong>al</strong> work involved with shapes is wearisome and frustrating,but when the shape is done it is a simple y<strong>et</strong> powerfu l element of yourprogramming arsen<strong>al</strong>.VocabularyDigitizeDRAWOffs<strong>et</strong>Point of OriginROTSCALEShapeShape TableSH LOADVectorVector DiagramXDRAW


CHAPTER 10-SHAPING UP 117Exercises1. Write a BASIC program to lower HIMEM and then load the shap<strong>et</strong>able which contains the saucer in the space created above HIMEM.2. Extend the program in Exercise 1 to DRAW the saucer at every evenpoint b<strong>et</strong>ween (6, 1 ([)([)) and (2©©, 1©©).3. Repeat Exercise 2, but use XDRAW instead of DRAW.4. At every point b<strong>et</strong>ween (6, 1©©) and (2©©, 1©©) XDRAW the saucertwice in succession, and then move to the next point and XDRAWtwice again, and so on.


11Graphs and ChartsObjectivesAfter reading Chapter 11 you should be able to:• Have your <strong>computer</strong> draw a bar, line, or circle graph using our programsand your data.• Use the <strong>computer</strong> to graph your own equations.• Label your graphs.Processing data, or "number crunching" as it is som<strong>et</strong>imes c<strong>al</strong>led, haslong been one of the strengths of the <strong>computer</strong>. In earlier machines, theresults of the processing were <strong>al</strong>l too often only pages and pages of numbers.Long columns of numbers often obscure the meaning those samefigures are supposed to convey, so displaying data in graphic form is animportant tool. Bar charts, line graphs, and circle graphs are the three mostcommonly used display m<strong>et</strong>hods, and we will give at least one example ofeach in this chapter. In three of the fo ur examples, the data to be graphedis contained in BASIC DATA statements so that you need only replace thesample data with yours, and the program will graph your data. The otherexample graphs a mathematic<strong>al</strong> equation over a specified range. Modifyingthat program will require you to change the fo rmula and the sc<strong>al</strong>ingfactors. We will t<strong>al</strong>k more about each example when the time comes.Belly Up to the BarListing 11- 1 creates a bar graph-with the bars running either vertic<strong>al</strong>ly orhorizont<strong>al</strong>ly. Key the program in and run it. As usu<strong>al</strong>, you may omit any or<strong>al</strong> I of the REM statements.119


120 APPLE II COMPUTER GRAPHICS1 REM BAR CHART DEMO2 REM3 REM ******************4 REM * INITIALIZATION *5 REM ******************6 REM7 REM DETERMINE VERT/HORIZ CHART8 REM9 TEXT : HOME1 PRINT "PRESS FOR A VERTICAL CHART"11 PRINT "PRESS 1 FOR A HORIZONTAL CHART"12 GET H$13 IF H$ < > "1" AND H$ < > "W' GOTO 914 H% = VAL (H$)15 REM16 REM READ NUMB BAR, WIDTH BAR,17 REM AND WIDTH SPACE18 REM19 READ NB% ,WB%, WS%2 REM21 REM CHECK TOTAL WIDTH22 REM23 IF NB% * (WB% + WS%) < 4 GOTO 3224 PRINT CHR$ (7) ; "CHART TOO WIDE"25 PRINT "CONTINUE? (Y/N) ": GET R$26 IF R$ = "N" GOTO 6727 IF R$ = "Y" GOTO 3228 GOTO 2429 REM3 REM READ DATA & FIND MAXIMUM31 REM32 DIM BAR(NB%) , LBL$(NB%)33 FOR I = 1 TO NB%34 READ BAR(I),LBL$(I)35 IF MAX < BAR(I) THEN MAX BAR(I)36 NEXT I37 REM38 REM ESTABLISH SCALE39 REM4 SCL = 39 I MAX41 REM42 REM **************43 REM * DRAW CHART *44 REM **************45 REM46 GR470 FOR I = 1 TO NB%


CHAPTER 11-GRAPHS AND CHARTS 12 1480 REM490 REM SET NEW COLOR500 REM510 C% = INT (15 * RND (1) + 1)520 IF C% = 5 OR CO% = C% GOTO 510530 COLOR = C.%540 CO% = C%550 REM560 REM PLOT A BAR AND PRINT LABEL570 REM580 HTAB ( XS + 1) : VTAB (21)590 PRINT LEFT$ (LBL$(I),WB%) ;600 FOR X = XS TO XS + WB%-1610 IF X > 39 THEN PRINT CHR$ (7) ;· "CHART TOO WIDE" : GOTO 670620 IF H% THEN HLIN 0 , BAR(I) * SCL AT X630 IF NOT (H%) THEN VLIN 39, 39-BAR(I) * SCL AT X640 NEXT X650 XS = X + WS%660 NEXT I670 END0·0 REM705 REM **************710 REM * DATA TABLE *715 REM **************720 REM730 DATA 4,5,2740 DATA 100' I 80750 DATA 80 ,'81760 DATA 50 , '82770 DATA 40 , '83Listing 11-1. Bar graphs.This routfne draws either horizont<strong>al</strong> or vertic<strong>al</strong> bars. Select the vertic<strong>al</strong>bar graph option when the <strong>computer</strong> screen displays:PRESS 0 FOR A VERTICAL CHARTPRESS 1 FOR A HORIZONTAL CHARTYou will see the bar chart appear with labels underneath to indicateyears. If you run the program again and select a horizont<strong>al</strong> graph, theyears sti ll appear at the bottom. Why? This happens because it is notpossible to put text labels in the body of the Low-Res screen. As you willsee in Listing 11-3, labels are often unique to each graph, and many timesthey have to be added after the graph is done, and in a less than elegantmanner.


122 APPLE II COMPUTER GRAPHICSL<strong>et</strong>'s have more fu n with this program before we continue. Change th<strong>et</strong>hird data v<strong>al</strong>ue in line 73


CHAPTER 11-GRAPHS AND CHARTS 123These lines select a random color for each bar. Line 520 prevents thenew color (C%) from being the same as the previous color (CO%) or colornumber 5. Color number 5 is excluded because there are two greys inLow-Res (colors 5 and 10).580 HTAB ( XS + 1) : VTAB (21)590 PRINT LEFT$ (LBL$(I), WB%) ;These position the cursor at the correct place and print as much of thelabel as will fit in the width of the bar.600 FOR X = XS TO XS + WB%-1610 IF X > 39 THEN PRINT CHR$ (7) ; "CHART TOO WIDE" :GOTO 670620 IF H% THEN HLIN 0 , BAR(I) * SCL AT X630 IF NOT (H%) THEN VLIN 39,39-BAR(I) * SCL AT X64 NEXT XThis loop draws the bar. If you selected a horizont<strong>al</strong> graph, line 620 willexecute; line 630 executes if you chose vertic<strong>al</strong>. The length of each bar isfound by multiplying the data v<strong>al</strong>ue being graphed by the sc<strong>al</strong>ing factorc<strong>al</strong>culated earlier. You may have wondered why the bar is drawn from 39to 39 minus its length, instead of being drawn from zero. It must becontinu<strong>al</strong>ly taken into account that 39 is the bottom of the screen and 0 isthe top, where most graphs use 0 at the bottom.650 XS = X + WS%XS represents the start of the current bar. After the bar has been drawn,this line moves the starting location to that of the next bar by adding the·bar width.A very similar routine could be written to plot the bar graph on the Hi­Res screen instead of on the Low-Res display. The limits would be changedfrom 39 to 159 or 279, depending on wh<strong>et</strong>her you plan to draw vertic<strong>al</strong>lyor horizont<strong>al</strong>ly, and you would be using the HPLOT TO command insteadof HLIN or VLIN.'This Is Where You Draw the LineSom<strong>et</strong>imes data is b<strong>et</strong>ter represented as a line ·graph. Listing 11-3 drawssuch a graph in Hi-Res and uses a shape table to put labels on the <strong>graphics</strong>screen. Enter and save the shape table by typing the following from BASIC:CALL -151300 : 0B 00 18 00 20 0 2B 00308: 36 00 41 00 4E 00 5D 00310 : 66 00 75 00 81 00 90 003 18 : 29 2D D8 24 24 JC 32 00320 : 29 2D 25 DB 0C 0C 0C 24


124 APPLE II COMPUTER GRAPHICSJ28 : JB JF 8 15 2D C 24JJ : 2J 44 JF JF 7 49 21JJ8 : 24 24 JC 9B J6 2E 2D DJ4 : 8 E 5 2D 2 lC JFJ48 : lC 24 2D 2D J 8 24J5: 24 C 2D J5 92 J6 JB 27J58: 18 8 2D 2D 21 c cJ6 : c c JC JF JF 9 2DJ68 : c 24 JB JF 2 c 2D 15J7 : J6 DB lJ J6 9 2D CJ78 : 24 JB JF 2 C 2D 15 J6J8 : 1 29 2D 2 24 24 JBJ88: JF J2 26 2E 28 28 28 J9 : 24 24 JDGBSAVE NUMBER TABLE, A$J,L$9JListing 11-2. Number table.This number table contains eleven shapes; fi rst come the digits 1 through9, then zero, and fin<strong>al</strong>ly a critter resembling a hyphen which will be usedto mark each axis of the graph.Now type in and run Listing 11-3.1 REM LINE CHART DEMO2 REMJ REM ******************4 REM * INITIALIZATION *5 REM ******************6 REM7 TEXT : HOME8 VTAB (21)9 PRINT CHR$ (4) ; "BLOAD NUMBER TABLE,A$JW'1 POKE 2J2,: POKE 2JJ,J11 SCALE = 112 REMlJ REM READ NUMBER OF POINTS14 REM AND SPACING15 REM16 READ NP%,SP%17 REM18 REM CHECK TOTAL WIDTH19 REM2 IF (NP%-1) * SP% < 28 GOTO J21 PRINT CHR$ (?) ; "CHART TOO WIDE"


22<strong>ii</strong>' PRINT "CONTINUE? (Y/N) ": GET R$23<strong>ii</strong>' IF R$ = "N" GOTO 86<strong>ii</strong>l24<strong>ii</strong>l IF R$ = "Y" GOTO J<strong>ii</strong>l<strong>ii</strong>l25<strong>ii</strong>l GOTO 21<strong>ii</strong>l26<strong>ii</strong>l REM27<strong>ii</strong>l REM READ DATA POINTS28<strong>ii</strong>l REM AND FIND MAXIMUM29<strong>ii</strong>l REMJ<strong>ii</strong>l<strong>ii</strong>l DIM DP(NP%-1)Jl<strong>ii</strong>l FOR I = <strong>ii</strong>' TO NP%-:132<strong>ii</strong>' READ DP(I)33<strong>ii</strong>' IF MAX < DP (I) THEN MAX DP(I)J4<strong>ii</strong>l NEXT IJ5<strong>ii</strong>l REMJ6<strong>ii</strong>l REM ESTABLISH SCALE37<strong>ii</strong>' REM38<strong>ii</strong>' SCL = 15<strong>ii</strong>l I MAXJ9<strong>ii</strong>l REM4<strong>ii</strong>l<strong>ii</strong>l REM DRAW FRAME41<strong>ii</strong>l REM42<strong>ii</strong>l HGR4J<strong>ii</strong>l HCOLOR = 644<strong>ii</strong>l HPLOT 24,<strong>ii</strong>l TO 24, 159 TO 278,159 TO278,<strong>ii</strong>l TO 24,<strong>ii</strong>l45<strong>ii</strong>l REM46<strong>ii</strong>l REM ******************47<strong>ii</strong>l REM * CUSTOM SCALING *48<strong>ii</strong>l REM ******************49<strong>ii</strong>l REM5<strong>ii</strong>l<strong>ii</strong>l REM SCALE VERTICAL AXIS51<strong>ii</strong>l REM52<strong>ii</strong>l HCOLOR = 35J<strong>ii</strong>l FOR I = 2<strong>ii</strong>' TO MAX STEP 2<strong>ii</strong>l54<strong>ii</strong>l ROT = 16 : DRAW 11 AT 24, 16<strong>ii</strong>l-I * SCL55<strong>ii</strong>l ROT = <strong>ii</strong>'56<strong>ii</strong>l N$ = STR$ (I) :NL = LEN (N$)57<strong>ii</strong>l FOR J = 1 TO NL58<strong>ii</strong>l DIGIT = VAL ( MID$ (N$,J,1) ):IF DIGIT = <strong>ii</strong>' THEN DIGIT = l<strong>ii</strong>l59<strong>ii</strong>l DRAW DIGIT AT (J-1) * 7,16<strong>ii</strong>l-I * SCL6<strong>ii</strong>'<strong>ii</strong>' NEXT J61<strong>ii</strong>l NEXT I62<strong>ii</strong>l REM6J<strong>ii</strong>l REM SCALE HORIZONTAL AXIS64<strong>ii</strong>l REM65<strong>ii</strong>l ROT = <strong>ii</strong>'661i' FOR I = 1 TO NP%-1CHAPTER 11-GRAPHS AND CHARTS 125


126 APPLE II COMPUTER GRAPHICS670 X = 24 + I * SP%680 IF X < 280 THEN DRAW 11 AT X , 159690 NEXT I700 VTAB (21)710 PRINT " J F M A M J J A S 0 N D J F MA E A P A U U U E C 0 E A E AN B R R Y N L G P T V C N B R";720 VTAB (20)730 REM740 REM ********************750 REM * PLOT DATA POINTS *760 REM ********************770 REM780 HCOLOR = 7790 HPLOT 24, 160-DP(0) * SCL800 FOR I = 1 TO NP%-1810 X = 24 + I * SP%820 Y = DP (I) * SCL830 IF X > 279 THEN PRINT CHR$ ( 7) ;"CHART TOO WIDE" : GOTO 860 : REM (END )840 HPLOT TO X ,160-Y850 NEXT I860 END900 REM910 REM **************920 REM * DATA TABLE *930 REM **************940 REM950 DATA 15 , 18960 DATA 100,130 ,90,80 ,100,210,170 ,60,120,30 ,50,40,100, 80, 90Listing 11-3. Line graph.You may change the data plotted by changing the v<strong>al</strong>ues given in line960. The numbers on the vertic<strong>al</strong> axis will change accordingly, but will stillincrement by 20. The increment for the vertic<strong>al</strong> sc<strong>al</strong>e may be adju sted inline 520, but we will t<strong>al</strong>k more about that soon. The fi rst v<strong>al</strong>ue in line 950gives the number of data points to be plotted, and the second item indicatesthe horizont<strong>al</strong> spacing b<strong>et</strong>ween plots. Both those numbers may be<strong>al</strong>tered and the program will behave properly, except for the horizont<strong>al</strong>labels which remain fixed.After you have played for a while, l<strong>et</strong>'s look at some of the more interestingparts of this program.90 PRINT CHR$ ( 4) ; "BLOAD NUMBER TABLE, A$30W'100 POKE 232,0: POKE 233 ,3110 SCALE = 1


CHAPTER 11-GRAPHS AND CHARTS 127These lines load the shape table from disk, and tell the system where it was·put. (See Chapter 10.)300 DIM DP (NP%-1)310 FOR I = 0 TO NP%-1320 READ DP(I)330 IF MAX < DP (I) THEN MAX340 NEXT I380 SCL = 150 I MAXDP(I)These lines read the data points into an array c<strong>al</strong>led DP and establish themaximum and the sc<strong>al</strong>ing factor. Thanks to line 380 the maximum point isplotted at Y-coordinate 150.400 REM DRAW FRAME420 HGR430 HCOLOR = 6440 HPLOT 24,0 TO 24, 159 TO 278,159 TO 278,0 TO 24,0Here a blue frame is drawn for the graph, and space is provided on the leftside for the labels.500 REM SCALE VERTICAL AXIS520 HCOLOR = 3530 FOR I = 20 TO MAX STEP 20540 ROT = 16 : DRAW 11 AT 24, 160-I * SCL550 ROT = 0560 N$ = STR$ (I) :NL = LEN (N$)570 FOR J 1 TO NL580 DIGIT = VAL ( MID$ (N$,J,1 )): IFDIGIT = 0 THEN DIGIT = 10590 DRAW DIGIT AT (J-1) * 7,160-I * SCL600 NEXT J610 NEXT IHere we . sc<strong>al</strong>e the vertic<strong>al</strong> by 20. If you wished to use a diffe rent sc<strong>al</strong>e,such as 10, you would change both occurrences of 20 in line 530 to 10.Line 540 rotates our little tic-mark (shape 11) and draws it across the axis.Line 550 r<strong>et</strong>urns the rotation to 0 and the section from lines 560 through600 proceeds to take each digit of the label (I) and draws it on the Hi-Resscreen by means of the shape table.630 REM SCALE HORIZONTAL AXIS650 ROT = 0660 FOR I = 1 TO NP% - 1670 X = 24 + I * SP%680 IF X < 280 THEN DRAW 11 AT X , 159690 NEXT I7(l VTAB (21)


128 APPLE II COMPUTER GRAPHICS71¥l PRINT "J F M A M J J A S 0 N D J F MA E A P A U U U E C 0 E A E AN B R R Y N L G P T V C N B R";72¥l VTAB (2¥l)This section labels the horizont<strong>al</strong> axis by a m<strong>et</strong>hod which makes up insimplicity what it lacks in style. Lines 650-69(/) draw the little tic-mark onthe axis, and line 71(/) prints the labels in the four lines of text at the bottomof the Hi-Res page. Line 71 (/) was actu<strong>al</strong>ly designed after the graph hadbeen plotted by using the "Escape Mode" editing features of the Apple. Ifyou use HGR2 or leave Hi-Res space below the graph, you could label thehorizont<strong>al</strong> axis in the same way we labeled the vertic<strong>al</strong>.75¥l REM * PLOT DATA POINTS *78¥l HCOLOR = 779¥l HPLOT 24, 16¥l-DP(¥l) * SCL8¥l¥l FOR I = 1 TO NP%-181¥l X = 24 + I * SP%82¥l Y = DP (I) * SCL83¥l IF X > 279 THEN PRINT CHR$ (7) ;"CHART TOO WIDE" : GOTO 86¥l : REM (END )84¥l HPLOT TO X ,16¥l-Y85¥l NEXT IThe heart of the routine is in this short section. Line 79(/) plots the fi rst pointto establish an initi<strong>al</strong> point for the line, then line 84(/) plots the rest of thepoints. The X-coordinate is the left side of the frame (24) plus the offs<strong>et</strong> foreach point. DP(4) will be plotted at 24 + 4 * 18, or 96.The Y-coord inate is c<strong>al</strong>culated by multiplying the data v<strong>al</strong>ue by thesc<strong>al</strong>ing factor. It should seem curious that the Y-coordinate is not plotteddirectly, but is subtracted from 160. Remember that the Apple thinks (/) is atthe top of the screen and 16(/) at the bottom, so the quick little subtractionturns things around to put (/) on the bottom where you usu<strong>al</strong>ly want it.The 15 data points are stored in DP((/)) through DP(14). This is why theFOR/NEXT loop established in line 8(/)(/) only goes to one less than thenumber of points (NP%-1).Function<strong>al</strong> PlottingMany times a graph may be drawn from a function or formula instead ofdata points. Listing 11-4 is a short example which graphs a couple offormu las. Try it out!1¥l REM FUNCTION DEMO2¥l REM3¥l DEF FN A(X) .¥l2 * x /\ 2 + 54¥l DEF FN B(X) . ¥l¥ll * x /\ 3


CHAPTER 11-GRAPHS AND CHARTS 12951il ex = 141iJ :CY = SliJ6(il REM7(il REM DRAW AXES8(il REM9(il HGR : HCOLOR = 3l(il(il HPLOT CX-75' CY TO ex + 75' CYll(il HPLOT CX,CY-75 TO CX , CY + 7512(il REMlJ(il REM PLOT GRAPH14(il REM15(il FOR X = -4(il TO 4(il16(il Y = FN A(X)17(il HPLOT ex + X,CY-Y18(il Y = FN B(X)19(il HPLOT ex + X ,CY-Y2(il(il NEXT XListing 11-4. Formula plotting.When the program runs, it draws a s<strong>et</strong> of axes with the center at thepoint (140,80) as s<strong>et</strong> in line 50. Then it draws the functions defined in lines30 and 40. The Apple thinks that (0,0) is in the upper left corner of thescreen, but we make it appear to be at (CX,CY) by adding those v<strong>al</strong>uesbefore plotting the point. As in the other line graph example, we mustSUBTRACT instead of adding the Y v<strong>al</strong>ue from CY since Apple believesthat Y increases from top to bottom, and we require Y to increase frombottom to top. Essenti<strong>al</strong>ly, we flip the graph over by using -Y in place of Y.The decim<strong>al</strong>s in the fu nction defi nition are sc<strong>al</strong>ing factors used to keepthe otherwise large numbers (40 to the third power is 64,000) within range.Convenient labels may be added to this as in the previous example.Graphing in CirclesCircle graphs, or pie charts, are a very popular form of presentation, butwe do not know anyone who enjoys the tedious c<strong>al</strong>culations required todraw them. Key in and run Listing 11-5.l(il REM PIE CHART DEMO2(il REM31i) REM ******************4(il REM * INITIALIZATION *5(il REM ******************6(il REM7(il DEF FN X (T)8(il DEF FN Y(T)9(il REM8(il * COS (T)7(il * SIN (T)


130 APPLE II COMPUTER GRAPHICS100 REM READ DATA110 REM120 READ NUM%130 DIM DTA( NUM%)140 FOR I = 1 TO NUM%150 READ DTA(I)160 TTL = TTL + DTA(I)170 NEXT I180 REM190 REM ************200 REM * DRAW PIE *210 REM ************220 REM230 HGR : HCOLOR = 3240 FOR T = 0 TO L57 STEP .01250 X = FN X (T) :Y = FN Y(T)260 HPLOT X + 140 , Y + 80270 HPLOT -X + 140, Y + 80280 HPLOT X + 140 , -Y + 80290 HPLOT -X + 140 ,-Y + 80300 NEXT T310 REM320 REM ***********330 REM * CUT PIE *340 REM ***********350 REM360 FOR I = 1 TO NUM%370 ACC = ACC + DTA(I)380 R = (ACC I TTL) * 6.28390 HPLOT 140,80 TO FN X (R) + 140,FN Y(R) + 80400 NEXT I410 END500 REM510 REM **************520 REM * DATA TABLE *530 REM **************540 REM550 DATA 5560 DATA 231.4,287,310.84,340, 390Listing 11-5. Pie charts.Line 550 gives the number of regions to be graphed, and line 560 liststhe actu<strong>al</strong> data used for each region. The regions are plotted clockwisefrom the right side of the circle. There are no re<strong>al</strong> restrictions on thev<strong>al</strong>ues, <strong>al</strong>though attempting to plot a region for a negative v<strong>al</strong>ue will giveyou a rather meaningless result.


CHAPTER 11-GRAPHS AND CHARTS 13 1After you have run the program a few times with your own data, we cango through it and hit the highlights.7¥' DEF FN X (T) =8Y, DEF FN Y(T) =8Y, * COS (T)7Y, * SIN (T)The fu nctions defi ned here are used to c<strong>al</strong>culate the X and Y-coordinatesof points on the circle. The v<strong>al</strong>ues 80 and 70 define the radius of the circle(two rad<strong>ii</strong>?!?). The dots on the screen are slightly higher than they are wide,so if you l<strong>et</strong> both numbers be 80 (as theor<strong>et</strong>ic<strong>al</strong>ly they should), the pielooks more like an egg.12¥' READ NUM%13¥' DIM DTA( NUM%)14Y, FOR . I = 1 TO NUM%15¥' READ DTA(I)16Y, TTL = TTL + DTA( I)17¥' NEXT INUM% is the number of regions, and the data v<strong>al</strong>ues are read into the arrayDTA. Since a tot<strong>al</strong> is required to c<strong>al</strong>culate the relative size of each "slice,"we accumulate the data v<strong>al</strong>ues in TTL.24¥' FOR T = Y, TO 1. 57 STEP • Y,125¥' X = FN X(T) :Y = FN Y(T)26Y, HPLOT X + 14Y,, Y + ' 8y,27¥' HPLOT -X + 14Y,, Y + 8Y,28¥' HPLOT X + 14¥', -Y + 8Y,29¥' HPLOT -X + 14Y,, -Y + 8Y,3¥'¥' NEXT TThe circle is drawn using a little simple trigonom<strong>et</strong>ry. (There is re<strong>al</strong>ly nosuch thing as simple trigonom<strong>et</strong>ry!) You will notice here (and <strong>al</strong>so later inthe program) that we place the center at (140,80) by adding 140 to each Xand 80 to each Y as we plot.The re<strong>al</strong> question is, why does T go from 0 to 1.57? The SIN and COSfunctions we use measure a circle in radians instead of degrees. There areroughly 6.28 radians in a fu ll circle (3.14 in a semicircle), and we use thesymm<strong>et</strong>ry of the circle so that we only need to c<strong>al</strong>culate the points for onequarterof the circle. The result of <strong>al</strong>l this is that we go one-quarter of theway to 6.28, and one-quarter of 6.28 is the elusive 1.57.36Y, FOR I = 1 TO NUM%37¥' ACC = ACC + DTA(I)38¥' R = (ACC / TTL) * 6.2839¥' HPLOT 14Y,, 8Y, TO FN X (R) + 14Y,,FN Y(R) + 8¥'4Y,Y, NEXT I41Y, END


132 APPLE II COMPUTER GRAPHICSAs we draw the wedges of the pie, our progress around the circle isproportion<strong>al</strong> to the amount of the data graphed so fa r. That is, if we areone-third of the way around the circle, then the accumulated amount ofdata <strong>al</strong>ready graphed is one-third of the tot<strong>al</strong> to be graphed. So, as we goaround, line 37© accumulates the graphed data in ACC, and line 38©c<strong>al</strong>culates the fraction of a full rotation. (Remember, in radian measure6.28 represents a full rotation.)Line 39© draws the line from the center of the circle (14©,8©) to the pointon the circle at the correct rotation.Round It OutYou have seen sever<strong>al</strong> fairly powerfu l schemes fo r graphing. They <strong>al</strong>l producea reasonable result on the screen, but what about g<strong>et</strong>ting a printoutof the graph? As with most any programming problem, where there's a willthere's a way, but the most practic<strong>al</strong> solution lies in pri nter hardware.There are sever<strong>al</strong> items presently on the mark<strong>et</strong> which <strong>al</strong>low you to"dump" the Hi-Res screen to a matrix printer, such as "GRAFTRAX*" forEpson printers, or the "GRAPPLER**" interface which works for most dotmatrixmachines. If you have, or acquire such an item, it comes withinstructions for dumping the Hi-Res screen.The programs presented here are designed to l<strong>et</strong> you plot your own datawith a minimum of modifications. After yeiu have used these routines for atime they will become clearer to you, and improvements will begin tosuggest themselves. Down that path lies hours of both creativity and frustration.Have fu n!VocabularyBar chartCircle graphLine graphPie chart*Graftrax is a trademark of Epson of America.**Gr<strong>apple</strong>r is a trademark of Microtech, Inc.


CHAPTER 11-,GRAPHS AND CHARTS 133Exercises1. Use Listing 11- 1 to do a bar graph showing a profit of 7,25© inJanuary, 4, 173 in February, 5,010 in March, 6,379 in April, 5,703 in·May, and 6,533 in June.2. Use the number table (Listing 11-2) and Listing 11-3 to graph thefollowing v<strong>al</strong>ues: 17.9, 26.4, 25.8, 32, 20.5, 10.9, 8.05, 6.1, 9.0, 12.7,and 16.3. Use the number table (Listing 11-2) and Listing 11-4 to graph thefunction Y. = X3-8X2-17X + 10. Label each axis.4. Use the number table to create a pie chart with the fo llowing data:150, 470, 173, 217, 301 , 522. Label the resulting graph.


12Byte-Move ShapesObjectivesAfter reading Chapter 12 you should be able to:• Use byte-move to perform a stationary and a vertic<strong>al</strong> animation.• Draw and digitize the seven horizont<strong>al</strong> separations of a byte-movefigure.• Animate a simple byte-move figure across the screen.The standard Apple shape is a fairly usefu l construct, particularly for theBASIC programmer because it l<strong>et</strong>s the user use a number of fast machinelevelroutines via the APPLESOFT shape commands (DRAW, XDRAW,SCALE, and others). Unfo rtunately, those routines are not fast enough fo rarcade type animation, so byte-move <strong>graphics</strong> are often used to gain extraspeed. To be honest, profession<strong>al</strong> byte-move animation is written in machinelanguage because BASIC is too slow to gain much advantage,whereas "look-up tables" are som<strong>et</strong>hing that a machine-level programhandles very well. These examples in BASIC serve to give you the "basic"idea behind byte-move, so if you are so inclined and endowed you canwrite these strategies into your machine-level program.It is important to re<strong>al</strong>ize how byte-move shapes differ from standardshapes. As you saw in Chapter 1 ([), a shape is defi ned by a series ofinstructions (vectors). For example, plot-up, skip-left, skip-left, plot-down,and so on. Each time the shape is plotted, the drawing routines followthose directions and reconstruct the shape. Since shapes can easily run toa hundred instructions or more, the vector m<strong>et</strong>hod can lead to a timedelay as each · vector is reprocessed and reinterpr<strong>et</strong>ed.135


136 APPLE II COMPUTER GRAPHICSA byte-move shape can be thought of as being pre-defi ned and predrawnsomeplace in the <strong>computer</strong>' s memor.. Plotting a byte-move shapeis a matter of moving the pre-drawn figure into the screen memory area,just as you might use pre-printed rub-on transfe rs for l<strong>et</strong>tering. In this way,a byte-move shape saves time since it does not have to be regeneratedeach time it is used, but has only to be moved from another locationprefab<strong>graphics</strong>.We are g<strong>et</strong>ting ahead of ourselves. Listing 12-1 gives a short example ofanimation using the byte-move process. Type it in and try it. You may omitthe REM statements if you wish.10 REM INITIALIZE Y20 REM COORDINATES30 REM40 Y1% = 1:Y2% = 2 :Y3% = 3 :Y4%Y5% = 5 : Y6% = 6: Y7% = 750 REM60 REM READ DATA FOR FIGURE70 REM ,80 FOR I = 1 TO 4: REM 4 FRAMES90 FOR J = 1 TO 7: REM 7 BYTES PERFRAME100 READ V%(I,J)110 NEXT J,I120 REM130 REM INITIALIZE ADDRESSES140 REM OF Y COORDINATES150 REM160 Y%(1)Y%(4)Y%(7)170 HGR180 REM8192 :Y%(2) = 9216 :Y%(3) = 10240 :11264 :Y%(5) = 12288 :Y%(6) = 13312 :14336190 REM POKE THE FOUR FRAMES200 REM210 FOR I = 1 TO 4220 POKE Y% (Y1%) , V% (I,1) :POKE Y% (Y7%) , V% (I, 7)230 POKE Y%(Y2%) , V% (I,2) :POKE Y%(Y6%) ,V%(I,6)240 POKE Y%(Y3%),V%(I,3) :POKE Y%(Y5%) , V% (I,5) :POKE Y%(Y4%) , V% (I,4)250 NEXT260 GOTO 210 : REM START AGAIN270 REM280 REM DATA FOR THE FOUR FRAMES4:


CHAPTER 12-BYTE-MOVE SHAPES 13729JJ REM3JJJJ DATA 1,2,4,8,16,32,6431JJ DATA 8,8,8, 8,8,8,832JJ DATA 64 ,32,16,8,4,2,133JJ DATA JJ , JJ , JJ ,127, JJ , JJ , JJListing 12-1.When the program is running correctly, you will see what could pass fo ran airplane propeller spinning in the corner of your screen. The effect isaccomplished by cycling through four different frames quickly enough tofool your eyes and brain into seeing a continuous motion-the POKE isquicker than the eye! The four frames are diagrammed in Figure 12-1,<strong>al</strong>ong with the bit patterns and decim<strong>al</strong> v<strong>al</strong>ues required to generate them.SCREEN BINARY . SCREEN BINARYPATTERN VALUE DECIMAL PATTERN VALUE DECIMAL--------------------------- ---------------------------X ------ JJJJJJJJ JJJJJJl 1---X --- JJJJJJJJ llJJJJJ 8-X ----- JJ000 0010 2---X --- 00JJ0 llJJJJJ 8--X ---- 0000 0100 4---X --- 0000 llJJJ0 8---X --- JJ000 1JJ00 8---X --- 0000 1000 8----X -- JJ001 0000 16---X --- 0000 1000 8-----X - 0010 0000 32---X --- 0000 10JJ0 8------X0100 0000 64---X --- 0000 1000 8FRAME #1 FRAME #2SCREEN BINARY SCREEN BINARYPATTERN VALUE DECIMAL PATTERN VALUE DECIMAL--------------------------- ---------------------------------X0100 0000 64------0JJJJ0 JJ0JJJJ 0-----X - 0010 000JJ 32------0JJJJ0 JJ0JJ0 0----X -- 00JJ1 0000 16------0JJ00 0000 0---X --- 0000 1000 8 xxxxxx 0111 1111 127--X ---- 000JJ 0100 4------0000 00JJJJ 0-X ----- JJ00JJ JJ010 2------0JJ00 JJ00JJ 0X ------ JJ00JJ JJJJ01 1------0JJ00 JJJJ0JJ 0FRAME #3 FRAME #4Figure 12-1.The v<strong>al</strong>ues fo r each frame are the ones used in the four DATA statementsin the program.When the program is run, there is a delay before anything seems tohappen. This delay is a characteristic of byte-move programs, and is


138 APPLE II COMPUTER GRAPHICScaused by the need to initi<strong>al</strong>ize a number of variables and arrays beforebeginning the animation. In Listing 12-1, line 4(/) s<strong>et</strong>s the seven Y coordinatesused in the figure, and line 16(/) assigns the corresponding memoryaddress to each of those coordinates. Lines 8(/) through 11(/) initi<strong>al</strong>ize anarray that contains seven data v<strong>al</strong>ues for each of the four versions of thepropeller.The actu<strong>al</strong> animating is done in lines 21(/) through 25(/) which POKE thev<strong>al</strong>ues for each frame into Hi-Res memory. The variables and arrays usedin the POKEs tend to obscure some of the mechanics of the program, bu'tthey <strong>al</strong>so enhance the execution, since it is much faster for the machine tolook up the v<strong>al</strong>ue of Y%(4) stored in memory than to generate the correspondingv<strong>al</strong>ue of 12288.In gener<strong>al</strong> it is faster to look som<strong>et</strong>hing up in a table than to generate iteach time it is needed...Line 22(/) POKEs the first and seventh bytes of the figure [V%(1, 1) andV%(1,7)], 23(/) the second and sixth, and 24(/) the third, fifth, and fourthbytes. POKEing the v<strong>al</strong>ues in that weird order improves the resulting effect,but you might try changing the order to POKE them sequenti<strong>al</strong>ly just to seewhat happens.Another reason fo r the variables is to make it easy to mod ify this programso that the figure will plot at different Y coordinates on the screen.The array Y% will contain the starting addresses for each of the 192 screenlines, and Yl % through Y7% will be the coordinates for the seven linesused in the shape.The propeller is an example of stationary animation. That is, animationthat takes place within a fixed region, and the different frames of thatregion are <strong>al</strong>ternated to simulate movement. There are many applicationsfor stationary animation. The APPLEVISION program on your DOS 3.3master is an excellent example, and so is a Hi-Res scoreboard for a game.(The ten diffe rent digits compose ten possible frames for display.)Drop the PropMost shapes used in a game are required to move about the screen. Wewill now <strong>al</strong>ter our program to <strong>al</strong>low for this. With Listing 12-1 still inmemory, type in the fo llowing changes.16 GOSUB 1 : REM CALC ADDRESSES245 Y1% Y1% + 1: Y2% Y2% + 1:Y3% Y3% + 1: Y4% Y4% + 1:Y5% Y5% + 1: Y6% Y6% + 1:Y7% Y7% + 1246 IF Y7% > 192 THEN END1 REM


CHAPTER 12-BYTE-MOVE SHAPES 139l!ill!il REM CALCULATE Yl!iJ2!il REM COORDINATESlYJJ!il REM1YJ4!il DIM Y%( 192)l!il5!il FOR I = 1 TO 185 STEP 8: READ SA%l!iJ6!il FOR J = !il TO 7:Y%(I + J) = SA% +J * lYJ241YJ7!il NEXT J,I1YJ8!il DATA 8192,8J2YJ, 8448,8576, 87YJ4,88J2,896YJ,9YJ881YJ9!il DATA 82J2,8J6YJ, 8488, 8616 , 8744,8872,9!il!iJYJ, 9128ll!il!il DATA 8272,84YJYJ,8528, 8656,8784, 8912 ,9YJ4YJ,9168lllYJ RETURNListing 12-2.Lines 1 ©©© th rough 111 fl) c<strong>al</strong> cu late the starting addresses for each of the192 screen lines by taking the base address of the box that the line lies inand adding the position address to it. That position address is c<strong>al</strong>culated asa multiple of 1©24. If that <strong>al</strong>gorithm escapes you, try reviewing the portionof Chapter 8 where we t<strong>al</strong>ked about c<strong>al</strong>culating addresses fo r Hi-Res memory.Line 245 is executed after each frame is displayed, and its purpose is toincrement each of the seven Y coordinates so that the prop will be plottedone line lower the next time.Run the mod ified program. You will see the propeller dropping downthe left edge of the screen as it spins. You will <strong>al</strong>so see a trail of garbageleft behind as the shape progresses. Most of each frame is wiped out whenthe next one plots over it, but since we are moving down b<strong>et</strong>ween frames,the top line of each frame remains on the screen to haunt you.When moving a shape using byte-move, it is often neccesary to makespeci<strong>al</strong> provisions for erasing the left-overs.We <strong>al</strong>l know that your first try never runs perfectly anyway, and theproblem is easily remedied by adding:215 POKE Y%(YYJ%) ,YJ244 YYJ% = Y1%Line 244 stores the Y-coordinate for the old top line as Y©%, and line 215POKEs a zero there to erase that byte.G<strong>et</strong>ting It AcrossSo far, you have performed a stationary and a vertic<strong>al</strong> animation using


140 APPLE II COMPUTER GRAPHICSbyte-move techniques. Now we take up the problem of horizont<strong>al</strong> animation,and a problem it turns out to be indeed, for it becomes about seventimes as complex as a vertic<strong>al</strong> animation. For that reason w' will confi neour byte-move examples to the animation of simple figures.Lucky SevenSeven seems to be the magic number for horizont<strong>al</strong> byte-move, and bymoving a single dot across the screen a little way, we can show you why.If you plan to turn on the dot in the extreme upper-left corner of the Hi­Res screen, you need to address you rself to the byte at 8192. The bytecontains seven bits, and you want to turn on only the left-most, so youneed the dot pattern:X------which corresponds to:! 1 or 1so you may turn the dot on by typing:HGRPOKE 8192,1Next, you want the second dot on, then the third, fourth, fifth, sixth, andseventh, in turn. Figure 12-2 s<strong>et</strong>s out the desired dot and bit patterns, andthen the corresponding v<strong>al</strong>ues.SCREEN BINARYPATTERN VALUE DECIMALX-------X...:----.:.-x-------X-------X-------X-------X 1 1 1 11 1 1 1248163264Figure 12-2.From BASIC type the fo llowing:HGRPOKE 8192,1POKE 8192,2POKE 8192,4POKE 8192,8POKE 8192,16


CHAPTER 12-BYTE-MOVE SHAPES 141POKE 8192,32POKE 8192,64The dot will move to the right, and the last statement will leave the dotat the right edge of byte number 8192. To continue the journey, the dotmust move to the first dot of byte number 8193, and then continue acrossthat byte. To accomplish that, you can type the same series of v<strong>al</strong>ues, butuse 8193 as the address:POKE 8193 ,1POKE 8193 ,2POKE 8193 ,4POKE 8193,8POKE 8193 ,16POKE 8193 ,32POKE 8193 ,64This pattern will repeat until you g<strong>et</strong> through address 8231, or you g<strong>et</strong>tired of it, whichever comes first.As you can see, each position across the byte requires a different v<strong>al</strong>ue,and there are seven positions across the byte, so seven becomes a veryimportant number. You can <strong>al</strong>so see that the last dot in every byte youtravelled across was left on. As we mentioned befo re, some arrangementmust be made to erase the last v<strong>al</strong>ue before moving on to the next byte.POKEing a zero as the fi n<strong>al</strong> v<strong>al</strong>ue in each series will erase that left-overdot.To move the dot across byte number 16 in that row (the row starts withbyte number (/)), you would fi nd the add ress by adding the offs<strong>et</strong>, 16, to thebase address, 8192 (did everybody g<strong>et</strong> 82(/)8?), and then POKE the series ofv<strong>al</strong>ues using that address.There is a nice relationship b<strong>et</strong>ween the X-coordinate of a point and theoffs<strong>et</strong> and v<strong>al</strong>ue needed to refer to it in byte-move. Consider the sevenv<strong>al</strong>ues used to move the dot across the byte as seven versions of the dot,and number them zero through six. If you wish to turn on a particular dotacross the row, you need to d<strong>et</strong>ermine which version of the dot to POKE,and the offs<strong>et</strong> from the beginning of the row.For example, l<strong>et</strong>' s turn on the dot in the fi rst row which has an X­coordinate of 75. Divide the coordinate by seven to fi nd the quotient (theresult of the division) and remainder. Seven goes into 75 ten times (rememberyour g'zintas?) with five remaining. The quotient (1(/J) is the offs<strong>et</strong>, andthe remainder (5) is the number of the separation (versions of the dot) tobe used. Therefore, the address is 82(/)2 (8192 + 1(/J), and the v<strong>al</strong>ue is 32.So,POKE 822,32and you will turn on the dot having 75 as its X-coord inate.


142 APPLE II COMPUTER GRAPHICSFor another example, turn on the dot with X-coordinate 1


CHAPTER 12-BYTE-MOVE SHAPES 1431BYTE #2VERSION # 02• • r • •• • •• •• • •• • • •• • • • •3456• • • • • • •• • • • • • •• • • • • • •• • • • • • •Figure 12-3.If you take the time to c<strong>al</strong>culate the v<strong>al</strong>ues needed to produce the sevenversions, you should arrive at:BYTE #1 BYTE #2DEC DOT DOT DECVALUE PATTERN PATTERN VALUE------------------------------ ---- -----126 -XXXXXX X------ 1124 --XXXXX XX----- 3121£l ---XXXX XXX---- 7112 ----XXX XXXX--- 1596 -----XX XXXXX-- 3164 ------X XXXXXX- 63Ill ------- xxxxxxx 127What you have in this example is a two-byte animation. Even though thefigure appears to be only one byte wide, in six of the seven possibleplacements, it requires two bytes to represent the line. You should considereach of the bytes in a figure to have its own s<strong>et</strong> of seven separations. ·It is another characteristic of byte-move that any figure requires sevenseparations for horizont<strong>al</strong> movement and, fu rther, that each figure takesone byte more than you would expect.The line which fits in one byte actu<strong>al</strong>ly requires a two-byte definition. Afour-byte figure will require five bytes for <strong>al</strong>l the separations. The onlyexception is a figure one dot wide, where <strong>al</strong>l seven versions will fit intoone byte.You can manu<strong>al</strong>ly move the line from BASIC by typingHGR


144 APPLE II COMPUTER GRAPHICSPOKE 8192,126 : POKE 8193,1POKE 8192,124 : POKE 8193 ,3POKE 8192,12 : POKE 8193,7POKE 8192,112 : POKE 8193 ,15POKE 8192,96: POKE 8193 ,31POKE 8192,64: POKE 8193 ,63POKE 8192,: POKE 8193 ,127Listing 12-4 gives you a program that will do essenti<strong>al</strong>ly what you did withthe series of POKEs.1 DIM A% (28) : REM 28 X-COORDINATES2 REM3 REM READ THE VALUES FOR4 REM THE 7 PAIRS OF FRAMES5 REM6 FOR I = TO 670 READ T%(I), H%(I)8 NEXT I9 REM100 HGR110 REM120 REM INITIALIZE THE TABLE130 REM OF ADDRESSES140 REM150 J = 160 FOR I 8192 TO 8231170 A% ( J) = I : J = J + 1180 NEXT19 REM20 REM PLOT THE LINE AT21 REM EACH X-COORDINATE22 REM230 FOR X = 1 TO 280240 Q% = INT ( X I 7)250 R% = X-(7 * Q%)260 C% = Q% + 1270 POKE A%(Q%) ,T%(R%) : POKEA%(C%) ,H%(R%)28 NEXT X29 END294 REM295 REM DATA TABLE296 REM30 DATA 126,1,124,3,120 ,7,112 ,15310 DATA 96,31,64,63,0 ,127Listing 12-4.


CHAPTER 12-BYTE-MOVE SHAPES 145Again, we use variables to make the program obscure. The v<strong>al</strong>ues for theline are read into arrays T% (for tail) and H% (head) in lines 31ll to Bill. ArrayA% contains· the addresses for each of the 41ll bytes across the top of thescreen, and is initi<strong>al</strong>ized in lines 120 through 180. The loop in lines 230 to280 plots the line at every X-coordinate across the screen, but lines 240,250, and 260 merit more study.Their purpose is to d<strong>et</strong>ermine which pair of bytes is being used, andwhich of the seven pairs of v<strong>al</strong>ues need to be POKEd. They do this byusing the quotient and remainder. For example, when the X-coordinate is42, seven g'zinta 42 six times, with zero left over. So you need to POKEthe sixth (and seventh) byte, with the zeroth pair of v<strong>al</strong>ues.When designing a byte-move shape, you will need to sk<strong>et</strong>ch and digitize<strong>al</strong>l seven separations of the figure as we did for the line. That data g<strong>et</strong>sstored in a table for use later in the program. As you can see, even assimple a figure as the line required seven different versions, and significantpreparation to animate horizont<strong>al</strong>ly, but the resultant animation is assmooth as you could wish fo r, even though it is a bit slow.VocabularyByte-moveQuotientRemainderSeparationExercises1. Design Hi-Res figures fo r the digits zero through nine, and write aBASIC program to place them into a region of the Hi-Res screen inascending order.2. Alter the program written for Exercise 1 to move each digit down onescreen line from the previous one.3. Draw and digitize the seven versions of the numer<strong>al</strong> "5" needed toanimate it horizont<strong>al</strong>ly.4. Write the code necessary to animate the numer<strong>al</strong> "5" across thescreen.


13Advanced MovesObjectivesAfter reading Chapter 13 you should be able to:• Use pre-c<strong>al</strong>culated tables to help enhance your animation.• Animate stationary figures using parti<strong>al</strong> modification.• Develop pre-shifts fo r shapes and use them for animation.We commend you for g<strong>et</strong>ting this far in your study of Apple II <strong>graphics</strong>.(No fair if you just skimmed to this point!) You have progressed from aknowledge of BASIC through the land of binary and hex numbers, onthrough the memory map maze, and to the re<strong>al</strong>m of Low-Res <strong>graphics</strong>.From there you progressed to the world of Hi-Res where you investigatedcolor, shapes, shape-table animation, and byte-move animation.You have seen most of the major schemes currently used to produce Hi­Res <strong>graphics</strong>, and we think you are ready for a few advanced techniq ues.In this chapter we will look at three different m<strong>et</strong>hods which can increas<strong>et</strong>he speed and efficiency of your <strong>graphics</strong>. Though the ideas discussed maybe applied in either BASIC or machine-code, the examples are presentedin BASIC for simplicity. The amount of benefit derived from each techniquewill depend on the particular application, and may vary from a lot tonone at <strong>al</strong>l (or worse). However, since a great amount of time spentprogramming any game is devoted to cleaning up the <strong>graphics</strong>, som<strong>et</strong>imesyou have to be satisfied with sever<strong>al</strong> sm<strong>al</strong>l improvements.We will start with the idea of parti<strong>al</strong> modification, where instead ofredrawing the entire figure each time, you only plot those bytes whichhave changed from the previous figure. A good illustration of this idea is147


148 APPLE II COMPUTER GRAPHICSprovided by a scoreboard in Hi-Res; the digits keep changing in a predictablemanner, and you can use that fact to shorten your code. Enter and runthe following program (you may omit the REM statements if you wish).1 REM PARTIAL MOD .2 REM3 HGR4 REM5 REM POKE EIGHT6 REM7 POKE 8192,6 : POKE 9216,66:POKE 124,66: POKE 11264,68 POKE 12288,66: POKE 13312 ,66:POKE 14336,6: POKE 1536,9 VTAB (24) : PRINT "PRESS A KEY 11 •GET R$1 REM11 REM POKE NINE12 REM13 POKE 8192,6 : POKE 9216,66:POKE 124,66: POKE 11264,614 POKE 12288,64: POKE 13312 ,64:POKE 14336,6: POKE 1536,15 VTAB (24) : PRINT "PRESS A KEY"GET R$16 REM17 REM POKE ZERO18 REM19 POKE 8192,6: POKE 9216,66:POKE 124,66: POKE 11264,662 POKE 12288,66: POKE 13312,66:POKE 14336,6: POKE 1536,21 VTAB (24) : PRINT "PRESS A KEY" :GET R$22 GOTO 7Listing 13-1.


CHAPTER 13-ADVANCED MOVES 149When you have everything keyed in correctly, you will see the digitseight, nine, and zero cycle on the Hi-Res screen. This was easily done byPOKEing the appropriate dot patterns into screen memory.For example, the numer<strong>al</strong>s.eight and nine are composed of eight rows ofdots which correspond to byte v<strong>al</strong>ues as shown below.DOT BIT DECIMAL DOT BIT DECIMALPATTERN PATTERN VALUE PATTERN PATTERN VALUE------------------------------------------------------------XXXX--X----X-X----X--XXXX--X----X-X----X--XXXX--------!il!illl ll!iJ!iJ!ill!il!il !il!ill!il!ill!il!il !il!ill!il!il!illl ll!iJ!iJ!ill!il!il !il!ill!il!ill!il!il !il!ill!il!il!illl ll!il!il!il!il!il!il !il!il!il!il6!iJ66666!iJ66666!iJ!iJ--XXXX--X----X-X----X--XXXX-------X------X--XXXX--------!iJ!illl ll!iJ!il!ill!il!il !iJl!iJ!il!ill!il!il !il!ill!il!il!illl ll!iJ!il!ill!il!il !il!ill!il!ill!il!il !il!ill!il!il!illl ll!il!il!il!il!il!il !il!il!il!il6!iJ66666!iJ66666!il!iJFigure 13-1.Now, if you take a minute to compare the v<strong>al</strong>ues used for eight withthose used for nine, you should notice that <strong>al</strong>l except two of the v<strong>al</strong>ues ar<strong>et</strong>he same. So the question natur<strong>al</strong>ly arises, "Since nine <strong>al</strong>ways followseight, why should I POKE <strong>al</strong>l the v<strong>al</strong>ues for the nine, when six of them ar<strong>et</strong>he same as before?!?" Funny you should ask!That is the idea behind parti<strong>al</strong> mod ification-<strong>al</strong>ter the existing figureinstead of tot<strong>al</strong>ly replacing it. Type these lines to change the previouslisting.13 REM14!il POKE 12288,64: POKE 13312 ,6419!il POKE 11264,662!iJ!iJ POKE 12288,66: POKE 13312 ,66Now lines 13© and 14© will POKE only the changes needed to turn theeight into the nine, and similarly lines 19© and 2©© POKE the changesrequired to turn nine into zero. When you run the program you will notsee much difference, but you will have the satisfaction of knowing thatyour code is more efficient.Granted, the time savings is insignificant in this example, but if you ar<strong>et</strong>rying to animate 15© bytes worth of Zylon spaceship, or what have you,parti<strong>al</strong> n1odification could potenti<strong>al</strong>ly save a great de<strong>al</strong> of time.Pre-c<strong>al</strong>culationThe next topic to consider is pre-c<strong>al</strong>culation. When a figure is moving


150 APPLE II COMPUTER GRAPHICSaround the screen, there are many c<strong>al</strong>culations to be made: the shape's Xand Y-coordinates, perhaps the address corresponding to those coordinates,and if you are animating using byte-move, which of the sevenversions of the shape is to be used at each coordinate.Som<strong>et</strong>imes the figuring can be done "on the fly," such as after the shapeis drawn on the screen and before it is erased to be moved. This increasesthe time the object is on the screen. This increases the ratio of the displaytime to the erase time, which in turn reduces flicker. However, c<strong>al</strong>culating<strong>al</strong>l of this within the animation routine will slow it down becausearithm<strong>et</strong>ic operations tend to gobble up relatively large amounts of processortime-remember, it is usu<strong>al</strong>ly faster to look it up than to figure it out.As an <strong>al</strong>ternative, it is possible to compute the path of an object before itstarts, and store each of the coordinates in a table (BASIC c<strong>al</strong>ls themarrays). It is usu<strong>al</strong>ly faster to look a number up in a table (especi<strong>al</strong>ly whenusing machine code) than it is to compute it on the spot. The gameTHRESHOLD uses tables, byte-move, and pre-c<strong>al</strong>culation in its animation.In Chapter 12 we used byte-move techniq ues to move a line across thescreen. If you still have that program lying around on a disk somewhere,go g<strong>et</strong> it because we are about to mod ify it. The compl<strong>et</strong>e listing is givenbelow, but if you have the old program you can avoid having to type mostof it in again.10 DIM A%(280 ): REM 280 X-COORDINATES20 REM30 REM READ THE VALUES FOR40 REM THE 7 PAIRS OF FRAMES50 REM60 FOR I = 0 TO 670 READ T%(I), H%(I)80 NEXT I90 REM100 REM110 REM120 REM INITIALIZE THE TABLE130 REM OF ADDRESSES140 REM150 J = 0160 FOR I170 A%(J)180 NEXT190 REM14336 TO 14374I:J = J + 1200 REM PRE CALC .205 REM210 DIM Q%(280 ),R% (280 )220 FOR X 1 TO 280230 Q%(X) = x I 7:R%(X) X-Q% ( X ) * 7


CHAPTER 13-ADVANCED MOVES 15124 NEXT X25 HGR : REM SET GRAPHICS254 REM255 REM HERE GOES! !256 REM26 FOR X = 1 TO 2827 POKE A%(Q%(X) ),T%(R%(X) ):POKE A% ( Q%(X ) + 1) ,H%(R%( X) )28 NEXT X29 END294 REM295 REM DATA TABLE296 REM3 DATA 126,1,124, 3,12,7, 112,1531 DATA 96, Jl,64,63,, 127Listing 13-2.Lines 100, 160, and 200 through 270 are the only new mod ifications.The arrays Q% and R% hold the quotients and remainders fo r each ofthe 280 X-coordinates. Dividing the X v<strong>al</strong>ue by 7 gives you the offs<strong>et</strong> (0-39)used to address the correct byte ac ross the screen, and <strong>al</strong>so which of theseven versions of the figure should be used (Remember?).Lines 160-180 fi ll array A% with the addresses for each of the 40 bytesacross the screen line, and lines 220-240 c<strong>al</strong>culate the 280 quotients andremainders. Fin<strong>al</strong>ly, line 250 turns on Hi-Res, and line 270 does the actu<strong>al</strong>POKEing.Sorry about the compound indexing [A%(Q%(X))], but it could not behelped. X is the coordinate number, so Q%(X) is the quotient belonging tothat coordinate, and A%(Q%(X)) is the address d<strong>et</strong>ermi ned by that quotient.This new version of the program runs the line across the screen in sevenseconds, as opposed to nine in the earlier one. Now, seven seconds is stillpr<strong>et</strong>ty slow (blame BASIC), but pre-c<strong>al</strong>culating did result in a significantimprovement (22 percent, since you asked.)Pre-sh<strong>ii</strong>ng ·And now, on to perhaps the most elegant of the techniques: pre-shifting.We are going to animate using a shape table, so take a couple of minutesnow to enter it. From BASIC type:CALL -151


152 APPLE II COMPUTER GRAPHICS3 : 2 6 45 3F 3F38: 3F 3F 3F 3F 8 2D 2D 2D31 : 2D 2D 2D 18 3F 3F 3F 3F318 : 3F 3F 8 2D 2D 2D 2D 2D32 : 2D .18 3F 3F 3F 3F 3F 3F328: 8 2D 2D 2D 2D 2D 2D 1833 : 3F 3F 3F 3F 3F 3F 8 2D338: 2D 2D 2D 2D 2D 18 3F 3F34 : 3F 3F 3F 3F 24 24 24348 : 24 DF DB DB DB 6 36 3635 : 36 36 3DGBSAVE SQUARE ,A$3, L$53With the table still in memory, l<strong>et</strong>'s fi nd out what we actu<strong>al</strong>ly have there.Type:POKE 232,: POKE 233,3HCOLOR = 3:ROT = :SCALE = l :HGRDRAW 1 AT 5,5The fi rst line tells APPLESOFT where the table is stored ($0300) in lobyte/hi-bytefo rm (00 and 03). The second line s<strong>et</strong>s <strong>al</strong>l the param<strong>et</strong>ers, andthe third draws the fi rst shape, a rectangle, at 50,50 on the Hi-Res screen.The second shape in the table is the horizont<strong>al</strong> pre-shift (the what!??) ofthe origin<strong>al</strong> rectangle. To explain, imagine that you shift the rectangle oneplace to the right. The bulk of the figure is unchanged; there is a single lineadded to the right side, and one del<strong>et</strong>ed from the left side. Pre-shifting, likeparti<strong>al</strong> modification, is a way to process only that portion of the figurewhich changes while leaving the rest <strong>al</strong>one.Now type:DRAW 2 AT 51,6to draw the pre-shift below the rectangle. Notice that the pre-shift has asingle line to the right of the rectangle, and another that lines up with theleft side of the rectangle. To affect the mod ification, we will XDRAW thepre-shift on top of the rectangle.L<strong>et</strong> us digress for a moment. When used to superimpose one figure onanother, XDRAW has the effect of comparing corresponding dots of th<strong>et</strong>wo shapes and forming a resultant figure from them. The resultant dot ison if either one of the origin<strong>al</strong> dots were on, but not both. The chart belowsummarizes the results from the four possibilities.Dot#!#2ONONONOFFOFFONOFFOFFResultOFFONONOFF


CHAPTER 13-ADVANCED MOVES 153So, when the pre-sh ift is XDRAWn over the rectangle, the dots on theleft side of both figures are on, so that whole row will be turned off. Butthe right side of the pre-shift is one row beyond the rectangle, and sinceonly the pre-shift dots are on, the result will be to turn that row of dots on.But enough words, l<strong>et</strong>'s try it! From BASIC type:XDRAW 2 AT 51,50XDRAW 2 AT 52,50XDRAW 2 AT 53 ,50XDRAW 2 AT 54,50XDRAW 2 AT 54,50XDRAW 2 AT 53,50The rectangle will move to the right, and then back to the left. If you aresurprised by the repeated XDRAWs at 54, remember that two consecutiveXDRAWs <strong>al</strong>ways cancel out. The fi rst XDRAW moves the rectangle right,and the second cancels it out to move the rectangle back to the left. Playwith XDRAWing this figure manu<strong>al</strong>ly until you can move it around comfortably.Listing 13-3 uses this idea to move the rectangle across the screen.10 REM PRE-SHIFT20 REM30 0$ = CHR$ (4)40 PRINT 0$ "BLOAD SQUARE"50 POKE 232,0 : POKE 233 ,360 HCOLOR = 3 : ROT = 0 : SCALE = 170 HGR80 DRAW 1 AT 20 ,10090 FOR I = 21 TO 275100 XDRAW 2 AT I ,100110 NEXT IListing 13-3.There are two very pleasant surprises with this program. The fi rst is that itis short and simple, and the second is that it moves the figure quickly andwith very little flicker!You might wish to compare this m<strong>et</strong>hod with the earlier one of XDRAWingthe shape on and off the screen. To do so, just make the followingchanges:80 FOR I = 20 TO 27590 XDRAW 2 AT I,100100 XDRAW 2 AT I,100Pre-shifting can be used for both horizont<strong>al</strong> and vertic<strong>al</strong> movement. Tod<strong>et</strong>ermine the pre-shift for any figure, just XDRAW it once, shift in the


154 APPLE II COMPUTER GRAPHICSdesired direction, and XDRAW it again. To demonstrate, l<strong>et</strong>'s fi nd thevertic<strong>al</strong> pre-shift of our rectangle. From BASIC type:HGRXDRAW 1 AT 50,50XDRAW 1 AT 50, 49The figure remaining on the screen is the vertic<strong>al</strong> pre-shift of the rectangle.To use it, you need to include it in a shape table and then XDRAW itover the rectangle. Doing this will make the rectangle appear to movevertic<strong>al</strong>ly. Creating a shape out of the pre-shift may still be a sm<strong>al</strong>l problem,but sever<strong>al</strong> of the Hi-Res editors are capable of that.Pre-shifting may <strong>al</strong>so be used with byte-move shapes. To do this, the fullfigure is fi rst drawn on the screen, and from then on pre-shifts are used tomove it. As a consequence, there are seven versions of the pre-shift insteadof seven versions of the figure, and the proper pre-shift is selected byexamining the remainder.Pre-shifting with byte-move requires that you "Exclusive Or" (EOR) thebytes in the pre-shift over the bytes on the screen. Remember, an "ExclusiveOr" operation compares two bits and gives a positive (ON) result ifeither one of the origin<strong>al</strong>s was ON, but not both. Otherwise, the result isnegative (OFF) . The XDRAW command does exactly that with shape tablefigures, but since byte-move does not use a shape table, you must arrive atyour own EOR routine. This is a pain from BASIC, but machine language,where byte-move is most advantageous anyway, has its own EOR commandso the routine is fairly easy to write.Back in the Paddle AgainJust for fu n, l<strong>et</strong>'s mod ify Listing 13-3 fu rther to use paddle (/) to control therectangle.80 DRAW 1 AT 20,10090 X = 20: XO = 20100 OD = 1110 REM120 IF PDL (0) < 90 THEN X = X-1:ND = 0: GOTO 140130 IF PDL (0) > 150 THEN X = X + 1:ND = 1140 IF X < 20 OR X > 275 THEN X XO150 IF X = XO GOTO 110160 IF ND < > OD THEN XDRAW 2 ATxo,100170 XDRAW 2 AT X ,100 : XO = X :OD = ND180 GOTO 110


CHAPTER 13-ADVANCED MOVES 155Lines 120 and 130 test the v<strong>al</strong>ue of PDL(0) and decrement or incrementthe X-coordinate as needed. Line 140 keeps everything within range, andline 150 skips the XDRAW statements if there is no movement of thesquare-this helps to avoid flicker.In line 160, if the old direction (OD) is different from the new direction(ND) then the extra XDRAW is done. (Remember that series of XDRAWstatements above?) Line 170 does the regular XDRAW and s<strong>et</strong>s the oldv<strong>al</strong>ues to the new v<strong>al</strong>ues.With these changes in effect, the rectangle will respond to paddle control.When you add parti<strong>al</strong> modification, pre-shifting, and pre-c<strong>al</strong>culation toyour repertoire, you give the fi n<strong>al</strong> polish to your animation skil ls. That isabout <strong>al</strong>l there is to know about designing and animating figures, and inthe next chapter we address a slightly diffe rent topic: collision d<strong>et</strong>ection.* * * One extra tid-bit for the Apple lie***One annoying source of flicker in animation stems from changing thedata being displayed while it is being scanned. The electron beam in yourmonitor or TV sweeps over the entire screen sixty times every second,much as a searchlight will sweep across the sky. The trick is to change thedisplay data when the beam is not "looking." Over one-quarter of the timein each sweep is spent g<strong>et</strong>ting the beam from the bottom back to the topof the screen so it can begin the next cycle. This is c<strong>al</strong>led Vertic<strong>al</strong> Blanking.Since nothing new can be displayed during this interv<strong>al</strong> (about one-200th of a second), this is the best time to switch the display data. With theApple II + there was no way to tell when the vertic<strong>al</strong> blanking was occurring,but the lie has a speci<strong>al</strong> Vertic<strong>al</strong> Blanking Location (VBL) at 49177($C019) which sign<strong>al</strong>s the vertic<strong>al</strong> blanking interv<strong>al</strong> to anyone who cares tolook. When the vertic<strong>al</strong> blanking interv<strong>al</strong> begins, the v<strong>al</strong>ue in the VBLdrops to less than 128, and this v<strong>al</strong>ue may be read using a PEEK statement.L<strong>et</strong>'s suppose that the routine to animate the figure by changing thedisplayed data begins at line 1000, then line 1000 could read:l(il(il(il IF PEEK ( 49177) > 127 GOTO l(il(il(ill(ill(il Routine to POKE in new v<strong>al</strong>uesLine 1000 will delay the changing of display v<strong>al</strong>ues until the vertic<strong>al</strong> blankingperiod begins, at which time it will drop through to line 1010 andbegin changing.The VBL is still too new to fairly ev<strong>al</strong>uate its usefu lness, but it is g<strong>et</strong>tingmixed reviews from some of the current game programmers. Although it isnice to change the display data during the vertic<strong>al</strong> blanki ng interv<strong>al</strong>, doing


156 APPLE II COMPUTER GRAPHICSso may slow the animation since movement is <strong>al</strong>ways delayed until thenext vertic<strong>al</strong> blanking interv<strong>al</strong>. Having to wait for less than one-sixti<strong>et</strong>h of asecond may not seem like much to you, but to your Apple it is quite a longtime. We will just have to wait and see what new techniques are developedfor the Apple·lie.VocabularyExclusive OR (EOR)Parti<strong>al</strong> ModificationPre-C<strong>al</strong>culationPre-ShiftVertic<strong>al</strong> BlankingVertic<strong>al</strong> Blanking Location (VBL)Exercises1. Suppose that a figure beginning at (2(/)(/), 19(/J) on the Hi-Res screentravels with a slope of one-h<strong>al</strong>f (moves up one and right two eachtime). Develop a table containing its X-Y coordinate pairs from itsorigin until it runs into the TOP of the screen. (In the course ofmoving it will contact the right side of the screen. At that time itshould begin to move up one and left two with each movement.)2. Use the table developed in Exercise 1 to animate the square from theshape table discussed in this chapter.3. Use parti<strong>al</strong> mod ification to cycle through ten digits on the Hi-Resscreen.4. Create a shape table containing a rectangle, its horizont<strong>al</strong> and vertic<strong>al</strong>pre-shifts, its two diagon<strong>al</strong> (up one, over one) pre-shifts, and the preshiftsrequired to move the rectangle up one, right two or up one, lefttwo. (Use a <strong>graphics</strong> editor if you own one!)5. Use each of the pre-shifts developed in Exercise 4 to move the rectangle.


15714Collision CourseObjectivesAfter reading Chapter 14 you should be able to:• D<strong>et</strong>ect a collision b<strong>et</strong>ween objects on the screen using either the boxm<strong>et</strong>hod or the contact m<strong>et</strong>hod.By this time you should have your figures moving smoothly about thescreen. In this chapter we will touch on a vit<strong>al</strong> element of most everyarcade game in existence: d<strong>et</strong>ecting a collision b<strong>et</strong>ween two objects on thescreen.It is trivi<strong>al</strong> for you to see when two objects me<strong>et</strong>; you simply look at themonitor! However, your Apple II does not have eyes, so it is in the sameposition as a blind person who uses a cane to probe for objects in his orher path. This an<strong>al</strong>ogy actu<strong>al</strong>ly contains more truth than it has a right to.Imagine the problem of de<strong>al</strong>ing with just two objects on the screen. It istheor<strong>et</strong>ic<strong>al</strong>ly possible to keep a list of <strong>al</strong>l the X-Y coordinates used in eachshape, and then to check fo r collision by seeing if there is a coordinatepair which lies in both shapes. Suppose that one shape is plotted on [(1 , 1),(1,2),(1,3)], and the second uses coordinates [(1,2),(2,2),(3,2)]. In this cas<strong>et</strong>here must be a collision, since the point (1,2) is a part of both shapes.Sound simple?Unfo rtunately, this m<strong>et</strong>hod quickly becomes unworkable since, whenanimating, you are continu<strong>al</strong>ly changing the points used in each shape, soyou would have to continu<strong>al</strong>ly change <strong>al</strong>l of the lists. More important, th<strong>et</strong>ask of cross-checking each point g<strong>et</strong>s quickly out of hand since even atrivi<strong>al</strong> figure will contain perhaps ten points. Cross-checking two such


158 APPLE II COM PUTER GRAPHICSfigures leads to 100 checks, and three figures may give you 100©! So muchfor that idea.We are going to examine two m<strong>et</strong>hods which both use the blind-man'scane approach. We will fi re a missile at a shape stolen from CROSSFIRE(by J. Sullivan), and as the missile moves, we will continu<strong>al</strong>ly check itspath. The shape table is given below, so take a few minutes now to enterand save it. From BASIC type:CALL -151300 : 02 00 06 00 4A 00 4D 49308 : 49 69 18 DF DB DF DB 07310 : 48 49 69 4D 49 18 DF DB318 : DB DB 07 48 0D 6D 0D 6D320 : 0D D8 DB FB DF DB 48 09328 : 0D 0D 0D 4D 01 D8 DF FB330: DF FB 08 4D 49 4D 49 05338 : 18 DF DB DB DB 07 08 4D340 : 49 49 49 05 D8 FB DB DB348 : DF 00 36 27 0D 36 00 0BSAVE XFSHAPE, A$30,L$53 DGPOKE 232,:POKE 233 ,3HGR :HCOLOR = 3 :SCALE = l :ROT = DRAW 1 AT 2,20DRAW 2 AT 2 50The first shape is our targ<strong>et</strong>. It should look a bit like a spider. The secondshape is the missile, and it should look like this:xxxxxxxWhen you g<strong>et</strong> the table saved correctly, you are ready to animate. Wewill leave the spider <strong>al</strong>one and move the missile back and forth across thebottom of the screen using paddle 0. When the paddle button is pressed,we will move the missile up the screen until we bump into either thespider or the top of the screen. To effect that movement, we will plot anderase the missile, then move up one line and do it again. Befo re eachmovement of the missile, we will check the space it is moving into and seeif there is som<strong>et</strong>hing (the targ<strong>et</strong>) <strong>al</strong>ready there. Instead of checki ng to see ifsom<strong>et</strong>hing hits the targ<strong>et</strong>, we will see if som<strong>et</strong>hing runs into the missile!We will develop the program in two stages. The following commandswill g<strong>et</strong> the basic movement started. You may omit any REM statementsexcept 15© and 50© which are used as entry points for GOTO statements.


CHAPTER 14-COLLISION COURSE 159l!i' D$ = CHR$ (4)2!i' PRINT D$ "BLOAD XFSHAPE"3!i' MY, = 5 :M = 58!i' REM85 POKE 232,Y,: POKE 233 , 39!i' HGR : HCOLOR = 3 : ROT = Ii': SCALE = 1l!i'!i' DRAW 1 AT 14!i',5!i'11 XDRAW 2 AT M,15!i'12!i' REMl3!i' REM CHECK PADDLE14!i' REM BUTTON15!i' REM16!i' IF PEEK ( -16287) > 127 THENGOSUB 5!i'!i'17!i' REM18!i' REM MOVE MISSILE19!i' REM2!i'!i' IF PDL (!i') < 9!i' THEN M = M-2 :GOTO 22!i'21!i' IF PDL (!i') > 15!i' THEN IF M < 277THEN M = M + 222!i' IF M < 1 THEN M = 123!i' IF M = Mli' GOTO 25!i'24!i' XDRAW 2 AT MY,,15!i' : XDRAW 2 AT M,15!i':M!i' = M25!i' GOTO 15!i'48!i' REM49!i' REM COLLISION DETECT5!i'!i' REM51!i' REM WE ' LL PUT THIS IN SHORTLY !52!i' PRINT CHR$(7) :RETURNListing 14-1.This listing will g<strong>et</strong> the spider onto the screen and the missile movingunder paddle control. When you press the paddle 0 button the <strong>computer</strong>will beep at you, which indicates that the (as y<strong>et</strong> non-existent) fire/coll ision-d<strong>et</strong>ect routine has been c<strong>al</strong>led.M0 is the "old" X-coordinate of the missile, and M is the new one. Themissile is moved by decrementing or incrementing M (lines 200 and 210),then XDRAWing to erase the old missile, and then XDRAWing again toplot the new one (line 240).After you have the missile moving across the bottom of the screen, wewill t<strong>al</strong>k about the collision routine. Ru n the program as you have it so far,and press CTRL-C to recapture control while leaving the spider on thescreen. Imagine that there is a little box drawn around the spider. In fact,put tne box there by typing these four lines from BASIC.


160 APPLE II COMPUTER GRAPHICSHPLOT 137,38 TO 153 ,38, HPLOT TO 153 , 51HPLOT TO 137, 51HPLOT TO 137 ,38As we move the missile up the screen we will check to see if it movesinside that box, and if so- BOOM! To g<strong>et</strong> that to happen, add these linesto your program:4(i' REM5(i' REM SET BOUNDARIES6(i' REM7(i' YMAX = 51:MINX = 137:MAXX 15351(i' PRINT CHR$ (7) : REM BELL52(i' C = <strong>ii</strong>': REM COLLISION FLAG53<strong>ii</strong>' FOR Y = 149 TO <strong>ii</strong>' STEP -154(i' IF Y < YMAX THEN IF M > MINXAND M < MAXX THEN C = 1: REM COLLISION ! !55<strong>ii</strong>' IF C THEN PRINT CHR$ (7) ; CHR$ (7) ;CHR$ (7) : XDRAW 2 AT M,Y + 1: GOTO 62<strong>ii</strong>'561i' XDRAW 2 AT M, Y + 1: XDRAW 2 AT M , Y571i' NEXT Y58<strong>ii</strong>' REM ERASE MISSILE @) TOP591i' y = y + 16(i'fi' XDRAW 2 AT M ,Y61fi' REM DRAW NEXT MISSILE62fi' XDRAW 2 AT M, 15<strong>ii</strong>'63fi' RETURNListing 14-2.Line 7(/J s<strong>et</strong>s the boundaries for the square. The upper boundary is ignoredin this example because the missiles <strong>al</strong>ways come from below.Line 54(/J is the line that actu<strong>al</strong>ly d<strong>et</strong>ects any collision. It first checks theY-coordinate at the tip of the missile to see if it is high enough to possiblystrike the box. If so, then the X position is checked to see if it is in thecorrect horizont<strong>al</strong> range. If the answer to <strong>al</strong>l of these is "yes," then wehave a coll ision and the flag (C) is s<strong>et</strong> to 1 for later reference.Line 55(/J is where you jump to g<strong>et</strong> your nifty explosion routine, but sinc<strong>et</strong>his is only an example, <strong>al</strong>l we do is s<strong>et</strong> off the bells and whistles. We thenerase the missile from its last position and jump to where a new missile idrawn at the bottom- big de<strong>al</strong>!In this case, since the targ<strong>et</strong> is stationary, it would be advantageous tocheck the X-coordinate before the Y. If the X v<strong>al</strong>ue is within the range ofthe targ<strong>et</strong>, there is bound to be a collision. In that case you need to checkonly the Y-coordinate as the missile rises in order to tell you when themissile strikes the targ<strong>et</strong>. If the X is not within range, then the missile will


CHAPTER 14-COLLISION COU RSE 16 1<strong>al</strong>ways miss the spider so you do not have to check either coordinateagain, just l<strong>et</strong> the missile make its way to the top of the screen. This sort ofspeci<strong>al</strong>ized adjustment can often streamline your animation.The box m<strong>et</strong>hod used in the example is fairly simple, but it is a littlesloppy since it declares a collision based on some imaginary squ are insteadof actu<strong>al</strong> contact with the targ<strong>et</strong>. Further, we only checked for the centerof the missile, and the center may actu<strong>al</strong>ly miss the spider when one of theedges hits it. Unfair! If you have ever played a game which has sloppycollision d<strong>et</strong>ection (and there are a bunch of them) you know how frustratingthat can be.Checking across the full width of the missile is pr<strong>et</strong>ty easy, so we leav<strong>et</strong>hat to you. The problem of d<strong>et</strong>ecting an actu<strong>al</strong> contact is more interesting.To accomplish that, you will have to take a PEEK at Hi-Res memory to seeif the missile is about to move onto a dot which is <strong>al</strong>ready turned on. (Inthis example, the only dots turned on are in the targ<strong>et</strong>.) De<strong>al</strong>ing directlywith memory from BASIC is <strong>al</strong>ways complex, and it g<strong>et</strong>s worse because weneed to look at ind ividu<strong>al</strong> bits. Type the following changes to the existingprogram.DEL 4, 75 GOSUB 1 : REM CALC Y ADDR .515 Q% = M I 7:R% = M-7 * Q% :R% R% + 1536 V% = PEEK (Y%(Y) + Q%)538 FOR I = 7 TO R% STEP -154 P% = 2 A I542 IF V% > P% THEN V% = V%-P%544 NEXT I546 IF V% > 2 A I THEN c = 11 REM11 REM CALCULATE Y12 REM COORDINATES13 REM14 DIM Y% (192)15 FOR I = TO 184 STEP 8: READ SA%16 FOR J = TO 7:Y%(I + J) = SA% +J * 12417 NEXT J,I18 DATA 8192,832, 8448,8576,874,8832, 896,98819 DATA 8232,836, 8488, 8616 ,8744,8872, 9,912811 DATA 8272,84,8528,8656,8784,8912, 94,9168111 RETURNLines 5 and 1000 to 1100 should be fam iliar from previous chapters; they


162 APPLE II COMPUTER GRAPHICSs<strong>et</strong> up a table which assigns to each line on the screen ((/J- 191) its startingaddress.Line 515 divides the X-coordinate by 7 to d<strong>et</strong>ermine which of the 4(/Jbytes across the screen the missile is in (Q%), and which dot in that bytewill be the point of the missile (R%). L<strong>et</strong>'s suppose that the missile is fi redat X-coordinate 45, so the tip is in byte number 6 and dot number 3(counting the fi rst dot in each byte as number (/)). As we move the missileup, we need to check the third dot in the byte above the missile before wemove. As long as that dot is off, then there is nothing to hit. If that dot ison, then we are about to run into ' the targ<strong>et</strong>, and need to s<strong>et</strong> the collisionflag.Isolating a particular dot is clumsy, but lines 338-546 do the job. Line336 s<strong>et</strong>s V% to the v<strong>al</strong>ue of the byte you need to check. To understandhow these lines operate, it is instructive (though tedious) to s<strong>et</strong> V% to, say!(/J(/)1 1 11(/J(/J (6(/J), and step through those lines by hand. Here we go.If the X-coordinate is 45, then line 515 c<strong>al</strong>culates R% to be three (we arechecking fo r dot number 3 in the byte), and then adds 1 to it so R% entersthe loop at 538 with a v<strong>al</strong>ue of four. Dot number 3 corresponds to thefou rth bit from the right, yes the FOU RTH bit, for they are numbered (/), 1,2, and 3.P% is the v<strong>al</strong>ue d<strong>et</strong>ermi ned by two to the "I" power, and that justhappens to correspond to each digit in a binary byte. Two to the eighth isthe left-most bit (128), two to the seventh is the next bit over, and so on.Each time the loop is executed, it effectively checks one of the bits in thebyte and turns it off if it happens to be on. This continues until the bitind icated by R% is reached. L<strong>et</strong>'s run through the example using Figure14-1 .IP%V%765412864321660602812! 0011 1100! 0011 1100i0001 ·· 1100! 0000 1100312 =Figure 14-1.The first time through the loop, I = 7, so P% = 128. Line 542 fi nds thecomparison (IF V% > = P%) f<strong>al</strong>se, so it does not subtract anything fromV%. The second time through, P% = 64, still greater than V%, so againnothing happens. But on the third iteration, I = 5, P% = 32, so P% issubtracted from V% to res<strong>et</strong> V% at !(/J(/J(/)1 11 (/)(/). As promised, the bit was


CHAPTER 14-COLLISION COU RSE 163turned off. The next time through, when I = 4, the next bit is <strong>al</strong>so turnedoff. After that the loop is h<strong>al</strong>ted since I has reached the v<strong>al</strong>ue of R%.Therefore, at the end of the loop, V% equ<strong>al</strong>s 12, which in binary is !l/Jl/Jl/Jl/J111/Jl/J. We were trying to check the fourth bit from the right, and we dothat now with line 546. The left-over v<strong>al</strong>ue of I is three, so V% is comparedagainst two to the third, or eight. The only way for V% to be greater thanor equ<strong>al</strong> to eight is for the fourth bit to be on, so the collision flag is s<strong>et</strong>.Whew!I P% V%------ -------------------------------7 128 52 ! 0(.Hl 01006 64 52 ! 0011 01005 32 20 ! 0001 01004 16 4 ! 0000 0100------ -------3 4------------------------Figure 14-2.Figure 14-2 shows the same procedure performed when V% = 52where the fourth bit is off. There should not be a collision indicated . If youfollow the loop through, you will see that the comparison at line 546 fails,so the flag is not s<strong>et</strong>. We told you that it would be rather awkward tocheck single bits!One weakness of this routine (as in the previous one) is that it onlychecks the center of the missile, so it is possible to contact the targ<strong>et</strong> withthe left or right sides and still not d<strong>et</strong>ect a collision. Two trivi<strong>al</strong> modificationsto the routine will cause it to check across the entire width of themissile, but again we leave them for you to discover!When you run this version of the program, you will notice that a collisionis d<strong>et</strong>ected only when the missile actu<strong>al</strong>ly contacts the targ<strong>et</strong>. You will<strong>al</strong>so notice that the animation is slower due to the increased processinginvolved in this scheme-you can't have everythi ng.With a little effort, the BASIC code can be streamlined to enhance theexecution, but the re<strong>al</strong> gain is made by writing this collision d<strong>et</strong>ect inmachine code, som<strong>et</strong>hing we will not cover here.The contact collision d<strong>et</strong>ect is further complicated if you have otherobjects on the screen, such as clouds, where the missile is required toignore the contact. There are two commonly used ways to handle this.In THRESHOLD, a table is kept of the current position for each targ<strong>et</strong>.When any collision is flagged, that table is scanned to see if there is atarg<strong>et</strong> close. If so it is destroyed, and if not the collision is ignored. Oneinteresting side effect is that when two THRESHOLD targ<strong>et</strong>s are close


164 APPLE II COMPUTER GRAPHICStog<strong>et</strong>her, the scanning routine will som<strong>et</strong>imes decide that the wrong onehas been shot.The other m<strong>et</strong>hod of de<strong>al</strong>ing with extraneous objects is to use both Hi­Res screens. One screen is displayed with <strong>al</strong>l the missiles, targ<strong>et</strong>s and otherjunk on it, while the other has only the missiles and targ<strong>et</strong>s on it, and is notdisplayed. The collision d<strong>et</strong>ect routine can then check the second screenand not even worry about the extra figures! In some applications the extratime needed to process two screens is offs<strong>et</strong> by not having to decide whatyou ran rnto.In Summary ....That about wraps it up. Hopefully this book has shed some light on waysto make Apple <strong>graphics</strong> do what you want them to, and how many of thegames achieve their effects.So now you have it-the tricks of the animation trade. The ideas presentedhere are the most common and effective techniques currently usedwith the APPLE II. Programming person<strong>al</strong> <strong>computer</strong>s is a creative and fastpacedsport. It is limited only by the programmer's imagination, so we canexpect that before too long there will be new, improved m<strong>et</strong>hods-that's(programming) life! However, the animation techniques you have learnedhere will still be usefu l, and the skills gained will help you to understandand implement the new approaches.VocabularyBox m<strong>et</strong>hodCollision flagContact m<strong>et</strong>hodExercises1. Use the table which contains the <strong>apple</strong> shape (from Chapter 1 l/J) toanimate two <strong>apple</strong>s on the screen, each under control of one of thepaddles. (If you do not have paddles, control the <strong>apple</strong>'s movementwith the keyboard.) Write a collision d<strong>et</strong>ect routine using either thebox or contact m<strong>et</strong>hod to d<strong>et</strong>ermine when the two shapes collide.2. Use the program from Chapter 13 (Listing 13-2), which moves thebyte-move line across the screen, and draw the spider shape in itspath. Write a routine to sign<strong>al</strong> when they contact.3. Write a <strong>computer</strong> game and make a million bucks.


Appendix 1Decim<strong>al</strong>, Hex, andBinaryWe are assuming that you have read the discussion of binary and hexadecim<strong>al</strong>in Chapter 2, and that you have a reasonable grasp of that materi<strong>al</strong>.In this section we will delve deeper into the structure of the three systemsof numeration in order to give you the background needed to understandthe techniques needed to transform the numbers used by your Apple II.For instance, why is the decim<strong>al</strong> fo rm of an address found from the hexform by taking 256 times the decim<strong>al</strong> v<strong>al</strong>ue of the hi-byte plus the decim<strong>al</strong>v<strong>al</strong>ue of the lo-byte? And how can anybody say that 384©© is equ iv<strong>al</strong>ent to-271 36??! All of this will be explained ... and more. But fi rst we are going toacquire some background which sounds, and is, distressingly like the NewMath.Positions, EveryoneWhen you were just a young sprout, your teachers probably explained toyou the significance of a digit's position within a number. Five does not<strong>al</strong>ways mean five; the five in 56 indicates fifty, but more precisely, fiv<strong>et</strong>ens. In 567, the 5 represents five hundreds, and in 5,678 it represents fiv<strong>et</strong>housands. The position of the digit within the number is significant. Fromright to left, you have the unit's place, the ten's place, hundred's place,and so forth. So 5,678 can be written as(5 x 1©3) + (6 x 1©2) + (7 x 1©1) + (8 x 1©0)which is(5 x 1 ©©©) + (6 x 1 ©©) + (7 x 1 ©) + (8 x 1)Many of us unfo rtunates spent hours of our young lives writing numbersthat way, c<strong>al</strong>led "expanded fo rm," for no apparent reason.There is little or no use in writing decim<strong>al</strong> numbers in that fo rm, but yourfourth grade teacher (Miss Whitherstare, or whoever) did you an inadvertentfavor by making you learn it, fo r expanded form is one way to convertnumbers from other outlandish bases, like binary and hex, to decim<strong>al</strong>. Toconvert a long hex number, like $89AB, to decim<strong>al</strong>, write it as(8 x 163) + (9 x 162) + (A x 161) + (B x 16°)165


166 APPLE II COMPUTER GRAPHICSThen convert each hex digit to decim<strong>al</strong> which gives you(8 x 4©96) + (9 x 256) + (1 © x 16) + (1 1 x 1) = 35243.There is a variation on this m<strong>et</strong>hod which is handy when you are proficientwith hex bytes. When de<strong>al</strong>ing with bytes, rec<strong>al</strong>l from Chapter 2 we mentionedthat two hex digits could be used to represent one base 256 digit.Then the address given by $FF3A can be c<strong>al</strong>culated as($FF x 2561) + ($3A x 256°)= (255 x 256) + (58 x 1)= 35243To use this idea efficiently, you must know or be able to quickly c<strong>al</strong>culat<strong>et</strong>he v<strong>al</strong>ue of numbers like $3A-a skill that comes with practice.The expanded fo rm m<strong>et</strong>hod <strong>al</strong>so works when converting binary to decim<strong>al</strong>,but since binary is base two, you g<strong>et</strong> two to <strong>al</strong>l those powers insteadof sixteen. From right to left in binary, the first position is the ones (2°)place, then the two's (21) place, the four's (22) place, the eight's (23) place,and so on. Therefore, you may write ! 1 1 ©1 ©1 as(1 x 25) + (1 x 24) + (© x 23) + (1 x 22)+ (© x 21) + (1 x 2°)Or even simpler, since it is either one or zero times the place v<strong>al</strong>ue32 + 16 + 4 + 1= 53This was c<strong>al</strong>culated by adding the place v<strong>al</strong>ues with ones in them, andignori ng the places containing zeros.Wonderfu l, but you <strong>al</strong>ready knew how to convert from binary and hexto decim<strong>al</strong>. These ideas will help you to become more fam iliar with whatyou are doing, and make you faster and more flexible in your conversions.Conve rting to decim<strong>al</strong> is fairly straightforward, but unfortunately the sameis not true of converting from decim<strong>al</strong> to binary or hex.Decim<strong>al</strong> ta BinaryWhen converting to binary, you can start by fi nding the highest power oftwo which will go into the decim<strong>al</strong> number-if you can g<strong>et</strong> your hands ona c<strong>al</strong>culator, now would be a good time to do so. L<strong>et</strong>'s take 421 as anexample to convert: 29 (512) is too large to divide by 421, but 28 = 256 (anumber to know in computing), and 256 will divide 421, <strong>al</strong>beit with aremainder of 165 (42 1-256), so we have a 1 in the 28 place which will beon the left end of our binary number. The next lowest power of two is 128,which divides 165 leaving a remainder of 37. So far our binary number is11 xxxxxxx. Dropping to the next power of two (64) we see that it will notdivide the present remainder (37) so we put a zero in the next place in our


APPENDIX 1-DECIMAL, HEX, AND BINARY 167number (1 1 0xxxxxx). 32 will divide 37 to yield a 1 in the next place(1 101 xxxxx) and a new remainder of 5. 16 and 8 both fail to divide the 5,so we place two more zeros (1 101 00xxx). 4 divides the 5, 2 fails to divid<strong>et</strong>he remainder (1 ), and we are left with a 1. So the last three digits are 101,to give us the binary equ iv<strong>al</strong>ent of 421 as !110100101. The <strong>al</strong>gorithm istedious, but simple-a good candidate for a <strong>computer</strong> program!REMEMBER: When converting decim<strong>al</strong> to binary, divide by the largestpossible power of two to form the quotient and remainder (no decim<strong>al</strong>points!). Take the remainder of the fi rst division and attempt to divide it bythe next lower power of two, and so on, attempting to divide each remainderby the next power of two until you reach a division by 1. Any successfuI division gives you a 1 in the corresponding place, while anunsuccessfu l division yields a 0 in that place.Decim<strong>al</strong> to HexConverting decim<strong>al</strong> to hex follows the same gener<strong>al</strong> outline as decim<strong>al</strong> tobinary, but it is complicated by having sixteen possible v<strong>al</strong>ues for eachdigit instead of two as in binary. When you were dividing by powers oftwo, each power of two went into your number either once, to give you a1, or not at <strong>al</strong>l, which gives you a 0. If your power divided the numbermore than once (two or more), then you knew that you should have beendividing by a higher power_ of two.The unfortunate who converts decim<strong>al</strong> to hex fi nds himself or herselfdividing by powers of sixteen (1, 16, 256, 4096), and a power of sixteenmay divide the number anywhere from zero to fifteen times! For example,l<strong>et</strong>'s convert 1019 to hex.256 is the largest power of sixteen which will divide 1019, and when youperform that division on your c<strong>al</strong>culator, the answer is 3.9804 and so on,so 256 goes into 1019 three times with som<strong>et</strong>hing remaining. That meansyou put a 3 in the 256's place (What 256's place?!). Since this is the firstdivision, the 3 is on the left end of the hex number, and now you need toknow the remainder of the division. To fi nd that (since your c<strong>al</strong>culator isgrossly inaccurate and truncates after ten or fifteen decim<strong>al</strong> places), multiplythree by 256 and subtract the result from 1019 (1019-3x256), to g<strong>et</strong>251.Now divide 251 by the next power of 16 (which happens to be 16 itself),and your c<strong>al</strong>culator will display a fifteen, a decim<strong>al</strong> point, and garbage.The fifteen indicates that you have an "F" (the hex equ iv<strong>al</strong>ent of 1 5) in thenext place of your hex number, which so far looks like $3F, and thedecim<strong>al</strong> point garbage means that you must c<strong>al</strong>culate a new remainder bymultiplying sixteen times "F" (15), and subtracting from 251 (251 -15 x16) to g<strong>et</strong> 11. Therefore, the last digit of the hex number is "B" (the hexequ iv<strong>al</strong>ent of eleven, sil ly!), so the entire number is $3FB. Still a little hazy,


168 APPLE II COMPUTER GRAPHICSyou say? It takes some practice before you will be comfortable with hex,unless you were born with sixteen fingers!L<strong>et</strong>' s convert 41 141 to hex. 4©96 (163) is the largest power to divide41 141 , and it does so ten times with a remainder, so the left-most digit willbe $A (hex for 1 ©) . C<strong>al</strong> cu lati ng the remainder (41 141 - 1 © x 4©96) givesyou 181. The next power of sixteen (1 62 or 256) will not divide 181 at <strong>al</strong>l,so put a zero iri the place following the "A" and g<strong>et</strong> $A©. Dividing 181 by16 gives 11 with a remainder of 5, so the last two digits are "B" and 5, andthe entire number is $A©B5. TA DA!!In a Nutshell:To convert a decim<strong>al</strong> number to hex, divide the decim<strong>al</strong> number by successivelysm<strong>al</strong>ler powers of sixteen, each time using the quotient as thehex digit, and the remainder as the decim<strong>al</strong> number for the next division.There is an <strong>al</strong>ternate m<strong>et</strong>hod which uses hex to help convert large uglydecim<strong>al</strong> numbers to large ugly binary numbers and vice versa. Take 65385for a sample. Start by converting to hex as we showed you earlier:65385 = (15 x 4©96) + (1 5 x 256) + (6 x 16) + (9 x 1)= $FF66Do not take our word for it-try it yourself! The next step is to converteach of the four hexad<strong>et</strong>im<strong>al</strong> digits to binary using the chart we gave youin Chapter 2: $F = !1 111, $6 = ©1 1©, and $9 = !1©©1 . Armed with thisinformation you can write the entire number in binary: 65385 = $FF69 =!1111 1111 ©1 1© 1©©1 . Using hex as an intermediate step saves you fromhaving to c<strong>al</strong>culate the highest power of two which goes into 65385.(Turns out to be 215 or 32768!)To convert a large binary number (say !1©©1 ©1 1© ©©©© ©©©©) to decim<strong>al</strong>we use the same scheme in reverse. That binary number is $96©© in hex,which in turn is (9 x 4©96) + (6 x 256) + (© x 16) + (© x 1) = 384©©.Adding and Subtracting HexWhen working with the <strong>computer</strong>' s memory, you will at some point orother be forced to add and/or subtract addresses. An example is given inthe discussion of shape tables where it is suggested that you might loadmultiple tables into memory consecutively, and then place the HIMEMboundary below them in order to protect the tables from the machinationsof APPLESOFT. Do not panic if you do not know about shape tables y<strong>et</strong>.For this application just think of a shape table as a bunch of bytes that youwant to load into memory.L<strong>et</strong>' s suppose that you have two tables to load, one $876 bytes long and


APPEN DIX 1-DECIMAL, HEX, AND BINARY 169the other $5A7 bytes. You will fi rst add the two lengths tog<strong>et</strong>her, and thensubtract the tot<strong>al</strong> from the HIMEM v<strong>al</strong>ue-$9600 if you have 48K andDOS.There are sever<strong>al</strong> m<strong>et</strong>hods used to perform the c<strong>al</strong>culations. The waymost of us began was to convert <strong>al</strong>l the numbers to decim<strong>al</strong>, perform theaddition and/or subtraction, and then convert to hex again-a safe, thoughtime-consuming approach. The other extreme is to do the entire operationin hex, and very few people can actu<strong>al</strong>ly carry that off. The technique wewill demonstrate leaves the numbers in hex, but you ment<strong>al</strong>ly converteach of the digits as you go. Write the two numbers to be added so thatone is atop the other, as below.$876+ $5A7A$876$5A7$E1 DBFigure A1-1.You begin, as usu<strong>al</strong>, from the right and add 6 and 7 to g<strong>et</strong> thirteen. MissWhitherstare told you that thirteen was "three carry one", but (aha!!) youare working in hex, so thirteen is written as "D" and there is no carry. Addthe next two digits, 7 and "A" to g<strong>et</strong> seventeen. Seventeen is one mor<strong>et</strong>han the magic number for hex (sixteen), so you write down the "1" andcarry the sixteen over to the next column as a one (See Figure A 1-1 B). Ifthat sounds strange, remember how you handle 7 + 4 in decim<strong>al</strong>: the sumis eleven (ten plus one), so you write the one down and carry the ten overas a one in the next column!In base ten you carry tens to the next column, and in base sixteen (hex)you carry sixteens.We are down to adding the last column, in which you add the 8, the 5,and the 1 from the carry, and g<strong>et</strong> fou rteen which is $E. This gives you$E1 Das the combined length of the two tables. You are doing very well sofar.Do not g<strong>et</strong> too confident though, you still need to subtract that amountfrom HIMEM ($9600) to fi nd the new v<strong>al</strong>ue to use.The first subtraction pits Ill against "D", and you have the age-old problemof borrowing, but in hex you borrow sixteens instead of tens. If you attendedpublic elementary school after 1959 you are probably used toborrowing across the top, but if you do it differently, bear with us-the


170 APPLE II COMPUTER GRAPHICS-$9600$0E1 0 -5 F16$9g00$0E1 0$ E3821BF16$f5fl1fl1OE1 0$87E3ABcFigure A1-2.idea is the same. You borrow one from the 6 and turn it into sixteen in thenext column to the right, while the 6 becomes a 5. You then borrow onefrom the sixteen, which becomes a fifteen ($F), and the borrowed onebecomes sixteen in the right-most column-Arrgh! (See Figure A 1 -28.)Now that the preliminaries are over, we begin the subtraction in earnestwith the right hand column: sixteen subtract "D" (thirteen) is three. Thenext column shows F (fifteen) subtract 1, which gives E (fourteen) as thatresult. The next column again requires a borrow, so borrow one from the9, making it 8, and add sixteen to the five to g<strong>et</strong> 21 (decim<strong>al</strong>!), as in FigureA 1-3C. Now you can c<strong>al</strong>culate twenty-one less "E" (fourteen) to g<strong>et</strong> 7, theeight carries down, and so the fi n<strong>al</strong> result is $87E3. If you got a differentanswer, stay after class and clean the erasers!L<strong>et</strong>'s try subtracting $4D from $3A7 for practice. Again the numbers arelined up, and you quickly see that a borrow is going to be needed (drat!).$3A7- $409 23$3)(7- $40$35AFigure A1-3.Borrowing hex one from the A leaves it a nine, and carries a decim<strong>al</strong>sixteen over to the seven to g<strong>et</strong> decim<strong>al</strong> twenty-three. Twenty-three minusD (1 3) is A (10). Then you take four from nine to g<strong>et</strong> five, and carry th<strong>et</strong>hree down. The fin<strong>al</strong> answer is $35A.Adding and Subtracting BinaryHaving to perform binary arithm<strong>et</strong>ic is a curse that often bef<strong>al</strong>ls programmers,even though the machine is supposed to do <strong>al</strong>l the work. Whenfaced with this proposition, you should keep in mind that zero and one are


APPENDIX 1-DECIMAL, HEX, AND BINARY 171the only available digits, and that a binary two is written as 1([), and binarythree is 11. Why are those important? We will demonstrate by adding!©©1 1 ©1 1 © to !©©©1 11([)1.-1'1 1 1! 001 1 01 10+ ! 0001 1101! 0101 001 1Figure A 1-4.Beginning at the right side, the fi rst column is © + 1 which gives you 1.You should <strong>al</strong>so g<strong>et</strong> a one in the second column without too muchtrouble-so far, so good. The third column g<strong>et</strong>s more complex as you have1 + 1. Any norm<strong>al</strong> second-grader can tell you that 1 + 1 = 2, butprogrammers should make no pr<strong>et</strong>ense at norm<strong>al</strong>cy. As we mentionedabove, two is written as 1([) (one-zero, not ten!), which puts the zero in theanswer and carries the one to the next column. Including the carry, thatnext column now requires you to add 1 + © + 1, which again gives 1©(write down the zero and carry the one) . The one is carried over to thefifth column where you now have 1 + 1 + 1 which yields three (1 1). Forthat, you must write down a one and carry a one to the next columnwhich then looks like 1 + 1 + ©, and requires you to write down a zeroand carry a one (1 ©). Then the seventh column is 1 + © + © which is aone (and no carry) and the last column is © + ©which is zero. That givesus a result of !©1©1 ©©1 1. WOW!Figure A 1-5 shows another example of adding binary, this tim with acarry off the end.1 '*' 1 1 1 1 '*'8 !1010 1100+ !1110 1010BITBUCKET ! 1001 01 10Figure A1-5.The fi rst column (right-most) gives zero, and both of the next twocolumns give ones. See how simple life is without a carry? The fourthcolumn gives zero, carry one, and the fifth then gives one with no carry.Things warm up a little with the sixth column which gives zero, and carriesone to the seventh. That column then <strong>al</strong>so gives zero and carries on to theeighth column. This makes the eighth addition 1 + 1 + 1 which inevitablygives 11 -write down one and carry one. Since a byte is only eight bitslong, the bit you try to carry to the ninth column f<strong>al</strong>ls into the bit buck<strong>et</strong>


172 APPLE II COMPUTER GRAPHICSand is ignored. For any of you interested in machine-level programming,when a bit is carried off the end of an addition, the processor s<strong>et</strong>s a sign<strong>al</strong>bit, c<strong>al</strong>led the carry flag, within the status register. That sign<strong>al</strong>s (to anybodywho knows where to look) that the latest operation had to dump a carryinto the buck<strong>et</strong>.SubtractionFigure A 1 -6 shows an example of binary subtraction.!001 1 01 11!0010 0101!0001 0010Figure A1-6.Again you work from right to left, and you should have no problemarriving at the result. This sample was carefully constructed so that youcould subtract without having to fiddle around with borrowing, not becauseof coward ice, but because you never re<strong>al</strong>ly have to worry aboutborrowing in binary. The fact is, your <strong>computer</strong> never subtracts, so whyshould you? Instead of subtracting a number, the arithm<strong>et</strong>ic unit of yourmachine <strong>al</strong>ways ADDS a thing c<strong>al</strong>led the "two's complement."Two1s ComplementForming the two's complement of a binary number takes two steps: firstchange <strong>al</strong>l the ones to zeros, and <strong>al</strong>l the zeros to ones, and second, addone to the result. To form the two's complement of !({)(1')10 (/)101, you fi rstturn it into !11(/)1 1(/)1(/) and then add 1 to g<strong>et</strong> !11(/)1 1011. Now we canrepeat the example from Figure A 1 -6 using addition, but instead of subtracting!(/')(/')1(/') (/)1 (/)1 , you will add its two's complement, !11(/)1 1(/)11.1 8 !001 1+ !1101!00010111101 10010Figure A1-7.The addition is shown in Figure A 1 -7, and is fairly straightforward. Theanswer comes out the same as before, except for the carry from the last


APPENDIX 1-DECIMAL, HEX, AND BINARY 173column which f<strong>al</strong>ls into the bit buck<strong>et</strong> that is ignored anyway. This is whythe two's complement can be used as if it were a negative number, whenit re<strong>al</strong>ly is not.If you add any number to its corresponding negative v<strong>al</strong>ue (five plusnegative five) you will <strong>al</strong>ways g<strong>et</strong> zero. The same thing happens when youadd any binary number to its two's complement ... <strong>al</strong>most. Try adding! (3(31(3 (31(31and its complement:! 11(31 1(311You will g<strong>et</strong> <strong>al</strong>l zeros, except for the fi n<strong>al</strong> carry which f<strong>al</strong>ls into the buck<strong>et</strong>.So we <strong>al</strong>ways expect that carry, and ignore it when it occurs.Figure A 1-8 shows another sample of subtraction using two's complement.1 !101 1 001 1 8 !1011 001 1!01 10 1100 - !1001 001 1 - + !1001 0100!0100 01 11Figure A1-8.Notice that it is <strong>al</strong>ways the number being subtracted which is changed toits two's complement form. !©1 1© 11©© is changed to !1©©1 ©©1 1, and thena 1 is added to g<strong>et</strong> !1©©1 ©1©©. This number is then added to !1©1 1 ©©1 1to g<strong>et</strong> !©1 ©© ©1 11, and as usu<strong>al</strong> we ignore the bit in the buck<strong>et</strong>.Negative AddressesThe two's complement essenti<strong>al</strong>ly changes a number to its negative equ iv<strong>al</strong>ent,so instead of subtracting a number you end up adding its negative.In a decim<strong>al</strong> s<strong>et</strong>ting you can think of it as changing the subtraction 9-3into the addition 9 + (-3) and the result is six in either case.The same idea l<strong>et</strong>s some addresses be written in negative form for convenience.When you wish to enter Monitor from BASIC you CALL theaddress -151. The true address for the Monitor entry point is 65385, and-151 is the two's complement of that address. If you convert 151 to its twobytebinary form (!©©©© ©©©© 1©©1 ©1 11) and take the two's complement(to account for the negative) you g<strong>et</strong> !1111 11_1 1 ©1 1© 1©©1 . The not-soquickconversion to decim<strong>al</strong> results in the expected v<strong>al</strong>ue of 65385. Youmust admit that -1 51 is much easier to remember that 65385.Another reason for using the two's complement, or negative, fo rm for


174 APPLE II COMPUTER GRAPHICSsome of the addresses is that the origin<strong>al</strong> language tor the Apple 11, IntegerBASIC, was incapable of handling a number greater than 32767. Thismeant that in order to CALL the 65385 address, you had to refer to itstwo's complement, -1 51 .You can convert any address to its negative equ iv<strong>al</strong>ent by either subtracting65536 from the address (the easy way) or by forming its two'scomplement (the hard way).At this point you have probably learned more than you ever wanted toknow about the hex and binary systems, but you have re<strong>al</strong>ly just scratchedthe surface. There are sever<strong>al</strong> interesting ties b<strong>et</strong>ween the topics we coveredhere and the fu ndament<strong>al</strong> <strong>al</strong>gorithms used by a <strong>computer</strong>. For example,a <strong>computer</strong> actu<strong>al</strong>ly figures the sum of two numbers by comparingcorresponding bits and using the logic<strong>al</strong> operations "AND" and " EOR"(described in the text). Pursuing these topics would take us beyond thescope of this text, but that info rmation may be found in a book on Booleanlogic.


(Lowercase)208 224 240$00 $E0 $F0p 0Q ! 1IfR2s # 3T $ 4u % 5v & 6'w7x ( 8y ) 9*z[ + ;' ' N '> N '> N? 0 - I ? 0 - I ? 0[Used with permission of Apple Computer Corporation]


176 APPLE II COMPUTER GRAPHICSTable A2-2. ASCII Character Codes.DEC is ASCII decim<strong>al</strong> codeHEX is ASCII hexadecim<strong>al</strong> codeCHAR is ASCII character namen/a = not accessible directly from the APPLE II keyboardDEC HEX CHAR WHAT TO TYPE DEC HEX CHAR WHAT TO TYPEl/J l/Jl/J NULL ctrl @ 48 31/J l/J l/J1 l/Jl SOH ctrl A31 1 12 l/J2 STX ctrl B Sl/J 32 2 23ETX ctrl C Sl 33 3 34 l/J4 ET ctrl D S2 34 4 4s I/JS ENQ ctrl E S3 3S s s6 l/J6 ACK ctrl F S4 36 6 67BEL ctrl G SS 37 7 78 l/J8 BS ctrl Hor- S6 38 8 89 l/J9 HT ctrl I S7 39 9 911/J l/JA LF ctrl J S8 3A11 l/JB VT ctrl K S9 3B12 I/JC FF ctrl L 61/J 3C < >1S l/JF SI ctrl 0 63 3F16 10 OLE ctrl P 64 40 @ @17 11 DC1 ctrl Q 6S 41 A A18 12 DC2 ctrl R 66 42 B B19 13 DC3 ctrl S 67 43 c c20 14 DC4 ctrl T 68 44 D D21 1S NAK ctrl U or - 69 4S E E22 16 SYN ctrl V46 F F23 17 ETB ctrl W 71 47 G G24 18 CAN ctrl X 72 48 H H2S 19 EM ctrl Y49 I I26 1A SUB ctrl Z4A J J27 1B ESCAPE ESC 7S 4B K K28 1C FS n/a4C L L29 1D GS ctrl shift-M 77 40 M M1E RS ctrl ' 78 4E N N31 1F us n/a4F 0 032 20 SPACE space 80 S0 p p33 21 ! 81 Sl Q Q34 22II82 S2 R R30l/J3l/J73S 23 # # 83 S3 s s36 24 $ $ 84 S4 T T37 2S % % 8S SS u u38 26 & & 86 S6 v v3927 87 S7 w w28 88 S8 x x41 29 89 S9 y y42 2A 90 SA z z2B + + 91 SB [ n/a44 2C 92 SC " n/a4S 20 93 SD l ] (shift-M)46 2E 94 SE2F 9S SF n/a404347497073747679(Used with permission of Apple Computer Corporation)


Appendix 3Memory MapsMemory Map of a 48K Apple IIFunctionAPPLE MONITORAPPLESOFTRESERVEDl/O DECODEDOSUNUSEDHI· RES PAGE 2HI· RES PAGE IUNUSEDTEXT/LO·RES PAGE 2TEXT/LO·RES PAGE IDOS VECTORSUNUSEDTEXT INPUT BUFFER6502 STACKZERO PAGEAddress$FSOO·SFFFF$EOOO·$F7FFSD000·SDFFFSCOOO·SCFFF$9600·$BFFF$6000·$95FFS4000·$5FFFS2000·$3FFFSC00·$1 FFF$800·$BFF$400·S7FFS3CO·S3FF$300·S3BF$200·$2FFSI00·$1FFSO·SFFFigure A3-1 .177


ZS$==e S - N · S - N S - N S - N - - - - - - - - - - N N N N N N N N N N ...>-a-arl'T'I=("'l0-ac:-tl'T'I::i:iC'\!:-a::t;:;V'l..cUICDa.:e"":::r"ClCD.,3<strong>ii</strong>i'UIc;·:s 'Tl-·Q... J:<strong>ii</strong> .,"ClCD"Cl J:<strong>ii</strong><strong>ii</strong>i wInQ !\)3"ClcC'tCD.,nQ.,"ClQ.,GIC'tc;·:s..$400 1024$480 1152$500 1280$580 1408$600 1536$680 1664$700 1792$780 1920$428 1064$4A8 1192$528 1320$5A8 1448$628 1576$6A8 1704$728 1832$7A8 1960$450 1104$400 1232$550 1360$500 1488$650 1616$600 1744$750 1872$700 2000


In each box:I 0 $ 00I 1024 Sll41lil...._..._. .. ... 2048 S081lil..._._ ... ... ... 3072 SllC ll- ... 4096 Sl ll- ... 5120 Sl41lil.. 6144 $1800I 7168 SIC ll>-i:i-i:iITIz0>.....00,,,."5·§:$2000§' $20800 $2100,,,.$2180a. $2200a. $2280 · $2300$2380§' $2028 $20A8$2128,,,. $21A8;:0$2228•,,,. $22A8;:$23280l$23A8$20500a.$2000·g· $21505·$21D0 $2250$2200$2350$2300819283208448857687048832896090888232836084888616874488729000912882728400852886568784891290409168!Sl=i!;;;;;;E;S - N M S - N M S - N M S - N M - - - - - - - - - - N N N N N N N N N N M M M M M M M M M M•Figure A3-3.[Used with permission of Apple Computer Corporation]


GlossaryA particular number that corresponds to each byte of mem­ADDRESS.ory.ANIMATE.To make an object seem <strong>al</strong>ive, to cause it to move about.ASCII. American Standard Code fo r Info rmation Interchange; <strong>al</strong>l thenumbers and l<strong>et</strong>ters used on a <strong>computer</strong> are assigned numeric v<strong>al</strong>ues.See page 138 of the APPLESOFT Reference Manu<strong>al</strong>.BINARY. A system of numeration having only two characters, (/) and 1.Any number may be represented using multiple digits. For example,decim<strong>al</strong> 3 is binary 11 and decim<strong>al</strong> 5 is binary 101. Binary fi les are fi lesstored in binary fo rm.BIT. The fu ndament<strong>al</strong> unit of memory in a <strong>computer</strong>. A bit can be in oneof two states-a high or low voltage. It is norm<strong>al</strong>ly represented as either1 or 0.BLOAD.The command to load a binary fi le from disk or tape.BOX METHOD. A m<strong>et</strong>hod of d<strong>et</strong>ecting collisions where you considerany shape entering a region around the targ<strong>et</strong> defi ned by a range of X­coordinates, and a range of Y-coord inates, to coll ide with a targ<strong>et</strong>.BSAVE.BUFFER.The command used to save a binary fi le on disk or tape.A temporary storage area.BYTE. A unit of info rmation in memory made up of eight bits. For example,in binary, 11 (/)1 01 11; in hexadecim<strong>al</strong> B7. A byte may contain anyv<strong>al</strong>ue b<strong>et</strong>ween (/) and 255 decim<strong>al</strong>, inclusive.BYTE-MOVE. An animation technique in which the digitized shape isstored in memory and then moved into Hi-Res memory fo r display.CALL. A BASIC statement similar to GOSUB, except it executes amachine-level routine as opposed to a BASIC routine.CLASHING. The result of trying to plot colors from diffe rent color groupstog<strong>et</strong>her in the same byte of memory. The results of clashing are unpredictablecolors.COLLISION.In animation, when one shape runs into another.COLLISION FLAG. A v<strong>al</strong>ue which is s<strong>et</strong> by your routine to sign<strong>al</strong> that acollision has occurred.COLOR BIT. The high-order (left-most) bit in a byte of the Hi-Res screenmemory. The color bit d<strong>et</strong>ermines to which of the two color groups thebyte belongs to.181


182 APPLE II COMPUTER GRAPHICSCOLOR GROUP. Bytes displayed on the Hi-Res screen belong to one oftwo color groups. Bytes in group 1 may display dots which are black,white, green, or viol<strong>et</strong>; bytes in group 2 may display black, white, blue,or orange.COLUMN ADDRESS. The - portion of the address of a byte on the <strong>graphics</strong>screen which must be d<strong>et</strong>ermined by that byte's position across thescreen.CONTACT METHOD. A m<strong>et</strong>hod for d<strong>et</strong>ecting collisions only when oneshape actu<strong>al</strong>ly contacts another, as opposed to the Box M<strong>et</strong>hod .COORDINATES, X and Y. The X-coordinate indicates a horizont<strong>al</strong> position,and the Y-coordinate indicates a vertic<strong>al</strong> position.DECIMAL. The norm<strong>al</strong> system of numeration which uses ten digits, (/)through 9.DIGITIZE. To create a series of data v<strong>al</strong>ues for a figure or other nonnumericitem.DITHERING. A technique which places sever<strong>al</strong> of the the six elementaryApple Hi-Res colors in a pattern so it produces non-standard colors.DOS. An acronym for disk operating system, the controlling programsthat make your disks behave properly (usu<strong>al</strong>ly).DRAW. An APPLESOFT command which draws a shape on the Hi-Resscreen.EDITOR. A program to simplify creating, <strong>al</strong>tering, and saving either textor <strong>graphics</strong>.EXCLUSIVE OR (EOR). A Boolea n (binary) operation which comparestwo bits and results in an ON state if either of the compared bits are ONbut not if both are ON.EXEC. A DOS command which causes the <strong>computer</strong> to process the contentsof a text fi le in the same way it usu<strong>al</strong>ly handles keyboard input.FLICKER. Flash on and off quickly. Flicker is the bane of animation programmers.FULL-SCREEN GRAPHICS.text at the bottom.The <strong>graphics</strong> screens without four lines ofHEX or HEXADECIMAL. Referring to hexadecim<strong>al</strong>; a system of numerationusing 16 diffe rent characters for digits: (/)through 9 and A through F.Hex numbers are convention<strong>al</strong>ly preceded by a "$" to diffe rentiat<strong>et</strong>hem from other types of numbers. $A is decim<strong>al</strong> 1 f/J, $B is decim<strong>al</strong> 11,and so forth.HIMEM. The v<strong>al</strong>ue which represents the top of user-available memory.The APPLESOFT variables use HIMEM as a starting point.


GLOSSARY 183HI-RES. High Resolution <strong>graphics</strong>. In Hi-Res <strong>graphics</strong>, <strong>al</strong>l objects displayedare made up of a number of sm<strong>al</strong>l dots on the screen.HI-RES PAG ES. The area in memory which may be displayed as pictureson the video output.HI-RES ROUTINE. A program designed to manipu late the contents ofthe Hi-Res pages of the Apple memory.HI-ORDER BYTE. In an address, the most significant (left-most) byte. Forexample, in the hex address $A3©F, A3 is the hi-order byte (hi-byte).HUN.A BASIC command used to draw a horizont<strong>al</strong> line in Low-Res.LO-BYTE/HI-BYTE. The form Apple uses for hex addresses; the loworderbyte comes before the hi-order byte.LOW-RES. A <strong>graphics</strong> mode of the Apple II where the video output is anumber of sm<strong>al</strong>l colored blocks.LOW-ORDER BYTE. In an address, the least significant (right-most) byte.For example, in the hex address $A3©F, ©F is the low-order byte (lobyte).MEMORY.MEMORY MAP.Data storage area within the <strong>computer</strong>.MIXED SCREEN GRAPHICS.at the bottom.A diagram of the <strong>computer</strong>' s memory.The <strong>graphics</strong> screen with four lines of textMONITOR. The ROM resident machine-level language in your Apple. Ifwritten without the capit<strong>al</strong> "M," the video display.MOVE.NIBBLE/NYBBLE.byte.A command available from Monitor to move a block of memory.Either of the first or last four bits in a byte-h<strong>al</strong>f of aOFFSET. A v<strong>al</strong>ue added to the base address to locate a byte with respectto that base address.OPTIMIST.A programmer who codes in pen.PAGE. A unit of memory. 256 bytes of memory make up one page.When used in the context of <strong>graphics</strong> display, a page refers not to 256bytes, but to the area of memory which is to be displayed on the videoscreen.PARTIAL MODIFICATION. An animation technique where only the portionsof the figure which change are redrawn. The rest are left <strong>al</strong>one.PEEK. A BASIC command which looks at a specified address in memoryand r<strong>et</strong>urns the v<strong>al</strong>ue that it finds there.PIXEL.The sm<strong>al</strong>lest unit of video display controllable by the <strong>computer</strong>.


184 APPLE II COMPUTER GRAPHICSPLOT.To put som<strong>et</strong>hing on the screen.POINT OF ORIGIN.begin.The point within a shape where the defining vectorsPOKE. A BASIC command which puts a specified v<strong>al</strong>ue into a givenlocation.POSITION ADDRESS. The portion of the address for a byte on the Hi­Res screen which is given by that byte's position within its box. See theHi-Res memory map.PRE-CALCULATION. A m<strong>et</strong>hod for reducing flicker. All the c<strong>al</strong>culationsneeded for the animation are done before the figure begins to move. Theresults are stored in a table for reference.PRE-SHIFT. (1) The result of DRAWing the shape once, shifting theshape in any direction, and then XDRAWing over the origin<strong>al</strong>. (2) A wayof animating a shape by XDRAWing the necessary changes on top of it.PRIMARY PAGE.The first of two pages of either <strong>graphics</strong> or text.PROMPT CHARACTER. The character the Apple prints on your screen toremind you which language you are speaking to. Either APPLESOFT (]),Integer BASIC (>), or Monitor ( * ) .QUOTIENT.The result of a division.REMAINDER. The result of subtracting the product of the divisor and theinteger quotient from the number origin<strong>al</strong>ly divided . Loosely (and muchmore understandably), what is left over after an integer division.RAM. An acronym for random access memory. RAM may be <strong>al</strong>tered bythe program.ROM. An acronym for read only memory. ROM cannot be changed bysoftware-you cannot write to it, you may only read it.ROM RESIDENT.Som<strong>et</strong>hing stored in ROM; therefore, it is p_ermanent.ROT. BASIC's Hi-Res shape rotation command. A shape rotates about itspoint of origin.ROUTINE.A program designed to accomplish a particular task.ROW ADDRESS. A portion of a <strong>graphics</strong> byte's address given by the rowwhich the byte's box occupies on the memory map.SCALE.A BASIC command used to enlarge shapes.SECONDARY PAG E.The second of two pages of either <strong>graphics</strong> or text.SECTOR. A bloc k of storage on a disk. One sector holds 256 bytes ofdata. DOS <strong>al</strong>lows for 496 sectors to be available to the user.


GLOSSARY 185SEPARATION. Any one of the seven versions of a figure required tomove a byte-move figure horizont<strong>al</strong>ly. Each separation is shifted acrossone screen dot from the previous one.SHAPE. SHAPE is an Apple <strong>graphics</strong> construct. Any figure on the screencan be digitized so that it will operate with the APPLESOFT DRAW,XDRAW, ROT, and SCALE commands. Digitizing the shape can be acomplex process, but a Hi-Res editor takes care of <strong>al</strong>l the grubby d<strong>et</strong>ailsso that creating shapes is a quick, simple task.SHAPE TABLE. A number of shapes strung tog<strong>et</strong>her. Again, a Hi-Reseditor makes creating the shape tables a trivi<strong>al</strong> exercise.SHLOAD.A command to load a shape table from tape.SOFT SWITCH. A memory location which controls som<strong>et</strong>hing. You mays<strong>et</strong> and interchange output modes using soft switches.TWO'S COMPLEMENT. A binary number formed from another by interchangingones and zeros and then adding one.USR. A BASIC command which passes a v<strong>al</strong>ue to a machine languageroutine. The starting address for the routine must be placed in locations$


IndexAddition, 168-1 7 2Address(es), negative, 173-1 74Addressing, 6-7ALTC HARSET, 36, 37Animation, 13Sbyte-move, see Byte-move processflicker in, 1 SSAPPLE EXC., S7, S8Apple screen characters, 17 StAPPLE TEXT, use, 112, 113APPLESOFT extensions, 21-30ASC II character codes, 32, 176tBar graph, creating of, 119-1 2 3BAS IC, memory move, S9f-60fBinary, 3, 163ad ding and subtracting, 170-1 72conversion of decim<strong>al</strong> to, 166-1 67conversion to decim<strong>al</strong>, 166difficulties in, 7Binary/ hex chart, 8f, 9Binary/ hex examples, 9, 9fBinary numberconversion of decim<strong>al</strong> to, 1 68writing of, 9Binary nibble, 9Bit(s) , 4, S, 171color, 82-84, 92, 96co mbinations, Sf, S, 6Hi-Res, 68, 69dot correspondence to, 69fpatterns, 6v<strong>al</strong>ue of, S-6BLOAD command, S 1 , 116Blocks, plotting and erasing, 48-49BSAVE command, use, S1, S3, 106, 107Byte(s), 6, 171ad dress of, 6-7divisions, for vector encoding, 102f,102, 103-1 04, 10Sv<strong>al</strong>ue of, 8, 9, 162changing of, 14, 1 SByte-move process, 13S-1 36, 1 SOanimation by, 136-1 38<strong>al</strong>tering of, 138-1 39for horizont<strong>al</strong> animation, 1 39-142two-byte animation, 142-1 4SByte-move shapes, pre-shifting with, 1 S4C<strong>al</strong>culation, 149-1 S1CALL command, use, 21, 24-26in memory move, 60Character codesApple screen, 1 7StASCII, 176tCircle graphs, 129-1 32Circuits, 4Clashing, 9S-9680 COL, 36Collision, d<strong>et</strong>ecting of, 1S7, 1 S8-1 64Colon, use, 1 SColorHi-Res, 81-96selection of, 123Color bit, 92, 96for Hi-Res <strong>graphics</strong>, 82-84fColor codes, Low- Res, 44fCommand(s), 2, 19Command extensions, 21Complements, forming of, 172-1 73Computer, physiology, 3-1 1CTRL-C, 6, 27, 1 S9Cursormovement, so, sotpositioning of, 123Data, SSdisplaying of, 119Data file, ss-s 7Data processing, 119DATA statements, 119, 137Decim<strong>al</strong>, 3conversion of, to hex, 167-1 68convers ion to, 9, 10of binary, 166-1 67, 168of numbers, 16S-1 66Digit(s), position within numbers ' 16SDigit<strong>al</strong>, digressing to, 4Digitizing, 99, 100, 109, 116Dithering, 81, 93, 943DOG command, 19DOS, 1S, 16, 18Dot(s)Hi-Res, 6S, 67, 68, 69-78double dot picture, 92, 92fquantity available, 91-93isolating of, 162patterns, POKEing of, 149plotting of, 109-1 1 Oprocessing of, 90shape plotting and, 100, 101, 103Draw, use of, 10S-1 06DRAW command, 1 08, 113, 114, 116, 13S186


Editoi:(s)Hi-Res, 66, 100Low- Res, 48, 49-50EOR operation, 154, 174Equipment, 1EXEC, use, 57-58Figure(s)digitizing of, 99, 100saving of, 50-51Filename, 56, 57FOR- N EXT loop, 54, 128Function<strong>al</strong> plotting, 128-1 29Glossary, 181-1 85GOTO statement, 28, 29, 158Graph, graphingcircle graphs, 129-1 32drawing, 128-1 29Graphics, speed and efficiency, 14 7Graphics mode, 31-33, 35, 35fGraphics switch, s<strong>et</strong>ting of, 34HCOLOR, 92, 106, 116Hex, see Hexadecim<strong>al</strong> systemHexadecim<strong>al</strong> digit, conversion to number,166Hexadecim<strong>al</strong> numberconversion to decim<strong>al</strong>, 165writing of, 9Hexadecim<strong>al</strong> system, 3, 7-8adding and subtracting, 168-1 70binary/hex chart, 8, 9fbinary/hex-examples, 9, 9fconversion of decim<strong>al</strong> to, 167-1 68conversion to decim<strong>al</strong>, 9-1 0digits, 9Hi-Res color, 91, 96clarification, 90-91'color bit, 82-84colors available, 93-96dots available, 91-93positions possible, 91program, 84-90Hi-Res <strong>graphics</strong>, 62, 106display, 68dot pattern, 65, 69-78memory, 6668, 161use, 65Hi-Res <strong>graphics</strong> editor, 66, 100Hi-Res memory map, 69, ?OfHi-Res mode, 31, 32-33, 35fHi-Res screen, 111clearing of, 73dots avai lable, 91-93positions avai lable, 90text placement on, 112-1 13use. 164HIMEN, 59, 60, 168, 169s<strong>et</strong>ting of, 107, 108HUN command, use, 21, 46, 47f, 47Horizont<strong>al</strong> animation, 140-1 42Horizont<strong>al</strong> axis, labeling of, 128Horizont<strong>al</strong> graph, 121Horizont<strong>al</strong> pre-shift, 152Li ne graph, drawi ng of, 123-1 28Loop, checking of bits, 162Low- Res color codes, 44fLow- Res editor, 48, 49-50Low- Res <strong>graphics</strong>, 62creating pictures, 44-46use, 43-44memory, 53, 54, 58Low- Res memory map, 41 fLow- Res mode, 31, 32, 35f, 39Low- Res scan, 54f, 55, 55fLow- Res screenmemory, 58placing text labels on, 121Memory, 4, 13, 161changing of, 1 5de<strong>al</strong>ing with, 48-50examining of, 14-1 5Hi-Res, 66-68, 161interpr<strong>et</strong>ation of data, 31Low- Res, 53, 54, 58moving of, 17-1 8, 58-61organization of, 15-1 6scanning of, 53-5 5size, 7writing from, 1 6-1 7Memory ad d ress, 7Memory map, 16, 16f, 18, 177f- 179fHi-Res, 69, 80fLow- Res, 41fuse, 40-4 1 , 41 fMemory move, BAS IC, 59f-60fModification, parti<strong>al</strong>, 147-1 49Monitor, 13-14, 15commands, 19use, 14-1 5, 73MOVE command, use, 17, 18, 59Negative address(es), 173-1 74Nibble, 9Number, digit position within, 165"Number crunching," 119Number table, 123, 124Numeric codes, assigning of, 32Offs<strong>et</strong>, 105"P" key, 27, 50Paddle 0, USj!, 154-1 55, 159PEEK function, 34, 37187


PEEK statement, use, 13, 21-23, 59, 161flictu resLow- Res, 44-46preserving of, 53-63Plot, hatching of, 46-48PLOT command, use, 21, 46, 47, 48Plotting, function<strong>al</strong>, 1 28-1 29PO KE command, use, 13, 21, 23-24, 25,99, 108PO KE function, 34, 37, 99, 138, 149, 151Power light, 4, 6Pre-c<strong>al</strong>culation, 149-1 51Pre-shifting, 141-1 54PRINT PEEK command, 22, 59"PRINT USR(C) " statement, 27-28Prompt characters, 14RAM, 4, 5<strong>al</strong>tering of bytes, 14, 15erasure from, 5READ command, 51, 99, 108ROM, 4, 5, 13, 14ROT, use, 108-1 09, 110SCALE, s<strong>et</strong>ting of, 106, 108Sc<strong>al</strong>ing factor (SCL) , 122, 127Scan, Low- Res, 54, 55SCL, see Sc<strong>al</strong>ing factorScreenclearing of, 16, 17shape generation on, 101SHAPE, use, 99Shape( s)byte-move, 154creating and placing of, 100, 1 09-1 15designing and digitizing of, 100, 116drawing of, 100-1 03, 105finding of, 105plotting of, 99UFO's, 103-1 04use of DRAW, 105-1 06use of ROT, 108-1 09, 110use of XDRAW, 115-1 16Shape commands, 135Shape table, 104-1 05, 111, 123, 127,158, 168enteringand saving, 1 06-1 08, 1 23-1 24location of, 105-1 06, 111shape drawing with, 109, 110Shifting, 151-1 54SH LOAD command, use, 108, 116Soft switches, 33-34add resses, 35ffor display control, 36-3 7toggling, 33, 34Sound assembly, 61-62Subtraction, 168-1 72Tape, saying of, 107-1 08Text, placing of, 112-1 13, 121Text modes, 31-32, 35fmemory map, 40-4 1use, 39-40, 41-42, 43Text screens, 31, 3 2 ·Toggle switches, 33, 33fTrigonom<strong>et</strong>ry, circle drawing with, 131UFO's, drawing of, 103-1 04USR statement, use, 26-29, 31Vector(s) , 135· digitizing of, 109shape drawing with, 100, 103, 135encoding of, 101-103Vector codes, 101 fplacing of, 102Vertic<strong>al</strong> add ress, 42Vertic<strong>al</strong> blanking, 155Vertic<strong>al</strong> Blanking Location (VBL) , 155VLIN command, use, 46, 47f, 47, 48Voltage, testing for, 4XDRAW command, 135, 152, 153, 154,155, H9use, 11 5-1 1 6188

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!