[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\tCR1 = %08X\tCR2 = %08X\tCR3 = %08X\n"
221 "CR4 = %08X\tCR5 = %08X\tCR6 = %08X\tCR7 = %08X\n",
222 State->ControlRegisters[FAST486_REG_CR0],
223 State->ControlRegisters[FAST486_REG_CR1],
224 State->ControlRegisters[FAST486_REG_CR2],
225 State->ControlRegisters[FAST486_REG_CR3],
226 State->ControlRegisters[FAST486_REG_CR4],
227 State->ControlRegisters[FAST486_REG_CR5],
228 State->ControlRegisters[FAST486_REG_CR6],
229 State->ControlRegisters[FAST486_REG_CR7]);
230 DPRINT1("\nDebug Registers:\n"
231 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\tDR3 = %08X\n"
232 "DR4 = %08X\tDR5 = %08X\tDR6 = %08X\tDR7 = %08X\n",
233 State->DebugRegisters[FAST486_REG_DR0],
234 State->DebugRegisters[FAST486_REG_DR1],
235 State->DebugRegisters[FAST486_REG_DR2],
236 State->DebugRegisters[FAST486_REG_DR3],
237 State->DebugRegisters[FAST486_REG_DR4],
238 State->DebugRegisters[FAST486_REG_DR5],
239 State->DebugRegisters[FAST486_REG_DR6],
240 State->DebugRegisters[FAST486_REG_DR7]);
241 }
242
243 VOID
244 NTAPI
245 Fast486Reset(PFAST486_STATE State)
246 {
247 INT i;
248 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
249 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
250 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
251 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
252 FAST486_IDLE_PROC IdleCallback = State->IdleCallback;
253 FAST486_BOP_PROC BopCallback = State->BopCallback;
254
255 /* Clear the entire structure */
256 RtlZeroMemory(State, sizeof(*State));
257
258 /* Initialize the registers */
259 State->Flags.AlwaysSet = 1;
260 State->InstPtr.LowWord = 0xFFF0;
261
262 /* Initialize segments */
263 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
264 {
265 /* Set the selector, base and limit, other values don't apply in real mode */
266 State->SegmentRegs[i].Selector = 0;
267 State->SegmentRegs[i].Base = 0;
268 State->SegmentRegs[i].Limit = 0xFFFF;
269 }
270
271 /* Initialize the code segment */
272 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
273 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
274
275 /* Initialize the IDT */
276 State->Idtr.Size = 0x3FF;
277 State->Idtr.Address = 0;
278
279 /* Initialize CR0 */
280 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
281
282 /* Restore the callbacks */
283 State->MemReadCallback = MemReadCallback;
284 State->MemWriteCallback = MemWriteCallback;
285 State->IoReadCallback = IoReadCallback;
286 State->IoWriteCallback = IoWriteCallback;
287 State->IdleCallback = IdleCallback;
288 State->BopCallback = BopCallback;
289 }
290
291 VOID
292 NTAPI
293 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
294 {
295 /* Set the hardware interrupt flag */
296 State->HardwareInt = TRUE;
297 State->PendingIntNum = Number;
298 }
299
300 VOID
301 NTAPI
302 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
303 {
304 /* Load the new CS */
305 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
306 {
307 /* An exception occurred, let the handler execute instead */
308 return;
309 }
310
311 /* Set the new IP */
312 State->InstPtr.Long = Offset;
313 }
314
315 VOID
316 NTAPI
317 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
318 {
319 /* Load the new SS */
320 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
321 {
322 /* An exception occurred, let the handler execute instead */
323 return;
324 }
325
326 /* Set the new SP */
327 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
328 }
329
330 VOID
331 NTAPI
332 Fast486SetSegment(PFAST486_STATE State,
333 FAST486_SEG_REGS Segment,
334 USHORT Selector)
335 {
336 /* Call the internal function */
337 Fast486LoadSegment(State, Segment, Selector);
338 }
339
340 /* EOF */