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