home *** CD-ROM | disk | FTP | other *** search
/ Dream 48 / Amiga_Dream_48.iso / Atari / c / sozobon-v2 / dlibsrc.lha / TIMER.C < prev    next >
C/C++ Source or Header  |  1988-09-29  |  3KB  |  99 lines

  1. #include <time.h>
  2.  
  3. clock_t    clock()
  4. /*
  5.  *    Return current value of 200Hz timer.  This is the ANSI compatible
  6.  *    timer function.  The number of seconds elapsed between two events
  7.  *    can be determined by calling this function twice and dividing the
  8.  *    difference between the two times by CLK_TCK (200L).
  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    ");    /* return system clock value */
  21.     }
  22.  
  23. clock_t start_timer(t)
  24.     clock_t *t;
  25. /*
  26.  *    Start a 200Hz timer.  This timer value can later be checked with
  27.  *    time_since() to determine elapsed time.  These functions provide
  28.  *    a very low-overhead way of timing events.
  29.  */
  30.     {
  31.     asm("    clr.l    -(sp)        ");
  32.     asm("    move.w    #$20,-(sp)    ");
  33.     asm("    trap    #1        ");    /* enter supervisor mode */
  34.     asm("    addq.l    #6,sp        ");
  35.     asm("    move.l    $4BA,-(sp)    ");    /* save system clock value */
  36.     asm("    move.l    d0,-(sp)    ");
  37.     asm("    move.w    #$20,-(sp)    ");
  38.     asm("    trap    #1        ");    /* exit supervisor mode */
  39.     asm("    addq.l    #6,sp        ");
  40.     asm("    move.l    (sp)+,d0    ");
  41.     asm("    move.l    $8(a6),a0    ");    /* grab pointer to timer */
  42.     asm("    move.l    d0,(a0)        ");    /* return clock value */
  43.     }
  44.  
  45. clock_t time_since(t)
  46.     clock_t *t;
  47. /*
  48.  *    Returns the number of 200Hz ticks since start_timer() was called
  49.  *    for timer <t>.
  50.  */
  51.     {
  52.     asm("    clr.l    -(sp)        ");
  53.     asm("    move.w    #$20,-(sp)    ");
  54.     asm("    trap    #1        ");    /* enter supervisor mode */
  55.     asm("    addq.l    #6,sp        ");
  56.     asm("    move.l    $4BA,-(sp)    ");    /* save system clock value */
  57.     asm("    move.l    d0,-(sp)    ");
  58.     asm("    move.w    #$20,-(sp)    ");
  59.     asm("    trap    #1        ");    /* exit supervisor mode */
  60.     asm("    addq.l    #6,sp        ");
  61.     asm("    move.l    (sp)+,d0    ");
  62.     asm("    move.l    $8(a6),a0    ");    /* grab pointer to timer */
  63.     asm("    sub.l    (a0),d0        ");    /* return elapsed time */
  64.     }
  65.  
  66. sleep(dt)
  67.     int dt;
  68. /*
  69.  *    Suspend operation for <dt> seconds.  This is implemented as a
  70.  *    start_timer()/time_since() tight loop waiting for the specified
  71.  *    amount of time to pass.  In a multi-tasking environment, this
  72.  *    function should be replaced by a call which will de-activate
  73.  *    this task for a period of time, allowing other tasks to run.
  74.  */
  75.     {
  76.     clock_t t, start_timer(), time_since();
  77.     register clock_t tt;
  78.  
  79.     tt = ((clock_t) dt) * CLK_TCK;
  80.     start_timer(&t);
  81.     while(time_since(&t) < tt)
  82.         ;
  83.     }
  84.  
  85. usleep(dt)
  86.     int dt;
  87. /*
  88.  *    Suspend operation for <dt> microseconds.  Works like sleep().
  89.  */
  90.     {
  91.     clock_t t, start_timer(), time_since();
  92.     register clock_t tt;
  93.  
  94.     tt = ((clock_t) dt) * (CLK_TCK / ((clock_t) 100));
  95.     start_timer(&t);
  96.     while(time_since(&t) < tt)
  97.         ;
  98.     }
  99.