2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: Minimal x86 machine emulator for the VDM
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
22 #include "hardware/cmos.h"
23 #include "hardware/keyboard.h"
24 #include "hardware/mouse.h"
25 #include "hardware/pic.h"
26 #include "hardware/ps2.h"
27 #include "hardware/speaker.h"
28 #include "hardware/timer.h"
29 #include "hardware/vga.h"
33 /* PRIVATE VARIABLES **********************************************************/
35 FAST486_STATE EmulatorContext
;
36 BOOLEAN CpuRunning
= FALSE
;
38 /* No more than 'MaxCpuCallLevel' recursive CPU calls are allowed */
39 static const INT MaxCpuCallLevel
= 32;
40 static INT CpuCallLevel
= 0;
42 // BOOLEAN VdmRunning = TRUE;
45 LPCWSTR ExceptionName
[] =
52 L
"Bound Range Exceeded",
58 // /* BOP Identifiers */
59 // #define BOP_DEBUGGER 0x56 // Break into the debugger from a 16-bit app
61 /* PRIVATE FUNCTIONS **********************************************************/
64 VOID
EmulatorException(BYTE ExceptionNumber
, LPWORD Stack
)
66 WORD CodeSegment
, InstructionPointer
;
69 ASSERT(ExceptionNumber
< 8);
72 InstructionPointer
= Stack
[STACK_IP
];
73 CodeSegment
= Stack
[STACK_CS
];
74 Opcode
= (PBYTE
)SEG_OFF_TO_PTR(CodeSegment
, InstructionPointer
);
76 /* Display a message to the user */
77 DisplayMessage(L
"Exception: %s occured at %04X:%04X\n"
78 L
"Opcode: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
79 ExceptionName
[ExceptionNumber
],
99 // FIXME: This function assumes 16-bit mode!!!
100 VOID
CpuExecute(WORD Segment
, WORD Offset
)
102 /* Tell Fast486 to move the instruction pointer */
103 Fast486ExecuteAt(&EmulatorContext
, Segment
, Offset
);
108 /* Dump the state for debugging purposes */
109 // Fast486DumpState(&EmulatorContext);
111 /* Execute the next instruction */
112 Fast486StepInto(&EmulatorContext
);
115 VOID
CpuSimulate(VOID
)
117 if (CpuCallLevel
> MaxCpuCallLevel
)
119 DisplayMessage(L
"Too many CPU levels of recursion (%d, expected maximum %d)",
120 CpuCallLevel
, MaxCpuCallLevel
);
127 DPRINT1("CpuSimulate --> Level %d\n", CpuCallLevel
);
130 while (VdmRunning
&& CpuRunning
) ClockUpdate();
132 DPRINT1("CpuSimulate <-- Level %d\n", CpuCallLevel
);
134 if (CpuCallLevel
< 0) CpuCallLevel
= 0;
136 /* This takes into account for reentrance */
140 VOID
CpuUnsimulate(VOID
)
142 /* Stop simulation */
146 static VOID WINAPI
CpuUnsimulateBop(LPWORD Stack
)
152 VOID
EmulatorTerminate(VOID
)
159 /* PUBLIC FUNCTIONS ***********************************************************/
161 BOOLEAN
CpuInitialize(VOID
)
163 // /* Initialize the internal clock */
164 // if (!ClockInitialize())
166 // wprintf(L"FATAL: Failed to initialize the clock\n");
170 /* Initialize the CPU */
171 Fast486Initialize(&EmulatorContext
,
177 EmulatorBiosOperation
,
178 EmulatorIntAcknowledge
,
179 NULL
/* TODO: Use a TLB */);
181 /* Initialize the software callback system and register the emulator BOPs */
182 // RegisterBop(BOP_DEBUGGER , EmulatorDebugBreakBop);
183 RegisterBop(BOP_UNSIMULATE
, CpuUnsimulateBop
);
188 VOID
CpuCleanup(VOID
)