home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / lockclock / interp_l.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  3KB  |  118 lines

  1. #include "sizint.h"
  2. float interp(kin)
  3. LONG kin;         /*tick fraction read by caller */
  4. {
  5. /*
  6.   this subroutine estimates the fraction of a tick between
  7.   when it is called and the next time the clock changes.
  8.   it does this by incrementing a variable until the tick
  9.   changes and then repeating the operation until it changes
  10.   again.  the first number is a partial tick and the second
  11.   one is a full tick and the ratio is thus the fraction
  12.   from when the routine is called until the next tick.
  13.  
  14.   For the Sun system, the interval betweek ticks in microseconds
  15.   must be specified in the file getdif.h as parameter tickus.
  16.   If this parameter is set to 0, then the interpolator is 
  17.   effectively disabled.
  18.  
  19.     the input parameter kin is the seconds fraction read
  20.     by the caller and it an attempt to deal with the 
  21.     possibility that the tick changed between the time
  22.     that the caller read the clock and this routine was
  23.     called.
  24.     if the first reading of the clock in the routine does
  25.     not match the parameter in the call, then the tick
  26.     changed during the call and the tick fraction is
  27.     essentially 0.  note that without this test, the
  28.     tick fraction returned by this routine would be almost
  29.     a full tick, which would produce an error of a full tick.
  30. */
  31. #include "getdif.h"
  32. #include <stdio.h>
  33. #ifdef IBMPC
  34. #include <dos.h>
  35. #endif
  36. #ifdef SUN
  37. #include <sys/time.h>
  38. struct timeval tvv,*tp;
  39. LONG old,dly;
  40. #endif
  41. int j;
  42. int k,kk;
  43. LONG mm,mmm;
  44. float xx;
  45. /*
  46.         get lower portion of time for IBMPC or current microseconds for sun
  47. */
  48. #ifdef IBMPC
  49.         _AH=0;
  50.         geninterrupt(0x1a);
  51.         k=_DX;
  52.     if(k != kin) return(0.);    /*tick changed during call*/
  53. #endif
  54. #ifdef SUN
  55.     tp= &tvv;
  56.     gettimeofday(tp,0);
  57.     old=tp->tv_usec;
  58.     if(tickus == 0) return (100.);    /*interpolation disabled */
  59.     if(old != kin) return(0.);    /*tick changed during call*/
  60. #endif
  61. #ifdef IBMPC
  62. /*
  63.   now wait until tick changes
  64. */
  65.     mmm=0;
  66.     do
  67.       {
  68.       mmm++;
  69.       _AH=0;
  70.       geninterrupt(0x1a);
  71.       kk=_DX;
  72.     } while (kk==k);
  73.     mm=0;
  74.     k=kk;
  75.         do
  76.           {
  77.           mm++;
  78.           _AH=0;
  79.            geninterrupt(0x1a);
  80.            kk=_DX;
  81.         } while (kk == k);
  82.         xx=mmm;
  83.         xx=100*xx/(float) mm;
  84.     if(xx < 0) return (0.);
  85.     if(xx > 100) return(100.);
  86.         return(xx);
  87. #endif
  88. #ifdef SUN
  89. /*
  90.     first increment counter until tick changes  by the 
  91.     time interval in microseconds specified in the
  92.     header file as parameter tickus. -- see nistime.h
  93. */
  94.     mmm=0;
  95.     do
  96.       {
  97.       mmm++;
  98.       gettimeofday(tp,0);
  99.       dly=tp->tv_usec - old;
  100.       if(dly < 0) dly += 1000000;  /*if rolled over to next second*/
  101.     } while(dly < tickus);
  102.     old=tp->tv_usec;
  103.     mm=0;
  104.     do
  105.       {
  106.       mm++;
  107.       gettimeofday(tp,0);
  108.       dly=tp->tv_usec - old;
  109.       if(dly < 0) dly += 1000000;
  110.     } while (dly < tickus);
  111.     xx=mmm;
  112.     xx=100*xx/(float)mm;
  113.     if(xx < 0) return(0.);
  114.     if(xx > 100) return(100.);
  115.     return(xx);
  116. #endif
  117. }
  118.