|
| 1 | +#include "gol.h" |
| 2 | + |
| 3 | +#include <windows.h> |
| 4 | + |
| 5 | +#if (defined(_MSC_VER) && defined(_M_X64)) || (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64))) |
| 6 | +#include <cpuid.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +// #ifdef _MSC_VER |
| 10 | +// #if _MSC_VER < 1300 |
| 11 | +// #define cpuid __emit 0x0f __emit 0xa2 |
| 12 | +// #endif |
| 13 | +// #endif |
| 14 | + |
| 15 | +// GLOBAL: GOLDP 0x10065ed4 |
| 16 | +HANDLE g_globalMutex; |
| 17 | + |
| 18 | +// GLOBAL: GOLDP 0x10065ed8 |
| 19 | +BOOL g_CPUID_detected; |
| 20 | + |
| 21 | +// GLOBAL: GOLDP 0x10065edc |
| 22 | +int g_maxCPUID; |
| 23 | + |
| 24 | +// GLOBAL: GOLDP 0x10065ee0 |
| 25 | +BOOL g_CPU_supports_MMX; |
| 26 | + |
| 27 | +// GLOBAL: GOLDP 0x10065ee4 |
| 28 | +char g_cpuManufacturer[13]; |
| 29 | + |
| 30 | +// STUB: GOLDP 0x10032b80 |
| 31 | +void SetGolImport(GolImport* p_import) |
| 32 | +{ |
| 33 | + // TODO |
| 34 | +} |
| 35 | + |
| 36 | +// FUNCTION: GOLDP 0x10032bf0 |
| 37 | +void LockGlobalMutex() |
| 38 | +{ |
| 39 | + if (g_globalMutex != NULL) { |
| 40 | + WaitForSingleObject(g_globalMutex, INFINITE); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// FUNCTION: GOLDP 0x10032c10 |
| 45 | +void ReleaseGlobalMutex() |
| 46 | +{ |
| 47 | + if (g_globalMutex != NULL) { |
| 48 | + ReleaseMutex(g_globalMutex); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#if defined(_M_IX86) && defined(_M_IX86) |
| 53 | +#define NAKED __declspec(naked) |
| 54 | +#else |
| 55 | +#define NAKED |
| 56 | +#endif |
| 57 | + |
| 58 | +// FUNCTION: GOLDP 0x10032c30 |
| 59 | +NAKED void DetectCPU() |
| 60 | +{ |
| 61 | +#ifdef _MSC_VER |
| 62 | +#ifdef _M_IX86 |
| 63 | + __asm { |
| 64 | + push ebx ; Save ebx register (eax, ecx, edx are caller saved) |
| 65 | + pushfd ; Push EFLAGS register on the stack |
| 66 | + pop eax ; EAX = EFLAGS value |
| 67 | + mov edx, eax ; EDX = EAX = EFLAGS value |
| 68 | + xor eax, 0x200000 ; EDX = EFLAGS value, EAX = EFLAGS ^ 0x200000 |
| 69 | + push eax ; Push modified EFLAGS value to the stack |
| 70 | + popfd ; Set the EFLAGS register to the modified value |
| 71 | + pushfd ; Push EFLAGS regsiter on the stack (again) |
| 72 | + pop eax ; EAX = current EFLAGS value; EDX = previous EFLAGS value |
| 73 | + cmp eax, edx ; Compre current and modified EFLAGS value |
| 74 | + jz no_cpuid ; If they are identical, then the CPU does not support CPUID |
| 75 | + |
| 76 | + xor eax, eax ; EAX=0: Highest Function Parameter and Manufacturer ID |
| 77 | +#if _MSC_VER > 1300 |
| 78 | + cpuid ; Run CPUID(0) [Highest Function Parameter and Manufacturer ID] |
| 79 | +#else |
| 80 | + __emit 0x0f |
| 81 | + __emit 0xa2 |
| 82 | +#endif |
| 83 | + mov [g_maxCPUID],eax ; CPUID(0) -> EAX = Highest Function Parameter (=maximum EAX for CPUID) |
| 84 | + mov dword ptr 0[g_cpuManufacturer],ebx ; CPUID(0) -> EBX = characters [0,3] of manufacturer ID |
| 85 | + xor eax,eax ; EAX = 0 |
| 86 | + mov dword ptr 4[g_cpuManufacturer],edx ; CPUID(0) -> EDX = characters [4,7] of manufacturer ID |
| 87 | + mov 12[g_cpuManufacturer], al ; Set terminating zero |
| 88 | + inc eax ; EAX = 1 |
| 89 | + mov dword ptr 8[g_cpuManufacturer],ecx ; CPUID(0) -> ECX = characters [8,12] of manufacturer ID |
| 90 | + mov [g_CPUID_detected],eax ; Store we succesfully detected cpuid |
| 91 | + |
| 92 | +#if _MSC_VER > 1300 |
| 93 | + cpuid ; Run CPUID(1) [Processor Info and Feature Bits] |
| 94 | +#else |
| 95 | + __emit 0x0f |
| 96 | + __emit 0xa2 |
| 97 | +#endif |
| 98 | + and edx,0x800000 ; Mask bit 23 of EDX: mmx feature (=64-bit SIMD) |
| 99 | + shr edx,0x17 ; Convert mmx feature bit to boolean |
| 100 | + mov dword ptr [g_CPU_supports_MMX],edx ; Store MMX feature |
| 101 | + |
| 102 | + no_cpuid: |
| 103 | + pop ebx ; Restore EBX |
| 104 | + ret ; Return to caller |
| 105 | + } |
| 106 | +#elif defined(_M_IX64) |
| 107 | + int cpuinfo[4]; |
| 108 | + _cpuid(cpuinfo, 0); |
| 109 | + g_maxCPUID = cpuinfo[0]; |
| 110 | + memcpy(&g_cpuManufacturer[0], &cpuinfo[1], 4); |
| 111 | + memcpy(&g_cpuManufacturer[4], &cpuinfo[3], 4); |
| 112 | + memcpy(&g_cpuManufacturer[8], &cpuinfo[2], 4); |
| 113 | + g_cpuManufacturer[12] = '\0'; |
| 114 | + _cpuid(cpuinfo, 1); |
| 115 | + g_CPU_supports_MMX = !!(cpuinfo[3] & 0x800000); |
| 116 | +#endif |
| 117 | +#elif defined(__i386__) || defined(x86_64) |
| 118 | + unsigned int eax; |
| 119 | + unsigned int ebx; |
| 120 | + unsigned int ecx; |
| 121 | + unsigned int edx; |
| 122 | + __get_cpuid(0, &eax, &ebx, &ecx, &edx); |
| 123 | + g_maxCPUID = eax; |
| 124 | + memcpy(&g_cpuManufacturer[0], &ebx, 4); |
| 125 | + memcpy(&g_cpuManufacturer[4], &edx, 4); |
| 126 | + memcpy(&g_cpuManufacturer[8], &ecx, 4); |
| 127 | + g_cpuManufacturer[12] = '\0'; |
| 128 | + __get_cpuid(1, &eax, &ebx, &ecx, &edx); |
| 129 | + g_CPU_supports_MMX = !!(edx & 0x800000); |
| 130 | +#endif |
| 131 | +} |
0 commit comments