2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2015 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;
56 /* Main execution loop */
59 Trap
= State
->Flags
.Tf
;
64 /* Check if this is a new instruction */
65 if (State
->PrefixFlags
== 0)
67 State
->SavedInstPtr
= State
->InstPtr
;
68 State
->SavedStackPtr
= State
->GeneralRegs
[FAST486_REG_ESP
];
71 /* Perform an instruction fetch */
72 if (!Fast486FetchByte(State
, &Opcode
))
74 /* Exception occurred */
75 State
->PrefixFlags
= 0;
79 // TODO: Check for CALL/RET to update ProcedureCallCount.
81 /* Call the opcode handler */
82 CurrentHandler
= Fast486OpcodeHandlers
[Opcode
];
83 CurrentHandler(State
, Opcode
);
85 /* If this is a prefix, go to the next instruction immediately */
86 if (CurrentHandler
== Fast486OpcodePrefix
) goto NextInst
;
88 /* A non-prefix opcode has been executed, reset the prefix flags */
89 State
->PrefixFlags
= 0;
93 * Check if there is an interrupt to execute, or a hardware interrupt signal
94 * while interrupts are enabled.
96 if (State
->DoNotInterrupt
)
98 /* Clear the interrupt delay flag */
99 State
->DoNotInterrupt
= FALSE
;
101 else if (Trap
&& !State
->Halted
)
103 /* Perform the interrupt */
104 Fast486PerformInterrupt(State
, FAST486_EXCEPTION_DB
);
106 else if (State
->Flags
.If
&& State
->IntSignaled
)
108 /* No longer halted */
109 State
->Halted
= FALSE
;
111 /* Acknowledge the interrupt and perform it */
112 Fast486PerformInterrupt(State
, State
->IntAckCallback(State
));
114 /* Clear the interrupt status */
115 State
->IntSignaled
= FALSE
;
118 while ((Command
== FAST486_CONTINUE
) ||
119 (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0) ||
120 (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0));
123 /* PUBLIC FUNCTIONS ***********************************************************/
127 Fast486DumpState(PFAST486_STATE State
)
129 DbgPrint("\nFast486DumpState -->\n");
130 DbgPrint("\nCPU currently executing in %s mode at %04X:%08X\n",
131 (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
) ? "protected" : "real",
132 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
133 State
->InstPtr
.Long
);
134 DbgPrint("\nGeneral purpose registers:\n"
135 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
136 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
137 State
->GeneralRegs
[FAST486_REG_EAX
].Long
,
138 State
->GeneralRegs
[FAST486_REG_ECX
].Long
,
139 State
->GeneralRegs
[FAST486_REG_EDX
].Long
,
140 State
->GeneralRegs
[FAST486_REG_EBX
].Long
,
141 State
->GeneralRegs
[FAST486_REG_ESP
].Long
,
142 State
->GeneralRegs
[FAST486_REG_EBP
].Long
,
143 State
->GeneralRegs
[FAST486_REG_ESI
].Long
,
144 State
->GeneralRegs
[FAST486_REG_EDI
].Long
);
145 DbgPrint("\nSegment registers:\n"
146 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
147 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
148 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
149 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
150 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
151 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
152 State
->SegmentRegs
[FAST486_REG_ES
].Selector
,
153 State
->SegmentRegs
[FAST486_REG_ES
].Base
,
154 State
->SegmentRegs
[FAST486_REG_ES
].Limit
,
155 State
->SegmentRegs
[FAST486_REG_ES
].Dpl
,
156 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
157 State
->SegmentRegs
[FAST486_REG_CS
].Base
,
158 State
->SegmentRegs
[FAST486_REG_CS
].Limit
,
159 State
->SegmentRegs
[FAST486_REG_CS
].Dpl
,
160 State
->SegmentRegs
[FAST486_REG_SS
].Selector
,
161 State
->SegmentRegs
[FAST486_REG_SS
].Base
,
162 State
->SegmentRegs
[FAST486_REG_SS
].Limit
,
163 State
->SegmentRegs
[FAST486_REG_SS
].Dpl
,
164 State
->SegmentRegs
[FAST486_REG_DS
].Selector
,
165 State
->SegmentRegs
[FAST486_REG_DS
].Base
,
166 State
->SegmentRegs
[FAST486_REG_DS
].Limit
,
167 State
->SegmentRegs
[FAST486_REG_DS
].Dpl
,
168 State
->SegmentRegs
[FAST486_REG_FS
].Selector
,
169 State
->SegmentRegs
[FAST486_REG_FS
].Base
,
170 State
->SegmentRegs
[FAST486_REG_FS
].Limit
,
171 State
->SegmentRegs
[FAST486_REG_FS
].Dpl
,
172 State
->SegmentRegs
[FAST486_REG_GS
].Selector
,
173 State
->SegmentRegs
[FAST486_REG_GS
].Base
,
174 State
->SegmentRegs
[FAST486_REG_GS
].Limit
,
175 State
->SegmentRegs
[FAST486_REG_GS
].Dpl
);
176 DbgPrint("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
178 State
->Flags
.Cf
? "CF" : "cf",
179 State
->Flags
.Pf
? "PF" : "pf",
180 State
->Flags
.Af
? "AF" : "af",
181 State
->Flags
.Zf
? "ZF" : "zf",
182 State
->Flags
.Sf
? "SF" : "sf",
183 State
->Flags
.Tf
? "TF" : "tf",
184 State
->Flags
.If
? "IF" : "if",
185 State
->Flags
.Df
? "DF" : "df",
186 State
->Flags
.Of
? "OF" : "of",
187 State
->Flags
.Nt
? "NT" : "nt",
188 State
->Flags
.Rf
? "RF" : "rf",
189 State
->Flags
.Vm
? "VM" : "vm",
190 State
->Flags
.Ac
? "AC" : "ac",
192 DbgPrint("\nControl Registers:\n"
193 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
194 State
->ControlRegisters
[FAST486_REG_CR0
],
195 State
->ControlRegisters
[FAST486_REG_CR2
],
196 State
->ControlRegisters
[FAST486_REG_CR3
]);
197 DbgPrint("\nDebug Registers:\n"
198 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
199 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
200 State
->DebugRegisters
[FAST486_REG_DR0
],
201 State
->DebugRegisters
[FAST486_REG_DR1
],
202 State
->DebugRegisters
[FAST486_REG_DR2
],
203 State
->DebugRegisters
[FAST486_REG_DR3
],
204 State
->DebugRegisters
[FAST486_REG_DR4
],
205 State
->DebugRegisters
[FAST486_REG_DR5
]);
207 #ifndef FAST486_NO_FPU
208 DbgPrint("\nFPU Registers:\n"
209 "ST0 = %04X%016llX\tST1 = %04X%016llX\n"
210 "ST2 = %04X%016llX\tST3 = %04X%016llX\n"
211 "ST4 = %04X%016llX\tST5 = %04X%016llX\n"
212 "ST6 = %04X%016llX\tST7 = %04X%016llX\n"
213 "Status: %04X\tControl: %04X\tTag: %04X\n",
214 FPU_ST(0).Exponent
| ((USHORT
)FPU_ST(0).Sign
<< 15),
216 FPU_ST(1).Exponent
| ((USHORT
)FPU_ST(1).Sign
<< 15),
218 FPU_ST(2).Exponent
| ((USHORT
)FPU_ST(2).Sign
<< 15),
220 FPU_ST(3).Exponent
| ((USHORT
)FPU_ST(3).Sign
<< 15),
222 FPU_ST(4).Exponent
| ((USHORT
)FPU_ST(4).Sign
<< 15),
224 FPU_ST(5).Exponent
| ((USHORT
)FPU_ST(5).Sign
<< 15),
226 FPU_ST(6).Exponent
| ((USHORT
)FPU_ST(6).Sign
<< 15),
228 FPU_ST(7).Exponent
| ((USHORT
)FPU_ST(7).Sign
<< 15),
235 DbgPrint("\n<-- Fast486DumpState\n\n");
240 Fast486Continue(PFAST486_STATE State
)
242 /* Call the internal function */
243 Fast486ExecutionControl(State
, FAST486_CONTINUE
);
248 Fast486StepInto(PFAST486_STATE State
)
250 /* Call the internal function */
251 Fast486ExecutionControl(State
, FAST486_STEP_INTO
);
256 Fast486StepOver(PFAST486_STATE State
)
258 /* Call the internal function */
259 Fast486ExecutionControl(State
, FAST486_STEP_OVER
);
264 Fast486StepOut(PFAST486_STATE State
)
266 /* Call the internal function */
267 Fast486ExecutionControl(State
, FAST486_STEP_OUT
);