resnicks@netcom.COM (Steve Resnick) (03/12/91)
Hi Netlanders! I'm looking for some C (preferably Turbo C) code to access the joystick port. What I need is a way to read the current X/Y values and the button status. This is going to be part of a PD game, so the code should be PD/royalty free to avoid copyright infringements as well as an outlay of cash :) Thanx, in advance, Steve ------------------------------------------------------------------------------- resnicks@netcom.com, steve@camphq, IFNA: 1:143/105.0, USNail: 530 Lawrence Expressway, Suite 374 Sunnyvale, Ca 94086 - In real life: Steve Resnick. Flames, grammar and spelling errors >/dev/null 0x2b |~ 0x2b, THAT is the question. The Asylum OS/2 BBS - (408)263-8017 12/2400,8,1 - Running Maximus CBCS 1.2 -------------------------------------------------------------------------------
nyet@nntp-server.caltech.edu (prof. n liu) (03/13/91)
resnicks@netcom.COM (Steve Resnick) writes: >I'm looking for some C (preferably Turbo C) code to access the joystick port. >What I need is a way to read the current X/Y values and the button status. >This is going to be part of a PD game, so the code should be PD/royalty free >to avoid copyright infringements as well as an outlay of cash :) Just whipped something up real quick (MSC 6.00) I think its short enough to post here. Note: this uses dos interrupt 15, and does not read the port... reading the port for this purpose (i think its address 400-407 or something) is a PAIN! I tried to read the port using C only and it sucks.. only way to do this is probably with assembly, since timing is vital. Turns out you have to TIME how long a certain bit stays high... joystick position is then proportional to that time. The interupt calls i did are almost identical to what you need for Turbo C, i think - the Turbo C bible (Waite Group) documents int86, at least. The interrupt info for the call itself i found in: iAPX 86 Interrupt Primer, by Ralf Brown (i got it somewhere by ftp, can't remember where), last updated 4/90. I highly recommend it for doing any type of interrupt programming. There's really nothing spectacular here, so i doubt i need to copyright it :) 'nother warning: this is by no means robust enough to just slam into any old code.. its just kinda a guideline. Use at your own risk. Nye Liu nyet@cobalt.caltech.edu Microsoft C 6.00 code follows: -----cut me here, press n if you don't want to read this:)----- #include <stdio.h> #include <conio.h> #include <dos.h> /* i think this is msdos.h for TC */ /* nye's quick 'n dirty joystick program */ /* uses dos interrupt 15, ah=84h */ /* pass stick = 0 or 1 depending on which joystick you want to read */ void getjoy(int stick, int *button1, int *button2, int *x, int *y) { union REGS in, out; /* pretty sure this is standard */ int byte; in.h.ah =0x84; in.x.dx =0x01; int86(0x15, &in, &out); /* probably have to change this for TC */ if (stick==0) { *x=(int)(out.x.ax ); *y=(int)(out.x.bx ); } if (stick==1) { *x=(int)(out.x.cx ); *y=(int)(out.x.dx ); } in.x.dx =0x00; int86(0x15, &in, &out); /* probably might have to change this too */ if (stick==0) byte=out.h.al; if (stick==1) byte=out.h.bl; *button1=*button2=0; if ((byte & 16) != 0) *button1=(-1); /* 0 = button down */ if ((byte & 32) != 0) *button2=(-1); } main() { int x,y,b1,b2; while(!kbhit()) { getjoy(0,&b1,&b2,&x,&y); /* status of joystick 0 */ printf("%d %d %d %d \r",b1,b2,x,y); } }
nyet@nntp-server.caltech.edu (prof. n liu) (03/13/91)
forgot to mention that i think the joystick interrupt only exists on the XT2, XT286, and AT's and up. Don't think original PC supports it. Also, the port for the joystick is 200h - 207h. Generally, the read is done at 201h. Got some random documentation on it if anyone wants it... nye nyet@cobalt.cco.caltech.edu