[SOFT386]
[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 Zero : 8;
236 ULONG Type : 4;
237 ULONG Storage : 1;
238 ULONG Dpl : 2;
239 ULONG Present : 1;
240 ULONG OffsetHigh : 16;
241 } SOFT386_IDT_ENTRY, *PSOFT386_IDT_ENTRY;
242
243 typedef struct _SOFT386_TABLE_REG
244 {
245 USHORT Size;
246 ULONG Address;
247 } SOFT386_TABLE_REG, *PSOFT386_TABLE_REG;
248
249 typedef union _SOFT386_FLAGS_REG
250 {
251 USHORT LowWord;
252 ULONG Long;
253
254 struct
255 {
256 ULONG Cf : 1;
257 ULONG AlwaysSet : 1;
258 ULONG Pf : 1;
259 ULONG Reserved0 : 1;
260 ULONG Af : 1;
261 ULONG Reserved1 : 1;
262 ULONG Zf : 1;
263 ULONG Sf : 1;
264 ULONG Tf : 1;
265 ULONG If : 1;
266 ULONG Df : 1;
267 ULONG Of : 1;
268 ULONG Iopl : 2;
269 ULONG Nt : 1;
270 ULONG Reserved2 : 1;
271 ULONG Rf : 1;
272 ULONG Vm : 1;
273 ULONG Ac : 1;
274 ULONG Vif : 1;
275 ULONG Vip : 1;
276 ULONG Id : 1;
277
278 // ULONG Reserved : 10;
279 };
280 } SOFT386_FLAGS_REG, *PSOFT386_FLAGS_REG;
281
282 typedef struct _SOFT386_TSS
283 {
284 ULONG Link;
285 ULONG Esp0;
286 ULONG Ss0;
287 ULONG Esp1;
288 ULONG Ss1;
289 ULONG Esp2;
290 ULONG Ss2;
291 ULONG Cr3;
292 ULONG Eip;
293 ULONG Eflags;
294 ULONG Eax;
295 ULONG Ecx;
296 ULONG Edx;
297 ULONG Ebx;
298 ULONG Esp;
299 ULONG Ebp;
300 ULONG Esi;
301 ULONG Edi;
302 ULONG Es;
303 ULONG Cs;
304 ULONG Ss;
305 ULONG Ds;
306 ULONG Fs;
307 ULONG Gs;
308 ULONG Ldtr;
309 ULONG IopbOffset;
310 } SOFT386_TSS, *PSOFT386_TSS;
311
312 struct _SOFT386_STATE
313 {
314 SOFT386_MEM_READ_PROC MemReadCallback;
315 SOFT386_MEM_WRITE_PROC MemWriteCallback;
316 SOFT386_IO_READ_PROC IoReadCallback;
317 SOFT386_IO_WRITE_PROC IoWriteCallback;
318 SOFT386_IDLE_PROC IdleCallback;
319 SOFT386_BOP_PROC BopCallback;
320 SOFT386_REG GeneralRegs[SOFT386_NUM_GEN_REGS];
321 SOFT386_SEG_REG SegmentRegs[SOFT386_NUM_SEG_REGS];
322 SOFT386_REG InstPtr;
323 SOFT386_FLAGS_REG Flags;
324 SOFT386_TABLE_REG Gdtr, Idtr, Ldtr, Tss;
325 ULONGLONG TimeStampCounter;
326 ULONG ControlRegisters[SOFT386_NUM_CTRL_REGS];
327 ULONG DebugRegisters[SOFT386_NUM_DBG_REGS];
328 ULONG ExceptionCount;
329 ULONG PrefixFlags;
330 SOFT386_SEG_REGS SegmentOverride;
331 BOOLEAN HardwareInt;
332 };
333
334 /* FUNCTIONS ******************************************************************/
335
336 VOID
337 NTAPI
338 Soft386Continue(PSOFT386_STATE State);
339
340 VOID
341 NTAPI
342 Soft386StepInto(PSOFT386_STATE State);
343
344 VOID
345 NTAPI
346 Soft386StepOver(PSOFT386_STATE State);
347
348 VOID
349 NTAPI
350 Soft386StepOut(PSOFT386_STATE State);
351
352 VOID
353 NTAPI
354 Soft386DumpState(PSOFT386_STATE State);
355
356 VOID
357 NTAPI
358 Soft386Reset(PSOFT386_STATE State);
359
360 VOID
361 NTAPI
362 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number);
363
364 VOID
365 NTAPI
366 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
367
368 VOID
369 NTAPI
370 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset);
371
372 #endif // _SOFT386_H_
373
374 /* EOF */