mpu.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/mpu.cpp $*
  25. * *
  26. * $Author:: Denzil_l $*
  27. * *
  28. * $Modtime:: 8/23/01 5:07p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Get_CPU_Rate -- Fetch the rate of CPU ticks per second. *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "always.h"
  37. #include "win.h"
  38. #include "mpu.h"
  39. #include "math.h"
  40. #include <assert.h>
  41. typedef union {
  42. LARGE_INTEGER LargeInt;
  43. struct QuadPart {
  44. unsigned long LowPart;
  45. unsigned long HighPart;
  46. } QuadPart;
  47. } QuadValue;
  48. /***********************************************************************************************
  49. * Get_CPU_Rate -- Fetch the rate of CPU ticks per second. *
  50. * *
  51. * This routine will query the CPU to determine how many clock per second it is. *
  52. * *
  53. * INPUT: high -- Reference to the location that will be filled with the upper 32 bits *
  54. * of the result. *
  55. * *
  56. * OUTPUT: Returns with the lower 32 bits of the result. *
  57. * *
  58. * WARNINGS: none *
  59. * *
  60. * HISTORY: *
  61. * 05/20/1997 JLB : Created. *
  62. *=============================================================================================*/
  63. unsigned long Get_CPU_Rate(unsigned long & high)
  64. {
  65. union {
  66. LARGE_INTEGER LargeInt;
  67. struct {
  68. unsigned long LowPart;
  69. unsigned long HighPart;
  70. } QuadPart;
  71. } value;
  72. if (QueryPerformanceFrequency(&value.LargeInt)) {
  73. high = value.QuadPart.HighPart;
  74. return(value.QuadPart.LowPart);
  75. }
  76. high = 0;
  77. return(0);
  78. }
  79. unsigned long Get_CPU_Clock(unsigned long & high)
  80. {
  81. int h;
  82. int l;
  83. __asm {
  84. _emit 0Fh
  85. _emit 31h
  86. mov [h],edx
  87. mov [l],eax
  88. }
  89. high = h;
  90. return(l);
  91. }
  92. /*
  93. **
  94. ** Cut and paste job from an intel example.
  95. **
  96. **
  97. **
  98. **
  99. **
  100. **
  101. */
  102. #define ASM_RDTSC _asm _emit 0x0f _asm _emit 0x31
  103. // Max # of samplings to allow before giving up and returning current average.
  104. #define MAX_TRIES 20
  105. #define ROUND_THRESHOLD 6
  106. // # of MHz to allow samplings to deviate from average of samplings.
  107. #define TOLERANCE 1
  108. static unsigned long TSC_Low;
  109. static unsigned long TSC_High;
  110. void RDTSC(void)
  111. {
  112. _asm
  113. {
  114. ASM_RDTSC;
  115. mov TSC_Low, eax
  116. mov TSC_High, edx
  117. }
  118. }
  119. int Get_RDTSC_CPU_Speed(void)
  120. {
  121. LARGE_INTEGER t0,t1;
  122. DWORD freq=0; // Most current freq. calc.
  123. DWORD freq2=0; // 2nd most current freq. calc.
  124. DWORD freq3=0; // 3rd most current freq. calc.
  125. DWORD total; // Sum of previous three freq. calc.
  126. int tries=0; // Number of times a calculation has been
  127. // made on this call
  128. DWORD total_cycles=0, cycles; // Clock cycles elapsed during test
  129. DWORD stamp0, stamp1; // Time Stamp for beginning and end of test
  130. DWORD total_ticks=0, ticks; // Microseconds elapsed during test
  131. // DWORD current = 0; // Elapsed time during loop
  132. LARGE_INTEGER count_freq; // Hi-Res Performance Counter frequency
  133. if ( !QueryPerformanceFrequency(&count_freq) ) return(0);
  134. HANDLE process = GetCurrentProcess();
  135. DWORD processPri = GetPriorityClass(process);
  136. SetPriorityClass(process, REALTIME_PRIORITY_CLASS);
  137. HANDLE thread = GetCurrentThread();
  138. int threadPri = GetThreadPriority(thread);
  139. SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL);
  140. /*
  141. ** On processors supporting the TSC opcode, compare elapsed time on the
  142. ** High-Resolution Counter with elapsed cycles on the Time Stamp Counter.
  143. */
  144. do {
  145. /*
  146. ** This do loop runs up to 20 times or until the average of the previous
  147. ** three calculated frequencies is within 1 MHz of each of the individual
  148. ** calculated frequencies. This resampling increases the accuracy of the
  149. ** results since outside factors could affect this calculation.
  150. */
  151. tries++; // Increment number of times sampled
  152. // on this call to cpuspeed
  153. freq3 = freq2; // Shift frequencies back to make
  154. freq2 = freq; // room for new frequency measurement
  155. /*
  156. ** Get high-resolution performance counter time
  157. */
  158. QueryPerformanceCounter(&t0);
  159. t1.LowPart = t0.LowPart; // Set Initial time
  160. t1.HighPart = t0.HighPart;
  161. /*
  162. ** Loop until 50 ticks have passed since last read of hi-res counter.
  163. ** This accounts for overhead later.
  164. */
  165. while ( (DWORD)t1.LowPart - (DWORD)t0.LowPart<50) {
  166. QueryPerformanceCounter(&t1);
  167. }
  168. ASM_RDTSC;
  169. _asm mov stamp0, EAX
  170. t0.LowPart = t1.LowPart; // Reset Initial Time
  171. t0.HighPart = t1.HighPart;
  172. /*
  173. ** Loop until 1000 ticks have passed since last read of hi-res counter.
  174. ** This allows for elapsed time for sampling.
  175. */
  176. while ( (DWORD)t1.LowPart - (DWORD)t0.LowPart < 1000 ) {
  177. QueryPerformanceCounter(&t1);
  178. }
  179. ASM_RDTSC;
  180. _asm mov stamp1, EAX
  181. cycles = stamp1 - stamp0; // # of cycles passed between reads
  182. double bigticks = (double)((DWORD)t1.LowPart - (DWORD)t0.LowPart);
  183. assert((bigticks * 100000.0) > bigticks);
  184. bigticks = bigticks * 100000.0; // Convert ticks to hundred
  185. // thousandths of a tick
  186. ticks = (DWORD)(bigticks / (double)(count_freq.LowPart / 10));
  187. // Hundred Thousandths of a
  188. // Ticks / ( 10 ticks/second )
  189. // = microseconds (us)
  190. total_ticks += ticks;
  191. total_cycles += cycles;
  192. if ( (ticks % count_freq.LowPart) > (count_freq.LowPart/2) ) ticks++; // Round up if necessary
  193. freq = cycles/ticks; // MHz = cycles / us
  194. if ( cycles%ticks > ticks/2 ) freq++; // Round up if necessary
  195. total = ( freq + freq2 + freq3 ); // Total last three frequency calcs
  196. } while ( (tries < 3 ) || (tries < 20) && ((abs(3 * freq -total) > 3*TOLERANCE )|| (abs(3 * freq2-total) > 3*TOLERANCE )|| (abs(3 * freq3-total) > 3*TOLERANCE )));
  197. SetThreadPriority(thread, threadPri);
  198. SetPriorityClass(process, processPri);
  199. /*
  200. ** Try one more significant digit.
  201. */
  202. freq3 = ( total_cycles * 10 ) / total_ticks;
  203. freq2 = ( total_cycles * 100 ) / total_ticks;
  204. if ( freq2 - (freq3 * 10) >= ROUND_THRESHOLD ) freq3++;
  205. int norm_freq = total_cycles / total_ticks;
  206. freq = norm_freq * 10;
  207. if ( (freq3 - freq) >= ROUND_THRESHOLD ) norm_freq++;
  208. return (norm_freq);
  209. }