[FAST486]
[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 #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 NextInst:
58 if (!State->Halted)
59 {
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->Flags.Tf)
89 {
90 /* No longer halted */
91 State->Halted = FALSE;
92
93 /* Perform the interrupt */
94 Fast486PerformInterrupt(State, 0x01);
95
96 /*
97 * Flags and TF are pushed on stack so we can reset TF now,
98 * to not break into the INT 0x01 handler.
99 * After the INT 0x01 handler returns, the flags and therefore
100 * TF are popped back off the stack and restored, so TF will be
101 * automatically reset to its previous state.
102 */
103 State->Flags.Tf = FALSE;
104 }
105 else if (State->IntStatus == FAST486_INT_EXECUTE)
106 {
107 /* Perform the interrupt */
108 Fast486PerformInterrupt(State, State->PendingIntNum);
109
110 /* Clear the interrupt status */
111 State->IntStatus = FAST486_INT_NONE;
112 }
113 else if (State->Flags.If && (State->IntStatus == FAST486_INT_SIGNAL))
114 {
115 /* Acknowledge the interrupt to get the number */
116 State->PendingIntNum = State->IntAckCallback(State);
117
118 /* Set the interrupt status to execute on the next instruction */
119 State->IntStatus = FAST486_INT_EXECUTE;
120 }
121 else if (State->IntStatus == FAST486_INT_DELAYED)
122 {
123 /* Restore the old state */
124 State->IntStatus = FAST486_INT_EXECUTE;
125 }
126 }
127 while ((Command == FAST486_CONTINUE) ||
128 (Command == FAST486_STEP_OVER && ProcedureCallCount > 0) ||
129 (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0));
130 }
131
132 /* DEFAULT CALLBACKS **********************************************************/
133
134 static VOID
135 NTAPI
136 Fast486MemReadCallback(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
137 {
138 UNREFERENCED_PARAMETER(State);
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 RtlMoveMemory((PVOID)Address, Buffer, Size);
148 }
149
150 static VOID
151 NTAPI
152 Fast486IoReadCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
153 {
154 UNREFERENCED_PARAMETER(State);
155 UNREFERENCED_PARAMETER(Port);
156 UNREFERENCED_PARAMETER(Buffer);
157 UNREFERENCED_PARAMETER(DataCount);
158 UNREFERENCED_PARAMETER(DataSize);
159 }
160
161 static VOID
162 NTAPI
163 Fast486IoWriteCallback(PFAST486_STATE State, ULONG Port, PVOID Buffer, ULONG DataCount, UCHAR DataSize)
164 {
165 UNREFERENCED_PARAMETER(State);
166 UNREFERENCED_PARAMETER(Port);
167 UNREFERENCED_PARAMETER(Buffer);
168 UNREFERENCED_PARAMETER(DataCount);
169 UNREFERENCED_PARAMETER(DataSize);
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... defaulted to single-step interrupt */
187 return 0x01;
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_BOP_PROC BopCallback,
200 FAST486_INT_ACK_PROC IntAckCallback,
201 PULONG Tlb)
202 {
203 /* Set the callbacks (or use default ones if some are NULL) */
204 State->MemReadCallback = (MemReadCallback ? MemReadCallback : Fast486MemReadCallback );
205 State->MemWriteCallback = (MemWriteCallback ? MemWriteCallback : Fast486MemWriteCallback);
206 State->IoReadCallback = (IoReadCallback ? IoReadCallback : Fast486IoReadCallback );
207 State->IoWriteCallback = (IoWriteCallback ? IoWriteCallback : Fast486IoWriteCallback );
208 State->BopCallback = (BopCallback ? BopCallback : Fast486BopCallback );
209 State->IntAckCallback = (IntAckCallback ? IntAckCallback : Fast486IntAckCallback );
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 PULONG Tlb = State->Tlb;
232
233 /* Clear the entire structure */
234 RtlZeroMemory(State, sizeof(*State));
235
236 /* Initialize the registers */
237 State->Flags.AlwaysSet = 1;
238 State->InstPtr.LowWord = 0xFFF0;
239
240 /* Set the CPL to 0 */
241 State->Cpl = 0;
242
243 /* Initialize segments */
244 for (i = 0; i < FAST486_NUM_SEG_REGS; i++)
245 {
246 State->SegmentRegs[i].Selector = 0;
247 State->SegmentRegs[i].Base = 0;
248 State->SegmentRegs[i].Limit = 0xFFFF;
249 State->SegmentRegs[i].Present = TRUE;
250 State->SegmentRegs[i].ReadWrite = TRUE;
251 State->SegmentRegs[i].Executable = FALSE;
252 State->SegmentRegs[i].DirConf = FALSE;
253 State->SegmentRegs[i].SystemType = 1; // Segment descriptor
254 State->SegmentRegs[i].Dpl = 0;
255 State->SegmentRegs[i].Size = FALSE; // 16-bit
256 }
257
258 /* Initialize the code segment */
259 State->SegmentRegs[FAST486_REG_CS].Executable = TRUE;
260 State->SegmentRegs[FAST486_REG_CS].Selector = 0xF000;
261 State->SegmentRegs[FAST486_REG_CS].Base = 0xFFFF0000;
262
263 /* Initialize the IDT */
264 State->Idtr.Size = 0x3FF;
265 State->Idtr.Address = 0;
266
267 #ifndef FAST486_NO_FPU
268 /* Initialize CR0 */
269 State->ControlRegisters[FAST486_REG_CR0] |= FAST486_CR0_ET;
270
271 /* Initialize the FPU control and tag registers */
272 State->FpuControl.Value = FAST486_FPU_DEFAULT_CONTROL;
273 State->FpuStatus.Value = 0;
274 State->FpuTag = 0xFFFF;
275 #endif
276
277 /* Restore the callbacks and TLB */
278 State->MemReadCallback = MemReadCallback;
279 State->MemWriteCallback = MemWriteCallback;
280 State->IoReadCallback = IoReadCallback;
281 State->IoWriteCallback = IoWriteCallback;
282 State->BopCallback = BopCallback;
283 State->IntAckCallback = IntAckCallback;
284 State->Tlb = Tlb;
285 }
286
287 VOID
288 NTAPI
289 Fast486Interrupt(PFAST486_STATE State, UCHAR Number)
290 {
291 /* Set the interrupt status and the number */
292 State->IntStatus = FAST486_INT_EXECUTE;
293 State->PendingIntNum = Number;
294 }
295
296 VOID
297 NTAPI
298 Fast486InterruptSignal(PFAST486_STATE State)
299 {
300 /* Set the interrupt status */
301 State->IntStatus = FAST486_INT_SIGNAL;
302 }
303
304 VOID
305 NTAPI
306 Fast486ExecuteAt(PFAST486_STATE State, USHORT Segment, ULONG Offset)
307 {
308 /* Load the new CS */
309 if (!Fast486LoadSegment(State, FAST486_REG_CS, Segment))
310 {
311 /* An exception occurred, let the handler execute instead */
312 return;
313 }
314
315 /* Set the new IP */
316 State->InstPtr.Long = Offset;
317 }
318
319 VOID
320 NTAPI
321 Fast486SetStack(PFAST486_STATE State, USHORT Segment, ULONG Offset)
322 {
323 /* Load the new SS */
324 if (!Fast486LoadSegment(State, FAST486_REG_SS, Segment))
325 {
326 /* An exception occurred, let the handler execute instead */
327 return;
328 }
329
330 /* Set the new SP */
331 State->GeneralRegs[FAST486_REG_ESP].Long = Offset;
332 }
333
334 VOID
335 NTAPI
336 Fast486SetSegment(PFAST486_STATE State,
337 FAST486_SEG_REGS Segment,
338 USHORT Selector)
339 {
340 /* Call the internal function */
341 Fast486LoadSegment(State, Segment, Selector);
342 }
343
344 /* EOF */