-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
DataInterface.cs
186 lines (164 loc) · 6.13 KB
/
DataInterface.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ZenStates
{
/// <summary>
/// Description of DataInterface.
/// </summary>
public class DataInterface
{
// Constants
public const UInt64 ServiceVersion = 10; // 0 = 0.1/b, 1 = 0.2, 2 = 0.2.x, 3 = 0.3, 4 = 0.3.xxxx, 5 = 0.4/0.5, 6 = 0.5, 7 = 0.6, 8 = 0.7, 9 = 0.7.1. 10 = 0.8.0
private const int bufferSize = 16;
private const string mmf_path = @"Global\ZenStates";
// Memory register map
public const int REG_P0 = 0x00;
public const int REG_P1 = 0x01;
public const int REG_P2 = 0x02;
/*public const int REG_P3 = 0x03;
public const int REG_P4 = 0x04;
public const int REG_P5 = 0x05;
public const int REG_P6 = 0x06;
public const int REG_P7 = 0x07;*/
public const int REG_PERF_BIAS = 0x03;
public const int REG_TEMP = 0x04;
public const int REG_PPT = 0x05;
public const int REG_TDC = 0x06;
public const int REG_EDC = 0x07;
public const int REG_SERVER_FLAGS = 0x08;
public const int REG_CLIENT_FLAGS = 0x09;
public const int REG_PING_PONG = 0x0A;
public const int REG_NOTIFY_STATUS = 0x0B;
public const int REG_SERVER_VERSION = 0x0C;
public const int REG_SCALAR = 0x0D;
public const int REG_SMU_VERSION = 0x0E;
public const int REG_PSTATE_OC = 0x0F;
public const int REG_CPU_TYPE = 0x10;
public const int REG_BOOST_FREQ_0 = 0x11;
public const int REG_BOOST_FREQ_1 = 0x12;
public const UInt64 FLAG_IS_AVAILABLE = 1 << 0;
public const UInt64 FLAG_SUPPORTED_CPU = 1 << 1;
public const UInt64 FLAG_SETTINGS_RESET = 1 << 2;
public const UInt64 FLAG_TRAY_ICON_AT_START = 1 << 3;
public const UInt64 FLAG_APPLY_AT_START = 1 << 4;
public const UInt64 FLAG_SETTINGS_SAVED = 1 << 5;
public const UInt64 FLAG_C6CORE = 1 << 6;
public const UInt64 FLAG_C6PACKAGE = 1 << 7;
public const UInt64 FLAG_SHUTDOWN_UNCLEAN = 1 << 8;
public const UInt64 FLAG_P80_TEMP_EN = 1 << 9;
public const UInt64 FLAG_CPB = 1 << 10;
public const UInt64 FLAG_OC = 1 << 11;
public const UInt64 NOTIFY_CLEAR = 0x00;
public const UInt64 NOTIFY_CLIENT_FLAGS = 0x01;
public const UInt64 NOTIFY_RESTORE = 0x02;
public const UInt64 NOTIFY_APPLY = 0x03;
public const UInt64 NOTIFY_SAVE = 0x04;
public const UInt64 NOTIFY_DONE = 0x80;
// Local vars
private MemoryMappedFile mmf;
private UInt64[] buffer;
public DataInterface(bool server)
{
try
{
buffer = new UInt64[bufferSize];
if (server)
{
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var security = new MemoryMappedFileSecurity();
security.AddAccessRule(new AccessRule<MemoryMappedFileRights>(everyone, MemoryMappedFileRights.FullControl, AccessControlType.Allow));
mmf = MemoryMappedFile.CreateOrOpen(mmf_path, bufferSize * 8,
MemoryMappedFileAccess.ReadWrite,
MemoryMappedFileOptions.DelayAllocatePages, security,
HandleInheritability.Inheritable);
}
else
{
mmf = MemoryMappedFile.OpenExisting(mmf_path);
}
}
catch (Exception ex)
{
throw new System.ApplicationException("Error initializing memory interface " + ex.Message);
}
}
public bool MemWrite(int offset, UInt64 data)
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
try
{
stream.Seek(offset * 8, SeekOrigin.Begin);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(data);
return true;
}
catch
{
return false;
}
}
}
public bool MemWriteAll()
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
try
{
stream.Seek(0, SeekOrigin.Begin);
BinaryWriter writer = new BinaryWriter(stream);
for (int i = 0; i < bufferSize; i++) writer.Write(buffer[i]);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public UInt64 MemRead(int offset)
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
try
{
stream.Seek(offset * 8, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
return reader.ReadUInt64();
}
catch (Exception ex)
{
return 0;
}
}
}
public UInt64[] MemReadAll()
{
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
try
{
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
for (int i = 0; i < bufferSize; i++) buffer[i] = reader.ReadUInt64();
return buffer;
}
catch (Exception ex)
{
return null;
}
}
}
public void BufferWrite(int offset, UInt64 data)
{
buffer[offset] = data;
}
public UInt64 BufferRead(int offset)
{
return buffer[offset];
}
}
}