

If you only do read/print then there is no reason for the joystick stuff
not to take 90% of the time even if it is efficient.


The truth is that int 15H joystick reading IS slow. I read it directly
from the hardware port. Note that doing so exposes you to intermittent
disturbance from interrupts, so you may want to read (say) twice in a
row and keep the lower results. Don't just turn interrupts off, it may
prove detrimental to the health of any high speed comms and other
devices.

Here is an example of how to do this:

/* ------------------------------ joy.c ------------------------------------- */

/* An example of how to read PC joystick port by polling the hardware port
 * directly.
 * Uses inp()/outp() for byte port access.
 * Will timeout when 'int count=0' counts up to zero.
 * This sample reads one port (A is presented and B is in the comments).
 * You can read both at once by merging the two, but it will time out
 * when either joystick is not connected.
 *
 * There is no need to optimize this routine since it runs for as long as
 * the joystick circuitry needs.
 *
 * Written by Eyal Lebedinsky (eyal@ise.canberra.edu.au).
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

extern int readjoy (int *x, int *y, int *b1, int *b2);

#define JOYPORT	0x201

#define	XJOY	0x01	/* for joystick B use 0x04 */
#define	YJOY	0x02	/* for joystick B use 0x08 */
#define XYMASK	(XJOY|YJOY)
#define	BUT1	0x10	/* for joystick B use 0x40 */
#define	BUT2	0x20	/* for joystick B use 0x80 */

int					/* returns 0 if ok */
readjoy (int *x, int *y, int *b1, int *b2)
{
	register int	count;
	register char	temp, joy;

	temp = (char)XYMASK;
	count = 0;
	outp (JOYPORT, 0);		/* set trigger */
	do {
		temp ^= (joy = (char)inp (JOYPORT));
		if (temp & XJOY)
			*x = count;
		if (temp & YJOY)
			*y = count;
	} while (++count && ((temp = joy) & XYMASK));
	*b1 = !(joy & BUT1);
	*b2 = !(joy & BUT2);

	return (!count);
}

/* This main() is for demonstration.
*/

int
main ()
{
	int	rc, x, y, b1, b2;