fb26ee1fab8d4c8f57a9a27dbae6a3c6c029318e
[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) >= BiosGetVideoMemoryStart())
36 && (Address < CONSOLE_VIDEO_MEM_END))
37 {
38 /* Call the VDM BIOS to update the video memory */
39 BiosUpdateVideoMemory(max(Address, BiosGetVideoMemoryStart()),
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) >= BiosGetVideoMemoryStart())
63 && (Address < CONSOLE_VIDEO_MEM_END))
64 {
65 /* Call the VDM BIOS to update the screen */
66 BiosUpdateConsole(max(Address, BiosGetVideoMemoryStart()),
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 return;
203 }
204 else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
205 {
206 /* It was an IRQ from the slave PIC */
207 BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8);
208 return;
209 }
210
211 switch (IntNum)
212 {
213 case BIOS_VIDEO_INTERRUPT:
214 {
215 /* This is the video BIOS interrupt, call the BIOS */
216 BiosVideoService();
217 break;
218 }
219 case BIOS_EQUIPMENT_INTERRUPT:
220 {
221 /* This is the BIOS "get equipment" command, call the BIOS */
222 BiosEquipmentService();
223 break;
224 }
225 case BIOS_KBD_INTERRUPT:
226 {
227 /* This is the keyboard BIOS interrupt, call the BIOS */
228 BiosKeyboardService();
229 break;
230 }
231 case BIOS_TIME_INTERRUPT:
232 {
233 /* This is the time BIOS interrupt, call the BIOS */
234 BiosTimeService();
235 break;
236 }
237 case 0x20:
238 {
239 DosInt20h(CodeSegment);
240 break;
241 }
242 case 0x21:
243 {
244 DosInt21h(CodeSegment);
245 break;
246 }
247 case 0x23:
248 {
249 DosBreakInterrupt();
250 break;
251 }
252 default:
253 {
254 DPRINT1("Unhandled interrupt: 0x%02X\n", IntNum);
255 break;
256 }
257 }
258 }
259 }
260
261 static VOID EmulatorHardwareInt(PVOID Context, BYTE Number)
262 {
263 /* Do nothing */
264 }
265
266 static VOID EmulatorHardwareIntAck(PVOID Context, BYTE Number)
267 {
268 /* Do nothing */
269 }
270
271 /* PUBLIC FUNCTIONS ***********************************************************/
272
273 BOOLEAN EmulatorInitialize()
274 {
275 /* Allocate memory for the 16-bit address space */
276 BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
277 if (BaseAddress == NULL) return FALSE;
278
279 /* Initialize the softx86 CPU emulator */
280 if (!softx86_init(&EmulatorContext, SX86_CPULEVEL_80286))
281 {
282 HeapFree(GetProcessHeap(), 0, BaseAddress);
283 return FALSE;
284 }
285
286 /* Initialize the softx87 FPU emulator*/
287 if(!softx87_init(&FpuEmulatorContext, SX87_FPULEVEL_8087))
288 {
289 softx86_free(&EmulatorContext);
290 HeapFree(GetProcessHeap(), 0, BaseAddress);
291 return FALSE;
292 }
293
294 /* Set memory read/write callbacks */
295 EmulatorContext.callbacks->on_read_memory = EmulatorReadMemory;
296 EmulatorContext.callbacks->on_write_memory = EmulatorWriteMemory;
297
298 /* Set MMIO read/write callbacks */
299 EmulatorContext.callbacks->on_read_io = EmulatorReadIo;
300 EmulatorContext.callbacks->on_write_io = EmulatorWriteIo;
301
302 /* Set interrupt callbacks */
303 EmulatorContext.callbacks->on_sw_int = EmulatorSoftwareInt;
304 EmulatorContext.callbacks->on_hw_int = EmulatorHardwareInt;
305 EmulatorContext.callbacks->on_hw_int_ack = EmulatorHardwareIntAck;
306
307 /* Connect the emulated FPU to the emulated CPU */
308 softx87_connect_to_CPU(&EmulatorContext, &FpuEmulatorContext);
309
310 /* Enable interrupts */
311 EmulatorSetFlag(EMULATOR_FLAG_IF);
312
313 return TRUE;
314 }
315
316 VOID EmulatorSetStack(WORD Segment, WORD Offset)
317 {
318 /* Call the softx86 API */
319 softx86_set_stack_ptr(&EmulatorContext, Segment, Offset);
320 }
321
322 VOID EmulatorExecute(WORD Segment, WORD Offset)
323 {
324 /* Call the softx86 API */
325 softx86_set_instruction_ptr(&EmulatorContext, Segment, Offset);
326 }
327
328 VOID EmulatorInterrupt(BYTE Number)
329 {
330 LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
331 UINT Segment, Offset;
332
333 /* Get the segment and offset */
334 Segment = HIWORD(IntVecTable[Number]);
335 Offset = LOWORD(IntVecTable[Number]);
336
337 /* Call the softx86 API */
338 softx86_make_simple_interrupt_call(&EmulatorContext, &Segment, &Offset);
339 }
340
341 VOID EmulatorExternalInterrupt(BYTE Number)
342 {
343 /* Call the softx86 API */
344 softx86_ext_hw_signal(&EmulatorContext, Number);
345 }
346
347 ULONG EmulatorGetRegister(ULONG Register)
348 {
349 if (Register < EMULATOR_REG_ES)
350 {
351 return EmulatorContext.state->general_reg[Register].val;
352 }
353 else
354 {
355 return EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val;
356 }
357 }
358
359 VOID EmulatorSetRegister(ULONG Register, ULONG Value)
360 {
361 if (Register < EMULATOR_REG_CS)
362 {
363 EmulatorContext.state->general_reg[Register].val = Value;
364 }
365 else
366 {
367 EmulatorContext.state->segment_reg[Register - EMULATOR_REG_ES].val = Value;
368 }
369 }
370
371 BOOLEAN EmulatorGetFlag(ULONG Flag)
372 {
373 return (EmulatorContext.state->reg_flags.val & Flag);
374 }
375
376 VOID EmulatorSetFlag(ULONG Flag)
377 {
378 EmulatorContext.state->reg_flags.val |= Flag;
379 }
380
381 VOID EmulatorClearFlag(ULONG Flag)
382 {
383 EmulatorContext.state->reg_flags.val &= ~Flag;
384 }
385
386 VOID EmulatorStep()
387 {
388 /* Print the current position - useful for debugging */
389 DPRINT("Executing at CS:IP = %04X:%04X\n",
390 EmulatorGetRegister(EMULATOR_REG_CS),
391 EmulatorContext.state->reg_ip);
392
393 /* Call the softx86 API */
394 if (!softx86_step(&EmulatorContext))
395 {
396 /* Invalid opcode */
397 EmulatorInterrupt(EMULATOR_EXCEPTION_INVALID_OPCODE);
398 }
399 }
400
401 VOID EmulatorCleanup()
402 {
403 /* Free the memory allocated for the 16-bit address space */
404 if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
405
406 /* Free the softx86 CPU and FPU emulator */
407 softx86_free(&EmulatorContext);
408 softx87_free(&FpuEmulatorContext);
409 }
410
411 VOID EmulatorSetA20(BOOLEAN Enabled)
412 {
413 A20Line = Enabled;
414 }
415
416 /* EOF */