2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2014 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 *******************************************************************/
34 /* DEFINES ********************************************************************/
44 /* PRIVATE FUNCTIONS **********************************************************/
49 Fast486ExecutionControl(PFAST486_STATE State
, FAST486_EXEC_CMD Command
)
52 FAST486_OPCODE_HANDLER_PROC CurrentHandler
;
53 INT ProcedureCallCount
= 0;
55 /* Main execution loop */
59 /* Check if this is a new instruction */
60 if (State
->PrefixFlags
== 0) State
->SavedInstPtr
= State
->InstPtr
;
62 /* Perform an instruction fetch */
63 if (!Fast486FetchByte(State
, &Opcode
))
65 /* Exception occurred */
66 State
->PrefixFlags
= 0;
70 // TODO: Check for CALL/RET to update ProcedureCallCount.
72 /* Call the opcode handler */
73 CurrentHandler
= Fast486OpcodeHandlers
[Opcode
];
74 CurrentHandler(State
, Opcode
);
76 /* If this is a prefix, go to the next instruction immediately */
77 if (CurrentHandler
== Fast486OpcodePrefix
) goto NextInst
;
79 /* A non-prefix opcode has been executed, reset the prefix flags */
80 State
->PrefixFlags
= 0;
83 * Check if there is an interrupt to execute, or a hardware interrupt signal
84 * while interrupts are enabled.
88 /* Perform the interrupt */
89 Fast486PerformInterrupt(State
, 0x01);
92 * Flags and TF are pushed on stack so we can reset TF now,
93 * to not break into the INT 0x01 handler.
94 * After the INT 0x01 handler returns, the flags and therefore
95 * TF are popped back off the stack and restored, so TF will be
96 * automatically reset to its previous state.
98 State
->Flags
.Tf
= FALSE
;
100 else if (State
->IntStatus
== FAST486_INT_EXECUTE
)
102 /* Perform the interrupt */
103 Fast486PerformInterrupt(State
, State
->PendingIntNum
);
105 /* Clear the interrupt status */
106 State
->IntStatus
= FAST486_INT_NONE
;
108 else if (State
->Flags
.If
&& (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
;
116 else if (State
->IntStatus
== FAST486_INT_DELAYED
)
118 /* Restore the old state */
119 State
->IntStatus
= FAST486_INT_EXECUTE
;
122 while ((Command
== FAST486_CONTINUE
) ||
123 (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0) ||
124 (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0));
129 Fast486ExecutionControl(PFAST486_STATE State
, FAST486_EXEC_CMD Command
);
132 /* PUBLIC FUNCTIONS ***********************************************************/
136 Fast486DumpState(PFAST486_STATE State
)
138 DbgPrint("\nFast486DumpState -->\n");
139 DbgPrint("\nCPU currently executing in %s mode at %04X:%08X\n",
140 (State
->ControlRegisters
[0] & FAST486_CR0_PE
) ? "protected" : "real",
141 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
142 State
->InstPtr
.Long
);
143 DbgPrint("\nGeneral purpose registers:\n"
144 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
145 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
146 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
147 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
148 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
149 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
150 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
151 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
152 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
153 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
154 DbgPrint("\nSegment registers:\n"
155 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
156 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
157 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
158 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
159 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
160 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
161 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
162 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
163 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
164 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
165 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
166 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
167 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
168 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
169 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
170 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
171 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
172 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
173 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
174 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
175 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
176 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
177 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
178 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
179 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
180 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
181 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
182 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
183 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
184 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
185 DbgPrint("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
187 State
->Flags
.Cf
? "CF" : "cf",
188 State
->Flags
.Pf
? "PF" : "pf",
189 State
->Flags
.Af
? "AF" : "af",
190 State
->Flags
.Zf
? "ZF" : "zf",
191 State
->Flags
.Sf
? "SF" : "sf",
192 State
->Flags
.Tf
? "TF" : "tf",
193 State
->Flags
.If
? "IF" : "if",
194 State
->Flags
.Df
? "DF" : "df",
195 State
->Flags
.Of
? "OF" : "of",
196 State
->Flags
.Nt
? "NT" : "nt",
197 State
->Flags
.Rf
? "RF" : "rf",
198 State
->Flags
.Vm
? "VM" : "vm",
199 State
->Flags
.Ac
? "AC" : "ac",
201 DbgPrint("\nControl Registers:\n"
202 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
203 State
->ControlRegisters
[FAST486_REG_CR0
],
204 State
->ControlRegisters
[FAST486_REG_CR2
],
205 State
->ControlRegisters
[FAST486_REG_CR3
]);
206 DbgPrint("\nDebug Registers:\n"
207 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
208 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
209 State
->DebugRegisters
[FAST486_REG_DR0
],
210 State
->DebugRegisters
[FAST486_REG_DR1
],
211 State
->DebugRegisters
[FAST486_REG_DR2
],
212 State
->DebugRegisters
[FAST486_REG_DR3
],
213 State
->DebugRegisters
[FAST486_REG_DR4
],
214 State
->DebugRegisters
[FAST486_REG_DR5
]);
216 #ifndef FAST486_NO_FPU
217 DbgPrint("\nFPU Registers:\n"
218 "ST0 = %04X%016llX\tST1 = %04X%016llX\n"
219 "ST2 = %04X%016llX\tST3 = %04X%016llX\n"
220 "ST4 = %04X%016llX\tST5 = %04X%016llX\n"
221 "ST6 = %04X%016llX\tST7 = %04X%016llX\n"
222 "Status: %04X\tControl: %04X\tTag: %04X\n",
223 FPU_ST(0).Exponent
| ((USHORT
)FPU_ST(0).Sign
<< 15),
225 FPU_ST(1).Exponent
| ((USHORT
)FPU_ST(1).Sign
<< 15),
227 FPU_ST(2).Exponent
| ((USHORT
)FPU_ST(2).Sign
<< 15),
229 FPU_ST(3).Exponent
| ((USHORT
)FPU_ST(3).Sign
<< 15),
231 FPU_ST(4).Exponent
| ((USHORT
)FPU_ST(4).Sign
<< 15),
233 FPU_ST(5).Exponent
| ((USHORT
)FPU_ST(5).Sign
<< 15),
235 FPU_ST(6).Exponent
| ((USHORT
)FPU_ST(6).Sign
<< 15),
237 FPU_ST(7).Exponent
| ((USHORT
)FPU_ST(7).Sign
<< 15),
244 DbgPrint("\n<-- Fast486DumpState\n\n");
249 Fast486Continue(PFAST486_STATE State
)
251 /* Call the internal function */
252 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
257 Fast486StepInto(PFAST486_STATE State
)
259 /* Call the internal function */
260 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
265 Fast486StepOver(PFAST486_STATE State
)
267 /* Call the internal function */
268 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
273 Fast486StepOut(PFAST486_STATE State
)
275 /* Call the internal function */
276 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);