0ad6988b51b3334c420655448d45d866c0128fdc
[reactos.git] / reactos / lib / fast486 / fast486.c
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486.c
4 *
5 * Copyright (C) 2015 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 #include "fpu.h"
33
34 /* DEFINES ********************************************************************/
35
36 typedef enum
37 {
38 FAST486_STEP_INTO,
39 FAST486_STEP_OVER,
40 FAST486_STEP_OUT,
41 FAST486_CONTINUE
42 } FAST486_EXEC_CMD;
43
44 /* PRIVATE FUNCTIONS **********************************************************/
45
46 VOID
47 NTAPI
48 Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command)
49 {
50 UCHAR Opcode;
51 FAST486_OPCODE_HANDLER_PROC CurrentHandler;
52 INT ProcedureCallCount = 0;
53
54 /* Main execution loop */
55 do
56 {
57 if (!State->Halted)
58 {
59 NextInst:
60 /* Check if this is a new instruction */
61 if (State->PrefixFlags == 0) State->SavedInstPtr = State->InstPtr;
62
63 /* Perform an instruction fetch */
64 if (!Fast486FetchByte(State, &Opcode))
65 {
66 /* Exception occurred */
67 State->PrefixFlags = 0;
68 continue;
69 }
70
71 // TODO: Check for CALL/RET to update ProcedureCallCount.
72
73 /* Call the opcode handler */
74 CurrentHandler = Fast486OpcodeHandlers[Opcode];
75 CurrentHandler(State, Opcode);
76
77 /* If this is a prefix, go to the next instruction immediately */
78 if (CurrentHandler == Fast486OpcodePrefix) goto NextInst;
79
80 /* A non-prefix opcode has been executed, reset the prefix flags */
81 State->PrefixFlags = 0;
82 }
83
84 /*
85 * Check if there is an interrupt to execute, or a hardware interrupt signal
86 * while interrupts are enabled.
87 */
88 if (State->DoNotInterrupt)
89 {
90 /* Clear the interrupt delay flag */
91 State->DoNotInterrupt = FALSE;
92 }
93 else if (State->Flags.Tf && !State->Halted)
94 {
95 /* Perform the interrupt */
96 Fast486PerformInterrupt(State, 0x01);
97
98 /*
99 * Flags and TF are pushed on stack so we can reset TF now,
100 * to not break into the INT 0x01 handler.
101 * After the INT 0x01 handler returns, the flags and therefore
102 * TF are popped back off the stack and restored, so TF will be
103 * automatically reset to its previous state.
104 */
105 State->Flags.Tf = FALSE;
106 }
107 else if (State->Flags.If && State->IntSignaled)
108 {
109 /* No longer halted */
110 State->Halted = FALSE;
111
112 /* Acknowledge the interrupt and perform it */
113 Fast486PerformInterrupt(State, State->IntAckCallback(State));
114
115 /* Clear the interrupt status */
116 State->IntSignaled = FALSE;
117 }
118 }
119 while ((Command == FAST486_CONTINUE) ||
120 (Command == FAST486_STEP_OVER && ProcedureCallCount > 0) ||
121 (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0));
122 }
123
124 /* DEFAULT CALLBACKS **********************************************************/
125
126 static VOID
127 NTAPI
128 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
129 {
130 UNREFERENCED_PARAMETER(State);
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 RtlMoveMemory((PVOID)Address, Buffer, Size);
140 }
141
142 static VOID
143 NTAPI
144 Fast486IoReadCallback(PFAST486_STATE State, USHORT Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
145 {
146 UNREFERENCED_PARAMETER(State);
147 UNREFERENCED_PARAMETER(Port);
148 UNREFERENCED_PARAMETER(Buffer);
149 UNREFERENCED_PARAMETER(DataCount);
150 UNREFERENCED_PARAMETER(DataSize);
151 }
152
153 static VOID
154 NTAPI
155 Fast486IoWriteCallback(PFAST486_STATE State, USHORT 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 Fast486BopCallback(PFAST486_STATE State, UCHAR BopCode)
167 {
168 UNREFERENCED_PARAMETER(State);
169 UNREFERENCED_PARAMETER(BopCode);
170 }
171
172 static UCHAR
173 NTAPI
174 Fast486IntAckCallback(PFAST486_STATE State)
175 {
176 UNREFERENCED_PARAMETER(State);
177
178 /* Return something... defaulted to single-step interrupt */
179 return 0x01;
180 }
181
182 static VOID NTAPI
183 Fast486FpuCallback(PFAST486_STATE State)
184 {
185 UNREFERENCED_PARAMETER(State);
186 }
187
188 /* PUBLIC FUNCTIONS ***********************************************************/
189
190 VOID
191 NTAPI
192 Fast486Initialize(PFAST486_STATE State,
193 FAST486_MEM_READ_PROC MemReadCallback,
194 FAST486_MEM_WRITE_PROC MemWriteCallback,
195 FAST486_IO_READ_PROC IoReadCallback,
196 FAST486_IO_WRITE_PROC IoWriteCallback,
197 FAST486_BOP_PROC BopCallback,
198 FAST486_INT_ACK_PROC IntAckCallback,
199 FAST486_FPU_PROC FpuCallback,
200 PULONG Tlb)
201 {
202 /* Set the callbacks (or use default ones if some are NULL) */
203 State->MemReadCallback = (MemReadCallback ? MemReadCallback : Fast486MemReadCallback );
204 State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
205 State->IoReadCallback = (IoReadCallback ? IoReadCallback : Fast486IoReadCallback );
206 State->IoWriteCallback = (IoWriteCallback ? IoWriteCallback : Fast486IoWriteCallback );
207 State->BopCallback = (BopCallback ? BopCallback : Fast486BopCallback );
208 State->IntAckCallback = (IntAckCallback ? IntAckCallback : Fast486IntAckCallback );
209 State->FpuCallback = (FpuCallback ? FpuCallback : Fast486FpuCallback );
210
211 /* Set the TLB (if given) */
212 State->Tlb = Tlb;
213
214 /* Reset the CPU */
215 Fast486Reset(State);
216 }
217
218 VOID
219 NTAPI
220 Fast486Reset(PFAST486_STATE State)
221 {
222 FAST486_SEG_REGS i;
223
224 /* Save the callbacks and TLB */
225 FAST486_MEM_READ_PROC MemReadCallback = State->MemReadCallback;
226 FAST486_MEM_WRITE_PROC MemWriteCallback = State->MemWriteCallback;
227 FAST486_IO_READ_PROC IoReadCallback = State->IoReadCallback;
228 FAST486_IO_WRITE_PROC IoWriteCallback = State->IoWriteCallback;
229 FAST486_BOP_PROC BopCallback = State->BopCallback;
230 FAST486_INT_ACK_PROC IntAckCallback = State->IntAckCallback;
231 FAST486_FPU_PROC FpuCallback = State->FpuCallback;
232 PULONG Tlb = State->Tlb;
233
234 /* Clear the entire structure */
235 RtlZeroMemory(State, sizeof(*State));
236
237 /* Initialize the registers */
238 State->Flags.AlwaysSet = 1;
239 State->InstPtr.LowWord = 0xFFF0;
240
241 /* Set the CPL to 0 */
242 State->Cpl = 0;
243
244 /* Initialize segments */
245 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
246 {
247 State->SegmentRegs[i].Selector = 0;
248 State->SegmentRegs[i].Base = 0;
249 State->SegmentRegs[i].Limit = 0xFFFF;
250 State->SegmentRegs[i].Present = TRUE;
251 State->SegmentRegs[i].ReadWrite = TRUE;
252 State->SegmentRegs[i].Executable = FALSE;
253 State->SegmentRegs[i].DirConf = FALSE;
254 State->SegmentRegs[i].SystemType = 1; // Segment descriptor
255 State->SegmentRegs[i].Dpl = 0;
256 State->SegmentRegs[i].Size = FALSE; // 16-bit
257 }
258
259 /* Initialize the code segment */
260 State->SegmentRegs[FAST486_REG_CS].Executable = TRUE;
261 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
262 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
263
264 /* Initialize the IDT */
265 State->Idtr.Size = 0x3FF;
266 State->Idtr.Address = 0;
267
268 #ifndef FAST486_NO_FPU
269 /* Initialize CR0 */
270 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
271
272 /* Initialize the FPU control and tag registers */
273 State->FpuControl.Value = FAST486_FPU_DEFAULT_CONTROL;
274 State->FpuStatus.Value = 0;
275 State->FpuTag = 0xFFFF;
276 #endif
277
278 /* Restore the callbacks and TLB */
279 State->MemReadCallback = MemReadCallback;
280 State->MemWriteCallback = MemWriteCallback;
281 State->IoReadCallback = IoReadCallback;
282 State->IoWriteCallback = IoWriteCallback;
283 State->BopCallback = BopCallback;
284 State->IntAckCallback = IntAckCallback;
285 State->FpuCallback = FpuCallback;
286 State->Tlb = Tlb;
287
288 /* Flush the TLB */
289 Fast486FlushTlb(State);
290 }
291
292 VOID
293 NTAPI
294 Fast486InterruptSignal(PFAST486_STATE State)
295 {
296 State->IntSignaled = TRUE;
297 }
298
299 VOID
300 NTAPI
301 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
302 {
303 /* Load the new CS */
304 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
305 {
306 /* An exception occurred, let the handler execute instead */
307 return;
308 }
309
310 /* Set the new IP */
311 State->InstPtr.Long = Offset;
312 }
313
314 VOID
315 NTAPI
316 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
317 {
318 /* Load the new SS */
319 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
320 {
321 /* An exception occurred, let the handler execute instead */
322 return;
323 }
324
325 /* Set the new SP */
326 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
327 }
328
329 VOID
330 NTAPI
331 Fast486SetSegment(PFAST486_STATE State,
332 FAST486_SEG_REGS Segment,
333 USHORT Selector)
334 {
335 /* Call the internal function */
336 Fast486LoadSegment(State, Segment, Selector);
337 }
338
339 VOID
340 NTAPI
341 Fast486Rewind(PFAST486_STATE State)
342 {
343 /* This function is used when an instruction has been interrupted remotely */
344 State->PrefixFlags = 0;
345 State->InstPtr.Long = State->SavedInstPtr.Long;
346
347 #ifndef FAST486_NO_PREFETCH
348 State->PrefetchValid = FALSE;
349 #endif
350 }
351
352 /* EOF */