From: [email protected] (Bill Frolik)
Newsgroups: comp.sys.ibm.pc
Subject: Re: How to read game port
Date: 6 Apr 90 17:47:16 GMT

> 1) Is there anyone that can tell me the specifications for the game port
>    connector?

       15-pin D-shell connector:

       PIN     SIGNAL          PIN     SIGNAL
       ---     ------          ---     ------
        1      +5V              8      +5V
        2      button 1         9      +5V
        3      stick 1         10      button 3
        4      GND             11      stick 3
        5      GND             12      GND
        6      stick 2         13      stick 4
        7      button 2        14      button 4
                               15      +5V

       Game card has a female connector;
       Joystick cable has male connector.
       Joystick wiring:

               +5      ________________
                                       |
               stick1  ________        |
                               | 100K  |
                           _/\/\/\_____|
               stick2  ________        |
                               | 100K  |
                           _/\/\/\_____|
               stick3  ________        |
                               | 100K  |
                           _/\/\/\_____|
               stick4  ________        |
                               | 100K  |
                           _/\/\/\_____|
                             ___
               button1 ______o o_______
                             ___       |
               button2 ______o o_______|
                             ___       |
               button3 ______o o_______|
                             ___       |
               button4 ______o o_______|
                                       |
               GND     ________________|


> 2) As near as I can figure, I have to use the inpw() function (I'm using
>    C) to read the ports.  Unfortunately, I don't know the port number to
>    give the function to read the port.  If this isn't the right/best way,
>    please let me know.

       You need to do byte I/O to and from port 201h.
       To read the joysticks (or your slide pot positions), you
       must first write a byte to port 201h.  It doesn't matter
       what value you send, as long as you perform an I/O write.
       This triggers the 558 timer on the game adapter.

       Game port 201h byte:
        _______________________________________________________
       |   7  |   6  |   5  |   4  |   3  |   2  |   1  |   0  |
       | but4 | but3 | but2 | but1 | stk4 | stk3 | stk2 | stk1 |
       |______|______|______|______|______|______|______|______|

       The most machine-independent way to sample the game port
       is to use a timer.  Note the time (e.g., read the countdown
       register in Timer 0, you need pretty fine resolution and this
       timer performs 65535 counts every 55 ms) just before you
       trigger the 558.  After triggering, sit in a loop reading
       port 201h and examining bits 0-3.  For those bits that have
       a joystick pot attached, you'll see them sit for a while at 0,
       then become 1.  As each bit flips back to 1, note the time
       again.  When all bit 0-3 have flipped back to 1, you're almost
       done.  Compute elapsed time for each bit, and you end up with
       a value that is proportional to pot position.

       If you're lazy and don't care about machine independence,
       you can just trigger the card, then sit in a loop and count
       up from zero.  As each bit flips back to 1, save the count
       for that bit.  I've seen a few games that probably use
       this method.  Problem is that if you then run the same code
       on a faster/slower machine, your calibration can get very
       easily messed up.

       Buttons can be read at any time just by reading port 201h
       and looking at bits 4-7.  No triggering is required.
       Button bits are normally 1; while a button is depressed,
       its bit will flip to 0.

___________________________________________

Bill Frolik             Hewlett-Packard Co.
hp-pcd!bill             Corvallis, Oregon
[email protected]   (503)750-4082