home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / packetdrivers.tar.gz / pd.tar / src / timeout.asm < prev    next >
Assembly Source File  |  1995-06-25  |  1KB  |  45 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. set_timeout:
  13. ;enter with ax = number of ticks (36.4 ticks per second).
  14.     inc    ax            ;the first times out immediately.
  15.     mov    cs:timeout,ax
  16.     mov    cs:timeout_counter,0
  17.     call    latch_timer
  18.     mov    cs:timeout_value,ax
  19.     ret
  20.  
  21. latch_timer:
  22.     mov    al,0            ;latch counter zero.
  23.     out    43h,al
  24.     in    al,40h            ;read counter zero.
  25.     mov    ah,al
  26.     in    al,40h
  27.     xchg    ah,al
  28.     ret
  29.  
  30.  
  31. do_timeout:
  32. ;call at *least* every 27.5ms when checking for timeout.  Returns nz
  33. ;if we haven't timed out yet.
  34.     call    latch_timer
  35.     xchg    ax,cs:timeout_value
  36.     sub    ax,cs:timeout_value
  37.     shl    ax,1            ;keep timeout in increments of 27.5 ms.
  38.     add    cs:timeout_counter,ax    ;has the counter overflowed yet?
  39.     jnc    do_timeout_1        ;no.
  40.     dec    cs:timeout        ;Did we hit the timeout value yet?
  41.     ret
  42. do_timeout_1:
  43.     or    sp,sp            ;ensure nz.
  44.     ret
  45.