1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #include "NstSystemInfo.hpp"
26 #include <windows.h>
27 
28 namespace Nestopia
29 {
30 	namespace System
31 	{
32 		namespace Info
33 		{
GetCpuCount()34 			uint GetCpuCount()
35 			{
36 				struct Stored
37 				{
38 					uint count;
39 
40 					Stored()
41 					{
42 						SYSTEM_INFO sysinfo;
43 						::GetSystemInfo( &sysinfo );
44 						count = NST_MIN(sysinfo.dwNumberOfProcessors,1);
45 					}
46 				};
47 
48 				static const Stored stored;
49 				return stored.count;
50 			}
51 
GetCpuSpeed()52 			uint GetCpuSpeed()
53 			{
54 				struct Stored
55 				{
56 					uint mHz;
57 
58 					Stored()
59 					: mHz(0)
60 					{
61 						HKEY hKey;
62 
63 						if
64 						(
65 							ERROR_SUCCESS == ::RegOpenKeyEx
66 							(
67 								HKEY_LOCAL_MACHINE,
68 								L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
69 								0,
70 								KEY_QUERY_VALUE,
71 								&hKey
72 							)
73 						)
74 						{
75 							DWORD data, size;
76 
77 							if
78 							(
79 								ERROR_SUCCESS == ::RegQueryValueEx
80 								(
81 									hKey,
82 									L"~MHz",
83 									NULL,
84 									&data,
85 									NULL,
86 									&size
87 								)
88 								&& data == REG_DWORD
89 								&& size == sizeof(DWORD)
90 								&& ERROR_SUCCESS == ::RegQueryValueEx
91 								(
92 									hKey,
93 									L"~MHz",
94 									NULL,
95 									NULL,
96 									reinterpret_cast<BYTE*>(&data),
97 									&size
98 								)
99 							)
100 								mHz = data;
101 
102 							::RegCloseKey( hKey );
103 						}
104 					}
105 				};
106 
107 				static const Stored stored;
108 				return stored.mHz;
109 			}
110 		}
111 	}
112 }
113