User`s guide

Page 27
www.commodorefree.com
Once it's assembled, the program assumes that the VIC-II is
looking at the default 16K bank as explained last week. For
completeness, however, you may add the code to force the
graphics chip to see bank zero, but try changing the banks
anyway to see what happens. Run the routine by SYS 4096,
and use the keyboard to move the simple sprite around the
screed, W is up, S is down, D moves right and A moves left and
RETURN exits the example program. Fortunately, I'll be
covering reading the joystick port next week, and as space is at
a premium, I'll leave it there. As mentioned, send over your
question that you have and I'll answer them as best I can.
The joys of movement - Part 12
We've reached the final instalment of these tutorials, so I hope
that you've enjoyed your first steps into the mystical world of
machine code programming. But it needn't end here; there is
the accompanying thread over on the Micro Mart forums at
tinyurl.com/C64-Coding, and even though this beginners guide
ends here, I'll still endeavour to answer any questions that you
have. Remember that the more you experiment, the more you
will learn!
You may have noticed that I haven't covered the Commodore
64's diverse sound capabilities. This is simply because covering
such a subject would take at least 12 instalments by itself. But
as you are now getting to grips with assembly language, take a
look at tinyurl.com/6ehvk54.
Last week’s example demonstrated moving a sprite around the
screen using the keyboard. Now we all know that the C64 has
two joystick ports, so it makes sense to use them.
The two joystick ports are read into memory locations dc00
and dc01 hexadecimal, which represents ports two and one
respectively. You'll note that most games use the second port,
but if you want to use the other then there's really nothing
stopping you, as the difference is only in the memory location
that you're reading and not in how it works.
The C64, like most 8-bits, used 8-way, digital controllers with a
single fire button, a standard set by the Atari with its 2600
console. This makes sense as most classic games don't require
more than 8 directional controls, and some don't use more
than two. Each direction is read as one or two bits in memory
and fire as required. Reading a binary octet from right to left,
the zero-bit is up, bit one is down, bit two is left and bit three
is right. The forth bit is set when fire is depressed. To test
which direction is set, we can use an 'And' gate, for instance:
READJOY
lda $dc00 ; We're testing port two here
and #%00000001 ; Check for up
beq MOVEUP
lda $dc00
and #%00000010 ; Check for down
beq MOVEDOWN
lda $dc00
and #%00000100 ; Check for left
beq MOVELEFT
lda $dc00
and #%00001000 ; Check for right
beq MOVERIGHT
lda $dc00
and #%00010000 ; Check for fire
beq FIRE
This technique is fine, but there's a more efficient way of doing
things. What we can do is to 'roll' the accumulator so that each
bit shifts to the right, so any bits that essentially 'fall off the
end' will be put onto the processor stack and the carry flag will
be set. So, if you logically shift the following bit pattern to the
right: %00000010, the new value will be %00000001. Do it
again, and you will get %00000000 + carry flag set. Here's an
example:
READJOY
clc ; It's good practice to clear the carry flag testing it
lda $dc01 ; Okay, we'll try port one this time
lsr a ; Logical shift RIGHT
bcs MOVEUP ; Branch on Carry Set
lsr a
bcs MOVEDOWN
lsr a
bcs MOVELEFT
lsr a
bcs MOVERIGHT
lsr a
bcs FIRE
This is more efficient that the first example, plus has other
benefits too, which is discussed on the forum thread
mentioned above. The important thing to remember is that it's
up to you as to how you write your code, as the first thing to
worry about is to get it working.
See if you can use either example this week with the code last
week and replace the keyboard controls with either joystick
port. You may need to slow things down a little as machine
code is many times quicker than BASIC. There was a way to do
this much earlier in the
series, but you know what
to do if you get stuck.
Whatever happens, keep
on coding as it can be the
most rewarding thing that
you can do with your old
Commodore. Bye for now!