ATARI BASIC
Joystick Routine

Kirk Gregg

Complete with many techniques for conserving space and speeding program execution, this handy routine, entirely in BASIC, provides you with a fairly fast, non-assembler dependent, joystick reading routine.

Two excellent Atari joystick reading routines have been published in COMPUTE! (July and August 1981, #14 and #15). So, is there really any need for another Atari joystick routine?

Glad you asked! Both of the published routines work, and both are fast. However, both routines use calls to assembler routines via the USR function call. This method requires that the machine language routine be included in the program encoded as DATA statements to be READ and POKE'd into memory. The code required to initialize the routines and the DATA statements take up space in memory.

There are still many of the original 8K 400s around. If you have one of them, and your program memory requirements begin to approach or exceed your machine's RAM limit, the initialization code and associated DATA statements, used only once in the program, become prime candidates for removal.

Also, since these routines both use calls to USR assembler routines, you have to know Atari/6502 assembler to modify them if necessary for certain applications. I suspect that many of us are on fairly good terms with Atari BASIC, but have not really gotten a handle on assembler just yet.

Therefore, what would be useful is a simple Atari BASIC joystick routine. It should be as compact as possible, for machines with limited memory. Also, it should operate as fast as possible so as not to unduly degrade program speed when used in game applications. So, branches within the routine itself should be avoided, since each transfer of program control to any line number except the next sequential line requires a line number search beginning at the top of program memory, comparing every line number in numerical order until the target line number is located.

Here, then, is my Atari BASIC joystick routine:

20 S = STICK(S)
30 DX = (S = 5 OR S = 6 OR S = 7) - (S = 9 OR S = 10 OR S = 11)
40 DY = (S = 5 OR S = 9 OR S = 13) - (S = 6 OR S = 10 OR S = 14)
50 RETURN

This routine, as written in the listing above, uses less than 200 bytes of RAM, including variables. It can be combined into one program line, requiring only 188 bytes.

To use this routine, set variable S equal to the number of the joystick you want to read (0-3). Then, call the subroutine, in this case GOSUB 20. The delta-X and delta-Y values are returned in variables DX and DY, respectively. Note that the value input to the routine in variable S is lost. If the input value needs to be retained for use by the calling routine, change the input joystick index parameter variable to any other available variable. For example, if your calling routine needs to read the joystick for each of the four players in a game control loop, it could contain the following sequence:

FOR I = 0 TO 3
GOSUB 20

(Code to handle DX and DY for player 1)

NEXT I

Then, the joystick routine entry point, line 20, would be changed to:

S = STICK(I)

In some applications, you may want to get joystick readings for only the four cardinal directions, that is, N-S-E-W, but ignore the diagonal stick readings. In that case, modify lines 30 and 40 of the routine as follows:

30 DX = (S = 7) - (S = 11)
40 DY = (S = 13) - (S = 14)

To try out this routine, type in the routine and the following short demonstration program:

10 GOTO 100
100 S = 0 : GOSUB 20
110 IF NOT(DX OR DY) THEN 100
120 ? , "dX = " ; DX, "dY = " ; DY : GOTO 100

Plug a joystick into the player #1 slot, then run the program. Observe the DX and DY values for each stick position.

Now, try the short demo program below. Study this program to see how the joystick routine can be used in your programs.

PROGRAM. Atari BASIC Joystick Routine.

1 REM ..... SCRIBBLE DEMO PROGRAM
10 GRAPHICS 23 : X = 79 : Y = 47 : COLOR 1 : GOTO 160
20 S = STICK(S)
30 DX = (S = 5 OR S = 6 OR S = 7) - (S = 9 OR S = 10 OR S = 11)
40 DY = (S = 5 OR S = 9 OR S = 13) - (S = 6 OR S = 10 OR S = 14)
50 RETURN
100 S = 0 : GOSUB 20
110 IF NOT(DX OR DY) THEN 100
120 X = X + DX : IF X > 159 THEN X = 0
130 IF X < 0 THEN X = 159
140 Y = Y + DY : IF Y > 95 THEN Y = 0
150 IF Y < 0 THEN Y = 95
160 PLOT X, Y
170 N = 255 - INT((X/159 + Y/95) * 125)
180 SOUND 0, N, 10, 8
190 GOTO 100

Return to Table of Contents | Previous Section | Next Section