home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 20.ddi / CLASSSRC.PAK / TIMER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  2.6 KB  |  109 lines

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