060ac2cea5d8ef12dcc952b4810b2b0914aa0a65
[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 /* If this is a new instruction, save the IP */
60 if (State->PrefixFlags == 0) State->SavedInstPtr = State->InstPtr;
61
62 /* Perform an instruction fetch */
63 if (!Soft386FetchByte(State, &Opcode)) continue;
64
65 // TODO: Check for CALL/RET to update ProcedureCallCount.
66
67 if (Soft386OpcodeHandlers[Opcode] != NULL)
68 {
69 /* Call the opcode handler */
70 Soft386OpcodeHandlers[Opcode](State, Opcode);
71 }
72 else
73 {
74 /* This is not a valid opcode */
75 Soft386Exception(State, SOFT386_EXCEPTION_UD);
76 }
77
78 if (Soft386OpcodeHandlers[Opcode] != Soft386OpcodePrefix)
79 {
80 /* A non-prefix opcode has been executed, reset the prefix flags */
81 State->PrefixFlags = 0;
82 }
83 else
84 {
85 /* This is a prefix, go to the next instruction immediately */
86 continue;
87 }
88
89 /* Increment the time stamp counter */
90 State->TimeStampCounter++;
91 }
92 while ((Command == SOFT386_CONTINUE)
93 || (Command == SOFT386_STEP_OVER && ProcedureCallCount > 0)
94 || (Command == SOFT386_STEP_OUT && ProcedureCallCount >= 0)
95 || (Soft386OpcodeHandlers[Opcode] == Soft386OpcodePrefix));
96 }
97
98 /* PUBLIC FUNCTIONS ***********************************************************/
99
100 VOID
101 NTAPI
102 Soft386Continue(PSOFT386_STATE State)
103 {
104 /* Call the internal function */
105 Soft386ExecutionControl(State, SOFT386_CONTINUE);
106 }
107
108 VOID
109 NTAPI
110 Soft386StepInto(PSOFT386_STATE State)
111 {
112 /* Call the internal function */
113 Soft386ExecutionControl(State, SOFT386_STEP_INTO);
114 }
115
116 VOID
117 NTAPI
118 Soft386StepOver(PSOFT386_STATE State)
119 {
120 /* Call the internal function */
121 Soft386ExecutionControl(State, SOFT386_STEP_OVER);
122 }
123
124 VOID
125 NTAPI
126 Soft386StepOut(PSOFT386_STATE State)
127 {
128 /* Call the internal function */
129 Soft386ExecutionControl(State, SOFT386_STEP_OUT);
130 }
131
132 VOID
133 NTAPI
134 Soft386DumpState(PSOFT386_STATE State)
135 {
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,
140 State->InstPtr.Long,
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",
185 State->Flags.Long,
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",
201 State->Flags.Iopl);
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]);
224 }
225
226 VOID
227 NTAPI
228 Soft386Reset(PSOFT386_STATE State)
229 {
230 INT i;
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;
237
238 /* Clear the entire structure */
239 RtlZeroMemory(State, sizeof(*State));
240
241 /* Initialize the registers */
242 State->Flags.AlwaysSet = 1;
243 State->InstPtr.LowWord = 0xFFF0;
244
245 /* Initialize segments */
246 for (i = 0; i < SOFT386_NUM_SEG_REGS; i++)
247 {
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;
252 }
253
254 /* Initialize the code segment */
255 State->SegmentRegs[SOFT386_REG_CS].Selector = 0xF000;
256 State->SegmentRegs[SOFT386_REG_CS].Base = 0xFFFF0000;
257
258 /* Initialize the IDT */
259 State->Idtr.Size = 0x3FF;
260 State->Idtr.Address = 0;
261
262 /* Initialize CR0 */
263 State->ControlRegisters[SOFT386_REG_CR0] |= SOFT386_CR0_ET;
264
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;
272 }
273
274 VOID
275 NTAPI
276 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number, BOOLEAN Hardware)
277 {
278 SOFT386_IDT_ENTRY IdtEntry;
279
280 if (Hardware)
281 {
282 /* Set the hardware interrupt flag */
283 State->HardwareInt = TRUE;
284 }
285
286 if (!Soft386GetIntVector(State, Number, &IdtEntry))
287 {
288 /* An exception occurred, let the handler execute */
289 return;
290 }
291
292 /* Perform the interrupt */
293 Soft386InterruptInternal(State,
294 IdtEntry.Selector,
295 MAKELONG(IdtEntry.Offset, IdtEntry.OffsetHigh),
296 IdtEntry.Type);
297 }
298
299 VOID
300 NTAPI
301 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
302 {
303 /* Load the new CS */
304 if (!Soft386LoadSegment(State, SOFT386_REG_CS, Segment))
305 {
306 /* An exception occurred, let the handler execute instead */
307 return;
308 }
309
310 /* Set the new IP */
311 State->InstPtr.Long = Offset;
312 }
313
314 VOID
315 NTAPI
316 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
317 {
318 /* Load the new SS */
319 if (!Soft386LoadSegment(State, SOFT386_REG_SS, Segment))
320 {
321 /* An exception occurred, let the handler execute instead */
322 return;
323 }
324
325 /* Set the new SP */
326 State->GeneralRegs[SOFT386_REG_ESP].Long = Offset;
327 }
328
329 VOID
330 NTAPI
331 Soft386SetSegment(PSOFT386_STATE State,
332 SOFT386_SEG_REGS Segment,
333 USHORT Selector)
334 {
335 /* Call the internal function */
336 Soft386LoadSegment(State, Segment, Selector);
337 }
338
339 /* EOF */