[SOFT386]
[reactos.git] / lib / soft386 / soft386.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: 386/486 CPU Emulation Library
4 * FILE: soft386.c
5 * PURPOSE: Functions meant for use by the host.
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 // #define WIN32_NO_STATUS
12 // #define _INC_WINDOWS
13 #include <windef.h>
14
15 // #define NDEBUG
16 #include <debug.h>
17
18 #include <soft386.h>
19 #include "common.h"
20 #include "opcodes.h"
21
22 /* DEFINES ********************************************************************/
23
24 typedef enum
25 {
26 SOFT386_STEP_INTO,
27 SOFT386_STEP_OVER,
28 SOFT386_STEP_OUT,
29 SOFT386_CONTINUE
30 } SOFT386_EXEC_CMD;
31
32 /* PRIVATE FUNCTIONS **********************************************************/
33
34 static
35 inline
36 VOID
37 NTAPI
38 Soft386ExecutionControl(PSOFT386_STATE State, INT Command)
39 {
40 UCHAR Opcode;
41 INT ProcedureCallCount = 0;
42
43 /* Main execution loop */
44 do
45 {
46 /* Perform an instruction fetch */
47 if (!Soft386FetchByte(State, &Opcode)) continue;
48
49 // TODO: Check for CALL/RET to update ProcedureCallCount.
50
51 if (Soft386OpcodeHandlers[Opcode] != NULL)
52 {
53 /* Call the opcode handler */
54 Soft386OpcodeHandlers[Opcode](State, Opcode);
55 }
56 else
57 {
58 /* This is not a valid opcode */
59 Soft386Exception(State, SOFT386_EXCEPTION_UD);
60 }
61
62 if (Soft386OpcodeHandlers[Opcode] != Soft386OpcodePrefix)
63 {
64 /* A non-prefix opcode has been executed, reset the prefix flags */
65 State->PrefixFlags = 0;
66 }
67
68 /* Increment the time stamp counter */
69 State->TimeStampCounter++;
70 }
71 while ((Command == SOFT386_CONTINUE)
72 || (Command == SOFT386_STEP_OVER && ProcedureCallCount > 0)
73 || (Command == SOFT386_STEP_OUT && ProcedureCallCount >= 0));
74 }
75
76 /* PUBLIC FUNCTIONS ***********************************************************/
77
78 VOID
79 NTAPI
80 Soft386Continue(PSOFT386_STATE State)
81 {
82 /* Call the internal function */
83 Soft386ExecutionControl(State, SOFT386_CONTINUE);
84 }
85
86 VOID
87 NTAPI
88 Soft386StepInto(PSOFT386_STATE State)
89 {
90 /* Call the internal function */
91 Soft386ExecutionControl(State, SOFT386_STEP_INTO);
92 }
93
94 VOID
95 NTAPI
96 Soft386StepOver(PSOFT386_STATE State)
97 {
98 /* Call the internal function */
99 Soft386ExecutionControl(State, SOFT386_STEP_OVER);
100 }
101
102 VOID
103 NTAPI
104 Soft386StepOut(PSOFT386_STATE State)
105 {
106 /* Call the internal function */
107 Soft386ExecutionControl(State, SOFT386_STEP_OUT);
108 }
109
110 VOID
111 NTAPI
112 Soft386DumpState(PSOFT386_STATE State)
113 {
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,
118 State->InstPtr.Long,
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",
163 State->Flags.Long,
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",
179 State->Flags.Iopl);
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]);
202 }
203
204 VOID
205 NTAPI
206 Soft386Reset(PSOFT386_STATE State)
207 {
208 INT i;
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;
214 SOFT386_BOP_PROC BopCallback = State->BopCallback;
215
216 /* Clear the entire structure */
217 RtlZeroMemory(State, sizeof(*State));
218
219 /* Initialize the registers */
220 State->Flags.AlwaysSet = 1;
221 State->InstPtr.LowWord = 0xFFF0;
222
223 /* Initialize segments */
224 for (i = 0; i < SOFT386_NUM_SEG_REGS; i++)
225 {
226 /* Set the selector, base and limit, other values don't apply in real mode */
227 State->SegmentRegs[i].Selector = 0;
228 State->SegmentRegs[i].Base = 0;
229 State->SegmentRegs[i].Limit = 0xFFFF;
230 }
231
232 /* Initialize the code segment */
233 State->SegmentRegs[SOFT386_REG_CS].Selector = 0xF000;
234 State->SegmentRegs[SOFT386_REG_CS].Base = 0xFFFF0000;
235
236 /* Initialize the IDT */
237 State->Idtr.Size = 0x3FF;
238 State->Idtr.Address = 0;
239
240 /* Initialize CR0 */
241 State->ControlRegisters[SOFT386_REG_CR0] |= SOFT386_CR0_ET;
242
243 /* Restore the callbacks */
244 State->MemReadCallback = MemReadCallback;
245 State->MemWriteCallback = MemWriteCallback;
246 State->IoReadCallback = IoReadCallback;
247 State->IoWriteCallback = IoWriteCallback;
248 State->IdleCallback = IdleCallback;
249 State->BopCallback = BopCallback;
250 }
251
252 VOID
253 NTAPI
254 Soft386Interrupt(PSOFT386_STATE State, UCHAR Number)
255 {
256 // TODO: NOT IMPLEMENTED!!!
257 UNIMPLEMENTED;
258 }
259
260 VOID
261 NTAPI
262 Soft386ExecuteAt(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
263 {
264 /* Load the new CS */
265 if (!Soft386LoadSegment(State, SOFT386_REG_CS, Segment))
266 {
267 /* An exception occurred, let the handler execute instead */
268 return;
269 }
270
271 /* Set the new IP */
272 State->InstPtr.Long = Offset;
273 }
274
275 VOID
276 NTAPI
277 Soft386SetStack(PSOFT386_STATE State, USHORT Segment, ULONG Offset)
278 {
279 /* Load the new SS */
280 if (!Soft386LoadSegment(State, SOFT386_REG_SS, Segment))
281 {
282 /* An exception occurred, let the handler execute instead */
283 return;
284 }
285
286 /* Set the new SP */
287 State->GeneralRegs[SOFT386_REG_ESP].Long = Offset;
288 }
289
290
291 /* EOF */