[FAST486]
[reactos.git] / reactos / lib / fast486 / fast486.c
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486.c
4 *
5 * Copyright (C) 2014 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 #include "fpu.h"
33
34 /* DEFINES ********************************************************************/
35
36 typedef enum
37 {
38 FAST486_STEP_INTO,
39 FAST486_STEP_OVER,
40 FAST486_STEP_OUT,
41 FAST486_CONTINUE
42 } FAST486_EXEC_CMD;
43
44 /* PRIVATE FUNCTIONS **********************************************************/
45
46 static
47 inline
48 VOID
49 NTAPI
50 Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command)
51 {
52 UCHAR Opcode;
53 INT ProcedureCallCount = 0;
54
55 /* Main execution loop */
56 do
57 {
58 /* Check if this is a new instruction */
59 if (State->PrefixFlags == 0) State->SavedInstPtr = State->InstPtr;
60
61 /* Perform an instruction fetch */
62 if (!Fast486FetchByte(State, &Opcode))
63 {
64 /* Exception occurred */
65 State->PrefixFlags = 0;
66 continue;
67 }
68
69 // TODO: Check for CALL/RET to update ProcedureCallCount.
70
71 if (Fast486OpcodeHandlers[Opcode] != NULL)
72 {
73 /* Call the opcode handler */
74 Fast486OpcodeHandlers[Opcode](State, Opcode);
75 }
76 else
77 {
78 /* This is not a valid opcode */
79 Fast486Exception(State, FAST486_EXCEPTION_UD);
80 }
81
82 if (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix)
83 {
84 /* This is a prefix, go to the next instruction immediately */
85 continue;
86 }
87
88 /* A non-prefix opcode has been executed, reset the prefix flags */
89 State->PrefixFlags = 0;
90
91 /*
92 * Check if there is an interrupt to execute, or a hardware interrupt signal
93 * while interrupts are enabled.
94 */
95 if (State->IntStatus == FAST486_INT_EXECUTE)
96 {
97 /* Perform the interrupt */
98 Fast486PerformInterrupt(State, State->PendingIntNum);
99
100 /* Clear the interrupt status */
101 State->IntStatus = FAST486_INT_NONE;
102 }
103 else if (State->Flags.If
104 && (State->IntAckCallback != NULL)
105 && (State->IntStatus == FAST486_INT_SIGNAL))
106 {
107 /* Acknowledge the interrupt to get the number */
108 State->PendingIntNum = State->IntAckCallback(State);
109
110 /* Set the interrupt status to execute on the next instruction */
111 State->IntStatus = FAST486_INT_EXECUTE;
112 }
113 else if (State->IntStatus == FAST486_INT_DELAYED)
114 {
115 /* Restore the old state */
116 State->IntStatus = FAST486_INT_EXECUTE;
117 }
118 }
119 while ((Command == FAST486_CONTINUE)
120 || (Command == FAST486_STEP_OVER && ProcedureCallCount > 0)
121 || (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0)
122 || (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix));
123 }
124
125 /* DEFAULT CALLBACKS **********************************************************/
126
127 static VOID
128 NTAPI
129 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
130 {
131 UNREFERENCED_PARAMETER(State);
132
133 RtlMoveMemory(Buffer, (PVOID)Address, Size);
134 }
135
136 static VOID
137 NTAPI
138 Fast486MemWriteCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
139 {
140 UNREFERENCED_PARAMETER(State);
141
142 RtlMoveMemory((PVOID)Address, Buffer, Size);
143 }
144
145 static VOID
146 NTAPI
147 Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
148 {
149 UNREFERENCED_PARAMETER(State);
150 UNREFERENCED_PARAMETER(Port);
151 UNREFERENCED_PARAMETER(Buffer);
152 UNREFERENCED_PARAMETER(DataCount);
153 UNREFERENCED_PARAMETER(DataSize);
154 }
155
156 static VOID
157 NTAPI
158 Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
159 {
160 UNREFERENCED_PARAMETER(State);
161 UNREFERENCED_PARAMETER(Port);
162 UNREFERENCED_PARAMETER(Buffer);
163 UNREFERENCED_PARAMETER(DataCount);
164 UNREFERENCED_PARAMETER(DataSize);
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 PULONG Tlb)
205 {
206 /* Set the callbacks (or use default ones if some are NULL) */
207 State->MemReadCallback = (MemReadCallback ? MemReadCallback : Fast486MemReadCallback );
208 State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
209 State->IoReadCallback = (IoReadCallback ? IoReadCallback : Fast486IoReadCallback );
210 State->IoWriteCallback = (IoWriteCallback ? IoWriteCallback : Fast486IoWriteCallback );
211 State->IdleCallback = (IdleCallback ? IdleCallback : Fast486IdleCallback );
212 State->BopCallback = (BopCallback ? BopCallback : Fast486BopCallback );
213 State->IntAckCallback = (IntAckCallback ? IntAckCallback : Fast486IntAckCallback );
214
215 /* Set the TLB (if given) */
216 State->Tlb = Tlb;
217
218 /* Reset the CPU */
219 Fast486Reset(State);
220 }
221
222 VOID
223 NTAPI
224 Fast486Reset(PFAST486_STATE State)
225 {
226 FAST486_SEG_REGS i;
227
228 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
229 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
230 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
231 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
232 FAST486_IDLE_PROC IdleCallback = State->IdleCallback;
233 FAST486_BOP_PROC BopCallback = State->BopCallback;
234 FAST486_INT_ACK_PROC IntAckCallback = State->IntAckCallback;
235 PULONG Tlb = State->Tlb;
236
237 /* Clear the entire structure */
238 RtlZeroMemory(State, sizeof(*State));
239
240 /* Initialize the registers */
241 State->Flags.AlwaysSet = 1;
242 State->InstPtr.LowWord = 0xFFF0;
243
244 /* Set the CPL to 0 */
245 State->Cpl = 0;
246
247 /* Initialize segments */
248 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
249 {
250 State->SegmentRegs[i].Selector = 0;
251 State->SegmentRegs[i].Base = 0;
252 State->SegmentRegs[i].Limit = 0xFFFF;
253 State->SegmentRegs[i].Present = TRUE;
254 State->SegmentRegs[i].ReadWrite = TRUE;
255 State->SegmentRegs[i].Executable = FALSE;
256 State->SegmentRegs[i].DirConf = FALSE;
257 State->SegmentRegs[i].SystemType = 1; // Segment descriptor
258 State->SegmentRegs[i].Dpl = 0;
259 State->SegmentRegs[i].Size = FALSE; // 16-bit
260 }
261
262 /* Initialize the code segment */
263 State->SegmentRegs[FAST486_REG_CS].Executable = TRUE;
264 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
265 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
266
267 /* Initialize the IDT */
268 State->Idtr.Size = 0x3FF;
269 State->Idtr.Address = 0;
270
271 #ifndef FAST486_NO_FPU
272 /* Initialize CR0 */
273 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
274
275 /* Initialize the FPU control and tag registers */
276 State->FpuControl.Value = FAST486_FPU_DEFAULT_CONTROL;
277 State->FpuStatus.Value = 0;
278 State->FpuTag = 0xFFFF;
279 #endif
280
281 /* Restore the callbacks and TLB */
282 State->MemReadCallback = MemReadCallback;
283 State->MemWriteCallback = MemWriteCallback;
284 State->IoReadCallback = IoReadCallback;
285 State->IoWriteCallback = IoWriteCallback;
286 State->IdleCallback = IdleCallback;
287 State->BopCallback = BopCallback;
288 State->IntAckCallback = IntAckCallback;
289 State->Tlb = Tlb;
290 }
291
292 VOID
293 NTAPI
294 Fast486DumpState(PFAST486_STATE State)
295 {
296 DbgPrint("\nFast486DumpState -->\n");
297 DbgPrint("\nCPU currently executing in %s mode at %04X:%08X\n",
298 (State->ControlRegisters[0] & FAST486_CR0_PE) ? "protected" : "real",
299 State->SegmentRegs[FAST486_REG_CS].Selector,
300 State->InstPtr.Long);
301 DbgPrint("\nGeneral purpose registers:\n"
302 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
303 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
304 State->GeneralRegs[FAST486_REG_EAX].Long,
305 State->GeneralRegs[FAST486_REG_ECX].Long,
306 State->GeneralRegs[FAST486_REG_EDX].Long,
307 State->GeneralRegs[FAST486_REG_EBX].Long,
308 State->GeneralRegs[FAST486_REG_ESP].Long,
309 State->GeneralRegs[FAST486_REG_EBP].Long,
310 State->GeneralRegs[FAST486_REG_ESI].Long,
311 State->GeneralRegs[FAST486_REG_EDI].Long);
312 DbgPrint("\nSegment registers:\n"
313 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
314 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
315 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
316 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
317 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
318 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
319 State->SegmentRegs[FAST486_REG_ES].Selector,
320 State->SegmentRegs[FAST486_REG_ES].Base,
321 State->SegmentRegs[FAST486_REG_ES].Limit,
322 State->SegmentRegs[FAST486_REG_ES].Dpl,
323 State->SegmentRegs[FAST486_REG_CS].Selector,
324 State->SegmentRegs[FAST486_REG_CS].Base,
325 State->SegmentRegs[FAST486_REG_CS].Limit,
326 State->SegmentRegs[FAST486_REG_CS].Dpl,
327 State->SegmentRegs[FAST486_REG_SS].Selector,
328 State->SegmentRegs[FAST486_REG_SS].Base,
329 State->SegmentRegs[FAST486_REG_SS].Limit,
330 State->SegmentRegs[FAST486_REG_SS].Dpl,
331 State->SegmentRegs[FAST486_REG_DS].Selector,
332 State->SegmentRegs[FAST486_REG_DS].Base,
333 State->SegmentRegs[FAST486_REG_DS].Limit,
334 State->SegmentRegs[FAST486_REG_DS].Dpl,
335 State->SegmentRegs[FAST486_REG_FS].Selector,
336 State->SegmentRegs[FAST486_REG_FS].Base,
337 State->SegmentRegs[FAST486_REG_FS].Limit,
338 State->SegmentRegs[FAST486_REG_FS].Dpl,
339 State->SegmentRegs[FAST486_REG_GS].Selector,
340 State->SegmentRegs[FAST486_REG_GS].Base,
341 State->SegmentRegs[FAST486_REG_GS].Limit,
342 State->SegmentRegs[FAST486_REG_GS].Dpl);
343 DbgPrint("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
344 State->Flags.Long,
345 State->Flags.Cf ? "CF" : "cf",
346 State->Flags.Pf ? "PF" : "pf",
347 State->Flags.Af ? "AF" : "af",
348 State->Flags.Zf ? "ZF" : "zf",
349 State->Flags.Sf ? "SF" : "sf",
350 State->Flags.Tf ? "TF" : "tf",
351 State->Flags.If ? "IF" : "if",
352 State->Flags.Df ? "DF" : "df",
353 State->Flags.Of ? "OF" : "of",
354 State->Flags.Nt ? "NT" : "nt",
355 State->Flags.Rf ? "RF" : "rf",
356 State->Flags.Vm ? "VM" : "vm",
357 State->Flags.Ac ? "AC" : "ac",
358 State->Flags.Iopl);
359 DbgPrint("\nControl Registers:\n"
360 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
361 State->ControlRegisters[FAST486_REG_CR0],
362 State->ControlRegisters[FAST486_REG_CR2],
363 State->ControlRegisters[FAST486_REG_CR3]);
364 DbgPrint("\nDebug Registers:\n"
365 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
366 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
367 State->DebugRegisters[FAST486_REG_DR0],
368 State->DebugRegisters[FAST486_REG_DR1],
369 State->DebugRegisters[FAST486_REG_DR2],
370 State->DebugRegisters[FAST486_REG_DR3],
371 State->DebugRegisters[FAST486_REG_DR4],
372 State->DebugRegisters[FAST486_REG_DR5]);
373
374 #ifndef FAST486_NO_FPU
375 DbgPrint("\nFPU Registers:\n"
376 "ST0 = %04X%016llX\tST1 = %04X%016llX\n"
377 "ST2 = %04X%016llX\tST3 = %04X%016llX\n"
378 "ST4 = %04X%016llX\tST5 = %04X%016llX\n"
379 "ST6 = %04X%016llX\tST7 = %04X%016llX\n"
380 "Status: %04X\tControl: %04X\tTag: %04X\n",
381 FPU_ST(0).Exponent | ((USHORT)FPU_ST(0).Sign << 15),
382 FPU_ST(0).Mantissa,
383 FPU_ST(1).Exponent | ((USHORT)FPU_ST(1).Sign << 15),
384 FPU_ST(1).Mantissa,
385 FPU_ST(2).Exponent | ((USHORT)FPU_ST(2).Sign << 15),
386 FPU_ST(2).Mantissa,
387 FPU_ST(3).Exponent | ((USHORT)FPU_ST(3).Sign << 15),
388 FPU_ST(3).Mantissa,
389 FPU_ST(4).Exponent | ((USHORT)FPU_ST(4).Sign << 15),
390 FPU_ST(4).Mantissa,
391 FPU_ST(5).Exponent | ((USHORT)FPU_ST(5).Sign << 15),
392 FPU_ST(5).Mantissa,
393 FPU_ST(6).Exponent | ((USHORT)FPU_ST(6).Sign << 15),
394 FPU_ST(6).Mantissa,
395 FPU_ST(7).Exponent | ((USHORT)FPU_ST(7).Sign << 15),
396 FPU_ST(7).Mantissa,
397 State->FpuStatus,
398 State->FpuControl,
399 State->FpuTag);
400 #endif
401
402 DbgPrint("\n<-- Fast486DumpState\n\n");
403 }
404
405 VOID
406 NTAPI
407 Fast486Continue(PFAST486_STATE State)
408 {
409 /* Call the internal function */
410 Fast486ExecutionControl(State, FAST486_CONTINUE);
411 }
412
413 VOID
414 NTAPI
415 Fast486StepInto(PFAST486_STATE State)
416 {
417 /* Call the internal function */
418 Fast486ExecutionControl(State, FAST486_STEP_INTO);
419 }
420
421 VOID
422 NTAPI
423 Fast486StepOver(PFAST486_STATE State)
424 {
425 /* Call the internal function */
426 Fast486ExecutionControl(State, FAST486_STEP_OVER);
427 }
428
429 VOID
430 NTAPI
431 Fast486StepOut(PFAST486_STATE State)
432 {
433 /* Call the internal function */
434 Fast486ExecutionControl(State, FAST486_STEP_OUT);
435 }
436
437 VOID
438 NTAPI
439 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
440 {
441 /* Set the interrupt status and the number */
442 State->IntStatus = FAST486_INT_EXECUTE;
443 State->PendingIntNum = Number;
444 }
445
446 VOID
447 NTAPI
448 Fast486InterruptSignal(PFAST486_STATE State)
449 {
450 /* Set the interrupt status */
451 State->IntStatus = FAST486_INT_SIGNAL;
452 }
453
454 VOID
455 NTAPI
456 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
457 {
458 /* Load the new CS */
459 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
460 {
461 /* An exception occurred, let the handler execute instead */
462 return;
463 }
464
465 /* Set the new IP */
466 State->InstPtr.Long = Offset;
467 }
468
469 VOID
470 NTAPI
471 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
472 {
473 /* Load the new SS */
474 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
475 {
476 /* An exception occurred, let the handler execute instead */
477 return;
478 }
479
480 /* Set the new SP */
481 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
482 }
483
484 VOID
485 NTAPI
486 Fast486SetSegment(PFAST486_STATE State,
487 FAST486_SEG_REGS Segment,
488 USHORT Selector)
489 {
490 /* Call the internal function */
491 Fast486LoadSegment(State, Segment, Selector);
492 }
493
494 /* EOF */