11

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
  • 4
    10 PRINT PDL(0),PDL(1):GOTO 10
    – fadden
    Commented May 26, 2016 at 23:09
  • 0 ? PDL(0),PDL(1):RUN Commented Apr 10, 2021 at 22:21

1 Answer 1

11

The paddles and joysticks on the Apple work by charging a capacitor 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 capacitors, 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 capacitor 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.

8
  • 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
    Commented May 27, 2016 at 7:09
  • Thanks. Is there a reason you got rid of the button 3 peek?
    – nevster
    Commented May 28, 2016 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
    Commented May 28, 2016 at 13:39
  • The button 3 PEEK was actually the casette input port ($C060), which is not a part of the Apple II game connector. I've never owned an actual analog joystick (only paddles), so it's quite possible they had an extra cable to connect to the cassette input on the back to get a fourth button, or maybe it's a feature of the later Apple models. And I can't remember a program that even uses the third button, so I don't really think it was used anywhere. So it's certainly not canonical.
    – dirkt
    Commented May 28, 2016 at 18:28
  • Button 3 is only found on the IIgs to my knowledge.
    – knghtbrd
    Commented May 28, 2016 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .