[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 #define NDEBUG
12
13 #include "emulator.h"
14 #include "bios.h"
15 #include "dos.h"
16 #include "vga.h"
17 #include "pic.h"
18 #include "ps2.h"
19 #include "timer.h"
20
21 /* PRIVATE VARIABLES **********************************************************/
22
23 FAST486_STATE EmulatorContext;
24
25 static BOOLEAN A20Line = FALSE;
26
27 /* PRIVATE FUNCTIONS **********************************************************/
28
29 static VOID WINAPI EmulatorReadMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
30 {
31 UNREFERENCED_PARAMETER(Context);
32
33 /* If the A20 line is disabled, mask bit 20 */
34 if (!A20Line) Address &= ~(1 << 20);
35
36 /* Make sure the requested address is valid */
37 if ((Address + Size) >= MAX_ADDRESS) return;
38
39 /* Read the data from the virtual address space and store it in the buffer */
40 RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
41
42 /* Check if we modified the console video memory */
43 if (((Address + Size) >= VgaGetVideoBaseAddress())
44 && (Address < VgaGetVideoLimitAddress()))
45 {
46 DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
47 LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
48
49 /* Read from the VGA memory */
50 VgaReadMemory(VgaAddress, VgaBuffer, Size);
51 }
52 }
53
54 static VOID WINAPI EmulatorWriteMemory(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
55 {
56 UNREFERENCED_PARAMETER(Context);
57
58 /* If the A20 line is disabled, mask bit 20 */
59 if (!A20Line) Address &= ~(1 << 20);
60
61 /* Make sure the requested address is valid */
62 if ((Address + Size) >= MAX_ADDRESS) return;
63
64 /* Make sure we don't write to the ROM area */
65 if ((Address + Size) >= ROM_AREA_START && (Address < ROM_AREA_END)) return;
66
67 /* Read the data from the buffer and store it in the virtual address space */
68 RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
69
70 /* Check if we modified the console video memory */
71 if (((Address + Size) >= VgaGetVideoBaseAddress())
72 && (Address < VgaGetVideoLimitAddress()))
73 {
74 DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
75 LPBYTE VgaBuffer = &Buffer[VgaAddress - Address];
76
77 /* Write to the VGA memory */
78 VgaWriteMemory(VgaAddress, VgaBuffer, Size);
79 }
80 }
81
82 static VOID WINAPI EmulatorReadIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
83 {
84 UNREFERENCED_PARAMETER(Context);
85 UNREFERENCED_PARAMETER(Size);
86
87 switch (Address)
88 {
89 case PIC_MASTER_CMD:
90 case PIC_SLAVE_CMD:
91 {
92 *Buffer = PicReadCommand(Address);
93 break;
94 }
95
96 case PIC_MASTER_DATA:
97 case PIC_SLAVE_DATA:
98 {
99 *Buffer = PicReadData(Address);
100 break;
101 }
102
103 case PIT_DATA_PORT(0):
104 case PIT_DATA_PORT(1):
105 case PIT_DATA_PORT(2):
106 {
107 *Buffer = PitReadData(Address - PIT_DATA_PORT(0));
108 break;
109 }
110
111 case PS2_CONTROL_PORT:
112 {
113 *Buffer = KeyboardReadStatus();
114 break;
115 }
116
117 case PS2_DATA_PORT:
118 {
119 *Buffer = KeyboardReadData();
120 break;
121 }
122
123 case VGA_AC_WRITE:
124 case VGA_AC_READ:
125 case VGA_SEQ_INDEX:
126 case VGA_SEQ_DATA:
127 case VGA_DAC_READ_INDEX:
128 case VGA_DAC_WRITE_INDEX:
129 case VGA_DAC_DATA:
130 case VGA_MISC_READ:
131 case VGA_MISC_WRITE:
132 case VGA_CRTC_INDEX:
133 case VGA_CRTC_DATA:
134 case VGA_GC_INDEX:
135 case VGA_GC_DATA:
136 case VGA_STAT_MONO:
137 case VGA_STAT_COLOR:
138 {
139 *Buffer = VgaReadPort(Address);
140 break;
141 }
142
143 default:
144 {
145 DPRINT1("Read from unknown port: 0x%X\n", Address);
146 }
147 }
148 }
149
150 static VOID WINAPI EmulatorWriteIo(PVOID Context, UINT Address, LPBYTE Buffer, INT Size)
151 {
152 BYTE Byte = *Buffer;
153
154 UNREFERENCED_PARAMETER(Context);
155 UNREFERENCED_PARAMETER(Size);
156
157 switch (Address)
158 {
159 case PIT_COMMAND_PORT:
160 {
161 PitWriteCommand(Byte);
162 break;
163 }
164
165 case PIT_DATA_PORT(0):
166 case PIT_DATA_PORT(1):
167 case PIT_DATA_PORT(2):
168 {
169 PitWriteData(Address - PIT_DATA_PORT(0), Byte);
170 break;
171 }
172
173 case PIC_MASTER_CMD:
174 case PIC_SLAVE_CMD:
175 {
176 PicWriteCommand(Address, Byte);
177 break;
178 }
179
180 case PIC_MASTER_DATA:
181 case PIC_SLAVE_DATA:
182 {
183 PicWriteData(Address, Byte);
184 break;
185 }
186
187 case PS2_CONTROL_PORT:
188 {
189 KeyboardWriteCommand(Byte);
190 break;
191 }
192
193 case PS2_DATA_PORT:
194 {
195 KeyboardWriteData(Byte);
196 break;
197 }
198
199 case VGA_AC_WRITE:
200 case VGA_AC_READ:
201 case VGA_SEQ_INDEX:
202 case VGA_SEQ_DATA:
203 case VGA_DAC_READ_INDEX:
204 case VGA_DAC_WRITE_INDEX:
205 case VGA_DAC_DATA:
206 case VGA_MISC_READ:
207 case VGA_MISC_WRITE:
208 case VGA_CRTC_INDEX:
209 case VGA_CRTC_DATA:
210 case VGA_GC_INDEX:
211 case VGA_GC_DATA:
212 case VGA_STAT_MONO:
213 case VGA_STAT_COLOR:
214 {
215 VgaWritePort(Address, Byte);
216 break;
217 }
218
219 default:
220 {
221 DPRINT1("Write to unknown port: 0x%X\n", Address);
222 }
223 }
224 }
225
226 static VOID WINAPI EmulatorBiosOperation(PFAST486_STATE State, WORD Code)
227 {
228 WORD StackSegment, StackPointer, CodeSegment, InstructionPointer;
229 BYTE IntNum;
230 LPWORD Stack;
231
232 /* Get the SS:SP */
233 StackSegment = State->SegmentRegs[FAST486_REG_SS].Selector;
234 StackPointer = State->GeneralRegs[FAST486_REG_ESP].LowWord;
235
236 /* Get the stack */
237 Stack = (LPWORD)((ULONG_PTR)BaseAddress + TO_LINEAR(StackSegment, StackPointer));
238
239 if (Code == EMULATOR_INT_BOP)
240 {
241 /* Get the interrupt number */
242 IntNum = LOBYTE(Stack[STACK_INT_NUM]);
243
244 /* Get the CS:IP */
245 InstructionPointer = Stack[STACK_IP];
246 CodeSegment = Stack[STACK_CS];
247
248 /* Check if this was an exception */
249 if (IntNum < 8)
250 {
251 /* Display a message to the user */
252 DisplayMessage(L"Exception: %s occured at %04X:%04X",
253 ExceptionName[IntNum],
254 CodeSegment,
255 InstructionPointer);
256
257 /* Stop the VDM */
258 VdmRunning = FALSE;
259 return;
260 }
261
262 /* Check if this was an PIC IRQ */
263 if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
264 {
265 /* It was an IRQ from the master PIC */
266 BiosHandleIrq(IntNum - BIOS_PIC_MASTER_INT, Stack);
267 return;
268 }
269 else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
270 {
271 /* It was an IRQ from the slave PIC */
272 BiosHandleIrq(IntNum - BIOS_PIC_SLAVE_INT + 8, Stack);
273 return;
274 }
275
276 switch (IntNum)
277 {
278 case BIOS_VIDEO_INTERRUPT:
279 {
280 /* This is the video BIOS interrupt, call the BIOS */
281 BiosVideoService(Stack);
282 break;
283 }
284 case BIOS_EQUIPMENT_INTERRUPT:
285 {
286 /* This is the BIOS "get equipment" command, call the BIOS */
287 BiosEquipmentService(Stack);
288 break;
289 }
290 case BIOS_KBD_INTERRUPT:
291 {
292 /* This is the keyboard BIOS interrupt, call the BIOS */
293 BiosKeyboardService(Stack);
294 break;
295 }
296 case BIOS_TIME_INTERRUPT:
297 {
298 /* This is the time BIOS interrupt, call the BIOS */
299 BiosTimeService(Stack);
300 break;
301 }
302 case BIOS_SYS_TIMER_INTERRUPT:
303 {
304 /* BIOS timer update */
305 BiosSystemTimerInterrupt(Stack);
306 break;
307 }
308 case 0x20:
309 {
310 DosInt20h(Stack);
311 break;
312 }
313 case 0x21:
314 {
315 DosInt21h(Stack);
316 break;
317 }
318 case 0x23:
319 {
320 DosBreakInterrupt(Stack);
321 break;
322 }
323 default:
324 {
325 DPRINT1("Unhandled interrupt: 0x%02X\n", IntNum);
326 break;
327 }
328 }
329 }
330 }
331
332 /* PUBLIC FUNCTIONS ***********************************************************/
333
334 BOOLEAN EmulatorInitialize()
335 {
336 /* Allocate memory for the 16-bit address space */
337 BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
338 if (BaseAddress == NULL) return FALSE;
339
340 /* Set the callbacks */
341 EmulatorContext.MemReadCallback = (FAST486_MEM_READ_PROC)EmulatorReadMemory;
342 EmulatorContext.MemWriteCallback = (FAST486_MEM_WRITE_PROC)EmulatorWriteMemory;
343 EmulatorContext.IoReadCallback = (FAST486_IO_READ_PROC)EmulatorReadIo;
344 EmulatorContext.IoWriteCallback = (FAST486_IO_WRITE_PROC)EmulatorWriteIo;
345 EmulatorContext.BopCallback = (FAST486_BOP_PROC)EmulatorBiosOperation;
346
347 /* Reset the CPU */
348 Fast486Reset(&EmulatorContext);
349
350 /* Enable interrupts */
351 EmulatorSetFlag(EMULATOR_FLAG_IF);
352
353 return TRUE;
354 }
355
356 VOID EmulatorSetStack(WORD Segment, DWORD Offset)
357 {
358 Fast486SetStack(&EmulatorContext, Segment, Offset);
359 }
360
361 // FIXME: This function assumes 16-bit mode!!!
362 VOID EmulatorExecute(WORD Segment, WORD Offset)
363 {
364 /* Tell Fast486 to move the instruction pointer */
365 Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
366 }
367
368 VOID EmulatorInterrupt(BYTE Number)
369 {
370 /* Call the Fast486 API */
371 Fast486Interrupt(&EmulatorContext, Number);
372 }
373
374 VOID EmulatorExternalInterrupt(BYTE Number)
375 {
376 /* Call the Fast486 API */
377 Fast486Interrupt(&EmulatorContext, Number);
378 }
379
380 ULONG EmulatorGetRegister(ULONG Register)
381 {
382 if (Register < EMULATOR_REG_ES)
383 {
384 return EmulatorContext.GeneralRegs[Register].Long;
385 }
386 else
387 {
388 return EmulatorContext.SegmentRegs[Register - EMULATOR_REG_ES].Selector;
389 }
390 }
391
392 ULONG EmulatorGetProgramCounter(VOID)
393 {
394 return EmulatorContext.InstPtr.Long;
395 }
396
397 VOID EmulatorSetRegister(ULONG Register, ULONG Value)
398 {
399 if (Register < EMULATOR_REG_ES)
400 {
401 EmulatorContext.GeneralRegs[Register].Long = Value;
402 }
403 else
404 {
405 Fast486SetSegment(&EmulatorContext, Register - EMULATOR_REG_ES, (USHORT)Value);
406 }
407 }
408
409 BOOLEAN EmulatorGetFlag(ULONG Flag)
410 {
411 return (EmulatorContext.Flags.Long & Flag) ? TRUE : FALSE;
412 }
413
414 VOID EmulatorSetFlag(ULONG Flag)
415 {
416 EmulatorContext.Flags.Long |= Flag;
417 }
418
419 VOID EmulatorClearFlag(ULONG Flag)
420 {
421 EmulatorContext.Flags.Long &= ~Flag;
422 }
423
424 VOID EmulatorStep(VOID)
425 {
426 /* Dump the state for debugging purposes */
427 // Fast486DumpState(&EmulatorContext);
428
429 /* Execute the next instruction */
430 Fast486StepInto(&EmulatorContext);
431 }
432
433 VOID EmulatorCleanup(VOID)
434 {
435 /* Free the memory allocated for the 16-bit address space */
436 if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
437 }
438
439 VOID EmulatorSetA20(BOOLEAN Enabled)
440 {
441 A20Line = Enabled;
442 }
443
444 /* EOF */