Computes Machine Language Rutines For The Commodore 64
Computes Machine Language Rutines For The Commodore 64
Machine
language
Routines for the
Commodore
64
Dozens of easy-to-use routines and programs
which make your Commodore 64 even more
powerful and versatile. Includes programming
aids, game enhancements, and high-speed
graphics utilities.
Machine
Language
Routines for the
Commodore
64
The following article was originally published in COMPUTE! magazine, copyright 1984,
COMPUTE! Publications, Inc.: “Dr. Video 6 4 ” (February).
The following articles were originally published in [Link] Gazette, copyright 1983,
COMPUTE! Publications, Inc.: “The Four-Speed Brake” (August); “RAMtest” (August —
originally titled “Machine Language for Beginners: The Easy Way”); “Disassembling”
(September — originally titled “Machine Language for Beginners: Disassembling”); “64
Searcher” (September); “64 Paddle Reader” (October — originally titled “Improved Paddle
Reader Routine”); “Windows and Pages” (October — originally titled “Machine Language for
Beginners: Windows and Pages”); “Disk Defaulter” (November — originally titled “VIC/64
Disk Defaulter”); “One-Touch Commands” (November — originally titled “One-Touch
Commands for the 6 4 ”); “The Assembler” (November — originally titled “Machine Language
for Beginners: The Assembler”); “Foolproof INPUT ” (December — originally titled “Foolproof
INPUT for VIC and 6 4 ”).
The following articles were originally published in COMPUTE!’s Gazette, copyright 1984,
COMPUTE! Publications, Inc.: “Auto Line Numbering” (February); “ASCII/POKE Printer”
(March — originally titled “ASCII/POKE Printer for VIC and 6 4 ”); “Numeric Keypad” (April);
“Step Lister” (May); “Scroll 6 4 ” (June); “Ultrafont + ” (July); “Sprite Magic” (August); “String
Search” (August).
The following article was originally published in COMPUTE! s First Book o f C om m odore 6 4
G am es, copyright 1983. COMPUTE! Publications, Inc.: “Maze Generator.”
The following program was originally published in Creating Arcade Games on the Com m odore
6 4 , copyright 1984, COMPUTE! Publications, Inc.: “Two-SpriteJoystick.”
Reproduction or translation of any part of this work beyond that permitted by Sections 107
and 108 of the United States Copyright Act without the permission of the copyright owner
is unlawful.
ISBN 0 -9 4 2 3 8 6 -4 8 -5
COMPUTE! Publications, Inc., Post Office Box 5406, Greensboro, NC 27403 (919) 2 75-9809,
is one of the ABC Publishing Companies and is not associated with any manufacturer of
personal computers. Commodore 64 is a trademark of Commodore Electronics Limited.
it
Contents
iii
Variable Lister
E.A. Cottrell .............................................................................. 102
Disk Defaulter
Eric B r a n d o n .............................................................................. 106
IV
A p p e n d ic e s ..........................................................................................213
A: A Beginner's Guide to Typing In Programs .................215
B: How to Type In P rogram s.................................................... 217
C: The Automatic Proofreader
Charles B r a n n o n ..................................................................... 219
D: Using the Machine Language Editor: MLX
Charles B r a n n o n ..................................................................... 223
E: The 6502 Instruction S e t ...................................................... 230
F: Number T a b le s......................................................................... 246
Index ......................................................................................................253
Foreword
Machine language. If you’re a BASIC programmer, these words may
seem mysterious. Perhaps intimidating. After all, the best programs
— from word processors to arcade games — are written in it. But why
is machine language so special?
\bur Commodore 64 doesn’t really speak in BASIC. You may be
well versed in that programming language, but your computer isn’t.
It has to look up everything you type in and translate it into the
language it does understand — machine language (ML).
ML is simply a series of 0 ’s and l ’s. Bits off or on. When you type
something into the computer — LET A = 2 + 2, for example — the
computer has to use its internal dictionary to look up what that means
in numbers. Then it has to return the answer to you in a way you
can understand.
This constant referring and translating, called BASIC, is just not
as fast as machine language. If you could somehow speak to your 64
in its native tongue, ML, you would be able to execute commands and
programs a hundred, even a thousand, times faster.
Fortunately, you don’t have to know how to write machine
language programs to use them. BASIC and ML can work together. The
routines and programs in this book can be added to your own
BASIC programs. All you have to do is type them in.
There are several routines which make it easier to write your own
BASIC programs, from automatically adding line numbers to slow
ing down listings on the screen. “BASIC Aid,” an all machine language
program, gives you 20 tools that simplify BASIC programming.
Graphics utilities let you create custom characters, sprites, or impres
sive designs, all at machine language speed. If you enjoy writing
games, you’ll find the ML joystick, paddle, and keyboard routines
helpful. You can insert other routines to make your screen scroll
horizontally or vertically, or use the maze generator to create random
mazes almost instantly. Other applications let you freeze the display,
sort thousands o f things quickly, merge files, search for strings, and
even test your computer’s RAM chip.
\bu don’t need to know how or why machine language works
to use these utilities. As long as you know BASIC, you’ll be able to
speed up, simplify, and amplify your own programs with the routines
in this book. What once took minutes can take only seconds with
ML. You’ll be amazed at how powerful your programs can become.
vn
Chapter 1
Introduction
Gregg Pede
Another Toolbox
Machine language is fast Computers are fast. I’m sure
a n d powerful. This simple you’ve heard that before.
introduction helps you under The processor inside your
stand what machine language Commodore 64, called the
can a n d should do. There’s 6510 chip, takes only one
millionth of a second (one
even a short routine which
microsecond) to complete
compares the speeds o f BASIC
one work cycle. The com
a n d machine language. puter can process over one
hundred thousand instructions in one second. Speedy.
Unfortunately, the 64 is only that quick when it’s executing
instructions written in its native tongue, machine language (ML).
Machine language is essentially a series of numbers stored in the
computer’s memory. Remembering those numbers, and what they
do, is sometimes hard for humans. That’s why most o f us use a dif
ferent language to speak to the computer. Called BASIC, it’s much
slower because it has to translate what you enter into code that
the computer can understand. There’s nothing inherently wrong
with this; if you didn’t have BASIC to use, you would have to talk
to the 64 in machine language. BASIC may be easier for the novice
programmer to remember, but it is slower.
Quick Clear
To see the difference in speed between BASIC and ML, let’s look at
two programs, both of which do the same thing. The high-resolution
screen on the Commodore 64 contains 8000 bytes of information.
Clearing that screen with a BASIC program takes at least 30 seconds.
Here’s what the BASIC version would look like:
5 PRINT"{CLR}"
10 POKE 53272,PEEK(53272)0R8
20 POKE 53265,PEEK(53265)OR32
30 FOR T=8192 TO 8192+8000sPOKE T,0:NEXT
40 GET A $ :IF A$="" THEN 40: REM HIT SPACE TO CONTI
NUE
50 POKE 53265,PEEK(53265)AND223
60 POKE 53272,21
Type it in and RUN it. It does the job, but it’s slow. Using machine
language, however, clears the screen almost instantly. Type in the
following program, RUN it, then enter SYS 49152 to see the quickness
of ML.
3
1: Introduction
10 1=49152:IFPEEK(49152)=169THENSYS49152:END
20 READ A:IF A=256 THEN SYS49152:END
30 POKE I,A:I=I+1:GOTO 20
49152 DATA 169,147,32,210,255,173,24
49159 DATA 208,9,8,141,24,208,173
49166 DATA 17,208,9,32,141,17,208
49173 DATA 169,0,168,133,252,169,32
49180 DATA 133,253,169,0,145,252,200
49187 DATA 208,249,230,253,165,253,201
49194 DATA 64,144,241,165,197,201,60
49201 DATA 208,250,173,17,208,41,223
49208 DATA 141,17,208,169,21,141,24
49215 DATA 208,96,256
5
Richard Mansfield
The Assembler
One o f the basic tools o f machine People often use the words
language (ML)programming is m a c h in e lan g u a g e and
an assembler. Richard Mansfield, a ssem bly la n g u a g e inter
senior editor o f COMPUTE! changeably. However,
Publications, offers his assembler m a c h in e la n g u a g e is
along with some elementary becoming the more common
term. It’s more accurate —
explanations an d examples on
when you program in this
how it’s used. language, you’re speaking
directly to your computer in its native tongue.
Unfortunately, the computer’s internal language is almost
impossible for humans to work with. These machines communicate
only with numbers, and very odd numbers at that. They’re binary,
consisting of only l ’s and 0 ’s, grouped together in eight-digit clusters
called bytes-. 01100111,11110001, and so on. Humans find it easier to
work with words. That’s where an a ssem b ler comes in.
The Primary Tool
We first need to build the basic tool for machine language (ML)
programming. Type in the program and you’ll have your own
working assembler.
The assembler works like this: You type in a wordlike, three-letter
code, and the assembler looks up the correct number (in the com
puter’s language) and POKEs it into RAM memory to start forming an
ML program. In a minute we’ll create a simple ML program to show
you how ML programming is done. But let’s clear up a few possible
sources of confusion first.
These wordlike codes are called m n em on ics, which means
they’ve been designed to be simple to remember. It’s easy enough to
remember what USA stands for. Likewise, you can quickly pick up
the essential ML words. There are 56 of these commands available to
you, roughly as many words as there are in BASIC. But, like BASIC,
there is a core group o f about 20 important ones. They are the only
ones you need to use to get almost anything accomplished. What’s
more, the ML words a r e easy to learn and remember. For example,
BRK stands for BReaK (like BASIC’s STOP), JSR is Jump to Sub-
Routine (GOSUB), and RTS is ReTurn from Subroutine (RETURN). The
command which does the same thing as BASIC’s GOTO is called
JMP, for JuMP.
6
Introduction: 1
A Kind of Swing
ML programming involves a kind of swing between Command and
Target. First you give a command, then you give the specific target for
that command. Then another command, another target. These
paired-event phenomena are called by many names and appear in
many disguises in programming as well as in real life. They’re called
Operator/Operand, Instruction/Argument, Mnemonic/Address,
Analyst/Analysand, Shopper/Apples, Thief/Victim.
Notice that the first half of the pair is the more general, the
second more specific. At a given moment, the apple is the specific
thing the shopper’s involved with, but the shopper will be buying
other things during this visit to the store. Similarly, a thief is always
a thief, but a victim is a victim only that once (we hope). Also, the
transaction which all these pairs have in common is that the first half
of the pair is d o in g som eth in g to the second half. Together they
form a complete action in the sense that Open/Envelope or Eat/Peach
are paired (command/target) actions.
A Robot Dinner
If you think about it, you can see this do-it-to-it rhythm throughout
BASIC programming: PEEK (8), PRINT “HELLO ”, SAVE “PROGRAM ”,
X = 15, X = X + 1, GOTO 1500, and so on. The reason we’re stressing
this distinction, this rhythmic swing between actor and acted-upon,
is because an ML program is constructed in precisely this way — you
make a list of tiny, elementary actions for the computer to later carry
out. It’s like a robot dinner: spear/meat, raise/arm, insert/food, chew/
morsel, lower/arm, spear___List enough of these mini-instructions
and you can do amazing things.
One result of all this is that an ML program doesn’t look like a
BASIC program. BASIC tends to spread these pairs out along a line:
100 Y=3:X=X+1:POKE 63222,Y:Y=PEEK(1200)
7
1: Introduction
8
Introduction: 1
you with the next available “line number” address in RAM, 832. The
mnemonic/address pair LDY #0 uses up two bytes.
You are ready to type in your next pair: LDA #66. Hit RETURN
on this line and you’ve put the code for the letter B into the A register.
Then type in the rest of our ML program, one pair per line:
JSR 65490
DEY
BNE 834
RTS
That’s it. To let the assembler know that you’re through with your
program, type END instead o f a normal mnemonic and it will tell
you the start and end addresses of your ML program. Then, having
done its job, the assembler quits. The mnemonics and addresses
were all POKEd into their proper places after being translated into the
machine’s language. To see what happens when this RUNs, you can
type SYS 830 and see the effect of the small ML loop we wrote. You’ll
get 256 B ’s onscreen in record time. Not something you’ve been
anxious to do? More useful things are on their way.
In the next few chapters we’ll look at some other aspects of
machine language, including a disassembler that you can use to pull
apart ML programs. You’ll see how a simple ML program is created
and then find valuable utilities that will allow you to use the speed
and power of machine language in your own programs.
You’re on your way to using machine language.
The Assembler
Fo r m istake-proof p ro gra m entry, be sure to read "The Autom atic Proofreader," A pp en d ix C.
9
1 : Introduction
10
Introduction: 1
11
1 : Introduction
12
Introduction: 1
20010 DATABNE8208,BPL8016,BRK0000,BVC8080,BVS8112,
CLC0024,CLD0216 »CLI0088 srem 114
20 020 DATACLV0184,CMP1193,CPX4224 »CPY4192,DEC2198,
DEX0202,DEY0136,EOR1065 srem 184
20030 DATAINC2230,INX0232,INY0200,JMP6076,JSR9032,
LDA1161,LDX5162,LDY5160 srem 200
20040 DATALSR3066,NOP0234,ORA1001,PHA0072,PHP0008,
PLA0104,PLP0040,ROL3034 :rem 185
20050 DATAROR3098,RTI0064,RTS0096,SBC1225,SEC0056,
SED0248,SEI0120,STA1129 :rem 216
20060 DATASTX2134,STY2132,TAX0170,TAY0168,TSX0186,
TXA0138,TXS0154,TYA0152 :rem 79
13
Richard Mansfield
Disassembling
Knowing how to assemble A disassem bler is the second
machine language (ML) p ro of two major tools you’ll
gram s isn’t enough; you also be using when you work
need to know how to convert the with machine language. In
numbers the computer uses into order to understand what
it does, we’ll need to briefly
things you can understand. A
review the other major
disassembler does this.
tool, an a ssem b ler, which
The simple-to-use disas was described in the pre
sem bler included here makes vious section.
ML program s less confusing An assembler is used to
a n d easier to study. write an ML (machine lan-
guage) program in the same way that BASIC is used to write a BASIC
program. An assembler lets you type in ML instructions like LDA #8
and then translates the instructions into numbers and POKEs them into
memory for you. Take a look at Program 3- The first line, numbered
884, says LDA (LoaD the Accumulator) with the number eight. This
same instruction appears in different form in line 882 o f Program 2:
DATA 169,8. An assembler would translate your LDA instruction into
the number 169. If you’re just starting out with ML, these instructions
won’t mean much to you yet, but for now all we want to do is get a
feel for the broad concepts of ML.
To look at “assembling” another way, it helps to realize that
there’s a similar process going on when you write a BASIC program.
After you type in a BASIC command, the BASIC interpreter translates
it into a token , a single-byte representation of the command, and
stores the token in memory. So, a line of BASIC is stored inside the
computer in a different form than you would see on the screen
when you type it in. The w o rd LIST would be stored in four bytes,
but the c o m m a n d LIST would be crunched down by BASIC into
only one byte. Similarly, an assembler takes your LDA and turns it
into the number 169, which can be stored in a single byte. These
words — LDA and LIST — are for our convenience. They are easier
for us to work with. The computer only needs numbers and so
BASIC and its ML equivalent, an assembler, accepts the words, but
stores numbers.
An Understandable Version
O f course, you need to go the opposite way sometimes, from
the numbers back to the words. If the computer stores, interprets,
and executes programs as pure numbers, how can we examine or
14
Introduction: 1
15
1: Introduction
Trying It Out
We can conclude with a few comments about the disassembler.
There are several graphics features of this disassembler which can
make it easier to visualize the programs it disassembles. All branch
ing instructions (like BEQ), JSRs, andJMPs (the equivalents of GOSUB
and GOTO) are offset on the screen to indicate that the flow of the
program might be taking a new course at that point. Likewise, the
RTS instruction (ReTurn from Subroutine, the equivalent of BASIC’s
RETURN) causes a line to be drawn, marking the end of a subroutine.
Line 210 of Program 1 PEEKS the ML command from memory.
If it cannot make a match against the array containing all legal ML
instructions (lines 8 2 0 -9 6 0 ), a ? is printed on screen in line 230.
When you see a series of question marks during a disassembly, it
means that you are not disassembling an ML program, but rather have
come across a “data table.” This would be a list of numbers or words
which might be u sed by an ML program, but is not actually ML code.
You can use the disassembler to look into the heart o f your
BASIC language. Just give an address between 40960-49151 as the
START ADDRESS, and you can see the insides of one of the most
complex ML programs ever written: your BASIC. The next article
will show you how to go directly into BASIC and access some of
its ML subroutines.
Program 1. A Disassembler
For mistake-proof p ro gra m entry, be sure to read ‘‘The Automatic Proofreader, ’' A ppendix C.
16
Introduction: 1
17
1: Introduction
18
Introduction: 1
Program 2. Fragment
Fo r mistake-proof p ro gra m entry, be sure to read "The Autom atic Proofreader,”A pp en d ix C.
19
1 : Introduction
20
Richard Mansfield
Windows and
Comparing short BASIC One of the most effective
program s a n d their machine ways to learn machine lan
language equivalents is one o f guage (ML) is to examine
the best ways to see how machine short routines in BASIC
language works. Using a “win and then see how the same
thing is accomplished in
dow ” into your Commodore 6 4 ’s
ML. After all, there are a
memory, this article takes you
fairly limited number of
step by step through the process basic programming tech
ofLOADing a n d manipulating niques in any language:
an ML routine. looping, branching, compar
ing, counting, and a few others. And long programs are not created
in a furious burst of nonstop programming. Rather, they are built by
knitting together many small subprograms, short routines which are
as understandable in ML as they are in BASIC. Looking at side-by-side
BASIC-ML examples is the best way to learn ML. Before you know it,
you’ll be able to think in both languages, and you’ll have a working
knowledge of machine language.
Peering into Memory
To start things off, type in Program 1. This is called a BASIC lo a d e r ,
and its function is to POKE a machine language program into RAM
memory. The numbers in the DATA statements are the ML program.
SAVE the program in case things go awry, then type RUN. Nothing
seems to happen. You can then type NEW because the little loader
has done its job: A short ML program is now in memory from address
864 to 875.
Program 1. 64 Loader
800 FOR ADRES=864 TO 875:READ DATTA:POKE ADRES,DAT
TAsNEXT ADRES
864 DATA 162,0,189,0,0,157
870 DATA 0,4,232,208,247,96
Because of the color memory problem (you have to POKE values into
the entire color memory before you can see things), enter this short
BASIC line in direct mode (without line numbers) before trying out
the ML program and after each time you clear the screen:
FOR I = 55296 TO 55552: POKE 1,1: NEXT
21
1: Introduction
To see what this ML program does, you can just SYS to the start
of it by typing SYS 864. If you typed in the DATA statements correctly,
you’ll see a collection of strange symbols on the screen. Now clear
the screen and type in Program 2. When RUN, it allows you to see
things happening. Some characters are flashing rapidly, some change
only in response to things you typed on the keyboard, some do
nothing. What you’re looking at is the first 256 memory addresses
in your computer. The flashing characters (160,161, and 162, count
ing down from the top-left corner of the screen) are your computer’s
clock. They’re what you get when you ask for TIS.
Program 2. The Window
10 SYS 864
20 GETK$
30 GOTO 10
22
Introduction: 1
Program 4. Disassembler
STARTING ADDRESS (DECIMAL)? 864
START ADDRESS HEX 0360
864 LDX # 0
866 LDA 0 ,X
869 STA 1024 ,X
872 INX
873 BNE 866
875 RTS
After the “line number” address, you see some groupings of two-
digit hex numbers. The first group is A2 00. These are the actual
numbers that the computer reads when it executes the ML program.
These numbers are the most elemental form of machine language
and sit in memory at the addresses indicated to their left. Finally, the
LDX #$ 0 0 is the disassembly (the tran slation ) of the ML A2 00.
LDX means “LoaD the X register.” The X register is like a variable, and
the # symbol tells the computer to load the n u m b er zero into the X
— as opposed to the number found at ad d ress zero. Without the #,
X would be given whatever number was currently in address zero.
The $ means that the number is in hex notation, not decimal.
Now take a look at Program 4. If you have saved a copy of the
disassembler from the previous section, LOAD it. When it asks you
for “starting address (decimal),” type in 864 and you’ll see the same
disassembly as illustrated in Program 4. Notice that 864 is translated
into hex (0360). Program 4 is nearly identical to Program 3 except that
the numbers between the address and the disassembly are not shown.
The second “line” in Program 4 “LoaDs the Accumulator” with
the item in address zero + X. That is, if X is 5, the item in 0 + 5 (address
5) is loaded into the accumulator. The accumulator is another “vari
able” in ML, used to hold things temporarily. Since we’re trying to
send all the items in zero page (addresses 0 -2 5 5 ) up to the screen
23
1: Introduction
memory so we can see them, our next job is to “STore the Accumu
lator” at address 1024 + X. (1024 is the starting address of screen
memory on the 64.) As you can see, we’re making X do double duty
here as an “index.” It’s acting as an offset for both the source items
(in zero page) as well as the target to which we’re sending those items
(screen memory).
The next line raises, or in crem en ts, X by 1. INcrement X causes
X = X + 1 to take place, so this first time through the loop, X goes
up from 0 to 1. The BNE means “Branch if Not Equal to zero.” Branch
is like GOTO, but instead of giving a line number as its target, it gives
an address in memory (866 in this case, the start of our loop). How
does X ever get to zero if it’s being INXed each time through the
loop? No single-byte variable in ML can go higher than 255. (Likewise,
no individual memory address in the computer can “hold” a num
ber beyond 255. This is similar to the fact that no decimal digit can
ever go higher than 9. After that, the digits “reset” to zero.) As soon
as you’ve raised X to 255, the next time you INX, it resets itself to zero
and starts over. So line 873 in Program 4 will throw us back to line
866 until we’ve been through the loop 256 times. Then we’ll finally
get to line 875, where RdTurn from Subroutine sends us out of ML
and back into BASIC where we left off. Notice that SYS-RTS has the
same effect as GOSUB-RETURN, except that the former moves
between BASIC and ML.
Making a Loader Automatically
Program 5 is another useful tool when you’re working with ML.
The loader in Program 1 POKEs an ML program into RAM memory
for you; Program 5, “Datamaker,” goes in the other direction and
translates an ML program from RAM into a BASIC loader.
After you type it in and SAVE it, try an experiment. To make
a loader out of “The Window,” change line 1 in Program 5 to read
S = 864:F = 875:L = 10. S is the starting address of your ML and F
is the finish. Then type RUN. You’ll see the loader created for
you onscreen.
The Datamaker destroys itself after it’s finished. Notice that the
line numbers created in the loaders made by Datamaker are also the
addresses where the ML will be POKEd. And don’t forget to change
the starting and ending addresses in line 800 before SAVEing a
finished loader.
24
Introduction: 1
Program 5. Datamaker
F o r m istake-proof p ro gra m entry, be sure to read "The Automatic Proofreader," A pp en d ix C.
1 S=826sF=1023sL=10sREM S&F=ADRES :rem 145
2 PRINT"{CLR}{2 D O W N } F O R I = S T O S + 4 8 S T E P 6 :IFI>FTHEN
NEXT:PRINT"GOT07":G0T06 srem 217
3 PRINTI;"DATA";:FORJ=0TO5sA$=STR$(PEEK(I+J))
:rem 11
4 PRINTRIGHT$(A$,LEN(A$)-1)",";sNEXTJ:PRINTCHR$(20
):NEXTI :rem 227
5 PRINT"S=“S+48":F="F":L="L":GOT02 :rem 225
6 POKE198,L sF 0 R K=1T0L:POKE630+K,13s NEXT sPRINT"
{HOME}":END srem 248
7 PRINT"{CLR}{2 DOWN}" $F0RI=1T07sPRINTIsNEXTsL=7sG
0T06 srem 155
800 FOR ADRES=826 TO 1023sREAD DATTAsPOKE A D R E S ,DA
TTAsNEXT ADRES srem 82
25
J.R. Chaffer
Stealing Characters
from ROM
Plotting bitmapped data Jim Butterfield dropped an
or graphics with machine intriguing hint in the May
language? Want to use standard 1983 issue of COMPUTE!
ROM characters on the same magazine when he used
screen, w ith out tediously bit the phrase which became
the title of this article.
m apping each one? H ere’s a
Normally, ROM characters
shortcut to do just that. More
cannot be displayed on
importantly, this article shows the same screen as high-
you how a simple machine resolution graphics unless
language routine is created each byte is carefully mapped
a n d how it works. out exactly as it exists
in ROM. However, the eight bytes which create each character
are contained permanently in ROM at locations 5 3 2 4 8 -5 7 3 4 3
($DOOO-$DFFF). They can easily be read and transferred to your
bitmap area with a subroutine. This c o u ld be done in BASIC; but if
you’re doing bitmapping, you’ve probably already discovered that
BASIC is too slow for this application. As long as you know the
address of each of the desired characters in ROM you’ll find this
method of “stealing” characters from ROM just what you need. Many
of these addresses are listed in Table 1.
Plotstring and Dummy Plot
This technique works best if you already have a machine language
subroutine which plots a byte of data by storing it at some specific
address in your 8K block of high-resolution memory. The dummy
routine Plot, called by the example program (“Plotstring”), theoret
ically does just this; it stores the byte contained in the accumulator
at the RAM address specified by PTR1A and PTR1B (low byte, high
byte format). When you are in bitmap mode, that byte then appears
on the screen at the location corresponding to its address in RAM.
Plotstring initially reads the first data byte in a specified two-byte
character address in ROM, and stores it at your prearranged RAM
location. It then does the same for each of the seven remaining bytes
of the character. You will, presumably, already have figured out the
correct RAM address for the desired screen location. The only other
thing you need is the ROM address from which to start reading the
eight-byte character. (Note that this technique will work for all the
Commodore graphics characters as well.)
26
Introduction: 1
27
1: Introduction
Routine Mechanics
I continually use this subroutine to get standard numbers and char
acters on my bitmap. Of course, the characters are still bitmapped,
but the data is taken directly from the ROM.
If you’re new to machine language listings, the labeled variables,
such as CHRS, BYTECNT, and so on, are simply single-byte storage
locations defined by the user. They can be anywhere in free RAM, with
the exception of PTR1A, PTR1B, PTR2A, and PTR2B, which must be
in page 0. You can use locations 25 1 -2 5 4 (hex $FB-$FE) and loca
tion 2 which are not used by BASIC. You can also use locations
139-143 (S8B-S8F) if you’re not using BASIC’s RND function.
This routine can be used repeatedly if the ROMADS 1 and
ROMADS2 addresses are set by each calling sequence. Note that
ROMADS 1 is the first byte of the two-byte address immediately fol
28
Introduction: 1
lowing the LDA in the line, LDA ROMADSl,Y; the LDA here is byte
185 (SB9). Likewise, ROMADS2 is the byte immediately following
the LD X in the next line, and the LDX is coded as byte 190 ($BE). So
what we are doing here is modifying the subroutine itself as part of
the calling sequence. Actually, four bytes must be set by the calling
routine: (1) The ROMADS1 byte; (2) the byte just following ROMADS1;
(3) ROMADS2; and (4) the byte immediately after ROMADS2.
Let’s say you want to plot the string, N ote 1: near the upper-left
corner of your bitmapped screen. And let’s say your bitmap storage
area is from 57344 to 65343 ($E000-$FF3F), which is under the
Kernal ROM. You’re starting the string in text column one, on line
one. (Remember that column and line numbers run from 0 to 39 and
from 0 to 24, respectively.) If you work out the address for this
screen location, you will find that the starting byte is 57672 (SE148).
Let’s also say your character ROM address table for the string Abie 1:
has been stored in RAM as in Table 2, starting at location 49152
(SCOOO). A functional calling sequence would be as follows:
Source Code 2. Calling Sequence
PLOTNOTE1 LDA #$48 ; lo-byte, screen location.
LDX #$E1 ; hi-byte, screen location.
STA PTR1A
STX PTR1B
LDA #$C0 ; hi-byte, Table 2 start.
LDX #$00 ; lo-byte, Table 2 start.
STA ROMADS1 + 1 ; address = ROMADS1 + 1
STA ROMADS2 + 1 ; address = ROMADS2 + 1
STX ROMADS1 ; lo-byte of address.
INX
BNE CONT
INC ROMADS2 + 1
CONT STX ROMADS2 ; ROMADS pointers set.
LDY #$07
STY CHRS ; seven characters in string.
JSR PLOTSTRING ; go plot “Note 1:”
Each string you plot must have its ROM character address table
stored somewhere in RAM, similar to Table 2. Note that the address
bytes are stored in standard lo-byte, hi-byte format. Thus, they appear
to be backwards when compared to the addresses given in Table 1.
This can be switched, if you modify Plotstring accordingly.
29
1: Introduction
30
Source Code 3. Demonote
0065 ZERO PAGE LOCATIONS:
0070
0075 TEXTMODE .DE $02 : UPPER/LOWERCASE.
0080
0085 PTR1A .DE $FB : POINTERS FOR BITMAP
0090 PTR1B .DE $FC : STORAGE.
0095
0 1 0 0 PTR2A .DE $FD : POINTERS FOR
0105 PTR2B .DE $FE : ROM ADDRESSES.
0110
0115 CHRS .DE $8B : NR. CHARS IN STRING.
0 1 2 0 CHRCOUNT .DE $8C : NR. CHARS PLOTTED SO FAR.
0125 BYTECNT .DE $8D: COUNTER FOR 8 BYTES.
0 1 3 0 BYTEVAL .DE $8E : BYTE VALUE TO PLOT.
0135 NDX .DE $C6 : NR CHARS IN KEY BUF.
0140
0145
0150 STORE ROM ADDRESS TABLE
0155 FOR STRING, “NOTE 1:”
0160
0165 .BA $C000
0170
0175 .OS
0180
C000- 70 DO 78 0185 .BY $70 $D0 $78 $D8 $A0 $D8 $28 $D8
Introduction:
i C003- D8 AO D8
C006- 28 D8
C008- 00 Dl 88 0190 .BY $00 $D1 $88 $D1 $D0 $D1
C00B-D1 DO Dl
0195
0200 ; SET BITMAP MODE, ETC:
0205
: Introduction
\Q
1: Introduction
40
Introduction: 1
P r o g r a m 2. Note Is
For mistake-proof program entry’, be sure to read “The Automatic Proofreader," A ppendix C.
41
1: Introduction
42
Chapter 2
Programming
Aids
Brent Anderson and Sheldon Leemon
BASIC Aid
Twenty o f the most useful We’d all like to customize
program m ing aids, from search BASIC to fit our individual
ing an d replacing strings to needs. It’s not difficult to
line renumbering, are included think of some valuable pro
in this one package. Several gramming aids that were
omitted from Commodore’s
DOS support commands also
BASIC. Renumbering, auto
make it simple to see the direc
matic line numbering,
tory (without affecting the searching and replacing,
program in memory), renam e merging, and deleting line
files, or load a n d run programs. ranges are just a few. Some
Mistake-proof entry is easy of the short routines else
when you use MLX to type where in this book add
in this program . just one or two o f these
functions. “BASIC Aid,”
however, gives you 20 new ways to help you write BASIC programs.
Even more importantly, they’re all in one package. You don’t have
to search and somehow splice together several different programs.
If you do any BASIC programming at all, this package will be one
of your most valuable utilities. It takes up nearly 4K bytes of memory,
but it’s safe from BASIC. You can still program normally. And by
using MLX, the Machine Language Editor found in Appendix D, you
can be assured it’s entered correctly.
Some History
The program was originally written for the PET/CBM computers by
James Strasma, and later modified by F. Arthur Cochrane. It’s become
one of the most popular programs available for Commodore com
puters. Until now, unfortunately, there hasn’t been a version written
expressly for the 64. Brent Anderson, who translated this program
to the 64, has been a user of Commodore computers since 1981,
when he joined Strasma and others to form the Central Illinois PET
Users Group. He currently heads ATUG, a Commodore users
group dedicated to exchanging information dealing with machine
language programs.
MLX and BASIC Aid
You’ll use MLX to enter BASIC Aid. Before you begin to type in this
program, make sure you read Appendix D. You’ll also need to type
in and save a copy of the MLX program to tape or disk. Once you’ve
done that, load and run MLX. It will ask for the starting and ending
45
2: Program m ing Aids
46
Program m ing Aids: 2
BREAK
Syntax: BREAK
The BREAK command is used to enter a machine language
monitor like Micromon or Supermon, if such a program has been
previously installed.
CHANGE
Syntax: CHANGE ¡search string/replacement string/, line number
range
or
CHANGE "search string”replacement string”, line number
range
CHANGE replaces one string of characters (the search string)
with another string of characters (the replacement string). Each line
in which the string is replaced is displayed on the screen. If the
optional range of line numbers is specified, the search string is
replaced within that range. Otherwise, the search string is changed
to the replacement string at every occurrence within the program.
Line number ranges should be specified as they are in the LIST com
mand. For instance, CHANGE/FIRST STRING/SECOND STRING/,
-100 changes the string in lines 0-100; CHANGE/F/ftSTSTRING/
SECOND STRING/ 100-200 changes it within lines 100-200; and
CHANGE/FIRST STRING/SECOND STRING/,200- changes it from
line 200 to the end of the program.
Two different delimiters can be used to set off the search and
replacement strings. This is because BASIC stores keyword com
mands (such as PRINT) differently than it does the same combination
of characters when they appear in quotes or as DATA. If it didn’t, it
would get terribly confused by statements such as PRINT “PRINT”.
It would try to execute the second PRINT, rather than printing it
on the screen. The CHANGE command lets you decide which type
of string you wish to change. If the backslash character is used to
separate the two strings, BASIC reserved keywords are recognized as
such. If quotation marks are used, all text is treated as strings of
characters. For example, if the program in memory is:
10 PRINT"ALL THAT THIS PROGRAM DOES IS PRINT"
47
2: Program m ing Aids
48
Program m ing Aids: 2
100, DELETE 100-200 deletes those two lines and all lines in between,
and DELETE 200- deletes lines 200 and up.
DUMP
Syntax: DUMP
DUMP lists the variables used in a BASIC program in the order
in which they were created, as well as shows their current value.
Only scalar (nonarray) variables are displayed. DUMP can be handy
for testing the effect of changing the value of a variable in a running
program. Just hit the STOP key, type DUMP to check the current value
of a variable, edit and enter a new value for the variable, and type
CONT to continue the program using the new value.
FIND
Syntax: FIND /search string/, lin e n u m b er ran g e
or
FIND ‘‘s earch strin g ”, lin e n u m b er ra n g e
The FIND command searches the BASIC program for a string
of characters, and displays the program lines in which the string
appears. This command displays every occurrence of the string,
unless a limiting range of line numbers is specified. The format of this
number range is the same used by the LIST command. If the back
slash character is used to enclose the string, BASIC keyword tokens
within the string are recognized as such, but if the string is enclosed
in quotes, such words are treated as their literal string o f characters.
For examples of this distinction, see the CHANGE command.
FLIST
Syntax: FLIST ‘‘B ASIC p r o g ra m file n a m e "
This reads a BASIC program file on disk and lists the program to
the screen without entering it into memory or otherwise affecting
the program currently in memory. FLIST allows you to make certain
you’ve got the right program before you try to LOAD or MERGE it.
HELP
Syntax: HELP
HELP displays the BASIC program line that was executing when
the program was stopped, and highlights in reverse video the last
character read by the program. It’s helpful for finding which state
ment of a multistatement program line caused an error. On the 64
it’s particularly useful when the screen has been changed from text
to high-resolution graphics, and error messages cannot be read. Since
changing the screen back to text with the RUN/STOP-RESTORE
49
2: Program m ing Aids
combination also erases the error message, HELP can show where
the error occurred.
HEX
Syntax: HEX Shexadecimal number
or
HEX decimal number
You can convert decimal numbers to hexadecimal notation and
vice versa, with this feature. If the number entered is preceded by a
dollar sign, it’s considered a hexadecimal number in the range $0000
to $FFFF, and its decimal equivalent is displayed. If no dollar sign is
entered, a decimal number in the range 0 to 65535 is converted to
hex and then displayed.
KILL
Syntax: KILL
This disables BASIC Aid. To restart the program, type SYS 49152.
MERGE
Syntax: MERGE “BASIC p r o g ra m f il e n a m e ”
This command reads a BASIC program file from disk, lists each
line on the screen, and enters the line just as if it had been typed in
from the keyboard. To use MERGE, first load one program into the
computer’s memory. Do not run it. Then type MERGE “filen am e" ,
using the filename of the program you want to merge into the first.
The program lines using numbers not already found in the first pro
gram (the one in memory) are added, while those that duplicate
numbers already in use will replace those lines.
OLD
Syntax: OLD
As you might have guessed, the OLD command is used to undo
the effects of an inadvertent NEW command. As long as no program
lines are entered after NEW has been entered, OLD can recover the
program. It can also be used to restore the program after a cold start
(either from the COLD command, or using a reset switch connected
to the user port for recovering from a crash).
OFF
Syntax: OFF
This command restores the normal IRQ vector, and turns off the
interrupt-driven functions, namely, program scrolling, quote mode/
50
Program m ing Aids: 2
REPEAT
Syntax: REPEAT
This command is used to toggle the repeat key function. When
BASIC Aid is started, all keys repeat if held down. Entering REPEAT
disables this feature for all but the cursor and space keys; typing it
again reenables it.
SCROLL
Syntax: SCROLL
SCROLL enables all of the interrupt-driven keystroke commands.
These are:
1. Program scrolling. When you move the cursor to the bottom-left
corner of the screen and press the cursor down key, the listing
will roll up, and the next program line will be printed at the bottom
of the screen. If you move the cursor to the top-left corner of the
screen and press the cursor up key combination, the listing will roll
down, and the previous program line will be displayed at the top.
2. Quote mode/insert mode escape. By pressing the fl key, quote
mode and insert mode are canceled, allowing you to move the
cursor normally.
51
2: Program m ing Aids
1. > [Link]. Erases the named file from the disk. If wildcards
such as * or ? appear in the filename, more than one file may
be erased.
2. > [Link] = o ld filen a m e. Changes the name of the disk
file from o ld file n a m e to n ew file n a m e .
3- > C O m ew filen am e = o ld file n a m e . This command copies disk
file o ld file n a m e to the file new file n a m e .
4. > VO. Performs a disk validation or collection, which reclaims disk
blocks marked as in use, but which are in fact not used.
5. > NO. d isk nam e, ID. Formats the disk for use, erasing all informa
tion that it previously contained, and giving it the title and disk
ID number entered in the command.
Two additional DOS support functions are included. Entering the
backslash (/) followed by the program filename loads that program
from disk. Typing the up arrow ( t ) followed by the program filename
loads and runs the program.
BASIC Aid
Be sure to read 1Using the M achine Language Editor: MLX, ’’A p p en d ix D, before typing in
this p ro gra m .
49152 :169,000,141,136,003,141,078
49158 s137,003,173,243,206,174,174
49164 :244,206,224,160,176,007,005
49170 :133,055,134,056,032,089,005
49176 :166,162,015,189,043,192,023
49182 :149,115,202,016,248,162,154
49188 :019,032,059,192,076,089,247
49194 :193,230,122,208,002,230,003
49200 :123,173,255,001,076,154,062
49206 :192,234,076,191,192,189,104
49212 :047,206,240,006,032,210,033
49218 :255,232,208,245,096,208,030
49224 :014,120,162,023,189,162,230
49230 :227,149,115,202,016,248,011
49236 2076,186,199,076,217,205,019
49242 :173,141,002,041,001,208,144
49248 :249,032,125,192,208,020,154
49254 :032,125,192,240,251,201,119
49260 2255,240,247,032,125,192,175
49266 2201,255,208,249,169,000,172
49272 2133,198,076,225,255,173,156
49278 2001,220,205,001,220,208,213
49284 2 248,201,239,096,133,098,123
49290 2162,144,056,032,073,188,025
49296 2076,221,189,230,135,208,179
53
2: Program m ing Aids
54
Program m ing Aids: 2
55
2: Program m ing Aids
56
Program m ing Aids: 2
57
2: Program m ing Aids
58
Program m ing Aids: 2
59
2: Program m ing Aids
60
Program m ing Aids: 2
61
2: Program m ing Aids
62
Program m ing Aids: 2
63
2: Program m ing Aids
64
Program m ing Aids: 2
65
2: Program m ing Aids
66
Program m ing Aids: 2
67
Jeff Young
Auto Line
Numbering
This short routine is a “Auto Line Numbering” is a
handy, time-saving utility utility which automatically
fo r programmers. generates a line number for
the current BASIC program
statement you’re entering. As written, the routine numbers programs
beginning with line 100 and increments by tens (100,110,120, and
so on). This can be easily modified.
How to Use the Program
Auto Line Numbering consists of a BASIC loader which places a
machine language subroutine into a free block of memory at location
49152 (5fC000). This area of memory is not used by BASIC, so the
program should be safe.
Type in the program and SAVE it. After LOADing, type RUN, press
RETURN, type NEW, press RETURN, then type SYS 49152. If you
wish to leave the program for any reason, just press RETURN immedi
ately after you see a new line number. To return to the program,
type SYS 49160. This will continue generating line numbers from
where you left off.
Although the program will always begin numbering with 100 and
increment by tens, you can modify either of these if you wish. If you
want to begin with a number other than 100, determine the number
with which you want to start, then subtract ten. POKE this number
in low-byte/high-byte format into 251 and 252, then SYS 49160.
For example, if you wish to begin with line 1000, subtract ten.
The number you are now working with is 990. To determine low-
byte/high-byte, divide 990 by 256. The result, 3, is the number you
POKE into location 252—POKE 252,3. The remainder of the divi
sion is 222. POKE 251,222. The low byte is location 251, and the
high byte, 252.
If you wished to begin the line numbering with 1000 then,
you’d enter:
POKE 251,222:POKE 252,3
SYS 49160
To change the increment from ten, POKE the desired number
into location 49179. If you want to increment by fives, for example,
you’d enter:
POKE 49179,5
69
2: Program m ing Aids
This utility program can save you a lot of time when pro
gramming, and it provides a neat, structured sequence for program
line numbers.
Auto Line Numbering
For mistake-proof pro gra m entry, be sure to read “The Automatic Proofreader,” A ppendix C.
70
Charles Kluepfd
Numeric Keypad
Turn your keyboard into a You could type in numbers
“Num eric Keypad”fo r more much faster and with fewer
efficient num eric input. The errors if the Commodore 64
program lets you toggle to had a numeric keypad. This
standard or num eric keypad. program offers this handy
feature by redefining a set of
keys to represent numbers instead of letters.
When you run “Numeric Keypad,” your computer will behave
normally until CTRL-N is pressed. The cursor disappears until you
press another key. Then the M, J, K, L, U, I, and O keys will be 0, 1,
2, 3, 4, 5, and 6. By using these along with the numeric keys 7, 8,
and 9, you have a numeric keypad. Pressing CTRL-N toggles the key
board back into its normal mode (again causing the cursor to disappear
until you press a key). If you press RUN/STOP-RESTORE, however,
you won’t be able to use the keypad option. You’ll need to reRUN
the program to restore the feature.
You can put press-apply transfer numbers on the affected keys
to help you remember which number each key represents. You should
use very small ones, so they won’t interfere with the normal identi
fication of the keys. (Transfer letters and numbers are available at
many art supply stores.)
Use Numeric Keypad in a Program
You also can activate and deactivate the numeric keypad from a pro
gram, in anticipation of numeric or nonnumeric input, by POKEing
location 50216 with 255 or 0 respectively. The user can always over
ride this with CTRL-N. (CTRL-N is never passed to the program, but
serves only as the toggle function.) Just don’t POKE any value other
than 0 or 255, because that would prevent you from toggling
with CTRL-N.
If you prefer that the keypad start out as activated, change the
next-to-last DATA item in line 520 from 0 to 255.
Redefining the Keys
To redefine the 64 keys, we transfer the Kemal from ROM into RAM,
change it to intercept the M, J, K, L, U, I, and O keys, and convert
the data to the appropriate numbers.
Lines 3 and 4 POKE the machine language into an unused area
of memory from the DATA statements in lines 5 0 0 -5 6 0 .
Lines 10 and 20 transfer the BASIC interpreter a n d the Kemal
from ROM to RAM with the same addresses, so we can modify them.
71
2: Program m ing Aids
72
Program m ing Aids: 2
Using this INPUT feature, and calling the keypad routine from
within your own program, you could create something like:
10 POKE 50216,255
20 INPUT "NUMBERS";N
30 PRINT N
40 POKE 50216,0
50 INPUT "WORDS";N$
60 PRINT N$
As long as the keypad routine has been placed in memory, you
can activate and deactivate it by POKEing location 50216 with 255
or 0 respectively. Then, using the INPUT statement, you can enter
numbers o r words. This makes the keypad routine even more versatile.
Numeric Keypad
F o r m istake-proof p ro gra m entry, be sure to rea d “The Autom atic Proofreader,"
A p p en d ix C.
73
David W. Martin
One-Touch
Commands
You can p u t the normally Unlike most people,
unused function keys on the 64 computers excel at per
to work with this pro gra m m er’s forming boring, repetitive
utility An entire com m and can tasks. What’s more, time-
be typed with a single keypress. consuming tasks which
annoy us can be performed
You ’ll quickly appreciate the
by an uncomplaining com
time saved with this technique
puter in a fraction of a
as you enter programs. second. It only makes sense
to let computers handle the things they do best.
One of these jobs is the routine typing o f frequently used com
mands. During a session with your computer, how many times do
you type RUN, LIST, SAVE, or LOAD? Probably more times than you
think. If you’re a hunt-and-peck typist new to typewriter-style key
boards, this can be a major annoyance. Even if you’re a fast touch-
typist, you probably stumble over such often-used commands as
POKE 53281,LPRINT“ {BLK} ” (which sets up an easier-to-iead white
screen background with contrasting black characters).
The utility presented here can free you from all that. It redefines
the special function keys (f 1 through f8 to the right of the keyboard)
so that a single keypress enters a whole command. The short while
it takes to type in this program can pay for itself many times over.
One-Touch Commands
Be sure to type the program carefully. As always, save it twice on tape
or disk before running it for the first time. The program is in the
familiar form of a BASIC loader—a BASIC program which includes
a machine language program encoded in DATA statements. A mistyped
number can “crash” the computer when the program is first run,
forcing you to switch it off, then on again to clear the machine. Saving
the program beforehand can prevent you from losing all your work.
Actually, this BASIC loader contains two machine language pro
grams. Neither program consumes any memory normally used by
BASIC (see Programmer’s Notes below). After activating the utility,
it erases the BASIC loader from memory and allows you to load your
own programs. The utility keeps working “in the background,” so to
speak, until you turn off the computer or reset it by pressing RUN/
STOP-RESTORE.
74
Program m ing Aids: 2
The utility is very easy to use. First, enter and run the BASIC
loader. You’ll see a screen prompt which asks:
FI?
Now, type in whatever command you’d like to have available
at a stroke of the f 1 key. Then press RETURN. For instance, if you
answer the prompt by typing LIST and pressing RETURN, hitting
f l after the utility is activated will print the command LIST on
the screen.
There’s a way to save even more keystrokes. If you answer the
prompt by typing the command followed by a back arrow — using
the back arrow key in the upper-left corner of the keyboard — the
utility will press RETURN for you, when activated. Otherwise, it’s
up to you to hit RETURN when using each command. In other
words, answering the prompt like this:
Fl? LIST [Press RETURN]
means that when the utility is working, it will type the command
LIST on the screen for you, but you’ll still have to press RETURN
yourself to actually execute the command. But if you answer the
prompt like this:
Fl? LIST <- [Press RETURN]
it means the utility, when working, will type LIST a n d press RETURN
for you when you hit the f 1 key. The back arrow makes the command
self-executing. Pressing the function key executes the command
instantly. Depending on the command, this may or may not be
desirable. For instance, you probably wouldn’t want the command
NEW to execute instantly because it would be too easy to acciden
tally wipe out a BASIC program. (In fact, you probably wouldn’t
want to program a function key with NEW at all.)
You can also answer the prompt with more than one command.
An example might be:
Fl? LOAD «-RUN <- [PressRETURN]
which means f 1 will automatically load and run the next program
from tape.
After answering the Fl? prompt, the utility asks for F2, F3, and
so on through F8. After F8, the utility immediately activates itself
and erases the BASIC loader from memory.
The function keys are now programmed. They will remain so
until you shut off the computer or trigger a warm start by pressing
RUN/STOP-RESTORE.
75
2: Program m ing Aids
Programmer’s Notes
The one-touch command utility consists of two machine language
programs tucked away in different parts of the Commodore 6 4 ’s
memory. The first part is in the cassette buffer, starting at memory
location 828 (S033C hexadecimal). This program asks for the key
definitions. Each time RETURN is pressed, it stores the ASCII values
of the characters into high memory.
Since the cassette buffer is used only temporarily, as you pro
gram the function keys, you can load or save programs from or to
tape and not erase this routine. There may be problems, however, if
you’re trying to use another machine language program which stores
data starting at location 49152. That’s because this routine uses that
area for its second program (see below).
After entering f8, control jumps to the second program, stored
in high memory at location 49152 ($C000 hex). This is a 4K block
of unused memory in the 64. The first two POKEs in the first line of
the BASIC loader fool BASIC into thinking that memory ends at 53248.
To restore normal vectors, you can enter POKE 56,l60:POKE 55,0.
The first machine language program also sets up an interrupt.
Every sixtieth of a second, the computer checks the second program
to see if a function key has been pressed. If so, the key’s definition is
printed on the screen. If a back arrow was defined after the com
mand, the program forces a RETURN to execute the command also.
One-Touch Commands
For m istake-proofprogram entry, be sure to read ‘ The Automatic Proofreader, ”A ppendix C.
1 POKE56,208;POKE55,0:F=0:C=PEEK(55)-120:IFC<0THEN
C=C+256:F=-1 :rem 84
2 D=PEEK(56)+F:POKE55,C :POKE56,D srem 139
3 S=828:1=146:GOSUB100 srem 11
10 DATA32,198,3,165,55,133,251,133,253,165,56,133,
252,133,254,169 :rem 184
15 DATA49,133,167,169,133,133,168,169,13,32,210,25
5,169,70,32,210 :rem 186
20 DATA255,165,167,32,210,255,169,61,32,210,255,16
9,63,32,210,255 :rem 178
25 DATA169,32,32,210,255,32,207,255,72,160,0,165,1
68,145,55,104 srem 76
30 DATA32,198,3,201,13,240,14,201,95,208,2,169,13,
145,55,32 ;rem 119
35 DATA207,255,76,124,3,230,167,165,167,41,1,208,1
0,24,165,168 :rem 33
40 DATA105,4,133,168,76,170,3,56,165,168,233,3,133
,168,165,167 :rem 43
45 DATA201,57,144,163,120,169,L0,141,20,3,169,80,1
41,21,3,88 srem 215
76
Program m ing Aids: 2
50 DATA169,0,133,167,32,68,166,76,116,164,166,55,2
08,2,198,56 srem 8
55 DATA198,55,96 srem 87
56 S=PEEK(55)+ 256*PEEK(56):1=120sGOSUBl00 srem 76
57 SYS(828) srem 94
58 END srem 68
60 DATA165,167,240,59,160,0,177,251,32,L99,H 0 ,176,
12,165,55,197 srem 147
65 DATA251,208,21,165,56,197,252,208,15,169,0,133,
167,165,253,133 srem 193
70 DATA251,165,254,133,252,76,49,234,166,198,177,2
51,157,119,2,230 srem 252
75 DATA198,32,L111,H0,165,198,201,11,144,204,230,1
67,76,49,234,165 srem 28
80 DATA215,32,L99,H 0 ,176,3,76,49,234,165,8,41,1,20
8,247,160 srem 197
85 DATA0,177,251,197,215,208,6,32,LI11,H 0 ,76,L 6 ,H0
,32,L111,H0 srem 106
90 DATA76,L81,H0,201,133,144,6,201,141,176,2,56,96
,24,96,166 srem 239
95 DATA251,208,2,198,252,198,251,96,0,0 srem 188
100 F=0 sFORD=STOS+Is READA?sIFASC(A $ )<58THENA=VAL(A
$)sG0T0115 srem 173
105 IFASC(A$)=76THENA=VAL(RIGHT?(A$,LEN(A$)-1))+PE
E K (55)sIFA>255THENA=A-256 sF=l srem 73
110 IFASC(A?)=72THENA=VAL(RIGHT?(A ? ,LEN(A?)-1))+PE
EK(56)+FsF=0 srem 22
115 POKED,AsNEXTsRETURN srem 10
77
David W. Martin
Dr. Video
The cursor control keys on While revising long pro
your 64 already give you some rams or doing repeated
o f the most pow erful screen numerical calculations in
editing capabilities o f any home immediate mode, it’s often
computer, but this utility adds useful to be able to clear a
portion of the screen dis
even more: clear screen below
play while leaving the rest
the cursor, clear screen above
intact. It’s also useful at
the cursor, a n d “hom e” the times to be able to “home”
cursor to the bottom left o f the cursor to the lower left
the screen, all at machine of the screen instead of the
language speed. usual upper-left position.
Although Commodore built excellent screen-editing features into
the 64, “Dr. Video” adds even more flexibility by giving you three
additional cursor control keys. A special technique allows Dr. Video
to function even while you are typing or running another program.
Since the program is written entirely in machine language, it doesn’t
take up any of the memory normally used for BASIC programming.
The new cursor control features are assigned to three of the 6 4 ’s
function keys. The assignments are as follows:
f 1 Clear display to the top of the screen starting with the line
containing the cursor.
f3 Clear display to the bottom of the screen starting with the
line containing the cursor.
f5 Move the cursor to the lower-left corner of the screen.
How the Doctor Operates
Every 'Ao second your 64 stops whatever it is doing and takes some
time to read the keyboard and perform other housekeeping tasks.
These breaks are called interrupts, and the machine language pro
gram which runs during this interrupt period is called the interrupt
service routine. When the microprocessor receives the interrupt
request (IRQ) signal, it looks at a pair of memory locations to find
the starting address (called the IRQ vector) of the interrupt service
routine to be executed. On the 64, the IRQ vector is contained in
locations 788 and 789, which normally point to address 59953, the
beginning of the standard IRQ service routine in ROM (unchanging
memory). However, since the IRQ vector is stored in RAM, changeable
memory, we can substitute the address of our own machine language
subroutine and add it to the normal interrupt service routine.
78
Program m ing Aids: 2
79
E. A. Cottrell
Step Lister
‘‘Step Lister” is a timesaving As you’re programming,
program m ing aid which lets you probably use the LIST
you look at your BASICprogram command often. It’s the
lines without repeatedly typing only way you can turn the
LIST. It’s short, a n d safe pages of your program
fro m BASIC. when it’s in your comput
er’s memory. But LIST can
be clumsy, for you have to either constantly type LIST and hit the
RUN/STOP key, or type LIST and a line number or range. You can
easily miss the line you want to examine. No matter what, you
have to type LIST a lot. “Step Lister” is a machine language w edge
(explained below) which allows you to step through a BASIC listing
one line at a time.
Type in and SAVE Step Lister. It’s in the familiar form of a BASIC
loader which POKEs a machine language program into high memory
starting at location 49152 and issues a SYS command to run it. Since
even a single error in typing it in can lock up your computer, forcing
you to turn it off, then back on, to restore control, make sure you
save Step Lister before RUNning it. That way, you won’t lose all
your typing.
RUN the program. It’s now safe from BASIC. You can LOAD
another program and use Step Lister to look through it.
To see the first line of your program, just type:
@ 0.
(Entering any other number after the @ will start the listing at that
line. There should be no spaces between the @ and the line number,
and the @ must be on the left edge of the screen.)
Then, press any key and the next line will be displayed. Press the
space bar and hold it down, and the listing will continue scrolling
until the space bar is released.
If you wish to stop Step Lister, press RUN/STOP. The cursor
returns to the screen and you can edit a line or lines. Step Lister is still
available; enter the @ symbol and a line number to see another part
of the program.
What Is a Wedge?
To understand a wedge, you must first have some knowledge of how
BASIC works. When you press RETURN, one of two things happens.
If the entered line has a number as the first character, the computer
assumes that a BASIC line is being entered. This line is then converted
to BASIC token s and put in its proper place in memory. (Tokens
80
Program m ing Aids: 2
81
2: Program m ing Aids
200 DATA255,133,20,133,21,160,1,132,198,160,1,132,
15,177,95,240,175,32 srem 144
220 DATA44,168,32,215,170,134,25,132,26,173,198,0,
240,251,169,0,141,198 srem 214
230 DATA0,166,25,164,26,200,177,95,170,200,177,95,
197,21,208,4,228,20 srem 120
250 DATA240,2,176,44,132,73,32,205,189,169,32,164,
73,41,127,32,71,171 srem 120
270 DATA201,34,208,6,165,15,73,255,133,15,200,240,
17,177,95,208,16,168 srem 171
290 DATA177,95,170,200,177,95,134,95,133,96,208,16
3,108,6,3,16,218,201 :rem 188
310 DATA255,240,214,36,15,48,210,56,233,127,170,13
2,73,160,255,202,240,8 srem 251
320 DATA200,185,158,160,16,250,48,245,200,185,158,
160,48,181,32,71,171,208 srem 112
350 DATA245,0,230,122,208,2,230,123,173,0,2,201,58
,240,10,201,32,240,239 srem 214
370 DATA76,13,192,234,234,234,96 srem 99
82
Charles Brannon
Foolproof INPUT
Machine language routines, Problems with INPUT
even short ones, can perform You are probably familiar
some impressive functions. with some of the problems
Overcoming some o f the p ro b with the INPUT statement.
lems o f the INPUT statement is First, it will not properly
relatively easy in machine lan handle input with commas
and colons. If you entered
guage. This routine reprograms
the previous sentence, the
BASIC’s own INPUT routine
computer would accept
a n d can be added to your own only the word “First” and
program . Since it’s in theform ignore the rest of the line
o f a BASIC loader, you do n’t (as the computer warns you
need any special knowledge with ?EXTRA IGNORED).
o f machine language. This is because the comma
is used to separate multiple
INPUTs on the same line, as in this example:
INPUT “ENTER NAME: FIRST,LAST”;A$,B$
The colon, too, triggers an ?EXTRA IGNORED message. Yet it
cannot be used to separate INPUT items, so it appears to be some
kind of a bug (error) in the BASIC language itself.
You can get around these problems somewhat, but they become
especially annoying when you are trying to read a file on tape or disk
containing these characters. In a mailing list program, for instance,
you need commas for address fields such as “Greensboro, NC, 27403”.
There are other difficulties with the INPUT statement. Quota
tion marks are not handled correctly. Leading and trailing spaces are
stripped away. INPUT also allows people to use all the cursor and
color control keys. Theoretically, you can place the cursor anywhere
on the screen where there is something you want to INPUT, and press
RETURN. In effect, this is what happens when you edit a program
(the same INPUT routine is used by both the system and BASIC). But
it just makes no sense to allow cursor moves all over the screen
when you simply want the user to answer a question. If the user
accidentally presses a cursor key and then tries to move the cursor
back, the entire line, including any prompts, is read.
This can also be a problem when you have carefully laid out a
screen format with blanks or boxes into which the user is supposed
to enter information. You have no way to control how many charac
ters the user can type, so if your blank space is only ten characters
long, there is nothing to prevent someone from typing more. Not
83
2: Program m ing Aids
only that, but with the standard INPUT routine, someone can move
the cursor out of the box you want them to use, clear the screen
entirely, or otherwise destroy your carefully planned format.
Improving on INPUT
What we need, then, is a new INPUT routine that will not allow
cursor moves. The DEL key should still let the user delete characters
to make corrections, however. Additionally, the ideal INPUT routine
should let your program limit the number of characters typed, yet
allow commas and colons.
The usual solution is to write your own INPUT routine using
the GET statement, which fetches one key at a time from the key
board. With such a simple statement as GET, however, you have to
reinvent the wheel anytime you need such a protected INPUT rou
tine. And it certainly isn’t as easy to use as a simple INPUT statement.
Well, I certainly wouldn’t bring such gloom to the scene without
a solution. The accompanying program is the key. It’s a machine
language routine that replaces the standard Commodore INPUT with
a protected INPUT like the one described above. The beauty of it is
that after you GOSUB 60000, all INPUT (and INPUT#) statements are
redefined. You don’t have to understand how the machine language
works in order to use it, and you don’t have to rewrite any existing
programs, other than to insert the GOSUB. You still have all the flexi
bility of the standard INPUT statement. Just add the subroutine to
the end of your program.
The machine language program has a couple of niceties. After
you GOSUB 60000, you can change the maximum number of charac
ters allowed by POKEing memory location 251 with the length (don’t
POKE with zero, or more than 88). The cursor is an underline by
default, but you can change the character used by POKEing its ASCII
value into memory location 2. For example, to change the cursor
into an asterisk, enter:
POKE 2,ASC(“ * ”)
or
POKE 2,42
When you use the routine to INPUT data from files, just remem
ber that it strips away all control characters, from CHRS(0) to CHR$(31)
and CHR$(128) to CHR$(159). This includes all special codes such as
cursor controls, function keys, color codes, etc. You’ll rarely write
these to a standard data file, anyway.
84
Program m ing Aids: 2
Cautions
Curiously, “Foolproof INPUT” does not work properly in direct
mode. To make BASIC accept commas and colons, an invisible quote
is added to the start of each line that is input. Naturally, direct mode
doesn’t like statements such as RUN or LIST. If you want the special
INPUT routine out of your way, just press RUN/STOP-RESTORE.
The invisible quote also prevents you from using something like
INPUT A. Only string variables work with this routine; use INPUT
A$ instead. If you want, you can include a line such as:
INPUT A$:A = VAL(A$)
instead of INPUT A.
To display the contents of AS (or any other string variable set
with INPUT using this routine), PRINT it from within a program.
For instance:
10 GOSUB 60000:INPUT A$
20 PRINT A$:END
would PRINT the contents of AS. If you INPUT something, then use
the PRINT statement in direct mode, you’ll get a SYNTAX ERROR.
Pressing RUN/STOP-RESTORE before printing, however, displays the
contents of the string variable correctly.
Foolproof INPUT
For mistake-proof p rogram entry, be sure to read “The Automatic Proofreader,”A ppendix C.
60000 IFPEEK(830)=133THEN60020 rem 145
60010 FORI=8 28T0983:READA:POKEI,A sNEXT rem 124
60020 SYS828:RETURN rem 179
60030 DATA 169,000,133,252,169,080 rem 135
60040 DATA 133,251,169,164,133,002 rem 131
60050 DATA 169,083,141,036,003,169 rem 142
60060 DATA 003,141,037,003,096,152 rem 127
60070 DATA 072,138,072,165,252,208 rem 144
60080 DATA 007,032,116,003,169,000 rem 123
60090 DATA 133,253,166,253,189,000 rem 143
60100 DATA 002,133,254,198,252,230 rem 129
60110 DATA 253,104,170,104,168,165 rem 133
60120 DATA 254,096,160,000,169,034 rem 135
60130 DATA 141,000,002,132,252,165 rem 115
60140 DATA 002,032,210,255,169,157 rem 132
60150 DATA 032,210,255,032,228,255 rem 131
60160 DATA 240,251,164,252,133,254 rem 135
60170 DATA 169,032,032,210,255,169 rem 141
60180 DATA 157,032,210,255,165,254 rem 141
60190 DATA 201,013,240,043,201,020 rem 111
60200 DATA 208,013,192,000,240,211 rem 114
85
2: Program m ing Aids
86
John Krause and David W. Martin
6 4 Searcher
“64 Searcher” is a time-saving When working on a long
utility that searches through your BASIC program, it pays to
BASICprogram and locates any plan ahead. But it seems
character or string o f characters. that no matter how hard
you try, you can’t keep track
of everything in your program. Can I use H to store the high score,
or is that variable already being used for something else? Where is
this subroutine called from? You probably end up searching for a
number or word hidden among scores of program lines.
“64 Searcher” allows you to spend less time searching and more
time programming. Simply give it the string of characters to search
for, and it tells you the numbers of all lines in which the string appears.
It can search a hundred lines faster than it takes you to search one.
It’s fast because it’s machine language. But you don’t have to know
machine language to use it.
Searching
Enter the program carefully and save it on tape or disk before running
it for the first time. In the form of a BASIC loader and a series of DATA
statements, the program must be entered exactly as it appears.
By using “The Automatic Proofreader” in Appendix C, you should
be able to type it in correctly the first time. If there’s even one
error, the computer may lock up (not respond to keypresses). You’ll
have to turn it off, then on again, to regain control. If you’ve saved
the program, you can load it again and begin looking for the
typing mistake.
To use 64 Searcher, load and run it, then load your BASIC
program. 64 Searcher doesn’t use any BASIC memory, so you can
work on your program normally. To initiate a search, type 0 followed
by the string you want to find. The string must be enclosed within
either slashes or quotes. Hit the RETURN key and the string is stored
in your program as line 0. If your program already has a line 0,
you’ll have to change that line number because the string must be the
first line in the program.
Type SYS49152 and press RETURN. Instantly you’ll see numbers
appear on the screen. These are the line numbers that contain the
string you specified. If no match is found, no numbers will be printed.
If the string occurs more than once in a line, the line number is
printed only once.
Once you’re done searching through a BASIC program, remem
ber to delete line 0 before saving or running it.
87
2: Program m ing Aids
After entering SYS49152,64 Searcher will find the AND in this line:
10 IF X=1 AND Y=2 THEN 50
but not in this line:
20 PRINT "THIS AND THAT"
To find the AND in line 20 above, you ’d use quotes instead of slashes.
64 Searcher is an excellent debugging aid, especially for long pro
grams, and will be a valuable addition to your toolbox of machine
language utilities.
64 Searcher
For mistake-proof program entiy, be sure to read ‘T he Automatic Proofreader, ”A ppendix C.
10 F0RI=49152T049255:READJ:K=K+J:POKEI,J sNEXT
srem 6 6
20 IFK<>16302THENPRINT"ERROR IN DATA S T A T E M E N T S S
TOP :rem 117
30 PRINT"{CLR}SYS49152 TO SEARCH" :rem 36
100 DATA169,1,133,251,169,8,133,252,160,0,177,251,
56,229,251,56 srem 80
110 DATA233,5,141,104,192,233,2,141,105,192,160,0,
177,251,170,200 srem 142
120 DATA177,251,240,67,133,252,134,251,160,0,177,2
51,56,229,251,170 srem 17
130 DATA202,134,2,198,2,165,2,205,104,192,48,222,1
33,253,173,105 srem 110
140 DATA192,133,254,164,253,177,251,164,254,217,5,
8,208,229,198,253 srem 45
150 DATA198,254,208,239,160,2,177,251,170,200,177,
251,32,205,189,169 srem 8 8
160 DATA32,32,210,255,76,26,192,96 srem 190
88
Dan Carmichael
The Four-Speed
Brake
Not only does this machine One small inconvenience
language routine vary the speed of programming with the
o f your listings, but it can Commodore 64 (if you don’t
also select the speed at which have a printer) is the limita
your BASIC program s execute. tion of being able to display
only relatively small sec
A nd it doesn’t use any o f your
tions of your programs on
BASIC program m ing memory.
the screen at one time. If
you have a large BASIC program, listings can be hard to follow. Even
slowing down the LIST command with the CTRL key isn’t much
help at times; the BASIC lines still pass by at a reasonably fast rate,
and because of the way the lines “jump,” they can be hard to follow.
The short program that follows will help slow things down for
you. It’s a “Four-Speed Brake” that lets you vary the speed of your
listings from reasonably slow to a complete stop. The program is writ
ten in machine language, and normally sits undisturbed in an area of
available memory called the cassette buffer. Once it is POKEd into
memory, it uses none of your available BASIC programming memory.
How to Use the Program
First, load your BASIC program into the computer, then either append
this program to it, or type it in after your program. Before running
the program for the first time, verify it carefully, and save it to tape
or disk. An error in this (or any) machine language program can
cause your system to crash, forcing you to turn your computer off
and then on to reset. After verification, type RUN 60000 and press
RETURN to POKE the machine language program into the cassette
buffer. Then type SYS828 and press RETURN. The Four-Speed Brake
is now running.
The program is controlled by the special function keys. The chart
illustrates what the function keys do.
To stop Four-Speed Brake, press RUN/STOP-RESTORE; to restart,
enter SYS828.
After the Four-Speed Brake has been successfully POKEd into
memory and tested, you may, if you wish, delete lines 6 0 0 00-60040.
Also, the CTRL key will still work as it normally does in slowing
down your listings, and might be considered a “fifth speed,” a little
faster than the f l key.
89
2: Program m ing Aids
Words of Caution
First, this program runs in the cassette buffer, and as is true with all
programs in this buffer, you cannot use the tape cassette while this
program is running. Second, because of the way the computer out
puts the lines while listing programs, you will encounter a glitch
every now and then. It will appear as if one line repeats itself. If you
continue to hold down the function key and let the screen scroll, it
will take care of itself. You can observe how this happens if you list
a program while holding down the f5 key.
If you’re a machine language programmer, the Four-Speed Brake
will also work, both in listing and running your ML programs. To
use, enter SYS828, then SYSXXX into your ML monitor as usual, or
SYSXXX into your machine language program. However, a word of
caution is needed here. The Four-Speed Brake uses all three registers
(A, X, and Y), so you’ll have to be careful when using these registers
in your own program.
Four-Speed Brake
For mistake-proof pro gra m entry, be sure to read “The Automatic Proofreader," A ppendix C.
90
Todd Heimarck
ASCII/POKE
Printer
The computer does the work Reference Tables
fo r you in this short routine, Chances are, PRINTing to
which automatically calculates the screen was one of the
ASCII and POKE values. A handy first things you learned to
program m ing utility, there’s even do in BASIC. You probably
a BASIC-machine language com also learned how to control
where the computer prints
parison included. A disassembly
by putting cursor com
o f the code is also listed.
mands within strings or by
using SPC and TAB commands. The PRINT command is common,
primarily because it is so easy to use. But in certain situations, you
may need to find out a character’s ASCII number. And sometimes it
is quicker to simply POKE a character onto the screen.
But before you can POKE, you have to know the character num
ber. Let’s put a row of hearts at the top of the screen. So we need to
POKE a bunch of 81’s. Wait, those are solid circles. What’s the number
for hearts? I know that list is somewhere.
If you use POKEs or ASCII values in programming, you know
how annoying it is to flip back and forth through the reference
book, losing time and patience. Even worse, you could lose the book
and end up typing the character and PEEKing screen memory to
get the POKE value.
Let the Computer Do the Work
Your computer already knows the POKE values and ASCII numbers,
so why not let it do the work?
This short machine language program, ‘ ASCII/POKE Printer,”
does not use any BASIC memory. Its 52 bytes remain in the cassette
buffer, ready to convert letters and graphics characters to POKE and
ASCII numbers whenever you want.
Note that if you write a program that POKEs any of the address
locations of the cassette buffer (828-1019), you may lose ASCII/POKE
Printer. Also, if you use a cassette player for SAVEs, LOADs, or tape
files, you will erase the machine language program. Fortunately, it is
entirely relocatable, so if you want to use the cassette buffer, you
can change line 10 to move it to another part of memory. On the 64,
it is usually safe to use any of the memory locations from 49152
to 53247.
91
2: Program m ing Aids
Special Cases
There are some ASCII numbers that have no equivalent POKE. For
example, adding CHR$(13) to a string will force a RETURN after the
string is printed. But ASCII 13 cannot be POKEd to the screen (what
would a RETURN look like?). ASCII/POKE Printer will give you the
correct ASCII numbers, but for certain characters, like RETURN, it
will print a blank space and list a POKE of 32 (which is the number
for a blank space). In the case of function keys, CLR/HOME, INST/DEL,
and color commands, it will print a reverse video character, as if in
quote mode, and the correct ASCII number. But the POKE number
will be wrong. Keys that perform a function — clearing the screen,
for example — are not characters that can be POKEd to the screen.
Also note that you cannot get values for reverse video characters,
which do not have separate ASCII numbers. To program a reverse
character, precede it with a CHR$(18). To POKE a reverse video
character, a d d 128 to the POKE value of the regular character.
This machine language utility will be most helpful when you are
writing BASIC programs. By letting the computer tell you ASCII and
POKE values, you can really save time. The program was written to
be short and simple, but if you are familiar with machine language,
you could modify it to do much more.
92
Program m ing Aids: 2
but why look in the book when the computer already knows the
numbers? Let the computer do the work.
Writing the machine language routine was fairly easy, because
once again the computer can do the work. There are a number o f
useful built-in ROM routines. Call the routines a few times and you
have the answer.
The first routine is GETIN. When you jump to this subroutine
(JSR $FFE4), it checks to see if a key has been pressed. If so, the
ASCII value of the key is put into the accumulator. If not, a zero is put
in the accumulator. Then it returns from the subroutine.
Another useful Kemal routine is CHROUT. When you JSR SFFD2,
the computer checks the value in the accumulator. The number is
translated from ASCII to a character and it is printed on the screen
(wherever the cursor happens to be at the time).
The final routine is at SBDCD. Among other things, it’s used by
BASIC’s LIST routine to print line numbers. It takes the number in
the accumulator (the most significant byte or MSB) and multiplies it
by 256. Then the number in the X register (the least significant byte,
or LSB) is added. The result is converted into ASCII numbers and
printed on the screen. For example, if you load the accumulator
with number two and put 88 into X, JSR jSBDCD calculates
2 * 256 + 88 = 600 and prints CHR$(54) + CHR$(48) + CHR*(48).
In other words, it prints the characters for the number 600.
The disassembly of the object code is listed as Program 2 at the
end of this article.
Here’s a line-by-line explanation of the disassembly:
Line Function
033C C h e ck to see if a key has b e e n pressed.
0 3 3F If not, g o b ack to 0 3 3C. If so, th e accum ulator h olds th e ASCII value
and th e p rogram co n tin u es.
0341 Save th e value in X .
0342 C om p are A to th e n u m b er $ 8 D (th e ASCII fo r shifted retu rn, to
ex it to BASIC).
0344 I f n o t eq u al (to $ 8 D ), c o n tin u e at 0 3 4 7 .
0346 A d o es eq u al SH IFT-RETURN , retu rn to BASIC.
0347 Put $ 9 3 into th e accu m u la to r (ASCII fo r cle a r screen).
0349 Print it.
034C Load A w ith $F F (2 5 5 )
034E Store it in th e q u o te flag (w h ich tu rns o n q u o te m o d e). N ow if
you push th e clea r scree n key, you w ill see a reverse heart.
0350 Transfer th e n u m b er in X b a c k to A .
0351 Print th e character.
0354 Load A w ith $ 2 0 (ASCII fo r a b lan k space).
0356 P rint it.
93
2: Program m ing Aids
94
Program m ing Aids: 2
10 F0RJ=828T0879:READK:P0KEJ,K:NEXT :rem 0
15 READY:IFY< >999THENSTOP :rem 151
20 DATA32,228,255,240,251,170,201,141,208,1,96,169
,147 :rem 142
21 DATA32,210,255,169,255,133,212,138,32,210,255,1
69,32 :rem 190
22 DATA32,210,255,169,0,32,205,189,169,13,32,210,2
55 :rem 40
23 DATA169,0,133,212,174,0,4,32,205,189,232,208,20
4 :rem 240
25 DATA999 srem 44
Program 2. Disassembly
033C JSR $FFE4
033F BEQ $033C
0341 TAX
0342 CMP #$8D
0344 BNE $0347
0346 RTS
0347 LDA #$93
0349 JSR $FFD2
034C LDA #$FF
034E STA $D4
0350 TXA
0351 JSR $FFD2
0354 LDA #$20
0356 JSR $FFD2
0359 LDA #$00
035B JSR $BDCD
035E LDA #$0D
95
2: Program m ing Aids
96
Thomas Henry
6 4 Escape Key
While program m ing, there are How many times has this
lots o f ways to get trapped inside happened to you? \bu’re sit
quotes a n d be unable to use ting at your Commodore
the cursor controls. Until now, 64, entering or editing a pro
your only recourse was to hit gram, and through a series
RETURN and try the line again. of keystrokes that you prob
ably don’t even remember,
With this handy utility, you
get into the following trap.
can escape fro m “quote m ode” When you push a cursor
traps by just hitting the p o u n d movement key, instead of the
sign key. The routine also serves cursor actually moving, you
as an example o f machine lan get a reverse video field
guage program m ing fo r those symbol on the screen.
who are interested in trying Frustrating, isn’t it? As you
their hand at it. have probably learned,
about the only way to get
free of the trap is to hit RETURN to get out of the line, and then
start over.
Here’s an easier way: a program that adds a valuable escape
option to your computer. With this feature, the seldom-used British
pound symbol (£) becomes an escape key. When you are stuck in
the cursor trap mentioned above, simply push the key; you will be
released from what’s called the qu ote m o d e and will be free to move
the cursor as desired. Before looking at the program, let’s examine the
problem in greater detail.
Store or Perform the Action
Some of the computer’s keys are able to perform two distinct jobs,
depending on whether the computer is in the immediate or program
mode. These keys include the four cursor keys, RVS ON, RVS OFF,
CLR, HOME, INST (Insert), DEL (Delete), and all of the color selec
tion keys. In the immediate mode, you push one of these keys and the
action is performed immediately. For example, depress the cursor
right key and the cursor moves one space to the right.
But one of the truly impressive features of your Commodore
computer is its ability to store or save the action implied by the key.
For example, here’s a one-line program:
10 PRINT"{RIGHT}HELLO"
The string contains the word HELLO preceded by a cursor-right.
When you type this line into the computer, the cursor-right movement
97
2: Program m ing Aids
98
Program m ing Aids: 2
off the quote mode, a zero in REVERS turns off the reverse screen
mode, and a zero in NOINST nulls out the number of inserts pend
ing. Turning off these three locations allows you to escape from all
of the “offending” modes.
99
2: Program m ing Aids
key. T h e n h it th e c u r s o r k e y o n c e m o r e . N o t ic e th a t th is tim e y o u
a c tu a lly m o v e to t h e r ig h t. T h i n k o f t h e m o s t o u t la n d is h c o m b i n a
tio n o f k e y s tro k e s th a t y o u c a n , th e n tr y th e e s c a p e . T h e q u o te m o d e
is p o w e r le s s to h o ld y o u r c u rs o r.
SEI
LDX #<NEWIRQ ;SET UP NEW IRQ VECTOR.
LDY # > NEWIRQ
STX IRQVEC
STY IRQVEC+1
CLI
RTS ;RETURN TO BASIC.
100
Program m ing Aids: 2
101
E. A. Cottrell
Variable Lister
You almost always use variables There are two types of
when you write a BASIC p ro variables, sim ple and array,
gram . Sometimes, though, in a and three categories in
long program , you can lose each type, flo a t in g p o in t
track o f them. This utility lists num eric, integer n u m eric,
and string. All of these
all your variables in order,
variables are stored in the
including variable type, such
64 immediately above the
as simple or array. It’s an
BASIC program.
especially helpful tool fo r writ The simple variables
ing program documentation. are stored below the arrays
starting at the address pointed to by memory locations 45 and 46
(see box). Each of these simple variables occupies seven bytes of
memory. The first two bytes contain the first two characters (in ASCII
code) of the name of the variable, with coding to indicate which
type of variable it is. This coding is accomplished by adding 128 to
b oth characters if it is an integer variable and by adding 128 to the
second character if it is a string variable. No coding indicates a float
ing point variable. The remaining bytes in numeric variables contain
the value of the variable. In the case of string variables, the remain
ing bytes contain the length of the string and the location at the top
of memory which contains the first character of the string.
Arrays are quite different in that the length of the variable is
determined by the number of elements in the array. The information
which must be stored for an array variable includes the name of the
variable, which is coded the same as for a simple variable, a pointer
to the location of the next variable, the number of dimensions in
the array, and the number of elements in the array.
In addition, the value of each element in numeric arrays, or the
pointer to the string and its length for string arrays, must be stored.
As you can see, array variables can eat up a lot o f memory in a hurry.
It is best to use the lowest possible number of elements in your arrays
If you do not specify the size of an array, the computer will set it
at eleven elements. If you need less than eleven, you’ll save a mini
mum of five bytes per element if you establish the size of the array
with a DIMension statement. Although a simple integer variable takes
up the same amount of memory as a simple floating point variable,
three bytes per element can be saved if you use integer instead of
floating point variables in arrays.
102
Program m ing Aids: 2
Address Pointers
Now and then you’ll see a reference to “pointers” within the com
puter’s memory. These are two-byte long numbers, usually located
in the first 2 5 6 m em ory cells o f the computer, w hich hold an
important address.
Things change while a program is running or being written. For
example, if you add a line to a BASIC program, you’ve expanded the
amount o f space that the program is taking up in RAM memory. Obvi
ously, w hen you go to save the program, the com puter has to know
where the BASIC program ends. So, it keeps track o f the “current
top o f BASIC program” in a pointer. This pointer is located in the 64
in addresses 45 and 46. The number held in cell 4 6 is multiplied
by 2 5 6 and then added to the number in cell 45 . To see at w hich
address in RAM m emory your current BASIC program ends, you
can type: ? PEEK (45) + PEEK (46) * 256.
There are a number o f other pointers as well, including “limit
o f memory,’’ “start o f arrays,’’ ‘‘string storage,’’ and ‘‘start o f BASIC.’’
The locations o f these pointers are listed in m em o ry m aps for each
computer. The best m emory map for the Com m odore 64 is M ap
p in g the 64, by Sheldon Leemon, published by COMPUTE! Books.
There are some interesting things you can do by manipulating
these pointers with POKEs. For one thing, you could fool the com
puter into reserving space for programs in odd places, or even parti
tioning m emory so that two independent BASIC programs could
run simultaneously. In any event, pointers hold inform ation essen
tial to the computer, and their values can be accessed using the
formula above.
103
2: Program m ing Aids
To use Variable Lister, first load and run it. Note the SYS number
you’ll later enter. Next, load the program whose variables you want
to list. Type RUN. The BASIC program has to be run before you give
the SYS to start the Lister. This is because the variables of a BASIC
program are not set up in memory until it’s run. Break out of the pro
gram by pressing the RUN/STOP-RESTORE keys, then enter the
correct SYS (which Lister gave you earlier).
What you’ll see on the screen is a list of the simple variables in
the order of appearance in the program, along with indicators of
their type. Next the array variables are listed with proper indicators.
Function variables (in the form fn(x)) are noted by an asterisk (*).
Variable Lister is especially useful when you write programs
with many variables and have to find new names. It is also valuable
for documenting programs once they’re completed.
The variables are listed across the screen to prevent them from
scrolling out of view. If you have a printer, the following changes
may be made to give you a listing which may be easier to read.
160 IF PA< >35126 THEN PRINT"DATA ERROR":END
260 DATA 32,210,255,169,13,32,210
420 DATA 41,32,210,255,169,13,32
To send the list to your printer, simply OPEN a file to your printer:
OPENl,4 :CMD1 :SYSXXXXX
Variable Lister
For mistake-proof pro gra m entry, be sure to read 'The Automatic Proofreader,” A ppendix C.
104
Program m ing Aids: 2
105
Eric Brandon
Disk Defaulter
Short a n d simple, this machine When Commodore designed
language routine saves typing the operating system used
if you regularly use a disk drive in the Commodore 64, the
instead o f a cassette recorder. designers assumed that most
SAVES, LOADS, a n d VERIFYs people would be using a
automatically default to disk cassette recorder for stor
age instead of the more
rather than tape when you
expensive disk drive. That’s
have this routine in memory. why, when you type LOAD
or SAVE, the computer responds by prompting “Press Play On Tape”
or “Press Record & Play On Tape.” It d efau lts to the tape recorder.
If you’re using a disk drive, you have to type the device number
— ,8 — after each command (as in LOAD“filename”,8). This can
become bothersome after a while.
“Disk Defaulter” is a short utility, written in machine language,
that modifies the computer’s operating system to recognize the disk
drive, instead of the cassette recorder, as the default device. As long
as the utility is activated, you no longer have to append ,8 to the
LOAD, SAVE, and VERIFY commands.
To use Disk Defaulter, enter the program. When you type RUN,
this BASIC loader POKEs the machine language into some free
memory space and activates the utility. To turn it off (for instance, if
you want to use cassette), press RUN/STOP-RESTORE. To turn it
back on, type SYS 679.
To load machine language programs, you still must type LOAD
“filename”,8,l. Also, pressing SHIFT-RUN/STOP will not access the
disk drive because it results in a “Missing Filename Error.” But other
wise, all LOAD, SAVE, and VERIFY commands will refer to disk.
The only program we’ve found that interferes with Disk Defaulter
is the PAL Assembler for the Commodore 64.
Disk Defaulter
For mistake-proofprogram entry; be sure to read ‘ The Automatic Proofreader, ”A ppendix C.
106
Program m ing Aids: 2
107
Chapter 3
High-Speed
Graphics
Charles Brannon
Ultrafont +
Character Editor
This fast, feature-packed, Anyone who has used graph
machine language utility paper to plot out characters,
makes custom characters a then tediously converted
breeze. Its unique features let the rows into decimal
you concentrate on your art numbers, can appreciate
a character editor. Instead
work instead o f program m ing.
of drawing and erasing on
paper, you can draw your characters freehand with a joystick. “Ultra
font + ” has been written to offer almost every conceivable aid to
help you design whole character sets.
Typing It In
Ultrafont + is written entirely in machine language, giving you speed
and efficiency that BASIC can’t match. Although this gives you a prod
uct of commercial quality, it carries the liability of lots of typing.
Ultrafont + is actually rather short, using less than 4K of memory at
hexadecimal location SCOOO (49152), which is reserved for programs
like Ultrafont + . Therefore, you don’t lose one byte of BASIC
programming space.
However, 4000 characters require three times as much typing,
since each byte must be represented by a three-digit number (0 0 0 -
255). With that much typing, mistakes are inevitable. To make things
manageable, we’ve prepared Ultrafont + to be typed in using MLX,
the Machine Language Editor. Full instructions are provided in
Appendix D. So despite the typing, rest assured that a few afternoons
at the keyboard will yield a substantial reward.
Once you’ve entered, saved, and run MLX, answer the two
questions, startin g ad d ress and en d in g address, with 49152 and
52409, respectively. After you’ve saved the program with MLX,
you can load it from disk with LOAD “filename”, 1,1 from tape or
LOAD “filename”,8,1 from disk. After it’s loaded, enter NEW, then
SYS 49152. This command runs the machine language program at
$C000 (12*4096 = 49152).
The Display
After you SYS to Ultrafont + , you should see the work area. At the
bottom of the screen are eight lines o f characters. These are the 256
characters you can customize, arranged in eight rows of 32 charac
ters. A flashing square is resting on the @ symbol, the home position
111
3: High-Speed Graphics
of the character set. Above the eight rows is the main grid, a blown-
up view of ten characters. The last row of the screen is reserved for
messages. The first time you SYS 49152, you’ll be asked whether you
want to edit the uppercase/graphics character set, or the lowercase set.
About the Grid
The grid is like a large-sized window on the character set. You see the
first five characters and the five beneath them. A large red cursor
shows you which character you are currently editing, and a smaller
flashing square is the cursor you use to set and clear pixels in order
to draw a character.
Moving Around
You can use the cursor keys (up, down, left, right) to move the large
red cursor to any character you want to edit. If you move to a char
acter not on the large grid (out of the window), the window will auto
matically scroll to make the character appear. You can also look at
the bottom of the screen to move the larger cursor, as the flashing
square on the character set moves with the main grid.
The HOME key moves the small cursor to the upper-left corner
of the screen. If you press it twice, it will take you back to the top of
the character set — to @ .
A joystick (plugged into port 2) moves the small cursor within
the grid. If you move the cursor out of the current character, the
red cursor will jump to the next character in whatever direction you
want to move. The display at the bottom will adjust, and the grid
will scroll as necessary. This means that you can ignore the traditional
boundaries between characters, and draw shapes as big as the entire
character set (256 x 64 pixels — a pixel is a picture element, or dot).
You can still edit one character at a time, or make a shape within a 2
x 2 box of characters. There is no wraparound for the cursor in the
bottom section of the screen. When it hits an edge, it will go no
further in that direction.
The fire button is used to set and clear points. When you press
fire, if the cursor is resting on a solid square, it will be turned off. If
the square is off, it will be turned on. If you hold down the fire but
ton while you move the joystick, you can stay in the same drawing
mode. If you set a point, you will continue to draw as you move. If
you clear a point, you can move around and erase points all over
the screen.
If the drawing cursor is too fast or too slow to use, just press V
to set the cursor velocity (speed). Answer the prompt with a speed
from 0 (slow) to 9 (too fast for practical use).
112
High-Speed Graphics: 3
Manipulations
There are several functions that affect the current character (where
the red box is). You can rotate, shift, mirror, reverse, erase, replace,
and copy characters. The best way to learn is to play with the func
tions. It’s really a lot of fun. The following keys control each function:
Function Keys
fl: Scroll character right. All pixels move right. The rightmost
column of pixels wraps around to the left.
f2: Scroll character left. Wraparound is like fl.
f3: Scroll character down. All pixels move down. The last row of
pixels wraps around to the top.
f4: Scroll character up. Wraparound is like f3.
R: Rotate. Rotates the character 90 degrees. Press twice to flip the
character upside down.
M: Mirror. Creates a mirror image of the character left to right.
CLR (SHIFT—CLR/HOME): Erases the current character.
CTRL-R or CTRL-9: Reverses the character. All set dots are clear,
and all empty dots are set. The bottom half of
the character set is the reversed image of the
top half.
CTRL-back arrow (-•— ): This causes the lower half of the charac
ter set to be the inverse o f the upper
half. This way, you only have to redraw
the normal characters, then use CTRL-
back arrow to create the inverse set.
F: Fix. Use this if you want to restore the normal pattern for the
character. If you’ve redefined A, and press F while the red cursor
is on the character, the Commodore pattern for A will be copied
back from ROM.
T: Type. This lets you try out your character set. The screen clears,
with a copy of the character set provided for reference. You can
type and move the cursor around, just as in BASIC. This is handy
for envisioning sample screens, and fitting together multiple-
character shapes. If you want to change the background color
while in Type mode, press the f l key. Cycle through the colors
by repeatedly hitting the f 1 key. Press the RUN/STOP key to exit
from Type and return to Ultrafont + .
Saving and Loading Character Sets
To save your creation to tape or disk, press S. Then press either T for
tape or D for disk. When requested, enter the filename, up to 16
113
3: High-Speed Graphics
characters. Don’t use the 0: prefix if you’re using a disk drive (it’s
added for you). The screen will clear, display the appropriate mes
sages, and then return to the editing screen if there are no errors. If
there a r e errors, such as the disk being full, Ultrafont + will read the
disk error message and display it at the bottom of the screen.
Press a key after you’ve read the message and try to correct the
cause of the error before you save again. The computer cannot detect
an error during a tape SAVE.
To load a character set previously saved, press L and answer the
TAPE OR DISK message. Enter the filename. If you’re using tape, be
sure the tape is rewound and ready. After the load, you will be
returned to the editing screen, and a glance is all it takes to see that
the set is loaded. If an error is detected on tape load, you will see
the message ERROR ON SAVE/LOAD. Once again, if you are using
disk, the error message will be displayed. Press a key to return to
editing so you can try again.
Copying and Moving Characters
You can copy one character to another with function keys 7 and 8.
When you press f7, the current character will flash briefly, and it
will be copied into a little buffer. Ultrafont + will remember that char
acter pattern. You can then position the cursor where you want to
copy the character and press f8. The memorized character will then
replace the character the cursor is resting on. You can also use the
buffer as a fail-safe device. Before you begin to edit a character you’ve
already worked on, press f7 to store it safely away. That way, if you
accidentally wipe it out or otherwise garble the character, you can
press f8 to bring back your earlier character.
Creating DATA Statements
A very useful command, CTRL-D, allows you to create DATA state
ments for whatever characters you’ve defined. Ultrafont + doesn’t
make DATA statements for all the characters, just the ones you’ve
changed. After you press CTRL-D, Ultrafont + adds the DATA state
ments to the end of whatever program you have in BASIC memory.
If there is no program, the DATA statements exist alone.
You can LOAD Ultrafont + , enter NEW to reset some BASIC
pointers, LOAD a program you are working on, then SYS 49152 to
Ultrafont + to add DATA to the end of the program. The DATA state
ments always start at line 63000, so you may want to renumber them.
If you press CTRL-D twice, another set of DATA statements will be
appended, also numbered from line numbers 63000 and up. Since the
keys repeat if held down, just tap CTRL-D. If you hold it down,
114
High-Speed Graphics: 3
you may find a hundred DATA statements have been created! See the
notes at the end of this article for more details on using the DATA
statements in your own programs.
Exiting Ultrafont +
After you create the DATA, you’ll still be in Ultrafont + . If you want
to exit to see the DATA statements or go on to other things, press
CTRL-X. The screen will reset to the normal colors and you’ll see
READY. If you’ve made DATA, a LIST will dramatically reveal it. I
recommend you enter the command CLR to make sure BASIC is ini
tialized properly after creating DATA statements. One thing to watch
out for: Don’t use RUN/STOP-RESTORE to exit Ultrafont + . Ultra
font + moves screen memory from the default area at 1024, and the
RUN/STOP-RESTORE combination does not reset the operating sys
tem pointers to screen memory. If you do press it, you will not be
able to see what you are typing. To fix it, blindly type POKE 648,4
or SYS 49152 to reenter Ultrafont + so you can exit properly.
Reentering Ultrafont +
To restart Ultrafont + within the program, press SHIFT-RUN/STOP.
After you’ve exited to BASIC, you can rerun Ultrafont + with SYS
49152. You’ll see the character set you were working on previously,
along with the message USE ROM SET? (Y/N). Usually, Ultrafont +
will copy the ROM character patterns into RAM where you can change
them. If you press N, however, the set you were working on pre
viously is left untouched. Press any other key, like RETURN, to reset
the characters to the ROM standard. You can copy either the upper
case/graphics set from ROM, or the lowercase set.
A Whole New World of Multicolor
We’re not finished yet. There is a whole other mode of operation
within Ultrafont + : the multicolor mode. In multicolor mode, any
character can contain up to four colors (one has to be used for the
background) simultaneously. Multicolor changes the way the com
puter interprets character patterns. Instead of a 1 bit representing a
solid pixel and 0 representing a blank, the eight bits are organized as
four p a ir s of bits. Each pair can represent four possibilities: 00, 01,
10, and 11. Each of these is also a number in decimal from 0 to 3-
Each two-bit pattern represents one o f the four colors. Programming
and using multicolor characters are described in my article “Advanced
Use of Character Graphics,” found in COMPUTER’S First B o o k o f 64
S ou n d a n d Graphics.
115
3: High-Speed Graphics
116
High-Speed Graphics: 3
Returning to Normal
\bu can switch back instantly to the normal character mode by press
ing f6. If you were drawing in multicolor, you can see the bit pat
terns that make up each color. Multicolor characters look just as
strange in normal mode as normal characters look in multicolor.
If you changed colors in the multicolor mode, some of the col
ors in the normal mode may have changed. You can change these
colors as in multicolor mode. Press SHIFT-1 to change the color of the
empty pixels, and SHIFT-4 to change the color of the eight rows of
characters. Use SHIFT-2 to change the color of the on pixels.
Programming
You’ll find the article “Advanced Use of Character Graphics ” in
COMPUTE! ’s First B o o k o f 64 S ound a n d G raphics quite informative.
It shows you how you can make the most of characters. The
article includes several short machine language utilities that you can
use when writing games or other programs using these custom char
acters. It shows how your program can read the SAVEd files directly,
without having to POKE from DATA statements. You should still have
a good grasp of the essentials of programming characters (see Orson
Scott Card’s “Make Your Own Characters,” also in COMPUTERS First
B o o k o f 64 S ou n d a n d G raphics). Ultrafont + is intended as an
artistic aid in your creations, letting the computer take over the
tedious tasks it is best suited for.
Notes: How to Use the DATA Statements
The DATA statements are created from lines 6 3 000 and up. Each line
of data has nine numbers. The first number is the internal code of
the character (the code you use when POKEing to the screen). It rep
resents an offset into the table of character patterns. The eight bytes
that follow are the decimal numbers for the eight bytes it takes to
define any character. A sample program to read and display them
could be:
10 POKE 56,48:CLR
50 READ A:IF A=-l THEN 70
60 FOR 1=0 TO 7:READ B:POKE 12288+A*8+I,B:NEXT:GOT
050
70 PRINT CHR$(147);"{10 DOWN}":REM TEN CURSOR DOWN
S
80 FOR I=0TO7:FORJ=0TO31:POKE1028+J+I*40,I*32+J:PO
KE55300+J+I*40,1:NEXT:NEXT
90 POKE 53272,(PEEK(53272)AND240)OR 12:END
117
3: High-Speed Graphics
You’ll also need to add the following line to the end o f your DATA
statements:
63999 DATA -1
If you want to have your cake and eat it, too — that is, also have the
normal ROM patterns — copy the normal patterns from ROM down
to RAM by adding:
20 POKE 56334,PEEK(56334)AND254:POKE 1,PEEK(1)AND2
51
30 FOR 1=0 TO 2047:POKE 12288+1,PEEK(53248+1):NEXT
40 POKE 1,PEEK(l)OR4:POKE 56334,PEEK(56334)ORl
118
High-Speed Graphics: 3
Ultrafont +
Be sure to read “Using the M achine Language Editor: M L X , ”A p p en d ix D, before typing in
this p ro gra m .
49152 :076,247,196,000,001,003,011
49158 :004,000,001,003,004,000,018
49164 :173,048,002,072,173,045,013
49170 :002,141,048,002,141,079,175
49176 2 002,032,047,193,104,141,031
49182 2048,002,169,100,133,252,222
49188 2169,000,133,251,133,167,121
49194 :169,216,133,168,169,008,137
49200 :141,040,002,169,002,141,031
49206 5042,002,169,005,141,041,198
49212 5002,174,003,192,173,079,171
49218 s002,205,048,002,208,002,021
49224 sl62,002,142,080,002,160,108
49230 5000,177,253,170,173,063,146
49236 5002,240,003,076,233,192,062
49242 sl69,207,145,251,138,010,242
49248 sl70,176,008,173,080,002,193
49254 5145,167,076,112,192,173,199
49260 5004,192,145,167,200,192,240
49266 5008,208,221,024,165,251,223
49272 5105,008,133,251,133,167,149
49278 sl65,252,105,000,133,252,009
49284 s105,116,133,168,024,165,075
49290 s253,105,008,133,253,165,031
49296 s254,105,000,133,254,056,178
49302 s238,079,002,206,041,002,206
49308 sl73,041,002,208,156,056,024
49314 sl73,079,002,233,005,141,027
49320 5079,002,056,165,253,233,188
49326 5039,133,253,165,254,233,227
49332 5000,133,254,206,040,002,047
49338 5173,040,002,240,003,076,208
49344 5056,192,206,042,002,173,095
49350 5042,002,240,030,169,008,177
49356 sl41,040,002,024,173,079,151
49362 5002,105,032,141,079,002,059
49368 5024,165,253,105,248,133,120
49374 s253,165,254,105,000,133,108
49380 s254,076,056,192,096,134,012
49386 5097,169,000,141,043,002,174
49392 5006,097,046,043,002,006,184
49398 5097,046,043,002,174,043,139
49404 5002,169,207,145,251,200,202
49410 sl69,247,145,251,136,189,115
49416 5003,192,145,167,200,145,092
49422 5167,200,192,008,208,215,236
119
3: High-Speed Graphics
120
High-Speed Graphics: 3
49722 208,249,230,254,230,252,201
49728 [Link].252.201.054
49734 [Link].208.133.187
49740 [Link].032.035.199
49746 193,162,004,189,006,192,060
49752 157,002,192,202,208,247,072
49758 096,169,112,133,252,169,001
49764 [Link].000. 133.137
49770 253,133,251,168,162,004,053
49776 [Link].145.253.242
49782 200,208,247,230,254,230,207
49788 252,202,208,240,096,032,130
49794 [Link]. 177.253.192
49800 [Link].042.145.199
49806 253,200,192,008,208,242,221
49812 [Link].047.193.188
49818 160.000. [Link].058
49824 010,040,106,145,253,200,146
49830 192,008,208,242,076,012,136
49836 [Link].160.000. 028
49842 177,253,133,097,200,177,191
49848 [Link].200.200.091
49854 192,008,208,245,165,097,081
49860 [Link].012.192.242
49866 032,047,193,160,007,177,050
49872 253,133,097,136,177,253,233
49878 200,145,253,136,016,247,187
49884 200,165,097,145,253,076,132
49890 012,192,032,047,193,160,094
49896 000,169,000,133,097,162,025
49902 008,177,253,010,102,097,117
49908 202,208,250,165,097,145,031
49914 253,200,192,008,208,233,064
49920 076,012,192,032,047,193,040
49926 [Link]. 153.048.032
49932 002,136,208,250,169,007,016
49938 [Link].169.000. 227
49944 133,007,177,253,074,145,045
49950 253,038,007,202,016,251,029
49956 166,097,165,007,029,049,037
49962 002,157,049,002,198,097,035
49968 165,097,016,224,200,192,174
49974 [Link].185.049.087
49980 [Link].016.248.092
49986 076,012,192,032,047,193,106
49992 160.000. [Link].214
49998 [Link].076.012.055
50004 192,120,169,127,141,013,078
50010 [Link].026.208.087
121
3: High-Speed Graphies
122
High-Speed Graphics: 3
50310 , ,
s146 0 1 9 087 ,069,082,063,148
50316 :095,169, 017 ,160,196,133,142
50322 :251,132/252 ,160,040,169,126
50328 :032,153, 191 ,103,136,208,207
50334 :250,177, 251 ,200,201,095,052
50340 s208,249, 136 ,132,097,152,114
50346 :074, 073, 255 ,056,105,020,241
50352 :168,162, 024 ,024,032,240,058
50358 :255,160, 000 ,177,251,032,033
50364 :210,255, 200 ,196,097,144,010
50370 :246,096, 133 ,251,132,252,024
50376 :160,040, 169 ,032,153,191,177
50382 :103 ,136, 208 ,250,162,024,065
50388 :160,000, 024 ,032,240,255,155
50394 s160,000, 177 ,251,201,095,078
50400 s240,006, 032 ,210,255,200,143
50406 :208,244, 096 ,174,076,002,006
50412 :240,008, 160 ,000,200,208,028
50418 :253,202, 208 ,250,096,173,144
50424 :002 ,221, 009 ,003,141,002,114
50430 :221,173, 000 ,221,041,252,138
50436 :009,002, 141 ,000,221,169,034
50442 , ,
:100 141 136 ,002,169,147,193
50448 :032,210, 255 ,169,000,141,055
50454 5134,002, 169 ,008,032,210,065
50460 :255,160, 000 ,152,153,128,108
50466 :099,200, 016 ,250,168,185,184
50472 s196,195, 153 ,128,099,200,243
50478 5192,023, 208 ,245,160,000,106
50484 5185,219, 195 ,153,192,099,071
50490 5200,192, 032 ,208,245,169,080
50496 5156,141, 044 ,002,169,012,076
50502 5141,032, 208 ,169,128,141,121
50508 s138,002, 032 ,085,195,169,185
50514 5048,141, 076 ,002,169,011,017
50520 5141,057, 002 ,169,007,169,121
50526 5000,141, 048 ,002,141,045,215
50532 5002,141, 063 ,002,173,006,231
50538 5192,009, 008 ,141,058,002,004
50544 s173,004, 192 ,141,034,208,096
50550 s173,005, 192 ,141,035,208,104
50556 5032,012, 192 ,032,094,193,167
50562 5169,203, 205 ,011,192,240,126
50568 5017,141, 011 ,192,162,208,099
50574 5142,082, 002 ,032,032,194,114
50580 5032,012, 192 ,076,170,197,059
50586 5169,098, 160 ,196,032,145,186
50592 sl96,032, 228 ,255,240,251,082
50598 5201,078, 240 ,029,169,117,232
123
3: High-Speed Graphics
124
High-Speed Graphics: 3
125
3: High-Speed Graphics
126
High-Speed Graphics: 3
127
3: High-Speed Graphics
128
High-Speed Graphics: 3
129
3: High-Speed Graphics
52368 s001,200,145,045,200,132,099
52374 :097,164,098,200,192,008,141
52380 :208,214,164,097,169,000,240
52386 s145,045,160,000,177,045,222
52392 :072,200,177,045,133,046,073
52398 s104,133,045,230,057,208,183
52404 s002,230,058,076,255,203,236
130
Charles Brannon
Sprite Magic:
An All-Machine-Language
Sprite Editor
Sprites make animation on What Is a Sprite Editor?
the 6 4 fu n a n d easy to program . Most of what you’ve read
B ut actu a lly d ra w in g a n d about sprites covers how to
creating sprites with graph program them: setting them
p a p er can be tedious. “Sprite up, protecting memory,
Magic” simplifies their creation, moving and animating them,
and using them in games.
a n d lets you concentrate on
But sprite design is usually
the artistic aspects o f sprite
left up to you.
design. You can even A sprite is defined by
animate minimovies! 63 binary numbers. The one
bits (on) represent solid pixels. Zeros (off) represent blank areas,
through which the screen background is visible. Normally, you sketch
a sprite on a grid 24 squares across and 21 squares high. This is three
bytes per row (8 bits *3 bytes = 24 bits)and21 rows of bytes (3*21 = 6 3
bytes). But after you’ve drawn the sprite, you have to convert the
squares into binary, and then into decimal so that you can put the
numbers in DATA statements.
There are utility programs that will do the conversion for you,
even editors that let you clear and set squares with a joystick. Since
you’re using a computer, other functions can be supported to let you
clear, invert, reflect, reverse, shift, and test out your sprite. The more
work the computer does, the less you have to think in terms o f binary
numbers. Having used many sprite editors, I craved a utility that
would make sprites easy to draw and fun to use. Although there are
many good sprite editors available for the 64, none had all the features
I wanted. So I wrote “Sprite Magic.”
Sprite Magic includes the best features of most sprite editors,
including true multicolor mode, and pulls it off with the speed and
power o f an all machine language program. Sprite Magic’s style (and
even some of the coding) came from “Ultrafont + ,” an all machine
language character editor also in this book. As a matter of fact, many
of the commands are the same, which lets you get up to speed quickly.
If you’ve learned how to use Ultrafont + , it won’t take much to
become comfortable with Sprite Magic.
131
3: High-Speed Graphics
Typing It In
Since Sprite Magic is an all machine language program, you cannot
enter it as you do a BASIC program. Machine language is basically a
bunch of numbers; the numbers make no sense in themselves. Only
the 6510 microprocessor in your machine can interpret and execute
these numbers. Since typing in numbers is no fun, we’ve tried to make
it as painless as possible with MLX, the Machine Language Editor.
You’ll find MLX, and the explanation of its use and commands, in
Appendix D of this book. If you haven’t already typed in MLX, do
so before you try to enter Sprite Magic. MLX is used for two other
machine language programs in this book, as well as in COMPUTE!
magazine and COMPUTE! ’s G azette, so save it for future use.
After you’ve typed in MLX, RUN it and answer the prompts of
Starting Address and Ending Address with 49152 and 51821, respec
tively. You’ll then be ready to start typing in Sprite Magic. Enter each
line from the listing at the end of this article. The last number in
each line is a checksum, so type it carefully. If the checksum you’ve
typed matches the checksum computed from the line you typed, a
pleasant bell tone tells you you’ve typed the line correctly. If the num
ber doesn’t match, a buzzer warns you to reenter the line. This way,
you should be able to type in Sprite Magic correctly the first time.
Assuming you’ve typed and saved Sprite Magic, here’s how you
get it up and running. If you used the filename “SPRITE MAGIC”, type:
LOAD “SPRITE MAGIC”,8,1 (for disk)
or
LOAD “SPRITE MAGIC” , 1,1 (for tape)
Be sure to add the (,1) to the end. After the computer comes back with
the READY message, type NEW and press RETURN. This resets some
important memory locations, but leaves Sprite Magic in its protected
cubbyhole at J5C000.
Doodle!
Activate Sprite Magic with SYS 49152. Instantly, the main screen
should appear, with a large 24 x 21 grid. The grid is a blowup of the
sprite you’re editing. The actual sprite will be seen to the right of the
grid. The flashing square within the large grid is your cursor. You
move the cursor with either the cursor keys on the keyboard, or
with a joystick plugged into port 2. To light up a blank spot (in other
words, to turn that pixel on), press either the space bar or the joystick
fire button. If the square is already lit, it will turn dark. This signifies
that the pixel has been turned off. The button or space bar thus tog
gles points on or off. You can draw your sprite quite easily in this
132
High-Speed Graphics: 3
manner. One fine point: With the joystick, you can hold down the
fire button and move the cursor If the first point you change was
set, the fire button will continue to set points as you move the joy
stick, regardless of the other points’ original state. If the first point
you change was empty, you can hold down the fire button and move
about, clearing anything the cursor passes over. Notice how any
changes are immediately visible in the actual sprite.
If you’ve just entered Sprite Magic, the grid is probably full of
garbage pixels. To clear out the grid for a new picture, press SHIFT-
CLR/HOME. You now have an empty area (a fresh canvas, so to speak)
to draw upon. You can press CLR/HOME without holding down
SHIFT to home the cursor to the upper left-hand corner of the grid.
Does the cursor move too slow or too fast? To change the velocity
(speed) of the cursor, press V. Answer the prompt with a number key
from 0 (slow) to 9 (very fast).
133
3: High-Speed Graphics
over, though, so you cannot erase in this mode. This command is not
only quite useful, but also a great deal of fun to play with. To return
to normal editing, press the back arrow key again.
Notice the number in the upper right-hand corner of the screen.
This is the sprite page number, which can range from 0 to 255. You
start out at the top of the sprite memory. The + and - keys are used
to go forward or backward through sprite shapes. Press the minus
key and see how you now have a new shape in the grid. There is a
limit to how far back you can go. If you have no BASIC program in
memory, you can step back to sprite page number 36. However, char
acter information resides in sprite pages below 128. You can still
clear the page and draw a sprite shape on pages below 128, but it
won’t really register. To be safe, use only the sprite pages from 128
on up. If you have a program in memory, Sprite Magic will not let you
step back past its end. This protects your program from being acci
dentally overwritten by a sprite shape. If you want maximum space
available for sprite shapes, be sure to NEW out any BASIC program
before you SYS 49152. You’ll sometimes want to keep a program in
memory, however. We’ll show you why a bit later.
Programming note: The sprite page number, when multiplied by
64, gives you the starting memory location for the 63 numbers
representing the sprite.
Put It in the Buffer
You might use Flip to design two views of a shape, such as a spaceship
pointing in two directions. Draw one freehand, then do the other
with Flip. Mirror can be used to design separate left and right views
as well. But what you first need is a way to copy the original shape
to another sprite area. One way to do this is to copy the sprite shape
to an area of memory (a buffer). You can use + or - to step to
another sprite page, then copy the buffer to the sprite. This, if you
remember, is the way you copy characters with Ultrafont + . The
same keys are used in Sprite Magic. Press f7 to copy the sprite to the
buffer. The grid flashes to affirm this. Then go to the sprite page
where you want to put the copy and press f8 (SHIFT-f7). The shape
in the buffer replaces any shape already in the sprite grid. You can
also use the buffer as a fail-safe device. Before modifying an existing
sprite, press f7 to save it in the buffer. Then, if you mangle the sprite,
or accidentally erase it, you can recall the previous shape from
the buffer.
134
High-Speed Graphics: 3
Computer Disney?
The buffer is also useful for animation. Since you can change sprite
pages so easily, you can also use Sprite Magic as an animation design
tool. Cartoons make only minor changes between frames. Too much
change makes the animation jerky. So put the first frame into the
buffer, copy it to the next area, then make a change. Put the new
image into the buffer, copy it again to a new area, then make another
small change. Continue in this fashion as you build up a whole series
of frames. Put different but similar shapes on adjacent pages, then hold
down plus or minus to step through the shapes. As with cartoon
animation, you will get the illusion of motion. Use a cursor velocity
of 9 for maximum speed. Even if you don’t care to program sprites,
Sprite Magic is a fun tool for making moving cartoons.
A Bit of Color
The normal drawing mode lets you set or clear points, but in only one
color. If you’re willing to give up half as many horizontal points, you
can have four colors to work with. Multicolor mode lets any square
be one of four colors, but gives you only 12 pixels across instead of
24. This is because two dots are grouped together to give four com
binations. The colors come from four memory locations:
Pattern Color location
00 53281 Background color register
01 53285 Sprite multicolor register 0
10 53287- Sprite color registers
53294
11 53286 Sprite multicolor register 1
There are two multicolor sprite registers, which are shared
between all sprites (in programming, but not in Sprite Magic, you can
have eight sprites on the screen at the same time). The bit pattern
marked 10 is unique to each sprite and comes from that sprite’s own
color register. 00 is blank, and whatever is underneath the sprite
shape will show through.
The reason for this sojourn into bits and addresses is that only
the 10 bit pattern has a unique color for that sprite. If you’re design
ing several sprites for a game, remember that anything drawn in that
color can be changed individually for each sprite. Squares drawn
with bit pattern 01 or 11 will be colored from two locations shared
by all sprites.
Many sprite editors let you see how the sprite would look in
multicolor, but you still have to pair up the pixels yourself and keep
135
3: High-Speed Graphics
track of binary bit pairs. No fun! Instead, Sprite Magic offers a multi
color mode. When you press f5, the screen instantly changes. Each
square in the grid is now rectangular, two squares wide. The cursor
has also been enlarged, and can be moved about as before in the new
grid. But the way you set and clear points has been changed, since
you are now working with four colors.
Multicolor Palette
The fire button or the space bar always sets a point, but you have to
tell Sprite Magic which color you are currently drawing in. The
number keys 1 to 4 select the drawing color. The number you press
is one number higher than the binary value of the bit-pairs in the
table above. The 1 key, for instance, chooses the 00 bit-pair, which
represents the background color. In practice, you are choosing from
a palette of four colors. The 1 key is normally used when you want
to erase.
When you press a number key from 1 to 4, the border color
changes to remind you which color you’re drawing with. If you
want to change one of the four colors, hold down SHIFT while you
type the number. The prompt ENTER COLOR KEY appears. Now
you have to enter another key combination. Press CTRL and one of
the number keys from 1 to 8, or hold down the Commodore key
and one of the number keys from 1 to 8. These are the same key com
binations you use to change the text color in BASIC. You can also
change the screen background color by pressing the letter B on the
keyboard until the color you want appears.
Some Sprite Magic commands act strangely in multicolor mode.
For example, a shift left or shift right (done with the fl or f2 key
respectively) moves the sprite over by only one bit, which changes
the color assignments. In general, you must press f 1 or f2 twice to
preserve the same colors. Pressing the M key (for Mirror) reverses the
bit-pairs, so that every 01 becomes a 10. The effect is that colors 2
and 3 are exchanged. The R key (Reverse) also inverts the bits, so that
01 becomes 10, 10 becomes 01, 00 becomes 11, and 11 becomes 00.
Colors 2 and 3 are switched, as well as colors 1 and 4. The Symmetry
command (back arrow) also does not work in multicolor mode.
If you want to go back to normal (nonmulticolor) mode, press
the f6 key (SHIFT-f5). There’s nothing to prevent you from designing
both normal and multicolor sprites on different pages.
If you changed colors in the multicolor mode, some of the colors
in the normal mode may have been changed. You can alter these
colors as in multicolor mode. Press SHIFT-1 to change the color of
136
High-Speed Graphics: 3
the empty pixels, and SHIFT-2 to alter the color of the on pixels.
(You’ll be prompted to press a color key after each SHIFT-1 or
SHIFT-2 combination.)
Action!
If you want to try out your sprite in action, press J (for Joystick). You
can now move the actual sprite around with the joystick. The speed
of movement depends on the current cursor velocity. When you’ve
finished putting your sprite through its paces, press the fire button
to return to Sprite Magic. Also, if you want to test the animation while
you are moving about, hold down the SHIFT key to step forward, or
the Commodore key to step backward. You can lock the SHIFT key
to keep the animation happening while you move around.
Saving Your Sprites
After all your work, you surely want to save your creations on tape
or disk for future use. You can save an individual shape, or all the
sprites. Press S (for Save), then either D (Disk) or T (Tape). Next, enter
the filename. You’ll be asked if you want to “Save all from here?” If
you press N for No, only the current sprite you are working on is
saved. If you press Y for Yes, every sprite from the current to sprite
255 will be saved. Thus, if you want to save a range of sprites, be sure
to use the minus key to step back to the first sprite you want saved.
To recall your sprites, press L. The Load command loads every
thing that was saved. If you’re loading in more than one sprite, be
sure you step backward far enough with the minus key so that all the
sprites will fit between the current sprite and sprite 255- The sprites
load starting at the current sprite page number. After you press L,
enter T or D for Tape or Disk.
Let There Be DATA
If you’re a programmer, you’re probably more interested in DATA
statements. That way, you can use BASIC to READ and POKE the
numbers into memory. If you have some kind of DATAmaker, you can
run it on the memory used by the sprite in Sprite Magic (again, the
memory location is the sprite number times 64). But Sprite Magic
has a special DATAmaker of its own. It’s similar to the Create DATA
option in Ultrafont + , but it’s been enhanced.
Press CTRL-D to create a series of DATA statements from the
current sprite in memory. Just tap the key, or you’ll get hundreds of
DATA statements as the key repeats. Sprite Magic will create eight
DATA statements, with eight bytes per line. The last byte is not strictly
used. Sprite shapes are made from 63 bytes, but the sprite areas are
137
3: High-Speed Graphics
138
High-Speed Graphics: 3
139
3: High-Speed Graphics
Sprite Magic
Be sure to read ‘ Using the M achine L anguage Editor: MLX, ’ A p p en d ix D, before typing in
this p ro gra m .
49152 2076,032/195/000,001,003,051
49158 2004,032,184,192,169,004,079
49164 2133,252,169,000,133,251,182
49170 2133,167,169,216,133,168,236
49176 2169,021,141,040,002,169,054
49182 2 003,141,041,002,160,000,121
49188 2177,253,170,173,048,002,091
49194 2240,003,076,138,192,169,092
49200 2207,145,251,138,010,170,201
49206 2 176,008,173,003,192,145,239
49212 2167,076,069,192,173,004,229
49218 2192,145,167,200,192,008,202
49224 2208,221,024,165,251,105,022
49230 2008,133,251,133,167,165,167
49236 2252,105,000,133,252,105,163
49242 2212,133,168,230,253,208,014
49248 2002,230,254,206,041,002,063
49254 2173,041,002,208,183,024,221
49260 2165,251,105,016,133,251,005
49266 2133,167,165,252,105,000,168
49272 2133,252,105,212,133,168,099
49278 2206,040,002,173,040,002,077
49284 2240,003,076,029,192,096,000
49290 2134,097,169,000,141,042,209
49296 2002,006,097,046,042,002,083
49302 2006,097,046,042,002,174,005
49308 2042,002,169,207,145,251,204
49314 2200,169,247,145,251,136,030
49320 2189,003,192,145,167,200,040
49326 2145,167,200,192,008,208,070
49332 2215,076,074,192,169,000,138
49338 2133,254,173,043,002,133,156
49344 2253,006,253,038,254,006,234
49350 2253,038,254,006,253,038,016
49356 2254,006,253,038,254,006,247
49362 2253,038,254,006,253,038,028
49368 2254,096,032,184,192,160,110
49374 2000,177,253,073,255,145,101
49380 2253,200,192,064,208,245,110
49386 2096,032,184,192,160,062,192
49392 2136,136,177,253,010,008,192
49398 2200,200,162,003,177,253,217
49404 2 040,042,008,145,253,136,108
49410 2202,208,245,040,192,255,120
49416 2208,230,096,032,184,192,182
49422 2160,000,200,200,177,253,236
140
High-Speed Graphics: 3
141
3: High-Speed Graphics
142
High-Speed Graphics: 3
143
3: High-Speed Graphies
144
High-Speed Graphics: 3
145
3: High-Speed Graphics
146
High-Speed Graphies: 3
147
3: High-Speed Graphics
51486 , , ,
s009,048, 141 0 0 0 0 0 1 0 9 6 , 0 6 9
51492 :056,165, 045 ,233,002,133,158
51498 :045,165, 046 ,233,000,133,152
51504 :046,169 ,001 ,133,097,169,151
51510 :008,133, 098 ,169,000,133,083
51516 :057,133 ,058 ,160,000,177,133
51522 :097,200, 017 ,097,240,027,232
51528 s160,002, 177 ,097,133,057,186
51534 :200,177 ,097 ,133,058,160,135
51540 -. , ,
0 0 0 1 1 1 097 ,072,200,177,039
51546 :097,133, 098 ,104,133,097,240
51552 , ,
1 0 1 6 0 6 3 201 ,024,165,057,170
51558 :105,001, 133 ,057,165,058,109
51564 :105,000, 133 ,058,032,184,108
51570 s192,160, 000 ,132,098,160,088
51576 :000,024, 165 ,045,105,037,240
51582 2145,045, 200 ,165,046,105,064
51588 :000,145; 045 ,200,165,057,232
51594 2145,045, 200 ,165,058,145,128
51600 2 045,200, 169 ,131,145,045,111
51606 2200,132, 097 ,164,098,132,205
51612 2098,177, 253 ,170,032,224,086
51618 2200,164, 097 ,173,002,001,031
51624 2145,045, 173 , 001 , 001 , 200,221
51630 2145,045, 173 ,000,001,200,226
51636 2145,045, 200 ,169,044,145,160
51642 2045,200, 132 ,097,164,098,154
51648 2200,152, 041 ,007,208,213,245
51654 2132,098, 164 ,097,136,169,226
51660 2000,145, 045 ,160,000,177,219
51666 2045,072, 200 ,177,045,133,114
51672 2046,104, 133 ,045,230,057,063
51678 2208,002, 230 ,058,164,098,214
51684 2192,064, 208 ,143,160,000,227
51690 2152,145, 045 ,200,145,045,198
51696 2024,165, 045 ,105,002,133,202
51702 2045,165, 046 ,105,000,133,228
51708 2046,076, 094 ,166,032,135,033
51714 2193,173, 045 ,002,010,109,022
51720 2045,002, 168 ,162,000,185,058
51726 2173,202, 157 ,237,202,200,161
51732 2232,224, 003 ,208,244,032,195
51738 2135,193, 173 ,045,002,010,072
51744 2109,045, 002 ,168,162,000,006
51750 2177,253, 029 ,237,202,145,057
51756 2253,200, 232 ,224,003,208,140
51762 2243,096, 173 ,056,002,073,181
51768 2001,141, 056 ,002,096,032,128
51774 2184,192, 160 ,000,162,060,052
148
High-Speed Graphics: 3
51780 5169,003,133,097,177,253,132
51786 :157,173,202,200,232,198,212
51792 :097,165,097,208,243,138,004
51798 5056,233,006,170,016,232,031
51804 s160,062,185,173,202,145,251
51810 5253,136,016,248,096,032,111
51816 s228,255,240,251,096,013,163
149
Chris Metcalf
The Graphics
Package
Creating graphics with machine Working in the Commo
language routines is one o f the dore’s high-resolution mode
most effective ways to use ML. can be rewarding, but it can
Machine language isfast, just also be frustrating. If you’re
the thingfo r displaying complex programming exclusively
in BASIC, plotting points,
patterns on the screen, especially
drawing lines, filling in areas,
when you ’re using the high-
or even doing simple things
resolution mode on the 64. These such as clearing the screen
fo u r short routines, com bined and selecting graphics
or used singly, make graphics modes can be hard work.
creation quick and simple. Worst of all, the graphics
are drawn on the screen with excruciating slowness.
Fortunately, machine language routines can overcome this lack
of speed. Each of the four programs in “The Graphics Package” can
create dazzling displays at machine language speed with a single SYS
command. And because they ’re listed in a BASIC loader form, you
won’t have any trouble adding them to your own programs.
Using these four routines, you’ll be able to plot a point on the
screen, draw a line, fill an area, set graphics or text mode, clear the
screen, or set the high-resolution display color. The point-plotting
routine is the only one that can operate alone; the others are inde
pendent of each other, but each requires that the plot routine be
present in memory. To call one of the routines, you don’t need to
POKE values into memory locations. Instead, you simply call the
routine with a SYS command and follow it with the appropriate
parameters. For example:
SYS(49152),147,83,1
turns on (plots) the pixel at x-position 147 and y-position 83 The
parameters for the other routines are set in the same way. Each rou
tine explains its parameters and values allowed.
In or Out
All of these SYS commands can be used in direct mode (without line
numbers) or called from within your own program, as long as the
routines have been loaded into your 6 4 ’s memory. In a program, treat
them as normal BASIC commands. In other words, use a colon to
150
High-Speed Graphics: 3
151
3: High-Speed Graphics
152
High-Speed Graphics: 3
color numbers. If specified, the routine will plot the point in the given
color. Otherwise, the point will appear in the pixel color already
assigned to that area (see the COLOR command in Program 4,
“Utilities,” for a way of setting the pixel colors on the entire screen).
A few examples of point plotting:
SYS(49152),12,13,1,1 plots a point in white at coordinate 12,13.
SYS(49152),319,199,0 erases the point at 319,199 (no color).
SYS(49152),160,100,2,11 reverses (exclusive OR) the point at
160,100 and plots dark gray (color 11).
SYS(49152),0,0,1 plots a point in the local color at 0,0.
Remember that to use this point-plotting routine, you must be
in the high-resolution mode. If you remain in the normal (text) mode,
these commands will do nothing. The easiest way to enter the high-
resolution mode is to have placed Program 4, Utilities, in memory.
Then you can set the screen for high-resolution graphics with a single
SYS. Read the explanation of the H command in the Utilities
description.
There is one difficulty with the use of color in the pure high-
resolution mode that I’ve been describing. The colors (foreground
or pixels, and background) are defined not for each pixel, but for each
group of 8 x 8 pixels (equivalent to one character in normal text
mode). Therefore, plotting two pixels close together in different
colors cannot be done, unless they are in different 8 x 8 blocks.
Multicolor Plotting. There is a solution to this problem. A
special multicolor mode exists on the Commodore 64 which allows
up to four different colors, including the background, to occupy one
8 x 8 area. Unfortunately, only 160 separate dots can be plotted
horizontally, not 320. This decreases resolution. When in multicolor
mode, therefore, another number has to be included, to specify
which of the three foreground colors you wish to use. This parameter,
which I call the brush, follows the mode number when in multicolor.
(Enter the multicolor mode by typing SYS(50400),m when Program
4, Utilities, is in memory.) The brush is required, as is the mode, and
uses a value from 1 to 3. Following the brush number is the color, if
you wish to specify it. Otherwise, it will plot a pixel with the specified
brush but in the previously defined color in that 8 x 8 square. Here
are a few more examples of multicolor plotting:
SYS(49152),0,0,1,1,3 will plot a pixel in brush 1 in the color cyan
at coordinate 0,0.
SYS(49152),l60,100,2,3,7 reverses the pixel at 160,100 (the
center of the screen) with brush 3 and sets the color to yellow).
SYS(49152),319,0,0 erases the pixel at the top right.
153
3: High-Speed Graphics
(Note that in multicolor mode an erase command can take only three
parameters: x-position, y-position, and the zero indicating erase.
Brush and color cannot apply, so they must be left out.)
The decrease in resolution has an effect on the plotting of multi
color points. The full range of 0-319 is still legitimate, but since
there are in fact only 160 points horizontally, even-numbered x-
positions will generate the same point as the next higher x-position.
For example, SYS(49152), 136,43,1,2 and SYS(49152),137,43,1,2 plot
the same point.
154
High-Speed Graphics: 3
155
3: High-Speed Graphics
156
High-Speed Graphics: 3
Showing Off
To see the complete Graphics Package in action, first load and run all
four routines. Now they’re in the computer’s memory. Type NEW,
then load Program 5. Enter RUN and watch it all work. Pressing any
key moves to the next screen display. And since the demonstration
program is all in BASIC, you won’t find it hard to change the SYS
commands’ parameters and alter the graphics.
Program 1. Point Plotting
For mistake-proof pro gra m entry, be sure to read "The Automatic Proofreader,” A ppendix C.
157
3: High-Speed Graphics
158
High-Speed Graphics: 3
159
3: High-Speed Graphics
160
High-Speed Graphics: 3
Program 4. Utilities
For m istake-proofprogram entry, be sure to read “The Automatic Proofreader,” A ppendix C.
10 FOR X=50400 TO 50621:READ As POKE X,AsCK=CK+1sNE
XT srem 164
20 IF CK< >222 THEN PRINT"ERROR IN DATA rem 202
30 END srem 58
50400 DATA 032,115, 0 0 0 240, 029,160
50406 DATA 008,217, 007, 197, 208,018
, rem 115
rem 146
50412 DATA 185.016, 197, 141, 252.196 rem 151
50418 DATA 185.017, 197, 141, 253.196 rem 159
50424 DATA 032,115, 000 076, 255,255 , rem 134
50430 DATA 136,136, 016, 2 29, 162,011 rem 133
50436 DATA 076,055, 164, 072, 044,077 rem 153
50442 DATA 044,084, 044, 087, 044,067 rem 149
50448 DATA 026.197, 056, 197, 065.197 rem 172
50454 DATA 089.197, 109, 197, 173,000 rem 160
50460 DATA 221,041, 252, 141, 000,221 rem 115
50466 DATA 169,040, 141, 024, 208,173 rem 144
50472 DATA 017.208, 009, 032, 141,017 rem 134
50478 DATA 208.173, 0 2 2 ,208, 041,239 rem 148
50484 DATA 141,022, 208, 096, 032,026 rem 139
50490 DATA 197,009, 016, 141, 022,208 rem 141
50496 DATA 096.173, 000 2 2 1 ,009.003 ,
50502 DATA 141,000, 2 2 1 , 169, 021,141
rem 137
rem 118
50508 DATA 024.208, 173, 017, 208,041 rem 138
50514 DATA 223,141, 017, 208, 076,047 rem 140
50520 DATA 197,169, 000 168, 132.003 , rem 139
161
3: High-Speed Graphics
162
High-Speed Graphics: 3
163
3: High-Speed Graphics
164
Chapter 4
Game
Programming
Gregg Pede
Two-Sprite
Joystick
Machine language joystick One of the greatest advan
routines are fairly common tages of machine language
gam e program m ing aids. You’ve is its speed of execution.
probably seen several. This What once seemed slow
routine, however, is a bit dif can instead seem fast.
Machine language is espe
ferent, fo r it allows you to use
cially handy when you’re
twojoysticks on the Commodore
designing and writing
64. Not only that, but it lets arcade-style games on your
you move sprites smoothly an d Commodore 64. BASIC is
quickly across the screen. just too slow and too cum-
bersome for many of the things you want your game to do. Moving
figures using the joystick is one aspect of game play that suffers
when you have only BASIC to work with. Sprites seem to move even
slower, and when you try to use the right-hand side of the screen,
getting across the invisible “seam” becomes a nightmare of POKEs
and PEEKs. Some programmers use only the left side of the screen
because of this.
“Two-Sprite Joystick” gives you the speed of machine language,
as well as easy use of sprites and joysticks. Once you’ve included it
as part of your own game program, you’ll be able to use both joystick
ports and the entire screen. It moves two sprites, each controlled
by a separate joystick, quickly and smoothly, even across the
dreaded seam.
All the sprite movements are calculated by the routine, including
checking for the seam and keeping the sprites on the screen. You
can use your own sprite data, creating your own sprite design, with
only minor modification. You can even change the color of the
sprites with a single POKE. Writing games, especially two-player
games, becomes much easier when you have this routine in hand.
Smooth and Fast
Type in and save Two-Sprite Joystick. It’s in the form of a BASIC loader
which POKEs the machine language routine into an area of memory
safe from any BASIC program. It’s a good idea to save the program
before you try to run it. Otherwise, if you’ve mistyped any part of
the program, and cause the computer to crash, you’d have to turn it
off, then on again, to regain control. That would erase all of your
167
4: Game Program m ing
typing. Once you have it saved, type LOAD, then RUN. Wait a few
moments for the data to be POKEd into memory, then respond to
the two prompts. Enter a number between 0 and 15 to select the
colors for the two sprites. The blocks then appear on the screen.
Sprite 0 is controlled by the joystick plugged into port 1, while
sprite 1 is controlled by the joystick inserted into port 2. Maneuver
the sprites around the screen, noticing how quickly and smoothly
each moves, even when it crosses the seam. The sprites stay on the
screen at all times. When you’re moving sprite 0, controlled by the
joystick in port 1, you’ll see odd characters such as 2, P, or the *-
(back arrow). This is unavoidable in the direct mode of the demon
stration, but will not occur when you use the routine in your
own program.
To use this routine in your own game, all you have to do is create
two sprites, and POKE the machine language data into memory
with a BASIC loader such as:
10 1=49152
20 READ A:IF A=256 THEN40
30 POKE I ,A :1=1+1:CK=CK+A:GOTO20
40 IFCK< >71433THENPRINT"{CLR}ERROR IN DATA STATEME
N T S ":END
When you run your complete program (which includes this BASIC
loader and the DATA statements), just SYS 49152 to access the two-
sprite joystick routine. You can even do that from within your
program if you want.
If you want to change the sprites’ colors in the middle of the
game, just insert the statement:
POKE 4 9 2 2 8 , x to ch an g e th e c o lo r o f sprite 0 , o r
POKE 4 9 2 3 3 , x to ch an g e th e c o lo r o f sprite 1, w h ere x in b o th statem ents
is th e c o lo r value, from 0 to 15. T h e sprite c o lo r w ill
ch an g e instantly. For instance, PO K E ing 4 9 2 2 8 ,1 4 turns
sprite 0 light blue.
Two-Sprite Joystick
For mistake-proof program entry', be sure to read "The Automatic Proofreader "A ppendix C.
168
Game Program m ing: 4
169
4: Game Program m ing
170
Peter M arcotty
Scroll 6 4
A window can make a static Someone spots a tornado
screen more dynamic. This short and reports it to the local
machine language routine gives weather bureau. Your tele
you control over screen scrolling vision beeps and a warning
fro m within BASIC programs. moves across the bottom
of the screen.
How would you create that effect on your 64? How do you make
words scroll sideways?
Scroll Control and Windows
When you LIST a program, the screen fills quickly. As new lines
appear, the screen scrolls from bottom to top (everything moves
up a notch).
But there may be times when you want movement from top to
bottom, or right to left. Or perhaps you want some information to
stay in one section of the screen while everything else moves.
You need a screen window. Things in the window move, while
everything else stays put. Some new computers, such as the Apple
Macintosh, have built-in windowing.
“Scroll 6 4 ” won’t turn your 64 into a Macintosh, but it can make
your screen displays more dynamic.
Asteroid Belts and Invoices
There are many ways to creatively use screen windows and scrolling.
For example, scrolling is common in certain types o f videogames.
You drive a car on a road that moves toward you. Or your spaceship
at the bottom of the screen has to shoot at descending asteroids. In
addition to the action window, there is usually a section with infor
mation about your current score, remaining fuel, velocity, and so
on. It would be confusing if your score moved with the asteroids, so
the action of the game is put in a window. Your score goes somewhere
outside the window.
Business programs can benefit from windows, as well. You might
want a command line in an invoicing program, to remind the user of
the various options. The window would cover all of the screen
except the last line, which says “FI = Help F3 = New F5 = Help
F7 = Continue.” Everything scrolls on the screen except the line at
the bottom. Another possibility is a product list window in the corner
of the screen. When the user of the invoice program wants to look
up a product number, the window opens up and the list scrolls by.
171
4: Game Programming
172
Game Programming: 4
Smoother Scrolling
Regular scrolls move whole characters. It’s like picking up a letter and
dropping it down one line.
The 64 can do smoother scrolls, moving characters a pixel at
a time. The key is memory locations 53270 (horizontal) and 53265
(vertical). To do smooth scrolls, use these formulas:
POKE 53270, (PEEK(53270)AND248) + X
POKE 53265, (PEEK(53265)AND248) + Y
X and Y can be any numbers from 0 to 7. Once you’ve gone to
7 or 0, you’ll have to do a regular scroll and reset the smooth scroll
to the other limit. Smooth scrolling can make an action game look
more realistic — the characters don’t jump around, they slide.
A minor annoyance in this method is that while the screen is
doing a smooth scroll, you may see small gaps at the edges. You can
get around this by turning off bit 3 of these two registers; in the POKEs
above, AND with 240 instead of 248. In effect, you pull the border
in a notch, resulting in a 38 column by 24 row display (instead
of 40 x 25).
Because smooth scrolling affects the whole screen, it is not
compatible with Scroll 64 windows. If you combined the two, you
would see smooth scrolling inside the window and jittery, vibrating
173
4: Game Programming
174
Game Programming: 4
175
Dan Carmichael and Tom R. Halfhill
6 4 Paddle Reader
An enhancem ent o f a program One of the articles in the
which first appeared in the fu ly premier issue of COM
1983 issue o f COMPUTE! s PUTE f ’s G azette was a
Gazette, this gam e utility reads paddle reader routine for
two paddles and reduces the the Commodore 64. The
idea was to reduce the
“jitters” commonly experienced
“jitter” in screen objects
with these gam e controllers.
controlled by the game
paddles. This jittering is caused by minor fluctuations in the paddle’s
readings. To calm down the jitter, Bobby Williams wrote a short
machine language routine which read the paddle 256 times in a split
second, averaged the readings, and used the average for a final
paddle value.
The routine worked fine, but some readers wanted more. The
original routine was for one paddle only, ignoring the second paddle.
The result of these readers’ requests is this new and improved rou
tine. It is still a machine language program, still POKEd into memory
by a BASIC loader so that you don’t need to know anything about
ML to use it, and it still reduces the paddle jitters. But now it works
with two controllers instead of only one.
A BASIC Loader
As before, you don’t have to know anything about machine language
to use this routine. It’s in the form of a BASIC loader — a short BASIC
subprogram which you add to your own BASIC programs. Using the
POKE statement, it loads decimal numbers into memory which
correspond to the proper machine language commands.
The program is stored in a normally safe area of memory, the 88
bytes from address 679 to 710 (decimal). This is not the same area
where the previous paddle reader routine was stored. The earlier
routine was stored in an often-used block of memory that we’ve
decided to preserve for other purposes.
Be sure to type in the program correctly, and as always when
dealing with machine language, SAVE the program before the first
RUN. This allows you to recover your work in case of a typing error
that crashes the computer.
Reading the Paddles
Once the routine is added to your BASIC program, it must be activated
with a SYS statement each time you want to read the paddles. To
start the routine, use SYS 679.
176
Gam e Program m ing: 4
You then read the paddles with a simple PEEK statement. To get
the averaged reading of paddle 1, use PEEK(251); for paddle 2, use
PEEK(252). Here’s an example:
10 SYS679 sP1=PEEK(251):P2=PEEK(252)
20 PRINTP1;SPRINTP2
30 GOTO10
Note that these locations are different from the usual paddle
locations. That’s because the routine stores the averaged readings
at 251 and 252, not at the customary locations (that is, 54297 on
the Commodore 64).
The short program above displays the values found at the paddle
locations. Plug the paddles into port 1. As you move the paddle
knobs, the values will change. Note that each time you read the
paddles, you must use the SYS 679 command.
64 Paddle Reader
For mistake-proof p rogram entry\ be sure to read "The Automatic Proofreader,”A ppendix C.
177
Charles Bond
Translated to machine language by Gary \i. Marsa and for the 64 by Gregg Peele.
Maze Generator
T his p r o g r a m , a lt h o u g h q u it e
“Maze Generator” is a
sh o rt, c r e a te s a to t a lly r a n d o m
remarkably short algorithm
m a z e d is p la y e v e r y t im e i t ’s ru n .
which produces random
U se it a s th e b a s i s f o r a n y m a z e
mazes on your television set
g a m e y o u d l ik e to d e s ig n .
or m°mtor. There are two
versions of the program
included here. The first, in BASIC, may seem shorter and easier to type
in, but if you compare it to the second program, you’ll see how slow
it is. Program 2 is a machine language program. In the form of a
BASIC loader which POKEs the data into your computer’s memory,
it creates mazes almost instantly. This is a good example of how fast
machine language is, compared to BASIC.
The BASIC version of the program has also been included to
better explain how the maze generator works. It’s difficult, if you
aren’t familiar with machine language, to understand what each
value POKEd into memory does. For those of us who are more com
fortable with BASIC, it’s much easier to see how that version operates.
Refer to Program 1 and the flowchart figure as the program’s
details are explained. You can use either Program 1 or Program 2
to see how the mazes are created. You can even use either version
as part of your own game program. However, remember that the
machine language version executes much faster. If you use the ML
version (Program 2), a player would have to wait only a moment.
Since Program 2 is in the form of a BASIC loader, SAVE it before you
try to run it. If you enter the data incorrectly, the computer could
crash, and you’d have to retype the entire listing unless it had been
previously saved on tape or disk.
The Background Field
The algorithm operates on a background field which must be gener
ated on the screen prior to line number 210 in Program 1. The field
must consist of an odd number of horizontal rows, each containing
an odd number of cells — in other words, a rectangular array. It’s
convenient to think of the field as a two-dimensional array with the
upper-left corner having coordinates X = 0 and Y = 0, where X is
the horizontal direction and Y is vertical. No coordinates are used to
identify absolute locations by the program, but the concept is useful
in configuring the field.
178
Gam e Program m ing: 4
Given that the upper-left cell of the field has coordinates 0,0,
then the terminal coordinates both horizontally and vertically must
be even numbers. (Remember that there is an o d d number of rows
and columns.) In addition, the background field must be surrounded
on all sides by memory cells whose contents are different from the
number used to identify the field. That is, if the field consists of
reverse video spaces, the number corresponding to that character
must not be visually adjacent to the field.
This could happen inadvertently if the screen RAM and system
ROM have contiguous addresses. A sufficient precaution is to avoid
covering the entire screen with field. Leave at least one space at the
beginning or end of each line and, in general, leave the uppermost
and lowermost lines on the screen blank.
179
4: Gam e Program m ing
180
Game Program m ing: 4
181
4: Gam e Program m ing
182
Game Programming: 4
183
4: Game Programming
184
Game Programming: 4
185
C h r is M e tc a lf
Multiple-Key
Scanner
Writing two-player gam es can This routine, located at
be difficult. One o f the problem s address 828, allows a pro
isfigu rin g out how to allow gram to input several keys
input by both players. Although simultaneously from the
there’s a two-joystick routine keyboard. In a two-player
keyboard action game, this
included in this book, you may
can be crucial, since both
not have (or want to use) joy
moves should be evaluated
sticks. H ere’s a short routine simultaneously. This sub
which allows both players to use routine can serve other
the keyboard at the same time. uses, too; it was originally
developed to allow chords to be struck on the keyboard and be
interpreted as chords, rather than single notes.
To use the routine, enter a SYS 828. The result of this SYS is to
place the ASCII values of all the keys that are being pressed at th at
moment in the keyboard buffer, where they can be read with GET
statements. For example, if two keys were being held down, s and /,
and a SYS 828 is executed, 5 and / are placed in the keyboard buffer.
Two GET statements would retrieve them; any further GETs would
return “ the null string. Up to ten characters can be interpreted in
this fashion.
Not Perfect
However, the routine is not perfect. It really can’t be. Commodore did
not design the keyboard to be used in this fashion, so multiple-key
reading does not always return the correct keys. As far as I know, any
two keys are reported accurately; most (but not all) three-key com
binations seem to work. As you get to four and above, unfortunately,
extraneous characters begin to be reported. Another problem is that
BASIC itself is always watching for keys being pressed. Because of this,
two copies of one of the keys are sometimes reported when it is
first pressed.
One feature of this routine is that it will continue to return the
keys pressed, even if the routine is called several times. Thus you
cannot only tell when a key is pressed, but also when it is released.
Each SYS 828 loads the buffer with the keys currently pressed,
regardless how many times the keys have been reported.
186
Game Programming: 4
Special Codes
Another feature of this routine is that it returns not only ASCII values
for all the keys (including function keys, cursor keys, and so forth),
but it also returns special codes to indicate when left-SHIFT, right-
SHIFT, Commodore, or CTRL keys are depressed. CHRjf(l) for the
left-hand SHIFT key and SHIFT LOCK, CHR$(2) for the right-hand
SHIFT, CHR$(3) for the Commodore key, and CHR$(4) for the CTRL
key are returned. This routine also removes the effects o f SHIFT or
Commodore on other keys. For instance, if SHIFT-3 is pressed, SYS
828 returns a CHR$(1) or CHR$(2) along with the code for 3, not
simply the code for #.
Enter and Demonstrate
Program 1, “Keyscan,” is a machine language program. Its data is
POKEd into the cassette buffer (starting at location 828) by the BASIC
loader (lines 10-30). Save the program before you first run it. That
will prevent the loss of your typing time if you’ve entered it incor
rectly and the computer locks up.
You can add this program to your own game, simply by chang
ing the line numbers (to 60000 and up, for example), adding a
RETURN to its end, and then calling the subroutine with a GOSUB.
Once the data has been placed in memory, you access the routine
by SYSing 828. Of course, you’ll also need to place a GET A$ (or
something similar) in your program to retrieve the characters that
the routine puts in the keyboard buffer. (See Program 2 for an
example of this.)
Program 2, “Demo/Scan,” is a BASIC program which shows you
what Keyscan can do. Make sure you’ve loaded and run Keyscan,
then load and run Program 2. It prints out the characters received
from SYS 828. You can get a good idea of the power and limitations
of the Keyscan routine by testing various combinations of keys.
Since Keyscan occupies the cassette buffer (locations 828-1019),
it will be erased when you load from or save to your Datassette. If
that happens, you’ll have to reload and rerun the routine to place the
machine language data back in memory.
Program 1. Keyscan
For mistake-proof program entry, be sure to read “The Automatic Proofreader,”A ppendix C.
10 FORI=828 TO 959:READA:POKEI,AsNEXT :rem 233
20 CK=CK+1 jIFCKol THEN PRINT "ERROR IN DATA"
:rem 84
30 END :rem 58
828 DATA 120,169,000,133,198,170 :rem 44
834 DATA 169,254,141,000,220,172 :rem 36
187
4: Game Programming
Program 2. Demo/Scan
For mistake-proofprogram entry, be sure to react' 'The Autom atic Proofreader," A ppendix C,
10 PRINT"HOLD DOWN KEYS IN VARIOUS COMBINATIONS"
:rem 19
20 PRINT"TO GET A FEEL FOR THE ROUTINE." :rem 47
30 PRINT"NOTE THAT SHIFT AND COMMODORE DO NOT"
srem 21
40 PRINT"AFFECT OTHER KEYS BUT GENERATE CODES OF"
rem 213
50 PRINT"THEIR OWN INSTEAD.{DOWN}" rem 237
100 SYS(828):REM STUFF GET-BUFFER rem 240
110 GETA$:IFA$=""THEN100 :rem 70
120 POKE216,1:PRINTA$; :rem 31
130 GETA$:IFA$<>""THEN120 rem 135
140 PRINT:GOTO100 srem 38
188
Chapter 5
Applications
and Utilities
Glen Colbert
String Search
Data m anagem ent program s Have you ever had to search
often use string arrays to store through a string array to
information. Retrieving that find a certain occurrence of
information can be time- a string? If you have, I’m
consum ing however, if you have sure that you can testify to
the slowness of BASIC. I
to rely on BASIC. This machine
have a data-managing pro
language routine searches an
gram that keeps track of
entire array fo r your designated addresses and bills. The
string, a n d even returns a fla g program used to take for
to mark its place. ever to find a string match.
The solution was to
use a short machine language program that would do the search and
return a flag to the BASIC program. I found Ronald A. Blattel’s “PET
Searcher” in the April 1983 issue of COMPUTE! magazine and began
converting the addresses so that it would run on my computer. But
after running the converted version a few times, I decided that it was
too clumsy. Each time a match was made, control returned to BASIC
and the search routine had to be restarted with the USR function. I
wanted to be able to scan forward and backward through the matched
strings, so I had to build an array and set a flag in a matching element
each time the program returned from the routine.
I was using memory space for an array already and I had to
check against that array in BASIC to see if there was a match. A more
efficient way of doing the search would be to have the ML routine
set the flags in the other array while it was working on the search.
Thus, when the program returned to BASIC, the checking array
would be ready.
Zero Page Swap
This routine utilizes one string variable for matching against the
array, a string array which contains the information to be checked,
and an integer array to keep track of which elements in the string
array match the string variable. It was written without internal JMPs
or JSRs so that it can be relocated anywhere in memory. In general,
the routine is very dependable. However, there are two things that
you must take into consideration when using it: First, it swaps out a
section of the zero page into the cassette buffer, and second, the
pointers to the variables must be where the routine expects them.
Anyone who has done any machine language programming on
191
5: Applications and Utilities
the 64 knows just how limited free space in zero page is. To get
around this, the routine moves a part of the zero page into the cassette
buffer to make room. Once the routine is finished with the zero
page work space, it moves the data from the buffer back where it
belongs. Normally this is not a problem, but if there are other ML
routines or unprocessed data in the cassette buffer, the routine will
write over them.
Picky Variables
The variables must be initialized in the correct sequence to place
them in memory locations where the routine can look for them. The
fir s t and seco n d variables defined in the program must be strings
(for example, AS = ” ” and BS = ” ”). The string variable that you want
to check for m ust be the s eco n d variable. The string array to be
searched m ust b e the first array DIMensioned. The integer array m ust
be the second array DIMensioned. All of this has to be done before
the routine is called. (Look at Program 2, lines 2 0 -4 0 , for an example
of the proper way to initialize these variables.)
192
Applications and Utilities: 5
193
5: Applications and Utilities
same string array, again displaying the time used and the strings
found. Notice the difference in time. The speed of machine language
is clearly demonstrated.
Lines 390 and 450 decide which strings to display. If the integer
variable Q% for a particular string does not equal 0 (in other words,
if it is a 1), the word searched for was included in that string, and it’s
printed on the screen. You can use the same process in your own
programs to see which strings include the item(s) you checked for.
Program 1. String Searcher BASIC Loader
For m istake-proofprogram entry, be sure to read ‘ The Automatic Proofreader, "A ppendix C.
194
Applications and Utilities: 5
Ultrasort
This is probably the fastest “Ultrasort” is a sequel. Sort
sorting program ever published II, I could have called it. It’s
fo r a home computer. It can an improved, faster version
alphabetize 1000 items in less of a program first published
than eight seconds. Included is in the February 1983 issue
of COMPUTE! magazine. In
the BASIC loader to place the
that article, entitled “Super
program in memory, as well
Shell Sort for PET/CBM,” I
as a demonstration that lets you described a shell sort for the
see how “Ultrasort” works. CBM 8032 written entirely
in machine language. It worked as expected and was, overall, quite
fast. But it had a couple of shortcomings. First of all, it had a clumsy
interface with BASIC; that is, the calling sequence was not very effi
cient. Second, the sorting was performed by a shell short algorithm.
This method of sorting, although faster than some other types of
sorts, is not the best available.
C.A.R. Hoare’s Quicksort algorithm is possibly the fastest yet
developed for most applications. So I rewrote my machine language
sort program based on the Quicksort algorithm.
Speed Improvements
How much better is it? In order to test the program, I wrote a small
sort test program (Program 2), similar to the one in my original
article. This program generates a character array containing N items
(line 110).
Different items are generated, depending on the value of the
random number seed, SD in line 140; SD must be a negative number.
I generated six 1000-element arrays and sorted them using both
the shell sort and Ultrasort. Super Shell Sort required an average of
29 60 seconds to sort all 1000 elements, while Ultrasort required an
average of only 8.32 seconds. The sorting time has decreased over
300 percent. I don’t believe you will find a faster sort for an eight-bit
machine anywhere.
The way you start the sort (see Program 2) has also been refined.
To run the sort, you simply type:
SYS 49152,N,AA$(K)
Running the Program
Ultrasort can be used either from within a program or in immediate
mode. Running Ultrasort causes N elements from array AA$, starting
with element K, to be sorted into ascending order. The sort occurs
196
Applications and Utilities: 5
197
5: Applications and Utilities
198
Applications and Utilities: 5
199
5: Applications and Utilities
200
Applications and Utilities: 5
201
D a n C a r m ic h a e l
6 4 Freeze
Freezing a BASIC program , I t ’s h a p p e n e d . Y o u ’re p la y
stopping it in midframe ; zs ¿z in g a fa s t- a c tio n a r c a d e
handy feature ; especially in gam e, an d y o u r h an d
gam e programs. Players get is c r a m p e d fr o m b e in g
exhausted , to answer the w r a p p e d to o tig h tly a ro u n d
th e jo y s t ic k . O r y o u r b a c k
telephone; or make a sandwich,
is g iv in g y o u s p a s m s a g a in .
but don't want to give up that
O r th e p h o n e rin g s a n d y o u
high score. “64 Freeze” lets ju s t h a v e to a n s w e r it. B u t
you stop and restart program s y o u ’v e g o t th e h ig h e s t s c o r e
with single keypresses. ever, a n d i f y o u g e t up, th e
g a m e w ill c o n t in u e . U n fo r tu n a te ly , th e jo y s t ic k c a n ’t r u n its e lf, a n d
y o u ’ll lo s e th e g a m e .
If you’ve placed “64 Freeze” in memory, however, you can stop
the program at any time by pressing one key. Nothing will be lost;
the program simply freezes. Anything on the screen still shows; it just
doesn’t move. Hitting another key unfreezes the program, restarting
it. You can continue with the program from where you left off.
Freeze Keys
Type 64 Freeze in and SAVE it to tape or disk. “The Automatic Proof
reader,” Appendix C, will make it simple to enter the program
correctly the first time.
After loading and running the program, you’ll see a display list.
You can customize 64 Freeze by selecting your own key combination
for freeze and unfreeze. If you want to use the default keys, just hit
RETURN twice. The f 1 key will then freeze the action, and the f3 key
restarts the program. To choose your own keys, enter the appropriate
number before hitting RETURN.
The SYS command to access the routine also shows on the
screen. Whenever you want to use 64 Freeze, just enter SYS679 in
either direct mode or as a program line within your game. If you use
the last method, make sure that 64 Freeze has been loaded into
memory before you try to call it.
Once you’ve selected the two control keys, try the freeze func
tion. Type NEW, then load and run a BASIC] program. Let it run for
a bit, then hit the freeze key (f 1 if you chose the default setting). The
program immediately pauses. Press the unfreeze key (f3 if default
used) to restart the program. That’s it.
202
Applications and Utilities: 5
Interrupting Danger
6 4 F re e z e u s e s a m a c h in e la n g u a g e in te r r u p t b y c a llin g t h e IR Q in te r
ru p t v e c to r s at $ 3 1 5 - 5 3 1 4 . B e c a u s e o f th is , i f y o u r p ro g ra m a ls o u se s
in te r r u p ts , 6 4 F r e e z e m a y n o t w o r k . P r o g r a m s w h ic h u s e m a c h i n e
la n g u a g e in o t h e r w a y s s till s h o u ld b e a b le to a c c e s s 6 4 F re e z e ; i t ’s
o n ly in te rr u p ts th a t in te rfe re . A n y c o m p le te ly B A SIC p ro g ra m c a n c a ll
th is r o u tin e . W e’v e u s e d th is p r o g r a m a t C O M P U T E ! f o r s e v e ra l
m o n th s , fr e e z in g p r o g r a m s s o th a t w e c a n ta k e p h o to g r a p h s o f th e
m o n it o r s c r e e n , a n d w e ’v e h a d d if f ic u lt ie s w it h o n ly a few . A ll o f
th e m u s e d m a c h i n e la n g u a g e in te r r u p ts .
64 Freeze
For m istake-proofpropram entry, be sure to r e a d ' The Automatic Proofreader,' ’A ppendix C.
203
Harold D. Vanderpool
6 4 Merge
DATA statements, subroutines, Up to now, if you wanted
a n d even entire files can be to put two BASIC programs
merged with other program s together, or add DATA state
using this machine language ments created by a sound,
utility. Not only will it save you sprite, or character editor to
your own program, you had
time, since you won’t have to
to retype everything. It
retype the lines merged, but it
was twice the work. Many
allows you to write long p ro programmers use a modular
gram s in pieces, or modules, approach to program design,
and then later link them together. and like to use the same
‘‘64 Merge” isfo r disk merges, subroutine over and over in
although an explanation o f different programs. Why
how to process tape merges retype that joystick routine
is included. when it’s already on a disk?
“64 ”Merge” lets you
avoid all that time at the keyboard. It splices programs together as if
you’d typed them in. New lines are placed in the proper order, and
if a new line number matches an old line number, the latter is replaced.
To use this program, you need a disk drive. There is a way to
merge programs using tape, which is also explained in this article,
but the method is more cumbersome. You can’t use the 64 Merge
program with tape.
Type in and save 64 Merge. Use Appendix C, “The Automatic
Proofreader,” to make sure it ’s entered correctly. As you type in the
DATA statements in lines 2 0 0 -2 4 0 , you’ll notice that there are strange -
looking number and letter combinations. You probably haven’t
typed in a listing like this before. The two-character combinations
do mean something; they are values converted into hexadecimal
notation. You don’t have to know how to convert from decimal to
hexadecimal, or even anything about machine language, to enter
this program. Simply type it in exactly as it appears. As soon as you’ve
done that, you’re ready to begin merging programs.
ASCII Files
In order to put together two programs using 64 Merge, you first have
to make an ASCII file of one of them. It’s probably easier if you
choose the shorter program for this.
Load the program or routine you wish to merge and then type
this line in direct mode (without a line number):
OPEN 2,8,3, “file n a m e , SEQ,W”:CMD2:LIST[nl-n2]
204
Applications and Utilities: 5
Merging
Load the program you want merged to (not the one you created the
ASCII file from). Make sure the disk with the ASCII file is in the disk
drive and then type:
SYS 882
SYS 904
205
5: Applications and Utilities
206
Applications and Utilities: 5
Press RETURN, then the tape PLAY button. The tape will move, even
tually displaying the ASCII filename you earlier specified. Do not
release the PLAY button on the tape player. The next few key entries
must be followed exactly. Press the SHIFT and CLR/HOME keys
together, wait for the screen to clear, and then press the cursor down
key three times. You’ll see the cursor blinking on line four. In direct
mode, enter:
PRINT“ {h o m e }”:POKE198,l:POKE631,13:POKE153,l
After the tape has moved and stopped a few times, you see either the
PSYNTAX ERROR or POUT OF DATA message on the screen. Ignore
it; it’s not a real error. Close the file by typing:
CLOSE2
When RETURN is pressed, the two programs have been merged. You
can save, run, or modify the merged version.
That’s all there is to it. By using this utility, you’ll find it easy to
splice two BASIC programs together. You’ll never have to retype your
favorite subroutines or DATA statements again.
64 Merge
For mistake-proof program entry, be sure to read ‘ The Automatic Proofreader, "A ppendix C.
5 PRINT"{CLR}{DOWN}"TAB(10)"COMMODORE 64 MERGE
{DOWN}" srem 7
10 INPUT"START ADDRESS"?AD srem 137
20 IFAD<820THENAD=828:REM DEFAULTS TO CASSETTE BUF
FER srem 14
30 INPUT"{DOWN}FILENAME";N$ sL=LEN(N $ )sIFN$ = ""THEN3
0 srem 233
40 PRINT"{DOWN}WAIT ONE MINUTE 1" srem 80
50 FORI=0TO89 sREADA$sA=ASC(A$)-48 sIFA>9THENA=A-7
srem 130
60 B=ASC(RIGHT?(A$,1))-48sIFB>9THENB=B-7 srem 49
70 N=A*16+BsPOKEAD+I,NsNEXTsBI=I+AD srem 129
80 F0RI=1T0Ls POKEBI+I-1,ASC(MID$(N $ ,1 ,1))sNEXT sBI =
I+BI srem 28
90 FORI=0TO5 sREADCs POKEBI+I-1,C sNEXT srem 37
100 POKEAD+ 55,L+6sAA=AD+88 sAl%=AA/256 sA2=AA-Al%*25
6 srem 31
110 POKEAD+57,A2 sPOKEAD+59,Al% srem 198
120 PRINT"{DOWN}OPEN"sPRINT"{2 SPACES}SYS"AD+54sPR
INT"{2 SPACES}SYS"AD+76 srem 241
130 PRINT"{DOWNjMERGE"sPRINT"{2 SPACES}PRESS [CLEA
R]"sPRINT"{2 SPACES}SYS"AD srem 44
140 PRINT"{DOWN}CLOSE"sPRINT"{2 SPACES}SYS"AD+54"s
SYS"AD+82 srem 167
207
5: Applications and Utilities
208
R ic h a r d M a n sfie ld
RAMtest
How can you test your Don’t let anyone tell you
com puter’s RAM chips to make that there’s something
sure they’re working? For all you impossibly complex about
know, they may be unreliable. machine language. It ca n be
This short machine language harder to debug (locate and
program tests every RAM fix errors) than BASIC is,
but it’s not inherently more
memory cell, from location 2048
difficult to learn or to write.
to address 40960, a n d tells you You just need to mem
if each is working properly orize some new commands;
obtain and practice with some new tools (assemblers, disassemblers,
monitors); and pick up a few new programming techniques. Dis
covering this for yourself, that machine language can be an easy —
and fascinating — way to communicate with your computer, may
spur you on to write your own ML programs.
“RAMtest,” the program below, is an example of one of the most
common ways that machine language routines are printed in books
and magazines. This kind of program is called a BASIC lo a d er. The
value of loaders is that the user need not understand anything about
the machine language program. It’s the easy way to use machine lan
guage. Just type in the BASIC program as it appears, type RUN, and
the machine language (the numbers in the DATA statements) is POKEd
into memory for you.
Strange Strings
RAMtest is a useful program: It tests your Random Access Memory
(RAM) to be sure that every cell is operating correctly. RAM chips are
generally quite reliable, but you might have one fail on you. There are
various odd things that can happen during a program RUN as the
result of a faulty RAM chip. One sign would be the sudden appearance
of strange strings. For example, you might type A$ = “ABCDEFG”
and when you asked to see A$ (by typing PAS), you would get
ABC)EFG or something.
Here’s how to use RAMtest:
1. Type it in.
2. Type SAVE (to keep a copy on tape or disk).
3. Type RUN. The DATA will be loaded into a safe area of your
computer which is not part of BASIC RAM. We’re loading the machine
language program into decimal address 828-1019. This is the cassette
buffer RAM, and it’s unused by BASIC except during Datassette opera
tions. We can’t store the machine language program in normal
209
5: Applications and Utilities
BASIC RAM because we’re going to fill each memory cell with all 256
possible numbers as our test. That would cause the program to test
— and thereby obliterate — itself. The cassette buffer is a popular, safe
place to put machine language since it is out of BASIC’s way.
4 . After you see READY on the screen, your machine language
is sitting down in lower RAM memory (decimal 8 6 4 -9 9 5 ), waiting
for you to activate it. You send control o f the computer to a machine
language program by using BASIC’s SYS command. However, machine
language programs do not necessarily start with the first number in
their sequence. The entry p o in t could be anywhere within the rou
tine. Unlike BASIC, which always begins with the lowest line num
ber, machine language might store text messages or other information
below the entry point. That’s the case in the RAMtest program. To
start it going, type SYS 884.
A Vibrating Square
If all your DATA numbers were correctly typed in, you should now
see two things happening onscreen. Up in the left corner you’ll
see a vibrating square. This is a visual demonstration of what’s hap
pening to each of your RAM memory cells in turn. As each number
from 0 to 255 is POKEd into each cell of the computer, it’s also
being POKEd into the first screen memory cell so you can see it
happening. (Machine language POKEs are called STA, meaning STore
the Accumulator.)
The other thing you’ll notice is that the decimal address range
currently being tested appears onscreen. This program tests cells
from 2048 up to address 40960. At the conclusion, the words TEST
OVER will signify that every memory cell tested has correctly stored
every possible number.
Now, type LIST. You can see the effect of our mass POKEs. For
aline number you get 65535- (However, for technical reasons, you
can’t actually use line numbers larger than 63999 in BASIC.) Line
numbers are always stored in two-byte units, and this is the biggest
number that the computer can hold within two memory bytes. Fol
lowing that are more than 200 pi symbols. This is the symbol you
get by typing ?CHR$(255). We’re not seeing screen RAM memory
when we ask for a LIST. Instead, we see a translation of a BASIC pro
gram. The series of 255’s appears, after this translation, as pi symbols.
It means that each of these cells — you’re looking at the bottom of
BASIC RAM where BASIC programs start — is now holding a number
255 after having held everything from 0 up to 255 during the test.
210
Applications and Utilities: 5
211
Appendices
Appendix: A
A Beginner’s Guide to
Typing In Programs
What Is a Program?
A computer cannot perform any task by itself. Like a car without gas,
a computer has potential, but without a program, it isn’t going any
where. Most of the programs published in this book are written in a
computer language called BASIC. BASIC is easy to learn and is built
into all Commodore 64s.
BASIC Programs
Computers can be picky. Unlike the English language, which is full
of ambiguities, BASIC usually has only one right way of stating some
thing. Every letter, character, or number is significant. A common
mistake is substituting a letter such as O for the numeral 0, a lower
case 1 for the numeral 1, or an uppercase B for the numeral 8. Also,
you must enter all punctuation such as colons and commas just as
they appear in the book. Spacing can be important. To be safe, type
in the listings exactly as they appear.
Braces and Special Characters
The exception to this typing rule is when you see the braces, such as
{ DOWN}. Anything within a set o f braces is a special character or
characters that cannot easily be listed on a printer. When you come
across such a special statement, refer to Appendix B, “How to Type
In Programs.”
About DATA Statements
Some programs contain a section or sections of DATA statements.
These lines provide information needed by the program. Some DATA
statements contain actual programs (called machine language); others
contain graphics codes. These lines are especially sensitive to errors
If a single number in any one DATA statement is mistyped, your
machine could lock up, or crash. The keyboard and STOP key may
seem dead, and the screen may go blank. Don’t panic — no damage
is done. To regain control, you have to turn off your computer,
then turn it back on. This will erase whatever program was in
memory, so a lw a y s sa v e a copy o f y o u r p r o g ra m b efo re y o u run
it. If your computer crashes, you can load the program and look
for your mistake.
215
A: Appendix
216
Appendix: B
217
B: Appendix
You also go into quote mode when you INSerT spaces into a line.
In any case, the easiest way to get out of quote mode is to just press
RETURN. You’ll then be out of quote mode and you can cursor up
to the mistyped line and fix it.
Use the following table when entering cursor and color
control keys:
{O F F } CTRL 0 ■ § «å COMMODORE 1 0 ”
{B L K } CTRL 1 ■ Í R 1 fl
1 B
{W H T } CTRL 2
m
ö
{ F: ¡ SHIFT fl ! B
{R E D } CTRL 3 { FS 1 f3
B
{C Y N } CTRL 4 { R } [SHIFT f3 IB
i l l
{P U R } CTRL 5 ( F5 } f5 II
□
{G R N } CTRL 6 { FS } SHIFT f5 SI
B ■ 1
{B L U } CTRL 7 { F? } f7
: ■
{Y E L } CTRL 8 S3 { F8 1 SHIFT f7
218
Appendix: C
Charles Brannon
219
C: Appendix
220
Appendix: C
221
C: Appendix
222
Appendix: D
Charles Brannon
223
D: Appendix
The starting SYS is always listed in the article which presents the
machine language program in MLX format.
Using MLX
Type in and save MLX (you’ll want to use it in the future). When you’re
ready to type in the machine language program, run MLX. MLX asks
you for two numbers: the starting address and the ending address.
These numbers are given in the article accompanying the ML pro
gram you’re typing. For example, the addresses for “BASIC Aid”
should be 49152 and 52997 respectively.
You’ll see a prompt. The prompt is the current line you are enter
ing from the MLX-format listing. It increases by six each time you
enter a line. That’s because each line has seven numbers — six actual
data numbers plus a checksum number. The checksum verifies that
you typed the previous six numbers correctly. If you enter any of the
six numbers wrong, or enter the checksum wrong, the 64 sounds a
buzzer and prompts you to reenter the line. If you enter the line
correctly, a bell tone sounds and you continue to the next line.
A Special Editor
You are not using the normal 64 BASIC editor with MLX. For example,
it will accept only numbers as input. If you make a typing error,
press the INST/DEL key; the entire number is deleted. You can press
it as many times as necessary, back to the start of the line. If you
enter three-digit numbers as listed, the computer automatically prints
the comma and goes on to accept the next number. If you enter less
than three digits, you can press either the space bar or the RETURN
key to advance to the next number. The checksum automatically
appears in reverse video for emphasis.
To make it even easier to enter these numbers, MLX redefines
part of the keyboard as a numeric keypad (lines 581-584).
U I o 7 8 9
H J K L becomes 0 4 5 6
M , . 1 2 3
When testing it, I’ve found MLX to be an extremely easy way to
enter long listings. With the audio cues provided, you don’t even
have to look at the screen if you’re a touch-typist.
Done at Last!
When you get through typing, assuming you type your machine
language program all in one session, you can then save the completed
224
Appendix: D
225
D: Appendix
Y ou c a n u se th e S av e a n d L o ad c o m m a n d s to m a k e c o p ie s o f th e
c o m p le t e m a c h in e la n g u a g e p r o g r a m . U se th e L o ad c o m m a n d to
r e lo a d th e ta p e o r d is k , th e n in s e r t a n e w ta p e o r d is k a n d u se th e S a v e
c o m m a n d to c r e a te a n e w c o p y . W h e n re s a v in g o n d is k it is b e s t to
u s e a d iffe r e n t file n a m e e a c h tim e y o u save. F o r e x a m p le , I lik e to
n u m b e r m y w o r k a n d u s e file n a m e s s u c h as A ID 1 , A I D 2 , A I D 3 ,
and so o n .
O n e q u ir k a b o u t ta p e s m a d e w it h th e M L X S a v e c o m m a n d :
W h e n y o u lo a d th e m , th e m e s s a g e “ F O U N D p r o g r a m ” m a y a p p e a r
tw ic e . T h e ta p e w ill lo a d ju s t fin e , h o w e v e r.
I th in k y o u ’ll fin d M L X to b e a tr u e la b o r-s a v in g p ro g ra m . S in c e
it h a s b e e n te s te d b y e n te r in g a c tu a l p ro g ra m s , y o u c a n c o u n t o n it as
a n a id f o r g e n e r a tin g b u g -fr e e m a c h in e la n g u a g e . B e s u re to sa v e
M L X ; it w ill b e u s e d f o r fu tu r e a p p lic a tio n s in C O M P U T E ! B o o k s ,
C O M P U T E / m a g a z in e , a n d C O M P U T E / S G a z e t t e .
MLX
For niislake-/>roof/>ro¡>ra>n entry be sure to read "Ibe Automatic Proofreader, " Appendix C.
226
Appendix: D
227
D: Appendix
228
Appendix: D
229
E: Appendix
Status Flags N Z C I D V
• • • •
Status Flags N Z C I D V
• •
230
Appendix: E
Accumulator ASL A OA 1
Zero Page ASL Arg 06 2
Zero Page, X ASL Arg, X 16 2
Absolute ASL Arg OF 3
Absolute, X ASL Arg, X IF 3
Status Flags N Z C I D• V
Status Flags N Z C I D V
Status Flags N Z C I D• V
231
F: Appendix
S tatus Flags N Z C I D» V
• • •
BMI B ra n c h o n Minus
S tatus Flags N Z C I D» V
Status Flags N Z C I D» V
BPL B ra n ch o n Plus
Status Flags N Z C I D V
232
Appendix: E
BR K B reak
S tatus Flags N Z C I D V
•
Implied BRK 00 1
Status Flags N Z C I D V
Status Flags N Z C I D• V
•
Implied CLC 18 1
233
E: Appendix
Status Flags N Z C I D V
•
Implied CLD D8 1
Status Flags N Z C I D• V
•
Implied CLI 58 1
Status Flags N Z C I D V
•
Implied CLV B8 1
234
Appendix: E
Status Flags N Z C I D V
• • •
DEC D e cre m e n t M em o ry by O ne
D EX D e cre m e n t X R eg ister by O ne
Status Flags N Z C I D• V
• •
Implied DEX CA 1
235
E: Appendix
D EY D e cre m e n t Y R eg ister by O ne
Status Flags N Z C I D V
• •
INC In cre m e n t M em o ry by O ne
Status Flags N Z C I D V
• •
236
Appendix: E
Implied INX E8 1
Implied INY C8 1
JM P Ju m p
Status Flags N Z C I D• V
Status Flags N Z C I D V
237
E: Appendix
Status Flags D
238
Appendix: E
Status Flags N Z C I E) V
• • •
Accumulator LSR A 4A 1
Zero Page LSR Arg 46 2
Zero Page, X LSR Arg, X 56 2
Absolute LSR Arg 4E 3
Absolute, X LSR Arg, X 5E 3
NOP No O p eratio n
Status Flags N Z C I D» V
Implied NOP EA *
Status Flags N Z C I D V
• •
239
E: Appendix
Status Flags N Z C I D V
Implied PHA 48 1
Status Flags N Z C I D» V
Implied PHP 08 1
Implied PLA 68 1
Implied PLP 28 1
240
Appendix: E
Accumulator ROL A 2A 1
Zero Page ROL Arg 26 2
Zero Page, X ROL Arg, X 36 2
Absolute ROL Arg 2E 3
Absolute, X ROL Arg, X 3E 3
Status Flags D
Accumulator ROR A 6A 1
Zero Page ROR Arg 66 2
Zero Page, X ROR Arg, X 76 2
Absolute ROR Arg 6E 3
Absolute, X ROR Arg, X 7E 3
Status Flags N Z C I D V
From Stack
Implied RTI 40 1
241
K: Appendix
Status Flags N Z C I D» V
Implied RTS 60 1
Status Flags N Z C I D* V
• • • •
Implied SEC 38 1
242
Appendix: E
Status Flags « z C I D V
•
Implied SED F8 1
Implied SEI 78 1
243
E: Appendix
Status Flags N Z C I D• V
Implied TAX AA 1
Implied TAY A8 1
244
Appendix: E
Status Flags N Z C 1 D V
• •
Implied TSX BA 1
Implied TXA 8A 1
Status Flags N Z C I D V
Status Flags N Z C I D V
• •
Implied TYA 98 1
245
F : Appendix
Number Tables
This lookup table should make it convenient when you need to
translate hex, binary, or decimal numbers. The first column lists the
decimal numbers between 1 and 255. The second column is the
hexadecimal equivalent. The third column is the decimal equivalent
of a hex m ost sig n ifica n t byte, or MSB. The fourth column is
the binary.
If you need to find out the decimal equivalent of the hex number
$FD15, look up $FD in the Hex column and you’ll see that it’s 64768.
Then look up the $15 in the Hex column (it’s 21 decimal) and add
21 + 64768 to get the answer: 64789.
Going the other way, from decimal to hex, you could translate
64780 into hex by looking in the MSB column for the closest number
(it must be smaller, however). In this case, the closest smaller number
is 64768 so jot down $FD as the hex MSB. Then subtract 64768 from
64780 to get the LSB: 12. Look up 12 in the decimal column (it is
$0C hex) and put the $FD MSB together with the $0C LSB for your
answer: $FD0C.
With a little practice, you can use this chart for fairly quick
conversions between the number systems. Most of your translations
will only involve going from hex to decimal or vice versa with the
LSB of hex numbers, the first 255 numbers, which require no addition
or subtraction. Just look them up in the table.
H ex LSB MSB B in a ry
01 1 256 00000001
02 2 512 00000010
03 3 768 00000011
04 4 1024 00000100
05 5 1280 00000101
06 6 1536 00000110
07 7 1792 00000111
08 8 2048 00001000
09 9 2304 00001001
0A 10 2560 00001010
246
Appendix: F
OB 11 2816 00001011
OC 12 3072 00001100
OD 13 3328 00001101
OE 14 3584 00001110
OF 15 3840 00001111
10 16 4096 00010000
11 17 4352 00010001
12 18 4608 00010010
13 19 4864 00010011
14 20 5120 00010100
15 21 5376 00010101
16 22 5632 00010110
17 23 5888 00010111
18 24 6144 00011000
19 25 6400 00011001
1A 26 6656 00011010
IB 27 6912 00011011
1C 28 7168 00011100
ID 29 7424 00011101
IE 30 7680 00011110
IF 31 7936 00011111
20 32 8192 00100000
2 1 33 8448 00100001
22 34 8704 00100010
23 35 8960 00100011
24 36 9216 00100100
25 37 9472 00100101
26 38 9728 00100110
27 39 9984 00100111
28 40 10240 00101000
29 41 10496 00101001
2A 42 10752 00101010
2B 43 110 0 8 00101011
2C 44 11264 00101100
2D 45 1 1 520 00101101
2E 46 117 7 6 00101110
2F 47 12032 00101111
30 48 12288 00110000
31 49 12544 00110001
32 50 128 0 0 00110010
33 51 13056 00110011
34 52 13312 00110100
35 53 13568 00110101
36 54 13824 00110110
37 55 14080 00110111
38 56 14336 00111000
39 57 14592 00111001
3A 58 148 4 8 00111010
3B 59 15104 00111011
3C 60 15 3 6 0 00111100
3D 61 15616 00111101
3E 62 15872 00111110
247
F: Appendix
3F 63 161 2 8 00111111
40 64 16384 01000000
4 1 65 1 6 640 01000001
42 66 16896 01000010
43 67 17152 01000011
44 68 17408 01000100
45 69 17664 01000101
46 70 1 7 920 01000110
47 71 18176 01000111
48 72 18432 01001000
49 73 18688 01001001
4A 74 18944 01001010
4B 75 19200 01001011
4C 76 19456 01001100
4D 77 19712 01001101
4E 78 19968 01001110
4F 79 20224 01001111
50 80 20 480 01010000
5 1 81 20736 01010001
52 82 20992 01010010
53 83 21248 01010011
54 84 21504 01010100
55 85 21760 01010101
56 86 22016 01010110
57 87 22272 01010111
58 88 22 528 01011000
59 89 22784 01 011001
5A 90 23040 01011010
5B 91 23296 01011011
5C 92 23552 01011100
5D 93 23 808 01011101
5E 94 24064 01011110
5F 95 24320 01011111
60 96 24576 01100000
61 97 24832 01100001
62 98 25088 01100010
63 99 25344 01100011
64 100 25600 01100100
65 101 25856 01100101
66 102 26112 01100110
67 103 26368 01100111
68 104 26624 01101000
69 105 26880 01101001
6A 106 27136 01101010
6B 107 27392 01101011
6C 108 27648 01101100
6D 109 27904 01101101
6E 110 28160 01101110
6F 111 28416 01101111
70 112 28672 01110000
7 1 113 28928 Oil 10001
72 114 29184 Oil 10 0 1 0
248
Appendix: F
249
F: A ppendix
250
Appendix: F
The following program will print copies of this number table. You might need to
make some adjustments to the printout conventions and your printer itself.
Table Printer
For mistake-proof p ro gra m entry, be sure to read "The Automatic Proofreader," A ppendix C.
251
F : Appendix
252
Index
accumulator 23 CLD (CLear Decimal mode) instruction
ADC (ADd memory to accumulator with 234
Carry) instruction 230 CLI (CLear Interrupt disable bit) instruc
address 8 tion 234
addressing modes 8 CLV (CLear oVerflow flag) instruction
"A Disassembler" program 14-19 234
AND (AND memory with accumulator) CMP (CoMPare memory and accu
instruction 230 mulator) instruction 234
animation 135 Cochrane, F. Arthur 45
arcade games 4 COLD command (in "BASIC Aid" pro
"Area-Fill Routine" (Graphics Package) gram) 48
154-55, 160-61 Commodore 64 Programmer's Reference
arrays, how stored 102 Guide 72, 92
ASCII code 91, 186 COMPUTERS First Book of 64 Sound and
ASCII files 204-5 Graphics 115,151
"ASCII/PO KE Printer" program 91-95 COMPUTERS Reference Guide to Com
ASL (Shift Left one bit) instruction 231 modore 64 Graphics 115, 151
assembler 5, 6-9, 14, 98 COMPUTERS Second Book of Machine
"Assembler, The" program 6-13 Language 5
AUTO command (BASIC Aid) 46 CPX (ComPare memory against
"Auto Line Numbering"‘program 69-70 x register) instruction 235
"Automatic Proofreader" 219-22 CPY (ComPare memory against
BASIC v, 3, 14 y register) instruction 235
"BASIC Aid" program v, 45 -6 7 CRT command (in "BASIC Aid" pro
"BASIC Maze Generator" program 181 gram) 48
BCC (Branch on Carry Clear) instruction cursor control 78-79
231 "DATAmaker" program 24-25
BCS (Branch on Carry Set) instruction DATA statement 117, 137-38, 215
231 DEC (DECrement memory by one)
BEQ (Branch if EQual) instruction 15, 231 instruction 235
BIT (test BITs in memory against accu DELETE command (in "BASIC Aid" pro
mulator) instruction 232 gram) 48-49
bitmapped graphics 26-30 "D em o/scan" program 188
BMI (Branch on Minus) instruction 232 DEX (DEcrement x register by one)
BNE (Branch if Not Equal to zero) instruction 235
instruction 24, 232 DEY (DEcrement y register by one)
BPL (Branch on PLus) instruction 232 instruction 236
BREAK command (in "BASIC Aid" "Disassembler" program 23
program) 47 disassembling 14—19
BRK (BReaK) instruction 6, 233 "Disk Defaulter" program 106-7
BVC (Branch on oVerflow Clear) instruc DOS support commands (in "BASIC
tion 233 Aid" program) 52-53
BVS (Branch on oVerflow Set) instruction "Dr. Video" program 78-79
233 DUMP command (in "BASIC Aid"
byte 6 program) 49
cassette buffer 76, 89, 91-92, 191, 205, EOR (Exclusive-OR memory with accu
209 mulator) instruction 236
CHANGE command (in "BASIC Aid" FIND command (in "BASIC Aid"
program) 47-48 program) 49
character sets, editing 111-18 FLIST command (in "BASIC Aid"
CHRGET ROM routine 81 program) 49
CHROUT ROM routine 93 "Foolproof Input" program 83-86
CLC (CLear Carry flag) instruction 233 "Four-Speed Brake" program 89-90
253
function keys 74-75, 89-90 ORA (OR memory with Accumulator)
GET statement 84 instruction 239
GETIN ROM routine 93 “Package Demonstration“ program
HELP command (in “BASIC Aid" pro 162-64
gram) 49-50 pages, memory 22
HEX command (in “BASIC Aid“ pro PHA (PusH Accumulator onto stack)
gram) 50 instruction 240
Hoare, C.A.R. 196 PHP (PusH Processor status onto stack)
immediate addressing 8 instruction 240
implied addressing 8 PLA (PuLl Accumulator from stack)
INC (INCrement memory by one) instruction 240
instruction 236 “Plotstring“ programs 26-41
INPUT statement 83-85 PLP (PuLl Processor status from stack)
interrupts 78 instruction 240
inverse video 92 “Point-Plot Routine“ 152-54, 157-58
INX (INcrement X) instruction 7, 24, 237 pointers, BASIC 103-4
INY (INcrement Y) instruction 237 PRINT command 91
IRQ (Interrupt ReQuest) 78-79, 98-99 “Quick Clear“ routines 3 -4
JMP (JuMP) instruction 6, 191, 237 Quicksort algorithm 196
joystick 112, 132-33, 167-68 quote mode 97-100
JSR (JumP to SubRoutine) instruction 6, “RAMtest“ program 209-11
191, 237 READ command (in “BASIC Aid“
Kemal ROM 29, 71 program) 51
“Keyscan" program 187-88 RENUMBER command (in “BASIC Aid“
KILL command (in “BASIC Aid“ pro program) 51
gram) 50 REPEAT command (in “BASIC Aid“
LDA (LoaD the Accumulator) instruction program) 51
14, 23, 238 ROL (ROtate one bit Left in memory or
LDX (LoaD X) instruction 23, 238 the accumulator) instruction 241
LDY (LoaD Y) instruction 7, 238 ROM character addresses 42
“Line-Draw Routine“ 154, 158-60 ROR (ROtate one bit Right in memory or
LIST command 14-15 the accumulator) instruction 241
listing conventions 217-18 RTI (ReTurn from Interrupt) instruction
LSR instruction 239 241
“Machine Language Maze Generator“ RTS (ReTurn from Subroutine) instruc
program 181-84 tion 6, 242
maze generator programs 178-85 SBC (SuBtract memory from accumulator
algorithm 178-80 with borrow) 242
flow chart 185 SCROLL command (in “BASIC Aid“
memory locations, safe 91 program) 51-52
MERGE command (in “BASIC Aid“ “Scroll 64“ program 171-75
program) 50 scrolling 171-74
mixing BASIC and ML 4-5 SEC (SEt Carry flag) instruction 242
“MLX“ program 45-46, 111, 132, 223-29 SED (SEt Decimal mode) instruction 243
mnemonics 5, 6 -8 SEI (SEt Interrupt disable status) instruc
“Monitor Disassembly“ program 22-23 tion 243
multicolor mode 115-16, 153-56 shell sort 196
NOP (No OPeration) instruction 239 6502 instruction set 230-45
number tables 246-51 6510 chip 3, 132
“Numeric Keypad“ program 71-73 “64 Escape Key“ program 97-101
colors and 72 “64 Freeze“ program 202-3
OFF command (in “BASIC Aid“ pro “64 Loader“ program 21-22
gram) 50-51 “64 Merge“ program 204-8
OLD command (in “BASIC Aid“ pro “64 Paddle Reader“ program 176-77
gram) 50 “64 Searcher“ program 87-88
“One-Touch Commands“ program 74-77 “Sort Test“ program 201
254
"Sprite Magic" sprite editor 131-49 tokens, BASIC 14, 80-81
sprite page number 134 TSX (Transfer Stack pointer to x register)
sprite seam 167 instruction 245
sprites 131-39 "Two-Sprite Joystick" program 167-70
multicolor 135-37 TXA (Transfer x register to Accumulator)
BASIC and 167 instruction 245
STA (STore the Accumulator) instruction TXS (Transfer x register to Stack pointer)
24, 210, 243 instruction 245
START command (in "BASIC Aid" TYA (Transfer y register to Accumulator)
program) 52 instruction 245
"Step Lister" program 80-82 "Ultrafont + Character Editor" program
Strasma, James 45 111-30, 131
"String Search" program 191-95 "Ultrasort" program 196-201
STX (STore x register in memory) instruc "Variable Lister" program 102-5
tion 244 variables, where stored 102
STY (STore Y) instruction 7, 244 wedges 80-81
"Table Printer" program 251-52 windowing 171-72
TAX (TrAnsfer accumulator to x register) "Window, The" program 22
instruction 244 word processors 4
TAY (Transfer Accumulator to y register) x register 23
instruction 244 y register 8
"Timed Search" program 195 zero page 191-92
255
CO M PUTE! B o o k s
P.O. Box 5406 Greensboro, NC 27403
COMPUTE!
PO Box 5406
Greensboro. NC 27403
My C om puter Is:
□ C om m odore 64 □ TI-99/4A □ Timex/Sinclair □ VIC-20 □ PET
□ Radio Shack Color Com puter □ Apple □ Atari □ O th e r____
□ Don't yet have one...
CO M PUTERS C A I I V f l
P.o. Box 5406
Greensboro, NC 27403
M y c o m p u te r is:
□ C o m m o d o re 64 □ V IC -2 0 □ O th e r _
01 02 03
Name
Address
C i t y S t a t e Z i j o
Country __________________
1284
ISBN 0-942386-48-5 NR04998
from the archives of
[Link]
This was brought to you
[Link]