home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSSRC.ZIP / TIMER.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  3KB  |  109 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  TIMER.CPP                                                             */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __TIMER_H )
  11. #include <Timer.h>
  12. #endif  // __TIMER_H
  13.  
  14. #if !defined( __DOS_H )
  15. #include <Dos.h>
  16. #endif  // __DOS_H
  17.  
  18. const unsigned long far * const dosTime =
  19.     (const unsigned long far * const)MK_FP( 0x40, 0x6C );
  20.  
  21. unsigned Timer::adjust = calibrate();
  22.  
  23. Timer::Timer() : time_(0), running(0)
  24. {
  25. }
  26.  
  27. void Timer::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 Timer::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 Timer::reset()
  65. {
  66.     time_ = 0;
  67.     if( running )
  68.         start();
  69. }
  70.  
  71. unsigned Timer::calibrate()
  72. {
  73.     adjust = 0;
  74.     unsigned long sum = 0;
  75.     Timer 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.     Timer 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.