C64C System Guide
C64C System Guide
C64C System Guide
COMMODORE
PERSONAL COMPUTER
system guide
Learning to program in BASIC 2.0
■
u
u
U
u
u
u
LJ
LJ
LJ
U
LJ
LJ
U
u
u
u
n
n COMMODORE 64C
n
I s
SYSTEM GUIDE
Learning to Program in BASIC 2.0
p.
u
u
USER'S MANUAL STATEMENT
WARNING: LJ
This equipment has been certified to comply with the limits for a Class B computing device, pursuant
to subpart J of Part 15 of the Federal Communications Commission's rules, which are designed to / -,
provide reasonable protection against radio and television interference in a residential installation. If Ss^J
not installed properly, in strict accordance with the manufacturer's instructions, it may cause such
interference. If you suspect interference, you can test this equipment by turning it off and on. If this | |
equipment does cause interference, correct it by doing any of the following: ■—'
If necessary, consult your Commodore dealer or an experienced radio-television technician for addi
tional suggestions. You may find the following FCC booklet helpful: "How to Identify and Resolve j j
Radio-TV Interference Problems." The booklet is available from the U.S. Government Printing
Office, Washington, D.C. 20402, stock no. 004-000-00345-4. , >
i I
This manual contains copyrighted and proprietary information. No part of this publication may be / j
reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, I—*
mechanical, photocopying, recording or otherwise, without the prior written permission of Commo
dore Electronics Limited. j J
U
Commodore BASIC 7.0
U
Copyright © 1986 by Commodore Electronics Limited
All rights reserved j j
u
i F
n
n
TABLE OF Chapter I—Introduction
n CONTENTS
Chapter 2—Getting Started in BASIC
n
Chapter 3—Advanced BASIC Programming 39
n
Chapter 4—Graphics, Color and Sprites 63
n
Chapter 5—Sound and Music 95
H
Chapter 6—BASIC 2.0 Encyclopedia 107
n
Appendices 149
n A. BASIC 2.0 Error Messages 151
B. Connectors/Ports for Peripheral Equipment 155
n C. Screen Display Codes 161
D. ASCn and CHR$ Codes 163
E. Screen and Color Memory Maps 167
R Derived Trigonometric Functions 169
G. Memory Map 171
H. BASIC 2.0 Abbreviations 173
I. Sprite Register Map 175
J. Sound and Music 177
n
Glossary 181
n
n Index 195
r—>
i \
n
n
n
n
u
u
U
u
u
u
LJ
LJ
LJ
U
LJ
LJ
U
u
u
u
■ - — .v-::M^MM;&<%j...■-.,■.■■. -.,;-v.:;:^aii&&;,kviv... ^';:^,v^^ysssi^
u
u
o
u
u
u
u
u
u
u
u
u
u
u
u
n
n
CHAPTER 1 HOW TO USE THIS GUIDE
n Introduction
n
n
n
(—1
n
n
n
CHAPTER 1-INTRODUCTION
u
u
u
LJ
LJ
U
U
U
LJ
U
U
u
u
LJ
U
U
u
n
n
How to Use This This Guide is designed to help you make full use of the advanced capabili
n Guide ties of the Commodore 64C™ personal computer. Here's how to use the
Guide:
n 1. Before you read any further in this book, make sure youVe read the
COMMODORE 64C QUICK-CONNECT GUIDE, which tells how
n to unpack and set up your new 64C computer and peripheral equip
ment. Also be sure to read the COMMODORE 64C INTRODUC
n
TORY GUIDE, which contains important information on getting
started with the Commodore 64C, including how to load and run
n prepackaged disk, cartridge and tape software. Both pieces of docu
mentation come packed in the computer carton.
n
2. If you are interested mostly in learning the BASIC® language to create
and run your own programs, you should first read Chapters 2 and 3 of
this book. Chapter 2 gets you started quickly by introducing you to
n BASIC 2.0 concepts and providing numerous explanations and exam
ples of commonly used commands and elementary programming tech
n niques. Chapter 3 defines a number of more advanced BASIC com
mands and programming techniques, again giving explanations and
n examples of how to use them. Together, these two chapters provide a
solid foundation from which you can move on to more specialized
n programming activities, such as graphics and sound.
n
CHAPTER 1-INTRODUCTION
n
u
u
The features of the Commodore 64C are so many and so varied that—even
in a book of this length—they can only be introduced. For additional u
information on all the technical features of the Commodore 64C, see the
COMMODORE 64® PROGRAMMER'S REFERENCE GUIDE, available u
from your Commodore dealer or at most bookstores. For additional infor
mation on BASIC, see the complete three-part course on BASIC LJ
programming-INTRODUCTION TO BASIC, PARTS I, lly and III. This
series of three books is also available from your Commodore dealer. LJ
U
Overview of the Key Commodore 64C features include: LJ
Commodore 64C —Fully compatible with Commodore 64 hardware, software and
Personal Computer peripherals U
—Versatile Commodore BASIC 2.0 programming language, offering more
than 70 commands and functions LJ
—64K of RAM (Random Access Memory)
u
—40 column screen output
u
—Ability to run thousands of off-the-shelf software programs for business,
industry, science, education and home, including word processors,
LJ
spreadsheets, databases, financial market software, telecommunications
programs, etc.
U
—Ability to handle software packaged in disk, tape or cartridge formats
U
—Ability to work with a wide variety of peripheral devices, including video
monitors, printers, modems, controllers (joysticks, mouse, etc.)
LJ
—Sophisticated graphics capabilities, including 8 individually program
mable sprites and several animation modes U
—Sixteen colors
LJ
—A professional style low-profile keyboard
U
—Ability to incorporate 6502 machine language data in BASIC programs
u
\ I
I 1
CHAPTER 1-INTRODUCTION
LJ
H
n
n
n
n
n
n
CHAPTER 1-INTRODUCTION
u
u
u
u
LJ
LJ
LJ
y
u
u
u
y
u
y
y
y
y
y
n
n
n
u
D
U
u
o
u
G
u
u
LJ
u
n
n
CHAPTER 2 BASIC PROGRAMMING LANGUAGE 13
n Getting Started Direct Mode 13
in BASIC Program Mode 13
n
USING THE KEYBOARD 14
n Keyboard Character Sets 14
Using the Command Keys 14
h Function Keys 19
Displaying Graphic Characters 20
n Rules for Typing BASIC Language Programs 20
n Variables 31
Strings 32
n
n
11 CHAPTER 2-GETTING STARTED IN BASIC
n
u
u
SAMPLE PROGRAM 33
LJ
LJ
LJ
LJ
LJ
U
12 CHAPTER 2-GETTING STARTED IN BASIC
LJ
n
n
BASIC The BASIC programming language is a special language that lets you com
n Programming municate with your Commodore 64C. Using BASIC is one means by
Language which you instruct your computer what to do.
n
BASIC has its own vocabulary (made up of commands, statements and
n functions) and its own rules of structure (called syntax). You can use
the BASIC vocabulary and syntax to create a set of instructions called a
n program, which your computer can then perform or
n Using BASIC, you can communicate with your Commodore 64C in two
ways: within a program, or directly (outside a program).
n
n Direct Mode
n Program Mode
n Once you have typed a program, you can use it immediately by typing the
RUN command and pressing the RETURN key. You can also store the
n program on disk or tape by using the SAVE command. Then you can
recall it from the disk or tape by using the LOAD command. This com
n mand copies the program from the disk or tape and places that program in
the Commodore 64C's memory. You can then use or "execute" the pro
n gram again by entering the RUN command. All these commands are
explained later in this section. Most of the time you will be using your
n computer with programs, including programs you yourself write, and com
mercially available software packages. The only time you operate in direct
n mode is when you are manipulating or editing your programs with com
mands such as LIST, LOAD, SAVE and RUN. As a rule, the difference
n between direct mode and operation within a program is that direct mode
commands have no line numbers.
n
n
13 CHAPTER 2-GETTING STARTED IN BASIC
n
u
u
Using the Keyboard Shown below is the keyboard of the Commodore 64C Personal Computer.
u
LJ
64C Keyboard
U
LJ
Keyboard Character Sets
U
The Commodore 64C keyboard offers two different sets of characters:
Return When you press the RETURN key, what you have LJ
typed is sent to the Commodore 64C computer's
U
U
14 CHAPTER 2-GETTING STARTED IN BASIC
U
n
n
memory. Pressing the RETURN key also moves the
n cursor (the small flashing rectangle that marks where
the next character you type will appear) to the
n beginning of the next line.
n
Shift There are two SHIFT keys on the bottom row of the
n keyboard. One key is on the left and the other is on
the right, just as on a standard typewriter keyboard.
n
The SHIFT key can be used in three ways:
n
15 CHAPTER 2-GETTING STARTED IN BASIC
n
U
u
Shift Lock When you press this key down, it locks into place.
Then, whatever you type will either be a capital u
letter, or the top character of a double-character key.
To release the lock, press down on the SHIFT u
LOCK key again.
u
Moving the Cursor Using the CRSR keys
u
You can use the two keys on the right side of the
bottom row of the main keyboard to move the LJ
cursor:
u
• Pressing the CRSR key alone moves the cursor
down* T LJ
• Pressing the CRSR and SHIFT keys together
moves the cursor up. U
• Pressing the CRSR key alone moves the cursor
right. LJ
• Pressing the CRSR and SHIFT keys together
U
moves the cursor left.
1 I
I 1
You don't have to keep tapping a cursor key to move
more than one space. Just hold the key down and the
u
cursor continues to move until it reaches the position
you want. I, J
I I
LJ
Inst/Del This is a dual purpose key. INST stands for INSerT,
I )
and DEL for DELete. I 1
LJ
16
U
CHAPTER 2-GETTING STARTED IN BASIC
LJ
n
n
Inserting Characters
n You must use the SHIFT key with the INST/DEL
key when you want to insert characters in a line.
n
Suppose you left some characters out of a line,
like this:
n
WHILE U WERE OUT
n To insert the missing characters, first use the cursor
keys to move the cursor back to the error, like this:
n
WHILEHWERE OUT
n Then, while you hold down the SHIFT key, press the
INST/DEL key until you have enough space to add
n the missing characters:
Deleting Characters
i \ When you press the DEL key, the cursor moves one
space to the left and erases the character that is
there. This means that when you want to delete
something, you move the cursor just to the right of
the character you want to DELete. Suppose you have
made a mistake in typing, like this:
H PRINT "ERROER"
PRINT "ERROR"
i \
n
17 CHAPTER 2-GETTING STARTED IN BASIC
U
u
Using INSerT and DELete Together
U
CTRL The CTRL (Control) key is used with other keys to
do special tasks called control functions. To perform LJ
a control function, hold down the CTRL key while
you press some other key. Control functions are LJ
often used in prepackaged software such as a word
processing system.
U
One control function that is used often is setting the i I
1I
character and cursor color. To select a color, hold
down the CTRL key while you press a number key
LJ
(1 through 8), on the top row of the keyboard. There
are eight more colors available to you; these can be
LJ
selected with the Cf key, as explained later.
U
Pressing CTRL during a screen printout will slow
the display. ( )
i )
LJ
n
n
To do this, hold down the RUN/STOP key and
H press RESTORE.
n
Function Keys
n The four large keys located to the right of the main keyboard (marked Fl,
F3, F5 and F7 on the top and F2, F4, F6 and F8 on the front) are called
n function keys. These keys are often used by prepackaged software to
allow you to perform a task with a single keystroke.
n
u
To display the graphic symbol on the right front face of a key, hold down 1—I
the SHIFT key while you press the key that has the graphic character you
want to print. You can display the right side graphic characters only when t—J
the keyboard is in the upper-case/graphics character set (the normal char
acter set usually available at power-up). i—J
To display the graphic character on the left front face of a key, hold down 1—»
the Cs key while you press the key that has the graphic character you want. ,
You can display the left graphic character while the keyboard is in either i—i
character set.
6. Distinguish clearly between the letter I and the numeral 1 and between
the letter O and the numeral 0. v j
7. The computer does not execute anything following the letters REM on
a program line. REM stands for REMark. You can use the REM state- LJ
ment to put comments in your program that tell anyone listing the
program what is happening at a specific point. { j
u
n
n
Getting Started— The PRINT command tells the computer to display information on the
n The PRINT screen. You can print both numbers and text (letters), but there are special
Command rules for each case, described in the following paragraphs.
H Printing Numbers
To print numbers, use the PRINT command followed by the number(s) you
n want to print. Try typing this on your Commodore 64C:
n PRINT 5
Then press the RETURN key. Notice the number 5 is now displayed on
n the screen.
n PRINT 5,6
In this PRINT command, the comma tells the Commodore 64C that you
h want to print more than one number. When the computer finds commas in
a string of numbers in a PRINT statement, each number that follows a
comma is printed starting in either the 1 lth, 21st or 31st column on the
screen, depending on the length of each number. If the previous number
has more than 7 digits, the following number is moved to the next starting
position, 10 columns to the right. The 64C always leaves at least two spaces
n plus one space for a sign between numbers which are separated by a
comma. (For example, a negative number like — 4 will be preceded by two
n spaces when it follows a comma.) If you don't want all the extra spaces, use
a semicolon (;) in your PRINT statement instead of a comma. The semico
lon tells the computer not to add any spaces between strings. Numbers and
numeric variables are printed with either a leading space or a minus sign,
and a trailing space. Omitting a semicolon, a comma, or any separators
acts the same as a semicolon. Type these examples and see what happens:
n
r-i
? i PRINT 5;6
PRINT 100;200;300;400;500
n
n You can use a question mark (?) as an abbreviation for the PRINT com-
mand. Many of the examples in this section use the ? symbol in place of the
n word PRINT. In fact, most of the BASIC commands can be abbreviated.
However, when you LIST a program, the keyword appears in the long
i i
version. The abbreviations for BASIC commands can be found in Appen
dix H of this Guide.
n
n
21 CHAPTER 2-GETTING STARTED IN BASIC
u
u
Printing Text
To print text, first type the PRINT command (i.e., the word PRINT or a '—'
question mark). Then type quotation marks, followed by the words or , }
characters you want to display, and another set of quotation marks. Then '—!
press the RETURN key. Remember that any words or characters you want s >
to display must be typed on the screen with a quote symbol at each end of •■—'
the string of characters. String is the BASIC name for any set of charao j .
ters surrounded by quotes* The quote character is obtained by pressing '—'
SHIFT and the numeral 2 key on the top row of the keyboard. Try these , ,
examples: '—'
u
Notice that when you press RETURN, the computer displays the charac
ters within the quotes on the screen. Also note that the second example | J
did not calculate 4*5 since it was treated as a string and not a mathematical
calculation. If you want to calculate the result 4*5, use the following [ J
command:
You can PRINT any string you want by using the PRINT command and | j
surrounding the printed characters with quotes. You can combine text and
calculations in a single PRINT command like this: [_J
LJ
LJ
LJ
U
* (
22 CHAPTER 2-GETT1NG STARTED IN BASIC L-'
LJ
n
n
Printing in Different Colors
n The Commodore 64C is capable of displaying 16 different colors on the
screen. You can change colors easily. All you do is hold down the CTRL
n key and press a numbered key between 1 and 8 on the top row of the main
keyboard. Notice that the cursor changes color according to the numbered
n key you pressed. All the succeeding characters are displayed in the color
you selected. Hold down the Cs key and press a numbered key between 1
n and 8, and eight additional colors are displayed on the screen.
/ «
The following table lists the colors available using the CTRL and Cs keys.
The table also shows the key used to specify a given color, and the resulting
n control character that appears within the quotes of a PRINT statement.
ORANGE
BROWN IS
LT. RED ►H
GRAY 1 m
PC*
GRAY 2
n
LT. GREEN ■1
LT. BLUE □
YELLOW GRAY 3
Using the Cursor Keys Inside Quotes with the PRINT Command
When you type the cursor keys inside quotation marks, graphic characters
are shown on the screen to represent the keys. These characters will NOT
be printed on the screen when you press RETURN. Try typing a question
mark (?), open quotes (SHIFTed 2 key); then press either of the down cur-
sor keys 10 times, enter the words "DOWN HERE", and close the quotes.
The line should look like this:
| DOWN HERE"
Now press RETURN. The Commodore 64C prints 10 blank lines, and on
the eleventh line, it prints "DOWN HERE". As this example shows, you
n
can tell the computer to print anywhere on your screen by using the cursor
control keys inside quotation marks.
i i
I i
U
Beginning to So far most of the commands we have discussed have been performed in
Program DIRECT mode. That is, the command was executed as soon as the
u
RETURN key was pressed. However, most BASIC commands and func
tions can also be used in programs.
u
U
What a Program Is
U
Line Numbers
U
The lines of a program are numbered so that the computer knows in what
order you want them executed or RUN. The computer executes the pro
gram lines in numerical order, unless the program instructs otherwise. You
LJ
can use any whole number from 0 to 63999 for a line number. Never use a
U
comma in a line number.
LJ
Many of the commands you have learned to use in DIRECT mode can be
easily made into program statements. For example, type this:
U
10 ? "COMMODORE 64C"
U
Notice the computer did not display COMMODORE 64C when you
pressed RETURN, as it would do if you were using the PRINT command
u
in DIRECT mode. This is because the number, 10, that comes before the
PRINT symbol (?) tells the computer that you are entering a BASIC pro
gram. The computer just stores the numbered statement and waits for the
LJ
next input from you.
U
Now type RUN and press RETURN. The computer prints the words
LJ
COMMODORE 64C. This is not the same as using the PRINT command
in DIRECT mode. What has happened here is that YOU HAVE JUST
U
WRITTEN AND RUN YOUR FIRST BASIC PROGRAM. The pro
gram is still in the computer's memory, so you can run it as many times as
you want.
U
LJ
LJ
LJ
24 CHAPTER 2-GETTING STARTED IN BASIC
U
n
H Your one-line program is still in the 64C's memory. Now clear the screen by
pressing the SHIFT and CLR/HOME keys together. The screen is empty.
n At this point you may want to see the program listing to be sure it is still in
memory. The BASIC language is equipped with a command that lets you
n do just this—the LIST command.
—To see from line Nl to line N2 inclusive, type LIST N1-N2 and
press RETURN.
The line numbers in a program have another purpose besides putting your
commands in the proper order for the computer. They serve as a reference
for the computer in case you want to execute the command in that line
n repetitively in your program. You use the GOTO command to tell the
computer to go to a line and execute the command(s) in it. Now type:
n 20 GOTO 10
n When you press RETURN after typing line 20, you add it to your program
in the computer's memory.
n
Notice that we numbered the first line 10 and the second line 20. It is very
n helpful to number program lines in increments of 10 (that is, 10, 20, 30, 40,
etc.) in case you want to go back and add lines in between later on. You
n can number such added lines by fives (15, 25 ...) ones (1,2...)—in fact,
by any whole number—to keep the lines in the proper order.
n
Type RUN and press RETURN, and watch the words COMMODORE U
64C move down your screen. To stop the message from printing on the j i
screen, press the RUN/STOP key on the left side of your keyboard.
The two lines that you have typed make up a simple program that repeats
itself endlessly, because the second line keeps referring the computer back \ \
to the first line. The program will continue indefinitely unless you stop it or
turn off the computer. ) /
Notice that even though you used the ? symbol for the PRINT statement, j [
your computer has converted it into the full command. This happens when
you LIST any command you have abbreviated in a program. j j
u
Clearing the Computer's Memory—The NEW Command
Anytime you want to start all over again or erase a BASIC program in the i—'
computer's memory, just type NEW and press RETURN. This command
clears out the computer's BASIC memory, the area where programs and '—'
data are stored. . .
<—f
To select color within a program, you must include the color selection j }
information within a PRINT statement. For example, clear your computer's
memory by typing NEW and pressing RETURN, then type the following, j j
being sure to leave space between each letter:
LJ
U
n
n
each letter. Press the SHIFT and the 2 keys to type a set of closing quota
tion marks and press the RETURN key. Now type RUN and press the
n RETURN key. The computer displays the word SPECTRUM with each
letter in a different color. Now type LIST and press the RETURN key.
Notice the graphic characters that appear in the PRINT statement in line
10. These characters tell the computer what color you want for each
n
printed letter. Note that these graphic characters do not appear when the
Commodore 64C PRINTs the word SPECTRUM in different colors.
n
The color selection characters, known as control characters, in the PRINT
n
statement in line 10 tell the Commodore 64C to change colors. The com
puter then prints the characters that follow in the new color until another
n color selection character is encountered. While characters enclosed in quo
tation marks are usually PRINTed exactly as they appear, control charac
ters are only displayed within a program LISTing.
Editing Your The following paragraphs will help you to type in your programs and make
H
Program corrections and additions to them.
n
Erasing a Line from a Program
n
Use the LIST command to display the program you typed previously. Now
n type 10 and press RETURN. You just erased line 10 from the program.
LIST your program and see for yourself. If the old line 10 is still on the
n screen, move the cursor up so that it is blinking anywhere on that line.
Now, if you press RETURN, line 10 is back in the computer's memory.
n
fmmt
I \ Duplicating a Line
Hold down the SHIFT key and press the CLR/HOME key on the upper
n
right side of your keyboard. This will clear your screen. Now LIST your
program. Move the cursor up again so that it is blinking on the "0" in the
line numbered 10. Now type a 5 and press RETURN. You have just dupli
cated (i.e., copied) line 10. The duplicate line is numbered 15. Type LIST
/ i
and press RETURN to see the program with the duplicated lines.
n
Replacing a Line
/ \
You can replace a whole line by typing in the old line number followed by
n the text of the new line, then pressing RETURN. The old version of the
line will be erased from memory and replaced by the new line as soon as
you press RETURN.
n
n 27 CHAPTER 2-GETTING STARTED IN BASIC
U
Changing a Line U
Suppose you want to add something in the middle of a line. Simply move u
the cursor to the character or space that immediately follows the spot
where you want to insert the new material. Then hold down the SHIFT
LJ
key and the INST/DEL key together until there is enough space to insert
your new characters. LJ
Try this example. Clear the computer's memory by typing NEW and press
U
ing RETURN. Then type:
U
Mathematical You can use the PRINT command to perform calculations like addition,
Operations subtraction, multiplication, division and exponentiation. You type the LJ
calculation after the PRINT command.
U
Addition and Subtraction
PRINT 10+15-5
U
PRINT 75 -100 LJ
PRINT 30+ 40,55-25
U
PRINT30 + 40;55-25
U
28 CHAPTER 2-GETTING STARTED IN BASIC Li
LJ
n Multiplication and Division
n Find the asterisk key (*) on the right side of your keyboard. This is the
symbol that the Commodore 64C uses for multiplication. The slash (/) key,
located next to the right SHIFT key, is used for division.
PRINT 5*3
n
PRINT 100/2
n
n Exponentiation
Order of Operations
You have seen how you can combine addition and subtraction in the same
/ i
PRINT command. If you combine multiplication or division with addition
or subtraction operations, you may not get the result you expect. For
i (
example, type:
PRINT(4 + 6)/2 U
This gives you the desired answer, 5. U
If you want the computer to divide before squaring in the second example, U
type:
PRINT (4*5)/(10*6) u
The answer is now .333333333. u
u
Constants, Variables Constants
and Strings
Constants are numeric values that are permanent: that is, they do not
change in value over the course of an equation or program. For example,
the number 3 is a constant, as is any number. This statement illustrates
how your computer uses constants:
U
10 PRINT 3
No matter how many times you execute this line, the answer will always LJ
be 3.
u
LJ
U
u
n
n
Variables
n Variables are values that can change over the course of an equation or
program statement. There is a part of the computer's BASIC memory that
m
is reserved for the characters (numbers, letters and symbols) you use in
your program. Think of this memory as a number of storage compartments
n in the computer that store information about your program; this part of
the computer's memory is referred to as variable storage. Type in this
H program:
10X = 5
n
20 ?X
n Now RUN the program and see how the computer prints a 5 on your
screen. You told the computer in line 10 that the letter X will represent the
n number 5 for the remainder of the program. The letter X is called a varia
ble, because the value of X varies depending on the value to the right of the
n equals sign. We call this an assignment statement because now there is a
storage compartment labeled X in the computer's memory, and the number
n 5 has been assigned to it. The = sign tells the computer that whatever
comes to the right of it will be assigned to a storage compartment (a mem
H ory location) labeled with the letter X to the left of the equals sign.
n The variable name on the left side of the = sign can be either one or two
letters, or one letter and one number (the letter MUST come first). The
n names can be longer, but the computer only looks at the first two charac
ters. This means the names PA and PART would refer to the same storage
n compartment. Also, the words used for BASIC commands (LOAD, RUN,
LIST, etc.) or functions (INT, ABS, SQR, etc.) cannot be used as names in
p your programs. Refer to the BASIC Encyclopedia in Chapter 5 if you have
any questions about whether a variable name is a BASIC keyword. Notice
n that the = in assignment statements is not the same as the mathematical
symbol meaning "equals", but rather means allocate a variable (storage
n compartment) and assign a value to it.
f"■»
In the sample program you just typed, the value of the variable X remains
at 5 throughout. You can put calculations to the right of the = sign to
n assign the result to a variable. You can mix text with constants in a PRINT
statement to identify them. Type NEW and press RETURN to clear the
n 64C's memory; then try this program:
10 A = 3* 100
n 20 B = 3*200
30 ?"A IS EQUAL TO "A
n 40 ?"B IS EQUAL TO "B
n
u
Now there are two variables, labeled A and B, in the computer's memory,
containing the numbers 300 and 600 respectively. If, later in the program, | 1
you want to change the value of a variable, just put another assignment
statement in the program. Add these lines to the program above and RUN ; i
it again. '
50 A = 900*30/10 Lj
60B = 95 + 32+128
70 GOTO 30 J
You'll have to press the STOP key to halt the program.
u
Now LIST the program and trace the steps taken by the computer. First, it t /
assigns the value to the right of the = sign in line 10 to the letter A. It does
the same thing in line 20 for the letter B. Next, it prints the messages in i j
lines 30 and 40 that give you the values of A and B. Finally, it assigns new
values to A and B in lines 50 and 60. The old values are replaced and can- « >
not be recovered unless the computer executes lines 10 and 20 again. When
the computer is sent to line 30 to begin printing the values of A and B j {
again, it prints the new values calculated in lines 50 and 60. Lines 50 and —'
60 reassign the same values to A and B and line 70 sends the computer j i
back to line 30. This is called an endless loop, because lines 30 through 70 '—
are executed over and over again until you press the RUN/STOP key to • i
halt the program. Other methods of looping are discussed later in this and
the following two sections. ■ i
Strings ^
A string is a character or group of characters enclosed in quotes. These \ f
characters are stored in the computer's memory as a variable in much the
same way numeric variables are stored. You can also use variable names to J_J
represent strings, just as you use them to represent numbers. When you put
the dollar sign ($) after the string variable name, it tells the computer that [ j
the name is for a string variable, and not a numeric variable.
u
Type NEW and press RETURN to clear your computer's memory, then
type in the program below: } '
10 A$ = "COMMODORE "
20X = 64C 1—>
30 B$ = " COMPUTER"
40Y = l LJ
50? "THE "A$;X;B$" IS NUMBER "Y
Lj
LJ
n
n
See how you can print numeric and string variables in the same statement?
Try experimenting with variables in your own short programs.
n
You can print the value of a variable in DIRECT mode, after the program
n has been RUN. Type ?A$;B$;X;Y after running the program above and
see that those four variable values are still in the computer's memory.
n
If you want to clear this area of BASIC memory but still leave your pro
n gram intact, use the CLR command. Just type CLR (RETURN) and all
constants, variables and strings are erased. But when you type LIST, you
n
can see the program is still in memory. The NEW command discussed
earlier erases both the program and the variables.
n
I \
Sample Program Here is a sample program incorporating many of the techniques and com-
mands discussed in this section.
n
This program calculates the average of three numbers (X, Y and Z) and
n
prints their values and their averages on the screen. Ydu can edit the pro
gram and change the assignments in lines 10 through 30 to change the
H
values of the variables. Line 40 adds the variables and divides by 3 to get
the average. Note the use of parentheses to tell the computer to add the
n numbers before it divides.
n
TIP: Whenever you are using more than one set of parentheses in a
statement, it's a good idea to count the number of left parentheses
n
and right parentheses to make sure they are equal.
10X = 46
20Y = 72
n 30Z=114
40A = (X + Y + Z)/3
n 60 ?"THE AVERAGE OF"X;Y;"AND "Z;"IS"A;
90 END
n
n 33 CHAPTER 2—GETTING STARTED IN BASIC
i i
U
Once you have created your program, you will probably want to store it
U
Storing and Reusing
Your Programs permanently so you will be able to recall and use it at some later time. To
do this, you'll need either a Commodore disk drive or a Commodore
U
Datassette.
U
You will learn several commands that let you communicate between your
computer and your disk drive or Datassette. These commands are con U
structed with the use of a command word followed by several parameters.
Parameters are numbers, letters, words or symbols in a command that sup
U
ply specific information to the computer, such as a filename, or a numeric
variable that specifies a device number. Each command may have several LJ
parameters. For example, the parameters of the disk format command
include a name for the disk and an identifying number or code, plus several U
other parameters. Parameters are used in almost every BASIC command;
some are variables which change and others are constants. These are the
LJ
parameters that supply disk information to the 64C and disk drive:
U
Disk Handling Parameters
u
disk name- arbitrary 16 character identifying name you
supply. u
file name— arbitrary 16 character identifying name you
supply. u
i.d.- arbitrary two-character identifier you supply
drive number- must use 0 for a single disk drive, 0 or 1 in a u
dual drive.
device number— a preassigned number for a peripheral device. u
For example, the device number for a Com
modore disk drive is usually 8. u
u
Formatting a Disk
To store programs on a new (or blank) disk, you must first prepare the disk u
to receive data. This is called "formatting" the disk. NOTE: Make sure
you turn on the disk drive before inserting any disk. u
The formatting process divides the disk into sections called tracks and LJ
sectors. A table of contents, called a directory, is created. Each time you
store a program on disk, the name you assign to that program will be u
added to the directory.
u
i >
t i
Li
LJ
n
n
To format a blank disk type this command:
n
OPEN 15,8,15: PRINT# 15, "N.A$,B$
n
In Place of A$, type a disk name of your choice; you can use up to 16 char-
n acters to identify the disk. In place of B$, type a two-character code of your
choice (such as W2).
n
The cursor disappears for a second or so. When the cursor blinks again,
H seal the disk with the following command:
n
n
The entire formatting process takes about a minute.
n
PI SAVEingonDisk
You can store your program on disk by using the following command:
n
SAVE"PROGRAM NAME",8
n The program name can be any name you choose, up to 16 characters long.
Be sure to enclose the program name in quotes. You cannot put two pro
n grams with the same name on the same disk. If you do, the second program
will not be accepted; the disk will retain the first one. In the example, the 8
n indicates that you are saving your program on device number 8.
SAVEing on Cassette
If you are using a Datassette to store your program, insert a blank tape in
n the recorder, rewind the tape if necessary, and type:
n NOTE: The screen will go blank while the program is being SAVEd,
but returns to normal when the process is completed.
Unlike disk, you can save two programs to tape under the same name.
However when you load it back into the computer, the first program
sequentially on the tape will be loaded, so avoid giving programs the same
name.
n
u
LJ
Once a program has been SAVEd, you lean LOAD it back into the com
puter's memory and RUN it anytime you wish. M
To load your BASIC program from a disk, use the following command: (—1
LOAD'TROGRAM NAME",8 JUI'IUKN y
In the example, the 8 indicates to the computer that you are loading from
device number 8. Be careful to type the! program name exactly as you <—I
typed it when SAVEing the program, dr the computer will respond "FILE
NOT FOUND." U
Once the program is loaded, type RUN[ and press RETURN to execute. LJ
U
LOADing from Cassette
and the next program on the tape will rje found. While the Datassette is
searching for the program the screen is blank. When the program is found, j j
the screen displays:
You can use the counter on the Datassette to identify the approximate <' /
starting position of the programs. Then, when you want to retrieve a pro
gram, simply wind the tape forward from 000 to the program's start loca- i j
tion, and type: ^
In this case you don't have to specify the PROGRAM NAME; your pro- < j
gram will load automatically because it |s the next program on the tape. LJ
LJ
LJ
n
Other Disk-Related Commands
n
Verifying a To verify that a program has been correctly saved,
n
Program use the following command:
n SEARCHING FOR $
LOADING
n
When the directory (the $ file) has been loaded, the
n READY message is displayed and the cursor reap
pears. You then type:
n
*********************************************
n You now know something about the BASIC language and some elementary
programming concepts. The next chapter builds on these concepts, introducing
f ( additional commands, functions and techniques that you can use to program in
BASIC.
H
n
37 CHAPTER 2-GETTING STARTED IN BASIC
n
y
u
u
y
u
y
u
y
u
u
u
LO
03
LO
L3
0
D
a
a
U
u
n
n
CHAPTER 3 COMPUTER DECISIONS—The IF-THEN Statement 43
n Advanced BASIC Using the Colon 44
Programming
i ( LOOPS-The FOR-NEXT Command 44
Empty Loops—Inserting Delays in a Program 45
H The STEP Command 46
H INPUTTING DATA 47
The INPUT Command 47
n Assigning a Value to a Variable 47
Prompt Messages 47
n Sample Program 48
The GET Command 49
r—>
BASIC FUNCTIONS 58
What Is a Function? 58
n The INTEGER Function (INT) 59
Generating Random Numbers—The RND Function 60
i i The ASC and CHR$ Commands 60
Converting Strings and Numbers 61
n The VAL Function 61
The STR$ Function 61
n The Square Root Function (SQR) 61
The Absolute Value Function (ABS) 61
n
41 CHAPTER 3-ADVANCED BASIC PROGRAMMING
n
LJ
LU
LU
U
Ul
u
u
iy
iy
u
yj
LJ
Ui
LJ
LJ
iii
LJ
Li
Ul
LJ
y
n
n
This chapter describes how to use a number of powerful BASIC com
n mands, functions and programming techniques.
r-i
These commands and functions allow you to program repeated actions
through looping and nesting techniques; handle tables of values; branch or
H jump to another section of a program, and return from that section; assign
varying values to a quantity—and more. Examples and sample programs
H show just how these BASIC concepts work and interact.
H
Computer In the preceding chapter you learned how to change the values of variables.
n Decisions— The next step is to have the computer make decisions based on these
The IF-THEN updated values. You do this with the IF-THEN statement. You tell the com
n Statement puter to execute a command only IF a condition is true (e.g., IF X = 5). The
command you want the computer to execute when the condition is true
n comes after the word THEN in the statement. Clear your computer's mem
ory by typing NEW and pressing RETURN, then type this program:
n
10 J = 0
20 ? J,"COMMODORE 64C"
n
3OJ = J+1
40 IF J<>5 THEN 20
60 END
n You no longer have to press the STOP key to break out of a looping pro
gram. The IF-THEN statement tells the computer to keep printing "COM
n MODORE 64C" and incrementing (increasing) J until J = 5 is true. When
an IF condition is false, the computer jumps to the next line of the pro
gram, no matter what comes after the word THEN.
Notice the END command in line 60. It is good practice to put an END
statement as the last line of your program. It tells the computer where to
p. stop executing statements.
SYMBOL MEANING
n EQUALS
GREATER THAN
LESS THAN
•L J
NOT EQUAL TO
n > = GREATER THAN OR EQUAL TO
< = LESS THAN OR EQUAL TO
n
43 CHAPTER 3-ADVANCED BASIC PROGRAMMING
n
U
U
These comparisions work in expected rhathematical ways with numbers.
There are different ways to determine if one string is greater than, less u
than, or equal to another. You can learn about these "string handling"
functions by referring to Chapter 6, BASIC 2.0 Encyclopedia. u
Now change line 10 to read N = 20, and RUN the program again. Notice
u
you can tell the computer to execute mcjre than one statement when N is
u
less than 5. You can put any statement(s) you want after the THEN com
mand. Remember that the GOTO 15 will not be reached until N<5 is true.
LJ
Any command that should be followed whether or not the specified
condition is met should appear on a separate line.
LJ
Loops—The
LJ
In the first RUN of the program used in the previous example, we made
FOR-NEXT the computer print the variable N five times by telling it to increase or
Command "increment" the variable N by units of one, until the value of N equalled
U
five; then we ended the program. There is a simpler way to do this in
BASIC. We can use a FOR-NEXT loop, like this:
u
10FORN=lTO5 LJ
20 ?N; "IS LESS THAN OR EQUAL TO 5"
30 NEXT N LJ
40 END
U
LJ
LJ
n
n
Type and RUN this program and compare the result with the result of the
n IF-THEN program—they are similar. In fact, the steps taken by the com
puter are almost identical for the two programs. The FOR-NEXT loop is a
n very powerful programming tool. You can specify the number of times the
computer should repeat an action. Let's trace the computer's steps for the
n program above.
H First, the computer assigns a value of 1 to the variable N. The 5 in the FOR
statement in line 10 tells the computer to execute all statements between
n the FOR statement and the NEXT statement, until N is equal to 5. In this
case there is just one statement—the PRINT statement.
n
This is how the computer interprets the inner workings of a FOR ...
n NEXT loop—it operates in much the same way as the IF... THEN exam
ple on the previous page. First, the 64C assigns a value of 1 to the variable
n N. It then executes all instructions between the FOR and NEXT key
words. When the NEXT statement is encountered, it tells the computer to
n increment the counter variable N (in this case by 1), compare N to 5 and
continue with another cycle through the FOR . . . NEXT loop if N = 5 is
n false. The increment defaults to 1 if no other increment is specified in the
FOR statement. After five passes through the loop, and once N = 5 is true,
r—i
! i the computer processes the statement which immediately follows the
NEXT statement and resumes with the rest of the program. Since the com
n puter does not compare the value of N to the start value of the loop varia
ble until the NEXT statement is encountered, every loop is executed at
n least once.
You can tell the computer to increment your counter by units (e.g. 10, 0.5 , ,
or any other number). You do this by Using a STEP command with the '—'
FOR statement. For example, if you want the computer to count by tens to >
100, type: U
10 FOR X = 0 TO 100 STEP 10 (_J
20 ?X
30 NEXT U
Notice that you do not need the X in the NEXT statement if you are only
executing one loop at a time—NEXT refers to the most recent FOR state- I—1
ment. Also, note that you do not have to increase (or "increment") your
counter—you can decrease (or "decrement") it as well. For example, change '—'
line 10 in the program above to read:
The computer will count backward from 100 to 0, in units of 10. <—'
If you don't use a STEP command with a FOR statement, the computer '—'
will automatically increment the counter by units of 1.
LJ
LJ
U
I [
46 CHAPTER 3-ADVANCED BASIC PROGRAMMING L™J
u
n
n
Inputting Data The INPUT Command
n
10K=10
n
20 FOR 1=1 TO K
30 ? "COMMODORE"
n
40 NEXT
50 END
n
In this program you can change the value of K in
n line 10 to make the computer execute the loop as
many times as you want it to. You have to do this
n when you are typing the program, before it is RUN.
What if you wanted to be able to tell the computer
H how many times to execute the loop at the time the
program is RUN?
n
In other words, you want to be able to change the
n value of the variable K each time you run the pro
gram, without having to change the program itself.
n We call this the ability to interact with the computer.
You can have the computer ask you how many times
n you want it to execute the loop. To do this, use the
INPUT command. For example, replace line 10 in
n the program with:
10 INPUT K
> i
n
47 CHAPTER 3-ADVANCED BASIC PROGRAMMING
u
u
quote marks of the prompt and the K. You may put
any message you want in the prompt, but the LJ
INPUT statement (line number included) must fit
within 80 characters, just as any BASIC command j I
must.
u
The INPUT statement can also be used with string
variables. The ssime rules that apply for numeric I |
variables apply for strings. Don't forget to use the $
to identify all your string variables. Clear your com- | 1
puter's memory by typing NEW and pressing
RETURN. Then type in this program. LJ
10 INPUT"WHAT IS YOUR NAME";N$ < ,
20 ? "HELLO ",N$ Lj
30 END (j
LJ
Sample Program
Now that you know how to use the FOR-NEXT loop and the INPUT com- •—'
mand, clear the computer's memory by typing NEW mm inrjj (then
type the following program: '—'
10T = 0 '■ jj
20 INPUT'HOW MANY NUMBERS";N
30FORJ=1TON (J
40 INPUT'TLEASE ENTER A NUMBER ";X
50T = T + X (J
60 NEXT
70 A=T/N : jj
80 PRINT
90 ? "YOU HAVE";N"NUMBERS TOTALING";T M
100 ? "AVERAGE =";A
110 END j_j
u
n
n
This program lets you tell the computer how many numbers you want to
n average. You can change the numbers every time you run the program
without having to change the program itself.
n
Let's see what the program does, line by line:
n
Line 10 assigns a value of 0 to T (which will be the running total of
n the numbers).
There are other BASIC commands you can use in your program to interact
n with the computer. One is the GET command which is similar to INPUT.
To see how the GET command works, clear the computer's memory and
n type this program.
n 10 GET A$
20 IF A$ = "" THEN GOTO 10
n 30? A$
40 END
i j
When you type RUN and press RETURN, nothing seems to happen. The
reason is that the computer is waiting for you to press a key. The GET com
n mand, in effect, tells the computer to check the keyboard and find out
what character or key is being pressed. The computer is satisfied with a
n null character (that is, no character). This is the reason for line 20. This
line tells the computer that if it gets a null character, indicated by the two
n
n
49 CHAPTER 3-ADVANCED BASIC PROGRAMMING
H
u
u
double quotes with no space between them, it should go back to line 10
and try to GET another character. This loop continues until you press a I—i
key. The computer then assigns the character on that key to A$.
The GET command is very important because you can use it, in effect, to
program a key on your keyboard. The Example below prints a message on I—1
the screen when Q is pressed. Type the program and RUN it. Then press Q
and see what happens. I—I
u
The READ-DATA Command >j
10T = 0 u
20FORJ=lTO5 L-J
30 READ X , |
40T=T+X ! U
50 NEXT , ,
60 A=T/5 U
70? "AVERAGE = ";A , ,
80 END L-1
90 DATA 5,12,1,34,18 , ,
u
n
n
When you run the program, the computer will print AVERAGE = 14.
n The program uses the variable T to keep a running total, and calculates the
average in the same way as the INPUT average program. The READ-
n DATA average program, however, finds the numbers to average on a
DATA line. Notice line 30, READ X. The READ command tells the com
n puter there must be a DATA statement in the program. It finds the DATA
line, and uses the first number as the current value for the variable X. The
n next time through the loop the second number in the DATA statement will
be used as the value for X, and so on.
n
You can put any number you want in a DATA statement, but you cannot
n put calculations in a DATA statement. The DATA statement can be any
where you want in the program—even after the END statement. This is
n because the computer never really executes the DATA statement; it just
refers to it. Be sure to separate your data items with commas, but be sure
n not to put a comma between the word DATA and the first number in
the list.
n
The computer uses an internal pointer to remind itself which piece of data
n was read last. After the computer reads the first number in the DATA
statement, the pointer points to the second number. When the computer
n comes to the READ statement again, it assigns the second number to the
variable name in the READ statement.
n
DATA statements can be placed anywhere in a program—at the beginning,
n in the middle, at the end, or interspersed throughout the program. For
efficiency, DATA statements are usually placed at the end of program. If
h you have more than one DATA statement in your program, the internal
DATA pointer will refer to the DATA statement containing the next
n unread DATA value.
n You can use as many READ and DATA statements as you need in a pro
gram, but make sure there is enough data in the DATA statements for the
n computer to read. Remove one of the numbers from the DATA statement
in the last program and run it again. The computer responds with ?OUT
n OF DATA ERROR IN 30. What happened is that when the computer
executed the loop for the fifth time, there was no data for it to read. That is
n what the error message is telling you. Putting too much into the DATA
statement doesn't create a problem because the computer never realizes the
n extra data exists.
n
51 CHAPTER 3-ADVANCED BASIC PROGRAMMING
n
u
u
The RESTORE Command
You can use the RESTORE command in a program to reset the data
LJ
pointer to the first piece of data if you need to. Replace the END statement i i
(line 80) in the program above with:
80 RESTORE LJ
and add: I j
85 GOTO 10
Now RUN the program. The program will run continuously using the
u
same DATA statement. NOTE: If the computer gives you an OUT OF
DATA ERROR message, it is because ^ou forgot to replace the number
u
that you removed previously from the DATA statement, so the data is all j i
used before the READ statement has been executed the specified number
of times. , ,
i I
You can use DATA statements to assigft values to string variables. The / >
same rules apply as for numeric data. Clear the computer's memory and
type the following program: , ,
10FORJ=lTO3
20 READ A$ LJ
30 ? A$
40 NEXT LJ
50 END
60 DATA COMMODORE,64C,GOMPUTER LJ
If the READ statement calls for a string variable, you can place letters or j i
numbers in the DATA statement. Notice however, that since the computer
is READing a string, numbers will be stored as a string of characters, not < |
as a value which can be manipulated. Numbers stored as strings can be
printed, but not used in calculations. Also, you cannot place letters in a i I
DATA statement if the READ statement calls for a number variable.
LJ
Using Arrays i j
You have seen how to use READ-DATA to provide many values for a vari
able. But what if you want the computer to remember all the data in the I I
DATA statement instead of replacing the value of a variable with the new
data? What if you want to be able to recall the third number, or the second 1 I
string of characters?
LJ
LJ
LJ
n
Each time you assign a new value to a variable, the computer erases the old
n value in the variable's box in memory and stores the new value in its place.
You can tell the computer to reserve a row of boxes in memory and store
n every value that you assign to that variable in your program. This row of
boxes is called an array.
n
Subscripted If the array contains all of the values assigned to the
n Variables variable X in the READ-DATA example, it is called
the X array. The first value assigned to X in the pro-
n gram is named X(l), the second value is X(2), and so
on. These are called subscripted variables. The num
n bers in the parentheses are called subscripts. You can
use a variable or a calculation as a subscript. The
n following is another version of the averaging pro
gram, this time using subscripted variables.
n
5 DIM X(5)
n 10T = 0
20FORJ=lTO5
n 30 READ X(J)
40T = T + X(J)
n 50 NEXT
60 A = T/5
n 70? "AVERAGE =";A
80 END
n 90 DATA 5,12,1,34,18
n
n CHAPTER 3—ADVANCED BASIC PROGRAMMING
53
H
LJ
U
COMMODORE 64C COMPUTER READ-DATA
program so the Computer will remember the ele
LJ
ments in the A$; array.
5 DIM ^ LJ
10FORjilTO3
20 READ A$(J) LJ
30?A$(J)i
40 NEXT LJ
50 END
60 DATA tOMMODORE,64C,COMPUTER LJ
TIP: You do not need the DIM statement in your U
program unless tihe array uses values greater than
A (10)—i.e., involves more than 11 elements. See U
DIMENSIONING ARRAYS.
LJ
Dimensioning Arrays can be used with nested loops, so the com
Arrays puter can handle data in a more advanced way. U
What if you hadj a large chart with 10 rows and 5
numbers in eacH row. Suppose you wanted to find U
the average of the five numbers in each row. You
could create 10 Arrays and have the computer calcu u
late the average of the five numbers in each one.
This is not necessary, because you can put all the u
numbers in a two-dimensional array. This array
would have the ^ame dimensions as the chart of LJ
numbers you want to work with—10 rows by 5
columns. The DIM statement for this array (we will u
call it array X) should be:
u
10 DIM Xj(10,5)
n 10 DIMX(10,5),A(10)
20FORR=1TO 10
n 30T = 0
35FORC=1TO5
n 40 READ X(R,C)
50T = T + X(R,C)
n 60 NEXT C
70 A(R) = T/5
n 80 NEXT R
90FORR=1TO 10
n 100 PRINT "ROW #";R
110FORC=1 TO 5
n 120 PRINT X(R,C):NEXT C
130 PRINT "AVERAGE = ";A(R)
n 140 FOR D= 1 TO 1000:NEXT
150 NEXT R
n 160 DATA 1,33,7,9
170 DATA 2,4,6,8,10
n 180 DATA 5,10,15,20,25
190 DATA 10,20,30,40,50
n 200 DATA 20,40,60,80,100
210 DATA 30,60,90,120,150
220 DATA 40,80,120,160,200
230 DATA 50,100,150,200,250
240 DATA 100,200,300,400,500
250 DATA 500,1000,1500,2000,2500
260 END
n
U
The part of program that the computer jumps to and executes is called a U
subroutine* Clear your computer's memory and enter the program below.
10 A$ = "SUBROUTINE":B$ = "PROGRAM"
u
20FORJ=lTO5
30 INPUT "ENTER A NUMBER";X
LJ
40 GOSUB 100
50 PRINT B$:PRINT
U
60 NEXT
70 END
U
100 PRINT A$:PRINT
110Z = XT2:PRINTZ
LJ
120 RETURN
U
This program will square the numbers you type and print the result. The
other print messages tell you when the computer is executing the subrou U
tine or the main program. Line 40 tells the computer to jump to line 100,
execute it and the statements following it until it sees a RETURN com- U
mand. The RETURN statement tells the computer to go back in the pro
gram to the statement following the GOSUB command and continue exe LJ
cuting. The subroutine can be anywhere in the program—including after
the END statement. Also, remember that the GOSUB and RETURN U
commands must always be used together in a program (like FOR-NEXT
and IF-THEN), otherwise the computer will give an error message. LJ
U
The ON GOTO/GOSUB Command
LJ
U
n
n
10 ? "ENTER A NUMBER BETWEEN ONE AND FIVE"
20 INPUT X
n
30 ON X GOSUB 100,200,300,400,500
40 END
100 ? "YOUR NUMBER WAS ONE":RETURN
200 ? "YOUR NUMBER WAS TWO":RETURN
300 ? "YOUR NUMBER WAS THREE":RETURN
400 1 "YOUR NUMBER WAS FOUR":RETURN
n
500 ? "YOUR NUMBER WAS FIVE":RETURN
n When the value of X is 1, the computer branches to the first line number in
the list (100). When X is 2, the computer branches to the second number in
n the list (200), and so on.
n
Using Memory Using PEEK and POKE for RAM/ROM Access
r—i
Locations
Each area of the computer's memory has a special function. For instance,
there is a very large area to store your programs and the variables associ
n
ated with them. This part of memory, called RAM, is cleared when you
n use the NEW command. Other areas are not as large, but they have very
specialized functions. For instance, there is an area of memory locations
that controls the music features of the computer.
There are two BASIC commands—PEEK and POKE—that you can use to
access and manipulate the computer's memory. Use of PEEK and POKE
commands can be a powerful programming device because the contents of
n
the computer's memory locations determine exactly what the computer
should be doing at a specific time.
n
n Using PEEK PEEK can be used to make the computer tell you
what value is being stored in a memory location (a
n memory location can store any value between 0 and
255). You can PEEK the value of any memory loca
n tion (RAM or ROM) in DIRECT or PROGRAM
mode. Type:
n P = PEEK(650)
?P
n
The computer assigns the value in memory location
n 650 to the variable P when you press RETURN after
the first line. Then it prints the value when you press
RETURN after entering the ? P command. Memory
location 650 determines whether or not keys like the
spacebar and CRSR repeat when you hold them
n
U
LJ
You cannot alter the value of all the memory loca
tions in the computer—the values in ROM can be
read, but not changed.
LJ
BASIC Functions What Is a Function?
LJ
A function is a predefined operation of the BASIC language that generally
provides you with a single value. When the function provides the value, it U
is said to "return" the value. For instance, the SQR function is a mathe
matical function that returns the square root of a specific number. U
U
n
n
Following are descriptions of some of the more commonly used functions.
For a complete list of BASIC 2.0 functions see Chapter 6, BASIC 2.0
n
Encyclopedia.
What if you want to round off a number to the nearest integer? You'll need
n to use INT, the integer function. The INT function takes away (truncates)
everything after the decimal point (for positive numbers only). Try typing
n these examples:
? INT(4.25)
n ? INT(4-75)
? INT(SQR(50))
If you want to round off to the nearest whole number, then the second
example should return a value of 5. In fact, you should round up any num
ber with a decimal of 0.5 and above. To do this, you have to add 0.5 to the
number before using the INT function. In this way, numbers with decimal
portions of 0.5 and above will be increased by 1 before being rounded
down by the INT function. Try this:
n
The computer added 0.5 to 4.75 before it executed the INT function, so
n that it rounded 5.25 down to 5 for the result. If you want to round off the
result of a calculation, do this:
n ?INT((100/6) + 0.5)
n You can substitute any calculation for the division shown in the inner
parentheses.
n
What if you want to round off numbers to the nearest 0.01? Instead of add
n ing 0.5 to your number, add 0.005, then multiply by 100. Let's say you
want to round 2.876 to the nearest 0.01. Using this method, you start with:
n
? (2.876 + 0-005)* 100 USHlg
< i
n Now use the INT function to get rid of everything after the decimal point
(which moves two places to the right when you multiply by 100). You are
left with:
which gives you a value of 288. All that's left to do is divide by 100 to get
n the value of 2.88, which is the answer you want. Using this technique, you
can round off calculations like the following to the nearest 0.01:
n ? INT((2.876 + 1.29+ 16.1*9-534 + 0.005)* 100)/100
10 R= INT(RND®*6+1)
20 ?R <—I
30 GOTO 10
( j
Each number generated represents one toss of a die. To simulate a pair of
dice, use two commands of this nature. Each number is generated sepa- j j
rately, and the sum of the two numbers represents the total of the dice.
LJ
The ASC and CHR$ Functions |_j
Every character that the Commodore 64C can display (including graphic
characters) has a number assigned to it. This number is called a character i—I
string code (CHR$) and there are 256 of them in the Commodore 64C.
There are two functions associated with this concept that are very useful. 1—1
The first is the ASC function. Type:
The computer responds with 81. 81 is the character string code for the Q ^ 1
key. Substitute any character for Q in the command above to find out the
Commodore ASCII code number for any character. I I
The computer responds with Q. In effect, the CHR$ function is the oppo
site of the ASC function. They both refer to the table of character string [ !
codes in the computer's memory. CHR$ values can be used to program
function keys. See Appendix E of this Guide for a full listing of ASC and ( j
CHR$ codes.
LJ
LJ
60 CHAPTER 3-ADVANCED BASIC PROGRAMMING |_J
u
n
n
Converting Strings and Numbers
n The VAL Function The VAL function returns a numeric value for a
string argument. Clear the computer's memory and
n type this program:
n 10 A$ = "64"
20 A = VAL(A$)
30 ? "THE VALUE OF ";A$;" IS";A
40 END
The square root function is SQR. For example, to find the square root of
50, type:
You can find the square root of any positive number in this way.
n
n The absolute value function (ABS) is very useful in dealing with negative
numbers. You can use this function to get the positive value of any
n number—positive or negative. Try these examples:
?ABS(-10)
n
? ABS(5)" IS EQUAL TO "ABS(- 5)
n
n
i i
The STOP and You can make the computer stop a program, and resume running it when u
CONT (Continue) you are ready. The STOP command must be included in the program. You
Commands can put a STOP statement anywhere you want to in a program. When the
LJ
computer "breaks" from the program (that is, stops running the program),
you can use DIRECT mode commands to find out exactly what is going on u
in the program. For example, you can find the value of a loop counter or
other variable. This is a powerful device when you are "debugging" or LJ
fixing your program. Clear the computer's memory and type the program ) )
below. d l
10X = INT(SQR(630)) u
20 Y = (.025*80)t2
30Z = INT(X*Y) u
40 STOP
45 ? "RESUME PROGRAMMING" u
50A = (X* Y) + Z
80 END u
Now RUN the program. The computer responds with "BREAK IN 40".
At this point, the computer has calculated the values of X, Y and Z. If you
want to be able to figure out what the rest of the program is supposed to
do, tell the computer to PRINT X;Y;Z. Often when you are debugging a
large program (or a complex small one), you'll want to know the value of a
variable at a certain point in the program.
Once you have all the information you need, you can type CONT (for LJ
CONTinue) and press RETURN assuming you have not edited anything
on the screen. The computer then CONTinues with the program, starting LJ
with the statement after the STOP command.
LJ
********************************************************* U
LJ
This chapter and the preceding one have been designed to familiarize you with
the BASIC programming language and some of its capabilities. Remember that U
more information on every command and programming technique in this book
can be found in the Commodore 64 Programmer's Reference Guide. The syntax U
for all Commodore 2.0 commands is given in Chapter 6, BASIC 2.0
Encyclopedia. LJ
U
LJ
LJ
LJ
62 CHAPTER 3-ADVANCED BASIC PROGRAMMING
LJ
n
n
u
u
G
LJ
a
u
u
u
u
u
U
U
u
n
n
CHAPTER 4 COLOR CHARACTER STRING CODES (CHR$) 67
Graphics, Color and
H
Sprites COLOR REGISTERS-CHANGING SCREEN, BORDER
n AND CHARACTER COLORS 68
COLOR MEMORY 72
n
ANIMATION 73
r»
SPRITE GRAPHICS 77
n
Sprite Concepts 77
Designing a Sprite Image 78
Converting Your Sprite Image Into Data 80
Controlling Sprites 83
Animating Your Sprites 86
Tying Your Sprite Program Together 88
n
PI GRAPHICS MODES 91
n
n
H
p
n
i f
n
r—■>
<—i
/_ i
u
u
u
u
u
LJ
u
u
u
u
LJ
u
u
n
Your Commodore 64C gives you exceptional graphics capabilities. The
n Commodore 64C offers sixteen colors, five graphics modes and program
mable animated objects called sprites. This chapter elaborates on the sev
G eral powerful graphics features built into the Commodore 64C and how
they are used.
H
n Color Character Each character on the 64C keyboard has a number associated with it.
String Codes When you press a key, the computer scans the keyboard and understands
n (CHR$) exactly which character is typed. A character code value is entered into
memory each time a key is pressed. These codes are referred to as character
string codes. Appendix D lists all the character string codes for the
Commodore 64C.
Within a program, you can select colors using character string codes
instead of holding down the CTRL key and pressing a numbered key. For
instance, enter the following sample program:
n 10 PRINT CHR$ (5)
20 PRINT "WHITE"
I s
When you RUN this program, the character color changes from blue to
white and the word "WHITE" is displayed. The other 15 colors also have a
character string code assigned to them. The following is a list of all the
n colors available on the Commodore 64C and the corresponding character
string codes:
H
Color CHR$Code Color CHR$Code
n CHR$(151)
White CHR$ (5) Dk. Gray
Red CHR$ (28) Gray CHR$ (152)
n Green CHR$ (30) Lt. Green CHR$ (153)
r-) Blue CHR$(31) Lt. Blue CHR$ (154)
Orange CHR$ (129) Lt. Gray CHR$ (155)
Black CHR$ (144) Purple CHR$ (156)
n Brown CHR$ (149) Yellow CHR$ (158)
Lt. Red CHR$ (150) Cyan CHR$ (159)
n
To select any of the 64C colors, PRINT the above character string codes
according to the colors you want to display on the screen. The following
program illustrates how to select colors within a program.
n
U
u
10 PRINTCHR$(5)
15 PRINT"WHITE"
20 PRINTCHR$(28) u
25 PRINT"RED"
30 PRINTCHR$(30) u
35 PRINT"GREEN"
40 PRINTCHR$(31)
45 PRINT"BLUE"
LJ
47 PRINTCHR$(129)
48 PRINT"ORANGE"
50 PRINTCHR$(144)
55 PRINT"BLACK"
60 PRINTCHR$(149)
65 PRINT"BROWN"
70 PRINTCHR$(150) U
7 5 PRINT"LT. RED"
80 PRINTCHR$(151) u
85 PRINT"DK. GRAY"
90 PRINTCHR$(152)
95 PRINT"GRAY" u
100 PRINTCHR$(153)
110 PRINT"LT. GREEN1 u
120 PRINTCHR$(154)
130 PRINT"LT. BLUE"
140 PRINTCHR$(155)
u
150 PRINT"LT. GRAY"
200 PRINTCHR$(156) u
210 PRINT"PURPLE"
220 PRINTCHR$(158) LJ
230 PRINT"YELLOW"
240 PRINTCHR$(159)
250 PRINT"CYAN" U
U
Color Registers— Your Commodore 64C has 64K of memory. This means the 64C holds 64
Changing Screen, times 1024 (65536) bytes of information. Think of the internal structure of U
Border and your computer as 65536 storage compartments piled one on top of the
Character Colors other. They are labeled starting from the bottom at location zero (0) and LJ
continue upward to location 65535 on top. You can also refer to each byte
as a register, so your 64C has 65536 registers. U
Each byte inside your computer is used for a specific purpose. For instance, u
you have 38911 bytes available to program in BASIC. Your Commodore
64C tells you this as soon as you turn on the computer and read the open LJ
ing screen. You may ask, what are all the rest of the bytes used for? They
control the computer's brain, known as the operating system. The operat U
ing system registers control all the features of your Commodore 64C.
LJ
A portion of the operating system controls graphics and color. You can
select different colors by changing the contents of the 64C color registers. U
There are three color registers which control the colors of the border, the
U
u
G
n
background and the characters. When you first turn on your 64C, the
n background color is dark blue and the character and border colors are light
blue. You can change the background, border and character color registers
with the BASIC POKE statement.
The POKE command modifies the contents of the specified location and
places the newly specified value in that location. The format of the POKE
n command is:
Did you notice what happened? The border color changed from light blue
to black. Location 53280 is the border color register. Location 53281 is the
n background color register and location 646 is the character color register.
Now change the background color from dark blue to black with the follow
n ing command:
n Note that the character color changes to white, but the characters already
on the screen remain the same color as before. All the characters you type
n from now on are displayed in white unless you change the character color
again.
n
You're probably wondering what the values that are POKEd into the color
registers mean. These values are the color information codes for the 16
colors available on the Commodore 64C. The following list contains all the
Commodore 64C colors and the corresponding color codes:
0 Black 8 Orange
1 White 9 Brown
2 Red 10 Light Red
3 Cyan 11 Dark Gray
n 4 Purple 12 Gray
5 Green 13 Light Green
6 Blue 14 Light Blue
7 Yellow 15 Light Gray
10 FORI=0TO15
15 POKE53280,I
16 FORJ=lTO500:NEXT
18 NEXT
19 POKE53280,0 LJ
20 FORI=0TO15
25 POKE53281,I U
26 FORJ=lTO500:NEXT
28 NEXT U
29 POKE53281,0
30 FORI=0TO15
LJ
35 POKE646,I
36 PRINT"COLOR"
37 FORJ=lTO500:NEXT U
38 NEXT
39 POKE646f14 U
50 POKE53280,14:POKE646, 14:POKE53281,6
i >
This program changes the color code value of each of the color registers
using a FOR... NEXT loop. Lines 10 through 18 POKE each color value LJ
from 0 (black) to 15 (light gray) into the border color register and displays
each border color on the screen. Lines 20 through 28 POKE each color U
value into the background color register and display each background color
on the screen. Lines 30 through 38 POKE each color value into the charac U
ter color register and display each character color on the screen.
Lines 16, 26 and 37 are FOR ... NEXT loops that slow down the program.
They are empty FOR... NEXT loops that delay program execution so LJ
you can notice the color changes on the screen. Try the program without
the delay loops and see how fast the Commodore 64C runs. Line 40 LJ
restores the original border, screen and character color registers.
LJ
Screen Memory Since graphics is one of the Commdore 64C's strongest features, the screen LJ
is an important part of the computer. The 64C's screen has 1000 character
positions—40 columns by 25 rows. Each character position uses one byte U
of memory, so the 64C needs 1000 bytes to store the information you see
on the screen.
\ J
In the Color Register section, we referred to the memory of the Commo
dore 64C as 65536 storage compartments piled one on top of the other.
LJ
t /
n
Screen memory uses part of those storage compartments starting at loca
n tion 1024 and ending at location 2023. The screen appears as a grid having
40 X (horizontal) positions and 25 Y (vertical) positions. In memory, how
n ever the character positions are actually stored sequentially.
n The top left character position on the screen, referred to as the HOME
position, is stored at location 1024. The character position directly to the
n right of HOME is stored at location 1025 and so on. The character posi
tion at the top right corner of the screen is stored at location 1063, 40 loca
tions past the beginning of screen memory. The last character position,
located at the bottom right corner of the screen, is stored at location 2023,
n the end of screen memory. Examine Figure 4-1 to understand the corre
spondence between the way the screen looks and the way information is
sequentially stored in memory.
1063
1024-
1064
1104
1144
1184
1224
1264
1304
1344
1384
n 1424
1464
1504
n 1544
1584
1624
1664
< i 1704
1744
1784
n 1824
1864
1904
1944
1984 24
2023
Remember when you learned about character string codes in the Color
Character String Code section? The Commodore 64C has a separate set of
codes used only by screen memory to display characters on the screen.
Instead of outputting characters to the screen in PRINT statements, you
POKE a screen code value directly into a specific screen memory location.
/ i
For example, enter the following line:
POKE 1024,1
n
71 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
n
u
u
Did you notice what happened? The letter "A" is displayed in the upper left
corner of the screen. Appendix C contains a list of the screen codes used u
in POKE statements to display characters on the screen. Notice that the
screen codes in Appendix C and the character string codes in Appendix D u
are different. Appendix C contains screen codes that can only be POKEd
directly into screen memory. Appendix D contains character string codes LJ
that are used more universally for inputting from and outputting to the
screen, printer, disk drive and Datassette. U
You can POKE any of the values in Appendix C into any of the screen
u
locations between 1024 and 2023. Experiment with different characters
and try displaying messages on the screen by POKEing a series of screen u
codes into consecutive screen memory locations. You can create character
graphic images by POKEing different screen code graphic symbols in pat u
terns that form picture images.
U
Color Memory Now that you have grasped the concept of screen memory, you need to
know how to control the color of each character position on the screen. u
The Commodore 64C has a separate section of memory called COLOR
MEMORY, that controls the color of information on the screen. The 64C u
uses 1000 bytes to store the color information for the 1000 character posi
tions on the screen. Each screen memory location has a corresponding u
color memory location assigned to it. Compare Figure 4-1 with Figure 4-2,
to understand the correspondence between screen memory and color mem u
ory and the way screen and color information are stored sequentially.
LJ
FIGURE 4-2: COLOR MEMORY MAP
COLUMN
LJ
20 30 39
55335 u
55296-
55336 LJ
55376
55416
55456
55496
U
55536
55576
55616 LJ
55656
55696
55736
55776 u
55816
55856
55896
55936
u
55976
56016
56056
u
56096 20
56136
56176 LJ
56216
56256
LJ
56295
n
Location 1024 in screen memory corresponds to location 55296 in color
n memory. Location 1063 corresponds to location 55335. Screen memory
location 2023 corresponds to color memory location 56295. Remember,
n each screen location has a one to one correspondence to a color memory
location that controls its color.
n
In the screen memory example you POKEd a 1 into location 1024 as
n follows:
POKE 1024,1
n
This places the letter "A" in the HOME position on the screen. Now
n change the color of the letter "A" in the HOME position with the following
POKE:
POKE 55296,1
Did you notice the difference? The letter "A* in the HOME position
changed from light blue to white. At this point you may wonder what the
"1" means in POKE 55296,1. This time the "1" is not a screen code that
represents a character. Instead it is the color code for white. Refer to the
n Color Registers section for the list of Commodore 64C colors and the cor
responding color codes.
n
Remember, if you want to POKE a character to the screen, you actually
n need two POKEs. First, POKE a screen code into screen memory to display
a character. Second, POKE a color code into color memory to display the
n color of the character.
n
Animation The Commodore 64C is capable of animating objects on the screen. The
j l
idea behind computer animation is to display an image on the screen and
simulate its motion through computer instructions.
n
Remember when you POKEd a character into screen memory and it was
displayed on the screen? That's what you are going to do to animate a
graphic character. To animate a graphic character on the screen, POKE its
screen code into a screen memory location. Next, POKE the screen code
for a blank (32) into the same screen location. Then POKE the graphic
n character screen code into a screen location next to the original one.
Repeat the process with a series of adjacent screen memory locations. Since
the computer is displaying and blanking out the graphic character in suc
cessive screen locations so quickly, the image appears to be moving. For
n example, type in the following program and RUN it.
G
73 CHAPTER 4-GRAPH1CS, COLOR AND SPRITES
H
u
u
10 PRINT"D"
20 FOR 1=1024 TO 2023 STEP41 LJ
30 POKEI,81
35 POKE54272+If7 LJ
40 FOR J=1TO45:NEXT
45 POKEI,32 LJ
50 NEXT
100 FOR I=2009TO1450 STEP-39 U
110 POKEI,81
120 POKE54272+I,7 U
130 FOR J=1TO45 :NEXT
140 POKEI,32 LJ
150 NEXT
160 GOTO20 LJ
This is your first taste of animation. You have just made a yellow ball \ (
bounce on the screen. Although the bouncing ball program is a simple
example of animation, you are now on your way to programming sophisti- [^J
cated, animated graphics.
LJ
Here's an explanation of the program:
t (
• Line 10 clears the screen. Loop 1, lines 10 through 50, displays and *—'
moves the ball from the top of the screen to the bottom. Line 20 ,
begins a loop at the start of screen memory. Notice the FOR ... ^
NEXT statement has the words STEP 41. This tells the computer to ) ,
increment the index variable I, by 41 locations at a time, starting at *—^
location 1024 and ending at location 2023. When STEP is not spec- ^
ified in a FOR ... NEXT loop, your computer cycles through each *—'
index variable one at a time. , ,
• Line 30 POKEs screen code value 81 into the screen location
according to the index variable I. The value 81 represents the screen ,
code for the ball character that bounces on the screen. The first "—^
cycle of the loop POKEs screen code 81 into location 1024. The s .
second cycle POKEs screen code 81 into screen location 1065 *—
(1024 + 41). The third cycle POKEs screen code 81 into screen loca- . .
tion 1106 (1065 + 41) and so on. Each cycle through the loop skips *—'
40 screen locations and POKEs the ball 41 locations past the pre- , j
vious screen location. *—*
LJ
LJ
LJ
74 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
u
n
n
• Line 35 POKEs color code 7 (yellow) into the color memory loca
n tion corresponding to the screen location that is POKED with the
ball character. Remember, when you POKE a screen code value
into screen memory, you also have to POKE a color code value into
the corresponding color memory location. See Figure 7-1 and 7-2 to
understand how each screen memory location corresponds to its
own color memory location.
n • In line 35, location 54272 4- I is the beginning of color memory
during the first cycle of the loop (54727 + 1024 = 55296). The
loop increments the color memory location the same way as screen
memory. The second cycle of the loop increments the index variable
I, so the POKE statement in line 35 POKEs the color code value
into location 55337 (55296 + 41). Color location 55337 corres
ponds to screen location 1065. As you can see, the loop takes care
of POKEing the screen location and corresponding color location
so that the ball is always displayed correctly in yellow.
• Line 40 is an empty FOR... NEXT loop. It acts as a time delay
to slow down the program so the animation appears smooth. Try
the program without line 40. You'll notice the program becomes
n choppy.
• Line 45 POKEs screen code value 32, the blank character, into the
same screen location that was POKEd with screen code 81 in line
30. This turns off the ball character. The ball character is turned
n on and off so quickly, it looks as though the ball is always on the
screen.
n
75 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
u
u
Here's another animation program that bounces the yellow ball off all four
"walls" of the screen. This program is based on program three, but it has '—'
five loops instead of three. Each of the five loops is just like the two loops
in the preceding program, except that the last three loops use different LJ
screen locations to control the three additional bounces of the ball.
u
10 PRINT"Q"
20 FOR 1 = 1024 TO 2023 STEP41 l_[
30 POKEI,81
35 POKE54272+I,7 |l
40 FOR J=1TO45:NEXT
45 POKEI,32 \j
50 NEXT
100 FOR I=2009TO1450 STEP-39 »J
110 POKEI,81
120 POKE54272+I,7 \_j
130 FOR J=1TO45 .-NEXT
140 POKEI,32 (J
150 NEXT
200 FOR I=1423TO1044 STEP-41 [\
210 POKEI,81
220 POKE54272+I,7 j_j
230 FOR J=1TO45 :NEXT
240 POKEI,32 [J
250 NEXT
300 FOR I=1050TO1554 STEP38 |J
310 POKEI,81
320 POKE54272 + I,7 )_}
330 FOR J=1TO45 :NEXT
340 POKEI,32 ]_j
350 NEXT
400 FOR I=1544TO2009 STEP42 «J
410 POKEI,81
420 POKE54272+I,7 j_j
430 FOR J=1TO45 :NEXT
440 POKEI,32 jj
45 0 NEXT
490 GOTO100 i i
i >
Now that you can animate a simple graphic character, it's time to learn a s >
much more sophisticated method called sprite animation.
LJ
LJ
[ j
76 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
LJ
n
n
Sprite Graphics Sprite Concepts
n
YouVe learned how to control color with the CTRL key, with PRINT
n statements, and with character string codes. You now know how to PRINT
alphanumeric and graphic characters on the screen within quotes, as char
acter strings, and by POKEing values directly into screen memory. Ani
mating existing character images, as described in the last section, has cer
n tain limitations. For true graphic animation, you need a way to create your
own images, color those images and control their movement on the screen.
That's where sprites come in.
n Sprites are programmable movable objects. They are animated, high reso
lution images you can create into any shape. You can move these images
anywhere on the screen and color them in 16 colors. The Commodore 64C
has a set of sprite registers that control the color, movement and shape of
the sprite. Sprites on the 64C provide you with true animation and sophis
ticated graphics capabilities.
A special chip inside the 64C, called the VIC (Video Interface Controller)
n chip, controls graphics modes and sprites. Border and screen color registers
as well as the sprite registers are all part of the VIC chip. The VIC chip
normally can control 8 sprites at once. Through advanced programming
you can control more than eight sprites. The VIC chip can even determine
I !
if a sprite has moved in front of or behind another sprite. The size of each
sprite can also be expanded both vertically and horizontally. You can use
sprites in any mode: standard character, multi-color, standard and multi
color bit map and extended color modes. See the discussion of Graphics
r-i Modes later in this section for more information.
Let's begin by examining the properties of characters first, and then relate
them to sprites. A character on the screen is an 8 by 8 dot grid. Since there
n are 40 columns by 25 lines on the screen, the entire screen has 320 (40 x 8
dots per character width) dots across times 200 (25 lines x 8 dots per char
acter height) tall, which equals 64,000 total dots.
Each dot on the screen is called a pixel. Pixel is a computer term for picture
element. A sprite is made up of a 24 by 21 pixel grid, compared to a charac
ter which is an 8 by 8 pixel grid. The width of a sprite is 24 pixels, which is
equal to the width of three screen characters (bytes). Since a sprite is 21
n
77 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
n
u
u
rows of three bytes wide, a sprite requires 63 bytes (21 rows x 3 bytes per
row) of storage. Figure 4-3 illustrates the layout and storage requirements of u
a sprite.
u
Designing a Sprite Image
u
The first step in programming a sprite is designing the sprite image. For a
beginner, the best way to design a sprite is on a piece of graph paper. Draw u
a box 24 blocks across by 21 blocks tall, just like Figure 4-3. The box you
have just drawn is 504 (21 x 24) square blocks. Each block represents a bit u
in memory. If you divide 504 by 8 bits per byte, you'll see that the sprite
uses up 63 bytes of memory. U
FIGURE 4-3. SPRITE GRID
LJ
SPRITE GRID
U
24 BITS<D0TSV8«3 BYTES PEF ROM
U
ROM
1 ........ |,
u
2 „......« J
3 , |. .
LJ
4 . ,
5 ........
LJ
6 ........
7
3
LJ
9
10
U
11
12 U
13 | ........
14 U
13
16 | ........
a
17 ........ 1 ........
13 ........ 1 ........ u
19 ........ I
20 u
21
u
24 BITS<D0T5> X 21 BITS<D0TS> * 504 DOTS
504 BITS/8 BITS PER BYTE = 63 BYTES
u
OR
21 ROWS X 3 BYTES PER ROW = 63 BYTES U
LJ
u
n
n
You can now start designing your sprite image. Keep in mind that each
n block within the box you have drawn represents one bit in the Commo
dore 64C's memory. As you probably know by now, a bit can take on one
LJ of two values, zero or one. That is why a bit is called a binary digit, since
the root "bi" means two. A zero (0) means that a bit is "off' and a one (1)
i \ means that a bit is turned "on".
n When you are designing your sprite on a piece of graph paper, darken a
block if you want that bit to be on, and leave a block blank if you want that
n bit off. The combination of darkened blocks and blank blocks forms your
sprite image. That is, if you want to turn on a dot in the sprite image, you
H must turn on a corresponding bit in memory where the sprite DATA
is stored.
<—i
I i
u
U
LJ
u
LJ
LJ
u
1 /
i )
The next step in programming a sprite is coding the sprite image into data u
the computer can understand. On your sheet of graph paper, label the top
of each column the same as in Figure 4-6. LJ
Label the first eight columns as follows: 128, 64, 32, 16, 8, 4, 2, 1. Label LJ
the second and third set of eight columns the same way.
LJ
You now have three sub-sets (bytes) of eight columns (bits) per row, each
labeled from 128 on the left to 1 on the right. Each 8 column sub-set repre U
sents 8 pixels that correspond to a byte of memory. Again, since there are
21 rows with three bytes each, the total amount of memory the sprite LJ
requires is 63 bytes.
U
LJ
LJ
LJ
80 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
LJ
n
n
FIGURE 4-6. LABELING THE SPRITE-MAKING GRID.
n
128 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1
H I I I I I I I I I I I I
4 ii
IB
1 !!!i |!
MB
■■■1 ■■
|=
ii
n
■ ■
■1 !"■■ i !
_■ I I II
1
•
_ -
i
n
-P =ff ■I■I
■
■Mi
n
■■■■ ■■■■
+I I -.
TTi
MB
■T 1 —
n Now you have a way to convert the graph paper image to computer data.
For each darkened square within an eight column sub-set (byte) add up the
n
number at the top of the column. Do this for each of the three 8 column
sub-sets per row or a total of 63 times. Do not add column values in which
individual squares are blank since these represent "off pixels. Only add up
the column values for the darkened squares. Once you calculate all the byte
n values for each eight column sub-set, you have 63 pieces of data to define
your sprite. These values must be READ by the 64C and stored in DATA
n statements within a program. Study Figure 4-7 to grasp the concept of
converting a sprite picture on graph paper to data used by the 64C.
n
LJ
U
FIGURE 4-7. SPRITE-MAKING GRID WITH DATA VALUES
U
128 64 32 16 8 4 2 1 |128 64 32 16 8 4 2 1 1126 64 32 16 8 4 2 1
I I I I I I I I
MJlllllll
!■
I
LJ
-L.- J-
U
1 ! !I I P
rT
LJ
mmi
E
■ ■
E
Ml
LJ
MMI MMB
i;
■■■a ■MM Ml
t:|Is! ■ffi I
MMB MMB
i !
u
MMI
Ml
■Hi
Ml
!
MMI
- I Mi
MM ■■■■ MM!
LJ
LJ
LJJ LJ
ZtJZC
100 DATA 0,0,0
LJ
110 DATA 0,126,0
120 DATA 1,129,128
130 DATA
140 DATA
2,0,64
12,0,48 U
150 DATA 8,0,16
LJ
160 DATA 19,197,200
170 DATA 16,0,8
180 DATA 32,195,4
190 DATA 32,195,4
200 DATA 32,24,4 U
210 DATA 32,24,4
220 DATA 32,24,4
230 DATA 16,126,8 U
240 DATA 17,60,136
250 DATA 8,129,16
260 DATA 8,126,16 LJ
270 DATA 4,0,32
280 DATA 2,0,64
290 DATA 1,129,128
300 DATA 0,126,0
In the program shown in Figure 4-7, the DATA values in line 100 corres u
pond to the three sub-sets of the first row of the sprite grid. All three pieces
of DATA equal zero since all three sub-sets of the first row of the sprite grid u
are blank (off). Line 110 corresponds to the second row of the sprite grid.
The first DATA value in line 110 equals zero, because again, no pixels are u
turned on in that sub-set. The second piece of DATA in line 110 equals
126, since the squares in the column positions labeled 64, 32, 16, 8, 4 and 2 u
in the middle sub-set are all turned on.
u
LJ
n
n
Again the third DATA value in line 110 is zero because none of the pixels
n in that 8 column sub-set is turned on. The DATA in line 120 represents
the pixel values for the third row of the sprite grid, line 130 represents the
n values in the fourth row of the sprite grid, and so on. Line 300 corresponds
to the last row of the sprite grid.
n
Now that you know how to design a sprite on a sheet of graph paper and
n code it into DATA that the Commodore 64C can understand, you are
almost ready to write your first sprite program. But first let's examine the
sprite registers and how they work.
n
Controlling Sprites
Special memory locations within the video chip, known as sprite registers,
are set aside to control sprites. Each sprite register is assigned a specific
task. The first register you need to set is the sprite enable register at loca
tion 53269. As the name implies, the sprite enable register turns on a
sprite. You must POKE a value into the sprite enable register, depending on
which sprite(s) you want to turn on. Here's a list of the POKE values that
n enable each sprite:
n You may have noticed the POKE value for each sprite is equal to two,
raised to the sprite number. For example, the POKE value for sprite seven is
n two raised to the seventh power, which equals 128. Figure 4-8 illustrates
this concept.
n
FIGURE 4-8. SPRITE POKE VALUES
n Decimal values of each
sprite number
n
21 0 0 0 0 0 1 0 | 0
n
n
83 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
u
LJ
The POKE command to turn on sprite 7 is:
Now you have to store the sprite DATA somewhere in the Commodore j j
64C's memory. Although you already converted your sprite picture into
DATA as in lines 100 through 300 in Figure 4-7, you still have to READ LJ
that DATA and POKE it into memory. Before you can do that, you must
tell the 64C where to store the DATA. LJ
You point out where the DATA is stored using a sprite pointer. Each of the [ |
eight sprites has its own pointer. The following is a list of the sprite pointer
memory locations: 1 j
4 2044
5 2045 u
6 2046
7 2047 u
Now that you know what location to POKE for each sprite pointer, you j j
need to know the value to POKE into these locations. Here's the formula:
LJ
1. Choose an available memory location that is not being used. For
this example, choose location 12288. LJ
2. Divide the chosen location by 64: 12288/64 = 192
3. POKE the sprite pointer of the sprite you previously enabled with (_j
the quotient from step 2. To continue our previous example, the
following POKE command uses the seventh sprite pointer to point l_J
to sprite DATA starting at location 12288:
LJ
LJ
\ \
84 CHAPTER 4-GRAPHICS, COLOR AND SPRITES ^
U
n
n
As mentioned before, the sprite DATA must be READ and then POKEd
h into memory once the sprite pointers tell the 64C where to store the
DATA. The sprite pointer was set with the previous POKE command. Now
n you can READ the sprite DATA you converted from your sprite image and
POKE it into memory starting at location 12288. POKEing the DATA into
memory actually creates the sprite. The following program segment
READs the DATA and POKEs it into memory starting at location 12288.
n
50 FOR N = 0 to 62
n 60 READ Q
70 POKE 12288+ N,Q
80 NEXT
So far you have enabled the sprite, set the sprite pointer to tell the 64C
where to store the sprite DATA and POKEd the sprite into memory. All
you need to do now is to assign a sprite color and control the sprite's move
ment on the screen, and your sprite program will be finished.
Each sprite has its own sprite color register. The following is a list of sprite
n color register locations:
To assign a sprite color, POKE a sprite color register with a color code
n
between 0 and 15. For example, if you enter:
n POKE 53294,7 ItltTUHN
sprite 7 is colored yellow. (For a list of color codes, see the Color Registers
n
discussion given earlier in this section.)
n
n
85 CHAPTER 4-GRAPHICS, COLOR AND SPRITES
u
u
Animating Your Sprites
Animation is the last step before your program can RUN. The key behind 1—I
animation is motion. Each of the eight sprites has two registers that control ,
movement on the screen. One register is the sprite X position, which con- L-J
trols the horizontal sprite movement. The other is the sprite Y position,
which controls the sprite's vertical movement. The following is a list of the <—f
sprite X and Y position registers for each sprite:
Notice that the FOR... NEXT loop moves sprite 7 the maximum number
of vertical values (200), but only moves horizontally 200 out of the 320 LJ
possible positions. That was done to keep the example program simple.
u
The sprite Y position register can store any of the 200 possible vertical
position values. The sprite X position register cannot store all of the 320 LJ
horizontal position values because the sprite position register, like all other
memory locations in the Commodore 64C, can only represent a value up Li
to 255.
u
LJ
n
n
How do you position a sprite past the 255th horizontal screen position?
n The answer is, you have to borrow a bit from another register in order to
represent a value greater than 255.
n
An extra bit is already set aside in the 64C's memory in case you want to
move a sprite past the 255th horizontal location. Location 53264 controls
n
sprite movement past position 255. Each of the 8 bits in 53264 controls a
sprite. Bit 0 controls sprite 0, bit 1 controls sprite 1 and so on. For example,
if bit 7 is on, sprite 7 can move past the 255th horizontal position.
n Each time you want a sprite to move across the entire screen, turn on the
borrowed bit in location 53264 when the sprite reaches horizontal position
n
255. Once the sprite moves off the right edge of the screen, turn off the
n borrowed bit so the sprite can move back onto the left edge of the screen.
The following POKE command allows sprite seven to move past the 255th
horizontal position:
[..I
POKE 53264,128
n
The number 128 is the resulting value from turning on bit 7. You arrive at
this value by raising two to the seventh power. If you want to enable bit 5,
n
raise two to the fifth power, which of course equals 32. The general rule is
to raise two to the power of the sprite number that you want to move past
n
the 255th horizontal screen position. Now you can borrow the extra bit
you need to move a sprite all the way across the screen. To allow the
sprite to reappear on the left side of the screen, turn off bit seven again,
as follows:
n
POKE 53264,0
n
Not all of the horizontal (X) and vertical (Y) positions are visible on the
r—i screen. Only vertical positions 50 through 249 and horizontal positions 24
i i
through 342 are visible. In the example, when you moved sprite 7 on the
screen, you started the sprite moving at horizontal location zero and verti
n cal position zero. Location 0,0 is off the screen as is any horizontal location
less than 24 and greater than 343. Any vertical location less than 50 and
greater than 249 is also off the screen. The OFF-SCREEN locations are set
aside so that an animated image can move smoothly onto and off of the
n screen. Study Figure 4-9 to understand the layout of the visible horizontal
and vertical sprite positions.
n
H
U
u
FIGURE 4-9. VISIBLE SPRITE POSITIONS
u
0 (S00) 24 (S18)
u
VISIBLE VIEWING AREA
LJ
NTSC*
u
40 COLUMNS
u
208 ($00)-1 25 ROWS
229 ($E5)
LJ
'North American television transmission standards lor your home TV
Now you are ready to tie all the sprite concepts together into a sprite pro
gram. Let's review the entire procedure. In order to program a sprite,
you must: u
LJ
U
n
n
10 PRINT"C"
n 20 POKE53269,128
30 POKE2047,192
n 50 FORN=0TO62
60 READ Q
70 POKE12288+N,Q
80 NEXT
85 FOR Z=lTO200
90 POKE53262,Z
95 POKE53263,Z
98 NEXT
100 DATA 0,0,0
110 DATA 0,126,0
120 DATA 1,129,128
130 DATA 2,0,64
n 140 DATA 12,0,48
150 DATA 8,0,16
n 160 DATA 19,197,200
170 DATA 16,0,8
n 180 DATA 32,195,4
190 DATA 32,195,4
200 DATA 32,24,4
210 DATA 32,24,4
220 DATA 32,24,4
230 DATA 16,126,8
n 240 DATA 17,60,136
250 DATA 8,129,16
260 DATA 8,126,16
270 DATA 4,0,32
280 DATA 2,0,64
290 DATA 1,129,128
h 300 DATA 0,126,0
Now add the following lines and RUN the program again.
n
55 POKE 53271,128
n 57 POKE 53277,128
Notice that the sprite now appears twice its original size. Location 53277
n controls horizontal expansion and location 53271 controls vertical expan
sion of the sprite. The value POKEd into these locations is calculated
n
according to which sprite you want to expand. The general rule is raise two
to the power of the sprite number. For example, to expand sprite 7, the
n value 128 in lines 55 and 57 is calculated as two raised to the seventh
power, or 128.
n
u
You have successfully written your first sprite program. Use this program as
a basis and try adding other sprites to it. Notice lines 100 through 300 only ii
contain three pieces of DATA each. The program is written this way to
illustrate the correspondence between each DATA item and each eight < j
column byte in Figure 4-7. When you become more familiar with sprite
concepts you can shorten the program by including more DATA items in ij
each DATA statement. Lines 100 through 300 are still stored as 80 charac
ter lines. The spaces are stored in memory just as visible characters, but ij
they use memory needlessly. The process of shortening programs is called
crunching. Later, when you become a more advanced programmer, you i j
will realize the value of crunching your programs and using the Commo
dore 64Cs memory more efficiently. j j
u
Two more sprites appear on the screen, one from the left side of the screen
and one from the top. Notice in the program, both sprites 5 and 6 use the j j
same sprite DATA as sprite 7. That's why all three sprites look the same. If
you want to change the way a sprite looks, design another sprite image on a [_J
piece of graph paper just as you did before. Then add another complete set
of sprite DATA as in lines 100 through 300. In addition, READ the DATA |_J
and POKE it into a section of memory other than locations 12288 through
12351, since the other sprite DATA is already there. Finally, set the sprite j_J
DATA pointer to the starting location where the sprite DATA is POKEd
into memory. LJ
All three sprites in the above program store their DATA starting at loca- j [
tion 12288. That's why lines 30, 35 and 37 POKE the same value into each
LJ
90 CHAPTER 4-GRAPHICS, COLOR AND SPRITES LJ
LJ
n
n of the three respective sprite DATA pointers. If all eight sprites were
enabled, each one could use the same DATA and you would have eight
n identical sprites on the screen.
n
Lines 43 and 45 color sprite 6 blue and sprite 5 red. Lines 92 through 97
control the movement of sprites 5 and 6. Line 99 puts the program into a
continuous loop. If you want to stop it, press the RUN/STOP key. Notice
the sprite remains on the screen. To clear the screen completely, hold down
the RUN/STOP key and press the RESTORE key.
n Up to now, youVe programmed three sprites on the screen. Try using all
vn eight. In a relatively short time you should be able to create your own
sprites in several colors and animate them on the screen. You can then
move on to explore the very sophisticated color, graphics and animation
n
features available on the 64C. Consult the Commodore 64 Programmer's
Reference Guide for more information on color graphics, sprites and
n animation.
n
Graphics Modes The Commodore 64C can operate in five different graphics modes. They
h
are divided into two groups known as character display modes and bit
map modes. Character display modes, as the name implies, display an
n
entire 8x8 dot character grid at a time. In character display modes, the
smallest unit of information you can display is an 8 x 8 pixel grid which
n equals one character. Bit map modes allow you to display each pixel, one at
a time. Bit map mode gives you absolute control over the screen image.
n Graphics performed in bit map mode are referred to as high resolution
graphics.
n
n
Bit map modes are separated into these two subdivisions:
n
1. Standard Bit Map Mode
2. Multi-Color Bit Map Mode
n
Each of the character display modes get character information from one of
n
two places in the 64Cs memory. Normally, character information is taken
from character memory stored in a separate chip called a ROM (Read Only
n Memory). However, the 64C gives you the option of designing your own
n
u
When you first turn on the 64C, you are automatically in standard charac- '—'
ter mode. When you write programs, the 64C is also in standard character ,
mode. Standard character mode displays characters in one of 16 colors on LJ
a background of one of 16 colors. All the information contained in this , f
chapter operates in standard character mode except sprites. Sprites are —»
classified separately from character display modes and bit map modes.
Multi-color character mode gives you more control over color than the
standard graphics modes. Each screen dot within an 8 x 8 character grid L-J
can have one of four colors, compared to the standard modes which can
only have one of two colors. Multi-color mode uses two additional back- >—'
ground color registers. The three background color registers and the char
acter color register together give you a choice of four colors for each dot —I
within an 8 x 8 dot character grid.
Extended background color mode allows you to control the background I—I
color and foreground color of each character. Extended background color
mode uses all four background color registers. In extended color mode, I—I
however, you can only use the first 64 characters of the screen code charac
ter set. The second set of 64 characters is the same as the first, but they are LJ
displayed in the color assigned to background color register 2. The same
holds true for the third set of 64 characters and background color register LJ
3, and the fourth set of 64 characters and background color register 4. The
character color is controlled by color memory. For example, in extended LJ
color mode, you can display a purple character with a yellow background
on a black screen. ! 1
Standard bit map mode allows you to control each screen dot in one of two LJ
colors. This gives you the ability to create detailed graphic images on the
screen. Bit mapping is a technique that stores a bit in memory for each dot 1 j
on the screen. If the bit in memory is turned off, the corresponding dot on
the screen becomes the color of the background. If the bit in memory is LJ
turned on, the corresponding dot on the screen becomes the color of the
foreground image. The series of 64,000 dots on the screen and 64,000 cor- LJ
u
92 CHAPTER 4-GRAPHICS, COLOR AND SPRITES LJ
LJ
n
n
responding bits in memory control the image you see on the screen. Most
of the finely detailed computer graphics you see in demonstrations and
n video games are bit mapped high resolution graphics.
n
Multi-color bit map mode is a combination of standard bit map mode and
multi-color character mode. You can display each screen dot in one of four
n
colors within an 8 x 8 character grid. Again, as in multi-color character
mode, there is a tradeoff between the horizontal resolution and color
h
control.
n
**************************************************************
n
This chapter has described a variety of color and graphics techniques based on
advanced programming concepts. The full explanation of these concepts is
n
beyond the scope of this Guide. If you want more details on graphics techniques
and graphics programming, refer to the Commodore 64 Programmer's Reference
n Guide.
n The next chapter completes your introduction to the Commodore 64C computer
by outlining the 64C}s varied sound and music capabilities.
n
n,
n
n
n
u
u
LJ
LJ
UJ
LJ
J
LJ
U
LJ
U
u
u
LJ
U
n
n
U
U
□
D
U
U
CJ
LJ
U
CJ
a
G
D
D
LJ
Li
n
n
CHAPTER 5 THE SID MICROPROCESSOR 99
n Sound and Music
MUSIC 99
n Playing From Sheet Music 99
Obtaining the Data 100
n Writing the Program 101
n
n
n
pi
n
u
u
u
LJ
U
U
LJ
LJ
LJ
LJ
LJ
LJ
LJ
LJ
LJ
LJ
LJ
n
n
The SID A special microprocessor known as the SID (Sound Interface Device) pro
n Microprocessor vides the 64C with extraordinary capabilities in generating musical tones
and sound effects. This chapter introduces you to these capabilities. For
r-1
{ ! more details, see Appendix J of this book and consult the Commodore 64
Programmer's Reference Guide.
n
H Music The Commodore 64C is capable of producing musical tones over a large
range—a full six octaves for up to three separate voices (musical instru
n ments) simultaneously. You can teach your 64C to play anything from
Happy Birthday to Beethoven's Fifth Symphony.
n
By controlling a series of internal registers in the SID, you can program
n your 64C to play a variety of complex musical sounds. These sounds or
notes have the qualities of a particular musical instrument and vary in
pitch and duration.
n
n MM I : I | I S I
^m
n I. ii I
ri r
: I :
i
n Middle
C
n
= QUARTER Note = DOTTED HALF Note
n
H
99 CHAPTER 5-SOUND AND MUSIC
n
LJ
U
To create these notes through the speakers of your monitor or TV, you
must turn ON several registers in the SID microprocessor. There are seven U
registers for each of three voices. Each must be filled with a particular
value. See Table 5-1 for the values of registers 2 through 6. Registers 0 and 1 U
are for sound frequency and are adjusted later in the program.
U
Table 5-1 ♦ Sound Register Values
Register number 2 3 4-ON 4-OFF 5 6 LJ
Musical
instruments: U
Piano 225 0 65 64 9 0
Flute 0 0 17 16 96 0 u
Harpsichord 0 0 33 32 9 0
Xylophone 0 0 17 16 0 240 u
Accordian 0 0 17 16 102 0 ! j
Trumpet 0 0 33 32 96 0
' '
}\ J J) .1
Hang down your head, Tom Doo - ley,
D7
J
Hang down your head and cry.
Am C 07
LJ
t=5l
LJ
Hang down your head, Tom Doo - ley,
C G LJ
m LJ
Poor boy, you're bound to die.
U
100
LJ
CHAPTER 5-SOUND AND MUSIC
U
H
n
1. Select the musical instrument and determine the register values
n from Table 5-1.
Piano: register 2 is 255, register 3 is 0; register 4 is 65 for ON and 64
n for OFF; register 5 is 9 and register 6 is 0.
2. Determine the name and value of each note; use Figures 5-1 and
n 5-2 as guides. Tabulate the results.
Notes read: D (eighth), D (quarter), D (eighth), E (quarter),
n G (quarter), B (half), B (half), etc.
3. Convert each note into the proper register settings called Nl and
n N2 from the Note Table in Appendix J and the duration (DR),
based upon the following note values:
n
Eighth note := 250
n Quarter note = 500
Half note = 1000
n Whole note == 2000
A note with ia dot = DR*'1.5
r-i
Tabulated Data
Note Value Nl N2 DR
n
D 1/8 18 104 250
n etc.
n
u
u
Selecting a register is done by the BASIC term POKE, followed by the
register number plus 54272, a comma and the proper value. |_J
20 POKES+ 24,15 LJ
3. Turn on registers 2, 3, 5 and 6, based upon the instrument you j j
are using (in this case, the piano):
30 POKES+ 2,255 LJ
40 POKES+ 3,0
50 POKES+ 5,9 1-1
60 POKES+ 6,0 .
90 POKES+ 4,65 LJ
6. Keep the sound on for the required time based on the value of t j
DR in your table. Since this value is a variable, it is represented by
its variable name, DR: i i
100FORZ=ltoDR:NEXTZ
120FORT=lto50:NEXTT LJ
9. Continue steps 4 through 8 with successive notes by using a j i
READ statement and a loop.
70READNl,N2,DR LJ
125 GOTO 70
LJ
r—>
Your sample program, when completed from sheet music, will look
like this:
H 500
140 DATA 30,245,1000,30,245,1000
150 DATA 18,104,250,18,104,500,18,104,250,20,169,500,24,146,
n
500
160 DATA 27,148,2000
H
170 DATA 18,104,250,18,104,500,18,104,250,20,169,500,24,146,
500
180 DATA 27,148,1000,27,148,1000
190 DATA 27,148,250,27,148,500,30,245,250,24,146,500,20,169,
500,24,146,1500
200 DATA 0,0,0
n
n
n
103 CHAPTER 5-SOUND AND MUSIC
n
U
u
Be sure to raise the volume on your monitor when you run your program.
To double the tempo, change line 100: u
100 FOR T = 1 TO DR/2: NEXT T
u
To play a different song, change DATA statements to the appropriate
values. u
Now that you have created your first song, experiment with other instru LJ
ments by varying the register values. You can also combine several voices to
represent chords or other instruments by adding 7 or 14 to each of the LJ
register numbers (except register 24). Thus, registers 7 through 13 can con
trol the second voice, and registers 14 through 20 the third voice. U
U
Sound Effects Besides music, you can also create special sound effects by using the noise
registers and varying the sound characteristics known as ADSR (Attack, U
Decay, Sustain and Release). These are combined in registers 5 and 6. A
thorough explanation is provided in the Commodore 64 Programmer's Refer- U
ence Guide. Below are register values for sample sound effects.
LJ
Sound Effects Register Values
Registers 0 1 2 3 4-ON 4-OFF 5 6 *
24 lJ
Variable
Names N2 Nl P2 PI Wl W2 AD SR DR V J
Sound effects:
Police Siren 85 36 0 0 33 32 136 129 350 15
LJ
Crash 251 5 0 0 129 128 129 65 50 0
Rocket Blast U
off 100 25 0 0 129 128 9 129 50 0
Machine Gun 75 34 0 0 129 128 8 1 50 15 LJ
Wailing N2 40 0 0 65 64 15 0 1 15
Shooting 200 40 0 0 129 128 15 15 1 0
U
The following program, called "Sound Effects", incorporates all these vari LJ
ables and can produce each of these sounds. The technique is identical to
creating music, except generally only one note is needed; hence there are U
no data statements. For details, see the Commodore 64 Programmer's Refer
ence Guide. LJ
LJ
LJ
LJ
LJ
n
n
10 CLR: REM ** SOUND EFFECTS **
n 15 PRINT"WHICH SOUND EFFECT?" :PRINT "1.
WAILING":PRINT "2. SHOOTING": PRINT "3. ";
/_ j 16 PRINT"SIREN":PRINT"4. ROCKET":PRINT"5. CRASH":
PRINT"6. MACHINE GUN"
H 17 INPUT X
20 S = 54272:FOR SW « S TO S + 24:POKESW,0:NEXT:K = - 1:
I. 1 Tl$ = "000000"
21 ON X GOTO 23,24,25,26,27,28
n 23V=15:N1 = W1 = 65:W2 = 64:AD=15:SR = O:DR=1:P1=9:
P2 = 255:Q= l:GOTO30:REM WAILING
n 24 N2 = 2OO:N1 = 4O:W1 = 129:W2 = 128:AD= 15:SR= 15:
DR= l;GOTO30:REM SHOOTING
r—»
25 N2 = 85:N1 = 36:Wl = 33:W2 = 32:AD = 136:SR = 129:
DR = 350:V = 15:Q=2:GOTO30:REM SIREN
n 26 N2 = 100:N 1 = 25-.W1 = 129:W2 = 128:AD = 9:SR = 129:
DR = 50:K = -.25:GOTO30:REM ROCKET
n 27N2 = 251 :N1 = 5:W1 = 129:W2=128:AD=129:SR = 65:
DR = 50:GOTO30:REM CRASH
n 28 N2 = 75:N1 = 34:Wl = 129:W2 = 128:AD = 8:SR = 1:DR = 50:
V= 15:REM MACHINE GUN
r i 30 POKE S + 2,P2:POKE S + 3,Pl:REM PULSE
40 POKE S + 5,AD:POKE S + 6,SR:REM ADSR
n 50 POKE S+ 1,N1:POKE S,N2:REM NOTE
55IFQ = 2THENQ=3
n 56 IF Q=2 THEN POKE S+ 1,64:POKE S.188
60 POKE S + 4,W1:REM ON SWITCH
n 63 IF Q<> 1 GOTO70
65 FOR N2 = 200TO5 STEP-1:POKE S,N2:NEXTN2
r—< 68 FOR N2= 150TO5 STEP-1:POKE S,N2:NEXTN2
70 FOR VL = 15 TO V STEP K:POKE S + 24,VL:REM VOLUME
H 80 FOR T= 1 TO DR:NEXT T:REM DURATION
90 NEXT VL
n 100 POKE S + 4,W2:REM SOUND OFF
110 IF TI$> = "000005"THEN 10
115 IFQ = 3THENQ=2:GOTO56
n
120 GOTO50
n
H
105 CHAPTER 5-SOUND AND MUSIC
n
u
u
Program Notes
The Sound Effects program contains six sound effects the user can pick <—'
from. Lines 10 through 21 clear all the variables and request a selection. ,
The variable K in line 20 is necessary for the rocket sound. TI$ sets the *-—'
built-in timer to zero. Lines 23 through 28 establish the values of the regis- ( .
ter variables for each sound. Lines 30 through 50 enter these values into {—*
the proper registers. The variable Q in lines 55, 56 and 115 restricts those .
lines to the siren. The variable Q in line 63 restricts lines 65 and 68 for wail- *—»
ing only. Line 70 allows for a variable volume; where none was required, . ,
V was set to 15. Line 80 allows for a variable note duration; when not re- '—'
quired, the variable DR was set to 1. Lines 60 and 100 are the main regis- . >
ters. Line 110 cuts off the sound after five seconds. You can then select '—*
another effect.
************************************************************** | j
LJ
Although by now you have experienced first hand the versatility and power of
the Commodore 64C compute^ you probably realize that you have only begun to t-*
tap the potential of this extraordinary computer. The next chapter defines the
format and use of all elements of the BASIC 2.0 programming language. Lj
LJ
n
:t«f^
■■'■\!&'"■■""' ' ■ .;..'■;• K '■' "' "■'■■ "■:"*■■ ' ' '"SV ■' ' ■ ""■ \-'V.' ■ ■ . ' ' ■■■■>. ■■•■]:■; ■■ * ; ■..'■; " ■ ■"■ ' ..'
^^^^■4jM^^:yX^^^MH
H
n
u
u
Q
U
u
u
u
a
(J
u
Li]
u
LJ
u
n
n
CHAPTER 6 INTRODUCTION 111
BASIC 2.0 Organization of Encyclopedia 111
Encyclopedia Definition Format 111
i i
n
H
109 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
y
LJ
LJ
LJ
LJ
LJ
LJ
LJ
LI
LJ
UJ
U
u
LJ
u
n
n
Introduction Organization of Encyclopedia
n This chapter lists BASIC 2.0 language elements. It gives a complete list of
the rules (syntax) of Commodore BASIC 2.0, along with a concise descrip
n tion of each.
n
Definition Format
Example(s)- EXAMPLE:
n
n
n
u
u
In a typical definition, the boldface line that defines the format consists of
the following elements: j 1
LOAD "filename" [,device number] [,relocate flag]
T t T jj
keyword argument additional arguments
(possibly optional) { .
ANGLE BRACKETS < > indicate the user MUST choose one of the jj
arguments listed.
LJ
A VERTICAL BAR | separates items in a list of arguments when the
choices are limited to those arguments listed. When the vertical bar \J
appears in a list enclosed in SQUARE BRACKETS, the choices are lim-
ited to the items in the list, but the user still has the option not to use any [ j
arguments. If a vertical bar appears within angle brackets, the user must
choose one of the listed arguments. \ j
n BASIC Commands
and Statements
This statement closes any files used by the OPEN statement. The number
or variable following the word CLOSE is the file number to be closed.
EXAMPLE:
CLR
This statement restores default I/O channels, clears (not closes) I/O chan
nels, resets DATA statement pointer, resets stack pointer, and resets varia
ble pointers, but leaves the program intact. This statement is automatically
executed when a RUN or NEW command is given.
n
u
u
CMD —Redirect screen output
CONT LJ
This command is used to restart a program that has been stopped by either
LJ
using the STOP key, a STOP statement, or an END statement. The pro
gram resumes execution where it left off. CONT will not resume the pro
LJ
gram execution if any editing of the program has been performed during
the pause. If the program stopped due to an error; or if you have caused an
u
error before trying to restart the program, CONT will not work. The error
message in this case is CANT CONTINUE ERROR.
u
u
DATA —Define data to be used by a program
LJ
u
D
n
EXAMPLE:
n DATA 100, 200, FRED, "HELLO, MOM",, 3, 14, ABC123
EXAMPLE:
Before arrays of variables can be used, the program must first execute a
DIM statement to establish DIMensions of the array (unless there are 11 or
fewer elements in the array). The DIM statement is followed by the name of
the array, which may be any legal variable name. Then, enclosed in paren
theses, put the number (or numeric variable) of elements in each dimen
sion. An array with more than one dimension is called a matrix. Any num
ber of dimensions may be used, but keep in mind the whole list of variables
n
being created takes up space in memory, and it is easy to run out of mem
ory if too many are used. Here's how to calculate the amount of memory
used by an array:
n
115 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
u
u
Integer arrays take up two-fifths the space of floating-point arrays (e.g.,
DIM A% (100) requires 209 bytes; DIM A (100) requires 512 bytes.)
LJ
Array elements are numbered 0 to N, where N is the maximum value speci
U
fied in the DIM statement. Thus, X(0) through X(10) indicates 11 elements.
U
More than one array can be dimensioned in a DIM statement by separat
ing the array variable names by commas. If the program executes a DIM
u
statement for any array more than once, the message "RE'DIMed ARRAY
ERROR" is posted. It is good programming practice to place DIM state
LJ
ments near the beginning of the program.
EXAMPLE: U
10 DIM A$(40),B7(15),CC%(4,4,4)
U
Dimensions three arrays, where arrays A$, B7, and CC% have,
respectively, 41 elements, 16 elements and 125 elements U
LJ
END —Define the end of program execution
LJ
END
LJ
n 10 FOR L = 1 TO 10
20 PRINT L
n 30 NEXT L
40 PRINT "FM DONE! L = "L
n This program prints the numbers from one to 10 followed by the message
I'M DONE! L= 11.
L j
The end value of the loop may be followed by the word STEP and another
number or variable. In this case, the value following the STEP is added
each time instead of one. This allows counting backwards, by fractions, or
n
in increments other than one.
The user can set up loops inside one another. These are known as nested
loops. Care must be taken when nesting loops so the last loop to start is the
first one to end. NEXT without a variable name completes the last exe
cuted FOR loop.
n
EXAMPLE:
10 FOR L = 1 TO 100
20 FOR A = 5 TO 11 STEP -5
30 NEXT A
40 NEXT L
n
The FOR ... NEXT loop in lines 20 and 30 is nested inside the one in
line 10 and 40. Using a STEP increment of .5 is used to illustrate the fact
that floating point indices are valid.
n
n GET —Receive input from the keyboard, one character at a time, without wait
ing for a key to be pressed
The GET statement is a way to receive data from the keyboard, one char
acter at a time. When GET is encountered in a program, the character that
is typed is stored in the 64C's memory. If no character is typed, a null
(empty) character is returned, and the program continues without waiting
n
n
U
LJ
for a key. There is no need to hit the RETURN key. The word GET is fol
lowed by a variable name, either numeric or string. U
If the program intends to GET a numeric key and a key besides a number U
is pressed, the program stops and an error message is displayed. The GET
statement may also be put into a loop, checking for an empty result. The Li
GET statement can be executed only within a program. Otherwise an
ILLEGAL DIRECT ERROR occurs. U
EXAMPLE:
LJ
10 GETA$:IF A$O"A"THEN 10 This line waits for the A
key to be pressed to U
continue.
be pressed.
u
\ •
LJ
n
The target of a GOSUB statement is called a subroutine. A subroutine
n is useful if a task is repeated several times within a program. Instead of
duplicating the section of program over and over, set up a subroutine,
n and GOSUB to it at the appropriate time in the program. See also the
RETURN statement.
n EXAMPLE:
EXAMPLES:
The IF... THEN statement evaluates a BASIC expression and takes one
of two possible courses of action depending upon the outcome of the ex
pression. If the expression is true, the clause following THEN is executed.
This can be any BASIC statement. If the expression is false, the program
resumes with the program line immediately following the program line
n
U
U
containing the IF statement. The entire IF... THEN statement must be
contained within 80 characters (two screen lines).
LJ
The IF... THEN statement can take two additional forms:
Li
INPUT —Receive a data string or a number from the keyboard and wait for the
user to press RETURN LJ'
INPUT ["prompt string";] variable list
u
The INPUT statement asks for data from the user while the program is
) )
RUNning and places the data into a variable or variables. The program \ )
stops, prints a question mark (?) on the screen, and waits for the user to
type the answer and hit the RETURN key. The word INPUT is followed u
by a prompt string and a variable name or list of variable names separated
by commas. The message in the prompt string inside quotes suggests u
(prompts) the information the user should enter. If this message is present,
there must be a semicolon (;) after the closing quote of the prompt. LJ
LJ
120 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA U
u
n
n
When more than one variable is INPUT, separate them by commas. The
n computer asks for the remaining values by printing two question marks (??).
If the RETURN key is pressed without INPUTting a value, the INPUT
n variable retains its previous value. The INPUT statement can be executed
only within a program.
I J EXAMPLE:
INPUT* —Inputs data from an I/O channel into a string or numeric variable
n INPUT* channel number, variable list
This statement works like INPUT, but takes the data from a previously
n
OPENed channel, usually on a disk or tape instead of the keyboard. No
prompt string is used. This statement can be used only within a program.
EXAMPLE:
n
10 OPEN 2,8,2
20 INPUT#2, A$, C, D$
This statement INPUTs the data stored in variables A$, C and D$ from
the disk channel number 2, which was OPENed in line 10.
The word LET is rarely used in programs, since it is not necessary. When
ever a variable is defined or given a value, LET is always implied. The vari
able name that receives the result of a calculation is on the left side of the
equal sign. The number, string or formula is on the right side. You can only
assign one value with each (implied) LET statement.
EXAMPLE:
n
10 LET A = 5 Assign the value 5 to numeric variable A.
n
U
U
LIST —List the BASIC program currently in memory
LIST 100- Shows from line 100 until the end of the
program.
LJ
LIST -100 Shows all lines from the beginning through line LJ
100.
LJ
LOAD —Load a program from a peripheral device such as a disk drive or
Datassette LJ
LOAD "filename" [,device number] [,relocate flag] U
This is the command used to recall a program stored on disk or cassette
tape. Here, the filename is a program name up to 16 characters long, in U
quotes. The name can be followed by a comma (outside the quotes) and a
device number to determine where the program is stored (disk or tape). If LJ
no number is supplied, the Commodore 64C assumes device number 1 (the
Datassette tape recorder). U
l_j
The relocate flag is a number (0 or 1) that determines where a program is
loaded in memory. A relocate flag of 0 tells the Commodore 64C to load
the program at the start of the BASIC program area. A flag of 1 tells the LJ
U
122 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA LJ
U
n
n
computer to LOAD from the point where it was SAVEd. The default
value of the relocate flag is 0. A value of 1 is generally used when loading
n machine language programs or bit-map screens.
H
The device most commonly used with the LOAD command is the disk
drive. This is device number 8.
n
If LOAD is typed with no arguments, followed by RETURN, the 64C
assumes you are loading from tape and you are prompted to "PRESS PLAY
ON TAPE". If you press PLAY, the 64C starts looking for a program on
n
tape. When the program is found, the 64C prints FOUNDufilename",
where the filename is the name of the first file which the Datassette finds
on the tape. Press the Commodore key or spacebar to LOAD the found
filename. (If you press no key, after about 10 seconds the file is loaded auto-
matically.) Once the program is LOADed, it can be RUN, LISTed or
modified.
EXAMPLES:
n
LJ
The OPEN statement allows the Commodore 64C to access files within LJ
devices such as a disk drive, a Datassette cassette recorder, a printer or even
the screen of the Commodore 64C. The word OPEN is followed by a logi U
cal file number, which is the number to which all other BASIC input/
output statements will refer, such as PRINT#(write), INPUT#(read), etc, LJ
This number is from 0 to 255, but for most uses it should be from 1 to 127.
The number zero and the numbers over 127 are reserved for special use. U
The second number, called the device number, follows the logical file U
number. Device number 0 is the Commodore 64C keyboard; 1 is the cas
sette recorder; 2 is RS-232; 3 is the Commodore 64C screen, 4-7 are usu U
ally for printers; and 8-11 are usually for disk drives. It is often a good idea
to use the same file number as the device number because it makes it easy u
to remember which is which. Valid device numbers are 0 to 30, of which
the values from 4 to 30 are assumed to be serial bus devices. LJ
LJ
U
124 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
u
n
n Following the device number may be a third parameter called the second
ary address. In the case of the cassette, this can be 0 for read, 1 for write
n and 2 for write with END-OF-TAPE marker at the end. In the case of the
disk, the number refers to the channel number. See your disk drive manual
for more information on channels and channel numbers. For the printer,
the secondary addresses are used to select certain programming functions.
There may also be a filename specified for disk or tape OR a string follow
i i ing the secondary address, which could be a command to the disk/tape
drive or the name of the file on tape or disk. If the filename is specified,
n the type and mode refer to disk files only. Disk file types currently include
PROGRAM, SEQUENTIAL, RELATIVE and USER; modes are READ
n and WRITE.
EXAMPLES:
n See also: CLOSE, CMD, GET#, INPUT#, and PRINT/ statements and
system variable ST.
n
u
previously. The value of the memory location determines the bit pattern of u
the memory location.
u
EXAMPLE:
Punctuation marks ;,
LJ
The characters inside quotes are often called literals because they are
printed literally, exactly as they appear. Variable names have the value they
contain (either a number or a string) printed. Functions also have their
number values printed.
Punctuation marks are used to help format the data neatly on the screen. \ i
i I
The comma separates printed output by 10 spaces, while for numeric out
put only the semicolon causes the numbers to be preceded by a space or
U
minus sign and followed by a cursor right. When used with text the semi
colon adds no spaces. Either punctuation mark can be used as the last
LJ
symbol in the statement. This results in the next PRINT statement acting
as if it is continuing the previous PRINT statement.
U
LJ
I i
I >
LJ
LJ
126 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
( i
n
n EXAMPLES: RESULTS
EXAMPLE:
n NOTE: After a CMD command has been used, the PRINT/ command is
used by itself to "unlisten" a device (e.g., close the channel to the printer)
n before closing the file, as shown in this example:
10 OPEN 4,4
20 CMD 4
30 PRINT#4,"PRINT WORDS"
40 PRINT#4
50 CLOSE 4
n
U
READ —Read data from DATA statements and input it into variable memory u
READ variable list LJ
This statement inputs information from DATA statements and stores it in
variables. The READ statement variable list may contain both strings and U
numbers. Be careful to avoid reading strings where the READ statement
expects a number and vice versa. This produces a TYPE MISMATCH LJ
ERROR message.
U
The data in the DATA statements are READ in sequential order. Each
READ statement can read one or more data items. Every variable in the < )
In a program, you can READ the data and then re-read it by issuing LJ
the RESTORE statement. The RESTORE sets the sequential data
pointer back to the beginning, where the data can be read again. See u
the RESTORE statement.
LJ
EXAMPLES:
LJ
REM —Comments or remarks about the operation of a program line
LJ
REM message
LJ
128 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
LJ
n
n
RESTORE —Reset DATA pointer so the DATA can be reREAD
n RESTORE [line #]
EXAMPLES:
RETURN
This statement is always paired with the GOSUB statement. When the
program encounters a RETURN statement, it goes to the statement im
mediately following the last GOSUB command executed. If no GOSUB
was previously issued, then a RETURN WITHOUT GOSUB ERROR
n message is displayed and the program stops. All subroutines end with a
RETURN statement.
n
U
EXAMPLE?
U
10 PRINT "ENTER SUBROUTINE" u
20 GOSUB 100
30 PRINT "BACK FROM SUBROUTINE" u
LJ
90 STOP u
100 PRINT "SUBROUTINE 1"
110 RETURN u
This example calls the subroutine at line 100 which prints the message
"SUBROUTINE 1" and RETURNs to line 30, the rest of the LJ
program.
LJ
u
n
H
comma and a second number or secondary address. If this number is 0, a
normal BASIC SAVE occurs. If the number is 1, the 64C saves the current
n
starting address in the tape header for use in subsequent LOAD opera
n tions. If the number is 2, the Commodore 64C puts an END-OF-TAPE
marker (EOT flag) after the program. If the number 3 is encountered, the
64C saves the current starting address in the tape header and an EOT
marker is set. If, in trying to LOAD a program, the Commodore 64C finds
one of these markers, the program is not loaded and a FILE NOT FOUND
ERROR is reported.
n EXAMPLES:
n A$.
SAVE "HELLO", 8 Stores on disk, with name HELLO (equivalent
to DSAVE "HELLO").
SAVE "HELLO", 1, 2 Stores on tape, with name HELLO, and places
H an END-OF TAPE marker after the program.
STOP
SYS —Call and execute a machine language subroutine at the specified address
SYS address
n This statement performs a call to a subroutine at a given address. The
address range is 0 to 65535. The program begins executing the machine-
ri language program starting at that memory location.
EXAMPLE:
LJ
n
n
always be 0. The mask-2 value flips any bits, so that an off condition can be
n tested for as well as an on condition. Any bits being tested for a 0 should
have a 1 in the corresponding position in mask-2. If corresponding bits of
n the <mask-l > and < mask-2 > operands differ, the exclusive-OR operation
gives a bit result of 1. If the corresponding bits get the same result the bit is
n 0. It is possible to enter an infinite pause with the WAIT statement, in
which case the RUN/STOP and RESTORE keys can be used to recover.
<—^
! i
The first example below WAITs until a key is pressed on the tape unit to
n continue with the program. The second example will WAIT until a sprite
collides with the screen background.
H
EXAMPLES:
WAIT 1, 32, 32
WAIT 53273, 6, 6
n BASIC Functions
H Function Format
FUNCTION (argument)
ABS(X)
The absolute value function returns the unsigned value of the argument X.
EXAMPLE:
35
n
n
133 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
u
u
ASC —Return CBM ASCII code for character
ASC(X$) u
This function returns the Commodore ASCII code of the first character of u
X$. You must append CHR$(0) to a null string, or else an ILLEGAL
QUANTITY ERROR is issued. LJ
EXAMPLE:
LJ
X$ = "CBM":PRINT ASC (X$)
67
U
U
ATN Compute arctangent, in radians, of X
U
—Return angle whose tangent is X radians
ATN(X) U
This function computes the arctangent, measured in radians, of X. The U
value returned is in the range — ir/2 through ir/2.
EXAMPLE: LJ
PRINT ATN (3)
U
1.24904577
u
U
COS —Return cosine for angle of X radians
LJ
COS(X)
i t
LJ
U
134 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
LJ
n
n
EXAMPLE:
n
PRINT COS (t/3)
r—\ .500000001
n
FNxx —Return value from user defined function
FNxx(x)
n This function returns the value from the user-defined function xx created
in a DEF FNxx statement.
EXAMPLE:
n
n FRE —Return number of available bytes in memory
FRE(X)
H
where X a dummy argument. The 64C returns the number of bytes as a
signed 16-bit value. To get the actual number of bytes, use:
n
PRINT FRE (0)<0* - 65536 + FRE(0)
n
EXAMPLE:
n PRINT FRE (0) Returns the current number of free bytes for
BASIC programs and variables.
n
n
135 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
H
u
u
INT —Return integer form (truncated) of a floating point value
u
INT(X)
This function returns the integer value of the expression. If the expression u
is positive, the fractional part is left out. Any fraction causes the next lower
integer to be returned. u
EXAMPLES:
LJ
PRINT INT(3.14)
3 LJ
PRINTINT(-3.14)
U
-4
U
LEFT$ —Return the leftmost characters of string
U
LEFT$ (string,integer)
U
LEN —Return the length of a string
U
LEN (string)
LJ
n This function returns the natural log of X, where X>0. The natural log
is log to the base e (see EXP(X)). To convert to log base 10, divide by
h LOG(IO).
EXAMPLES
n
PRINT LOG (37/5)
H 2.00148
n
MID$ —Return a substring from a larger string
n
MID$ (string,starting position!,length])
n This function returns a substring specified by the LENGTH, starting at
the character specified by the starting position. The starting position of
rn
i j
the substring defines the first character where the substring begins. The
length of the substring is specified by the length argument. The starting
n position value can range from 1 to 255; the length value can range from 0
to 255. If the starting position value is greater than the length of the string,
or if the length value is zero, then MID$ returns a null string value. If the
length argument is left out, all characters to the right of and including the
n starting position are returned.
EXAMPLE?
H
PRINT MID$("COMMODORE 64C",3,5)
n MMODO
H
PEEK —Return contents of a specified memory location
H PEEK(X)
EXAMPLE:
n
PRINT PEEK (650)
n 0
n
137 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
u
u
—Return the value of pi (3.14159265)
u
TT
EXAMPLE: u
PRINT -k This returns the result 3.14159265. u
LJ
POS —Return the current cursor column position on the screen
POS(X) LJ
The POS function indicates in which screen column the cursor is currently U
located. X is a dummy argument, which must be specified, but the value is
ignored. U
EXAMPLE:
u
10 PRINT"CURSOR IS IN COLUMN";
20 PRINTPOS(O)
LJ
When you run this program, the screen displays this:
U
CURSOR IS IN COLUMN 19
138
LJ
CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
n
RND —Return a random number
n
RND(X)
r-i
This function returns a random number between 0 and 1. This is useful in
games, to simulate dice roll and other elements of chance. It is also used in
n some statistical applications.
SGN(X)
n
This function returns the sign,(positive, negative or zero) of X. The result
is + 1 if X > 0, 0 if X = 0, and - 1 if X < 0.
EXAMPLE:
n
PRINT SGN(4.5);SGN(0);SGN(- 23)
n 1 0 -1
n
SIN —Return sine of argument
n
SIN(X)
n
139 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
u
u
SPC —Skip spaces on print output
u
SPC(X)
PRINT "COMMODORE";SPC(3);"64C" LJ
COMMODORE 64C
u
EXAMPLE:
LJ
PRINT SQR(25)
5
U
U
STR$ —Return string representation of number
U
STR$(X)
PRINT STR$(1E2O) LJ
IE+ 20
LJ
LJ
140 CHAPTER 6—BASIC 2.0 ENCYCLOPEDIA
n
n
TAB —Move cursor to tab position in present statement
n
TAB (X)
n. The TAB function is used in PRINT and PRINT/ commands to condition
ally skip to a specified column position. TAB operates differently with
n screens than with printers or disk files. For printers or disk output, TAB
acts exactly as SPC does (see the SPC description). For screen output, if
n column X is to the right of the current column position, then X becomes
the current column position. If X is at the same position as or left of the
n current column position, TAB has no effect. Characters passed over are
not erased.
n
EXAMPLE:
n 10 PRINT"COMMODORE"TAB(25)"64C"
COMMODORE 64C
n
n TAN(X)
n USR(X)
When this function is used, BASIC puts the value of X into the Floating
h Accumulator (FAC) in locations $0061 through $0066 (97 through 102)
and calls the USR vector. You must put your machine language routine's
n address at $0311(785) and $0312(786) (low/high bytes). Since USR is a
function, it returns a real value. Whatever is in the FAC when your
n machine language routine returns is passed. The USR vector defaults
to an ILLEGAL QUANTITY ERROR routine.
n
EXAMPLE:
H 10 POKE 785,0
20 POKE 786,192
H 30 A = USR(X)
40 PRINT A
n
n
141 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
u
u
Place starting location ($C000 = 49152:$00 = 0:$C0 = 192) of machine
language routine in location 785 and 786. Line 30 stores the returning u
value from the floating point accumulator.
u
U
142 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
LJ
p
p
STRING VARIABLES are those used for character data, which may con
p tain numbers, letters and any other characters the Commodore 64C can
display. An example of a string variable is "Commodore 64C."
1 I
EXAMPLES:
p Numeric Variable Names: A, A5, BZ
Integer Variable Names: A%, A5%, BZ%
p String Variable Names: A$, A5$, BZ$
p ARRAYS are lists of variables with the same name, using an extra number
(or numbers) to specify an element of the array. Arrays are defined using
p the DIM statement and may be floating point, integer or string variable
arrays. The array variable name is followed by a set of parentheses () enclos
p ing the number of the variable in the list.
EXAMPLE:
p
A(7),BZ%(11),A$(87)
p Arrays can have more than one dimension. A two-dimensional array may
be viewed as having rows and columns, with the first number identifying
the row and the second number identifying the column (as if specifying a
certain grid on a map).
EXAMPLE:
p A(7,2), BZ%(2,3,4), Z$(3,2)
TI and TI$ are variables that relate to the real time clock built into the
Commodore 64C. The system clock is updated every l/60th of a second. It
starts at 0 when the Commodore 64C is turned on, and is reset only by
P
changing the value of TI$. The variable TI gives the current value of the
I !
n
143 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
H
u
u
clock in l/60th of a second. TI$ is a string that reads the value of the real .
time clock as a 24-hour clock. The first two characters of TI$ contain the I—1
hour, the third and fourth characters are minutes and the fifth and sixth ( .
characters are seconds. This variable can be set to any value (so long as all '—'
characters are numbers) and will be updated automatically as a 24-hour .
clock. LJ
EXAMPLE: IJ
The value of the clock is lost when the Commodore 64C is turned off. It
starts at zero when the Commodore 64C is turned on, and is reset to zero j j
when the value of the clock exceeds 235959 (23 hours, 59 minutes and 59
seconds). l j
u
Operators
On a line containing more than one operator, there is a set order in which
operations always occur. If several operators are used together, the con> I—I
puter assigns priorities as follows: First, exponentiation, then multiplication
and division, and last, addition and subtraction. If two operators have the I—I
same priority, then calculations are performed in order from left to right. If
these operations are to occur in a different order, Commodore 64C BASIC I—i
allows giving a calculation a higher priority by placing parentheses around
it. Operations enclosed in parentheses will be calculated before any other 1—1
operation. Make sure the equations have the same number of left and right
parentheses, or a SYNTAX ERROR message is posted when the program I I
is run.
u
u
u
144 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
u
n
n
There are also operators for equalities and inequalities, called RELA
n TIONAL operators. Arithmetic operators always take priority over rela
tional operators.
= is equal to
< is less than
> is greater than
< = or = < is less than or equal to
) = or = ) is greater than or equal to
<> or >< is not equal to
C 1
Finally, there are three LOGICAL operators, with lower priority than
( I both arithmetic and relational operators:
AND
OR
NOT
These are most often used to join multiple formulas in IF ... THEN state
ments. When they are used with arithmetic operators, they are evaluated
last (i.e., after + and —). If the relationship stated in the expression is
n true, the result is assigned an integer value of — 1. If false, a value of 0 is
assigned.
n EXAMPLES:
n
n
n
145 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
u
u
Reserved Words and Reserved System Words (Keywords)
Symbols u
This section lists the words and symbols used to make up the BASIC 2.0
language. These words and symbols cannot be used within a program as u
other than a component of the BASIC language. The only exception is
that they may be used within quotes in a PRINT statement. M
ABS FN LIST READ PRINT
AND FOR LOAD REM PRINT/ 1 i
I )
ASC FRE LOG RESTORE STR$
ATN GET MID$ RETURN SYS
1 1
CHR$ GET# NEW RIGHT$ TAB
CLOSE GOSUB NEXT RND TAN
I i
CLR GO NOT RUN THEN
CMD GOTO ON SAVE TI t j
t i
CONT IF OPEN SGN TIME
COS INPUT OR SIN TIME$ 1 {
DATA INPUT# PEEK SPC TI$
DEF INT POKE SQR TO } )
L-J
DIM LEFT$ POS ST USR
END LEN PRINT STEP VAL
LET PRINT/ STOP VERIFY
U
EXP
WAIT
U
LJ
i i
< i
LJ
i i
LJ
LJ
LJ
LJ
n
n
Reserved System Symbols
H
The following characters are reserved system symbols.
Symbol Use(s)
f i
n
n
147 CHAPTER 6-BASIC 2.0 ENCYCLOPEDIA
n
u
u
u
u
u
u
u
u
u
u
u
u
u
u
u
u
u
n
n APPENDICES
n
APPENDIX A - BASIC 2.0 ERROR MESSAGES
H APPENDIX B - CONNECTORS/PORTS FOR PERIPHERAL
EQUIPMENT
n APPENDIX C - SCREEN DISPLAY CODES
APPENDIX D - ASCH AND CHR$ CODES
n APPENDIX E - SCREEN AND COLOR MEMORY MAPS
APPENDIX F - DERIVED TRIGONOMETRIC FUNCTIONS
r-i
APPENDIX G -MEMORY MAP
APPENDIX H - BASIC 2.0 ABBREVIATIONS
n APPENDIX I - SPRITE REGISTER MAP
APPENDIX J - SOUND AND MUSIC
n
n
n
n
r—t
/ s
149 APPENDICES
n
u
u
u
u
LJ
U
u
u
u
u
LJ
LJ
LJ
LJ
U
LJ
u
n
n
APPENDIX A ERROR MESSAGES
n
BASIC 2.0 ERROR
n MESSAGES
MESSAGE What the Problem Is What to Do
BAD DATA String data was received from an Make sure data was saved with a
n open file, but the program was separator between each item.
expecting numeric data.
<—-i
I \ BAD The program was trying to Verify you have dimensioned the
SUBSCRIPT reference an element of an array array properly. In direct mode,
n whose number is outside the range have the 64C print the value of the
specified in the DIM statement. subscript as a clue.
CANT The CONT command will not You probably made a correction;
CONTINUE work, either because the program reRUN the program.
was never RUN, there has been an
error, or a line has been edited.
DEVICE NOT The required I/O device not Verify the peripheral you are
PRESENT available for an OPEN, CLOSE, calling for is on and proper OPEN
CMD, PRINT #, INPUT #, or statement is used.
GET#.
FILE NOT No file with that name exists. Verify you have the correct tape
FOUND or disk and you spelled the name
correctly; note especially spacing
and upper-case characters.
FILE NOT OPEN The file specified in a CLOSE, Open file. Verify you used proper
CMD, PRINT #, INPUT #, or file number.
GET #, must first be OPENed.
FILE OPEN An attempt was made to open a Close file first or use new file
file using the number of an number.
n already open file.
FORMULA TOO The string expression being Use smaller strings. Reduce the
COMPLEX evaluated should be split into at number of parentheses.
least two parts for the system to
n work with, or a formula has too
many parentheses.
ILLEGAL Occurs when you try to access a Use correct device number.
DEVICE device illegally (e.g., LOADING
NUMBER from keyboard, screen or RS-
232C).
LJ
MESSAGE What the Problem Is What to Do
large enough. LJ
LOAD There is a problem with the Reload.
program on disk. LJ
MISSING FILE LOADs and SAVEs from the Key in the file name. i i
NEXT This is caused by either Verify the loop has a starting and u
WITHOUT FOR incorrectly nesting loops or ending point. Do not jump into
the middle of a loop.
having a variable name in a
LJ
NEXT statement that doesn't
correspond with one in a FOR
statement. U
NOT INPUT An attempt was made to INPUT Correct the OPEN statement's t |
FILE or GET data from a file which secondary address.
specified to be for output only.
NOT OUTPUT An attempt was made to PRINT Correct the OPEN statement's u
FILE data to a file which was specified secondary address.
as input only. LJ
OUT OF DATA A READ statement was executed Verify data was not missed; add
but there is no data left unREAD more data if necessary. u
in a DATA statement.
u
152 APPENDIX A-BASIC 2.0 ERROR MESSAGES u
u
n
n
MESSAGE What the Problem Is What to Do
n REDO FROM Character data was typed in Provide the proper INPUT
START during an INPUT statement when response.
n STRING TOO A string can contain up to 255 Keep strings to 255 characters and
LONG characters. any single INPUT to 80
n characters.
TYPE This error occurs when a number Verify $ signs were typed where
MISMATCH is used in place of a string, or vice- they belong.
n versa.
UNDEF'D A user defined function was Define the function with DEF
n FUNCTION referenced, but it has never been within the program.
defined using the DEFFN
n statement.
UNDEF'D An attempt was made to GOTO Make sure line numbers exist.
VERIFY The program on tape or disk does Save the program again, under
I \
not match the program currently another name.
in memory.
/ s
L)
LJ
u
u
LJ
LJ
LJ
LJ
LJ
U
U
n
n
APPENDIX B COMMODORE CONNECTIONS FOR PERIPHERALS
n
CONNECTORS/
PORTS FOR
n
PERIPHERAL
EQUIPMENT
n
CO (2) CO
H
n
n
r—)
{_ \
f >
/ i
n
LJ
Side Panel 1. Power Socket—The free end of the cable from the power supply is U
Connections attached here.
U
2. Power Switch—Turns on power from the transformer.
LJ
3. Control Ports—There are two Control ports, numbered 1 and 2. Each
Control port can accept a joystick or game controller paddle. A light LJ
pen or mouse can be plugged only into port 1, the port closest to the
front of the computer. Use the ports as instructed with the software. U
U
Control Port 1
Pin Type Note u
1 JOYAO
2 JOYA1
LJ
3 JOYA2
4 JOYA3
5 POT AY U
6 BUTTON A/IP
(front view of port)
7 + 5V MAX. 50mA LJ
8 GND
9 POT AX
LJ
Control Port 2 U
Pin Type Note
1 JOYBO LJ
2 JOYB1
3 JOYB2
4 JOYB3 U
5 POT BY
6 BUTTON B u
7 + 5V AAAX. 50mA
8
9
GND
POT BX
LJ
LJ
LJ
LJ
LJ
LJ
U
156 APPENDIX B-CONNECTORS/PORTS FOR PERIPHERAL EQUIPMENT
u
LJ
n
Rear Connections 4. Expansion Port—This rectangular slot is a parallel port that accepts
n program or game cartridges as well as special interfaces.
n
Cartridge Expansion Slot
r-i
Pin Type Pin Typt
12 BA 1 GND
n 13 DMA 2 +5V
14 07 3 +5V
H 15 D6 4 IRQ
16 D5 5 R/W
n 17 D4 6 Dot Clock
18 D3 7 I/O 1
19 D2 8 GAME
20 D1 9 EXROM
21 00 10 I/O 2
22 GND 11 ROML
n Y A0 I All
z GND M A10
ZV XWVUTSRPNMLKJHFEDCIA
/ I
LJ
U
LJ
(view of port while facing the rear of the 64C)
U
U
Pin Type Note
u
Serial I/O
Pin LJ
SERIAL SRQIN
GND u
SERIAL ATN IN/OUT
SERIAL CLK IN/OUT
SERIAL DATA IN/OUT u
RESET
(view of port u
while facing the
rear of the 64C) LJ
LJ
LJ
U
n
9. Cassette Port—A 1530 Datassette recorder can be attached here to
n store programs and information.
n
Cassette
I _ \
Pin Type
A-l GND 12 3 4 5 6
n B-2 +5V
C-3 CASSEHE MOTOR
1 GND
n 2
3
+5V
RESET
MAX. 100 mA
4 CNT1
5 SP1
6 CNT2
7 SP2
8 PC2
9 SER. ATN IN
G 10 9 VAC AAAX. 100 mA
11 9 VAC MAX. 100 mA
12 GND
A GND
B FLAG2
C PBO
D PB1
n E PB2
F P*3
H PB4
J P&5
K PB6
L PB7
M PA2
N GND
n
12 3 U 6 7 8 9 10 1112
ABCDEFHJKLMN
n
159 APPENDIX B-CONNECTORS/PORTS FOR PERIPHERAL EQUIPMENT
n
u
u
u
u
u
u
u
u
u
u
u
u
u
u
u
LJ
u
u
u
n
p
APPENDIX C The following chart lists all of the characters built into the Commodore
screen character sets. It shows which numbers should be POKEd into the
n
SCREEN DISPLAY VIC chip (40 column) screen memory (location 1024 to 2023) to get a
CODES desired character on the 40-column screen. (Remember, to set color mem
ory, use locations 55296 to 56295.) Also shown is which character corres
ponds to a number PEEKed from the screen.
Two character sets are available, but only one is available at a time. The sets
n are switched by holding down the SHIFT and O (Commodore) keys
simultaneously. The entire screen of characters changes to the selected
character set.
n
From BASIC, PRINT CHR$(142) will switch to upper-case/graphics mode
and PRINT CHR$(14) will switch to upper/lower-case mode.
@ 0 Q q 17
it
34
n A a 1 R r 18 # 35
B b 2 S 19
n 8 $ 36
C c 3 T t 20 % 37
D d 4 U U 21 & 38
E e 5 V V 22 1
39
w w 23
F f 6 ( 40
n X X 24
G g 7 ) 41
H h B Y y 25 42
I i 9 z 2 26 43
27 44
J i 10 t
K k 11 £ 28 - 45
L I 12 ] 29 46
n 30 47
M m 13 T 1
N n 14 31 0 48
■A
0 0 15
■ 32 1 49
P P 16 ! 33 2 50
n
u
u
8ET1 SET2 POKE SET1 SET 2 POKE SET 1 SET 2 POKE
3 51 s M 77 a 103 u
4 52 N 78 Q 104
u
5 53 □ 0 79 B 0 105
n u
6 54 P 80
□ 106
7 55 m Q 81 107 u
8 56 □ R 82
a 108
H u
9 57 S 83
a 109
:
58 D T 84
H 110 u
; 59 a U 85
□ 111
V 86 u
< 60
CB 112
- 61 O w 87
B 113 LJ
> 62 IS X 88
B 114
CD u
? 63 Y 89
ffl 115
B 64 SI z 90 D 116 LJ
A 65 ffl 91 c 117
E U
m B 66
92 LI 118
B C 67 CD 93 n 119 U
B D 68 9 94 n 120
a ES U
a E 69 95 y 121
□ F 70
3 96 □ 0 122 U
D G 71
E 97
ED 123
u
a H 72 H 98
a 124
1 73 n 99
EJ 125 u
J 74
□ too E 128
u
K 75 □ 101 B 127
L 76 B 102 u
Codes from 128-255 are reversed Images of codes 0-127.
u
u
u
LJ
LJ
u
H
n
APPENDIX D This appendix shows you what characters will appear if you PRINT
CHR$(X), for all possible values of X. It also shows the values obtained by
ASCII AND CHR$ typing PRINT ASC ("X"), where X is any character that can be displayed.
CODES This is useful in evaluating the character received in a GET statement,
converting upper to lower case and printing character-based commands
(like switch to upper/lower case) that could not be enclosed in quotes.
0
n 26 4 52 N 78
1 27 5 53 0 79
2
1
28 6 54 P 80
3 29 7 55 Q 81
4 30 8 56 R 82
H 31 9 57 83
S
n
6
m 32 I 58 T 84
7
• 33 ; 59 U 85
O.SABLEsHiae 34 <Z 60 V 86
ENABLES HBQ9 # 35 61
w 87
10 $ 36 z> 62 X 88
n 11 % 37 ? 63 Y 89
12 & 38 @ 64
z 90
^^^^fl 13 A
• 39 65
91
BnffllHnHM 1^ (
40 B 66
15 c 92
)
41 C 67
H
16 ) 93
•
42 D 68
gg 17 43 E T 94
+
69
KRk 18 44 F 70
•- 95
I i
■ 19 - 45 Q 71 B 96
B 20 .
46 H 72 ® 97
21 47 I 73 CD 98
22 0 48 J 74 B 99
23 1 49 K 75 B 100
24 2 50 L 76 □ 101
B
/ \
25 3 51 M 77 102
n
u
u
MINTS CM* MINTS CHfl$ PAINTS CNRS MINTS CNW
u
D 103 E 124 ■ 145 ■ 166
f6
138
139
X 159 D 180
u
118 IDD 160 c 181
O 119
(8 140
B 161 J 182 u
IS ma D141
120 H 162 n 183
a iflrti |142 u
121 n 163
u
LJ
LJ
u
n
n
MINTS CMfH MINTS CNNI MINTS CHW MINTS vffffW
n
0 164 □ 186 a 186 B 190
u
LJ
u
u
u
u
y
Q
U
U
u
u
u
u
n
n
APPENDIX E The following maps display the memory locations used in specifying the
n placement and color of characters on the screen. Each map is separately
SCREEN AND controlled and consists of 1,000 positions (25 lines of 40 characters each).
COLOR MEMORY
MAPS
Screen Memory The characters displayed on the maps can be controlled directly with the
Map POKE command. (Remember to POKE the colors to the color map as
well.)
n
H VIC SCREEN MEMORY MAP
COLUMN
0 10 20 30 39
1063
1024-
1064
1104
1144
1184
1224
n 1264
1304
1344
1384
i \ 1424
1464
1504
1544
1584
1624
1664
H 1704
1744
1784
1824
1864
1904
1944
1984
t
2023
The Screen Map is POKEd with a Screen Display Code value (see
Appendix C). For example:
J t
POKE 1024,13
n
n
u
55335
55296-
55336
55376 u
55416
55456
55496 LJ
55536
55576
55616
55656
U
55696
55736
55776 y
55816
55856
55896
55936
u
55976
56016
56056 u
56096
56136
56176 i !
< 1
56216
56256 24
56295 y
The color RAM appears in this range in I/O space. If the color map is
u
POKEd with a color value; this changes the character color. For example:
u
POKE 55296,1
will change the letter M inserted above from light green to white.
u
Note: Only the lower nybble (4 bits) is used. If you PEEK color RAM, do u
this to determine color at location X:
u
= PEEK(X)andl5
Color Codes
u
u
0 Black 8 Orange
White 9 Brown
Red
u
10 Light Red
Cyan 11 Dark Gray
Purple
u
12 Medium Gray
5 Green 13 Light Green
6 Blue 14 Light Blue
u
7 Yellow 15 Light Gray
LJ
Border Control Memory 53280
Background Control Memory 53281 LJ
n
APPENDIX F FUNCTION BASIC EQUIVALENT
n
DERIVED SECANT SEC(X)=1/COS(X)
J ;
I \
H
169 APPENDIX F-DERIVED TRIGONOMETRIC FUNCTIONS
u
u
u
u
u
LJ
u
u
Lf
U
U
u
LJ
u
u
u
n
n
APPENDIX G
n
MEMORY MAP
n
n
COMMODORE 64C COMMODORE 64C
MEMORY MAP MEMORY MAP
n
Cartridges 64C Cartridges 64C
n FFFF , , FFFF 4000
KERNAL
GAME CARD and
EDITOR
I \
H
E000 E000
C00O C000
0800
1 I
VIC (40 Column) TEXT
SCREEN
1 \ 0400
n 0300
H
0200
n SYSTEM STACK
0100
0000 ■
n
171 APPENDIX G-MEMORY MAP
u
u-
u
u
LJ
u
LJ
u
u
n
ABS B LEFTS LE
ATN T LIST
n
CHR$ H LOAD L
n
CLOSE O LOG NONE LOG
n CLR C L
'□ AAIDS M
H
COS NONE cos NOT
n DATA A ON NONE ON
DEF D E OPEN
DIM D I OR NONE OR
END E N
0 PEEK P
pB
EXP E X E | POKE P ggjim o
FOR F O
n PRINT
n
FRE F R PRINT# P R
GET G E READ R E
GOSUB GO S RESTORE RE S RE f%
GOTO G O G □ RETURN RE T
IF NONE IF RIGHTS I
INPUT#
• 0 RUN U
LJ
LJ
LJ
LJ
LJ
LJ
174 APPENDIX H-BASIC 2.0 ABBREVIATIONS
U
U
n
n
APPENDIX I
<—>
8 8 S4X7 S4X0 SPRITE 4 X
S5Y0
n 11 B S5Y7 SPRITE 5 Y
Y SCROLL
YSCL2 YSCL1 YSCLO
n
17 11 RC8 ECM BMM BLNK RSEL
MODE
n
Register #
Dec Hex DB7 DB6 DB5 DB4 DB3 DB2 DB1 DBO
Register # Register
Dec Hex
Color Dec Hex
Color
BACKGROUND
33 21 40 28 SPRITE 1 COLOR
COLOR 0
BACKGROUND
34 22 41 29 SPRITE 2 COLOR
COLOR 1
BACKGROUND
35 23 42 2A SPRITE 3 COLOR
COLOR 2
BACKGROUND
36 24 43 2B SPRITE 4 COLOR
COLOR 3
SPRITE
37 25 44 2C SPRITE 5 COLOR
MULTICOLOR 0
SPRITE
38 26 45 2D SPRITE 6 COLOR
MULTICOLOR 1
46 2E SPRITE 7 COLOR
( t
n
h
APPENDIX] Note values are POKEd into two memory locations 54272 and 54273,
n also known as registers or switches 0 and 1 respectively.
SOUND AND POKE the value Nl (the HIGH value) into Register 1 (location 54273)
n MUSIC and the value N2 (the LOW value) into Register 0 (location 54272).
The list below covers three octaves of notes for the Bass and Treble Clefs.
For the full list of note values, see the Commodore 64 Programmer's Reference
n Guide.
n
Music Note Table
n
n F G A B CD]
n (I fTTTTTTTTTTTTT
i i
r^
i \ NOTE Nl N2
G 6 36
H G# 6 130
A 6 228
m
I .! A# 7 77
B 7 189
n C 8 50
C# 8 175
n D 9 51
D# 9 191
n E 10 84
F 10 241
n F# 11 152
G 12 73
G# 13 4
A 13 201
n A# 14 156
B 15 122
*C 16 101
C# 17 96
D 18 104
D# 19 128
E 20 169
* MIDDLE C
H 177 APPENDIX J-SOUND AND MUSIC
n
u
F 21 227
u
F# 23 49
u
G 24 146
G# 26 8
A 27 148
LJ
A# 29 57
245
u
B 30
C 32 204
192
u
C# 34
D 36 208
39 1
u
D#
E 41 83
200
u
F 43
F# 46 99
u
u
Sound Control Each sound parameter is POKEd into a register of the specialized sound
Settings generating chip. Each register is a memory location (called byte address) u
starting with 54272.
Each sound has a characteristic ADSR consisting of the following four u
parameters: Attack, Decay, Sustain, Release.
Attack is the rate sound rises to maximum volume. It can vary from a 2- LJ
millisecond cycle to an 8'Second cycle. The corresponding register value is
0tol5. U
Decay is the rate sound falls from maximum volume to sustain level.
This varies from a 6-millisecond cycle to 24 seconds, corresponding with 0 u
to 15.
The values of Attack and Decay are POKEd together into register 5 by a u
single number derived by multiplying the ATTACK value by 16 and add-
ing the DECAY value. LJ
Sustain is the amplitude level at which the sound is held, varying from
0% to 100% of maximum level corresponding to register values of 0 to 15. U
Release is the rate at which volume falls from the sustain level to zero;
similar in timing to the decay rate.
U
Sustain and Release are POKEd into register 6 together as one number
derived by multiplying SUSTAIN by 16 and adding the RELEASE value. LJ
Waveform is the shape of the sound wave produced. The waveforms
called Triangle, Sawtooth and Pulse are related to the sound of musical
u
instruments. Noise is a randomized waveform. Only specific register values
will activate this characteristic of sound.
LJ
Pulse is the tonal quality of the Pulse waveform. Thus, whenever register
4 is activated with a 65, a value other than zero must be POKEd into either
U
register 2 or 3 for the Pulse Rate.
n
Frequency is the vibratory level of sound which distinguishes one note
H from another. Concert A is 440 cycles per second. Registers 0 and 1 are
required to define the frequency. 256 times the value in Register 1 plus the
H value of Register 0 is the sound generator's oscillator frequency. This is
directly proportional to the sound frequency.
n Following is a table of values which can be POKEd into these registers.
The actual memory location is 54272 plus the register number.
n
Register Description Range of Values
n Voice 1 Voice 2 Voice 3
0 7 14 frequency 0 to 255
n 1 8 15 frequency 0 to 255
2 9 16 pulse 0 to 255
n 3 10 17 pulse 0tol5
4 11 18 Waveform 16,32,64,128
n 17,33,65,129
5 12 19 Attack/Decay 0 to 255
n 6 13 20 Sustain/Release 0 to 255
All voices
n 21 Filter-low cutoff 0to7
22 Filter-high cutoff 0 to 255
n 23 Resonance 16,32,64,128
or any sum
n 23 Filter switch/
voice 1,2 or 4
n 24 Volume 0tol5
n
H 179 APPENDIX J-SOUND AND MUSIC
n
u
LJ
u
u
LJ
U.
LJ
LJ
H
n GLOSSARY
n
n
GLOSSARY This glossary provides brief definitions of frequently used computing
n terms.
n
181 GLOSSARY
n
u
u
Assembly Language: A machine-oriented language in which mnemonics
are used to represent each machine-language instruction. Each CPU LJ
has its own specific assembly language. See CPU and machine
language. LJ
Attack: The rate at which the volume of a musical note rises from zero to
peak volume. I—I
Background Color: The color of the portion of the screen that the char- LJ
acters are placed upon.
u
BASIC: Acronym for Beginner's All-purpose Symbolic Instruction Code.
LJ
Baud: Serial-data transmission speed. Originally a telegraph term, 300
baud is approximately equal to a transmission speed of 30 bytes or I I
characters per second.
Bit: The abbreviation for Binary diglT. A bit is the smallest unit in a I s
computer. Each binary digit can have one of two values, zero or one.
A bit is referred to as enabled or "on" if it equals one. A bit is dis- 1 I
abled or "off* if it equals zero.
u
Bit Control: A means of transmitting serial data in which each bit has a
significant meaning and a single character is surrounded with start Lj
and stop bits.
u
Bit Map Mode: An advanced graphic mode in the Commodore 128 in
which you can control every dot on the screen. 1 I
LJ
IJ
182 GLOSSARY
u
n
n
Bus: Parallel or serial lines used to transfer signals between devices. Com
H puters are often described by their bus structure.
183 GLOSSARY
u
u
Color Memory: The area in the Commodore 64C's memory that controls
the color of each location in screen memory. I—I
CPU: Acronym for Central Processing Unit. The part of the computer i I
containing the circuits that control and perform the execution of
computer instructions. 1 j
LJ
Cursor: The flashing square that marks the current location on the
screen. [ j
Data: Numbers, letters or symbols that are input into the computer to be I I
processed.
u
Data Base: A large amount of data stored in a well-organized manner. A
data-base management system is a program that allows access to the ' \
information.
i i
Data Rate or Data Transfer Rate: The speed at which data is sent to a
receiving computer—given in baud, or bits per second (bps). l j
LJ
184 GLOSSARY ^
LJ
p
p
Datassette: A Commodore device used to store programs and data files
p
sequentially on tape.
p
Debug: To correct errors in a program.
p
Decay: The rate at which the volume of a musical note decreases from its
peak value to a mid-range volume called the sustain level. See
Sustain.
p
Delay Loop: An empty FOR ... NEXT loop that slows the execution of a
program.
Disk Drive: A random access, mass-storage device that saves and loads
files to and from a floppy diskette.
p
Disk Operating System: Program used to transfer information to and
p from a disk. Often referred to as a DOS.
185 GLOSSARY
H
u
u
Enable: To turn on a bit, byte or specific operation of the computer. < ,
File: A program or collection of data treated as a unit and stored on disk *—'
or tape. , ,
Lj
Firmware: Computer instructions stored in ROM, as in a game cartridge. t }
I i
Frequency: The number of sound waves per second of a tone. The fre- ^ l
quency corresponds to the pitch of the audible tone. i—i
Full-Duplex Mode: Allows two computers to transmit and receive data at '—'
the same time. s ;
Function Keys: The four keys on the far right of the Commodore 64C ^ }
keyboard. Each key can be programmed to execute a series of instruc- '—•
tions. Since the keys can be SHIFTed, you can create eight different t
sets of instructions. '—'
GCR Format: The abbreviation for Group Code Recording, a method of '—'
storing information on a disk in CP/M mode. v }
i j
186 GLOSSARY
LJ
n
n
n Grid: A two-dimensional matrix divided into rows and columns. Grids are
used to design sprites and programmable characters.
n
IC: Integrated Circuit. A silicon chip containing an electric circuit made
up of components such as transistors, diodes, resistors and capacitors.
Integrated circuits are smaller, faster and more efficient than the indi
vidual circuits used in older computers.
Input: Data fed into the computer to be processed. Input sources include
the keyboard, disk drive, Datassette or modem.
I/O: Input/output. Refers to the process of entering data into the computer,
n or transferring data from the computer to a disk drive, printer or
storage medium.
H
187 GLOSSARY
n
u
u
Loop: A program segment executed repetitively a specified number of i i
times.
Memory: Storage locations inside the computer. ROM and RAM are two
different types of memory. j i
Motherboard: In a bus-oriented system, the board that contains the bus —'
lines and edge connectors to accommodate the other boards in the i ,
system. '—'
Multi*Color Character Mode: A graphic mode that allows you to display j /
four different colors within an 8 X 8 character grid. '—'
Multi'Color Bit Map Mode: A graphic mode that allows you to display L^
one of four colors for each pixel within an 8 X 8 character grid. See v ;
Pixel. '—>
u
188 GLOSSARY
u
n
n
n Multiple*Access Network: A flexible system by which every station can
have access to the network at all times; provisions are made for times
when two computers decide to transmit at the same time.
n Null String: An,empty character ("")• A character that is not yet assigned
a character string code.
n
Octave: One full series of eight notes on the musical scale.
n
Operating System: A built-in program that controls everything your
computer does.
n
n Operator: A symbol that tells the computer to perform a mathematical,
logical or relational operation on the specified variables, constants or
array elements in the expression. The mathematical operators are -I-,
n
-, *, / and t . The relational operators are <, =, >, < = , > = and
< >. The logical operators are AND, OR NOT, and XOR.
n Parallel Port: A port used for transmission of data one byte at a time over
multiple wires.
n
Parity Bit: A 1 or 0 added to a group of bits that identifies the sum of the
n bits as odd or even.
Pixel: Computer term for picture element. Each dot on the screen that
makes up an image is called a pixel. Each character on the screen is
displayed within an 8 X 8 grid of pixels. The entire screen is com
posed of a 320 X 200 pixel grid. In bit-map mode, each pixel corres
n ponds to one bit in the computer's memory.
/ i
n
189 GLOSSARY
u
u
Polling: A communications control method used by some computer/ j i
terminal systems whereby a "master" station asks many devices
attached to a common transmission medium, in turn, whether they < !(i
have information to send. ^
Port: A channel through which data is transferred to and from the CPU. *—'
Printer: Peripheral device that outputs the contents of the computer's '—'
memory onto a sheet of paper. This paper is referred to as a hard , ,
copy. u
LJ
Random Access Memory (RAM): The programmable area of the com- ^—J
puter's memory that can be read from and written to (changed). All s
RAM locations are equally accessible at any time in any order. The *—>
contents of RAM are erased when the computer is turned off. . f
LJ
LI
190 GLOSSARY
u
n
n
Release: The rate at which the volume of a musical note decreases from
H
the sustain level to zero.
n
RGBI Monitor: Red/Green/Blue/Intensity. A high-resolution display
device necessary to produce an 80-column screen format.
n
Ribbon Cable: A group of attached parallel wires.
Screen Memory: The area of the Commodore 64C's memory that con
tains the information displayed on the video screen.
Serial Port: A port used for serial transmission of data; bits are transmit
ted one bit after the other over a single wire.
n
Serial Transmission: The sending of sequentially ordered data bits.
n
Software: Computer programs (sets of instructions) stored on disk, tape or
} I cartridge that can be loaded into random access memory. Software,
in essence, tells the computer what to do.
H
Sound Interface Device (SID): The sound synthesizer chip responsible
for all the audio features of the Commodore 64C. See the Commo
dore 64 Programmer's Reference Guide for chip specifications.
H
191 GLOSSARY
u
u
Source Code: A non-executable program written in a high-level language. < .
A compiler or assembler must translate the source code into an
object code (machine language) that the computer can understand. j ^
Start Bit: A bit or group of bits that identifies the beginning of a data
word. LJ
Stop Bit: A bit or group of bits that identifies the end of a data word and '—*
defines the space between data words. t ,
LJ
192 GLOSSARY
LJ
n
Video Interface Controller (VIC): The chip responsible for the 40-
n column graphics features of the Commodore 64C. See the Commo
dore 64 Programmer's Reference Guide for chip specifications.
n
n
n
/ i
n
n
193 GLOSSARY
n
u
u
u
LJ
LJ
u
LJ
LJ
U
LJ
LJ
U
U
H
n
INDEX Comma, 21
n Abbreviations-BASIC, 173-174 Commodore key, 19
ABSolute function, 61, 133 Connections, 135-159
n Accessories, 5 constants, 30
CHR$ codes, 67
n keys, 23 G
memory map, 72 Game controls and ports, 156
n screen and border registers, 68 GET statement, 48-49, 117
screen codes, 69 GET# statement, 118
n
195 INDEX
n
J
u
u
GOSUB statement, 56-57, 118 Music programs, 101-104
GOTO statement, 25-26, 119 Musical notes, 101, 177
u
Graphic keys, 20 Musical scale, 99, 177
Graphic modes, 91-93
u
N
NEW command, 26, 123 u
High resolution mode, 91-93 NEXT statement, 44-46, 116
HOME key, 19 Noise, 178 LJ
Hyperbolic functions, 169 Null string, 49, 191
Numeric variables, 31 LJ
I
U
IF... THEN statement, 43-44, 119 o
INPUT statement, 47-49, 120 ON statement, 56, 124
INPUT#, 121 OPEN statement, 35, 124-125
U
INSerT key, 16-18 Operators, 144
INTeger function, 59, 136 arithmetic, 28, 144 LJ
Integer variable, 142 logical, 145
order of, 29 U
relational, 145
J U
Joystick ports, 156
Joysticks, 156 P
Paddle, 156
LJ
Parentheses, 30, 147
K PEEK function, 57, 137
LJ
Keyboard, 14-20 Peripherals, 155-159
Pi, 138 U
Pixel, 77
L POKE statement, 57, 58-125 LJ
LEFT$ function, 136 Ports, 155-159
LENgth function, 136 POS function, 138 U
LET statement, 121 PRINT statement, 21-23, 126
LIST command, 25, 122 Printers, 6
LOAD command, 36
U
PRINT#, 127
LOADing cassette software, 36 Program, 24
LOADing disk software, 36 line numbering, 24
U
LOGarithm function, 137 mode, 13
Loops, 44-46,116 music, 101 LJ
viewing, 25
M
Programmable keys, 19 U
Programmer's Reference Guide, 6
Machine language, 131, 141
Memory, 57-58, 70-72
Pulse, 178 u
Memory maps, 71, 72, 167, 168, 171
MID$ function, 137 Q LJ
Modem, 5, 6 Question mark, 21
Multicolors, 23, 67-70, 91-93 Quotation marks, 20, 22 LJ
Multiplication, 29, 144, 147 Quote mode, 20, 22, 23
U
I (
< >
196 INDEX
LJ
n
n
R STOP key, 18
n RETURN statement, 56
RIGHT$ function, 138
ROM, 171
T
RUN command, 24 TAB function, 141
H
197 INDEX
n
u
u
T V
TAB function, VALue function,
Variables
u
TAN function,
Telecommunications,
u
array,
THEN, dimensions,
TI variable, floating point,
TI$ variable, integer, u
Trackball, numeric,
Troubleshooting chart, string ($), u
VERIFY command,
VIC chip, u
u Voice,
Up arrow key,
u
Upper case/graphic mode,
Upper/Lower Case mode, w
User groups, WAIT command, u
User port, Waveform,
USR function, LJ
u
u
LJ
LJ
U
u
LJ
u
198 INDEX
LJ
n
— NOTES
n
n
n
n
n
n
n
o
n
n
n
n
n
n
n
n
n
n
n
n
u
NOTES ^
y
u
u
u
u
LJ
u
u
u
u
n
n NOTES
n
a
n
n
n
n
o
n
n
n
n
n
a
n
n
n
n
n
n
n
n
n
n
u
NOTES U
u
u
u
u
u
u
u
u
u
u
LJ
u
u
u
u
LJ
u
u
LJ
n
H NOTES
n
n
n
n
n
n
n
n
n
n
a
H
D
n
n
n
u
NOTES- u
u
u
u
u
u
u
u
u
u
LJ
u
u
u
u
u
u
n
n
n
WHEN YOU SUBSCRBE TO
n
n
COMMODORE MAGAZINE.
n You'll find unbiased in-depth reviews of the best new software
and hardware... discover how to make your own programs...
i i learn practical home and business applications. And much,
much more.
n Plus, in every issue, you'll receive free programs—
both games and practical applications—you can type in
n
and use right away!
n And, if you act now we'll send you a FREE "Best of Loadstar"
disk.. .full of great games, practical programs, plus utilities,
n graphics, music, tutorials and much more!
n Subscribe or renew your subscription now at the low rate of $26.95 and we'll
send you a full year of Commodore Magazine (12 issues, total) PLUS your FREE
n "Best of Loadstar" disk ($6.95 value).
n
C64SG
n
Yes,j I want to save 10% off the
n basic subscription rate of $30.00for
Commodore Magazine and receive the
n "Best ofLoadstar" Disk, FREE.
n Name.
Address _
n City . State. .Zip.
METHOD OF PAYMENT
n □ Enclosed is my check or money order for $26.95
(Make payable to COMMODORE PUBLICATIONS)
□ Bill me
□ Charge my VISA or MasterCard Card number
n ■ i i i i i i i i i i i i i rrn
n Signature
Expiration Date
n.
u
u
u
u
u
u
u
u
u
u
u
u
u
LJ
NO POSTAGE u
NECESSARY
IF MAILED
LJ
IN THE
UNITED STATES
u
u
Commodore Publications
Magazine Subscription Department u
Box 651
Holmes, PA 19043 u
u
u
u
u
u
u
LJ
u
u
u
u
Lf
U
U
u
u
u
u
u
u
u
u
LJ
U
u
u
u
n
COMMODORE^