TIMERINI.CPP (14264B)
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 115 // 116 // Install the timer callback event handler 117 // 118 //TimerHandle = timeSetEvent ( 1000/freq , 1 , Timer_Callback , 0 , TIME_PERIODIC); 119 // Add TIME_KILL_SYNCHRONOUS flag. ST - 2/13/2019 5:07PM 120 TimerHandle = timeSetEvent ( 1000/freq , 1 , Timer_Callback , 0 , TIME_PERIODIC | TIME_KILL_SYNCHRONOUS); 121 TimerSystemOn = success = ( TimerHandle !=0 ); 122 123 if (success) { 124 if (!partial) { 125 WindowsTimer=this; 126 TickCount.Start(); 127 } 128 }else{ 129 char error_str [128]; 130 sprintf (error_str, "Error - timer system failed to start. Error code %d\n", GetLastError()); 131 OutputDebugString(error_str); 132 } 133 } 134 135 136 137 /*************************************************************************** 138 * WinTimerClass::~WinTimerClass -- Removes the timer system. * 139 * * 140 * * 141 * INPUT: NONE. * 142 * * 143 * OUTPUT: BOOL was it removed successfuly * 144 * * 145 * WARNINGS: * 146 * * 147 * HISTORY: * 148 * 10/5/95 3:47PM : ST Created. * 149 *=========================================================================*/ 150 WinTimerClass::~WinTimerClass( void ) 151 { 152 153 if ( TimerHandle ){ 154 timeKillEvent ( TimerHandle ); 155 TimerHandle = 0; //ST - 2/13/2019 5:12PM 156 } 157 158 TimerSystemOn = FALSE; 159 timeEndPeriod ( 1000/Frequency ); 160 } 161 162 163 164 165 166 /*********************************************************************************************** 167 * Timer_Callback -- Main timer callback. Equivalent to a timer interrupt handler * 168 * * 169 * * 170 * * 171 * INPUT: uint timer ID * 172 * uint reserved * 173 * long 0 (application defined) * 174 * long reserved * 175 * long reserved * 176 * * 177 * OUTPUT: Nothing * 178 * * 179 * WARNINGS: None * 180 * * 181 * HISTORY: * 182 * 10/5/95 3:19PM ST : Created * 183 *=============================================================================================*/ 184 185 186 void CALLBACK Timer_Callback (UINT , UINT , DWORD , DWORD , DWORD) 187 { 188 //CONTEXT context; 189 190 InTimerCallback++; 191 192 // Removed. ST - 2/13/2019 5:11PM 193 //if (!TimerThreadHandle){ 194 // DuplicateHandle (GetCurrentProcess(), GetCurrentThread() , GetCurrentProcess() ,&TimerThreadHandle , 0 , TRUE , DUPLICATE_SAME_ACCESS); 195 //} 196 197 198 if (WindowsTimer) { 199 WindowsTimer->Update_Tick_Count(); 200 } 201 InTimerCallback--; 202 } 203 204 205 206 207 208 209 /*********************************************************************************************** 210 * WinTimerClass::Update_Tick_Count -- update westwood timers * 211 * * 212 * * 213 * * 214 * INPUT: Nothing * 215 * * 216 * OUTPUT: Nothing * 217 * * 218 * WARNINGS: None * 219 * * 220 * HISTORY: * 221 * 10/5/95 3:58PM ST : Created * 222 *=============================================================================================*/ 223 224 void WinTimerClass::Update_Tick_Count ( void ) 225 { 226 /* 227 * 228 * Increment westwood timers 229 * 230 */ 231 SysTicks++; 232 UserTicks++; 233 234 } 235 236 237 238 239 240 241 242 243 /* 244 ;*************************************************************************** 245 ;* GET_NUM_INTERRUPTS -- Returns the number of interrupts that have occured* 246 ;* * 247 ;* INPUT: TRUE - returns num RM ints. * 248 ;* FALSE - return num PM ints. * 249 ;* * 250 ;* OUTPUT: * 251 ;* * 252 ;* WARNINGS: * 253 ;* * 254 ;* HISTORY: * 255 ;* 07/12/1994 SKB : Created. * 256 ;*=========================================================================* 257 PROC Get_Num_Interrupts C Near 258 USES esi 259 ARG realmode:DWORD 260 261 mov esi,[RealModePtr] 262 cmp [realmode],0 263 je ??prot_mode 264 mov eax,[(TimerType PTR esi).NumRMInts] 265 ret 266 ??prot_mode: 267 mov eax,[(TimerType PTR esi).NumPMInts] 268 ret 269 270 ENDP 271 */ 272 273 274 275 276 /*********************************************************************************************** 277 * WinTimerClass::Get_System_Tick_Count -- returns the system tick count * 278 * * 279 * INPUT: Nothing * 280 * * 281 * OUTPUT: tick count * 282 * * 283 * WARNINGS: None * 284 * * 285 * HISTORY: * 286 * 10/5/95 4:02PM ST : Created * 287 *=============================================================================================*/ 288 289 unsigned WinTimerClass::Get_System_Tick_Count ( void ) 290 { 291 return ( SysTicks ); 292 } 293 294 295 296 /*********************************************************************************************** 297 * WinTimerClass::Get_User_Tick_Count -- returns the user tick count * 298 * * 299 * INPUT: Nothing * 300 * * 301 * OUTPUT: tick count * 302 * * 303 * WARNINGS: None * 304 * * 305 * HISTORY: * 306 * 10/5/95 4:02PM ST : Created * 307 *=============================================================================================*/ 308 309 unsigned WinTimerClass::Get_User_Tick_Count ( void ) 310 { 311 return ( UserTicks ); 312 }