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