home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CLASSSRC.PAK / TIMER.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  3KB  |  111 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  TIMER.CPP                                                             */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __DOS_H )
  11. #include <dos.h>
  12. #endif
  13.  
  14. #define BUILDBIDSTIMER
  15.  
  16. #if !defined( CLASSLIB_TIMER_H )
  17. #include <classlib/timer.h>
  18. #endif
  19.  
  20. const unsigned long far * const dosTime =
  21.     (const unsigned long far * const)MK_FP( 0x40, 0x6C );
  22.  
  23. unsigned TTimer::Adjust = Calibrate();
  24.  
  25. TTimer::TTimer() : Time_(0), Running(0)
  26. {
  27. }
  28.  
  29. void TTimer::Start()
  30. {
  31.     if( !Running )
  32.         {
  33.         outportb( 0x43, 0x34 );
  34.         asm jmp __1;
  35.     __1:
  36.         outportb( 0x40, 0 );
  37.         asm jmp __2;
  38.     __2:
  39.         outportb( 0x40, 0 );
  40.         StartTime.DosCount = *dosTime;
  41.         StartTime.TimerCount = 0;
  42.         Running = 1;
  43.         }
  44. }
  45.  
  46. void TTimer::Stop()
  47. {
  48.     outportb( 0x43, 0 );
  49.     unsigned char temp = inportb( 0x40 );
  50.  
  51.     TIME stopTime;
  52.     stopTime.TimerCount = (inportb( 0x40 ) << 8) + temp;
  53.     stopTime.DosCount = *dosTime;
  54.  
  55.     TIME elapsedTime;
  56.     elapsedTime.DosCount = stopTime.DosCount - StartTime.DosCount;
  57.     elapsedTime.TimerCount = -( stopTime.TimerCount - Adjust );
  58.  
  59.     const double fudge = 83810.0/100000.0;
  60.     Time_ += ((elapsedTime.DosCount << 16) + elapsedTime.TimerCount)*fudge;
  61.  
  62.     Running = 0;
  63.  
  64. }
  65.  
  66. void TTimer::Reset()
  67. {
  68.     Time_ = 0;
  69.     if( Running )
  70.         Start();
  71. }
  72.  
  73. unsigned TTimer::Calibrate()
  74. {
  75.     Adjust = 0;
  76.     unsigned long sum = 0;
  77.     TTimer w;
  78.     for( int i = 0; i < 100; i++ )
  79.         {
  80.         w.Start();
  81.         w.Stop();
  82.         sum += w.Time();
  83.         w.Reset();
  84.         }
  85.     return (unsigned)((sum+5)/100);
  86. }
  87.  
  88. #if defined( TEST_TIMER )
  89. #include <iostream.h>
  90. #include <stdio.h>
  91.  
  92. int main( void )
  93. {
  94.     delay( 0 );
  95.     cout << "Resolution: " << Timer::Resolution() << endl;
  96.     TTimer w;
  97.     for( unsigned del = 0; del < 10; del++ )
  98.         {
  99.         unsigned d1 = del*100;
  100.         w.Start();
  101.         delay( d1 );
  102.         w.Stop();
  103.         printf( "%4u ms., actual time = %6f seconds.\n", d1, w.Time() );
  104.         w.Reset();
  105.         }
  106.     return 0;
  107. }
  108. #endif
  109.  
  110.  
  111.