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