home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / phreak_utils_pc / timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-02  |  2.1 KB  |  98 lines

  1. #include"timer.h"
  2. #include<dos.h>
  3. #include<assert.h>
  4. #include<stdlib.h>
  5.  
  6. // Hey! Compile this with stack overflow checking off or you'll be sorry!
  7.  
  8. Timers globalTimers(32);
  9.  
  10. Timers::~Timers(void)
  11. {if (intrSpeed!=1)
  12.      {/* return the ticks to normal speed */
  13.       disable();
  14.       outp(0x43, 0x36);
  15.       outp(0x40, 0);
  16.       outp(0x40, 0);
  17.       enable();
  18.       }
  19.  _dos_setvect(0x08, oldHandler);
  20. }
  21.  
  22. void Timers::addTimer(int volatile *value,int countSpeed)
  23. {int i;
  24.  for (i=0;i<topTimer && timer[i];i++);
  25.  assert(i<MAXNMTIMERS-1);
  26.  disable();
  27.  timer[i]=value;
  28.  timerDivisor[i]=(((long)countSpeed)*18L*((long)intrSpeed))/100L;
  29.  if (timerDivisor[i]<1) timerDivisor[i]=1;
  30.  timerFrac[i]=timerDivisor[i];
  31.  if (i>=topTimer) topTimer=i+1;
  32.  enable();
  33. }
  34.  
  35. void Timers::addTimerExact(int volatile *value,int countSpeed)
  36. {int i;
  37.  for (i=0;i<topTimer && timer[i];i++);
  38.  assert(i<MAXNMTIMERS-1);
  39.  disable();
  40.  timer[i]=value;
  41.  timerDivisor[i]=countSpeed;
  42.  timerFrac[i]=timerDivisor[i];
  43.  if (i>=topTimer) topTimer=i+1;
  44.  enable();
  45. }
  46.  
  47.  
  48. void Timers::removeTimer(volatile int *value)
  49. {int i;
  50.  for (i=0;i<topTimer && timer[i]!=value;i++);
  51.  disable();
  52.  if (i<topTimer)
  53.     timer[i]=NULL;
  54.  enable();
  55. }
  56.  
  57. static Timers *installed=NULL;
  58. static void interrupt timerISR(...)
  59. {if (installed)
  60.     installed->tick();
  61. }
  62.  
  63. Timers::Timers(int tickSpeedFactor)
  64. {unsigned int timerSpeed;
  65.  topTimer=0;
  66.  intrCount=0;
  67.  intrSpeed=tickSpeedFactor;
  68.  installed=this;
  69.  oldHandler = _dos_getvect(0x08);
  70.  _dos_setvect(0x08, timerISR);
  71.  if (tickSpeedFactor!=1)
  72.     {/* tell the timer to speed up the ticks */
  73.      timerSpeed=0x10000L/tickSpeedFactor;
  74.      disable();
  75.      outp(0x43, 0x36);
  76.      outp(0x40, timerSpeed & 0xff);
  77.      outp(0x40, timerSpeed >> 8);
  78.      enable();
  79.     }
  80. }
  81.  
  82. void Timers::tick(void)
  83. {int i;
  84.  for (i=0;i<topTimer;i++)
  85.     {if (timer[i] && *(timer[i]) && !(--timerFrac[i]))
  86.     {(*(timer[i]))--;
  87.      timerFrac[i]=timerDivisor[i];
  88.     }
  89.     }
  90.  if (++intrCount>=intrSpeed)
  91.     {intrCount=0;
  92.      oldHandler();
  93.     }
  94.  else
  95.     outp(0x20,0x20);
  96. }
  97.  
  98.