[FAST486]
[reactos.git] / lib / fast486 / fast486.c
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486.c
4 *
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 /* INCLUDES *******************************************************************/
23
24 // #define WIN32_NO_STATUS
25 // #define _INC_WINDOWS
26 #include <windef.h>
27
28 // #define NDEBUG
29 #include <debug.h>
30
31 #include <fast486.h>
32 #include "common.h"
33 #include "opcodes.h"
34
35 /* DEFINES ********************************************************************/
36
37 typedef enum
38 {
39 FAST486_STEP_INTO,
40 FAST486_STEP_OVER,
41 FAST486_STEP_OUT,
42 FAST486_CONTINUE
43 } FAST486_EXEC_CMD;
44
45 /* PRIVATE FUNCTIONS **********************************************************/
46
47 static
48 inline
49 VOID
50 NTAPI
51 Fast486ExecutionControl(PFAST486_STATE State, INT Command)
52 {
53 UCHAR Opcode;
54 INT ProcedureCallCount = 0;
55
56 /* Main execution loop */
57 do
58 {
59 /* Check if this is a new instruction */
60 if (State->PrefixFlags == 0)
61 {
62 State->SavedInstPtr = State->InstPtr;
63
64 /* Check if interrupts are enabled and there is an interrupt pending */
65 if (State->Flags.If && State->HardwareInt)
66 {
67 FAST486_IDT_ENTRY IdtEntry;
68
69 /* Get the interrupt vector */
70 if (Fast486GetIntVector(State, State->PendingIntNum, &IdtEntry))
71 {
72 /* Perform the interrupt */
73 Fast486InterruptInternal(State,
74 IdtEntry.Selector,
75 MAKELONG(IdtEntry.Offset, IdtEntry.OffsetHigh),
76 IdtEntry.Type);
77 }
78
79 /* Clear the interrupt pending flag */
80 State->HardwareInt = FALSE;
81 }
82 }
83
84 /* Perform an instruction fetch */
85 if (!Fast486FetchByte(State, &Opcode)) continue;
86
87 // TODO: Check for CALL/RET to update ProcedureCallCount.
88
89 if (Fast486OpcodeHandlers[Opcode] != NULL)
90 {
91 /* Call the opcode handler */
92 Fast486OpcodeHandlers[Opcode](State, Opcode);
93 }
94 else
95 {
96 /* This is not a valid opcode */
97 Fast486Exception(State, FAST486_EXCEPTION_UD);
98 }
99
100 if (Fast486OpcodeHandlers[Opcode] != Fast486OpcodePrefix)
101 {
102 /* A non-prefix opcode has been executed, reset the prefix flags */
103 State->PrefixFlags = 0;
104 }
105 else
106 {
107 /* This is a prefix, go to the next instruction immediately */
108 continue;
109 }
110 }
111 while ((Command == FAST486_CONTINUE)
112 || (Command == FAST486_STEP_OVER && ProcedureCallCount > 0)
113 || (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0)
114 || (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix));
115 }
116
117 /* PUBLIC FUNCTIONS ***********************************************************/
118
119 VOID
120 NTAPI
121 Fast486Continue(PFAST486_STATE State)
122 {
123 /* Call the internal function */
124 Fast486ExecutionControl(State, FAST486_CONTINUE);
125 }
126
127 VOID
128 NTAPI
129 Fast486StepInto(PFAST486_STATE State)
130 {
131 /* Call the internal function */
132 Fast486ExecutionControl(State, FAST486_STEP_INTO);
133 }
134
135 VOID
136 NTAPI
137 Fast486StepOver(PFAST486_STATE State)
138 {
139 /* Call the internal function */
140 Fast486ExecutionControl(State, FAST486_STEP_OVER);
141 }
142
143 VOID
144 NTAPI
145 Fast486StepOut(PFAST486_STATE State)
146 {
147 /* Call the internal function */
148 Fast486ExecutionControl(State, FAST486_STEP_OUT);
149 }
150
151 VOID
152 NTAPI
153 Fast486DumpState(PFAST486_STATE State)
154 {
155 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
156 (State->ControlRegisters[0] & FAST486_CR0_PE) ? "protected" : "real",
157 State->SegmentRegs[FAST486_REG_CS].Selector,
158 State->InstPtr.Long);
159 DPRINT1("\nGeneral purpose registers:\n"
160 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
161 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
162 State->GeneralRegs[FAST486_REG_EAX].Long,
163 State->GeneralRegs[FAST486_REG_ECX].Long,
164 State->GeneralRegs[FAST486_REG_EDX].Long,
165 State->GeneralRegs[FAST486_REG_EBX].Long,
166 State->GeneralRegs[FAST486_REG_ESP].Long,
167 State->GeneralRegs[FAST486_REG_EBP].Long,
168 State->GeneralRegs[FAST486_REG_ESI].Long,
169 State->GeneralRegs[FAST486_REG_EDI].Long);
170 DPRINT1("\nSegment registers:\n"
171 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
172 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
173 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
174 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
175 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
176 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
177 State->SegmentRegs[FAST486_REG_ES].Selector,
178 State->SegmentRegs[FAST486_REG_ES].Base,
179 State->SegmentRegs[FAST486_REG_ES].Limit,
180 State->SegmentRegs[FAST486_REG_ES].Dpl,
181 State->SegmentRegs[FAST486_REG_CS].Selector,
182 State->SegmentRegs[FAST486_REG_CS].Base,
183 State->SegmentRegs[FAST486_REG_CS].Limit,
184 State->SegmentRegs[FAST486_REG_CS].Dpl,
185 State->SegmentRegs[FAST486_REG_SS].Selector,
186 State->SegmentRegs[FAST486_REG_SS].Base,
187 State->SegmentRegs[FAST486_REG_SS].Limit,
188 State->SegmentRegs[FAST486_REG_SS].Dpl,
189 State->SegmentRegs[FAST486_REG_DS].Selector,
190 State->SegmentRegs[FAST486_REG_DS].Base,
191 State->SegmentRegs[FAST486_REG_DS].Limit,
192 State->SegmentRegs[FAST486_REG_DS].Dpl,
193 State->SegmentRegs[FAST486_REG_FS].Selector,
194 State->SegmentRegs[FAST486_REG_FS].Base,
195 State->SegmentRegs[FAST486_REG_FS].Limit,
196 State->SegmentRegs[FAST486_REG_FS].Dpl,
197 State->SegmentRegs[FAST486_REG_GS].Selector,
198 State->SegmentRegs[FAST486_REG_GS].Base,
199 State->SegmentRegs[FAST486_REG_GS].Limit,
200 State->SegmentRegs[FAST486_REG_GS].Dpl);
201 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
202 State->Flags.Long,
203 State->Flags.Cf ? "CF" : "cf",
204 State->Flags.Pf ? "PF" : "pf",
205 State->Flags.Af ? "AF" : "af",
206 State->Flags.Zf ? "ZF" : "zf",
207 State->Flags.Sf ? "SF" : "sf",
208 State->Flags.Tf ? "TF" : "tf",
209 State->Flags.If ? "IF" : "if",
210 State->Flags.Df ? "DF" : "df",
211 State->Flags.Of ? "OF" : "of",
212 State->Flags.Nt ? "NT" : "nt",
213 State->Flags.Rf ? "RF" : "rf",
214 State->Flags.Vm ? "VM" : "vm",
215 State->Flags.Ac ? "AC" : "ac",
216 State->Flags.Vif ? "VIF" : "vif",
217 State->Flags.Vip ? "VIP" : "vip",
218 State->Flags.Iopl);
219 DPRINT1("\nControl Registers:\n"
220 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
221 State->ControlRegisters[FAST486_REG_CR0],
222 State->ControlRegisters[FAST486_REG_CR2],
223 State->ControlRegisters[FAST486_REG_CR3]);
224 DPRINT1("\nDebug Registers:\n"
225 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
226 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
227 State->DebugRegisters[FAST486_REG_DR0],
228 State->DebugRegisters[FAST486_REG_DR1],
229 State->DebugRegisters[FAST486_REG_DR2],
230 State->DebugRegisters[FAST486_REG_DR3],
231 State->DebugRegisters[FAST486_REG_DR4],
232 State->DebugRegisters[FAST486_REG_DR5]);
233 }
234
235 VOID
236 NTAPI
237 Fast486Reset(PFAST486_STATE State)
238 {
239 INT i;
240 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
241 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
242 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
243 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
244 FAST486_IDLE_PROC IdleCallback = State->IdleCallback;
245 FAST486_BOP_PROC BopCallback = State->BopCallback;
246
247 /* Clear the entire structure */
248 RtlZeroMemory(State, sizeof(*State));
249
250 /* Initialize the registers */
251 State->Flags.AlwaysSet = 1;
252 State->InstPtr.LowWord = 0xFFF0;
253
254 /* Initialize segments */
255 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
256 {
257 /* Set the selector, base and limit, other values don't apply in real mode */
258 State->SegmentRegs[i].Selector = 0;
259 State->SegmentRegs[i].Base = 0;
260 State->SegmentRegs[i].Limit = 0xFFFF;
261 }
262
263 /* Initialize the code segment */
264 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
265 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
266
267 /* Initialize the IDT */
268 State->Idtr.Size = 0x3FF;
269 State->Idtr.Address = 0;
270
271 /* Initialize CR0 */
272 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
273
274 /* Restore the callbacks */
275 State->MemReadCallback = MemReadCallback;
276 State->MemWriteCallback = MemWriteCallback;
277 State->IoReadCallback = IoReadCallback;
278 State->IoWriteCallback = IoWriteCallback;
279 State->IdleCallback = IdleCallback;
280 State->BopCallback = BopCallback;
281 }
282
283 VOID
284 NTAPI
285 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
286 {
287 /* Set the hardware interrupt flag */
288 State->HardwareInt = TRUE;
289 State->PendingIntNum = Number;
290 }
291
292 VOID
293 NTAPI
294 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
295 {
296 /* Load the new CS */
297 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
298 {
299 /* An exception occurred, let the handler execute instead */
300 return;
301 }
302
303 /* Set the new IP */
304 State->InstPtr.Long = Offset;
305 }
306
307 VOID
308 NTAPI
309 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
310 {
311 /* Load the new SS */
312 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
313 {
314 /* An exception occurred, let the handler execute instead */
315 return;
316 }
317
318 /* Set the new SP */
319 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
320 }
321
322 VOID
323 NTAPI
324 Fast486SetSegment(PFAST486_STATE State,
325 FAST486_SEG_REGS Segment,
326 USHORT Selector)
327 {
328 /* Call the internal function */
329 Fast486LoadSegment(State, Segment, Selector);
330 }
331
332 /* EOF */