2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2014 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
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.
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.
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.
22 /* INCLUDES *******************************************************************/
34 /* DEFINES ********************************************************************/
44 /* PRIVATE FUNCTIONS **********************************************************/
48 Fast486ExecutionControl(PFAST486_STATE State
, FAST486_EXEC_CMD Command
)
51 FAST486_OPCODE_HANDLER_PROC CurrentHandler
;
52 INT ProcedureCallCount
= 0;
54 /* Main execution loop */
60 /* Check if this is a new instruction */
61 if (State
->PrefixFlags
== 0) State
->SavedInstPtr
= State
->InstPtr
;
63 /* Perform an instruction fetch */
64 if (!Fast486FetchByte(State
, &Opcode
))
66 /* Exception occurred */
67 State
->PrefixFlags
= 0;
71 // TODO: Check for CALL/RET to update ProcedureCallCount.
73 /* Call the opcode handler */
74 CurrentHandler
= Fast486OpcodeHandlers
[Opcode
];
75 CurrentHandler(State
, Opcode
);
77 /* If this is a prefix, go to the next instruction immediately */
78 if (CurrentHandler
== Fast486OpcodePrefix
) goto NextInst
;
80 /* A non-prefix opcode has been executed, reset the prefix flags */
81 State
->PrefixFlags
= 0;
85 * Check if there is an interrupt to execute, or a hardware interrupt signal
86 * while interrupts are enabled.
88 if (State
->Flags
.Tf
&& !State
->Halted
)
90 /* Perform the interrupt */
91 Fast486PerformInterrupt(State
, 0x01);
94 * Flags and TF are pushed on stack so we can reset TF now,
95 * to not break into the INT 0x01 handler.
96 * After the INT 0x01 handler returns, the flags and therefore
97 * TF are popped back off the stack and restored, so TF will be
98 * automatically reset to its previous state.
100 State
->Flags
.Tf
= FALSE
;
102 else if (State
->IntStatus
== FAST486_INT_EXECUTE
)
104 /* No longer halted */
105 State
->Halted
= FALSE
;
107 /* Perform the interrupt */
108 Fast486PerformInterrupt(State
, State
->PendingIntNum
);
110 /* Clear the interrupt status */
111 State
->IntStatus
= FAST486_INT_NONE
;
113 else if (State
->Flags
.If
&& (State
->IntStatus
== FAST486_INT_SIGNAL
))
115 /* Acknowledge the interrupt to get the number */
116 State
->PendingIntNum
= State
->IntAckCallback(State
);
118 /* Set the interrupt status to execute on the next instruction */
119 State
->IntStatus
= FAST486_INT_EXECUTE
;
121 else if (State
->IntStatus
== FAST486_INT_DELAYED
)
123 /* Restore the old state */
124 State
->IntStatus
= FAST486_INT_EXECUTE
;
127 while ((Command
== FAST486_CONTINUE
) ||
128 (Command
== FAST486_STEP_OVER
&& ProcedureCallCount
> 0) ||
129 (Command
== FAST486_STEP_OUT
&& ProcedureCallCount
>= 0));
132 /* DEFAULT CALLBACKS **********************************************************/
136 Fast486MemReadCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
138 UNREFERENCED_PARAMETER(State
);
139 RtlMoveMemory(Buffer
, (PVOID
)Address
, Size
);
144 Fast486MemWriteCallback(PFAST486_STATE State
, ULONG Address
, PVOID Buffer
, ULONG Size
)
146 UNREFERENCED_PARAMETER(State
);
147 RtlMoveMemory((PVOID
)Address
, Buffer
, Size
);
152 Fast486IoReadCallback(PFAST486_STATE State
, USHORT Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
154 UNREFERENCED_PARAMETER(State
);
155 UNREFERENCED_PARAMETER(Port
);
156 UNREFERENCED_PARAMETER(Buffer
);
157 UNREFERENCED_PARAMETER(DataCount
);
158 UNREFERENCED_PARAMETER(DataSize
);
163 Fast486IoWriteCallback(PFAST486_STATE State
, USHORT Port
, PVOID Buffer
, ULONG DataCount
, UCHAR DataSize
)
165 UNREFERENCED_PARAMETER(State
);
166 UNREFERENCED_PARAMETER(Port
);
167 UNREFERENCED_PARAMETER(Buffer
);
168 UNREFERENCED_PARAMETER(DataCount
);
169 UNREFERENCED_PARAMETER(DataSize
);
174 Fast486BopCallback(PFAST486_STATE State
, UCHAR BopCode
)
176 UNREFERENCED_PARAMETER(State
);
177 UNREFERENCED_PARAMETER(BopCode
);
182 Fast486IntAckCallback(PFAST486_STATE State
)
184 UNREFERENCED_PARAMETER(State
);
186 /* Return something... defaulted to single-step interrupt */
190 /* PUBLIC FUNCTIONS ***********************************************************/
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
,
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
);
211 /* Set the TLB (if given) */
220 Fast486Reset(PFAST486_STATE State
)
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
;
233 /* Clear the entire structure */
234 RtlZeroMemory(State
, sizeof(*State
));
236 /* Initialize the registers */
237 State
->Flags
.AlwaysSet
= 1;
238 State
->InstPtr
.LowWord
= 0xFFF0;
240 /* Set the CPL to 0 */
243 /* Initialize segments */
244 for (i
= 0; i
< FAST486_NUM_SEG_REGS
; i
++)
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
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;
263 /* Initialize the IDT */
264 State
->Idtr
.Size
= 0x3FF;
265 State
->Idtr
.Address
= 0;
267 #ifndef FAST486_NO_FPU
269 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_ET
;
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;
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
;
289 Fast486Interrupt(PFAST486_STATE State
, UCHAR Number
)
291 /* Set the interrupt status and the number */
292 State
->IntStatus
= FAST486_INT_EXECUTE
;
293 State
->PendingIntNum
= Number
;
298 Fast486InterruptSignal(PFAST486_STATE State
)
300 /* Set the interrupt status */
301 State
->IntStatus
= FAST486_INT_SIGNAL
;
306 Fast486ExecuteAt(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
308 /* Load the new CS */
309 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Segment
))
311 /* An exception occurred, let the handler execute instead */
316 State
->InstPtr
.Long
= Offset
;
321 Fast486SetStack(PFAST486_STATE State
, USHORT Segment
, ULONG Offset
)
323 /* Load the new SS */
324 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Segment
))
326 /* An exception occurred, let the handler execute instead */
331 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Offset
;
336 Fast486SetSegment(PFAST486_STATE State
,
337 FAST486_SEG_REGS Segment
,
340 /* Call the internal function */
341 Fast486LoadSegment(State
, Segment
, Selector
);