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