home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP2 / TIME1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  5.9 KB  |  238 lines

  1. /**********************************************************************
  2.  
  3. FILE
  4.     time1.c  -  simple timer routine that uses real time
  5.  
  6. ENTRY ROUTINES
  7.     SetTimer    -  set timer interval
  8.     TimeUp      -  has timer run out?
  9.     ResetTimer  -  restore timer to default state
  10.  
  11.     tick        -  timer interrupt service routine
  12.  
  13. INITIALIZATION ROUTINE
  14.     InitTimer  -  initialize time module
  15.  
  16. REMARKS
  17.     Need to be linked with alarm.c and timeinta.asm
  18.  
  19. LAST UPDATE
  20.     23 January 1986
  21.         fix minor LINT complaints
  22.     26 January 1988
  23.         revise to used getivec() and new version of setivec()
  24.  
  25.     Copyright(c) 1986-1988  D.M. Auslander and C.H. Tham
  26.  
  27. ***********************************************************************/
  28.  
  29. /***********************************************************************
  30.                             I M P O R T S
  31. ***********************************************************************/
  32.  
  33. #include "envir.h"
  34. #include "8259.h"
  35. #include "inout.h"
  36. #include "alarm.h"
  37.  
  38. /*
  39.  *  The following routines are to be found in timeinta.asm
  40.  */
  41.  
  42. #ifdef ANSI
  43.  
  44. extern void     tmrisr(void);
  45. extern void     setivec(int, unsigned, long);
  46. extern long     getivec(int);
  47. extern int      enable(void);
  48. extern int      disable(void);
  49.  
  50. #else
  51.  
  52. extern void     tmrisr();
  53. extern void     setivec();
  54. extern long     getivec();
  55. extern int      enable();
  56. extern int      disable();
  57.  
  58. #endif
  59.  
  60.  
  61. /***********************************************************************
  62.                     P R I V A T E    D A T A
  63. ***********************************************************************/
  64.  
  65. static long mstick;             /* milliseconds per tick */
  66. static long dtime;              /* count down in ticks */
  67. static long orig_isr;           /* pointer to original timer isr */
  68.  
  69.  
  70. /***********************************************************************
  71.                     E N T R Y    R O U T I N E S
  72. ***********************************************************************/
  73.  
  74. /*----------------------------------------------------------------------
  75. PROCEDURE
  76.     TICK  -  timer interrupt service routine
  77.  
  78. SYNOPSIS
  79.     called by timer interrupt
  80.  
  81. REMARKS
  82.     Interrupts are enabled when tick is called.  However, we know
  83.     that the low level isr, tmrisr() will not send an EOI to the
  84.     8259 until this routine is done, interrupts are effectively
  85.     disabled since the timer interrupt has the highest priority.
  86.  
  87. LAST UPDATE
  88.     6 June 1985
  89. ----------------------------------------------------------------------*/
  90.  
  91. void tick()
  92. {
  93.  
  94.     if (dtime > 0L)
  95.         --dtime;
  96. }
  97.  
  98.  
  99.  
  100. /*---------------------------------------------------------------------
  101. PROCEDURE
  102.     SETTIMER  -  set timer
  103.  
  104. SYNOPSIS
  105.     void SetTimer(ms)
  106.     long ms;
  107.  
  108. PARAMETER
  109.     ms  -  time interval in milliseconds
  110.  
  111. REMARKS
  112.     (mstick >> 1) is equivalent to mstick/2, thus the formula
  113.     is equivalent to: dtime = (ms / mstick)  +  0.5
  114.  
  115. LAST UPDATE
  116.     5 June 1985
  117. ---------------------------------------------------------------------*/
  118.  
  119. void SetTimer(ms)
  120. long ms;
  121. {
  122.  
  123.     dtime = (ms + (mstick >> 1)) / mstick;
  124.  
  125. }
  126.  
  127.  
  128.  
  129. /*----------------------------------------------------------------------
  130. FUNCTION
  131.     TIMEUP  -  has timer run out?
  132.  
  133. SYNOPSIS
  134.     int TimeUp()
  135.  
  136. RETURNS
  137.     1  if timeup, 0 otherwise or error
  138.  
  139. LAST UPDATE
  140.     23 January 1986
  141. ----------------------------------------------------------------------*/
  142.  
  143. int TimeUp()
  144. {
  145.  
  146.     return(dtime <= 0L ? 1 : 0);
  147. }
  148.  
  149.  
  150.  
  151. /*----------------------------------------------------------------------
  152. PROCEDURE
  153.     RESETTIMER  -  restore timer to original state
  154.  
  155. SYNOPSIS
  156.     void ResetTimer()
  157.  
  158. REMARKS
  159.     Restoring the vector contents must be atomic; hence we disable
  160.     interrupts to prevent the CPU from responding should a timer
  161.     interrupt occur during the process.
  162.  
  163. LAST UPDATE
  164.     26 January 1988
  165.         revised for new setivec() and getivec()
  166. ----------------------------------------------------------------------*/
  167.  
  168. void ResetTimer()
  169. {
  170.     int istat;      /* interrupt status */
  171.  
  172.  
  173.     istat = disable();
  174.  
  175.     setivec(TMRVEC, sizeof(orig_isr), orig_isr);    /* restore vector */
  176.  
  177.     setalarm(-1.0);         /* restore DOS timer interrupt frequency */
  178.  
  179.     if (istat)
  180.         enable();
  181.  
  182. }
  183.  
  184.  
  185. /***********************************************************************
  186.             I N I T I A L I Z A T I O N    R O U T I N E S
  187. ***********************************************************************/
  188.  
  189. /*---------------------------------------------------------------------
  190. PROCEDURE
  191.     INITTIMER  -  initialize this module
  192.  
  193. SYNOPSIS
  194.     InitTimer(ms)
  195.     long ms;
  196.  
  197. PARAMETER
  198.     ms  -  milliseconds per tick
  199.  
  200. REMARKS
  201.     Interrupt vector contents are obtained and set using setivec()
  202.     and getivec(), defined in timeinta.asm.  This initialization
  203.     routine writes directly to the 8259 interrupt controller to ensure
  204.     that time interrupt requests are not disabled by a previos program.
  205.  
  206. LAST UPDATE
  207.     26 January 1988
  208.         revised for new setivec() and getivec()
  209. ---------------------------------------------------------------------*/
  210.  
  211. void InitTimer(ms)
  212. long ms;
  213. {
  214.     int istat;      /* interrupt status */
  215.  
  216.  
  217.     if (ms > MAX_MS)                /* range check - cannot exceed */
  218.         ms = MAX_MS;                /*  hardware constraints.      */
  219.  
  220.     mstick = ms;
  221.  
  222.     orig_isr = getivec(TMRVEC);     /* save original vector contents */
  223.  
  224.     istat = disable();              /* disable interrupts */
  225.  
  226.     setivec(TMRVEC, sizeof(tmrisr), (long)tmrisr);
  227.  
  228.     out(MASKPORT, in(MASKPORT) & ~TMRMASK);     /* unmask timer IRQ */
  229.     out(EOIPORT, TMREOI);                       /* send EOI just in case */
  230.  
  231.     setalarm((double)ms);       /* set timer tick interrupt rate */
  232.  
  233.     if (istat)                  /* if interrupts were enabled before  */
  234.         enable();               /*  disable() was called, enable them */
  235.  
  236. }
  237.  
  238.