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