69471a117c9aae72e6232190476a500712b56a1e
[reactos.git] / subsystems / ntvdm / emulator.h
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: emulator.h
5 * PURPOSE: Minimal x86 machine emulator for the VDM (header file)
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 #ifndef _EMULATOR_H_
10 #define _EMULATOR_H_
11
12 /* INCLUDES *******************************************************************/
13
14 #include "ntvdm.h"
15
16 #ifndef NEW_EMULATOR
17 #include <softx86/softx86.h>
18 #include <softx86/softx87.h>
19 #endif
20
21 /* DEFINES ********************************************************************/
22
23 /* FLAGS */
24 #define EMULATOR_FLAG_CF (1 << 0)
25 #define EMULATOR_FLAG_PF (1 << 2)
26 #define EMULATOR_FLAG_AF (1 << 4)
27 #define EMULATOR_FLAG_ZF (1 << 6)
28 #define EMULATOR_FLAG_SF (1 << 7)
29 #define EMULATOR_FLAG_TF (1 << 8)
30 #define EMULATOR_FLAG_IF (1 << 9)
31 #define EMULATOR_FLAG_DF (1 << 10)
32 #define EMULATOR_FLAG_OF (1 << 11)
33 #define EMULATOR_FLAG_NT (1 << 14)
34 #define EMULATOR_FLAG_RF (1 << 16)
35 #define EMULATOR_FLAG_VM (1 << 17)
36 #define EMULATOR_FLAG_AC (1 << 18)
37 #define EMULATOR_FLAG_VIF (1 << 19)
38 #define EMULATOR_FLAG_VIP (1 << 20)
39 #define EMULATOR_FLAG_ID (1 << 21)
40
41 /* CR0 */
42 #define EMULATOR_CR0_PE (1 << 0)
43 #define EMULATOR_CR0_MP (1 << 1)
44 #define EMULATOR_CR0_EM (1 << 2)
45 #define EMULATOR_CR0_TS (1 << 3)
46 #define EMULATOR_CR0_ET (1 << 4)
47 #define EMULATOR_CR0_NE (1 << 5)
48 #define EMULATOR_CR0_WP (1 << 16)
49 #define EMULATOR_CR0_AM (1 << 18)
50 #define EMULATOR_CR0_NW (1 << 29)
51 #define EMULATOR_CR0_CD (1 << 30)
52 #define EMULATOR_CR0_PG (1 << 31)
53
54 /* GDT Access byte */
55 #define GDT_SEG_ACCESSED (1 << 0)
56 #define GDT_DATA_WRITEABLE (1 << 1)
57 #define GDT_CODE_READABLE (1 << 1)
58 #define GDT_CONFORMING (1 << 2)
59 #define GDT_DIRECTION (1 << 2)
60 #define GDT_CODE_SEGMENT (1 << 3)
61 #define GDT_PRESENT (1 << 7)
62
63 /* GDT flags */
64 #define GDT_32BIT_SEGMENT (1 << 2)
65 #define GDT_PAGE_GRANULARITY (1 << 3)
66
67 /* Common definitions */
68 #define EMULATOR_NUM_GENERAL_REGS 8
69 #define EMULATOR_NUM_SEGMENT_REGS 6
70 #define EMULATOR_NUM_CONTROL_REGS 8
71 #define EMULATOR_NUM_DEBUG_REGS 8
72 #define MAX_GDT_ENTRIES 8192
73 #define SPECIAL_INT_NUM 0xFF
74
75 enum
76 {
77 EMULATOR_EXCEPTION_DIVISION_BY_ZERO,
78 EMULATOR_EXCEPTION_DEBUG,
79 EMULATOR_EXCEPTION_NMI,
80 EMULATOR_EXCEPTION_BREAKPOINT,
81 EMULATOR_EXCEPTION_OVERFLOW,
82 EMULATOR_EXCEPTION_BOUND,
83 EMULATOR_EXCEPTION_INVALID_OPCODE,
84 EMULATOR_EXCEPTION_NO_FPU,
85 EMULATOR_EXCEPTION_DOUBLE_FAULT,
86 EMULATOR_EXCEPTION_FPU_SEGMENT,
87 EMULATOR_EXCEPTION_INVALID_TSS,
88 EMULATOR_EXCEPTION_NO_SEGMENT,
89 EMULATOR_EXCEPTION_STACK_SEGMENT,
90 EMULATOR_EXCEPTION_GPF,
91 EMULATOR_EXCEPTION_PAGE_FAULT
92 };
93
94 enum
95 {
96 EMULATOR_REG_AX,
97 EMULATOR_REG_CX,
98 EMULATOR_REG_DX,
99 EMULATOR_REG_BX,
100 EMULATOR_REG_SP,
101 EMULATOR_REG_BP,
102 EMULATOR_REG_SI,
103 EMULATOR_REG_DI,
104 EMULATOR_REG_ES,
105 EMULATOR_REG_CS,
106 EMULATOR_REG_SS,
107 EMULATOR_REG_DS,
108 EMULATOR_REG_FS,
109 EMULATOR_REG_GS
110 };
111
112 typedef union
113 {
114 struct
115 {
116 BYTE LowByte;
117 BYTE HighByte;
118 };
119 WORD LowWord;
120 DWORD Long;
121 } EMULATOR_REGISTER, *PEMULATOR_REGISTER;
122
123 typedef struct
124 {
125 ULONG Limit : 16;
126 ULONG Base : 24;
127 ULONG AccessByte : 8;
128 ULONG LimitHigh : 4;
129 ULONG Flags : 4;
130 ULONG BaseHigh : 8;
131 } EMULATOR_GDT_ENTRY;
132
133 typedef struct
134 {
135 ULONG Offset : 16;
136 ULONG Selector : 16;
137 ULONG Zero : 8;
138 ULONG TypeAndAttributes : 8;
139 ULONG OffsetHigh : 16;
140 } EMULATOR_IDT_ENTRY;
141
142 typedef struct
143 {
144 WORD Size;
145 DWORD Address;
146 } EMULATOR_TABLE_REGISTER;
147
148 typedef struct
149 {
150 EMULATOR_REGISTER Registers[EMULATOR_NUM_GENERAL_REGS
151 + EMULATOR_NUM_SEGMENT_REGS];
152 EMULATOR_REGISTER Flags;
153 EMULATOR_REGISTER InstructionPointer;
154 EMULATOR_REGISTER ControlRegisters[EMULATOR_NUM_CONTROL_REGS];
155 EMULATOR_REGISTER DebugRegisters[EMULATOR_NUM_DEBUG_REGS];
156 ULONGLONG TimeStampCounter;
157 BOOLEAN OperandSizeOverload;
158 BOOLEAN AddressSizeOverload;
159 EMULATOR_TABLE_REGISTER Gdtr, Idtr;
160 EMULATOR_GDT_ENTRY CachedDescriptors[EMULATOR_NUM_SEGMENT_REGS];
161 UINT ExceptionCount;
162 } EMULATOR_CONTEXT, *PEMULATOR_CONTEXT;
163
164 typedef VOID (*EMULATOR_OPCODE_HANDLER)(PEMULATOR_CONTEXT Context, BYTE Opcode);
165
166 /* FUNCTIONS ******************************************************************/
167
168 BOOLEAN EmulatorInitialize();
169 VOID EmulatorSetStack(WORD Segment, WORD Offset);
170 VOID EmulatorExecute(WORD Segment, WORD Offset);
171 VOID EmulatorInterrupt(BYTE Number);
172 VOID EmulatorExternalInterrupt(BYTE Number);
173 ULONG EmulatorGetRegister(ULONG Register);
174 VOID EmulatorSetRegister(ULONG Register, ULONG Value);
175 BOOLEAN EmulatorGetFlag(ULONG Flag);
176 VOID EmulatorSetFlag(ULONG Flag);
177 VOID EmulatorClearFlag(ULONG Flag);
178 VOID EmulatorStep();
179 VOID EmulatorCleanup();
180 VOID EmulatorSetA20(BOOLEAN Enabled);
181
182 #endif
183
184 /* EOF */
185