Apple He Technical Reference Manual

Includes ROM Listings for Memory Expandable He

> $^^.^s fpt

USA

^ple® Technical Library Titles for the Apple He and lie

The Official Publications from Apple Computer, Inc.

Apple He and Apple lie programmers, developers, and enthusiasts will find a wealth of information in the Apple Technical Library, an ongoing series of comprehensive reference manuals. The first volumes in the Library contained detailed information about the Apple He and Apple He computers. They describe the hardware, firmware, the ProDOS 8 operating sys- tem, and the Applesoft BASIC programming lan- guage found in Apple lie and He computers.

These books, written and produced by Apple Computer, Inc., provide definitive references for diose interested in getting the most out of their Apple He or He.

Apple Technical Library Titles for the Apple He and He include:

Apple He Technical Reference Apple He Technical Reference Applesoft Tlitorial Applesoft BASIC Programmer's Reference

Manual ProDOS 8 Technical Reference BASIC Programming with ProDOS Apple Numerics Manual ImageWriter II Technical Reference

Manual

Apple^ II Apple lie Technical

Reference Manual

A TT

Addison- Wesley Publishing Company, Inc.

Reading, Massachusetts Menlo Park, California Don Mills, Ontario Wokingham, England Amsterdam Bonn Sydney Singapore Tokyo Madrid Bogota Santiago San Juan

# APPLE COMPUTER, INC.

Copyright © 1984, 1986 by- Apple Computer, Inc.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of Apple Computer, Inc. Printed in the United States of America.

Apple, the Apple logo, ProDOS, and LaserWriter are registered trademarks of Apple Computer, Inc.

Macintosh is a trademark of Apple Computer, Inc.

Microsoft is a registered trade- mark of Microsoft Corporation.

POSTSCRIPT is a trademark of Adobe Systems Incorporated.

ITC Garamond, ITC Avant Garde Gothic, and ITC Zapf Dingbats are registered trademarks of International Typeface Corporation.

Simultaneously published in the United States and Canada.

ISBN 0-201-17752-8 ABCDEFGHIJ-DO-89876 First printing, March 1987

WARRANTY INFORMATION

ALL LMPUED WARRANTIES ON THIS MANUAL, INCLUDING LMPUED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE LIMITED IN DURATION TO NINETY (90) DAYS FROM THE DATE OF THE ORIGINAL RETAIL PURCHASE OF THIS PRODUCT.

Even though Apple has reviewed this manual, APPLE MAKES NO WARRANTY OR REPRESENTA- TION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS MANUAL, ITS QUALITY, ACCURACY, MERCHANTABIUTY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS MANUAL IS SOLD "AS IS," AND YOU, THE PURCHASER, ARE ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND ACCURACY.

IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT, INDIRECT, SPECLVL, INCIDENTAL, OR CONSEQUEmiAL DAMAGES RESULTING FROM ANY DEFECT OR INACCURACY IN THIS MANUAL, even if advised of the possibility of such damages.

THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLU- SIVE AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR IMPUED. No Apple dealer, agent, or employee is authorized to make any modifica- tion, extension, or. addition to this warranty.

Some states do no allow the exclu- sion or limitation of implied warran- ties or liability for incidental or consequential damages, so the above limitation or exclusion may not apply to you. This warranty gives you specific legal rights, and you may also have other rights which vary from state to state.

Contents

Figures and tables xiv

Preface About This iVianual xxi

Contents of this manual xxi

The Apple lie family xxiii

Identifying your Apple lie xxiii The original Apple lie xxiv The UniDisk 3.5 Apple lie xxiv The memory expansion Apple lie xxiv

Conventions used in this manual xxv

Chapter 1 introduction 1

The outside of the machine 2

The keyboard 3 Features 3

Special function keys 4 Cursor movement keys 4 Modifier keys 5 The 80/40 switch 5 The keyboard switch 6 Disk-use and power lights 7

The speaker 8

The built-in disk drive 8

The back panel 9 The inside of the machine 1 1

The internal voltage converter 11

The main logic board 12

The other circuit boards 15

Chapter 2 Memory Organization and Control 17

The 65C02 microprocessor 18

Overview of the address space 20 Memory map and memory switching 20

Main RAM addresses C$0000-$BFFF and $DOOO-$FFFF) 22

Auxiliary RAM addresses (SOOOO-BFFF and $DOOO-$FFFF) 22

ROM addresses ($C100-$FFFF) 22

Hardware addresses C$COOO-SCOFF) 23 Bank-switched memory 24

Page allocations 26

Page $00 (one -byte addresses) 26 Page $01 (the 65C02 stack) 26 Pages $D0-$FF (ROM and RAM) 26

Using bank selector switches 27 48K memory 36

Page allocations 36

Page $02 (the input buffer) 36

Page $03 (global storage and vectors) 36

Pages $04-$07 (text and low-resolution Page 1) 36

Pages $08-$0B (text and low-resolution Page 2) 38

Pages $08 (communication port buffers) 38

Pages $20-$3F (high-resolution Page 1) 38

Pages $40-$5F (high-resolution Page 2) 39

Using 48K memory switches 39

Transfers between main and auxiliary memory 42 Transferring data 42 Transferring control 43

Using display memory switches 44 The reset routine 49

The cold-start procedure (power on) 51

The warm-start procedure (Control-Reset) 51

Forced cold start (Open Apple-Control-Reset) 52

The reset vector 52

Chapters introduction to Apple lie I/O 55

The standard I/O links 56 Standard input features 58 RdKey subroutine 58 Keyin subroutine 58 GetLn subroutine 59 Escape codes with GetLn 60 Editing with GetLn 63

Cancel line 63

Backspace 63

Retype 63

Iv Contents

Standard output features 64

COut subroutine 64

Control characters with COutl 65

Control characters with C3COutl 65

The stop-list feature 67

The text window 68

Normal, inverse, and flashing text 69 Primary character set display 70 Alternate character set display 70 Port I/O 71

Standard link entry points 71

Firmware protocol 72

Port I/O space 73

Port ROM space 73

Expansion ROM space 74

Port screen hole RAM space 74 Interrupts 75

Chapter 4 Keyboard and Speaker 77

Keyboard input 78

Reading the keyboard 78

Monitor firmware support for keyboard input 82 Speaker output 82

Using the speaker 83

Monitor firmware support for speaker output 84

Chapters Video Display Output 85

Video display specifications 87 Text modes 88

Text character sets 88

MouseText 90

40-column versus 80-column text 91 Graphics modes 94

Low-resolution graphics 94

High-resolution graphics 95

Double high-resolution graphics 97

Mixed-mode displays 98

Display pages 99

Display mode switching 101

Display page maps 105

Monitor support for video display output 112

I/O firmware support for video display output ll6

Contents

Chapter 6 Block Device I/O 119

Disk drive I/O 120 Startup 121

Cold start 121 Warm start 123 Memory expansion card I/O 123 The Smartport I/O interface 123 Locating the Smartport 124 Issuing a call to the Smartport 125

Cautions 126 Descriptions of the Smartport calls 126 STATUS 128

Parameter descriptions 128 Possible errors 132 READ BLOCK 132

Parameter descriptions 133 Possible errors 133 WRITE BLOCK 134

Parameter descriptions 134 Possible errors 135 FORMAT 135

Parameter descriptions 135 Possible errors 136 CONTROL 136

Parameter descriptions 136 Possible errors 139 INIT 139

Parameter descriptions 140 Possible errors 140 OPEN 140

Parameter descriptions 140 Possible errors 141 CLOSE 141

Parameter descriptions 141 Possible errors 142 READ 142

Parameter descriptions 142 Possible errors 143 WRITE 143

Parameter descriptions 144

Possible errors 144

An example: issuing a Smartport call 145

Summary of commands and parameters 149

Summary of error codes 150

vi Contents

Chapter? Serial I/O Port 1 153

Using serial port 1 155 Characteristics of port 1 at startup 159 Hardware page locations for port 1 159 I/O firmware support for port 1 l60 Screen hole locations for port 1 l60 Changing port 1 characteristics l6l Data format and baud rate l63 Carriage return and line feed l64 Sending special characters l65 Displaying output on the screen l65

Chapters Serial I/O Port 2 167

Using serial port 2 l69 Characteristics of port 2 at startup 173 Hardware page locations for port 2 173 I/O firmware support for port 2 174 Screen hole locations for port 2 174 Changing port 2 characteristics 176 Data format and baud rate 177 Carriage return and line feed 179 Routing input and output 179 Half-duplex operation 180 Full-duplex operation 182 Terminal mode 184

Chapter 9 Mouse and Game Input 185

Mouse input 186

Jvlouse connector signals 187

Mouse operating modes 187 Transparent mode 187 Movement interrupt mode 187 Button interrupt mode 188 Movement/button interrupt mode 188 Vertical blanking active modes 188

Mouse soft switches 189

I/O firmware support for mouse input 191 Pascal support 195 BASIC and assembly-language support

Screen holes 196

Using the mouse as a hand controller 198

195

Contents

VII

Game input 198

The hand controller connector signals 199 Switch inputs (SwO and Swl) 200 Analog inputs (PdlO and Pdll) 200

Monitor support for game input 201

Chapter 10 Using the Monitor 203

Invoking the Monitor 204

Syntax of Monitor commands 205

Monitor memory commands 205 Examining memory contents 206 Memory dump 206 Changing memory contents 208 Changing one byte 208 Changing consecutive locations 209 Moving data in memory 210 Comparing data in memory 211

Monitor register commands 212 Changing registers 213 Examining registers 213

Miscellaneous Monitor commands 213 Display inverse and normal 214 Back to BASIC 214 Redirecting input and output 215 Hexadecimal arithmetic 215

Advanced operations 2l6

Multiple-command lines 2l6 Filling memory 2l6 Repeating commands 217 Creating your own commands 218

Machine-language programs 219 Running a program 219 Disassembled programs 220

The STEP and TRACE commands 221

The Mini-Assembler 223

Starting the Mini-Assembler 223 Using the Mini-Assembler 224 Mini-Assembler instruction formats 226

Summary of Monitor commands 227 Examining memory 227 Changing the contents of memory 227 Moving and comparing 227 The Register command 228 Miscellaneous Monitor commands 228 Running and listing programs 229

viii Contents

Chapter 11 Hardware Implementation 231

Environmental specifications 232

Power requirements 233

The external power supply 233 The external power connector 234 The internal converter 234

Apple lie overall block diagram 235

The 65C02 microprocessor 237 65C02 block diagram 237 65C02 timing 239

The custom integrated circuits 241

The memory management unit (MMU) 241 The input/output unit (lOU) 243 The timing generator (TMG) 245 The general logic unit (GLU) 245 The disk controller unit (rWM) 247

Memory addressing 248 ROM addressing 249 RAM addressing 251

Dynamic RAM refreshment 251 Dynamic RAM timing 252

The keyboard 254

The speaker 256

Volume control 256 Output jack 256

The video display 257

The video counters 257 Display memory addressing 258 Display address mapping 258 Video display modes 26l Text displays 263 Low-resolution display 266 High-resolution display 267 Double high-resolution display 269 Video output signals 270 Monitor output 270 Video expansion output 271

Disk I/O 273

Serial I/O 274

ACIA control register 278 ACIA command register 280 ACIA status register 281 ACIA transmit/receive register 282

Contents

Mouse input 282 Hand controller input 287 Memory expansion card 291 Schematic diagrams 291

Appendix A The 65C02 Microprocessor 297

Differences between 6502 and 65C02 297 Differing cycle times 297 Differing instruction results 298

Data sheet 298

Appendix B Memory Map 308

Page $00 308 Page $03 312 Screen holes 312 The hardware page 3l6

Appendix C Important Firmware Locations 322

The tables 322

Port addresses 323

Other video and I/O firmware addresses 326

Applesoft BASIC interpreter addresses 326

Monitor addresses 326

Appendix D Operating Systems and Languages 328

Operating systems 328

ProDOS 328

DOS 328

Pascal Operating System 329 Languages 329

Applesoft BASIC 329

Integer BASIC 330

Pascal 330

Fortran 330

Logon 330

Appendix E

Contents

Interrupts 331

Introduction 331

What is an interrupt? 331 Interrupts on Apple II computers 332 Interrupt handling on the 65C02 333 The intermpt vector at $FFFE 333

The built-in interrupt handler 334

Saving the memory configuration 335 Managing main and auxiliary stacks 336 User's interrupt handler at $03FE 336 Handling break instructions 337 Sources of interrupts 338 Firmware handling of interrupts 339 Firmware for mouse and VBL 339 Firmware for keyboard interrupts 340

Using keyboard buffering firmware 341 Using keyboard interrupts through firmware 342 Using external interrupts through firmware 342 Firmware for serial interrupts 343

Using serial buffering transparently 343 Using serial interrupts through firmware 344 Transmitting serial data 344 A loophole in the firmware 345 Bypassing the interrupt firmware 345

Using mouse interrupts without the firmware 345 Using ACIA interrupts without the firmware 347

Appendix F Apple II Series Differences 348

Overview 348

Type of processor 350 Machine indentification 350 Memory structure 351

Amount and address ranges of RAM 351 Amount and address ranges of ROM 351 Peripheral-card memory spaces 352 Hardware addresses 353

$COOO-SCOOF 353

$C010-SC01F 353

$C020-SC02F 354

$C030-SC03F 354

$C040-SC04F 354

$C050-SC05F 354

$C060-SC06f 355

$C070-SC07F 355

$C080-SC08F 356

$C090-SC0FF 356 Monitors 356

Contents xi

I/O in general 357

DMA transfers 357

Slots versus ports 357

Interrupts 357 The keyboard 357

Keys, switches, and lights 358

Character sets 358 The speaker 359 The video display 359

Character sets 359

MouseText 360

Vertical blanking 360

Display modes 360 Disk I/O 361 Serial I/O 36l

Serial ports versus serial cards 361

Serial I/O buffers 362 Mouse and hand controllers 363

Mouse input 363

Hand controller input and output 363 Cassette I/O 364 Hardware 365

Power 365

Custom chips 365

Appendix G USA and International Models 366

Keyboard layouts and codes 366

USA standard (Sholes) keyboard 367 USA simplified (Dvorak) keyboard 370 ISO layout of USA keyboard 371 English keyboard 372 French keyboad 373 Canadian keyboard 375 German keyboard 376 Italian keyboard 378 Western Spanish keyboard 380

ASCII character sets 381

Certification 383

Product safety 383

Important safety instructions 383

Power supply specifications 383

xii Contents

Appendix H Conversion Tables 384

Bits and bytes 384 Hexadecimal and decimal 387 Hexadecimal and negative decimal 388 Peripheral identification numbers 389 Eight-bit code conversions 391

Appendix I FIrnnware Listings 396

Glossary 509 Bibliography 533 Index 535 Tell Apple Card

Contents

XIII

Figures and tables

Chapter 1 Introduction 1

Figure 1-1 Figure 1-2 Figure 1-3 Figure 1-4

Apple lie external features, front 2

Apple lie external features, back 2

Front of Apple lie with standard USA keyboard 3

USA standard (or Sholes) keyboard,

keyboard switch up 6

Figure 1-5 USA simplified (or Dvorak) keyboard, keyboard switch down 7

Figure 1-6 Speaker, volume control, and audio output jack 8

Figure 1-7 Built-in disk drive 9

Figure 1-8 Back panel connectors 10

Figure 1-9 Inside the machine 11

Figure 1-10 Power supply and voltage converter 12

Figure 1-11 Original and UniDisk 3.5 lie main logic board 13

Figure 1-12 Memory expansion lie main logic board 14

Table 1-1 Keyboard specifications

Chapter 2 Memory Organization and Control 17

Figure 2-1 Internal model of the 65C02 microprocessor 19

Figure 2-2 Apple lie memory map 21

Figure 2-3 Bank-switched memory map 25

Figure 2-4 Read ROM 29

Figure 2-5 Read ROM, write RAM, and use first $D0 bank 30

Figure 2-6 Read ROM, write RAM, and use second $D0 bank 31

Figure 2-7 Read RAM and use first $D0 bank 32

Figure 2-8 Read RAM and use second $D0 bank 33

Figure 2-9 Read and write RAM and use first $D0 bank 34

Figure 2-10 Read and write RAM and use second $D0 bank 35

Figure 2-11 48K memory map 37

Figure 2-12 48K RAM selection, split pairs 40

Figure 2-13 48K RAM selection, one side only 41

Figure 2-14 Page2 selections, 80Store on and HiRes off 47

Figure 2-15 Page2 selections, 80Store on and HiRes on 48

Figure 2-l6 Reset routine flowchart 49

Table 2-1 Bank selector switches 28

XIV

Table 2-2 48K memory switches 39

Table 2-3 48K RAM transfer routines 42

Table 2-4 Parameters for MoveAux routine

Table 2-5 Parameters for XFer routine 43

Table 2-6 Display memory switches 45

Table 2-7 Page $03 vectors 50

43

Chapter 3 introduction to Apple lie I/O 55

Table 3-1 Prompt characters 59

Table 3-2 Escape codes with GetLn 6l

Table 3-3 Control characters with COutl 65

Table 3-4 Control characters with C3COutl 66

Table 3-5 Text window memory locations 69

Table 3-6 Port characteristics 71

Table 3-7 Firmware protocol locations 72

Table 3-8 Port I/O locations 73

Table 3-9 Port screen hole memory locations 74

Cliapter4 Keyboard and Speaker 77

Table 4-1 Keyboard input characteristics 79

Table 4-2 Keys and ASCII codes 80

Table 4-3 Speaker output characteristics 83

CtiapterS video Display Output 85

Figure 5-1 MouseText characters 91

Figure 5-2 40-column and 80-column text with alternate character set 92

Figure 5-3 Text mode characteristics and switching 93

Figure 5-4 High-resolution display bits 96

Figure 5-5 Map of 40-column text display 107

Figure 5-6 Map of 80-column text display 108

Figure 5-7 Map of low-resolution graphics display 109

Figure 5-8 Map of high-resolution graphics display 1 10

Figure 5-9 Map of double high-resolution graphics display 111

Table 5-1 Video output port characteristics 86

Table 5-2 Video display specifications 87

Table 5-3 Display character sets 89

Table 5-4 Low-resolution graphics colors 94

Table 5-5 High-resolution graphics colors 97

Table 5-6 Double high-resolution graphics colors 99

Table 5-7 Video display page locations 101

Table 5-8 Display soft switches 102

Figures and tables

XV

Table 5-9 Display modes supported by firmware,

including Applesoft 104 Table 5-10 Other display modes 104 Table 5-11 Monitor firmware routines 112 Table 5-12 Port 3 firmware protocol table 116 Table 5-13 Pascal video control functions 117

Chapter 6 Block Device I/O 119

Figure 6-1 Summary of Smartport calls 149

Table 6-1 Disk I/O port characteristics 120

Chapter? Serlall/O Port 1 153

Figure 7-1 Diagram of port 1 characteristics storage

Figure 7-2 Data format l63

Table 7-1 Serial port 1 characteristics 154

Table 7-2 Printer port commands 155

Table 7-3 Port 1 hardware page locations 159

Table 7-4 Port 1 I/O firmware protocol l60

Table 7-5 Port 1 screen hole locations l60

162

Chapters Serial I/O Port 2 167

Figure 8-1 Diagram of port 2 characteristics storage 177

Figure 8-2 Devices in a typical communication setup 178

Figure 8-3 Effect of IN#2 180

Figure 8-4 Effect of IN#2 and T command, half duplex 181

Figure 8-5 Effect of IN#2 and T command,

full-duplex terminal 182

Figure 8-6 Effect of IN#2, PR#2, and T conunand,

full-duplex host 183

Table 8-1 Serial port 2 characteristics l68

Table 8-2 Modem port commands 170

Table 8-3 Port 2 hardware page locations 174

Table 8-4 Port 2 I/O firmware protocol 174

Table 8-5 Port 2 screen hole locations 175

Chapter 9 Mouse and Game Input 185

Table 9-1 Mouse input port characteristics 186

Table 9-2 Mouse soft switches 189

Table 9-3 Mouse firmware routines 193

Table 9-4 Mouse port I/O firmware protocol 195

Table 9-5 Mouse port screen hole locations 197

Table 9-6 Game input characteristics 199

XVI

Figures and tables

Chapter 10 Using the Monitor 203

Table 10-1 Mini-Assembler address formats 226

Ctiapter 1 1 Hardwai

Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure

Figure

Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure Figure

e Implementation 231

•1 External power connector 234

•2 Apple lie block diagram 236

■3 65C02 block diagram 238

■4 65C02 timing signals 240

•5 MMU pinouts 242

•6 lOU pinouts 243

■7 TMG pinouts 245

•8 GLU pinouts 246

■9 rWM pinouts 247

■10 Memory bus organization 249

•11 23128 ROM pinouts 249

■12 2316 ROM pinouts 250

•13 2364 pinouts 250

■14 64K RAM pinouts 251

•15 RAM timing signals 253

l6 Keyboard circuit diagram 254

■17 Keyboard signals 255

■18 Speaker circuit diagram 256

•19 Display address transformation 260

•20 40-column text display memory 261

-21 Video display circuits 262

-22 7-MHz video timing signals; 40-column,

low-resolution, and high-resolution display

-23 14-MHz video timing signals: 80-column

and double high-resolution display 265

-24 Video output back panel connectors 270

-25 Video expansion connector pinouts 272

-26 Disk drive connector 274

-27 Serial port circuits 275

-28 6551 ACIA block diagram 276

-29 6551 pinouts 277

-30 Serial port connectors 278

-31 ACIA control register 279

-32 ACIA command register 280

-33 ACIA status register 281

-34 Sample mouse waveform 283

-35 Mouse movement and direction waveforms

-36 Mouse connector 284

-37 Mouse circuits 285

-38 Mouse button signals 286

264

283

Figures and tables

XVII

Figure 11-39 Hand controller connector 287

Figure 1 1-40 How to connect switch inputs 288

Figure 11-41 Hand controller circuits 288

Figure 11-42 Hand controller signals 289

Figure 11-43 Memory expansion card connector

pinout diagram 291

Figure 11-44 Apple lie schematic diagram 292

Table 11-1 Environmental specifications 232

Table 11-2 Power supply specifications 233

Table 11-3 External power connector signals 234

Table 11-4 Internal converter specifications 234

Table 11-5 65C02 microprocessor specifications 239

Table 11-6 65C02 timing signal descriptions 240

Table 11-7 MMU signal descriptions 242

Table 11-8 lOU signal descriptions 243

Table 11-9 TMG signal descriptions 245

Table 11-10 GLU signal descriptions 246

Table 11-11 IWM signal descriptions 247

Table 11-12 RAM address multiplexing 252

Table 11-13 RAM timing signals 253

Table 11-14 Display memory addressing 260

Table 11-15 Memory address bits for display modes 260

Table 11-16 Character-generator control signals 266

Table 11-17 Video expansion connector signals 272

Table 11-18 Disk drive connector signals 274

Table 11-19 6551 signal descriptions 277

Table 11-20 Serial port connector signals 278

Table 11-21 Mouse connector signals 284

Table 11-22 Hand controller connector signals 287

Appendix A The 65C02 Microprocessor 297

Table A-1 Cycle time differences 298

Appendix B Memory Map 308

Table B-1 Page $00 use 309

Table B-2 Page $03 use 312

Table B-3 Main memory screen hole allocations 313

Table B-4 Auxiliary memory screen hole allocations 315

Table B-5 Addresses $C000-$C03F 3l6

Table B-6 Addresses $C040-$C05F 318

Table B-7 Addresses $C060-$C07F 319

Table B-8 Addresses $C080-$C0AF 320

Table B-9 Addresses $C0B0-$C0FF 321

xvili

Figures and tables

Appendix C Important Firmware Locations 322

Table C-1 Serial port 1 addresses 323

Table C-2 Serial port 2 addresses 324

Table C-3 Video firmware addresses 324

Table C-4 Mouse port addresses 325

Table C-5 Apple lie enhanced video

and miscellaneous firmware 326

Table C-6 Apple lie monitor entry points and vectors 326

Appendix E Interrupts 331

Table E-1 Interrupt-handling sequence 335

Table E-2 Activating mouse interrupts 346 Table E-3 Reading mouse interrupts 346

Appendix F Apple II Series Differences 348

Apple II, II Plus, and He hand controller signals 364

Figure F-1 Table F-1

Apple II series indentification bytes 350

Appendix G USA and International Models 366

Figure G-1 USA standard (or Sholes) keyboard,

keyboard switch up 368 Figure G-2 USA simplified (or Dvorak) keyboard,

keyboard switch down 370 Figure G-3 ISO version of USA standard keyboard,

keyboard switch up 371 Figure G-4 English keyboard, keyboard switch up 372 Figure G-5 French keyboard, keyboard switch down 373 Figure G-6 Canadian keyboard, keyboard switch down 375 Figure G-7 German keyboard, keyboard switch down 376 Figure G-8 Italian keyboard, keyboard switch down 378 Figure G-9 Western Spanish keyboard, keyboard

switch down 380 Table G-1 Keys and ASCII codes 368 Table G-2 English keyboard code differences

from Table G-1 372 Table G-3 French keyboard code differences

from Table G-1 374 Table G-4 Canadian keyboard code differences

from Table G-1 375 Table G-5 German keyboard code differences

from Table G-1 377

Figures and tables

XIX

Table G-6 Italian keyboard code differences

from Table G-1 379 Table G-7 Western Spanish keyboard code differences

from Table G-1 381 Table G-8 ASCII code equivalents 381 Table G-9 50-Hz power supply specifications 383

Appendix H Conversion Tables 384

Figure H-1 Bits, nibbles, and bytes 386

Table H-1 What a bit can represent 385

Table H-2 Values represented by a nibble 386

Table H-3 Hexadecimal/decimal conversion 387

Table H-4 Hexadecimal to negative decimal conversion 388

Table H-5 PIN numbers 390

Table H-6 Control characters, high bit off 392

Table H-7 Special characters, high bit off 393

Table H-8 Uppercase characters, high bit off 394

Table H-9 Lowercase characters, high bit off 395

Appendix I Firmware Listings 396

Table I-l Main side ROM map 397

Table 1-2 Auxiliary side ROM map 398

XX

Figures and tables

Preface

About This Manual

This is the reference manual for the Apple® lie personal computer. It contains detailed descriptions of all the hardware and firmware that make up the Apple lie and provides the technical information that peripheral-card designers and programmers need.

The information in this manual is aimed at assembly-language programmers and hardware designers, but others interested in the internal operation of the Apple lie can also benefit from reading it.

This manual tells you how the Apple lie works, but not how to use it. If you need to know how to set up and use your Apple He, read the Apple lie Owner's Manual.

This manual describes three versions of the Apple IIc:

n the original Apple IIc

n the Apple IIc that supports the UniDisk™ 3.5 drive

n the Apple IIc that supports the Memory Expansion Card

More information on the various versions of the Apple IIc is provided under "The Apple IIc Family," later in this Preface.

Contents of ttiis manual

The Apple IIc is presented in this manual from the outside in.

Chapter 1 introduces the Apple IIc, including external controls, connectors, and the main internal components.

Chapter 2 introduces the 65C02 microprocessor and its directly addressable memory space.

Chapter 3 introduces the I/O characteristics of the Apple IIc. Chapters 4 and 9 cover specific areas of the I/O interface.

XXI

Chapter 4 describes the keyboard and speaker.

Giapter 5 describes the video display.

Chapter 6 describes block device I/O, including the Smartport firmware interface.

Chapter 7 describes serial port 1.

Chapter 8 describes serial port 2.

Chapter 9 describes the mouse/game paddle port.

Chapter 10 describes the Apple He's built-in Monitor firmware. The Monitor helps you write, disassemble, and debug machine- language programs, as well as providing you with a means to look at and manipulate the contents of main memory.

Chapter 11 describes the Apple lie hardware in detail.

Appendix A describes the 65C02 microprocessor in detail, including the differences between it and the 6502 microprocessor used on early-model Apple II's. Most of this appendix is a reprint of the manufacturer's data sheet for the 65C02.

Appendix B contains a memory map of die Apple Uc main memory. Detailed maps are provided for memory pages $00 and $03, the screen holes, and the hardware page.

Appendix C lists the Apple lie firmware entry points, including those for the I/O firmware and the Monitor firmware.

Appendix D describes some of the operating systems and languages supported by Apple Computer for the Apple lie.

Appendix E describes the operation of the Apple He interrupt handler firmware and how to use it in your programs.

Appendix F outlines the differences and similarities between the diverse members of the Apple II family of computers.

Appendix G describes the various international versions of the Apple He keyboard and character set Power and safety information for international versions of the Apple He is also included in this appendix.

Appendix H contains tables to aid you in code and number base conversions.

Appendix I contains the firmware listing for the new version of the Apple nc and information on obtaining listings for the original and UniDisk 3.5 ROMs.

The Glossary defines many of the technical terms used in this manual.

xxll Preface: About This Manual

The Bibliography lists articles and books with additional information about the Apple lie.

Finally, after the index at the back of this manual, you'll find the Tell Apple Card; please take a minute to fill this card out and mail it back to us. Your experience with this and other Apple manuals can help us plan new reference materials.

The Apple lie family

Changes have been made to the Apple lie since the original version was introduced. The first change was made in order to support the UniDisk 3.5 external drive, and included a set of ROM-based machine-language routines called the Protocol Converter. The latest version incorporates all the UniDisk 3-5 upgrade features, a new version of the Protocol Converter called the Smartport, and support for an optional memory expansion card. All of these versions are described in this manual. Where there are differences between the various versions of the Apple lie, they will be called out in the manual. For the sake of convenience, the various versions of the Apple lie are identified by the features they support, such as memory expansion for the newest lie and UniDisk 3-5 for the version that introduced the UniDisk 3.5 drive support. Unless specified, all versions of the Apple lie operate identically.

Imporfant Smartport is merely a new name for the Protocol Converter; all the specifications for the Smartport apply to the Protocol Converter, and vice versa.

Identifying your Apple lie

There are basically three versions of the Apple IIc:

D the original Apple IIc

n the UniDisk 3-5 Apple IIc

n the memory expansion Apple IIc

You can tell which Apple IIc you have by checking the value of the ID byte at ROM location 64447 ($FBBF in hexadecimal). The value of this byte is 255 ($FF) in the original Apple IIc, 0 ($00) in the UniDisk 3.5 version, and 3 ($03) in the memory expansion version.

Checking the ID byte: You can check the value of the ID byte from Applesoft by typing PRINT PEEK (64447).

The Apple ilc family xxill

The original Apple lie

The original Apple lie is the oldest member of the lie family. It has the following features:

D the 65C02 microprocessor

D 128KofRAM

The UniDisk 3.5 Apple lie

The Apple lie that introduced support for the UniDisk 3.5 drive is Identified in this manual as the UniDisk 3-5 version. It includes the following changes from the original Apple IIc:

D the Protocol Converter, to support the UniDisk 3.5 external disk drive

n a 256k ROM IC to replace the 128K ROM

n some new serial port commands

D the Mini-Assembler

D two new Monitor commands (STEP and TRACE)

n built-in diagnostics

The UniDisk 35 Apple IIc also includes improved intermpt handler features and new external drive startup procedures.

The memory expansion Apple IIc

The Apple IIc that supports an optional memory expansion card supports all the features of the UniDisk 3.5 version. It includes the following changes from the UniDisk 3.5 IIc:

n an internal connector to support an optional memory expansion card

D 4 64Kx4 RAM ICs to replace the l6 64Kxl ICs

The Apple IIc that supports the memory expansion option also reorganizes the I/O port ("slot") entry points in the firmware. The mouse, located at port 4 in the original and UniDisk 3-5 versions, is now at port 7. The memory expansion card uses port 4 in the new Apple IIc. What this means is that all the mouse I/O entry point addresses have been changed from $C4XX to $C7XX.

xxiv Preface: About This Manual

To avoid confusion and maintain compatibility with previous versions, the text and tables in this book still show the values used for the original and UniDisk 3.5 versions of the Apple lie. However, a statement reminding you of the change appears near affected tables.

Remember that the Smartport and the Protocol Converter are the same thing.

Conventions used in this manual

Special text in this manual is set off in several different ways, as shown in these examples.

Warning Important warnings look like this. These flag potential danger to the Apple lie, its software, or you.

Important Text set off like this is less urgent or threatening than text In a Warning box, but still of a critical nature.

Original lie Text set off like this applies only to the original version of the Apple lie.

UniDisk 3.5 Text set off like this applies only to the UniDisk 3.5 version of the Apple lie.

Memory expansion Text set off like this applies only to the memory expansion version of the Apple lie.

By the way: Information that is useful but incidental to the text is set off like this. You may want to skip over such information and return to it later.

Terms that appear in boldface in the text are defined in the Glossary or a marginal gloss.

Computer voice is used to indicate text that should be identical to your screen display or printout.

Conventions used in this manual xxv

Chapter 1

Introduction

This chapter introduces you to the working parts of the Apple lie by briefly describing the major components of the computer ^both internal and external hardware and firmware ^and telling you where in the manual to find out more about them.

The outside of the machine

This section briefly describes the Apple He's keyboard, controls, indicators, and expansion connectors.

The Apple lie comes equipped with a keyboard, speaker (with audio output jack and volume control), built-in disk drive, external power supply, and internal voltage converter. It also has built-in interfaces with external connectors for a serial printer, video monitor, special video display adapters, modem, mouse, and game controllers. These external connectors allow you to plug in accessory equipment without having to go inside the machine to use expansion slots like those in the Apple lie.

Figure 1-1 shows the front and right side of an Apple lie, and Figure 1-2 shows the back and left side.

Keyboard Disk Drive

(See Figs. 1-4 and 1-5) (See Fig. 1-7)

Figure 1-1

Apple He external features, front

Baci< Panel (See Fig. 1-8)

Speal<er

Volume Control

(See Fig. 1-6)

Figure 1-2

Apple He external features, back

Chapter 1 : Introduction

The keyboard

ASCII stands for American Standard Code for Information Interctiange. Table 4-2 lists the ASCII character encoding for the standard and simplified USA keyboards. Appendix G lists the encoding for International keyboards.

The Apple He's primary input device is the keyboard, shown in Figure 1-3. The keyboard has a 63-key typewriter layout with both uppercase and lowercase characters and can generate all 128 standard ASCII characters. A reset key, 80/40-column display selector switch, keyboard layout selector switch, disk-use light, and power light are also located on the front of the computer.

Reset Switch -

80/40-Column Switch -

Keyboard Switch -

I I . ': i ':

i 1 1 1 M I i i ! ! I M n I

/ /

n , 1

1

r"^ ~

_ 1 -- -rJ

^ ... -... ;.'■ ,

mmmmmm

m

Disl<-Use Light Power Light

Figure 1-3

Front of Apple lie with standard USA l<eyboard

Table 1-1 lists the characteristics of all Apple lie keyboards and front panels.

Features

The Apple lie keyboard has automatic repeat on all character keys. This means that if you hold the key down longer than about a second, the character it generates repeats until you let up the key. It also has two-key rollover, which means if you press a key before releasing the one you pressed before it, the second character enters the computer the same as though you had released the previous key first. (This is important for fast touch-typists.)

The outside of the machine

Table 1-1

Keyboard specifications

Number of keys

Character encoding

Number of codes

Features

Special function l<eys

Cursor movement l<eys

Modifier Iceys Front-panel switches Front-panel lights

63

ASCII

128

Automatic fe^at, twe-key rollover

Reset, Open Apple, Solid Apple,

Left Arrow, Right Arrow, Down Arrow,

Up Arrow, Return, Delete, Tab

Control, Shift, Caps Lock, Escape

80/40 switch, keyboard switch

Power light, disk-use light

The Open Apple and Solid Apple keys are connected to 1-bit addresses in memory, described in Chapter 9.

Chapter 2 describes the results of the various reset procedures.

Special function keys

The Apple lie keyboard has three special function keys: Reset, and two keys marked with apples one outlined (Open Apple) and one filled in (Solid Apple).

Reset has a direct line to the 65C02 microprocessor's RESET signal line (see Chapter 11): holding down Control while pressing Reset causes the Apple lie to restart processing with an internal firmware program that puts the machine in a known state (see Chapter 2).

You can restart the Apple lie without turning the power off and back on again, by holding down both Control and Open Apple while pressing Reset. Restarting this way is less stressful to the Apple He's components than normal powerup.

Cursor movement keys

The Apple lie keyboard has four cursor movement keys with arrows marked on them: left, right, down, and up. Three other keys can also cause cursor movements: Return, Delete, and Tab. All seven of these keys generate ASCII control characters (see Table 4-2). It is up to the operating system or application program to interpret and act on the control codes that these keys generate.

Chapter 1 : introduction

The Monitor Is a built-in program that performs some of the basic activities of the computer, such as retrieving and storing key codes OS they come In, and clearing or updating the display screen.

Modifier l<eys

Three special keys Control, Shift, and Caps Lock generate no codes when pressed by themselves, but change the codes generated by other keys they are pressed in combination with. A fourth key, Escape, generates a nonprinting control code that causes the Monitor to interpret certain subsequent keystroke in a modified way.

D Control, when pressed in combination with letter keys or certain other keys, produces ASCII control characters. Most of the control characters are invisible most of the time.

n Shift works the same on the Apple lie as on an ordinary

typewriter: it selects uppercase letters and the upper characters on the keys.

n Caps Lock, in its down position, changes the letter keys to uppercase, but does not affect other keys.

n Escape is not a modifier key in the same sense as Control and Shift: you do not hold it down while pressing other keys. Rather, you press Escape and it generates the ASCII escape (ESC) control character (key code $1B see Table 4-2). When the Escape key is pressed, many programs including the built-in Monitor program then interpret other specific keys as designating an escape sequence.

The 80/40 switch

The 80/40 switch lets you specify whether a program should display information in 40 or 80 columns per line. The switch indicates 40- column display when in its down position, and 80-column display when in its up position.

Important Not all progranns check this sv\/itch. Even programs that do

check the svs/itch may do so only v^i'hen the program first starts up. If that is the case, changing the sv\/ltch position while the program Is running W\\\ have no effect on the program's display, (See Table 4-1.)

The outside of the machine

The keyboard switch

You use the keyboard switch to select for use one of the two keyboard layouts and screen character sets built into your Apple lie. On USA versions of the Apple lie, you select the standard Sholes keyboard layout (Figure 1-4) with the switch in the up position, and the Dvorak simplified layout (Figure 1-5) with the switch in the down position.

If you normally use the Dvorak keyboard layout, you can gently pry up the keys from the keyboard and rearrange and replace them in their Dvorak positions.

/reset / IjBom jjkt

keyboard

ldisi;use I power

esc

1 /

@ 2

3 4 5 6 7 8 9 0 - = delete

tab

Q

W

E

B

T

Y

U

[ J 1 / 0 P [ ] \

controi

A

S

D

F

G

H

J

II

K L ; ' return

< > ?

shift Z X C V B N M . . / shift

caps lock

\

d « ^- -^ 1 t

Figure 1-4

USA standard (or Sholes) keyboard, keyboard switch up

Chapter 1 : Introduction

/reset / Hsomo 11 h

keyboard

I disk use I power

! @ # $ esc 1 2 3 4

I - & 5 6 7

* ( ) [ ]

8 9 0 [ } delete

tab

II

1

<

>

P

Y F G C

R

? + 1 / / = \

control

A

0

E

U

1 D H T N

S

return

shift

:

Q

J

K

X

B

M W

V Z

shift

caps lock

\

c5

« ^- -^ ^ t

Figure 1-5

USA simplified (or Dvoral<) keyboard, keyboard switch down (sinaded characters may be in different positions on some modeis)

Appendix G illustrates the keyboard layouts for both keyboard switch posltloris on several International versions of the Apple lie.

On international models, the keycaps indicate the character positions for the local keyboard layout, which is selected when the keyboard switch is down. When up, the keyboard switch selects the USA standard characters and key layout.

Disk-use and power lights

The red disk-use light glows whenever the built-in disk drive's motor is switched on.

The green power light glows when the Apple lie is turned on and normal power is present at the Apple He's internal power supply.

Warning If the power light flashes on and off, turn off the computer

immediately. Find out what caused the condition (such as a brownout or short circuit) and fix the problem before turning the computer on again. Above all, do not use the disk drive when the power light Is flashing; this may damage the computer.

The outside of the machine

The way programs control the speaker is described under "Speai<er Output" In Chapter 4.

The speaker

The Apple lie has a speaker in the bottom of the case, as shown in Figure 1-6. The speaker lets Apple lie programs produce a variety of sounds. There is also a volume control on the left side of the Apple lie case, and a jack for connecting headphones or an external speaker. The jack accepts either She-charfnel (monaural) or two-channel (stereo) plugs, although speaker output is monaural only. Inserting a plug disconnects the built-in speaker

Volume Control Knob Speaker (Inside) Audio Output Jack

Figure 1 -6

Speaker, volume control, arid audio output jack

The built-in disl< drive

The Apple He's built-in disk drive (Figure 1-7) is fully compatible with the Apple Disk lie that reads and writes 5.25-inch single-sided 35-track disks. The drive door is on the right side of the Apple lie case.

Chapter 1 ; Introduction

Disk Drive-Door

Figure 1-7

Built-in disk drive

The back panel

The back panel of the Apple lie (Figure 1-8) has seven connectors and a main power switch. From left to right they are

n a 9-pin D-type miniature connector for connecting hand controllers, a mouse, a joystick, or some other device (see Chapters 9 and 11)

D a 5-pin DIN connector for serial input and output (port 2; normally for a modem) (see Chapters 7 and 1 1)

n a 15-pin D-type connector for video expansion (see Chapter 11)

n an RCA-type jack for a video monitor (see Chapter 1 1)

D a 19-pin D-type connector for connecting one or more external devices, such as intelligent disk drives (see Chapters 6 and 1 1)

n another 5-pin DIN connector for serial input and output (port 1; normally for a printer or plotter) (see Chapters 8 and 11)

n a special 7-pin DIN connector for power input (see Chapter 11)

Before attaching cables to the Apple lie back panel connectors, be sure to move the handle until it clicks into position for propping up the computer. The handle should be down whenever the computer is running so that it can maintain proper cooling airflow.

The installation manuals for external devices contain instructions for connecting them to the Apple lie.

The outside of the machine

Hon

Mouse and Hand

Control Connector

(See Figs. 11-37

and 11-42)

Serial Port 2

Connector

(See Fig. 11-30)

Video Expansion

Connector (See Fig. 11-25)

Handle

Serial Port 1

Connector

(See Fig. 11-30)

Power Switch

Video Output

Connector (See Fig. 11-24)

External

Disk Drive

Connector

(See Fig. 1 1-26)

External Power

Connector (See Fig. 11-1)

Figure 1 -8

Back panel connectors

10

Chapter 1 : Introduction

The inside of the machine

Figure 1-9 shows the main components inside the Apple lie computer.

Voltage Converter

Built-in Disk Drive

Main Logic Board (See Figure 1-11)

Speaker (underneath)

Figure 1-9

Inside the machine

Complete specifications of the Apple He power supply and voltage converter appear in Chapter 11.

The internal voltage converter

The built-in voltage converter operates from a 12 to 15 VDC input source, such as provided by the external power supply furnished with the Apple lie (Figure 1-10). The voltage converter provides power for the logic board, built-in disk drive, one external disk drive, and the I/O signals available at the back panel.

The inside of the machine

n

Internal Voltage Converter

Power Switch

Power Supply

Figure 1-10

Power supply and voltage converter

The voltage converter produces three different voltages: +5V, +12V, and -12V. (Minus 5V, needed by some components in the Apple lie, is derived from -12V on the main logic board.) It is a high-efficiency switching converter that protects itself and the rest of the Apple lie against short circuits and other electrical mishaps.

The main logic board

The main logic board, which is mounted flat in the bottom of the Apple lie's case, has almost all the electronic parts of the computer attached to it.

12

Chapter 1 : Introduction

Firmware is program code Vnai is stored in ROIV1. It can be read and executed, but not cl^anged.

Figure 1-11 shows the main logic board and the most important integrated circuits (ICs) in the Apple lie. They are the CPU (central processing unit), RAM (random-access memory), ROM (read- only memory) ICs for keyboard encoding, display character generation, and firm-ware, and the five custom ICs.

The processor is a 65C02 microprocessor. The 65C02 is a CMOS version of the 6502 used in other members of the Apple II family. It is an 8-bit microprocessor with a l6-bit address bus. In the Apple lie, the 65C02 runs at 1 MHz and performs up to 500,000 8-bit operations per second.

Character Generator ROM

IWM

TMG

GLU

Auxiliary RAIVI

Main RAIVI-

lOU

MMU

Keyboard ROM

65C02 Microprocessor

Firmware ROM

Figure 1-11

Original and UnlDlsk 3.5 lie main logic board

The inside of tine machine

13

The Applesoft language interpreter is described in the Applesoft Tutorial and the Applesoft BASIC Programmer's Reference Manual.

Figure 1-12

Memory expansion lie main iogic board

The keyboard is scanned by an IC that generates matrix values for a ROM. The value of the ASCII code supplied by the ROM is latched at a specified memory location and is readable by programs.

The character generator ROM converts ASCII character values to a form that the video display can use.

The other ROM contains the Monitor, the Applesoft BASIC interpreter, enhanced video firmware, and other input/output firmware. The firmware that this ROM contains is described throughout this manual.

14

Chapter 1 ; Introduction

For more on memory addressing, see Chapter 2.

See Chapters 3 through 9,

Chapter 1 1 discusses the functions of these Integrated circuits in some detail.

Five of the large ICs on the main logic board are custom-made for the Apple IIc:

n The memory management unit CMMU) contains most of the logic that controls memory addressing in the Apple IIc.

D The input/output unit (lOU) contains most of the logic that controls the built-in input and output features of the Apple IIc.

n The timing generator OMG) generates all the system and I/O clock and timing signals from a 14-MHz oscillator.

n The general logic unit (GLU) performs the remaining required logic functions.

n The disk controller unit, also known as the Integrated Woz Machine (IWM), is a single-chip version of the Apple Disk II controller card. It controls the built-in and external disk drives connected to the Apple IIc.

The other circuit boards

The Apple IIc contains other circuit boards that serve special purposes: a motor-speed control and read/write logic board for the disk drive, and a matrix board for detecting the position of keys pressed. This manual does not discuss these circuit boards.

Warning Adjustment of disk drive speed must be done by an authorized Apple Service Center. Do not attempt to adjust Vr\e speed of your built-in disk drive. If you do, you may damage It and you will void your warranty.

The inside of the machine

15

Chapter 2

Memory Organization and Controi

17

This chapter introduces the Apple He's processor, the 65C02, and the memory ranges and locations in the Apple lie that have been set aside for special purposes. The last section of this chapter describes the reset routines, which restore the computer to a known state.

Each of the other registers holds eight bits (one byte), so the 65C02 is called on 8-bit processor.

Appendix A lists the instructions the 65C02 can carry out, their use, and their effects on the registers. For further information, consult the pertinent books listed in the Bibliography.

The 65C02 microprocessor

The 65C02 is a general-purpose 8-bit CMOS microprocessor similar in operation to the 6502 used in other members of the Apple II family of computers.

Figure 2-1 is a model of the 65C02 microprocessor's register organization. Registers are fast-acting built-in storage areas where the processor performs and keeps track of its work. The 65C02 has one l6-bit register and five 8-bit registers.

The l6-bit register is called the program counter (IPC). It specifies the address in memory that contains the instruction the processor is currently carrying out. A l6-bit register can specify any one of 65,536 memory addresses, and so the 65C02 is said to have an address space of 65,536 locations.

The five 8-bit registers in the 65C02 are the following:

a The accumulator, or A register. The accumulator is Uke a desk top where the processor performs mathematical and logical operations on information.

The index registers, X and Y. The processor uses these registers to modify the address where information is to be found or placed, and to pass information from one program to another.

n A stack pointer, or S register. The processor uses a 256-byte region of memory page $01 as an area to stack up bytes for future use. The stack is empty when the computer is turned on. Several 65C02 instructions either push (store) the contents of a register onto the stack, or pull (retrieve) a byte from the stack and place it in a register. The S register keeps track of the address of the byte in the stack that is currently ready for use.

n A processor status register, or P register. Seven of the eight bits of this register are used as flags to record the outcome of processor activities, and can be checked by later instructions to determine what has happened and what the processor should do next.

18

Chapter 2: Memory Orgariization and Control

AO A1 A2 A3 - A4 A5 - A6 A7 -

Address J Bus"\

A8 - A9 A10 - A11 A12 - A13- A14 A16 -

Figure 2-1

Internal model of the 65C02 microprocessor (copyright < used by permission)

1982 by NCR Corporation;

The 65C02 microprocessor

19

Soft switches are described more fully under "Bank- Switched Memory" and '48K Memory.'

There ore two other ROMs in the Apple lie: one to generate characters corresponding to keystrokes and another to generate characters for display. (See "The Keyboard" and "The Video Display" in Chapter 9.) However, these ROMs are not addressable by the microprocessor.

Overview of the address space

The Apple He's 65C02 microprocessor can address 65,536 (64K) memory locations. All the Apple He's RAM, ROM, and input and output (I/O) devices are accessed using addresses in this 64K address range. Some functions have the same addresses but not at the same time. The Apple lie controls its shared addresses by using soft switches. A soft switch is a memory location that controls some aspect of the computer's operation when it is accessed.

All input and output in the Apple lie is memory mapped that is, specific memory addresses (all in the $C0 page) are allocated to each I/O device. In this chapter, the I/O memory spaces are described simply as areas of memory. For details of the built-in I/O features and firmware, refer to the descriptions in Chapters 3 through 9.

A contiguous block of 256 address locations in the 65C02's address range is called a page. A 1-byte address counter or 8-bit register can specify 1 of 256 different locations. Thus, page $00 consists of memory locations from 0 through 255 (hexadecimal $00 through $FF); page $01 consists of locations 256 through 511 (hexadecimal $0100 through $01FF); and so on. In this manual, all page numbers are given in hexadecimal format.

Note: The first two digits of a four-digit hexadecimal address are the page number. There are 256 pages of 256 bytes each in the address space. This kind of page is different from the display areas in the Apple lie, v/hieh are sometimes referred to as Page 1 and Page 2. In this manual, dollar signs ($) in addresses signify that the addresses are in hexadecimal notation.

Memory map and memory switching

Figure 2-2 is a map of the Apple He's memory address space and what the major blocks of addresses are used for. As you can see in the figure, addresses $C000 through $C0FF contain hardware only, and addresses $C100 through $CFFF contain ROM only. At all other addresses there are two to five blocks of RAM or ROM locations. At any given time, only one block of RAM or ROM occupies each set of addresses. As described later in this chapter, soft switches in the hardware page control that blocks the processor is currently using.

20

Chapter 2: Memory Organization and Control

Hardware

ROM

Main RAM

$FFFF

"> r

Auxiliary RAM

A

$DOO0 _ $C100 ,

$cooo

$0200 _ $0000

r

V.

Monitor

Applesoft

BASIC Interpreter

I/O Firmware

Banl< 1

Bank 2

Bank 1

Bank 2

r

Hardware Page

Pages $00>.

and $01 ■^

Figure 2-2

Apple lie memory map

Memory map and memory switching

21

Main RAM addresses ($0000-$BFFF and $DOOO-$FFFF)

The area labeled Main RAM in Figure 2-2 is so called because some or all of it is present in all models of the Apple II series of computers. The Apple lie has 64K bytes of main RAM.

Auxiliary RAM addresses ($0000-$BFFF and $DOOO-$FFFF)

The Apple lie has 64K of auxiliary RAM built in. Some or all of that range of auxiliary memory is present in an Apple He with one of the 80-column text cards installed (see Appendix F), but there is no auxiliary RAM in the Apple II or II Plus.

A range of addresses in auxiliary RAM cannot be used simultaneously with the same range of addresses in main RAM; your programs must use the soft switches described in this chapter to select either main or auxiliary memory for any given range of addresses.

ROM addresses ($C100-$FFFF)

ROM addresses contain the built-in Apple lie firmware. Addresses $C100 through $CFFF belong exclusively to ROM. Addresses $DO0O through $FFFF are shared by ROM, main RAM, and auxiliary RAM; the selection techniques are described later in this chapter.

The Apple lie's built-in ROM pages $C1 through CF (addresses $C100 through $CFFF) contain I/O firmware. The Apple lie I/O firmware is roughly divided among the built-in I/O devices as follows:

D Serial port 1 (RS-232 device) firmware entry points are on

page $C1. Much, but not all, of the firmware for the port is in the $C100 space.

D Serial port 2 (communication device) firmware entry points are on page $C2. Much, but not all, of the firmware for the port is in the $C100 space.

22 Chapter 2: Memory Organization and Control

The operation of the Applesoft interpreter firmware is described in the Applesoft BASIC Programmer's Reference Manual.

a Video output firmware entry points are on page $C3; the enhanced video firmware and miscellaneous I/O support routines occupy pages $C8 through $CF. This is partly because there are no slots 8 through F on the Apple lie and because the firmware takes up more than one page of firmware memory space.

n Mouse firmware entry points are on page $C4 Cpage $C7 in the memory expansion version).

D Block device I/O firmware entry points are on page $C6.

Note: This correspondence of ports and entry points does not imply that all of each port's firmware occupies a specific page. The Apple lie I/O port firmware space is allocated in a way that provides the best possible performance in the available space.

The ROM address range of pages $D0 through $FF contain the Applesoft BASIC interpreter and the Monitor firmware, allocated as follows:

D Pages $D0 through $F7 (addresses $D000 through $F7FF) contain the Applesoft interpreter firmware.

n Pages $F8 through $FF (addresses $F800 through $FFFF) contain the Monitor, described in Chapter 10. You can use some of the built-in Monitor routines to make input and output procedures in your assembly-language programs easier to write. These routines are described in Chapters 3 through 9.

Chapters 3 through 9 describe the Apple lie's input and output locations. Appendix B lists these locations in address order, rather than by function.

Bit numbering in a byte is explained in Appendix H.

Hardware addresses ($COOO-$COFF)

The soft switches that the Apple lie and your programs use to control the Apple He's built-in input and output functions are all found in the $C0 memory page (addresses $C000 through $COFF). In the same range of memory are the switches for selecting blocks of memory throughout the address space. This chapter describes the address space (memory) switches.

The hardware functions of the switches in this page fall into five basic categories:

n Data inputs. The only data input is location $C000, where the low-order seven bits (bits 6 through 0) represent the keyboard key just pressed. (These data are guaranteed valid only when bit 7 = 1.)

D Flag inputs. Most built-in input locations are single-bit flags in the high-order (bit 7) position of their respective memory addresses. Flags have only two values: on (greater than or equal to 128 or $80) or off Oess than 128 or $80).

Memory map and memory switching

23

The switch, hand controller (analog) and button inputs, and the keyboard strobe are examples of flag inputs. The locations for reading soft-switch states are also of this type.

D Strobe outputs. The clear keyboard strobe (Chapter 4) and paddle timer strobe (Chapter 9) outputs are controlled by memory locations. If your program reads the contents of one of these locations, then the function associated with that location will be activated.

D Toggle switches. The Apple lie has only one toggle switch: the speaker switch. A toggle switch has only one address assigned to it; each time you access it, it changes to its other state (on or off).

Reading the speaker toggle at location $C030 clicks the speaker once. However, if you write to the speaker location, the microprocessor activates the address bus twice during successive clock cycles, causing the speaker toggle to end up in its original state before the speaker cone can move. Therefore, you should read, rather than write, to use this device.

The processor cannot read the on/off status of the speaker switch.

D Soji switches. Soft switches are two-position switches turned on by accessing one address and turned off by accessing another address. Most of these switches have a third address associated with them for reading the state of the switch.

There are eight soft switches that select different combinations of bank -switched memory. Four of these eight switches require that your program read them twice in succession to activate them.

Bank-switched memory

The memory areas described in this section are called bank- switched memory (Figure 2-3) because so many banks (ranges) of addresses one bank of ROM and up to four banks of RAM occupy the same group of locations among the upper addresses of memory. Pages $00 and $01, at the low end of memory, are included here because the two sets of them one in main RAM and one in auxiliary RAM are controlled by the same switches as the high-address banks. The stack and zero page are switched this way so that system software running in the bank-switched memory space can maintain its own stack and zero page while it manipulates the 48K memory space.

24 Chapter 2: Memory Organization and Control

ROM

Main RAM

Auxiliary RAM

^

f $FFFF

$F800 $F7FF

S

w

c CD

$E000 $DFFF

$D000 _ ' $CFFF

$C100 , $BFFF '

Pages $00 f

and $01

•L

$0200 $0000

Monitor Firmware

Applesoft

BASIC Interpreter

Banki

Bank 2

Bank 1

Bank 2

Figure 2-3

Bank-switched memory map

Bank-switched memory

25

Page allocations

Pages $00 and $01 are used by many of the 65C02 instructions. The ROM and RAM addresses in bank-switched memory are usually occupied by system software such as interpreters, compilers, and operating systems.

Page $00 (one-byte addresses)

Several of the 65C02 microprocessor's addressing modes for example, indirect addressing require the use of addresses in page $00, or zero page. However, the Monitor, the interpreters, and the operating systems all make extensive use of page $00, too. One way to avoid conflicts is to use only those page-$00 locations not already used by these other programs. But there is another way.

As you can see from Table B-1 in Appendix B, page $00 is pretty well used up, except for a few bytes here and there. Rather than trying to squeeze your data into an unused corner, you may prefer a safer alternative: turn off interrupts, save the contents of part of page $00, use that part, then restore the previous contents to page $00, restore interrupts to their previous state, and then pass control to another program.

Page $01 (the 65C02 stack)

The 65C02 microprocessor uses page $01 as its stack a place where it can store subroutine return addresses, in last-in, first-out sequence. Programs can also use the stack for temporary storage of registers (via push and pull instructions). However, programs should use the stack carefully.

These memory banks are controlled by the soft switches described under "Using Bonk Selector Switches."

Pages $D0-$FF (ROM and RAM)

The memory address space from locations $IX)00 through $FFFF is used for both ROM and RAM. The 12K bytes of ROM in this address space contain the Monitor and the Applesoft BASIC interpreter.

There are 16K bytes of main RAM in this 12K space, with two banks occupying the 4K of addresses from $D000 through $DFFF. The RAM is normally used for storing other languages such as Pascal, or operating systems such as ProDOS®.

There are also 16k bytes of auxiliary RAM in this 12K space, again with double occupancy in the address range $D000 through $DFFF.

26

Chapter 2; Memory Organization and Control

Using bank selector switches

You switch banks of memory in the same way you switch other functions in the Apple IIc: by using soft switches. These soft switches do four things:

D select either RAM or ROM in this memory space

n allow or inhibit (write-protect) writing to the RAM when RAM is selected

n select the first or second 4K-byte bank of RAM in the address space $D000 through $DFFF

n select either main RAM or auxiliary RAM

Warning Do not use soft switches without carefui pianning. Careless switching between RAM and ROM Is almost certain to have catastrophic effects on your program.

Table 2-1 shows the addresses of the soft switches for selecting all allowed combinations of reading and writing in this memory space, and the addresses of the locations to read the switch settings. Figures 2-4 through 2-10 illustrate how to select the combinations and what the resulting status of each switch is.

To make sure you do not inadvertently remove write protection from bank-switched RAM, the four write-enable addresses require that you read them twice in succession (indicated by RR in Table 2-1).

Because the AltZP switch shares the read keyboard address, you must write iWm Table 2-1) to its locations to change the switch setting.

To find out which way a switch is set, read the appropriate location and then check bit 7 (shown as J?7in Table 2-1). If the bit is a 1, the answer to the question given in the table is affirmative.

Note that there is no way to check whether write protection is on or off.

Important You can't read one RAM banl< and write to the other; if you

select either RAM bonk for reading, you get that one for writing as well. However, you can read ROM and write RAM (Figures 2-5 and 2-6), which makes It easy to transfer firmware to bank-switched RAM if you want to use It with a program there.

Bank-switched memory 27

Table 2-1

Bank selector switches

Name

Acflon Hex

Dec

Function

R

$C080

49280

Read RAM; no write; use $D000 bank 2

RR

$C081

49281

Read ROM; write RAM; use$DOOO bank 2

R

$C082

49282

Read ROM; no write; use $D000 bank 2

RR

$C083

49283

Read and write RAM; use $D000 bank 2

R

$C088

49288

Read RAM; no write; use $D000 bank 1

RR

$C089

49289

Read ROM; write RAM; use$D000 bank 1

R

$C08A

49290

Read ROM; no write; use $DOO0 bank 1

RR

$C08B

49291

Read and write RAM; use $D000 bank 1

RdBnk2 R7

$con

49169

Read whether $D000 bank 2 (1) or bank 1 (0)

RdLCRAM R7

$C012

49170

Read RAM (1) or ROM CO)

AltZP W

$C008

49160

Off: Use main bank, page $00 and page $01

AltZP W

$C009

49161

On: Use auxiliary bank, page $00 and page $01

RdAltZP R7

$C016

49174

Read whether auxiliary (1) or main (0) bank

28

CJ-iapter 2: Memory Organization and Controi

Select memory: W $C008 R or R

ROM

Turn off AltZP $C082 Read ROM, use bank 2* $C08A Read ROM, use bank 1 *

Main RAM Auxiliary RAM I 1 I

Select memory: W $C009 R $C082

ROM

Page $01 C Page $00 C

Bank 1

Bank 2

Bank

1

Bank 2

Turn on AltZP Read ROM, use bank 2* or R $C08A Read ROM, use bank 1*

Main RAM Auxiliary RAM

r

1 r

n

Bank

1

Bank 2

Bank 1

Bank 2

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 0

R7 $C01 1 "Read Bank2; bit 7 = 1 or 0

R7 $C012 Read EnLCRAM; bit 7 = 0

Read resulting status:

R7 $0016 Read AitZP; bit 7 = 1

R7 $C011 * Read Bank2; bit 7 = 1 or 0

R7 SC0 1 2 Read EnLCRAM; bit 7 = 0

Legend:

Read memory [ = Inactive memory

Write memory R = Read

R7 = Read, check bit 7 W = Write

RR Read twice in succession

Figure 2-4

Read ROM

Bank-switched memory

29

Select memory:

W $C008 Turn off AltZP RR $C089

Select memory:

W $C009 Turn on AltZP RR $C089

ROM

Main RAM Auxiliary RAM I 1 I 1

ROM

Page $01 Q Page $00 C

^

1 1 1

Bank 2

Bank 1

Bank 2

Main RAM Auxiliary RAM

1 I

Bank

1

Bank 2

Read resulting status:

R7 $C016 Read AltZP: bit 7 = 0 R7 $CC11 Read Bank2: bit7 = 0 R7 $CC12 Read EnLCRAM: bit 7 = 0

Read resulting status:

R7 $00 16 Read AltZP; bit 7 = 1 R7 $C01 1 Read Bank2; bit 7 = 0 R7 $C012 Read EnLCRAM; bit 7 = 0

Legend:

= Read memory | | = Inactive memory R7 = Read, check bit 7 RR = Read twice in succession

= Write memory R = Read W = Write

Figure 2-5

Read ROM, write RAM, and use first $D0 bank

30

Chapter 2: Memory Organization and Control

Select memory. W $C008 RR $C081

Turn off AltZP

Select memory:

W $C009 Turn on AltZP RR $C081

c a CO _

Page $01 Q Page $00 C

Main RAM

Auxiliary RAM I 1

ROM

Bank 1

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 0 R7 $C01 1 Read Bank2; bit 7 = 1 R7 $C012 Read EnLCRAM; bit 7 - 0

Bank 2

Main RAM Auxiliary RAM I 1 I

Bank 1

Bank 2

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 1

R7 $C01 1 Read Bank2; bit 7 = 1

R7 $C012 Read EnLCRAM; bit7 = 0

Legend:

= Read memory | | = Inactive memory R7 = Read, cfieck bit 7

= Write memory R = Read W = Write

RR = Read twice In succession

Figure 2-6

Read ROM, write RAM, and use second SDObank

Bank-switched memory

31

Select memory:

W $C008 Turn off AltZP R $C088

Select memory: W $C009 R $C088

Turn on AltZP

ROM

Main RAM

Auxiliary RAM

ROM

Main RAM

Auxiliary RAM

CQ

Page $01 Q Page $00 C

p

^Bank^

Bank 2

Bank 1

Bank 2

Bank 1

Bank 2

^

1

9

Bank 2

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 0 R7 $C01 1 Read Bank2; bit 7 = 0 R7 $C012 Read EnLCRAM; bit 7 = 1

Read resulting status:

R7 $0016 Read AltZP; bit 7 - 1 R7 $cai1 Read Bank2; bit 7 = 0 R7 $0012 Read EnLCRAM; bit7 = 1

Legend:

= Read memory = Write memory

[ [ = Inactive memory R = Read

R7 = Read, ctieck bit 7 W = Write

RR = Read twice in succession

Figure 2-7

Read RAM and use first $D0 bank

32

Chapter 2; Memory Organization and Controi

Select memory:

W $C008 Turn off AltZP R $COSO

Select memory:

W $C009 Turn on AltZP R $C080

ROM

1

o

F

<D

^

■D

(1)

£

O

%

CO

c

(0

m

Main RAM Auxiliary RAM I 1 I 1

ROM I 1

Main RAM Auxiliary RAM I 1 I 1

Page $01 C Page $00 C

w

Bank 1

1 2 1

^$$$'$$$9t

SXffiffiS!

Bank 1

Bank 2

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 0 R7 $C01 1 Read Bank2; bit 7 - 1 R7 $C012 Read EnLCRAM; bit 7 = 1

Bank 1

Bank 2

1

Bank 1

|Bank|

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 1

R7 $C01 1 Read Bank2; bit 7 = 1

R7 $CC12 Read EnLCRAM; bit 7 = 1

Legend:

= Read memory [ H = Inactive memory

= Write memory R = Read

R7 = Read, check bit 7 W = Write

RR = Read twice in succession

Figure 2-8

Read RAM and use second $D0 bank

Bank-switched memory

33

Select memory; W $C008 RR $C08B

Turn off AltZP

Select memory: W $C009 RR $C08B

Turn on AltZP

ROM

Main RAM

Auxiliary RAM

ROM

Main RAM

Auxiliary RAM

Page $01 1^ Page $00 C

r

Ssankg » 1 S

Bank 2

Bank

1

Bank 2

Bank 1

Bank 2

SBankg

Bank 2

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 0 R7 $C011 Read Bank2; bit 7 = 0 R7 $C012 Read EnLCRAM; bit 7 = 1

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 1 R7 $C011 Read BankZ; bit 7 = 0 R7 $C012 Read EnLCRAM; bit 7 = 1

Legend:

Read memory Write memory

R

Inactive memory Read

R7 = Read, ctieck bit 7 W = Write

RR = Read twice in succession

Figure 2-9

Read and write RAM and use first $D0 banl<

34

Chapter 2: Memory Organization and Control

Select memory:

W $C008 Turn off AltZP RR $C083

Select memory: W $C009 RR $C083

Turn on AltZP

ROM I 1

Main RAM Auxiliary RAM I 1 I 1

ROM

I 1

Main RAM

Auxiliary RAM

o

E

5

Page $01 [^ Page $00 C

m

Bank

1

ZBank^

Bank 1

Bank 2

Bank

1

Bank 2

9

Ma

Bank

1

y/////A

:^Bank^

UA

'^99S>5^5^

'i$S$SSSS^

Read resulting status:

R7 $0016 Read AltZP; bit 7 = 0 R7 $C01 1 Read Bank2; bit 7 = 1 R7 $C012 Read EnLCRAM; bit 7 = 1

Read resulting status:

R7 $C016 Read AltZP; bit 7 = 1 R7 $C011 Read Bank2; bit 7 = 0 R7 $C012 Read EnLCRAM; bit 7 = 1

Legend:

Read memory | | = Inactive memory

Write memory R = Read

R7 = Read, check bit 7 W = Write

RR = Read twice in succession

Figure 2-10

Read and write RAM and use second $D0 bank

Bank-switched memory

35

48K memory

The 48K memory space (actually, 47. 5K) extends from location $0200 to location $BFFF (Figure 2-11) in both main and auxiliary RAM. The amount of storage available in this address space depends on what language or operating system you are using, and what video display needs your program has.

Page allocations

Most of the Apple He's 48K RAM is available for storing your programs and data. However, a few RAM pages are reserved for the use of the Monitor firmware, the Applesoft BASIC interpreter, and whatever video display you may select.

Important The system does not prevent your using ttiese pages, but if you do use them, you must be carefui not to disturb the system data they contain.

A buffer is any storage area set aside for one program or device to put information into and another to take information out of at a different time or rate,

Page $02 (the input buffer)

The GetLn input routine uses page $02 as its keyboard-input buffer. The size of this buffer (256 bytes) sets the maximum size of input strings read by Applesoft or the Monitor. If you know that you won't be typing any long input strings (more than, say, 30 characters), you can store temporary data at the upper end of page $02.

i?efer to Appendix D and to the appropriate programmer and reference manuais for operating system use of page $03.

Global storage refers to an area reserved for information that programs use in common. Vectors— the addresses of speciai routines— are exampies of this l<ind of information. See "The Reset Routine" about the giobai storage and vectors found on page $03.

Page $03 (global storage and vectors)

The Monitor and operating systems use parts of page $03 for global storage and vectors. Table 2-7, later in this chapter, shows the part of page $03 the built-in firmware uses.

Pages $04-$07 (text and low-resolution Page 1)

The most often used display buffer is the text and low-resolution graphics Page 1 (TLPl in Figure 2-11), which occupies main memory pages $04 through $07. It is not usable for program and data storage if you are using Monitor routines or Applesoft, or with almost any other program that uses text or low-resolution display.

36

Chapter 2; Memory Organization and Control

r

Bank- Switched Memory

ROM/HW

$FFFF

V $D000 r $CFFF

r-

I

$cooo

$BFFF

$6000 '^SSFFF

High- Resolution * Pages

$4000_ $3FFF

v$2000 $1FFF"

r $obff

Text and J $0800_ Low-Resolution t $07FF

P^9^^ U0400_

i^ $0200

{$oiff" $0000

Figure 2- n

48K memory map

Main RAM

I ^ J

Auxiliary RAM

I- -r -I

HRP2

HRP1

TLP2

TLP1 =

HRP2X

HRP1X

TLP2X

TLP1X =

Main Memory Screen Holes

Auxiliary Memory Screen Holes

48K memory

37

See "Port Screen Hole RAM Space' in Chapter 3.

Text and low-resolution Page IX (TLPIX) is an identical display page occupying auxiliary memory pages $04 through $07. This pair of text and low-resolution graphics pages are used together to produce 80-column text display.

There are 128 locations in pages $04 through $07 (.64 in main RAM, 64 in auxiliary RAM) that are not displayed on the screen. These locations are called screen holes.

Warning The screen holes are reserved for use by the built-in firmware.

Pages $08-$0B (text and low-resolution Page 2)

The second text and low-resolution graphics display buffer, TLP2, occupies main memory pages $08 through $0B. Most programs do not use Page 2 for displays, but TLP2 is there for display use if required.

Text and low-resolution Page 2X (TLP2X) is an identical display buffer occupying pages $08 through $0B in auxiliary memory.

Note that Apple lie firmware does not provide a way to use the second pair of text and low-resolution graphics pages for 80- column text display.

For more on serial port 2, see Chapter 8.

Page $08 (communication port buffers)

Serial port 2 uses the first half of auxiliary memory page $08 (addresses $0800 through $087F) as a keyboard input buffer, and the second half of the page (addresses $0880 through $08FF) as a serial input buffer. These buffers increase the data transfer rates possible with the serial communication port. Appendix E explains how to use these features. If your program does not use this page for buffers, it can use it as part of TLP2X.

See Chapter 5.

Pages $20-$3F (tiigti-resoiution Page 1)

The primary high-resolution graphics display buffer, high- resolution Page 1 (HRPl), occupies the 32 memory pages from $20 through $3F Oocations $2000 through $3FFF). If your program doesn't use high-resolution graphics, this area is usable for programs or data.

High-resolution Page IX (HRPIX) is an identical display page occupying auxiliary memory pages $20 through $3F.

The Apple lie can display double high-resolution graphics by interleaving HRPl and HRPIX.

38

Chapter 2: Memory Organization and Control

For more Information about ttie display buffers, see CInapter 5.

Pages $40-$5F (high-resolution Page 2)

High-resolution Page 2 occupies main memory pages $40 through $5F Oocations $4000 through $5FFF). Most programs use this area for program or data storage, but it is also available as a second high-resolution page.

High-resolution Page 2X (HRP2X) occupies auxiliary memory pages $40 through $5F.

Apple lie firmware provides high-resolution graphics routines for HRPl and HRP2 only. Refer to the Applesoft BASIC Programmer's Reference Manual.

For details, refer to "Using Display Memory Switches.'

Using 48K memory switches

Two switches select main or auxiliary RAM in the 48K memory space: RAMRd determines which to use for reading, and RAMWrt determines which to use for writing. When these switches are on, they select auxiliary memory. When they are off, they select main memory. (This discussion assumes that the SOStore switch, used to control display memory, is off.)

Each switch has three locations assigned to it (Table 2-2): one to turn it on, one to turn it off, and a third to read its state. Because the memory locations for turning the switches on and off are shared with keyboard reading functions, you must write to these addresses to use them for memory switching. For each switch, you can read bit 7 at its third location to check whether the switch is on or off If the switch is on, bit 7 is 1; if the switch is off, bit 7 is 0.

Table 2-2

48K memory

switches

Name

Action

Hex

Dec

Function

RAMRd

W

$C002

49154

Off: Read main 48K RAM

RAMRd

W

$C003

49155

On: Read auxiliary 48K RAM

RdRAMRd

R7

$C013

49171

Read whether main (0) or aux. (1)

RAMWrt

W

$C004

49156

Off: Write to main 48K RAM

RAMWrt

W

$C005

49157

On: Write to auxiliary 48K RAM

RdRAMWrt

R7

$C014

49172

Read whether main (0) or aux. (1)

Note: SOStore must be off to switch all memory in this range, including display memory (Table 2-6).

48K memor/ 39

Select memory: W $C000 W $C002 W $C005

Main RAM I 1

Turn off SOStore

Read from main memory

Write to auxiliary memory

Auxiliary RAM

' '

r 1

I 1 1

I I I I 1 1

<HRP2X<;

^HRP1XS

^^v.

.TLP2X •TLP1X

Select memory: W $C0O0 W $C003 W $C004

Main RAM

Turn off SOStore

Read from auxiliary memory

Write to main memory

Auxiliary RAM

I 1

I 1

I i I

/HRP2X/

:;hrpix^

:^////.\

^^

f\

I I

TLP2X TLPIX

Read resulting status:

R7 SC0 18 Read SOStore; bit 7 = 0 R7 $C013 Read RAMRd; bit 7 = 0 R7 $C014 Read RAMWrt; bit 7 = 1

Read resulting status:

R7 $C018 Read SOStore; bit 7 = 0 R7 $C013 Read RAMRd; bit7 = 1 R7 $C014 Read RAMWrt; bit 7 = 0

Legend:

^ ^ Read memory ^ = Write memory

I I = Inactive memory R7 = Read, check bit 7

R = Read W = Write

Figure 2-12

48K RAM selection, split pairs

40

Chapter 2: Memory Organization and Control

Select memory:

W $C000 Turn off eostore

W $C002 W $C004 Main RAM I 1

I 1 1

Read from main memory

Write to main memory

Auxiliary RAM

I 1

I 1

I I

I 1 1

I I I I 1 1

I I

HRP2X

HRP1X

TLP2X ■TLP1X

Read resulting status: R7 $C018 Read SOStore; bit 7 = 0 R7 $C013 Read RAMRd; bit 7 = 0 R7 $C014 Read RAMWrt; bit 7 = 0

Select memory:

W $C000 Turn off SOStore

W $C003 W $C005 Main RAM I 1

I 1

I I I 1

HRP2

HRP1

Read from auxiliary memory Write to auxiliary memory Auxiliary RAM

r

1

/ TLP2X ■^TLPIX

<^P^

^^^

TLP2

TLP1

' '

Read resulting status:

R7 $C018 Read SOStore; bit 7 = 0 R7 $C013 Read RAMRd; bit7 = 1 R7 $C014 Read RAMWrt; bit 7 = 1

Legend:

= Read memory = Write memory

= inactive memory = Read

R7 W

= Read, check bit 7 = Write

V/a

R

m

Figure 2-13

48 K RAM selection, one side oniy

48K memory

41

Transfers between main and auxiliary memory

If you want to write assembly-language programs that use auxiliary memory but you don't want to manage the auxiliary memory yourself, you can use the built-in 48K RAM transfer routines. These routines Oisted in Table 2-3) make it possible to move between main and auxiliary memory without having to manipulate the soft switches described earlier in this chapter.

Important The routines described below mal<e it easier to use auxiliary

memory, but they do not protect you from errors. You stiii have to pian your use of auxiliary memory to avoid catastrophic effects on your program.

Table 2-3

48K RAM transfer routines

Name

Action Hex

Function

MoveAux JSR $C3n

XFer JMP $C314

Move data blocks between main and auxiliary 48K memory.

Transfer program control between main and auxiliary 48K memory.

Transferring data

In your assembly-language programs, you can use the built-in routine named MoveAux to copy blocks of data from main memory to auxiliary memory or from auxiliary memory to main memory. Before calling this routine, you must put the data addresses into byte pairs in page $00 and set or clear the carry bit to select the direction of the move.

Warning Don't try to use MoveAux to copy data In bank-sv\/itched memory (page $00, page $01 , or pages $D0 through $FF), MoveAux uses page $00 all during the copy.

The pairs of bytes you use for passing addresses to this routine are called Al, A2, and A4, and they are used for parameter passing by several of the Apple He's built-in routines. The addresses of these byte pairs are shown in Table 2-4.

Put the addresses of the first and last bytes of the block of memory you want to copy into Al and A2. Put the starting address of the block of memory you want to copy the data to into A4.

42

Chapter 2: Memory Organization and Control

Table 2-4

Parameters for MoveAux routine

Name Location Parameter passed

Carry

AIL

$3C

AlH

$3D

A2L

$3E

A2H

$3F

A4L

$42

A4H

$43

X, Y,

A

1 = Move from main to auxiliary memory. 0 = Move from auxiliary to toain memory. Source starting address, low-order byte. Source starting address, high-order byte. Source ending address, low-order byte. Source ending address, high-order byte. Destination starting address, low-order byte. Destination starting address, high-order byte. These registers are preserved.

The MoveAux routine uses the carry bit to select the direction to copy the data. To copy data from main memory to auxiliary memory, set the carry bit (SEC instruction); to copy data from auxiliary memory to main memory, dear the carry bit (CLC instruction).

When you make the subroutine call to MoveAux, the subroutine copies the block of data as specified by the A register and the carry bit. When it is finished, the accumulator and the X and Y registers are just as they were when you called it.

Transferring control

You can use the built-in routine named XFer to transfer control to and from program segments in auxiliary memory. You must set up three parameters before using XFer: the address of the routine you are transferring to, the direction of the transfer, and which page $00 and stack you want to use (Table 2-5).

Table 2-5

Parameters for XFer routine

Name

Location Parameter passed

Carry

Overflow

$03ED $03EE X, Y, A

1 = Transfer from main to auxiliary memory.

0 = Transfer from auxiliary to main memory.

1 = Use page $00 and stack in auxiliary memory.

0 = Use page $00 and stack in main memory. Program starting address, low-order byte. Program starting address, high-order byte. These registers are preserved.

48K memory

43

Put the transfer address into the two bytes at locations $03ED and $03EE, with the low-order byte first, as usual. The direction of the transfer is controlled by the carry bit: set the carry bit to transfer to a program in auxiliary memory; clear the carry bit to transfer to a program in main memory.

Use the overflow bit to select which page $00 and stack you want to use: clear the overflow bit to use the main memory; set the overflow bit (cause an overflow condition) to use the auxiliary memory.

After you have set up the parameters, pass control to the XFer routine by a jump instruction, rather than a subroutine call.

Warning It is your responsibility as the programmer to save the current stack pointer before using XFer and to restore it after regaining control. Failure to do so will cause program errors. Refer to Appendix E for Instructions on how to do this.

Using display memory switches

Selection of main or auxiliary RAM for the 48K memory space is described earlier in this chapter. However, under many circumstances your program may want to control reading and writing to display pages separately. The switches discussed in this section override the effects of RAMRd and RAMWrt for display pages only.

Three switches are involved in the display page selection process. Each of them has three locations assigned to it: one to turn it on, one to turn it off, and a third to read its state (Table 2-6). One of the switches, SOStore, shares its on and off addresses with a keyboard reading function. As a result, your program must write to these locations to turn the switch on and off.

44 Chapter 2: Memory Organization and Control

Table 2-6

Display memory switches

Name

Action Hex

Dec

Function

SOStore W

SOStore W

RdSOStore R7 Page2 R

Page2 R

RdPage2 R7

HiRes R

HiRes

R

RdHiRes R7

lOUDis W

lOUDis W

$C000 49152

$C001 49153

$C018 49176 $C054 49236 $C055 49237

$C01C 49180

$C056 49238

$C057 49239

$C01D 49181

$C07E 49278

$C07F 49279

Off: RAMRd and RAMWrt determine RAM locations.

On: Page2 switcties between TLPl and TLPIX, and Of HiRes on) between HRPl and HRPIX.

Read whether 80Store on CD or off CO).

Off: Select TLPl and HRPl.

On: If 80Store off, switch to TLP2, and Cif HiRes on) to HRP2. If 80Store on, switch to TLPIX, and Cif HiRes on) to HRPIX.

Read whether Page2 on CD or off CO).

Off: Display text and low-resolution page.

On: Display high- resolution pages; make Page2 switch between high-resolution pages.

Read whether HiRes on CD or off CO).

On: Disable lOU access for addresses$C058 to $C05F; enable access to DHiRes switch*.

Off: Enable lOU access for addresses $C058 to $C05F; disable access to DHiRes switch*.

48K memory

45

Table 2-6 (continued) Display memory switches

Name

Action Hex

Dec

Function

RdlOUDis R7

DHiRes

DHiRes

R/W

$C07E 49278 $C05E 49246

R/W $C05F 49247

RdDHiRes R7

$C07F 49279

Read lOUDis switch -a=ofOt

On: (If lOUDis on) turn on double high- resolution.

Off: (If lOUDis on) turn off double high- resolution.

Read DHiRes switch (l=on)t.

* The firmware normally leaves lOUDis on.

t Reading or writing any address in the range $CO70-$CO7F also triggers the paddle timer and resets VBlInt (see Chapter 9).

For each switch, you can read bit 7 at its third location to check whether the switch is on or off. If the switch is on, bit 7 is 1; if the switch is off, bit 7 is 0.

Here is how these switches work for reading and writing:

n If HiRes is off, then Page2 switches between text and low- resolution graphics pages (TLP) only. If HiRes is on, then Page2 switches between TLP and high-resolution graphics pages (HRP).

n If SOStore is off, RAMRd and RAMWrt (Table 2-2) determine whether main or auxiliary RAM locations are used. Page2 selects pages for display (Chapter 5), but not for reading and writing.

n If SOStore is on, it overrides RAMRd and RAMWrt with respect to the display pages selected by HiRes and Page2 (Figures 2-14 and 2-15).

46

Chapter 2: Memory Organization and Controi

High-Resolution Graphics Pages

Text and Low-Resolution r

Select memory: W $C001 R $C056 R $C054

Main RAM I 1

I . 1

I I I

I 1 1

I I

Graphics Pages ^^^^ XLP 1

HRP2

HRP1

Turn on SOStore Turn off HiRes Turn off Page2

Auxiliary RAM 1 1

I 1

I I

I , 1

'TLP2

I I

HRP2X

HRP1X

I I

-TLP2X

■TLP1X

Select memory: W $C001 R $C056 R $0055

Main RAM I 1

I— L-l

HRP2

HRP1

iiiiiiiiiirT

Turn on SOStore Turn off HiRes Turn on Page2

Auxiliary RAM I 1

I I

■TLP2 ■TLP1

I

I 1-

I I I |__j 1

I

H

HRP2X

HRP1X

TLP2X •TLP1X

Read resulting status:

R7 $0018 Read SOStore; bit 7 = 1 R7 $00 ID Read HiRes; bit 7 = 0 R7 $O01C Read Page2; bit 7 = 0

Read resulting status:

R7 $0018 Read SOStore; bit 7 = 1 R7 $001D Read HiRes; bit 7 = 0 R7 $0010 Read Page2; bit 7 = 1

^ ^

= Read memory = Write memory

R = W = R7 =

Read Write Read, checl< bit 7

Legend:

= Inactive memory

RAMWrt (See Figs. 2-1 2 and 2-1 3)

=igure 2-14

^age2 selections, SOStore on and HiRes off

48K memory

47

High-Resolution Graphics Pages

Text and Low-Resolution F Graphics Pages

Select memory: W $C001 R $C057 R $C054 Main RAM

r

I

■T 1

I I

-> 1

HRP2

^^^TLP1

Turn on SOStore Turn on HiRes Turn off Page2

Auxiliary RAM

I 1

I 1

I I

I . 1

-TLP2

1

mil

HRP2X

mil

HRP1X

III null III

I I

TLP2X

•TLP1X

Select memory: W $C001 R $0057 R $0055 Main RAM

Turn on SOStore Turn on HiRes Turn on Page2

Auxiliary RAM

II II

HRP1

Mill

I . I

•TLP2 •TLP1

I I I

I ' 1

I I

HRP2X

Read resulting status: R7 $0018 Read SOStore; bit 7 = 1 R7 $00 ID Read HiRes; bit 7 = 1 R7 $0010 Read Page2; bit 7 = 0

Read resulting status:

R7 $0018 Read SOStore; bit 7 = 1 R7 SOOID Read HiRes; bit 7 = 1 R7 $0010 Read Page2; bit 7 = 1

Legend: P

1

/^ = Read memory N^ = Write memory

= Inactive memory

Controlled by RAMRd and

RAM Wrt (See Figs. 2-1 2 and 2-1 3)

R = W = R7 =

Read Write Read, check bit 7

Figure 2-15

Page2 selections, SOStore on and HiRes on

48

Chapter 2: Memory Organization and Control

The reset routine

A procedure called the reset routine (Figure 2-1 6) puts the Apple lie into a known state when it has just been turned on or when you hold down Control while pressing Reset. The reset routine puts the Apple lie into its normal operating mode and restarts the program indicated at locations $03F2 and $03F3 (Table 2-7).

When you initiate a reset, hardware in the Apple He sets the memory-controlling soft switches to normal: main ROM and RAM are enabled, auxiliary RAM is disabled and the bank-switched memory space is set up to read from ROM and write to RAM, using the second bank at $D000.

c

Power On {Cold Start)

3

- Read/write main 48K RAM

- Read ROM. write main bank- switched RAM, use bank 2

- Display 40-column text page $01 , primary character set, normal format, cursor at bottom left

- Enable access to DHiRes switch

- I/O links: keyboard input (KSW), display output (CSW)

- Ports: startup settings

- Clear keyboard strobe

- Sound the speaker

Write trash in one location

per memory page (including

power-up validity byle|

Restart program with variables intact

Restart Applesoft or Integer BASIC

says to do

Clear screen; display

Apple I Ic, load reset

vector and power-up byte;

initiate disk startup

firmware

Figure 2-16

Reset routine flovi/chart

The reset routine

49

Table 2-7

Page $03 vectors

The reset vector validity check is described under "The Reset Vector.'

Vector address Vector function

$03F0 (1008) $03F1 (1009)

$03F2 (1010)

$03F4 (1012)

$03F5 (1013) $03F6 (1014)

$03F7 (1015)

$03F8 (1016) $03F9 (1017) $03FA (1018)

$03FB (1019) $03FC (1020) $03FD (1021)

$03FE (1022) $03FF (1023)

Address of the subroutine ttiat handles BRK requests (normally $59, $FA)

Reset vector (see text) $03F3 (1011)

Power-up byte (see text)

Jump instruction to the subroutine that handles Applesoft and commands (normally $4C,$58,$FF)

Jump instruction to the subroutine that handles user Control-Y commands

Jump instruction to the subroutine that handles nonmaskable interrupts (not used on Apple lie)

Interrupt vector (address of the subroutine that handles interrupt requests) (Appendix E)

The reset routine sets the display-controlling soft switches to display 40-column text Page 1 using the primary character set, then sets the display window equal to the full 40-column display, puts the cursor at the bottom of the screen, and sets the text display format to normal.

The reset routine also sets the keyboard and display as the standard input and output devices (Chapter 3). It masks mouse interrupts and sets mouse defaults (Table 9-1). Finally, it enables DHiRes switch access (by turning on lOUDis), clears the keyboard strobe, and sounds the speaker.

The Apple lie has three types of reset: power-on reset, also called cold-start reset; warm-start reset; and forced cold-start reset. The procedure descrit)ed above is the same for any type of reset. What happens next depends on the reset vector. The reset routine checks the reset vector to determine whether it is valid or not. If the reset was caused by turning the power on, the vector will not be valid, and the reset routine will perform the cold-start procedure. If the vector is valid, the routine will perform the warm-start procedure.

50

Chapter 2: Memor/ Organization and Control

The cold-start procedure (power on)

If the reset vector is not valid, either the Apple lie has just been turned on or something has caused memory contents to be changed. The reset routine clears the display and puts the string Apple© lie at the top of the display. It loads the reset vector and the validity-check byte, then initiates the startup routine that resides in the disk controller firmware. The bootstrap routine then loads whatever operating system resides on the disk in the built-in drive. When the operating system has been loaded, it displays other messages on the screen. If there is no disk in the disk drive, the drive motor keeps spinning for a brief time. Then the firmware shuts it off and displays the message Check Disk Drive at the bottom of the screen.

If you press Control-Reset again before the startup procedure is completed, the reset routine continues without using the disk, and passes control to the Applesoft BASIC interpreter.

The warm-start procedure (Control-Reset)

whenever you press Control-Reset when the Apple lie has already completed a cold-start reset, the reset vector is still valid and it is not necessary to reinitialize the entire system. The reset routine simply uses the vector to transfer control to the program it points to, which at power-up is the Applesoft interpreter.

If the vector does point to the Applesoft interpreter, your Applesoft program and variables are still intact. If you are using DOS or ProDOS, that operating system is the resident program and it restarts the BASIC interpreter you were using when you pressed Control-Reset.

Important A program residing oniy in bank-switclied RAM cannot use tiie reset vector to regain controi after a reset, because upon reset tine hardware selects the ROM for reading in the bank-switched memory space.

The reset routine 51

Forced cold start (Open Apple-Control-Reset)

If a program has set the reset vector to point to its own warm-start address, as described below, pressing Control-Reset causes transfer of control to that program. If you want to stop such a program without turning the power off and on, you can force a cold-start reset by holding down Control and Open Apple, then pressing and releasing Reset.

Important When you want to stop a program unconditionally— for example, to start up the Apple He withi some otlier program— you should use the forced cold-start reset. Open Apple-Control-Reset, instead of turning the power off and on.

UniDisk 3.5 You must hold Open Apple down until the built-in drive starts to spin. If you release Open Apple before the drive starts to spin, the Apple lie drops Into BASIC instead of rebooting.

The forced cold-start reset works as follows. First, it destroys the program or data in memory by writing two bytes of arbitrary data into each page of main RAM. The two bytes that get written over in page $03 are the ones that contain the reset vector. The warm-start reset routine finds the error, and so performs a normal cold-start reset.

Note that if you press both Open Apple and Solid Apple during power-up or Control-Reset, built-in exercise code is executed. This code is for production and has no end-user value.

Ihe reset vector

The cold-start reset routine stores the starting address of the built-in Applesoft interpreter, low-order byte first, in the reset vector address at locations $03F2 and $03F3. It then stores a validity-check byte, also called the power-up byte, at location $03F4. The validity- check byte is computed by performing an exclusive-OR of the second byte of the vector with the constant l65 (hexadecimal $A5). Each time you reset the Apple lie, the reset routine uses this byte to determine whether the reset vector is still valid.

52

Chapter 2: Memory Organization and Control

You can change the reset vector so that the reset routine will transfer control to your program instead of to the Applesoft interpreter. For this to work, you must also change the validity-check byte to the exclusive-OR of the high-order byte of your new reset vector with the constant l65 ($A5). If you fail to do this, then the next time you reset the Apple lie, the reset routine will determine that the reset vector is invalid and perform a cold-start reset, eventually transferring control to the disk bootstrap routine or to Applesoft.

There is a subroutine that generates the validity-check byte for the current reset vector. This subroutine, called SetPWRC, is at location $FB6F. When your program finishes, it can return the Apple lie to normal operation by restoring the original reset vector and again calling the subroutine to fix up the validity-check byte.

The reset routine 53

Chapter 3

Introduction to Apple lie I/O

55

This chapter is an introduction to the built-in I/O capabilities of the Apple lie. It outlines

D standard I/O links and their functions

D I/O firmware protocols

n dedicated memory storage locations

n direct I/O

The next six chapters discuss these capabilities in detail.

The standard I/O links

You can use some of the routines in the Apple He's firmware for your own programs. This can save you both program space and the time and effort of writing all your own I/O routines.

To use the built-in firmware routines, your program must perform a JSR to the routine's entry address. The called routine then performs an indirect jump through an address stored somewhere in RAM and begins executing. When the routine has finished doing its work, it returns (with an RTS) to your program at the first instruction following the JSR used to call the routine. Memory locations used for transferring control to other subroutines, such as the indirect jump's address used by the character I/O routine, are sometimes called vectors. In this manual, the locations used for transferring control to the Apple He's I/O subroutines are called the I/O links.

In an Apple lie running without an operating system, each I/O link normally contains the address of the standard input or output subroutine. An operating system will typically place addresses of its own I/O routines in these link locations instead.

By calling the I/O subroutines that then jump to the routines pointed to by the link addresses instead of calling the standard subroutines direcdy, you ensure that your program will work properly with other software, such as the operating system or a device driver. The I/O links contain the addresses of Keyin and COutl if the enhanced video firmware is off (when the display shows a flashing checkerboard cursor), and of C3KeyIn and C3COutl if that firmware is on (when the display shows an inverse solid cursor).

The standard I/O links are two pairs of locations in the Apple lie RAM in the range $36 through $39 that are used for controlling character input and output.

Note: Not all operating systems use the standard I/O links. For example, Apple Pascal does not use them.

56 Chapter 3: Introduction to Apple lie I/O

The link at locations $36 and $37 is called CSW (character output switch). Individually, location $36 is called CSWL (CSW low) and location $37 is called CSWHQZSW high). This link holds the starting address of the subroutine the Apple lie is currently using for single-character output. This address is normally $FDFO, the address of routine COutl.

The Monitor Is discussed In When you issue either a PR#n from BASIC or an n Control-P from

Chapter 10, ^j^g Monitor, the Apple lie changes this link address to the first

address in the ROM space allocated to port n. That address has the form $CnOO. Subsequent calls for character output are thus transferred to the firmware starting at that address. When it has finished, the firmware executes an RTS (return from subroutine) instruction to return control to the calling program. Sometimes a PR#n will cause both input and output switches to be changed (as in the 80-column firmware).

A similar link at locations $38 and $39 is called /TS'W" (keyboard input switch). Individually, location $38 is called KSWL (KSW low) and location $39 is called KSWHQf^SW high). This link holds the starting address of the routine currently being used for single- character input normally $FD1B, the starting address of the standard input routine Keyln.

When you issue an IN#n command from BASIC or an n Control-K from the Monitor, the Apple lie changes the link address in KSW to $CnOO, the beginning of an I/O firmware subroutine. Subsequent calls for character input are thus transferred to that firmware. The firmware puts the input character, with its high bit set, into the accumulator and executes an RTS (return from subroutine) instruction to return control to the program that requested input.

When a disk operating system (EXDS or ProDOS) is running, one or both of the standard I/O links hold addresses of the disk operating system's input and output routines. The operating system has internal locations that hold the addresses of the currently active character input and output routines.

Warning If a program that Is running with DOS or ProDOS changes the standard iink addresses, either directly or via iN# and PR# commands, the operating system may be disconnected from the system. To avoid this problem, when programming in BASIC you should always issue an empty PRINT statement (to be sure that what follows begins a new line) before issuing the PRINT statement containing Controi-D and the IN# or PR# command.

The standard I/O iini<s 57

Refer to the section on input and output link addresses In the operating system manuals for further details.

After changing either CSW or KSW, your assembly-language programs running under DOS should call the subroutine at location $03EA. This subroutine transfers the link address to a location inside the operating system and then restores the operating system link address in the standard link location.

GetLn also provides on-screen editing features. See 'Editing With GetLn.-

Standard input features

The Apple He's firmware includes two different subroutines for reading from the keyboard, RdKey (read key) and GetLn (get line).

RdKey calls the current character input routine (that is, the one whose address is stored at KSW). This is normally Keyin or C3KeyIn, which accepts one character from the keyboard. GetLn accepts a sequence of characters terminated with a carriage return. Thus GetLn allows line-oriented input using the current input routine.

RdKey subroutine

A program can get a character from the keyboard by making a subroutine call to RdKey at memory location $FDOC. RdKey passes control via the input link KSW to the current input subroutine, which is normally Keyln.

RdKey displays a cursor at the current cursor position, which is immediately to the right of whatever character you last sent to the display (normally by using the COut routine, described below).

Keyln subroutine

KeyIn is the standard input subroutine. Wlien your program calls it, Keyln displays a cursor, waits until someone presses a key, then inserts the ASCII code of the key just pressed in the accumulator and returns to the calling program.

If the enhanced video firmware is inactive, Keyln displays a cursor by alternately storing a checkerboard block in the cursor location, storing the original character, then storing the checkerboard again. If the firmware is active, C3KeyIn places a block cursor on the screen by inverting (swapping black for white) the character at the cursor position.

58

Chapter 3: Introduction to Apple He I/O

Keyin ako generates a random number. While it is waiting for the user to press a key, KeyIn repeatedly increments the l6-bit number in memory locations $4E and $4F. This number keeps increasing from 0 to $FFFF (65535), then starts over again at 0. The value of this number changes so rapidly that it is very difRcult to predict what it will be after a key is pressed. A program that reads from the keyboard can use this value as a random number or as a seed for a pseudo-random number routine.

GetLn subroutine

Programs often need strings of characters as input. While you could call RdKey repeatedly to get several characters from the keyboard, there is an easier way to do it The routine that you want to use in this case is named GetLn, and it starts at location $FD6A. Using repeated calls to RdKey, GetLn accepts characters from the standard input subroutine usually KeyIn and puts them into the input buffer located in the memory page from $0200 to $02FF. GetLn also provides you with some basic on-screen editing and control features.

The first thing GetLn does when you call it is to display a prompt The prompt indicates to the user that the program is waiting for input. Different programs use different prompt characters, helping to remind the user which program is requesting the input. Table 3-1 shows the prompt characters xosed by different programs on the Apple lie.

GetLn uses the character stored at memory location $33 as the prompt character. In an assembly-language program, you can change the prompt to any character you wish. In BASIC, changing the prompt character has no effect because both BASIC interpreters and the Monitor restore it each time they request input from the user.

Table 3-1

Prompt characters

Prompt

character Program requesting Input

? User's BASIC program 0NPUT statement)

] Applesoft BASIC (Appendix D)

> Integer BASIC (Appendix D)

* Firmware Monitor (Chapter 10)

Note: Applesoft uses GetLnl ($FD6f) when a program is executing. GetLnl does not print a prompt.

Stardard input features 59

As the user types each character, GetLn sends the character to the standard output routine normally COutl ^which displays it at the current cursor position and then advances the cursor to indicate the next character position. Control characters echoed by GetLn are not executed.

GetLn stores the characters in its buffer, starting at memory location $0200 and using the X register to index the buffer. GetLn continues to accept and display characters until the user presses Return (or Control-X to cancel the line). Then it clears the remainder of the line the cursor is on, stores the carriage-return code to mark the end of the buffer, places the cursor at the beginning of the next line, and returns.

The maximum line-length that GetLn can handle is 255 characters. If the user types more than this, GetLn sends a backslash (\) and a carriage return to the display, cancels the line it has accepted so far, and starts over. To warn the user that the line is getting full, GetLn sounds a bell (tone) at every keypress after the 248th.

Note: The Applesoft interpreter accepts only 239 characters.

Escape codes with GetLn

GetLn has many special functions that you invoke by typing escape codes on the keyboard. An escape code is sent by pressing Escape, releasing it, and then pressing some other key, as shown in Table 3-2.

Important Be sure to release Escape right away. If you hiold It too long, the auto-repeat mechanism begins, which may cancel the Escape,

60 Chapter 3: Introduction to Apple lie I/O

Table 3-2

Escape codes with GetLn

Escape code Function

Escape Clears the window and homes the cursor

(places it in the upper-left corner of the screen); exits from escape mode

Escape A Moves the cursor right one line; exits

or Escape a from escape mode

Escape B Moves the cursor left one line; exits

or Escape b from escape mode

Escape C Moves the cursor down one line; exits

or Escape c from escape mode

Escape D Moves the cursor up one line; exits from

or Escape d escape mode

Escape E Clears to the end of the line; exits from

or Escape e escape mode

Escape F Clears to the bottom of the window; exits

or Escape f from escape mode

Escape I Moves the cursor up one line; remains in

or Escape i escape mode

or Escape Up Arrow

Escape J Moves the cursor left one space; remains

or Escape j in escape mode*

or Escape Left Arrow

Escape K Moves the cursor right one space;

or Escape k remains in escape mode*

or Escape Right Arrow

Escape M Moves the cursor down one line;

or Escape m remains in escape mode*

or Escape Down Arrow

Escape 4 Switches to 40-column mode; sets links to

C3KeyIn and C3COutl; restores normal window size (Table 3-5); exits from escape modef

Standard input features 61

Table 3-2 (continued) Escape codes with GetLn

Escape code Function

Escape 8 Switches to 80-column mode; sets links

to C3KeyIn and C3COutl; restores normal window si2e CTable 3-5); exits from escape model

Escape Control-D Disables control characters; only

carriage return, linefeed, bell, and backspace have an effect when printed

Escape Control-E Reactivates control characters

Escape Control-Q Deactivates the enhanced video

firmware; sets links to Keyin and COutl; restores normal window size (Table 3-5); exits from escape modef

Cursor-control key: see text.

t This code functions only when the enhanced video firmware is active.

In escape mode, you can keep using the arrow keys and the cursor movement keys I, J, K, and M without pressing Escape again. This enables you to perform repeated cursor moves by holding down the appropriate key.

When GetLn is in escape mode, it displays an inverse plus sign as the cursor. You leave escape mode by typing any key other than a cursor movement key.

Note: The escape codes with the arrow keys are the standard cursor movement keys on the Apple lie. The escape codes with I, J, K, and M are the standard cursor movement keys on the Apple II and II Plus, and are present on the Apple lie for compatibility.

Escape sequences can be used in the middle of an input line to change the appearance of the screen. They have no effect on the input line.

62 Chapter 3: Introduction to Apple lie I/O

For an Introduction to editing witin tl^ese features, refer to tl^e Applesoft Tutorial.

Editing wim GetLn

Subroutine GetLn provides the standard on-screen editing features used by the BASIC interpreters and the Monitor. Any program that uses GetLn for reading the keyboard has these features.

Cancel line

Any time you are typing a line, pressing Control-X causes GetLn to cancel the line. GetLn displays a backslash (\) and issues a carriage return, then displays the prompt and waits for you to type a new line. GetLn takes the same action when you type more than 255 characters, as desaibed above.

Backspace

When you press Left Arrow (or Control-H), GetLn moves its buffer pointer back one space, effectively deleting the last character in its buffer. It also sends a backspace character to routine COut, which moves the cursor back one space. If you type another character now, it replaces the character you backspaced over, both on the display and in the line buffer.

Each time you press Left Arrow, it moves the cursor left and deletes another character, until you are back at the beginning of the line. If you then press Left Arrow one more time, you have effectively canceled the line, and GetLn issues a carriage return and displays the prompt. The cursor moves even if the deleted character is an invisible control character. Thus it is possible for screen alignment and buffer alignment to be different.

See 'Escape Codes With GetLn.'

Retype

Right Arrow (or Control-U) has a function that is complementary to the backspace function. When you press Right Arrow, GetLn picks up the character under the cursor just as if it had been typed on the keyboard. You can use this procedure to pick up characters that you just deleted by backspacing across them. You can use the backspace and retype functions with the cursor-motion functions to edit data on the display.

Standard input features

63

standard output features

The standard output routine is named COut (character output). COut normally calls COutl or C3COutl, which sends one character to the display, advances the cursor position, and scrolls the display when necessary. COutl and C3COutl restrict their use of the display to an active area called the text window, described later in this chapter.

COut subroutine

Your program makes a subroutine call to COut at memory location $FDED with a character in the accumulator. COut then passes control via the output link CSW to the current output subroutine, normally COutl or C3COutl, which takes the character in the accumulator and writes it out. If the accumulator contains an uppercase or lowercase letter, a number, or a special character, COutl or C3COutl displays it; if the accumulator contains a control character, COutl or C3COutl either performs one of the special functions described below or ignores the character.

Each time you send a character to COutl or C3COutl, it displays the character at the current cursor position, replacing whatever was there, and then advances the cursor position one space to the right. If the cursor position is already at the right edge of the window, COutl or C3COutl moves it to the leftmost position on the next line down. If this would move the cursor position past the end of the last line in the window, COutl or C3COutl scrolls the display up one line and sets the cursor position at the left end of the new bottom line.

The cursor position is controlled by the values in memory locations $24 and $25. These locations are named CH, for cursor horizontal, and CV, for cursor vertical. COutl and C3COutl do not display a cursor, but the input routines described above do, and they use this cursor position. However, changing CV directly does not change the cursor's vertical position until the next carriage return or reaching the end of the current line causes a call to VTab (for setting the base address within windows). If some other routine displays a cursor, it will not necessarily put it in the cursor position used by COutl or C3COutl.

Warning When the video firmware is set for 80-column display, the value of CH is kept at 0 and the true horizontal position is stored at $057B, When the 80-column video firmware is active, use $057B Instead of CH.

64 Chapter 3; introduction to Apple lie I/O

Escape codes are described under "Escape Codes With GetLn.'

Control characters with COutl

COutl does not display control characters. Instead, the control characters listed in Table 3-3 are used to initiate some action by the firmware. Other control characters are ignored. Most of the functions listed here can also be invoked from the keyboard, either by typing the control character listed or by using the appropriate escape code. The stop-list function, described separately, can only be invoked from the keyboard.

Table 3-3

Control characters witti COutl

Control character

ASCII Apple lie name name

Action taken by COutl

Control-G BEL Bell

Control-H BS Backspace

Control-J LF Line feed

Control-M CR Return

Produces a 1000-Hz tone for 0.1 second

Moves cursor position one space to the left; from left edge of window, moves to right end of line above

Moves cursor position down to next line in window; scrolls if needed

Moves cursor position to left end of next line in window; scrolls if needed

Control characters with C3COutl

when the 80-column firmware is active, COut calls C3COutl instead of COutl for character output. C3COutl does not display control characters, but you can use some control characters to control some of what the routine does. All other control characters are ignored.

The control characters listed in Table 3-4 are used to initiate some action by the firmware. Except for the stop-list function (Control-S) you can send control characters to C3COutl either from a program or from the Apple He's keyboard. The stop-list function can only be invoked from the keyboard. Most of the functions listed here can also be performed by using an equivalent escape code.

Standard output features

65

Table 3-4

Control characters with C3COutl

Control character

ASCII narrw

Apple He name

Action taken by C3COut1

Control-G BEL

Control-H BS

Bell Produces a 1000-Hz tone

for 0.1 second

Backspace Moves cursor position one space to the left; from left edge of window, moves to right end of line above

Control-J

LF

Line feed

Moves cursor position down to next line in window; scrolls if needed

Control-K

VT

Clear EOS

Clears from cursor position to the end of the screen*

Control-L

FF

Home and clear

Moves cursor position to upper-left comer of window and clears window*

Control-M CR

Return Moves cursor position to

left end of next line in window; scrolls if needed

Control-N

SO

Normal

Sets display format normal*

Control-O

SI

Inverse

Sets display format inverse*

Control-Q

DCl

40-column

Sets display to 40-column*

Control-R

DC2

80-column

Sets display to 80-column*

Control-S

DC3

Stop-list

Stops listing characters on the display until another key is pressedt

Control-U

NAK

Quit

Turns off enhanced video firmware*

Control-V

SYN

Scroll

Scrolls the display down one line, leaving the cursor in the current position*

Control-W ETB Scroll-up

Scrolls the display up one line, leaving the cursor in the current position*

66

Chapter 3: Introduction to Apple lie I/O

Table 3-4 (continued)

Control characters with CSCOutl

Control character

ASCII Apple lie name name

Action taken by C3COut1

Control-X CAN Disable

MouseText

Control-Y EM

Home

Control-Z SUB Clear line

Control-! ESC Enable

MouseText

Control- \ FS Fwd. space

Control-

Control-

GS

US

Clear EOL

Up

Doesn't work from the keyboard, t Only works from the keyboard.

Disables MouseText character display, uses inverse uppercase

Moves cursor position to upper-left comer of window (but doesn't clear)*

Clears the line the cursor position is on*

Maps inverse uppercase characters to MouseText characters

Moves cursor position one space to the right; from right edge of window, moves it to left end of line below*

Clears from the current Cursor position to the end of the line (that is, to the right edge of the window)*

Moves cursor up a line, no scroll

The stop-list feature

You can stop the Apple lie from updating its display (if it is using either COutl or C3COutl) by pressing Control-S. Whenever COutl or C3COutl gets a carriage return from the program, it checks the keyboard for a Control-S. If a Control-S has been pressed, CCXitl or C3COutl stops and waits for another key to be pressed before resuming. The character code of the key that is pressed is ignored unless it is Control-C, which is passed to the program. This feature lets you exit BASIC programs from stop-list mode.

Standard output features

67

The text window

The active portion of the display is called the text uHndow. After you start up the computer or perform a reset, the entire display is the text window. COutl or C3COutl puts characters only into the window; when it reaches the end of the last line in the window, it scrolls only the contents of the window.

You can restrict video activity to any rectangular portion of the display by changing the current text window. Your programs can thus control the placement of text in the display and protect other portions of the screen from being written over by new text. To do this, store the appropriate values into four locations in memory to set the top, bottom, left margin, and width of the text window. The following memory locations control the text window:

n The left margin is stored in memory location $20. This number is normally 0, the number of the leftmost column in the display. In a 40-column display, the maximum value for this number is 39 Oiexadecimal $27); in an 80-column display, the maximum value is 79 (hexadecimal $4F).

D The width of the text window is stored in memory location $21. For a 40-column display, this value is normally 40 (hexadecimal $28); for an 80-column display, it is normally 80 (hexadecimal $50).

n The position of the top line of the text window is stored in memory location $22. This is normally 0, the topmost line in the display. Its maximum value is 23 (hexadecimal $17).

D The position of the bottom line of the screen plus 1 is stored in memory location $23. It is normally 24 (hexadecimal $18) for the bottom line of the display. Its minimum value is 1.

Important Pascal does not use this method of supporting window widths,

Warning Be careful not to let the sum of the window width and the

leftmost position in the window exceed the width of the display you are using (40 or 80 columns). If this happens, COutl or C3COutl may put characters into memory locations outside the display page, possibly destroying programs or data.

Table 3-5 summarizes the memory locations and the possible values for the text window parameters.

68

Chapter 3: Introduction to Apple lie i/O

Table 3-5

Text window memotY locations

Normal values

Maximum values

LA/inrlnw

Location

value

40-col.

80-col.

40-col.

80-col.

parameter

Dec

Hex

Dec

Hex

Dec

Hex

Dec

Hex

Dec

Hex

Dec Hex

Left edge

32

$20

00

$00

00

$00

00

$00

39

$27

79 $4F

Width

33

$21

00

$00

40

$28

80

$50

40

$28

80 $50

Top edge

34

$22

00

$00

00

$00

00

$00

23

$17

23 $17

Bottom edge

35

$23

01

$01

24

$18

24

$18

24

$18

24 $18

hesG display character sets are described in Cl^apter 5.

Normal, inverse, and flashing text

The way that the Apple lie displays characters is affected by two things: the value that is stored in the inverse flag (zero page location $32), and whether the enhanced video firmware is off or on. The inverse flag's influence is discussed in the next two subsections.

If the enhanced video firmware is off, the Apple lie displays what is called the primary character set; if the video firmware is on, the Apple lie displays what is called the alternate character set.

The primary character set includes normal Oight on dark), inverse (dark on light), and flashing (alternating normal and inverse) characters. Lowercase inverse characters are not included in the primary character set.

The alternate character set includes normal and inverse characters (including lowercase inverse), and a set of graphic characters called MouseText. Flashing characters are not included in the alternate character set.

If you want your program to display a character, it should first load the character to be displayed in the accumulator, and then call the character-output subroutine COut. For example, to display the character corresponding to $C8, you can use something like this:

LDA #$C8 JSR COut

Standard output features

69

For a brief explanation of logical functions, refer to Appendix H.

Primary character set display

The primary character set is displayed by COutl, which operates only when the enhanced video firmware is off. The primary character set includes text in normal, inverse, or flashing format, but not inverse or flashing lowercase text.

If the value of the character sent to COutl is greater than or equal to $A0, that value is logically ANDed with the value of the inverse flag (at location $32), then displayed. (If you're curious about which ASCII character is being sent, subtract $80 from the value being sent to COutl.) You can use the following inverse flag values:

D $FF (decimal 255) produces the normal character format.

D $3F (decimal 63) produces the inverse character format.

a $7F (decimal 127) produces the flashing character format.

Important To avoid unusual character display results, use only tine three values $3F, $7F, and $FF.

COutl interprets character values from $80 through $9F as control characters and tries to execute them.

Character values from $00 through $7F are all interpreted as display characters, not control characters.

MouseText is described more fully In Ctiapter 5,

Alternate ctiaracter set display

The alternate character set includes normal and inverse format characters and the MouseText graphic characters. You should use C3COutl, the standard output link when the enhanced video firmware is active, to display the alternate character set Here are the rules for using the alternate character set:

n Control characters are not displayed. Characters sent to C3COutl are interpreted as control characters if they are in the range $00 through $ IF or $80 through $9F.

D Characters in the range $20 through $7F and $A0 through $FF are displayed.

D If inverse flag (location $32) bit 7 is 1, the character is normal.

D If inverse flag bit 7 is 0, the character is inverse.

n If MouseText is off, characters $40 through $5F are remapped to the range $00 through $1F and are displayed as uppercase inverse characters.

D If MouseText is on, character values $40 through $5F are left unchanged, and the characters are displayed as MouseText.

Chapter 3; Introduction to Apple lie I/O

See "MouseText" In ChopterS.

70

Port I/O

The Apple lie is a member of the Apple II family of computers; however, unlike the Apple II, H Plus, and He, the Apple lie does not have peripheral connector slots. In place of these, it has ports the equivalent of firmware interface cards installed in slots.

Standard link entry points

To maintain compatibility^ with existing software and its protocols, each port's I/O firmware has the same standard entry points (SCnOO) as its equivalent slot in another Apple II would have. Table 3-6 shows these equivalents, as well as listing the chapter where each port is described.

The section on the standard I/O links desaibes how and when these entry addresses are placed in CSW and KSW. For example, issuing PR#n or IN#n changes the output and input links, respectively, so that subsequent output or input is handled by the firmware starting at address $CnOO, and thus goes to or comes from the selected device.

expansion The

memory expansion version of the Appie iic piaces the

mouse at $C700 and the memory expansion card at $C400,

Table 3-6

Port characteristics

Entry

Port

point

Port connector

Use

Chapter

1

$C100

Serial port 1

Printers

7

2

$C200

Serial port 2

Communication

8

3

$C300

Video connectors

Enhanced video firmware

5

4

$C400

Mouse

Mouse

9

5

$C500

Intelligent disk port devices

6

$C600

Disk drives

Built-in and external drives

6

7

$C700

No device

Reserved

6

Important The addresses shown in Table 3-6

are not entry points in the

sense that you

can send characters to be printed by sending

them to JSi? SCnOO.

Port I/O

71

Firmware protocol

The Apple lie supports a standard firmware protocol that, in addition to the standard link address, provides a table of device identification and entry points to standard and optional firmware subroutines. The protocol is equivalent to the Pascal 1.1 firmware protocol in use on other Apple II's, and is outlined in Table 3-7.

Tablo 3-7

Firmware protocol locations

Address

Value

Description

$Cn05

$38

Pascal firmware card/port identifier.

$Cn07

$18

Pascal firmware card/port identifier.

$CnOB

$01

Generic signature byte of a firmware card/port.

$CnOC

$ci

Device signature byte: i is an identifier (not necessarily unique).

c = device class (not all used on the Apple lie):

$00

reserved

$01

printer

$02

hand control or other X-Y device

$03

serial or parallel I/O card/port

$04

modem

$05

sound or speech device

$06

clock

$07

mass-storage device

$08

80-column card/port

$09

network or bus interface

$0A

special purpose (none of the above)

$OB-OF

reserved

$CnOD

ii

$Cnii is

the initialization entry address (PInit).

$CnOE

rr

$Cnrr is

the read routine entry address (PRead)

(returns character read in A register).

$CnOF

WW

$Cnww is the write routine entry address (PWrite) (enters with character to write in A register).

$CnlO

ss

$Cnss is

the status routine entry address

$Cnll $00

(PStatus) (enters with request code in A register: 0 to ask "Are you ready to accept output'" or 1 to ask "Do you have input ready?").

If additional address bytes follow; nonzero if not.

72

Chapter 3: Introduction to Apple He I/O

Each table begins with identification bytes C$Cn05 through $CnOC). Then, starting with address $CnOD, each byte in the table represents the low-order byte of the entry-point address of a firmware routine. The high-order byte of each address is always $Cn, where n is the port number. Your program uses these byte values to construct its own jump table for subroutine calls to the ports.

All port routines require, on entry, that the X register contain $Cn and that the Y register contain $nO.

All routines, on exit, return an error code in the X register (0 means no error occurred; 3 means the request was invalid). The carry bit in the program status register usually contains a reply to a request code CO means no; 1 means yes).

All the Apple lie ports except the disk port conform to this protocol. The disk port is described in Chapter 6.

For more information, refer to the tiardware page memory map in Appendix B.

Table 3-8

Port I/O locations

Port

Locations

$C090-$C09F

$COAO-$COAF

$COEO-$COEF

Port I/O space

By a convention used in other Apple II series machines, each port or slot has exclusive use of 16 memory locations set aside for data input and output. The addresses of these locations are of the form $C080 + #nO, where n is the port or slot number. Table 3-8 lists the port I/O space used in the Apple lie.

Port ROM space

In the Apple II and lie, one 256-byte page of memory space is allocated to each slot This space is used for read-only memory CROM or PROM on the interface card) with driver programs that control the operation of input/output devices, as outlined in Table 3-7. On the Apple lie, this space is dedicated to port firmware. However, I/O ROM space in the Apple lie is used as efficiently as possible, and there is not a strict correspondence between firmware for port n and the $CnOO space, except as regards entry points.

Port I/O

73

Expansion ROM space

The 2K-byte memory space from $C800 to $CFFF in the Apple lie called expansion ROM space on the Apple II, II Plus, and He contains the enhanced video firmware and port and memory transfer subroutines. The Apple lie, unlike the II, II Plus, or lie, always has this space switched in.

Port screen hole RAM space

There are 128 bytes of memory (64 in main memory, 64 in auxiliary memory) allocated to the ports, eight bytes per port, as shown in Table 3-9. These bytes are reserved for use by the system, except as described in Chapters 4 through 9.

Table 3-9

Port screen hole memory locations

Ports

Base

address

1

2

3

4

5

6

7

$0478

$0479

$047A

$047B

$047C

$047D

$047E

$047F

$04F8

$04F9

$04FA

$04FB

$04FC

$04FD

$04FE

$04FF

$0578

$0579

$057A

$057B

$057C

$057D

$057E

$057F

$05F8

$05F9

$05FA

$05FB

$05FC

$05FD

$05FE

$05FF

$0678

$0679

$067A

$067B

$067C

$067D

$067E

$067F

$06F8

$06F9

$06FA

$06fb

$06FC

$06FD

$06fe

$06ff

$0778

$0779

$077A

$077B

$077C

$077D

$077E

$077F

$07F8

$07F9

$07FA

$07FB

$07FC

$07FD

$07FE

$07FF

These addresses are unused bytes in the RAM reserved for text and low-resolution graphics displays, and hence they are sometimes called screen holes. These particular locations are not displayed on the screen and their contents are not changed by the built-in output routines. In other words, they are used by the output routines but they are not part of the video display.

Waming All thie screen holes In auxiliary memory, and many of them in main memory, are reserved for special use by Apple lie firmware— for example, to store initialization information. Do not use any locations marked reserved in this manual.

The way that port firmware uses these RAM locations and their addresses is covered in Chapters 4 through 10.

74

Chapter 3: Introduction to Apple lie I/O

Interrupts

Appendix E describes Interrupt Intermpts are a way to more efficiently use the hardware in a

handling on the Apple lie. computer. Internipt support built into the Apple He's firmware is

described briefly below.

When the IRQ line on the 65C02 microprocessor is activated, the 65C02 transfers program control through the vector in locations $FFFE through $FFFF of ROM or whichever bank of RAM is switched in (Chapter 2). If ROM is switched in, this vector is the address of the Monitor's interrupt handler, which determines whether the request is due to an interrupt that should be handled internally. If so, the Monitor handles it and then returns control to the interrupted program.

If the interrupt is due to a BRK ($00) instruction, control is transferred through the BRK vector ($03F0-03F1). Otherwise, control is transferred through the IRQ vector ($03FE-$03FF).

Interrupts 75

Chapter 4

Keyboard and Speaker

77

This chapter describes how to use two of the Apple He's built-in devices: the keyboard and the speaker.

Keyboard input

Table 4-1 describes the characteristics of the keyboard that relate to programming. You won't have to write routines to read the keyboard from all your assembly-language programs since the Apple lie firmware Monitor provides keyboard support through the three standard input routines described in Chapter 3 RdKey, Keyin, and GetLn. You can do all your keyboard handling directly in your programs if you want to, but it's nice to know that you're not forced to.

For a description of how the l<eyboard strobe works, refer to Appendix E.

Reading the Iceyboard

The keyboard encoder and ROM (see Chapter 11) can generate all 128 ASCII codes, so all the special character codes in the ASCII character set are available from the keyboard. Your machine- language programs can call RdKey to get characters from the keyboard. RdKey reads characters a byte at a time from the keyboard data location ($COO0) shown in Table 4-1.

Here is how your programs should go about reading the keyboard:

1 . Test bit 7 of address $CO(X) to see if a key has been pressed. Bit 7 is the keyboard strobe bit.

2. When bit 7 goes to a 1, you know that the low-order seven bits of $C000 are a valid character.

3. Clear the keyboard strobe Obit 7) at $C000 by reading or writing anything to address $C010.

$C010 has another function besides clearing the keyboard strobe: its high bit is a 1 while a key is pressed (except the Apple keys, Control, Shift, Caps Lock, and Reset). Bit 7 at this location is therefore called any-key-down. You could use this to let a program do something useful other than just waiting for the next key to be pressed. (People are generally a lot slower than the Apple lie.) Check $C010 occasionally to see if something should be done.

Important

If your program needs to read both ttie l<eyboard flag and the strobe, it must read the strobe bit first. Any time you read the any-key-down bit at $0010, you aiso ciear the keyboard strobe bit at $0000,

78

Chapter 4: Keyboard and Speaker

Table 4-1

Keyboard Input characteristics

Dn gam© Input switches, see Chapter 9,

On GetLn. GetLn 1. and RdKey. see Chapter 3.

Port numbor None

Commands Keyboard is always on, in the sense that any

keypress generates a KSTRB.

Initial Reset routine clears the keyboard strobe and sets

characteristics the keyboard as the standard input device (that is, sets KSW to point to RdKey).

Hardware locations

$C000 Keyboard data and strobe

$C010 Any-key-down flag and clear-strobe switch

$C060 40-column switch status on bit 7; 1 = 40-column

display = switch down

$C06l Open Apple status on bit 7; 1 = pressed (also

game input switch 0)

$C062 Solid Apple status on bit 7; 1 = pressed

Monitor firmware routines

Location

$FD6A

Name

GetLn

$FD67

GetLnZ

$fd6f

GetLnl

$FD1B

Keyin

$FD35

RdChar

$FDOC

RdKey

Description

Gets an input line with prompt

Gets an input line with preceding carriage return

Gets an input line, but with no preceding prompt

The keyboard input subroutine

Gets an input character or escape code

The standard character input subroutine

Use of ottier pages

Page 2 The standard character string input buffer (see GetLn

description)

After your program has cleared the keyboard strobe, the strobe remains low until another key is pressed.

Table 4-2 shows the ASCII codes generated by all the keys on the Apple lie keyboard. Remember, if the strobe bit is set, the character values that your program sees will be equal to the values given in Table 4-2 plus $80.

Keyboard input

79

Tablo 4-2

Keys and ASCII codes

Key alone

■I- Control

+ Shift

+ Both

Key

Code

1 Char

Code

» Char

Code

» Char

Code

» Char

Delete

7F

DEL

7F

DEL

7F

DEL

7F

DEL

Left Arrow

08

BS

08

BS

08

BS

08

BS

Tab

09

HT

09

HT

09

HT

09

HT

Down Arrow

OA

LF

OA

LF

OA

LF

OA

LF

Up Arrow

OB

VT

OB

VT

OB

VT

OB

VT

Return

OD

CR

OD

CR

OD

CR

OD

CR

Right Arrow

15

NAK

15

NAK

15

NAK

15

NAK

Escape

IB

ESC

IB

ESC

IB

ESC

IB

ESC

Space

20

SP

20

SP

20

SP

20

SP

1 It

27

1

27

f

22

H

22

M

, <

2C

»

2C

1

3C

<

3C

<

- _

2D

-

IF

US

5F

IF

US

. >

2E

2E

,

3E

>

3E

>

/?

2F

/

2F

/

3F

?

3F

7

0)

30

0

30

0

29

)

29

)

1 !

31

1

31

1

21

I

21

I

2@

32

2

00

NUL

40

@

00

NUL

3#

33

3

33

3

23

#

23

#

4$

34

4

34

4

24

$

24

$

5%

35

5

35

5

25

%

25

%

6a

36

6

IE

RS

5E

A

IE

RS

7&

37

7

37

7

26

&

26

&

8

38

8

38

8

2A

*

2A

*

9(

39 3B

9

»

39 3B

9

f

28 3A

(

28 3A

(

= +

3D

ss

3D

=

2B

+

2B

+

[{

5B

[

IB

ESC

7B

{

IB

ESC

\ 1

5C

\

IC

FS

7C

1

IC

FS

]}

5D

]

ID

GS

7D

}

ID

GS

! ~

60

!

60

!

7E

~

7E

~

A

61

a

01

SOH

41

A

01

SOH

B

62

b

02

STX

42

B

02

STX

C

63

c

03

ETX

43

C

03

ETX

D

64

d

04

EOT

4A

D

04

EOT

E

65

e

05

ENQ

45

E

05

ENQ

F

66

f

06

ACK

A6

F

06

ACK

G

61

g

07

BEL

47

G

07

BEL

H

68

h

08

BS

48

H

08

BS

I

69

i

09

HT

49

I

09

HT

80

Chapter 4; Keyboard and Speaker

Table 4-2 (continued) Keys and ASCII codes

Key alone Code Char

+ Control

+ Shift

+ Both

Key

Code

1 Char

Code

1 Char

Code

> Char

J

6A

J

OA

LF

4A

J

OA

LF

K

6B

k

OB

VT

4B

K

OB

VT

L

6C

1

OC

FF

4C

L

OC

FF

M

6D

m

OD

CR

4D

M

OD

CR

N

6e

n

OE

SO

4E

N

OE

SO

O

6f

o

OF

SI

4F

O

OF

SI

P

70

P

10

DLE

50

P

10

DLE

Q

71

q

11

DCl

51

Q

11

DCl

R

72

r

12

DC2

52

R

12

DC2

S

73

s

13

DC3

53

S

13

DC3

T

74

t

14

DC4

54

T

14

DC4

U

75

u

15

NAK

55

U

15

NAK

V

76

V

16

SYN

56

V

16

SYN

w

77

w

17

ETB

57

W

17

ETB

X

78

X

18

CAN

58

X

18

CAN

Y

79

y

19

EM

59

Y

19

EM

Z

7A

z

lA

SUB

5A

Z

lA

SUB

Note: Codes are in hexadecimal here; refer to Table G-8 for decimal equivalents.

Keystrol<©s con also generate interrupts. See Appendix E.

The reset routine Is described In Cl-iapter 2.

For information on how to have programs interpret l<eystroi<es In standard way, refer to the Apple II Design Guidelines listed n the Bibliography.

There are several keys that do not generate ASCII codes themselves, but alter the characters produced by other keys. These modifier keys are Control, Shift, and Caps Lock.

Your programs can also use the Open Apple and Solid Apple as character modifier keys while handling keyboard input, and, if one or both of them are pressed, branch to a special routine, such as a help program. Your program can read Open Apple at $C06l and Solid Apple at $C062.

Another key that doesn't generate a code is Reset, located at the upper-left corner of the keyboard; it is connected directly to the Apple He's processor. Pressing Reset with Control depressed normally causes the system to stop whatever program it's running and restart itself. If you hold Open Apple while pressing Control- Reset, the Apple lie performs a forced cold start. The restart sequence is described in Chapter 2.

Keyboard input

81

Monitor firmware support for keyboard input

Chapter 3 describes the three standard Monitor input routines serving the keyboard: GetLn, RdKey, and Keyln. This section discusses the three other available Monitor routines.

GetLnZ

GetLnZ (at address $FD67) is an alternate entry point for GetLn that first sends a carriage return to the standard output, then continues into GetLn.

GetLn 1

GetLn 1 (at address $FD6F) is an alternate entry point for GetLn that does not issue a prompt before it accepts the input line. However, if the user cancels the input line with too many backspaces or with Control-X, then GetLnl issues the prompt stored at location $33 when it gets another line.

RdChar

RdChar (at address $FD35) is a subroutine that gets characters from the standard input subroutine, and also interprets the escape codes listed in Chapter 3.

If the enhanced video firmware is active. Right Arrow (Control-U) reads a character from the screen as if it were typed from the keyboard. This is a function of the Monitor's built-in editing capability described in Chapter 3.

Electrical specifications of the speaker circuit appear In Chapter 1 1 .

Speaker output

The Apple lie has a small speaker mounted near the front of the bottom plate of its case. The speaker is connected to a soft switch that toggles; that is, the switch has two states, off and on, and it changes from one to the other each time it is accessed. Table 4-3 describes the speaker output characteristics.

82

Chapter 4: Keyboard and Speaker

Table 4-3

Speaker output characteristics

Port number None.

Commands Some programs sound the speaker in response to

Control-G.

Initial Reset routine sounds the speaker,

characteristics

Hardware iocation

$C030 Toggle speaker (read only).

Monitor firmware routines

Location Name Description

$FBDD Belli Sends a beep to the speaker.

$FF3A Bell

Sends Control-G to the current output.

Using the speaker

If you switch the speaker once, by reading or writing to $C030, it emits a click; to make longer sounds, access the speaker repeatedly. The switch for the speaker uses memory location $C030. You can make various tones and buzzes with the speaker by using combinations of timing loops in your program.

Important You shouid always use a read operation to toggle the speaker. If you write to this soft switch, it switches twice In rapid succession. The resulting pulse is so short that the speaker doesn't have time to respond; It doesn't make a sound.

Speaker output

83

Monitor firmware support for spealcer output

See Chapter 3. The Monitor supports the speaker with one simple routine, Belli. A

related routine, Bell, supports the current output device the one that CSW points to.

BeUl

Belli (at address $FDBB) makes a beep through the speaker by generating a l-kHz tone in the Apple He's speaker for 0.1 second. This routine scrambles the A and X registers.

Bell

The Monitor routine Bell (at location $FF3A) writes a bell control character (ASCII Control-G) to the current output device. This routine leaves the accumulator holding $87.

84 Chapter 4; Keyboard and Speaker

Chapter 5

Video

Display

Output

85

NTSC stands for National Television Standards Committee, a group that formulates broadcast and reception guidelines used by ttie USA and several other countries.

The Apple He's primary output device is its video display. You can use any ordinary color or monochrome video monitor with the Apple lie. An ordinary monitor is one that accepts NTSC- compatible composite video. If you use Apple lie color graphics with a black-and-white monitor, the display vwU appear as black, white, and two shades of gray.

If you are only using graphics modes and 40-cblumn text, you can use a television set for your video display. If the TV set has an input connector for composite video, you can connect it directly to your Apple IIc; otherwise, you must attach an RF video modulator between the Apple IIc and the television set

Important The Apple IIc can produce an 80-column text display. However, if you use an ordinary color or black-and-white television set, 80-column text will be too blurry to read. For a clear 80-coiumn display, you must use a high-resolution video monitor with a bandwidth of 14 MHz or greater.

Table 5-1 summarizes the video output port's characteristics and points to other information in this chapter.

Table 5-1

Video output port characteristics

Port number

Commands

Initial characteristics

Hardware locations

Monitor firmware routines

I/O firmware entry points

Output port 3.

See Figure 5-3.

See Figure 5-3.

Note: If a program is to use the enhanced video firmware, it should turn it on and then immediately check the 80/40 switch. If the switch is in the 40 position, the program should issue a Control-Q.

See Table 5-7. See Table 5-11. See Table 5-12.

86

Chapter 5: Video Display Output

Video display specifications

Table 5-2 summarizes the video display's specifications, and provides a further guide to other information in this chapter.

Table 5-2

Video display specifications

Display modes

Text capacity

Ctioracter set

Display formats

Low-resolution graphics

High-resolution graphics

Double high-resolution graphics

40-column text; map: Figure 5-5 80-column text; map: Figure 5-6

Low-resolution color graphics; map: Figure 5-7

High-resolution color graphics; map: Figure 5-8

Double high-resolution color graphics; map: Figure 5-9

24 lines by 80 columns (character positions)

96 ASCII characters (uppercase and lowercase)

Normal, inverse, flashing, MouseText CTable 5-3)

16 colors (Table 5-4): 40 horizontal by 48 vertical; map: Figure 5-7

6 colors (Table 5-5): 140 horizontal by 192 vertical (restricted)

Black and white: 280 horizontal by 192 vertical; map: Figure 5-8

16 colors (Table 5-6): 140 horizontal by 192 vertical (no restrictions)

Black and white: 560 horizontal by 192 vertical; map: Figure 5-9

See "Video Output Signals" In Chapter 1 1 for more on video expansion hardware.

The video signal produced by the Apple He is NTSC-compatible composite color video available at two places on the back panel of the Apple IIc: the RCA-type phono jack and the 15-pin D-type connector. Use the RCA-type phono jack to connect a video monitor, and the DB-15 connector for an external video modulator or other video expansion hardware.

Video display specifications

87

Text modes

Either of the Apple He's two text modes can display all 96 ASCII characters: uppercase and lowercase letters, the ten digits, punctuation marks, and special characters. Each character is displayed in an area of the screen that is seven dots wide by eight dots high. The characters are formed by a dot matrix five dots wide (with a few exceptions, such as underscore), leaving two blank columns of dots between characters in a row. Except for lowercase letters with descenders, the characters are only seven dots high, leaving one blank line of dots between rows of characters.

The normal display has white (or other monochrome color used by your monitor) dots on a dark background. Characters can also be displayed as black dots on a white background; this is called inverse video.

Text Character sets

The Apple lie can display either of two text character sets: the primary set and an alternate set (Table 5-3). The forms of the characters in the two sets are actually the same, but the available display formats are different. The display formats are

' n normal, with white dots on a black screen

D inverse, with black dots on a white screen

n flashing, alternating between normal and inverse

The Apple He can display uppercase characters in all three formats normal, inverse, and flashing ^with the primary character set. Lowercase letters can only be displayed in normal format. This makes the primary character set compatible with most software written for the Apple II and II Plus, which can display text in flashing format but don't have lowercase characters.

The alternate character set trades the flashing format for a complete set of inverse characters. With the alternate character set, the See "MouseText." Apple lie can display uppercase letters, lowercase letters, numbers,

and special characters in either normal format or inverse format It can also display MouseText.

88 Chapter 5: Video Display Output

To identify particular characters and vaiues, refer to Table 4-2.

You can select between character sets with the alternate-text soft switch, described later in this chapter. Table 5-3 shows the character codes in decimal and hexadecimal for the Apple lie primary and alternate character sets in normal, inverse, and flashing formats.

Table 5-3

Display character sets

Hex

Primary character set Character type Format

Alternate character set

values

Character type

Format

$00-$ IF

Uppercase letters

Inverse

Uppercase letters

Inverse

$20-$3F

Special characters

Inverse

Special characters

Inverse

$40-$ 5F

Uppercase letters

Flashing

MouseText

$60-$7F

Special characters

Flashing

Lowercase letters

Inverse

$80-$9F

Uppercase letters

Normal

Uppercase letters

Normal

$AO-$BF

Special characters

Normal

Special character

Normal

$CO-$DF

Uppercase letters

Normal

Uppercase letters

Normal

$EO-$FF

Lowercase letters

Normal

Lowercase letters

Normal

Each character on the screen is stored as one byte of display data. The low-order six bits make up the ASCII code of the character being displayed. The remaining two (high-order) bits select format and the group within ASCII.

Text modes

89

MouseText

The alternate character set contains 32 graphics characters called MouseText in place of the primary set's inverse uppercase characters from $40 through $5F. These graphics are especially convenient to use with a mouse because they can be generated by character codes instead of groups of high-resolution byte values, and they can be moved around quickly. To use MouseText characters, do the following:

1. Turn on the enhanced video firmware with PR#3 or 6 Control-P.

2. Set inverse mode: use the INVERSE command or put $3F in location $32, or print Control-O.

3. Turn on MouseText with PRINT CHR$(27); or pass $1B to COut in the accumulator.

4. Print the uppercase letter (or other ASCII character in the range $40 throu^ $5F:@[ \] A or _ ) that corresponds to the MouseText character you want.

5. Turn off MouseText with PRINT CHR$(24); or pass $18 to COutl in the accumulator.

6. Set normal mode: use the NORMAL command or put $FF in location $32, or print a Control-N.

Here is a sample Applesoft program that prints all the MouseText characters:

10 D$=CHR$(4)

20 PRINT PRINT D$;"PR#3"

30 INVERSE

40 PRINT CHR$ (27) ;"ABCDEFGHIJKLMNOPQRSTUVWXYZ []'■_",

50 PRINT CHR$(24) ;

60 NORMAL

MouseText characters and their corresponding ASCII characters are shown in Figure 5-1.

90 Chapter 5: Video Display Output

Ik 6 K Z ^ B HI

@ A B C D E F

<- ... >|. t ~ 4J

H I J K L M

ilL ^ ^ - L ^

M N

P Q

I z

U

Jl-

-.r 3

[ \ ]

Figure 5-1

MouseText characters

w

40-column versus 80-column text

The Apple lie has two text display modes: 40-column and 80- column. The number of dots in each character does not change, but the characters in 80-column mode are only half as wide as the characters in 40-column mode. Compare the two displays in Figure 5-2. On an ordinary color or black-and-white television set, the narrow characters in the 80-column display blur together; you must use the 40-column mode to display text on a television set.

Text modes

91

]LIST 0,100

10 REM APPLESOFT CHARACTER DEMO

20 TEXT : HOME

30 PRINT : PRINT "Applesoft Char

acter Demo" 40 PRINT : PRINT "Which characte

r set " 50 PRINT : INPUT "Primary (P) or

Alternate (A) ?";A$ 60 IF LEN (A$) < 1 THEN 50 65 LET A$ = LEFT$ (A$,l) 70 IF A$ = "P" THEN POKE 4 9166,

0 80 IF A$ = "A" THEN POKE 4 9167,

0 90 PRINT : PRINT "...printing th

e same line, first" 100 PRINT " in NORMAL, then INVE RSE ,then FLASH:": PRINT

]LIST

10 REM APPLESOFT CHARACTER DEMO

20 TEXT : HOME

30 PRINT : PRINT "Applesoft Character Demo"

40 PRINT : PRINT "Which character set "

50 PRINT : INPUT "Primary (P) or Alternate (A) ?";A$

60 IF LEN (A$) < 1 THEN 50

70 LET A$ = LEFT$ (A$, 1)

80 IF A$ = "P" THEN POKE 4 9166,0

90 IF A$ = "A" THEN POKE 4 9167,0

100 PRINT : PRINT "...printing the same line, first"

150 PRINT " in NORMAL, then INVERSE ,then FLASH:": PRINT

160 NORMAL : GOSUB 1000

17 0 INVERSE : GOSUB 1000

180 FLASH : GOSUB 1000

190 NORMAL : PRINT : PRINT : PRINT "Press any key to repeat." GET A$

200 GOTO 10

1000 PRINT : PRINT "SAMPLE TEXT: Now is the time 12:00"

1100 RETURN

]■

Figure 5-2

40-column and 80-column text with alternate character set

92 Chapter 5: Video Display Output

Figure 5-3 shows the characteristics of the text display modes and how to switch between them.

Power On

(ESC)(C0NTR0r)-rQl Resets full 40-column window

Cursor: square box Input hook: C3Keyln Output hook: C3C0ut1 Window: 40 columns,

24 lines Character Set: alternate

Figure 5-3

Text mode characteristics and switclning

Cursor: narrow box Input hook: C3Keyln Output hook: CSCOutI Window: 80 columns,

24 lines Character Set: alternate

Text modes

93

Graphics modes

Table 5-4

Low-resolution

graphics

colors

Nibble value

Dec

Hex

Color

0

$00

Black

1

$01

Magenta

2

$02

Dark blue

3

$03

Purple

4

$04

Dark green

5

$05

Gray 1

6

$06

Medium blue

7

$07

Light blue

8

$08

Brown

9

$09

Orange

10

$0A

Gray 2

11

$0B

Pink

12

$0C

Light green

13

$0D

Yellow

14

$0E

Aquamarine

15

$0F

White

The Apple lie can produce color video graphics in any of three different modes:

D low-resolution graphics, 48 rows by 40 columns

n high-resolution graphics, 192 rows by 280 columns

n double high-resolution graphics, 192 rows by 560 columns

Each graphics mode treats the screen as a rectangular array of spots. Normally, your programs will use the features of some high- level language to draw graphics dots, lines, and shapes on the screen; this section describes the way the resulting graphics data are stored in the Apple He's memory.

Low-resolution graphics

The Apple lie displays an array of 48 rows by 40 columns of colored blocks in the low-resolution graphics mode. Each block can be any one of sixteen colors, including black and white. On a black-and- white monitor or television set, these colors appear as black, white, and two shades of gray. There are no blank dots between blocks; adjacent blocks of the same color merge to make a larger shape.

The low-resolution graphics display data are stored in the same part of memory as the data for the 40-column text display. Each byte contains data for two low-resolution graphics blocks. The two blocks are displayed one atop the other in a display space the same size as a 40-column text character, seven dots wide by eight dots high.

Half a byte four bits, or one nibble is assigned to each graphics block. Each nibble can have a value from 0 to 15, and this value determines which one of sixteen colors appears on the screen. The colors and their corresponding nibble values are shown in Table 5-4. In each byte, the low-order nibble sets the color for the top block of the pair, and the high-order nibble sets the color for the bottom block. Thus, a byte containing the hexadecimal value $D8 produces a brown block atop a yellow block on the screen.

Note: colors may vary, depending on adjustment of monitor or television set.

94

Chapter 5: Video Display Output

As explained earlier in this chapter, the text display and the low- resolution graphics display use the same area in memory. Your programs should usually dear this part of memory when they change display modes, but you can store data as text and display them as graphics, or vice versa. All you have to do is change the mode switch, described later in this chapter, without changing the display data. This usually produces meaningless jumbles on the display, but some programs have used this technique to good advantage for producing complex low-resolution graphics displays quickly.

High-resolution graphics

In the high-resolution graphics mode, the Apple lie displays an array of colored dots in 192 rows and 280 columns. The colors available are black, white, purple, green, orange, and blue, although the colors of the individual dots are limited, as described below, by the color of adjacent dots. Adjacent dots of the same color merge to form a continuous colored area.

High-resolution graphics display data are stored in either of two 8192-byte areas in memory. These areas are called high-resolution Page 1 and Page 2; think of them as display data buffers. Normally, your programs will use the features of some high-level language to draw graphics dots, lines, and shapes to display, this section describes the way the resulting graphics data are stored in the Apple He's memory.

The Apple lie high-resolution graphics display is bit-mapped: each dot on the screen corresponds to a bit in the Apple He's memory. The seven low-order bits of each display byte control a row of seven adjacent dots on the screen, and 40 adjacent bytes in memory control a row of 280 (7 times 40) dots. The eighth bit (the most significant) of each byte is not displayed; it selects one of two color sets, as described below. The least significant bit of each byte is displayed as the leftmost dot in a row of seven, followed by the next- least significant bit, and so on, as shown in Figure 5-4.

Graphics modes 95

0

Bits in Data Byte

0

1

2

3

4

5

6

Dots on Graphics Screen

Figure 5-4

High-resolution display bits

There is a simple correspondence between bits in memory and dots on the screen on a black-and-white monitor. A dot is white if the bit controlling it is on (1), and the dot is black if the bit is off (0). On a black-and-white television set, pairs of dots merge together; alternating black and white dots merge to a continuous gray.

A dot whose controlling bit is off (0) is black on an NTSC color monitor or a color television set. If the bit is on, the dot is white or a color, depending on its position, the dots on either side, and the setting of the high-order bit of the byte. Call the leftmost column of dots column 0, and assume (for the moment) that the high-order bits of all the data bytes are off (0). If the bits that control them are on, dots in even-numbered columns, 0, 2, 4, and so forth, are purple, and dots in odd-numbered columns are green ^but only if the dots on either sidfe are black. If two adjacent dots are both on, they are both white.

You select the other two colors, blue and orange, by turning the high-order bit 0>it 7) of a data byte on (1). The colored dots controlled by a byte with the high-order bit on are either blue or orange: the dots in even-numbered columns are blue, and the dots in odd-numbered columns are orange (again, only if the dots on either side are black). Within each horizontal line of seven dots controlled by a single byte, you can have black, white, and one pair of colors. To change the color of any dot to one of the other pair of colors, you must change the high-order bit of its byte, which affects the colors of all seven dots controlled by the byte.

96

Chapter 5: Video Display Output

In brief, high-resolution graphics displayed on a color monitor or television set are made up of colored dots, according to the following rules:

n Dots in even-numbered columns can be black, purple, or blue.

n Dots in odd-numbered columns can be black, green, or orange.

n If adjacent dots in a row are both on, they are both white.

n The colors in each row of seven dots controlled by a single byte are either purple and green, or blue and orange, depending on whether the high-order bit is off (0) or on (1).

These rules are summarized in Table 5-5. The blacks and whites are numbered to remind you that the high-order bit is different.

Table 5-5

High-resolution graphics colors

Bits 0-6

Bit 7 off Bit 7 on

Adjacent columns off Even columns on Odd columns on Adjacent columns on

Black 1 Black 2

Purple Blue

Green Orange

White 1 White 2

Note: Colors may vary, depending on adjustment of monitor or television set.

For more details about the way the Apple lie produces color on a TV set, see Chapter 11. For a table of reversed bit patterns, refer to Appendix H.

The peculiar behavior of the high-resolution colors reflects in part the way NTSC color television works. The dots that make up the Apple lie video signal are spaced to coincide with the frequency of the color subcarrier used in the NTSC system. Alternating on and off dots at this spacing cause a color monitor or TV set to produce color, but two or more on dots together do not.

Double high -resolution graphics

The horizontal resolution of double high-resolution graphics is 560 dots per line, with 192 lines. Double high-resolution graphics maps the low-order seven bits of the bytes in the two double high- resolution graphics pages. A double high-resolution page is made up of a 8192-byte page in main memory and an equivalent page having the same address in auxiliary memory. In most cases, only the first double high-resolution graphics page is used.

Graphics modes

97

The bytes in the main-memory and auxiliary-memory pages are displayed in exactly the same manner as the charaaers in 80- column text: of each pair of identical addresses, the auxiliary- memory byte is displayed first, and the main-memory byte is displayed second. A dot whose controlling bit is off (0) is black when displayed.

Unlike high-resolution color, double high-resolution color has no restrictions on which colors can be adjacent. Color is determined by any four adjacent dots along a line. Think of a four-dot-wide window moving across the screen: at any given time, the color displayed corresponds to the 4-bit value from Table 5-6 that corresponds to the windov/s position (Figure 5-9). Effective horizontal resolution with color is 140 (560 divided by 4).

Table 5-6 describes the data values used to produce colors in double high-resolution graphics. To use the table, divide the column number by four and use the remainder to find the correct column: abO is a byte residing in auxiliary memory corresponding to a remainder of 0 Obyte 0, 4, 8, and so on), mbl is a byte residing in main memory corresponding to a remainder of 1 (byte 1, 2, 9 and so on), and similarly for ab2 and mb3-

Mixed-mode displays

Any of the graphics displays can have four lines of text, either 40- column or 80-column, at the bottom of the screen. Graphics displays with text at the bottom are called mixed-mode displays. To use them, the TEXT switch must be off (read $C050) and the MIXED switch on (read $C053).

Important You cannot display 40-column text wltti double tilgh-resoiution graphiics.

To determine what appears where in mixed-mode displays, refer to Figures 5-5 through 5-9 later in this chapter. See the bottom sixth of the appropriate text display (Figure 5-5 or 5-6) and the upper five- sixths (down to the heavy horizontal line) in the appropriate graphics display (Figures 5-7 to 5-9).

98 Chapter 5: Video Display Output

Table 5-6

Double high-resolution graphics colors

Repeated

Color

abO

mbl

ab2

mb3

bit pattern

Black

$00

$00

$00

$00

0000

Magenta

$08

$11

$22

$44

0001

Brown

$44

$08

$11

$22

0010

Orange

$4C

$19

$33

$66

0011

Dark green

$22

$44

$08

$11

0100

Gray 1

$2A

$55

$2A

$55

0101

Green

$66

$4C

$19

$33

0110

Yellow

$6e

$5D

$3B

$77

0111

Dark blue

$11

$22

$44

$08

1000

Purple

$19

$33

$66

$4C

1001

Gray 2

$55

$2A

$55

$2A

1010

Pink

$5D

$3B

$77

$6e

1011

Medium blue

$33

$66

$40

$19

1100

Light blue

$3B

$77

$6e

$5D

1101

Aqua

$77

$6E

$5D

$3B

1110

White

•$7F

$7F

$7F

$7F

nil

Note: Colors may vary, depending on adjustment of monitor or television set.

Display pages

The Apple lie uses data stored in specific areas in memory to generate its video displays. These areas, called display pages, serve as buffers where your programs can put data to be displayed. Each byte in a display buffer controls an object a character, a colored block, or a group of adjacent dots ^at a certain location on the display, depending on the current display mode.

Display pages

99

The 40-column-text and low-resolution-graphics modes use two display pages of 1024 bytes each. These are called text Page 1 and text Page 2, and they are located at $0400 through $07FF and $0800 through $OBFF in main memory. Normally, only Page 1 is used, but you can put text or graphics data into Page 2 and switch between displays. Either page can be displayed as 40-colunin text, low- resolution graphics, or mixed-mode (four lines of text at the bottom of a graphics display).

The 80-column text mode displays twice as much data as the 40- column mode 1920 bytes but it cannot switch pages when the enhanced video firmware is active. The 80-column text display uses a combination page made up of text Page 1 in main memory plus another page in auxiliary memory. This additional memory is not the same as text Page 2 in fact, it is text Page IX, and it occupies the same address space as text Page 1 (see Figure 2-11). The built-in firmware I/O routines descnised in Chapter 3 take care of this extra addressing automatically, that is one reason to use these routines for all normal text output.

Important The built-in video firmware aiways displays Page 1 text. You cannot write text to Page 2 witti thie built-in firmware.

The high-resolution graphics mode also has two display pages, but each page is 8192 bytes long. In the 40-column text and low- resolution graphics modes each byte controls a display area seven dots wide by eight dots high. In high-resolution graphics mode each byte controls an area seven dots wide by one dot high. Thus, a high-resolution display requires eight times as much data storage as a low-resolution display, as shown in Table 5-7.

The double high-resolution graphics mode interleaves the two high-resolution pages (Pages 1 and IX) in exactly the same way as 80-column text mode interleaves the text pages: column 0 and all subsequent even-numbered columns come from the auxiliary page; column 1 and all subsequent odd-numbered columns come from the main page.

100 Chapter 5: Video Display Output

Table 5-7

Video display page

locations

Display mods

Display page

Lowest address

Highest address

40-column text,

low-resolution

graphics

1 2*

$0400 1024 $0800 2048

$07FF 2047 $OBFF 3071

80-column text

1 2*

$0400 1024 $0800 2048

$07FF 2047 $ObFF 3071

High-resolution graphics

1 2

$2000 8192 $4000 16384

$3FFF 16385 $5FFF 24575

Double high- resolution graphics

It 2t

$2000 8192 $4000 6384

$3FFF 16383 $5FFF 24575

This is not supported by firmware; for instructions on how to switch

pages, refer to "Display Mode Switching." t See "Double High-Resolution Graphics."

Display mode switching

Table 5-8 shows the reserved locations for the soft switches that control the different display modes. The column of the table labeled Action indicates what to do to activate or read a switch setting: J? means read the location, W means write anything to the location, R/Wmezns read or write, and i?7 means read the location and then check bit 7.

Table 5-9 lists the display modes that the firmware can set up automatically. In the 40-column modes, the contents of the standard I/O hooks KSW and CSW (Chapter 3) determine whether the enhanced video firmware features are available or not. The firmware also takes care of setting or clearing AltChar.

Table 5-10 lists other display modes available but not supported by firmware. For modes that display Page 2 with the 80Col switch on, your program may have to turn SOStore off after the firmware has turned it on.

Double low-resolution shows on the display screen when HiRes is off and both 80Col and DHiRes are on. It is the low-resolution graphics equivalent of 80-column text, and it uses the same map (Figure 5-6), giving you 48 rows of 80 blocks.

Display mode svi/itching

101

The lOUDis ($C07E) switch must be on to allow you to use locations $C05E and $C05F to change DHiRes. The firmware in fact leaves it on ^and your program should, too unless it wants to use locations $C05E and $C05F to change mouse values (Chapter 9).

Table 5-8

Display soft switches

Nam«

Acflon

Hex

Function

AltChar W $CO0E Off: Display text using

primary character set

AltChar W $COOF On: Display text using

alternate character set

Read AltChar switch (1 = on)

Off: Display 40 columns

On: Display 80 columns

Read 80Col switch (1 = on)

Off: Cause Page2 on to select auxiliary RAM

SOStore W $C001 On: Allow Page2 to switch

main RAM areas

RdAltChar

R7

$C01E

80Col

W

$C00C

SOCol

W

$C00D

RdSOCol

R7

$C01F

SOStore

W

$C000

RdSOStore

R7

$C018

Read 80Store switch (1 = on)

Page2

R/W

$C054

Off: Select Page 1

Page2

R/W

$C055

On: Select Page IX (SOStore on) or 2

RdPage2

R7

$C01C

Read Page2 switch (1 = on)

TEXT

R/W

$C050

Off: Display graphics or (if MIXED on) mixed

TEXT

R/W

$C051

On: Display text

RdTEXT

R7

$C01A

Read TEXT switch (1 = on)

MIXED

R/W

$C053

Off: Display only text or only graphics

102

Chapter 5: Video Display Output

Table 5-8 (continued) Display soft switches

Name

Action

Hex

Function

RdMIXED R7 HiRes R/W

MIXED R/W $C054 On: Of TEXT off) display text

and graphics

$C01B Read MIXED switch (1 = on)

$C057 Off: Of TEXT off) display low-resolution graphics

HiRes R/W $C058 On: (If TEXT off) display

high-resolution or (if DHiRes on) double high-resolution graphics

RdHiRes R7 $C01D Read HiRes switch (1 = on)

lOUDis W $C07E On: Disable lOU access for

addresses $C058 to $C05F; enable access to DHiRes switch

lOUDis W $C07F Off: Enable lOU access for

addresses $C058 to $C05F; disable access to DHiRes switch*

RdlOUDis R7 $C07E Read lOUDis switch (1 = off)t

DHiRes R/W $C05E On: (If lOUDis on) turn on

double high-resolution

DHiRes R/W $C05F Off: Of lOUDis on) turn off

double high-resolution

RdDHiRes R7 $C07F Read DHiRes switch (1 = on)t

The firmware normally leaves lOUDis on. See also the following

footnote, t Reading or writing any address in the range $C070-$C07F also triggers

the paddle timer and resets VBLInt (Chapter 9).

Display mode switching

103

Table 5-9

Display modes supported by firmware. Including Applesoft

Display

Switches

col/res

Type

Page

8OC0I

eostore

Page2

TEXT

MIXED

HiRes

DHIRes

40-column

Text

Off

Off

On

Off

Off

Off

SO-column

Text

On

On

Low-res

Graphics

Off

Off

Off

Off

Off

Off

40/low

Mixed

Off

Off

Off

On

Off

so/low

Mixed

On

*

Off

Off

On

Off

Off

Hi-res

Graphics

Off

Off

Off

Off

On

Hi-res

Graphics

Off

On

Off

Off

On

40/high

Mixed

Off

Off

Off

On

On

80/high

Mixed

On

*

Off

Off

On

On

Off

SOStore is set

by the firmware when 8OC0I is

turned on.

Table 5-10

Other display modes

Display

Switches

coi/res

Type

Page

8OC0I

80Store

Page2

TEXT

MIXED

HIRes

DHIRes

40-column

Text

2

Off

On

On

80-column

2

On

Off

On

On

Low-res

Graphics

2

Off

On

Off

Off

Off

40/low

Mixed

2

Off

On

Off

On

Off

80/low

Mixed

2

On

Off

On

Off

On