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\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
]);
237 Fast486Reset(PFAST486_STATE State
)
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
;
247 /* Clear the entire structure */
248 RtlZeroMemory(State
, sizeof(*State
));
250 /* Initialize the registers */
251 State
->Flags
.AlwaysSet
= 1;
252 State
->InstPtr
.LowWord
= 0xFFF0;
254 /* Initialize segments */
255 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
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;
263 /* Initialize the code segment */
264 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
265 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
267 /* Initialize the IDT */
268 State
->Idtr
.Size
= 0x3FF;
269 State
->Idtr
.Address
= 0;
272 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
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
;
285 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
287 /* Set the hardware interrupt flag */
288 State
->HardwareInt
= TRUE
;
289 State
->PendingIntNum
= Number
;
294 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
296 /* Load the new CS */
297 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
299 /* An exception occurred, let the handler execute instead */
304 State
->InstPtr
.Long
= Offset
;
309 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
311 /* Load the new SS */
312 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
314 /* An exception occurred, let the handler execute instead */
319 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
324 Fast486SetSegment(PFAST486_STATE State
,
325 FAST486_SEG_REGS Segment
,
328 /* Call the internal function */
329 Fast486LoadSegment(State
, Segment
, Selector
);