home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / timer / milli / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-12  |  2.0 KB  |  92 lines

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