From: cnc@balboa.eng.uci.edu (Christopher Christensen)
Subject: Re: Joystick Routines Needed
Newsgroups: comp.os.msdos.programmer,rec.games.programmer
Organization: University of California, Irvine
Date: 17 Nov 92 18:38:52 GMT


Here's a joystick routine I wrote.  Hope it helps.

----------------------------- cut -------------------------------
/*
"jstk.c" -- Joystick demo code

read_jstk() is an assembly language routine which does the actual
reading.  It puts the values in jstk*_*.  The values will range
between 0 and jrange.  The jsense value controls the joystick
sensitivity (the lower the value, the more sensitive the reading).
The jsense value will have to be adjusted to suit the cpu speed.
Oh, the status of the 4 buttons is returned in the 4 LSBs of the
return value of read_jstk().

make recipe (Borland C):
bcc jstk.c jstka.asm

*/

extern int jstk1_x;
extern int jstk1_y;
extern int jstk2_x;
extern int jstk2_y;
extern int jsense;
extern int jrange;
extern read_jstk();

int main()
{
int button, button2;
int new_button;

button=0;
jsense=20;
jrange=500;   /* arbitrary */
while(!kbhit())
  {
  button2=button;
  button=read_jstk();
  new_button=(button^button2)&button;
  if (new_button&1) jsense--;
  if (new_button&2) jsense++;
  if (jsense<1) jsense=1;
  printf("%04x  %04x  %04x  %04x    s=%d\n",
         jstk1_x, jstk1_y, jstk2_x, jstk2_y, jsense);
  }
return(0);
}
----------------------------- cut -------------------------------

----------------------------- cut -------------------------------
; "jstka.asm"
GAMEPORT       =      201h

               IDEAL
               MODEL  small

               DATASEG
               PUBLIC _jstk1_x, _jstk1_y, _jstk2_x, _jstk2_y
               PUBLIC _jsense, _jrange

_jstk1_x       dw     0          ; joystick 1 x-axis
_jstk1_y       dw     0          ; joystick 1 y-axis
_jstk2_x       dw     0          ; joystick 2 x-axis
_jstk2_y       dw     0          ; joystick 2 y-axis
_jsense        dw     1          ; joystick sensitivity
_jrange        dw     300        ; joystick range
               CODESEG

               PUBLIC _read_jstk
PROC           _read_jstk
               mov    dx,GAMEPORT
               out    dx,al          ;start timer
               xor    ax,ax
               mov    [_jstk1_x],ax  ;initialize to zero
               mov    [_jstk1_y],ax  ;
               mov    [_jstk2_x],ax  ;
               mov    [_jstk2_y],ax  ;
               mov    cx,[_jrange]   ;# times to iterate rangeloop
rangeloop:
               mov    bx,[_jsense]
delay:
               in     al,dx
               dec    bx
               jnz    delay

               shr    al,1
               adc    [_jstk1_x],bx
               shr    al,1
               adc    [_jstk1_y],bx
               shr    al,1
               adc    [_jstk2_x],bx
               shr    al,1
               adc    [_jstk2_y],bx
               loop   rangeloop
               not    al
               and    ax,0fh          ;return the button status in ax
               ret
ENDP           _read_jstk

               END
----------------------------- cut -------------------------------


--
|  Christopher  |  Computer Engineering graduate student, UC Irvine  |
|  Christensen  |  cnc@balboa.eng.uci.edu                            |
Newsgroups: comp.os.msdos.programmer