e87810dd66ac9407b2a01b3861c71411bd6da93b
[reactos.git] / include / reactos / libs / soft386 / soft386.h
1 /*
2 * Soft386 386/486 CPU Emulation Library
3 * soft386.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 _SOFT386_H_
23 #define _SOFT386_H_
24
25 /* DEFINES ********************************************************************/
26
27 #define SOFT386_NUM_GEN_REGS 8
28 #define SOFT386_NUM_SEG_REGS 6
29 #define SOFT386_NUM_CTRL_REGS 8
30 #define SOFT386_NUM_DBG_REGS 8
31
32 #define SOFT386_CR0_PE (1 << 0)
33 #define SOFT386_CR0_MP (1 << 1)
34 #define SOFT386_CR0_EM (1 << 2)
35 #define SOFT386_CR0_TS (1 << 3)
36 #define SOFT386_CR0_ET (1 << 4)
37 #define SOFT386_CR0_NE (1 << 5)
38 #define SOFT386_CR0_WP (1 << 16)
39 #define SOFT386_CR0_AM (1 << 18)
40 #define SOFT386_CR0_NW (1 << 29)
41 #define SOFT386_CR0_CD (1 << 30)
42 #define SOFT386_CR0_PG (1 << 31)
43
44 #define SOFT386_IDT_TASK_GATE 0x5
45 #define SOFT386_IDT_INT_GATE 0x6
46 #define SOFT386_IDT_TRAP_GATE 0x7
47 #define SOFT386_IDT_INT_GATE_32 0xE
48 #define SOFT386_IDT_TRAP_GATE_32 0xF
49
50 #define SOFT386_PREFIX_SEG (1 << 0)
51 #define SOFT386_PREFIX_OPSIZE (1 << 1)
52 #define SOFT386_PREFIX_ADSIZE (1 << 2)
53 #define SOFT386_PREFIX_LOCK (1 << 3)
54 #define SOFT386_PREFIX_REPNZ (1 << 4)
55 #define SOFT386_PREFIX_REP (1 << 5)
56
57 struct _SOFT386_STATE;
58 typedef struct _SOFT386_STATE SOFT386_STATE, *PSOFT386_STATE;
59
60 typedef enum _SOFT386_GEN_REGS
61 {
62 SOFT386_REG_EAX,
63 SOFT386_REG_ECX,
64 SOFT386_REG_EDX,
65 SOFT386_REG_EBX,
66 SOFT386_REG_ESP,
67 SOFT386_REG_EBP,
68 SOFT386_REG_ESI,
69 SOFT386_REG_EDI
70 } SOFT386_GEN_REGS, *PSOFT386_GEN_REGS;
71
72 typedef enum _SOFT386_SEG_REGS
73 {
74 SOFT386_REG_ES,
75 SOFT386_REG_CS,
76 SOFT386_REG_SS,
77 SOFT386_REG_DS,
78 SOFT386_REG_FS,
79 SOFT386_REG_GS
80 } SOFT386_SEG_REGS, *PSOFT386_SEG_REGS;
81
82 typedef enum _SOFT386_CTRL_REGS
83 {
84 SOFT386_REG_CR0,
85 SOFT386_REG_CR1,
86 SOFT386_REG_CR2,
87 SOFT386_REG_CR3,
88 SOFT386_REG_CR4,
89 SOFT386_REG_CR5,
90 SOFT386_REG_CR6,
91 SOFT386_REG_CR7
92 } SOFT386_CTRL_REGS, *PSOFT386_CTRL_REGS;
93
94 typedef enum _SOFT386_DBG_REGS
95 {
96 SOFT386_REG_DR0,
97 SOFT386_REG_DR1,
98 SOFT386_REG_DR2,
99 SOFT386_REG_DR3,
100 SOFT386_REG_DR4,
101 SOFT386_REG_DR5,
102 SOFT386_REG_DR6,
103 SOFT386_REG_DR7
104 } SOFT386_DBG_REGS, *PSOFT386_DBG_REGS;
105
106 typedef enum _SOFT386_EXCEPTIONS
107 {
108 SOFT386_EXCEPTION_DE = 0x00,
109 SOFT386_EXCEPTION_DB = 0x01,
110 SOFT386_EXCEPTION_BP = 0x03,
111 SOFT386_EXCEPTION_OF = 0x04,
112 SOFT386_EXCEPTION_BR = 0x05,
113 SOFT386_EXCEPTION_UD = 0x06,
114 SOFT386_EXCEPTION_NM = 0x07,
115 SOFT386_EXCEPTION_DF = 0x08,
116 SOFT386_EXCEPTION_TS = 0x0A,
117 SOFT386_EXCEPTION_NP = 0x0B,
118 SOFT386_EXCEPTION_SS = 0x0C,
119 SOFT386_EXCEPTION_GP = 0x0D,
120 SOFT386_EXCEPTION_PF = 0x0E,
121 SOFT386_EXCEPTION_MF = 0x10,
122 SOFT386_EXCEPTION_AC = 0x11,
123 SOFT386_EXCEPTION_MC = 0x12
124 } SOFT386_EXCEPTIONS, *PSOFT386_EXCEPTIONS;
125
126 typedef
127 BOOLEAN
128 (NTAPI *SOFT386_MEM_READ_PROC)
129 (
130 PSOFT386_STATE State,
131 ULONG Address,
132 PVOID Buffer,
133 ULONG Size
134 );
135
136 typedef
137 BOOLEAN
138 (NTAPI *SOFT386_MEM_WRITE_PROC)
139 (
140 PSOFT386_STATE State,
141 ULONG Address,
142 PVOID Buffer,
143 ULONG Size
144 );
145
146 typedef
147 VOID
148 (NTAPI *SOFT386_IO_READ_PROC)
149 (
150 PSOFT386_STATE State,
151 ULONG Port,
152 PVOID Buffer,
153 ULONG Size
154 );
155
156 typedef
157 VOID
158 (NTAPI *SOFT386_IO_WRITE_PROC)
159 (
160 PSOFT386_STATE State,
161 ULONG Port,
162 PVOID Buffer,
163 ULONG Size
164 );
165
166 typedef
167 VOID
168 (NTAPI *SOFT386_IDLE_PROC)
169 (
170 PSOFT386_STATE State
171 );
172
173 typedef
174 VOID
175 (NTAPI *SOFT386_BOP_PROC)
176 (
177 PSOFT386_STATE State,
178 USHORT BopCode
179 );
180
181 typedef union _SOFT386_REG
182 {
183 union
184 {
185 struct
186 {
187 UCHAR LowByte;
188 UCHAR HighByte;
189 };
190 USHORT LowWord;
191 };
192 ULONG Long;
193 } SOFT386_REG, *PSOFT386_REG;
194
195 typedef struct _SOFT386_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 } SOFT386_SEG_REG, *PSOFT386_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 } SOFT386_GDT_ENTRY, *PSOFT386_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 } SOFT386_CALL_GATE, *PSOFT386_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 } SOFT386_IDT_ENTRY, *PSOFT386_IDT_ENTRY;
255
256 typedef struct _SOFT386_TABLE_REG
257 {
258 USHORT Size;
259 ULONG Address;
260 } SOFT386_TABLE_REG, *PSOFT386_TABLE_REG;
261
262 typedef union _SOFT386_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 } SOFT386_FLAGS_REG, *PSOFT386_FLAGS_REG;
294
295 typedef struct _SOFT386_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 } SOFT386_TSS, *PSOFT386_TSS;
324
325 struct _SOFT386_STATE
326 {
327 SOFT386_MEM_READ_PROC MemReadCallback;
328 SOFT386_MEM_WRITE_PROC MemWriteCallback;
329 SOFT386_IO_READ_PROC IoReadCallback;
330 SOFT386_IO_WRITE_PROC IoWriteCallback;
331 SOFT386_IDLE_PROC IdleCallback;
332 SOFT386_BOP_PROC BopCallback;
333 SOFT386_REG GeneralRegs[SOFT386_NUM_GEN_REGS];
334 SOFT386_SEG_REG SegmentRegs[SOFT386_NUM_SEG_REGS];
335 SOFT386_REG InstPtr, SavedInstPtr;
336 SOFT386_FLAGS_REG Flags;
337 SOFT386_TABLE_REG Gdtr, Idtr, Ldtr, Tss;
338 ULONGLONG TimeStampCounter;
339 ULONG ControlRegisters[SOFT386_NUM_CTRL_REGS];
340 ULONG DebugRegisters[SOFT386_NUM_DBG_REGS];
341 ULONG ExceptionCount;
342 ULONG PrefixFlags;
343 SOFT386_SEG_REGS SegmentOverride;
344 BOOLEAN HardwareInt;
345 };
346
347 /* FUNCTIONS ******************************************************************/
348
349 VOID
350 NTAPI
351 Soft386Continue(PSOFT386_STATE State);
352
353 VOID
354 NTAPI
355 Soft386StepInto(PSOFT386_STATE State);
356
357 VOID
358 NTAPI
359 Soft386StepOver(PSOFT386_STATE State);
360
361 VOID
362 NTAPI
363 Soft386StepOut(PSOFT386_STATE State);
364
365 VOID
366 NTAPI
367 Soft386DumpState(PSOFT386_STATE State);
368
369 VOID
370 NTAPI
371 Soft386Reset(PSOFT386_STATE State);
372
373 VOID
374 NTAPI
375 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number, BOOLEAN Hardware);
376
377 VOID
378 NTAPI
379 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
380
381 VOID
382 NTAPI
383 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
384
385 VOID
386 NTAPI
387 Soft386SetSegment
388 (
389 PSOFT386_STATE State,
390 SOFT386_SEG_REGS Segment,
391 USHORT Selector
392 );
393
394 #endif // _SOFT386_H_
395
396 /* EOF */