bb09cd02662187edbc78d8d87319c46de58fff76
[reactos.git] / reactos / subsystems / ntvdm / cpu / cpu.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 "cpu.h"
14
15 #include "emulator.h"
16 #include "callback.h"
17 #include "bop.h"
18 #include <isvbop.h>
19
20 #include "clock.h"
21 #include "bios/rom.h"
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"
30
31 #include "io.h"
32
33 /* PRIVATE VARIABLES **********************************************************/
34
35 FAST486_STATE EmulatorContext;
36 BOOLEAN CpuRunning = FALSE;
37
38 /* No more than 'MaxCpuCallLevel' recursive CPU calls are allowed */
39 static const INT MaxCpuCallLevel = 32;
40 static INT CpuCallLevel = 0;
41
42 // BOOLEAN VdmRunning = TRUE;
43
44 #if 0
45 LPCWSTR ExceptionName[] =
46 {
47 L"Division By Zero",
48 L"Debug",
49 L"Unexpected Error",
50 L"Breakpoint",
51 L"Integer Overflow",
52 L"Bound Range Exceeded",
53 L"Invalid Opcode",
54 L"FPU Not Available"
55 };
56 #endif
57
58 // /* BOP Identifiers */
59 // #define BOP_DEBUGGER 0x56 // Break into the debugger from a 16-bit app
60
61 /* PRIVATE FUNCTIONS **********************************************************/
62
63 #if 0
64 VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack)
65 {
66 WORD CodeSegment, InstructionPointer;
67 PBYTE Opcode;
68
69 ASSERT(ExceptionNumber < 8);
70
71 /* Get the CS:IP */
72 InstructionPointer = Stack[STACK_IP];
73 CodeSegment = Stack[STACK_CS];
74 Opcode = (PBYTE)SEG_OFF_TO_PTR(CodeSegment, InstructionPointer);
75
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],
80 CodeSegment,
81 InstructionPointer,
82 Opcode[0],
83 Opcode[1],
84 Opcode[2],
85 Opcode[3],
86 Opcode[4],
87 Opcode[5],
88 Opcode[6],
89 Opcode[7],
90 Opcode[8],
91 Opcode[9]);
92
93 /* Stop the VDM */
94 EmulatorTerminate();
95 return;
96 }
97 #endif
98
99 // FIXME: This function assumes 16-bit mode!!!
100 VOID CpuExecute(WORD Segment, WORD Offset)
101 {
102 /* Tell Fast486 to move the instruction pointer */
103 Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
104 }
105
106 VOID CpuStep(VOID)
107 {
108 /* Dump the state for debugging purposes */
109 // Fast486DumpState(&EmulatorContext);
110
111 /* Execute the next instruction */
112 Fast486StepInto(&EmulatorContext);
113 }
114
115 VOID CpuSimulate(VOID)
116 {
117 if (CpuCallLevel > MaxCpuCallLevel)
118 {
119 DisplayMessage(L"Too many CPU levels of recursion (%d, expected maximum %d)",
120 CpuCallLevel, MaxCpuCallLevel);
121
122 /* Stop the VDM */
123 EmulatorTerminate();
124 return;
125 }
126 CpuCallLevel++;
127 DPRINT1("CpuSimulate --> Level %d\n", CpuCallLevel);
128
129 CpuRunning = TRUE;
130 while (VdmRunning && CpuRunning) ClockUpdate();
131
132 DPRINT1("CpuSimulate <-- Level %d\n", CpuCallLevel);
133 CpuCallLevel--;
134 if (CpuCallLevel < 0) CpuCallLevel = 0;
135
136 /* This takes into account for reentrance */
137 CpuRunning = TRUE;
138 }
139
140 VOID CpuUnsimulate(VOID)
141 {
142 /* Stop simulation */
143 CpuRunning = FALSE;
144 }
145
146 static VOID WINAPI CpuUnsimulateBop(LPWORD Stack)
147 {
148 CpuUnsimulate();
149 }
150
151 #if 0
152 VOID EmulatorTerminate(VOID)
153 {
154 /* Stop the VDM */
155 VdmRunning = FALSE;
156 }
157 #endif
158
159 /* PUBLIC FUNCTIONS ***********************************************************/
160
161 BOOLEAN CpuInitialize(VOID)
162 {
163 // /* Initialize the internal clock */
164 // if (!ClockInitialize())
165 // {
166 // wprintf(L"FATAL: Failed to initialize the clock\n");
167 // return FALSE;
168 // }
169
170 /* Initialize the CPU */
171 Fast486Initialize(&EmulatorContext,
172 EmulatorReadMemory,
173 EmulatorWriteMemory,
174 EmulatorReadIo,
175 EmulatorWriteIo,
176 NULL,
177 EmulatorBiosOperation,
178 EmulatorIntAcknowledge,
179 NULL /* TODO: Use a TLB */);
180
181 /* Initialize the software callback system and register the emulator BOPs */
182 // RegisterBop(BOP_DEBUGGER , EmulatorDebugBreakBop);
183 RegisterBop(BOP_UNSIMULATE, CpuUnsimulateBop);
184
185 return TRUE;
186 }
187
188 VOID CpuCleanup(VOID)
189 {
190 // Fast486Cleanup();
191 }
192
193 /* EOF */