[NTVDM]
[reactos.git] / subsystems / ntvdm / emulator.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: emulator.c
5 * PURPOSE: Minimal x86 machine emulator for the VDM
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include "emulator.h"
12 #include "bios.h"
13 #include "dos.h"
14 #include "pic.h"
15 #include "ps2.h"
16 #include "timer.h"
17
18 /* PRIVATE VARIABLES **********************************************************/
19
20 static softx86_ctx EmulatorContext;
21 static softx87_ctx FpuEmulatorContext;
22 static BOOLEAN A20Line = FALSE;
23
24 /* PRIVATE FUNCTIONS **********************************************************/
25
26 static VOID EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
27 {
28 /* If the A20 line is disabled, mask bit 20 */
29 if (!A20Line) Address &= ~(1 << 20);
30
31 /* Make sure the requested address is valid */
32 if ((Address + Size) >= MAX_ADDRESS) return;
33
34 /* Are we reading some of the console video memory? */
35 if (((Address + Size) >= CONSOLE_VIDEO_MEM_START)
36 && (Address < CONSOLE_VIDEO_MEM_END))
37 {
38 /* Call the VDM BIOS to update the video memory */
39 BiosUpdateConsole(max(Address, CONSOLE_VIDEO_MEM_START),
40 min(Address + Size, CONSOLE_VIDEO_MEM_END));
41 }
42
43 /* Read the data from the virtual address space and store it in the buffer */
44 RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
45 }
46
47 static VOID EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
48 {
49 /* If the A20 line is disabled, mask bit 20 */
50 if (!A20Line) Address &= ~(1 << 20);
51
52 /* Make sure the requested address is valid */
53 if ((Address + Size) >= MAX_ADDRESS) return;
54
55 /* Make sure we don't write to the ROM area */
56 if ((Address + Size) >= ROM_AREA_START && (Address < ROM_AREA_END)) return;
57
58 /* Read the data from the buffer and store it in the virtual address space */
59 RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
60
61 /* Check if we modified the console video memory */
62 if (((Address + Size) >= CONSOLE_VIDEO_MEM_START)
63 && (Address < CONSOLE_VIDEO_MEM_END))
64 {
65 /* Call the VDM BIOS to update the screen */
66 BiosUpdateConsole(max(Address, CONSOLE_VIDEO_MEM_START),
67 min(Address + Size, CONSOLE_VIDEO_MEM_END));
68 }
69 }
70
71 static VOID EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
72 {
73 switch (Address)
74 {
75 case PIC_MASTER_CMD:
76 case PIC_SLAVE_CMD:
77 {
78 *Buffer = PicReadCommand(Address);
79 break;
80 }
81
82 case PIC_MASTER_DATA:
83 case PIC_SLAVE_DATA:
84 {
85 *Buffer = PicReadData(Address);
86 break;
87 }
88
89 case PIT_DATA_PORT(0):
90 case PIT_DATA_PORT(1):
91 case PIT_DATA_PORT(2):
92 {
93 *Buffer = PitReadData(Address - PIT_DATA_PORT(0));
94 break;
95 }
96
97 case PS2_CONTROL_PORT:
98 {
99 *Buffer = KeyboardReadStatus();
100 break;
101 }
102
103 case PS2_DATA_PORT:
104 {
105 *Buffer = KeyboardReadData();
106 break;
107 }
108 }
109 }
110
111 static VOID EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
112 {
113 BYTE Byte = *Buffer;
114
115 switch (Address)
116 {
117 case PIT_COMMAND_PORT:
118 {
119 PitWriteCommand(Byte);
120 break;
121 }
122
123 case PIT_DATA_PORT(0):
124 case PIT_DATA_PORT(1):
125 case PIT_DATA_PORT(2):
126 {
127 PitWriteData(Address - PIT_DATA_PORT(0), Byte);
128 break;
129 }
130
131 case PIC_MASTER_CMD:
132 case PIC_SLAVE_CMD:
133 {
134 PicWriteCommand(Address, Byte);
135 break;
136 }
137
138 case PIC_MASTER_DATA:
139 case PIC_SLAVE_DATA:
140 {
141 PicWriteData(Address, Byte);
142 break;
143 }
144
145 case PS2_CONTROL_PORT:
146 {
147 KeyboardWriteCommand(Byte);
148 break;
149 }
150
151 case PS2_DATA_PORT:
152 {
153 KeyboardWriteData(Byte);
154 break;
155 }
156 }
157 }
158
159 static VOID EmulatorSoftwareInt(PVOID Context, BYTE Number)
160 {
161 WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
162 BYTE IntNum;
163
164 /* Check if this is the special interrupt */
165 if (Number == SPECIAL_INT_NUM)
166 {
167 /* Get the SS:SP */
168 StackSegment = EmulatorContext.state->segment_reg[SX86_SREG_SS].val;
169 StackPointer = EmulatorContext.state->general_reg[SX86_REG_SP].val;
170
171 /* Get the interrupt number */
172 IntNum = *(LPBYTE)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
173
174 /* Move the stack pointer forward one word to skip the interrupt number */
175 StackPointer += sizeof(WORD);
176
177 /* Get the CS:IP */
178 InstructionPointer = *(LPWORD)((ULONG_PTR)BaseAddress
179 + TO_LINEAR(StackSegment, StackPointer));
180 CodeSegment = *(LPWORD)((ULONG_PTR)BaseAddress
181 + TO_LINEAR(StackSegment, StackPointer + sizeof(WORD)));
182
183 /* Check if this was an exception */
184 if (IntNum < 8)
185 {
186 /* Display a message to the user */
187 DisplayMessage(L"Exception: %s occured at %04X:%04X",
188 ExceptionName[IntNum],
189 CodeSegment,
190 InstructionPointer);
191
192 /* Stop the VDM */
193 VdmRunning = FALSE;
194 return;
195 }
196
197 /* Check if this was an PIC IRQ */
198 if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
199 {
200 /* It was an IRQ from the master PIC */
201 BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT);
202 }
203 else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
204 {
205 /* It was an IRQ from the slave PIC */
206 BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8);
207 }
208
209 switch (IntNum)
210 {
211 case VIDEO_BIOS_INTERRUPT:
212 {
213 /* This is the video BIOS interrupt, call the BIOS */
214 BiosVideoService();
215 break;
216 }
217 case VIDEO_KBD_INTERRUPT:
218 {
219 /* This is the keyboard BIOS interrupt, call the BIOS */
220 BiosKeyboardService();
221 break;
222 }
223 case 0x20:
224 {
225 DosInt20h(CodeSegment);
226 break;
227 }
228 case 0x21:
229 {
230 DosInt21h(CodeSegment);
231 break;
232 }
233 case 0x23:
234 {
235 DosBreakInterrupt();
236 break;
237 }
238 }
239 }
240 }
241
242 static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
243 {
244 /* Do nothing */
245 }
246
247 static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
248 {
249 /* Do nothing */
250 }
251
252 /* PUBLIC FUNCTIONS ***********************************************************/
253
254 BOOLEAN EmulatorInitialize()
255 {
256 /* Allocate memory for the 16-bit address space */
257 BaseAddress = HeapAlloc(GetProcessHeap(), 0, MAX_ADDRESS);
258 if (BaseAddress == NULL) return FALSE;
259
260 /* Initialize the softx86 CPU emulator */
261 if (!softx86_init(&EmulatorContext, SX86_CPULEVEL_80286))
262 {
263 HeapFree(GetProcessHeap(), 0, BaseAddress);
264 return FALSE;
265 }
266
267 /* Initialize the softx87 FPU emulator*/
268 if(!softx87_init(&FpuEmulatorContext, SX87_FPULEVEL_8087))
269 {
270 softx86_free(&EmulatorContext);
271 HeapFree(GetProcessHeap(), 0, BaseAddress);
272 return FALSE;
273 }
274
275 /* Set memory read/write callbacks */
276 EmulatorContext.callbacks->on_read_memory = EmulatorReadMemory;
277 EmulatorContext.callbacks->on_write_memory = EmulatorWriteMemory;
278
279 /* Set MMIO read/write callbacks */
280 EmulatorContext.callbacks->on_read_io = EmulatorReadIo;
281 EmulatorContext.callbacks->on_write_io = EmulatorWriteIo;
282
283 /* Set interrupt callbacks */
284 EmulatorContext.callbacks->on_sw_int = EmulatorSoftwareInt;
285 EmulatorContext.callbacks->on_hw_int = EmulatorHardwareInt;
286 EmulatorContext.callbacks->on_hw_int_ack = EmulatorHardwareIntAck;
287
288 /* Connect the emulated FPU to the emulated CPU */
289 softx87_connect_to_CPU(&EmulatorContext, &FpuEmulatorContext);
290
291 /* Enable interrupts */
292 EmulatorSetFlag(EMULATOR_FLAG_IF);
293
294 return TRUE;
295 }
296
297 VOID EmulatorSetStack(WORD Segment, WORD Offset)
298 {
299 /* Call the softx86 API */
300 softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
301 }
302
303 VOID EmulatorExecute(WORD Segment, WORD Offset)
304 {
305 /* Call the softx86 API */
306 softx86_set_instruction_ptr(&EmulatorContext, Segment, Offset);
307 }
308
309 VOID EmulatorInterrupt(BYTE Number)
310 {
311 LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
312 UINT Segment, Offset;
313
314 /* Get the segment and offset */
315 Segment = HIWORD(IntVecTable[Number]);
316 Offset = LOWORD(IntVecTable[Number]);
317
318 /* Call the softx86 API */
319 softx86_make_simple_interrupt_call(&EmulatorContext, &Segment, &Offset);
320 }
321
322 VOID EmulatorExternalInterrupt(BYTE Number)
323 {
324 /* Call the softx86 API */
325 softx86_ext_hw_signal(&EmulatorContext, Number);
326 }
327
328 ULONG EmulatorGetRegister(ULONG Register)
329 {
330 if (Register < EMULATOR_REG_ES)
331 {
332 return EmulatorContext.state->general_reg[Register].val;
333 }
334 else
335 {
336 return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
337 }
338 }
339
340 VOID EmulatorSetRegister(ULONG Register, ULONG Value)
341 {
342 if (Register < EMULATOR_REG_CS)
343 {
344 EmulatorContext.state->general_reg[Register].val = Value;
345 }
346 else
347 {
348 EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = Value;
349 }
350 }
351
352 BOOLEAN EmulatorGetFlag(ULONG Flag)
353 {
354 return (EmulatorContext.state->reg_flags.val & Flag);
355 }
356
357 VOID EmulatorSetFlag(ULONG Flag)
358 {
359 EmulatorContext.state->reg_flags.val |= Flag;
360 }
361
362 VOID EmulatorClearFlag(ULONG Flag)
363 {
364 EmulatorContext.state->reg_flags.val &= ~Flag;
365 }
366
367 VOID EmulatorStep()
368 {
369 /* Call the softx86 API */
370 if (!softx86_step(&EmulatorContext))
371 {
372 /* Invalid opcode */
373 EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
374 }
375 }
376
377 VOID EmulatorCleanup()
378 {
379 /* Free the memory allocated for the 16-bit address space */
380 if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
381
382 /* Free the softx86 CPU and FPU emulator */
383 softx86_free(&EmulatorContext);
384 softx87_free(&FpuEmulatorContext);
385 }
386
387 VOID EmulatorSetA20(BOOLEAN Enabled)
388 {
389 A20Line = Enabled;
390 }
391
392 /* EOF */