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

  1. /***********************************************************************
  2.  
  3. FILE
  4.     timebios.c  -  how to chain routine to BIOS timer interrupt service
  5.  
  6. REMARKS
  7.     This program can only be compiled with Microsoft C version 5.00.
  8.  
  9.     It demonstrates how to chain a timer interrupt service routine
  10.     to the BIOS timer interrupt service routine.  Normally, a timer
  11.     interrupt service routine must save registers, switch to the
  12.     program data segment, service the interrupt, send EOI bytes to
  13.     the 8259 interrupt controller, restore the registers and execute
  14.     an iret instruction.  With Microsoft C version 5.00, there is a
  15.     special Microsoft specific keyword, interrupt, which tells the
  16.     compiler to generate code to switch and restore register context.
  17.     On IBM-PC's, timer interrupts vector to a BIOS timer interrupt
  18.     service routine which updates the computer's date and time-of-day
  19.     clock.  This routine then performs a software interrupt to vector
  20.     28, allowing routines to be chained to it.
  21.  
  22.     This example shows how to chain to the timer vector 28 using
  23.     Microsoft C version 5.00 and services in timeinta.asm.
  24.  
  25.         masm timeinta /ml;
  26.         cl /Gs timebios.c timeint
  27.  
  28.     You must compile with the /Gs option.  Do not use any optimization
  29.     options as there may be problems using optimizations with routines
  30.     declared with the "interrupt" keyword.
  31.  
  32.     Notice how the declarations for setivec() and getivec() differs
  33.     from those in timeintc.c.  Wierd, isn't it?
  34.  
  35. LAST UPDATE
  36.     26 January 1988
  37.  
  38.     Copyright(c) 1987-1988  D.M. Auslander and C.H. Tham
  39.  
  40. ***********************************************************************/
  41.  
  42. /***********************************************************************
  43.                             I M P O R T S
  44. ***********************************************************************/
  45.  
  46. #include "envir.h"
  47.  
  48. extern void (interrupt far *getivec(int))();
  49. extern void setivec(int, unsigned, void (interrupt far *)());
  50.  
  51.  
  52. /***********************************************************************
  53.                        P R I V A T E    D A T A
  54. ***********************************************************************/
  55.  
  56. #define VECTOR  28              /* called by BIOS timer isr */
  57.  
  58. static int tock = 0;            /* count of interrupts */
  59. static int flag = 0;            /* flag each 1 second period */
  60.  
  61.  
  62. /***********************************************************************
  63.                            R O U T I N E S
  64. ***********************************************************************/
  65.  
  66. /*----------------------------------------------------------------------
  67. PROCEDURE
  68.     TICK  -  high level timer interrupt service routine
  69.  
  70. SYNOPSIS
  71.     void interrupt far tick(void)
  72.  
  73. REMARKS
  74.     Count 18 timer interrupts (equivalent to 1 second) and set flag
  75.  
  76. LAST UPDATE
  77.     26 January 1988
  78. ----------------------------------------------------------------------*/
  79.  
  80. void interrupt far tick()
  81. {
  82.     if (++tock >= 18)
  83.         flag = 1;
  84. }
  85.  
  86.  
  87.  
  88. /*----------------------------------------------------------------------
  89. PROCEDURE
  90.     MAIN  -  the main program
  91.  
  92. LAST UPDATE
  93.     26 January 1988
  94.         take advantage of new setivec() and getivec()
  95. ----------------------------------------------------------------------*/
  96.  
  97. main()
  98. {
  99.     void (interrupt far *orig_isr)();   /* original vector */
  100.     int seconds = 0;                    /* elasped time in seconds */
  101.  
  102.  
  103.     orig_isr = getivec(VECTOR);     /* save original vector contents */
  104.  
  105.     setivec(VECTOR, sizeof(tick), tick);    /* install tmrisr() */
  106.  
  107.     while (seconds < 10)            /* not 10 seconds yet */
  108.     {
  109.         while (!flag)               /* wait for 1 second indication */
  110.             ;
  111.  
  112.         tock = 0;                   /* reset interrupt count */
  113.         flag = 0;                   /* reset flag */
  114.  
  115.         printf("%d\n", ++seconds);  /* print elasped time in seconds */
  116.     }
  117.  
  118.     setivec(VECTOR, sizeof(orig_isr), orig_isr);
  119.  
  120. }
  121.  
  122.  
  123.