home *** CD-ROM | disk | FTP | other *** search
- joystklo.se Low Level Joystick Routines based on JOY.ARC
- Preliminary Structured English
-
- MODULES:
- Low Level /* this module */
-
- 1: detect : presence through bios or port
- 2: get_valid_axes : return mask of valid axes
- 3: get_active_axes : return mask of active axes
- 4: set_active_axes : set mask of active axes
- 5 button_state ; current button state
- 6: button_change : returns bitmask of button changes
- 7: axis_position : gets position of given axis
-
- Middle Level /* joystkmd.se */
-
- 8: initialize : detect & inititalize joystick globals
- 9: axis_position_change : returns changes in active axes
- 10: real_coords_to_rel : translates axis pos relative to center
- 11: get_center_coords : gets center position of joystick
- 12: set_center_coords : grabs current position as new center
- 13: get_min_coords ; gets minimum position of joystick
- 14: set_min_coords ; grabs current position as new minimum
- 15: get_max_coords ; gets maximum position of joystick
- 16: set_max_coords ; grabs current position as new maximum
- 17: get_tolerance : get tolerance of change to ignore
- 18: set_tolerance : set tolerance of change to ignore
-
- High Level /* do it yo-sef */
-
- 19: create : grab interrupt 1Ch & set up queue
- 20: destroy : release interrupt 1Ch & queue
- 21: get_joystick_event : get event from queue
- 22: push_joystick_event : push event into queue
- 23: interrupt_handler : timer interrupt 1Ch polling routine
-
- PURPOSE:
- To create an event compatible set of routines to read input from
- the game port. (particularly geared to joysticks).
- Based on the file c_joy.c written by Gary Blaine,
- CIS 72707,1736 March 4, 1990 which is available in the
- Borland C Programming Forum on Compuserve in the file JOY.ARC.
- Use assembly for time-critical operations.
-
- GLOBAL DEFINES:
- ; return values for button_change and button state
-
- JA1_BUTTON_DOWN 16 ; joystick A, button 1, down
- JA1_BUTTON_UP 1 ; joystick A, button 1, up
- JA2_BUTTON_DOWN 32 ; joystick A, button 2, down
- JA2_BUTTON_UP 2 ; joystick A, button 2, up
-
- JB1_BUTTON_DOWN 64 ; joystick B, button 1, down
- JB1_BUTTON_UP 4 ; joystick B, button 1, up
- JB2_BUTTON_DOWN 128 ; joystick B, button 2, down
- JB2_BUTTON_UP 8 ; joystick B, button 2, up
-
- J_ANY_BUTTON_DOWN 0xf0 ; any button down: either stick
-
- J_A_BUTTON_DOWN 0x30 ; any button down: joystick A
- J_B_BUTTON_DOWN 0xC0 ; any button down: joystick B
-
- J_BUTTON_1_DOWN 0x50 ; button 1 down: either joystick
- J_BUTTON_2_DOWN 0xA0 ; button 2 down: either joystick
-
- ; parameters for axis_position and set_active_axes
-
- JAX_AXIS 1 ; x axis mask for joystick A
- JAY_AXIS 2 ; y axis mask for joystick A
- JBX_AXIS 4 ; x axis mask for joystick B
- JBY_AXIS 8 ; y axis mask for joystick B
-
- ; parameters for set_active_axes
-
- JOYSTICK_A 3 ; axis mask for joystick A
- JOYSTICK_B 0x0C ; axis mask for joystick B
-
- GLOBAL VARIABLES:
- G_Stick_Last_Button: bit image of last button state
-
- G_Stick_Active_Axes: bit mask of currently active axes
- G_Stick_Valid_Axes: bit mask of valid axes
-
- /*---------------------------------------------------------------------*/
- get_valid_axes
- BEGIN
- return G_Stick_Valid_Axes
- END
- /*---------------------------------------------------------------------------*/
- get_active_axes
- BEGIN
- return G_Stick_Active_Axes
- END
- /*---------------------------------------------------------------------------*/
- set_active_axes( new_mask )
- parameters: new_mask
- variables: check_val
- BEGIN
- set check_val to new_mask AND G_Stick_Valid_Axes
- if check_val != new_mask
- return error
- set G_Stick_Active_Axes to new_mask
- return success
- END
- /*---------------------------------------------------------------------*/
- detect
- globals:G_Stick_Active_Axes, G_Stick_Valid_Axes
- BEGIN
- trigger game adapter one shot timers
- wait for timers to flip back
- read game adapter into game_value1
- trigger game adapter one shot timers
- read game adapter into game_value2
- valid_axes = bits differing between game_value1 & game_value2 (XOR)
- G_Stick_Active_Axes = valid_axes
- G_Stick_Valid_Axes = valid_axes
- return valid_axes
- END
-
- /*---------------------------------------------------------------------*/
- button_state
- registers : current_state, changed_bit_mask, button_downs, button_ups
- changed_event_bitfield, last_button
- BEGIN
- read game port
- AND current_state with 0xf0 to lose lower nibble
- save current_state to G_Stick_Last_Button
- XOR current_state with 0xf0 to flip bits
- save current_state temporarily
- XOR current_state with 0xf0 to get button_ups
- SHIFT button_ups right 4 bits into lower nibble
- OR button_downs with button_ups to get button_state
- return button_state
- END button_state
-
-
- /*---------------------------------------------------------------------*/
- button_change
- globals : G_Stick_Last_Button
- registers : current_state, changed_bit_mask, button_downs, button_ups
- changed_event_bitfield, last_button
- BEGIN
- read game port
- loose lower nibble
- save current_state temporarily
- XOR current_state with G_Stick_Last_Button to get changed_bit_mask
- if changed_bit_mask is NULL,
- return NULL
- else
- save G_Stick_Last_Button to last_button
- save current_state to G_Stick_Last_Button
- AND G_Stick_Last_Button with changed_bit_mask to get button_downs
- XOR button_downs with changed_bit_mask to get button_ups
- SHIFT button_ups right 4 bits into lower nibble
- OR button_downs with button_ups to get changed_event_bitfield
- return changed_event_bitfield
- END button_change
-
- /*---------------------------------------------------------------------*/
- axis_position
- parameters: axis_mask
- registers: counter, game_value, start_time, end_time, result
- defines: time_out
- BEGIN
- get currently active axes from G_Stick_Active_Axes
- if given axis is not active (valid)
- return with error
- disable interrupts
- latch 8253 timer 0
- read start_time from 8253
- trigger game adapter
- do
- read game adapter into game_value
- if game_value AND axis_mask == 0 /* adapter bit has fired */
- end while loop
- increment the counter
- while counter != time_out
- if counter == time_out
- enable interrupts
- return NULL
- latch 8253 timer 0
- read end_time from 8253
- enable interrupts
-
- if start_time >= end_time
- result = start_time - end_time
- else
- result = FFFFh - end_time + start_time
- result = result AND 1FF0h
- result = result >> 4
-
- do
- read game adapter into game_value
- if game_value AND Fh == 0 /* all adapter bits have fired */
- end while loop
- increment the counter
- while counter != time_out
- return result
- END axis_position
-
- /*-- end joystklo.se ------------------*/
-