home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / ddjcompr / hstest / lib / mikro.asm < prev    next >
Assembly Source File  |  1990-09-24  |  4KB  |  145 lines

  1. ; \sources\tomlib\mikro.asm  17.mai.87 tom
  2. ;
  3. ; routinen fuer precise zeitmessung auf PC/AT (quelle && idee BYTE 1'87 S.157)
  4. ; precision ca. +- 2 musec
  5. ;
  6. ; long ticks_abs()  : gebe anzahl von ticks absolute zurueck
  7. ;                   : Einheit 838.096 nanosekunden
  8. ; long mikro_abs()  : gebe absolute zeit in mikrosekunden zurueck
  9. ; long mikro_since(): gebe zeit seit letztem aufruf von mikro_since()
  10. ;                   : oder mikro_diff() zurueck
  11. ; long mikro_diff() : wie mikro_since(), aber um die aufrufzeit von
  12. ;                   : mikrodiff selbst bereinigt.
  13. ;                   : i.e.
  14. ;                   :       mikro_diff();
  15. ;                   :       i = i+1;
  16. ;                   :       time = mikro_diff();
  17. ;                   : ergibt exact die zeit fuer "i = i+1;"
  18. ;
  19. ;
  20.     NAME    MIKRO
  21.  
  22. include tomlib.equ
  23.  
  24. timer_low       = 006ch
  25. bios_dataseg    = 0040h
  26. timer_mode      = 43h
  27. timer0          = 40h
  28. multconst       = 54925     ; counts in musec verwandeln
  29.  
  30.  
  31. _DATA SEGMENT
  32. lastticklow  dw 0
  33. lasttickhigh dw 0
  34. bios_timer_low_ptr dw timer_low,bios_dataseg
  35.  
  36. diffmikrosec dw 0
  37. init_label  dw offset _mikro_init
  38. _DATA ends
  39.  
  40.  
  41.  
  42. textsegment
  43.  
  44.     public _mikro_since,_mikro_diff,_mikro_abs,_ticks_abs,_mikro_init
  45.  
  46.  
  47.  
  48. _MIKRO proc near
  49.  
  50.  
  51. _mikro_init:
  52. ;
  53. ;  initialize timer for software analysis
  54. ;  kann wiederholt aufgerufen werden, initialisiert nur beim ersten mal
  55. ;
  56.  
  57.         mov     word ptr init_label,offset noinit_lab
  58.  
  59.         mov     al,00110100b    ; initialize right mode for counting
  60.         out     timer_mode,al
  61.  
  62.         xor     ax,ax
  63.         out     timer0,al
  64.         push    ax              ; Dummy : OUT nicht ueberfordern
  65.         pop     ax
  66.         out     timer0,al
  67.  
  68.         mov     diffmikrosec,0  ; see what time we get if no correction done
  69.         localcall _mikro_diff     ; initialize mikro_diff
  70.         localcall _mikro_diff     ;
  71.         mov     diffmikrosec,ax ; and store this into correction value
  72.  
  73. ticks_abs:
  74.         jmp    word ptr init_label
  75.  
  76. noinit_lab:
  77.  
  78.         les     bx,dword ptr bios_timer_low_ptr
  79. retry_ticks:
  80.         sub     al,al
  81.         mov     dx,es:[bx]      ; dx contains BIOS-maintained count
  82.  
  83.         pushf
  84.         cli
  85.         out     timer_mode,al   ; read ticks now
  86.         in      al,timer0
  87.         mov     ah,al
  88.         jmp short l3
  89. l3:     in      al,timer0
  90.         popf
  91.  
  92.         xchg    ah,al           ; ax contains count from timer
  93.         neg     ax
  94.         cmp     dx,es:[bx]      ; dx contains BIOS-maintained count
  95.         jne retry_ticks ; interrupt occured
  96.  
  97.         ret
  98.  
  99.  
  100. _MIKRO  endp
  101.  
  102. procedure _MIKRO1
  103.  
  104. _mikro_since:
  105.  
  106.         call    ticks_abs      ; get ticks to ax,dx
  107.         mov     bx,ax           ; duplicate
  108.         mov     cx,dx
  109.         xchg    bx,lastticklow
  110.         xchg    dx,lasttickhigh
  111.         sub     ax,bx           ; time = -(actticks - lastticks)
  112.         sbb     cx,dx           ; in ax:cx
  113.  
  114. convert_to_musec:
  115.         mov     bx,multconst    ; make musec from ticks
  116.         mul     bx              ; mikroseconds now in dx
  117.         mov     ax,cx           ; high part of ticks
  118.         mov     cx,dx           ; save musecs
  119.         mul     bx              ; dx:ax = musecs from ticks
  120.         add     ax,cx           ; add musecs from count
  121.         adc     dx,0            ; add carry
  122.  
  123.         ret                     ; that's it
  124.  
  125. _mikro_abs:
  126.         call    ticks_abs
  127.         mov     cx,dx
  128.         jmp     convert_to_musec
  129.  
  130. _mikro_diff:
  131.         localcall    _mikro_since        ; get mikroseconds
  132.         sub     ax,diffmikrosec     ; and correct for routine overhead
  133.         sbb     dx,0
  134.         ret
  135.  
  136. _ticks_abs:
  137.     call  ticks_abs
  138.     ret
  139.  
  140. _MIKRO1  endp
  141.  
  142. TEXTEND
  143.  
  144.     END
  145.