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