home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
-
- FILE
- time1.c - simple timer routine that uses real time
-
- ENTRY ROUTINES
- SetTimer - set timer interval
- TimeUp - has timer run out?
- ResetTimer - restore timer to default state
-
- tick - timer interrupt service routine
-
- INITIALIZATION ROUTINE
- InitTimer - initialize time module
-
- REMARKS
- Need to be linked with alarm.c and timeinta.asm
-
- LAST UPDATE
- 23 January 1986
- fix minor LINT complaints
- 26 January 1988
- revise to used getivec() and new version of setivec()
-
- Copyright(c) 1986-1988 D.M. Auslander and C.H. Tham
-
- ***********************************************************************/
-
- /***********************************************************************
- I M P O R T S
- ***********************************************************************/
-
- #include "envir.h"
- #include "8259.h"
- #include "inout.h"
- #include "alarm.h"
-
- /*
- * The following routines are to be found in timeinta.asm
- */
-
- #ifdef ANSI
-
- extern void tmrisr(void);
- extern void setivec(int, unsigned, long);
- extern long getivec(int);
- extern int enable(void);
- extern int disable(void);
-
- #else
-
- extern void tmrisr();
- extern void setivec();
- extern long getivec();
- extern int enable();
- extern int disable();
-
- #endif
-
-
- /***********************************************************************
- P R I V A T E D A T A
- ***********************************************************************/
-
- static long mstick; /* milliseconds per tick */
- static long dtime; /* count down in ticks */
- static long orig_isr; /* pointer to original timer isr */
-
-
- /***********************************************************************
- E N T R Y R O U T I N E S
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- TICK - timer interrupt service routine
-
- SYNOPSIS
- called by timer interrupt
-
- REMARKS
- Interrupts are enabled when tick is called. However, we know
- that the low level isr, tmrisr() will not send an EOI to the
- 8259 until this routine is done, interrupts are effectively
- disabled since the timer interrupt has the highest priority.
-
- LAST UPDATE
- 6 June 1985
- ----------------------------------------------------------------------*/
-
- void tick()
- {
-
- if (dtime > 0L)
- --dtime;
- }
-
-
-
- /*---------------------------------------------------------------------
- PROCEDURE
- SETTIMER - set timer
-
- SYNOPSIS
- void SetTimer(ms)
- long ms;
-
- PARAMETER
- ms - time interval in milliseconds
-
- REMARKS
- (mstick >> 1) is equivalent to mstick/2, thus the formula
- is equivalent to: dtime = (ms / mstick) + 0.5
-
- LAST UPDATE
- 5 June 1985
- ---------------------------------------------------------------------*/
-
- void SetTimer(ms)
- long ms;
- {
-
- dtime = (ms + (mstick >> 1)) / mstick;
-
- }
-
-
-
- /*----------------------------------------------------------------------
- FUNCTION
- TIMEUP - has timer run out?
-
- SYNOPSIS
- int TimeUp()
-
- RETURNS
- 1 if timeup, 0 otherwise or error
-
- LAST UPDATE
- 23 January 1986
- ----------------------------------------------------------------------*/
-
- int TimeUp()
- {
-
- return(dtime <= 0L ? 1 : 0);
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- RESETTIMER - restore timer to original state
-
- SYNOPSIS
- void ResetTimer()
-
- REMARKS
- Restoring the vector contents must be atomic; hence we disable
- interrupts to prevent the CPU from responding should a timer
- interrupt occur during the process.
-
- LAST UPDATE
- 26 January 1988
- revised for new setivec() and getivec()
- ----------------------------------------------------------------------*/
-
- void ResetTimer()
- {
- int istat; /* interrupt status */
-
-
- istat = disable();
-
- setivec(TMRVEC, sizeof(orig_isr), orig_isr); /* restore vector */
-
- setalarm(-1.0); /* restore DOS timer interrupt frequency */
-
- if (istat)
- enable();
-
- }
-
-
- /***********************************************************************
- I N I T I A L I Z A T I O N R O U T I N E S
- ***********************************************************************/
-
- /*---------------------------------------------------------------------
- PROCEDURE
- INITTIMER - initialize this module
-
- SYNOPSIS
- InitTimer(ms)
- long ms;
-
- PARAMETER
- ms - milliseconds per tick
-
- REMARKS
- Interrupt vector contents are obtained and set using setivec()
- and getivec(), defined in timeinta.asm. This initialization
- routine writes directly to the 8259 interrupt controller to ensure
- that time interrupt requests are not disabled by a previos program.
-
- LAST UPDATE
- 26 January 1988
- revised for new setivec() and getivec()
- ---------------------------------------------------------------------*/
-
- void InitTimer(ms)
- long ms;
- {
- int istat; /* interrupt status */
-
-
- if (ms > MAX_MS) /* range check - cannot exceed */
- ms = MAX_MS; /* hardware constraints. */
-
- mstick = ms;
-
- orig_isr = getivec(TMRVEC); /* save original vector contents */
-
- istat = disable(); /* disable interrupts */
-
- setivec(TMRVEC, sizeof(tmrisr), (long)tmrisr);
-
- out(MASKPORT, in(MASKPORT) & ~TMRMASK); /* unmask timer IRQ */
- out(EOIPORT, TMREOI); /* send EOI just in case */
-
- setalarm((double)ms); /* set timer tick interrupt rate */
-
- if (istat) /* if interrupts were enabled before */
- enable(); /* disable() was called, enable them */
-
- }
-
-