home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 314_01 / timer.asm < prev   
Assembly Source File  |  1990-05-15  |  5KB  |  190 lines

  1.     title    timers.. timerins, timerrem, and timerint
  2.     page    60,132
  3. ;==============================================================================
  4. ;
  5. ;                   The Microcom MNP Library
  6. ;                    (Microsoft C Version)
  7. ;------------------------------------------------------------------------------
  8. ;
  9. ;    timerins -- timer install routine
  10. ;
  11. ;    synopsis: timerins();
  12. ;
  13. ;------------------------------------------------------------------------------
  14. ;
  15. ;    timerrem -- timer remove routine
  16. ;
  17. ;    synopsis: timerrem();
  18. ;
  19. ;------------------------------------------------------------------------------
  20. ;
  21. ;    timerint -- timer interrupt routine (invoked on timer tick)
  22. ;
  23. ;------------------------------------------------------------------------------
  24. ;
  25. ;    Modification History
  26. ;
  27. ;    10/29/87 - timerint allowed to run with interrupts enabled..gp
  28. ;
  29. ;==============================================================================
  30.  
  31.     extrn    _tmr:word
  32.  
  33. _data    segment word public 'DATA'
  34. _data   ends
  35. dgroup  group    _data
  36.  
  37. _data   segment
  38. old_t_vct    label    dword        ; old timer vector contents
  39. old_bx        dw    0        ; base
  40. old_es        dw    0        ; segment
  41. timer_active    db    0        ; timer active flag
  42. tick        db    18        ; clock tick count
  43.  
  44. timer_no  equ    6            ; number of timers in table
  45. _data    ends
  46.  
  47. _text    segment byte public 'CODE'
  48.     assume    cs:_text,ds:dgroup
  49.  
  50.     public    _timerins,_timerrem
  51.     public    cdsg
  52.     public     _timer_int
  53.  
  54.  
  55. ; Data in code segment
  56. cdsg    dw    0
  57.  
  58.     page
  59. ;PUBLIC***********************************************************************
  60. ;
  61. ;    timerins - timer installation routine
  62. ;
  63. ;*****************************************************************************
  64.  
  65. _timerins proc near
  66.  
  67.     push    bp
  68.     push    es                ; save es and ds
  69.     push    ds
  70.  
  71. ; Check to be sure that the timer interrupt handler is not already
  72. ; installed.  Do nothing in this case.
  73.     cmp    timer_active,1        ; already installed?
  74.     je    tin9                ; yes-go return
  75.  
  76. ; Initialize
  77.     mov    timer_active,1        ; set timer active flag
  78.     mov    cs:cdsg,ds        ; save data segment
  79.  
  80. ; Save current interrupt vector contents then install our own handler.
  81.     mov    ah,35h            ; get vector request
  82.     mov    al,1ch            ; we want the timer one
  83.     int    21h                ; int to DOS
  84.     mov    old_bx,bx            ; save vector contents
  85.     mov    old_es,es
  86.     mov    dx,offset _timer_int    ; point to our handler
  87.     mov    ax,cs            ; be sure ds is right
  88.     mov    ds,ax
  89.     mov    ah,25h            ; set vector request
  90.     mov    al,1ch            ; we want the timer one
  91.     int    21h                ; int to DOS
  92.  
  93. ; Return
  94. tin9:
  95.     pop    ds                ; restore data segment
  96.     pop    es                ; restore es
  97.     pop    bp
  98.     ret
  99.  
  100. _timerins endp
  101.      page
  102. ;PUBLIC***********************************************************************
  103. ;
  104. ;    timerrem - timer removal subroutine
  105. ;
  106. ;*****************************************************************************
  107.  
  108. _timerrem proc near
  109.  
  110.     push    bp
  111. ; Check to be sure that the timer interrupt handler is already
  112. ; installed.  Do nothing if not.
  113.     cmp    timer_active,1        ; installed?
  114.     jne    tir9                ; no-go return
  115.  
  116. ; Recover old vector contents and put them back.
  117.     push    ds                ; save ds register
  118.     mov    ah,25h            ; set vector request
  119.     mov    al,1ch            ; we want the timer one
  120.     mov    dx,old_bx            ; recover old vector
  121.     mov    ds,old_es
  122.     int    21h                ; int to DOS
  123.     pop    ds                ; restore ds register
  124.     mov    timer_active,0        ; reset timer active flag
  125.  
  126. ; Return
  127. tir9:
  128.     pop    bp
  129.     ret
  130.  
  131. _timerrem endp
  132.     page
  133. ;*****************************************************************************
  134. ;
  135. ;    timerint - timer interrupt routine
  136. ;
  137. ;*****************************************************************************
  138.  
  139. _timer_int proc far
  140.  
  141. ; Initialize
  142.     push    ds                ; save registers
  143.     mov    ds,cs:cdsg        ; enable data addressability
  144.  
  145. ; Re-enable interrupts
  146. ;    sti
  147.  
  148. ; When a clock interrupt occurs, decrement the second ticker.  If the
  149. ; ticker goes to zero, count down the second-based timers.
  150. t1:
  151.     dec    tick                ; decrement ticker
  152.     jnz    tiret            ; go on if not zero
  153.  
  154. ; Pass through second-based timers and decrement any that are not already
  155. ; zero.  Zero can mean either 'elapsed' or 'inactive'.
  156. ti1:
  157.     mov    tick,18             ; reset ticker
  158.  
  159.     push    bx                ; save registers
  160.     push    cx
  161.     mov     bx,offset _tmr
  162.     mov    cx,timer_no        ; do for each timer
  163.  
  164. ti3:
  165.     cmp    word ptr [bx],0     ; active?
  166.     je    ti4                ; no-go on
  167.     dec    word ptr [bx]        ; yes-count it down
  168.  
  169. ti4:
  170.     add    bx,2                ; advance to next timer
  171.     loop    ti3                ; and try again
  172.  
  173.     pop    cx                ; restore registers
  174.     pop    bx                ; and go on
  175.  
  176. tiret:
  177.     cli                    ;  disable interrupts before call
  178.     pushf                ; this is to simulate an int instruction
  179.     call    old_t_vct            ; inter-segment indirect call
  180.  
  181.     pop    ds
  182.     sti                    ; re-enable interrupts
  183.     iret                    ; and return from interrupt
  184.  
  185. _timer_int endp
  186.  
  187. _text    ends
  188.     end
  189.