home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCGPEV10.ZIP / JOYSTICK.TXT < prev    next >
Text File  |  1994-05-10  |  10KB  |  256 lines

  1.                     ┌──────────────────────────────┐
  2.                     │ Programming the PC Joystick  │
  3.                     └──────────────────────────────┘
  4.  
  5.                        Written for the PC-GPE by
  6.                      Steve McGowan and Mark Feldman
  7.  
  8.  
  9. ┌──────────────────┬───────────────────────────────────────────────────────
  10. │ Programming Info │
  11. └──────────────────┘
  12.  
  13. All joystick programming is done via port 201h.
  14.  
  15.                       ┌───┬───┬───┬───┬───┬───┬───┬───┐
  16.                       │ 7 │ 6 │ 5 │ 4 │ 3 │ 2 │ 1 │ 0 │
  17.                       └───┴───┴───┴───┴───┴───┴───┴───┘
  18.                         │   │   │   │   │   │   │   │
  19. Joystick B, Button 2 ───┘   │   │   │   │   │   │   └─── Joystick A, X Axis
  20. Joystick B, Button 1 ───────┘   │   │   │   │   └─────── Joystick A, Y Axis
  21. Joystick A, Button 2 ───────────┘   │   │   └─────────── Joystick B, X Axis
  22. Joystick A, Button 1 ───────────────┘   └─────────────── Joystick B, Y Axis
  23.  
  24. Reading the status of the joystick buttons is fairly simple. Just read the
  25. byte from the joystick port and check the status of the appropriate bit. A
  26. clear bit (0) means the button is pressed, a set bit (1) means it is not
  27. pressed. Note that the button's are not hardware debounced. Each time a
  28. button is pressed it's bit may "bounce" between 0 and 1 a couple of times.
  29.  
  30. Reading the position of the stick positions is a bit more complicated. You
  31. must first write a dummy byte (any value will do) to the joystick port. This
  32. will set each axis bit to 1. You must then time how long the bit takes to
  33. drop back to 0, this time is roughly proportional to the position of
  34. the joystick axis (see Steve McGowan's discussion below).
  35.  
  36. AT computers also have a BIOS call which supports the joystick. I have come
  37. across numerous machines which apparently did not support this call. My own
  38. machine supports reading the joystick buttons apparently can't read the
  39. stick position values, so I do not advise using this call for any serious
  40. games. In any case here is info on the call:
  41.  
  42. Joystick Support BIOS Call
  43.  
  44. Int 15h
  45.  
  46. To call:
  47.   AH = 84h
  48.   DX = 00h Read switch settings
  49.        01h Read joystick position
  50.  
  51. Returns:
  52.     PC, PCjr : Carry flag set, AH = 80h
  53.        PC XT : Carry flag set, AH = 86h
  54.   All others : DX = 00h on calling
  55.                  AL = Switch settings (bits 4 - 7)
  56.                  Carry flag set on error
  57.                DX = 01h on calling
  58.                  AX = A(X) value
  59.                  BX = A(Y) value
  60.                  CX = B(X) value
  61.                  DX = B(Y) value
  62.  
  63. ┌─────────────────┬──────────────────────────────────────────────────────────
  64. │ Hardware Pinout │
  65. └─────────────────┘
  66.  
  67. The joystick connects to a 15 pin female plug :
  68.  
  69.                      __________________________
  70.                      \ 8  7  6  5  4  3  2  1 /
  71.                       \ 9  10 11 12 13 14 15 /
  72.                        ----------------------
  73.  
  74.                   ┌───────────────────────────────┐
  75.                   │ Pin #  Joystick               │
  76.                   ├───────────────────────────────┤
  77.                   │  1     +5v                    │
  78.                   │  2     Joystick A, Button 1   │
  79.                   │  3     Joystick A, X Axis     │
  80.                   │  4     Gnd                    │
  81.                   │  5     Gnd                    │
  82.                   │  6     Joystick A, Y Axis     │
  83.                   │  7     Joystick A, Button 2   │
  84.                   │  8     +5v                    │
  85.                   │  9     +5v                    │
  86.                   │  10    Joystick B, Button 1   │
  87.                   │  11    Joystick B, X Axis     │
  88.                   │  12    Gnd                    │
  89.                   │  13    Joystick B, Y Axis     │
  90.                   │  14    Joystick B, Button 2   │
  91.                   │  15    +5v                    │
  92.                   └───────────────────────────────┘
  93.  
  94.  
  95. ┌──────────────────────────────────────────────────┬─────────────────────────
  96. │ Misc notes on Joystick handling by Steve McGowan │
  97. └──────────────────────────────────────────────────┘
  98.  
  99. With a polling loop on a 486-66 I got x/y values between 8 and 980. When
  100. I centered the stick the value was usually a value around 330.
  101.  
  102. NOTE: a Gravis Game Pad it only put out 3 values, 8(min), 330(center),
  103. and 980(max). Every joystick I have tried has been non-linear.
  104.  
  105. The "speed compensation" that some games require is due to the fact that
  106. the game designer did not anticipate the range of values that could
  107. come back on faster machines. On a 486-25 you may see max values of 360,
  108. I saw 980, on a Pentium the max value could be well over 2000. If you
  109. had used a unsigned byte value you probably would have been in good
  110. shape on an AT, or 386 but you would be in big trouble with faster machines.
  111.  
  112. Because the joystick logic returns a non linear value, if you base your
  113. scaling only on the 4 corners then the center will be off (biased towards
  114. a corner). If you just use the center value and a single scaling factor
  115. (i.e. of the center is at 330 then full throw should be at 660), then the
  116. stick will saturate (660) half way to the full throw position (980).
  117. That is why most joystick setup programs make the distinction between
  118. hitting the 4 corners and centering the stick.
  119.  
  120. Joystick position vs. loop count
  121.  
  122.      x,y--------------------
  123.      8,8|      330,8       | 980,8
  124.         |                  |
  125.         |                  |    delta 330
  126.         |                  |
  127.    8,330|      330,330     | 980,330 (y centered)
  128.         |                  |
  129.         |                  |    delta 650
  130.         |                  |
  131.    8,980|      330,980     | 980,980
  132.         --------------------
  133.             (x centered)
  134.  
  135. For the best effect you basically need 2 scale factors, depending on whether
  136. you are above or below the center value. I think the curve is actually an
  137. exponential (charging capacitor) but a straight line approximation should
  138. do fine.
  139.  
  140. The 10% dead zone in the center is a good idea. The centering mechanism of
  141. joysticks vary in repeatablity, they don't always come back to the same place.
  142. I have a cheap one that (1 time in 8) does not return to the X center if I
  143. just let it snap to center. It hangs on the high side.
  144.  
  145. I would recommend disabling interrupts while polling. An interrupt
  146. in the middle of your polling loop will really throw off the results. And
  147. any DMA that takes place will also give you bad values.
  148.  
  149. Joysticks are noisy, so holding the stick in a fixed position will return
  150. values that vary +-5% easily. I added a smoothing function to my joystick
  151. code where I throw away single values that are not continuous. It helped
  152. a lot with the noise and the DMA.
  153.  
  154. I use protected mode and the interrupt disable() call doesn't actually work
  155. because it only disables interrupts for the process not the processor.
  156. The smoothing trick can help here too.
  157.  
  158. If I turn on my machine and start the polling loop immediately, it will
  159. put out a centered value of 330,330 but after warming up for 10 minutes
  160. the value changes to 285,285. This variance also needs to be absorbed in
  161. your center dead zone. If after warming up the 'center' value is outside your
  162. dead zone then the cursor will drift (to the left and/or up). Make
  163. sure your game has a "center joystick" command to get around joystick
  164. interfaces with lousy temperature compensation.
  165.  
  166. You must wait for all of the axis bits to settle before initiating
  167. another read, otherwise strange results may come out. So, instead of
  168. reading X, then Y, in two separate loops (which take twice as much time)
  169. Read both X and Y simultaneously, polling until both bits settle. This
  170. can be extended for two joysticks, assuming that they are both attached.
  171. The respective X/Y bits never come true if there is no joystick attached.
  172.  
  173.  
  174. ┌─────────────────────────────┬──────────────────────────────────────────────
  175. │ A Simple Demo Joystick Unit │
  176. └─────────────────────────────┘
  177.  
  178. {
  179.   JOY.PAS - By Mark Feldman
  180.             e-mail address : u914097@student.canberra.edu.au
  181.                              myndale@cairo.anu.edu.au
  182.  
  183.  
  184.   A simple Pascal Joystick Unit.
  185. }
  186.  
  187.  
  188. unit Joy;
  189.  
  190. Interface
  191.  
  192. { Define constants for use as JoystickButton and JoystickPosition parameters }
  193. const JoystickAButton1 = $10;
  194.       JoystickAButton2 = $20;
  195.       JoystickBButton1 = $40;
  196.       JoystickBButton2 = $80;
  197.       JoystickAAxisX   = $01;
  198.       JoystickAAxisY   = $02;
  199.       JoystickBAxisX   = $04;
  200.       JoystickBAxisY   = $08;
  201.  
  202. function JoystickButton(buttonnum : byte) : boolean;
  203. function JoystickPosition(axisnum : byte) : word;
  204.  
  205. Implementation
  206.  
  207. const JOYSTICKPORT = $201;
  208.  
  209. { Button returns true is button is pressed }
  210. function JoystickButton(buttonnum : byte) : boolean;
  211. begin
  212.   JoystickButton := (Port[JOYSTICKPORT] and buttonnum) = 0;
  213. end;
  214.  
  215. { Returns position value of joystick. The value returned is highly
  216.   dependent on machine speed. Changing the setting of the computer's
  217.   Turbo speed button will affect the value returned.
  218.   Returns $FFFF if the joystick is not connected
  219. }
  220. function JoystickPosition(axisnum : byte) : word;
  221. var count : word;
  222. begin
  223.   asm
  224.     mov word ptr count, 0
  225.     cli          { Disable interrupts so they don't interfere with timing }
  226.     mov dx, JOYSTICKPORT   { Write dummy byte to joystick port }
  227.     out dx, al
  228.     @joystickloop:
  229.     inc count              { Add one to count }
  230.     cmp count, $FFFF       { Check for time out }
  231.     je @done
  232.     in al, dx              { Get joystick port value }
  233.     and al, axisnum        { Test the appropriate bit }
  234.     jne @joystickloop
  235.     @done:
  236.     sti                    { Enable interrupts again }
  237.   end;
  238.   JoystickPosition := count;
  239. end;
  240.  
  241. end.
  242.  
  243.  
  244. ┌─────────────┬──────────────────────────────────────────────────────────────
  245. │ References  │
  246. └─────────────┘
  247.  
  248. Title : Flights of Fantasy
  249. Author : Christopher Lampton
  250. Publishers : The Waite Group
  251. ISBN : 1-878739-18-2
  252.  
  253. Title : DOS and BIOS Functions Quick Reference
  254. Publishers : Que Corporation
  255. ISBN : 0-88022-426-6
  256.