-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStructs.cs
308 lines (269 loc) · 7.08 KB
/
Structs.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Runtime.InteropServices;
namespace x86CS
{
public delegate uint ReadCallback(ushort addr, int size);
public delegate void WriteCallback(ushort addr, uint value, int size);
public enum CPURegister
{
EAX,
ECX,
EDX,
EBX,
ESP,
EBP,
ESI,
EDI,
EIP,
}
[Flags]
public enum CPUFlags : uint
{
CF = 0x0001,
Spare = 0x0002,
PF = 0x0004,
Spare2 = 0x0008,
AF = 0x0010,
Spare3 = 0x0020,
ZF = 0x0040,
SF = 0x0080,
TF = 0x0100,
IF = 0x0200,
DF = 0x0400,
OF = 0x0800,
IOPL = 0x1000,
NT = 0x4000,
Spare4 = 0x9000,
RF = 0x10000,
VM = 0x20000,
AC = 0x40000,
VIF = 0x00080000,
VIP = 0x00100000,
ID = 0x00200000,
Spare5 = 0x00400000,
Spare6 = 0x00800000,
Spare7 = 0x01000000,
Spare8 = 0x02000000,
Spare9 = 0x04000000,
Spare10 = 0x08000000,
Spare11 = 0x10000000,
Spare12 = 0x20000000,
Spare13 = 0x40000000,
Spare14 = 0x80000000,
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IDTEntry
{
private ushort baseLow;
private ushort selector;
private byte reserved;
private byte flags;
private ushort baseHigh;
public uint Base
{
get { return (uint)((baseHigh << 16) + baseLow); }
}
public ushort Selector
{
get { return selector; }
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct GDTEntry
{
private ushort limitLow;
private ushort baseLow1;
private byte baseLow2;
private sbyte flags;
private sbyte limitHighAndFlags2;
private byte baseHigh;
private uint baseAddress;
public uint Limit
{
get { return (uint)(((limitHighAndFlags2 & 0x0f) << 16) + limitLow); }
set
{
limitLow = (ushort)value;
limitHighAndFlags2 = (sbyte)(byte)((value & 0x0fffffff) >> 16);
}
}
public uint BaseAddress
{
get { return baseAddress; }
set
{
baseLow1 = (ushort)value;
baseLow2 = (byte)(value >> 16);
baseHigh = (byte)(value >> 24);
baseAddress = (uint)((baseHigh << 24) + (baseLow2 << 16) + baseLow1);
}
}
public byte PrivLevel
{
get { return (byte)(flags & 0x60); }
}
public bool IsAccessed
{
get { return ((flags & 0x1) == 0x1); }
set
{
if (value)
flags |= 0x1;
else
flags &= ~0x1;
}
}
public bool IsWritable
{
get { return !IsCode && ((flags & 0x2) == 0x2); }
set
{
if (value)
flags |= 0x2;
else
flags &= ~0x2;
}
}
public bool ReadExecute
{
get { return IsCode && ((flags & 0x2) == 0x2); }
}
public bool ExpansionConforming
{
get { return ((flags & 0x4) == 0x4); }
}
public bool IsCode
{
get { return ((flags & 0x8) == 0x8); }
set
{
if (value)
flags |= 0x8;
else
flags &= ~0x8;
}
}
public bool IsSystemDescriptor
{
get { return ((flags & 0x10) == 0); }
}
public bool IsPresent
{
get { return ((flags & 0x80) == 0x80); }
}
public bool Is32Bit
{
get { return ((limitHighAndFlags2 & 0x40) == 0x40); }
set
{
if (value)
limitHighAndFlags2 |= 0x40;
else
limitHighAndFlags2 &= ~0x40;
}
}
public bool Granularity
{
get { return ((limitHighAndFlags2 & 0x80) == 0x80); }
}
public void RefreshBase()
{
baseAddress = (uint)((baseHigh << 24) + (baseLow2 << 16) + baseLow1);
}
}
public struct Segment
{
public GDTEntry GDTEntry;
public uint Selector;
}
public struct TableRegister
{
public ushort Limit;
public uint Base;
}
[StructLayout(LayoutKind.Explicit)]
public struct Register
{
[FieldOffset(0)]
public uint DWord;
[FieldOffset(0)]
public ushort Word;
[FieldOffset(0)]
private byte Byte;
public byte HighByte
{
get { return (byte)Word.GetHigh(); }
set { Word = Word.SetHigh(value); }
}
public byte LowByte
{
get { return (byte)Word.GetLow(); }
set { Byte = value; }
}
}
public class CharEventArgs : EventArgs
{
public CharEventArgs(char charToWrite)
{
Char = charToWrite;
}
public char Char { get; set; }
}
public class UIntEventArgs : EventArgs
{
public UIntEventArgs(uint num)
{
Number = num;
}
public uint Number { get; set; }
}
public class ByteArrayEventArgs : EventArgs
{
public ByteArrayEventArgs(byte[] args)
{
ByteArray = args;
}
public byte[] ByteArray { get; set; }
}
public class TextEventArgs : EventArgs
{
public TextEventArgs(string textToWrite)
{
Text = textToWrite;
}
public string Text { get; set; }
}
public class InterruptEventArgs : EventArgs
{
public InterruptEventArgs(byte irq, byte vector)
{
IRQ = irq;
Vector = vector;
}
public byte IRQ { get; set; }
public byte Vector { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public IntPtr msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
public static class NativeMethods
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
[DllImport("user32.dll")]
public static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);
public const uint MAPVK_VK_TO_VSC = 0x00;
public const uint MAPVK_VSC_TO_VK = 0x01;
public const uint MAPVK_VK_TO_CHAR = 0x02;
public const uint MAPVK_VSC_TO_VK_EX = 0x03;
public const uint MAPVK_VK_TO_VSC_EX = 0x04;
}
}