cpudetect.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ** Command & Conquer Renegade(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 : wwlib *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/cpudetect.h $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 3/29/02 4:09p $*
  31. * *
  32. * $Revision:: 11 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef WWLIB_CPU_DETECT_H__
  41. #define WWLIB_CPU_DETECT_H__
  42. #include "always.h"
  43. #include "wwstring.h"
  44. class CPUDetectInitClass;
  45. class CPUDetectClass
  46. {
  47. public:
  48. typedef enum
  49. {
  50. MANUFACTURER_UNKNOWN = 0,
  51. MANUFACTURER_INTEL,
  52. MANUFACTURER_UMC,
  53. MANUFACTURER_AMD,
  54. MANUFACTURER_CYRIX,
  55. MANUFACTURER_NEXTGEN,
  56. MANUFACTURER_VIA,
  57. MANUFACTURER_RISE,
  58. MANUFACTURER_TRANSMETA
  59. } ProcessorManufacturerType;
  60. typedef enum
  61. {
  62. INTEL_PROCESSOR_UNKNOWN,
  63. INTEL_PROCESSOR_80386,
  64. INTEL_PROCESSOR_80486DX,
  65. INTEL_PROCESSOR_80486SX,
  66. INTEL_PROCESSOR_80486DX2,
  67. INTEL_PROCESSOR_80486SL,
  68. INTEL_PROCESSOR_80486SX2,
  69. INTEL_PROCESSOR_80486DX2_WB,
  70. INTEL_PROCESSOR_80486DX4,
  71. INTEL_PROCESSOR_80486DX4_WB,
  72. INTEL_PROCESSOR_PENTIUM,
  73. INTEL_PROCESSOR_PENTIUM_OVERDRIVE,
  74. INTEL_PROCESSOR_PENTIUM_MMX,
  75. INTEL_PROCESSOR_PENTIUM_PRO_SAMPLE,
  76. INTEL_PROCESSOR_PENTIUM_PRO,
  77. INTEL_PROCESSOR_PENTIUM_II_OVERDRIVE,
  78. INTEL_PROCESSOR_PENTIUM_II_MODEL_3,
  79. INTEL_PROCESSOR_PENTIUM_II_MODEL_4,
  80. INTEL_PROCESSOR_CELERON_MODEL_5,
  81. INTEL_PROCESSOR_PENTIUM_II_MODEL_5,
  82. INTEL_PROCESSOR_PENTIUM_II_XEON_MODEL_5,
  83. INTEL_PROCESSOR_CELERON_MODEL_6,
  84. INTEL_PROCESSOR_PENTIUM_III_MODEL_7,
  85. INTEL_PROCESSOR_PENTIUM_III_XEON_MODEL_7,
  86. INTEL_PROCESSOR_CELERON_MODEL_8,
  87. INTEL_PROCESSOR_PENTIUM_III_MODEL_8,
  88. INTEL_PROCESSOR_PENTIUM_III_XEON_MODEL_8,
  89. INTEL_PROCESSOR_PENTIUM_III_XEON_MODEL_A,
  90. INTEL_PROCESSOR_PENTIUM_III_MODEL_B,
  91. INTEL_PROCESSOR_PENTIUM4
  92. } IntelProcessorType;
  93. typedef enum
  94. {
  95. AMD_PROCESSOR_UNKNOWN,
  96. AMD_PROCESSOR_486DX2,
  97. AMD_PROCESSOR_486DX4,
  98. AMD_PROCESSOR_5x86,
  99. AMD_PROCESSOR_486,
  100. AMD_PROCESSOR_K5_MODEL0,
  101. AMD_PROCESSOR_K5_MODEL1,
  102. AMD_PROCESSOR_K5_MODEL2,
  103. AMD_PROCESSOR_K5_MODEL3,
  104. AMD_PROCESSOR_K6_MODEL6,
  105. AMD_PROCESSOR_K6_MODEL7,
  106. AMD_PROCESSOR_K6_2_3DNOW_MODEL8,
  107. AMD_PROCESSOR_K6_3_3DNOW_MODEL9,
  108. AMD_PROCESSOR_K6_3_PLUS,
  109. AMD_PROCESSOR_K6,
  110. AMD_PROCESSOR_ATHLON_025,
  111. AMD_PROCESSOR_ATHLON_018,
  112. AMD_PROCESSOR_DURON,
  113. AMD_PROCESSOR_ATHLON_018_IL2,
  114. AMD_PROCESSOR_ATHLON
  115. } AMDProcessorType;
  116. typedef enum
  117. {
  118. VIA_PROCESSOR_UNKNOWN,
  119. VIA_PROCESSOR_IDT_C6_WINCHIP,
  120. VIA_PROCESSOR_IDT_C6_WINCHIP2,
  121. VIA_PROCESSOR_IDT_C6_WINCHIP3,
  122. VIA_PROCESSOR_CYRIX_III_SAMUEL
  123. } VIAProcessorType;
  124. typedef enum
  125. {
  126. RISE_PROCESSOR_UNKNOWN,
  127. RISE_PROCESSOR_DRAGON_025,
  128. RISE_PROCESSOR_DRAGON_018,
  129. RISE_PROCESSOR_DRAGON2_025,
  130. RISE_PROCESSOR_DRAGON2_018
  131. } RiseProcessorType;
  132. inline static ProcessorManufacturerType Get_Processor_Manufacturer() {return ProcessorManufacturer;}
  133. static const char* Get_Processor_Manufacturer_Name();
  134. inline static bool Has_CPUID_Instruction() { return HasCPUIDInstruction; }
  135. inline static bool Has_RDTSC_Instruction() { return HasRDTSCInstruction; }
  136. inline static bool Has_CMOV_Instruction() { return HasCMOVSupport; }
  137. inline static bool Has_MMX_Instruction_Set() { return HasMMXSupport; }
  138. inline static bool Has_SSE_Instruction_Set() { return HasSSESupport; }
  139. inline static bool Has_SSE2_Instruction_Set() { return HasSSE2Support; }
  140. inline static bool Has_3DNow_Instruction_Set() { return Has3DNowSupport; }
  141. inline static bool Has_Extended_3DNow_Instruction_Set() { return HasExtended3DNowSupport; }
  142. // Call these functions after determining the manufacturer to find out which of the manufacturers processors
  143. // the system has.
  144. inline static IntelProcessorType Get_Intel_Processor() { return IntelProcessor; }
  145. inline static AMDProcessorType Get_AMD_Processor() { return AMDProcessor; }
  146. inline static VIAProcessorType Get_VIA_Processor() { return VIAProcessor; }
  147. inline static RiseProcessorType Get_Rise_Processor() { return RiseProcessor; }
  148. // Note that processor speed is only calculated at start and could change during execution, so
  149. // this number is not to be relied on!
  150. inline static int Get_Processor_Speed() { return ProcessorSpeed; }
  151. inline static __int64 Get_Processor_Ticks_Per_Second() { return ProcessorTicksPerSecond; } // Ticks per second
  152. inline static double Get_Inv_Processor_Ticks_Per_Second() { return InvProcessorTicksPerSecond; } // 1.0 / Ticks per second
  153. static unsigned Get_Feature_Bits() { return FeatureBits; }
  154. static unsigned Get_Extended_Feature_Bits() { return ExtendedFeatureBits; }
  155. // L2 cache
  156. static unsigned Get_L2_Cache_Size() { return L2CacheSize; }
  157. static unsigned Get_L2_Cache_Line_Size() { return L2CacheLineSize; }
  158. static unsigned Get_L2_Cache_Set_Associative() { return L2CacheSetAssociative; }
  159. // L1 data cache
  160. static unsigned Get_L1_Data_Cache_Size() { return L1DataCacheSize; }
  161. static unsigned Get_L1_Data_Cache_Line_Size() { return L1DataCacheLineSize; }
  162. static unsigned Get_L1_Data_Cache_Set_Associative() { return L1DataCacheSetAssociative; }
  163. // L1 instruction cache
  164. static unsigned Get_L1_Instruction_Cache_Size() { return L1InstructionCacheSize; }
  165. static unsigned Get_L1_Instruction_Cache_Line_Size() { return L1InstructionCacheLineSize; }
  166. static unsigned Get_L1_Instruction_Cache_Set_Associative() { return L1InstructionCacheSetAssociative; }
  167. // L1 instruction trace cache
  168. static unsigned Get_L1_Instruction_Trace_Cache_Size() { return L1InstructionTraceCacheSize; }
  169. static unsigned Get_L1_Instruction_Trace_Cache_Set_Associative() { return L1InstructionTraceCacheSetAssociative; }
  170. // System memory
  171. static unsigned Get_Total_Physical_Memory() { return TotalPhysicalMemory; }
  172. static unsigned Get_Available_Physical_Memory() { return AvailablePhysicalMemory; }
  173. static unsigned Get_Total_Page_File_Size() { return TotalPageMemory; }
  174. static unsigned Get_Available_Page_File_Size() { return AvailablePageMemory; }
  175. static unsigned Get_Total_Virtual_Memory() { return TotalVirtualMemory; }
  176. static unsigned Get_Available_Virtual_Memory() { return AvailableVirtualMemory; }
  177. static unsigned Get_Processor_Type() { return ProcessorType; }
  178. inline static const char* Get_Processor_String() { return ProcessorString; }
  179. inline static const StringClass& Get_Processor_Log() { return ProcessorLog; }
  180. inline static const StringClass& Get_Compact_Log() { return CompactLog; }
  181. static bool CPUID(
  182. unsigned& u_eax_,
  183. unsigned& u_ebx_,
  184. unsigned& u_ecx_,
  185. unsigned& u_edx_,
  186. unsigned cpuid_type);
  187. private:
  188. // static void Detect_CPU_Type();
  189. static void Init_CPUID_Instruction();
  190. static void Init_Processor_Speed();
  191. static void Init_Processor_String();
  192. static void Init_Processor_Manufacturer();
  193. static void Init_Processor_Family();
  194. static void Init_Processor_Features();
  195. static void Init_Memory();
  196. static void Init_OS();
  197. static void Init_Intel_Processor_Type();
  198. static void Init_AMD_Processor_Type();
  199. static void Init_VIA_Processor_Type();
  200. static void Init_Rise_Processor_Type();
  201. static void Init_Cache();
  202. static void Init_Processor_Log();
  203. static void Init_Compact_Log();
  204. static void Process_Cache_Info(unsigned value);
  205. static void Process_Extended_Cache_Info();
  206. friend class CPUDetectInitClass;
  207. static StringClass ProcessorLog;
  208. static StringClass CompactLog;
  209. static int ProcessorType;
  210. static int ProcessorFamily;
  211. static int ProcessorModel;
  212. static int ProcessorRevision;
  213. static int ProcessorSpeed;
  214. static __int64 ProcessorTicksPerSecond; // Ticks per second
  215. static double InvProcessorTicksPerSecond; // 1.0 / Ticks per second
  216. static ProcessorManufacturerType ProcessorManufacturer;
  217. static IntelProcessorType IntelProcessor;
  218. static AMDProcessorType AMDProcessor;
  219. static VIAProcessorType VIAProcessor;
  220. static RiseProcessorType RiseProcessor;
  221. static unsigned FeatureBits;
  222. static unsigned ExtendedFeatureBits;
  223. // L2 cache information
  224. static unsigned L2CacheSize;
  225. static unsigned L2CacheLineSize;
  226. static unsigned L2CacheSetAssociative;
  227. // L1 data cache information
  228. static unsigned L1DataCacheSize;
  229. static unsigned L1DataCacheLineSize;
  230. static unsigned L1DataCacheSetAssociative;
  231. // L1 instruction cache information
  232. static unsigned L1InstructionCacheSize;
  233. static unsigned L1InstructionCacheLineSize;
  234. static unsigned L1InstructionCacheSetAssociative;
  235. // L1 instruction trace cache information
  236. static unsigned L1InstructionTraceCacheSize;
  237. static unsigned L1InstructionTraceCacheSetAssociative;
  238. static unsigned TotalPhysicalMemory;
  239. static unsigned AvailablePhysicalMemory;
  240. static unsigned TotalPageMemory;
  241. static unsigned AvailablePageMemory;
  242. static unsigned TotalVirtualMemory;
  243. static unsigned AvailableVirtualMemory;
  244. static unsigned OSVersionNumberMajor;
  245. static unsigned OSVersionNumberMinor;
  246. static unsigned OSVersionBuildNumber;
  247. static unsigned OSVersionPlatformId;
  248. static StringClass OSVersionExtraInfo;
  249. static bool HasCPUIDInstruction;
  250. static bool HasRDTSCInstruction;
  251. static bool HasSSESupport;
  252. static bool HasSSE2Support;
  253. static bool HasCMOVSupport;
  254. static bool HasMMXSupport;
  255. static bool Has3DNowSupport;
  256. static bool HasExtended3DNowSupport;
  257. static char VendorID[20];
  258. static char ProcessorString[48];
  259. };
  260. struct CPUIDStruct
  261. {
  262. unsigned Eax;
  263. unsigned Ebx;
  264. unsigned Ecx;
  265. unsigned Edx;
  266. CPUIDStruct(unsigned cpuid_type)
  267. {
  268. if (!CPUDetectClass::Has_CPUID_Instruction()) {
  269. Eax=Ebx=Ecx=Edx=0;
  270. return;
  271. }
  272. CPUDetectClass::CPUID(Eax,Ebx,Ecx,Edx,cpuid_type);
  273. }
  274. };
  275. #endif // WWLIB_CPU_DETECT_H__