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