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