home *** CD-ROM | disk | FTP | other *** search
- #include"timer.h"
- #include<dos.h>
- #include<assert.h>
- #include<stdlib.h>
-
- // Hey! Compile this with stack overflow checking off or you'll be sorry!
-
- Timers globalTimers(32);
-
- Timers::~Timers(void)
- {if (intrSpeed!=1)
- {/* return the ticks to normal speed */
- disable();
- outp(0x43, 0x36);
- outp(0x40, 0);
- outp(0x40, 0);
- enable();
- }
- _dos_setvect(0x08, oldHandler);
- }
-
- void Timers::addTimer(int volatile *value,int countSpeed)
- {int i;
- for (i=0;i<topTimer && timer[i];i++);
- assert(i<MAXNMTIMERS-1);
- disable();
- timer[i]=value;
- timerDivisor[i]=(((long)countSpeed)*18L*((long)intrSpeed))/100L;
- if (timerDivisor[i]<1) timerDivisor[i]=1;
- timerFrac[i]=timerDivisor[i];
- if (i>=topTimer) topTimer=i+1;
- enable();
- }
-
- void Timers::addTimerExact(int volatile *value,int countSpeed)
- {int i;
- for (i=0;i<topTimer && timer[i];i++);
- assert(i<MAXNMTIMERS-1);
- disable();
- timer[i]=value;
- timerDivisor[i]=countSpeed;
- timerFrac[i]=timerDivisor[i];
- if (i>=topTimer) topTimer=i+1;
- enable();
- }
-
-
- void Timers::removeTimer(volatile int *value)
- {int i;
- for (i=0;i<topTimer && timer[i]!=value;i++);
- disable();
- if (i<topTimer)
- timer[i]=NULL;
- enable();
- }
-
- static Timers *installed=NULL;
- static void interrupt timerISR(...)
- {if (installed)
- installed->tick();
- }
-
- Timers::Timers(int tickSpeedFactor)
- {unsigned int timerSpeed;
- topTimer=0;
- intrCount=0;
- intrSpeed=tickSpeedFactor;
- installed=this;
- oldHandler = _dos_getvect(0x08);
- _dos_setvect(0x08, timerISR);
- if (tickSpeedFactor!=1)
- {/* tell the timer to speed up the ticks */
- timerSpeed=0x10000L/tickSpeedFactor;
- disable();
- outp(0x43, 0x36);
- outp(0x40, timerSpeed & 0xff);
- outp(0x40, timerSpeed >> 8);
- enable();
- }
- }
-
- void Timers::tick(void)
- {int i;
- for (i=0;i<topTimer;i++)
- {if (timer[i] && *(timer[i]) && !(--timerFrac[i]))
- {(*(timer[i]))--;
- timerFrac[i]=timerDivisor[i];
- }
- }
- if (++intrCount>=intrSpeed)
- {intrCount=0;
- oldHandler();
- }
- else
- outp(0x20,0x20);
- }
-
-