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