[comp.lang.c] nap for TurboC

toma@killer.UUCP (Tom Armistead) (01/09/88)

This is a 'little thing I put together' when I was in the
need for an Accurate Millisecond timer for the PC. This is
pretty accurate, + or - 5 milliseconds on a 4.77 mhz XT and
even better on an AT (~+ or - 2 milliseconds)!!!

I have included the uuencoded TEST.EXE demo program. See the READ.ME
for a better explanation...

Tom
---
UUCP: ...!ihnp4!killer!toma
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  READ.ME nap.c test.c test.uue
# Wrapped by toma@killer on Sat Jan  9 02:33:23 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f READ.ME -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"READ.ME\"
else
echo shar: Extracting \"READ.ME\" \(1070 characters\)
sed "s/^X//" >READ.ME <<'END_OF_READ.ME'
X
X                nap(1)                PC-DOS            Turbo-C
X
X                This  is an implementation of unix (curses) nap
X                function for Turbo C. Timings are NOT processor 
X                speed specific  and  therefore produce the same
X                delay on a  10mhz  AT as on a 4.77 mhz XT.
X
X                Curses  nap() is used to produce a  millisecond
X                delay.
X
X                Tom Armistead
X                UUCP: ...!ihnp4!killer!toma
X
X                --------------------------------------------------
X                Synopsis:       int nap(delay)
X                                int     delay;
X
X                Description:    produces  a   'delay'  millisecond
X                                delay.
X                                Must be  called once to initialize
X                                itself, this will produce  about a
X                                750 millisecond delay. May then be
X                                called  normally  to  produce  the
X                                specified delay.
X
X
X
END_OF_READ.ME
echo shar: Missing newline added to \"READ.ME\"
echo shar: 1 control character may be missing from \"READ.ME\"
if test 1070 -ne `wc -c <READ.ME`; then
    echo shar: \"READ.ME\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f nap.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"nap.c\"
else
echo shar: Extracting \"nap.c\" \(3628 characters\)
sed "s/^X//" >nap.c <<'END_OF_nap.c'
X/*.
X**      nap             sleep a specified number of milliseconds
X**                      TurboC v1.0
X**
X**      synopsis        int nap(d)
X**                      int     d;      * # of milliseconds to sleep *
X**
X**      description:    The first time this function is called, it
X**                      initializes it self to find out how long it
X**                      takes to delay 1 millisecond. This is done
X**                      by using a for loop and the dos clock and
X**                      takes about 750 milliseconds to complete.
X**                      This makes the code NON-CPU speed dependent.
X**
X**      caveats:        If you have things running in backround, they
X**                      will definately affect the accuracy here.
X**
X**      author:         Tom Armistead
X**                      UUCP: ...!ihnp4!killer!toma
X**
X**      credits:        I got this idea from something posted by Tim Pozar
X**                      some months back
X**
X**      PD:             This code is hereby placed in the Public Domain
X**                      and you may use it (or chunk it) as you see fit.
X**                      Just remember where you got it...
X**
X**
X*/
X#include <dos.h>
X
Xnap(d)
Xint     d;
X{       static  long count = 0;
X        long    loop,delay[10];
X        struct  time t1, t2;
X        int     i;
X unsigned char  cnt;
X
X        if (!count)                     /* 1st time through     */
X        {   count = 1000L;              /* base timer value (random) */
X            gettime(&t1);
X            cnt = t1.ti_hund;           /* currnet ms           */
X            while (t1.ti_hund == cnt)
X                gettime(&t1);           /* wait for next ms     */
X
X            /*
X            ** stay in this loop until we find a count value
X            ** that will take at leaste 1 clock tick
X            */
X            do
X            {   /*
X                ** do a short delay, to find out how long it takes
X                */
X                gettime(&t1);
X                for (loop = count; loop; loop--)
X                    ;
X                gettime(&t2);
X                delay[0] = (t2.ti_sec * 100 + t2.ti_hund) - /* # of msecs   */
X                           (t1.ti_sec * 100 + t1.ti_hund);  /* used in loop */
X                if (!delay[0])
X                    count *= 2;
X            } while (!delay[0]);
X
X            if (delay[0] < 0)
X                delay[0] += 6000;       /* check for seconds rollover   */
X            /*
X            ** now we have a delay value, go through loop
X            ** 10 times and take an average of those 10 delays
X            ** for the actual delay value
X            */
X            for (i=1; i<=10; i++)
X            {   gettime(&t1);
X                for (loop = count; loop; loop--)
X                    ;
X                gettime(&t2);
X                delay[i] = (100 * t2.ti_sec + t2.ti_hund) - /* # of msecs   */
X                           (100 * t1.ti_sec + t1.ti_hund);  /* used in loop */
X                if (delay[i] < 0)
X                    delay[i] += 6000;
X            }
X            for (delay[0] = 0,i=1; i<=10; i++)
X                delay[0] += delay[i];
X            delay[0] /= 10;             /* now the average of the 10    */
X            count /= 10 * delay[0];     /* # of loops for 1 millisecond */
X#ifdef TESTING
Xprintf("%ld times through loop, delayed %d ms\n", count, 10*delay[0]);
Xprintf("# of loops for 1 ms is %ld\n",count);
X#endif
X        } /* end if !count */
X
X        /*
X        ** count has been computed, now delay
X        */
X        else for (loop = count * d; loop; loop--)
X            ;
X} /* end of nap */
X
X/* end of nap.c */
X
END_OF_nap.c
echo shar: Missing newline added to \"nap.c\"
echo shar: 1 control character may be missing from \"nap.c\"
if test 3628 -ne `wc -c <nap.c`; then
    echo shar: \"nap.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f test.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"test.c\"
else
echo shar: Extracting \"test.c\" \(934 characters\)
sed "s/^X//" >test.c <<'END_OF_test.c'
X/*.
X**      test.c          test module for nap.c
X**                      TurboC v1.0
X**
X**      compile:        tcc test.c nap.c
X**
X**
X*/
X#include <stdio.h>
X
Xmain()
X{       int     i,j;
X
X        nap(0);
X
X        for (i=1; i<=10; i++)
X        {       /*
X                ** this loop will take 20 seconds
X                */
X                for (j=1; j<=80; j++)
X                {       nap(250);
X                        putchar('X');
X                }
X                /*
X                ** this loop will take 8 seconds
X                */
X                for (j=1; j<=80; j++)
X                {       nap(100);
X                        putchar('X');
X                }
X                /*
X                ** this loop will take .8 seconds
X                */
X                for (j=1; j<=80; j++)
X                {       nap(10);
X                        putchar('X');
X                }
X
X        }
X} /* end of main */
X
X/* end of test.c */
X
END_OF_test.c
echo shar: Missing newline added to \"test.c\"
echo shar: 1 control character may be missing from \"test.c\"
if test 934 -ne `wc -c <test.c`; then
    echo shar: \"test.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f test.uue -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"test.uue\"
else
echo shar: Extracting \"test.uue\" \(6535 characters\)
sed "s/^X//" >test.uue <<'END_OF_test.uue'
Xbegin 644 test.exe
XM35J$  H !@ @  #__P&          "(    ! /L0<FH!    9 0  $H$   [
XM!   (00  .4"                                                
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                                                            
XM                    NN, +HD6;@&T,,TABRX" (L>+ ".VJ.& (P&A ")
XM'H  B2Z8 ,<&B@#__X[#,\"Y_W^+^(O8)H$].#=U&2:+50* ^CUU$(#FW_\&
XMB@" _EEU!/\&B@#RKN-C0R8X!776@,V ]]F)#GX @\,'@>/\_]'CB1Z" !ZX
XM #7-(8D>= ",!G8 N  E#A^Z8@'-(1\._Q90 HL^3@*[  $[^W<"B_N,VH'#
XME )R%+\ $+$$T^M#*^H[[W<)A]\[[W<#Z9$ B]\#VHD>D ")'I0 H80 *]B.
XMP+1*S2'3YX[2B^?HY GHR@HSP"Z.!FX!OU0"N90"*\_SJO\V? #_-GH _S9X
XM .AM %#H#@2X "7%%G0 S2$NCAYN 0[_%E(",\"+\+DO )#\ @2 U !&XO@M
XM-I!T"KD9 )"Z+P#H"0"+[(I& K1,S2&T0+L" "Z.'FX!S2'#N1X D+I6 .CI
XM_[@# %#HI?^Y#@"0ND@ Z-C_Z^T  %97,\!0Z+4 6;\! .FC +X! .LLN/H 
XM4.BB %F[M #_!WT0L%B[O@#_!XL?B$?_M #K#+BT %"P6%#H>@=964:#_E!^
XMS[X! .LLN&0 4.AL %F[M #_!WT0L%B[O@#_!XL?B$?_M #K#+BT %"P6%#H
XM1 =964:#_E!^S[X! .LLN H 4.@V %F[M #_!WT0L%B[O@#_!XL?B$?_M #K
XM#+BT %"P6%#H#@=964:#_E!^ST>#_PI_ ^E5_U]>PU95B^R#[#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PP                       %1U
XM<F)O+4,@+2!#;W!Y<FEG:'0@*&,I(#$Y.#<@0F]R;&%N9"!);G1L+@!.=6QL
XM('!O:6YT97(@87-S:6=N;65N= I$:79I9&4@97)R;W(*06)N;W)M86P@<')O
XM9W)A;2!T97)M:6YA=&EO;@H                               "4 @  
XM                     !0%% 44!0  "0(          *8    * @$     
XM    M     (" @        #"      #_         -       /\         
XMW@      _P        #L      #_         /H      /\         " $ 
XM    _P         6 0    #_         "0!     /\         ,@$     
XM_P        !  0    #_         $X!     /\         7 $     _P  
XM      !J 0    #_         '@!     /\         A@$     _P      
XM  "4 0    #_         *(!     /\         L $!( (@ B $( (@____
XM____________________________________    $P("! 4&" @(%!4%$_\6
XM!18"_________________P4%_____________________P___P+_#_____\3
XL__\" @4/ O___Q/___________________\3_P   $("0@(        0!043
X 
Xend
END_OF_test.uue
if test 6535 -ne `wc -c <test.uue`; then
    echo shar: \"test.uue\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
-- 
-------------
Tom Armistead
UUCP:  ...!ihnp4!killer!toma