Inside the Apple 1/c Gary B. Little Brady Communications Company, Inc. A Simon & Schuster Publishing Company _ New York, NY 10020 Inside the Apple lie. Copyright© 1985 by Brady Communications Company, Inc. All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electroniC or mechanical, inch.).ding photocopying and recording, or by any information storage and retrieval system, without permission in writing from the publisher. For information, address Brady Communications Com pany, Inc., A Simon & Schuster Publishing Company, 1230 Avenue of the Americas, New York, NY 10020. Library of Congress Cataloging in Publication Data Little, Gary B., 1954- Inside the Apple lie. On t.p.IIc appears as //c. Includes bibliographies and index. 1. Apple lie (Computer) I. Title: Inside the Apple 2c. II. Title: Inside the Apple two c. III. Title. QA76.8.A66225L57 1985 001.64 84~27419 ISBN 0-89303-564-5 Printed ii1 the United States of America 85 86 87 88 89 90 91 92 93 94 95 2 3 4 5 6 7 8 9 10 Production Editor/Text Designer: Michael J. Rogers Art Director: Don Sellers Assistant Art Director: Bernard Vervin Cover Photography: George Dodson Manufacturing Director: John A. Komsa Copy Editor: Rita Progler Typesetting: Automated Graphic Systems, White Plains, MD Printing: R. R. Donnelley & Sons Co., Harrisonburg, VA Typefaces: Helvetica (display), Aster (text), and Universal Mono type #3 H-P (computer programs) To my wife, Pamela Limits of Liability and Disclaimer of Warranty The author and publisher of this book have used their best efforts in pre paring this book and the programs contained in it. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. Note to Authors Have you written a book related to personal computers? Do you have an idea for developing such a project? If so, we would like to hear from you. Brady produces a complete range of books for the personal computer market. We invite you to write to Editorial Dept., Brady Communications Co., A Simon & Schuster Publishing Company, 1230 Avenue of the Americas, New York, NY 10020. Trademarks of Material Mentioned in This Text Apple //e, Applesoft, Apple II, Apple II Plus, Apple 1/c, Apple I, Integer BASIC, DOS 3.3, Lisa, Macintosh, and ProDOS are trademarks of Apple Computer, Inc. Contents Preface I xlll 1 An Introduction to Apple and the Apple //c I 1 A Condensed History of Apple Computer, Inc. I 1 Hardware and the Apple 1/c I 7 Learning the Fundamentals I 8 What Won't Be Covered I 9 Using the Optional Diskette I 9 Further Reading for Chapter 1 I 18 2 The 65C82 Microprocessor I 11 Important 65C02 Concepts I 12 Zero Page and the Stack I 12 65C02 Instruction Set I 13 65C02 Registers I 21 The Accumulator-A I 21 The Index Registers-X andY I 22 The Processor Status Register-P I 23 Carry Flag (C) I 23 Zero Flag (Z) I 24 Interrupt Disable Flag (I) I 24 Decimal Mode Flag (D) I 25 Break Flag (B) I 25 Overflow Flag (V) I 25 Negative Flag (N) I 26 The Stack Pointer-S I 26 The Program Counter-PC I 27 65C02 Addressing Modes I 27 Immediate I 28 Absolute I 29 Accumulator I 38 Implied I 38 Zero-Page Indexed Indirect I 38 Zero-Page Indirect I 31 Indirect Indexed I 31 Absolute Indexed I 32 Relative I 32 Absolute Indirect I 33 Absolute Indexed Indirect I 33 65C02 Input/Output Handling I 33 65C02 Interrupts I 34 Reset Interrupt I 36 v c=J vi lnsidetheApple//c ---------------- Interrupt Request (IRQ) I 36 The BRK Instruction I 37 The 65C02 Memory Space on the llc I 38 RAM Memory I 38 Input/Output (IIO) Memory I 41 ROM Memory I 42 Further Reading for Chapter 2 I 42 3 The System Monitor I 45 The System Monitor Commands I 46 The DISPLAY Command : Displaying the Contents of Memory I 46 The STORE Command : Changing the Contents of M;emory I 49 The MOVE Command: Copying the Contents of Memory I 51 The VERIFY Command : Comparing Ranges of Memory I 53 The EXAMINE Command : Examining the 65C02 's Registers I 53 The GO Command : Running a Program I 54 The LIST Command: Disassembling Assembly-Language Programs I 55 The NORMAL and INVERSE Commands: Changing Video Display Modes I 57 The ADD and SUBTRACT Commands: Simple Arithmetic I 57 The BASIC and CONTINUE BASIC Commands : Entering Applesoft I 57 The USER Command: User-Defined Command I 58 The KEYBOARD and PRINTER Commands : Redirecting Input and Output I 59 Multiple Commands on One Line I 61 System Monitor Subroutines I 61 Further Reading for Chapter 3 I 65 4 Applesoft BASIC I 67 Applesoft Memory Map I 68 Tokenization of Applesoft Programs I 73 Keyword Tokens I 74 Storage of Applesoft Variables I 77 Storage of Simple Variables I 78 The Name Header I 79 The Data Field I 80 End of Simple Variables I 82 Storage of Array Variables I 83 The Name Header I 83 Dimensioning Bytes I 83 The Data Field I 85 End of Array Variables I 85 Representation of Integer Numbers I 85 Representation of Real Numbers I 87 c:=J ----------------------.., Contents vii Number Theory I 87 Binary Floating-Point Format I 87 How an Applesoft Program Runs I 89 'The CHARGET Subroutine I 91 Changing Program Flow I 93 Finding Line Numbers I 93 Linking Applesoft to Assembly-Language Programs I ,94 The CALL Command I 95 The & Command / 95 The USR Function I 96 Applesoft's Built-In Subroutines I 97 Using Applesoft's Built-ln Subroutines I 104 Locating Variables I 104 Evaluating Formulas I 108 Converting Numbers I 108 Further Reading for Chapter 4 I 112 5 The ProDOS Disk Operating System I 115 Formatting Diskettes I 116 ProDOS Memory Map I 116 :ProDOS Page 3 Vectors I 118 Filenames and Pathnames I 118 BASIC.SYSTEM Commands I 121 File Management Commands I 122 File Loading and Execution Commands I 123 File Input/Output Commands I 124 Miscellaneous Commands I 125 ProDOS File Storage I 126 Volume Bit Map I 126 Diskette Directory I 127 "Protecting" Files I 130 Storing File Data I 131 MLI-Accessing the Diskette Directly I 132 READ.BLOCK Program I 134 Further Reading for Chapter 5 I 140 6 Character Input and the Keyboard I 141 Standard Character Input Subroutines I 145 Reading One Character I 146 RDKEY ($FD0C) I 146 Keyboard Input I 148 Escape Sequences I 148 RDCHAR ($FD35) and ESCRDKEY ($CCED) I 150 Readiu'g a· Line of Characters I 150 Changing Input Devices :The Input Link I 152 How About Output? I 153 viii [=:J Inside the Apple //c --------------- Designing a KSW Input Subroutine I 153 Replacing the Keyboard Input Subroutine I 153 ProDOS and the Input Link I 154 The Keyboard I 157 Encoding of Keyboard Characters I 157 Special Keys I 159 The "Apple" Keys I 159 Keyboard 110 Locations I 168 Modifying the Keyboard Input Subroutine I 164 Keyboard Auto-Repeat I 168 Resetting the Apple llc I 173 Special RESET Procedure I 173 Trapping "Soft" RESETs I 174 Trapping RESET from Assembly Language I 175 Trapping RESET from Applesoft I 176 Further Reading for Chapter 6 I 181 7 Character and Graphic Output and VIdeo Display Modes I 183 Text Mode I 184 The 80140 Switch I 185 Turning on the Text Display I 185 Text Mode Memory Mapping I 188 40-Column Text Mode I 189 80-Column Text Mode I 191 Using Page2 of Text I 192 Video Display Attributes: Normal, Inverse, Flash I 194 MouseText I 196 Standard Character Output Subroutines I 199 Video Output I 288 Video Screen Windowing I 282 How COUTl and C3COUT1 Set the Video Attribute I 283 Changing Output Devices : The OUTPUT Link I 285 Designing a CSW Output Subroutine I 206 Replacing the Video Output Subroutine I 206 ProDOS and the Output Link I 286 Low-Resolution Graphics Mode I 287 Turning on the Low-Resolution Graphics Display I 208 Low-Resolution Graphics Screen Memory Mapping I 209 Low-Resolution Graphics Colors I 209 Double-Width Low-Resolution Graphics I 210 Turning on Double-Width Low-Resolution Graphics I 210 Double~ Width Low-Resolution Graphics Screen Memory Mapping I 212 Double-Width Low-Resolution Graphics Colors I 212 Built-In Support for Low-Resolution Graphics I 213 ' High-Resolution Graphics Mode I 214 c::=J --------------------- Contents ix Turning on the High-Resolution Graphics Display I 215 High-Resolution Graphics Screen Memory Mapping I 217 High-Resolution Graphics Colors I 219 Animation with High-Resolution Graphics I 220 Double-Width High-Resolution Graphics I 221 Turning on Double-Width High-Resolution Graphics I 222 Double-Width High-Resolution Graphics Screen Memory Mapping I 222 Double-Width High-Resolution Graphics Colors I 223 Built-In Support for High-Resolution Graphics I 223 Further Reading for Chapter 7 I 225 8 Memory Management I 229 16K Bank-Switched RAM Areas I 230 Using Bank-Switched RAM I 231 Reading the Status of Bank-Switched RAM Soft Switches I 232 Auxiliary Bank-Switched RAM I 234 Playing with Bank-Switched RAM I 235 Bank-Switched RAM and ProDOS I 236 Auxiliary RAM Memory Area I 236 Using Auxiliary Memory I 237 The ALTZP Switch I 237 The RAMRD and RAMWRT Switches I 239 Auxiliary Memory Support Subroutines I 241 AUXMOVE ($C311)-Transferring data to and from auxiliary memory I 241 XFER ($C314)-Transferring control to a program from main or auxiliary memory I 244 Running Co-Resident Programs I 245 Initialization of the Auxiliary Stack I 251 Using CONCURRENT I 251 Limitations of CONCURRENT I 252 Further Reading for Chapter 8 I 253 9 The Speaker I 255 Generating Musical Notes I 255 Generating Music I 259 Further Reading for Chapter 9 I 263 10 Mouse and Game Controller Input I 265 The Apple Mouse I 265 How the Mouse Works I 266 Mouse Operating Modes I 267 Passive (Transparent) Mode I 267 Movement Interrupt Mode I 268 Button Interrupt Mode I 268
Description: