home *** CD-ROM | disk | FTP | other *** search
/ Megazine / Megazine-1.iso / PROGRAMA / C / ZTIMER23 / SRC / ZTIMER / ULZTIMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-05  |  5.3 KB  |  208 lines

  1. /****************************************************************************
  2. *
  3. *                          Ultra Long Period Timer
  4. *
  5. *                   Copyright (C) 1993-4 SciTech Software
  6. *                            All rights reserved.
  7. *
  8. * Filename:        $Workfile:   ulztimer.c  $
  9. * Version:        $Revision:   1.0  $
  10. *
  11. * Language:        ANSI C
  12. * Environment:    IBM PC (MSDOS and Windows)
  13. *
  14. * Description:    Module to interface to the BIOS Timer Tick for timing
  15. *                code that takes up to 24 hours (ray tracing etc). There
  16. *                is a small overhead in calculating the time, this
  17. *                will be negligible for such long periods of time.
  18. *
  19. * $Date:   05 Feb 1996 14:50:22  $ $Author:   KendallB  $
  20. *
  21. ****************************************************************************/
  22.  
  23. #include "ztimer.h"
  24. #include "pmode.h"
  25.  
  26. #ifdef  __WINDOWS32__
  27. #undef    TRUE
  28. #undef    FALSE
  29. #define    WIN32_LEAN_AND_MEAN
  30. #include <windows.h>
  31. #include <mmsystem.h>
  32.  
  33. /* Win32 compatability routines. Win32 apps cannot program the timer and
  34.  * ToolHelp is not available. However we can use the timeGetTime() function
  35.  * to get the current time in ms since Windows started
  36.  */
  37.  
  38. PRIVATE ulong   tmStart,tmEnd;
  39. PRIVATE    ulong    start,finish;
  40.  
  41. void ZTimerInit(void) {}
  42.  
  43. void _ASMAPI LZTimerOn(void)
  44. { tmStart = timeGetTime(); }
  45.  
  46. ulong _ASMAPI LZTimerLap(void)
  47. {
  48.     ulong tmLap = timeGetTime();
  49.     return (tmLap - tmStart) * 1000L;
  50. }
  51.  
  52. void _ASMAPI LZTimerOff(void)
  53. { tmEnd = timeGetTime(); }
  54.  
  55. ulong _ASMAPI LZTimerCount(void)
  56. { return (tmEnd - tmStart) * 1000L; }
  57.  
  58. void ULZTimerOn(void)
  59. { start = timeGetTime(); }
  60.  
  61. void ULZTimerOff(void)
  62. { finish = timeGetTime(); }
  63.  
  64. ulong ULZTimerLap(void)
  65. { return (timeGetTime() - start); }
  66.  
  67. ulong ULZTimerCount(void)
  68. { return (start - finish); }
  69.  
  70. float ULZTimerResolution(void)
  71. { return 0.001; }
  72.  
  73. #elif    defined(__WINDOWS16__)
  74. #undef    TRUE
  75. #undef    FALSE
  76. #include <windows.h>
  77. #include <toolhelp.h>
  78.  
  79. /* 16 bit Windows compatability routines. Rather than reading the timer
  80.  * directly under Windows, we use the Windows API routines (which only
  81.  * provide an accuracy of 1ms however). The timer interrupts are virtualised
  82.  * under windows, which causes us to get incorrect results if we program
  83.  * the timer directly.
  84.  */
  85.  
  86. PRIVATE    TIMERINFO    tmStart,tmEnd,tmLap;
  87. PRIVATE    TIMERINFO    start,finish;
  88.  
  89. void ZTimerInit(void)
  90. {
  91.     tmStart.dwSize = tmEnd.dwSize = tmLap.dwSize = sizeof(TIMERINFO);
  92.     start.dwSize = finish.dwSize = sizeof(TIMERINFO);
  93. }
  94.  
  95. void _ASMAPI LZTimerOn(void)
  96. { TimerCount(&tmStart); }
  97.  
  98. ulong _ASMAPI LZTimerLap(void)
  99. {
  100.     TimerCount(&tmLap);
  101.     return (tmLap.dwmsSinceStart - tmStart.dwmsSinceStart) * 1000L;
  102. }
  103.  
  104. void _ASMAPI LZTimerOff(void)
  105. { TimerCount(&tmEnd); }
  106.  
  107. ulong _ASMAPI LZTimerCount(void)
  108. { return (tmEnd.dwmsSinceStart - tmStart.dwmsSinceStart) * 1000L; }
  109.  
  110. void ULZTimerOn(void)
  111. { TimerCount(&start); }
  112.  
  113. void ULZTimerOff(void)
  114. { TimerCount(&finish); }
  115.  
  116. ulong ULZTimerLap(void)
  117. {
  118.     TimerCount(&tmLap);
  119.     return (tmLap.dwmsSinceStart - start.dwmsSinceStart);
  120. }
  121.  
  122. ulong ULZTimerCount(void)
  123. { return (finish.dwmsSinceStart - start.dwmsSinceStart); }
  124.  
  125. float ULZTimerResolution(void)
  126. { return 0.001; }
  127.  
  128. #else
  129.  
  130. PRIVATE    ulong    start,finish;
  131. ushort             _ASMAPI _ZTimerBIOS;
  132.  
  133. void _ASMAPI LZ_disable(void);
  134. void _ASMAPI LZ_enable(void);
  135.  
  136. void ZTimerInit(void)
  137. { _ZTimerBIOS = PM_getBIOSSelector(); }
  138.  
  139. void ULZTimerOn(void)
  140. { start = ULZReadTime(); }
  141.  
  142. void ULZTimerOff(void)
  143. { finish = ULZReadTime(); }
  144.  
  145. ulong ULZTimerLap(void)
  146. { return ULZElapsedTime(start,ULZReadTime()); }
  147.  
  148. ulong ULZTimerCount(void)
  149. { return ULZElapsedTime(start,finish); }
  150.  
  151. ulong ULZReadTime(void)
  152. /****************************************************************************
  153. *
  154. * Function:        ULZReadTime
  155. * Returns:        Current timer tick count.
  156. *
  157. * Description:    We turn of interrupts while we
  158. *                get the value from the BIOS data area since it is stored
  159. *                as two bytes and an interrupt COULD stuff up our reading.
  160. *
  161. ****************************************************************************/
  162. {
  163.     ulong    ticks;
  164.  
  165.     LZ_disable();            /* Turn of interrupts               */
  166.     ticks = PM_getLong(_ZTimerBIOS,0x6C);
  167.     LZ_enable();             /* Turn on interrupts again         */
  168.  
  169.     return ticks;
  170. }
  171.  
  172. ulong ULZElapsedTime(ulong start,ulong finish)
  173. /****************************************************************************
  174. *
  175. * Function:        ULZElapsedTime
  176. * Parameters:    start    - Starting timer tick value
  177. *                finish    - Ending timer tick value
  178. *
  179. * Returns:        Elapsed timer between starting time and ending time in
  180. *                1/18 ths of a second.
  181. *
  182. ****************************************************************************/
  183. {
  184.     /* Check to see whether a midnight boundary has passed, and if so
  185.      * adjust the finish time to account for this. We cannot detect if
  186.      * more that one midnight boundary has passed, so if this happens
  187.      * we will be generating erronous results.
  188.      */
  189.  
  190.     if (finish < start)
  191.         finish += 1573040L;            /* Number of ticks in 24 hours        */
  192.  
  193.     return finish - start;
  194. }
  195.  
  196. float ULZTimerResolution(void)
  197. /****************************************************************************
  198. *
  199. * Function:        ULZTimerResolution
  200. * Returns:        Resolution of the ULZTimer routines (seconds in a tick)
  201. *
  202. ****************************************************************************/
  203. {
  204.     return 0.054925;
  205. }
  206.  
  207. #endif    /* DOS */
  208.