[NTVDM]
[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 EMULATOR_BOP 0xC4C4
74 #define EMULATOR_INT_BOP 0xBEEF
75
76 enum
77 {
78 EMULATOR_EXCEPTION_DIVISION_BY_ZERO,
79 EMULATOR_EXCEPTION_DEBUG,
80 EMULATOR_EXCEPTION_NMI,
81 EMULATOR_EXCEPTION_BREAKPOINT,
82 EMULATOR_EXCEPTION_OVERFLOW,
83 EMULATOR_EXCEPTION_BOUND,
84 EMULATOR_EXCEPTION_INVALID_OPCODE,
85 EMULATOR_EXCEPTION_NO_FPU,
86 EMULATOR_EXCEPTION_DOUBLE_FAULT,
87 EMULATOR_EXCEPTION_FPU_SEGMENT,
88 EMULATOR_EXCEPTION_INVALID_TSS,
89 EMULATOR_EXCEPTION_NO_SEGMENT,
90 EMULATOR_EXCEPTION_STACK_SEGMENT,
91 EMULATOR_EXCEPTION_GPF,
92 EMULATOR_EXCEPTION_PAGE_FAULT
93 };
94
95 enum
96 {
97 EMULATOR_REG_AX,
98 EMULATOR_REG_CX,
99 EMULATOR_REG_DX,
100 EMULATOR_REG_BX,
101 EMULATOR_REG_SP,
102 EMULATOR_REG_BP,
103 EMULATOR_REG_SI,
104 EMULATOR_REG_DI,
105 EMULATOR_REG_ES,
106 EMULATOR_REG_CS,
107 EMULATOR_REG_SS,
108 EMULATOR_REG_DS,
109 EMULATOR_REG_FS,
110 EMULATOR_REG_GS
111 };
112
113 typedef union
114 {
115 struct
116 {
117 BYTE LowByte;
118 BYTE HighByte;
119 };
120 WORD LowWord;
121 DWORD Long;
122 } EMULATOR_REGISTER, *PEMULATOR_REGISTER;
123
124 typedef struct
125 {
126 ULONG Limit : 16;
127 ULONG Base : 24;
128 ULONG AccessByte : 8;
129 ULONG LimitHigh : 4;
130 ULONG Flags : 4;
131 ULONG BaseHigh : 8;
132 } EMULATOR_GDT_ENTRY;
133
134 typedef struct
135 {
136 ULONG Offset : 16;
137 ULONG Selector : 16;
138 ULONG Zero : 8;
139 ULONG TypeAndAttributes : 8;
140 ULONG OffsetHigh : 16;
141 } EMULATOR_IDT_ENTRY;
142
143 typedef struct
144 {
145 WORD Size;
146 DWORD Address;
147 } EMULATOR_TABLE_REGISTER;
148
149 typedef struct
150 {
151 EMULATOR_REGISTER Registers[EMULATOR_NUM_GENERAL_REGS
152 + EMULATOR_NUM_SEGMENT_REGS];
153 EMULATOR_REGISTER Flags;
154 EMULATOR_REGISTER InstructionPointer;
155 EMULATOR_REGISTER ControlRegisters[EMULATOR_NUM_CONTROL_REGS];
156 EMULATOR_REGISTER DebugRegisters[EMULATOR_NUM_DEBUG_REGS];
157 ULONGLONG TimeStampCounter;
158 BOOLEAN OperandSizeOverload;
159 BOOLEAN AddressSizeOverload;
160 EMULATOR_TABLE_REGISTER Gdtr, Idtr;
161 EMULATOR_GDT_ENTRY CachedDescriptors[EMULATOR_NUM_SEGMENT_REGS];
162 UINT ExceptionCount;
163 } EMULATOR_CONTEXT, *PEMULATOR_CONTEXT;
164
165 typedef VOID (*EMULATOR_OPCODE_HANDLER)(PEMULATOR_CONTEXT Context, BYTE Opcode);
166
167 /* FUNCTIONS ******************************************************************/
168
169 BOOLEAN EmulatorInitialize();
170 VOID EmulatorSetStack(WORD Segment, WORD Offset);
171 VOID EmulatorExecute(WORD Segment, WORD Offset);
172 VOID EmulatorInterrupt(BYTE Number);
173 VOID EmulatorExternalInterrupt(BYTE Number);
174 ULONG EmulatorGetRegister(ULONG Register);
175 VOID EmulatorSetRegister(ULONG Register, ULONG Value);
176 BOOLEAN EmulatorGetFlag(ULONG Flag);
177 VOID EmulatorSetFlag(ULONG Flag);
178 VOID EmulatorClearFlag(ULONG Flag);
179 VOID EmulatorStep();
180 VOID EmulatorCleanup();
181 VOID EmulatorSetA20(BOOLEAN Enabled);
182
183 #endif
184
185 /* EOF */
186