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
)) continue;
63 // TODO: Check for CALL/RET to update ProcedureCallCount.
65 if (Fast486OpcodeHandlers
[Opcode
] != NULL
)
67 /* Call the opcode handler */
68 Fast486OpcodeHandlers
[Opcode
](State
, Opcode
);
72 /* This is not a valid opcode */
73 Fast486Exception(State
, FAST486_EXCEPTION_UD
);
76 if (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
)
78 /* This is a prefix, go to the next instruction immediately */
82 /* A non-prefix opcode has been executed, reset the prefix flags */
83 State
->PrefixFlags
= 0;
86 * Check if there is an interrupt to execute, or a hardware interrupt signal
87 * while interrupts are enabled.
89 if (State
->IntStatus
== FAST486_INT_EXECUTE
)
91 FAST486_IDT_ENTRY IdtEntry
;
93 /* Get the interrupt vector */
94 if (Fast486GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
96 /* Perform the interrupt */
97 Fast486InterruptInternal(State
,
99 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
103 /* Clear the interrupt status */
104 State
->IntStatus
= FAST486_INT_NONE
;
106 else if (State
->Flags
.If
107 && (State
->IntAckCallback
!= NULL
)
108 && (State
->IntStatus
== FAST486_INT_SIGNAL
))
110 /* Acknowledge the interrupt to get the number */
111 State
->PendingIntNum
= State
->IntAckCallback(State
);
113 /* Set the interrupt status to execute on the next instruction */
114 State
->IntStatus
= FAST486_INT_EXECUTE
;
117 while ((Command
== FAST486_CONTINUE
)
118 || (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0)
119 || (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0)
120 || (Fast486OpcodeHandlers
[Opcode
] == Fast486OpcodePrefix
));
123 /* DEFAULT CALLBACKS **********************************************************/
127 Fast486MemReadCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
129 UNREFERENCED_PARAMETER(State
);
131 RtlMoveMemory(Buffer
, (PVOID
)Address
, Size
);
136 Fast486MemWriteCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
138 UNREFERENCED_PARAMETER(State
);
140 RtlMoveMemory((PVOID
)Address
, Buffer
, Size
);
145 Fast486IoReadCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
147 UNREFERENCED_PARAMETER(State
);
148 UNREFERENCED_PARAMETER(Port
);
149 UNREFERENCED_PARAMETER(Buffer
);
150 UNREFERENCED_PARAMETER(DataCount
);
151 UNREFERENCED_PARAMETER(DataSize
);
156 Fast486IoWriteCallback(PFAST486_STATE State
, ULONG Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
158 UNREFERENCED_PARAMETER(State
);
159 UNREFERENCED_PARAMETER(Port
);
160 UNREFERENCED_PARAMETER(Buffer
);
161 UNREFERENCED_PARAMETER(DataCount
);
162 UNREFERENCED_PARAMETER(DataSize
);
167 Fast486IdleCallback(PFAST486_STATE State
)
169 UNREFERENCED_PARAMETER(State
);
174 Fast486BopCallback(PFAST486_STATE State
, UCHAR BopCode
)
176 UNREFERENCED_PARAMETER(State
);
177 UNREFERENCED_PARAMETER(BopCode
);
182 Fast486IntAckCallback(PFAST486_STATE State
)
184 UNREFERENCED_PARAMETER(State
);
186 /* Return something... */
190 /* PUBLIC FUNCTIONS ***********************************************************/
194 Fast486Initialize(PFAST486_STATE State
,
195 FAST486_MEM_READ_PROC MemReadCallback
,
196 FAST486_MEM_WRITE_PROC MemWriteCallback
,
197 FAST486_IO_READ_PROC IoReadCallback
,
198 FAST486_IO_WRITE_PROC IoWriteCallback
,
199 FAST486_IDLE_PROC IdleCallback
,
200 FAST486_BOP_PROC BopCallback
,
201 FAST486_INT_ACK_PROC IntAckCallback
,
204 /* Set the callbacks (or use default ones if some are NULL) */
205 State
->MemReadCallback
= (MemReadCallback
? MemReadCallback
: Fast486MemReadCallback
);
206 State
->MemWriteCallback
= (MemWriteCallback
? MemWriteCallback
: Fast486MemWriteCallback
);
207 State
->IoReadCallback
= (IoReadCallback
? IoReadCallback
: Fast486IoReadCallback
);
208 State
->IoWriteCallback
= (IoWriteCallback
? IoWriteCallback
: Fast486IoWriteCallback
);
209 State
->IdleCallback
= (IdleCallback
? IdleCallback
: Fast486IdleCallback
);
210 State
->BopCallback
= (BopCallback
? BopCallback
: Fast486BopCallback
);
211 State
->IntAckCallback
= (IntAckCallback
? IntAckCallback
: Fast486IntAckCallback
);
213 /* Set the TLB (if given) */
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
;
233 PULONG Tlb
= State
->Tlb
;
235 /* Clear the entire structure */
236 RtlZeroMemory(State
, sizeof(*State
));
238 /* Initialize the registers */
239 State
->Flags
.AlwaysSet
= 1;
240 State
->InstPtr
.LowWord
= 0xFFF0;
242 /* Set the CPL to 0 */
245 /* Initialize segments */
246 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
248 State
->SegmentRegs
[i
].Selector
= 0;
249 State
->SegmentRegs
[i
].Base
= 0;
250 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
251 State
->SegmentRegs
[i
].Present
= TRUE
;
252 State
->SegmentRegs
[i
].ReadWrite
= TRUE
;
253 State
->SegmentRegs
[i
].Executable
= FALSE
;
254 State
->SegmentRegs
[i
].DirConf
= FALSE
;
255 State
->SegmentRegs
[i
].SystemType
= 1; // Segment descriptor
256 State
->SegmentRegs
[i
].Dpl
= 0;
257 State
->SegmentRegs
[i
].Size
= FALSE
; // 16-bit
260 /* Initialize the code segment */
261 State
->SegmentRegs
[FAST486_REG_CS
].Executable
= TRUE
;
262 State
->SegmentRegs
[FAST486_REG_CS
].Selector
= 0xF000;
263 State
->SegmentRegs
[FAST486_REG_CS
].Base
= 0xFFFF0000;
265 /* Initialize the IDT */
266 State
->Idtr
.Size
= 0x3FF;
267 State
->Idtr
.Address
= 0;
270 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
272 /* Restore the callbacks and TLB */
273 State
->MemReadCallback
= MemReadCallback
;
274 State
->MemWriteCallback
= MemWriteCallback
;
275 State
->IoReadCallback
= IoReadCallback
;
276 State
->IoWriteCallback
= IoWriteCallback
;
277 State
->IdleCallback
= IdleCallback
;
278 State
->BopCallback
= BopCallback
;
279 State
->IntAckCallback
= IntAckCallback
;
285 Fast486DumpState(PFAST486_STATE State
)
287 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
288 (State
->ControlRegisters
[0] & FAST486_CR0_PE
) ? "protected" : "real",
289 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
290 State
->InstPtr
.Long
);
291 DPRINT1("\nGeneral purpose registers:\n"
292 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
293 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
294 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
295 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
296 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
297 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
298 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
299 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
300 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
301 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
302 DPRINT1("\nSegment registers:\n"
303 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
304 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
305 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
306 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
307 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
308 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
309 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
310 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
311 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
312 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
313 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
314 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
315 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
316 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
317 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
318 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
319 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
320 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
321 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
322 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
323 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
324 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
325 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
326 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
327 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
328 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
329 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
330 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
331 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
332 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
333 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
335 State
->Flags
.Cf
? "CF" : "cf",
336 State
->Flags
.Pf
? "PF" : "pf",
337 State
->Flags
.Af
? "AF" : "af",
338 State
->Flags
.Zf
? "ZF" : "zf",
339 State
->Flags
.Sf
? "SF" : "sf",
340 State
->Flags
.Tf
? "TF" : "tf",
341 State
->Flags
.If
? "IF" : "if",
342 State
->Flags
.Df
? "DF" : "df",
343 State
->Flags
.Of
? "OF" : "of",
344 State
->Flags
.Nt
? "NT" : "nt",
345 State
->Flags
.Rf
? "RF" : "rf",
346 State
->Flags
.Vm
? "VM" : "vm",
347 State
->Flags
.Ac
? "AC" : "ac",
348 State
->Flags
.Vif
? "VIF" : "vif",
349 State
->Flags
.Vip
? "VIP" : "vip",
351 DPRINT1("\nControl Registers:\n"
352 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
353 State
->ControlRegisters
[FAST486_REG_CR0
],
354 State
->ControlRegisters
[FAST486_REG_CR2
],
355 State
->ControlRegisters
[FAST486_REG_CR3
]);
356 DPRINT1("\nDebug Registers:\n"
357 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
358 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
359 State
->DebugRegisters
[FAST486_REG_DR0
],
360 State
->DebugRegisters
[FAST486_REG_DR1
],
361 State
->DebugRegisters
[FAST486_REG_DR2
],
362 State
->DebugRegisters
[FAST486_REG_DR3
],
363 State
->DebugRegisters
[FAST486_REG_DR4
],
364 State
->DebugRegisters
[FAST486_REG_DR5
]);
369 Fast486Continue(PFAST486_STATE State
)
371 /* Call the internal function */
372 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
377 Fast486StepInto(PFAST486_STATE State
)
379 /* Call the internal function */
380 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
385 Fast486StepOver(PFAST486_STATE State
)
387 /* Call the internal function */
388 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
393 Fast486StepOut(PFAST486_STATE State
)
395 /* Call the internal function */
396 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);
401 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
403 /* Set the interrupt status and the number */
404 State
->IntStatus
= FAST486_INT_EXECUTE
;
405 State
->PendingIntNum
= Number
;
410 Fast486InterruptSignal(PFAST486_STATE State
)
412 /* Set the interrupt status */
413 State
->IntStatus
= FAST486_INT_SIGNAL
;
418 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
420 /* Load the new CS */
421 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
423 /* An exception occurred, let the handler execute instead */
428 State
->InstPtr
.Long
= Offset
;
433 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
435 /* Load the new SS */
436 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
438 /* An exception occurred, let the handler execute instead */
443 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
448 Fast486SetSegment(PFAST486_STATE State
,
449 FAST486_SEG_REGS Segment
,
452 /* Call the internal function */
453 Fast486LoadSegment(State
, Segment
, Selector
);