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; // == 0: CPU stopped; >= 1: CPU running or halted
43 LPCWSTR ExceptionName
[] =
50 L
"Bound Range Exceeded",
56 // /* BOP Identifiers */
57 // #define BOP_DEBUGGER 0x56 // Break into the debugger from a 16-bit app
59 /* PRIVATE FUNCTIONS **********************************************************/
62 VOID
EmulatorException(BYTE ExceptionNumber
, LPWORD Stack
)
64 WORD CodeSegment
, InstructionPointer
;
67 ASSERT(ExceptionNumber
< 8);
70 InstructionPointer
= Stack
[STACK_IP
];
71 CodeSegment
= Stack
[STACK_CS
];
72 Opcode
= (PBYTE
)SEG_OFF_TO_PTR(CodeSegment
, InstructionPointer
);
74 /* Display a message to the user */
75 DisplayMessage(L
"Exception: %s occured at %04X:%04X\n"
76 L
"Opcode: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
77 ExceptionName
[ExceptionNumber
],
97 // FIXME: This function assumes 16-bit mode!!!
98 VOID
CpuExecute(WORD Segment
, WORD Offset
)
100 /* Tell Fast486 to move the instruction pointer */
101 Fast486ExecuteAt(&EmulatorContext
, Segment
, Offset
);
106 /* Dump the state for debugging purposes */
107 // Fast486DumpState(&EmulatorContext);
109 /* Execute the next instruction */
110 Fast486StepInto(&EmulatorContext
);
113 VOID
CpuSimulate(VOID
)
115 if (CpuCallLevel
> MaxCpuCallLevel
)
117 DisplayMessage(L
"Too many CPU levels of recursion (%d, expected maximum %d)",
118 CpuCallLevel
, MaxCpuCallLevel
);
125 DPRINT("CpuSimulate --> Level %d\n", CpuCallLevel
);
128 while (VdmRunning
&& CpuRunning
) ClockUpdate();
130 DPRINT("CpuSimulate <-- Level %d\n", CpuCallLevel
);
132 if (!VdmRunning
|| CpuCallLevel
< 0) CpuCallLevel
= 0;
134 /* This takes into account for reentrance */
135 if (VdmRunning
&& (CpuCallLevel
> 0)) CpuRunning
= TRUE
;
138 VOID
CpuUnsimulate(VOID
)
140 /* Stop simulation */
144 static VOID WINAPI
CpuUnsimulateBop(LPWORD Stack
)
149 /* PUBLIC FUNCTIONS ***********************************************************/
151 BOOLEAN
CpuInitialize(VOID
)
153 // /* Initialize the internal clock */
154 // if (!ClockInitialize())
156 // wprintf(L"FATAL: Failed to initialize the clock\n");
160 /* Initialize the CPU */
161 Fast486Initialize(&EmulatorContext
,
167 EmulatorBiosOperation
,
168 EmulatorIntAcknowledge
,
169 NULL
/* TODO: Use a TLB */);
171 /* Initialize the software callback system and register the emulator BOPs */
172 // RegisterBop(BOP_DEBUGGER , EmulatorDebugBreakBop);
173 RegisterBop(BOP_UNSIMULATE
, CpuUnsimulateBop
);
178 VOID
CpuCleanup(VOID
)