home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / joyread.zip / JOYREAD.ASM < prev    next >
Assembly Source File  |  1989-09-01  |  13KB  |  477 lines

  1. ;******************************************************************************
  2. ;
  3. ;  Program:    JOYREAD.ASM
  4. ;
  5. ;  Purpose:    This code compiles to a .COM program that reads and displays
  6. ;           the status of the buttons and potentiometers on the joystick(s)
  7. ;           attached to the Game Adaptor port of an IBM-PC compatible computer.
  8. ;
  9. ;  Usage  :    This program requires MASM (or equivalent), LINK and EXE2BIN
  10. ;           to be converted to a .COM program, as follows...
  11. ;
  12. ;           masm joyread;
  13. ;           link joyread;
  14. ;           exe2bin joyread joyread.com
  15. ;           del joyread.obj
  16. ;           del joyread.exe
  17. ;
  18. ;              The final JOYREAD.COM program may be run from the DOS prompt.
  19. ;
  20. ;  Programmer: M. B. Young
  21. ;
  22. ;******************************************************************************
  23. ;
  24. ;  The Equates Corral
  25. ;
  26.       ESCAPE      equ   1Bh
  27. ;
  28.       VIDEO       equ   10h
  29. ;
  30.          SET_MODE    equ   0
  31.          SET_CRSR    equ   2
  32.          WRITE_TTY   equ   0Eh
  33.          GET_MODE    equ   0Fh
  34. ;
  35.       CONFIGURATION  equ   11h
  36. ;
  37.       IO_EXTRA    equ   15h
  38. ;
  39.          READ_JSTK   equ   84h
  40. ;
  41.       KEYBOARD    equ   16h
  42. ;
  43.          KBD_READ    equ   0
  44.          KBD_STATUS  equ   1
  45. ;
  46.       DOS_CALL    equ   21h
  47. ;
  48.          E_C_EXIT    equ   4Ch
  49. ;
  50.       GAME_PORT   equ   201h
  51.       MAX_COUNT   equ   100h
  52. ;
  53. ;******************************************************************************
  54. ;
  55. ;     Main Code
  56. ;
  57. ;******************************************************************************
  58. ;
  59. joyread_code   segment  para   'code'
  60. ;
  61.       assume cs:joyread_code, ds:joyread_code, es:joyread_code
  62.       org 100h
  63. ;
  64. begin:
  65. ;
  66.       mov ah , GET_MODE             ; save initial video mode
  67.       int VIDEO
  68.       mov old_mode , ax
  69.       mov ah , SET_MODE
  70.       mov al , 3
  71.       int VIDEO                     ; set to 80x25, colour text mode
  72. ;
  73.       mov si , offset msg1          ; print title
  74.       mov cx , msg1_len
  75.       mov dh , 1
  76.       mov dl , 22
  77.       call text_print
  78. ;
  79.       mov si , offset msg2          ; print column headings
  80.       mov cx , msg2_len
  81.       mov dh , 3
  82.       mov dl , 19
  83.       call text_print
  84. ;
  85.       mov si , offset msg3          ; print stick1, status line 1
  86.       mov cx , msg3_len
  87.       mov dh , 5
  88.       mov dl , 19
  89.       call text_print
  90. ;
  91.       mov si , offset msg4          ; stick1, status line 2
  92.       mov cx , msg4_len
  93.       mov dh , 6
  94.       mov dl , 19
  95.       call text_print
  96. ;
  97.       mov si , offset msg5          ; stick2, status line 1
  98.       mov cx , msg5_len
  99.       mov dh , 9
  100.       mov dl , 19
  101.       call text_print
  102. ;
  103.       mov si , offset msg6          ; stick2, status line 2
  104.       mov cx , msg6_len
  105.       mov dh , 10
  106.       mov dl , 19
  107.       call text_print
  108. ;
  109.       mov si , offset msg7          ; pot read method
  110.       mov cx , msg7_len
  111.       mov dh , 12
  112.       mov dl , 6
  113.       call text_print
  114. ;
  115.       mov si , offset mode2         ; default is BIOS Int 15h, Fn 84h
  116.       mov cx , mode_len
  117.       mov dh , 12
  118.       mov dl , 53
  119.       call text_print
  120. ;
  121.       mov si , offset msg8          ; quitting instructions
  122.       mov cx , msg8_len
  123.       mov dh , 16
  124.       mov dl , 30
  125.       call text_print
  126. ;
  127. main_event:
  128. ;
  129.       call buttons                  ; get and display button states
  130.       call pots                     ; get and display potentiometer values
  131.       mov ah , KBD_STATUS           ; see if user pressed a key
  132.       int KEYBOARD
  133.       jz main_event                 ; no, loop back
  134.       mov ah , KBD_READ             ; yes, see what key was pressed
  135.       int KEYBOARD
  136.       cmp al , ESCAPE
  137.       je get_going
  138.       cmp al , 'm'                  ; does user want to toggle pot read mode ?
  139.       jz toggle_mode
  140.       cmp al , 'M'                  ; just in case...
  141.       jnz main_event
  142. ;
  143. toggle_mode:
  144.       mov al , pot_mode             ; get current mode
  145.       xor al , 0FFh                 ; and flip it
  146.       mov pot_mode , al
  147.       mov si , offset mode1         ; display current mode
  148.       or al , al
  149.       jz toggle_1
  150.       mov si , offset mode2
  151. ;
  152. toggle_1:
  153.       mov cx , mode_len
  154.       mov dh , 12
  155.       mov dl , 53
  156.       call text_print
  157.       jmp main_event
  158. ;
  159. get_going:
  160.       mov ax , old_mode             ; restore initial video mode
  161.       mov ah , SET_MODE
  162.       int VIDEO
  163. ;
  164.       xor al , al                   ; set return code = 0
  165.       mov ah , E_C_EXIT
  166.       int DOS_CALL
  167. ;
  168. ;
  169. ;  Sneak in a data segment here
  170. ;
  171. ;
  172.       msg1        db    '-Joystick Status/Calibration Program-'
  173.       msg1_len    equ   $-msg1
  174.       msg2        db    '              Potentiometers    Buttons'
  175.       msg2_len    equ   $-msg2
  176.       msg3        db    'Stick1-           X:             1:'
  177.       msg3_len    equ   $-msg3
  178.       msg4        db    '                  Y:             2:'
  179.       msg4_len    equ   $-msg4
  180.       msg5        db    'Stick2-           X:             1:'
  181.       msg5_len    equ   $-msg5
  182.       msg6        db    '                  Y:             2:'
  183.       msg6_len    equ   $-msg6
  184.       msg7        db    'Press M to toggle Potentiometer read method:'
  185.       msg7_len    equ   $-msg7
  186.       msg8        db    'Press <Esc> to quit'
  187.       msg8_len    equ   $-msg8
  188.       mode1       db    'Direct Software Loop'
  189.       mode_len    equ   $-mode1
  190.       mode2       db    'BIOS Int 15h, Fn 84h'
  191. ;
  192.       string      db    '     '
  193.       is_on       db    'ON '
  194.       is_off      db    'OFF'
  195.       old_mode    dw    ?
  196.       pot_counts  db    4 dup( 0 )
  197.       pot_mode    db    0FFh
  198. ;
  199. ;
  200. ;******************************************************************************
  201. ;
  202. ;     TEXT_PRINT  -  Prints a message to the screen
  203. ;     Assumes that:  ds:[si] -> beginning of string
  204. ;                    cx = number of bytes in string
  205. ;                    dh,dl = row,col to print string at
  206. ;
  207. ;******************************************************************************
  208. ;
  209. text_print  proc  near
  210. ;
  211.       push ax
  212.       push bx
  213.       mov ah , SET_CRSR
  214.       xor bx , bx
  215.       int VIDEO
  216. tp_1:
  217.       lodsb
  218.       mov ah , WRITE_TTY
  219.       int VIDEO
  220.       loop tp_1
  221. ;
  222.       pop bx
  223.       pop ax
  224.       ret
  225. ;
  226. text_print  endp
  227. ;
  228. ;
  229. ;******************************************************************************
  230. ;
  231. ;     I_TO_A - converts the value in ax to an ASCII string
  232. ;     Assumes that:  es:[di] -> end of a string buffer long enough to hold
  233. ;                       the decimal representation of the value in ax
  234. ;
  235. ;******************************************************************************
  236. ;
  237. i_to_a   proc  near
  238. ;
  239.       push cx
  240.       push dx
  241.       mov cx , 10
  242. i2a_1:
  243.       xor dx , dx                   ; clear 'high byte'
  244.       div cx                        ; divide ax by 10
  245.       add dl , '0'                  ; convert remainder to ASCII
  246.       mov [di] , dl                 ; store in srrihg buffer
  247.       dec di
  248.       cmp ax , 0                    ; check if quotient > 0
  249.       ja i2a_1
  250. ;
  251.       pop dx
  252.       pop cx
  253.       ret
  254. ;
  255. i_to_a   endp
  256. ;
  257. ;
  258. ;******************************************************************************
  259. ;
  260. ;     BUTTONS -  Reads and displays the status of the buttons... the settings
  261. ;              are returned in bits 4-7 of al after reading the game adaptor
  262. ;              port. A high bit (1) corresponds to an open (unpressed) state.
  263. ;
  264. ;******************************************************************************
  265. ;
  266. buttons  proc  near
  267. ;
  268.       mov dx , GAME_PORT            ; read game adaptor port
  269.       in al , dx
  270.       push ax
  271. ;
  272.       mov si , offset is_off
  273.       test al , 00010000b           ; Stick 1, Button 1
  274.       jnz br_1
  275.       mov si , offset is_on
  276. br_1:
  277.       mov cx , 3
  278.       mov dh , 5
  279.       mov dl , 55
  280.       call text_print
  281. ;
  282.       pop ax
  283.       push ax
  284.       mov si , offset is_off
  285.       test al , 00100000b           ; Stick 1, Button 2
  286.       jnz br_2
  287.       mov si , offset is_on
  288. br_2:
  289.       mov cx , 3
  290.       mov dh , 6
  291.       mov dl , 55
  292.       call text_print
  293. ;
  294.       pop ax
  295.       push ax
  296.       mov si , offset is_off
  297.       test al , 01000000b           ; Stick 2, Button 1
  298.       jnz br_3
  299.       mov si , offset is_on
  300. br_3:
  301.       mov cx , 3
  302.       mov dh , 9
  303.       mov dl , 55
  304.       call text_print
  305. ;
  306.       pop ax
  307.       mov si , offset is_off
  308.       test al , 10000000b           ; Stick 2, Button 2
  309.       jnz br_4
  310.       mov si , offset is_on
  311. br_4:
  312.       mov cx , 3
  313.       mov dh , 10
  314.       mov dl , 55
  315.       call text_print
  316. ;
  317.       ret
  318. ;
  319. buttons  endp
  320. ;
  321. ;
  322. ;******************************************************************************
  323. ;
  324. ;     POTS - reads and displays the potentiometer settings
  325. ;
  326. ;******************************************************************************
  327. ;
  328. pots  proc  near
  329. ;
  330.       test pot_mode , 1             ; get current mode
  331.       jnz use_bios
  332.       mov si , offset pot_counts
  333.       call time_pots
  334.       jmp display_counts
  335. ;
  336. use_bios:
  337.       mov ah , READ_JSTK
  338.       mov dx , 1
  339.       int IO_EXTRA
  340.       mov si , offset pot_counts
  341.       mov [si] , al
  342.       mov [si+1] , bl
  343.       mov [si+2] , cl
  344.       mov [si+3] , dl
  345. ;
  346. display_counts:
  347.       cld
  348.       mov al , ' '                  ; space-out the string
  349.       mov si , offset string
  350.       mov di , si
  351.       mov cx , 4
  352. rep   stosb
  353.       dec di
  354.       xor ah , ah
  355.       mov al , byte ptr pot_counts     ; Stick 1, X-pot
  356.       call i_to_a
  357.       mov cx , 5
  358.       mov dh , 5
  359.       mov dl , 39
  360.       call text_print
  361. ;
  362.       mov al , ' '                  ; space-out the string
  363.       mov si , offset string
  364.       mov di , si
  365.       mov cx , 4
  366. rep   stosb
  367.       dec di
  368.       xor ah , ah
  369.       mov al , byte ptr pot_counts + 1 ; Stick 1, Y-pot
  370.       call i_to_a
  371.       mov cx , 5
  372.       mov dh , 6
  373.       mov dl , 39
  374.       call text_print
  375. ;
  376.       mov al , ' '                  ; space-out the string
  377.       mov si , offset string
  378.       mov di , si
  379.       mov cx , 4
  380. rep   stosb
  381.       dec di
  382.       xor ah , ah
  383.       mov al , byte ptr pot_counts + 2 ; Stick 2, X-pot
  384.       call i_to_a
  385.       mov cx , 5
  386.       mov dh , 9
  387.       mov dl , 39
  388.       call text_print
  389. ;
  390.       mov al , ' '                  ; space-out the string
  391.       mov si , offset string
  392.       mov di , si
  393.       mov cx , 4
  394. rep   stosb
  395.       dec di
  396.       xor ah , ah
  397.       mov al , byte ptr pot_counts + 3 ; Stick 2, Y-pot
  398.       call i_to_a
  399.       mov cx , 5
  400.       mov dh , 10
  401.       mov dl , 39
  402.       call text_print
  403. ;
  404.       ret
  405. ;
  406. pots  endp
  407. ;
  408. ;******************************************************************************
  409. ;
  410. ;     TIME_POTS - Reads the settings of the joystick potentiometers connected
  411. ;                 to the game adaptor. Call with ds:[si] pointing to a 4 byte
  412. ;                 array to hold the four count values
  413. ;
  414. ;******************************************************************************
  415. ;
  416. time_pots   proc  near
  417. ;
  418.       mov dx , GAME_PORT
  419.       cli                           ; disable interrupts during timing loop
  420.       out dx , al                   ; initialize timers
  421.       mov cx , MAX_COUNT            ; maximum wait value
  422.       mov bl , 00001111b            ; use bl to record timer states
  423. ;
  424. time_1:
  425.       in al , dx                    ; get timer states  (bits 0-3)
  426.       and al , bl                   ; mask out bits 4-7
  427.       cmp al , bl                   ; has states changed ?
  428.       loopz time_1                  ; no changes
  429.       jcxz time_2                   ; exceeded maximum wait time
  430.       xor al , bl                   ; save changed timer states in al
  431.       mov ah , cl                   ; and count down value in ah
  432.       push ax                       ; save on stack
  433.       dec cx                        ; account for elapsed time
  434.       xor bl , al                   ; update timer states
  435.       jnz time_1                    ; continue if any timers still high
  436.       jmp time_3
  437. ;
  438. time_2:
  439.       push bx                       ; timed out
  440. ;
  441. time_3:
  442.       sti                           ; all done reading pots, restore interrupts
  443.       mov dl , 4                    ; convert pushed values to counts
  444. ;
  445. time_4:
  446.       pop ax                        ; get count-change pair
  447.       sub ah , MAX_COUNT - 1        ; convert count down value to time
  448.       neg ah
  449.       mov cx , 4                    ; now see which pot this pair is for
  450. ;
  451. time_5:
  452.       shr al , 1                    ; set CF if low bit is 1
  453.       jnc time_6
  454.       mov [si] , ah                 ; store count value
  455.       dec dl                        ; one timer down, dl yet to do
  456. ;
  457. time_6:
  458.       inc si                        ; point to next timer save byte
  459.       loop time_5                   ; test current count-change pair again
  460.       sub si , 4                    ; point si back to start of array
  461.       or dl , dl                    ; have we got readings for all 4 pots ?
  462.       jnz time_4                    ; not yet
  463. ;
  464.       ret                           ; all done
  465. ;
  466. time_pots   endp
  467. ;
  468. ;******************************************************************************
  469. ;
  470. ;     Th-th-th-that's all folks....
  471. ;
  472. ;******************************************************************************
  473. ;
  474. joyread_code   ends
  475. ;
  476.                end begin
  477.