Table Of Contentu
u
u
LJ
U
U'
COMPUTERS
's
Guide
Ottis R. Cowper George W. Miller
David Florance Kevin Mykytyn
Todd D. Heimarck Philip I. Nelson
n
John Krause Tim Victor
O
COMPUTE!'PublicationsJnc^S
n
PartofABCConsumerMagazines,Inc.
OneoftheABCPublishingCompanies
Greensboro, North Carolina
n
u
u
Copyright 1985, COMPUTE! Publications, Inc. Allrightsreserved.
Reproductionortranslation ofanypartofthisworkbeyond thatpermittedbySec
tions 107and 108 oftheUnited States CopyrightActwithoutthepermission ofthe
copyrightownerisunlawful.
Printedin the United States ofAmerica
10 9 8 7 6 5 4
ISBN 0-87455-031-9 u
Theauthorsandpublisherhavemadeeveryeffortinthepreparationofthisbooktoinsuretheac
curacyoftheprogramsandinformation.However,theinformationandprogramsinthisbookare
soldwithoutwarranty,eitherexpressorimplied. NeithertheauthorsnorCOMPUTE!Publica- 4 |
tions,Inc.willbeliableforanydamagescausedorallegedtobecauseddirectly,indirectly,inci- L
dentally,orconsequentiallybytheprogramsorinformationinthisbook. ^""""^
COMPUTE! Publications, Inc., PostOfficeBox5406, Greensboro, NC27403, (919) >
275-9809,ispartofABC ConsumerMagazines, Inc., oneoftheABCPublishing ^
Companies, andisnotassociatedwith anymanufacturerofpersonal computers. Com- ^—
modore 64 and Commodore 128 aretrademarksofCommodore Electronics Limited.
H
Contents
H
Foreword v
f-"> Introduction , vii
<w~ Chapter 1. BASIC Programming 1
Chapter 2. Graphics 125
Chapter 3. Sound and Music 169
Chapter 4. Peripherals 207
Chapter 5. CP/M Mode 273
Chapter 6. Machine Language 311
Chapter 7. System Architecture 341
Appendices 371
A: Character, Screen, and Keyboard Codes 373
B: BASIC and Disk Error Messages 393
C: 8502 and Z80 Machine Language Opcodes 407
D: Memory Map 425
E: Musical Note Values 433
F: CP/M Basic Disk Operating System Service Calls 437
Index 442
n
u
u
u
LJ
U
U'
n
n
Foreword
COMPUTE!"s 128 Programmer's Guide is a book you'll want at
—_, your side whenever you're programming or using the Commo-
! I dore 128. It's not intended to replace the 128 System Guide. In
stead, it's a source of additional ideas, programming advice,
and technical information. It covers the 128 in all of its sev
eral modes, and every explanation is written in the clear, easy-
to-read style that's the hallmark of COMPUTE! publications.
BASIC programmers, for instance, will make frequent
reference to the detailed explanation of every 128 BASIC com
mand. Machine language devotees will be particularly inter
ested in the ROM maps and listings of Kernal routines. And if
you've bought the 128 for its CP/M capabilities, you'll find
the discussion of CP/M commands and Z80 machine language
invaluable.
This book begins with a brief survey of the 128's many
capabilities, and instructions on how to enter programs in
BASIC and machine language. Chapter 1 contains valuable
programming advice as well as a discussion of each BASIC
command. Graphics, music and sound, and machine language
are all examined in detail. Chapters are also devoted to the
disk drive and other peripherals, CP/M operations, machine
language programming, and system architecture.
The authors—the technical staff of COMPUTE! Publica
tions—have distilled years of computer experience and many
hours of research into the pages of COMPUTE!fs 128 Program
mer's Guide. For almost every level of programming, from rank
beginner to veteran programmer, this book can be your guide
to greater understanding of your machine and more effective
programming methods. It not only teaches, but is also a thor
ough reference for the experienced programmer.
n
n
n
u
u
u
u
Acknowledgments
Every book is the work of many hands. The authors would
particularly like to thank their families and friends, who sup
ported them throughout this endeavor, as well as everyone at
COMPUTE! who provided advice and assistance in this excit
ing, satisfying project.
Ottis R. Cowper
David Florance
Todd D. Heimarck
John Krause
George W. Miller
Kevin Mykytyn
Philip I. Nelson
Tim Victor
October 1985
LJ
LJ
LJ
u
VI
n
Introduction
n
The Commodore 128 offers a lot to programmers; it's
three computers in one. It can operate as a Commo
dore 64 with 64K of memory, BASIC 2.0, and
6502/8502 machine language. It's also a Commodore 128
with 128K of memory (expandable to 512K), BASIC 7.0, and
8502 machine language. Finally, it's a CP/M computer with a
transient program area (TPA) of 59K, a variety of languages
available, and Z80 machine language. Plus, you can purchase
versions of Logo, Pilot, C, Comal, Pascal, COBOL, and many
more languages. In one computer, you can choose to work
with two BASICs, two versions of machine language, and
three different operating systems. Where do you begin?
If you're a VIC or 64 owner who is already familiar with
programming, you may want to start with Chapter 1, which
explains BASIC in detail. The 128 has many new commands
to make programming easier. You could then move on to the
sections on graphics, music, machine language, or peripherals.
Readers who have come to the 128 with a CP/M background
may want to start with the CP/M chapter. And if the 128 is
your first computer, you should probably start with the System
Guide, the 400-page book that came with your 128 and which
provides a good introduction to programming. COMPUTERS
128 Programmer's Guide was not written to replace the System
Guide as an introduction to BASIC. Rather, it is a guide for
programmers, a reference book, and a source of ideas.
Even if you're comparatively new to programming, you
should not have difficulty understanding most parts of this
book. We've tried to avoid jargon whenever possible, and ex
plain each subject in clear, everyday language.
However, certain technical terms are unavoidable: For in
stance, the most descriptive name for the BASIC computer
language is simply "BASIC." The term microprocessor is uni-
P"| versally understood to mean the computer's main chip—the
"electronic brain" that controls everything else—and so on.
When a technical term is first introduced, it's italicized and
j~""[ described.
The Commodore 128 has 128K of memory. You can think
of this as the equivalent of 131,070 characters, but what does
j""| that really mean? A single memory location can hold a byte or
number in the range 0-255. The term K stands for kilobyte and
n
Vll
Introduction *—^
u
refers to a 1,024-byte package of memory (128 * 1,024 = ^ j
131,070). Less often, you'll see the term page, which refers to a *—'
256-byte memory zone. In most cases, the words location and
address are interchangeable, both referring to a particular one- <| [
byte-long space in the computer's memory. s—'
Just as memory is divided into single byte-size locations, a
byte can be divided into individual bits, or binary digits. A
byte contains eight bits, each of which represents a binary
(base 2) number of 1 or 0.
We all understand decimal numbers. When referring to
the 1,024th memory location in the computer, we'll ordinarily
call it location 1024 (since the computer doesn't accept com
mas in numbers, we use 1024 instead of 1,024). In many
cases, it's helpful to programmers to know the hexadecimal
(base 16) form of an address as well. One reason for this is
that all of the major subdivisions in computer memory occur
at spots that divide evenly in hexadecimal, not decimal. BASIC
program space, for instance, starts at decimal location 7168—
which looks very odd unless you realize that the hexadecimal
equivalent of 7168 is $lC00. This location falls at an even
page boundary in memory (7168/256=28). A dollar sign ($)
in front of a number tells you that it's hexadecimal. Thus,
we'll usually refer to the 1,024th byte in memory as location
1024 ($0400).
If hexadecimal numbers sound confusing or difficult to
you, don't worry about it. You won't need to deal with them
very often in ordinary programming. And BASIC provides two
functions (DEC and HEX$) that make it easy to translate from
one number base to another (see Chapter 1). Or you can use
the machine language monitor (see explanation below) to con
vert into four different number bases—decimal, hexadecimal,
binary, and even octal.
How to Type In BASIC Programs ,
Many of the programs in this book contain special control LJ
characters (cursor controls, color keys, reverse video, etc.). To
make it easy to know exactly what to type when entering one j i
of these programs into your computer, we have established 1 I
the following listing conventions.
Generally, program listings will contain words within » »
braces which spell out any special characters: {DOWN} would \ J
Vlll