By Bobby Williams — originally published in COMPUTE!’s Gazette Volume 1, Issue 1
Beginning this month, “Power BASIC” will be a series of short utilities and routines which use machine language to enhance the power and usefulness of your computer’s BASIC. You do not need to understand machine language to use these routines — each will be presented in the form of a short BASIC program ready to type in and use.
If you’ve ever tried to use the Commodore 64 paddle controllers in a BASIC program, you may have noticed how the value you PEEK to read the paddles “jitters” or jumps around even when the paddle isn’t touched. This is due to the analog-to-digital conversion going on, plus the paddle’s sensitivity.
The VIC doesn’t seem to suffer as much from this “jitter.” So what can we do to get the 64’s paddles to “calm down”?
One way is to read the paddle several times and average the results. This could be done with a short loop in BASIC. But even a very short loop in BASIC is relatively slow, especially if you’re trying to read the paddle very fast, as in a game. The answer is to use machine language.
256 Readings Per Split Second
Don’t worry — you won’t need to understand machine language to use the following program. It’s been converted into a BASIC loader, a short BASIC subroutine which loads the machine language into memory as part of your regular BASIC program. The routine reads the paddle 256 times in a split second and takes the average as the final value.
All you need to do is add these lines to the beginning of any new or existing BASIC program that uses the paddles:
When you want to read the paddle, simply type:
SYS PR:PA = PEEK(251)
We are PEEKing memory location 251 instead of the actual paddle location (54297) because that is where the subroutine stores the average that you want.
X And Y Coordinate Readings
If you want the paddle reading to be an X or Y coordinate of a sprite, simply use these lines instead of those above:
Fill in “LO” with the low byte and “HI” with the high byte of the sprite’s X or Y address. This will automatically update your sprite position when you type SYS PR (either as a line in your program or directly from the keyboard).
Both of these routines read the X paddle. If you want to read the Y paddle, change the 25 in the DATA statements to a 26. Also, the variable PR in line 10 may be any value where you have 17 available memory locations. For example, to store the routine in the cassette buffer, make PR = 828.
This machine language routine is so fast that even though it reads the paddle 256 times during each call, it is only about eight microseconds slower than a single PEEK in BASIC.
