21b61719e25efe289656bb8e112d6d61eb3c6a41
[reactos.git] / include / reactos / libs / soft386 / soft386.h
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: 386/486 CPU Emulation Library
4 * FILE: soft386.h
5 * PURPOSE: Header file for Soft386.
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 #ifndef _SOFT386_H_
10 #define _SOFT386_H_
11
12 /* DEFINES ********************************************************************/
13
14 #define SOFT386_NUM_GEN_REGS 8
15 #define SOFT386_NUM_SEG_REGS 6
16 #define SOFT386_NUM_CTRL_REGS 8
17 #define SOFT386_NUM_DBG_REGS 8
18
19 #define SOFT386_CR0_PE (1 << 0)
20 #define SOFT386_CR0_MP (1 << 1)
21 #define SOFT386_CR0_EM (1 << 2)
22 #define SOFT386_CR0_TS (1 << 3)
23 #define SOFT386_CR0_ET (1 << 4)
24 #define SOFT386_CR0_NE (1 << 5)
25 #define SOFT386_CR0_WP (1 << 16)
26 #define SOFT386_CR0_AM (1 << 18)
27 #define SOFT386_CR0_NW (1 << 29)
28 #define SOFT386_CR0_CD (1 << 30)
29 #define SOFT386_CR0_PG (1 << 31)
30
31 #define SOFT386_IDT_TASK_GATE 0x5
32 #define SOFT386_IDT_INT_GATE 0x6
33 #define SOFT386_IDT_TRAP_GATE 0x7
34 #define SOFT386_IDT_INT_GATE_32 0xE
35 #define SOFT386_IDT_TRAP_GATE_32 0xF
36
37 #define SOFT386_PREFIX_SEG (1 << 0)
38 #define SOFT386_PREFIX_OPSIZE (1 << 1)
39 #define SOFT386_PREFIX_ADSIZE (1 << 2)
40 #define SOFT386_PREFIX_LOCK (1 << 3)
41 #define SOFT386_PREFIX_REPNZ (1 << 4)
42 #define SOFT386_PREFIX_REP (1 << 5)
43
44 struct _SOFT386_STATE;
45 typedef struct _SOFT386_STATE SOFT386_STATE, *PSOFT386_STATE;
46
47 typedef enum _SOFT386_GEN_REGS
48 {
49 SOFT386_REG_EAX,
50 SOFT386_REG_ECX,
51 SOFT386_REG_EDX,
52 SOFT386_REG_EBX,
53 SOFT386_REG_ESP,
54 SOFT386_REG_EBP,
55 SOFT386_REG_ESI,
56 SOFT386_REG_EDI
57 } SOFT386_GEN_REGS, *PSOFT386_GEN_REGS;
58
59 typedef enum _SOFT386_SEG_REGS
60 {
61 SOFT386_REG_ES,
62 SOFT386_REG_CS,
63 SOFT386_REG_SS,
64 SOFT386_REG_DS,
65 SOFT386_REG_FS,
66 SOFT386_REG_GS
67 } SOFT386_SEG_REGS, *PSOFT386_SEG_REGS;
68
69 typedef enum _SOFT386_CTRL_REGS
70 {
71 SOFT386_REG_CR0,
72 SOFT386_REG_CR1,
73 SOFT386_REG_CR2,
74 SOFT386_REG_CR3,
75 SOFT386_REG_CR4,
76 SOFT386_REG_CR5,
77 SOFT386_REG_CR6,
78 SOFT386_REG_CR7
79 } SOFT386_CTRL_REGS, *PSOFT386_CTRL_REGS;
80
81 typedef enum _SOFT386_DBG_REGS
82 {
83 SOFT386_REG_DR0,
84 SOFT386_REG_DR1,
85 SOFT386_REG_DR2,
86 SOFT386_REG_DR3,
87 SOFT386_REG_DR4,
88 SOFT386_REG_DR5,
89 SOFT386_REG_DR6,
90 SOFT386_REG_DR7
91 } SOFT386_DBG_REGS, *PSOFT386_DBG_REGS;
92
93 typedef enum _SOFT386_EXCEPTIONS
94 {
95 SOFT386_EXCEPTION_DE = 0x00,
96 SOFT386_EXCEPTION_DB = 0x01,
97 SOFT386_EXCEPTION_BP = 0x03,
98 SOFT386_EXCEPTION_OF = 0x04,
99 SOFT386_EXCEPTION_BR = 0x05,
100 SOFT386_EXCEPTION_UD = 0x06,
101 SOFT386_EXCEPTION_NM = 0x07,
102 SOFT386_EXCEPTION_DF = 0x08,
103 SOFT386_EXCEPTION_TS = 0x0A,
104 SOFT386_EXCEPTION_NP = 0x0B,
105 SOFT386_EXCEPTION_SS = 0x0C,
106 SOFT386_EXCEPTION_GP = 0x0D,
107 SOFT386_EXCEPTION_PF = 0x0E,
108 SOFT386_EXCEPTION_MF = 0x10,
109 SOFT386_EXCEPTION_AC = 0x11,
110 SOFT386_EXCEPTION_MC = 0x12
111 } SOFT386_EXCEPTIONS, *PSOFT386_EXCEPTIONS;
112
113 typedef
114 BOOLEAN
115 (NTAPI *SOFT386_MEM_READ_PROC)
116 (
117 PSOFT386_STATE State,
118 ULONG Address,
119 PVOID Buffer,
120 ULONG Size
121 );
122
123 typedef
124 BOOLEAN
125 (NTAPI *SOFT386_MEM_WRITE_PROC)
126 (
127 PSOFT386_STATE State,
128 ULONG Address,
129 PVOID Buffer,
130 ULONG Size
131 );
132
133 typedef
134 VOID
135 (NTAPI *SOFT386_IO_READ_PROC)
136 (
137 PSOFT386_STATE State,
138 ULONG Port,
139 PVOID Buffer,
140 ULONG Size
141 );
142
143 typedef
144 VOID
145 (NTAPI *SOFT386_IO_WRITE_PROC)
146 (
147 PSOFT386_STATE State,
148 ULONG Port,
149 PVOID Buffer,
150 ULONG Size
151 );
152
153 typedef
154 VOID
155 (NTAPI *SOFT386_IDLE_PROC)
156 (
157 PSOFT386_STATE State
158 );
159
160 typedef
161 VOID
162 (NTAPI *SOFT386_BOP_PROC)
163 (
164 PSOFT386_STATE State,
165 USHORT BopCode
166 );
167
168 typedef union _SOFT386_REG
169 {
170 union
171 {
172 struct
173 {
174 UCHAR LowByte;
175 UCHAR HighByte;
176 };
177 USHORT LowWord;
178 };
179 ULONG Long;
180 } SOFT386_REG, *PSOFT386_REG;
181
182 typedef struct _SOFT386_SEG_REG
183 {
184 USHORT Selector;
185
186 /* Descriptor cache */
187 ULONG Accessed : 1;
188 ULONG ReadWrite : 1;
189 ULONG DirConf : 1;
190 ULONG Executable : 1;
191 ULONG SystemType : 1;
192 ULONG Dpl : 2;
193 ULONG Present : 1;
194 ULONG Size : 1;
195 ULONG Limit;
196 ULONG Base;
197 } SOFT386_SEG_REG, *PSOFT386_SEG_REG;
198
199 typedef struct
200 {
201 ULONG Limit : 16;
202 ULONG Base : 24;
203 ULONG Accessed : 1;
204 ULONG ReadWrite : 1;
205 ULONG DirConf : 1;
206 ULONG Executable : 1;
207 ULONG SystemType : 1;
208 ULONG Dpl : 2;
209 ULONG Present : 1;
210 ULONG LimitHigh : 4;
211 ULONG Avl : 1;
212 ULONG Reserved : 1;
213 ULONG Size : 1;
214 ULONG Granularity : 1;
215 ULONG BaseHigh : 8;
216 } SOFT386_GDT_ENTRY, *PSOFT386_GDT_ENTRY;
217
218 typedef struct
219 {
220 ULONG Offset : 16;
221 ULONG Selector : 16;
222 ULONG Zero : 8;
223 ULONG Type : 4;
224 ULONG Storage : 1;
225 ULONG Dpl : 2;
226 ULONG Present : 1;
227 ULONG OffsetHigh : 16;
228 } SOFT386_IDT_ENTRY, *PSOFT386_IDT_ENTRY;
229
230 typedef struct _SOFT386_TABLE_REG
231 {
232 USHORT Size;
233 ULONG Address;
234 } SOFT386_TABLE_REG, *PSOFT386_TABLE_REG;
235
236 typedef union _SOFT386_FLAGS_REG
237 {
238 USHORT LowWord;
239 ULONG Long;
240
241 struct
242 {
243 ULONG Cf : 1;
244 ULONG AlwaysSet : 1;
245 ULONG Pf : 1;
246 ULONG Reserved0 : 1;
247 ULONG Af : 1;
248 ULONG Reserved1 : 1;
249 ULONG Zf : 1;
250 ULONG Sf : 1;
251 ULONG Tf : 1;
252 ULONG If : 1;
253 ULONG Df : 1;
254 ULONG Of : 1;
255 ULONG Iopl : 2;
256 ULONG Nt : 1;
257 ULONG Reserved2 : 1;
258 ULONG Rf : 1;
259 ULONG Vm : 1;
260 ULONG Ac : 1;
261 ULONG Vif : 1;
262 ULONG Vip : 1;
263 ULONG Id : 1;
264
265 // ULONG Reserved : 10;
266 };
267 } SOFT386_FLAGS_REG, *PSOFT386_FLAGS_REG;
268
269 typedef struct _SOFT386_TSS
270 {
271 ULONG Link;
272 ULONG Esp0;
273 ULONG Ss0;
274 ULONG Esp1;
275 ULONG Ss1;
276 ULONG Esp2;
277 ULONG Ss2;
278 ULONG Cr3;
279 ULONG Eip;
280 ULONG Eflags;
281 ULONG Eax;
282 ULONG Ecx;
283 ULONG Edx;
284 ULONG Ebx;
285 ULONG Esp;
286 ULONG Ebp;
287 ULONG Esi;
288 ULONG Edi;
289 ULONG Es;
290 ULONG Cs;
291 ULONG Ss;
292 ULONG Ds;
293 ULONG Fs;
294 ULONG Gs;
295 ULONG Ldtr;
296 ULONG IopbOffset;
297 } SOFT386_TSS, *PSOFT386_TSS;
298
299 struct _SOFT386_STATE
300 {
301 SOFT386_MEM_READ_PROC MemReadCallback;
302 SOFT386_MEM_WRITE_PROC MemWriteCallback;
303 SOFT386_IO_READ_PROC IoReadCallback;
304 SOFT386_IO_WRITE_PROC IoWriteCallback;
305 SOFT386_IDLE_PROC IdleCallback;
306 SOFT386_BOP_PROC BopCallback;
307 SOFT386_REG GeneralRegs[SOFT386_NUM_GEN_REGS];
308 SOFT386_SEG_REG SegmentRegs[SOFT386_NUM_SEG_REGS];
309 SOFT386_REG InstPtr;
310 SOFT386_FLAGS_REG Flags;
311 SOFT386_TABLE_REG Gdtr, Idtr, Ldtr, Tss;
312 ULONGLONG TimeStampCounter;
313 ULONG ControlRegisters[SOFT386_NUM_CTRL_REGS];
314 ULONG DebugRegisters[SOFT386_NUM_DBG_REGS];
315 ULONG ExceptionCount;
316 ULONG PrefixFlags;
317 SOFT386_SEG_REGS SegmentOverride;
318 BOOLEAN HardwareInt;
319 };
320
321 /* FUNCTIONS ******************************************************************/
322
323 VOID
324 NTAPI
325 Soft386Continue(PSOFT386_STATE State);
326
327 VOID
328 NTAPI
329 Soft386StepInto(PSOFT386_STATE State);
330
331 VOID
332 NTAPI
333 Soft386StepOver(PSOFT386_STATE State);
334
335 VOID
336 NTAPI
337 Soft386StepOut(PSOFT386_STATE State);
338
339 VOID
340 NTAPI
341 Soft386DumpState(PSOFT386_STATE State);
342
343 VOID
344 NTAPI
345 Soft386Reset(PSOFT386_STATE State);
346
347 VOID
348 NTAPI
349 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number);
350
351 VOID
352 NTAPI
353 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
354
355 VOID
356 NTAPI
357 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
358
359 #endif // _SOFT386_H_
360
361 /* EOF */