2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
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.
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.
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.
22 /* INCLUDES *******************************************************************/
24 // #define WIN32_NO_STATUS
25 // #define _INC_WINDOWS
35 /* DEFINES ********************************************************************/
45 /* PRIVATE FUNCTIONS **********************************************************/
51 Fast486ExecutionControl(PFAST486_STATE State
, INT Command
)
54 INT ProcedureCallCount
= 0;
56 /* Main execution loop */
59 /* Check if this is a new instruction */
60 if (State
->PrefixFlags
== 0)
62 State
->SavedInstPtr
= State
->InstPtr
;
64 /* Check if interrupts are enabled and there is an interrupt pending */
65 if (State
->Flags
.If
&& State
->HardwareInt
)
67 FAST486_IDT_ENTRY IdtEntry
;
69 /* Get the interrupt vector */
70 if (Fast486GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
72 /* Perform the interrupt */
73 Fast486InterruptInternal(State
,
75 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
79 /* Clear the interrupt pending flag */
80 State
->HardwareInt
= FALSE
;
84 /* Perform an instruction fetch */
85 if (!Fast486FetchByte(State
, &Opcode
)) continue;
87 // TODO: Check for CALL/RET to update ProcedureCallCount.
89 if (Fast486OpcodeHandlers
[Opcode
] != NULL
)
91 /* Call the opcode handler */
92 Fast486OpcodeHandlers
[Opcode
](State
, Opcode
);
96 /* This is not a valid opcode */
97 Fast486Exception(State
, FAST486_EXCEPTION_UD
);
100 if (Fast486OpcodeHandlers
[Opcode
] != Fast486OpcodePrefix
)
102 /* A non-prefix opcode has been executed, reset the prefix flags */
103 State
->PrefixFlags
= 0;
107 /* This is a prefix, go to the next instruction immediately */
111 while ((Command
== FAST486_CONTINUE
)
112 || (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0)
113 || (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0)
114 || (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
));
117 /* PUBLIC FUNCTIONS ***********************************************************/
121 Fast486Continue(PFAST486_STATE State
)
123 /* Call the internal function */
124 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
129 Fast486StepInto(PFAST486_STATE State
)
131 /* Call the internal function */
132 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
137 Fast486StepOver(PFAST486_STATE State
)
139 /* Call the internal function */
140 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
145 Fast486StepOut(PFAST486_STATE State
)
147 /* Call the internal function */
148 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);
153 Fast486DumpState(PFAST486_STATE State
)
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",
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",
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
]);
245 Fast486Reset(PFAST486_STATE State
)
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
;
255 /* Clear the entire structure */
256 RtlZeroMemory(State
, sizeof(*State
));
258 /* Initialize the registers */
259 State
->Flags
.AlwaysSet
= 1;
260 State
->InstPtr
.LowWord
= 0xFFF0;
262 /* Initialize segments */
263 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
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;
271 /* Initialize the code segment */
272 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
273 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
275 /* Initialize the IDT */
276 State
->Idtr
.Size
= 0x3FF;
277 State
->Idtr
.Address
= 0;
280 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
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
;
293 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
295 /* Set the hardware interrupt flag */
296 State
->HardwareInt
= TRUE
;
297 State
->PendingIntNum
= Number
;
302 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
304 /* Load the new CS */
305 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
307 /* An exception occurred, let the handler execute instead */
312 State
->InstPtr
.Long
= Offset
;
317 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
319 /* Load the new SS */
320 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
322 /* An exception occurred, let the handler execute instead */
327 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
332 Fast486SetSegment(PFAST486_STATE State
,
333 FAST486_SEG_REGS Segment
,
336 /* Call the internal function */
337 Fast486LoadSegment(State
, Segment
, Selector
);