C Code To Interface Game port Joystick
Article by: Ravee H Bhalla
Program To Read Joystick Button Values
/* Program to read joystick button values */
#include<stdio.h>
#include<conio.h>
#include<dos.h>
union REGS in, out;
int main()
{
unsigned char al, j1b1, j1b2, j2b1, j2b2;
in.h.ah = 0x84; // function number
in.x.dx = 0x00; // sub-function number
while(!kbhit())
{
int86(0x15,&in,&out); // interrupt 15h
al = out.h.al; // output obtained in al register
// 0 indicates pressed
if((al&0x80) == 0x00) // bit d7 corresponds to button1 of joystick1
j1b1 = 1;
else
j1b1 = 0;
if((al&0x40) == 0x00) // bit d6 corresponds to button2 of joystick1
j1b2 = 1;
else
j1b2 = 0;
if((al&0x20) == 0x00) // bit d5 corresponds to button1 of joystick2
j2b1 = 1;
else
j2b1 = 0;
if((al&0x10) == 0x00) // bit d4 corresponds to button1 of joystick2
j2b2 = 1;
else
j2b2 = 0;
printf("(Joystick 1)Button 1: %d Button 2: %d ",j1b1,j1b2);
printf("(Joystick 2)Button 1: %d Button 2: %d\n",j2b1,j2b2);
}
getch();
return 0;
}
Program To Read Joystick (X,Y) Co-ordinates
/* Program to read joystick (x,y) coordinates */
#include<stdio.h>
#include<conio.h>
#include<dos.h>
union REGS in, out;
int main()
{
unsigned char j1x, j1y, j2x, j2y;
in.h.ah = 0x84; // function number
in.x.dx = 0x01; // sub-function number
while(!kbhit())
{
int86(0x15,&in,&out); // interrupt 15h
j1x = out.x.ax; // x coordinate of joystick 1
j1y = out.x.bx; // y coordinate of joystick 1
j2x = out.x.cx; // x coordinate of joystick 2
j2y = out.x.dx; // y coordinate of joystick 2
printf("(Joystick 1) X: %d Y: %d ",j1x,j1y);
printf("(Joystick 2) X: %d Y: %d\n",j2x,j2y);
delay(50);
}
getch();
return 0;
}