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 /* If this is a new instruction, save the IP */
60 if (State
->PrefixFlags
== 0) State
->SavedInstPtr
= State
->InstPtr
;
62 /* Perform an instruction fetch */
63 if (!Soft386FetchByte(State
, &Opcode
)) continue;
65 // TODO: Check for CALL/RET to update ProcedureCallCount.
67 if (Soft386OpcodeHandlers
[Opcode
] != NULL
)
69 /* Call the opcode handler */
70 Soft386OpcodeHandlers
[Opcode
](State
, Opcode
);
74 /* This is not a valid opcode */
75 Soft386Exception(State
, SOFT386_EXCEPTION_UD
);
78 if (Soft386OpcodeHandlers
[Opcode
] != Soft386OpcodePrefix
)
80 /* A non-prefix opcode has been executed, reset the prefix flags */
81 State
->PrefixFlags
= 0;
85 /* This is a prefix, go to the next instruction immediately */
89 /* Increment the time stamp counter */
90 State
->TimeStampCounter
++;
92 while ((Command
== SOFT386_CONTINUE
)
93 || (Command
== SOFT386_STEP_OVER
&& ProcedureCallCount
> 0)
94 || (Command
== SOFT386_STEP_OUT
&& ProcedureCallCount
>= 0)
95 || (Soft386OpcodeHandlers
[Opcode
] == Soft386OpcodePrefix
));
98 /* PUBLIC FUNCTIONS ***********************************************************/
102 Soft386Continue(PSOFT386_STATE State
)
104 /* Call the internal function */
105 Soft386ExecutionControl(State
, SOFT386_CONTINUE
);
110 Soft386StepInto(PSOFT386_STATE State
)
112 /* Call the internal function */
113 Soft386ExecutionControl(State
, SOFT386_STEP_INTO
);
118 Soft386StepOver(PSOFT386_STATE State
)
120 /* Call the internal function */
121 Soft386ExecutionControl(State
, SOFT386_STEP_OVER
);
126 Soft386StepOut(PSOFT386_STATE State
)
128 /* Call the internal function */
129 Soft386ExecutionControl(State
, SOFT386_STEP_OUT
);
134 Soft386DumpState(PSOFT386_STATE State
)
136 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n"
137 "Time Stamp Counter = %016X\n",
138 (State
->ControlRegisters
[0] & SOFT386_CR0_PE
) ? "protected" : "real",
139 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
141 State
->TimeStampCounter
);
142 DPRINT1("\nGeneral purpose registers:\n"
143 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
144 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
145 State
->GeneralRegs
[SOFT386_REG_EAX
].Long
,
146 State
->GeneralRegs
[SOFT386_REG_ECX
].Long
,
147 State
->GeneralRegs
[SOFT386_REG_EDX
].Long
,
148 State
->GeneralRegs
[SOFT386_REG_EBX
].Long
,
149 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
,
150 State
->GeneralRegs
[SOFT386_REG_EBP
].Long
,
151 State
->GeneralRegs
[SOFT386_REG_ESI
].Long
,
152 State
->GeneralRegs
[SOFT386_REG_EDI
].Long
);
153 DPRINT1("\nSegment registers:\n"
154 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
155 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
156 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
157 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
158 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
159 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
160 State
->SegmentRegs
[SOFT386_REG_ES
].Selector
,
161 State
->SegmentRegs
[SOFT386_REG_ES
].Base
,
162 State
->SegmentRegs
[SOFT386_REG_ES
].Limit
,
163 State
->SegmentRegs
[SOFT386_REG_ES
].Dpl
,
164 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
165 State
->SegmentRegs
[SOFT386_REG_CS
].Base
,
166 State
->SegmentRegs
[SOFT386_REG_CS
].Limit
,
167 State
->SegmentRegs
[SOFT386_REG_CS
].Dpl
,
168 State
->SegmentRegs
[SOFT386_REG_SS
].Selector
,
169 State
->SegmentRegs
[SOFT386_REG_SS
].Base
,
170 State
->SegmentRegs
[SOFT386_REG_SS
].Limit
,
171 State
->SegmentRegs
[SOFT386_REG_SS
].Dpl
,
172 State
->SegmentRegs
[SOFT386_REG_DS
].Selector
,
173 State
->SegmentRegs
[SOFT386_REG_DS
].Base
,
174 State
->SegmentRegs
[SOFT386_REG_DS
].Limit
,
175 State
->SegmentRegs
[SOFT386_REG_DS
].Dpl
,
176 State
->SegmentRegs
[SOFT386_REG_FS
].Selector
,
177 State
->SegmentRegs
[SOFT386_REG_FS
].Base
,
178 State
->SegmentRegs
[SOFT386_REG_FS
].Limit
,
179 State
->SegmentRegs
[SOFT386_REG_FS
].Dpl
,
180 State
->SegmentRegs
[SOFT386_REG_GS
].Selector
,
181 State
->SegmentRegs
[SOFT386_REG_GS
].Base
,
182 State
->SegmentRegs
[SOFT386_REG_GS
].Limit
,
183 State
->SegmentRegs
[SOFT386_REG_GS
].Dpl
);
184 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
186 State
->Flags
.Cf
? "CF" : "cf",
187 State
->Flags
.Pf
? "PF" : "pf",
188 State
->Flags
.Af
? "AF" : "af",
189 State
->Flags
.Zf
? "ZF" : "zf",
190 State
->Flags
.Sf
? "SF" : "sf",
191 State
->Flags
.Tf
? "TF" : "tf",
192 State
->Flags
.If
? "IF" : "if",
193 State
->Flags
.Df
? "DF" : "df",
194 State
->Flags
.Of
? "OF" : "of",
195 State
->Flags
.Nt
? "NT" : "nt",
196 State
->Flags
.Rf
? "RF" : "rf",
197 State
->Flags
.Vm
? "VM" : "vm",
198 State
->Flags
.Ac
? "AC" : "ac",
199 State
->Flags
.Vif
? "VIF" : "vif",
200 State
->Flags
.Vip
? "VIP" : "vip",
202 DPRINT1("\nControl Registers:\n"
203 "CR0 = %08X\tCR1 = %08X\tCR2 = %08X\tCR3 = %08X\n"
204 "CR4 = %08X\tCR5 = %08X\tCR6 = %08X\tCR7 = %08X\n",
205 State
->ControlRegisters
[SOFT386_REG_CR0
],
206 State
->ControlRegisters
[SOFT386_REG_CR1
],
207 State
->ControlRegisters
[SOFT386_REG_CR2
],
208 State
->ControlRegisters
[SOFT386_REG_CR3
],
209 State
->ControlRegisters
[SOFT386_REG_CR4
],
210 State
->ControlRegisters
[SOFT386_REG_CR5
],
211 State
->ControlRegisters
[SOFT386_REG_CR6
],
212 State
->ControlRegisters
[SOFT386_REG_CR7
]);
213 DPRINT1("\nDebug Registers:\n"
214 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\tDR3 = %08X\n"
215 "DR4 = %08X\tDR5 = %08X\tDR6 = %08X\tDR7 = %08X\n",
216 State
->DebugRegisters
[SOFT386_REG_DR0
],
217 State
->DebugRegisters
[SOFT386_REG_DR1
],
218 State
->DebugRegisters
[SOFT386_REG_DR2
],
219 State
->DebugRegisters
[SOFT386_REG_DR3
],
220 State
->DebugRegisters
[SOFT386_REG_DR4
],
221 State
->DebugRegisters
[SOFT386_REG_DR5
],
222 State
->DebugRegisters
[SOFT386_REG_DR6
],
223 State
->DebugRegisters
[SOFT386_REG_DR7
]);
228 Soft386Reset(PSOFT386_STATE State
)
231 SOFT386_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
232 SOFT386_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
233 SOFT386_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
234 SOFT386_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
235 SOFT386_IDLE_PROC IdleCallback
= State
->IdleCallback
;
236 SOFT386_BOP_PROC BopCallback
= State
->BopCallback
;
238 /* Clear the entire structure */
239 RtlZeroMemory(State
, sizeof(*State
));
241 /* Initialize the registers */
242 State
->Flags
.AlwaysSet
= 1;
243 State
->InstPtr
.LowWord
= 0xFFF0;
245 /* Initialize segments */
246 for (i
= 0; i
< SOFT386_NUM_SEG_REGS
; i
++)
248 /* Set the selector, base and limit, other values don't apply in real mode */
249 State
->SegmentRegs
[i
].Selector
= 0;
250 State
->SegmentRegs
[i
].Base
= 0;
251 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
254 /* Initialize the code segment */
255 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
= 0xF000;
256 State
->SegmentRegs
[SOFT386_REG_CS
].Base
= 0xFFFF0000;
258 /* Initialize the IDT */
259 State
->Idtr
.Size
= 0x3FF;
260 State
->Idtr
.Address
= 0;
263 State
->ControlRegisters
[SOFT386_REG_CR0
] |= SOFT386_CR0_ET
;
265 /* Restore the callbacks */
266 State
->MemReadCallback
= MemReadCallback
;
267 State
->MemWriteCallback
= MemWriteCallback
;
268 State
->IoReadCallback
= IoReadCallback
;
269 State
->IoWriteCallback
= IoWriteCallback
;
270 State
->IdleCallback
= IdleCallback
;
271 State
->BopCallback
= BopCallback
;
276 Soft386Interrupt(PSOFT386_STATE State
, UCHAR Number
, BOOLEAN Hardware
)
278 SOFT386_IDT_ENTRY IdtEntry
;
282 /* Set the hardware interrupt flag */
283 State
->HardwareInt
= TRUE
;
286 if (!Soft386GetIntVector(State
, Number
, &IdtEntry
))
288 /* An exception occurred, let the handler execute */
292 /* Perform the interrupt */
293 Soft386InterruptInternal(State
,
295 MAKELONG(IdtEntry
.Offset
, IdtEntry
.OffsetHigh
),
301 Soft386ExecuteAt(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
303 /* Load the new CS */
304 if (!Soft386LoadSegment(State
, SOFT386_REG_CS
, Segment
))
306 /* An exception occurred, let the handler execute instead */
311 State
->InstPtr
.Long
= Offset
;
316 Soft386SetStack(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
318 /* Load the new SS */
319 if (!Soft386LoadSegment(State
, SOFT386_REG_SS
, Segment
))
321 /* An exception occurred, let the handler execute instead */
326 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
= Offset
;
331 Soft386SetSegment(PSOFT386_STATE State
,
332 SOFT386_SEG_REGS Segment
,
335 /* Call the internal function */
336 Soft386LoadSegment(State
, Segment
, Selector
);