home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / TIMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-08  |  1.8 KB  |  64 lines

  1. #include <d:\usr\stdlib\time.h>
  2.  
  3. long start_timer(t)
  4. TIMER *t;
  5. /*
  6.  *    Start a 200Hz timer.  This timer value can later be checked with
  7.  *    time_since() to determine elapsed time.  These functions provide
  8.  *    a very low-overhead way of timing events.
  9.  */
  10. {
  11.     asm("    clr.l    -(sp)        ");
  12.     asm("    move.w    #$20,-(sp)    ");
  13.     asm("    trap    #1        ");    /* enter supervisor mode */
  14.     asm("    addq.l    #6,sp        ");
  15.     asm("    move.l    $4BA,-(sp)    ");    /* save system clock value */
  16.     asm("    move.l    d0,-(sp)    ");
  17.     asm("    move.w    #$20,-(sp)    ");
  18.     asm("    trap    #1        ");    /* exit supervisor mode */
  19.     asm("    addq.l    #6,sp        ");
  20.     asm("    move.l    (sp)+,d0    ");
  21.     asm("    move.l    $8(a6),a0    ");    /* grab pointer to timer */
  22.     asm("    move.l    d0,(a0)        ");    /* return clock value */
  23. }
  24.  
  25. long time_since(t)
  26. TIMER *t;
  27. /*
  28.  *    Returns the number of 200Hz ticks since start_timer() was called
  29.  *    for timer <t>.
  30.  */
  31. {
  32.     asm("    clr.l    -(sp)        ");
  33.     asm("    move.w    #$20,-(sp)    ");
  34.     asm("    trap    #1        ");    /* enter supervisor mode */
  35.     asm("    addq.l    #6,sp        ");
  36.     asm("    move.l    $4BA,-(sp)    ");    /* save system clock value */
  37.     asm("    move.l    d0,-(sp)    ");
  38.     asm("    move.w    #$20,-(sp)    ");
  39.     asm("    trap    #1        ");    /* exit supervisor mode */
  40.     asm("    addq.l    #6,sp        ");
  41.     asm("    move.l    (sp)+,d0    ");
  42.     asm("    move.l    $8(a6),a0    ");    /* grab pointer to timer */
  43.     asm("    sub.l    (a0),d0        ");    /* return elapsed time */
  44. }
  45.  
  46. sleep(dt)
  47. int dt;
  48. /*
  49.  *    Suspend operation for <dt> seconds.  This is implemented as a
  50.  *    start_timer()/time_since() tight loop waiting for the specified
  51.  *    amount of time to pass.  In a multi-tasking environment, this
  52.  *    function should be replaced by a call which will de-activate
  53.  *    this task for a period of time, allowing other tasks to run.
  54.  */
  55. {
  56.     long t, start_timer(), time_since();
  57.     register long tt;
  58.  
  59.     tt = ((long) dt) * 200L;
  60.     start_timer(&t);
  61.     while(time_since(&t) < tt)
  62.         ;
  63. }
  64.