home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / asm / 80x0393 / joystick.txt < prev    next >
Encoding:
Text File  |  1993-10-30  |  46.2 KB  |  842 lines

  1.                  Emil Gilliam's Guide to Game Port Programming
  2.                                Revised 10/30/93
  3.  
  4. TABLE OF CONTENTS
  5.  
  6. Section  Name         
  7.  
  8.          LEGAL INFORMATION
  9.    0     SUMMARY OF CHANGES FROM 10/22/93 AND 06/10/93 REVISIONS
  10.    1     INTRODUCTION
  11.    2     ROM BIOS GAME PORT FUNCTIONS
  12.    3     ACCESSING THE GAME I/O PORT DIRECTLY
  13.    4     INTERPRETING THE JOYSTICK X AND Y VALUES
  14.    5     GRAVIS GAMEPAD
  15.    6     PADDLES
  16.    7     DETECTING THE GAME PORT AND CONTROLLERS
  17.    8     GAME PORT PINOUTS
  18.    9     HOW TO CONTACT ME
  19.  
  20. ------------------------------------------------------------------------------
  21.  
  22. LEGAL INFORMATION
  23.  
  24.    This "Joystick Guide" is (C) Copyright 1993 Emil Gilliam.  All rights 
  25. reserved.
  26.  
  27. THIS DOCUMENT AND THE ACCOMPANYING SOURCE CODE FILE ARE PROVIDED "AS IS" 
  28. WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF 
  29. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  EMIL GILLIAM WILL NOT BE 
  30. HELD LIABLE FOR ANY DAMAGES OR LOSSES OF ANY KIND THAT RESULT FROM THE USE OR 
  31. THE INABILITY TO USE THE INFORMATION PROVIDED IN THIS DOCUMENT OR THIS SOURCE 
  32. CODE FILE, INCLUDING, BUT NOT LIMITED TO, LOSS OF PROPERTY OR INCOME. 
  33.  
  34.    This document and its accompanying source code file are freeware, not 
  35. public domain.  They may be distributed freely provided that neither file is 
  36. modified, and that they are distributed together along with FILE_ID.DIZ in 
  37. their entirety, including the legal notice, and that: 
  38.    If they are distributed by a computer club, no more than $5 U.S. is charged 
  39. for the disk on which the archive containing this document and the 
  40. accompanying source code file is stored; if they are to be distributed by a 
  41. shareware/freeware company (for example, on a CD-ROM collection of files), my 
  42. permission is obtained first. 
  43.    This legal information supersedes all previous notices.
  44.  
  45. ------------------------------------------------------------------------------
  46.  
  47. 0.0  SUMMARY OF CHANGES FROM 10/22/93 AND 06/10/93 REVISIONS
  48.  
  49. Changes from 10/22/93 revision:
  50.  
  51.     Improved copyright notice, thanks to:
  52.         Kip Cooley at the Diamond Bar BBS (909) 923-1031 (1:218/101).
  53.         Ian Remmler at the DownTown BBS (210) 625-4479 (1:387/1001).
  54.  
  55.     Bug fix (SI and DI should be saved, ES was saved incorrectly in 
  56.         read_2_joysticks) in read_1_joystick and read_2_joysticks.  Thanks to
  57.         Kip Cooley (see above).
  58.  
  59.     A little optimization in the detect_game_port procedure, thanks to David
  60.         Kirschbaum, "Toad Hall," at the Federal Post BBS (1:3634/2).
  61.  
  62.     FILE_ID.DIZ file added.
  63.  
  64.     A big thanks to everyone who has made suggestions for this Guide!
  65.  
  66. Changes from 06/10/93 revision:
  67.  
  68.     More specific about precision and ranges of coordinates in section 4.1.
  69.     Added information about game-port bit in equipment-status word.
  70.     More specific about the joystick port itself (in section 8).
  71.  
  72. ------------------------------------------------------------------------------
  73.  
  74. 1.0  INTRODUCTION
  75.         
  76. 1.1  About the game port
  77.         
  78.    Any machine in the IBM PC family of computers can have a game port, either 
  79. built-in or on an add-on card.  As suggested by its name, the game port is 
  80. where you can connect one or two joysticks, or other controllers suitable for 
  81. playing games with, to the computer.  This document describes how a program 
  82. accesses the game port, so if you're writing a game or other program that uses 
  83. a joystick or other such controller, you'll know how to get your program to 
  84. get the status of the joystick or whatever is hooked up to the game port. 
  85.    As far as I know, three things can be connected to a game port: an analog 
  86. joystick (you can have up to two connected at once), a Gravis GamePad, which 
  87. is a digital joystick (see section 5), or up to four paddles (see section 6).  
  88. By far, analog joysticks are more commonly used for game controllers than the 
  89. other two.  (I have never actually seen a paddle for the IBM in my life, nor 
  90. seen a program that uses it; I only know that such a thing exists, but it must 
  91. be extremely rare or old.  I only included information on it here for 
  92. completeness.) 
  93.    When analog joysticks are connected to the game port, they are referred to 
  94. as joysticks A and B (for player 1 and player 2 in a game, respectively).  
  95. Each analog joystick has two buttons on it.  The stick itself is opposed to 
  96. the type of joystick on a Gravis GamePad or to what's used for a Nintendo or 
  97. Sega, or to the joysticks used on many early home computers that have 9-pin 
  98. joystick connectors, all of which are "digital" joysticks.  (Usually in this 
  99. document, if I refer to simply the "joystick," I'm talking about the analog 
  100. joystick.) 
  101.         
  102. 1.2  The difference between an analog and a digital joystick
  103.         
  104.    What's the difference between an analog joystick and a digital joystick?  
  105. Inside a digital joystick like the Gravis GamePad, there are simple switches 
  106. which can only detect if the joystick is all the way up, down, left, or right, 
  107. at any of the 4 diagonals, or at the center.  The joystick therefore has only 
  108. 9 distinct positions.  In an analog joystick, however, the joystick has 
  109. potentiometers inside it that can tell where the stick's position is more 
  110. accurately, so a program doesn't have to know that the joystick is somewhere 
  111. to the left of the center or whatever; it can know just how far it is to the 
  112. left. The joystick gives information to the computer about where the stick's 
  113. coordinates are. 
  114.         
  115. 1.3  Reading the controller values
  116.  
  117.    The game port isn't tied to any interrupts; if you're writing a game or 
  118. some other application that does something whenever a joystick button is 
  119. pressed or whenever the stick is moved (or the paddle is turned), you have to 
  120. program it to "poll" the game port, in other words, read the game port status 
  121. over and over (perhaps many times per second) and see if a button is being 
  122. pressed or the joystick is in a particular position. 
  123.    There are two ways for a program to get the status of the game port (and 
  124. thus of whatever controller is connected to it): It can call the ROM BIOS 
  125. functions to do it (see section 2), or it can directly use the I/O ports that 
  126. are connected to the game port (see section 3). 
  127.       
  128. ------------------------------------------------------------------------------
  129.  
  130. 2.0  ROM BIOS JOYSTICK FUNCTIONS
  131.  
  132. 2.1  About the functions
  133.         
  134.    Here are the ROM BIOS functions for reading the joystick positions and 
  135. button status.  (You can also use them for getting the status of the paddles, 
  136. see section 6.)  Beware that some older machines (original PC's and PCjr's, 
  137. and XT's whose BIOSes date back to 11/08/82) don't support these INT 15h 
  138. functions in their ROM BIOSes.  (It's ironic that PCjr's don't support these 
  139. functions, because all PCjr's have a game port - I think.  XT's with newer 
  140. BIOSes, 01/10/86, do support these functions.)  
  141.  
  142. 2.2  Function to get button status
  143.         
  144. INT 15h Function 84h - joystick functions
  145.  
  146.     Subfunction 0 - get joystick button status
  147.     DON'T USE THIS UNLESS YOU ABSOLUTELY HAVE TO -- IT'S SLOW!!!  See 2.5.
  148.     Call with: AH=84h, DX=0
  149.     Returns:
  150.         On PC's and PCjr's (which don't support this function), AH=80h and 
  151.         CF=1, other flags: undefined
  152.         
  153.         On PC XT's whose BIOSes date to 11/08/82 (which also don't support
  154.             this function, AH=86h and CF=1, other flags undefined
  155.  
  156.         On all other machines (which do support this function), CF=0,
  157.             AH: Bits 0-3 - undefined (probably 0 but don't count on it)
  158.                 Bit  4   - Joystick A button 1 status (1=button not pressed,
  159.                                                        0=button being pressed)
  160.                 Bit  5   - Joystick A button 2 status (ditto)
  161.                 Bit  6   - Joystick B button 1 status (ditto)
  162.                 Bit  7   - Joystick B button 2 status (ditto)
  163.             Note: Those are the bit definitions for the analog joystick.  See
  164.             the sections on the Gravis GamePad (section 5) and the paddles 
  165.             (section 6) for the bit definitions for those controllers.
  166.             DX: Undefined (probably 201h but don't bet your life on it)
  167.             Other flag registers: undefined
  168.  
  169.             Officially, this function will can return CF=1 if there was an 
  170.             error reading the joystick button values or there is no game 
  171.             adapter.  I don't see how that can ever happen; the ROM BIOS code 
  172.             can't return CF=1 and never checks to see if a game adapter is 
  173.             installed at all, at least in the 01/10/86 XT BIOS. 
  174.  
  175. 2.3  Function to get joystick position
  176.     
  177. INT 15h Function 84h - joystick functions
  178.  
  179.     Subfunction 1 - get joystick position
  180.     Call with: AH=84h, DX=1
  181.     Returns:
  182.         On PC's, PCjr's, and XT's with 11/08/82 BIOSes (which don't support 
  183.         this function), see section 2.2. 
  184.  
  185.         On all other machines (which do support this function), CF=0,
  186.             AX = Joystick A x-coordinate (see section 4 about these)
  187.             BX = Joystick A y-coordinate (ditto)
  188.             CX = Joystick B x-coordinate (ditto)
  189.             DX = Joystick B y-coordinate (ditto)
  190.             Note: These are the register definitions for the analog joystick.  
  191.             See the sections on the Gravis GamePad (section 5) and the paddles 
  192.             (section 6) for the register definitions for those controllers.
  193.             Other flag registers: undefined
  194.  
  195.             Officially, this function can also return CF=1 if there was an 
  196.             error, like no adapter card.  However, at least in the 01/10/86 XT 
  197.             BIOS, this can't ever happen; the code never checks for an adapter 
  198.             card.  Upon exit, any of the four registers for joystick values 
  199.             will be equal to 0 if that joystick isn't connected (or if the 1 
  200.             bit in the game port register for that coordinate never turned 0 
  201.             within a reasonable amount of time; you'd know what the heck I'm 
  202.             talking about if you read section 3.) 
  203.  
  204. 2.4  Advantage to using ROM BIOS functions
  205.  
  206.    Really, there aren't a whole lot of reasons for using the ROM BIOS 
  207. functions for reading the joystick (or other controller) status.  I suppose if 
  208. you're doing some quick-and-dirty program, it's easier because you don't have 
  209. to come up with the routine yourself.  If there is a joystick emulator of some 
  210. sort installed, it might intercept the joystick calls to interrupt 15h (but it 
  211. would more likely put the processor into V86 mode and trap I/O to port 201h if 
  212. the processor is a 386 or up) and so your program might work with it better.  
  213. This is quite unlikely because I've never seen anything that emulates a 
  214. joystick or other controller. 
  215.  
  216. 2.5  Disadvantages to using the ROM BIOS functions
  217.  
  218.    The big minus to using the ROM BIOS functions for reading the joystick (or 
  219. other controller) status is that they're really SLOW!  Considering how many 
  220. things have to trap interrupt 15h (DOS, the XMS driver, and probably other 
  221. things), and how the ROM BIOS has to check the function number and do other 
  222. maintenance garbage, like with the CMOS RAM on some newer ROM BIOSes, the 
  223. system wastes a lot of time just getting to the joystick routines.  Also, the 
  224. ROM BIOS routine for reading the joystick position is quite slow because it 
  225. reads the joystick coordinates one at a time instead of all at once (as far as 
  226. I know that's how it's done in all ROM BIOSes).  At least, don't call INT 15h 
  227. for getting the joystick button values if you can help it; you can get the 
  228. joystick button status in just two instructions; I'll even give some code for 
  229. doing that if you are a beginner at assembly language or are too lazy to 
  230. figure this one out.  (See the routine button_status in JOYSTICK.ASM.)
  231.    The other disadvantage is that some older machines (original PC's, PCjr's, 
  232. and XT's whose BIOSes date back to 11/08/82) don't support these INT 15h 
  233. functions in their ROM BIOSes.  (XT's with newer BIOSes, 01/10/86, do support 
  234. these functions.)  On the machines that don't support the joystick functions 
  235. in their ROM BIOSes, in order to read the joystick values or get the status of 
  236. the buttons, you have to do it yourself with hardware ports.  (See section 3.) 
  237.  
  238. ------------------------------------------------------------------------------
  239.  
  240. 3.0  ACCESSING THE GAME I/O PORT DIRECTLY
  241.  
  242. 3.1  How it works
  243.  
  244.    The game port is accessed through a single 8-bit I/O port, 201h.  
  245. (Officially, ports 200h through 20Fh are for the game port; only 201h is used, 
  246. however.  The rest of these ports aren't used by the game port but maybe there 
  247. are other things that use them [?].)  When you read an 8-bit value from port 
  248. 201h, here's what the bits are (for the analog joystick; see the sections on 
  249. the Gravis GamePad [section 5] and the paddles [section 6] for information on 
  250. those controllers).  Be aware that the game port itself has no idea what is 
  251. connected to it; it's the controller that gives it the bits that you get 
  252. through port 201h. 
  253.  
  254.         Bit     Description
  255.          0      Joystick A x-coordinate
  256.          1      Joystick A y-coordinate
  257.          2      Joystick B x-coordinate
  258.          3      Joystick B y-coordinate
  259.          4      Status of joystick A button 1 (1=not pressed, 0=pressed)
  260.          5      Status of joystick A button 2 (ditto)
  261.          6      Status of joystick B button 1 (ditto)
  262.          7      Status of joystick B button 2 (ditto)
  263.  
  264.    Reading the joystick button status is very easy; you simply IN from port 
  265. 201h and the top 4 bits of AL will be the button status.  (This takes a 
  266. whopping two instructions; again, see button_status in JOYSTICK.ASM.) 
  267.    You're probably wondering how the x and y coordinates of the joysticks can 
  268. fit within single bits, especially for the analog joystick which can detect 
  269. many x and y coordinates.  This is the slow part about reading the joystick 
  270. position and it's a pain to write a routine to do that that's very optimized.  
  271. (Look in JOYSTICK.ASM and you'll find routines to do that, already there for 
  272. you to use and modify; they are read_1_joystick and read_2_joysticks.  It's 
  273. much better to rely on these than the ROM BIOS routines which can be horribly 
  274. inaccurate.) 
  275.    To read the joystick coordinates, first you write a value to the 8-bit port 
  276. 201h.  It doesn't matter what the value is, just as long as you write a value.  
  277. This does what's called firing the joystick one-shots.  The four bottom bits 
  278. of port 201h will immediately become 1.  You read the joystick port over and 
  279. over again and time how long it takes for the bits to turn 0.  The bits do not 
  280. become 0 all at once; each one changes from 1 to 0 in an amount of time 
  281. (elapsed time after you wrote the value to port 201h) proportional to the x or 
  282. y coordinate that that bit represents. 
  283.    Yes, I know what you're thinking.  That's a pain in the gluteus maximus.  
  284. For one thing, that might be slow especially if you're reading the joystick 
  285. positions several times a second, (it takes the same amount of time on every 
  286. computer regardless of speed).  It would be nice if there were separate I/O 
  287. ports for each coordinate where you read the port and that's the value right 
  288. there.  But this is a lot easier for the hardware engineers. 
  289.    The reason the ROM BIOS routine for reading the joystick position is slow 
  290. is because it reads the four joystick coordinate bits one at a time; it will 
  291. fire the joystick one-shots, time how long it takes for bit 0 to change from 1 
  292. to 0, fire the joystick one-shots again, time how long it takes for bit 1 to 
  293. change from 1 to 0, and so on.  That's not very efficient; the fastest 
  294. possible routine for reading the joystick position would fire the joystick 
  295. one-shots once and then time all four bits at once.  That's kind of hard to do 
  296. considering that even on slow computers you want the timing to be as accurate 
  297. as possible but at the same time the computer has to sort out which bits 
  298. changed when, and which coordinates they would represent.  JOYSTICK.ASM,
  299. included in this archive, has a routine (read_2_joysticks) that will time all 
  300. four joystick coordinate bits at once.  It might not be very accurate on 
  301. slower computers, however, because many timer chip counts might pass by on 
  302. each run of the loop while the CPU tries to figure out what to do about the 
  303. bits in the joystick port value that changed.  But if you're only going to 
  304. read joystick A, you can use read_1_joystick instead which is more accurate on 
  305. slower computers because the loop size is smaller and uses registers instead 
  306. of memory operands to save the coordinates it's figured out so far.
  307.    Be aware that if a joystick isn't connected, the bits representing that 
  308. joystick will either be 0 and stay 0 or will be 1 and stay 1.  If the bits for 
  309. a joystick are 1 and don't become 0 within a reasonable amount of time, assume 
  310. that the joystick isn't connected or that something's wrong with the joystick.  
  311. Just how long is a "reasonable amount of time" ?  See section 3.2.2. 
  312.    Note: Whenever you fire the joystick one-shots, you have to wait for all 
  313. four 1-bits to turn 0 before you can fire the joystick one-shots again.  
  314.  
  315. 3.2  Timings
  316.  
  317.    In your routine for reading the joystick position, you should use the timer 
  318. chip as a way of finding out the length of time between when you fire the 
  319. joystick one-shots and a bit changes to 1.  
  320.  
  321. 3.2.1  Lengths of time
  322.  
  323.    Have your joystick-reading routine count the number of timer chip clock 
  324. ticks (there are 1193180 ticks per second) that pass between when you fire 
  325. the joystick one-shots and when the bit turns from 1 to 0.  Then have it 
  326. divide this value by 16.  This will be the value for the coordinate you're 
  327. reading.  (Actually you don't have to use 16/1193180's of a second as the 
  328. units; you can use any units you want for the coordinates of the joystick.  
  329. However, these are the units which the values returned by the ROM BIOS 
  330. joystick-reading function are in, they're the units used in this document 
  331. whenever joystick x or y coordinates are given, and they're also the units 
  332. used by the functions in JOYSTICK.ASM.
  333.  
  334. 3.2.2  How long is too long?
  335.  
  336.    If you fire the joystick one-shots, and the 1 bits don't turn to 0 within a 
  337. normal amount of time (or don't turn to 0 at all), something must be wrong 
  338. with the joystick port or the joystick(s).  How long is a reasonable amount of 
  339. time before assuming that something's wrong?  The maximum x or y coordinate 
  340. value that you can get back from the ROM BIOS is 1FFh.  On my joystick, 
  341. coordinates don't usually go past 100 or 150.  I guess 1FF0h clock ticks 
  342. after when you write to the joystick port would be a good time to assume that 
  343. something's wrong.
  344.    Of course, this could also depend on what your program expects the maximum 
  345. possible x or y coordinate to be; it gets this information from whenever it 
  346. calibrates.  But the length of time after you fire off the joystick one-shots 
  347. needs to be considerably more than what the program considers to be the 
  348. maximum coordinate before it cries wolf.  For example, if during calibration 
  349. (see section 8) the program found out that the maximum x-coordinate returned 
  350. by the joystick was 140, and then during the program the 1 bit hasn't changed 
  351. to 0 by the time 140*16 clock ticks have intervened, don't assume something's 
  352. wrong yet; wait until at least 30*16 more clock ticks, perhaps, and if the 1 
  353. bit changes to 0 within that time, your program can treat this extraordinarily 
  354. high x coordinate to be the same as 140.  Remember, the joystick is not very 
  355. exact.
  356.    In the XT ROM BIOS, the ROM BIOS waits for a certain number of runs of a 
  357. loop to happen before it assumes that there's an error; but that's not a good 
  358. way to do it because that's processor-speed specific.
  359.  
  360. ------------------------------------------------------------------------------
  361.  
  362. 4.0  INTERPRETING THE JOYSTICK X AND Y VALUES
  363.  
  364. 4.1  Now for the hard part...
  365.  
  366.    Analog joysticks are not very exact.  Their values can "float" around over 
  367. periods of time, and constantly require calibration, both on the joystick and 
  368. by the program.  The maximum x-coordinate is about 146 on my joystick (which 
  369. is a Kraft QuickShot), and the maximum y-coordinate is about 154, but the 
  370. routine in my ROM BIOS which reads the joystick values is not very accurate at 
  371. all, and the maximum y-coordinates can jump around all the way from 105 to 
  372. 155!  And then, of course, turning the calibration knobs on my joystick could 
  373. change it somewhat, and on a different brand or model of joystick the ranges 
  374. might be very different.  According to "Advanced MS-DOS Programming" by Ray 
  375. Duncan, the coordinates should be in the range 0 to 416 for a 250 KOhm 
  376. joystick.  I'd appreciate it if people could please give me the values they 
  377. get for their joysticks.
  378.    Maybe the only thing you can be certain of is that higher x-coordinates are 
  379. right and that higher y-coordinates are down, and that the coordinates are 
  380. evenly spaced (i.e. moving the joystick the same amount in a particular 
  381. direction makes the coordinates change the same amount, more or less, no 
  382. matter what position the joystick was in to begin with).  If the joystick 
  383. stays in a particular direction, the x- and y-coordinates will probably jump 
  384. around by 1 or 2; you can't expect the joystick values to be precise all the 
  385. way to the least significant bit.  This is probably caused by vibration of the 
  386. joystick (or of the hand of the one holding it) and by the simple way in which 
  387. the joystick converts the potentiometer output to timing outputs (not by 
  388. anything fancy like a timing chip with an analog-to-digital converter, unless 
  389. any of the really nice joysticks do this, but probably with a simple capacitor 
  390. circuit of some sort).
  391.  
  392. 4.2  Calibration
  393.  
  394.    How do we solve this problem?  Obviously the joystick cannot be used for 
  395. very precise control (if you need very accurate and precise control like for a 
  396. drawing program, the mouse would be a better input device).  However, a way to 
  397. resolve the differences in coordinate ranges is to have a joystick 
  398. calibration routine somewhere in your program.
  399.    If you have played games that use the joystick (and the joystick isn't 
  400. used for much other than games), surely you have seen calibration screens.  
  401. They are usually in the configuration menus of the game (or the game does its 
  402. joystick calibration every time you start the game, but that's a pain for the 
  403. user because that has to be done every time the game is started).  Calibration 
  404. is when the game finds out what normal x and y coordinates are for different 
  405. positions on your joystick.  That way, during the game, it can know what sort 
  406. of values to interpret as what.  
  407.  
  408. 4.2.1  How to calibrate - the not-so-good way
  409.  
  410.    One way to calibrate the joystick is to ask the user to center the joystick 
  411. and press the button.  When the button press is detected, the program reads 
  412. the coordinates of the joystick.  Now the program will know that if the 
  413. coordinates of the joystick are ever close to this value, the joystick can be 
  414. regarded as being in the center.  This method doesn't require much user 
  415. intervention; as a matter of fact, one game assumes that the joystick is 
  416. centered when you start the game because it just reads the joystick 
  417. coordinates upon startup.  However, this method of calibration isn't very good 
  418. because it can't figure out what good coordinates for left, right, up, or down 
  419. would be.
  420.  
  421. 4.2.2  How to calibrate - a better way
  422.  
  423.    A better way to calibrate asks the user to move the joystick to its upper-
  424. left hand position and press the joystick button.  When the user presses the 
  425. joystick button, the program reads the joystick coordinates and assumes that 
  426. they are good minimum x and y values.  Then the program asks the user to move 
  427. the joystick to its lower-right hand position and press the button.  When the 
  428. user presses the joystick button, the program reads the joystick coordinates 
  429. and assumes that they are good maximum x and y values.
  430.    Some games that calibrate the joystick this way ask the user to press 
  431. button 1 when the joystick is in its upper-left hand corner, and button 2 when 
  432. the joystick is in its lower-right hand corner.  This might be handy in order 
  433. to make sure that the user knows which button is what, and in order to make 
  434. sure that both buttons are responding.
  435.  
  436. 4.2.3  How to calibrate - digital type joysticks
  437.  
  438.    Some games don't take advantage of the fact that the joystick is analog as 
  439. opposed to digital.  If the joystick is to the right, the character runs 
  440. right, but always at the same speed, regardless of how far to the right the 
  441. joystick is; if the joystick is to the left, the character runs left at the 
  442. same speed; if the joystick is near the center, the character doesn't move at 
  443. all.  If your application that uses the joystick is going to do this, a better 
  444. way to calibrate might be to ask the user to move the joystick to the upper-
  445. left hand corner, press the button, move the joystick to the lower-right hand 
  446. corner, press the button, center the joystick, and press the button.  This 
  447. way, you'll have a more accurate determination of which direction is the 
  448. center.  (You shouldn't do this if you're going to use the joystick as an 
  449. analog joystick, however; this would make it very hard to interpret joystick 
  450. coordinates in that case.)
  451.  
  452. 4.2.4  When things go wrong...
  453.  
  454.    If the maximum x value from calibration turns out to be less than or equal 
  455. to the minimum x value, or the maximum y value is less than or equal to the 
  456. minimum y value, then something went wrong (either something's wrong with the 
  457. joystick or the port, or the user didn't follow the calibration instructions 
  458. correctly!)  In this case, you should just give some sort of error message 
  459. saying that calibration failed. 
  460.  
  461. 4.2.5  Recalibration
  462.  
  463.    Remember that the joystick values might float around even in the middle of 
  464. a program.  You might calibrate the joystick at the beginning of a game, and 
  465. ten minutes into the game, you let go of the joystick and let it go to the 
  466. center position, and the character on the screen moves left.  The joystick 
  467. requires constant recalibration which can be a real pain.  If you're writing a 
  468. game that uses the joystick, however, let there be some way by which the user 
  469. can recalibrate the joystick in the middle of the game, without having to 
  470. start the game over. 
  471.  
  472. 4.2.6  The calibration knobs on the joystick
  473.  
  474.    The calibration knobs on the joystick modify the joystick values to a 
  475. certain extent.  When the joystick knobs are adjusted so that the center of 
  476. the joystick has coordinates that are near the maximum or minimum x or y 
  477. coordinates, the joystick cannot detect as many positions because joystick 
  478. positions that go off the edge tend to be "clipped."  Therefore, it might be a 
  479. good idea to ask the user to also adjust the calibration knobs on the 
  480. joystick, and you might give some sort of pixel display which shows whatever 
  481. values the joystick is currently returning so that the user can adjust the 
  482. joystick to give x and y coordinates for the center that are as near to the 
  483. center of the pixel display as possible.
  484.  
  485. 4.3  Interpreting the values
  486.  
  487. 4.3.1  Analog
  488.  
  489.    Now that your program knows what minimum and maximum x and y coordinates 
  490. are from calibration, whenever the joystick is read during the program, how 
  491. does it know what to interpret the values as?  Here you're going to be using 
  492. some linear interpolation.  If the x value is halfway between the minimum and 
  493. maximum x values, and the y value is halfway between the minimum and maximum 
  494. y values, you can assume that the joystick is centered.  (Don't assume that 
  495. the joystick is exactly halfway between, though; leave a certain range of x 
  496. and y values which are centered.)  Likewise, if the x value is 3/4 of the way 
  497. from the minimum x to the maximum x, the joystick is 3/4 of the way to the 
  498. right.  If any of the coordinates turn out to be more than the maxima, or less 
  499. than the minima, then just assume them to be equal to the maximum or minimum 
  500. coordinates. 
  501.  
  502. 4.3.2  Digital
  503.  
  504.    If your program is using the joystick as a digital joystick, then your 
  505. program is probably just going to have exact cutoff x and y coordinates for 
  506. determining which position the joystick is in.  Here's an illustration (ASCII 
  507. doesn't make it very easy to do this...)
  508.  
  509.    +--------+--------+--------+ <- minimum y value
  510.    |        |        |        |
  511.    |   1    |   2    |   3    |
  512.    |        |        |        |
  513.    +--------+--------+--------+ <- 1/3 of the way from the min y to the max y
  514.    |        |        |        |
  515.    |   4    |   5    |   6    |
  516.    |        |        |        |
  517.    +--------+--------+--------+ <- 2/3 of the way from the min y to the max y
  518.    |        |        |        |
  519.    |   7    |   8    |   9    |                         1=upper-left
  520.    |        |        |        |                         2=up
  521.    +--------+--------+--------+ <- maximum y value      3=upper-right
  522.             |        |                                  4=left
  523.    |        |  2/3 from min   |                         5=centered
  524. minimum x   |  y to max y     maximum x value           6=right
  525.   value     |                                           7=lower-left
  526.             |                                           8=down
  527. 1/3 from min x to max y                                 9=lower-right
  528.  
  529.    Of course, the fractions could be anything you wanted them to be.
  530.    If you had calibrated the joystick the way described in 4.2.3, it might be 
  531. more like this:
  532.  
  533.    +--------+--------+--------+ <- minimum y value
  534.    |        |        |        |
  535.    |   1    |   2    |   3    |
  536.    |        |        |        |
  537.    +--------+--------+--------+ <- 2/3 from minimum y to center y
  538.    |        |        |        |
  539.    |   6    |   5    |   4    | <- center y value
  540.    |        |        |        |
  541.    +--------+--------+--------+ <- 1/3 from center y to maximum y
  542.    |        |        |        |
  543.    |   7    |   8    |   9    |
  544.    |        |        |        |
  545.    +--------+--------+--------+ <- maximum y value
  546.    |       /    |     \       |
  547. minimum x  | center x |     maximum x
  548.            |          |
  549. 2/3 from min x     1/3 from center x
  550. to center x        to max x
  551.  
  552.    Again, of course, the fractions can be whatever you want them to be!
  553.  
  554. ------------------------------------------------------------------------------
  555.  
  556. 5.0  GRAVIS GAMEPAD
  557.  
  558. 5.1  Description
  559.  
  560.    The Gravis GamePad is a digital controller that connects to the regular 
  561. joystick port.  It looks similar to the Super Nintendo pad.  It has four 
  562. buttons, which can be used either as four buttons or as two normal buttons and 
  563. two turbo buttons.  Whenever you press a "turbo" button, it's like pressing 
  564. the normal button that it corresponds to except that a timer of some sort in 
  565. the joystick rapidly turns the signal for the button on and off, making rapid-
  566. fire in a game easy.  A switch on the GamePad can make it suitable for either 
  567. left-handed or right-handed play; you can hold the joystick the other way, 
  568. and the joystick will invert which pad direction is which and which button is 
  569. which.  Note that both the rapid-fire and the left/right-handed switching of 
  570. the joystick controls are invisible to the program; the software does not (and 
  571. can not) find out which modes the GamePad is in, and it doesn't need to invert 
  572. its idea of which button is which or whatever if the joystick is held the 
  573. other way because the joystick does all that.  If a turbo button is pressed, 
  574. it only appears to the software as a joystick button being turned on and off.
  575.  
  576. 5.2  Digital joystick
  577.  
  578.    A Gravis GamePad is not very suitable for playing games which use the 
  579. joystick as an analog joystick, because there is only one up position, only 
  580. one left position, and so on.  The GamePad can only detect nine positions of 
  581. the pad--the center, up, down, left, right, and four intermediate positions.  
  582. Probably, the values on a Gravis GamePad don't float around very much, but I'm 
  583. not sure about this because I don't have a Gravis GamePad to try this with.
  584.  
  585. 5.3  Programming the Gravis GamePad
  586.  
  587. 5.3.1  Buttons
  588.  
  589.    When the Gravis GamePad is in four-button mode, the third and fourth 
  590. buttons appear to be the first and second buttons of joystick B.  This means 
  591. that if you read from I/O port 201h to get the button status (or call the ROM 
  592. BIOS function to do this), bits 4 through 7 will give the status of GamePad 
  593. buttons 1 through 4 in that order (1 means the button is not pressed, 0 means 
  594. the button is pressed).  If you connect more than one Gravis GamePad to the 
  595. computer at a time, buttons 3 and 4 of each joystick don't do anything for 
  596. this reason, since there aren't enough bits for all the buttons.  (At least, 
  597. that's what I think happens; unless both buttons 3 and 4 on joystick A and 
  598. buttons 1 and 2 on joystick B trigger the same bits, but this would depend on 
  599. the Y-connector that you use to connect the two GamePads to the game port.)  
  600. In two-button mode, buttons 3 and 4 act like turbo buttons for buttons 1 and 
  601. 2, but when a turbo button is pressed, it only appears to the program that the 
  602. button is being pressed and released over and over.  Turbo doesn't mean 
  603. anything special from the program's point of view.
  604.    
  605. 5.3.2  The pad itself
  606.  
  607.    Since the pad is digital and can only detect nine positions, and there are 
  608. no calibration knobs on the Gravis GamePad, the values that you get from 
  609. reading it probably don't "float around" very much.  (I'm not sure about this 
  610. because I don't have a Gravis GamePad to test this on; if you have one, please 
  611. let me know about the typical values that you get for each of the nine pad 
  612. positions.)  If your program is going to use the joystick as a digital 
  613. joystick, it probably doesn't even have to know whether or not there is a 
  614. Gravis GamePad (unless you plan to use buttons 3 and 4).  After all, it 
  615. returns values that are similar to those values that a regular joystick would 
  616. return in those nine positions, so if your program is going to use the 
  617. joystick as a digital joystick, after calibration it can treat all joysticks 
  618. the same way.
  619.  
  620. ------------------------------------------------------------------------------
  621.  
  622. 6.0  PADDLES
  623.  
  624. 6.1  Description
  625.  
  626.    A paddle is another type of controller that can be connected to the game 
  627. port.  It is quite different from an analog or digital joystick, in that, 
  628. while joysticks have two axes (x and y), the paddle only has one axis.  A 
  629. paddle consists of a knob which you turn left and right, and one button.  
  630. Paddles are suitable for games in which you are only going to move something 
  631. on the screen left and right (or up and down), such as Pong-type games in 
  632. which you move something left and right to catch a ball and bounce it back at 
  633. the other player's paddle on the screen.  The paddles that you connect to the 
  634. game port are probably named such because of table-tennis paddles.  Up to four 
  635. paddles can be connected to the game port at a time.
  636.  
  637. 6.2  Programming
  638.  
  639. 6.2.1  Getting the button status and the coordinates
  640.  
  641.    When you read from I/O port 201h to get the status of the paddles, the bits 
  642. will have the following definitions:
  643.  
  644.         Bit     Description
  645.          0      Paddle A coordinate
  646.          1      Paddle B coordinate
  647.          2      Paddle C coordinate
  648.          3      Paddle D coordinate
  649.          4      Status of paddle A button (1=not pressed, 0=pressed)
  650.          5      Status of paddle B button (ditto)
  651.          6      Status of paddle C button (ditto)
  652.          7      Status of paddle D button (ditto)
  653.    
  654.    Of course, you can also call the ROM BIOS functions to get the status of 
  655. the paddles.  The bits for the buttons will be the same as above, and for the 
  656. function which gives you the joystick coordinates, instead of getting the 
  657. joystick A and B x and y coordinates in AX, BX, CX, and DX, you will get the 
  658. coordinates of the four paddles (with a 0 coordinate for a paddle which isn't 
  659. connected or isn't responding).  
  660.    Getting the coordinates of the paddles is very much the same as getting the 
  661. coordinates of the joysticks.  You fire the one-shots by writing any 8-bit 
  662. value to port 201h, and then you time how long it takes for the 1 bits in bits 
  663. 0-3 to change to 0 (the bit for a paddle which isn't connected is undefined, 
  664. but will probably be 0 and stay 0 or be 1 and stay 1).  The timings are 
  665. probably very much the same as for a joystick.  Of course, if you are writing 
  666. a routine to read the coordinate of just paddle A, the routine will probably 
  667. be relatively small and efficient because it only has to look at one bit, as 
  668. opposed to the routine for reading the joystick coordinates which has to time 
  669. at least 2 bits.
  670.  
  671. 6.2.2  Interpreting the coordinates
  672.  
  673.    Since I don't have a paddle (and I don't know anyone who has one; I only 
  674. know from my reference that they exist), I wouldn't know this for sure, but it 
  675. is most likely that when the paddle is in its rightmost (most clockwise) 
  676. position, the coordinate of that paddle is the highest, and that when the 
  677. paddle is in its leftmost (most counter-clockwise) position, the coordinate 
  678. of that paddle is the lowest.
  679.    I don't know whether or not paddles have calibration knobs, or whether or 
  680. not the lower and upper bounds of the coordinates are very well defined, but 
  681. since I don't know what typical coordinates are, or whether or not coordinates 
  682. are very constant across paddles (probably not), you probably need calibration 
  683. of some sort.  Probably the program would ask the user to put the paddle in 
  684. its leftmost position and press the button, and then it would read the paddle 
  685. coordinate; then it would ask the user to put the paddle in its rightmost 
  686. position and press the button, and then it would read the paddle coordinate 
  687. again.  Calibration fails, of course, if the upper bound is less than or equal 
  688. to the lower bound.  The value halfway between these two values can be assumed 
  689. to be the center. 
  690.  
  691. ------------------------------------------------------------------------------
  692.  
  693. 7.0  DETECTING THE GAME PORT AND CONTROLLERS
  694.  
  695. 7.1  Detecting the game port
  696.  
  697.    How do you tell whether or not there's a game port connected to the system?  
  698. One way is to read from I/O port 201h, and if the value is 0F0h, there is a 
  699. game port connected to the system, otherwise there isn't (unless there are 
  700. joysticks connected which are giving different values; if you haven't fired
  701. the joystick one-shots recently this shouldn't be the case).  This is not 
  702. really a good way to do it because some game ports could be nonstandard and 
  703. return different values if the joysticks are not connected.  My joystick port 
  704. is this way; if no joysticks are connected and I have the computer read from 
  705. I/O port 201h, the value it gets is 0F3h.
  706.    The computer's equipment status word (which you can get by reading the word 
  707. at 0040h:0010h or by doing an int 11h which returns this word in AX) contains 
  708. a bit that tells (or is supposed to tell) whether or not there is a game card 
  709. (on machines other than PS/2's).  If this bit is 1, there is a game card; if 
  710. it's 0, there isn't.  This bit is bit 12 of the word.  Beware; bit 12 of the 
  711. equipment status word is unused (or used for something else) on PS/2's, so 
  712. this is not really a good way to check for a game port.  When the computer is 
  713. booted up, the BIOS checks to see whether or not there is a game port (on 
  714. XT's and probably on most other machines, it uses the same method described 
  715. above, which isn't really the best way), and it sets this bit accordingly.
  716.    A better way to detect whether or not there is a game port is to read the 
  717. port, and if the value is 0FFh (the value you get from any I/O port to which 
  718. no device is connected), there is no game port; otherwise there is.  Make sure 
  719. that you haven't fired the joystick one-shots recently when you do this so 
  720. that if there is a game port and joysticks are connected, 0FFh isn't the 
  721. actual value that is being returned.  There's already a C-callable function in 
  722. JOYSTICK.ASM which does this (detect_game_port).  This may not be 100% 
  723. reliable, however, so be sure to give the user a way to override whether or 
  724. not the program thinks there's a game port. 
  725.    In any case, the program will probably not need to know whether or not 
  726. there is a game port, just whether or not the controller is connected.  
  727. So it would probably just be easier to detect whether or not a controller is 
  728. connected in the first place since a game port isn't much use without 
  729. something connected to it.  
  730.  
  731. 7.2  Detecting joysticks
  732.  
  733.    There really isn't any way that the program can tell which type of 
  734. controller is connected to the game port.  The program probably has to ask the 
  735. user for this information.  If your program is going to use the joystick as a 
  736. digital joystick, it probably won't need to know whether or not there is a 
  737. Gravis GamePad connected (unless it could use buttons 3 and 4 in this case). 
  738.    However, there is a way to tell whether or not controllers are connected, 
  739. and if so, how many (assuming that you know whether the controllers are 
  740. paddles or joysticks).  Fire the one-shots by writing any value to the 8-bit 
  741. port 201h.  Then continue to read from port 201h, and if any of the lower 4
  742. bits changes values within a "reasonable amount of time" (see section 3.2.2), 
  743. the controller that it goes to is connected.  The joystick-reading procedures 
  744. in JOYSTICK.ASM can be used for detecting joysticks because they return 0FFFFh 
  745. for the coordinates of the bits in the joystick port value that never changed.
  746.    One method which you should NOT use to detect joysticks: Call the ROM BIOS 
  747. function to read the joystick values, and if a joystick coordinate that it 
  748. returns is not 0, that joystick is connected.  You shouldn't use this method.  
  749. You see, if the bits for a joystick which isn't connected are always 0, the 
  750. ROM BIOS function might realize that these bits are 0 on the first or second 
  751. run of its timing loop and assume that these bits had just turned 0 (when in 
  752. fact they were always 0), and therefore return a really low value like 1 or 2 
  753. but not 0.  On my computer, although I only have one joystick connected to the 
  754. game port, the ROM BIOS function for getting the joystick coordinates always 
  755. returns 1 or 2 for the x and y coordinates of joystick 2.  You have to 
  756. actually detect a CHANGE in a bit for a controller in order to assume that the 
  757. joystick is there.  The joystick routines in JOYSTICK.ASM look for bit 
  758. changes, instead of noticing that a bit is 0 and thinking that it had just 
  759. turned 0.  This is probably more reliable than the method for telling whether 
  760. or not a game port is connected to the machine, but you might still want to 
  761. give the user a chance to override what the program thinks. 
  762.  
  763. ------------------------------------------------------------------------------
  764.  
  765. 8.0  PINOUTS
  766.  
  767. 8.1  Introduction
  768.  
  769.    For you hardware hackers out there, (no, I'm not one), this section is 
  770. about the pinouts on the game port.  The game port is useful for other things 
  771. than connecting joysticks or other controllers; it could be used for 
  772. diagnostic purposes since reading from I/O port 201h simply gives the status 
  773. of certain pins on the game port.
  774.    Here's one thing which I need help on from someone: The list of pins on the 
  775. game connector that I have doesn't say anything about a pin which does 
  776. anything whenever the joystick one-shots are fired.  However, the joystick has 
  777. to know somehow whether or not the one-shots are fired so it can start timing 
  778. so that it knows when to turn the coordinate bits from 1 to 0.  Help!
  779.    When a joystick button is being pressed, the bit for that button is 0, and 
  780. when the button is not pressed, the bit is 1.  You would expect it to be the 
  781. other way around.  The reason is probably because the voltages that the 
  782. computer uses for signals are backwards - a higher voltage means a 0 and a 
  783. lower voltage means a 1.  (Can anyone give me precise values for the 
  784. voltages?  I would expect +5V with respect to ground to be a 0 bit and 0V to 
  785. be a 1 bit; this explains why the buttons return a 0 when pressed and 1 when 
  786. not pressed, since they are normally open buttons.  I need to test this with a 
  787. voltmeter some time...)
  788.    Remember, to the game port itself, there is no functional difference 
  789. between the pins that give the joystick position and the pins that give the 
  790. button status; the circuitry within the joystick is what converts the outputs 
  791. from the variable resistors that read the position to high and low signals 
  792. whose timings are based on them.  All the game port itself does is take the 
  793. high and low signals given to it on the pins and convert them to 0's and 1's 
  794. when you read port 201h, and fire the joystick one-shots somehow when you 
  795. write to that port.
  796.  
  797. 8.2  Pinouts
  798.  
  799.    The computer's connector is a female DB-15.  I'm not sure which pins are 
  800. which numbers, but a male DB-15 on something that you connect to the game port 
  801. would probably have the numbers of the end pins marked.
  802.                                 
  803. Pin number      What it does
  804. ----------      ------------
  805.     1           +5V DC
  806.     2           Bit 4 of I/O port 201h
  807.     3           Bit 0 of I/O port 201h
  808.     4           Ground
  809.     5           Ground
  810.     6           Bit 1 of I/O port 201h
  811.     7           Bit 5 of I/O port 201h
  812.     8           +5V DC
  813.     9           +5V DC
  814.     10          Bit 6 of I/O port 201h
  815.     11          Bit 2 of I/O port 201h
  816.     12          Ground
  817.     13          Bit 3 of I/O port 201h
  818.     14          Bit 7 of I/O port 201h
  819.     15          +5V DC
  820.  
  821.    All signals that come into the game port (the bits of the I/O port) are 
  822. "active high," whatever that means.
  823.  
  824. ------------------------------------------------------------------------------
  825.  
  826. 9.0  HOW TO CONTACT ME
  827.  
  828.    Please let me know of anything I left out (I know I'm missing some info 
  829. like the joystick one-shots on the pinouts, and definite voltages for the 
  830. pinouts [high and low] with respect to ground) and anything that's inaccurate 
  831. in this document.  Any comments or suggestions are appreciated!  I can be 
  832. contacted at: 
  833.  
  834. SnailMail: 713 N.W. 141 St., Edmond, OK, 73013-1920.
  835.  
  836. FidoNet: Usually I'm on the lookout for any messages addressed to me on the 
  837. 80XXX echo. 
  838.  
  839. InterNet: emil.gilliam@oubbs.telecom.uoknor.edu
  840.  
  841.  
  842.