home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / tctimer.arc / TCTIMER.C next >
Text File  |  1989-01-26  |  2KB  |  86 lines

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