2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: 386/486 CPU Emulation Library
5 * PURPOSE: Functions meant for use by the host.
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
11 // #define WIN32_NO_STATUS
12 // #define _INC_WINDOWS
22 /* DEFINES ********************************************************************/
32 /* PRIVATE FUNCTIONS **********************************************************/
38 Soft386ExecutionControl(PSOFT386_STATE State
, INT Command
)
41 INT ProcedureCallCount
= 0;
43 /* Main execution loop */
46 /* Perform an instruction fetch */
47 if (!Soft386FetchByte(State
, &Opcode
)) continue;
49 // TODO: Check for CALL/RET to update ProcedureCallCount.
51 if (Soft386OpcodeHandlers
[Opcode
] != NULL
)
53 /* Call the opcode handler */
54 Soft386OpcodeHandlers
[Opcode
](State
, Opcode
);
58 /* This is not a valid opcode */
59 Soft386Exception(State
, SOFT386_EXCEPTION_UD
);
62 if (Soft386OpcodeHandlers
[Opcode
] != Soft386OpcodePrefix
)
64 /* A non-prefix opcode has been executed, reset the prefix flags */
65 State
->PrefixFlags
= 0;
68 /* Increment the time stamp counter */
69 State
->TimeStampCounter
++;
71 while ((Command
== SOFT386_CONTINUE
)
72 || (Command
== SOFT386_STEP_OVER
&& ProcedureCallCount
> 0)
73 || (Command
== SOFT386_STEP_OUT
&& ProcedureCallCount
>= 0));
76 /* PUBLIC FUNCTIONS ***********************************************************/
80 Soft386Continue(PSOFT386_STATE State
)
82 /* Call the internal function */
83 Soft386ExecutionControl(State
, SOFT386_CONTINUE
);
88 Soft386StepInto(PSOFT386_STATE State
)
90 /* Call the internal function */
91 Soft386ExecutionControl(State
, SOFT386_STEP_INTO
);
96 Soft386StepOver(PSOFT386_STATE State
)
98 /* Call the internal function */
99 Soft386ExecutionControl(State
, SOFT386_STEP_OVER
);
104 Soft386StepOut(PSOFT386_STATE State
)
106 /* Call the internal function */
107 Soft386ExecutionControl(State
, SOFT386_STEP_OUT
);
112 Soft386DumpState(PSOFT386_STATE State
)
114 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n"
115 "Time Stamp Counter = %016X\n",
116 (State
->ControlRegisters
[0] & SOFT386_CR0_PE
) ? "protected" : "real",
117 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
119 State
->TimeStampCounter
);
120 DPRINT1("\nGeneral purpose registers:\n"
121 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
122 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
123 State
->GeneralRegs
[SOFT386_REG_EAX
].Long
,
124 State
->GeneralRegs
[SOFT386_REG_ECX
].Long
,
125 State
->GeneralRegs
[SOFT386_REG_EDX
].Long
,
126 State
->GeneralRegs
[SOFT386_REG_EBX
].Long
,
127 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
,
128 State
->GeneralRegs
[SOFT386_REG_EBP
].Long
,
129 State
->GeneralRegs
[SOFT386_REG_ESI
].Long
,
130 State
->GeneralRegs
[SOFT386_REG_EDI
].Long
);
131 DPRINT1("\nSegment registers:\n"
132 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
133 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
134 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
135 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
136 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
137 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
138 State
->SegmentRegs
[SOFT386_REG_ES
].Selector
,
139 State
->SegmentRegs
[SOFT386_REG_ES
].Base
,
140 State
->SegmentRegs
[SOFT386_REG_ES
].Limit
,
141 State
->SegmentRegs
[SOFT386_REG_ES
].Dpl
,
142 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
,
143 State
->SegmentRegs
[SOFT386_REG_CS
].Base
,
144 State
->SegmentRegs
[SOFT386_REG_CS
].Limit
,
145 State
->SegmentRegs
[SOFT386_REG_CS
].Dpl
,
146 State
->SegmentRegs
[SOFT386_REG_SS
].Selector
,
147 State
->SegmentRegs
[SOFT386_REG_SS
].Base
,
148 State
->SegmentRegs
[SOFT386_REG_SS
].Limit
,
149 State
->SegmentRegs
[SOFT386_REG_SS
].Dpl
,
150 State
->SegmentRegs
[SOFT386_REG_DS
].Selector
,
151 State
->SegmentRegs
[SOFT386_REG_DS
].Base
,
152 State
->SegmentRegs
[SOFT386_REG_DS
].Limit
,
153 State
->SegmentRegs
[SOFT386_REG_DS
].Dpl
,
154 State
->SegmentRegs
[SOFT386_REG_FS
].Selector
,
155 State
->SegmentRegs
[SOFT386_REG_FS
].Base
,
156 State
->SegmentRegs
[SOFT386_REG_FS
].Limit
,
157 State
->SegmentRegs
[SOFT386_REG_FS
].Dpl
,
158 State
->SegmentRegs
[SOFT386_REG_GS
].Selector
,
159 State
->SegmentRegs
[SOFT386_REG_GS
].Base
,
160 State
->SegmentRegs
[SOFT386_REG_GS
].Limit
,
161 State
->SegmentRegs
[SOFT386_REG_GS
].Dpl
);
162 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
164 State
->Flags
.Cf
? "CF" : "cf",
165 State
->Flags
.Pf
? "PF" : "pf",
166 State
->Flags
.Af
? "AF" : "af",
167 State
->Flags
.Zf
? "ZF" : "zf",
168 State
->Flags
.Sf
? "SF" : "sf",
169 State
->Flags
.Tf
? "TF" : "tf",
170 State
->Flags
.If
? "IF" : "if",
171 State
->Flags
.Df
? "DF" : "df",
172 State
->Flags
.Of
? "OF" : "of",
173 State
->Flags
.Nt
? "NT" : "nt",
174 State
->Flags
.Rf
? "RF" : "rf",
175 State
->Flags
.Vm
? "VM" : "vm",
176 State
->Flags
.Ac
? "AC" : "ac",
177 State
->Flags
.Vif
? "VIF" : "vif",
178 State
->Flags
.Vip
? "VIP" : "vip",
180 DPRINT1("\nControl Registers:\n"
181 "CR0 = %08X\tCR1 = %08X\tCR2 = %08X\tCR3 = %08X\n"
182 "CR4 = %08X\tCR5 = %08X\tCR6 = %08X\tCR7 = %08X\n",
183 State
->ControlRegisters
[SOFT386_REG_CR0
],
184 State
->ControlRegisters
[SOFT386_REG_CR1
],
185 State
->ControlRegisters
[SOFT386_REG_CR2
],
186 State
->ControlRegisters
[SOFT386_REG_CR3
],
187 State
->ControlRegisters
[SOFT386_REG_CR4
],
188 State
->ControlRegisters
[SOFT386_REG_CR5
],
189 State
->ControlRegisters
[SOFT386_REG_CR6
],
190 State
->ControlRegisters
[SOFT386_REG_CR7
]);
191 DPRINT1("\nDebug Registers:\n"
192 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\tDR3 = %08X\n"
193 "DR4 = %08X\tDR5 = %08X\tDR6 = %08X\tDR7 = %08X\n",
194 State
->DebugRegisters
[SOFT386_REG_DR0
],
195 State
->DebugRegisters
[SOFT386_REG_DR1
],
196 State
->DebugRegisters
[SOFT386_REG_DR2
],
197 State
->DebugRegisters
[SOFT386_REG_DR3
],
198 State
->DebugRegisters
[SOFT386_REG_DR4
],
199 State
->DebugRegisters
[SOFT386_REG_DR5
],
200 State
->DebugRegisters
[SOFT386_REG_DR6
],
201 State
->DebugRegisters
[SOFT386_REG_DR7
]);
206 Soft386Reset(PSOFT386_STATE State
)
209 SOFT386_MEM_READ_PROC MemReadCallback
= State
->MemReadCallback
;
210 SOFT386_MEM_WRITE_PROC MemWriteCallback
= State
->MemWriteCallback
;
211 SOFT386_IO_READ_PROC IoReadCallback
= State
->IoReadCallback
;
212 SOFT386_IO_WRITE_PROC IoWriteCallback
= State
->IoWriteCallback
;
213 SOFT386_IDLE_PROC IdleCallback
= State
->IdleCallback
;
215 /* Clear the entire structure */
216 RtlZeroMemory(State
, sizeof(*State
));
218 /* Initialize the registers */
219 State
->Flags
.AlwaysSet
= 1;
220 State
->InstPtr
.LowWord
= 0xFFF0;
222 /* Initialize segments */
223 for (i
= 0; i
< SOFT386_NUM_SEG_REGS
; i
++)
225 /* Set the selector, base and limit, other values don't apply in real mode */
226 State
->SegmentRegs
[i
].Selector
= 0;
227 State
->SegmentRegs
[i
].Base
= 0;
228 State
->SegmentRegs
[i
].Limit
= 0xFFFF;
231 /* Initialize the code segment */
232 State
->SegmentRegs
[SOFT386_REG_CS
].Selector
= 0xF000;
233 State
->SegmentRegs
[SOFT386_REG_CS
].Base
= 0xFFFF0000;
235 /* Initialize the IDT */
236 State
->Idtr
.Size
= 0x3FF;
237 State
->Idtr
.Address
= 0;
240 State
->ControlRegisters
[SOFT386_REG_CR0
] |= SOFT386_CR0_ET
;
242 /* Restore the callbacks */
243 State
->MemReadCallback
= MemReadCallback
;
244 State
->MemWriteCallback
= MemWriteCallback
;
245 State
->IoReadCallback
= IoReadCallback
;
246 State
->IoWriteCallback
= IoWriteCallback
;
247 State
->IdleCallback
= IdleCallback
;
252 Soft386Interrupt(PSOFT386_STATE State
, UCHAR Number
)
254 // TODO: NOT IMPLEMENTED!!!
260 Soft386ExecuteAt(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
262 /* Load the new CS */
263 if (!Soft386LoadSegment(State
, SOFT386_REG_CS
, Segment
))
265 /* An exception occurred, let the handler execute instead */
270 State
->InstPtr
.Long
= Offset
;
275 Soft386SetStack(PSOFT386_STATE State
, USHORT Segment
, ULONG Offset
)
277 /* Load the new SS */
278 if (!Soft386LoadSegment(State
, SOFT386_REG_SS
, Segment
))
280 /* An exception occurred, let the handler execute instead */
285 State
->GeneralRegs
[SOFT386_REG_ESP
].Long
= Offset
;