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) State
->SavedInstPtr
= State
->InstPtr
;
60 /* Perform an instruction fetch */
61 if (!Fast486FetchByte(State
, &Opcode
))
63 /* Exception occurred */
64 State
->PrefixFlags
= 0;
68 // TODO: Check for CALL/RET to update ProcedureCallCount.
70 if (Fast486OpcodeHandlers
[Opcode
] != NULL
)
72 /* Call the opcode handler */
73 Fast486OpcodeHandlers
[Opcode
](State
, Opcode
);
77 /* This is not a valid opcode */
78 Fast486Exception(State
, FAST486_EXCEPTION_UD
);
81 if (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
)
83 /* This is a prefix, go to the next instruction immediately */
88 * Check if there is an interrupt to execute, or a hardware interrupt signal
89 * while interrupts are enabled.
91 if (State
->IntStatus
== FAST486_INT_EXECUTE
)
93 FAST486_IDT_ENTRY IdtEntry
;
95 /* Get the interrupt vector */
96 if (Fast486GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
98 /* Perform the interrupt */
99 Fast486InterruptInternal(State
,
101 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
105 /* Clear the interrupt status */
106 State
->IntStatus
= FAST486_INT_NONE
;
108 else if (State
->Flags
.If
109 && (State
->IntAckCallback
!= NULL
)
110 && (State
->IntStatus
== FAST486_INT_SIGNAL
))
112 /* Acknowledge the interrupt to get the number */
113 State
->PendingIntNum
= State
->IntAckCallback(State
);
115 /* Set the interrupt status to execute on the next instruction */
116 State
->IntStatus
= FAST486_INT_EXECUTE
;
119 /* A non-prefix opcode has been executed, reset the prefix flags */
120 State
->PrefixFlags
= 0;
122 while ((Command
== FAST486_CONTINUE
)
123 || (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0)
124 || (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0)
125 || (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
));
128 /* DEFAULT CALLBACKS **********************************************************/
132 Fast486MemReadCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
134 UNREFERENCED_PARAMETER(State
);
136 RtlMoveMemory(Buffer
, (PVOID
)Address
, Size
);
141 Fast486MemWriteCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
143 UNREFERENCED_PARAMETER(State
);
145 RtlMoveMemory((PVOID
)Address
, Buffer
, Size
);
150 Fast486IoReadCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
152 UNREFERENCED_PARAMETER(State
);
153 UNREFERENCED_PARAMETER(Port
);
154 UNREFERENCED_PARAMETER(Buffer
);
155 UNREFERENCED_PARAMETER(DataCount
);
156 UNREFERENCED_PARAMETER(DataSize
);
161 Fast486IoWriteCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
163 UNREFERENCED_PARAMETER(State
);
164 UNREFERENCED_PARAMETER(Port
);
165 UNREFERENCED_PARAMETER(Buffer
);
166 UNREFERENCED_PARAMETER(DataCount
);
167 UNREFERENCED_PARAMETER(DataSize
);
172 Fast486IdleCallback(PFAST486_STATE State
)
174 UNREFERENCED_PARAMETER(State
);
179 Fast486BopCallback(PFAST486_STATE State
, UCHAR BopCode
)
181 UNREFERENCED_PARAMETER(State
);
182 UNREFERENCED_PARAMETER(BopCode
);
187 Fast486IntAckCallback(PFAST486_STATE State
)
189 UNREFERENCED_PARAMETER(State
);
191 /* Return something... */
195 /* PUBLIC FUNCTIONS ***********************************************************/
199 Fast486Initialize(PFAST486_STATE State
,
200 FAST486_MEM_READ_PROC MemReadCallback
,
201 FAST486_MEM_WRITE_PROC MemWriteCallback
,
202 FAST486_IO_READ_PROC IoReadCallback
,
203 FAST486_IO_WRITE_PROC IoWriteCallback
,
204 FAST486_IDLE_PROC IdleCallback
,
205 FAST486_BOP_PROC BopCallback
,
206 FAST486_INT_ACK_PROC IntAckCallback
,
209 /* Set the callbacks (or use default ones if some are NULL) */
210 State
->MemReadCallback
= (MemReadCallback
? MemReadCallback
: Fast486MemReadCallback
);
211 State
->MemWriteCallback
= (MemWriteCallback
? MemWriteCallback
: Fast486MemWriteCallback
);
212 State
->IoReadCallback
= (IoReadCallback
? IoReadCallback
: Fast486IoReadCallback
);
213 State
->IoWriteCallback
= (IoWriteCallback
? IoWriteCallback
: Fast486IoWriteCallback
);
214 State
->IdleCallback
= (IdleCallback
? IdleCallback
: Fast486IdleCallback
);
215 State
->BopCallback
= (BopCallback
? BopCallback
: Fast486BopCallback
);
216 State
->IntAckCallback
= (IntAckCallback
? IntAckCallback
: Fast486IntAckCallback
);
218 /* Set the TLB (if given) */
227 Fast486Reset(PFAST486_STATE State
)
231 FAST486_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
232 FAST486_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
233 FAST486_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
234 FAST486_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
235 FAST486_IDLE_PROC IdleCallback
= State
->IdleCallback
;
236 FAST486_BOP_PROC BopCallback
= State
->BopCallback
;
237 FAST486_INT_ACK_PROC IntAckCallback
= State
->IntAckCallback
;
238 PULONG Tlb
= State
->Tlb
;
240 /* Clear the entire structure */
241 RtlZeroMemory(State
, sizeof(*State
));
243 /* Initialize the registers */
244 State
->Flags
.AlwaysSet
= 1;
245 State
->InstPtr
.LowWord
= 0xFFF0;
247 /* Set the CPL to 0 */
250 /* Initialize segments */
251 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
253 State
->SegmentRegs
[i
].Selector
= 0;
254 State
->SegmentRegs
[i
].Base
= 0;
255 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
256 State
->SegmentRegs
[i
].Present
= TRUE
;
257 State
->SegmentRegs
[i
].ReadWrite
= TRUE
;
258 State
->SegmentRegs
[i
].Executable
= FALSE
;
259 State
->SegmentRegs
[i
].DirConf
= FALSE
;
260 State
->SegmentRegs
[i
].SystemType
= 1; // Segment descriptor
261 State
->SegmentRegs
[i
].Dpl
= 0;
262 State
->SegmentRegs
[i
].Size
= FALSE
; // 16-bit
265 /* Initialize the code segment */
266 State
->SegmentRegs
[FAST486_REG_CS
].Executable
= TRUE
;
267 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
268 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
270 /* Initialize the IDT */
271 State
->Idtr
.Size
= 0x3FF;
272 State
->Idtr
.Address
= 0;
274 #ifndef FAST486_NO_FPU
276 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
279 /* Restore the callbacks and TLB */
280 State
->MemReadCallback
= MemReadCallback
;
281 State
->MemWriteCallback
= MemWriteCallback
;
282 State
->IoReadCallback
= IoReadCallback
;
283 State
->IoWriteCallback
= IoWriteCallback
;
284 State
->IdleCallback
= IdleCallback
;
285 State
->BopCallback
= BopCallback
;
286 State
->IntAckCallback
= IntAckCallback
;
292 Fast486DumpState(PFAST486_STATE State
)
294 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
295 (State
->ControlRegisters
[0] & FAST486_CR0_PE
) ? "protected" : "real",
296 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
297 State
->InstPtr
.Long
);
298 DPRINT1("\nGeneral purpose registers:\n"
299 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
300 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
301 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
302 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
303 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
304 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
305 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
306 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
307 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
308 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
309 DPRINT1("\nSegment registers:\n"
310 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
311 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
312 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
313 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
314 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
315 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
316 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
317 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
318 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
319 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
320 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
321 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
322 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
323 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
324 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
325 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
326 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
327 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
328 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
329 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
330 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
331 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
332 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
333 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
334 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
335 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
336 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
337 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
338 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
339 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
340 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
342 State
->Flags
.Cf
? "CF" : "cf",
343 State
->Flags
.Pf
? "PF" : "pf",
344 State
->Flags
.Af
? "AF" : "af",
345 State
->Flags
.Zf
? "ZF" : "zf",
346 State
->Flags
.Sf
? "SF" : "sf",
347 State
->Flags
.Tf
? "TF" : "tf",
348 State
->Flags
.If
? "IF" : "if",
349 State
->Flags
.Df
? "DF" : "df",
350 State
->Flags
.Of
? "OF" : "of",
351 State
->Flags
.Nt
? "NT" : "nt",
352 State
->Flags
.Rf
? "RF" : "rf",
353 State
->Flags
.Vm
? "VM" : "vm",
355 DPRINT1("\nControl Registers:\n"
356 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
357 State
->ControlRegisters
[FAST486_REG_CR0
],
358 State
->ControlRegisters
[FAST486_REG_CR2
],
359 State
->ControlRegisters
[FAST486_REG_CR3
]);
360 DPRINT1("\nDebug Registers:\n"
361 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
362 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
363 State
->DebugRegisters
[FAST486_REG_DR0
],
364 State
->DebugRegisters
[FAST486_REG_DR1
],
365 State
->DebugRegisters
[FAST486_REG_DR2
],
366 State
->DebugRegisters
[FAST486_REG_DR3
],
367 State
->DebugRegisters
[FAST486_REG_DR4
],
368 State
->DebugRegisters
[FAST486_REG_DR5
]);
373 Fast486Continue(PFAST486_STATE State
)
375 /* Call the internal function */
376 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
381 Fast486StepInto(PFAST486_STATE State
)
383 /* Call the internal function */
384 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
389 Fast486StepOver(PFAST486_STATE State
)
391 /* Call the internal function */
392 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
397 Fast486StepOut(PFAST486_STATE State
)
399 /* Call the internal function */
400 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);
405 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
407 /* Set the interrupt status and the number */
408 State
->IntStatus
= FAST486_INT_EXECUTE
;
409 State
->PendingIntNum
= Number
;
414 Fast486InterruptSignal(PFAST486_STATE State
)
416 /* Set the interrupt status */
417 State
->IntStatus
= FAST486_INT_SIGNAL
;
422 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
424 /* Load the new CS */
425 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
427 /* An exception occurred, let the handler execute instead */
432 State
->InstPtr
.Long
= Offset
;
437 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
439 /* Load the new SS */
440 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
442 /* An exception occurred, let the handler execute instead */
447 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
452 Fast486SetSegment(PFAST486_STATE State
,
453 FAST486_SEG_REGS Segment
,
456 /* Call the internal function */
457 Fast486LoadSegment(State
, Segment
, Selector
);