[SOFT386]
[reactos.git] / lib / soft386 / soft386.c
1 /*
2 * Soft386 386/486 CPU Emulation Library
3 * soft386.c
4 *
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 /* INCLUDES *******************************************************************/
23
24 // #define WIN32_NO_STATUS
25 // #define _INC_WINDOWS
26 #include <windef.h>
27
28 // #define NDEBUG
29 #include <debug.h>
30
31 #include <soft386.h>
32 #include "common.h"
33 #include "opcodes.h"
34
35 /* DEFINES ********************************************************************/
36
37 typedef enum
38 {
39 SOFT386_STEP_INTO,
40 SOFT386_STEP_OVER,
41 SOFT386_STEP_OUT,
42 SOFT386_CONTINUE
43 } SOFT386_EXEC_CMD;
44
45 /* PRIVATE FUNCTIONS **********************************************************/
46
47 static
48 inline
49 VOID
50 NTAPI
51 Soft386ExecutionControl(PSOFT386_STATE State, INT Command)
52 {
53 UCHAR Opcode;
54 INT ProcedureCallCount = 0;
55
56 /* Main execution loop */
57 do
58 {
59 /* Check if this is a new instruction */
60 if (State->PrefixFlags == 0)
61 {
62 State->SavedInstPtr = State->InstPtr;
63
64 /* Check if interrupts are enabled and there is an interrupt pending */
65 if (State->Flags.If && State->HardwareInt)
66 {
67 SOFT386_IDT_ENTRY IdtEntry;
68
69 /* Get the interrupt vector */
70 if (Soft386GetIntVector(State, State->PendingIntNum, &IdtEntry))
71 {
72 /* Perform the interrupt */
73 Soft386InterruptInternal(State,
74 IdtEntry.Selector,
75 MAKELONG(IdtEntry.Offset, IdtEntry.OffsetHigh),
76 IdtEntry.Type);
77 }
78
79 /* Clear the interrupt pending flag */
80 State->HardwareInt = FALSE;
81 }
82 }
83
84 /* Perform an instruction fetch */
85 if (!Soft386FetchByte(State, &Opcode)) continue;
86
87 // TODO: Check for CALL/RET to update ProcedureCallCount.
88
89 if (Soft386OpcodeHandlers[Opcode] != NULL)
90 {
91 /* Call the opcode handler */
92 Soft386OpcodeHandlers[Opcode](State, Opcode);
93 }
94 else
95 {
96 /* This is not a valid opcode */
97 Soft386Exception(State, SOFT386_EXCEPTION_UD);
98 }
99
100 if (Soft386OpcodeHandlers[Opcode] != Soft386OpcodePrefix)
101 {
102 /* A non-prefix opcode has been executed, reset the prefix flags */
103 State->PrefixFlags = 0;
104 }
105 else
106 {
107 /* This is a prefix, go to the next instruction immediately */
108 continue;
109 }
110
111 /* Increment the time stamp counter */
112 State->TimeStampCounter++;
113 }
114 while ((Command == SOFT386_CONTINUE)
115 || (Command == SOFT386_STEP_OVER && ProcedureCallCount > 0)
116 || (Command == SOFT386_STEP_OUT && ProcedureCallCount >= 0)
117 || (Soft386OpcodeHandlers[Opcode] == Soft386OpcodePrefix));
118 }
119
120 /* PUBLIC FUNCTIONS ***********************************************************/
121
122 VOID
123 NTAPI
124 Soft386Continue(PSOFT386_STATE State)
125 {
126 /* Call the internal function */
127 Soft386ExecutionControl(State, SOFT386_CONTINUE);
128 }
129
130 VOID
131 NTAPI
132 Soft386StepInto(PSOFT386_STATE State)
133 {
134 /* Call the internal function */
135 Soft386ExecutionControl(State, SOFT386_STEP_INTO);
136 }
137
138 VOID
139 NTAPI
140 Soft386StepOver(PSOFT386_STATE State)
141 {
142 /* Call the internal function */
143 Soft386ExecutionControl(State, SOFT386_STEP_OVER);
144 }
145
146 VOID
147 NTAPI
148 Soft386StepOut(PSOFT386_STATE State)
149 {
150 /* Call the internal function */
151 Soft386ExecutionControl(State, SOFT386_STEP_OUT);
152 }
153
154 VOID
155 NTAPI
156 Soft386DumpState(PSOFT386_STATE State)
157 {
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,
162 State->InstPtr.Long,
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",
207 State->Flags.Long,
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",
223 State->Flags.Iopl);
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]);
246 }
247
248 VOID
249 NTAPI
250 Soft386Reset(PSOFT386_STATE State)
251 {
252 INT i;
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;
259
260 /* Clear the entire structure */
261 RtlZeroMemory(State, sizeof(*State));
262
263 /* Initialize the registers */
264 State->Flags.AlwaysSet = 1;
265 State->InstPtr.LowWord = 0xFFF0;
266
267 /* Initialize segments */
268 for (i = 0; i < SOFT386_NUM_SEG_REGS; i++)
269 {
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;
274 }
275
276 /* Initialize the code segment */
277 State->SegmentRegs[SOFT386_REG_CS].Selector = 0xF000;
278 State->SegmentRegs[SOFT386_REG_CS].Base = 0xFFFF0000;
279
280 /* Initialize the IDT */
281 State->Idtr.Size = 0x3FF;
282 State->Idtr.Address = 0;
283
284 /* Initialize CR0 */
285 State->ControlRegisters[SOFT386_REG_CR0] |= SOFT386_CR0_ET;
286
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;
294 }
295
296 VOID
297 NTAPI
298 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number)
299 {
300 /* Set the hardware interrupt flag */
301 State->HardwareInt = TRUE;
302 State->PendingIntNum = Number;
303 }
304
305 VOID
306 NTAPI
307 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
308 {
309 /* Load the new CS */
310 if (!Soft386LoadSegment(State, SOFT386_REG_CS, Segment))
311 {
312 /* An exception occurred, let the handler execute instead */
313 return;
314 }
315
316 /* Set the new IP */
317 State->InstPtr.Long = Offset;
318 }
319
320 VOID
321 NTAPI
322 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
323 {
324 /* Load the new SS */
325 if (!Soft386LoadSegment(State, SOFT386_REG_SS, Segment))
326 {
327 /* An exception occurred, let the handler execute instead */
328 return;
329 }
330
331 /* Set the new SP */
332 State->GeneralRegs[SOFT386_REG_ESP].Long = Offset;
333 }
334
335 VOID
336 NTAPI
337 Soft386SetSegment(PSOFT386_STATE State,
338 SOFT386_SEG_REGS Segment,
339 USHORT Selector)
340 {
341 /* Call the internal function */
342 Soft386LoadSegment(State, Segment, Selector);
343 }
344
345 /* EOF */