home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / UCLOCK_.ASM < prev    next >
Assembly Source File  |  1997-07-05  |  5KB  |  160 lines

  1. ;  +++Date last modified: 05-Jul-1997
  2.  
  3.         page    55,132
  4.         title   UCLOCK_
  5.  
  6. ;  Requires MASM 5.1 or later or equivalent
  7. ;
  8. ;  Assemble with:       MASM /Mx /z ...
  9. ;                       TASM /jMASM /mx /z ...
  10.  
  11. %       .MODEL  memodel,C               ;Add model support via command
  12.                                         ;line macros, e.g.
  13.                                         ;MASM /Dmemodel=LARGE,
  14.                                         ;MASM /Dmemodel=SMALL, etc.
  15.  
  16.         PUBLIC Uclock_, SetMode2_
  17.  
  18.  
  19. Uclock_T        STRUC
  20.         count   DW      ?
  21.         lticks  DW      ?
  22.         hticks  DW      ?
  23. Uclock_T        ENDS
  24.  
  25.         .DATA
  26.         .CODE
  27.  
  28. ;=============================================================================
  29. ; This code is excerpted from:
  30. ;
  31. ; File:         PCTIM002.TXT
  32. ; Description:  FAQ / Application Note: Timing on the PC under DOS
  33. ; Author:               Kris Heidenstrom (kheidens@actrix.gen.nz)
  34. ; Version:              19950816, Release 2
  35. ;
  36. ; For additional information, ftp:
  37. ; oak.oakland.edu//SimTel/msdos/info/pctim*.zip.
  38. ;
  39. ; Quoting Section 1.3:
  40. ;
  41. ; This document (including sample code and programs) is Copyright (c)
  42. ; 1994-1995 by K. Heidenstrom.
  43. ;
  44. ; ...
  45. ;
  46. ; The sample code and sample programs may be freely used in any commercial or
  47. ; non-commercial software.
  48. ;=============================================================================
  49.  
  50.  
  51. ;=============================================================================
  52. ; Excerpted and adapted from Sample program #16
  53. ;
  54. ; typedef struct {
  55. ;       unsigned int part;
  56. ;       unsigned long ticks;
  57. ;       } Uclock_T;
  58. ;
  59. ; void Uclock_(Uclock_T *uclock_now);
  60. ;
  61. ; Func:   Return absolute timestamp (48-bit) in
  62. ;         units of 0.83809534452us since midnight
  63. ;         in the current day (range 000000000000h
  64. ;         to 001800AFFFFFh) using BIOS tick count
  65. ;         variable and CTC channel zero count in
  66. ;         progress, assuming CTC channel 0 is
  67. ;         operating in mode 2 with a reload value
  68. ;         of 0 (65536 divisor).
  69. ;
  70. ; In:     Pointer to Uclock_T buffer to be filled in
  71. ;
  72. ; Out:    Nothing
  73. ;
  74. ; Note:   This routine briefly disables then
  75. ;         enables then disables interrupts
  76. ;         regardless of the state of the
  77. ;         interrupt flag on entry.
  78. ;         It restores the original interrupt
  79. ;         flag state on exit.
  80. ;=============================================================================
  81.  
  82.  
  83. Uclock_  PROC USES BX CX DX SI DI ES,clock:ptr Uclock_T
  84.         pushf                   ; Preserve interrupt flag
  85.         xor     ax,ax           ; Zero
  86.         mov     es,ax           ; Address low memory with ES
  87.         cli
  88.         mov     si,es:[46Ch]    ; Loword of tick count
  89.         mov     di,es:[46Eh]    ; Hiword of tick count
  90.         mov     al,0            ; Latch count for CTC channel 0
  91.         out     43h,al          ; Send it
  92.         jmp     SHORT $+2       ; Delay
  93.         in      al,40h          ; Get lobyte of count
  94.         mov     ah,al           ; Save in AH
  95.         jmp     SHORT $+2       ; Delay
  96.         in      al,40h          ; Get hibyte of count
  97.         sti                     ; Make sure interrupts are enabled now
  98.         xchg    al,ah           ; Get bytes the right way round
  99.         nop                     ; Sniff for interrupt
  100.         neg     ax              ; Convert to ascending count
  101.         cli                     ; No interrupts again for reading count
  102.         mov     dx,es:[46Ch]    ; Loword of tick count again
  103.         mov     bx,es:[46Eh]    ; Hiword of tick count again
  104.         popf                    ; Restore original interrupt flag
  105.         cmp     dx,si           ; Did tick count change?
  106.         je      GotIt           ; If not, just return second tick count
  107.  
  108.         test    ax,ax           ; Is tick count low or high?
  109.         jns     GotIt           ; If low, read was just past interrupt
  110.  
  111.         mov     dx,si           ; If high, previous tick count is right
  112.         mov     bx,di           ; Get hiword of tick count too
  113.  
  114. GotIt:
  115. if    @DataSize
  116.         les     di,clock
  117.         mov     word ptr es:[di+count],ax
  118.         mov     word ptr es:[di+lticks],dx
  119.         mov     word ptr es:[di+hticks],bx
  120. else
  121.         mov     di,clock
  122.         mov     word ptr [di+count],ax
  123.         mov     word ptr [di+lticks],dx
  124.         mov     word ptr [di+hticks],bx
  125. endif
  126.         ret
  127.  
  128. Uclock_ ENDP
  129.  
  130. ; Excerpted and translated from Sample Program #15
  131. ;
  132. ; void SetMode2_(void);
  133.  
  134. SetMode2_       PROC USES DX ES BP
  135.         mov     bp,sp
  136.         xor     ax,ax                    ; Wait for tick count to change
  137.         mov     es,ax
  138.         mov     ax,word ptr es:[46Ch]
  139.         mov     word ptr [bp-2],ax
  140. @wait:
  141.         mov     ax,word ptr es:[46Ch]
  142.         cmp     ax,word ptr [bp-2]
  143.         je      short @wait
  144.         pushf                             ; Mask interrupts
  145.         cli
  146.         mov     dx,43h                    ; Set channel 0, mode 2
  147.         mov     al,34h
  148.         out     dx,al
  149.         mov     dx,40h                    ; Low word of divisor (0=>65536)
  150.         mov     al,0
  151.         out     dx,al
  152.         jmp     SHORT $+2
  153.         out     dx,al                     ; High word of divisor (0=>65536)
  154.         popf                              ; Restore interrupt mask
  155.         ret
  156.  
  157. SetMode2_       endp
  158.  
  159.         end
  160.