Академический Документы
Профессиональный Документы
Культура Документы
COMMODORE 64
OMNIBUS
PETER LUPTON & FRAZER ROBINSON
CENTURY COMMUNICATIONS
LONDON
NOTE:
CONTENTS
Acknowledgements
Introduction
PART 1
INTRODUCING COMMODORE 64 PROGRAMMING
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
3
8
18
34
52
59
73
82
89
93
119
146
158
184
204
PART 2
ADVANCED COMMODORE 64 PROGRAMMING
17
18
19
20
21
22
23
24
25
26
211
234
262
280
301
315
331
348
364
403
APPENDICES
1
2
3
4
5
6
7
8
9
10
11
12
Abbreviations
BASIC Commands
BASIC Tokens and Abbreviations
Summary of DOS Commands
BASIC Error Messages
DOS Error Messages
Speeding U p Programs
Colour Codes
Musical Notation
N umbering Systems
Kernal Routines
The 6510 Instruction Set
419
420
431
434
435
441
446
448
449
453
458
464
13
14
15
16
17
18
Index
SID Registers
VlC TI Registers
Screen Memory Maps
Character Codes
System Memory Map
Graphics Loader Pro grams
472
474
476
478
483
484
497
ACKNOWLEDGEMENTS
It is customary at this point to mention all those
people without whose help this book would not
have been possible. We would like to
acknowledge the support and enthusiasm of Peter
Rhodes without whom none of our books would
have been possible. We haven't been able to say
so until now - Thanks Peter!
INTRODUCTION
This book is an omnibus edi tion of our two books,
PART 1
INTRODUCING COMMODORE 64
PROGRAMMING
CHAPTER 1
CHAPTER 2
SETTING UP YOUR 64
Before you can use your Commodore 64 you must
connect it to apower supply and to a television. To
"
Cassette
Interface
User
Port
\
\
Cartridge
Socket
Tuning
Screw
TV
Lead
Audio &
Monitor
Senal
Interface
~,.~
L/
Joystick
Sockets
On/Off
Switch
Power
Socket
POWER
The 64 needs a low voltage DC supply, and this is
obtained from the power supply unit supplied with
your computer. Plug this power supply into the
mains, and plug the output lead into the computer.
The socket for the power supply is at the righthand side ofthe machine. Do not switch on yet!
DISPLAY
The Commodore 64 uses a standard domestic TV to
communicate with you, and for this almost any TV
will do. To get the best results, use a modern, good
quality colour TV. Ifyou use a black and white set,
the colour displays produced by the computer will
appear as shades of grey.
To connect the 64 to the TV, plug the supplied
aerial lead into the aerial socket of the TV, and
plug the other end into the socket at the back ofthe
computer (check the diagram). The lead has a
different type ofplug at each end, so take care that
you don't try to force in the wrong one.
CASSETTE RECORDER
The Commodore 64, like most other small
computers, uses cassette tapes to save programs or
information, so that you don't have to type them in
every time you need them.
You cannot use an ordinary cassette recorder wi th
the 64: you must use the special Commodore
Setting up your 64
DISKDRIVE
Pro grams can also be stored on floppy disks. A
floppy disk drive is faster than a cassette unit and
is more flexible in use. (It is also much more
expensive). The disk drive connects to the round
serial interface socket at the rear of the computer.
The disk unit requires its own power supply and so
must also be plugged into the mains.
PRINTER
If you have a printer, its lead plugs into the serial
interface socket at the rear of the computer. If you
are also using a disk drive you should plug the
printer lead into the spare socket at the rear of the
disk drive. The printer also has aseparate mains
lead.
SWITCHING ON
When you have connected everything together, you
are ready to switch on. The equipment must be
switched on in the right order, or there is a risk of
damaging the computer.
First swi tch on the TV.
Second, switch on the disk drive and then the
printer. (Remember that you should never switch a
disk unit on or offwith disks inside it.)
Last, switch on the computer itself.
TUNING
To get a display to appear on the TV screen, tune to
channel 36, or, on a pushbutton set, use aspare
channel and keep tuning until you see this appear
on the screen:
Setting up yauf 64
CHAPTER 3
FIRST STEPS
Before you can make use of your Commodore 64,
you must find out how to communicate with it.
Communication is a two-way process: you must
give the 64 instructions, and you must be able to
find out how it responds to them. You give
instructions using the keyboard, and the 64
displays its response on the TV screen.
Type the following phrase on the keyboard:
PRINT "CBM 64"
First Steps
11
10
First Steps
11
NUMBERS
As weIl as printing words, the Commodore 64 can
also handle numbers. Try:
PRINT 5
(RETURN)
Subtraction:
PRINT 7-4
Multiplication:
PRINT 3*5
Division:
PRINT 15/6
Powers (exponentiation):
PRINT 3 i 2
12
Second
Third
Fourth
2 * 2 +
2 + 2 *
6 + 3 /
3 t 2 +
(3 + 4)
2 * 2
2 + 2
5- 4
4 t 3 /
* 2
COLOUR
The colour of the screen display and its border can
be changed to be any one of 16 colours, as can the
colour of the characters displayed on the screen.
To change the colour of the characters, hold down
the CTRL key and press one of the numeric keys . the cursor will change to the colour written on the
front ofthe key. Now type some characters - these
will appear in the same colour as the cursor. You
can change the colour of the characters at any time
First Steps
13
14
RETURNING TO NORMAL
If at any time you want to return to the dark
blue/light blue screenlborder combination, simply
hold down the RUN/STOP key and press the
RESTORE key.
TEXT
There are two other things you can do which alter
the appearance ofthe display.
When you first switch on and type, all the letters on
the screen are displayed as capitals, or upper case.
The 64 can also display text in lower case.
PRINT "THIS IS A TEST"
First Steps
15
REVERSE MODE
Up to now, all our characters have been in light
blue on a dark blue background. The 64 allows you
to display reversed text. To try this hold down the
CTRL key and press the 9 key (note that the words
RVS ON are written on the front of the key). From
now on, anything you type on that line appears
reversed, with dark characters on a light
background. To turn this off, hold down CTRL and
press the 0 key (RVS OFF) or press RETURN, since
reverse video is active only for one line at a time.
GRAPHICS CHARACTERS
You will notice that displayed on the front ofmany
of the keys are two symbols or graphie characters.
These can also be displayed on the screen by
holding down the CBM key or the SHIFT key
together with the appropriate key. Pressing a key
in conjunction with the CBM key will display the
left hand graphics character; using the SHIFT key
instead selects the right hand character.
MULTIPLE COMMANDS
You can put more than one instruction in one
command if you separate the instructions with
colons (:). Try this: (see also the note which follows
the examples)
PRINT "{CLS}":PRINT "3+4="3+4
:PRINT
Or this:
16
Note
In the two examples above, the characters within
the 'curly' brackets { } indicate which key to press
when typing in the listing - they should not be
typed, for example:
PRINT "{CLS}"
First Steps
17
SUMMARY
PRINT instructs the 64 to print something on the
screen.
PRINT "ABCDEFG"
CHAPTER 4
PROGRAMMING
At the end ofChapter 3, we saw that it is possible to
give the computer a number of instructions at one
time by wri ting them one after another on one line.
We will now look at a much more powerful way of
doing this - using a program.
A computer program is a numbered list of computer
instructions which are entered together and stored
by the computer to be obeyed later. Let's look at the
last example of Chapter 3 and see how we can turn
it into a program. In Chapter 3 we had:
PRINT"{CLS}":POKE 53280,8:POKE
53281,5:PRINT"{GRN}LURID COLOUR
SCHEME"
PRINT "{CLS}"
POKE 53280,8
POKE 53281,5
PRINT"{GRN}LURID COLOUR
SCHEMEl"
Programming
19
30-50
LIST
20
LIST - 30
LIST
30-
20
POKE 53281,2
Programming
21
CLOSE ENCOUNTERS
Here is a more interesting program for you to try it mayaIso be useful if you're in the habit of
attempting to communicate with aliens! Don't
worry if you don't understand all the instructions you will do when you've read the rest of the bookjust type it in, RUN it and wait for a te lose
Encounter'!
10
20
30
40
50
60
70
100
110
120
130
200
210
220
230
240
250
260
270
300
500
510
W=850:DIM NO(4,1)
DEF FNA(X)=INT(RND(1)*16)
FOR Z=O TO 4
FOR J=O TO 1
READ NO(Z,J)
NEXT J,Z
PRINT CHR$(147)
POKE 54296,15
POKE 54278,83
POKE 54277,245
POKE 54276,33
FOR L=O TO 15
GOSUB 500
FOR P=O TO 200:NEXT P
POKE 54276,32
FOR P=O TO 400:NEXT P
POKE 54276,33
W=W-50
NEXT L
GOSUB 500:GOTO 300
FOR Z=O TO 4
POKE 54273,NO(Z,0)
22
520
530
540
550
560
570
580
590
600
610
POKE 54272,NO{Z,1)
B=FNA{X)
S=FNA{X)
POKE 53280,B
POKE 53281,S
FOR p=o TO W:NEXT P
NEXT Z
RETURN
DATA 18,209,21,31,16
DATA195,8,97,12,143
PRINT"{CLS}"
PRINT "3+5 = " 3+5
Programming
23
HEIGHT
1__________
....
4 - - - - WIDTH - - - - - - - - . .
WIDTH = 8
HEIGHT = 12
AREA = WIDTH * HEIGHT
PRINT "AREA = 11 AREA
24
VARIABLE NAM ES
Any number of letters can be used in a variable
name, but only the first two characters of the name
are considered by the 64; the rest are ignored. This
me ans, for example, that ifyou use these names:
FR
FRUIT
FRIEDEGG
the computer will treat them as the same variable,
FR.
All variable names must begin with a letter, but
the other characters may be numbers, for example
Al, E7, LI. There must not be any spaces in a
variables name: this will confuse the computer.
NOTE
Programming
25
TYPES OF VARIABLE
There are three different types of variable, two of
which represent numbers, the third representing
words. The types which represent numbers are
Real and Integer variables; the type representing
words are called String variables because they
contain sequences or tstrings' of characters. The
variables in the previous program are real
variables.
REAL VARIABLES
These are used to represent numbers and can have
any value, whole numbers or fractions. Real
variables should be used in all arithmetical
programs. Real variables can have any value. For
example:
REAL = 3.72
SIZE = 87.3
2.5
INTEGER VARIABLES
These can only represent whole numbers (or
integers), not fractions. They may be used when
counting events or objects, or to store constant
integer values. For example, a program which kept
arecord ofthe number of chocolate bars in stock at
a shop could use integer variables to count them,
~ut the takings of the shop would have to be stored
In real variables, or fractions of pounds (Le.
pennies) would be ignored.
Integer variables must be identified by the % sign
after the variable name, as in CHOCBARS%. Here
are some examples:
26
NUMBER%
= 3+6
COUNT %
= COUNT% + 1
CHOCBARS% = 1000000
NUMBER% = 7.5
PRINT NUMBER%
STRING VARIABLES
Astring variable is used to store words or letters or
numbers. The variable name for astring variable
must be followed by a dollar sign ($) to distinguish
it from the other types ofvariable. Examples are:
NAME$
"WINSTON CHURCHILL"
STRING$ = "ABCDEFG"
NOTE
27
Programming
ARRAYS
Each of the three types of BASIC variable - real,
integer and string - may be used in the form of
what is called an Array. In normal use a variable
name represents one stored number or string. In
an array, the variable name represents a collection
of stored information, with one or more reference
numbers identifying the individual items. An
array can be pictured as a collection of boxes. Here
is a diagram of an array which uses one reference
number.
REFERENCE
NUMBER
CONTENTS
LUCY
JULIE
SHEILA
NICK
BRIAN
SUE
ArrayNAME$
NAME$(O)
NAME$(l)
NAME$(2)
NAME$(3)
NAME$(4)
=
=
=
=
=
"LUCY"
"JULIE"
"8HEILA"
"NICK"
"BRIAN"
28
60
100
110
120
NAME$(5) = "SUE"
FOR N =
TO 5
PRlNT N, NAME$(N)
NEXT N
OlM P$(2,2)
P$(O,O) = "FLORENCE"
P$(O,l) = "NlGHTlNGALE"
P$(l,O) = "GEORGE"
P$(l,l) = "WASHINGTON"
P$(2,0) = "lSAAC"
P$(2,1) = "NEWTON"
FOR A =
TO 2
FOR B =
TO 1
PRlNT P$(A,B): " " ,
NEXT B
PRlNT
NEXT A
29
Programming
B = 0
B = 1
A=O
FlORENCE
NIGHTINGALE
A = 1
GEORGE
WASHINGTON
A=2
ISAAC
NEWTON
Array P$
30
10
20
INPUT NUMBER
PRINT NUMBER
INPUT NAME$
PRINT NAME$
Programming
31
32
REMARKS
Line 10 of the last program is aremark or
comment statement. These are used to hold
comments, notes and titles to make the purpose of a
program and the way in which it works clear to
someone reading the listing. Remarks are
identified by the word REM before the remark and
have no effect when the program is RUN.
You should always put plenty of remarks in
programs, because although you may understand
how a program works when you write it, three
months later you may have forgotten. If you need
to modify a program at some time after wri ting i t,
remarks will make it much easier to remember
how the program works.
SUMMARY
PROGRAMS
A program is a numbered sequence of computer
instructions.
LIST
RUN
NEW
VARIABLES
Variables represent numbers and words In
programs.
There are three types ofvariable:
Programming
33
Real
Integer
String
INPUT
CHAPTER 5
PROGRAM CONTROL
In the last chapter we defined a program as a
numbered list of instructions to the computer,
which are obeyed in order from beginning to end.
In this chapter we will find out how to write
programs in which some instructions are executed
more than once. This makes programs more
efficient, as instructions which must be repeated
need to be written only once. We will also discover
that the computer can make decisions.
REPETITION
A section of program can be repeated many times
using the instructions FOR and NEXT. Try this
example:
10
20
30
FOR COUNT = 1 TO 10
PRINT COUNT
NEXT COUNT
Program Control
35
FOR COUNT
= 0 TO 30 STEP 3
30
NEXT
= X TO Y STEP Z
36
NESTED LOOPS
FOR ... NEXT loops can be nested - that is, one
loop can be contain one or more other loops. Here is
anexample:
10
20
30
40
50
FOR A = 1 TO 3
FOR B = 1 TO 4
PRINT A, B
NEXT B
NEXT A
r+
[1;
L
START
10 FOR A = 1 TO 5
20 FOR B = 1 TO 5
30 PRINT A, B
40 NEXT B
50 NEXT A
END
37
START
10 FOR A = 1 TO 5
20 FOR B = 1 TO 5
30 PRINT A, B
40 NEXT A
50 NEXT B
END
NEXT B, A
JUMPS
The comp uter can be instru cted to jump from one
progr am line to anoth er using the comm and
GOTO . Enter this progr am
10
20
GOTO 30
PRINT "LINE 20"
38
30
PAUSES
There are two ways to instruct the computer to
pause during the RUNning of a program.
For pauses, the duration ofwhich is not critical, we
can use a FOR ... NEXT loop which does nothing at
all except count its way through the steps. The
following pro gram is an example :
10
20
30
FOR 1= 0 TO 5000
NEXT I
PRINT "ABOUT 5 SECONDS"
TH E SYSTEM CLOCK
Whenever you switch on your 64, a built in clock
starts running, and will continue to run until you
switch off the machine. The clock continually
Program Control
39
PRINT "{CLS}"
PRINT "{HOM}" TI
GOTO 20
PRINT "{CLS}"
TI$ = "000000"
PRINT" {HOM}" TI$
GOTO 30
40
15
20
30
40
50
60
100
110
120
130
140
150
193
194
195
196
197
198
199
200
210
220
230
240
293
294
295
296
297
298
299
300
310
320
330
340
393
GOSUB 700
PRINT"{CLS}"
PRINT"{HOM}"
PRINT TAB(16)LEFT$(TI$,2)"."
MIO$(TI$,3,2)"."RIGHT$(TI$,2)
IF AS=O THEN 100
IF TI$=AT$ THEN POKE 53280,2:
POKE 54296,15
GET K$:IF K$="" THEN 30
IF K$="{Fl}" THEN 200
IF K$="{F3}" THEN 300
IF K$="{F5}" THEN 400
IF K$="{F7}" THEN 500
GOTO 30
REM
REM ************
REM *
*
REM * SET TIME *
REM *
*
REM ************
REM
INPUT "{CLS}{CO}{CO}{CO}{CO}
{CO} CURRENT TIME";CT$
IF CT$="" THEN 20
A$=CT$:GOSUB 1000
IF OK=l THEN TI$=CT$:GOTO 20
GOTO 200
REM
REM *************
REM *
*
REM * SET ALARM *
REM *
*
REM *************
REM
INPUT "{CLS}{CO}{CO}{CO}{CO}
{CO} ALARM TIME";AT$
IF AT$="" THEN 20
A$=AT$:GOSUB 1000
IF OK=l THEN AS=l:GOTO 20
GOTO 300
REM
Program Control
394
395
396
397
398
399
400
410
420
430
440
450
493
494
495
496
497
498
499
500
510
520
693
694
695
696
697
698
699
700
710
720
730
740
750
993
994
995
REM **************
REM *
*
REM * ALARM TIME *
REM *
*
REM **************
REM
IF AS=O THEN 30
IF AF=1 THEN 430
PRINT"{HOM}{CD}{CD}"TAB(28)
LEFT$(AT$,2)"."MID$(AT$,3,2)
"."RIGHT$(AT$,2}:GOTO 440
PRINT "{HOM}{CD){CD}"TAB(28)
11
":REM 10 SPACES
AF=I-AF
GOTO 30
REM
REM *************
REM *
*
REM * ALARM OFF *
REM *
*
REM *************
REM
POKE 53280,14:POKE 54296,0
AS=O
GOTO 430
REM
REM ***************
REM *
*
REM * ALARM NOISE *
REM *
*
REM ***************
REM
POKE 54278,83
POKE 54277,245
POKE 54276,33
POKE 54273,18
POKE 54272,209
RETURN
REM
REM ***************
REM *
*
41
42
996
997
998
999
1000
1010
1020
1030
1040
1050
1060
Program Description
Line 10 sets the foreground colour to red and the
screen colour to black.
Line 40 prints the current value of TI$ as hours,
minutes and seconds, separated by a full stop.
Lines 100 to 150 control the mode ofthe dock:
F1 allows you to set the current time using lines
200 to 240.
Program Control
43
FOR X = 1 TO 10
PRINT X
IF X = 5 THEN PRINT "X = 5"
FOR P = 0 TO 2000
NEXT P
NEXT X
= 10
44
>= Y
<= Y
1 OR X
IF A
3 AND NAME$
"CBM 64"
Program Control
20
30
40
50
60
45
10
20
30
40
50
60
70
100
110
120
130
200
210
220
230
240
250
260
270
280
300
310
320
330
REM
SORTING PROGRAM
DIM NUM(10),S(10)
REM INPUT 10 NUMBERS
PRINT "{CLS}"
FOR N=l TO 10
INPUT"ENTER A NUMBER";NUM(N)
NEXT N
REM COPY NUMBERS TO ARRAY S
FOR C=l TO 10
S(C) = NUM(C)
NEXT C
REM SORT NUMBERS
COUNT = 0
FOR N=l TO 9
IF S(N+1) >= S(N) THEN 280
TEMP = S(N+1)
S(N+1) = S(N)
S(N) = TEMP
COUNT = COUNT + 1
NEXT N
IF COUNT <> 0 THEN 210
FOR Z=l TO 10
PRINT NUM(Z),S(Z)
NEXT
46
SUBRQUTINES
A subroutine is a section of program which is
executed at a number of different times in the
course of a program, but is written only once. The
computer is diverted from the main pro gram to the
subroutine, and returns to the main program at the
point from which it left.
The command GOSUB followed by a line number
or a variable diverts the computer to the
Program Control
47
GOSUB 110
C = A+B
GOSUB 210
END
REM SUBROUTINE TO INPUT A & B
INPUT "A, B"; A, B
RETURN
REM SUBROUTINE TO DISPLAY
RESULT
PRINT "{CLS}"
PRINT
PRINT A "+" B " = " C
RETURN
48
Program Control
49
results in
GOTO 100
if N
GOTO 200
if N
GOTO 300
if N
Similarly:
=
=
=
1
2
3
50
ON P GOSUB 500,200,500,400,100
SUMMARY
-LOOPS
The loop structure has the form:
FOR V = A TO B STEP C ... NEXT in which
the instructions between the FO Rand NEXT
commands are repeated once for each value ofV
indicated by the FOR ... STEP command. A, B
and C may be variables or numbers; V must be a
variable. Loops may be nested one within
another.
DECISIONS
IF (condition) THEN (instruction) decides
between two actions. The condi tion after IF is
usually a test of a variable. There may be more
than one instruction after THEN, in which ca se
colons (:) must be used to separate them. The
complete IF ... THEN command must be in one
line.
GOTO need not be included if it would be the only
command after THEN, but it must be if there is
another command before i t:
100 IF ACE
hut
1 THEN 20
Program Control
51
SUBROUTINES
Diversions from a program, using GOSUB and
RETURN. GOSUB calls the subroutine,
RETURN at the end of the subroutine returns the
computer to the instruction after the GOSUB.
ON ... GOSUB ON ... GOTO
Selects adestination from a list:
ON N GOTO
A, B, C
CHAPTER 6
GET
The G ET command can be used in two ways:
Firstly, it can be used to check which key, if any, is
being pressed as the program runs through, as in
this example:
10
20
30
GET A$
PRINT A$
GOTO 10
53
GET A$
IF A$ = "" THEN 10
PRINT A$
GOTO 10
54
10
20
30
40
50
60
1000
1010
1020
1030
1040
1050
55
"e",
"D"
56
"0";
"E";
"F"
will display:
ABCOEF
"CBM 64"
57
TAB(N)
The TAB command instructs the 64 to move the
cursor N spaces from the left side of the screen
before printing. For example,
PRINT TAB(17) "CBM 64"
58
PRINT "{CLS}"
PRINT "TOP LINE"
CD$ = "{10 * CD}"
CU$ = "{12 * CU}"
FOR DELAY = 0 TO 3000:NEXT
PRINT CD$"MIDDLE LINE"
FOR DELAY = 0 TO 3000:NEXT
PRINT CU$ "BACK TO THE TOP!"
SUMMARY
GET is used to assign a keystroke value from the
keyboard to a variable. G ET does not normally
wait for a key to be pressed be fore continuing, and
it is usual to include programming to ensure that it
does.
READ and DAT Aare used to copy large amounts
of information into variables without writing lots
of program lines.
PRINTING
There are a number of special commands which
allow control of the format of data displayed on the
screen.
CHAPTER 7
PIECES OF STRINGS
String variables are used to store sequences 'strings' - of characters. There are a number of
BASIC commands which are used to manipulate
strings, and these are described in this chapter.
We saw in Chapter 4 that astring variable can
store a sequence of characters. For example:
NAME $
= "WILLIAM SHAKESPEARE"
GAME $
=" SPACE INVADERS"
= "ABC123D"
REFERENCE$
A$ = "FLORENCE"
B$ = "NIGHTINGALE"
SPACE$ = " "
60
40
50
NAME$ = A$ + SPACE$ + B$
PRINT NAME$
CUnlNG STRINGS
There are three BASIC functions which are used to
extract information from strings. The functions
are:
LEFT$
RIGHT$
and
MID$
S$ = "UVWXYZ"
N=3
Pieces of Strings
30
40
61
R$ = RIGHT$ (S$,N)
PRINT R$
displays 'XYZ'.
The third function, MID$, is a li ttle more" complex,
as we have to specify not only the string and the
number of characters to be read, but also where in
the string the characters are to be found.
PRINT MID$ ("12345678",3,4)
displays 'DEFGHIJK'.
62
C = 65
A$ = CHR$(C)
PRINT A$
B = ASC("B")
PRINT B
B$ = "B"
PRINT ASC(B$)
63
displays '65'.
FIGURES IN STRINGS
Astring can contain any characters - including
numerals. There are two functions which can be
used to make strings of number characters from
actual numerals and to find the numeric value of
the number characters in astring.
.
The function STR$ creates from a number astring
containing the characters ofthat number:
10
20
A$ = STR$(12345)
PRINT A$
A$ = "123456"
A = VAL(A$)
PRINT A
64
PRINT VAL("123ABC45")
displays '123'.
The signs + and - may appear before the number
characters. They will be treated by V AL as the
sign ofthe number.
TESTING STRINGS
Strings can be compared with each other,in the
same way as numbers, using IF ... THEN. A
routine like this one can be used to check that an
entry is suitable for the program:
10
20
30
PRINT"{CLS}"
PRINT:PRINT"SELECT OPTION"
PRINT:PRINT"S SCREEN COLOUR"
PRINT:PRINT"B BORDER COLOUR"
PRINT:PRINT"K KEY REPEAT"
GET K$:IF K$="" THEN 60
Pieces of Strings
70
80
90
100
1000
1010
1020
1030
2000
2010
65
Warning
Be careful when using STR$ and making
comparisons. If you compare "123"
with
STR$ (123) you will find that the 64 thinks they
are not the same. This is because any number
which is shown as a result of using STR$ is
preceded by aspace (or a minus sign if negative).
This seems to be a feature shared by all
Commodore machines. However, you can get round
the problem by using MID$ to remove the first
character of the string:
A$ = MIO$(STR$(123),2)
66
PRINT "{CLS}"
DIM W$(20)
C = O:L = 0
INPUT "ENTER A WORD";W$(C)
IF W$(C) = "ZZZ" OR C = 20 THEN
C = C-l:PRINT"{CLS}": GOSUB
1000:GOTO 100
C = C+l:GOTO 40
REM SORT STRINGS
S = O:L = 20
FOR Z=O TO C
IF W$(Z+lW$(Z) THEN 170
TEMP$ = W$(Z+l)
W$(Z+l) = W$(Z)
W$(Z) = TEMP$
S = S+l
NEXT Z
IF S <> 0 THEN 100
PRINT "{CLS}"
GOSUB 1000:END
REM DISPLAY NUMBERS
PRINT"{CLS}"
FR P = 0 TO C
PRINT TAB(L)W$(P)
NEXT P
RETURN
Pieces of Strings
67
9
10
20
30
40
50
60
70
80
90
93
94
95
96
97
98
99
100
110
REM ***********
REM *
*
REM * HANGMAN *
REM *
*
REM ***********
REM
POKE 53280,5:POKE53281,13:
PRlNT"{GR1}"
PRINT"{CLS}{CO}" TAB(15)"{RVS}
HANGMAN {ROF}"
OlM 0$(30)
G=l:W=O:CF=O:W$=""
FOR Z=OT030
REAO O$(Z):NEXT Z
FOR z=o TO 9
REAO D(Z),A(Z):NEXTZ
REM
REM ****************
REM *
*
REM * DRAW GALLOWS *
REM *
*
REM ****************
REM
Gl$=CHR$(176)+CHR$(195)+CHR$
(195)+CHR$(174)
G2$=CHR$(221)
68
120
G3$=CHR$(183)+CHR$(183)+CHR$
(183)+CHR$(183)+CHR$(183)+"{CU)
130
140
150
160
183
184
185
186
187
188
189
190
200
210
220
230
240
PRINT"{CD}{CD}{CD}{CD}"
PRINT TAB(6)Gl$
FOR z=o T05:PRINTTAB(6)G2$:NEXT
PRINT TAB(4)G3$
REM
REM ********************
REM *
*
REM * RANDOM SELECTION *
REM *
*
REM ********************
REM
N = INT(RND(1)*31)
C$ = D$(N)
L = LEN(C$)
FOR Z=l TO L
PRINT TAB(14)"- "i:NEXT
PRINT"{HOM}{CD}"TAB(30) "GUESS
243
244
245
246
247
248
249
250
255
260
265
270
275
280
REM
REM ********************
REM *
*
REM * INPUT YOUR GUESS *
REM *
*
REM ********************
REM
GET G$:IF G$=""THEN 250
IF G$<"A" OR G$>"Z" THEN 250
FOR Z=l TO LEN(U$)
IF G$=MID$(U$,Z,l) THEN 250
NEXT Z
G=G+l:U$=U$+G$:GOSUB 400
IF W=10 THEN M$="{RED} YOU
LOSE!!":GOSUB 360:GOTO 310
IF C=L THEN M$="{RED} YOU
WIN!!" : GOTO 310
GOTO 240
PRINT" {HOM} {CD}"M$
290
300
310
"
" iG
Pieces of Strings
320
69
PRINT"{HOM}{18 * CD}"TAB(ll)"
ANOTHER GO (Y/N)?"
330 GET A$: IF A$='"'THEN 330
340 IF A$="Y" THEN RUN
350 PRINT "{CLS}":END
360 PRINT" {HOM} {14 * CD} "TAB (14 );
370 FOR Z=l TO L
380 PRINT MID$(C$,Z,l)" "j:NEXT:
RETURN
393 REM
394 REM ***************
*
395 REM *
396 REM * CHECK GUESS *
*
397 REM *
398 REM ***************
399 REM
400 FOR J=l TO L
410 IF MID$(C$,J,l)=G~THEN CF=l:
C=C+1:PRINT"{HOM}{14 * CD}"
TAB(12+2*J)G$
420 NEXT J
430 IF CF=l THEN 480
440 W$=W$+G$
450 PRINT"{HOM}{22 * CD}"TAB(5)W$
460 POKE A(W),D(W):POKEA(W)+54272,6
470 W = W+1
480 CF = O:RETURN
993 REM
994 REM ***********************
995 REM *
*
996 REM * DATA FOR DICTIONARY *
*
997 REM *
998 REM ***********************
999 REM
1000 DATA "BYTE","BASIC","BINARY",
"MEMORY","POKE","PEEK","DATA","
PRINT","LIST"
1010 DATA "BUFFER","SEQUENTIAL",
"REGISTER","CURSOR","SERIAL","P
ARALLEL"
70
Program Description
20 - 90
100 - 160
190 - 240
250 - 380
71
SUMMARY
Astring is a sequence of characters. These can be
stored in string variables, which are distinguished
by $ after the variable name.
LEN(string) gives the number of characters in a
string.
Strings can be added together:
PRINT "ABC" + "DEF" + "GHI"
displays (ABCDEFGHI'.
LEFT$, RIGHT$ and MID$ are used to separate
parts of strings.
LEFT$ (string,N) takes the first N characters
of the string
RIGHT$ (string,M) reads the
characters
last
72
displays tA'.
displays t65'.
A$ = STR$(123)
PRINT A$
displays t123'.
COMPARISONS.
Strings can be compared using IF ... THEN and the
relational operators =, < , and >. This is useful
for testing inputs and sorting strings.
CHAPTER 8
FUNCTIONS
SQUARE ROOTS
The square root of a number is calculated by the
function SQR(N). (The square root of a number or
variable N is the number which when multiplied
by itself, or squared, gives N.) Try:
PRINT SQR(4)
74
ABSOLUTE VALUES
The function ABS finds the absolute value of a
number: the value of the numerical part,
disregarding the sign. The function changes
negative numbers to positive numbers, but has no
effect on positive numbers.
PRINT ABS( 123.456)
INTEGER CONVERSION
The function INT removes the fractional part of a
number and returns the next lower whole number.
Try:
PRINT INT(123.4567)
Functions
75
displays (-3'.
SIGNS
SGN(N) returns a value whieh indieates the sign of
a number or variable N. Ifthe number is positive,
the result is 1; if the number is zero, the result is
zero; and if the number is less than zero, SG N
returns -1.
PRINT SGN (5)
PRINT SGN (0)
PRINT SGN (-7)
displays (1'
displays (0', and
displays (_1'.
TRIGONOMETRY
The trigonometrieal functions of sine, eosine,
tangent and aretangent are available In
Commodore BASIC. They are written:
SIN(N)
COS(N)
TAN(N)
ATN(N) where N is a number or a variable.
Note that the angles on whieh these functions
operate must be given in radians, not in degrees.
A radi an is the ratio of the length of an are of a
circle to the radius. In the diagram overleaf, the
angle A in radi ans is L/R. The circumference of a
circle is 21T times its radius (21TR), so it follows that
360 degrees is equivalent to 21T radians.
From this we can work out that 1 degree is 21T/360
radians (about 0.175), and 1 radi an is 360/21T
degrees (about 57.3). To eonvert from degrees to
76
....
L II "
I
,,
I
LOGARITHMS
Natural logarithms are provided by the function
LOG( ).
PRINT LOG (10)
displays '2.30258509'
PRINT LOG(5000)
displays '8.5171932'
Functions
77
RANDOM NUMBERS
The 64 has a function which provides random
numbers. The function is RND(X), which prints a
number between 0 and 1. The tseed' value - X can be any number or letter, but does directly
influence the value of the random number that
results. Try PRINTing RND(1) a few times. You
will get a different number each time.
The random numbers provided by RND are not
truly trandom' - it is very difficult and expensive to
make a machine which will give perfectly random
numbers. If you switch your 64 off and on, and
immediately type PRINT RND ( 1 ) , you will get the
same number every time (on our 64 it's
78
Functions
79
Dice Throwing
We can use random numbers in programs to
imitate the throwing of dice. Try this:
10
20
30
40
50
60
70
80
90
A = RND(-TI)
PRINT "{CLS}PRESS ANY KEY TO
THROW THE DICE"
GET K$:IF K$="" THEN 30
PRINT "{CLS}"
DICE = INT( (RND(0)*6) + 1)
D$ = "YOUR NUMBER IS" +
STR$(DICE)
PRINT D$
FOR DELAY = 0 TO 500:NEXT
GOTO 20
DEF FNA(N) = iT * Nt 2
PRINT "PROGRAM TO CALCULATE
AREAS OF CIRCLES"
PRINT
INPUT "ENTER THE RADIUS";R
PRINT: PRINT "AREA OF A CIRCLE
RADIUS "R" IS "FNA(R)
IF FNA(R) < 100000 THEN 30
80
SUMMARY
There are several built-in functions in Commodore
BASIC whieh operate on numbers or variables:
SQR(N) ealeulates the square root of a number N.
ABS(N) returns the absolute value of a number ehanging negative numbers into positive ones, and
leaving positive numbers unehanged.
INT(N) returns the integer value of N, removing
any fraetional part.
SGN(N) gives 1 ifN is positive, 0 ifN is zero and-1
ifN is negative.
The trigonometrie funetions:
SIN(N)
COS(N)
TAN(N)
ATN(N)
Functions
81
o and 1.
CHAPTER 9
LOGICAL THINKING
As well as doing ari thmetic, the Commodore 64 can
perform tests to compare numbers and strings. We
have already seen this when using IF .. THEN. In
this chapter we will examine in more detail the
way in which the 64 makes comparisons.
Consider the program line:
220
IF A = 3 THEN 500
A = 3
PRINT A
Logical Thinking
83
10 A = 5
(oranyothernumber)
the pro gram will print O.
Less than
>
Greater than
<>
Notequal
>=
<=
Try this:
10
20
PRINT A <= 7
If you try out the other tests, you will find they all
behave in the same way.
LOGIC
If two tests are combined, the same true and false
answers are still obtained:
10
20
A = 5
PRINT ( A < 7 ) AND ( A > 3 )
84
XANDY
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE
XORY
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
Logical Thinking
10
20
85
A = 3
PRINT NOT A>10
NOTX
TRUE
FALSE
FALSE
TRUE
B=1001
86
AANDB
Logical Thinking
87
T = 3-2*(P=Q)
= 00001010
A = A + 1
IF A > 15 THEN A = 0
= (A+1) AND 15
88
SUMMARY
The Commodore 64 can not only handle numerical
calculations: it can solve logical problems too.
Relational tests such as those performed after IF
are represented numerically:
True
False
OR
NOT
CHAPTER 10
MEMORY MANAGEMENT
The memory of a computer is buHt up from a large
n umber of storage uni ts, each of which can hold one
number. Each storage unit is called a memory
IDeation, and location has a unique reference
number called its address. In a microcomputer like
the Commodore 64, each memory location can hold
an eight-bit binary number, which can have a
value between 0 and 255. Larger numbers need
more than one location. (If you are not sure about
binary numbers, they are explained in Appendix
10.) Eight bits of storage are called a byte in
computer jargon, so the memory locations of the 64
hold one byte of da ta each.
The microprocessor used in the Commodore 64 (the
6510) is able to make use of 65535 memory
locations (65535 is 2 16 , or 64k). You will have
noticed that the message displayed when you
switch on the computer announces that 38911
bytes are free for use by BASIC programs. What
happens to the others?
The first lk of memory is used as a storage area by
the machine code pro grams that interpret your
BASIC pro grams and operate the machine. The
next 1000 bytes are used to store the display
picture - 1 byte for every character. A second
block of memory, locations 55296 to 56295, holds
the colour data for each character.
BASIC programs begin at loeation 2048, and all the
memory up to 40k is free for BASIC pro grams and
90
Memory Management
91
EMPTY SPACE
As you write longer programs, you will find that
even the 38k ofprogram space provided by the 64 is
not unlimi ted. If a pro gram uses large arrays for
data storage, the memory can be eaten up at an
alarming rate. The function FRE(X) tells you how
much memory is left unused.
If the amount free is greater than 32767, FRE(X)
92
CHAPTER 11
94
Time
Amplitude
Simple Wave
95
~~
Triangle Waveform
Sawtooth Waveform
1
1
Pulse Waveform
~F
~
Noise Waveform
96
v
o
I
u
m
...----.. ..
Attack
Decay
Sustain
Release
REM
REM
REM
POKE
POKE
REM
210
300
310
320
400
410
500
510
600
610
620
97
POKE 54296, 15
REM SET ATTACK/DECAY &
SUSTAIN/RELEASE
POKE 54277,54
POKE 54278,168
REM TURN ON THE SOUND WITH
SAWTOOTH WAVEFORM
POKE 54276, 33
REM WAIT A WEILE
FOR T = 1 TO 500 : NEXT T
REM SWITCH OFF SOUND
POKE 54276, 32
POKE 54296, 0
Property Controlled
54272
54279
54286
54273
54280
54287
54274
54281
54288
Pulse Waveform
Properties - Low Byte
54275
54282
54289
Pulse Waveform
Properties - High Byte
54276
54283
54290
54277
54284
54291
54278
54285
54292
98
Triangle
Sawtooth
Pulse
Noise
99
F = 250
F = F * 2t(1/12)
FOR T = 1 TO 50 : NEXT
IF F < 61800 THEN 105
WAVEFORMS
The pro gram uses the Sawtooth waveform which
gives a slightly buzzy tone. To hear the Triangle
waveform, modify the pro gram again by typing:
410
POKE 54276, 17
--....F
It is possible to alter the proportions of the wave by
altering the width of the peaks, to produce a
variety of alternative waveforms.
This is done by specifying the width ofthe peak as a
proportion ofthe whole cycle. The proportion is set
100
1
1
LJ
LJ
Pulse width 0
~F
I
Pulse Width = 2047 (50%)
101
VOLUME
The volume of the the sound of all three voices is
controlled by location 54296. The possible values
are from 0 to 15, with 0 giving no sound and 15
maximum volume. Reload the first program again
and type these lines:
210
220
1000
INPUT "VOLUME"; V
POKE 54296, V
GOTO 210
ENVELOPE
The final way of altering the sound of a voice
channel is by altering the Envelope, or volume
profile. This is done wi th the four parameters
Attack, Decay, Sustain and Release. For the last
time, reload the first program. Type in the
modifications below:
310
320
330
340
350
360
510
620
INPUT "ATTACK"; A
INPUT "DECAY"; D
INPUT "SUSTAIN"; S
INPUT "RELEASE"; R
POKE 54277, A AND 15) * 16)
OR (D AND 15)
POKE 54278, ( (S AND 15) * 16)
OR (R AND 15)
FOR T = 1 TO 500: NEXT T
GOTO 310
102
ATTACK
DECAY
RELEASE
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2 ms
8ms
16 ms
24 ms
38 ms
56 ms
68 ms
80 ms
100 ms
250 ms
500 ms
800 ms
1s
3s
5s
8s
6ms
24 ms
48 ms
72 ms
114 ms
168 ms
204ms
240 ms
300ms
750 ms
1.5 s
2.4s
3s
9s
15 s
24s
6ms
24ms
48ms
72 ms
114 ms
168 ms
204ms
240 ms
300 ms
750 ms
1.5 s
2.4s
3s
9s
15s
24s
103
FILTERING
In addition to the three voiees, the SID ehip has a
built-in filtering faeility whieh filters the output
from all three voiee ehannels together. The filter is
eontrolled by the four registers 54293 to 54296.
Three different filters may be used, either singly or
in eombination. The three are a High-pass filter,
100%
0%
Frequency
High-Pass Filter
Frequency
Low-Pass Filter
Frequency
Band-Pass Filter
104
I
~-----------------------------Frequency
100%
0%
~-------------~----------------------
Frequency
MEANING
VALUE
0-3
Volume
0-15
16
32
64
128
105
MEANING
VALUE
Filter Voice 1
Filter Voice 2
Filter Voice 3
4-7
Resonance
16 - 240
106
Pulse
128
64
TriSawtooth angle
32
16
Test
Ring
Mod
Sync
Gate
SOUND EFFECTS
The SID chip can not only be used for playing
music, but for producing all kinds of sound effects
and strange noises. By varying the frequency
while a note is playing, siren effects can be
achieved. It is possible to alter the waveform while
a note is playing by setting the appropriate bits of
the control register (remember not to clear the
GATE bit of the register, or the sound will be cut
om. The SID chip has extra facilities for producing
more complex sounds than those we have heard so
107
SYNC
The SYNC bit of the control register of each voice
synchronises the output of the voice with the
frequency of one of the other voices. The voices are
paired thus:
Voice 1 is synchronised wi th V oice 3
Voice 2 is synchronised wi th V oice 1
Voice 3 is synchronised wi th V oice 2
The SYNC function controls the waveform of the
voice by repeatedly restarting the wave at the
frequency of the controlling voice. If Voice 1 is
producing a triangle wave like this:
..
..
~./'.....
/'........
:~
~--------~
Voice 3 Frequency
108
INPUT"VOICE 1" ; VI
V3=35
FOR 8=54272 TO 54296: POKE
8,0: NEXT
POKE 54296, 15
POKE 54273, VI
POKE 54287, V3
POKE 54277, 84
POKE 54291, 84
POKE 54278, 168
POKE 54292, 168
POKE 54276, 19
FOR 8=1 TO 1000: NEXT
POKE 54276, 0
POKE 54290, 0
GOTO 10
109
RING MODULATION
If bit 3 of a voiee eontrol register is set, the output
ofthe voiee is ring modulated with the frequeney of
one of the other voiees, resulting in a nonharmonie mixture of frequencies. To use ring
modulation, the tri angle waveform must be used
for the voiee to be modulated. The properties of the
modulating voiee, other than its frequeney, have no
effeet. As for SYNC, the voiees pair thus:
MODULATED VOICE
MODULATING FREQUENCY
Voice 1
Voice 3
Voice 2
Voice 1
Voice 3
Voice 2
POTX
POTY
OSC 3
ENV3
110
POKE 54273, 35
POKE 54287, 3
POKE 54276, 33
POKE 54290, 64
POKE 54272, PEEK(54299)
GET K$: IF K$="" THEN 130
111
MAKING MUSIC
Now that we have dealt with the theory of
Commodore 64 sound generation, we will look at
the practical applications ofthe SID chip. The first
and most obvious use ofthe synthesiser chip is as a
synthesiser. The example program below turns
your 64 into an electronic organ, giving you full
control over the waveforms of the voices and the
filtering:
10
20
30
40
50
60
*******************
***
***
*** SYNTHESlSER ***
***
***
*******************
70
REM
REM
REM
REM
REM
REM
REM
990
992
994
996
REM *********************
REM * SET UP NOTE TABLE *
REM *********************
REM
1000
1010
1020
1030
1040
DlM N(255,1)
READ K
lF K = 0 THEN 1100
READ N(K,O), N(K,l)
GOTO 1010
112
1090
1092
1094
1096
1098
REM
REM ***********************
REM * CLEAR SIO REGISTERS *
REM ***********************
REM
1100
1110
1120
1130
1990
1992
1994
1996
1998
REM
REM **************
REM * FIRST MENU *
REM **************
REM
2000
2010
2020
2030
2100
2110
2120
2130
2140
2150
2160
113
2200
GOTO 2100
2490
2492
2494
2496
2498
REM
REM ************************
REM * SET VOICE PROPERTIES *
REM ************************
REM
2500
2510
2520
2530
2540
2550
2560
2600
2610
2620
2630
2640
2650
2700
2710
2720
2730
2740
2750
114
2760
2770
2780
2790
2800
2900
2910
2950
P = P(VV)*40.95
POKE SID+V+2, (P AND 255)
POKE SID+V+3, INT(P/256)
GOTO 2810
PRINT: PRINT TAB(5) W$(VV):
PRINT
PRINT "ATTACK (WAS"
A ( VV) " ) " ;: INPUT A ( VV): A =
(A(VV) AND 15)*16
PRINT "DECAY
(WAS"
D( VV) " ) " ;: INPUT D ( VV): D =
(D(VV) AND 15)
PRINT "SUSTAIN (WAS" S(VV)
")";: INPUT S(VV): S=(S(VV)
AND 15)*16
PRINT "RELEASE (WAS" R(VV)
")";: INPUT R(VV): R=(R AND
15)
POKE SID+V+5,A+D
POKE SID+V+6,S+R
PRINT"{CD}FILTER
{RVS}Y{ROF}/{RVS}N{ROF} (WAS "
F$(VV) " )";: INPUT F$(VV)
IF F$(VV)="Y" THEN RF=RF OR
(2t(VV- 1:GOT02910
IF F$ (VV) < > "N" THEN
PRINT"{CU}{CU}{CU}":GOTO 2870
RF=RF AND (255-2t(VV-1
POKE SID+23,RF
RETURN
2990
2992
2994
2996
2998
REM
REM *************************
REM * SET FILTER PROPERTIES *
REM *************************
REM
3000
2810
2820
2830
2840
2850
2860
2870
2880
2890
3010
115
3160
3170
3500
4990
4992
4994
4996
4998
REM
REM
REM
REM
REM
5000
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3150
5010
5020
********
* PLAY *
********
116
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5300
5310
5320
5400
5500
117
118
+-
Q WER
{HOME
,.
DEL
CHAPTER 12
CHARACTER GRAPHICS
The simplest way to produce graphics displays on
the Commodore 64 is to use the special characters
available from the keyboard. These characters can
be obtained by holding down either a SHIFT key
and pressing the character key to obtain the righthand symbol of the two shown on the face of the
key, or by holding down the Commodore logo key
and pressing the character key to obtain the lefthand symbol of the two. You can produce simple
but effective displays by using these symbols and
the cursor control characters in PRINT commands.
Try this short pro gram. Shifted characters are
represented by {X}, characters requiring the
Commodore key are shown as [X]. The program
should print a smiling face. The symbol ois used to
represent aspace:
10
20
30
40
50
60
120
Character Graphics
121
CODE
COLOUR
CODE
COLOUR
Black
Orange
White
Brown
Red
10
Light Red
Cyan
11
Dark Grey
Purpie
12
Mid Grey
Green
13
Light Green
Blue
14
Light Blue
Yellow
1S
Light Grey
CODEBREAKER
This program plays the weIl known game. You
have ten attempts to find out the secret code which
the 64 generates, guided by the clues which the
pro gram displays after each attempt. Simple
character graphics are used to create the display.
***************
*
*
* CODEBREAKER *
*
*
***************
1
2
3
4
5
6
REM
REM
REM
REM
REM
REM
CM=55296: SM=1024
POKE 53280, 11: POKE 53281, 12
10
122
20
30
35
40
41
45
50
55
60
65
70
75
85
90
100
110
115
120
130
140
993
Character Graphics
123
994
995
996
997
998
999
REM
REM
REM
REM
REM
REM
1000
1010
1020
1050
C$ = ""
FOR I=l TO 4
C$ = C$ + RIGHT$( STR$( INT(
RND(0)*8)+1),1)
NEXT I
PRINT "{CLS}{CD}" TAB(14)
"{BLU}CODEBREAKER{GRY}":
PRINT: PRINT
RETURN
1993
1994
1995
1996
1997
1998
1999
REM
REM
REM
REM
REM
REM
REM
2000
2060
2080
2090
2092
2093
2094
2095
2096
REM
REM
REM
REM
REM
1030
1040
2020
2030
2040
2050
************
*
*
* SET CODE *
*
*
************
***************
*
*
* INPUT GUESS *
*
*
***************
*******************
*
*
* RIGHT COLOUR
*
* AND RIGHT PLACE *
124
2097
2098
2099
REM *
*
REM *******************
REM
2100
2110
2150
FOR 1=1 TO 4
IF MID$(G$,I,l) <>
MID$(CODE$,I,l) THEN 2150
Cl = Cl+l
G$ = LEFT$(G$,I-l) + "9" +
MID$(G$,I+l)
CO$ = LEFT$(CO$,I-l) + "9 11 +
MID$(CO$,I+l)
NEXT I
2192
2193
2194
2195
2196
2197
2198
2199
REM
REM *******************
REM *
*
REM * RIGHT COLOUR
*
REM * BUT WRONG PLACE *
REM *
*
REM *******************
REM
2200
2210
2220
2260
2270
FOR 1=1 TO 4
FOR J=l TO 4
IF MID$(G$,I,l) =MID$(CO$,J,l)
AND MID$(G$,I,1)<>"9" THEN
C2=C2+1: GOTO 2240
GOTO 2260
G$ = LEFT$(G$,I-l) + "9" +
MID$(G$,I+l)
CO$ = LEFT$(CO$,J-l) + "9" +
MID$(CO$,J+l)
NEXT J
NEXT I
2293
2294
2295
2296
2297
REM
REM ****************
REM *
*
REM * DISPLAY CLUE *
REM *
*
2120
2130
2140
2230
2240
2250
Character Graphics
125
2298
2299
REM ****************
REM
2300
2310
2340
2350
2360
2393
2394
2395
2396
2397
2398
2399
REM
REM
REM
REM
REM
REM
REM
2400
Z$ = n{RVS}{RED}
YOU WIN
{ROF}n
PRINT n THE CODE WAS n;
FOR I=l TO 4
POKE CM+974+(I*2),
VAL(MID$(C$,I,l-l
POKE SM+974+(I*2), 160
NEXT
PRINT TAB(27) nANOTHER Y/N?n;
PRINT n{HOM}{CD}n TAB(13) Z$;
GETA$: IF A$=nn THEN 2470
IF A$=ny n THEN 100
PRINT n{CLs}n: END
2320
2330
2420
2430
2440
2445
2450
2460
2465
2470
2480
2490
***************
*
*
* REVEAL CODE *
*
*
***************
126
CHARACTER DEFINITIONS
The shapes of all the displayable characters are
defined by numbers held in a special area of
memory. The 64 allows you to create new symbols
by entering new definitions for the characters to
replace those held in memory. Each symbol in the
character set is defined by the contents of eight
memory locations, which correspond to a grid of
eight rows; each row consisting of eight dots which
Character Graphics
127
2
Memory
Location:
3
4
5
6
7
Bit:
543
+ Start of set
128
+ 53248 = 53440
Character Graphics
50
60
70
100
110
120
129
NEXT A
POKE 1, PEEK(l) OR 4
POKE 56334, PEEK(56334) OR 1
FOR B=O TO 7
PRINT C(B)
NEXT B
o
The Character X
REDEFINING CHARACTERS
You may be wondering how it is possible to create
new characters when the standard character
definitions are held in ROM - it's difficult to write
130
Character Graphics
BANK
MEMORY RANGE
0-16383
16384 - 32767
32768 - 49151
49152 - 65535
131
100
110
120
130
140
150
160
132
BANKO
BANK 1
BANK2
BANK 3
16384
32768
49152
2048
18432
34816
51200
4096
20480
36864
53248
6144
22528
38912
55296
8192
24576
40960
57344
10
10240
26624
43008
59392
12
12288
28672
45056
61440
14
14336
30720
47104
63488
170
180
190
NEXT X
POKE 1, PEEK(l) OR 4
POKE 56334, PEEK(56334) OR 1
Character Graphics
133
END
REM INVERT CHARACTERS
FOR X = 14336 TO 16376 STEP 8
FOR Y = 0 TO 7
C(Y)=PEEK(X+Y)
NEXT Y
FOR Z=O TO 7
POKE X+7-Z, C(Z)
NEXT Z
NEXT X
134
BANKO
BANK 1
BANK2
BANK3
16384
32768
49152
16
1024
17408
33792
50176
32
2048
18432
34816
51200
48
3072
19456
35840
52224
64
4096
20480
36864
53248
80
5120
21504
37888
54272
96
6144
22528
38912
55296
112
7168
23552
39936
56320
128
8192
24576
40960
57344
144
9216
25600
41984
58368
160
10240
26624
43008
59392
176
11264
27648
44032
60416
192
12288
28672
45056
61440
208
13312
29696
46080
62464
224
14336
30720
47104
63488
240
15360
31744
48128
64512
Character Graphics
135
REM
REM
REM
REM
REM
100
110
120
190
192
194
REM ***********************
REM * REDEFINE CHARACTERS *
REM ***********************
200
210
220
230
240
295
300
310
395
400
410
420
500
510
520
530
***********************
*
*
* CHARACTER GENERATOR *
*
*
***********************
136
540
550
CC = SC+X+40*(R+7)+10
GOTO 500
990
992
994
996
REM
REM
REM
REM
1000
1010
1020
1030
1040
1045
1050
1060
1070
1080
1090
1100
N = PEEK( SX+R-l )
X=8
A%(R,9) = N: REM VALUE OF ROW
REM STORE BIT PATTERN
IF N/2 = INT(N/2) THEN
A%(R,X)=O: GOTO 1050
A%(R,X) = 1
N = INT(N/2): X=X-l
IF N >=1 THEN 1040
FOR I=X TO 1 STEP -1
A% (R, I) = 0
NEXT I
RETURN
1990
1992
1994
REM *********************
REM * DISPLAY CHARACTER *
REM *********************
2000
2003
2005
PRINT "{CLS}";
FOR 1=55627 TO 55907 STEP 40
FOR 11=0 TO 7: POKE 1+11, 1:
NEXT: NEXT
FOR R=l TO 8
FOR X=l TO 8
IF A%(R,X)=l THEN CS = 160:
GOTO 2040
CS=46
POKE SC+(X+I0+(R+7)*40), CS
NEXT X
POKE 214, R+6: PRINT: PRINT
TAB(24) STR$(A%(R,9
NEXT R
2010
2020
2030
2035
2040
2050
2060
2070
************************
* STORE CHARACTER DATA *
*
IN ARRAY
*
************************
Character Graphics
137
2080
RETURN
2990
2992
2994
2996
3000
3010
3020
3030
3040
3050
3060
3080
3090
3100
3110
3120
3130
3140
3150
3160
3190
3192
3194
138
3200
3210
3290
3292
3294
3300
3310
3320
3330
3990
3992
3994
4000
4020
4030
4040
4050
4060
4070
4080
4100
M$ = "REPROGRAMMING CHARACTER"
+ STR$ (C)
POKE 214,2 1: PRIN T: PRINT
TAB( 5) M$
FOR R=1 TO 8
FOR X=1 TO 8
o = 0+(2 t(8-X * A%(R ,X)
NEXT X
POKE SX+R -l, 0
0=0: NEXT R
EX=1
RETURN
9990
9992
9994
REM *************
REM * MAIN MENU *
REM **** **** **** *
4010
Character Graphics
139
REM **********************
REM * LOAD CHARACTER SET *
REM *
FROM TAPE
*
REM **********************
140
12020
12030
12040
12100
12500
C$ = CHR$(PEEK(28672+C;
PRlNT#l, C$
NEXT C
CLOSE 1
RETURN
Character Graphics
141
142
143
Character Graphics
o
1
2
3
Memory
Location:
4
5
6
7
Bit:
654
144
0-63
64 -127
53281
53282
128-191
00
01
10
192 - 255
11
53284
53283
Character Graphics
10
20
30
50
60
70
80
145
CHAPTER 13
* 200
147
POKE 53272,PEEK(53272) OR 8
POKE 53265,PEEK(53265) OR 32
FOR Z=8192 TO 16192: POKE Z,O:
NEXT
148
I4
FOREGROUND COLOUR
121
BACKGROUND COLOUR
2f 1
+2f2+2f4=
149
22
H-t-+-+-+-+-+-++++-_..... ............................... 2
I-I-+-+-+++++-+-Hf--- ...................................... . o
I-I-+-+-+++++-+-Hr--" ....................................... .
I-I-+-+-+++++-+-Hr--' ........................................ . o
............................................. P
H-t-+-+-+-+++-t-H-............................................. I
I-+-t-+-+++++-++-+_ ............................................ .
I-+-r-+-+-++++-+-+-I-' ........................................... . X
I-+-t-+-+++++-++-+_ ........................................... .. E
I-+-r-+-+-++++-+-+-I--" ........................................ .. L
H-t-+-+++++-t-H- ........................................... ..
I-+-+-+-+++++-++-+r-' .......................................... .. S
V
E
R
T
I
A
L
~~~~--~~--------~ Y
150
8
9
10
16
17
18
24
25
26
312
313
314
315
316
317
318
319
632
633
634
635
636
637
638
639
151
POKE 53272,PEEK(53272) OR 8
POKE 53265,PEEK(53265) OR 32
FOR I = 8192 TO 16192:POKE
I,O:NEXT
FOR I = 1024 TO 2023:POKE
I,22:NEXT
FOR J = 0 TO 200
X = INT(RND(1)*320)
Y = INT(RND(1)*200)
152
80
90
100
999
GOSUB 1000
NEXT J
GOTO 100
REM DISPLAY PIXEL
1000
1010
1020
1030
1040
1050
1060
50
60
70
80
Y = 100
FOR X =0 TO 319
GOSUB 1000
NEXT X
X = 100
FOR Y = 0 TO 199
GOSUB 1000
NEXT Y
R = 50
60
70
80
90
100
110
153
X = 0.75 * R * SIN(I)+160
R = 50
FOR J = 0 TO 500
R = R - 0.1
X = 160 + R*SIN(J/32*n)
Y = 100 + R*COS(J/32*n)
GOSUB 1000
NEXT J
GOTO 120
POKE 53272,PEEK(53272) OR 8
154
20
30
35
40
50
60
70
80
100
110
120
130
140
150
POKE 53265,PEEK(53265) OR 32
FOR I = 8192 TO 16191
POKE I,O:NEXT I
FOR I = 1024 TO 2023
POKE I,13+X
IF I/40=INT(I/40) THEN X = X+16
IF X=256 THEN X=O
NEXT I
FOR Y=4 TO 124 STEP 8
FOR X=O TO 39
BYTE = 8192+INT(Y/8)*320+X*8
POKE BYTE,255
NEXT X,Y
GOTO 150
155
156
1000
1010
1020
1030
1040
1050
157
b)
c)
d)
00
BACKROUND REGISTER 0
01
10
11
CHAPTER 14
SPRITES
Sprites, or Moveable Object Blocks as they are
sometimes known, are a special breed of user
defined characters - that is they operate on similar
principles, but are endowed with a few extra
facilities which make them a flexible and simple
way of creating moving high resolution displays.
The CBM-64 allows you to use up to 8 sprites at a
time, each ofwhich can be in anyone of16 colours.
One of the reasons sprites are so useful is that you
can move them around the screen very easily,
simply by giving them an X and Y coordinate based
on the 320 by 200 pixel bit mapped display and the
VIC TI chip does all the rest. This makes moving
graphics in BASIC very easy to create.
Sprites
159
160
BYTE 0
BYTE 1
BYTE 2
BYTE 3
BYTE 4
BYTE 5
BYTE 60
BYTE 61
BYTE 62
o
o
1
3
7
15
31
31
63
63
63
63
63
254
255
255
255
255
126
255
247
227
247
255
255
254
252
248
248
252
o
o
128
192
224
o
o
128
192
224
192
128
o
o
o
o
o
o
31
31
15
7
3
Sprites
255
255
126
192
128
161
o
o
162
Sprites
163
164
SPRITE COLOURS.
Each sprite can be displayed in any of 16 possible
colours and the colour is controlled by yet another
register. For Sprite 0, the SPRITE COLOUR
REGISTER is at location 53287. Try POKEing
this location with a few values (0-15) and watch
the colours change. Notice that the screen
background shows through those parts of the sprite
where no pixels are defined.
The following pro gram will display all 8 sprites in
different colours and move them around the screen.
Notice that all sprites are the same design because
every sprite data pointer is set to point at the same
data by lines 70 and 80.
A FLOCK OF SPRITES
10
20
30
40
50
60
70
80
90
100
PRINT /I{CLS}/I
R = 53248: X = 100:Y = 100
FOR I=O TO 63
READ D
POKE 832+I,D
NEXT I
FOR I=2040 TO 2047
POKE I,13:NEXT I
POKE 53269,255
FOR N=O TO 15 STEP 2
Sprites
110
120
130
140
150
160
170
180
1000
1010
1020
1030
1040
1050
1060
1070
1080
165
X=X+(RND(1)*20)-(RND(1)*20)
Y=Y+(RND(1)*20)-(RND(1)*20)
IF X>255 OR X<O THEN X=100
IF Y>249 OR Y<30 THEN Y=100
POKE 53248+N,X
POKE 53249+N,Y
NEXT N
GOTO 100
DATA 0,0,0,0,126,0,1,255,128
DATA 3,247,192,7,227,224,15
DATA 247,192,31,255,128,31
DATA 255,0,63,254,0,63,252,0
DATA 63,248,0,63,248,0,63,252
DATA 0,63,254,0,31,255,0,31
DATA 255,128,15,255,192,7,255
DATA 224,3,255,192,1,255
DATA 128,0,126,0,0
SPRITE PRIORITIES
You will notice that when two sprites collide they
cross over each other, the lowest numbered sprite
appearing to cross IN FRONT of the other. This
sprite to sprite display priority is pre-determined,
the lowest numbered sprite will always cross on top
of the other.
It is possible to control the sprite to background
priority, so that sprites can be made to appear to
cross in front of or behind other screen data, by
using the SPRITE TO BACKGROUND REGISTER
at 53275. If the bit associated with a sprite in this
register is a 1, then it will cross 'behind' any
background data; if i t is 0 then the sprite has a
higher priority than the background data and will
pass in front of it. The following program
demonstrates this:
5
10
20
PRINT"{CLS}"
FOR I=O TO 63
READ D
166
30
40
50
60
70
75
80
POKE
POKE
POKE
POKE
POKE
POKE
POKE
100
300
W$=" {RED}{RVS}
{ROF}
{PUR}{RVS}
{ROF}
{GRN} {ROF}
{RVS}
{YEL} {ROF}
{RVS}"
PRINT"{HOM}{CD}{CD}{CD}{CD}"
FOR I=O TO 11:PRINT W$:NEXT
FOR C=l TO 345
XO=PEEK(53248)
X1=PEEK(53250)
IF XO=255 THEN XO=O: POKE
53264, 1
IF X1=0 THEN X1=255: POKE
53264, 0
XO=XO+1
X1=X1-1
POKE 53248,XO
POKE 53250,X1
IF XO/80=INT(XO/80) THEN POKE
53275,1-PEEK(53275)
NEXT C
1000
1010
1020
1030
1040
1050
DATA
DATA
DATA
DATA
DATA
DATA
110
120
200
210
220
230
240
250
260
270
280
290
832+I,D:NEXTI
2040,13:POKE 2041,14
53287,0:POKE 53288,1
53248,0:POKE 53249,140
53250,90:POKE 53251,140
53264,2
53269,3
24,24,24,24,60,24,24,126
24,12,255,48,15,255,240,3
255,192,7,255,224,15,255
240,24,126,24,48,126,12,96
126,6,255,255,255,255,255
255,255,255,255,255,255,
255
1060'DATA 127,255,254,56,60,28,28,
24
1070 DATA 56,14,0,112,28,0,56,56,0,
28,0
Sprites
167
EXPANDING SPRITES
There are two further registers associated with
sprites which allow you to expand them in the X
and Y directions. Each bit in these two registers
controls the size of one sprite in each direction - a 0
me ans the sprite is normal size in that direction; a
1 means it is twice the size. To expand asprite, use
the following statement:
X direction: POKE 53277, PEEK (53277) OR 2tN
Ydirection: POKE 53271,PEEK(53271} OR 2tN
where N is the number ofthe sprite.
To revert to normal size :
Xdirection: POKE 53277,PEEK(53277} AND
Ydirection:
255-2tN
POKE 53271,PEEK(53271}
255-2tN
AND
COLLISION DETECTION
Another useful feature of sprites is that you can
determine when one sprite comes into contact with
another sprite or with other data on the screen.
When a collision between sprites is detected, the
bits appropriate to those sprites in the SPRITE TO
SPRITE COLLISION REGISTER at location 53278
168
GOSUB 500
GET K$:IF K$="" THEN 160
IF K$="{CD}" THEN X=X-5
IF K$="{CR}" THEN X=X+5
IF X>250 THEN X=O:POKE 53264,1
:GOTO 150
IF x=o THEN X=255:POKE 53264,0
POKE 53248,X
OB=INT(RND(0)*4*DF)
OP=INT(RND(O)*WD)
IF OB=l THEN O$="{BLK}t{WHT}"
:GOTO 190
0$=""
PRINT TAB(10+I)"e"SPC(OP)0$
SPC(WD-OP)"e"
S=S+l:T=T+l
IF PEEK(53279)=1 THEN POKE
53287,11:H=H+l:GOT0230
POKE 53287,7
IF S<SS THEN 280
IF DI=O THEN 270
I=I-l:IF I=O THEN DI=l-DI
GOTO 280
I=I+l:IF I=BB THEN S=O:DI=l-DI
Sprites
280
290
300
310
320
330
340
350
360
500
510
520
530
540
550
560
570
580
590
600
610
620
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
169
170
Sprites
BIT PAIR
COLOUR
00
Screen Colour
01
10
11
171
172
SCREEN COLOUR
00
FACE
SPRITE COLOUR
10
EYES
REGISTER 0
01
LIPS
REGISTER 1
11
Sprites
EJ
EJ
Screen Colour:
173
Blue
Green
30
40
50
60
174
70
80
90
100
110
120
130
140
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
POKE 53248+I,50+(10*I)
NEXT I
FOR I=O TO 7
POKE 53287+I,5+I
NEXT I
POKE 53285,1
POKE 53286,2
POKE 53269,255
DATA 8,136,136,42,170
DATA 168,42,170,168,37
DATA 170,88,148,105,22
DATA 165,170,90,170,170
DATA 170,170,170,170,42
DATA 40,168,42,40,168
DATA 170,170,170,175,255
DATA 250,181,85,94,181
DATA 85,94,173,85,122,43
DATA 85,232,42,255,168
DATA 10,170,160,2,170
DATA 128,0,170,0,0,40,0,0
ANIMATION
Because of the way in which the VIC II chip is
directed to the start of a block of sprite data, simple
animation is just a matter of setting up several
blocks of data corresponding to the various stages
ofmovement, then changing the value ofthe sprite
pointer to point to each block in turn. The change
in the appearance is instantaneous, and so the
illusion of movement is easily created. For
instance, our first character would look much more
convincing if 4is mouth opened and closed as he
moved, and his mouth always faced his direction of
motion. Here is a pro gram which doesjust that and
moves the sprite around the screen:
10
20
PRINT"{CLS}"
POKE 53281,15
Sprites
30
40
50
100
110
130
135
140
145
150
155
160
165
170
180
500
510
520
530
540
550
560
570
600
610
620
630
640
650
660
700
997
998
175
176
999
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1095
1097
1098
1099
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2097
2098
2099
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3095
REM
DATA 0,0,0,0,126,0,1,255
DATA 128,3,247,192,7,227
DATA 224,15,247,192,31
DATA 255,128,31,255,0,63
DATA 254,0,63,252,0,63
DATA 248,0,63,248,0,63
DATA 252,0,63,254,0,31
DATA 255,0,31,255,128,15
DATA 255,192,7,255,224,3
DATA 255,192,1,255,128
DATA 0,126,0,0
REM
REM LEFT FACING SPRITE
REM
DATA 0,0,0,0,126,0,1,255
DATA 128,3,247,192,7,227
DATA 224,3,247,240,1,255
DATA 248,0,255,248,0,127
DATA 252,0,63,252,0,31
DATA 252,0,31,252,0,63,252
DATA 0,127,252,0,255,248
DATA 1,255,248,3,255,240
DATA 7,255,224,3,255,192
DATA 1,255,128,0,126,0,0
REM
REM CLOSED MOUTH
REM
DATA 0,0,0,0,126,0,1,255
DATA 128,3,247,192,7,227
DATA 224,15,247,240,31,255
DATA 248,31,255,248,63,255
DATA 252,63,255,252,63
DATA 255,252,63,255,252
DATA 63,255,252,63,255
DATA 252,31,255,248,31
DATA 255,248,15,255,24
DATA 7,255,224,3,255,192
DATA 1,255,128,0,126,0,0
Sprites
177
SPRITE CREATION
The process of defining sprites is very laborious,
and, like user defined characters, is one with which
your computer can assist. The following program
will allow you to create and modify your sprites,
save the data on tape and many other useful
things, so it's weIl worth persevering in typing it
all in!
1
2
3
4
5
6
10
20
30
40
50
60
93
94
REM *****************
REM *
*
REM * SPRITE EDITOR *
REM *
*
REM *****************
REM
REM
DIM D%(23,20),DA%(64)
CS=1065:CX=0:CY=0:CC=CS:CD%(0)=
43:CD%(1)=81
POKE53280,13:POKE53281,12:PRINT
I{GR3}"
RS=53248:DS=12288:SN=0
B$="
{CU}":REM 37 SPACES
MO=l
REM
REM **************
178
95.
96
97
98
99
100
110
120
130
140
150
160
170
180
493
494
495
496
497
498
499
500
510
520
530
540
550
560
570
580
590
600
650
993
994
995
996
997
998
REM *
*
REM * PRINT GRID *
REM *
*
REM **************
REM
PRINT" {CLS}"
G$=" ++++++++++++++++++++++++"
FOR I=O TO 20
PRINT G$:NEXT I
CD = PEEK(CS)
PRINT "{HOM}"
FOR I=l TO 8:PRINT TAB(32)I
IF I<8 THEN PRINT:PRINT
NEXT I
REM
REM ***************************
REM *
*
REM * SET UP SPRITE REGISTERS *
REM *
*
REM ***************************
REM
FOR I=O TO 7:REM DO EACH SPRITE
POKE 53248+2*I,50:REM X POS
POKE 53249+2*I,52+(25*I):REM Y
POS
POKE 53287+I,1:REM COLOUR
POKE 2040+I,192+I:REM POINTERS
NEXT I
POKE 53264,255:REM X MSB
POKE 53269,255:REM ENABLE
POKE 53277,0:REM NO X EXPANSION
POKE 53271,0:REM NO Y EXPANSION
POKE 53276,0:REM MULTI COL. OFF
PRINT SPC(6)"{RVS}P{ROF}ROGRAM
{RVS}L{ROF}OAD {RVS}S{ROF}AVE"
REM
REM ******************
REM *
*
REM * KEYBOARD INPUT *
REM *
*
REM ******************
Sprites
999
1000
1005
1010
1020
1030
1040
1060
1070
1090
1095
1100
1110
1120
1130
1200
1210
1493
1494
1495
1496
1497
1498
1499
1500
1510
1520
1530
1540
1600
179
REM
GET K$:IF K$="" THEN 1500
K = ASC{K$)
IF K$="{CR}" AND CX<23 THEN
CX=CX+l:POKE CC,CD
IF K$="{CL}" AND CX>O THEN
CX=CX-l:POKE CC,CD
IF K$="{CD}" AND CY<20 THEN
CY=CY+l:POKE CC,CD
IF K$="{CU}" AND CY>O THEN
CY=CY-l:POKE CC,CD
IF K$=" " THEN SD=l-SD:CD=
CD%(SD)
IF K$="P" THEN SA=56222:L=6:
CO=2:GOSUB 1200:GOSUB 3000:
CO=ll:GOSUB 1200
IF K$="L"THEN 4100
IF K$="S"THEN 4000
IF K>48 AND K<57 THEN GOSUB
2000
IF K>32 AND K<41 THEN GOSUB
2500
CC=CS+CX+(40*CY):D%(CX,CY)=SD
POKE CC,CD:GOTO 1000
FOR I=O TO L
POKE SA+I,CO:NEXT:RETURN
REM
REM ****************
REM *
*
REM * FLASH CURSOR *
REM *
*
REM ****************
REM
CT = PEEK(CC)
C%=(CT OR 128)-(CT AND 128)
POKE CC,C%
FOR I=O TO 100:NEXT
GOTO 1000
PRINT SPC(6)"{RVS}P{ROF}ROGRAM
{RVS}L{ROF}OAD {RVS}S{ROF}AVE"
: RETURN
180
1993
1994
1995
1996
1997
1998
1999
2000
2010
2020
2030
2493
2494
2495
2496
2497
2498
2499
2500
2510
2520
2530
2540
2550
2560
2993
2994
2995
2996
2997
2998
2999
3000
3010
3020
REM
REM ********************
REM *
*
REM * SPRITE SELECTION *
REM *
*
REM ********************
REM
IF SN=O THEN 2020
POKE SS+54272,11
SN = K-48: SS=CS-88+(SN*120):
POKE SS+54272,3
GOSUB 3500:RETURN
REM
REM *********************
REM *
*
REM * CLEAR SPRITE DATA *
REM *
*
REM *********************
REM
PRINT"{HOM}{22 * CD}"
PRINT B$:PRINT" ARE YOU SURE ?"
GETA$:IF A$="" THEN 2520
IF A$<> "Y" THEN PRINT"{CU}"B$:
GOSUB 1600:RETURN
PRINT"{CU}"B$:PRINT" CLEARING
DATA"
FOR 1=0 TO 63:POKE DS+((K33)*64)+I,0:NEXT I
PRINT"{CU}"B$:GOSUB 1600:
RETURN
REM
REM ***************************
REM *
*
REM * CONVERT GRID TO DATA
*
REM *
*
REM ***************************
REM
I = O:FOR Y=O TO 20
FOR BYTE=O TO 2:DA%(I)=0
FOR BIT=O TO 7
Sprites
181
3030DA%(I)=DA%(I)+(2t(7-BIT*(PEEK(CS+(BIT)+(8*BYTE)+(40*Y
=81)
3040 NEXT BIT
3041 POKE DS+((SN-1)*64)+I,DA%(I):
I=I+1
3050 NEXT BYTE
3060 NEXT Y
3120 RETURN
3493 REM
3494 REM ************************
3495 REM *
*
3496 REM * CONVERT DATA TO GRID *
3497 REM *
*
3498 REM ************************
3499 REM
3500 FOR I=O TO 63:DA%(I)=PEEK
(DS+((SN-1)*64)+I):NEXT:I=0
3510 FOR y=o TO 20
3520 FOR BYTE=O TO 2
3525 N = DA%(I)
3530 FOR BIT=7 TO 0 STEP-1
3540 B2 = 2tBIT
3550 IF N - B2 >=0 THEN CH=81:N=NB2:GOTO 3570
3560 CH = 43
3570 POKE CS+(7-BIT)+(8*BYTE)
+( 40*Y) ,CH
3590 NEXT BIT:I = I+1:NEXT BYTE
3600 NEXT Y:RETURN
3993 REM
3994 REM ********************
3995 REM *
*
3996 REM * SAVE SPRITE DATA *
3997 REM *
*
3998 REM ********************
3999 REM
4000 PRINT"{HOM}{23 * CD}"
4010 PRINT B$
4020 INPUT" FILE NAME";N$
4025 PRINT"{CU}"B$"{CU}"
182
Sprites
183
CHAPTER 15
PERMANENT STORAGE
The 64 is able to store pro grams onto cassette tapes
or floppy disks, and to LOAD and RUN programs
from tape, disk or plug-in cartridge. Tapes and
disks mayaIso be used to store data for future use.
PROGRAMS ON TAPE
To load a program from tape, put the cassette in the
tape unit and rewind it to the beginning. Type the
command:
LOAD "NAME"
1111
Permanent Storage
185
The message:
186
PROGRAMS ON DISK
Loading and saving programs from disk is very
similar to using the tape. The same commands are
used, with the suffix ',8' added which instructs the
64 to use the disk drive instead of the cassette uni t.
To load a program from disk, put the disk into the
drive and elose the door. Type the command:
LOAD "NAME",
Permanent Storage
187
11
NAME 11
188
OPEN F, D, S, "NAME"
Permanent Storage
189
OPEN 1, 1, 1, "TEST"
PRINT#l, "THIS IS A TEST FILE"
CLOSE 1
STOP
OPEN 3, 1, 0, "TEST"
INPUT#l, A$
CLOSE 1
PRINT A$
190
adjacent items of data. Using commas or semicolons suppresses the carriage returns with the
result that the INPUT# statement will not read
the data properly. This is where GET# is useful,
reading single bytes from the file without regard
for carriage returns or any other markers. If you
need to store single byte numbers, then writing
them to the tape with PRINT#; and reading them
with GET# makes very efficient use ofthe tape, as
all the space is taken up by data with no separating
characters. This method is used in the Character
Generator program in Chapter 12 to save character
sets onto tape.
OTHER PERIPHERAL COMMANDS
STATUS, or ST, is a system variable which
provides information about tape files. The variable
has eight possible meanings, which are shown in
the table:
STATUS
MEANING
1,2
OK
SHORT BLOCK
LONG BLOCK
16
32
CHECKSUM ERROR
64
END OF FILE
-128
END OF TAPE
Permanent Storage
191
DATAFILE
This program uses the file handling commands to
maintain a simple database on tape. The program
is written as an address book, but there are many
other applications. As well as loading and saving
the data, the program allows you to insert and
remove entries, and to search through the index for
specific i tems.
1
2
3
4
REM
REM
REM
REM
REM
*****************
*
*
* 64 DATAFILE *
*
*
*****************
192
10
GOSUB 50000:
90
92
94
96
98
REM ****************
REM *
*
REM * FIRST MENU *
REM *
*
REM ****************
100
230
240
900
990
992
994
996
998
REM ******************
REM *
*
REM * INSPECT FILE *
REM *
*
REM ******************
1000
110
120
130
200
210
220
1010
1020
1030
REM
SET UP
Permanent Storage
1040
193
1150
1190
1192
1194
REM
REM *** SAFETY CHECK ***
REM
1200
1250
1260
1990
1992
1994
1996
1998
REM ******************
REM *
*
REM * ENTER NEW OATA *
REM *
*
REM ******************
1050
1100
1110
1120
1130
1140
1210
1220
1225
1230
1240
194
2000
2030
2090
2092
2094
REM
REM *** ADD NEW ITEM ***
REM
2100
2110
2120
2130
2140
2150
2190
2192
2194
REM
REM *** DISPLAY NEW ITEM ***
REM
2200
2210
2220
2230
2250
2260
2270
2280
2300
2990
2992
REM *************
REM *
*
2010
2020
2240
Permanent Storage
195
2994
2996
2998
3000
3010
3190
3200
3210
3220
3230
3290
3292
3294
REM
REM *** DO THE SEARCH ***
REM
3300
3310
FOR 1=0 TO P
IF A$(I,F)=FI$ THEN FP(FF)=I:
FF=FF+1
NEXT I
PRINT "{CLS}{CD}{CD}{CD} "FF
" ITEMS FOUND"
IF FF=O THEN W=3: GOSUB 30000:
RETURN: REM WAIT 3 SECONDS
3020
3030
3040
3050
3060
3100
3110
3120
3130
3320
3400
3410
3490
3492
3494
REM
REM ** DISPLAY FOUND ITEMS **
REM
196
3600
3610
3620
3690
3692
3694
REM
REM IF NO ITEMS PRINT MESSAGE
REM
3700
3720
3990
3992
3994
3996
3998
REM ********************
REM *
*
REM * LIST ALL ENTRIES *
REM *
*
REM ********************
4000
4010
4020
3500
3510
3520
3530
3535
3540
3550
3560
3570
3580
3590
3710
4090
4100
4110
Permanent Storage
4120
197
4200
4290
4292
4293
4294
REM
REM
REM
REM
4300
4320
PRINT TAB(5)"{CD}{CD}{RVS}
THERE IS NOTHING TO LIST
{ROF}"
W=3: GOSUB 30000: REM WAIT 3
SECS
RETURN
4990
4992
4994
4996
4998
REM *********************
REM *
*
REM * SAVE FILE TO TAPE *
REM *
*
REM *********************
5000
5010
5020
4130
4140
4150
4160
4170
4180
4310
5030
5035
5040
198
5120
5130
5140
5150
9990
9992
9994
9996
9998
REM ***********************
REM *
*
REM * LOAD FILE FROM TAPE *
REM *
*
REM ***********************
5050
5060
5070
5080
5090
5100
5110
Permanent Storage
10992
10994
10996
10998
REM
REM
REM
REM
199
*
*
* NEW FILE *
*
*
************
*******************
*
*
* DISPLAY AN ITEM *
*
*
*******************
19990
19992
19994
19996
19998
REM
REM
REM
REM
REM
20000
20010
20020
20030
20040
20050
20990
20992
20994
20996
20998
REM
REM
REM
REM
REM
******************
*
*
* DELETE AN ITEM *
*
*
******************
200
REM
REM
REM
REM
REM
*****************
*
*
* SCREEN HEADER *
*
*
*****************
REM
REM
REM
REM
REM
*****************
*
*
* WAIT ROUTINE *
*
*
*****************
30000 TI$="OOOOOO"
30010 IF TI < W*60 THEN 30010
30020 RETURN
49990
49992
49994
49996
49998
REM
REM
REM
REM
REM
50000
50010
50020
50030
50040
50050
50060
50070
50080
50100
50110
EL
= 100
FLDS = 6
F$(O) = "SURNAME"
F$(l) = "FIRST NAME"
F$(2) = "HOUSE AND STREET"
F$(3) = "TOWN"
F$(4) = "COUNTY"
F$(5) = "POST CODE"
F$(6) = "PHONE NUMBER"
OlM A$(EL, FLDS)
OlM FP(EL)
******************
*
*
* INITIAL SET-UP *
*
*
******************
Permanent Storage
201
202
EL
Permanent Storage
203
FLDS
F$()
FL$
ID
FF
FP()
CHAPTER 16
Advanced Techniques
205
SYS 2048
A = USR(X)
206
207
Advanced Techniques
Key not
valid
CHECK KEY
INPUT
208
PART 2
ADVANCED COMMODORE 64
PROGRAMMING
CHAPTER 17
212
The display will look like the two left hand columns
in the list below if you have typed the pro gram in
exactly as it appears:
ADDRESS
CONTENTS
COMMENT
2048
2049
19
2050
2051
10
2052
2053
153
2054
32
(space)
2055
34
"
2056
84
2057
69
2058
83
2059
84
2060
34
"
2061
58
2062
137
GOTO
2063
32
(space)
2064
49
2065
48
2066
Null
2067
Null
2068
Null
213
214
215
216
20
REM
2067
25
2068
2069
20
2070
2071
143
REMtoken
2072
Null
2073
Null
2074
Null
217
Une No.
10 1
Pointerto
Une No.
BASIC UNE
BASIC UNE
Une No.
Pointer to
END OF PROGRAM
218
219
RETURN
115
117
119
121
124
126
128
130
132
133
135
136
138
E6
DO
E6
AD
C9
BO
C9
FO
38
E9
38
E9
60
7A
02
7B
B7 12
3A
OA
20
EF
30
DO
INC
BNE
INC
LDA
CMP
BCS
CMP
BEQ
SEC
SBC
SEC
SBC
RTS
$7A
$02
$7B
$12B7
#$3A
RETURN
#$20
START
#$30
#$DO
220
221
DELAY
QUIET
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
BPL
LDA
, STA
LDA
STA
RTS
#37
54271
#100
54273
#15
54296
#54
54277
#168
54278
#33
54276
#122
162
162
DELAY
#32
54276
#0
54296
;set up SID
;registers
;store into
;Jiffy c10ck
;1oop unti1
;zero
;turn off the
;noise
222
#0
TABLE,Y
CHRGET,Y
#6
INIT
iModify the
ifirst six
ibytes of
iCHRGET
iroutine
READ
#35
RETURN
BEEP
COPY
iget char
iis it a #?
$7A
R2+1
$7B
R2+2
R2+1
R2
R2+2
$0800
iCOPY $7A
iand $7B
iinto new
iread
iroutine &
iget next
icharacter
$7A
iCOPY of
ino
iyes! BEEP
iupdate $7A
iand $7B
223
510
520
530 EXIT
590
591
800 BEEP
805
810
815
820
825
830
835
840
845
850
855
860
865
870 DELAY
875
880 QUIET
885
890
895
900
990
991
1000 TABLE
1010
BNE EXIT
INC $7B
RTS
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
BPL
LDA
STA
LDA
STA
RTS
:start of
:CHRGET
:routine
#37
54271
#100
54273
#15
54296
#54
54277
#168
54278
#33
54276
#122
162
162
DELAY
#32
54276
#0
54296
JSR PROG
JSR COPY
:new start
iof CHRGET
224
20010DATA 201,35,208,6,32,57,192,
32,50,192,96,165,122,141,47,19
2,165
20020DATA 123,141,48,192,238,47,
192,208,3,238,48,192,173,97,8,
96,230
20030 DATA 122,208,2,230,123,96,169,
37,141,255,211,169,100,141,1,2
12,169
20040 DATA 15,141,24,212,169,54,141,
5,212,169,168,141,6,212,169,33
,141
20050 DATA 4,212,169,122,133,162,
165,162,16,252,169,32,141,4,21
2,169,0
20060 DATA 141,24,212,96,32,14,192,
32,50,192,64
NOTE
SYS 49152
225
226
STRINGS
BOTTOM OF STRINGS 51, 52
ARRAYS
_______________1
VARIABLES
--------------BASIC
PROGRAM
STORAGE
Integer Variables
The diagram shows how the seven bytes are used
by an integer variable.
VARIABLE NAME
1st
2nd
+ 128
+128
VARIABLE VALUE
High
Byte
NULL
Low
Byte
NULL
NULL
227
2nd
VARIABLE VALUE
Expone-
Mantiss-
Mantiss-
Mantiss-
Mantiss-
-nt
-a
-a
-a
-a
String Variables
Astring variable is stored in two parts: as an entry
in the variable table as illustrated in the diagram
below, and as the characters comprising the string,
which are stored in aseparate area of memory,
pointed to by the entry in the variable table.
VARIABLE NAME
1st
POINTER TO CHARACTERS
2nd
Noof
Low
High
+ 128
Chars
Byte
Byte
NULL
NULL
228
Ifyou run the program you will see that before the
variables are ereated, the start ofvariables and the
start of arrays are at the same loeation. However
when the variables have been ereated by the
program there is a differenee of 28 bytes between
the two - sinee eaeh variable oeeupies 7 bytes and
the program ereated 4 variables.
Type in the following eommand to examine the
variable area:
229
193
128
0
100
0
0
0
2272
2273
2274
2275
2276
2277
2278
66
0
138
122
0
0
0
2279
2280
2281
2282
2283
2284
2285
67
128
6
142
8
0
0
2286
2287
2288
2289
2290
2291
2292
68
128
12
244
159
0
0
230
Array Variables
All three types of variable may be stored in array
form, in which case different methods are used for
allocating memory. Arrays are stored immediately
above simple variables, starting from the address
pointed to by locations 47 and 48. The array type
(floating point, integer or string) is indicated by the
way in which the array name is stored - in exactly
231
Element
Element
Element
Element
Element
Format of an array
NOTE:
2nd
TOTAL
BYTES
Lo"w
High
No.of
Size of Nth
Size of N-1th
Byte
Byte
DIMensions
DIMension
DIMension
232
Expone-
Mantiss-
Mantiss-
Mantiss-
Mantiss-
-nt
-a 1
-a 2
-a 3
-a 4
INTEGER
High
Low
Byte
Byte
NO.of
High
Low
Chars.
Byte
Byte
STRING
Pointer
DlM AB(10,20)
2067
2068
2069
2070
2071
2072
90
140
1
112
233
2073
2074
2075
65
66
2076
2077
140
4
2078
2079
2080
0
21
2081
2082
0
11
2083
2084
2085
0
0
0
CHAPTER 18
235
236
Interrupt disable.
BITO
z c
Bi t 5 is not used.
The 6510 has a number of instructions which test
the states of these flags and cause the pro gram to
branch if a flag is in a certain state. These
instructions correspond loosely to the IF ... THEN
... GOTO of BASIC, and play an important part in
all machine code pro grams.
237
Stack Pointer S
This is an eight bit register used to index the
memory area in which subroutine return addresses
are stored.
THE 6510 INSTRUCTION SET
The instructions in the machine code instruction
set include commands for moving data between the
64's memory and the three data registers, for
performing arithmetic and logical operations on
the contents ofthe accumulator, and for testing the
results of these operations.
All 6510 instructions take the form of single byte
numbers, but may be followed by one or two further
bytes as an operand. For example,
169
$01
238
#$01
Transferring Data
The first group of instructions move data between
the 6510 registers and the memory.
LDA
STA
For example:
LDA $0401
STA $0402
239
240
A9 01
AO 70 00
LDX
LDY
STX
STY
ABSOLUTE
AE
AC
8E
8C
IMMEDIATE
A2
AO
ZERO PAGE
A6
A4
86
84
241
FUNCTION
TAX
AA
TAY
A8
CopiesA to Y
TXA
8A
CopiesX toA
TYA
9A
Copies YtoA
Arithmetic
The 6510 can perform addition and subtraction
operations. Multiply and divide must be calculated
by program, as instructions are not provided.
Addition is performed by the command ADe. This
adds a number in memory to the number in the
accumulator, storing the result in the accumulator.
If the carry flag in the processor status register was
set before the operation, 1 is added to the result. If
the result is greater then 255, the least significant
eight bits remain in the accumulator and the carry
flag is set; otherwise it is cleared.
The use of the carry flag allows n umbers of two
bytes or more to be added together, wi th the carry
operating in the same way as when you add two
numbers with pencil and paper.
To add two single byte numbers stored in, say,
$1000 and $1001:
CLC
LDA $1000
242
ADC $1001
STA $1002
Store result.
ADC<address>
6D
Immediate
ADC# <number>
69
Zero Page
$A1
$A4
$Bl
$A2
$A5
$B2
$A3
$A6
$B3
243
Subtraction
ADC is complemented by a subtraction command
SBC, which subtracts a number from that in the
accumulator. This is used in a similar fashion to
the ADC command, and again the carry flag is
used when handling large numbers. There is an
important difference - the carry flag must be set
before the subtraction, and will be cleared to
indicate a cborrow' when the result of the
subtraction is less than zero. If the flag is clear
before subtraction, 1 is subtracted from the result.
The different addressing modes are again
available:
Absolute
ED
Immediate
SBC# <number>
E9
Zero Page
$0401
#1
$0401
$0402
#0
$0402
244
CODE
INX
E8
Adds 1 to X register
DEX
CA
INY
C8
Adds 1 to Y
DEY
88
Subtracts 1 from Y
DEC <address>
245
246
'10
LDX
~DEX
BNE 253
LDA $ABCD
etc.
A2 OA
CA
DO FD
AD CD AB
DEX
247
LDA $1234
BNE
OPERATION
=0 (Z set)
BEQ
FO
Branch on result
BNE
00
BeS
90
Bee
BO
BMI
30
BPL
10
BVS
70
Branch if V set(overflow)
BVe
50
Branch if V clear
248
249
MODE
CMP
CPX
Cpy
ABSOLUTE
CD
EC
CC
IMMEDIATE
C9
EO
CO
ZERO PAGE
C5
E4
C4
ADDRESSING MODES
Many of the 6510 instructions can take several
forms, providing different ways for specifying the
memory location to be used by the instruction.
These different forms of the instructions are said to
use different addressing modes, because they
address the memory in different ways. We have
already introduced three addressing modes:
absolute addressing, zero page addressing and
immediate addressing. These and the other modes
are explained below.
Absolute Addressing
This mode uses a two byte operand after the
instruction code. The two bytes are the low and
high bytes of the address of the data in memory.
Absolute addressing is indicated by the full address
after the instruction:
LDA $ABCD
250
Indexed Addressing
Indexed addressing uses the X or Y index register
to modify the address given after the instrueton, so
that the address of the data is Operand + X or
Operand + Y. For example:
Absolute:
LDA $ABCD
Absolute Indexed:
LDA $ABCD,X
251
Indexed Indireet:
LDA (TABLE,X)
252
BCS 35
Accumulator Addressing
The instruction acts only on the accumulator.
Again no operand is used. This addressing mode is
used only by the register shift instructions.
Indirect:
JMP ($ABCD)
253
254
st~ck
by
OPERATION
PHA
48
A to STACK
PLA
68
STACK to A
PHP
08
P to STACK
PLP
28
STACK to P
BA
TXS
9A
255
INTERRUPTS
A microprocessor in a computer such as the 64 has
several jobs to do. As weIl as running the BASIC
interpreter program, the screen must be managed,
the keyboard checked for keypresses, and many
other routine operations performed. The ideal
microprocessor would be able to run many separate
programs at the same time, but such a processor
does not exist. Instead, the 6510 has a facility
which allows pro grams to be interrupted while
another program is run and then restarted at the
point at which they were stopped.
Two of the 40 pins on the 6510 chip are interrupt
request pins. Peripheral devices applying signals
to one or other of these pins will stop the 6510 in
the middle of whatever it is doing, and divert it to
another piece of program. The 6510 will be
returned to the original program by an instruction
at the end of the interrupt program. The two pins
are similar in use, the difference being that the
IRQ (interrupt re quest) pin is ignored if the I bit of
the processor status register has been set by an SEI
instruction, whereas the NMI (non-maskable
interrupt) pin can not be ignored.
When a suitable signal is applied to the IRQ pin the
6510 finishes the current instruction, stores the P
register and the program counter on the stack, sets
256
257
A.ORB
AEORB
AANDB
SHIFTS
There are four shift instruetions available whieh
shift or rotate the bits in the aeeumulator or
memory.
ASL
258
BIT7
ACCUMULATOR
BITO
BIT 7
ACCUMULATOR
BITO
I~
BITO~
'------+0------C
259
c
The ROR operation
USING MACHINE CODE WITH BASIC
Machine code pro grams are called from BASIC by
the SYS command. This is similar in effect to the
GOSUB command except that the subroutine
called is a machine code routine at the address
specified in the SYS command. Control is returned
to BASIC when the routine is completed by the
machine code instruction RTS. The SYS command
is followed by the address of the start of the
machine code program. For example:
SYS 64738
260
261
CHAPTER 19
263
BANK
Starting
Location
oto 16k
16kt032k
16384
32k to 48k
32768
48k to 64k
49152
264
265
When run, the pro gram will set up a highresolution display and wait until you press a key,
after which the normal display will be restored.
Colour
The colour of a bit-mapped display is controlled by
the numbers in the text screen memory. Each
number in this memory area controls the colour of
an 8x8 block of pixels on the high resolution
display. The 'foreground' colour - the colour
displayed for a bit set to 1 in display memory - is
controlled by the four highest bits of the screen
memory, while the background colour depends on
the four lowest bits. For example, to set a
foreground colour of red (colour 2) and a white
background (colour 1), each location in the text
memory must be set to 16*2 + 1, which is 33.
Add the following two lines to the program. Line
50 sets the colours, and line 60 clears the screen
memory.
266
50
60
BIT
PATTERN
COLOUR DISPLAYED
00
01
10
11
Colour memory
267
= 689
in Standard BMM)
Colour 3 (MC BMM
only)
1 = BMM, =MC
BMM
268
140
160
170
180
190
210
220
230
240
245
250
BMM
LDA #1
STA MCBM
BNE CLEAR
MCBMM
LDA #0
STA MCBM
CLEAR
LDA #$60
260
STA 252
270
LDA #0
280
STA 251
290
LDX #64
300
JSR WIPE
310 ! SET COLOURS
320 COLOUR LDA # 92
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
STA
LDA
STA
LDA
ASL
ASL
ASL
ASL
ORA
LDX
JSR
480
490
500
510
520
STA
LDA
STA
LDX
LDA
252
#0
251
COL1
A
A
A
A
COL2
#8
WIPE
LDA MCBM
BNE HIRES
LDA #$D8
252
#0
251
#8
COL3
Multiply COLi by i6
AddCOL2
530
540
545
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
795
800
269
JSR WIPE
! NOW SET POINTERS FOR BM MODE
HIRES
LDA
ORA
STA
LDA
AND
ORA
STA
LDA
ORA
STA
LDA
STA
56578
#3
56578
56576
#252
#2
56576
53265
#32
53265
#120
53272
LDA
BNE
LDA
ORA
STA
MCBM
BMMEND
53270 [{MC, setMC mode
#16
53270
Set bank 2
SetBMMon
BMMEND RTS
820
830
840
850
860
870
880
890
900
STA
LDA
AND
ORA
STA
LDA
AND
STA
LDA
56578
56576
#252
#3
56576
53265
#223
53265
53270
Clear MC bit
270
910
920
930
940
950
955
960
AND
STA
LDA
STA
RTS
#239
53270
#21
53272
Reset pointers
965
970 WIPE
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
LDY #127
Subroutine to fiU
memorywith
character in
accumulator
F1 STA (251),Y
DEY
BPL F1
PHA
CLC
LDA 251
ADC #128
STA 251
LDA #0
ADC 252
STA 252
PLA
DEX
BNE WIPE
RTS
END
271
49152
MCBMM 49159
CLEAR
49164
COLOUR 49177
Seteolours
IDRES
49221
Set to BM or MCBMM
LORES
49266
NOTE:
Beeause these routines plaee the bitmapped sereen in the middle of the
BASIC program area, at 24k, there is a
risk that the display may be eorrupted
by the BASIC or by pro gram variables.
The bit-mapped sereen ean be proteeted
from BASIC by resetting the top of
BASIC memory thus:
POKE 55,0: POKE 56, 92: CLR
272
PLOTTING POINTS
The bit-mapped screen is arranged as a grid of 200
rows of 320 pixels (or 160 pixels in multicolour
mode). Each byte in the screen memory represents
a row of 8 pixels.
320 PIXELS HORIZONTALLY
R
T
I
C
A
L
L
Y
o
1
8
9
10
16
17
18
24
25
26
312
313
314
273
315
316
317
318
319
3
4
5
6
7
= INT(Y/8)
= INT(X/8)
Y AND 7
************
*** PLOT ***
************
40
50
60 * = $COB8
80
90
100 BYTELO = 251
Starts at 49366
274
110
120
130
140
150
160
170
180
190
200
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
BYTEHI
Tl
T2
XLO
XHI
Y
COLNO
MCBM
BASE
PLOT
=
=
=
=
=
=
=
=
=
252
253
254
679
680
681
685
689
24576
= YI8
LDA
LSR
LSR
LSR
STA
Y
A
A
A
ROW
Find ROW
LDA
LSR
LDA
ROR
LSR
LDX
XHI
A
XLO
A
A
MCBM
FindCHAR
370
380
BEQ PLOT1
LSR A
390 PLOT1
400
410
420
430
440
450
460
470
480
490
500 PI
STA CHAR
= YAND7
ROW
ROW
Tl
#0
T2
#6
TIMES2
= ROW*64
LDA Y
AND #7
STA LINE
LDA
STA
LDA
STA
LDX
JSR
I{ MC mode then
CHAR =X14
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
DEX
BNE
LDA
STA
LDA
STA
JSR
JSR
CLC
LDA
ADC
STA
LDA
ADC
STA
275
PI
T2
BYTEHI
Tl
BYTELO
TIMES2 AddROW* 256
TIMES2
Tl
BYTELO
BYTELO
T2
BYTEHI
BYTEHI BYTE now holds
ROW*320
670
680
690
700
710
720
730
740
750
760
LDA
STA
LDA
STA
JSR
JSR
JSR
CLC
LDA
Tl
770
780
790
800
810
ADC
STA
LDA
ADC
STA
BYTELO
BYTELO
T2
BYTEHI
BYTEHI BYTE now holds
FindCHAR*8
#0
T2
CHAR
Tl
TIMES2
TIMES2
TIMES2
Add CHAR*8 to
BYTE
ROW*320
820
830
840
850
860
870
880
CLC
LDA
ADC
STA
LDA
ADC
+ CHAR*8
LINE
BYTELO
BYTELO
#0
BYTEHI
276
890
900
910
920
930
940
950
960
970
975
980 ! BYTE
CHAR*8
985
990
+ CHAR*8
CLC
ADC
STA
LDA
ADC
STA
BYTELO
BYTELO
#>BASE High byte ofBASE
BYTEHI
BYTEHI
BASE + ROW*320 +
+ LINE
LDA MCBM
BEQ MCPLOT
1000
1010
1020
1040
1050 BMPLOT LDA XLO
1060
AND #7
1070
STA BITT
SEC
1080
LDA #7
1090
1100
SBC BITT
1110
STA BITT
1120
CLC
1130
1140
1150
1160
1170 P2
1180
1190
LDA
LDX
BEQ
ASL
DEX
BNE
1200
1210 P3
LDY #0
Branch if MC mode
selected
= X AND 7
BITT=7-(X AND 7)
IfBITT < > 0 then
{ind2tBITT
#1
BITT
P3
A
P2
Accumulator now
holds 2 tB ITT
277
1220
1230
LDX COLNO Test tor Plotor Unplot
1240
BEQ UNPLOT Branch i{COLNO=O
1250
ORA (BYTELO) , Y
1260
STA (BYTELO),y Plot the pixel
1270
1280
RTS
1290
1310 UNPLOT EOR #$FF
Clear the pixel
1320
AND (BYTELO),Y
1330
STA (BYTELO),y
1340
RTS
Standard mode plot
ends here
1350
1360
1380
1390 MCPLOT LDA XLO
1400
AND #3
1410
STA BITT
1420
SEC
1430
LDA #3
1440
SBC BITT
1450
ASL A
1460
STA BITT
Plot in MC mode
BITT=2*(3-(X AND
3)
1470
1480
1490
1500
1510
1515
1520 MCPl
1530
1540
1550 MCP2
LDY
LDA
AND
LDX
BEQ
ASL
DEX
BNE
STA
1560
1570
LDA #%11111100
1580
1585
LDX BITT
BEQ MCP4
#0
COLNO
#3
BITT
MCP2
A
MCP1
COLS
COLS = COLNO *
2tBITT
Setup mask
tor pixel
278
1590
1600 MCP3
SEC
ROL A
1610
1620
1630
1640 MCP4
DEX
BNE MCP3
AND (BYTELO),Y
1650
ORA COLS
1660
1665
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
STA (BYTELO),Y
RTS
End of MC plot
MULTIPLY (T1,T2) BY 2
TIMES2 LDA
ASL
ASL
ADC
STA
RTS
ROW
CHAR
LINE
BITT
COLS
#0
T2
Tl
T2
T2
NOP
NOP
NOP
NOP
NOP
END
279
Note that the pro gram does not perform any checks
to make sure that the co-ordinates are within the
screen limits. Using overlarge values of X or Y is
unlikely to have any ill effects other than spoiling
the appearance of the display, but you should
design your BASIC pro grams to avoid using
incorrect values.
Location 685 (called COLNO in the pro gram) is
used to select the colour in which the pixel is
plotted. In standard two colour mode, PO KE 685
with 1 to plot in the foreground colour, or with zero
to plot in the background colour (to erase a pixel).
In multicolour mode, the colours are numbered
from 0 to 3, as shown in the table on page 56.
The PLOT routine begins at location 49336, and so
may be run by the command:
SYS 49336.
CHAPTER 20
DRAWING LlNES
Straight lines may be drawn using the equation:
Y=M*X+C
281
100
************
110
*** DRAW ***
120
************
130
140
150 * = $C1C8
160 !
170
190
200 DRAW
LDA X2L
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390 DRAW1
400
410
420
430
440
STA
LDA
STA
LDA
STA
XTL
X2H
XTH
Y
YT
LDA
STA
SEC
LDA
SBC
BCS
LDA
EOR
STA
SEC
LDA
SBC
STA
#0
NEG
Starts at 49608
DY
Y2
Y
DRAW1
NEG
#1
NEG
= ABS(Y2-Y)
Y
Y2
DY
SEC
LDA X2L
SBC XL
STA DXL
DX
= ABS (X2-X)
282
450
460
470
480
LDA
SBC
BCS
LDA
X2H
XH
DRAW2
NEG
490
500
510
520
530
540
550
560
570 DRAW2
580
590
600
610
620
EOR
STA
SEC
LDA
SBC
STA
LDA
SBC
STA
#1
NEG
BNE
LDA
BNE
LDA
DRAW2A
DXL
DRAW2A
IfDX =0 then set
#1
XL
X2L
DXL
XH
X2H
DXH
NEG=I
630
STA NEG
640
650 DRAW2A LDA DY
660
670
680
690
700 DRAW2B
IfDY=Othenset
NEG=I
BNE DRAW2B
LDA #1
STA NEG
LDA DXH
710
BNE SHALLOW
720
LDA DXL
730
CMP DY
740
BCS SHALLOW
JMP
STEEP
750
760
770 SHALLOW SEC
Draw shallow lines
with DYIDX < =I
LDA X2L
780
Ir X2 <Xl then swap
co-ordinates
790
800
810
820
830
835
840 SHAL1
SBC
LDA
SBC
BCS
JSR
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120 DRAW3
1130
1140
1150
1160
1170
STA
LDA
STA
LDA
STA
LDA
STA
JSR
DIVLO
#0
DIVHI
DXL
01
DXH
02
DIVIDE
LDA
STA
LDA
STA
JSR
STA
LDA
STA
LDX
BEQ
CLC
LDA
ADC
STA
LDA
ADC
STA
JMP
LDA
SBC
STA
LDA
SBC
STA
XL
MXL
XH
MXH
MULT
C
P4
CH
NEG
DRAW3
283
XL
X2H
XH
SHAL1
SWAP
LDA DY
Find gradient
M=DYIDX
Calculate C
First find X* M
C
Y
C
CH
#0
CH
SHLOOP
Y
C=Y-(M*X)
C
C
#0
CH
CH
284
1180 !
1190 SHLOOP
1200
1210
1220
1230
1240
1250
1260
1270
LDA
STA
LDA
STA
JSR
XH
MXH
XL
MXL
MULT
LDX NEG
BNE SHL1
CLC
Loop from X to X2
FindM*X
1280
1290
1300
1310 SHL1
ADC C
STA Y
JMP SHL2
SEC
1320
1330
1340
1350 SHL2
1355
1360
1370
1380
1390
1400
1410
1420
1425
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520 STEEP
LDA
SBC
STA
JSR
C
P3
Y
PLOT
CLC
LDA
ADC
STA
LDA
ADC
STA
XL
#1
XL
XH
#0
XH
SEC
LDA
SBC
LDA
SBC
BCS
JMP
X2L
XL
X2H
XH
SHLOOP IfX2> =X then repeat
TIDY
Finish off
1530
See ifX=X2
SEC
LDA Y2
1540
1550
1560
285
CMP Y
BCS STEEP1
JSR SWAP
IfY2 <Y then swap
coordinates
1565
1570 STEEP1 LDA DXL
Find gradient DX/DY
1580
STA DIVLO
1590
LDA DXH
1600
STA DIVHI
1610
LDA DY
1620
STA D1
1630
LDA #0
1640
STA D2
1650
JSR DIVIDE
1655
1660
LDA Y
Find Y*M
1670
STA MXL
1680
LDA #0
1690
STA MXH
1700
JSR MULT
1710
STA C
1720
LDX NEG
1730
BEQ ST1
1740
CLC
IfM -uethenC= M*Y
+X
1750
1760
1770
1780
1790
1800
1810 ST1
ADC
STA
LDA
ADC
STA
JMP
LDA
XL
C
#0
XH
CH
STLOOP
XL
If M + ue then C = X M*Y
1820
SBC
1830
STA
1840
LDA
1850
SBC
1860
STA
1865
1870 STLOOP LDA
1880
STA
C
C
XH
#0
CH
Y
MXL
Loop from Y to Y2
286
1890
1900
1910
1920
1930
LDA
STA
JSR
LDX
BNE
#0
MXH
MULT
NEG
STL1
1940
1950
1960
1970
1980
1990
2000
2005
2010 STL1
2020
2030
2040
2050
2060
2070
2075
2080 STL2
2085
2090
2100
2110
2120
2130
2140
2150
2155
2156
2160 TIDY
CLC
ADC
STA
LDA
ADC
STA
JMP
C
XL
CH
P4
XH
STL2
SEC
LDA
SBC
STA
LDA
SBC
STA
C
P3
XL
CH
P4
XH
2170
2180
2190
2200
2210
2220
FindM*Y
Ir slope -ve then
branch
X
M*Y+C
= C-M*Y
JSR PLOT
Plot pixel
CLC
LDA
ADC
STA
CMP
BCC
BEQ
Iry <
LDA XTH
STA
LDA
STA
LDA
STA
RTS
Y
#1
Y
Y2
STLOOP
STLOOP
XH
XTL
XL
YT
Y
287
2230
2235
2240 MULT
LDA 01
Multiply number in
MX by gradient in Q 1-
2250
STA M1
2260
2270
2280
2290
2300
LDA
STA
LDA
STA
LDA
02
M2
03
M3
#0
2310
2320
2330
2340
2350
2360
2365
2370
STA
STA
STA
STA
STA
STA
P1
P2
P3
P4
MX3
MX4
LDY #24
2375
2380 MULT1
2390
2400
2410
LSR
ROR
ROR
BCC
M3
M2
M1
MULT2
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
CLC
LDA
ADC
STA
LDA
ADC
STA
LDA
ADC
STA
LDA
ADC
STA
MXL
P1
P1
MXH
P2
P2
MX3
P3
P3
MX4
P4
P4
Q3
Initialise product P to
zero
Set Y to repeat 24
times
Shirt M to the right
Ir nothing in carry
then branch
AddMXtoP
288
2545 !
2550 MULT2
2560
2570
2580
2585
2590
2600
2605
2610
ASL
ROL
ROL
ROL
MXL
MXH
MX3
MX4
DEY
BNE MULTI
LDA P3
Store result in
accumulator
RTS
2620
2630
2635
2640 DIVIDE LDA #0
STA 03
2650
2660
2670
2680
2685
2690
2695
2700
2710
2720
2730
2740
2750
2760
2765
2770 DVDI
M ultiply MX by 2
Diuides DIV by D
First set result Q to
zero
STA 02
STA 01
STA DIV3
LDY #24
SEC
LDA
SBC
STA
LDA
SBC
STA
DIVHI
Dl
DIVHI
DIV3
D2
DIV3
PHP
2775
2780
ROL 01
Rotate Q: mult by 2
and add carry
2790
2800
2810
2820
ROL
ROL
ASL
ROL
02
03
DIVLO
DIVHI
M ultiply Diu by 2
2830
2835
2840
2850
2855
2860
2870
2880
2890
2900
2910
2920
2930
2935
2940 DVD2
ROL DIV3
PLP
BCC DVD2
Reload P register
I{DN-D was <0 then
branch
LDA DIVHI
SBC
STA
LDA
SBC
STA
CLV
BVC
D1
DIVHI
DIV3
D2
DIV3
DVD3
LDA DIVHI
2950
ADC 01
2960
STA DIVHI
2970
LDA DIV3
2980
ADC D2
2990
STA DIV3
2885
3000 DVD3
DEY
3010
BNE DVD1
3015
3020
ROL Q1
3030
ROL Q2
3040
ROL Q3
3050
RTS
3060
3070
3080 SWAP LDA X2L
3090
3100
3110
3120
3130
3140
289
LDY
STA
STY
LDA
LDY
STA
XL
XL
X2L
X2H
XH
XH
Jump on to DVD3
Add D to new value o{
DIV
290
3150
3160
3170
3180
3190
3200
3210
3220
3225
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
STY
LDA
LDY
STA
STY
RTS
X2H
Y2
Y
Y2
Label definitions
PLOT
XL
XH
Y
X2L
X2H
Y2
XTL
XTH
YT
DXL
DXH
DY
D1
D2
DIVLO
DIVHI
DIV3
MXL
MXH
MX3
MX4
C
CH
01
02
03
MI
M2
M3
PI
P2
= $COB8
679
= 680
= 681
= 682
= 683
= 684
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
3550
3560
3570
3580
P3
P4
NEG
END
291
NOP
NOP
NOP
292
FILLING BLOCKS
The fourth mach ine code progr am in the graph ics
set is a FILL routin e, to plot all the pixel s in a
given rectan gle in one colour. The routin e is very
short, the mach ine code equiv alent of a BASIC
progr am such as:
10
20
FOR X = Xl TO X2
POKE 679, X AND 255: POKE 680,
X/256
FOR Y = Y1 TO Y2
POKE 681, Y
SYS 49336
NEXT Y
NEXT X
30
40
50
60
70
************
*** FILL ***
************
= $C4E2
Starts at 50402
90
100
110
120
130
140
150
160
170
180
190
200
210
230
240
XL
XH
Y
= 679
= 680
= 681
X2L
X2H
Y2
= 682
= 683
= 684
PLOT
= $COB8
FILL
LDA Y
250
260
280 FILLI
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
STA YT
JSR
CLC
LDA
ADC
STA
PLOT
Plot point
Y
#1
Y
lncrement Y
LDA Y2
CMP Y
BCS FILLI
LDA
STA
CLC
LDA
ADC
STA
LDA
ADC
STA
Repeatloop
until Y>Y2
YT
Y
Reset Y
XL
#1
XL
XH
#0
XH
IncrementX
SEC
LDA X2H
SBC XH
FindX2-X
293
294
510
520
530
LDA X2L
SBe XL
Bes FILL1
540
550
560 !
570 YT
RTS
Repea t loop if
X<=X 2
NOP
REM *********************
110
120
130
140
150
160
170
1000
1010
1020
1030
1040
1050
1060
1070
2000
2005
2010
2020
2030
2040
2050
2060
2090
2100
2500
2510
2520
2530
2540
3000
3010
3020
3030
3040
3050
295
296
3060
3070
3080
3090
4000
4010
4020
4030
4040
4050
4060
4070
Plotting Points
This program uses the PLOT routine to draw a
pattern on the screen.
10
20
30
35
40
50
60
70
80
90
100
110
120
130
297
Headache!
This program uses the DRAW routine to create a
pattern on the multi-colour bit-mapped mode
screen and demonstrates the effect of changing the
colours using the COLOUR routine.
4
5
6
10
15
20
30
40
45
50
H=180:W=140:XC=80:YC=100:S=10
POKEI020,0:POKEI021,96
POKE685,1
POKE 686,2:POKE 687,I:POKE
688,6:POKE 53281,12: SYS 49159
FOR Y=YC-H/2 TO YC+H/2 STEP 5
POKE 679, (XC-W/2)AND 255:POKE
680,(XC-W/2)/256:POKE 681,Y
POKE 682,XC AND 255:POKE 683,
XC/256:POKE 684,YC
SYS 49608:POKE 685, (PEEK(685)
+1-(PEEK(685)=3AND 3
NEXT
FOR X=XC-W/2+S TO XC+W/2 STEP
5
60
70
80
90
115
120
130
140
145
150
298
160
170
180
185
190
191
192
193
194
195
196
197
200
210
220
Lace
This program uses the DRAW routine to create a
lace pattern on the standard bit-mapped screen.
10
20
30
40
50
60
70
100
110
120
130
160
170
180
190
200
210
500
510
299
JR'sHat
This program uses the DRAW routine to draw a
three dimensional graph - it takes quite a while to
run.
100
110
120
200
210
220
300
500
510
520
530
540
550
600
610
620
630
640
650
660
670
680
300
690
700
710
720
730
740
750
760
1000
1010
UB(X2-XA)=Y2
POKE 679,(X1/2) AND 255:POKE
680,(X1/2)/256:POKE 681,Y1
POKE 682,(X2/2) AND 255:POKE
683,(X2/2)/256:POKE 684,Y2
POKE 685,1:SYS 49608
X1=X2
Y1=Y2
NEXT X
NEXT Z
GET K$:IF K$="" THEN 1000
SYS 49266
CHAPTER 21
DISPLAY INTERRUPTS
The VIC-II chip is a very powerful device and
allows many different graphics displays to be
created. It is possible to extend these capabilities
to enable the creation of displays using several
different character sets, or more than eight sprites
for example - you can even have mixed text and
graphics.
This chapter deals wi th the techniques used in
creating such displays, but to understand fully you
must know how the VIC-I1 chip generates its
displays, and how a TV or monitor works.
TV Pictures
The pictures on a TV screen are created by an
electron beam which is directed at the phosphor
coated inner surface of the screen. Where this
beam strikes the screen the phosphor glows. To
create a full picture the electron beam scans across
the screen in rows, varying in intensity as it goes.
This variation in intensity is dictated by
information from the TV transmitter and produces
a corresponding variation in the intensity with
which the phosphor glows. When the electron
beam reaches the edge of the screen i t is turned off
and the process starts again from a position just
below the last starting position. This process,
called raster scanning, continues until the bottom
of the screen is reached, at which point it starts all
over again at the top. The process must happen
302
PRINT"{CLS}";PEEK(53266):
GOTO 10
Display Interrupts
303
304
Display Interrupts
396
420
421
422
423
424
425
NORMAL
STA SCREEN
RR2 PLA
TAY
PLA
TAX
PLA
RTI
JMP IRQVEC
305
change colour
restore values in registers
before interrupt
end
handle other interrupts
10
20
30
40
50
20000
20010
20020
20030
20040
306
Display Interrupts
307
,.**************
, *
;* 16 sprites *
, *
*
,.**************
,
POINTER=2040
YPOS=53249
SMCREG=53276
IRQLO=788
IRQHI=789
RASREG=53266
IENREG=53274
INTREG=53273
IRQVEC=59953
*=$COOO
SEI
LDA
STA
LDA
STA
#<PROG
IRQLO
#>PROG
IRQHI
308
230
240
242
244
245
246
247
250
260
270
300
310
320
330
340
350
360
370
380
390
400
410
420
421
422
424
425
426
427
430
440
450
500
510
520
540
550
570
580
581
582
LDA
STA
LDA
STA
LDA
AND
STA
CLI
RTS
PROGLDA
AND
BEQ
STA
LDA
BNE
LDA
STA
LDX
STD LDA
STA
DEX
BPL
LDX
LDA
Y1 STA
DEX
DEX
BPL
LDA
STA
JMP
MULT LDA
STA
LDX
LDA
LOOPSTA
DEX
BPL
LDA
LDX
#1
IENREG
#0
RASREG
53265
#127
53265
INTREG
#1
NOR
INTREG
RASREG
MULT
#145
RASREG
#7
#14
POINTER,X
STD
#14
#70
YPOS,X
Y1
#0
SMCREG
EXIT
#0
RASREG
#7
#13
POINTER,X
LOOP
#200
#14
STA
584 Y2
DEX
585
DEX
586
BPL
587
LDA
590
STA
600
610 EXIT PLA
TAY
620
PLA
630
TAX
640
PLA
650
RTI
660
670 NOR JMP
309
YPOS,X
Y2
#255
SMCREG
IRQVEC
NOTE:
10
20
30
40
50
20000
310
180
190
200
998
999
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1999
2000
2010
2020
2030
2040
2050
2060
2070
311
312
Display Interrupts
520
530
540
550
560
565
566
570
600 TEXT
610
620
700 EXIT
710
720
730
740
750
800 NORM
BEQ
STA
LDA
BNE
JSR
LDA
STA
JMP
JSR
LDA
STA
PLA
TAY
PLA
TAX
PLA
RTI
JMP
313
NORM
INTREG
RASREG
TEXT
HIRES
#200
RASREG
EXIT
LORES
#0
RASREG
IRQVEC
314
OTHERINTERRUPTS
In addition to setting Bit 0 when the current raster
position equals the stored raster position, the
interrupt status register provides an indication of
sprite collisions.
Bit 1 is set when asprite to data collision occurs
and will remain set until cleared. If the
corresponding bit in the interrupt enable register is
set, an interrupt will be generated. Similarly, Bit 2
will be set when asprite to sprite collision occurs.
No information is provided as to which sprite(s) are
involved - that must be done by your program. But
using interrupts means your pro gram does not
have to be constantly checking the sprite collision
registers so i t will run more quickly.
Interrupts are used in many ways by computers another example is in the pro gram in Chapter 22,
where movement of the joystick generates an
interrupt. This allows very smooth motion of the
cursor, and means that other pro grams can run at
the same time.
CHAPTER 22
USER FRIENDLINESS
There are two si des to making programs userfriendly. They must allow the user to select options
easily and quickly, and they must also tell the user
what options are available at all times. A program
may have supporting documentation, but the
screen display should present enough information
for the pro gram to be usable without referring to
the notes once the basics have been learnt.
Menus
One of the simplest and most effective ways of
assisting the user to control the pro gram is the
menu. This is a list of the options available, with
some means of indicating how to select an option.
The simplest form presents the list with a key
legend next to each item, indicating that the key
should be pressed to select that feature. The
function keys of the 64 work weIl with this type of
menu; other possibilities are to use the initial letter
316
317
SKETCHPAD PROGRAM
This program demonstrates the use of icons on the
64, using ajoystick to draw pictures in multicolour
bit-mapped mode.
90
100
105
110
120
130
140
150
160
170
180
990
318
1000
1010
1020
1030
1040
1050
1060
1100
1110
1120
1130
1140
1150
1160
1170
1990
2000
2010
2020
2030
2040
2045
2050
2055
2060
2065
2070
2075
2080
2090
2095
2099
2100
2105
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
319
320
2220
2230
2240
2990
3000
3005
3010
3020
3030
3040
3050
3060
3070
3080
3100
3110
3120
3990
4000
4005
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4990
5000
321
XL=PEEK(691): XH=PEEK(692):
Y=PEEK(693)
GOSUB 4200:POKE 685,0: GOSUB
4200: POKE685,FB
IF (PEEK(56320)AND16) <>16
THEN 4050
GOSUB 4200
IF X1+256*X2>XL+256*XH THEN
T1=X1: T2=X2: X1=XL: X2=XH:
XL=T1: XH=T1
IF Y1>Y THEN T=Y: Y=Y1: Y1=T
POKE 679,X1:POKE 680,X2: POKE
681,Y1
POKE 682,XL: POKE 683,XH: POKE
684,Y
SYS 50402
POKE 690,1: RETURN
POKE 682,X1: POKE 683,X2: POKE
684,Y1
POKE 679,XL: POKE 680,XH: POKE
681,Y1
SYS 49608
POKE 682,XL: POKE 683,XH: POKE
684,Y1
POKE 679,XL: POKE 680,XH: POKE
681,Y
SYS 49608
POKE 682,XL: POKE 683,XH: POKE
684,Y
POKE 679,X1: POKE 680,X2: POKE
681,Y
SYS 49608
POKE 682,X1: POKE 683,X2: POKE
684,Y
POKE 679,X1: POKE 680,X2: POKE
681,Y1
SYS 49608: RETURN
REM LOAD PICTURE
PRINT n{CLS}{WHT}{CD}n TAB(12)
n{RVS} 64 SKETCH PAD {ROF}n:
PRINT: PRINT: PRINT
322
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
7000
7010
7020
7030
323
324
325
326
Load Picture:
Save Picture:
Change Colours:
Stop Program:
327
IRQLO
IRQHI
XL
XH
Y
=
=
=
=
=
CURSORXL
CURSORXH
CURSORY
788
789
64 interrupt vector
stored here
679
680
681
=
=
=
691
692
693
IRQVEC
= 14
= 75
= 41
= 239
= 59953
PLOT
PLOTOK
=
=
XMIN
XMAX
YMIN
YMAX
$COB8
690
Maxand min
cursor co-ordinates
Normal interrupt
address
Plot routine
Plot on/off
328
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
JOYREG = 56320
SPRXL
SPRXH
SPRY
J1B
J2
J2B
J3
53248
53264
53249
$C56E
SETUP
PROG
=
=
=
SEI
LDA
STA
LDA
STA
CLI
RTS
LDA
LSR
BCS
PHA
LDA
CMP
BCC
DEC
PLA
LSR
BCS
PHA
LDA
CMP
BCS
INC
PLA
Joystick port
Cursor (sprite 0)
position registers
Starts at 50542
J2
SPRY
#YMIN
J1B
SPRY
Stick held up
Ifnotat top
moue cursor up
A
J3
Branch if bit 1 of
joystick port set
SPRY
#YMAX
J2B
SPRY
LSR A
BCS J4
Branch if bit 2
ofjoystick port set
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
J3A
J3B
J4
J4A
J4B
J5
PHA
LDA
AND
BNE
LDA
CMP
BCC
SEC
LDA
SBC
STA
BCS
LDA
AND
STA
PLA
SPRXH
#1
J3A
SPRXL
#XMIN
J3B
329
SPRXL
#1
SPRXL
J3B
SPRXH
#254
SPRXH
LSR
BCS
PHA
LDA
AND
BEQ
LDA
CMP
BCS
INC
BNE
LDA
ORA
STA
PLA
SPRXH
#1
J4A
SPRXL
#XMAX
J4B
SPRXL
J4B
SPRXH
#1
SPRXH
LSR
BCS
SEC
LDA
SBC
STA
SEC
LDA
A
EXIT
Branch ifbit4 of
joystick port set
J5
B ranch if bit 3 of
joystick port set
SPRY
Fire button down
#40
resetplotco-ordinates
CURSORY
SPRXL
330
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360 EXIT
1370
1380
1390
1400 RESET
1410
1420
1430
1440
1450
1460
SBC
STA
LDA
SBC
AND
STA
LDA
LSR
STA
LDA
ROR
STA
LDA
BEQ
LDA
STA
LDA
STA
LDA
STA
JSR
#12
CURSORXL
SPRXH
#0
#1
CURSORXH
CURSORXH
A
CURSORXH
CURSORXL
A
CURSORXL
PLOTOK Check ifplot allowed
EXIT
If not branch
CURSORXL
XL
CURSORXH
XH
CURSORY
Y
PLOT
Plot pixel
SEI
LDA
STA
LDA
STA
CLI
RTS
#<IRQVEC
IRQLO
#>IRQVEC
IRQHI
Disable routine
by resetting
interrupt vector
to point to
normal interrupt
handler
CHAPTER 23
Playi ng Tune s
To get the 64 to play tunes , you must 'feed' the SID
chip with the appro priate frequ ency data for the
notes comp rising the tune, in the correc t order and
at the correc t speed .
One way of doing this is show n in this exam ple:
1
10
20
30
40
50
500
GOSUB 1000
LO=I NT(R ND(1 )*256 )
HI=IN T(RN D(1)* 256)
GOSUB 500
FOR D=O TO 50:NE XT D
GOTO 10
REM CHANGE NOTE
332
510
520
530
999
1000
1030
1040
1050
1060
POKE 54272,LO
POKE 54273,HI
RETURN
REM SET UP SID CHIP
POKE 54296,15:REM VOL
POKE 54277,54:REM ATTACK/DECAY
POKE 54278,168:REM SUST/REL
POKE 54276,33:REM WAVEFORM
RETURN
3020
3030
3040
3050
333
GOSUB 1000
L$="%%%%?**%%/%"
H$="{CO}{CO}{CO}{CO}{HOM}
{RVS}{RVS}{CO}{CO}"+CHR$(16)+"
{CO}"
FOR Q=l TO LEN(L$)
LO=ASC(MIO$(L$,Q,l
HI=ASC(MIO$(H$,Q,l
GOSUB 500
FOR x=o TO 100:NEXT X
HI=O:LO=O:GOSUB 500
NEXT Q
STOP
REM CHANGE NOTE
POKE 54272,LO
POKE 54273,HI
RETURN
REM SET UP SIO CHIP
POKE 54296,15:REM VOL
POKE 54277,54:REM ATTACK/OECAY
334
1030
1040
1050
L$="32132121214"
FOR D=O TO 100*VAL(MID$(L$,Q,
1) ) : NEXT D
335
*=$COOO
VOICEILOW=54272
VOICEIHIGH=54273
ATTDEC=54277
SUSREL=54278
CONTROL=54296
NOTES=679
INIT
PLAY
LOOP
LDX
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
LDA
STA
INX
LDA
STA
INX
LDA
INX
TAY
JSR
LDA
STA
LDY
DEY
BNE
#0
#13
NOTES
#15
CONTROL
#54
ATTDEC
#168
SUSREL
#33
54276
TABLE,X
VOICEILOW
TABLE,X
VOICEIHIGH
TABLE,X
DELAY
#32
54276
#255
LOOP
336
1100
1110
1120
1130
1140
1150
1160
2000
2010
2015
2020
2030
2040
2050
5000
5010
QUIET
DELAY
TIME
DEC
BNE
LDA
STA
LDA
STA
RTS
LDA
STA
LDA
BPL
NOTES
PLAY
#32
54276
#0
54296
#100
162
162
TIME
DEY
BNE DELAY
RTS
TABLE BYT 177,25,2,177,25,2,
227,22,1,177,25,1,214,28
,2,154,21,4
BYT
63,19,4,37,17,2,37,17,
2,47,16,1,37,17,1,63,19,
2,107,14,4
337
OlM TO%(2,199),N$(199):Hl=0:
LO=O:GOTO 15000
REM
REM **************************
REM *
*
REM * CALCULATE NOTE, OCTAVE *
REM *
*
REM **************************
REM
N=ASC(LEFT$(Z$,1-65:lF N<O
OR N>6 THEN EF=l:GOTO 1040
0=ASC(MlO$(Z$,2,1-48:lF 0<0
OR 0>7 THEN EF=l:GOTO 1040
lF LEN(Z$)=2 THEN P=O:GOTO
1040
P=ASC(RlGHT$(Z$,l:lF P<>35
ANO P<>45 THEN EF=l
RETURN
REM
REM **************
REM *
*
REM * CALC PlTCH *
REM *
*
REM **************
REM
lFN>l THEN N=N-2:GOTO 2020
N=N+5
N=N*2
lF N>4 THEN N=N-1
lF P=35 THEN N=N+1
338
2050
2060
2992
2993
2994
2995
2996
2997
2998
3000
3010
3020
3030
9992
9993
9994
9995
9996
9997
9998
10000
10010
10020
10030
10040
10050
10060
14992
14993
14994
14995
14996
14997
14998
15000
15010
15020
15060
15070
15080
15085
15090
15095
15100
15110
339
GOSUB 3000
PRINT"HIGH FREQUENCY=":HI
PRINT"LOW FREQUENCY=":LO
TD%(O,Z)=HI:TD%(l,Z)=LO:
N$(Z)=Z$
GOSUB 10040: Z=Z+l
PRINT "PRESS ANY KEY TO
CONTINUE"
GET K$:IF K$="" THEN 15100
HI=O:LO=O:GOSUB 10040:GOTO
15010
PICTURES AT AN EXHIBITION
This program plays a piece of classical music in
three part harmony to demonstra te some of the
techniques covered in this chapter. A lot of typing
is involved hut the end result is worth it!
1
10
15
20
30
40
50
60
999
1000
1030
1040
1050
1060
1999
2000
2010
2020
2030
2099
3000
340
3010
3020
3050
10000
10010
10020
10025
10030
10040
10050
10055
10060
10070
341
342
343
344
10530 DA TA 19,63,15,70,11,114,19,63,
15,70,11,114,22,227,14,107,
4,208
10540DATA 17,37,14,107,4,208,19,
63,15,70,11,114,19,63,15,
70,11,114
10545 REM ************************
10550DATA 17,37,12,216,10,60,
17,37,12,216,10,205,14,107,11,
114,8,147
10560DATA 14,107,11,114,8,147,15,
70,11,114,9,159,15,70,11,114,
9,159
10570DATA 17,37,12,216,10,60,17,
37,12,216,10,205,14,107,11,
114,8,147
10580DATA 14,107,11,104,8,147,15,
70,11,114,9,159,19,63,11,114,
9,159
10585 REM ************************
10590DATA 17,37,12,216,10,205,17,
37,12,216,10,205,14,107,11,114
,9,159
10600 DATA 14,107,11,114,9,159,17,
37,12,216,10,205,17,37,12,216,
10,205
10700DATA 22,227,11,114,0,0,22,227,
11,114,0,0,20,100,15,70,12,216
10710DATA 19,63,0,0,9,159,17,37,14,
107,11,114,15,70,11,114,9,159
10715 REM ************************
10720DATA 17,37,11,114,3,155,17,37,
11,114,3,155,19,63,11,114,4,12
10730DATA 19,63,11,114,4,12,22,227,
17,37,14,107,22,227,17,37,
14,107
10740DATA 25,177,20,100,15,70,30,
141,0,0,15,70,22,227,11,114,
2,220
345
346
347
CHAPTER 24
349
350
TRACK NUMBER
NUMBER OF
SECTORS
1 - 17
21
18 - 24
20
25 - 30
18
31 - 35
17
351
352
353
354
This will not clear the disk, but will change the
directory he ader and make an the sectors available
for use so that you can write over any existing data.
TH E BLOCK AVAILABLITY MAP (BAM)
The DOS needs to know which of the sectors of a
disk have not yet been used, so that maximum use
can be made of the space available. To keep track
ofwhat's left, track 18 sector 0 is reserved for a map
of the available sectors of each track on the disko
This map is known as the block availablity map or
BAM.
The arrangement of data in the BAM is shown in
the diagram below:
BYTE
CONTENTS
0, 1
18,01
65
Not used
4 - 143
BAM
USE
355
356
followed by:
SAVE "MACHINE CODE",8
357
OPEN 15,8,15
INPUT#15,A$,B$,C$,D$
PRINT "ERROR CODE "A$
PRINT "ERROR TYPE "B$
PRINT"
TRACK "C$
PRINT"
SECTOR "D$
358
Initialise
The INITIALISE command (abbreviated to I)
returns the disk drive to its normal state by
copying the BAM into disk drive RAM. All files are
c10sed and error conditions cancelled. The syntax
IS:
PRINT#15,"I"
Validate
A VALIDATE operation will 'tidy up' and force the
DOS to re-organise the files on the disk and thus
make optimum use of the available disk space and
free any partially used blocks. The command is
abbreviated to V, and its format is:
PRINT#15,"V"
Scratch
You can delete files from the disk with the
SCRATCH (abbreviated to S) command - the
formatis:
PRINT#15,"S:FILE NAME"
359
Rename
It is possible to alter the directory entry for a file
using the RENAME (R) command:
PRINT#15,"R:NEW ENTRY=OLD ENTRY"
THEDOSSUPPORTPROGRAM
One of the pro grams on the demonstration disk
supplied with the 1541 disk drive is a short
machine code pro gram which supports the DOS
resident in the disk drive. Known as "DOS 5.1", its
function is to simplify disk handling commands.
The machine code is loaded into the 64 by a short
BASIC program called "C-64 WEDGE", which is
360
361
Error Status
When a disk error is indicated by the flashing red
LED, the status can be read by typing:
@ (or
362
PROGRAM NAME
PROGRAM NAME
363
@N:DISK NAME,ID
DOS 5.1 make s the life ofthe 1541 disk user much
easier , but it's not very conve nient to have to load it
into the comp uter from the demo nstra tion disk
every time you use the mach ine. It is a good idea to
copy it onto your own disks. To do this first load
DOS 5.1 into the 64 from the demo nstrat ion disk,
then type:
POKE 44,20 4:PO KE 43,0
POKE 46,20 7:PO KE 45,89
CHAPTER 25
365
366
367
368
BYTE
CONTENTS
Poi nter to T & S of next data block
0,1
2-255
CONTENTS
0, 1
Record Length
4,5
6, 7
8,9
10, 11
12,13
14, 15
16-255
369
370
OPEN 15,8,15
OPEN 2,8,2,"RELATIVE
TEST,L,"+CHR$(100)
PRINT#15,"P"CHR$(2)CHR$(100)
CHR$(O)
INPUT#15,A,B$,C,D
PRINT "ERROR"A;B$
PRINT#2,"THIS IS THE 100TH
RECORD"
CLOSE 15:CLOSE 2
END
371
372
PRINT#15,"P"CHR$(2)CHR$(15)CHR$(0)CHR$
(10)
INPUT"WHICH RECORD";R
PRINT#15,"P"CHR$(2)CHR$(R)
CHR$(O)CHR$(O)
GET#2,T$:IF T$=CHR$(255) THEN
PRINT "EMPTY RECORD":GOTO 150
373
374
Note that the parameters are separated by semicolons (;) and not commas as described in some
versions of the 1541 disk drive manual. The drive
number,Drv, is 0 for a single drive unit like the
1541 and can be omitted.
The following program demonstrates the use of the
BLOCK-READ command to display the contents
of any track and sector .
5
10
20
30
40
50
60
70
80
90
100
110
375
376
OPEN 15,8,15
OPEN 5,8,5,"#"
FOR 1=0 TO 25
PR1NT#5,CHR$(1+65)
NEXT 1
PR1NT#15,"B-W:"5;0;1;1
CLOSE 15:CLOSE 5
377
378
T=l:S=l
OPEN 15,8,15
OPEN 5,8,5,"#"
OPEN 4,8,4,"@0:INDEX,S,W"
INPUT"NUMBER OF ENTRIES";N
PRINT#4,N
FOR X=l TO N
PRINT"{CLS}ENTER DA TA FOR
ENTRY NUMBER";X
80
90
100
110
120
125
130
140
150
160
170
379
INPUT D$
PRINT#5,D$
PRINT#15,"B-A:"OiTiS
INPUT#15,A,B$,C,D
IF A=65 THEN T=C:S=D:GOTO 100
PRINT#15,"B-W:"5iOiTiS
PRINT#4,T","S
NEXT X
PRINT"BYEll"
CLOSE 4:CLOSE 5:CLOSE 15
END
OPEN 15,8,15
OPEN 5,8,5,"#"
OPEN 4,8,4,"INDEX,S,R"
INPUT#4,N
INPUT "WHICH RECORD TO
RETRIEVE"iNR
IF NR>N THEN PRINT"NO SUCH
RECORD l":GOTO 240
FOR X=l TO NR
INPUT#4,T,S
380
280
290
300
310
330
340
NEXT X
PRINT#15,"B-R:"5;0;T;S
INPUT#5,A$
PRINT A$
CLOSE 4:CLOSE 5:CLOSE 15
END
381
382
20
30
40
50
60
70
80
90
100
110
120
130
140
150
OPEN 15,8,15
OPEN 5,8,5,"#"
OPEN 4,8,4,"B-P INDEX,S,R"
INPUT"FIELD TO VIEW (0-10)";R
IF R<O OR R>10 THEN 30
BP=R*20+1
INPUT#4,T,S
PRINT#15,IB-R:"5;0;T;S
PRINT#15,IB-P:"5;BP
INPUT#5,A$,N
PRINT"{CLS}FIELD"R"CONTAINS"A$
PRINT "AND STARTS AT BYTE"N
CLOSE4:CLOSE5:CLOSE15
END
383
HOME BASE
1
2
3
4
5
6
7
8
9
10
20
40
50
60
70
80
100
110
120
130
135
140
150
199
200
210
220
REM *************
REM *
*
REM * HOME BASE *
REM *
*
REM *************
REM
REM REQUIRES 1541 OISK ORIVE
REM
PRINT"{CLS}"
GOSUB 10000
GOSUB 240
PRINT TAB(13);"{RVS} HOME BASE
{ROF}{CO}{CO}{CO}{CO}"
PRINT TAB(9)"{RVS}I{ROF}
CREATE NEW FILE{CO}"
PRINT TAB(9)"{RVS}2{ROF}
ENTER RECORO{CO}"
PRINT TAB(9)"{RVS}3{ROF}
SEARCH FOR RECORO{CO}"
PRINT TAB(9)"{RVS}4{ROF}
EXIT{CO}{CO}{CO}{CO}{CO}"
PRINT L$
PRINT TAB(10);"{OKGRY}ENTER
CHOICE {RVS}I{ROF} TO
{RVS}4{ROF}{GRN}";
GET A$:IF A$="" THEN 120
IF A$<"I" OR A$>"4" THEN 120
M$=BK$:GOSUB 300
ON ASC(A$)-48 GOSUB 1000,4000,
5000,6000
GOTO 20
REM REAO ERROR CHANNEL
OPEN 15,8,15
INPUT#15,A$,B$,C$,O$
CLOSE 15
384
230
239
240
250
260
270
299
300
310
320
330
349
350
360
993
994
995
996
997
998
999
1000
1010
1020
1030
1040
1050
1060
1080
RETURN
REM CREATE SCREEN LAYOUT
PRINT"{GRN}{CLS}{ROF}CHR$
(169){14 SPACES}CHR$(223)
{RVS}{24 SPACES}"
PRINT"{16 SPACES}{{RVS}{24
SPACES} {ROF} " i
PRINTTAB(39)iCHR$(223)
RETURN
REM OISPLAY M$ ON STATUS LINE
PRINT"{HOM}{ 23 * CO}"iSPC((
40-LEN(M$/2)iM$i"{GRN}":IF
NB=l THEN 330
FOR N=l TO NU:PRINT"{CU}" i:
NEXT N
PRINT BK$"{CU}"
NU=O:RETURN
REM ORAW HORIZ LINE
PRINT"{HOM}{21 * CO}"iL$i
"{HOM}{CO}{CO}"
RETURN
REM
REM *********************
REM *
*
REM * CREATE A NEW FILE *
REM *
*
REM *********************
REM
GOSUB 240
PRINT TAB(10)i"{RVS} CREATE A
NEW FILE {ROF}{CO}{CO}
{CO}{CO}"
INPUT" FILE NAME 15 CHARS)"i
FT$
IF LEN(FT$14 THEN 1000
IF FT$ = "" THEN RETURN
PRINT:PRINT:INPUT" HOW MANY
FIELOS/RECORO ll)"iNF
IF NF=O OR NF>10 THEN
PRINT"{CU}{CU}{CU}":GOTO 1050
FOR Z=l TO NF
1090
1100
1105
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1255
1260
1270
1275
1280
1290
1300
1310
385
386
1320
1330
1340
1350
3993
3994
3995
3996
3997
3998
3999
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4220
PRINT#4,N$(Z)","FL(Z)","
NEXT Z
CLOSE 4
RETURN
REM
REM ********************
REM *
*
REM * ENTER NEW RECORD *
REM *
*
REM ********************
REM
IF LEN(FT$)<>O THEN 4100
OPEN 4,8,4,"INDEX,S,R"
GOSUB 200
IF B$="OK" THEN CLOSE 4:GOTO
4060
NU=5: M$="{RED}"+B$:GOSUB 300
:FOR Q=O TO 1000:NEXT Q
CLOSE 4:RETURN
OPEN 4,8,4,"INDEX,S,R":
INPUT#4,FT$,FR,NF,RL
FOR Z=l TO NF
INPUT#4,N$(Z),FL(Z):NEXT Z
CLOSE 4
FOR Z=l TO NF:GOSUB 240:GOSUB
350
PRINT "{HOM}{CD}{CR}{DKGRY}"
FT$"{GRN}"
PRINT
TAB(ll) i "{CD}{CD}{RVS}ENTER A
RE CORD {ROF}{CD}{CD}{CD}{CD}"
M$="MAX FIELD LENGTH ="+STR$
(FL(Z:NU=16:GOSUB 300
PRINT N$(Z)i
INPUT R$(Z)
IF LEN(R$(Z<FL(Z) THEN 4220
NB=l:M$="{RED}FIELD LENGTH
EXCEEDED":GOSUB 300:NB=0
FOR T=O TO 400:NEXT T:GOTO
4110
NEXT Z
4230
4240
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4355
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4993
4994
4995
4996
387
388
4997
4998
4999
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5185
5190
5200
5210
REM *
*
REM *********************
REM
SE$="":Y=O:PO=O:IF LEN(FT$)<>O
THEN 5100
OPEN 4,8,4,"INOEX,S,R"
GOSUB 200
IF B$="OK" THEN CLOSE 4:GOTO
5060
NU=5: M$="{REO}"+B$:GOSUB 300
:FOR Q=O TO 1000:NEXT Q
CLOSE 4:RETURN
OPEN 4,8,4,"INOEX,S,R":
INPUT#4,FT$,FR,NF,RL
FOR Z=l TO NF
INPUT#4,N$(Z),FL(Z):NEXT Z
CLOSE 4
GOSUB 240:GOSUB 350
PRINT "{HOM}{CO}{CR}{OKGRY}
"FT$"{GRN}"
PRINT TAB(10);"{CO}{CO}{RVS}
SEARCH FOR RECORO {ROF}{CO}
{CO}{CO}{CO}"
FOR Z=l TO NF
PRINT "{RVS}"CHR$(Z+64)"{ROF}
";N$(Z)
NEXT Z
M$="tOKGRY}
SELECT FIELO FOR
SEARCH {RVS}A{ROF} TO "+
"{RVS}"+CHR$(NF+64)+"{ROF}"
NB=l:GOSUB 300:NB=0
GET A$:IF A$="" THEN 5180
IF A$<"A" OR A$>CHR$(NF+64)
THEN 5180
F=ASC(A$)-64:GOSUB 240:GOSUB
350:T$=""
PRINT "{HOM}{CO}{CR}{OKGRY}
"FT$"{GRN}"
PRINT TAB(10);"{CO}{CO}{RVS}
SEARCH FOR RECORO {ROF}{CO}
{CO}{CO}{CO}"
5220
5225
5230
5260
5300
5310
5315
5320
5330
5350
5360
5399
5400
5405
5410
5415
5420
5430
5440
5499
5500
5505
5510
5511
5515
5520
389
390
5530
5540
5550
5560
5565
5570
5580
5600
5610
5620
5630
5640
5700
5710
5720
5730
5740
5750
5755
5760
5770
5775
5780
5799
PO=PO+FL(Q-1)
PRINT#15,"P"CHR$(2)CHR$(LO)
CHR$(HI)CHR$(PO)
INPUT#2,R$(Q-1)
PRINT BK$"{CR}{CU}{RVS}";
CHR$(Q+64)"{ROF} "N$(Q);"
";R$(Q-1)
NEXT Q
IF Y=l THEN 5700
IF Z=Y-1 THEN Z=-l
M$=BK$:GOSUB 300:NB=1:
M$="{DKGRY}
SHOW NEXT
RECORD {RVS}Y{ROF} OR
{RVS}N{ROF}?":GOSUB 300:NB=0
GET YN$:IF YN$="" THEN 5610
IF YN$="N" THEN AR=Z:Z=Yl:GOTO 5700
IF YN$<>"Y" THEN 5610
NEXT Z
M$=BK$:GOSUB300:NB=1:M$=
"{DKGRY}
{RVS}A{ROF}MEND
{RVS}DEL{ROF}ETE OR {RVS}
RETURN{ROF}?":GOSUB 300:NB=0
GET YN$:IF YN$="" THEN 5710
IF YN$="A" THEN 5800
IF YN$=CHR$(13) THEN 5780
IF YN$<>CHR$(20) THEN 5710
M$=BK$:GOSUB300:M$="{DKGRY}
{RVS} ARE YOU SURE? {ROF}"
:NB=1:GOSUB300:NB=0
GET YN$:IF YN$="" THEN 5755
IF YN$<>"Y" THEN 5700
PO=l:FOR J=l TO NF:FOR K=l TO
FL(J)-l:DE$=DE$+" u:NEXT K:
PO=PO+FL(J-1)
PRINT#15,"P"CHR$(2)CHR$(LO)
CHR$(HI)CHR$(PO):PRINT#2,DE$:D
E$="":NEXT J
Y=0:PO=0:CLOSE2:CLOSE15:RETURN
REM AMEND RE CORD
5800
5805
5810
5820
5830
5840
5845
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
391
PRINT"{HOM}{CD}{CO}{CO}{CO}
IBK$I{CR}{CU}"TAB(14} 11 {RVS}AME
NO RECORO{ROF}"
M$=BK$:GOSUB 300:M$="{OKGRY}
{RVS}A{ROF}MEND WHICH FIELO?"
:NB=l:GOSUB 300:NB=0
GET YN$:IF YN$="" THEN 5810
IF YN$<"A" OR YN$> CHR$(NF+64}
THEN 5810
GOSUB 240:GOSUB 350:PRINT"
{HOM}{CO}{CR} {OKGRY} "FT$" {GRN}
"
PRINT TAB(14}:"{CO}{CD}{RVS}
AMEND RECORO {ROF}{CO}{CO}"
Q=ASC(YN$)-64
PRINT N$(Q);SPC(2):R$(Q-1)
M$=BK$:GOSUB 300:M$="MAX FIELO
LENGTH ="+STR$(FL(Q)}:NB=l:
GOSUB 300:NB=0
PRINT"{HOM}{10 * CO}"N$(Q};
SPC(2);:INPUT R$(Q-1}
GOSUB 240:GOSUB 350
PRINT "{HOM}{CO}{CR}{OKGRY}
"FT$"{GRN}"
PRINT TAB(14};"{CO}{CD}{RVS}
AMENO RECORO {ROF}{CO}{CO}"
FOR Y=l TO NF
PRINT N$(Y};SPC(1};R$(Y-1}:
NEXT Y
M$="{RVS}{OKGRY}Y{ROF} TO
ACCEPT OR {RVS}N{ROF} TO
REJECT":NB=l:GOSUB 300:NB=0
GET YN$:IF YN$="" THEN 5940
IF YN$="N" THEN RETURN
IF YN$<>"Y" THEN 5940
PO=l:FOR y=o TO NF-1:PO=PO+
FL(Y}
PRINT#15,"P I CHR$(2}CHR$(LO}
CHR$(HI}CHR$(PO}
PRINT#2,R$(Y}:NEXT Y:CLOSE2:
CLOSE15
392
5995
6000
9993
9994
9995
9996
9997
9998
9999
10000
10010
RETURN
PRINT"{CLS}BYEll":END
REM
REM *******************
REM *
*
REM * SET UP ROUTINES *
REM *
*
REM *******************
REM
POKE 53280,5:POKE 53281,15
FOR Z=O TO 39:L$=L$+CHR$(192):
NEXT Z
10020 FOR Z=O TO 38:BK$=BK$+
CHR$ ( 32 ) : NEXT Z
10100 RETURN
MainMenu
The main menu comprises four options:
1
ENTER ARECORD
SEARCHFORRECORD
EXIT
393
En ter aReco rd
394
Amending aRecord
The field to be amended is selected by reference
letter and the new data entered. The amended
record will be displayed for you to accept or reject.
Pressing 'Y' will save the amended record and the
main menu will be redisplayed.
Deleting aRecord
If you opt to delete arecord and press 'Y' in
response to the ARE YOU SURE? prompt, the
re cord will be deleted and the main menu
redisplayed.
395
396
File name
N umber of fields/record
Array of field names
Array of field lengths
Record length
Horizontalline
Message for status line
Blankline
Next free record number
R$(l)
397
398
BLOCK-EXECUTE (B-E)
This command allows you to load machine code
routines from the disk into disk drive RAM and
execute them. It is similar to the B-R command
except that after loading the code, the disk drive's
microprocessor begins to execute it. The program
must end in an RTS (ReTurn from Subroutine)
instruction. The format ofthe command is:
PRINT#15,"B-E:"Ch;Dr;T;S
399
400
USER
OPERATION
U1 or UA
B-Rcommand
U2 or UB
B-Wcommand
U3 or UC
Jump to $0500
U40r UD
Jump to $0503
U5 or UE
Jump to $0506
U6 or UF
Jump to $0509
U7 or UG
Jump to $050C
U8 or UH
Jump to $050F
U9 or UI
Jump to $FFFA
U; or UJ
Power up Vector
401
POKE 53265,27
402
CHAPTER 26
404
Testing
The MPS801 is provided wi th a test facili ty - insert
some paper and move the three position switch at
the rear ofthe printer to tT'. The printer will print
the complete 64 character set and continue to do so
until you switch off or move the switch away from
the tT' position.
where:
LFn is the logical file number (any number from 0
to 255) which is used to reference the channel.
405
OPEN 4,4
FOR Z=l TO 26
PRINT Z,CHR$(Z+64)
PRINT#4,Z,CHR$(Z+64)
NEXT Z
CLOSE 4
END
406
407
PRINTING MODES
The MPS801 operates in various printing modes
selected by control characters in the data sent to
the printer. The table below shows the print modes
and the control characters used to select them.
The characters in the above table are sent to the
printer in PRINT# commands and interpreted by
the printer as control characters. The mode so
selected will continue to be the printing mode until
a further control character is detected by the
printer.
Cursor U p and Cursor Down
The characters 'cursor up' (CHR$(145 and 'cursor
down' (CHR$(17 select which of the two 128
character sets is to be used for printing. The two
fonts are the same as those selected on the 64 by
holding down the SHIFT key and pressing the
CBM key. The following program illustrates the
difference:
10
20
30
408
CODE
FUNCTlON
145
17
Cursor up mode
Cursor down mode
Graphics mode
16
18
146
14
10
Une feed
13
27, 16
Carriage return
Specify dot address
26
Repeat graphicdata
Reverse on
Reverse off
NOTE:
NEXT Z
OPEN 4,4:PRINT#4,A$
CLOSE 4
OPEN 4,4,7
PRINT#4,A$
CLOSE 4
END
Graphics Mode
Graphics mode (CHR$(8 allows the printing of
user defined characters.
409
8
16
32
64
2
4
8
16
32
64
18
34
104
104
34
18
(+ 128)
410
OPEN 4,4
FOR N=O TO 30
FOR R=O TO 6
D$=CHR$2tR)+128)
PRINT#4,CHR$(8)D$
NEXT R
FOR R=6 TO 0 STEP -1
D$=CHR$2tR)+128)
PRINT#4,CHR$(8)D$i
NEXT R
NEXT N
CLOSE 4
411
OPEN 4,4
A$="REVERSED TEXT"
FOR I=l TO LEN(A$}
R=l-R
IF R=O THEN R$=CHR$(146}:GOTO
70
R$=CHR$(18}
PRINT#4,R$;MID$(A$,I,1};
NEXT I
CLOSE 4
END
412
10
20
30
40
50
OPEN 4,4
PRlNT#4, "LlNE 1"
FOR z=o TO 9
PRlNT#4,CHR$(10)
NEXT Z
PRlNT#4,"LlNE 12"
413
OPEN 4,4
PRINT#4,CHR$(8)CHR$(26)CHR$
(100)CHR$(255)
CLOSE 4
OPEN 4,4:PRINT#4,CHR$(15)
PRINT#4,TAB(30)"A CENTRED
HEADING"
PRINT#4,TAB(29)CHR$(8)CHR$
(26)CHR$(114)CHR$(129)
CLOSE 4:END
SereenDump
The printer manual contains a program to output
the contents of a standard screen to the printer, but
414
OPEN 4,4:PRINT#4,CHR$(15)
FOR A=1024 TO 2023 STEP 40
FOR X=O TO 39
O=PEEK(X+A)
IF 0>127 THEN 0=0-128:
R$=CHR$(18):O$=CHR$(146)
P=0-((0<32 OR D>95)*64)-((0>63
ANO 0<96)*32)
P$=P$+R$+CHR$(P)+O$
R$="":O$=""
NEXT X
PRINT#4,P$
P$="":NEXT A
CLOSE 4:END
415
1****************
1* PRINTER DEMO *
1****************
1
ORG $COOO
LDA
JSR
LDA
TAX
LDY
JSR
JSR
LDX
JSR
160
170 OP
180
190
200
210
220
230
240 TAB
250
270
280
290
300
310
SETNAM
SETLFS
OPEN
CHKOUT
CHROUT
start address
#0
SETNAM 0 = no {ilename
LFn=4
#4
Device No. = 4
No Secondary Add
#255
SETLFS Set up Logical File
Open{ile
OPEN
LFn =4
#4
CHKOUT Open channel tor
output
LDX #0
Initialise counter
LDA TAB,X Get character {rom
table
JSR CHROUT Output to printer
INX
Increment counter
Last item in table?
CPX #34
BNE OP
No, then get next
character
JSR CLALL All done so dose all
channels
Back to BASIC
RTS
BYT 80,82,73,78,84,69,
68,32,70,82,79,77,32,77,
65,67,72,73,78,69
BYT 32,67,79,68,69,32,
80,82,79,71,82,65,77,13
= 65469
= 65466
= 65472
= 65481
= 65490
416
320 CLALL
= 65511
10
20
30
40
50
20000
20010
20020
20030
20040
APPENDICES
APPENDIX 1
ABBREVIATIONS
LlSTlNG
MEANING
KEYS TO PRESS
{BLK}
BLACK
CTRLand 1
{WHT}
WHITE
CTRL and 2
{RED}
RED
CTRL and 3
{CYN}
CYAN
CTRLand 4
{PUR}
PURPLE
CTRLand 5
{GRN}
GREEN
CTRL and 6
{BLU}
BLUE
CTRL and 7
{YEL}
YELLOW
CTRL and 8
{ORG}
ORANGE
CBM and 1
{BRN}
BROWN
CBM and 2
{LTRED}
LIGHT RED
CBM and 3
{DKGRY}
DARK GREY
CBM and 4
{GRY}
GREY
CBM and 5
{LTGRN}
LIGHT GREEN
CBM and 6
{LTBLU}
LIGHT BLUE
CBM and 7
{LTGRY}
LlGHTGREY
CBM and 8
{RVS}
REVERSE VIDEO ON
CTRLand 9
{ROF}
CTRLand 0
{CLS}
CLEAR SCREEN
{HOM}
CURSOR HOME
CLR/HOME
{CU}
CURSOR UP
{CD}
CURSOR DOWN
{CL}
CURSOR LEFT
{CR}
CURSOR RIGHT
CRSR ~
SHIFT and
CRSR ..
CRSR ..
APPENDIX 2
BASIC COMMANDS
ABS(N)
AND
ASC (C$)
ATN(N)
N.
Chapter8
CHR$(N)
BASIC Commands
421
CLOSE(N)
Closes a channel, N, to a
peripheral device (disk,
printer, etc)
Chapter 15
CLR
CMD(N)
CONT
COS(N)
DATA
DEFFNA(N)
definable
422
DIM A(L,M)
END
EXP (N)
FNA (N)
FOR
GETN
BASIC Commands
423
GOSUB
GOTO
IF (condition)
THEN (action)
INPUTN
INPUT <data";N
INPUT#(N)
424
returns 3.
Chapter8
LEFT$(C$,N)
LEN(C$)
LET
and
P = 5
LOADA$,D,A
BASIC Commands
425
LOG(N)
MID$ (C$,X,N)
Returns N characters of
string beginning at the Xth
character.
Chapter7
NEW
NEXTN
NOT
ONNGOSUB
ONNGOTO
OR
426
B OR C
A is true if B or C is true.
Can also act on binary values
ofnumbers.
Chapter9
PEEK(LOC)
POKE LOC,N
POS (0)
PRINT#N
READ
BASIC Commands
427
REM
Allows remarks to be
inserted in pro grams as an
aid to clarity. Remarks are
i_gnored when program is run.
Chapter4
RESTORE
RETURN
RIGHT$ (C$,N)
RND(N)
RUN
428
SAVEA$,D,A
SGN(N)
Returns 1, 0 or -1 according
to whether number is
positive, zero or negative.
Chapter8
SIN
SPC (N)
SQR(N)
STATUS
STOP
STR$ (N)
BASIC Commands
429
SYS(N)
TAB(N)
TAN(N)
THEN
See IF.
Chapter5
TIME
TIME$
TO
See FOR.
Chapter5
USR(N)
430
VAL (C$)
VERIFY
Compares a specified
pro gram fi le wi th the
program currently in
me~ory to check for correct
savlng.
Chapter 15
W AlT LOC,X,Y
APPENDIX 3
COMMAND
ABS
AND
ASC
ATN
CHR$
CLOSE
CLR
CMD
CONT
COS
DATA
DEF
DIM
END
EXP
FN
FOR
FRE
GET
GET#
GOSUB
GOTO
IF
aB
aN
aS
aT
eH
clO
cL
cM
cO
dA
dE
dl
eN
eX
fO
fR
gE
gaS
gO
HEX TOKEN
182
175
198
193
199
160
156
157
154
190
131
150
134
128
189
165
129
184
161
B6
AF
C6
C1
C7
AO
9C
9D
9A
BE
83
96
86
80
BD
A5
81
B8
A1
141
137
139
8D
89
8B
432
INPUT
INPUT#
INT
LEFT$
LEN
LET
LIST
LOAD
LOG
MID$
NEW
NEXT
NOT
ON
OPEN
OR
PEEK
POKE
POS
PRINT
PRINT#
READ
REM
RESTORE
RETURN
RIGHT$
RND
RUN
SAVE
SGN
SIN
iN
leF
IE
11
10
ml
nE
nO
oP
pE
pO
?
pR
rE
reS
reT
rl
rN
rU
sA
sG
si
133
132
181
200
195
136
155
147
188
202
162
130
168
145
159
176
194
151
185
153
152
135
143
140
142
201
187
138
148
180
191
85
84
B5
C8
C3
88
9B
93
BC
CA
A2
82
A8
91
9F
BO
C1
97
B9
99
98
87
8F
8C
8E
C9
BB
8A
94
B4
BF
433
SPC
SQR
STATUS
STEP
STOP
STR$
SYS
TAB
TAN
THEN
TIME
TIME$
TO
USR
VAL
VERIFY
WAIT
+
*
/
=
NOTE:
sP
sQ
ST
stE
sT
stR
sY
tA
tH
TI
TI$
uS
vA
vE
wA
166
186
A6
BA
169
144
196
158
163
192
167
A9
90
C4
9E
A3
CO
A7
164
183
197
149
146
170
171
172
173
178
A4
B7
C5
95
92
AA
AB
AC
AD
B2
APPENDIX 4
COMMAND
FORMAT
NEW
N: DISK NAME, ID
COPY
c:
RENAME
SCRATCH
S: FILE NAME
INITIALISE
VALIDATE
BLOCK-READ
BLOCK-WRITE
BLOCK ALLOCATE
BLOCKFREE
"B-F:" Drive;Track;Block
BUFFER POINTER
POSITION
BLOCK-EXECUTE
MEMORY-READ
"M-R:" CHR$(Low)CHR$(High)
MEMORY-WRITE
"M-W:"CHR$(Low)CHR$(High)CHR$(no.
of Chars)
USER
"Un:"
APPENDIX 5
BADDATA
436
BAD SUBSCRIPT
An array element outside the dimensions of that
array has been accessed, or the wrong n umber of
dimensions has been used, for example:
Z(9, 9 , 9 )
..
when Z is a two-dimensional
array.
BREAK
Displayed when pro gram is halted with the
RUN/STOP key.
CAN'T CONTINUE
You may not continue the RUNning of a program,
using the command CO NT, if the pro gram has
been edited in any way.
DEVICE NOT PRESENT
Displayed when the specified device is not
connected to the computer, and an attempt has
been made to access files on that device.
DIVISION BY ZERO
It is impossible to divide by zero.
EXTRAIGNORED
An attempt has been made to enter too many items
in response to an INPUT statement. The extra
data is not considered.
437
438
LOAD
The computer has been unable to LOAD the
program from tape and has aborted the attempt.
NEXTWITHOUTFOR
The variable in a NEXT statement has no
corresponding FO R statement or FO R .. NEXT
loops have been incorrect1y nested.
NOT INPUT FILE
An attempt has been made to read data from a file
which has previously been specified as a write only
file.
NOT OUTPUT FILE
An attempt has been made to write data to a file
which has previously been specified as a read only
file.
OUTOFDATA
This error occurs when a READ command is
executed, but there is either no DATA, or all the
DAT A has previously been read. The pro gram
either tried to read too much data, or there was
insufficient data in the DAT Astatement.
OUT OF MEMORY
Either the program is too large, or you have used
too many variables, too many FOR ... NEXT loops,
too many GOSUBs, or you have allocated too much
space for arrays with the DIM command.
439
OVERFLOW
The result of a calculation, or a direct1y entered .
number, was larger than 1.70141 * E38. If an
underflow occurs, the result is given as zero, and no
error message is displayed.
REDIM'D ARRA Y
After an array was DIMensioned, a statement
DIMensioning the same array was encountered.
REDO FROM START
Astring function has been entered when the
program was executing an INPUT statement. The
message will be repeated until a number is entered.
RETURN WITHOUT GOSUB
An attempt has been made to execute aRETURN
statement which was not preceded by a GOSUB
statement.
STRING TOO LONG
Astring has been concatenated - made up from
several smaller strings joined together - and i ts
length has exceeded 255 characters.
SYNTAX
This can be caused by :
Use of a non-existent (or mispelled)
BASIC command
Missing brackets
Incorrect punctuation
Missing parameters
Illegal characters
440
TYPE MISMATCH
An attempt has been made to assign a numeric
value to astring variable, or vice-versa . A
numeric argument was passed to a function
requiring astring argument, or vice-versa. For
example:
PR INT ASe ( A) instead of
PRINT Ase ( "A" )
or
A=Z$
A$ = 2
instead of A=Z
instead of A$ = " 2 "
UNDEF'D FUNCTION
An attempt to reference an undefined user defined
function has been made.
UNDEF'D STATEMENT
The pro gram attempted to GOTO, GOSUB,
THEN or RUN anon-existentstatement.
VERIFY
The program on tape is not the same as the
program currently in memory.
APPENDIX 6
442
25 WRITE ERROR
The data block written to disk has been checked
against that in DOS memory and been found to be
incorrectly written.
26 WRITE PROTECT ON
The DOS has detected the presence of a wri te
protect tab over the wri te protect notch.
27 READ ERROR
A checksum error in the header for a data block has
been detected and the block has not been
transferred to DOS memory - possibly due to poor
earthing connections.
28 WRITE ERROR
A time-out error has occurred as the DOS tried to
locate the sync byte for a data block - caused by
bad disk format or hardware failure.
29 DISK ID l\'IISl\'IATCH
The DOS has detected a non initialised disko
30 SYNTAXERROR
The command sent to DOS via Channel 15
illegal.
31 SYNTAXERROR
The command sent to DOS is invalid.
IS
443
32 SYNTAXERROR
The command se nt to DOS contains more than 58
characters.
33 SYNTAXERROR
The file name in a command is invalid.
34 SYNTAXERROR
Either no file name was sent or the syntax of the
command was incorrect causing the DOS to
misinterpret the command.
39 SYNTAXERROR
An unrecognisable command has been sent to DOS.
51 OVERFLOW IN RECORD
An attempt has been made to write too much data
to arecord in a relative file. Remember that the
carriage return terminator counts as one character.
444
445
70 NO CHANNEL
The requested channel is not available, or all
channels are in use.
71 DIRECTORYERROR
A discrepancy exists between the BAM count and
the directory - possibly caused by overwriting the
BAM. Re-initialise the disk to force DOS to recreate the BAM.
72 DISKFULL
Either no blocks are available or the maximum of
144 directory entries has been reached.
73 DOS MISMATCH
An attempt has been made to write to a disk
formatted under a non-compatible DOS.
APPENDIX 7
SPEEDING UP PROGRAMS
There are several things you can do to increase the
running speed of your BASIC pro grams.
Unfortunately, this is usually at the expense of
clarity, and you may find it useful to keep a 'slow',
but easy-to-follow version ofyour program should
you wish to amend it at a later date.
Speeding Up Programs
447
APPENDIX 8
COLOUR CODES
COLOUR
CODE
BLACK
WHITE
RED
CYAN
PURPLE
GREEN
BLUE
ORANGE
6
7
8
BROWN
LIGHT RED
10
LIGHT GREY
11
MEDIUM GREY
12
LIGHT GREEN
13
LIGHT BLUE
14
DARKGREY
15
YELLOW
NOTE
APPENDIX 9
MUSICAL NOTATION
This appendix will not teach you all about music,
but it contains the basic infonnation you need to
translate sheet music into 64 programs.
Music is written by positioning symbols which
represent the length of notes on a framework
(called astave) representing the pitch.
G
-------------------------E
_________________________ c F
D
_________________________ A
_________________________ F
D E
_________________________ B
C
A
-------------------------EGC F
-------------------------A DB
F
j
)
is twice as long as
which is twice as
longas
which is twice as
long as
which is twice as
long as
450
J. = J_J
The mark
is a tie which means the notes
are joined together.
J_J =J
Volume is indicated by markings below the stave.
ff
f
Veryloud
Loud
mf
Moderately loud
mp
Moderately soft
p
Soft
pp Verysoft
<::::::::
Getlouder
~ Get softer
Musical Notation
Presto
Allegro
Allegro moderato
Moderato
Andante
Largo
451
Pictures at an Exhibition
Here is the beginning of Pictures at an Exhibition,
one of the examples used in Chapter 7. The notes
'p ~
j Jj
tJ rIcJ rrrj J I
I -
452
APPENDIX 10
NUMBERING SYSTEMS
Computers store and operate upon numbers in a
different way from humans - they use a numbering
system known as Binary Notation.
Binary notation is a means of representing
quantities with groups of ls and Os. We are more
used to a system called Decimal Notation, in which
quantities can be represented by combinations of
up to ten symbols (the numbers 0 to 9).
Computers use the binary system because they are
able to recognise and differentiate between only
two states - ON and OFF. These two states can
conveniently be represented by 1 (ON) and 0 (OFF).
A single 1 or 0 is called a BInary digiT, or BIT, and
computers store data in the form of groups of eight
ofthese bits, known as BYTES.
In the computer memory, one memory location is
able to store one Byte of data (eight bits). A
collection of 1024 of these bytes is called a
KILOBYTE, or k for short. We can get an idea of
the data storage capacity of a computer from the
number ofk ofmemory it has (48k, for example, is
48 * 1024 * 8 bits).
We have described a byte as a collection of eight
bits, like this:
11111111
Numbering Systems
454
means:
2*100 + 5*10 + 5*1
4 2 1
111
255
455
=
=
=
=
=
=
=
=
85
42
21
10
5
2
1
0
remainder
0
1
0
1
0
1
0
1
(reading upwards)
ADDITION OF BINARYNUMBERS
Binary numbers can be added together in the same
way as decimal numbers. An example will make
this clear.
To perform the sum:
105
--.li
124
456
Numbering Systems
01101001
00010011
01111100
01100100
00110010
50
00110010
00110010
11001101
11001110
457
01100100
11001110
00110010
HEXADECIMAL
Manipulating numbers in binary is a lot easier for
a computer than it is for a human, and one way in
which binary numbers can be made more digestible
is by representing them in hexadecimal notation,
or hex.
Hex is a system of counting in base 16, using the
symbols 0 to 9 and A to F as follows
DECIMAL 0 1. .. 9 10 11 15 16 17
HEX
0 1 9
AB... F 10 11
APPENDIX 11
KERNAL ROUTINES
ACPTR
$FFA5
65445
CHKIN
$FFC6
65478
CHKOUT
$FFC9
65481
CHRIN
$FFCF
65487
CHROUT
$FFD2
65490
CIOUT
$FFA8
65488
Kernal Routines
459
CINT
$FF81
65409
CLALL
$FFE7
65511
CLOSE
$FFC3
65475
CLRCHN
$FFCC
65484
GETIN
$FFEA
65508
lOBASE
$FFF3
65523
IOINIT
$FF84
65412
460
LISTEN
$FFBI
65457
LOAD
$FFD5
65493
MEMBOT
$FF9C
65436
MEMTOP
$FF99
65433
OPEN
$FFCO
65472
PLOT
$FFFO
65520
Kernal Routines
RAMTAS
$FF87
461
65415
RDTIM
$FFDE
65502
READST
$FFB7
65463
RESTOR
$FF8A
65418
SAVE
$FFD8
65496
SCNKEY
$FF9F
65439
SCREEN
$FFED
65517
462
SECOND
$FF93
65427
SETLFS
$FFBA
65466
SETMSG
$FF90
65424
SETNAM
$FFBD
65469
SETTIM
$FFDB
65499
SETTMO
$FFA2
65442
STOP
$FFEI
65505
Kernal Routines
TALK
$FFB4
463
65460
TKSA
$FF96
65430
UDTIM
$FFEA
65514
UNLSN
$FFAE
65454
UNTALK
$FFAB
65451
VECTOR
$FF8D
65421
APPENDIX 12
AND
Performs the logieal AND operation between the
aeeumulator and data. Deposits the result in the
aeeumulator.
ASL
Shifts the eontents of the aeeumulator or memory
left by one position. Bit 7 moves into the earry flag
and Bit 0 is set to zero.
Bee
If the earry flag is clear program branches to
current address plus a signed displacement ( + 127
to -128).
BeS
If the earry flag is set program branehes to current
address plus a signed displaeement ( + 127 to -128).
BEQ
If the zero flag is set program branehes to eurren t
address plus a signed displaeement ( + 127 to -128).
465
BIT
Performs the logical AND operation between the
accumulator and memory. If the comparison
succeeds the zero flag is set and Bits 6 and 7 of the
memory location are copied into the V and N flags.
BMI
If the N flag is set (the result of the last operation
was negative) the program branches to the current
address plus a signed displacement ( + 127 to -128).
BNE
If the zero flag is clear the pro gram branches to
current address plus a signed displacement (+ 127
to -128).
BPL
If the N flag is clear the pro gram branches to
BRK
Saves the program counter and status register on
the stack and copies the contents of $FFFE and
$FFFF into PCLow and PCHigh.
BVC
If the overflow flag is clear the pro gram branches to
current address plus a signed displacement ( + 127
to -128).
466
BVS
If the overflow flag is set the program branches to
current address plus a signed displacement ( + 127
to -128).
CLC
Clears the carry flag.
CLD
Clears the decimal flag.
CLI
Clears the interrupt mask to enable interrupts.
CLV
Clears the overflow flag.
CMP
Compares the accumulator eontents with memory.
Sets the Z flag if they are equal or clears it if not.
The C flag is set if the eon ten ts of the memory
Ioeation are greater than those ofthe aeeumulator.
CPX
Compares the X register with memory data.
Cpy
Compares the Y register with memory data .
DEC
Decrements the specified memory Ioeation.
467
DEX
Deerements the X register.
DEY
Deerememts the Y register.
EOR
Performs the Exclusive OR operation between
memory and the aeeumulator, storing the result in
the aeeumulator.
INe
Inerements the specified memory Ioeation.
INX
Inerements the X register.
INY
Inerements the Y register.
JMP
Program exeeution eontinues at the specified
memory Ioeation.
JSR
Pro gram exeeution eontinues at the subroutine
eommencing at the sepcified address.
LDA
Loads the aeeumulator with data.
468
LDX
Loads the X register with data.
LDY
Loads the Y register with data.
LSR
Shifts the eontents of the aeeumulator or memory
Ioeation right one position. Bit 7 is set to zero and
Bi t 0 transferred to the earry flag.
NOP
Performs no operation for two clock cycles.
ORA
Performs the Iogical OR operation between the
accumulator and data.
PHA
Pushes the eontents of the accumulator on to the
stack.
PHP
Pushes the processor status register onto the stack.
PLA
Pulls the first item of data from the stack and loads
it into the accumulator.
469
PLP
Pulls the first item of data from the stack and loads
it into the processor status register.
ROL
Rotates the contents of the specified memory
location one position to the left. The carry flag is
transferred into Bit 0 and Bit 7 is moved into the
carryflag.
ROR
Rotates the contents of the specified memory
location one position to the right. The carry flag is
transferred into Bit 7 and Bit 0 is moved into the
carryflag.
RTI
Retrieves the status register and program counter
from the stack and returns from an interrupt
routine.
RTS
Restores and increments the program counter after
a subroutine call and returns from the subroutine.
SBC
Subtracts data from the accumulator with a borrow
and stores the result in the accumulator.
SEC
Sets the carry flag.
470
SED
Sets decimal mode.
SEI
Sets the interrupt disable mask.
STA
Stores the aeeumulator eontents at a specified
memory Ioeation.
STX
Stores the X register eontents at a specified
memory Ioeation.
STY
Stores the Y register eontents at a specified
memory Ioeation.
TAX
Transfers the aeeumulator eontents to the X
register.
TAY
Transfers the aeeumulator eontents to the Y
register.
TSX
Transfers the staek pointer eontents to the X
register.
471
TXA
Transfers the X register contents to the
accumulator.
TXS
Transfers the X register contents to the stack
pointer.
TYA
Transfers the Y register con ten ts to the
accumulator.
ADDRESS
REGISTER FUNCTION
54272
F7
F6
F5
F4
F3
F2
F1
FO
LOWFREQ
VOICE 1
54273
F15
F14
F13
F 12
F 11
F10
F9
F8
HIGH FREQ
54274
PW7
PW6
PW5
PW4
PW3
PW2
PW1
PWO
54275
PW11
PW10
PW9
PW8
54276
NOI
PUL
SAW
TRI
TEST
RING
SYNC
GATE
CONTROL REGISTER
'"
-C
ATTACK/DECAY
54277
54278
SUS 3
SUS2
SUS 1
SUSO
REL3
REL2
REL 1
RELO
SUSTAIN I RELEASE
VOICE 2
54279
F7
F6
F5
F4
F3
F2
F1
FO
LOWFREQ
54280
F 15
F14
F13
F 12
F 11
F10
F9
F8
HIGH FREQ
54281
PW7
PW6
PW5
PW4
PW3
PW2
PW1
PWO
54282
PW11
PW10
PW9
PW8
GATE
CONTROL REGISTER
54283
NOI
PUL
SAW
TRI
TEST
RING
SYNC
54284
ATK3
ATK2
ATK1
ATKO
DEC3
DEO
DEC 1 DECO
54285
SUS 3
SUS2
SUSl
SUSO
REL3
REL 2
REL 1
RELO
ATTACK/DECAY
SUSTAIN I RELEASE
G'\
-I
'"
m
~
'"
"'tJ
"'tJ
Z
0
X
....a.
ADDRESS
REGISTER FUNCTION
54286
F7
F6
F5
F4
F3
F2
F1
FO
LOWFREQ
VOICE 3
54287
F 15
F14
F13
F12
F 11
Fl0
F9
F8
HIGH FREQ
54288
PW7
PW6
PW5
PW4
PW3
PW2
PWl
PWO
54289
PWll
PW10
PW9
PW8
54290
NOI
PUL
SAW
TRI
TEST
RING
SYNC
GATE
CONTROL REGISTER
54291
54292
SUS 3
SUS 2
SUS 1
SUSO
REL3
REL2
DEC 1 DECO
REL 1
RELO
SUSTAIN 1RELEASE
ATTACK/DECAY
54293
FC 2
FC 1
FCO
LOWFILTER
54294
FC10
FC9
FC8
FC 7
FC6
FC 5
FC4
FC3
HIGH FILTER
54295
RES 3
RES 2
RES 1
RESO
FILTX
FILT3
54296
30FF
HP
BP
LP
FILTER
FILT 2 FILT 1
VOLO
E
Vi'
iir
~
RESONANCE 1 FILTER
MODE 1 VOLUME
MISCELLANEOUS
54297
PX 7
PX 6
PX 5
PX4
PX 3
PX 2
PX 1
PXO
54298
PY 7
PY 6
PY 5
PY 4
PY 3
PY 2
PY 1
PYO
POTX
POTY
54299
OSCIL 31 RANDOM
54300
VOICE 3 ENVELOPE
01:1.
APPENDIX 14
VIC-II REGISTERS
ADDRESS
FUNCTION
53248
53249
SPRITE 0
X POSITION
SPRITE 0
Y POSITION
53250
SPRITE 1
X POSITION
53251
SPRITE 1
Y POSITION
53252
SPRITE 2
X POSITION
53253
SPRITE 2
Y POSITION
53254
SPRITE 3
X POSITION
53255
SPRITE 3
Y POSITION
53256
SPRITE 4
X POSITION
53257
SPRITE 4
Y POSITION
53258
SPRITE 5 X POSITION
53259
SPRITE 5
Y POSITION
53260
SPRITE 6
X POSITION
53261
SPRITE 6
Y POSITION
53262
SPRITE 7
X POSITION
53263
SPRITE 7
Y POSITION
53264
53265
CONTROL REGISTER
53266
RASTER REGISTER
53267
53268
53269
SPRITE ENABLE
475
VIC-II Registers
53270
53271
CONTROL REGISTER
53272
53273
INTERRUPT REGISTER
53274
ENABLE INTERRUPT
53275
53276
53277
SPRITE Y EXPAND
SPRITE
x EXPAND
53278
53279
53280
BORDER COLOUR
53281
BACKGROUND COLOUR 0
53282
BACKGROUNDCOLOURl
53283
BACKGROUND COLOUR 2
53284
BACKGROUNDCOLOUR3
53285
SPRITE MULTICOLOUR 0
53286
53287
SPRITE 0 COLOUR
53288
SPRITE 1 COLOUR
53289
SPRITE 2 COLOUR
53290
SPRITE 3 COLOUR
53291
SPRITE 4 COLOUR
53292
SPRITE 5 COLOUR
53293
SPRITE 6 COLOUR
53294
SPRITE 7 COLOUR
APPENDIX 15
...c:.....................
25
rows
1024
1025
1064
1065
I
I
I
I
I
I
I
I
I
I
40 columns..
-----------------------------
1062
1063
1102
1103
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
2144
2145
2184
2185
-----------------------------
2182
2183
2022
2023
-----------------------------
55296
55297
55336
55337
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
56216
56217
---------------
56 54
56255
56256
56257
---------------
56294
56295
55334
55335
55374
55375
477
-00(.....................
Block
Block
Block
Block
40
41
Block
Block
920
921
Block
Block
960
961
40 columns
..................... ~
---------------
Block
---------------
Block
Block
78
79
---------------
Block
Block
958
959
---------------
Block
Block
998
999
38
Block
39
25
rows
Byte 2
Byte 3
Byte 4
ByteS
Byte 6
Byte 7
APPENDIX 16
CHARACTER CODES
SET 1
SET 2
POKE
SET1
SET2
POKE
21
42
22
43
23
44
24
45
25
46
26
27
28
10
SET 1
,.
SET2
:
47
48
49
29
50
30
51
31
52
11
32
SPACE
SPACE
53
12
33
54
"
55
13
34
"
14
56
15
36
57
16
37
58
17
38
&
&
59
18
39
60
<
<
19
40
61
20
41
62
>
>
479
POKE
SET 1
SET2
POKE
SET 1
SET2
POKE
SET 1
SET2
63
85
[Lj
107
CE
CE
64
El
El
86
108
C.
C.
65
[i]
[J
87
109
88
110
[B
Ei]
67
El
89
111
68
EJ
U
90
[I]
[]
[I]
[B
Ei]
112
91
~
~
D
D
0
92
EI3
iJ
~
~
EI3
iJ
113
93
[JJ
115
94
[JJ
[jj]
8J
D
8J
D
95
~
~
116
117
IJ
IJ
96
SPACE
SPACE
118
[]
[]
97
IJ
IJ
119
98
66
69
70
71
72
73
74
75
~
~
~
114
m m
76
120
77
lSl
99
121
78
[2'1
100
[Z]
0
0
101
D
0
79
D
0
122
123
102
124
~
~
81
11]
103
125
~
~
~
82
104
126
83
[!J
105
127
84
106
[]
[J
80
~
~
~
~
~
480
Character Codes
CHARACTER
CODE
CHARACTER
28
RED
56
29
CRSR RIGHT
57
30
GREEN
58
31
BLUE
59
32
SPACE
60
<
33
61
34
"
62
>
35
63
CODE
CHARACTER
WHITE
DisSHIFTC=
36
64
En SHIFT C =
37
65
10
38
&
66
11
39
67
12
40
68
13
RETURN
41
69
14
LOWER (ASE
42
70
43
71
72
15
16
17
44
(RSR DOWN
45
73
74
18
RVSON
46
19
HOME
47
75
20
DEL
48
76
21
49
77
22
50
78
23
51
79
24
52
80
25
53
81
26
54
82
27
55
83
481
CODE
CHARACTER
CODE
CHARACTER
CODE
CHARACTER
84
113
11
142
UPPER CASE
85
114
143
86
115
[!]
144
BLACK
87
116
145
CRSR UP
88
117
[Li
146
RVS OFF
89
118
147
CLR
90
119
148
INST
91
120
149
92
121
93
122
[I]
[]
[l]
94
123
EB
152
95
(-
124
153
96
El
125
154
97
[iJ
rn
126
1T
155
98
rn
127
156
PURPLE
128
157
CRSR LEFT
129
158
YELLOW
130
159
CYAN
131
160
SPACE
132
161
IJ
99
100
101
102
103
El
U
Ll
D
0
150
151
104
133
Fl
162
105
~
~
~
134
F3
163
135
F5
164
136
F7
165
D
0
0
LSl
137
F2
166
138
F4
167
110
[Zl
139
F6
168
111
140
F8
169
112
141
SHIFT RETURN
170
[]
106
107
108
109
482
Character Codes
CODE
CHARACTER
CODE
CHARACTER
CODE
CHARACTER
171
[]3
178
Er3
185
172
C.
179
186
173
[g
180
187
174
181
[]
188
~
~
175
182
189
176
[B
~
183
CI
D
U
177
184
190
191
~
~
~
APPENDIX 17
MEMORYMAP
Addresses
o
1024
2048
40960
49152
53248
55296
56320
57344
65535
Contents
Storage for operating system
Standard screen memory
Random Access Memory BASIC pr0J;ams and
varia les,
Bit Mapped Screen,
User Defined Characters,
etc.
BASIC interpreter in ROM
Unused RAM
VIC and SID
chip registers
Screen colour
memory
Character
Definitions in
ROM
Input/Output
chip registers
Operating system in ROM
APPENDIX 18
485
486
BIT-MAP ROUTINE
10
20
30
40
50
60
10000
10010
10020
10030
10040
10050
487
PLOT ROUTINE
10
20
30
40
50
60
11000
488
489
FILL ROUTINE
10
20
30
40
50
60
13000
490
DRAW ROUTINE
10
20
30
40
50
60
12000
12010
12020
12030
12040
12050
12060
12070
12080
491
492
493
494
495
DATAMAKER
This program will convert the contents of any area
ofmemory into aseries ofDATA statements which
are appended to the DATAMAKER program. To
use the program enter the line number at which
you want the DATA to start, and the start and end
address of the area of memory you want to convert
into DATA statements. When the program has
finished, delete lines 5 to 500 and save the DAT A
statements for use in your program.
5
10
REM DATAMAKER
INPUT "FIRST DATA LINE
NUMBER"; LN
496
20
CODE"iS
30
100
110
120
130
140
150
160
170
200
210
300
310
320
330
340
350
500
INDEX
1525 Printer
1540 Disk Drive
1541 Disk Drive
16 Sprites
6510 microprocessor
403
400
348
307
235
354
1
420
212,227
221
230
127,230
146,262
377
Block-Execute
Block-Free
Block-Read
Block-Write
Boolean algebra
Branching
- in machine code
Buffer Pointer
Byte
398
378
374
375
82
37
245
380
89
Cassette unit
Channels
Character sets
eHR$
CHRGET Routine
Clock
elOSE
elR
CLRlHOME key
Collision detection
eMD
Colour
Colour memory
Comparisons in
machine code
eONT
COpy
Copying Tapes
COS
CTRL key
Cursor
Cursor keys
Cursor Down Mode
Cursor Up Mode
4
188
128
62
219
38
188
92
10
167
190
12
120
DATA
Decay
DEFFN
DEL key
248
49
359
402
75
12
8
9
407
407
54
95
79
10
498
OlM
29
Directo ry
353,360
Disk Drives
400
Disk Status
358,36 2
Display Memo ry
263,26 5
DOS
349
DOS Suppo rt Program
359
DotAd dress
412
Dot Matrix
403
Double Width Mode
411
DRAW routine
281
Drawin g Lines
280
Editing
END
Envelope
Error messages
Error status
Executing progra ms
EXP
10
48
95
221
361
219
Files
Fill Program
Filter
Floatin g Point
Variables
Floppy Disks
FN
FOR
Forma tting disks
FRE
Functions
187
292
103
227
349
79
34
352
91
73
GET
GET#
GOSUB
GOTO
Graphics
- Symbols
- Bit mappe d
Graphics Mode Printin g
52
188
46
37
119
15
146
408
77
297
234
12
234
383
357
299
37
252
211,260
8
420
1
60
59
424
215
19
Index
LOAD
184
184
355,362
LOG
76
Logical Operations
256
Loops
34
Lowercase
14
Loading programs
Loading Programs
NMI
21
352
34
255
NOT
84
NEW
New disks
NEXT
Notes
333,427
Numbering systems
230
ON ... GOSUB
ON ... GOTO
OPEN
Operating System
OR
49
49
187
204
84
90
Plotting Points
273
Pictures program
340
Pixel
127, 158,272
PEEK
POKE
POS
PRINT
PRINT#
Processor status
register
499
13,90
57
8,55
188
Program Counter
236
18
236
363
RAM
Random numbers
Random Files
Raster Register
Raster Scan
90
Prog~m
77
373
302
301
READ
53
Real variables
25
Register
91
Relative addressing
251
Relative Files
367
Release
95
REM
32
Rename
359
Renumber Program
217
Reserved words 24,214,420
RESTORE
55
RESTORE key
11,14
RETURN
47
Reverse video
15
Reverse Mode Pri nti ng 412
RIGHT$
60
RND
77
ROM
90
RUN
19
RUN/STOP key
11
Running Machine Code 258
SAVE
185
Saving
Programs
185,355,362
Scratch
359
Screen Dump Program 413
500
Screen memo ry
120,133,239, 263, 265
Sector
349
Seque ntial Files
364
SGN
75
Shifts
257
SID Chip
331
SID registers
96,235
Side Sectors
368
Simple variables
225
SIN
75
Sketchpad Program
317
SPC
56
Sprites
158
SQR
73
Stack
253
Stack Pointe r
237,25 3
STATUS
190
Status Register
235
STEP
35
STOP
49
Stoppi ng progra ms
Storing Machin e Code 259
String Variables
227
String variables
26
STR$
63
Structu red progra mming 207
Su brouti nes
46
- in machin e code
252
Sustain
95
Syntax errors
221
SYS
204
TAB
Tab Print Head
TAN
Television
THEN
TIME
TIME$
TO
Token
57
410
75
4
43
39
39
34
214
Track
Tunes
349
331
Upper ca se
User
User define d functio ns
User define d characters
User Friend ly
USR
14
399
79
129
315
205
VAL
63
Valida te
358
Variables
22
Variab le display prog
228
Variab le dump
progra m
230
Variab le Storag e
225
VERIFY
185
VIC-II Chip
130,26 3,301
WAIT
206
X registe r
235
Y registe r
235
240
SUPER BASIC
SUPER BASIC is an extension to the standard
BASIC 2.0 of the Commodore 64, providing 36
extra BASIC commands which give you full control
over the Graphics, Sprites and Sound capabilities of
your 64, and add many of the features found in
more modern versions of BASIC.
VOLUME
SPRCOL
SPRITE
SPRMOV
SPROFF
SPRSET
SPRSIZ
Century Software
: To:
:
I
:I
MailOrder Dept.,
Tiptree Book Services Ltd,
Ctiurch Road, Tiptree
Colchester, Essex C05 OSR
: Please send me
copies of SUPER BASIC by
: Peter Lupton anCf'Frazer Robinson, at f9.95 each.
ISBN 0 7126 06661
I enclose my cheque/postal order for f __
(cheques should be made payable to Tiptree
Book Services Ltd).
Please debit my Access/Barclaycard account
(delete as appropriate).
~~~b~;
IIIIIIIIIIIIIIIII
Name
Address----___________________________
Signed
(Horne address please if paying by credit card)
Please allow 28 days for delivery.