c47814bb52622edcf7b0fe6e9f38240bafe3f2c9
[reactos.git] / reactos / 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, 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 }
125 while ((Command == FAST486_CONTINUE)
126 || (Command == FAST486_STEP_OVER && ProcedureCallCount > 0)
127 || (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0)
128 || (Fast486OpcodeHandlers[Opcode] == Fast486OpcodePrefix));
129 }
130
131 /* DEFAULT CALLBACKS **********************************************************/
132
133 static VOID
134 NTAPI
135 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
136 {
137 UNREFERENCED_PARAMETER(State);
138
139 RtlMoveMemory(Buffer, (PVOID)Address, Size);
140 }
141
142 static VOID
143 NTAPI
144 Fast486MemWriteCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
145 {
146 UNREFERENCED_PARAMETER(State);
147
148 RtlMoveMemory((PVOID)Address, Buffer, Size);
149 }
150
151 static VOID
152 NTAPI
153 Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
154 {
155 UNREFERENCED_PARAMETER(State);
156 UNREFERENCED_PARAMETER(Port);
157 UNREFERENCED_PARAMETER(Buffer);
158 UNREFERENCED_PARAMETER(DataCount);
159 UNREFERENCED_PARAMETER(DataSize);
160 }
161
162 static VOID
163 NTAPI
164 Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
165 {
166 UNREFERENCED_PARAMETER(State);
167 UNREFERENCED_PARAMETER(Port);
168 UNREFERENCED_PARAMETER(Buffer);
169 UNREFERENCED_PARAMETER(DataCount);
170 UNREFERENCED_PARAMETER(DataSize);
171 }
172
173 static VOID
174 NTAPI
175 Fast486IdleCallback(PFAST486_STATE State)
176 {
177 UNREFERENCED_PARAMETER(State);
178 }
179
180 static VOID
181 NTAPI
182 Fast486BopCallback(PFAST486_STATE State, UCHAR BopCode)
183 {
184 UNREFERENCED_PARAMETER(State);
185 UNREFERENCED_PARAMETER(BopCode);
186 }
187
188 static UCHAR
189 NTAPI
190 Fast486IntAckCallback(PFAST486_STATE State)
191 {
192 UNREFERENCED_PARAMETER(State);
193
194 /* Return something... */
195 return 0;
196 }
197
198 /* PUBLIC FUNCTIONS ***********************************************************/
199
200 VOID
201 NTAPI
202 Fast486Initialize(PFAST486_STATE State,
203 FAST486_MEM_READ_PROC MemReadCallback,
204 FAST486_MEM_WRITE_PROC MemWriteCallback,
205 FAST486_IO_READ_PROC IoReadCallback,
206 FAST486_IO_WRITE_PROC IoWriteCallback,
207 FAST486_IDLE_PROC IdleCallback,
208 FAST486_BOP_PROC BopCallback,
209 FAST486_INT_ACK_PROC IntAckCallback,
210 PULONG Tlb)
211 {
212 /* Set the callbacks (or use default ones if some are NULL) */
213 State->MemReadCallback = (MemReadCallback ? MemReadCallback : Fast486MemReadCallback );
214 State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
215 State->IoReadCallback = (IoReadCallback ? IoReadCallback : Fast486IoReadCallback );
216 State->IoWriteCallback = (IoWriteCallback ? IoWriteCallback : Fast486IoWriteCallback );
217 State->IdleCallback = (IdleCallback ? IdleCallback : Fast486IdleCallback );
218 State->BopCallback = (BopCallback ? BopCallback : Fast486BopCallback );
219 State->IntAckCallback = (IntAckCallback ? IntAckCallback : Fast486IntAckCallback );
220
221 /* Set the TLB (if given) */
222 State->Tlb = Tlb;
223
224 /* Reset the CPU */
225 Fast486Reset(State);
226 }
227
228 VOID
229 NTAPI
230 Fast486Reset(PFAST486_STATE State)
231 {
232 FAST486_SEG_REGS i;
233
234 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
235 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
236 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
237 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
238 FAST486_IDLE_PROC IdleCallback = State->IdleCallback;
239 FAST486_BOP_PROC BopCallback = State->BopCallback;
240 FAST486_INT_ACK_PROC IntAckCallback = State->IntAckCallback;
241 PULONG Tlb = State->Tlb;
242
243 /* Clear the entire structure */
244 RtlZeroMemory(State, sizeof(*State));
245
246 /* Initialize the registers */
247 State->Flags.AlwaysSet = 1;
248 State->InstPtr.LowWord = 0xFFF0;
249
250 /* Set the CPL to 0 */
251 State->Cpl = 0;
252
253 /* Initialize segments */
254 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
255 {
256 State->SegmentRegs[i].Selector = 0;
257 State->SegmentRegs[i].Base = 0;
258 State->SegmentRegs[i].Limit = 0xFFFF;
259 State->SegmentRegs[i].Present = TRUE;
260 State->SegmentRegs[i].ReadWrite = TRUE;
261 State->SegmentRegs[i].Executable = FALSE;
262 State->SegmentRegs[i].DirConf = FALSE;
263 State->SegmentRegs[i].SystemType = 1; // Segment descriptor
264 State->SegmentRegs[i].Dpl = 0;
265 State->SegmentRegs[i].Size = FALSE; // 16-bit
266 }
267
268 /* Initialize the code segment */
269 State->SegmentRegs[FAST486_REG_CS].Executable = TRUE;
270 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
271 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
272
273 /* Initialize the IDT */
274 State->Idtr.Size = 0x3FF;
275 State->Idtr.Address = 0;
276
277 #ifndef FAST486_NO_FPU
278 /* Initialize CR0 */
279 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
280 #endif
281
282 /* Restore the callbacks and TLB */
283 State->MemReadCallback = MemReadCallback;
284 State->MemWriteCallback = MemWriteCallback;
285 State->IoReadCallback = IoReadCallback;
286 State->IoWriteCallback = IoWriteCallback;
287 State->IdleCallback = IdleCallback;
288 State->BopCallback = BopCallback;
289 State->IntAckCallback = IntAckCallback;
290 State->Tlb = Tlb;
291 }
292
293 VOID
294 NTAPI
295 Fast486DumpState(PFAST486_STATE State)
296 {
297 DPRINT1("\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 DPRINT1("\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 DPRINT1("\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 DPRINT1("\nFlags: %08X (%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.Iopl);
358 DPRINT1("\nControl Registers:\n"
359 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
360 State->ControlRegisters[FAST486_REG_CR0],
361 State->ControlRegisters[FAST486_REG_CR2],
362 State->ControlRegisters[FAST486_REG_CR3]);
363 DPRINT1("\nDebug Registers:\n"
364 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
365 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
366 State->DebugRegisters[FAST486_REG_DR0],
367 State->DebugRegisters[FAST486_REG_DR1],
368 State->DebugRegisters[FAST486_REG_DR2],
369 State->DebugRegisters[FAST486_REG_DR3],
370 State->DebugRegisters[FAST486_REG_DR4],
371 State->DebugRegisters[FAST486_REG_DR5]);
372 }
373
374 VOID
375 NTAPI
376 Fast486Continue(PFAST486_STATE State)
377 {
378 /* Call the internal function */
379 Fast486ExecutionControl(State, FAST486_CONTINUE);
380 }
381
382 VOID
383 NTAPI
384 Fast486StepInto(PFAST486_STATE State)
385 {
386 /* Call the internal function */
387 Fast486ExecutionControl(State, FAST486_STEP_INTO);
388 }
389
390 VOID
391 NTAPI
392 Fast486StepOver(PFAST486_STATE State)
393 {
394 /* Call the internal function */
395 Fast486ExecutionControl(State, FAST486_STEP_OVER);
396 }
397
398 VOID
399 NTAPI
400 Fast486StepOut(PFAST486_STATE State)
401 {
402 /* Call the internal function */
403 Fast486ExecutionControl(State, FAST486_STEP_OUT);
404 }
405
406 VOID
407 NTAPI
408 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
409 {
410 /* Set the interrupt status and the number */
411 State->IntStatus = FAST486_INT_EXECUTE;
412 State->PendingIntNum = Number;
413 }
414
415 VOID
416 NTAPI
417 Fast486InterruptSignal(PFAST486_STATE State)
418 {
419 /* Set the interrupt status */
420 State->IntStatus = FAST486_INT_SIGNAL;
421 }
422
423 VOID
424 NTAPI
425 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
426 {
427 /* Load the new CS */
428 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
429 {
430 /* An exception occurred, let the handler execute instead */
431 return;
432 }
433
434 /* Set the new IP */
435 State->InstPtr.Long = Offset;
436 }
437
438 VOID
439 NTAPI
440 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
441 {
442 /* Load the new SS */
443 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
444 {
445 /* An exception occurred, let the handler execute instead */
446 return;
447 }
448
449 /* Set the new SP */
450 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
451 }
452
453 VOID
454 NTAPI
455 Fast486SetSegment(PFAST486_STATE State,
456 FAST486_SEG_REGS Segment,
457 USHORT Selector)
458 {
459 /* Call the internal function */
460 Fast486LoadSegment(State, Segment, Selector);
461 }
462
463 /* EOF */