[NTVDM]
[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
128 CpuRunning = TRUE;
129 while (VdmRunning && CpuRunning) ClockUpdate();
130
131 CpuCallLevel--;
132 if (CpuCallLevel < 0) CpuCallLevel = 0;
133
134 /* This takes into account for reentrance */
135 CpuRunning = TRUE;
136 }
137
138 VOID CpuUnsimulate(VOID)
139 {
140 /* Stop simulation */
141 CpuRunning = FALSE;
142 }
143
144 static VOID WINAPI CpuUnsimulateBop(LPWORD Stack)
145 {
146 CpuUnsimulate();
147 }
148
149 #if 0
150 VOID EmulatorTerminate(VOID)
151 {
152 /* Stop the VDM */
153 VdmRunning = FALSE;
154 }
155 #endif
156
157 /* PUBLIC FUNCTIONS ***********************************************************/
158
159 BOOLEAN CpuInitialize(VOID)
160 {
161 // /* Initialize the internal clock */
162 // if (!ClockInitialize())
163 // {
164 // wprintf(L"FATAL: Failed to initialize the clock\n");
165 // return FALSE;
166 // }
167
168 /* Initialize the CPU */
169 Fast486Initialize(&EmulatorContext,
170 EmulatorReadMemory,
171 EmulatorWriteMemory,
172 EmulatorReadIo,
173 EmulatorWriteIo,
174 NULL,
175 EmulatorBiosOperation,
176 EmulatorIntAcknowledge,
177 NULL /* TODO: Use a TLB */);
178
179 /* Initialize the software callback system and register the emulator BOPs */
180 // RegisterBop(BOP_DEBUGGER , EmulatorDebugBreakBop);
181 RegisterBop(BOP_UNSIMULATE, CpuUnsimulateBop);
182
183 return TRUE;
184 }
185
186 VOID CpuCleanup(VOID)
187 {
188 // Fast486Cleanup();
189 }
190
191 /* EOF */