[FAST486]
[reactos.git] / reactos / lib / fast486 / fast486dbg.c
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * fast486dbg.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 #if 0
47 static inline VOID
48 NTAPI
49 Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command)
50 {
51 UCHAR Opcode;
52 FAST486_OPCODE_HANDLER_PROC CurrentHandler;
53 INT ProcedureCallCount = 0;
54
55 /* Main execution loop */
56 do
57 {
58 NextInst:
59 /* Check if this is a new instruction */
60 if (State->PrefixFlags == 0) State->SavedInstPtr = State->InstPtr;
61
62 /* Perform an instruction fetch */
63 if (!Fast486FetchByte(State, &Opcode))
64 {
65 /* Exception occurred */
66 State->PrefixFlags = 0;
67 continue;
68 }
69
70 // TODO: Check for CALL/RET to update ProcedureCallCount.
71
72 /* Call the opcode handler */
73 CurrentHandler = Fast486OpcodeHandlers[Opcode];
74 CurrentHandler(State, Opcode);
75
76 /* If this is a prefix, go to the next instruction immediately */
77 if (CurrentHandler == Fast486OpcodePrefix) goto NextInst;
78
79 /* A non-prefix opcode has been executed, reset the prefix flags */
80 State->PrefixFlags = 0;
81
82 /*
83 * Check if there is an interrupt to execute, or a hardware interrupt signal
84 * while interrupts are enabled.
85 */
86 if (State->IntStatus == FAST486_INT_EXECUTE)
87 {
88 /* Perform the interrupt */
89 Fast486PerformInterrupt(State, State->PendingIntNum);
90
91 /* Clear the interrupt status */
92 State->IntStatus = FAST486_INT_NONE;
93 }
94 else if (State->Flags.If && (State->IntStatus == FAST486_INT_SIGNAL)
95 && (State->IntAckCallback != NULL))
96 {
97 /* Acknowledge the interrupt to get the number */
98 State->PendingIntNum = State->IntAckCallback(State);
99
100 /* Set the interrupt status to execute on the next instruction */
101 State->IntStatus = FAST486_INT_EXECUTE;
102 }
103 else if (State->IntStatus == FAST486_INT_DELAYED)
104 {
105 /* Restore the old state */
106 State->IntStatus = FAST486_INT_EXECUTE;
107 }
108 }
109 while ((Command == FAST486_CONTINUE) ||
110 (Command == FAST486_STEP_OVER && ProcedureCallCount > 0) ||
111 (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0));
112 }
113 #else
114 VOID
115 NTAPI
116 Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command);
117 #endif
118
119 /* PUBLIC FUNCTIONS ***********************************************************/
120
121 VOID
122 NTAPI
123 Fast486DumpState(PFAST486_STATE State)
124 {
125 DbgPrint("\nFast486DumpState -->\n");
126 DbgPrint("\nCPU currently executing in %s mode at %04X:%08X\n",
127 (State->ControlRegisters[0] & FAST486_CR0_PE) ? "protected" : "real",
128 State->SegmentRegs[FAST486_REG_CS].Selector,
129 State->InstPtr.Long);
130 DbgPrint("\nGeneral purpose registers:\n"
131 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
132 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
133 State->GeneralRegs[FAST486_REG_EAX].Long,
134 State->GeneralRegs[FAST486_REG_ECX].Long,
135 State->GeneralRegs[FAST486_REG_EDX].Long,
136 State->GeneralRegs[FAST486_REG_EBX].Long,
137 State->GeneralRegs[FAST486_REG_ESP].Long,
138 State->GeneralRegs[FAST486_REG_EBP].Long,
139 State->GeneralRegs[FAST486_REG_ESI].Long,
140 State->GeneralRegs[FAST486_REG_EDI].Long);
141 DbgPrint("\nSegment registers:\n"
142 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
143 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
144 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
145 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
146 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
147 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
148 State->SegmentRegs[FAST486_REG_ES].Selector,
149 State->SegmentRegs[FAST486_REG_ES].Base,
150 State->SegmentRegs[FAST486_REG_ES].Limit,
151 State->SegmentRegs[FAST486_REG_ES].Dpl,
152 State->SegmentRegs[FAST486_REG_CS].Selector,
153 State->SegmentRegs[FAST486_REG_CS].Base,
154 State->SegmentRegs[FAST486_REG_CS].Limit,
155 State->SegmentRegs[FAST486_REG_CS].Dpl,
156 State->SegmentRegs[FAST486_REG_SS].Selector,
157 State->SegmentRegs[FAST486_REG_SS].Base,
158 State->SegmentRegs[FAST486_REG_SS].Limit,
159 State->SegmentRegs[FAST486_REG_SS].Dpl,
160 State->SegmentRegs[FAST486_REG_DS].Selector,
161 State->SegmentRegs[FAST486_REG_DS].Base,
162 State->SegmentRegs[FAST486_REG_DS].Limit,
163 State->SegmentRegs[FAST486_REG_DS].Dpl,
164 State->SegmentRegs[FAST486_REG_FS].Selector,
165 State->SegmentRegs[FAST486_REG_FS].Base,
166 State->SegmentRegs[FAST486_REG_FS].Limit,
167 State->SegmentRegs[FAST486_REG_FS].Dpl,
168 State->SegmentRegs[FAST486_REG_GS].Selector,
169 State->SegmentRegs[FAST486_REG_GS].Base,
170 State->SegmentRegs[FAST486_REG_GS].Limit,
171 State->SegmentRegs[FAST486_REG_GS].Dpl);
172 DbgPrint("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
173 State->Flags.Long,
174 State->Flags.Cf ? "CF" : "cf",
175 State->Flags.Pf ? "PF" : "pf",
176 State->Flags.Af ? "AF" : "af",
177 State->Flags.Zf ? "ZF" : "zf",
178 State->Flags.Sf ? "SF" : "sf",
179 State->Flags.Tf ? "TF" : "tf",
180 State->Flags.If ? "IF" : "if",
181 State->Flags.Df ? "DF" : "df",
182 State->Flags.Of ? "OF" : "of",
183 State->Flags.Nt ? "NT" : "nt",
184 State->Flags.Rf ? "RF" : "rf",
185 State->Flags.Vm ? "VM" : "vm",
186 State->Flags.Ac ? "AC" : "ac",
187 State->Flags.Iopl);
188 DbgPrint("\nControl Registers:\n"
189 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
190 State->ControlRegisters[FAST486_REG_CR0],
191 State->ControlRegisters[FAST486_REG_CR2],
192 State->ControlRegisters[FAST486_REG_CR3]);
193 DbgPrint("\nDebug Registers:\n"
194 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
195 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
196 State->DebugRegisters[FAST486_REG_DR0],
197 State->DebugRegisters[FAST486_REG_DR1],
198 State->DebugRegisters[FAST486_REG_DR2],
199 State->DebugRegisters[FAST486_REG_DR3],
200 State->DebugRegisters[FAST486_REG_DR4],
201 State->DebugRegisters[FAST486_REG_DR5]);
202
203 #ifndef FAST486_NO_FPU
204 DbgPrint("\nFPU Registers:\n"
205 "ST0 = %04X%016llX\tST1 = %04X%016llX\n"
206 "ST2 = %04X%016llX\tST3 = %04X%016llX\n"
207 "ST4 = %04X%016llX\tST5 = %04X%016llX\n"
208 "ST6 = %04X%016llX\tST7 = %04X%016llX\n"
209 "Status: %04X\tControl: %04X\tTag: %04X\n",
210 FPU_ST(0).Exponent | ((USHORT)FPU_ST(0).Sign << 15),
211 FPU_ST(0).Mantissa,
212 FPU_ST(1).Exponent | ((USHORT)FPU_ST(1).Sign << 15),
213 FPU_ST(1).Mantissa,
214 FPU_ST(2).Exponent | ((USHORT)FPU_ST(2).Sign << 15),
215 FPU_ST(2).Mantissa,
216 FPU_ST(3).Exponent | ((USHORT)FPU_ST(3).Sign << 15),
217 FPU_ST(3).Mantissa,
218 FPU_ST(4).Exponent | ((USHORT)FPU_ST(4).Sign << 15),
219 FPU_ST(4).Mantissa,
220 FPU_ST(5).Exponent | ((USHORT)FPU_ST(5).Sign << 15),
221 FPU_ST(5).Mantissa,
222 FPU_ST(6).Exponent | ((USHORT)FPU_ST(6).Sign << 15),
223 FPU_ST(6).Mantissa,
224 FPU_ST(7).Exponent | ((USHORT)FPU_ST(7).Sign << 15),
225 FPU_ST(7).Mantissa,
226 State->FpuStatus,
227 State->FpuControl,
228 State->FpuTag);
229 #endif
230
231 DbgPrint("\n<-- Fast486DumpState\n\n");
232 }
233
234 VOID
235 NTAPI
236 Fast486Continue(PFAST486_STATE State)
237 {
238 /* Call the internal function */
239 Fast486ExecutionControl(State, FAST486_CONTINUE);
240 }
241
242 VOID
243 NTAPI
244 Fast486StepInto(PFAST486_STATE State)
245 {
246 /* Call the internal function */
247 Fast486ExecutionControl(State, FAST486_STEP_INTO);
248 }
249
250 VOID
251 NTAPI
252 Fast486StepOver(PFAST486_STATE State)
253 {
254 /* Call the internal function */
255 Fast486ExecutionControl(State, FAST486_STEP_OVER);
256 }
257
258 VOID
259 NTAPI
260 Fast486StepOut(PFAST486_STATE State)
261 {
262 /* Call the internal function */
263 Fast486ExecutionControl(State, FAST486_STEP_OUT);
264 }
265
266 /* EOF */