8

How does one use joysticks in a program on the Apple //?

I have three different joysticks here and I would like to visualize their state (pot and button values), maybe similar to the SDL joystick test, for anyone who's ever seen that. That seems like it should be fairly easy to do in Applesoft and run real-time enough to see jitter or worn switches.

If the BASIC program is useful more as an exercise than a diagnostic, is there a better tool for testing joysticks?

  • 2
    10 PRINT PDL(0),PDL(1):GOTO 10 – fadden May 26 '16 at 23:09
9

The paddles and joysticks on the Apple work by charging a capacity through a variable resistance (potentiometer). For the first joystick, the X-axis corresponds to paddle 0, and the Y-axis to paddle 1, and similarly for the second joystick.

Accessing address $C070 discharges all capacities, so they can now charge with a speed that is determined by the resistance. Addresses $C064 to $C067 can be read to check if the capacity is fully charged (MSB set). BASIC is too slow to do that accurately, so you can use the inbuilt PDL(x) commands, which call the monitor routine PREAD.

The 3 buttons can be read by checking the MSB of addresses $C061 to $C063. In BASIC, you can use the following PEEK commands to get the values.

  • PEEK 49249 - PADDLE 0 BUTTON (>127 IF BUTTON PRESSED)
  • PEEK 49250 - PADDLE 1 BUTTON (>127 IF BUTTON PRESSED)
  • PEEK 49251 - PADDLE 2 BUTTON (>127 IF BUTTON PRESSED)

Here is a simple program:

10 LET X = PDL(0)
20 LET Y = PDL(1)
30 PRINT "X=";X;" Y=";Y
40 GOTO 10

I've just tested that in AppleWin using the mouse as the joystick and it works fine.

More details about reading the paddles can be found in the appnote here, which explains some of the quirks you'll need to be aware of.

  • 4
    Using the PEEKs for the game paddle positions by themselves won't work. Apple paddles are basically potentiometers, to measure them you discharge a capacity through them and measure them time it takes to do so. So you need to access address 49264 ($C070) to start measurement and then poll the "read game paddle" address until the high bit gets set (> 127). BASIC is too slow for that, so use the PDL command as you've done. Buttons work fine as PEEKs. – dirkt May 27 '16 at 7:09
  • Oh, ok. I tried writing a quick program but thought it might not be working because it was running in an emulator. Would you like to edit the answer? – nevster May 27 '16 at 7:30
  • Edited as requested. – dirkt May 28 '16 at 7:01
  • Thanks. Is there a reason you got rid of the button 3 peek? – nevster May 28 '16 at 9:02
  • @nevster if you actually managed to attach game paddles or a joystick to the emulator, I'd love to hear which emulator it is. – Mr Lister May 28 '16 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.