home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / pwrglvio.asm < prev    next >
Assembly Source File  |  1996-03-19  |  6KB  |  332 lines

  1.     TITLE    GLOVEDEL - Powerglove timing and i/o support
  2.     NAME    GLOVEDEL
  3.  
  4.     ; SPECIAL VERSION FOR COMBO GLOVE/SEGA DRIVER PORTS
  5.     ; rewritten 20/12/93
  6.  
  7.     COMMENT    $
  8.  
  9. /*
  10.  This code is part of the VR-386 project, created by Dave Stampe.
  11.  VR-386 is a desendent of REND386, created by Dave Stampe and
  12.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  13.  Stampre for VR-386.
  14.  
  15.  Copyright (c) 1994 by Dave Stampe:
  16.  May be freely used to write software for release into the public domain
  17.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  18.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  19.  this software or source code into their products!  Usually there is no
  20.  charge for under 50-100 items for low-cost or shareware products, and terms
  21.  are reasonable.  Any royalties are used for development, so equipment is
  22.  often acceptable payment.
  23.  
  24.  ATTRIBUTION:  If you use any part of this source code or the libraries
  25.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  26.  and any other authors in your documentation, source code, and at startup
  27.  of your program.  Let's keep the freeware ball rolling!
  28.  
  29.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  30.  REND386, improving programmer access by rewriting the code and supplying
  31.  a standard API.  If you write improvements, add new functions rather
  32.  than rewriting current functions.  This will make it possible to
  33.  include you improved code in the next API release.  YOU can help advance
  34.  VR-386.  Comments on the API are welcome.
  35.  
  36.  CONTACT: dstampe@psych.toronto.edu
  37. */
  38.  
  39. Algorithms, assembler by Dave Stampe, 22/12/93
  40.  
  41.         $
  42.  
  43.         .MODEL large
  44.         .386
  45.  
  46.         .DATA
  47.  
  48. extrn   _counts_per_tick      ; MUST contain value programmed into timer
  49.  
  50. EXTRN    _glove_in_port        ; GLOVE INTERFACE SPECS
  51. EXTRN    _glove_out_port         ; SET UP IN C CODE
  52.  
  53. EXTRN    _glove_none_mask
  54. EXTRN    _glove_data_mask
  55. EXTRN    _glove_latch_mask
  56. EXTRN    _glove_clock_mask
  57. EXTRN    _glove_clock_latch
  58.  
  59. EXTRN    _glove_bit_delay
  60.  
  61. EXTRN    _port_image
  62. EXTRN     _glove_write_mask
  63.  
  64.  
  65.     .CODE devices
  66.  
  67.  
  68. READ_TIMER     MACRO    ; result will be in ax
  69.     pushf
  70.     cli
  71.     mov    al,00h            ;/* read PC timer */
  72.     out    043h,al
  73.     nop
  74.     nop
  75.     in    al,040h
  76.     mov    ah,al
  77.     nop
  78.     nop
  79.     in    al,040h
  80.     xchg    al,ah
  81.     popf
  82.     ENDM
  83.  
  84. WRITE_TIMER     MACRO    ; write from BX
  85.     pushf
  86.     cli
  87.     mov    al,34h
  88.     out    43h,al    ; prepare for write
  89.     xor    al,al
  90.     nop
  91.     mov    al,bl
  92.     out    40h,al
  93.     nop
  94.     mov    al,bh
  95.     out    40h,al
  96.     popf
  97.     ENDM
  98.  
  99.  
  100.  
  101. ; bit delay macro for byte read, line toggle
  102.  
  103. bitdelay macro
  104.     local _localloop
  105.     mov    cx,_glove_bit_delay
  106. _localloop:
  107.     in    al,41h
  108.     loop    _localloop
  109. endm
  110.  
  111.  
  112.  
  113.  ;
  114.  ; int timed_glove_delay(int count);  /* returns time in 1.1925 MHZ ticks */
  115.  ;                                    /* to perform <count> delay steps   */
  116.  
  117.         PUBLIC    _timed_glove_delay
  118.  
  119. count equ [bp+6]
  120.  
  121. _timed_glove_delay    proc    far
  122.  
  123.     push    bp
  124.     mov    bp,sp
  125.     push    cx
  126.     push    dx
  127.     pushf
  128.  
  129.     mov    bx,0        ; reset timer for no ovf
  130.     WRITE_TIMER             ; this changes timing by factor of 2?
  131.  
  132.     cli
  133.     READ_TIMER
  134.     mov    bx,ax
  135.  
  136.     mov    cx,count
  137. caloop: in      al,40h        ; do delay
  138.     loop    caloop
  139.  
  140.     READ_TIMER
  141.     sub    ax,bx           ; assumes default, count rollover at 65536
  142.     neg    ax
  143.  
  144.     push    ax
  145.     mov    bx,_counts_per_tick    ; restore timer
  146.     WRITE_TIMER
  147.     pop    ax
  148.  
  149.     popf
  150.     pop    dx
  151.     pop    cx
  152.     mov    sp,bp
  153.     pop    bp
  154.     ret
  155.  
  156. _timed_glove_delay    endp
  157.  
  158.  
  159.  ;
  160.  ; int glove_delay(int count);    /* performs <count> delay steps   */
  161.  ;
  162.  
  163.         PUBLIC    _glove_delay
  164.  
  165. count equ [bp+6]
  166.  
  167. _glove_delay    proc    far
  168.  
  169.     push    bp
  170.     mov    bp,sp
  171.     push    cx
  172.     push    dx
  173.     mov    cx,count
  174.  
  175. gdel:    in    al,41h
  176.     loop    gdel
  177.  
  178.     pop    dx
  179.     pop    cx
  180.     mov    sp,bp
  181.     pop    bp
  182.     ret
  183.  
  184. _glove_delay    endp
  185.  
  186.  
  187.  
  188.  ;
  189.  ; int get_glove_byte();    /* reads a byte from glove */
  190.  ;
  191.  
  192.         PUBLIC    _get_glove_byte
  193.  
  194. _get_glove_byte    proc    far
  195.  
  196.     push    bp
  197.     mov    bp,sp
  198.     push    si
  199.     push    cx
  200.     push    dx
  201.  
  202.     mov    dx,_glove_out_port
  203.     mov    si,_glove_in_port
  204.  
  205.     mov    ax,_glove_clock_mask    ; preset lines
  206.     pushf
  207.     cli
  208.     mov    ah,BYTE PTR _glove_write_mask
  209.     not    ah
  210.     and    ah,BYTE PTR _port_image
  211.     or    al,ah
  212.     mov    BYTE PTR _port_image,al
  213.     out    dx,al                   ; output data
  214.     popf
  215.     bitdelay
  216.  
  217.     mov    ax,_glove_clock_latch
  218.     pushf
  219.     cli
  220.     mov    ah,BYTE PTR _glove_write_mask
  221.     not    ah
  222.     and    ah,BYTE PTR _port_image
  223.     or    al,ah
  224.     mov    BYTE PTR _port_image,al
  225.     out    dx,al                   ; output data
  226.     popf
  227.     bitdelay
  228.  
  229.     mov    ax,_glove_clock_mask
  230.     pushf
  231.     cli
  232.     mov    ah,BYTE PTR _glove_write_mask
  233.     not    ah
  234.     and    ah,BYTE PTR _port_image
  235.     or    al,ah
  236.     mov    BYTE PTR _port_image,al
  237.     out    dx,al                   ; output data
  238.     popf
  239.     bitdelay
  240.  
  241.     mov    bx,8000h                ; clear accumulator
  242.  
  243. bitloop:
  244.     xchg    si,dx                   ; add bit to accumulator
  245.     in    al,dx
  246.     xchg    si,dx
  247.     test    al,BYTE PTR _glove_data_mask
  248.     jz    z0
  249.     or    bl,bh
  250. z0:                                     ; next bit
  251.     shr    bh,1
  252.     jz    endbyte
  253.  
  254.     mov    ax,_glove_none_mask     ; pulse clock line
  255.     pushf
  256.     cli
  257.     mov    ah,BYTE PTR _glove_write_mask
  258.     not    ah
  259.     and    ah,BYTE PTR _port_image
  260.     or    al,ah
  261.     mov    BYTE PTR _port_image,al
  262.     out    dx,al                   ; output data
  263.     popf
  264.     bitdelay
  265.  
  266.     mov    ax,_glove_clock_mask
  267.     pushf
  268.     cli
  269.     mov    ah,BYTE PTR _glove_write_mask
  270.     not    ah
  271.     and    ah,BYTE PTR _port_image
  272.     or    al,ah
  273.     mov    BYTE PTR _port_image,al
  274.     out    dx,al                   ; output data
  275.     popf
  276.     bitdelay
  277.     jmp    bitloop
  278.  
  279. endbyte:
  280.     mov    ax,bx
  281.     xor    ah,ah
  282.     pop    dx
  283.     pop    cx
  284.     pop    si
  285.     mov    sp,bp
  286.     pop    bp
  287.     ret
  288.  
  289. _get_glove_byte    endp
  290.  
  291.  
  292.  ;
  293.  ; void set_glove_bits(int data);   /* sets glove clock, data lines */
  294.  ;                                  /* and does a bit delay         */
  295.  
  296.         PUBLIC    _set_glove_bits
  297.  
  298. bits equ [bp+6]
  299.  
  300. _set_glove_bits    proc    far
  301.  
  302.     push    bp
  303.     mov    bp,sp
  304.     push    dx
  305.     mov    dx,_glove_out_port
  306.     mov    ax,bits
  307.  
  308.     pushf
  309.     cli
  310.     and    al,BYTE PTR _glove_write_mask
  311.     mov    ah,BYTE PTR _glove_write_mask
  312.     not    ah
  313.     and    ah,BYTE PTR _port_image
  314.     or    al,ah
  315.     mov    BYTE PTR _port_image,al
  316.     out    dx,al                   ; output data
  317.     popf
  318.  
  319.     push    cx
  320.     bitdelay
  321.     pop    cx
  322.  
  323.     pop    dx
  324.     mov    sp,bp
  325.     pop    bp
  326.     ret
  327.  
  328. _set_glove_bits    endp
  329.  
  330.         end
  331.  
  332.