[FAST486][NTVDM]
[reactos.git] / lib / fast486 / fast486.c
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486.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 #include <windef.h>
25
26 // #define NDEBUG
27 #include <debug.h>
28
29 #include <fast486.h>
30 #include "common.h"
31 #include "opcodes.h"
32
33 /* DEFINES ********************************************************************/
34
35 typedef enum
36 {
37 FAST486_STEP_INTO,
38 FAST486_STEP_OVER,
39 FAST486_STEP_OUT,
40 FAST486_CONTINUE
41 } FAST486_EXEC_CMD;
42
43 /* PRIVATE FUNCTIONS **********************************************************/
44
45 static
46 inline
47 VOID
48 NTAPI
49 Fast486ExecutionControl(PFAST486_STATE State, INT Command)
50 {
51 UCHAR Opcode;
52 INT ProcedureCallCount = 0;
53
54 /* Main execution loop */
55 do
56 {
57 /* Check if this is a new instruction */
58 if (State->PrefixFlags == 0)
59 {
60 State->SavedInstPtr = State->InstPtr;
61
62 /*
63 * Check if there is an interrupt to execute, or a hardware interrupt signal
64 * while interrupts are enabled.
65 */
66 if ((State->IntStatus == FAST486_INT_EXECUTE)
67 || (State->Flags.If
68 && (State->IntAckCallback != NULL)
69 && (State->IntStatus == FAST486_INT_SIGNAL)))
70 {
71 FAST486_IDT_ENTRY IdtEntry;
72
73 if (State->IntStatus == FAST486_INT_SIGNAL)
74 {
75 /* Acknowledge the interrupt to get the number */
76 State->PendingIntNum = State->IntAckCallback(State);
77 }
78
79 /* Get the interrupt vector */
80 if (Fast486GetIntVector(State, State->PendingIntNum, &IdtEntry))
81 {
82 /* Perform the interrupt */
83 Fast486InterruptInternal(State,
84 IdtEntry.Selector,
85 MAKELONG(IdtEntry.Offset, IdtEntry.OffsetHigh),
86 IdtEntry.Type);
87 }
88
89 /* Clear the interrupt status */
90 State->IntStatus = FAST486_INT_NONE;
91 }
92 }
93
94 /* Perform an instruction fetch */
95 if (!Fast486FetchByte(State, &Opcode)) continue;
96
97 // TODO: Check for CALL/RET to update ProcedureCallCount.
98
99 if (Fast486OpcodeHandlers[Opcode] != NULL)
100 {
101 /* Call the opcode handler */
102 Fast486OpcodeHandlers[Opcode](State, Opcode);
103 }
104 else
105 {
106 /* This is not a valid opcode */
107 Fast486Exception(State, FAST486_EXCEPTION_UD);
108 }
109
110 if (Fast486OpcodeHandlers[Opcode] != Fast486OpcodePrefix)
111 {
112 /* A non-prefix opcode has been executed, reset the prefix flags */
113 State->PrefixFlags = 0;
114 }
115 else
116 {
117 /* This is a prefix, go to the next instruction immediately */
118 continue;
119 }
120 }
121 while ((Command == FAST486_CONTINUE)
122 || (Command == FAST486_STEP_OVER && ProcedureCallCount > 0)
123 || (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0)
124 || (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix));
125 }
126
127 /* DEFAULT CALLBACKS **********************************************************/
128
129 static VOID
130 NTAPI
131 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
132 {
133 UNREFERENCED_PARAMETER(State);
134
135 RtlMoveMemory(Buffer, (PVOID)Address, Size);
136 }
137
138 static VOID
139 NTAPI
140 Fast486MemWriteCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
141 {
142 UNREFERENCED_PARAMETER(State);
143
144 RtlMoveMemory((PVOID)Address, Buffer, Size);
145 }
146
147 static VOID
148 NTAPI
149 Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size)
150 {
151 UNREFERENCED_PARAMETER(State);
152 UNREFERENCED_PARAMETER(Port);
153 UNREFERENCED_PARAMETER(Buffer);
154 UNREFERENCED_PARAMETER(Size);
155 }
156
157 static VOID
158 NTAPI
159 Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG Size)
160 {
161 UNREFERENCED_PARAMETER(State);
162 UNREFERENCED_PARAMETER(Port);
163 UNREFERENCED_PARAMETER(Buffer);
164 UNREFERENCED_PARAMETER(Size);
165 }
166
167 static VOID
168 NTAPI
169 Fast486IdleCallback(PFAST486_STATE State)
170 {
171 UNREFERENCED_PARAMETER(State);
172 }
173
174 static VOID
175 NTAPI
176 Fast486BopCallback(PFAST486_STATE State, UCHAR BopCode)
177 {
178 UNREFERENCED_PARAMETER(State);
179 UNREFERENCED_PARAMETER(BopCode);
180 }
181
182 static UCHAR
183 NTAPI
184 Fast486IntAckCallback(PFAST486_STATE State)
185 {
186 UNREFERENCED_PARAMETER(State);
187
188 /* Return something... */
189 return 0;
190 }
191
192 /* PUBLIC FUNCTIONS ***********************************************************/
193
194 VOID
195 NTAPI
196 Fast486Initialize(PFAST486_STATE State,
197 FAST486_MEM_READ_PROC MemReadCallback,
198 FAST486_MEM_WRITE_PROC MemWriteCallback,
199 FAST486_IO_READ_PROC IoReadCallback,
200 FAST486_IO_WRITE_PROC IoWriteCallback,
201 FAST486_IDLE_PROC IdleCallback,
202 FAST486_BOP_PROC BopCallback,
203 FAST486_INT_ACK_PROC IntAckCallback)
204 {
205 /* Set the callbacks (or use default ones if some are NULL) */
206 State->MemReadCallback = (MemReadCallback ? MemReadCallback : Fast486MemReadCallback );
207 State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
208 State->IoReadCallback = (IoReadCallback ? IoReadCallback : Fast486IoReadCallback );
209 State->IoWriteCallback = (IoWriteCallback ? IoWriteCallback : Fast486IoWriteCallback );
210 State->IdleCallback = (IdleCallback ? IdleCallback : Fast486IdleCallback );
211 State->BopCallback = (BopCallback ? BopCallback : Fast486BopCallback );
212 State->IntAckCallback = (IntAckCallback ? IntAckCallback : Fast486IntAckCallback );
213
214 /* Reset the CPU */
215 Fast486Reset(State);
216 }
217
218 VOID
219 NTAPI
220 Fast486Reset(PFAST486_STATE State)
221 {
222 USHORT i;
223
224 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
225 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
226 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
227 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
228 FAST486_IDLE_PROC IdleCallback = State->IdleCallback;
229 FAST486_BOP_PROC BopCallback = State->BopCallback;
230 FAST486_INT_ACK_PROC IntAckCallback = State->IntAckCallback;
231
232 /* Clear the entire structure */
233 RtlZeroMemory(State, sizeof(*State));
234
235 /* Initialize the registers */
236 State->Flags.AlwaysSet = 1;
237 State->InstPtr.LowWord = 0xFFF0;
238
239 /* Initialize segments */
240 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
241 {
242 /* Set the selector, base and limit, other values don't apply in real mode */
243 State->SegmentRegs[i].Selector = 0;
244 State->SegmentRegs[i].Base = 0;
245 State->SegmentRegs[i].Limit = 0xFFFF;
246 }
247
248 /* Initialize the code segment */
249 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
250 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
251
252 /* Initialize the IDT */
253 State->Idtr.Size = 0x3FF;
254 State->Idtr.Address = 0;
255
256 /* Initialize CR0 */
257 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
258
259 /* Restore the callbacks */
260 State->MemReadCallback = MemReadCallback;
261 State->MemWriteCallback = MemWriteCallback;
262 State->IoReadCallback = IoReadCallback;
263 State->IoWriteCallback = IoWriteCallback;
264 State->IdleCallback = IdleCallback;
265 State->BopCallback = BopCallback;
266 State->IntAckCallback = IntAckCallback;
267 }
268
269 VOID
270 NTAPI
271 Fast486DumpState(PFAST486_STATE State)
272 {
273 DPRINT1("\nCPU currently executing in %s mode at %04X:%08X\n",
274 (State->ControlRegisters[0] & FAST486_CR0_PE) ? "protected" : "real",
275 State->SegmentRegs[FAST486_REG_CS].Selector,
276 State->InstPtr.Long);
277 DPRINT1("\nGeneral purpose registers:\n"
278 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
279 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
280 State->GeneralRegs[FAST486_REG_EAX].Long,
281 State->GeneralRegs[FAST486_REG_ECX].Long,
282 State->GeneralRegs[FAST486_REG_EDX].Long,
283 State->GeneralRegs[FAST486_REG_EBX].Long,
284 State->GeneralRegs[FAST486_REG_ESP].Long,
285 State->GeneralRegs[FAST486_REG_EBP].Long,
286 State->GeneralRegs[FAST486_REG_ESI].Long,
287 State->GeneralRegs[FAST486_REG_EDI].Long);
288 DPRINT1("\nSegment registers:\n"
289 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
290 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
291 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
292 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
293 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
294 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
295 State->SegmentRegs[FAST486_REG_ES].Selector,
296 State->SegmentRegs[FAST486_REG_ES].Base,
297 State->SegmentRegs[FAST486_REG_ES].Limit,
298 State->SegmentRegs[FAST486_REG_ES].Dpl,
299 State->SegmentRegs[FAST486_REG_CS].Selector,
300 State->SegmentRegs[FAST486_REG_CS].Base,
301 State->SegmentRegs[FAST486_REG_CS].Limit,
302 State->SegmentRegs[FAST486_REG_CS].Dpl,
303 State->SegmentRegs[FAST486_REG_SS].Selector,
304 State->SegmentRegs[FAST486_REG_SS].Base,
305 State->SegmentRegs[FAST486_REG_SS].Limit,
306 State->SegmentRegs[FAST486_REG_SS].Dpl,
307 State->SegmentRegs[FAST486_REG_DS].Selector,
308 State->SegmentRegs[FAST486_REG_DS].Base,
309 State->SegmentRegs[FAST486_REG_DS].Limit,
310 State->SegmentRegs[FAST486_REG_DS].Dpl,
311 State->SegmentRegs[FAST486_REG_FS].Selector,
312 State->SegmentRegs[FAST486_REG_FS].Base,
313 State->SegmentRegs[FAST486_REG_FS].Limit,
314 State->SegmentRegs[FAST486_REG_FS].Dpl,
315 State->SegmentRegs[FAST486_REG_GS].Selector,
316 State->SegmentRegs[FAST486_REG_GS].Base,
317 State->SegmentRegs[FAST486_REG_GS].Limit,
318 State->SegmentRegs[FAST486_REG_GS].Dpl);
319 DPRINT1("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
320 State->Flags.Long,
321 State->Flags.Cf ? "CF" : "cf",
322 State->Flags.Pf ? "PF" : "pf",
323 State->Flags.Af ? "AF" : "af",
324 State->Flags.Zf ? "ZF" : "zf",
325 State->Flags.Sf ? "SF" : "sf",
326 State->Flags.Tf ? "TF" : "tf",
327 State->Flags.If ? "IF" : "if",
328 State->Flags.Df ? "DF" : "df",
329 State->Flags.Of ? "OF" : "of",
330 State->Flags.Nt ? "NT" : "nt",
331 State->Flags.Rf ? "RF" : "rf",
332 State->Flags.Vm ? "VM" : "vm",
333 State->Flags.Ac ? "AC" : "ac",
334 State->Flags.Vif ? "VIF" : "vif",
335 State->Flags.Vip ? "VIP" : "vip",
336 State->Flags.Iopl);
337 DPRINT1("\nControl Registers:\n"
338 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
339 State->ControlRegisters[FAST486_REG_CR0],
340 State->ControlRegisters[FAST486_REG_CR2],
341 State->ControlRegisters[FAST486_REG_CR3]);
342 DPRINT1("\nDebug Registers:\n"
343 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
344 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
345 State->DebugRegisters[FAST486_REG_DR0],
346 State->DebugRegisters[FAST486_REG_DR1],
347 State->DebugRegisters[FAST486_REG_DR2],
348 State->DebugRegisters[FAST486_REG_DR3],
349 State->DebugRegisters[FAST486_REG_DR4],
350 State->DebugRegisters[FAST486_REG_DR5]);
351 }
352
353 VOID
354 NTAPI
355 Fast486Continue(PFAST486_STATE State)
356 {
357 /* Call the internal function */
358 Fast486ExecutionControl(State, FAST486_CONTINUE);
359 }
360
361 VOID
362 NTAPI
363 Fast486StepInto(PFAST486_STATE State)
364 {
365 /* Call the internal function */
366 Fast486ExecutionControl(State, FAST486_STEP_INTO);
367 }
368
369 VOID
370 NTAPI
371 Fast486StepOver(PFAST486_STATE State)
372 {
373 /* Call the internal function */
374 Fast486ExecutionControl(State, FAST486_STEP_OVER);
375 }
376
377 VOID
378 NTAPI
379 Fast486StepOut(PFAST486_STATE State)
380 {
381 /* Call the internal function */
382 Fast486ExecutionControl(State, FAST486_STEP_OUT);
383 }
384
385 VOID
386 NTAPI
387 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
388 {
389 /* Set the interrupt status and the number */
390 State->IntStatus = FAST486_INT_EXECUTE;
391 State->PendingIntNum = Number;
392 }
393
394 VOID
395 NTAPI
396 Fast486InterruptSignal(PFAST486_STATE State)
397 {
398 /* Set the interrupt status */
399 State->IntStatus = FAST486_INT_SIGNAL;
400 }
401
402 VOID
403 NTAPI
404 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
405 {
406 /* Load the new CS */
407 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
408 {
409 /* An exception occurred, let the handler execute instead */
410 return;
411 }
412
413 /* Set the new IP */
414 State->InstPtr.Long = Offset;
415 }
416
417 VOID
418 NTAPI
419 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
420 {
421 /* Load the new SS */
422 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
423 {
424 /* An exception occurred, let the handler execute instead */
425 return;
426 }
427
428 /* Set the new SP */
429 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
430 }
431
432 VOID
433 NTAPI
434 Fast486SetSegment(PFAST486_STATE State,
435 FAST486_SEG_REGS Segment,
436 USHORT Selector)
437 {
438 /* Call the internal function */
439 Fast486LoadSegment(State, Segment, Selector);
440 }
441
442 /* EOF */