TIMERINI.CPP 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : Temp timer for 32bit lib *
  19. * *
  20. * File Name : TIMERINI.CPP *
  21. * *
  22. * Programmer : Scott K. Bowen *
  23. * *
  24. * Start Date : July 6, 1994 *
  25. * *
  26. * Last Update : July 6, 1994 [SKB] *
  27. * *
  28. *-------------------------------------------------------------------------*
  29. * Functions: *
  30. * Init_Timer_System -- Initialize the WW timer system. *
  31. * Remove_Timer_System -- Removes the timer system. *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. #include <wwstd.h>
  34. #include <mmsystem.h>
  35. #include "timer.H"
  36. #include <profile.h>
  37. #include <stdio.h>
  38. /////////////////////////////////////////////////////////////////////////////////
  39. /////////////////////////////////// Defines /////////////////////////////////////
  40. #define COPY_FROM_MEM TRUE
  41. /////////////////////////////////////////////////////////////////////////////////
  42. ////////////////////////////// timera.asm functions//////////////////////////////
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. extern BOOL Install_Timer_Interrupt(VOID *bin_ptr, UINT rm_size, UINT freq, BOOL partial);
  47. extern BOOL Remove_Timer_Interrupt(VOID);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. /////////////////////////////////////////////////////////////////////////////////
  52. /////////////////////////////// Global Data /////////////////////////////////////
  53. BOOL TimerSystemOn = FALSE;
  54. // Global timers that the library or user can count on existing.
  55. TimerClass TickCount(BT_SYSTEM);
  56. CountDownTimerClass CountDown(BT_SYSTEM, 0);
  57. // Prototype for timer callback
  58. void CALLBACK Timer_Callback ( UINT event_id, UINT res1 , DWORD user, DWORD res2, DWORD res3 );
  59. HANDLE TimerThreadHandle = 0; //Handle of timer thread
  60. int InTimerCallback = 0; //Flag to say if we are in a timer callback
  61. /////////////////////////////////////////////////////////////////////////////////
  62. /////////////////////////////////// Code ////////////////////////////////////////
  63. #pragma warning (disable : 4996)
  64. /***************************************************************************
  65. * WinTimerClass::WinTimerClass -- Initialize the WW timer system. *
  66. * *
  67. * *
  68. * INPUT: UINT : user timer frequency. *
  69. * *
  70. * OUTPUT: *
  71. * *
  72. * WARNINGS: *
  73. * *
  74. * HISTORY: *
  75. * 10/5/95 3:47PM : ST Created. *
  76. *=========================================================================*/
  77. WinTimerClass::WinTimerClass (UINT freq, BOOL partial)
  78. {
  79. BOOL success;
  80. //
  81. // Inform windows that we want a higher than normal
  82. // timer resolution
  83. //
  84. #ifdef __SW_EP
  85. timeBeginPeriod(1000/PROFILE_RATE);
  86. Frequency = PROFILE_RATE;
  87. #else
  88. timeBeginPeriod ( 1000/freq );
  89. Frequency = freq;
  90. #endif
  91. SysTicks = 0;
  92. UserTicks = 0;
  93. //
  94. // Install the timer callback event handler
  95. //
  96. //TimerHandle = timeSetEvent ( 1000/freq , 1 , Timer_Callback , 0 , TIME_PERIODIC);
  97. // Add TIME_KILL_SYNCHRONOUS flag. ST - 2/13/2019 5:07PM
  98. TimerHandle = timeSetEvent ( 1000/freq , 1 , Timer_Callback , 0 , TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
  99. TimerSystemOn = success = ( TimerHandle !=0 );
  100. if (success) {
  101. if (!partial) {
  102. WindowsTimer=this;
  103. TickCount.Start();
  104. }
  105. }else{
  106. char error_str [128];
  107. sprintf (error_str, "Error - timer system failed to start. Error code %d\n", GetLastError());
  108. OutputDebugString(error_str);
  109. }
  110. }
  111. /***************************************************************************
  112. * WinTimerClass::~WinTimerClass -- Removes the timer system. *
  113. * *
  114. * *
  115. * INPUT: NONE. *
  116. * *
  117. * OUTPUT: BOOL was it removed successfuly *
  118. * *
  119. * WARNINGS: *
  120. * *
  121. * HISTORY: *
  122. * 10/5/95 3:47PM : ST Created. *
  123. *=========================================================================*/
  124. WinTimerClass::~WinTimerClass( void )
  125. {
  126. if ( TimerHandle ){
  127. timeKillEvent ( TimerHandle );
  128. TimerHandle = 0; //ST - 2/13/2019 5:12PM
  129. }
  130. TimerSystemOn = FALSE;
  131. timeEndPeriod ( 1000/Frequency );
  132. }
  133. /***********************************************************************************************
  134. * Timer_Callback -- Main timer callback. Equivalent to a timer interrupt handler *
  135. * *
  136. * *
  137. * *
  138. * INPUT: uint timer ID *
  139. * uint reserved *
  140. * long 0 (application defined) *
  141. * long reserved *
  142. * long reserved *
  143. * *
  144. * OUTPUT: Nothing *
  145. * *
  146. * WARNINGS: None *
  147. * *
  148. * HISTORY: *
  149. * 10/5/95 3:19PM ST : Created *
  150. *=============================================================================================*/
  151. void CALLBACK Timer_Callback (UINT , UINT , DWORD , DWORD , DWORD)
  152. {
  153. //CONTEXT context;
  154. InTimerCallback++;
  155. // Removed. ST - 2/13/2019 5:11PM
  156. //if (!TimerThreadHandle){
  157. // DuplicateHandle (GetCurrentProcess(), GetCurrentThread() , GetCurrentProcess() ,&TimerThreadHandle , 0 , TRUE , DUPLICATE_SAME_ACCESS);
  158. //}
  159. if (WindowsTimer) {
  160. WindowsTimer->Update_Tick_Count();
  161. }
  162. InTimerCallback--;
  163. }
  164. /***********************************************************************************************
  165. * WinTimerClass::Update_Tick_Count -- update westwood timers *
  166. * *
  167. * *
  168. * *
  169. * INPUT: Nothing *
  170. * *
  171. * OUTPUT: Nothing *
  172. * *
  173. * WARNINGS: None *
  174. * *
  175. * HISTORY: *
  176. * 10/5/95 3:58PM ST : Created *
  177. *=============================================================================================*/
  178. void WinTimerClass::Update_Tick_Count ( void )
  179. {
  180. /*
  181. *
  182. * Increment westwood timers
  183. *
  184. */
  185. SysTicks++;
  186. UserTicks++;
  187. }
  188. /*
  189. ;***************************************************************************
  190. ;* GET_NUM_INTERRUPTS -- Returns the number of interrupts that have occured*
  191. ;* *
  192. ;* INPUT: TRUE - returns num RM ints. *
  193. ;* FALSE - return num PM ints. *
  194. ;* *
  195. ;* OUTPUT: *
  196. ;* *
  197. ;* WARNINGS: *
  198. ;* *
  199. ;* HISTORY: *
  200. ;* 07/12/1994 SKB : Created. *
  201. ;*=========================================================================*
  202. PROC Get_Num_Interrupts C Near
  203. USES esi
  204. ARG realmode:DWORD
  205. mov esi,[RealModePtr]
  206. cmp [realmode],0
  207. je ??prot_mode
  208. mov eax,[(TimerType PTR esi).NumRMInts]
  209. ret
  210. ??prot_mode:
  211. mov eax,[(TimerType PTR esi).NumPMInts]
  212. ret
  213. ENDP
  214. */
  215. /***********************************************************************************************
  216. * WinTimerClass::Get_System_Tick_Count -- returns the system tick count *
  217. * *
  218. * INPUT: Nothing *
  219. * *
  220. * OUTPUT: tick count *
  221. * *
  222. * WARNINGS: None *
  223. * *
  224. * HISTORY: *
  225. * 10/5/95 4:02PM ST : Created *
  226. *=============================================================================================*/
  227. unsigned WinTimerClass::Get_System_Tick_Count ( void )
  228. {
  229. return ( SysTicks );
  230. }
  231. /***********************************************************************************************
  232. * WinTimerClass::Get_User_Tick_Count -- returns the user tick count *
  233. * *
  234. * INPUT: Nothing *
  235. * *
  236. * OUTPUT: tick count *
  237. * *
  238. * WARNINGS: None *
  239. * *
  240. * HISTORY: *
  241. * 10/5/95 4:02PM ST : Created *
  242. *=============================================================================================*/
  243. unsigned WinTimerClass::Get_User_Tick_Count ( void )
  244. {
  245. return ( UserTicks );
  246. }