/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* JOYSTICK.C -- by Gary Neal, Jr. *
* *
* Source code for our functions that access the joystick controls. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "joystick.h"
/* Joystick potentiometer values */
unsigned short joyPotVal[4];
/* Flags if using BIOS or direct access for joystick interface */
char joystickDirectAccess = 0;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Joyin *
* *
* Samples all four joystick pots and stores their values in *
* joyPotVal[0] through joyPotVal[3]. *
* *
* For Joystick's X-axis, the minimum value is LEFT. *
* For Joystick's Y-axis, the minimum value is UP. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void Joyin(int joyTest)
{
union REGS regs; /* Declare register variable, BIOS call */
unsigned int limit; /* Limit for direct access timing */
unsigned char joyState; /* Current state for direct access */
static char joyMask; /* Joystick direct access mask enabler */
if (joystickDirectAccess) {
/* Clear the joystick potentiometer values */
joyPotVal[0] = joyPotVal[1] = joyPotVal[2] = joyPotVal[3] = limit = 0;
/* Enable all joystick pots if in test mode */
if (joyTest) joyMask = 0x0F;
disable(); /* Disable interrupts for direct access */
outp(0x201, 0); /* Discharge joystick caps */
/* While limit > 0 and joystick caps not fully charged */
while (joyState = (inp(0x201) & joyMask)) {
/* Test and increment joystick pot values */
joyPotVal[JsA_Xaxis] += joyState & 1;
joyState >>= 1;
joyPotVal[JsA_Yaxis] += joyState & 1;
joyState >>= 1;
joyPotVal[JsB_Xaxis] += joyState & 1;
joyState >>= 1;
joyPotVal[JsB_Yaxis] += joyState & 1;
/* Count the loop limit, break if limit reached */
if (!(++limit)) break;
}
enable(); /* Re-enable interrupts */
if (joyTest) {
/* Mask off caps with no corresponding pot values */
joyMask = (joyPotVal[JsA_Xaxis] != 0) << JsA_Xaxis | /* Bit 0 */
(joyPotVal[JsA_Yaxis] != 0) << JsA_Yaxis | /* Bit 1 */
(joyPotVal[JsB_Xaxis] != 0) << JsB_Xaxis | /* Bit 2 */
(joyPotVal[JsB_Yaxis] != 0) << JsB_Yaxis; /* Bit 3 */
}
} else {
regs.h.ah = 0x84; /* Joystick BIOS functions */
regs.x.dx = 0x01; /* Get joystick pot values */
/* Call BIOS to get the pot values */
int86(0x15, ®s, ®s);
/* Store the registers returned as our pot values */
joyPotVal[JsA_Xaxis] = regs.x.ax;
joyPotVal[JsA_Yaxis] = regs.x.bx;
joyPotVal[JsB_Xaxis] = regs.x.cx;
joyPotVal[JsB_Yaxis] = regs.x.dx;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* JoystickButtonPressed *
* *
* Samples the joystick buttons state and returns the desired bits *
* determined by the bit mask (buttonMask). *
* *
* If you want to return the bits for all buttons, pass (JsAllButtons) *
* to this function. *
* *
* The state of the desired bits is 1 if button is pressed, 0 if not. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int JoystickButtonPressed(int buttonMask)
{
union REGS regs; /* Declare register variable for BIOS call */
if (joystickDirectAccess) {
/* Return the buttons state */
return ((~inp(0x201)) & buttonMask);
} else {
regs.h.ah = 0x84; /* Joystick BIOS functions */
regs.x.dx = 0x00; /* Read joystick buttons state */
/* Call BIOS to get the buttons state */
int86(0x15, ®s, ®s);
/* Return the buttons state */
return ((~regs.h.al) & buttonMask);
}
}
               (
geocities.com/siliconvalley/park/7113/GameLib)                   (
geocities.com/siliconvalley/park/7113)                   (
geocities.com/siliconvalley/park)                   (
geocities.com/siliconvalley)