Joystick Programming

The following is a little bit of information that will help you program the game port (Joystick). It shows how to access both joysticks.

The Joystick is accessed through Port 201h

Bit 0 - joystick 1, x coord (0 - timing is active)
Bit 1 - joystick 1, y coord (0 - timing is active)
Bit 2 - joystick 2, x coord (0 - timing is active)
Bit 3 - joystick 2, y coord (0 - timing is active)
Bit 4 - joystick 1, button 1 (0 - pressed)
Bit 5 - joystick 1, button 2 (0 - pressed)
Bit 6 - joystick 2, button 1 (0 - pressed)
Bit 7 - joystick 2, button 2 (0 - pressed)

Bits 3-0 are resistive inputs with the length of the pulse determined by 0-100k ohm resistive load.

Use the formula:
time = 24.2µ + (0.011µ * resistance)
resistance = (time -24.2) / 0.011

A read should be immediately preceded by a write (any data) to start timing for the resistive values.

This info was taken out of HELPPC 2.10 by David Hurgens. See my Links page to download it from a Simtel mirror.

Now that you know how to do it the hard way, lets let the BIOS do the hard part and just do a BIOS interrupt call using the registers.

Using service 84h of Interrupt 15h we can get the status of two (2) joysticks and their button status from the game port.
By calling this service with DX=0, the BIOS will return the four (4) buttons' status in bits 7-4 of AL.
Bit 4 is Switch 1 of Joystick 1 (1 if pressed)
Bit 5 is Switch 2 of Joystick 1 (1 if pressed)
Bit 6 is Switch 1 of Joystick 2 (1 if pressed)
Bit 7 is Switch 2 of Joystick 2 (1 if pressed)

By calling this service with DX=1, the BIOS will return the two (2) positional coordinates of each joystick in the registers AX, BX, CX, and DX.
AX = x-coord. of Joystick 1
BX = y-coord. of Joystick 1
CX = x-coord. of Joystick 2
DX = y-coord. of Joystick 2

These coords. are not like the mouse drivers coords. as you might think so. These are exponential operations rather than linear operation. The best think to do is at program startup, ask the user to put the joystick in the center position and press one of the buttons to acknowledge, then have the user put the joystick in the TOP-LEFT most position and then the BOTTOM-RIGHT most position to find the range of the joystick.

If you want to find the position of the joystick, take the value it is now and check it against the value that was returned when the joystick was put in the center position at startup.

This service is not supported by all BIOS, so make sure that your BIOS supports this service before you rely on it.

You can check by calling this service with DX=1 and checking the carry flag. If the carry flag is not 0 then there is an error and your BIOS does not support this function.
Also see Detecting the Gameport on this site for another way of detecting the game port.

Something to think about:
If you connect a device to the gameport that uses resistance just like the joystick, then you can communicate through a home-made device through this port.
Also note that if you want to communicate with a device that only has two switches that can be either on or off, you can also use this port by using the correct pins on the cable. (make the BIOS think that the switches resemble the switches on the joysticks)