[FAST486]
[reactos.git] / include / reactos / libs / fast486 / fast486.h
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486.h
4 *
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #ifndef _FAST486_H_
23 #define _FAST486_H_
24
25 /* DEFINES ********************************************************************/
26
27 #define FAST486_NUM_GEN_REGS 8
28 #define FAST486_NUM_SEG_REGS 6
29 #define FAST486_NUM_CTRL_REGS 3
30 #define FAST486_NUM_DBG_REGS 6
31
32 #define FAST486_CR0_PE (1 << 0)
33 #define FAST486_CR0_MP (1 << 1)
34 #define FAST486_CR0_EM (1 << 2)
35 #define FAST486_CR0_TS (1 << 3)
36 #define FAST486_CR0_ET (1 << 4)
37 #define FAST486_CR0_NE (1 << 5)
38 #define FAST486_CR0_WP (1 << 16)
39 #define FAST486_CR0_AM (1 << 18)
40 #define FAST486_CR0_NW (1 << 29)
41 #define FAST486_CR0_CD (1 << 30)
42 #define FAST486_CR0_PG (1 << 31)
43
44 #define FAST486_DR4_B0 (1 << 0)
45 #define FAST486_DR4_B1 (1 << 1)
46 #define FAST486_DR4_B2 (1 << 2)
47 #define FAST486_DR4_B3 (1 << 3)
48 #define FAST486_DR4_BD (1 << 13)
49 #define FAST486_DR4_BS (1 << 14)
50 #define FAST486_DR4_BT (1 << 15)
51
52 #define FAST486_DR5_L0 (1 << 0)
53 #define FAST486_DR5_G0 (1 << 1)
54 #define FAST486_DR5_L1 (1 << 2)
55 #define FAST486_DR5_G1 (1 << 3)
56 #define FAST486_DR5_L2 (1 << 4)
57 #define FAST486_DR5_G2 (1 << 5)
58 #define FAST486_DR5_L3 (1 << 6)
59 #define FAST486_DR5_G3 (1 << 7)
60 #define FAST486_DR5_LE (1 << 8)
61 #define FAST486_DR5_GE (1 << 9)
62 #define FAST486_DR5_GD (1 << 13)
63
64 #define FAST486_DBG_BREAK_EXEC 0
65 #define FAST486_DBG_BREAK_WRITE 1
66 #define FAST486_DBG_BREAK_READWRITE 3
67
68 #define FAST486_DR4_RESERVED 0xFFFF1FF0
69 #define FAST486_DR5_RESERVED 0x0000DC00
70
71 #define FAST486_IDT_TASK_GATE 0x5
72 #define FAST486_IDT_INT_GATE 0x6
73 #define FAST486_IDT_TRAP_GATE 0x7
74 #define FAST486_IDT_INT_GATE_32 0xE
75 #define FAST486_IDT_TRAP_GATE_32 0xF
76
77 #define FAST486_PREFIX_SEG (1 << 0)
78 #define FAST486_PREFIX_OPSIZE (1 << 1)
79 #define FAST486_PREFIX_ADSIZE (1 << 2)
80 #define FAST486_PREFIX_LOCK (1 << 3)
81 #define FAST486_PREFIX_REPNZ (1 << 4)
82 #define FAST486_PREFIX_REP (1 << 5)
83
84 struct _FAST486_STATE;
85 typedef struct _FAST486_STATE FAST486_STATE, *PFAST486_STATE;
86
87 typedef enum _FAST486_GEN_REGS
88 {
89 FAST486_REG_EAX,
90 FAST486_REG_ECX,
91 FAST486_REG_EDX,
92 FAST486_REG_EBX,
93 FAST486_REG_ESP,
94 FAST486_REG_EBP,
95 FAST486_REG_ESI,
96 FAST486_REG_EDI
97 } FAST486_GEN_REGS, *PFAST486_GEN_REGS;
98
99 typedef enum _FAST486_SEG_REGS
100 {
101 FAST486_REG_ES,
102 FAST486_REG_CS,
103 FAST486_REG_SS,
104 FAST486_REG_DS,
105 FAST486_REG_FS,
106 FAST486_REG_GS
107 } FAST486_SEG_REGS, *PFAST486_SEG_REGS;
108
109 typedef enum _FAST486_CTRL_REGS
110 {
111 FAST486_REG_CR0 = 0,
112 FAST486_REG_CR2 = 1,
113 FAST486_REG_CR3 = 2,
114 } FAST486_CTRL_REGS, *PFAST486_CTRL_REGS;
115
116 typedef enum _FAST486_DBG_REGS
117 {
118 FAST486_REG_DR0 = 0,
119 FAST486_REG_DR1 = 1,
120 FAST486_REG_DR2 = 2,
121 FAST486_REG_DR3 = 3,
122 FAST486_REG_DR4 = 4,
123 FAST486_REG_DR5 = 5,
124 FAST486_REG_DR6 = 4, // alias to DR4
125 FAST486_REG_DR7 = 5 // alias to DR5
126 } FAST486_DBG_REGS, *PFAST486_DBG_REGS;
127
128 typedef enum _FAST486_EXCEPTIONS
129 {
130 FAST486_EXCEPTION_DE = 0x00,
131 FAST486_EXCEPTION_DB = 0x01,
132 FAST486_EXCEPTION_BP = 0x03,
133 FAST486_EXCEPTION_OF = 0x04,
134 FAST486_EXCEPTION_BR = 0x05,
135 FAST486_EXCEPTION_UD = 0x06,
136 FAST486_EXCEPTION_NM = 0x07,
137 FAST486_EXCEPTION_DF = 0x08,
138 FAST486_EXCEPTION_TS = 0x0A,
139 FAST486_EXCEPTION_NP = 0x0B,
140 FAST486_EXCEPTION_SS = 0x0C,
141 FAST486_EXCEPTION_GP = 0x0D,
142 FAST486_EXCEPTION_PF = 0x0E,
143 FAST486_EXCEPTION_MF = 0x10,
144 FAST486_EXCEPTION_AC = 0x11,
145 FAST486_EXCEPTION_MC = 0x12
146 } FAST486_EXCEPTIONS, *PFAST486_EXCEPTIONS;
147
148 typedef enum _FAST486_INT_STATUS
149 {
150 FAST486_INT_NONE = 0,
151 FAST486_INT_EXECUTE = 1,
152 FAST486_INT_SIGNAL = 2
153 } FAST486_INT_STATUS, *PFAST486_INT_STATUS;
154
155 typedef
156 BOOLEAN
157 (NTAPI *FAST486_MEM_READ_PROC)
158 (
159 PFAST486_STATE State,
160 ULONG Address,
161 PVOID Buffer,
162 ULONG Size
163 );
164
165 typedef
166 BOOLEAN
167 (NTAPI *FAST486_MEM_WRITE_PROC)
168 (
169 PFAST486_STATE State,
170 ULONG Address,
171 PVOID Buffer,
172 ULONG Size
173 );
174
175 typedef
176 VOID
177 (NTAPI *FAST486_IO_READ_PROC)
178 (
179 PFAST486_STATE State,
180 ULONG Port,
181 PVOID Buffer,
182 ULONG Size
183 );
184
185 typedef
186 VOID
187 (NTAPI *FAST486_IO_WRITE_PROC)
188 (
189 PFAST486_STATE State,
190 ULONG Port,
191 PVOID Buffer,
192 ULONG Size
193 );
194
195 typedef
196 VOID
197 (NTAPI *FAST486_IDLE_PROC)
198 (
199 PFAST486_STATE State
200 );
201
202 typedef
203 VOID
204 (NTAPI *FAST486_BOP_PROC)
205 (
206 PFAST486_STATE State,
207 USHORT BopCode
208 );
209
210 typedef
211 UCHAR
212 (NTAPI *FAST486_INT_ACK_PROC)
213 (
214 PFAST486_STATE State
215 );
216
217 typedef union _FAST486_REG
218 {
219 union
220 {
221 struct
222 {
223 UCHAR LowByte;
224 UCHAR HighByte;
225 };
226 USHORT LowWord;
227 };
228 ULONG Long;
229 } FAST486_REG, *PFAST486_REG;
230
231 typedef struct _FAST486_SEG_REG
232 {
233 USHORT Selector;
234
235 /* Descriptor cache */
236 ULONG Accessed : 1;
237 ULONG ReadWrite : 1;
238 ULONG DirConf : 1;
239 ULONG Executable : 1;
240 ULONG SystemType : 1;
241 ULONG Dpl : 2;
242 ULONG Present : 1;
243 ULONG Size : 1;
244 ULONG Limit;
245 ULONG Base;
246 } FAST486_SEG_REG, *PFAST486_SEG_REG;
247
248 typedef struct
249 {
250 ULONG Limit : 16;
251 ULONG Base : 24;
252 ULONG Accessed : 1;
253 ULONG ReadWrite : 1;
254 ULONG DirConf : 1;
255 ULONG Executable : 1;
256 ULONG SystemType : 1;
257 ULONG Dpl : 2;
258 ULONG Present : 1;
259 ULONG LimitHigh : 4;
260 ULONG Avl : 1;
261 ULONG Reserved : 1;
262 ULONG Size : 1;
263 ULONG Granularity : 1;
264 ULONG BaseHigh : 8;
265 } FAST486_GDT_ENTRY, *PFAST486_GDT_ENTRY;
266
267 typedef struct
268 {
269 ULONG Offset : 16;
270 ULONG Selector : 16;
271 ULONG ParamCount : 5;
272 ULONG Reserved : 3;
273 ULONG Type : 4;
274 ULONG SystemType : 1;
275 ULONG Dpl : 2;
276 ULONG Present : 1;
277 ULONG OffsetHigh : 16;
278 } FAST486_CALL_GATE, *PFAST486_CALL_GATE;
279
280 typedef struct
281 {
282 ULONG Offset : 16;
283 ULONG Selector : 16;
284 ULONG Zero : 8;
285 ULONG Type : 4;
286 ULONG Storage : 1;
287 ULONG Dpl : 2;
288 ULONG Present : 1;
289 ULONG OffsetHigh : 16;
290 } FAST486_IDT_ENTRY, *PFAST486_IDT_ENTRY;
291
292 typedef struct _FAST486_TABLE_REG
293 {
294 USHORT Size;
295 ULONG Address;
296 } FAST486_TABLE_REG, *PFAST486_TABLE_REG;
297
298 typedef union _FAST486_FLAGS_REG
299 {
300 USHORT LowWord;
301 ULONG Long;
302
303 struct
304 {
305 ULONG Cf : 1;
306 ULONG AlwaysSet : 1;
307 ULONG Pf : 1;
308 ULONG Reserved0 : 1;
309 ULONG Af : 1;
310 ULONG Reserved1 : 1;
311 ULONG Zf : 1;
312 ULONG Sf : 1;
313 ULONG Tf : 1;
314 ULONG If : 1;
315 ULONG Df : 1;
316 ULONG Of : 1;
317 ULONG Iopl : 2;
318 ULONG Nt : 1;
319 ULONG Reserved2 : 1;
320 ULONG Rf : 1;
321 ULONG Vm : 1;
322 ULONG Ac : 1;
323 ULONG Vif : 1;
324 ULONG Vip : 1;
325 ULONG Id : 1;
326
327 // ULONG Reserved : 10;
328 };
329 } FAST486_FLAGS_REG, *PFAST486_FLAGS_REG;
330
331 typedef struct _FAST486_TSS
332 {
333 ULONG Link;
334 ULONG Esp0;
335 ULONG Ss0;
336 ULONG Esp1;
337 ULONG Ss1;
338 ULONG Esp2;
339 ULONG Ss2;
340 ULONG Cr3;
341 ULONG Eip;
342 ULONG Eflags;
343 ULONG Eax;
344 ULONG Ecx;
345 ULONG Edx;
346 ULONG Ebx;
347 ULONG Esp;
348 ULONG Ebp;
349 ULONG Esi;
350 ULONG Edi;
351 ULONG Es;
352 ULONG Cs;
353 ULONG Ss;
354 ULONG Ds;
355 ULONG Fs;
356 ULONG Gs;
357 ULONG Ldtr;
358 ULONG IopbOffset;
359 } FAST486_TSS, *PFAST486_TSS;
360
361 struct _FAST486_STATE
362 {
363 FAST486_MEM_READ_PROC MemReadCallback;
364 FAST486_MEM_WRITE_PROC MemWriteCallback;
365 FAST486_IO_READ_PROC IoReadCallback;
366 FAST486_IO_WRITE_PROC IoWriteCallback;
367 FAST486_IDLE_PROC IdleCallback;
368 FAST486_BOP_PROC BopCallback;
369 FAST486_INT_ACK_PROC IntAckCallback;
370 FAST486_REG GeneralRegs[FAST486_NUM_GEN_REGS];
371 FAST486_SEG_REG SegmentRegs[FAST486_NUM_SEG_REGS];
372 FAST486_REG InstPtr, SavedInstPtr;
373 FAST486_FLAGS_REG Flags;
374 FAST486_TABLE_REG Gdtr, Idtr, Ldtr, Tss;
375 ULONG ControlRegisters[FAST486_NUM_CTRL_REGS];
376 ULONG DebugRegisters[FAST486_NUM_DBG_REGS];
377 ULONG ExceptionCount;
378 ULONG PrefixFlags;
379 FAST486_SEG_REGS SegmentOverride;
380 FAST486_INT_STATUS IntStatus;
381 UCHAR PendingIntNum;
382 };
383
384 /* FUNCTIONS ******************************************************************/
385
386 VOID
387 NTAPI
388 Fast486Continue(PFAST486_STATE State);
389
390 VOID
391 NTAPI
392 Fast486StepInto(PFAST486_STATE State);
393
394 VOID
395 NTAPI
396 Fast486StepOver(PFAST486_STATE State);
397
398 VOID
399 NTAPI
400 Fast486StepOut(PFAST486_STATE State);
401
402 VOID
403 NTAPI
404 Fast486DumpState(PFAST486_STATE State);
405
406 VOID
407 NTAPI
408 Fast486Reset(PFAST486_STATE State);
409
410 VOID
411 NTAPI
412 Fast486Interrupt(PFAST486_STATE State, UCHAR Number);
413
414 VOID
415 NTAPI
416 Fast486InterruptSignal(PFAST486_STATE State);
417
418 VOID
419 NTAPI
420 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset);
421
422 VOID
423 NTAPI
424 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset);
425
426 VOID
427 NTAPI
428 Fast486SetSegment
429 (
430 PFAST486_STATE State,
431 FAST486_SEG_REGS Segment,
432 USHORT Selector
433 );
434
435 #endif // _FAST486_H_
436
437 /* EOF */