home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / gsidemo / timeout.asm < prev    next >
Encoding:
Assembly Source File  |  1994-09-14  |  1.3 KB  |  46 lines

  1. ;put into the public domain by Russell Nelson, nelson@crynwr.com
  2.  
  3. ;we read the timer chip's counter zero.  It runs freely, counting down
  4. ;from 65535 to zero.  We sample the count coming in and subract the previous
  5. ;count.  Then we double it and add it to our timeout_counter.  When it overflows,
  6. ;then we've waited a tick of 27.5 ms.
  7.  
  8. timeout        dw    ?        ;number of ticks to wait.
  9. timeout_counter    dw    ?        ;old counter zero value.
  10. timeout_value    dw    ?
  11.  
  12.     public    set_timeout
  13. set_timeout:
  14. ;enter with ax = number of ticks (36.4 ticks per second).
  15.     inc    ax            ;the first times out immediately.
  16.     mov    cs:timeout,ax
  17.     mov    cs:timeout_counter,0
  18.     call    latch_timer
  19.     mov    cs:timeout_value,ax
  20.     ret
  21.  
  22. latch_timer:
  23.     mov    al,0            ;latch counter zero.
  24.     out    43h,al
  25.     in    al,40h            ;read counter zero.
  26.     mov    ah,al
  27.     in    al,40h
  28.     xchg    ah,al
  29.     ret
  30.  
  31.     public    do_timeout
  32. do_timeout:
  33. ;call at *least* every 27.5ms when checking for timeout.  Returns nz
  34. ;if we haven't timed out yet.
  35.     call    latch_timer
  36.     xchg    ax,cs:timeout_value
  37.     sub    ax,cs:timeout_value
  38.     shl    ax,1            ;keep timeout in increments of 27.5 ms.
  39.     add    cs:timeout_counter,ax    ;has the counter overflowed yet?
  40.     jnc    do_timeout_1        ;no.
  41.     dec    cs:timeout        ;Did we hit the timeout value yet?
  42.     ret
  43. do_timeout_1:
  44.     or    sp,sp            ;ensure nz.
  45.     ret
  46.