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