c64 Programmers Reference Guide 00 Toc Introduction
c64 Programmers Reference Guide 00 Toc Introduction
==- COMPUTER
FIRST EDITION
THIRD PRINTING-1983
This manual is copyrighted and contains proprietary information. No part of this publica-
tion may be reproduced, stored in a retrieval system, or transmitted in any form or by any
means, eledronic, mechanical, photocopying, recording, or otherwise, without the prior
written permission of COMMODORE BUSINESS MACHINES, Inc.
ii
TABLE OF CONTENTS
INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Relational Operators 12
Logical Operators .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. ~3
Hierarchy of Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Stri ng Operations 16
String Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 17
. Programming Techniques 18
Data Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
iii
3. PROGRAMMING GRAPHICS ON THE
COMMODORE64 ... 99
· Graphics Overview 100
Character Display Modes . . . . . . . . . . . . . . . . . . . . . . . . . .. 100
Bit Map Modes 100
Sprites 100
· GraphicsLocations 101
VideoBankSelection.. . . . . . .. ... . . ... . .. . . ... . .. .. 101
Screen Memory 102
ColorMemory .1.. . . .. . . .. . . .. 103
Character Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 103
. Standard Character Mode. . . . . . . . . . . . . . . . . . . . . . . . . . .. 107
Character Definitions 107
· Programmable Characters 108
. Multi-Color Mode Graphics 115
Multi-Color Mode Bit 115
. Extended Background Color Mode 120
. BitMappedGraphics... . ... . ... ... . . . .. . . ... . '" . ... 121
Standard High-Resolution Bit Map Mode. . ... . ... . 122
HowIt Works. . . .. . . ... ... . ... . . .. . . . . . . ... . .. 122
. Multi-ColorBitMap Mode. . .. . .. . . . . . . . . . . .. . . .. . . ... 127
. SmoothScrolling 128
. Sprites 131
Defining a Sprite 131
Sprite Poi nters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 133
Turning Sprites On 134
Turning Sprites Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 135
Colors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 135
Multi-Color Mode 135
Setting a Sprite to Multi-Color Mode. . . . . . .. . . ... . ... 136
Expanded Sprites 136
Sprite Positioning 137
Sprite PositioningSummary. . . . . . . . . . . . . . . . . . . . . . . .. 143
SpriteDisplayPriorities.. . . . . . . . . . . . . . . . . . . . . . . . . .. 144
Collision Detects 144
. Other Graphics Features 150
Screen Blanking 150
Raster Reg ister . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 150
Interrupt Status Register. . . . . . . . . . . . . . . . . . . . . . . . . . .. 151
Suggested Screen and Character Color Combinations... 152
iv
. Programming Sprites-Another Look 153
Making Sprites in BASIC-A Short Program. ... . .. . . .. 153
Crunching Your Sprite Programs 156
Positioning Sprites on the Screen 157
Sprite Priorities .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 161
Drawing a Sprite 162
Creati ng a Sprite . . . Step by Step 163
Moving Your Sprite on the Screen. . .. . . .. . . .. . ... . . .. 165
VerticalScrolling.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 166
The Dancing Mouse-A Sprite Program Example. . . . . .. 166
EasySpritemakingChart.. . ... . ... . . .. . ... . . ... 176
SpritemakingNotes. . . .. . . .. . ... . . .. . . .. . . .. . ... . .. 177
5. BASICTOMACHINELANGUAGE 209
. What is Machine Language? . .. . .. . . ... . . .. . . ... ... . .. 210
What Does Machine Code Look Like? . . . . . . . . . . . . . . . .. 211
Simple Memory Map of the Commodore 64 . . . . . . . . . . .. 212
The Registers Inside the 6510 Microprocessor 213
. How Do You Write Machine Language Programs? 214
64MON 215
. Hexadecimal
Notation.. ... . .. . . .. . . . . . . . .. . ... .. 215
Your First Machine Language Instruction 218
Writing Your First Program 220
. Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 221
v
. Indexing 223
Indirect Indexed 223
Indexed Indirect 224
Branches and Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 226
. Subroutines 228
. Useful Tips for the Beginner 229
. Approaching a Large Task. . . . . . . . . . . . . . . . . . . . . . . . . . .. 230
vi
Receiver/Transmitter Buffer Base Location Pointers 357
Zero-Page Memory Locations and Usage
for RS-232 System Interface. . . . . . . . . . . . . . . . . . . . . .. 358
Nonzero-Page Memory Locations and Usage
for RS-232 System Interface.. . . . . . . . . . . . . . . . . . . . .. 358
. The User Port. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 359
Port Pin Description 359
. The Serial Bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 362
Serial Bus Pinouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 363
. The Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
Port 366
. Z-80MicroprocessorCartridge. . . . . . . . . . . . . . . . . . . . . . . .. 368
Using Commodore CP/M@ . . . . . . . . . . . . . . . . . . . . . . . . . .. 369
Running Commodore CP/M@ . . . . . . . . . . . . . . . . . . . . . . . .. 369
APPENDICES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 373
A. Abbreviations for BASICKeywords. . . . . . . . . . . . . . . . . . .. 374
B. Screen Display Codes 376
C. ASCIIand CHR$ Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 379
F. Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 388
G. VIC Chip Register Map 391
H. Deriving Mathematical Functions 394
I. Pinouts for Input/Output Devices. . . . . . . . . . . . . . . . . . . . .. 395
INDEX 483
vii
INTRODUCTION
The COMMODORE64 PROGRAMMER'SREFERENCEGUIDE has been
developed as a working tool and reference source for those of you who
want to maximize your use of the built-in capabilities of your COMMO-
DORE64. This manual contains the information you need for your pro-
grams, from the simplest example all the way to the most complex. The
PROGRAMMER'SREFERENCEGUIDE is designed so that everyone from
the beginning BASIC programmer to the professional experienced in
6502 machine language can get information to develop his or her own
creative programs. At the same time this book shows you how clever
your COMMODORE 64 really is.
This REFERENCEGUIDE is not designed to teach the BASIC pro-
gramming language or the 6502 machine language. There is, however,
an extensive glossary of terms and a "semi-tutorial" approach to many
of the sections in the book. If you don't already have a working knowl-
edge of BASIC and how to use it to program, we suggest that you study
the COMMODORE 64 USER'SGUIDE that came with your computer. The
USER'SGUIDE gives you an easy to read introduction to the BASICpro-
gramming language. If you still have difficulty understanding how to use
BASIC then turn to the back of this book (or Appendix N in the USER'S
GUIDE) and check out the Bibliography.
The COMMODORE 64 PROGRAMMER'SREFERENCEGUIDE is just
that; a reference. Like most reference books, your ability to apply the
information creatively really depends on how much knowledge you have
about the subject. In other words if you are a novice programmer you
will not be able to use all the facts and figures in this book until you
expand your current programming knowledge.
ix
What you can do with this book is to find a considerable amount of
valuable programming reference information written in easy to read,
plain English with the programmer's jargon explained. On the other
hand the programming professional will find all the information needed
to use the capabilities of the COMMODORE64 effectively.
WHAT'S INCLUDED?
x INTRODUCTION
HOW TO USE THIS REFERENCEGUIDE
INTRODUCTION xi
Programming examples in this book are shown with blanks separating
words and operators for the sake of readability. Normally though,
BASIC doesn't require blanks between words unless leaving them out
would give you an ambiguous or incorrect syntax.
Shown below are some examples and descriptions of the symbols
used for various statement parameters in the following chapters. The list
is not meant to show every possibility, but to give you a better under-
standing as to how syntax examples are presented.
When you first thought about buying a computer you probably asked
yourself, "Now that I can afford to buy a computer, what can I do with
it once I get one?"
The great thing about your COMMODORE 64 is that you can make it
do what YOU want it to do! You can make it calculate and keep track of
home and business budget needs. You can use it for word processing.
You can make it play arcade-style action games. You can make it sing.
Youcan even create your own animated cartoons, and more. The best
part of owning a COMMODORE 64 is that even if it did only one of the
things listed below it would be well worth the price you paid for it. But
the 64 is a complete computer and it does do EVERYTHINGlisted and
then some!
xii INTRODUCTION
By the way, in addition to everything here you can pick up a lot of
other creative and practical ideas by signing up with a local Commo-
dore Users' Club, subscribing to the COMMODORE and POWER/PLAY
magazines, and joining the COMMODOREINFORMATIONNETWORKon
CompuServe™ .
APPLICATION COMMENTS/REQUIREMENTS
ACTION PACKED You can get real Bally Midway arcade games
GAMES like Omega Race, Gorf and Wizard of Wor, as
well as "play and learn" games like Math
Teacher I, Home Babysitter and Commodore
Artist.
ADVERTISING & Hook your COMMODORE64 to a TV, put it in
MERCHANDISING a store window with a flashing, animated,
and musical message and you've got a great
point of purchase store display.
ANIMATION Commodore's Sprite Graphics allow you to
create real cartoons with 8 different levels so
that shapes can move in front of or behind
each other.
BABYSITTING The COMMODORE 64 HOME BABYSITTER
cartridge can keep your youngest child occu-
pied for hours and teach alphabet/ keyboard
recognition at the same time. It also teaches
special learning concepts and relationships.
BASIC PROGRAMMING Your COMMODORE 64 USER'SGUIDE and the
TEACH YOURSELF PROGRAMMING series of
books and tapes offer an excellent starting
point.
INTRODUCTION xiii
Not only that, if you join the COMMODORE
INFORMATIONNETWORK on CompuServe™
you can get the latest news and updates on
all Commodore products, financial informa-
tion, shop at home services, you can even
play games with the friends you make through
the information systems you join.
COMPOSING SONGS The COMMODORE 64 is equipped with the
most sophisticated built-in music synthesizer
available on any computer. It has three com-
pletely programmable voices, nine full music
octaves, and four controllable waveforms.
look for Commodore Music Cartridges and
Commodore Music books to help you create or
reproduce all kinds of music and sound effects.
CP/M* Commodore offers a CP/M* add-on and ac-
cess to software through an easy-to-Ioad car-
tridge.
DEXTERITY TRAINING Hand/Eye coordination and manual dexterity
are aided by several Commodore games ...
including "Jupiter lander" and night driving
simulation.
EDUCATION While working with a computer is an educa-
tion in itself, The COMMODORE Educational
Resource Book contains general information
on the educational uses of computers. We
also have a variety of learning cartridges de-
signed to teach everything from music to math
and art to astronomy.
FOREIGN LANGUAGE The COMMODORE 64 programmable char-
acter set lets you replace the standard char-
acter set with user defined foreign language
characters.
GRAPHICS AND ART In addition to the Sprite Graphics mentioned
above, the COMMODORE 64 offers high-
resolution, multi-color graphics plotting, pro-
xiv INTRODUCTION
grammable characters, and combinations of
all the different graphics and character dis-
play modes.
INSTRUMENT Your COMMODORE 64 has a serial port,
CONTROl RS-232 port and a user port for use with a
variety of special industrial applications. An
IEEE/488 cartridge is also available as an op-
tional extra.
INTRODUCTION xv
SIMULATIONS Computer simulations let you conduct danger-
ous or expensive experiments at minimum risk
and cost.
These are just a few of the many applications for you and your
COMMODORE64. As you can see, for work or play, at home, in school
or the office, your COMMODORE64 gives you a practical solution for
just about any need.
Commodore wants you to know that our support for users only STARTS
with your purchase of a Commodore computer. That's why we've
created two publications with Commodore information from around the
world, and a "two-way" computer information network with valuable
input for users in the U.S. and Canada from coast to coast.
In addition, we wholeheartedly encourage and support the growth of
Commodore Users' Clubs around the world. They are an excellent source
of information for every Commodore computer owner from the beginner
to the most advanced. The magazines dnd network, which are more
fully described below, have the most up-to-date information about how
to get involved with the Users' Club in your area.
Finally, your local Commodore dealer is a useful source of Commo-
dore support and information.
POWER/PLAY
The Home Computer Magazine
When it comes to entertainment, learning at home and practical home
applications, POWER/PLAYis THEprime source of information for Com-
modore home users. Find out wh,ere your nearest user clubs are and
what they're doing, learn about software, games, programming tech-
niques, telecommunications, and new products. POWER/PLAYis your
personal connection to other Commodore users, outside software and
hardware developers, and to Commodore itself. Published quarterly.
Only $10.00 for a year of home computing excitement.
xvi INTRODUCTION
COMMODORE
The Microcomputer Magazine
Widely read by educators, businessmen and students, as well as
home computerists, COMMODORE Magazine is our main vehicle for
sharing exclusive information on the more technical use of Commodore
systems. Regular departments cover business, science and education,
programming tips, "excerpts from a technical notebook," and many
other features of interest to anyone who uses or is thinking about pur-
chasing Commodore equipment for business, scientific or educational
applications. COMMODOREis the ideal complement to POWER/ PLAY.
Published bi-monthly. Subscription price: $15.00 per year.
AND FOR EVEN MORE INFORMATION ...
...DIAL UP OUR PAPERLESS USER MAGAZINE
INTRODUCTION xvii
For more information, visit your Commodore dealer or contact Com-
puServe™ customer service at 800-848-8990 (in Ohio, 614-457-8600).
xviii INTRODUCTION