Friday, April 19, 2024 | Toby Opferman
 

Joystick Programming

Toby Opferman
http://www.opferman.net
programming@opferman.net



                             The Joystick

        The joystick is a little bit more complex than the mouse.
of course, there is an interrupt to call to get the joystick
values (INT 15h), it's a better idea & faster to access the joystick
manually through ports.

        The Joystick port is port 201h and you can only read/write bytes
to this one location.  The byte is set up as follows:

[ Joy2 Button2 | Joy2 Button1 | Joy1 Button2 | Joy1 Button1 |
  
  This is the top Nibble of the byte.

  Simple to mask as u can see.  Here is the code to get the
  buttons, the code is formatted in Watcom C/C++ inline assembly.


#pragma aux Joy1Button1 =\
 "MOV DX, 201h"\
 "MOV AL, 0FFh"\
 "OUT DX, AL"\
 "IN AL, DX"\
 "AND AL, 10h"\
 VALUE [ AL ]\
 MODIFY [ AX DX ];

#pragma aux Joy1Button2 =\
 "MOV DX, 201h"\
 "MOV AL, 0FFh"\
 "OUT DX, AL"\
 "IN AL, DX"\
 "AND AL, 20h"\
 VALUE [ AL ]\
 MODIFY [ AX DX ];

#pragma aux Joy2Button1 =\
 "MOV DX, 201h"\
 "MOV AL, 0FFh"\
 "OUT DX, AL"\
 "IN AL, DX"\
 "AND AL, 40h"\
 VALUE [ AL ]\
 MODIFY [ AX DX ];

#pragma aux Joy2Button2 =\
 "MOV DX, 201h"\
 "MOV AL, 0FFh"\
 "OUT DX, AL"\
 "IN AL, DX"\
 "AND AL, 80h"\
 VALUE [ AL ]\
 MODIFY [ AX DX ];



Now for the bottom Nibble

|  Joy2 Y |  Joy2 X |  Joy1 Y  |  Joy1 X ]


 Now, how can the X, Y posititions for the joysticks be 1 bit?
The answer is that you must poll the joystick's port and check the
desired Position bit and keep a count until the bit is set.
When the bit is set, the count is the value.  



   
#pragma aux Joy1X = \
 "CLI"\
 "MOV DX, 201h"\
 "XOR AL, AL"\
 "OUT DX, AL"\
 "XOR CX, CX"\
 "GETVAL: IN AL, DX"\
 "TEST AL, 1"\
 "LOOPNE GETVAL"\
 "XOR AX, AX"\
 "SUB AX, CX"\
 "STI"\
 VALUE [ AX ]\
 MODIFY [ AX CX DX ] ;


#pragma aux Joy1Y = \
 "CLI"\
 "MOV DX, 201h"\
 "XOR AL, AL"\
 "OUT DX, AL"\
 "XOR CX, CX"\
 "GETVAL: IN AL, DX"\
 "TEST AL, 2"\
 "LOOPNE GETVAL"\
 "XOR AX, AX"\
 "SUB AX, CX"\
 "STI"\
 VALUE [ AX ]\
 MODIFY [ AX CX DX ] ;

#pragma aux Joy2X = \
 "CLI"\
 "MOV DX, 201h"\
 "XOR AL, AL"\
 "OUT DX, AL"\
 "XOR CX, CX"\
 "GETVAL: IN AL, DX"\
 "TEST AL, 4"\
 "LOOPNE GETVAL"\
 "XOR AX, AX"\
 "SUB AX, CX"\
 "STI"\
 VALUE [ AX ]\
 MODIFY [ AX CX DX ] ;

#pragma aux Joy2Y = \
 "CLI"\
 "MOV DX, 201h"\
 "XOR AL, AL"\
 "OUT DX, AL"\
 "XOR CX, CX"\
 "GETVAL: IN AL, DX"\
 "TEST AL, 8"\
 "LOOPNE GETVAL"\
 "XOR AX, AX"\
 "SUB AX, CX"\
 "STI"\
 VALUE [ AX ]\
 MODIFY [ AX CX DX ] ;


Some computers are faster than others of course.  So, to make up for
the value differances, you can calibrate the joystick.  Do do so, have
the user roll the joystick around in a complete circle a few times, and
press the button when done.  As the joystick is being ruled, set
Max and Min values for X, Y.  Then ask user to let the joystick still to get
center values or just find a middle between minx, maxx and miny, maxy.
Then when you check a joystick X or Y, just compare it to the values
given in the calibration.


 
About Toby Opferman

Professional software engineer with over 15 years...

Learn more »
Codeproject Articles

Programming related articles...

Articles »
Resume

Resume »
Contact

Email: codeproject(at)opferman(dot)com