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
, FAST486_EXEC_CMD 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 */
87 /* A non-prefix opcode has been executed, reset the prefix flags */
88 State
->PrefixFlags
= 0;
91 * Check if there is an interrupt to execute, or a hardware interrupt signal
92 * while interrupts are enabled.
94 if (State
->IntStatus
== FAST486_INT_EXECUTE
)
96 FAST486_IDT_ENTRY IdtEntry
;
98 /* Get the interrupt vector */
99 if (Fast486GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
101 /* Perform the interrupt */
102 Fast486InterruptInternal(State
,
104 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
107 /* Restore the prefix flags, which would be set to OPSIZE for 32-bit real mode */
108 State
->PrefixFlags
= 0;
111 /* Clear the interrupt status */
112 State
->IntStatus
= FAST486_INT_NONE
;
114 else if (State
->Flags
.If
115 && (State
->IntAckCallback
!= NULL
)
116 && (State
->IntStatus
== FAST486_INT_SIGNAL
))
118 /* Acknowledge the interrupt to get the number */
119 State
->PendingIntNum
= State
->IntAckCallback(State
);
121 /* Set the interrupt status to execute on the next instruction */
122 State
->IntStatus
= FAST486_INT_EXECUTE
;
124 else if (State
->IntStatus
== FAST486_INT_DELAYED
)
126 /* Restore the old state */
127 State
->IntStatus
= FAST486_INT_EXECUTE
;
130 while ((Command
== FAST486_CONTINUE
)
131 || (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0)
132 || (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0)
133 || (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
));
136 /* DEFAULT CALLBACKS **********************************************************/
140 Fast486MemReadCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
142 UNREFERENCED_PARAMETER(State
);
144 RtlMoveMemory(Buffer
, (PVOID
)Address
, Size
);
149 Fast486MemWriteCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
151 UNREFERENCED_PARAMETER(State
);
153 RtlMoveMemory((PVOID
)Address
, Buffer
, Size
);
158 Fast486IoReadCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
160 UNREFERENCED_PARAMETER(State
);
161 UNREFERENCED_PARAMETER(Port
);
162 UNREFERENCED_PARAMETER(Buffer
);
163 UNREFERENCED_PARAMETER(DataCount
);
164 UNREFERENCED_PARAMETER(DataSize
);
169 Fast486IoWriteCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
171 UNREFERENCED_PARAMETER(State
);
172 UNREFERENCED_PARAMETER(Port
);
173 UNREFERENCED_PARAMETER(Buffer
);
174 UNREFERENCED_PARAMETER(DataCount
);
175 UNREFERENCED_PARAMETER(DataSize
);
180 Fast486IdleCallback(PFAST486_STATE State
)
182 UNREFERENCED_PARAMETER(State
);
187 Fast486BopCallback(PFAST486_STATE State
, UCHAR BopCode
)
189 UNREFERENCED_PARAMETER(State
);
190 UNREFERENCED_PARAMETER(BopCode
);
195 Fast486IntAckCallback(PFAST486_STATE State
)
197 UNREFERENCED_PARAMETER(State
);
199 /* Return something... */
203 /* PUBLIC FUNCTIONS ***********************************************************/
207 Fast486Initialize(PFAST486_STATE State
,
208 FAST486_MEM_READ_PROC MemReadCallback
,
209 FAST486_MEM_WRITE_PROC MemWriteCallback
,
210 FAST486_IO_READ_PROC IoReadCallback
,
211 FAST486_IO_WRITE_PROC IoWriteCallback
,
212 FAST486_IDLE_PROC IdleCallback
,
213 FAST486_BOP_PROC BopCallback
,
214 FAST486_INT_ACK_PROC IntAckCallback
,
217 /* Set the callbacks (or use default ones if some are NULL) */
218 State
->MemReadCallback
= (MemReadCallback
? MemReadCallback
: Fast486MemReadCallback
);
219 State
->MemWriteCallback
= (MemWriteCallback
? MemWriteCallback
: Fast486MemWriteCallback
);
220 State
->IoReadCallback
= (IoReadCallback
? IoReadCallback
: Fast486IoReadCallback
);
221 State
->IoWriteCallback
= (IoWriteCallback
? IoWriteCallback
: Fast486IoWriteCallback
);
222 State
->IdleCallback
= (IdleCallback
? IdleCallback
: Fast486IdleCallback
);
223 State
->BopCallback
= (BopCallback
? BopCallback
: Fast486BopCallback
);
224 State
->IntAckCallback
= (IntAckCallback
? IntAckCallback
: Fast486IntAckCallback
);
226 /* Set the TLB (if given) */
235 Fast486Reset(PFAST486_STATE State
)
239 FAST486_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
240 FAST486_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
241 FAST486_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
242 FAST486_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
243 FAST486_IDLE_PROC IdleCallback
= State
->IdleCallback
;
244 FAST486_BOP_PROC BopCallback
= State
->BopCallback
;
245 FAST486_INT_ACK_PROC IntAckCallback
= State
->IntAckCallback
;
246 PULONG Tlb
= State
->Tlb
;
248 /* Clear the entire structure */
249 RtlZeroMemory(State
, sizeof(*State
));
251 /* Initialize the registers */
252 State
->Flags
.AlwaysSet
= 1;
253 State
->InstPtr
.LowWord
= 0xFFF0;
255 /* Set the CPL to 0 */
258 /* Initialize segments */
259 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
261 State
->SegmentRegs
[i
].Selector
= 0;
262 State
->SegmentRegs
[i
].Base
= 0;
263 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
264 State
->SegmentRegs
[i
].Present
= TRUE
;
265 State
->SegmentRegs
[i
].ReadWrite
= TRUE
;
266 State
->SegmentRegs
[i
].Executable
= FALSE
;
267 State
->SegmentRegs
[i
].DirConf
= FALSE
;
268 State
->SegmentRegs
[i
].SystemType
= 1; // Segment descriptor
269 State
->SegmentRegs
[i
].Dpl
= 0;
270 State
->SegmentRegs
[i
].Size
= FALSE
; // 16-bit
273 /* Initialize the code segment */
274 State
->SegmentRegs
[FAST486_REG_CS
].Executable
= TRUE
;
275 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
276 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
278 /* Initialize the IDT */
279 State
->Idtr
.Size
= 0x3FF;
280 State
->Idtr
.Address
= 0;
282 #ifndef FAST486_NO_FPU
284 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
287 /* Restore the callbacks and TLB */
288 State
->MemReadCallback
= MemReadCallback
;
289 State
->MemWriteCallback
= MemWriteCallback
;
290 State
->IoReadCallback
= IoReadCallback
;
291 State
->IoWriteCallback
= IoWriteCallback
;
292 State
->IdleCallback
= IdleCallback
;
293 State
->BopCallback
= BopCallback
;
294 State
->IntAckCallback
= IntAckCallback
;
300 Fast486DumpState(PFAST486_STATE State
)
302 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
303 (State
->ControlRegisters
[0] & FAST486_CR0_PE
) ? "protected" : "real",
304 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
305 State
->InstPtr
.Long
);
306 DPRINT1("\nGeneral purpose registers:\n"
307 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
308 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
309 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
310 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
311 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
312 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
313 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
314 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
315 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
316 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
317 DPRINT1("\nSegment registers:\n"
318 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
319 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
320 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
321 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
322 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
323 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
324 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
325 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
326 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
327 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
328 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
329 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
330 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
331 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
332 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
333 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
334 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
335 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
336 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
337 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
338 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
339 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
340 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
341 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
342 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
343 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
344 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
345 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
346 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
347 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
348 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
350 State
->Flags
.Cf
? "CF" : "cf",
351 State
->Flags
.Pf
? "PF" : "pf",
352 State
->Flags
.Af
? "AF" : "af",
353 State
->Flags
.Zf
? "ZF" : "zf",
354 State
->Flags
.Sf
? "SF" : "sf",
355 State
->Flags
.Tf
? "TF" : "tf",
356 State
->Flags
.If
? "IF" : "if",
357 State
->Flags
.Df
? "DF" : "df",
358 State
->Flags
.Of
? "OF" : "of",
359 State
->Flags
.Nt
? "NT" : "nt",
360 State
->Flags
.Rf
? "RF" : "rf",
361 State
->Flags
.Vm
? "VM" : "vm",
363 DPRINT1("\nControl Registers:\n"
364 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
365 State
->ControlRegisters
[FAST486_REG_CR0
],
366 State
->ControlRegisters
[FAST486_REG_CR2
],
367 State
->ControlRegisters
[FAST486_REG_CR3
]);
368 DPRINT1("\nDebug Registers:\n"
369 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
370 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
371 State
->DebugRegisters
[FAST486_REG_DR0
],
372 State
->DebugRegisters
[FAST486_REG_DR1
],
373 State
->DebugRegisters
[FAST486_REG_DR2
],
374 State
->DebugRegisters
[FAST486_REG_DR3
],
375 State
->DebugRegisters
[FAST486_REG_DR4
],
376 State
->DebugRegisters
[FAST486_REG_DR5
]);
381 Fast486Continue(PFAST486_STATE State
)
383 /* Call the internal function */
384 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
389 Fast486StepInto(PFAST486_STATE State
)
391 /* Call the internal function */
392 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
397 Fast486StepOver(PFAST486_STATE State
)
399 /* Call the internal function */
400 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
405 Fast486StepOut(PFAST486_STATE State
)
407 /* Call the internal function */
408 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);
413 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
415 /* Set the interrupt status and the number */
416 State
->IntStatus
= FAST486_INT_EXECUTE
;
417 State
->PendingIntNum
= Number
;
422 Fast486InterruptSignal(PFAST486_STATE State
)
424 /* Set the interrupt status */
425 State
->IntStatus
= FAST486_INT_SIGNAL
;
430 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
432 /* Load the new CS */
433 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
435 /* An exception occurred, let the handler execute instead */
440 State
->InstPtr
.Long
= Offset
;
445 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
447 /* Load the new SS */
448 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
450 /* An exception occurred, let the handler execute instead */
455 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
460 Fast486SetSegment(PFAST486_STATE State
,
461 FAST486_SEG_REGS Segment
,
464 /* Call the internal function */
465 Fast486LoadSegment(State
, Segment
, Selector
);