home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TIMER / TIMER.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  2KB  |  102 lines

  1. #include    <dos.h>
  2. #include    "timer.h"
  3. /*#pragma        inline*/
  4.  
  5. #define TimerResolution    1193L
  6.  
  7. unsigned long cardinal(long l)
  8. {
  9.     return((unsigned long)((l<0)?4294967296.0 + (long)l : (long)l));
  10. }
  11.  
  12. unsigned long
  13. elapsedtime(long start, long stop)
  14. {
  15.     return((unsigned long)(cardinal(stop - start) / TimerResolution));
  16. }
  17.  
  18. void
  19. initializetimer(void)
  20. {
  21.  
  22.   outp(0x043,0x034);
  23.   _asm\
  24.   {
  25.    jmp short NullJump1
  26.  
  27. NullJump1:;
  28.   }
  29.   outp(0x040,0x000);
  30.   _asm\
  31.   {
  32.    jmp short NullJump2
  33.  
  34. NullJump2:;
  35.   }
  36.   outp(0x040,0x000);
  37.  
  38. }
  39.  
  40. void
  41. restoretimer(void)
  42. {
  43.   outp(0x043,0x036);
  44.   _asm\
  45.   {
  46.    jmp short NullJump1
  47.  
  48. NullJump1:;
  49.   }
  50.   outp(0x040,0x000);
  51.   _asm\
  52.   {
  53.    jmp short NullJump2
  54.  
  55. NullJump2:;
  56.   }
  57.   outp(0x040,0x000);
  58.  
  59. }
  60.  
  61. /* #pragma warn -rvl*/    /* shut up Turbo C warning about return value */
  62. long
  63. readtimer(void)
  64. {
  65.   _asm\
  66.   {
  67.   cli                /* Disable interrupts */
  68.   mov  dx,020h        /* Address PIC ocw3   */
  69.   mov  al,00Ah        /* Ask to read irr    */
  70.   out  dx,al
  71.   mov  al,00h        /* Latch timer 0 */
  72.   out  043h,al
  73.   in   al,dx        /* Read irr      */
  74.   mov  di,ax        /* Save it in DI */
  75.   in   al,040h        /* Counter --> bx*/
  76.   mov  bl,al        /* LSB in BL     */
  77.   in   al,040h
  78.   mov  bh,al        /* MSB in BH     */
  79.   not  bx            /* Need ascending counter */
  80.   in   al,021h        /* Read PIC imr  */
  81.   mov  si,ax        /* Save it in SI */
  82.   mov  al,00FFh        /* Mask all interrupts */
  83.   out  021h,al
  84.   mov  ax,040h        /* read low word of time */
  85.   mov  es,ax        /* from BIOS data area   */
  86.   mov  dx,es:[06Ch]
  87.   mov  ax,si        /* Restore imr from SI   */
  88.   out  021h,al
  89.   sti                /* Enable interrupts */
  90.   mov  ax,di        /* Retrieve old irr  */
  91.   test al,001h        /* Counter hit 0?    */
  92.   jz   done            /* Jump if not       */
  93.   cmp  bx,0FFh        /* Counter > 0x0FF?    */
  94.   ja   done            /* Done if so        */
  95.   inc  dx            /* Else count int req. */
  96. done:;
  97.   mov ax,bx            /* set function result */
  98.   }
  99. }
  100. /*#pragma warn .rvl*/    /* reset Turbo C return value warning */
  101.  
  102.