home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / JOYDOCS.ZIP / design.doc next >
Encoding:
Text File  |  1995-05-06  |  10.1 KB  |  177 lines

  1. The Advanced OS/2 Joystick Device Driver Design Overview Document
  2.  
  3. INTRODUCTION
  4.  
  5. The IBM PC game port adapter provides a simple interface to joysticks and
  6. other similar analog devices.  The status of up to 4 analog and 4 digital
  7. signals is read through I/O port address 201h.  Two analog and two digital
  8. signals are commonly grouped together in the form of a dual axis joystick
  9. with two "fire" buttons.  Some modern joysticks use two axes for movement
  10. and an additional axis for throttle.
  11.  
  12. Each analog and digital signal is assigned one bit in the game adapter port.
  13. A low (binary zero) value for a bit connected to a joystick button is
  14. returned when that button is depressed.  Bits representing analog signals
  15. are implemented as one-shot outputs.  All one-shots are fired when the I/O
  16. port is written to and remain high until an RC timing circuit decays.  The
  17. variable position of each joystick axis provides the neccessary resistance
  18. for this delay circuit.  If a joystick axis is not connected, the infinite
  19. resistance across that signal holds the one-shot high indefinitely.
  20.  
  21. Determining the position of each joystick axis is done by measuring the
  22. duration of the high state for each one-shot.  Two methods are commonly
  23. used to measure this duration: counting loop iterations and reading the
  24. hardware timer.  Although the iterative method is dependant on the speed
  25. of the processor, with proper calibration of each axis this limitation
  26. becomes irrelevant.  This is the method chosen to sample the game port
  27. adapter in this driver.
  28.  
  29. The execution of a DOS application inside an OS/2 VDM is controlled by the
  30. operating system.  As just one of many tasks being handled by the scheduler,
  31. the DOS application can be switched away from at any moment to be returned
  32. to that point at some later time.  If this occurs when the joysticks are
  33. being read by either of the above two methods, the result is catastrophic.
  34. In the case of the iterative method, the suspending of execution causes the
  35. sampling procedure to terminate early and return low values for all axes.
  36. On the other hand, the delay introduced by the suspension of execution has
  37. the opposite effect on the timed sampling method in that the value read from
  38. the timer chip is incorrectly high.  In either case, the effect the user
  39. sees is that of the joystick "flickering" from the correct position making
  40. the device unusable.
  41.  
  42. One of the primary functions of this driver is to provide a remedy to this
  43. problem for DOS applications using the iterative sampling method.  Not only
  44. is this method of reading the port substantially more common, but providing
  45. a fix is also much more feasible (selectively returning fabricated values
  46. from an emulated timer chip could have other more dramatic side effects
  47. making this procedure inadvisable).  The solution is as simple as returning
  48. high-values to the DOS application for each of the one-shot outputs until
  49. the correct count has been reached.  The counts are determined as part of
  50. the normal sampling procedure inside the real-time clock timer handler.
  51.  
  52. Given that OS/2 does not provide an API for reading joysticks, the obvious
  53. other function of this driver is to define and implement an interface.  The
  54. generic IOCTL interface was chosen as the method of communication with the
  55. driver for its ability to transfer specific packets of information to/from
  56. the application.  Since this component of the driver is simpler than the
  57. "flicker" reduction component while also supplying a large part of its
  58. functionality, it will be discussed in detail first.
  59.  
  60.  
  61. OS/2 API
  62.  
  63. Defining and implementing an API for OS/2 applications is one of the two
  64. primary functions of this driver.  After opening the device, applications
  65. communicate with the driver via the OS/2 device IOCTL interface.  Functions
  66. are provided for getting and setting device parameters, reading the state
  67. of the devices connected, and calibrating those devices.
  68.  
  69. The driver samples the game port adapter inside the real-time clock timer
  70. handler.  This handler is registered with the OS at device initialization
  71. and is called every 32 milliseconds thereafter.  It determines if the game
  72. adapter port requires sampling based on client demand and the current
  73. sampling rate.  Client demand for the sampling procedure only exists if an
  74. OS/2 app has the device open or if a VDM is emulating the port (see
  75. "FLICKER" REDUCTION section).
  76.  
  77. The joysticks connected are determined and calibrated as part of the
  78. device initialization.  These parameters can be modified by an OS/2 app
  79. or refreshed through a calibration IOCTL request.
  80.  
  81. In addition to the standard device read function, functions are also defined
  82. for returning the state of the device connected at the next button press or
  83. at the next timed read.  These functions block the calling process until the
  84. given event occurs when the process is run from inside the timer handler.
  85.  
  86.  
  87. "FLICKER" REDUCTION
  88.  
  89. As explained earlier, the pre-emptive multi-tasking nature of OS/2 can
  90. cause DOS applications reading the game adapter port to deduce incorrect
  91. positions for the joysticks connected.  The solution implemented in this
  92. driver to this problem is to regularly sample the game adapter port inside
  93. the Physical Device Driver (PDD) and then return fabricated values to the
  94. DOS app that reflect that state.  With the logic for regularly sampling
  95. the port already in the PDD, all that must be done in that regard is to
  96. activate that procedure.
  97.  
  98. Before any of this can be initiated, the first thing that must be done is
  99. to trap any access to the game port by the DOS application.  Any time I/O
  100. ports are to be trapped or emulated, an OS/2 Virtual Device Driver (VDD) is
  101. required.  The VDD works in conjunction with the PDD to provide the device
  102. emulation by capturing port reads and returning values that reflect the
  103. current state as determined by the PDD.  The VDD is also responsible for
  104. determining if a given VDM is to have direct access to the port and if so,
  105. to obtain owership of it.
  106.  
  107. At device initialization, the VDD first opens the PDD and then retrieves
  108. a pointer to the data area which will be used to store the current state
  109. of the device.  The next job for the VDD comes when a DOS session is started
  110. at which time it hooks the I/O port for the game adapter.  Knowing that
  111. the device must be written to by the CPU to fire the one-shots before the
  112. read sequence can begin, the hook handler for the write operation is the
  113. function where device ownership or emulation is resolved.
  114.  
  115. If the "GAME_DIRECT_ACCESS" DOS property is "ON" for that session, this
  116. specifies that the device is to be directly accessed by the VDM.  In this
  117. case, the VDD must first obtain ownership of the device before allowing
  118. this operation to proceed.  Device ownership is one of the more complex
  119. issues handled by this driver.  It first involves getting permission from
  120. the PDD which can only be granted if the device is presently unused either
  121. by an OS/2 app or by another VDM through the emulation system.  If not
  122. available, the user is given the choice to to kill the session, wait for the
  123. device to become free, or ignore that error and use the device in emulation
  124. mode.  If the user chooses to wait for the device to become free, the DOS
  125. session process will be blocked on an event semaphore to be posted when
  126. the device is available.
  127.  
  128. Once the VDD is granted ownership of the device by the PDD, it then must
  129. arbitrate final ownership amongst all VDMs competing for it.  A mutual
  130. exclusion (MUTEX) semaphore is used as the final decision maker in this
  131. process and the VDM which is granted ownership of it is given the same
  132. status with game adapter port.  When the DOS application which owns the
  133. device terminates, this MUTEX is released allowing any other VDMs waiting
  134. for it to compete for ownership once again.  If no VDMs are waiting, the
  135. ownership of the device is returned to none through a call to the PDD.
  136.  
  137. When the "GAME_DIRECT_ACCESS" property for a VDM is in its default state
  138. of "OFF" and the game adapter port is written to, the emulation system
  139. is activated.  First the VDD informs the PDD that it wishes to become a
  140. client of the sampling procedure.  If no other clients are already active,
  141. this request forces an initial sampling so that valid data is immediately
  142. available to the VDD.  Next the VDD copies the game port data from the
  143. PDD space to its own VDM instance data area using the pointer retrieved
  144. at initialization.  This copy of the data will be used when the DOS app
  145. reads the game port.
  146.  
  147. Next, the emulation system uses the other DOS property registered at
  148. initialization: "GAME_DIGITAL_RESPONSE".  If set to "ON", the joysticks
  149. connected are emulated as fixed resistance style joysticks such as the
  150. Gravis GamePad (*).  In this case, the values for each axis are selected
  151. from a field in the PDD data area which corresponds to the range the actual
  152. value is presently within.  That is, if the current value for an axis is
  153. less than the upper limit for the lower range, a given fixed value for the
  154. lower range is chosen.
  155.  
  156. With the given value for each axis in hand, the VDD is ready to emulate
  157. the game port adapter when read by the DOS application.  First the count
  158. on the number of reads is zeroed as the last operation in the port write
  159. handler.  When the port read handler is called, the value for each axis
  160. is compared against the number of reads by this VDM and if greater, a one
  161. for that bit is generated.  The state of each position bit mirrors the
  162. value returned by the actual hardware when the PDD last sampled it on that
  163. iteration.  The bits for each of the buttons is retrieved from the PDD
  164. data area and used in generating the byte returned.  This information
  165. is transferred on each read to maintain a "freshness" in the button data.
  166.  
  167.  
  168. FUTURE DIRECTIONS
  169.  
  170. Future directions for this driver may include a high-performance interface
  171. for DOS applications.  Not only would this remove the inefficient sampling
  172. loop making the DOS app run faster, but it could also be used to implement
  173. a more effective multi-tasking system by suspending the task altogether.
  174.  
  175.  
  176. (*) Registered trademark of Advanced Gravis Technologies
  177.