home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / SLEEP / SLEEP.C next >
C/C++ Source or Header  |  1993-12-01  |  2KB  |  116 lines

  1. /*
  2.  *  definitions and includes
  3.  *
  4.  */
  5.  
  6. #include <dos.h>
  7.  
  8. typedef struct
  9. { /* time holder */
  10.   int hour;
  11.   int minute;
  12.   int sec;
  13.   int hsec;
  14. } TIME, *TIME_PTR;
  15.  
  16. #define GET_TIME    0x2c        /* time request        */
  17. #define DOS_INT        0x21        /* int to call DOS    */
  18. #define INIT        6        /* initial clock set    */
  19. /*
  20.  * get_time(n)
  21.  * TIME_PTR n;
  22.  *
  23.  * fills timetype structure n with current time using DOS interrupt 21
  24.  *
  25.  */
  26.  
  27. get_time(n)
  28. TIME_PTR n;
  29. {
  30.   union REGS inregs;
  31.   union REGS outregs;
  32.  
  33.   inregs.h.ah = GET_TIME;
  34.  
  35.   int86(DOS_INT, &inregs, &outregs);
  36.  
  37.   n->hour = outregs.h.ch;
  38.   n->minute  = outregs.h.cl;
  39.   n->sec  = outregs.h.dh;
  40.   n->hsec = outregs.h.dl;
  41.  
  42.   return(0);
  43. }
  44.  
  45. sleep(x)
  46. int x;
  47. {
  48.   int i;
  49.   unsigned s;
  50.   TIME n;               /* current time record */
  51.  
  52.   i = 0;
  53.   get_time(&n);
  54.   s = n.sec;
  55.  
  56.   while (i < x){
  57.     while (s == n.sec)
  58.       get_time(&n);
  59.     s = n.sec;
  60.     ++i;
  61.   }
  62. }
  63.  
  64. msleep(ms)
  65.   int ms;
  66. { /* sleep for ms miliseconds */
  67.   static long count = 0;
  68.   long loops;
  69.   TIME n1, n2;
  70.   int delay;
  71.   
  72.   if (count == 0)
  73.   { /* wait for a clock tick */
  74.     count = 1000L;
  75.     get_time(&n1);
  76.     delay = n1.hsec;
  77.     while (n1.hsec == delay)
  78.       get_time(&n1);
  79.     /* first get a ballpark figure */
  80.     for (loops = count; loops--; loops = (loops << 1) / 2);
  81.     get_time(&n2);
  82.     delay = (100*n2.sec + n2.hsec) - (100*n1.sec + n1.hsec);
  83.     if (delay < 0) delay += 6000;
  84. printf("1st time %2d:%02d:%02d.%02d\n", n1.hour, n1.minute, n1.sec, n1.hsec);
  85. printf("%02d.%02d ... %02d.%02d\n", n1.sec, n1.hsec, n2.sec, n2.hsec);
  86. printf("delay %d ms\n", 10*delay);
  87. if (delay < 0) exit(0);
  88.     /* this is the first guess */
  89.     count = 100*count / delay;
  90. printf("Delay %d loops/ms\n", count);
  91.     /* this one should be about 2 sec */
  92.     get_time(&n1);
  93.     for (loops = INIT*count; loops--; loops = (loops << 1) / 2);
  94.     get_time(&n2);
  95.     delay = (100*n2.sec + n2.hsec) - (100*n1.sec + n1.hsec);
  96.     if (delay < 0) delay += 6000;
  97. printf("delay %d ms\n", 10*delay);
  98.     count = INIT*100*count / delay;
  99. printf("Delay %d loops/ms\n", count);
  100.   }
  101.  
  102.   for (loops = ms*count/1000; loops--; loops = (loops << 1) / 2);
  103. }
  104.  
  105. #ifdef    TEST
  106. #include <stdio.h>
  107. main() {
  108.   msleep(0);
  109.   bdos(6,7); /* bell */
  110.   msleep(6000);
  111.   bdos(6,7);
  112.   msleep(3000);
  113.   bdos(6,7);
  114. }
  115. #endif
  116.