2 * Soft386 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 *******************************************************************/
24 // #define WIN32_NO_STATUS
25 // #define _INC_WINDOWS
35 /* DEFINES ********************************************************************/
45 /* PRIVATE FUNCTIONS **********************************************************/
51 Soft386ExecutionControl(PSOFT386_STATE State
, INT Command
)
54 INT ProcedureCallCount
= 0;
56 /* Main execution loop */
59 /* Check if this is a new instruction */
60 if (State
->PrefixFlags
== 0)
62 State
->SavedInstPtr
= State
->InstPtr
;
64 /* Check if interrupts are enabled and there is an interrupt pending */
65 if (State
->Flags
.If
&& State
->HardwareInt
)
67 SOFT386_IDT_ENTRY IdtEntry
;
69 /* Get the interrupt vector */
70 if (Soft386GetIntVector(State
, State
->PendingIntNum
, &IdtEntry
))
72 /* Perform the interrupt */
73 Soft386InterruptInternal(State
,
75 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
79 /* Clear the interrupt pending flag */
80 State
->HardwareInt
= FALSE
;
84 /* Perform an instruction fetch */
85 if (!Soft386FetchByte(State
, &Opcode
)) continue;
87 // TODO: Check for CALL/RET to update ProcedureCallCount.
89 if (Soft386OpcodeHandlers
[Opcode
] != NULL
)
91 /* Call the opcode handler */
92 Soft386OpcodeHandlers
[Opcode
](State
, Opcode
);
96 /* This is not a valid opcode */
97 Soft386Exception(State
, SOFT386_EXCEPTION_UD
);
100 if (Soft386OpcodeHandlers
[Opcode
] != Soft386OpcodePrefix
)
102 /* A non-prefix opcode has been executed, reset the prefix flags */
103 State
->PrefixFlags
= 0;
107 /* This is a prefix, go to the next instruction immediately */
111 /* Increment the time stamp counter */
112 State
->TimeStampCounter
++;
114 while ((Command
== SOFT386_CONTINUE
)
115 || (Command
== SOFT386_STEP_OVER
&& ProcedureCallCount
> 0)
116 || (Command
== SOFT386_STEP_OUT
&& ProcedureCallCount
>= 0)
117 || (Soft386OpcodeHandlers
[Opcode
] == Soft386OpcodePrefix
));
120 /* PUBLIC FUNCTIONS ***********************************************************/
124 Soft386Continue(PSOFT386_STATE State
)
126 /* Call the internal function */
127 Soft386ExecutionControl(State
, SOFT386_CONTINUE
);
132 Soft386StepInto(PSOFT386_STATE State
)
134 /* Call the internal function */
135 Soft386ExecutionControl(State
, SOFT386_STEP_INTO
);
140 Soft386StepOver(PSOFT386_STATE State
)
142 /* Call the internal function */
143 Soft386ExecutionControl(State
, SOFT386_STEP_OVER
);
148 Soft386StepOut(PSOFT386_STATE State
)
150 /* Call the internal function */
151 Soft386ExecutionControl(State
, SOFT386_STEP_OUT
);
156 Soft386DumpState(PSOFT386_STATE State
)
158 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n"
159 "Time Stamp Counter = %016X\n",
160 (State
->ControlRegisters
[0] & SOFT386_CR0_PE
) ? "protected" : "real",
161 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
163 State
->TimeStampCounter
);
164 DPRINT1("\nGeneral purpose registers:\n"
165 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
166 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
167 State
->GeneralRegs
[SOFT386_REG_EAX
].Long
,
168 State
->GeneralRegs
[SOFT386_REG_ECX
].Long
,
169 State
->GeneralRegs
[SOFT386_REG_EDX
].Long
,
170 State
->GeneralRegs
[SOFT386_REG_EBX
].Long
,
171 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
,
172 State
->GeneralRegs
[SOFT386_REG_EBP
].Long
,
173 State
->GeneralRegs
[SOFT386_REG_ESI
].Long
,
174 State
->GeneralRegs
[SOFT386_REG_EDI
].Long
);
175 DPRINT1("\nSegment registers:\n"
176 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
177 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
178 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
179 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
180 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
181 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
182 State
->SegmentRegs
[SOFT386_REG_ES
].Selector
,
183 State
->SegmentRegs
[SOFT386_REG_ES
].Base
,
184 State
->SegmentRegs
[SOFT386_REG_ES
].Limit
,
185 State
->SegmentRegs
[SOFT386_REG_ES
].Dpl
,
186 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
187 State
->SegmentRegs
[SOFT386_REG_CS
].Base
,
188 State
->SegmentRegs
[SOFT386_REG_CS
].Limit
,
189 State
->SegmentRegs
[SOFT386_REG_CS
].Dpl
,
190 State
->SegmentRegs
[SOFT386_REG_SS
].Selector
,
191 State
->SegmentRegs
[SOFT386_REG_SS
].Base
,
192 State
->SegmentRegs
[SOFT386_REG_SS
].Limit
,
193 State
->SegmentRegs
[SOFT386_REG_SS
].Dpl
,
194 State
->SegmentRegs
[SOFT386_REG_DS
].Selector
,
195 State
->SegmentRegs
[SOFT386_REG_DS
].Base
,
196 State
->SegmentRegs
[SOFT386_REG_DS
].Limit
,
197 State
->SegmentRegs
[SOFT386_REG_DS
].Dpl
,
198 State
->SegmentRegs
[SOFT386_REG_FS
].Selector
,
199 State
->SegmentRegs
[SOFT386_REG_FS
].Base
,
200 State
->SegmentRegs
[SOFT386_REG_FS
].Limit
,
201 State
->SegmentRegs
[SOFT386_REG_FS
].Dpl
,
202 State
->SegmentRegs
[SOFT386_REG_GS
].Selector
,
203 State
->SegmentRegs
[SOFT386_REG_GS
].Base
,
204 State
->SegmentRegs
[SOFT386_REG_GS
].Limit
,
205 State
->SegmentRegs
[SOFT386_REG_GS
].Dpl
);
206 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
208 State
->Flags
.Cf
? "CF" : "cf",
209 State
->Flags
.Pf
? "PF" : "pf",
210 State
->Flags
.Af
? "AF" : "af",
211 State
->Flags
.Zf
? "ZF" : "zf",
212 State
->Flags
.Sf
? "SF" : "sf",
213 State
->Flags
.Tf
? "TF" : "tf",
214 State
->Flags
.If
? "IF" : "if",
215 State
->Flags
.Df
? "DF" : "df",
216 State
->Flags
.Of
? "OF" : "of",
217 State
->Flags
.Nt
? "NT" : "nt",
218 State
->Flags
.Rf
? "RF" : "rf",
219 State
->Flags
.Vm
? "VM" : "vm",
220 State
->Flags
.Ac
? "AC" : "ac",
221 State
->Flags
.Vif
? "VIF" : "vif",
222 State
->Flags
.Vip
? "VIP" : "vip",
224 DPRINT1("\nControl Registers:\n"
225 "CR0 = %08X\tCR1 = %08X\tCR2 = %08X\tCR3 = %08X\n"
226 "CR4 = %08X\tCR5 = %08X\tCR6 = %08X\tCR7 = %08X\n",
227 State
->ControlRegisters
[SOFT386_REG_CR0
],
228 State
->ControlRegisters
[SOFT386_REG_CR1
],
229 State
->ControlRegisters
[SOFT386_REG_CR2
],
230 State
->ControlRegisters
[SOFT386_REG_CR3
],
231 State
->ControlRegisters
[SOFT386_REG_CR4
],
232 State
->ControlRegisters
[SOFT386_REG_CR5
],
233 State
->ControlRegisters
[SOFT386_REG_CR6
],
234 State
->ControlRegisters
[SOFT386_REG_CR7
]);
235 DPRINT1("\nDebug Registers:\n"
236 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\tDR3 = %08X\n"
237 "DR4 = %08X\tDR5 = %08X\tDR6 = %08X\tDR7 = %08X\n",
238 State
->DebugRegisters
[SOFT386_REG_DR0
],
239 State
->DebugRegisters
[SOFT386_REG_DR1
],
240 State
->DebugRegisters
[SOFT386_REG_DR2
],
241 State
->DebugRegisters
[SOFT386_REG_DR3
],
242 State
->DebugRegisters
[SOFT386_REG_DR4
],
243 State
->DebugRegisters
[SOFT386_REG_DR5
],
244 State
->DebugRegisters
[SOFT386_REG_DR6
],
245 State
->DebugRegisters
[SOFT386_REG_DR7
]);
250 Soft386Reset(PSOFT386_STATE State
)
253 SOFT386_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
254 SOFT386_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
255 SOFT386_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
256 SOFT386_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
257 SOFT386_IDLE_PROC IdleCallback
= State
->IdleCallback
;
258 SOFT386_BOP_PROC BopCallback
= State
->BopCallback
;
260 /* Clear the entire structure */
261 RtlZeroMemory(State
, sizeof(*State
));
263 /* Initialize the registers */
264 State
->Flags
.AlwaysSet
= 1;
265 State
->InstPtr
.LowWord
= 0xFFF0;
267 /* Initialize segments */
268 for (i
= 0; i
< SOFT386_NUM_SEG_REGS
; i
++)
270 /* Set the selector, base and limit, other values don't apply in real mode */
271 State
->SegmentRegs
[i
].Selector
= 0;
272 State
->SegmentRegs
[i
].Base
= 0;
273 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
276 /* Initialize the code segment */
277 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
= 0xF000;
278 State
->SegmentRegs
[SOFT386_REG_CS
].Base
= 0xFFFF0000;
280 /* Initialize the IDT */
281 State
->Idtr
.Size
= 0x3FF;
282 State
->Idtr
.Address
= 0;
285 State
->ControlRegisters
[SOFT386_REG_CR0
] |= SOFT386_CR0_ET
;
287 /* Restore the callbacks */
288 State
->MemReadCallback
= MemReadCallback
;
289 State
->MemWriteCallback
= MemWriteCallback
;
290 State
->IoReadCallback
= IoReadCallback
;
291 State
->IoWriteCallback
= IoWriteCallback
;
292 State
->IdleCallback
= IdleCallback
;
293 State
->BopCallback
= BopCallback
;
298 Soft386Interrupt(PSOFT386_STATE State
, UCHAR Number
)
300 /* Set the hardware interrupt flag */
301 State
->HardwareInt
= TRUE
;
302 State
->PendingIntNum
= Number
;
307 Soft386ExecuteAt(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
309 /* Load the new CS */
310 if (!Soft386LoadSegment(State
, SOFT386_REG_CS
, Segment
))
312 /* An exception occurred, let the handler execute instead */
317 State
->InstPtr
.Long
= Offset
;
322 Soft386SetStack(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
324 /* Load the new SS */
325 if (!Soft386LoadSegment(State
, SOFT386_REG_SS
, Segment
))
327 /* An exception occurred, let the handler execute instead */
332 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
= Offset
;
337 Soft386SetSegment(PSOFT386_STATE State
,
338 SOFT386_SEG_REGS Segment
,
341 /* Call the internal function */
342 Soft386LoadSegment(State
, Segment
, Selector
);