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 *******************************************************************/
33 /* DEFINES ********************************************************************/
43 /* PRIVATE FUNCTIONS **********************************************************/
49 Fast486ExecutionControl(PFAST486_STATE State
, INT Command
)
52 INT ProcedureCallCount
= 0;
54 /* Main execution loop */
57 /* Check if this is a new instruction */
58 if (State
->PrefixFlags
== 0)
60 State
->SavedInstPtr
= State
->InstPtr
;
63 * Check if there is an interrupt to execute, or a hardware interrupt signal
64 * while interrupts are enabled.
66 if ((State
->IntStatus
== FAST486_INT_EXECUTE
)
68 && (State
->IntAckCallback
!= NULL
)
69 && (State
->IntStatus
== FAST486_INT_SIGNAL
)))
71 FAST486_IDT_ENTRY IdtEntry
;
73 if (State
->IntStatus
== FAST486_INT_SIGNAL
)
75 /* Acknowledge the interrupt to get the number */
76 State
->PendingIntNum
= State
->IntAckCallback(State
);
79 /* Get the interrupt vector */
80 if (Fast486GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
82 /* Perform the interrupt */
83 Fast486InterruptInternal(State
,
85 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
89 /* Clear the interrupt status */
90 State
->IntStatus
= FAST486_INT_NONE
;
94 /* Perform an instruction fetch */
95 if (!Fast486FetchByte(State
, &Opcode
)) continue;
97 // TODO: Check for CALL/RET to update ProcedureCallCount.
99 if (Fast486OpcodeHandlers
[Opcode
] != NULL
)
101 /* Call the opcode handler */
102 Fast486OpcodeHandlers
[Opcode
](State
, Opcode
);
106 /* This is not a valid opcode */
107 Fast486Exception(State
, FAST486_EXCEPTION_UD
);
110 if (Fast486OpcodeHandlers
[Opcode
] != Fast486OpcodePrefix
)
112 /* A non-prefix opcode has been executed, reset the prefix flags */
113 State
->PrefixFlags
= 0;
117 /* This is a prefix, go to the next instruction immediately */
121 while ((Command
== FAST486_CONTINUE
)
122 || (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0)
123 || (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0)
124 || (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
));
127 /* DEFAULT CALLBACKS **********************************************************/
131 Fast486MemReadCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
133 UNREFERENCED_PARAMETER(State
);
135 RtlMoveMemory(Buffer
, (PVOID
)Address
, Size
);
140 Fast486MemWriteCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
142 UNREFERENCED_PARAMETER(State
);
144 RtlMoveMemory((PVOID
)Address
, Buffer
, Size
);
149 Fast486IoReadCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG Size
, UCHAR Width
)
151 UNREFERENCED_PARAMETER(State
);
152 UNREFERENCED_PARAMETER(Port
);
153 UNREFERENCED_PARAMETER(Buffer
);
154 UNREFERENCED_PARAMETER(Size
);
155 UNREFERENCED_PARAMETER(Width
);
160 Fast486IoWriteCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG Size
, UCHAR Width
)
162 UNREFERENCED_PARAMETER(State
);
163 UNREFERENCED_PARAMETER(Port
);
164 UNREFERENCED_PARAMETER(Buffer
);
165 UNREFERENCED_PARAMETER(Size
);
166 UNREFERENCED_PARAMETER(Width
);
171 Fast486IdleCallback(PFAST486_STATE State
)
173 UNREFERENCED_PARAMETER(State
);
178 Fast486BopCallback(PFAST486_STATE State
, UCHAR BopCode
)
180 UNREFERENCED_PARAMETER(State
);
181 UNREFERENCED_PARAMETER(BopCode
);
186 Fast486IntAckCallback(PFAST486_STATE State
)
188 UNREFERENCED_PARAMETER(State
);
190 /* Return something... */
194 /* PUBLIC FUNCTIONS ***********************************************************/
198 Fast486Initialize(PFAST486_STATE State
,
199 FAST486_MEM_READ_PROC MemReadCallback
,
200 FAST486_MEM_WRITE_PROC MemWriteCallback
,
201 FAST486_IO_READ_PROC IoReadCallback
,
202 FAST486_IO_WRITE_PROC IoWriteCallback
,
203 FAST486_IDLE_PROC IdleCallback
,
204 FAST486_BOP_PROC BopCallback
,
205 FAST486_INT_ACK_PROC IntAckCallback
)
207 /* Set the callbacks (or use default ones if some are NULL) */
208 State
->MemReadCallback
= (MemReadCallback
? MemReadCallback
: Fast486MemReadCallback
);
209 State
->MemWriteCallback
= (MemWriteCallback
? MemWriteCallback
: Fast486MemWriteCallback
);
210 State
->IoReadCallback
= (IoReadCallback
? IoReadCallback
: Fast486IoReadCallback
);
211 State
->IoWriteCallback
= (IoWriteCallback
? IoWriteCallback
: Fast486IoWriteCallback
);
212 State
->IdleCallback
= (IdleCallback
? IdleCallback
: Fast486IdleCallback
);
213 State
->BopCallback
= (BopCallback
? BopCallback
: Fast486BopCallback
);
214 State
->IntAckCallback
= (IntAckCallback
? IntAckCallback
: Fast486IntAckCallback
);
222 Fast486Reset(PFAST486_STATE State
)
226 FAST486_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
227 FAST486_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
228 FAST486_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
229 FAST486_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
230 FAST486_IDLE_PROC IdleCallback
= State
->IdleCallback
;
231 FAST486_BOP_PROC BopCallback
= State
->BopCallback
;
232 FAST486_INT_ACK_PROC IntAckCallback
= State
->IntAckCallback
;
234 /* Clear the entire structure */
235 RtlZeroMemory(State
, sizeof(*State
));
237 /* Initialize the registers */
238 State
->Flags
.AlwaysSet
= 1;
239 State
->InstPtr
.LowWord
= 0xFFF0;
241 /* Initialize segments */
242 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
244 State
->SegmentRegs
[i
].Selector
= 0;
245 State
->SegmentRegs
[i
].Base
= 0;
246 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
247 State
->SegmentRegs
[i
].Present
= TRUE
;
248 State
->SegmentRegs
[i
].ReadWrite
= TRUE
;
249 State
->SegmentRegs
[i
].Executable
= FALSE
;
250 State
->SegmentRegs
[i
].DirConf
= FALSE
;
251 State
->SegmentRegs
[i
].SystemType
= 1; // Segment descriptor
252 State
->SegmentRegs
[i
].Dpl
= 0;
253 State
->SegmentRegs
[i
].Size
= FALSE
; // 16-bit
256 /* Initialize the code segment */
257 State
->SegmentRegs
[FAST486_REG_CS
].Executable
= TRUE
;
258 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
259 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
261 /* Initialize the IDT */
262 State
->Idtr
.Size
= 0x3FF;
263 State
->Idtr
.Address
= 0;
266 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
268 /* Restore the callbacks */
269 State
->MemReadCallback
= MemReadCallback
;
270 State
->MemWriteCallback
= MemWriteCallback
;
271 State
->IoReadCallback
= IoReadCallback
;
272 State
->IoWriteCallback
= IoWriteCallback
;
273 State
->IdleCallback
= IdleCallback
;
274 State
->BopCallback
= BopCallback
;
275 State
->IntAckCallback
= IntAckCallback
;
280 Fast486DumpState(PFAST486_STATE State
)
282 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
283 (State
->ControlRegisters
[0] & FAST486_CR0_PE
) ? "protected" : "real",
284 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
285 State
->InstPtr
.Long
);
286 DPRINT1("\nGeneral purpose registers:\n"
287 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
288 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
289 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
290 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
291 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
292 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
293 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
294 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
295 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
296 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
297 DPRINT1("\nSegment registers:\n"
298 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
299 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
300 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
301 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
302 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
303 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
304 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
305 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
306 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
307 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
308 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
309 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
310 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
311 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
312 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
313 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
314 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
315 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
316 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
317 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
318 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
319 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
320 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
321 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
322 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
323 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
324 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
325 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
326 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
327 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
328 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
330 State
->Flags
.Cf
? "CF" : "cf",
331 State
->Flags
.Pf
? "PF" : "pf",
332 State
->Flags
.Af
? "AF" : "af",
333 State
->Flags
.Zf
? "ZF" : "zf",
334 State
->Flags
.Sf
? "SF" : "sf",
335 State
->Flags
.Tf
? "TF" : "tf",
336 State
->Flags
.If
? "IF" : "if",
337 State
->Flags
.Df
? "DF" : "df",
338 State
->Flags
.Of
? "OF" : "of",
339 State
->Flags
.Nt
? "NT" : "nt",
340 State
->Flags
.Rf
? "RF" : "rf",
341 State
->Flags
.Vm
? "VM" : "vm",
342 State
->Flags
.Ac
? "AC" : "ac",
343 State
->Flags
.Vif
? "VIF" : "vif",
344 State
->Flags
.Vip
? "VIP" : "vip",
346 DPRINT1("\nControl Registers:\n"
347 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
348 State
->ControlRegisters
[FAST486_REG_CR0
],
349 State
->ControlRegisters
[FAST486_REG_CR2
],
350 State
->ControlRegisters
[FAST486_REG_CR3
]);
351 DPRINT1("\nDebug Registers:\n"
352 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
353 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
354 State
->DebugRegisters
[FAST486_REG_DR0
],
355 State
->DebugRegisters
[FAST486_REG_DR1
],
356 State
->DebugRegisters
[FAST486_REG_DR2
],
357 State
->DebugRegisters
[FAST486_REG_DR3
],
358 State
->DebugRegisters
[FAST486_REG_DR4
],
359 State
->DebugRegisters
[FAST486_REG_DR5
]);
364 Fast486Continue(PFAST486_STATE State
)
366 /* Call the internal function */
367 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
372 Fast486StepInto(PFAST486_STATE State
)
374 /* Call the internal function */
375 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
380 Fast486StepOver(PFAST486_STATE State
)
382 /* Call the internal function */
383 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
388 Fast486StepOut(PFAST486_STATE State
)
390 /* Call the internal function */
391 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);
396 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
398 /* Set the interrupt status and the number */
399 State
->IntStatus
= FAST486_INT_EXECUTE
;
400 State
->PendingIntNum
= Number
;
405 Fast486InterruptSignal(PFAST486_STATE State
)
407 /* Set the interrupt status */
408 State
->IntStatus
= FAST486_INT_SIGNAL
;
413 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
415 /* Load the new CS */
416 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
418 /* An exception occurred, let the handler execute instead */
423 State
->InstPtr
.Long
= Offset
;
428 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
430 /* Load the new SS */
431 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
433 /* An exception occurred, let the handler execute instead */
438 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
443 Fast486SetSegment(PFAST486_STATE State
,
444 FAST486_SEG_REGS Segment
,
447 /* Call the internal function */
448 Fast486LoadSegment(State
, Segment
, Selector
);