"THIS EQUIPMENT GENERATES AND USES RADIO FREQUENCY ENERGY AND IF NOT INSTALLED PROPERLY, THAT IS, IN STRICT ACCORDANCE WITH THE MANUFACTURERS' INSTRUCTIONS, MAY CAUSE INTERFERENCE TO RADIO AND TELEVISION RECEPTION. IT HAS BEEN
TYPE TESTED AND FOUND TO COMPLY WITH THE LIMITS FOR A CLASS
B COMPUTING DEVICE IN ACCORDANCE WITH THE SPECIFICATIONS IN
SUBPART J OF PART 15 OF FCC RULES, WHICH ARE DESIGNED TO
PROVIDE REASONABLE PROTECTION AGAINST SUCH INTERFERENCE
IN A RESIDENTIAL INSTALLATION. HOWEVER, THERE IS NO GUARANTEE THAT INTERFERENCE WILL NOT OCCUR IN A PARTICULAR INSTALLATION. IF THIS EQUIPMENT DOES CAUSE INTERFERENCE TO
RADIO OR TELEVISION RECEPTION, WHICH CAN BE DETERMINED BY
TURNING THE EQUIPMENT OFF AND ON, THE USER IS ENCOURAGED
TO TRY TO CORRECT THE INTERFERENCE BY ONE OR MORE OF THE
FOLLOWING MEASURES:
• REORIENT THE RECEIVING ANTENNA
• RELOCATE THE COMPUTER WITH RESPECT TO THE RECEIVER
• MOVE THE COMPUTER AWAY FROM THE RECEIVER
• PLUG THE COMPUTER INTO A DIFFERENT OUTLET SO THAT
COMPUTER AND RECEIVER ARE ON DIFFERENT BRANCH CIRCUITS
"IF NECESSARY, THE USER SHOULD CONSULT THE DEALER OR AN
EXPERIENCED RADIO/TELEVISION TECHNICIAN FOR ADDITIONAL SUG-
GESTIONS. THE USER MAY FIND THE FOLLOWING BOOKLET PRE-
PARED BY THE FEDERAL COMMUNICATIONS COMMISSION HELPFUL:
'HOW TO IDENTIFY AND RESOLVE RADIO-TV INTERFERENCE PROBLEMS.' THIS BOOKLET IS AVAILABLE FROM THE U.S. GOVERNMENT
"WARNING: THIS EQUIPMENT HAS BEEN CERTIFIED TO COMPLY WITH
THE LIMITS FOR A CLASS B COMPUTING DEVICE, PURSUANT TO SUBPART J OF PART 15 OF FCC RULES. ONLY PERIPHERALS (COMPUTER
INPUT/OUTPUT DEVICES, TERMINALS, PRINTERS, ETC.) CERTIFIED TO
COMPLY WITH THE CLASS B LIMITS MAY BE ATTACHED TO THIS COM-
PUTER. OPERATION WITH NON-CERTIFIED PERIPHERALS IS LIKELY TO
RESULT IN INTERFERENCE TO RADIO AND TV RECEPTION.
Page 3
0
•
•
•
•
•
•
COMMO
U
SE
R
'S G
D
O
R
E 64
I
U
D
C
E
•
0
Commodore Business Machines, Inc.
11
0
0
Published
by.
Page 4
First Edition
Ninth Printing - 1984
Copyright
All rights reserved.
This manual is copyrighted and contains proprietary information. No part of this publica-
tion may be reproduced
means,electronic, mechanical
written permission of COMMODORE BUSINESS MACHINES, Inc.
Congratulations, on your purchase of one of the best computers in the
world. You are now the proud owner of the
modore is known
being friendly is giving you easy to read, easy to use and easy to
understand instruction manuals.
designed to give you all the information you need to properly set up your
equipment, get acquainted with operating
give you a simple, fun start at learning to make
For those of you who don't want to bother learning how to program,
we've put all the information you need to use Commodore programs or
other prepackaged programs and/or game cartridges (third party
software) right up front. This means you don't have to hunt through the
entire book to get started.
Now let's look at some of the exciting features that are just waiting for
you inside your
you've got the most advanced picture maker in the microcomputer industry. We call it
own pictures in 4 different colors, just like the ones you see on arcade
type video games. Not only that,
many as 8 different picture
soon be available as a software program that you can load directly into
COMMODORE
your
screen
MODORE 64
the computer to take the action you want when the sprites hit each
other.
rival many well known music synthesizers. This part of your computer
gives you 3 independent voices, each with a full 9 octave "piano-type"
range. In addition you get 4 different waveforms (sawtooth, triangle,
variable pulse, and noise), a programmable ADSR (attack, decay, sus-
tain, release
bandpass filter for the voices, and variable
trols. If you want
, even pass
even provides automatic collision detection which instructs
Next
, the COMMODORE
) envelope generator and a programmable high, low, and
as The Friendly Computer
The COMMODORE 64 USER'S GUIDE is
COMMODORE
SPRITE GRAPHICS
64. You can move
one image in
your
music to play back with professional sound re-
64. First, when it comes to graphics
, and it allows you to design your
the SPRITE EDITOR
levels at one
front of or behind another. Your COM-
64 has built-in music and sound effects that
your
COMMODORE
company, and part of
the COMMODORE 64, and
own programs.
your
let's you animate as
time. The
creations anywhere on the
resonance
SPRITE EDITOR will
and volume con-
64. Com-
vii
Page 10
production,
the COMMODORE
64 allows you to connect your audio out-
put to almost any high-quality amplification system.
While we're on the subject of connecting
other pieces of equipment . . . your system can be expanded by adding
accessories, known as peripherals, as your computing needs grow.
Some of your options include items like a DATASSETTE* recorder or as
many as 5, VIC 1541 disk drive storage units for the programs you make
and/or play. If you already have a VIC 1540 disk drive your dealer can
update it for use with the
matrix printer to give you printed copies of your programs, letters, invoices, etc. . . If you want to connect up with larger computers and their
massive data bases then just plug in a VICMODEM cartridge, and get
the services of hundreds of specialists and a variety of information networks through your home or business telephone. Finally if you're one of
those people interested in the wide variety of applications software
available in CP/M**, the
Z-80 microprocessor.
Just as important as all the available hardware is the fact that this
'
USER
It won't tell you everything there is to know about computers, but it will
refer you to a wide variety of publications for more detailed information
about the topics presented. Commodore wants you to really enjoy your
new COMMODORE
not the kind of thing you can learn in a day. Be patient with yourself as
you go through
minutes to fill out and mail in the owner/registration card that came with
your computer. It will ensure that your
registered with Commodore Headquarters and that you receive the most
up-to-date information regarding future enhancements for your machine. Welcome to a whole new world of fun!!
S GUIDE
will help you develop your understanding of computers.
64. And to have fun, remember: programming is
the USER
COMMODORE
COMMODORE
'
S GUIDE
. But before you start, take a few
the COMMODORE 64 to
64. You can add a VIC dot
64 can be fitted with a plug-in
COMMODORE
64 is properly
S
0
NOTE:
Many programs are under development while this manual is being
produced. Please check with your local Commodore dealer and with
Commodore User's Magazines and Clubs, which will keep you up to
date on the wealth of applications programs being written for the
Commodore 64, worldwide.
*DATASSETTE is a registered trademark of Commodore Business Machines, Inc.
** CP/M is a registered trademark of Digital Research Inc. Specifications subject to
change.
Page 11
C
H
A
P
TE
R
• Unpacking' and
Commodore 64
• Installation
• Optional. Connections
• Operation
• Color
Adjustment
Connecting the
0
Page 12
UNPACKING AND CONNECTING THE
COMMODORE 64
The following step-by-step instructions show you how to connect the
Commodore 64 to your television set, sound system, or monitor and
make sure everything is working properly.
Before attaching anything to the computer, check the contents of the
Commodore 64 container. Besides this manual, you should find the fol-
lowing items:
1. Commodore 64
2. Power supply (black box with
an AC
plug and supply cord)
3. Video cable
4. TV Switchbox (
If any items are missing check
a replacement.
,
First
let's take a look at the arrangement of the various connections
on the computer and how each functions.
SIDE PANEL
1. Power Socket
attached here to supply power to the Commodore 64.
2. Power Switch
3. Game Ports
controller paddle, while the lightpen can only be plugged into the
game port closest to the front of your computer.
CONNECTIONS
small silver box with short antenna leads).
back with your
. The free end of the cable from the power supply is
. Turns on power to the Commodore 64.
. Each game connector can accept a joystick or game
dealer immediately for
REAR CONNECTIONS
4. Cartridge
game cartridges.
5. Channel Selector
computer's picture will be displayed on.
6. TV Connector
your television set.
7. Audio & Video Output
can be connected to a high quality sound system, and a "composite" video signal, which can be fed into a television "monitor."
8. Serial
the Commodore 64 through this connector.
Slot. The rectangular slot to the left accepts program or
. Use this switch to select which TV channel the
. This connector supplies both the picture and sound to
. This connector supplies direct audio, which
Port. You can attach a printer or single disk drive directly to
w
•
ah
•
2
Page 13
GAME POWER POWER
PORTS SWITCH SOCKET
<-Ch. 3 Ch. 4->
CARTRIDGE CHANNEL TV AUDIONIDEO SERIAL CASSETTE USER
SLOT SELECTOR CONNECTOR CONNECTOR PORT INTERFACE PORT
9. Cassette Interface
. A DATASSETTE recorder can be attached to the
computer so you can save information entered for use at a later
time.
10. User
Port. Various interface cartridges can be attached to the user
port, such as the VICMODEM, or RS 232 communication cartridge.
INSTALLATION
CONNECTIONS
Connect the computer to your TV as shown on page 4.
1. Attach one end of the TV cable to the phono type TV signal jack at
the rear of the Commodore 64. Just push it in. Either end of the
cable can be used.
2. Connect the other end of the cable to the antenna switchbox. Just
push it in.
TO YOUR TV
3
Page 14
Ft^a
TO
TV SIGNAL
JACK
TV
SWITCH BOX
TO 300 OHM
ANTENNA
INPUT
POWER
SUPPLY
3. If you have a VHF antenna, disconnect it from your TV set.
4. Connect your VHF antenna cable to the screw terminals labeled "antenna input" on the switchbox. If your antenna cable is the round
75-ohm coax type, use a 75-ohm to 300-ohm adapter (not supplied)
to attach your antenna cable to the switchbox.
5. Connect the twin lead output cable of the antenna switchbox to the
VHF antenna terminals of your TV set. If your set is one of the newer
types with a round 75-ohm VHF connector, you will need a 300-ohm
to 75-ohm converter (not supplied) to connect the switchbox to the
75-ohm VHF antenna input on the set.
6. Set the TV's VHF tuner to the channel number indicated on the computer's channel selector switch (channel 3 move the switch to the left,
channel 4 move the switch to the right). If a strong local TV signal is
present on one of these channels, select the other channel to avoid
possible interference.
8. Plug the power supply cable into the power socket on the side of the
Commodore 64. Just push it in. It is "keyed" to allow insertion in only
one direction, so you can't connect the power cord the wrong way.
The power supply converts household current into the form the com-
puter uses.
4
Page 15
The Commodore
nections are required to use the computer
switchbox will connect the computer
the "computer
"
position.
will operate normally.
64 is now correctly connected. No additional con-
to the TV
When the switch
5
with your TV. The
when the slide switch is in
is in the
"TV" position
antenna
your set
Page 16
OPTIONAL CONNECTIONS
Since the Commodore 64 furnishes a channel of high fidelity sound,
you may wish to play it through a quality amplifier to realize the best
sound possible. In addition, the Commodore 64 also provides a standard "composite" video signal, which can be fed into a television
monitor.
These options are made possible by the audio/video output jack on
the rear panel of the Commodore 64. The easiest way to gain access to
these signals is by using a standard 5-Pin DIN audio cable (not
supplied). This cable connects directly to the audio/video connector on
the computer. Two of the four pins on the opposite end of the cable
contain the audio and video
own cable, using the pinouts shown in Appendix I as a guide.
signals
O
S
0
. Optionally, you can construct your
6
Page 17
Normally, the BLACK connector of the DIN cable supplies the AUDIO
signal. This plug may be connected to the AUXILIARY input of an amplifier, or the AUDIO IN connector of a monitor or other video system,
such as a video cassette recorder (VCR).
The WHITE or RED connector usually supplies the direct VIDEO signal.
This plug is connected to the VIDEO IN connector of the monitor or video
input section of some other video system, such as a VCR.
Depending on the manufacturer of your DIN cable, the color coding
of the plugs may be different. Use the pinouts shown in Appendix I to
match up the proper plugs if you don't get an audio or video signal
using the suggested connections.
AUDIOIVIEO
OUTPU
T
4
TO AUXILIARY
INPUT OR
TUNER INPUT TO VIDEO IN
I
TV MONITOR
AUDIO SYSTEM
If you purchased peripheral equipment, such as a VIC 1541 disk drive
or a VIC 1515 printer, you may wish to connect it at this time. Refer to
the user's manuals supplied with any additional equipment for the
proper procedure for connecting it to the computer.
6
a
J
7
Page 18
A completed system might look like this.
Aft
•
OPERATION
USING THE COMMODORE 64
1. Turn on the computer using the rocker
when you
2. After a few moments the following will be displayed on the TV
screen:
E41 RAM 'SYSTEM 3
READY
■
--
'
re looking at the computer from
+*+ i_ C rir1C ODC'RE 64 E:A ; I C: 2 +*++
A .IC E
?-'911 E
,'
:
switch
on the right
the front.
•"iTE'=• FREE
CURSOR SIGNALS
COMMODORE 64 IS
WAITING FOR YOUR
INPUT.
-
side panel
•
8
9
Page 19
3. If your TV has a manual fine tuning knob, adjust the TV until you get
a clear picture.
4. You may also want to adjust the color and tint controls on the TV for
the best display. You can use the color adjustment procedure described later to get everything setup properly. When you first get a
picture, the screen should appear mostly
dark blue, with a light
blue border and letters.
If you don't get the expected results, recheck the cables and connec-
tions. The accompanying chart will help you isolate any problem.
TROUBLESHOOTING CHART
Symptom Cause
Indicator Light
not "On"
Computer not
Power cable Check power socket
not plugged for loose or dis-
in connected power
Power supply Check connection
not plugged with wall outlet
in
Bad fuse in Take system to
computer authorized dealer
TV on wrong Check other
channel channel for
Incorrect
hookup VHF
Remedy
Make sure power
switch is in "On"
position
cable.
for replacement of
fuse
picture (3 or 4)
Computer hooks up to
antenna terminals
Video cable Check TV output
not plugged cable connection
in
Computer set Set computer for
for wrong some channel as TV
channel (3 or 4)
9
Page 20
Symptom
Cause
Remedy
Random pattern
on TV with
cartridge in
place
Picture without
color
Picture with
poor color
Sound with
excess
background
noise
Picture OK,
but no sound
Cartridge not
properly
inserted
Poorly tuned
TV
Bad color
adjustment
on TV
TV volume up
high
TV volume too
low
Aux. output
not properly
connected
Reinsert
cartridge after
turning off power
Retune TV
Adjust color/
hue/brightness
controls on TV
Adjust volume of
TV
Adjust volume of
TV
Connect sound
jack to aux. input
on amplifier and
select aux. input
TIP: The
But we at Commodore recognize that computer users may, occasionally, run into
difficulties. To help answer your questions and give you some fun programming
ideas, Commodore has created several publications to help you. You might also find
that it's a good idea to join a Commodore Users Club to help you meet some other
COMMODORE
COMMODORE
64 owners who can help you gain knowledge and experience.
64 was designed to be used by everyone.
CURSOR
The flashing square under READY is called the cursor and indicates
where what you type on the keyboard will be displayed on the screen.
As you type, the cursor will move ahead one space, as the original
cursor position is replaced with the character you typed. Try typing on
the keyboard and watch as characters you type are displayed on the TV
screen.
10
Page 21
COLOR ADJUSTMENT
There is a simple way to get a pattern of colors on the TV so you can
.
easily adjust the set
operation of the computer right now
how easy it is to use the Commodore 64.
First, look on the left side of the keyboard and locate the key marked
M. This stands for ConTRoL and is used, in conjunction with other
keys, to instruct the computer to do a specific task.
•LK tMIn s>NI CYM PUR No ILU
s .^... 8 • W E
Even though
you may not be familiar with the
just follow along, and you'll see
,
a 15
.
: R . 'r . Y- U I C3
YEt . o«
RUN
soP aeaR
OP
8T
Cc a1N
A 8 C3 F C3 H J K
T z
/P
Mai P_'3a Q
B
x
C
E4 P3
- V
8 sit 1
-
N M
1;- rTfi:
To use a control function, you hold down the= key while depress-
ing a second key.
Try this: hold the= key while also depressing
theakey
lease both keys. Nothing obvious should have happened, but if you
touch any key now, the screen will show the character displayed in reverse type, rather than normal type-like the opening message or anything you typed
earlier.
Hold down the . What happens? If you did the above pro-
cedure correctly, you should see a light blue bar move across the screen
. Then re-
11
Page 22
and then move down to the next line as long as the
SPACE BAR
pressed.
*++* COMMODORE 64 BASIC V2 *+**
64K RAM SYSTEM 3
E911 E:A
SIC BYTES
FREE
READY
Now, hold =while depressing any of the other number keys. Each
of them has a color marked on the front. Anything displayed from this
point will be in that color. For example, hold M and thefkey and
is de-
release
change the bar to any of the other colors indicated on the number keys
by holding • and the appropriate key.
color and tint controls on your TV so the display matches the colors you
selected.
both. Now hold the
Watch the display. The bar is now in yellow! In a like manner you can
Change the bar to a few more different colors and then adjust the
The display should appear something like this:
**** COFIMCODORE 64 BASIC Y2 *+**
64K RAM SYSTEM
READY
SPACE BAR
3
8911 BASIC BYTES FREE
WORED BAR
O GREEN BAR
O BLUE BAR
p YELLOW BAR
At this point everything is properly adjusted and working correctly.
The following chapters will introduce you to the BASIC language. However, you can immediately start using some of the many prewritten ap-
plications and games available for the Commodore 64 without knowing
anything about computer programming.
Each of these packages contains detailed information about how to
use the program. It is suggested, though, that you read through the first
few chapters of this manual to become more familiar with the basic
operation of your new system.
12
Page 23
CHAPTER
GETTING STARTED
• Keyboard
• Back to Normal
• Loading and Saving Programs
• PRINT and Calculations
• Precedence
• Combining Things
m
Page 24
KEYBOARD
Now that you
few moments to familiarize yourself with the keyboard which is your
most important means of communication with the Commodore 64.
You will find the
found in most areas
control specialized functions
various keys and how they function.The detailed operation of each key
will be covered in later sections.
'
ve got everything set up and adjusted, please take a
keyboard similar to a standard typewriter
.
There are,however
. What
,
a number of new keys which
follows is a brief description of the
keyboard
RETURN
The key signals the computer to look at the information that
you typed and enters that information into memory.
SHIFT
The ® key works like that on a standard typewriter. Many keys
are capable of displaying two letters or symbols and two graphic characters. In the "upper/lower case" mode the ® key gives you standard upper case characters. In the "upper case/graphic" mode the
® key will display the graphic character on the right hand side of
the front part of the key.
In the case of special YELLOW function keys, the ® key will give
you the function marked on the front of the key.
14
•
Page 25
EDITING
No one is perfect, and the Commodore 64 takes that into account. A
number of editing keys let you correct typing mistakes and move information around on the screen.
CRSR
There are two keys marked IM (CuRSoR), one with up and down
arrows
ffl=
the other with left and right arrows ^ . You can
use these keys to move the cursor up and down or left and right. In the
unshifted mode, the keys will let you move the cursor down and to
the right. Using the key and keys allows the cursor to be
moved either up or to the left. The cursor keys have a special repeat
feature that keeps the cursor moving until you release the key.
INST/DEL
If you hit the key, the cursor will move back a space, erasing (DELeting) the previous character you typed. If you're in the middle
of a line, the character to the left is deleted and the characters to the
right automatically move together to close up the space.
A ®ed ®i allows you to INSerT information on a
example, if you noticed a typing mistake in the beginning of
perhaps you left out part of a name-you could use the
=CRSR
move back to the error and then hit ®i to insert a
just type in the missing letter.
CLR/HOME
CLR/HOME
positions the cursor at the "HOME" position of the screen,
which is the upper left-hand corner. A shifted will clear the
screen and place the cursor in the home position.
line. For
a line-
key to
space. Then
^^IIt
operates as the name implies. It restores the computer to the
normal state it was in before you changed things with a program or
some command. A lot more will be said on this in later chapters.
15
Page 26
FUNCTION KEYS
The four function keys on the right side of the keyboard can be "pro-
grammed" to handle a variety of functions. They can be defined in
many ways to handle repetitive tasks.
CTRL
TheMkey, which stands for ConTRoL, allows you to set colors, and
perform other specialized functions. You hold the =key down while
depressing another designated key to get a control function. You had an
opportunity to try the= key when you changed text colors to create
different color bars during the setup procedure.
RUN/STOP
Normally, depressing the key will stop the execution of a
BASIC program. It signals the computer to STOP doing something. Using
16
l
Page 27
the key in the shifted mode will allow you to automatically
load a program from tape.
® COMMODORE KEY
The Commodore key ® performs a number of functions. First, it
allows you to move between the text and graphic display modes.
When the computer is first turned on, it is in the Upper Case/Graphic
mode, that is, everything you type is in upper case letters. As was men-
tioned, using the ® key in this mode will display the graphic on the
right side of the keys.
If you hold down the ® key and ® key, the display will change
to upper and lower case. Now, if you hold down the ® key and any
other key with a graphic symbol, the graphic shown on the left side of
the key will be displayed.
To get bock into the upper case/graphic mode hold down the ® key
and ® key again.
The second function of the ® key is to allow you access to a second
set of eight text colors. By holding down the ® key and any of the
number keys, any text now typed will be in the alternate color available
from the key you depressed. Chapter 5 lists the text colors available
from each key.
BACK TO NORMAL
Now that you've had a chance to look over the keyboard, let's explore
some of the Commodore 64's many capabilities.
If you still have the color bars on the screen from adjusting your TV
set, hold ® and . The screen should clear and the cursor
will be positioned in the "home" spot (upper left-hand corner of the
screen).
Now, simultaneously hold ® and the o key. This sets the text color
back to light blue. There is one more step needed to get everything back
to normal. Hold M and
back to normal. If you remember, we turned REVERSE type on with the
MQ to create
spaces). If we were in the normal text mode during the color test, the
cursor would have moved, but just left blank spaces.
a (Zero not
the color bars (the color bars were actually reversed
Oh!). This sets the display mode
17
Page 28
TIP:
Now that you've done things the hard way, there is a simple way to reset the
machine to the normal display. First press the key and then press
the
JIM
This will clear the screen and return everything to normal. If there is a program in
the computer, it will be left untouched. This is a good sequence to remember, especially if you do a lot of programming.
If you wish to reset the machine as if it were turned off and then switched on
again, type, SYS64759 and press I=.. Be careful using this command! It will
wipe out any program or information that is currently in the computer.
key. must always be held down in order to use the
key function.
LOADING AND SAVING PROGRAMS
One of the most important features of the Commodore 64 is the ability
to save and load programs to and from cassette tape or disk.
This capability allows you to save the programs you write for use at a
later time, or purchase prewritten programs to use with the Commodore
64.
Make sure that either the disk drive or datasette unit is attached
properly.
LOADING PREPACKAGED PROGRAMS
For those of you interested in using only prepackaged programs
available on cartridges, cassette, or disk here's all you have to do:
1. CARTRIDGES
and games on cartridge. The programs offer a wide variety of business
and personal applications and the games are just like real arcade
games-not imitations. To load these games, first turn on your TV set.
Next turn OFF
MODORE 64 BEFORE INSERTING OR REMOVING CARTRIDGES OR YOU
MAY DAMAGE THE CARTRIDGE AND/OR YOUR COMMODORE 64!
Third insert the cartridge. Now turn your Commodore 64 on. Finally type
the appropriate START key as is listed on the instruction sheet that comes
with each game.
2. CASSETTES
: The Commodore 64 computer has a line of programs
your
Commodore 64. YOU
MUST TURN OFF YOUR COM-
: Use your DATASSETTE recorder and the ordinary audio cas-
18
0
Page 29
settes that came as part of your prepackaged program. Make sure
the tape is completely rewound to the beginning of the first side.
Then, just type LOAD. The computer will answer with PRESS PLAY ON
TAPE, so you respond by pressing play on your datasette machine. At
this point the computer screen will go blank until the program is
found. The computer will say FOUND (PROGRAM NAME) on the
screen. Now you press down on the ® KEY. This will actually
load the program into the computer. If you want to stop the loading
simply press the key.
3. DISK
: Using your disk drive, carefully insert the preprogrammed disk
so that the label on the disk is facing up and is closest to you. Look
for a little notch on the disk (it might be covered with a little piece of
tape). If you're inserting the disk properly the notch will be on the left
side. Once the disk is inside close the protective gate by pushing down
on the lever. Now type LOAD "PROGRAM NAME", 8 and hit the
^^IIIID
key. The disk will make
noise
and your screen will say:
SEARCHING FOR PROGRAM NAME
LOADING
READY
I
When the READY comes on and the 0 is on, just type RUN, and
your prepackaged software is ready to use.
LOADING PROGRAMS FROM TAPE
Loading a program back from tape or disk is just as simple. For tape,
rewind the tape back to the beginning and type:
LOAD"PROGRAM NAME"
If you don't remember the program name, just type LOAD and the
first program on the tape will be loaded into memory.
After you press i the computer will respond with:
19
Page 30
PRESS PLAY ON TAPE
After you depress the play key, the screen will blank, turning the
s
®
1^
11
border color of the
screen as
the computer
searches
for the program.
When the program is found, the screen will display:
FOUND
To actually LOAD the program, depress the
LOADing procedure, hit ^ . If you hit the Commodore key, the
screen will again turn the border color while the program is LOADed.
After the LOADing procedure is completed, the screen will return to the
normal state and the READY prompt will reappear.
LOADING PROGRAMS FROM DISK
Loading a program from disk follows the same format. Type:
PROGRAM NAME
®
key. To abandon the
I
I
I
II
4
LORD
The 8 is the code for the disk, so you're just letting the computer know
that you want the program loaded from the disk.
After you hit
shows:
SEARCHING FOR PROGRAM NAME
LOADING
READY
I
PROGRAM NAME",8
"
the disk will start whirring and the display
20
II h
Page 31
NOTE:
When you load a new program into the computer's memory, any in-
structions that were in the computer previously will be erased. Make
sure you save a program you're working on before loading a new one.
Once a program has been loaded, you can RUN it, LIST it, or make
changes and re-save the new version.
SAVING PROGRAMS ON TAPE
After entering a program, if you wish to save it on tape, type:
SAVE "PROGRAM NAME"
"PROGRAM NAME" can be up to 16 characters long. After you hit
the computer will respond with:
PRESS PLAY AND RECORD ON TAPE
Press both the record and play keys on the datasette. The screen will
blank, turning the color of the border.
After the program is saved on tape, the READY prompt will reappear,
indicating that you can start working on another program, or just turn
off the computer for a while.
SAVING PROGRAMS ON DISK
Saving a program on disk is even simpler. Type:
SAVE "PROGRAM NAME"
,
21
Page 32
The 8 is the code for the disk, so you're just letting the computer know
you want the program saved to disk.
After you press ^ the disk will start to turn and the computer
will respond with:
SAVIt-IG "PROGRA11 NAME"
READY
0
PRINT
Now that you've gotten through
tions you need in order to
some programs
Try typing the following exactly
AND CALCULATIONS
keep the programs
for you to save.
PR I tIT "C:OMMO ADORE. 64"
CCiPINFOO 'E h4
a couple
as shown:
of the
TYPE THIS
more
you like, lets start making
LINE AND
HIT
READY
■
If you make a typing mistake, use the ®i key to erase the character immediately to the left of the cursor. You can delete as many
characters as necessary.
Let's see what went on in the example above. First, you instructed
(commanded) the computer to PRINT whatever was inside the quote
marks. By hitting ^ you told the computer to do what you in-
structed and COMMODORE 64 was printed on the screen.
When you use the PRINT statement in this form, whatever is enclosed
in quotes is
If the computer responded with:
printed exactly as you typed it.
COMPUTER TYPED
difficult opera-
?SYNTAX ERROR
ask yourself if you made a mistake in typing, or forgot the quote marks.
22
40
Page 33
The computer is precise and expects instructions to be given in a specific
form.
But don't get worried; just remember to enter things as we present
them in the examples and you'll get along great with the Commodore 64.
Remember, you can't hurt the computer by typing on it, and the best
way to learn BASIC is to try different things and see what happens.
PRINT is one of the most useful and powerful commands in the BASIC
language. With it, you can display just about anything you wish, including graphics and results of computations.
For example, try the following. Clear the screen by holding down the
® key and key and type (be sure to use the '1' key for
one, not a letter 'I'):
PRINT 12 +
TYPE THIS LINE AND
HIT
Aphu
COMPUTER PRINTED
THE ANSWER ,
What you've discovered is that the Commodore 64 is a calculator in its
basic form. The result of "24" was calculated and printed automatically.
In fact, you can also perform subtraction, multiplication, division, exponentiation, and advanced math functions such as calculating square
roots, etc. And you're not limited to a single calculation on a line, but
more on that later.
Note that in the above form, PRINT behaved differently from the first
example. In this case, a value or result of a calculation is printed, rather than
the exact message you entered because the quote marks were omitted.
ADDITION
The plus sign (+) signals addition: we instructed the computer to print
the result of 12 added to 12. Other arithmetic operations take a similar
form to addition. Remember to always hit after typing PRINT
and the calculation.
23
Page 34
SUBTRACTION
To subtract, use the conventional minus (-) sign. Type:
MULTIPLICATION
ii
If you wanted to multiply 12 times 12, use the asterisk (*) to represent
multiplication. You would type:
DIVISION
Division uses
the familiar
"/". For example
, to divide 144 by 12, type:
•1
24
0
Page 35
EXPONENTIATION
In a like fashion, you can easily raise a number to a power (this is the
same as multiplying a number by itself a specified number of times).
The 'T' (Up arrow) signifies exponentiation.
PRINT 12 ' 5
248832
This is the same as typing:
PRINT 12 * 12 * 12 * 12 * 12
248832
TIP:
BASIC has a number of shortcut ways of doing things. One such way is abbreviating BASIC commands (or keywords). A ? can be used in place of PRINT, for example. As we go on you'll be presented with many commands; Appendix D shows the
abbreviations for each and what will be displayed on the screen when you type the
abbreviated form.
The last example brings up another important point: many calculations may be performed on the same line, and they can be of mixed
types.
You could calculate this problem:
25
Page 36
Up to this point we've just used small numbers and simple examples.
However, the Commodore 64 is capable of more complex calculations.
You could, for example, add a number of large figures together. Try
this, but don't use any commas, or you'll get an error:
: 123.45 + 345.
'1
78 + 7895.687
8364.917
That looks fine, but now try this:
12123123.45 + 345.78 + 7895.687
12131364.9
If you took the time to add this up by hand, you would get a different
result.
What's going on here? Even though the computer has a lot of power,
there's a limit to the numbers it can handle. The Commodore 64 can
work with numbers containing 10 digits. However when a number is
printed, only nine digits are displayed.
So in our example, the result was "rounded" to fit in the proper
range. The Commodore 64 rounds up when the next digit is five or more;
it rounds down when the next digit is four or less.
Numbers between 0.01 and 999,999,999 are printed using standard
notation. Numbers outside this range are printed using scientific nota-
tion.
Scientific notation is just a process of expressing a very large or small
number as a power of 10.
If you type:
12300
1.23E+17
0000
000000000
26
w
Page 37
This is the same as 1.23 * 10T17 and is used just to keep things tidy.
There is a limit to the numbers the computer can handle, even in
scientific notation. These limits are:
Largest: ±- 1.70141183E+38
Smallest (different from zero): ± 2.93873588-39
PRECEDENCE
If you tried to perform some mixed calculations different from the
examples we showed earlier, you might not have gotten the results that
you expected. The reason is that the computer performs calculations in a
certain order.
In this calculation:
20 + 8/2
you can't tell whether the answer should be 24 or 14 until you know in
which order to perform the calculations. If you add 20 to 8 divided by 2
(or 4), then the result is 24. But, if you add 20 plus 8 and then divide by
2 the answer is 14. Try the example and see what result you get.
The reason you got 24 is because the Commodore 64 performs calcu-
lations left to right according to the following:
First: - minus sign indicating negative numbers
Second: T exponentiation, left to right
Third: multiplication and divisions, left to right
Fourth: +- addition and subtraction, left to right
Follow along according to the order of precedence, and you will see
that in the above example the division was performed first and then the
addition to get a result of 24.
Make up some problems of your own and see if you can follow along
and predict the results according to the rules set down above.
There's also an easy way to alter the precedence process by using
parentheses to set off which operations you want performed first.
For example, if you want to divide 35 by 5-plus-2 you type:
27
Page 38
you will get 35 divided by 5 with 2 added to the answer, which is not
what you intended at all. To get what you really wanted, try this:
2 35 / (5 + 2)
5
What happens now is that the computer evaluates what is contained
in the parentheses first. If there are parentheses within parentheses, the
innermost parentheses are evaluated first.
Where there are a number of parentheses on a line, such as:
7 (1
+ 9) * (6 + 1)
21
147
the computer evaluates them left to right. Here 21 would be multiplied
by 7 for the result of 147.
COMBINING THINGS
Even though we've spent a lot of time in areas that might not seem
very important, the details presented here will make more sense once
you start to program, and will prove invaluable.
To give you an idea how things fit in place, consider the following:
how could you combine the two types of print statements we've examined so far to print something more meaningful on the screen?
We know that by enclosing something within quote marks prints that
information on the screen exactly as it was entered, and by using moth
operators, calculations can be performed. So why not combine the two
types of PRINT statements like this:
Ah
"
5
*
9
5 * 9
= 4
= ' 5
5
SEMICOLON MEANS NO SPACE.
*
9
28
Page 39
Even thcugh this might seem a bit redundant, what we've done is
simply use both types of print statements together. The first part prints
"5 * 9 =" exactly as it was typed. The second part does the actual work
and prints the result, with the semicolon separating the message part of
the statement from the actual calculation.
You can separate the parts of a mixed print statement with punctua-
tion for various formats. Try a comma in place of the semicolon and see
what happens.
For the curious, the semicolon causes the next part of the statement to
be printed immediately after the previous part, without any spaces. The
comma does something different. Even though it is an acceptable
separator, it spaces things out more. If you type:
the numbers will be printed across the screen and down on to the next
line.
The Commodore 64's display is organized into 4 areas of 10 columns
each. The comma tabs each result into the next available area. Since
we asked for more information to be printed than would fit on one line,
(we tried to fit five 10-column areas on one line) the last item was moved
down to the next line.
The basic difference between the comma and semicolon in formatting
PRINT statements can be used to our advantage when creating more
complex displays: it will allow us to create some sophisticated results
very easily.
29
Page 40
Page 41
CHAPTE
BEGINNING
PROGRAMMING
• The Next Step
-GOT4
• Editing Tips"
• Variables
• IF . . . THEN
• FOR . . . NEXT Loops
R
BASIC
m
Page 42
THE NEXT STEP
Up to now we've performed some simple operations by entering a
single line of instructions into the computer. Once ^ was depressed, the operation that we specified was performed immediately.
This is called the IMMEDIATE or CALCULATOR mode.
But to accomplish anything significant, we must be able to have the
computer operate with more than a single line statement. A number of
statements combined together is called a PROGRAM and allows you to
use the full power of the Commodore 64.
To see how easy it is to write your first Commodcre 64 program, try
this:
Clear the screen by holding the ® key, and then depressing the
CLR'HOME
Type NEW and press . (This just clears out any numbers that
might have been left in the computer from your experimenting.)
Now type the following exactly as shown (Remember to hit '
after each line)
key.
0
40
•
10 ?"C0Mt`lCIDCIRE 64"
GUTU
20
Now, type RUN and hit -watch what happens. Your screen
will come alive
the display, hit
A number of important concepts were introduced in this short pro-
gram that are the basis for all programming.
Notice that here we preceded each statement with a number. This
LINE number tells the computer in what order to work with each statement. These numbers are also a reference point, in case the program
needs to get back to a particular line. Line numbers can be any whole
number (integer) value between 0-63,999.
It is good programming practice to number lines in increments of
10-in case you need to insert some statements later on.
Besides PRINT
GOTO.
This instructs the computer to go directly to a particular line and
,
our program also used another BASIC command,
perform it,then continue from that point.
-+ 10 PRINT "COMMODORE 64"
20 GOTO 10
In our example, the program prints the message in line 10, goes to
the next line (20), which instructs it to go back to line 10 and print the
message over again. Then the cycle repeats. Since we didn't give the
computer a way out of this loop, the program will cycle endlessly, until
we physically stop it with the key.
Once you've stopped the program, type: LIST. Your program will be
displayed, intact, because it's still in the computer's memory. Notice,
too, that the computer converted the ? into PRINT for you. The program
can now be changed, saved, or run again.
Another important difference between typing something in the immediate mode and writing a program is that once you execute and
clear the screen of an immediate statement, it's lost. However, you can
always get a program back by just typing LIST.
By the way, when it comes to abbreviations don't forget that the
computer may run out of space on a line if you use too many.
33
Page 44
EDITING TIPS
If you make a mistake on a line, you have a number of editing
options.
1. You can retype a line anytime
and the computer will automatically
,
substitute the new line for the old one.
2. An unwanted line can be erased by simply typing the line number
and
3. You
can also easily edit an existing line, using the
cursor keys and
editing keys.
Suppose you made a typing mistake in a line of the example. To
correct it without retyping the entire line
Type LIST,
cursor up until it is positioned on the line that needs to be changed.
Now, use the cursor
want to change
t»M
If you need more space on the line, position the cursor where the
space is needed and hit ® and 1=111 at the same time and a
space will open up. Now just type in the additional information and hit
s
cursor to the right of the unwanted character and hitting the
key.
To verify that changes were entered
rected program will be displayed! And lines don
numerical order
proper sequence.
Try editing our sample program on page 33 by changing line 10 and
adding a comma to the end of the line. Then RUN the program again.
then using the ® and I keys together move the
-
right key to move the cursor to the character you
typing the change over the old character
,
and the corrected line will replace the old one.
. Likewise
,
you can delete unwanted characters by placing the
.
The computer will automatically place them in the
, try this:
.
Now hit
IN*
,
type LIST again,and the cor-
'
t have to be entered in
111
10 PRINT
"COMMODORE",
VARIABLES
Variables are some of the most used features of any programming
language, because variables can represent much more information in
the computer. Understanding how variables operate will make comput-
ing easier
otherwise.
and allow us to accomplish feats that would not be possible
34
DON'T FORGET TO MOVE THE
CURSOR PAST
YOU RUN THE PROGRAM.
LINE 20 BEFORE
S
e
Page 45
C^ t1MC ADORE
G IMMClDORE
COMMODORE
COMMODORE
COMMODORE
COMMODORE
COMMODORE
COMMODORE
COMMODORE COMMODORE COMMODORE COMMODORE
COMMODORE
COMMODORE
COMMODORE
COMMODORE
BREAK: IN 10
READY
Imagine a number of boxes within the computer that can each hold a
number or a string of text characters. Each of these boxes is to be
labeled with a name that we choose. That name is called a variable
and represents the information in the respective box.
The computer might represent the variables like this:
X% 15
X 23.5
X$ THE SUM OF X%+X =
A variable name represents the box, or memory location, where the
current value of the variable is stored. As you can see, we can assign
either an integer number, floating point number, or a text string to a
variable.
The % symbol following a variable name indicates the variable will
represent an integer number. The following are valid integer variable
names:
35
Page 46
A%
X%
A1%
NM%
The '$' following the variable name indicates the variable will repre-
sent a text string
.
The following are examples of string variables:
A$
X$
Ml$
Floating point variables follow the some format
, with the type indi-
cator:
Al
X
Y
MI
In assigning a name to a variable there are a few things to keep in
mind. First, a variable name can have one or two characters. The first
character must be an alphabetic character from A to Z; the second
character can be either alphabetic or numeric (in the range 0 to 9). A
third character can be included to indicate the type of variable (integer
or text string), % or $.
You can use variable names having more than two alphabetic
characters,but only the first two are recognized by the computer. So
PA and PARTNO are the same and would refer to the some variable
box.
The last rule for variable names is simple: they can't contain any
BASIC keywords (reserved words) such as GOTO, RUN, etc. Refer back
to Appendix D for a complete list of BASIC reserved words.
To see how variables can be put to work, type in the complete program that we introduced earlier and RUN it. Remember to hit
after each line in the program.
^tID
NEW
= 23.
15
"
5
THE
"X!
X$;
10
:•: =
2
0
X
30X$ =
PRINT
40
50
PRINT
OF X% + X =
SUM
= " . Xi:, "X =
X% + X
. X
36
Page 47
If you did everything as shown, you should get the following result
printed on the screen.
RUN
X% =
15
THE SUM OF
X
Xj
=
.5
23
+ X = 38.5
READY
We've put together all the tricks learned so far to format the display
as you see
it and print the sum of the two variables.
In lines 10 and 20 we assigned an integer value to X% and assigned a
floating point value to X. This puts the number associated with the variable in its box. In line 30, we assigned a text string to X$. Line 40
combines the two types of PRINT statements to print a message and the
actual value of X% and X. Line 50 prints the text string assigned to X$
and the sum of X% and X.
Note that even though X is used as part of each variable name, the
identifiers % and $ make X%, X, and X$ unique, thus representing
three distinct variables.
But variables are much more powerful. If you change their value, the
new value replaces the original
value
in the same box. This allows you
to write a statement like:
X = X + 1
This would never be accepted in normal algebra, but is one of the
most used concepts in programming. It means: take the current value of
X, add one to it and place the new sum into the box representing X.
IF ... THEN
Armed with the ability to easily update the value of variables, we can
What we've done is introduce two new BASIC commands, and pro-
vided some control over our runaway little print program introduced at
the start of this chapter.
IF . . . THEN adds some logic to the program. It says IF a condition
holds true THEN do something. IF the condition no longer holds true,
THEN do the next line in the program.
A number of conditions can be set up in using an IF . . . THEN state-
ment:
c_ 5 THEN 20
SYMBOL MEANING
< Less Than
> Greater Than
<>
< = Less Than
The use of any one of these
powerful.
10 CT = 0
20 ?"COMMODORE 64"
30 CT = CT + 1
40 IF CT < 5 THEN 20
I
50 END
Equal To
Not Equal To
Greater Than or Equal
or Equal To
conditions
38
is simple
To
, yet surprisingly
Page 49
In the sample program
straints placed on it by saying: IF a value is less than some number
THEN do something.
Line 10 sets CT
adds one to the variable CT. This line counts how many times we do the
loop. Each time the loop is executed
Line 40 is our control line. If CT is less than 5
cuted the loop less than 5 times, the program goes back to line 20 and
prints again
64's were printed
the program.
Try the program and see what we mean
line 40 you can have any number of lines printed.
IF . . . THEN has a multitude of other uses
examples.
(
CounT
.
When CT becomes equal to 5
-
,
we've set up a
)
equal to 0. Line 20 prints our message
,
the program goes to line 50
"
loop" that has some con-
CT goes up by one.
meaning we
,
indicating 5 COMMODORE
,
which signals to END
By changing the CT limit in
.
,
which we'll see in future
.
'
ve exe-
FOR ... NEXT LOOPS
Line 30
There is a simpler
the previous example by using a FOR
, and preferred way to accomplish what we did in
. . . NEXT loop. Consider the
following:
am
113 FOR C. T = 1 TO 5
20 PRINT "COMMODORE 64"
:11
0 NEXJ CT
RIIN
COMMODORE 64
COMMODORE 64
CCIMI.ICICIC IRE 64
COMMODORE 64
I_ CIpIMC ID0F'E 64
As you can see, the program has become much smaller and more
direct.
,
CT starts at 1 in line 10. Then
line 20 does some printing
In Line 30
.
39
Page 50
CT is incremented by 1. The NEXT statement in line 30 automatically
sends the program back to line 10 where the FOR part of the FOR .. .
NEXT statement is located. This process will continue until CT reaches the
limit you entered.
The variable used in a FOR . . . NEXT loop can be incremented by
smaller amounts than 1, if needed.
Try this:
NEW
10 FOR NB = 1 TO 10 STEP .5
20 PRINT NB,
30 NEXT NB
RUN
1
3
5
7
9
1.5
3.5
5.5
7.5
9.5
2
4
6
10
2.5
4.5
6.5
8.5
If you enter and run this program, you'll see the numbers from 1 to
10, by .5, printed across the display.
All we're doing here is printing the values that NB assumes as it goes
through the loop.
You can even specify whether the variable is increasing or decreas-
ing. Substitute the following for line 10:
10 FOR NB
and watch the opposite occur,
order.
= 10 to 1 STEP -.5
as NB goes
40
from 10 to 1 in descending
S
0
•
S
•
Page 51
G
HAP
7E
l
t
ADVANCED BASIC
• Introduction
• Simple Animation
-Nested Loops
• INPUT
• GET
• Random Numbers and Other Functions
• Guessing Game
• Your Roll
• Random Graphics
-CHR$ and ASC Functions
m
Page 52
INTRODUCTION
The next few chapters have been written for people who have become relatively familiar with
concepts
For those
may find
completely
SPRITE GRAPHICS and CREATING SOUND
examples that
a good
capabilities available
If you decide that you want
BASIC,
ual.
If you
will help you
es. More detailed information can befound
PROGRAMMER
Commodore
necessary to write
of you who
some
. But take heart. . . because
idea of how to
we've put a
are already
of the
are written for the new user. The examples will give you
on your COMMODORE 64.
bibliography (Appendix
familiar with BASIC
get started with advanced
'
S REFERENCE MANUAL, available through
dealer.
the BASIC
more
are just starting to learn how to program, you
information a bit too technical to understand
use the sophisticated sound and graphics
to learn more
programming language and the
advanced
programs.
for these two fun chapters,
,
we've set up some simple
about writing
N) in the back of this man-
programming,these chapters
BASIC
programming
in the COMMODORE 64
programs in
techniqu-
your local
4
2
Page 53
SIMPLE
ANIMATION
Let's exercise some of the Commodore 64's graphic capabilities by
putting together what we've seen so far, together with a few new con-
cepts. If you're ambitious, type in the following program and see what
happens. You will notice that within the print statements we can also
include cursor controls and screen commands. When you see something
like {CRSR LEFT} in a program listing, hold the ® key and hit the
CRSR LEFT/ RIGHT key. The screen will show the graphic representation
of a cursor left (two vertical reversed bars). In the same way, pressing
® and shows as a reversed heart.
NEW
10 REM BOUNCING BALL
20 PRINT
25 FOR
30 FOR
40 PRINT"
50 FOR TM
"{CLR/HOME}
X = 1 TO
BL = 1 TO 40
•{CRSR LEFT)";:
&
TO 5
10 : PRINT "{CRSR/DOWN
REM (• is a SHIFT-Q)
}":
NEXT
60 NEXT TM
70 NEXT BL
75 REM MOVE
80 FOR BL
90 PRINT" {CRSR
B1' RIGHT TO LEFT
,;-,-
40 TO
1 STEP -1
LEFT}{
CRSR LEFT
}•{
CRSR
LEFT)";
100 FOR TM = 1 TO 5
110 NEXT TM
120 NEXT BL
130 GOTO 20
TIP:
All words in this text will be completed on one line. However, as long as you don't
hit
word.
your 64 will automatically move to the
The program will display a bouncing ball moving from left to right,
and back again, across the screen.
If we look at the program closely, (shown on page 44) you can see
how this feat was accomplished.
Line 10 is a REMark that just tells what the program does; it has no
43
next line even
in the middle of a
Page 54
10 REM BOUNCING BALL
-20 PRINT "{CLR/HOME}"
25 FOR X = 1 TO 10 : PRINT "{CRSR/DUWN}": NEXT
30 FOR BL = i TO 40
40 PRINT" •{CRSR LEFT} ";:R.EM (• is a SHIFT-Q)
50 FOR TM = 1 TO 5
60
70
75
80
90
E100 1 TO5
XT TM
NE
NEXT
REM
FOR
PRINT
BL
MOVE BALL RIGHT TO LEFT
BL
=
40 TO 1 STEP -1
" {
CRSR LEFT}{CRSR LEFT
T M
}•{
CRSR LEFT)";
110 NEXT TM
120 NEXT BL
- 130 GGT0 20
effect on the program itself. Line 20 clears the screen of any information.
Line 25 PRINTs 10 cursor-down commands. This just positions the ball
in the middle of the screen. If line 25 was eliminated the ball would
move across the top line of the screen.
Line 30 sets up a loop for moving the ball the 40 columns from the left
to right.
Line 40 does a lot of work. It first prints a space to erase the previous
ball positions, then it prints the ball, and finally it performs a cursor-left
to get everything ready to erase the current ball position again.
The loop set up in lines 50 and 60 slows the ball down a bit by delay-
ing the program. Without it, the ball would move too fast to see.
Line 70 completes the loop that prints balls on the screen, set up in
line 30. Each time the loop is executed, the ball moves another space to
the right. As you notice from the illustration, we have set up a loop
within a loop.
This is perfectly acceptable. The only time you get in trouble is when
the loops cross over each other. It's helpful in writing programs to check
yourself as illustrated here to make sure the logic of a loop is correct.
To see what would happen if you cross a loop, reverse the statements
in lines 60 and 70. You will get an error because the computer gets
confused and cannot figure out what's going on.
Lines 80 through 120 just reverse the steps in the first part of the
program, and move the ball from right to left. Line 90 is slightly different from line 40 because the ball is moving in the opposite direction (we
have to erase the ball to the right and move to the left).
44
Page 55
And when that's all done the program goes back to line 20 to start the
whole process over
down and hit
For a variation on the program, edit line 40 to read:
40 PRINT "•";
Run the program and see what happens now. Because we left out the
cursor control, each ball remains on the screen until erased by the ball
moving right to left in the second part of the program.
again
. Pretty neat! To stop the program hold
TO MAKE THE S. HOLD THE SHIFT
KEY DOWN AND HIT THE LETTER "Q."
INPUT
Up to now, everything within a program has been set before it is run.
Once the program was started, nothing could be changed. INPUT
allows us to pass new information to a program as it is running and
have that new information acted upon.
To get an idea of how INPUT works, type NEW
short program:
and enter this
10 INPUT A$
20 PRINT "YOU TYPED:
.30 PRINT
40 GOTO 10
RUN
2
COMMODORE 54
YOU TYPED: COMMODORE 64
What happens when you run this program is simple. A question mark
will appear, indicating that the computer is waiting for you to type
something. Enter any character, or group of characters, from the
keyboard and hit i. The computer will then respond with "YOU
TYPED :" followed by the information you entered.
This may seem very elementary, but imagine what you can have the
computer do with any information you enter.
You can INPUT either numeric or string variables, and even have the
INPUT statement prompt the user with a message. The format of INPUT is:
INPUT "PROMPT MESSAGE";VARIABLE
YOU TYPED
COMPUTER
RESPONDED
PROMPT MUST BE 38 CHARACTERS OR LESS.
45
Page 56
Or, just:
INPUT VARIABLE
NOTE:
To get out of this program
um M
6
keys.
hold down the
MIsI
The following program is not only useful, but demonstrates a lot of
what has been presented so far, including the new input statement.
If you enter and run this program, you'll see INPUT in action.
the input statement to not only gather information, but
Line 10
also print our prompt. Also notice that we can ask for either a number or
string (by using a numeric or string variable).
Lines 20, 30, and 40 do some checks on what is typed in. In line 20, if
nothing is entered (just ^ is hit), then the program goes back to
line 10 and requests the input again. In line 30, if F is typed, you know
the user wants to convert a temperature in degrees Fahrenheit to Cel-
sius, so
Line 40 does one more check. We know there are only two valid
choices the user can enter. To get to line 40, the user must have typed
some character other than
acter is a C; if not, the program requests input
This may seem like a lot of detail, but it is good programming prac-
uses
the program branches to the part that does that conversion.
F. Now, a
check is made to see if that char-
'again.
Page 57
tice. A user not familiar with the program can become very frustrated if
it does something strange because a mistake was made entering information.
Once we determine what type of conversion to perform, the program
does the calculation and prints out the temperature entered and the
converted temperature.
The calculation is just straight math, using the established formula for
temperature conversion. After the calculation is finished and answer
printed, the program loops back and starts over.
After running, the screen might look like this:
CONVERT FROM FAHRENHEIT OR CELSIUS (F/C): ?F
ENTER
:32 DEG
DEGREESFAHRENHEIT: 32
. FAHRENHEIT
= 0 DEG. CELSIUS
CONVERT FROM FAHRENHEIT
After running the program
program, as well as others presented throughout the manual
the base of your program
,
library.
OR CELSIUS (F,C):
make sure to save it on disk or tape. This
,
can form
GET
GET allows you to input one character at a time from the keyboard
without hitting.
cations.Whatever key is hit is assigned to the variable you specify with
GET.
The following routine illustrates how GET works:
NEW
This really speeds entering data in many appli-
1 PRINT " {CLR/HOME} "
10 GET A$: IF AS = " THEN 10
20 PRINT AS;
30 GOTO 10
47
Page 58
If you RUN the program, the screen will clear and each time you hit a
key, line 20 will print it on the display, and then GET another character.
It is important to note that the character entered will not be displayed
unless you specifically PRINT it to the screen, as we've done here.
The second statement on line 10 is also important. GET continually
works, even if no key is pressed (unlike INPUT that waits for a response),
so the second part of this line continually checks the keyboard until a key
is hit.
See what happens if the second part of line 10 is eliminated.
To stop this program you can hit the MM and J:M keys.
The first part of the temperature conversion program could easily be
rewritten to use GET. LOAD the temperature conversion program, and
modify lines 10, 20 and 40 as shown:
10 PRINT
(F/C7"
20 GET A$
40 IF
This modification will make the program operate smoother, as nothing
will happen unless the user types in one of the desired responses to
select the type of conversion.
Once this change is made, make sure you save the new version of the
program.
RANDOM
The Commodore 64 contains a number of functions that are used to
perform special operations. Functions could be thought of as built-in
programs included in BASIC.. But rather than typing in a number of
statements each time you need to perform u specialized calculation, you
just type the command for the desired function and the computer does
the rest.
Many times when designing a game or educational program, you
need to generate a random number, to simulate the throw of dice, for
example. You could certainly write a program that would generate these
numbers, but an easier way to call upon the RaNDom number function.
To see what RND actually does, try this short program:
"
CONVERT FROM FAHRENHEIT
IF AS = "''
:
AS C:> "
C" THEN 20
NUMBERS
OR CELSIUS
_
T
HEN
AND OTHER FUNCTIONS
48
Page 59
NEW
o
10 FOR
20 PRINT RND
30 NEXT
X = 1 T
1e
(
1)
IF YOU LEAVE OUT THE COMMA YOUR LIST
, OF NUMBERS
AS
After running the program, you will see a display like this:
WILL APPEAR
1 COLUMN
.789280697
.256373663
.682952381
.402343724
.158209063
Your numbers don't match? Well, if they did we would all be in
trouble, as they should be completely random!
Try running the program a few more times to verify that the results are
always different. Even if the numbers don't follow any pattern, you
should start to notice that some things remain the same every time the
program is run.
First, the results are always between 0 and 1, but never equal to 0 or
1 . This will certainly never do if we want to simulate the random toss of
dice, since we're looking for numbers between 1 and 6.
The other important feature to look for is that we are dealing with real
numbers (with decimal places). This could also be a problem since
whole (integer) numbers are often needed.
There are a number of simple ways to produce numbers from the
RND function in the range desired.
Replace line 20 with the following and run the program again:
.664673
.
0123442287
3.90587275E-04
.
879300926
.245596701
958
20 PRINT 6*RND(1),
RUN
3
3.6056
5.47238963
3.19265054
3.16331095
664
9.32527884
4.53660853
40850227
8.
4.39547668
5.50620749
4.17090293
49
Page 60
That cured the problem of not having results larger than 1, but we still
hove the decimal part of the result to deal with.Now, another function
can be called upon.
The INTeger function converts real numbers into integer values.
Once more
see the effect of the change:
,
replace line 20 with the following and run the program to
0
PRINT
M
A
That took
generating random numbers between 1 and 6. If you examine
what we generated this last time
0 to 5, only.
As a last step,add a one to the statement
20 PRINT INT(6*RND
Now, we have achieved the desired results.
In general,you can place a number, variable
sion within the parentheses of the INT function. Depending on the range
desired
example
type:
you just multiply the upper limit by the RND function. For
,
to generate random numbers between 1 and 25
,
Ira? : 6* tl[I :: 1
El
care of a lot
,
(1))+1,
I
getting us closer to our original goal of
, you'll find that
the results range from
,
as follows:
or any BASIC expres-
,
you could
,
closely
(
20 PRINT INT
The general formula for generating a set of random numbers in a
certain range is:
25*RND(1))+1
NUMBER=INT(LOWER LIMIT+(UPPER
GUESSING GAME
Since we've gone to some lengths to understand random numbers,
why not put this information to use? The following game not only illus-
50
-
LOWER+1)*RND(1))
Page 61
trates a good use of random numbers, but also introduces some additional programming theory.
In running this program, a random number, NM, will be generated.
You can specify how large the number will be at the start of the program. Then, it's up to you to guess what the number is.
A sample run follows along with an explanation.
ENTER UPPER LIMIT
I'VE GOT THE
WHAT'S YOUR GUESS 7 15
MY NUMBER IS HIGHER.
WHAT'S YOUR GUESS ? 20
MY NUMBER. IS LOWER.
WHAT
GREAT ! YOU GOT MY
IN ONLY 3 GUESSES.
DO YOU J WANT TO TRY ANOTHER. (Y/N`-
S YOUR GUE
'
NUMBER..
FOR GUESS?
SS ? 19
NUMBER
51
Page 62
IF/THEN statements compare your guess to the number generated.
Depending on your guess
,
the program tells you whether
your
guess was
higher or lower than the random number generated.
From the formula given for determining random number range, see if
you can add a few lines to the program that allow the user to also
specify the lower range of numbers generated.
Each time you make a guess, CN is incremented by I to keep track of
the number of guesses.In using the program, see if you can use good
reasoning to guess a number in the least number of tries.
,
When you get the right answer
YOU GOT MY
NUMBER" message, along with the number of tries it took.
You can then start the process over again
the program prints out the
.
Remember
,
"GREAT!
the program
generates a new random number each time.
PROGRAMMING TIPS:
In lines 40 and 50, a colon is used to separate multiple statements on a single line.
This not only saves typing, but in long programs will conserve memory space.
Also notice in the IF/THEN statements on the same two lines, we instructed the
computer to PRINT something, rather than immediately branching to some other point
in the program.
The last point illustrates the reason behind using line numbers in increments of 10:
After the program was written, we decided to add the count part. By just adding
those new lines at the end of the program, numbered to fall between the proper
existing lines, the program was easily modified.
YOUR ROLL
The following program simulates the throw of two dice. You can enjoy
it as it stands, or use it as part of a larger game.
PRINT
5
PRINT
10
PRINT
20
PRINT
30
GET AS: IF AS = "" THEN 40
40
IF AS = CHRS(
50
Care to try your luck?
From what you've learned about random numbers and BASIC, see if
you can follow what is going on.
" Care
"RED DICE =
"WHITE DICE =
"HIT SPACE BAR FOR ANOTHER
to try ,Your luck? "
"
;INT(6•RND(1))+1
"
;INT(6*RND(1))+1
32) THEN 10
52
ROLL
":PRINT
Page 63
RANDOM GRAPHICS
As a final note on random numbers, and as an introduction to design-
ing graphics, take a moment to enter and run this neat little program:
PRINT
10
PRINT
20
GOTrO
40
As you may have expected, line 20 is the key here. Another function,
CHR$ (Character String), gives you a character, based on a standard
code number from 0 to 255. Every character the Commodore 64 can
print is encoded this way (see Appendix F).
To quickly find out the code for any character, just type:
PRINT ASC("X")
where X is the character you're checking
character, including graphics). The response is the code for the character you
which returns the standard
You can now print that character
PRINT CHR$(X)
If you try typing:
typed. As you
"{C:LR: HOME)
CHR$
(205.5 + RND':Ii) ;
20
probably figured
"ASCII" code for
by typing:
this can be any printable
(
out, "ASC"
the character
is another function,
you typed.
PRINT CHR$ (205
you will see the two right side graphic characters on the M and N keys.
These are the two characters that the program is using for the maze.
By using the formula 205
number between 205.5 and 206
number being above or below 206. CHR$ ignores any fractional values,
so half the time the character with code 205 is printed and the remaining time code 206 is displayed.
If you'
adding or subtracting a couple tenths from it
acter a greater chance of being selected.
d like to experiment with this program
CHR$(206)
);
5 + RND
.
1) the computer will pick a random
(
.
5. There is a
53
fifty-fifty
,
try changing
This will give either char-
.
chance of the
205.5 by
Page 64
s
01
0
0
2
0
0
0
Page 65
CHAPTER
5
ADVANCED COLOR
AND GRAPHIC
COMMANDS
• Color and Graphics
• PRINTing Colors
• Color CHR$ Codes
• PEEKS
• Screen Graphics
• More
and POKES'
Bouncing Balls
b11
Page 66
COLOR AND GRAPHICS
Up to now we've explored some of the sophisticated computing
capabilities of the Commodore 64. But one of its most fascinating features is an outstanding ability to produce color and graphics.
You've seen a quick example of graphics in the "bouncing ball" and
"maze" programs. But these only touched on the power you command.
A number of new concepts will be introduced in this section to explain
graphic and color programming and show how you can create your own
games and advanced animation.
Because we've concentrated on the computing capabilities of the machine, all the displays we've generated so far were a single color (light
blue text on a dark blue background, with a light blue border).
In this chapter we'll see how to add color to programs and control all
those strange graphic symbols on the keyboard.
PRINTING COLORS
As you discovered if you tried the color alignment test in Chapter 1,
you can change text colors by simply holding the= key and one of
the color keys. This works fine in the immediate mode, but what hap-
pens if you want to incorporate color changes in your programs?
When we showed the "bouncing ball" program, you saw how
keyboard commands, like cursor movement, could be incorporated
within PRINT statements. In a like way, you can also add text color
changes to your programs.
You have a full range of 16 text colors to work with. Using them
key and a number key, the following colors are available:
1 2 3 4 5 6 7 8
Black White Red Cyan Purple Green Blue Yellow
If you hold down the ® key along with the appropriate number
key, these additional eight colors can be used:
1 2 3 4 5 6 7 8
Orange Brown It
. Gray 1 Gray 2 Lt. It. Gray 3
Red Green Blue
TYPE NEW, and experiment with .he following. Hold down them
key and at the some time hit the 0 key. Next, hit the Q key without
56
•
Page 67
holding down the =key. Now, while again depressing the Mkey
at the same time hit the key. Release theMkey and hit theOkey.
Move through the numbers, alternating with the letters, and type out the
word RAINBOW as follows:
10 PRINT
00
R1A1I1N1B
" 1
0
00000
1O1W"
RUN
RAINBOW
Just as cursor controls show as graphic characters within the quote
marks of print statements, color controls are also represented as graphic
characters.
In the previous example, when you held down 0 and typedua
'£" was displayed. M0 displayed a "-". Each color control will
display its unique graphic code when used in this way. The table shows
the graphic representations of each printable color control.
Even though the PRINT statement may look a bit strange on the
screen, when you RUN the program, only the text will be displayed. And
it will automatically change colors according to the color controls you
placed in the print statement.
Try a few examples of your own, mixing any number of colors within a
single PRINT statement. Remember, too, you can use the second set of
text colors by using the Commodore key and the number keys.
TIP:
You will
"READY
mode change
7r::r6iPlli
notice
" prompt
and
after
. To get
running
and any
a program with color or mode (
additional text
back to the normal display,
you type is
57
reverse
the same as the lost
remember
to depress:
changes, that the
)
color or
Page 68
COLOR
CHR$ CODES
Take a brief look at Appendix F, then turn back to this section.
You may have noticed in looking over the list of CHR$ codes in
Appendix F that each color (as well as most other keyboard controls,
such as cursor movement) has a unique code. These codes can be
printed directly to obtain the same results as typing • and the
appropriate key within the PRINT statement.
For example, try this:
NEW
10 FEINT CHR# 1::14 ' : PEN { C•LF'i`
L_t FE:I'ITi::HHHGE' ME Tii"i
RUN
CHR :: _ 0;' CHANGE'_; ME TO?
HCOME }
The text should now be green. In many cases, using the CHR$ function will be much easier, especially if you want to experiment with
changing colors. The following program is a different way to get a rain-
bow of colors. Since there are a number of lines that are similar (40-
110) use the editing keys to save a lot of typing. See the notes after the
listing to refresh your memory on the editing procedures.
Type lines 5 through 40 normally. Your display should look like this:
1
REM AU
5
PRINT CHR$(147)
PRINT
113
20
CL = I NT *RND 1 +1
30
ON C:L GOTO 40,50,60,70, 80,90,100
40
PRINT CHR'$<5::' ; CiOTO 10
TOMATIC: COLOR BARE:
REM i_HR
CHR$(18 } : "
$(147;'= CLR,-'HOME
"; :REM REVERSE BARS
.,110
EDITING NOTES
Use the CRSR-UP key to position the cursor on line 40. Then type 5
over the 4 of 40. Next, use the CRSR-RIGHT key to move over to the 5 in
the CHR$ parentheses. Hit ®
rm-M
to open up a space and type
'28'. Now just hit i with the cursor anywhere on the line.
The display should now look like this:
REM
1
5
10
20 CL = I NT (=:*
:30 ON CL OOTO 40,`U,513
AUTOMATI
PRINT CHR$':: 14
C COLOR E:ARS
)
PRINT CHR$':: 18::' ; . : REM
RND':: 1
REM CHF
:' )+
1
$ f 14
„•0..80,90 101+.110
::' = CLR:-'
HCir1E
REVER'_.E BAR
50 PRINT CHR$ :'2 ::, :: GO TO 10
0
Don't worry. Line 40 is still there. LIST the program and see. Using the
same procedure, continue to modify the last line with a new line number
and CHR$ code until all the remaining lines have been entered. See, we
told you the editing keys would come in handy. As a final check, list the
entire program to make sure all the lines were entered properly before
you RUN it.
Here is a short explanation of what's going on.
You've probably figured out most of the color bar program by now
except for some strange new statement in line 30. But let's quickly see
59
Page 70
what the whole program actually does. Line 5 prints the CHR$ code for
CLR/HOME.
Line 10 turns reverse type on and prints 5 spaces, which turn out to be
a bar, since they're reversed. The first time through the program the bar
will be light blue, the normal text color.
Line 20 uses our workhorse, the random function to select a random
color between 1 and 8.
Line 30 contains a variation of the IF . . . THEN statement which is
called ON . . . GOTO. ON . . . GOTO allows the program to choose
from a list of line numbers to go to. If the variable (in this case CL) has a
value of 1, the first line number is the one chosen (here 40). If the value
is 2, the second number in the list is used, etc.
Lines 40-110 just convert our random key colors to the appropriate
CHR$ code for that color and return the program to line 10 to PRINT a
section of the bar in that color. Then the whole process starts over
again.
See if you can figure out how to produce 16 random numbers, expand ON . . . GOTO to handle them, and add the remaining CHR$
codes to display the remaining 8 colors.
O
1
■
PEEKS AND POKES
No, we're not talking about jabbing the computer, but we will be able
to "look around" inside the machine and "stick" things in there.
Just as variables could be thought of as a representation of "boxes"
within the machine where you placed your information, you can also
think of some specially defined "boxes" within the computer that represent specific memory locations.
The Commodore 64 looks at these memory locations to see what the
screen's background and border color should be, what characters are to
be displayed on the screen-and where-and a host of other tasks.
By placing, "POKEing," a different value into the proper memory location, we can change colors, define and move objects, and even
create music.
These memory locations could be represented like this:
53280
X
BORDER
COLOR
53281
Y
BACKGROUND
COLOR
53282
53283
60
0
Page 71
On page 60 we showed just four locations,
screen and background colors.
Try typing this:
two of which
POKE 53281,7
The background color of the screen will change to yellow because we
placed the value '7'-for yellow-in the location that controls the
background color of the screen.
Try POKEing different values into the background color location, and
see what results you get. You can POKE any value between 0 and 255,
but only 0 through 15 will work.
The actual values to POKE for each color are:
control the
0
1
2RED
3
4
5GREEN
6
7
BLACK
WHITE
CYAN
PURPLE
BLUE
YELLOW
8
9
10
11
12
13
14
15
ORANGE
BROWN
Light RED
GRAY 1
GRAY 2
Light GREEN
Light BLUE
GRAY 3
Can you think of a way to display the various background and border
combinations? The following may be of some help:
10 FOR 88 = 0 TO 15
20 FOR BO = 0 TO 15
30 POKE
53280, BR.
40 POKE 53281, BO
50 FOR
60 NEXT 8
X = 1 TO 2000 NE:<T X
0
: NEXT 88
RUN
Two simple loops were set up to POKE various values to change the
background and border colors. The DELAY loop in line 50 just slows
things down a bit.
61
Page 72
For the curious, try:
? PEEK(53280) AND 15
You should
and makes sense because
GRAY (value 15) after the program is run.
By entering
cause
of the way color codes are stored in the computer. Normally you
get a value
of 15. This is the last value BORDER was given
both the background and border colors are
AND 15 you eliminate all other values except 1-15, be-
would expect to find the some value that was last POKEd in the location.
In general
, PEEK
lets us examine
a specific location and see what value
is presently there. Can you think of a one line addition to the program
that will display the value of BACK and BORDER as the program runs?
How about this:
25 PRINT CHR$
GROUND
(
147); "BORDER = ";PEEK
= ";
PEEK (53281) AND 15
(
53280
) AND 15, "BACK-
SCREEN GRAPHICS
In all the printing of information that you've done so for, the computer
normally handled information in a sequential fashion: one character is
printed after the next, starting from the current cursor position (except
where you asked for a new line, or used the ',' in PRINT formatting).
To PRINT data in a particular spot you can start from a known place
on the screen and PRINT the proper number of cursor controls to format
the display. But this takes program steps and is time consuming.
But just as there are certain spots in the Commodore 64's memory to
control color, there are also locations that you can use to directly control
each location on the screen.
SCREEN MEMORY MAP
Since the computer'
columns by 25 lines) there are 1000 memory locations set aside to handle what is placed on the screen. The layout of the screen could be
thought of as a grid, with each square representing a memory location.
And since each location in memory can contain a number from 0 to
255, there are 256 possible values for each memory location. These
values represent the different characters the Commodore 64 can display
(see Appendix Q. By POKEing the value for a character in the appro-
s screen is
capable of holding 1000 characters (40
62
W
Page 73
priate screen memory location, that character will be displayed in the
Screen memory in the Commodore 64 normally begins at memory
location 1024, and ends at location 2023. Location 1024 is the upper left
corner of the screen. Location 1025 is the position of the next character
to the right of that, and so on down the row. Location 1063 is the
right-most position of the first row. The next location following the last
character on a row is the first character on the next row down.
Now, let's say that you're controlling a ball bouncing on the screen.
The ball is in the middle of the screen, column 20, row 12. The formula
for calculation of the memory location on the screen is:
POINT = 1024 + X + 40*Y
COLUMN
ROW
where X is the column and Y is the row.
Therefore, the memory location of the ball is:
1024 + 20 + 480 or 1524
63
COLUMN
ROW (40' 12)
Page 74
Clear the screen with ® and
CIR/HOME
and type:
POKE 1524,81
POKE 55796,1
COLOR
Li
LOCATION
COLOR MEMORY MAP
A ball appears in the middle of the screen! You have placed a char-
acter directly into screen memory without using the PRINT statement.
The ball that appeared was white. However there is a way to change
the color of an object on the screen by altering another range of memory. Type:
POKE 55796,2. COLOR
LOCATION
The ball's color changes to red. For every spot on the Commodore 64's
screen there are two memory locations, one for the character code, and
the other for the color code. The color memory map begins at location
55296 (top left-hand corner), and continues on for 1000 locations. The
same color codes, from 0-15, that we used to change border and
background colors can be used here to directly change character colors.
The formula we used for calculating screen memory locations can be
modified to give the locations to POKE color codes. The new formula is:
COLOR PRINT = 55296 + X + 40*Y
MORE BOUNCING BALLS
Here's a revised bouncing ball program that prints directly on the
screen with POKEs, rather than using cursor controls within PRINT statements. As you will see after running the program, it is much more flexible than the earlier program, and will lead up to programming much
more sophisticated animation.
NEW
10
PRINT "{CLR/HUME)"
20
POKE 53280,7 : POKE 53281,13
30
40
50
60
70
80
90
100
110
120
X =
DX =
POKE
FOR
POKE
X = X + DX
1 : Y = 1
1 DY = 1
1024 + X + 40*Y.81
=
T
IF X < = 0 OR X i =
Y = Y + DY
IF Y = 0 OR Y
GOTO 50
1 TO 10 : NEXT
1024
+
X + 40*Y,32
39 THEN DX = -DX
? =
24 THEN DY = -DY
Line 10 clears the screen, and line 20 sets the background to light
green with a yellow border.
The X and Y variables in line 30 keep track of the current row and
column position of the ball. The DX and DY variables in line 40 are the
horizontal and vertical direction of the ball's movement. When a +1 is
added to the X value, the ball is moved to the right; when -1 is added,
the ball moves to the left. A + 1 added to Y moves the ball down a row;
a -1 added to Y moves the ball up a row.
Line 50 puts the ball on the screen at the current cursor position. Line
60 is the familiar delay loop, leaving the ball on the screen just long
enough to see it.
Line 70 erases the ball by putting a space (code 32) where the ball
was on the screen.
65
Page 76
Line 80 adds the direction factor to X. Line 90 tests to see if the ball
has reached one of the side walls, reversing the direction if there's a
bounce. Lines 100 and 110 do the same thing for the top and bottom
walls.
Line 120 sends the program back to display and moves the ball
again.
By changing the code in line 50 from 81 to another character code,
you can change the ball to any other character. If you change DX or DY
to 0 the ball will bounce straight instead of diagonally.
We can also add a little more intelligence. So for the only thing you
checked for is the X and Y values getting out of bounds for the screen.
Add the following lines to the program.
21 FOR L = 1 TO 10
25 POKE 1024 + INT(RND(
27 NEXT L
55 IF PEEK(1024 + X + 40*Y) = 166 THEN DX _ -DX:
GOTO 80
105 IF PEEK<1024 + X + 40*Y) = 166 THEN DY = -DY:
GOTO 100
1)*1000), 166
Lines 21 to 27 put 10 blocks on the screen in random positions. Lines
85 and 105 check (PEEK) to see if the ball is about to bounce into a
block, and changes the ball's direction if so.
0
e
•
0
66
Page 77
HAP
C
T
ER
SPRITE
• Introduction to Sprites
• Sprite Creation
• Additional
• Binary Arithmetic
GRAPHICS
Notes on Sprite
67
Page 78
INTRODUCTION TO SPRITES
In previous chapters dealing with graphics
symbols could be used in PRINT statements to create animation and add
chartlike appearances to our displays.
we saw that graphic
,
A yygy was also shown to POKE character codes in specific screen
memory locations
This would then place the appropriate characters di-
.
rectly on the screen in the right spot.
Creating animation in both these cases requires a lot of work because
objects must be created from existing graphic symbols. Moving the object requires a number of program statements to keep track of the ob-
And, because of the limitation of using
ject and move it to a new spot
the shape and resolution of the object might not be as
graphic symbols
,
.
good as required.
Using sprites in animated sequences eliminates a lot of these problems. A sprite is a high-resolution programmable object that can be
made into just about any shape
can be easily moved around the screen by simply telling the computer
the position the sprite should be moved to. The computer takes care of
the rest.
And sprites have much more power than just that. Their color can be
changed
made to go in front and behind another
panded in size, just for starters.
The penalty for all this is minimal. However
knowing some more details about how the Commodore 64 operates and
how numbers are handled within the computer
sounds
sprites do amazing things in no time.
;
you can tell if one object collides with another
.
though
,
Just follow the examples and you'll be making your own
through BASIC commands. The object
-
;
they can be
;
and they can be easily ex-
,
using sprites requires
.
It's not as difficult as it
AM
IML
r
SPRITE CREATION
Sprites are controlled by a separate picture-maker in the Commodore
64. This picture maker handles the video display. It does all the hard
work of creating and keeping track of characters and graphics, creating
colors, and moving around.
This display circuit has 46 different "ON/OFF" locations which act like
internal memory locations. Each of these locations breaks down into a
series of 8 blocks. And each block can either be "on" or "off". We'll get
into more detail about this later. By POKEing the appropriate decimal
value in the proper memory location you can control the formation and
movement of your sprite creations.
68
Page 79
In addition to accessing many of the picture making locations we will
also be using some of the Commodore 64's main memory to store infor-
mation (data) that defines the sprites. Finally, eight memory locations
directly after the screen memory will be used to tell the computer exactly
which memory area each sprite will get its data from.
As we go through some examples, the process will be very
straightforward, and you'll get the hang of it.
So let's get on with creating some sprite graphics. A sprite object is 24
dots wide by 21 dots long. Up to eight sprites can be controlled at a
time. Sprites are displayed in a special independent 320 dot wide by
200 dot high area. However, you can use your sprite with any mode,
high-resolution, low-resolution, text etc.
Say you want to create a balloon and have it float around the sky.
The balloon could be designed as in the 24 by 21 grid on page 70.
The next step is to convert the graphic design into data the computer
can use. Get a piece of notebook or graph paper and set up a sample
grid that is 21 spaces down and 24 spaces across. Across the top write
128,64,32,16,8,4,2,1, three times (as shown) for each of the 24
squares. Number down the left side of the grid 1-21 for each row. Write
the word DATA at the end of each row. Now fill in the grid with any
design or use the balloon that we have. It's easiest to outline the shape
first and then go back and fill in the grid.
Now if you think of all the squares you filled in as "on" then substitute
a 1 for each filled square. For the one's that aren't filled in, they're "off"
so put a zero.
Starting on the first row, you need to convert the dots into three sepa-
rate pieces of data the computer can read. Each set of 8 squares is
equal to one piece of data called a byte in our balloon. Working from
the left, the first 8 squares are blank, or 0, so the value for that series of
numbers is 0.
The middle series looks like this (again a 1 indicates a dot, 0 is a
space):
128 64 32 16 8 4 2 1
The third series on the first row also contains blanks, so it, too, equals
zero. Thus, the data for the first line is:
0
1
11
T T T T T T T T
0 + 64 + 32
+
16 + 8 + 4 + 2 + 1 = 127
DATA 0, 127, 0
69
1
11
1
Page 80
SERIES SERIES SERIES
1 2 3
128 32 8 2 128 32 8 2 128 32 8 2
64 16 4 11 64 16 4 11 64 16 4 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
5
10 15
COLUMN
20
a
0
24
The series that make up row two are calculated like this:
Series 1:
Series 2: 1 1
0 0
0 0 0 0
1 1 1 1 1
T T T T T T T T
+
4 + 2 + 1 = 255
0
0
0 0
Series 3:
128 + 64 + 32 + 16 + 8
1
1
00
I
T T
128 + 64
For row 2, the data would be:
DATA
In the same way, the three
would be converted into their decimal value. Take the time to do the
remainder of the conversion in this example.
Now that you have the data for your object, how can it be put to use?
Type in the following program and see what happens.
series
1,255,192
that make up each remaining row
1
1 = 1
1
= 192
•
•
70
Page 81
1 REM
5 PRINT
UP, UP, AND RWRY!
"{CLRIHOME}"
10 V= 53248 : REM START OF DISPLAY CHIP
11 POKE V+21.
12 POKE 2042,13 : REM
4 : REM
ENABLE SPRITE 2
SPRITE 2 DATA FROM 13TH BLK
20 FOR N = 0 TO 62: READ Q : POKE 832+N,Q: NEXT
30 FOR
40 POKE
X = 0 TO
V+4,X: REM UPDATE X COORDINATES
200 GETS ITS INFO. FROM DATA'
50 POKE V+5,X: REM UPDATE Y COORDINATES
60 NEXT X
70 GOTO 30_ INFO.
200 DATA
0,127,0,1,255,192,3,255,224,3,231,224
READ IN FROM Q-
210 DATA 7,217,240,7,223,240,7,217,240,3,231,224
220 DATA
230 DATA
240 DATA 0,62,0,0
FOR MORE DETAIL ON READ & DATA SEE CHAPTER 8.
3,255,224,3,255,224,2,255,160,1,127,64
1,62,64,
0,156,128,0,156,128,0
,
62,0,0,62,0,0,28,0
,73,0,0,73,0
If you typed everything correctly, your balloon is smoothly flying
across the sky (page 72).
In order to understand what happened, first you need to know what
picture making locations control the functions you need. These locations,
called registers,could be illustrated in this manner:
Register(s) Description
0 X coordinate of sprite 0
1 Y coordinate of sprite 0
2 - 15 Paired like 0 and 1 for sprites 1-7
16 Most Significant Bit-X Coordinate
21 Sprite appear:
29 Expand sprite in "X" Direction
1 =appear 0=disappear
23 Expand sprite in "Y" Direction
39 - 46 Sprite 0 - 7 color
In addition to this information you need to know from which 64 byte
section sprites will get their data (1 byte is not used).
This data is handled by 8 locations directly after screen memory:
2040
SPRITE 0
Now let
41 42 43 44
T T T T
1 2 3 4
'
s outline the exact procedure to get things moving
45 46 2047
T T T
5 6 7
write a program.
and finally
71
Page 82
r
ACTUAL SCREEN PHOTO
There are only a few things necessary to actually create and move an
object.
1. Make the proper sprite(s) appear on the screen by POKEing into location 21 a 1 for the bit which turns on the sprite.
2. Set sprite pointer (locations 2040-7) to where sprite data should be
read from.
3. POKE actual data into memory.
4. Through a loop, update X and Y coordinates to move sprite around.
5. You can, optionally, expand the object, change colors, or perform a
variety of special functions. Using location 29 to expand your sprite in
the "X" direction and location 23 in the "Y" direction.
There are only a few items in the program that might not be familiar
from the discussion so far.
In line 10;
V=53248
0
0
sets V to the starting memory location of the video chip. In this way we
just increase V by the memory number to get the actual memory location. The register numbers are the ones given on the sprite register map.
72
Page 83
In line 11,
POKE V+21,4
makes sprite 2 appear by placing a 4 in what is called the sprite enable
register (21) to turn on sprite 2. Think of it like this:
SPRITES
Decimal
values of each
sprite number
Sprite Level Number
=4
21
128 64 32 16
7
6
0
0
2 1
5
0
4
3
0
0
2
1
1
0
0
0
Put a I For The SPRITE
Each sprite level is represented in section 21 of the sprite memory and
4 happens to be sprite level 2. If you were
using
level 3 you would put a
1 in sprite 3 which has a value of 8. In fact if you used both sprites 2
and 3 you would put a 1 in both 4 and 8. You would then add the
numbers together just like you did with the DATA on your graph paper.
So, turning on sprites 2 and 3 would be represented as V+21,12.
In line 12;
POKE 2042,13
instructs the computer to get the data for sprite 2 (location 2042) from
the 13th area of memory. You know from making your sprite that it
takes up 63 sections of memory. You may not have realized it, but those
numbers you put across the top of your grid equal what is known as 3
bytes of the computer. In other words each collection of the following
numbers, 128,64,32,16,8,4,2,1 equals 1 byte of computer memory.
Therefore with the 21 rows of your grid times the 3 bytes of each row,
each sprite takes up 63 bytes of memory
READ Q: POKE 832+N
20 FOR N
=
0 to 62
:
. I WHOLE SPRITE
,
Q: NEXT
You Want
This line handles the actual sprite creation. The 63 bytes of data that
represent the sprite you created are READ in through the loop and
POKEd into the 13th block of memory. This starts at location 832.
30 FOR X = 0 TO 200
40 POKE V+41
50 POKE V+S,X
If you
remember
horizontal movement across the screen and the Y coordinate represents
the sprite's vertical movement across the screen. Therefore as the values
from school the X coordinate represents an objects
73
Page 84
of X change in line 30 from 0 to 200 (one number at a time) the sprite
moves across the screen DOWN and TO THE RIGHT one space for each
number. The numbers are READ by the computer fast enough to make
the movement appear to be continuous, instead of 1 step at a time. If
you need more details take a look at the register map in Appendix O.
When you get into moving multiple objects, it would be impossible for
one memory section to update the locations of all eight objects. Therefore each sprite has its own set of 2 memory sections to make it move on
the screen.
Line 70 starts the cycle over again, after one pass on the screen. The
remainder of the program is the data for the balloon. Sure looks different on the screen, doesn't it?
Now, try adding the following line:
POKE V+29,4: REM EXPAND
25 POKE V+23,4
and RUN the program again. The balloon has expanded to twice the
original size
sprite 2
X and Y direction.
It's important to note that the sprite will start in the upper left-hand
corner of the object. When expanding an object in either direction, the
starting point remains the same.
For some added excitement
! What we did
)
into memory sections 23 and 29
:
was simple. By POKEing 4 (again to indicate
sprite 2 was expanded in the
,
make the following changes:
,
P
D
5
11 POKE V+21,12
12 POKE 2042
30 FOR X
45 POKE V+6,X
55 POKE V
A second sprite
memory location that makes the sprite appear
sprites 3 and 2 on (00001100 = 12).
The added lines 45 and 55 move sprite 3 around by POKEing values
into sprite 3's X and Y coordinate locations
Want to fill the sky with even more action
tions:
11 POKE V+21,28-
12
POKE 2042,13:POKE 2043,13:POKE 2044,13
25
POKE V+23,12 : POKE V+29,12
48
POKE V+8,X
58
POKE V+9,100
,
13 : POKE 2043,13
=
1 to 190
+
7,190-X
number 3)has been turned
(
on by
(V+21). The
(V+6 and V+7).
?
Try making these addi-
28 IS REALLY
(SPRITE 3) + 16 (SPRITE 4)
74
4 (SPRITE 2) + 8
POKEing 12 into the
12 turns
Page 85
In line 11 this time, another sprite (4) was made to appear by POKEing 28 into the appropriate "on" location of the sprite memory section.
Now sprites 2-4 are on (00011100 = 28).
Line 12 indicates that sprite 4 will get its data from the same
memory area (13th 63 section area) as the other sprites by POKEing
2044,13.
In line 25, sprites 2 and 3 are expanded by POKEing 12 (Sprites 2
and 3 on) into the X and Y direction expanded memory locations (V+23
and V+29).
Line 48 moves sprite 3 along the X axis. Line 58 positions sprite 3
halfway down the screen, at location 100. Because this value does not
change, like it did before with X=0 to 200, sprite 3 just moves horizon-
tally.
ADDITIONAL NOTES ON SPRITES
Now that you've experimented with sprites, a few more words are in
order. First, you can change a sprite's color to any of the standard 16
color codes (0-15) that were used to change character color. These can
be found in Chapter 5 or in appendix G.
For example, to change sprite 1 to light green, type: POKE
V+40,13 (be sure to set V=53248).
You may have noticed in using the example sprite programs that
the object never moved to the right-hand edge of the screen. This was
because the screen is 320 dots wide and the X direction register can
only hold a value up to 255. How then can you get an object to move
across the entire screen?
There is a location on the memory map that has not been men-
tioned yet. Location 16 (of the map) controls something called the most
significant bit (MSB) of the sprite's X direction location. In effect, this
allows you to move the sprite to a horizontal spot between 256 and 320.
The MSB of X register works like this: after the sprite has been
moved to X location 255, place a value into memory location 16 repre-
senting the sprite you want to move. For example, to get 2 to move to
horizontal locations 256-320, POKE the value for sprite 2 which is (4) into
memory location 16:
POKE
Now start from 0 again in the usual X direction register for sprite 2
(which is in location 4 of the map). Since you are only moving another 64
spaces, X locations would only range between 0 and 63 this time.
V+ 16,4.
75
Page 86
This whole concept
is best illustrated with a version of the original
sprite 1 program:
10
V= 532
20
FOR N = 0 Ti] 62 : READ Q : POKE 832+N,Q
25
POKE V+5, 100
30
FOR X=0TO255
40
POKE V+4,X
50
NEXT
60
POKE V+16,4
70
FORX=0TO63
-48 : POKE V+211,4 : POKE 2042,13
80 POKE 'd+4 , X
90 NEXT
100 POKE V+16,0
110 GOTO 30
Line 60 sets the most significant bit for sprite 2. Line 70 starts moving
the standard X direction location, moving sprite 2 the rest of the way
across the screen.
Line 100 is important because it "turns off" the MSB so that the
sprite can start moving from the left edge of the screen again.
To define multiple sprites, you may need additional blocks for the
sprite data. You can use some of BASIC's RAM by moving BASIC. Before
typing or loading your program type:
5
NEXT
5
10
I
1k
W
0
0
POKE44,16:POKE 16*256,0:NEW
Now, you can use blocks 32 through 41 (locations 2048 through 4095) to
store sprite data.
BINARY ARITHMETIC
It is beyond the scope of this introductory manual to go into details of
how the computer handles numbers. We will, however, provide you with
a good base for understanding the process and get you started on
sophisticated animation.
But, before you get too involved we have to define a few terms:
BIT-This is the smallest amount of information a computer can store.
O
76
Page 87
Think of a BIT as a switch that is either "on" or "off". When a BIT is
"on" it has a value of 1; when a BIT is "off" it has a value of 0.
After BIT, the next level is BYTE.
-This is defined as a series of BITS. Since a BYTE is made up of
BYTE
8 BITS, you can actually have a total of 256 different combinations
of BITS. In other words, you can have all BITS "off" so your BYTE
will look like this:
128
0
64
32
0
0
16
0
8
0
4
0
2
0
and its value will be 0. All BITS "on" is:
128 64 32
1
which
1
1
is 128+64+32+16+8
16
1
+2+1=255.
8
1
4
1
2
1
The next step up is called a REGISTER.
REGISTER
-Defined as a block of BYTES strung together. But, in this
case each REGISTER is really only 1 BYTE long. A series of REGISTERS makes up a REGISTER MAP. REGISTER MAPS are charts like
the one you looked at to make your BALLOON SPRITE. Each REGISTER controls a different function, like turning on the SPRITE is really
called the ENABLE REGISTER. Making the SPRITE longer is the EX-
PAND X REGISTER, while making the SPRITE wider is the EXPAND Y
REGISTER. Keep in mind that a REGISTER is a BYTE that performs a
specific task.
0
s move on to the rest
Now let
'
BINARY TO DECIMAL CONVERSION
128 64 32 16 8 4 2 1
00
0
00
0
0
0
0
1
0
0
0
0
1
0
0
0
00
0
0
1
0
0
of BINARY ARITHMETIC.
Decimal Value
0
00
0
1
0
00
0
000
0
1
00
0
0
77
0
1
00
0
00
0
1
1
0
0
0
0
0
0
0
0
0
0
0
2T0
2 TI
2T2
2T3
2T4
2T5
2T6
217
Page 88
Using combinations of all eight bits, you can obtain any decimal value
from 0 to 255. Do you start
to see
why when we POKEd character or
S
0
color values into memory locations the values had to be in the 0-255
range? Each memory location can hold a byte of information.
Any possible combination of eight 0's and l's will convert to a
unique decimal value between 0-255. If all places contain a 1 then the
value of the byte equals 255. All zeros equal a byte value of zero;
"00000011" equals 3, and so on. This will be the basis for creating data
that represents sprites and manipulating them. As just one example, if
this byte grouping represented part of a sprite (0 is a space, 1 is a
colored area):
2t 2s
128+ 64
Then we would POKE 255 into the appropriate memory location to
represent
T1PF
need to
idea to
that part of the object.
To save you the trouble of converting binary numbers into decimal
do that a lot-the following program will do the work for you.
enter
25
+
32+ 16+ 8
and save the program for future use.
24
23
+ 4+ 2+ 1 + = 255
22
21
20
values-we'll
It's a good
S
0
s
s
5 REM BINARY TO DECIMAL CONVERTER
10 INPUT "ENTER 8-BIT BINARY NUMBER :":A$
12 IF LEN (A$) :: 8 THEN PRINT "8 BITS PLEASE...":
GOTO 10
15 TL = 0 : C = 0
20 FOR X = 8 to 1 STEP -1 : C = C_. + 1
30 TL = TL + VAL(MID$(A$,C,1))*2f(X-1)
40 NEXT X
50 PRINT A$;" BINARY = ";TL;" DECIMAL"
60 GOTO 10
This program takes your binary number, which
at each character of the string, from left to right (the MID$ function). The variable C
indicates
are dealing
example
power of 2. Since the first value is in the 2T7 place, in the example, TL would first
equal
zero.
This process is repeated for all eight characters as TL keeps track of the running
total decimal value of the
what character to work on as the program
The SAL function, in line 30, returns the actual value of the character. Since we
with numeric characters,
, if the first character of AS is 1 then the value would also be 1.
The final part of line 30 multiplies the value of the current character by the proper
1 times 128 or 128. If the bit is 0 then the value for that place would also be
binary number.
the value
was entered as a string
through the loop.
goes
is the same as the character. For
, and looks
78
0
S
Page 89
^,
CHAPTER
,.;
. 7
CREATING SOUND
• Using Sound
Programmer
• Structure of a Sound Program
• Sample Sound Program
• Making Music on Your Commodore 64
• Important Sound Settings
• Playing a Song on the Commodore 64
• Creating Sound Effects
• Sample Sound
if You'
Effects to Try
re Not a Computer-
79
Page 90
USING
SOUND
IF YOU
'RE NOT
A COMPUTER
"PROGRAMMER"
Most programmers use computer sound for two purposes: making
music and generating sound effects. Before getting into the "intricacies"
of programming sound, let's take a quick look at how a typical sound
program is structured . . . and give you a short sound program you can
experiment with.
STRUCTURE OF A SOUND PROGRAM
To begin with, there are five settings which you should know in order
to generate sound on your
COMMODORE 64: VOLUME
,
ATTACK/ DECAY, SUSTAIN/RELEASE(ADSR),
FREQUENCY/LOW FREQUENCY
ONCE at the beginning of your program. The high and low frequency
settings must be set for EACH NOTE you play. The waveform control
starts and stops each note.
WAVEFORM CONTROL and HIGH
. The first three settings are usually set
SAMPLE SOUND PROGRAM
Before you start you have to choose
Each voice requires different sound setting numbers for Waveform, etc.
You can play 1, 2 or 3 voices together but our sample
NUMBER
RETURN key after each line:
First clear sound chip. 5 FORL=54272TO54296:
1. Set VOLUME
2. Set ATTACK
define how fast a note rises to and
falls from its peak volume level (0
to 255).
3. Set
level to prolong note
release it.
4. Find
play in the
NOTES
the HIGH
FREQUENCY values for that note
each note requires 2 POKEs).
(
1. Type in this program line by line . . . be sure to hit the
at highest setting.
/DECAY
SUSTAIN/RELEASE
the note
in Appendix
-
FREQUENCY and LOW-
/tone you want to 40 POKE54273,17:POKE54272,37
TABLE OF MUSICAL
rates to 20 POKE54277,190
to define 30 POKE 54278,
and rate to ,b
M and enter
80
a VOICE
POKEL,O:NEXT
10 POKE54296,15
. There are 3 voices.
uses only VOICE
248 is
S
S
0
0
Page 91
5. Start
WAVEFORM
with one of 50 POKE54276,17
4 standard settings (17, 33, 65
or 129);.
6. Enter a time loop to set the
DURATION
played (a quarter note is approx.
"250" but may vary since a longer
program can affect the timing).
7. Turn off note.
of the note to be
60 FORT=1TO250:NEXT
70 POKE54276,16
To hear the note you just created
key. To view the program type the word LIST and hit
, type the word
RUN and then hit the
To change it, retype the lines you want to alter.
MAKING MUSIC ON YOUR COMMODORE 64
You don't have to be a musician to make music on your COMMODORE
64! All you need to know are a few simple numbers which tell your
computer how loud to set the volume, which notes to play, how long to
play them, etc. But first . . . here's a program which gives you a quick
demonstration
using only ONE of your computer's 3 separate
Type the word NEW and hit to
gram
then enter
,
key.
of the COMMODORE 64's incredible music capabilities,
voices.
erase
your previous pro-
this program, type the word RUN and hit the
L
5 REM MUSICAL SCALE 4 .-Title
7 FORL=54272TO54296:POKEL,O:NEXT
10 POKE 54296,15-E
20 POKE 54277,9 t
30 POKE 54276,17'
40 FORT=1TO30O
50 READ A'
NEXI<
:
of program.
Sets volume at highest setting (15).
Sets Attack/Decay
Sustain
/Release
Determines waveform
Duration(how long)each note plays.
Reads first number
81
level (each note)
(type of sound).
in line 110 DATA.
Page 92
60 READ B F
70 IFB=-1THENEND
80 POKE 54273,A: POKE54272,
B E--POKES the first number from DATA
85 POKE 54276,17 E -- Start note
90 FORT=1 TO250: NEXT: POKE54276, 16
FORT=1TO50:NEXTTime for release.
1
100 GOT020 E
0
0
110 DATA] 7,37,19,63,21,154,22,227
120 DATA 25,177,28,214,32,94,34,175
note
value
110 DATA.
in line 110 (A= 17)
HIGH and LOW
54273=HIGH
54272=
chart
in Appendix
"
C" of the 4th oc-
"
D" and so on.
,
and so on,
Reads second number in line
ENDS when it READS -1 in line 900.
as HIGH FREQUENCY and second number (B=37)
as LOW
FREQUENCY.Next time program loops
around it READS A as 19 and B as 63
and POKEs these numbers into the
FREQUENCY locations.The number
FREQUENCY for VOICEI and
QUENCY for VOICEI.
Let it ploy
Loops back to reset CONTROL and play new note.
Musical
note
Each pair of numbers represents one note. For
M.
example
tave, 19 and 63 represent
then stop note
values
from
17 and 37 represent
,
LOW FRE-
900 DATA-1,-1
When program
FREQUENCY
line 70.
-1 it turns off HIGH/LOW
reaches
settings and ENDS as instructed in
To change the sound to a "harpsichord," change Line 85 to read
POKE54276,33 and Line 90 to read FORT= 1T0250:NEXT: POKE54276,32
and RUN the program
again
. (To change the line, hit the
to stop the program, type the word LIST and hit
the program line you want to change
the new line will automatically
;
RUN/STOP
, then retype
replace the old one). What we did here is change the "waveform" from
a "triangular
WAVEFORM
MODORE 64 . . .
shaped sound wave to a "sawtooth" wave. Changing the
"
can drastically change the sound produced
by the COM-
but . . . waveform is only one of several settings you
can change to make different musical tones and sound effects! You can
also change
the ATTACK/DECAY
change from a "harpsichord
rate of each note . . . for example, to
sound to a more "banjo
"
"
sound try chang-
ing lines 20 and 30 to read:
O
key
e
S
20 POKE54277,3
30 POKE54278,0
82
Sets no
sustain
for banjo effect.
S
Page 93
As you'
different
ve just seen
,
you can make
musical instruments
your COMMODORE
.
Let's take a closer look at how each sound
setting works
IMPORTANT SOUND SETTINGS
64 sound like
1 VOLUME
POKE 54296,15. The volume
-To turn on the volume and set it to the highest level, type:
setting ranges
from 0 to 15 but you
most of the time. To turn "off" the volume, type:
POKE 54296,0
You only have to set the volume ONCE at the beginning of your program, since the some setting activates all three of the Commodore 64's
VOICES. (Changing the volume during
can produce interesting results but is beyond the scope of this introduction.)
2. ADSR and WAVEFORM CONTROL SETTING
how changing the waveform can change the sound effect from
"xylophone" to "harpsichord" Each VOICE has its own WAVEFORM
CONTROL SETTING which lets you define four different types of
waveforms: Triangle, Sawtooth, Pulse (Square
TROL also activates the COMMODORE 64's ADSR feature, but we'll
come back to this
in a moment.A sample
like this:
POKE 54276,17
a musical
) and Noise
waveform start
note or sound effect
-You've already seen
'll use 15
. The CON-
setting looks
where the first number (54276) represents the control setting for VOICE 1
and the second number (
waveform.
The settings
are shown in the table below.
ADSR AND WAVEFORM CONTROL SETTINGS
VOICE 1
VOICE 2
VOICE 3
Although the
waveform settings are the same for each type of waveform
17) represents
the start
for a triangular
for each VOICE and WAVEFORM combination
CONTROL
REGISTER
54276
54283
54290
TRIANGLE SAWTOOTH PULSE
17/16
17/16
17/16
Note Start
33/
32
33/
32
33/
32
/
Stop Numbers
65/64
65/64
65/64
control registers are different for each voice the
83
NOISE
129/128
129/128
129/128
.
To see how
Page 94
this works, look at
Lines
85 and 90 in the
musical scale
program. In this
program, immediately
after setting
the frequency in Line 80, we set the
CONTROL SETTING for VOICE 1 in Line 85 by POKEing 54276,17. This
turned on the CONTROL for VOICE 1 and set it to a TRIANGLE
WAVEFORM (17). In Line 70 we POKE 54276,16, stopping the note. Later, we changed the waveform start setting from 17 to 33
SAWTOOTH WAVEFORM and this
gave the scale a
"harpsichord" effect.
to create a
See how the CONTROL SETTING and WAVEFORM interact? Setting the
waveform is similar to setting the volume, except each voice has its own
setting
and instead
of POKEing volume
levels
Next, we'll look at another aspect of sound . . . the ADSR
3. ATTACK/DECAY SETTING
CONTROL SETTING not only defines the waveform but it also activates
the ADSR
, or ATTACK/
DECAY/SUSTAIN
MODORE 64. We'll begin by looking at the ATTACK/DECAY setting. The
following chart shows the various ATTACK and DECAY levels for each
voice. If you're not familiar with the concepts of sound attack and de-
cay, you might think of "attack" as the rate at which a note/sound arises
to its MAXIMUM VOLUME. The DECAY is the rate at which the note/
sound falls from its highest volume level back to the SUSTAIN level. The
following chart shows the ATTACK/DECAY setting for each voice, and the
numbers for each attack and decay setting. Note that YOU MUST
COMBINE ATTACK AND DECAY SETTINGS BY ADDING THEM UP AND
ENTERING THE TOTAL. For example, you can set a HIGH ATTACK rate
and a LOW DECAY rate by adding the high attack number (64) to the
low decay number (1). The total (65) will tell the computer to set the high
attack rate and low decay
by adding them together (128 + 64 + 32 + 16 = MAX. ATTACK RATE
of 240).
If you set an attack rate with no decay, the decay is automatically
zero, and vice-versa. For example, if you POKE 54277,64 you set a
medium attack rate with zero decay for VOICE 1. If you POKE 54277,66
you set a medium attack rate and a low decay rate (because 66=64+2
and sets BOTH settings). You can also add up several attack values, or
several decay values. For example, you can add a low attack (32) and a
84
•
Page 95
medium attack (64) for a combined attack rate of 96, then add a
medium decay of 4 and . . . presto . . . POKE 54277,100.
At this point, a sample program will better illustrate the effect. Type
the word NEW, hit HIT M
-L and type in this program and RUN it:
,
5 FOR
10 PRINT
L=54272TO54296:
"HIT ANY KEY" --
POKE L
0:NEXT(
20 POKE54296,15 <
30 POKE54277,64 <
40 P0KE54273
60 GETK$
:
70 POKE54276,17:FORT
80 POKE54276,16:FORT
17:P0KE54272,37 E
,
IFK$=""THEN60 <-
=1 T0200:NEXT
=1T050:NEXT <
90 GOTO2O E
Here, we're using VOICE 1 to create one note at a time
MEDIUM ATTACK
ATTACK/
rate
DECAY setting with the number 64 activates a MEDIUM attack
.
The result sounds like someone bouncing a ball in an oil drum.
Now for the fun part. Hit the
type the word LIST and hit
RATE and
ZERO DECAY.
®r
^ID
. Now type this line and hit
The key is Line 40. POKEing the
key to stop the program, then
(the new line 40 automatically replaces the old line 40):
40 POKE 54277,190
Duration the note plays.
Screen message.
Set volume at highest level.
Set Attack/Decay.
Poke one note
Check the keyboard.
Set Vhvefonn control (
Turn off settings.
Loop back
into VOICE 1.
triangle).
and do it again.
. . .
with a
Type the word RUN and hit to see how it sounds. What we've
done here is combine several attack and decay settings. The settings
are: HIGH ATTACK
+
HIGH DECAY (8) + MEDIUM DECAY
128) + LOW ATTACK(32) + LOWEST ATTACK (16)
(
(
4) + LOW DECAY
(2) = 190.
This effect sounds like a sound an oboe or other "reedy" instrument
might make. If you'd like to experiment, try changing the waveform and
attack/decay numbers in the musical scale example to see how an
"oboe" sounds. Thus . . . you can see that changing the attack/decay
rates can be used to create different types of sound effects.
4. SUSTAIN/RELEASE SETTING
-Like Attack/Decay, the SUSTAIN/
RELEASE setting is activated by the ADSR/WAVEFORM Control. SUSTAIN/
RELEASE lets you "extend" (SUSTAIN) a portion of a particular sound, like
the "sustain pedal" on a piano or organ which lets you prolong a note.
Any note or sound can be sustained at any one of 16 levels. The
SUSTAIN/RELEASE Setting may be used with a FOR . . . NEXT loop to
85
Page 96
determine how long the note will be held at SUSTAIN volume before
being released. The following chart shows the numbers you have to
POKE to reach different SUSTAIN/RELEASE,
SUSTAIN/
SUSTAIN
/
RELEASE HIGH MEDIUM LOW LOWEST HIGH MED
CONTROL SETTING SUSTAIN SUSTAIN SUSTAIN SUSTAIN RELEASE RELEASE RELEASE RELEASE
VOICE 1
VOICE 2
VOICE 3
54278
54285
54292
128
128
128
RELEASE
64
6432
6432
32
RATE SETTINGS
16
16
16
rates.
.
LOW LOWEST
8
8
8
4
4
4
2
2
2
As an example, if you're using VOICEI, you can set a HIGH SUSTAIN
LEVEL by typing: POKE 54278,128 or you could combine a HIGH SUSTAIN
LEVEL with a LOW RELEASE RATE by adding 128 + 2 and then POKE
54278,130. Here's the same sample program we used in the ATTACK/
DECAY section above . . . with a SUSTAIN/RELEASE feature added.
Notice the difference in sounds.
Set VAoveform control (
Torn off settings.
Loop back and do it again.
Release
triangle).
In Line 30, we tell the computer to SUSTAIN the note at a HIGH SUS-
TAIN LEVEL (128 from chart above) . . . after which the tone is released
in Line 80. You can vary the duration of a note by changing the "count"
in Line 70. To see the effect of using the release function try changing
Line 30 to POKE54278,89 (SUSTAIN = 80, RELEASE = 9).
5. CHOOSING VOICES AND SETTING HIGH/LOW FREQUENCY
SOUND VALUES
-Each individual note on the Commodore 64 requires
TWO SEPARATE POKE COMMANDS . . . one for HIGH FREQUENCY and
one for LOW FREQUENCY. The MUSICAL NOTE VALUE table in Appendix
M shows you the corresponding POKES you need to play any note in the
86
Page 97
Commodore 64's eight octave range. The HIGH and LOW FREQUENCY
POKE COMMANDS are different for each VOICE you use-this allows
you to program all 3 voices independently to create 3-voice music or
exotic sound effects.
The HIGH and LOW FREQUENCY POKE COMMANDS for each voice
are shown in the chart below, which also contains the NOTE VALUES for
the middle (fifth) octave.
VOICE NUMBER POKE
& FREQUENCY NUMBER
VOICEI/HIGH
VOICEI/LOW
VOICE2/HIGH
VOICE2/LOW
54273
54272
54280
54279
VOICE3/HIGH 54287
VOICE3/LOW 54286
C
C#
34
36 38 40 43
7585
34
75
34
75
126
36
126
85
36 38
85 126
3
SAMPLE MUSICAL NOTES
D
D#
E
F
45
200
440
200
40
200
198 127
52
43
45
52
198
43 45
52 198
127
127
F#
48
48
48
51
97,
51
97
51
97
-FIFTH OCTAVE
G G#A
54 57
172 126
111
5457
172
111
54
57
172
111
A#
61
64
188 149 169
61
6468
126
188 149 169
61
6468
126 188 149
As you can see, there are 2 settings for each voice, a HIGH FRE-
QUENCY setting and a LOW FREQUENCY setting. To play a musical note,
you must POKE a value into the HIGH FREQUENCY location and POKE
another value into the LOW FREQUENCY location. Using the settings in
our VOICE/FREQUENCY/NOTE VALUE table, here's the setting that plays
a C note from the 5th octave (VOICED:
POKE 54273,34:POKE 54272,75.
The same note on VOICE2 would be:
POKE 54280,34:POKE 54279,75.
B
C
C#
72
68
72
72
169
Used in a program, it looks like this:
5 FORL=54272TO54296:POKE L,O:NEXT
10 V=54296:W=54276:A=54277:
S=54278:H =54273:L=54272
20 POKEV,15:POKEA,190:POKES,
30 POKEH,34:POKEL,75
40 POKEW,33:FORT=1TO200:NEXTstart note, let it play
50 POKEW,32 ( stop note
87
89
^- POKE
Set numbers equal to letters.
,
.oIume
woveform
, ottac
k/de<ay.
POKE hi/lo freq. notes
Page 98
PLAYING A
The following program can be used to compose or play a song (using
VOICEI). There are two important lessons in this program: First, note
how we abbreviate all the long control numbers in the first line of the
program ... after that, we can use the letter W for "Waveform" instead
of the number 54276.
SONG ON THE COMMODORE 64
The second
lesson
concerns the way we use the DATA. This program
is set up to let you enter 3 numbers for each note: the HIGH FREQUENCY
NOTE VALUE, the LOW FREQUENCY NOTE VALUE, and the DURATION
THE NOTE WILL BE PLAYED.
For this song, we used a duration "count" of 125 for an eighth note,
250 for a quarter note, 375 for a dotted quarter note, 500 for a half
note and 1000 for a whole note. These number values can be increased
or decreased to match a particular tempo, or your own musical taste.
To see how a song gets entered, look at Line 100. We entered 34
and 75 as our HIGH and LOW FREQUENCY settings to play a "C" note
(from the sample scale shown previously) and then the number 250 for a
quarter note. So the first note in our song is a quarter note C. The
second note is also a quarter note, this time the note is "E" ... and so
on to the end of our tune. You can enter almost any song this way,
adding as many DATA statement lines as you need. You can continue the
note and duration numbers from one line to the next but each
begin with the word DATA. DATA-1,-1,-1 should be the last line in your
program. This line "ends" the song.
Type the word NEW to erase your previous program and type in the
Unlike music, sound effects are more often tied to a specific programming "action" such as the explosion made by an astro-fighter as it
crashes through a barrier in a space game . . . or the warning buzzer in
a business program that tells the user he's about to erase his disk by
mistake.
You have a wide range of options available if you want to create
different sound effects. Here are 10 programming ideas which might
help you get started experimenting with sound effects-
1. Change the volume while a note is playing, for example to create
an "echo" effect.
2. Vary between two notes rapidly to create a sound "tremor."
3. Waveform . . . try different settings for each voice.
4. Attack/Decay . . . to alter the rate a sound rises toward its "peak"
volume and rate it diminishes from that peak.
5. Sustain/
and rate it diminishes from that volume.
6. Multivoice effects . . . playing more than one voice at the same
time, each voice independently controlled, or one voice playing
longer or shorter than another, or serving as an "echo" or response
to a first note.
7. Changing notes on the scale, or changing octaves, using the values
in the MUSICAL NOTE VALUE table.
8. Use the Square Waveform and different Pulse Settings to create
different effects.
9. Use the Noise Waveform to generate "white noise" for accenting
tonal sound effects or creating explosions, gunshots or footsteps.
The same musical notes that create music can also be used with the
Noise Waveform to create different types of white noise.
10. Combine several HIGH/LOW frequencies in rapid succession across
different octaves.
11. Filter . . . try the extra POKE setting in Appendix M.
,
75,250
,
,
97,500
Release
,
57,172,250
,
0,0,125
51,97,500
,
,
43,52
. . . to change sustain to volume of a sound effect,
250,51,97,375
43,52
,
EFFECTS
D
,
250,51
43,52
,
,
97,250
,
125,51,97
,57,172
89
Page 100
I
SAMPLE SOUND EFFECTS TO TRY
The following programs may be added to almost any BASIC program.
They are included to give you some programming ideas and demonstrate the Commodore 64's sound effect range.
Notice the programming shortcut we're using in Line 10. We can
abbreviate those long cumbersome sound setting numbers by
them as easy
that these easy to remember LETTERS can be used instead of those long
numbers
Frequency
letters instead of numbers in our program
shorter
spot.
,
-
to-use letters
.
typing faster
, V = Volume, W=Waveform, A=Attack/Decay,
Here
(VOICE]), and L=
,
and the sound settings easier to remember and
numeric variables). Line 10 simply means
(
Low Frequency
(VOICE]
. . .
). We then use these
making our program
DOLL CRYING
10 V=54296:W=54276:A=54277: H=54273:L=54272
20 POKEV,15:POKEW,65:POKEA,15
30 FORX=20OTO5STEP
40 FORX
50 POKEW,O
=
150TO5STEP-2:POKEH
-
2:POKE H
,
40:POKE L,X:NEXT
,
40:POKEL
,
X:NEXT
defining
H=High
S
•
SHOOTING SOUND
USING VOICE1
,
...
VOLUME
V=54296:W=54276:A=54277:H =54273:L=54272
10
FORX= I5TOOSTEP-1:POKEV,X:POKEW,129:POKEA,
20
15:POKEH,40:POKEL,200:NEXT
POKEW,O:POKEA,0
30
90
NOISE WAVEFORM,FADING
S
S
•
0
01
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.