[FAST486]
[reactos.git] / reactos / lib / fast486 / debug.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->Flags.Tf)
87 {
88 /* Perform the interrupt */
89 Fast486PerformInterrupt(State, 0x01);
90
91 /*
92 * Flags and TF are pushed on stack so we can reset TF now,
93 * to not break into the INT 0x01 handler.
94 * After the INT 0x01 handler returns, the flags and therefore
95 * TF are popped back off the stack and restored, so TF will be
96 * automatically reset to its previous state.
97 */
98 State->Flags.Tf = FALSE;
99 }
100 else if (State->IntStatus == FAST486_INT_EXECUTE)
101 {
102 /* Perform the interrupt */
103 Fast486PerformInterrupt(State, State->PendingIntNum);
104
105 /* Clear the interrupt status */
106 State->IntStatus = FAST486_INT_NONE;
107 }
108 else if (State->Flags.If && (State->IntStatus == FAST486_INT_SIGNAL))
109 {
110 /* Acknowledge the interrupt to get the number */
111 State->PendingIntNum = State->IntAckCallback(State);
112
113 /* Set the interrupt status to execute on the next instruction */
114 State->IntStatus = FAST486_INT_EXECUTE;
115 }
116 else if (State->IntStatus == FAST486_INT_DELAYED)
117 {
118 /* Restore the old state */
119 State->IntStatus = FAST486_INT_EXECUTE;
120 }
121 }
122 while ((Command == FAST486_CONTINUE) ||
123 (Command == FAST486_STEP_OVER && ProcedureCallCount > 0) ||
124 (Command == FAST486_STEP_OUT && ProcedureCallCount >= 0));
125 }
126 #else
127 VOID
128 NTAPI
129 Fast486ExecutionControl(PFAST486_STATE State, FAST486_EXEC_CMD Command);
130 #endif
131
132 /* PUBLIC FUNCTIONS ***********************************************************/
133
134 VOID
135 NTAPI
136 Fast486DumpState(PFAST486_STATE State)
137 {
138 DbgPrint("\nFast486DumpState -->\n");
139 DbgPrint("\nCPU currently executing in %s mode at %04X:%08X\n",
140 (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE) ? "protected" : "real",
141 State->SegmentRegs[FAST486_REG_CS].Selector,
142 State->InstPtr.Long);
143 DbgPrint("\nGeneral purpose registers:\n"
144 "EAX = %08X\tECX = %08X\tEDX = %08X\tEBX = %08X\n"
145 "ESP = %08X\tEBP = %08X\tESI = %08X\tEDI = %08X\n",
146 State->GeneralRegs[FAST486_REG_EAX].Long,
147 State->GeneralRegs[FAST486_REG_ECX].Long,
148 State->GeneralRegs[FAST486_REG_EDX].Long,
149 State->GeneralRegs[FAST486_REG_EBX].Long,
150 State->GeneralRegs[FAST486_REG_ESP].Long,
151 State->GeneralRegs[FAST486_REG_EBP].Long,
152 State->GeneralRegs[FAST486_REG_ESI].Long,
153 State->GeneralRegs[FAST486_REG_EDI].Long);
154 DbgPrint("\nSegment registers:\n"
155 "ES = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
156 "CS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
157 "SS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
158 "DS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
159 "FS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n"
160 "GS = %04X (Base: %08X, Limit: %08X, Dpl: %u)\n",
161 State->SegmentRegs[FAST486_REG_ES].Selector,
162 State->SegmentRegs[FAST486_REG_ES].Base,
163 State->SegmentRegs[FAST486_REG_ES].Limit,
164 State->SegmentRegs[FAST486_REG_ES].Dpl,
165 State->SegmentRegs[FAST486_REG_CS].Selector,
166 State->SegmentRegs[FAST486_REG_CS].Base,
167 State->SegmentRegs[FAST486_REG_CS].Limit,
168 State->SegmentRegs[FAST486_REG_CS].Dpl,
169 State->SegmentRegs[FAST486_REG_SS].Selector,
170 State->SegmentRegs[FAST486_REG_SS].Base,
171 State->SegmentRegs[FAST486_REG_SS].Limit,
172 State->SegmentRegs[FAST486_REG_SS].Dpl,
173 State->SegmentRegs[FAST486_REG_DS].Selector,
174 State->SegmentRegs[FAST486_REG_DS].Base,
175 State->SegmentRegs[FAST486_REG_DS].Limit,
176 State->SegmentRegs[FAST486_REG_DS].Dpl,
177 State->SegmentRegs[FAST486_REG_FS].Selector,
178 State->SegmentRegs[FAST486_REG_FS].Base,
179 State->SegmentRegs[FAST486_REG_FS].Limit,
180 State->SegmentRegs[FAST486_REG_FS].Dpl,
181 State->SegmentRegs[FAST486_REG_GS].Selector,
182 State->SegmentRegs[FAST486_REG_GS].Base,
183 State->SegmentRegs[FAST486_REG_GS].Limit,
184 State->SegmentRegs[FAST486_REG_GS].Dpl);
185 DbgPrint("\nFlags: %08X (%s %s %s %s %s %s %s %s %s %s %s %s %s) Iopl: %u\n",
186 State->Flags.Long,
187 State->Flags.Cf ? "CF" : "cf",
188 State->Flags.Pf ? "PF" : "pf",
189 State->Flags.Af ? "AF" : "af",
190 State->Flags.Zf ? "ZF" : "zf",
191 State->Flags.Sf ? "SF" : "sf",
192 State->Flags.Tf ? "TF" : "tf",
193 State->Flags.If ? "IF" : "if",
194 State->Flags.Df ? "DF" : "df",
195 State->Flags.Of ? "OF" : "of",
196 State->Flags.Nt ? "NT" : "nt",
197 State->Flags.Rf ? "RF" : "rf",
198 State->Flags.Vm ? "VM" : "vm",
199 State->Flags.Ac ? "AC" : "ac",
200 State->Flags.Iopl);
201 DbgPrint("\nControl Registers:\n"
202 "CR0 = %08X\tCR2 = %08X\tCR3 = %08X\n",
203 State->ControlRegisters[FAST486_REG_CR0],
204 State->ControlRegisters[FAST486_REG_CR2],
205 State->ControlRegisters[FAST486_REG_CR3]);
206 DbgPrint("\nDebug Registers:\n"
207 "DR0 = %08X\tDR1 = %08X\tDR2 = %08X\n"
208 "DR3 = %08X\tDR4 = %08X\tDR5 = %08X\n",
209 State->DebugRegisters[FAST486_REG_DR0],
210 State->DebugRegisters[FAST486_REG_DR1],
211 State->DebugRegisters[FAST486_REG_DR2],
212 State->DebugRegisters[FAST486_REG_DR3],
213 State->DebugRegisters[FAST486_REG_DR4],
214 State->DebugRegisters[FAST486_REG_DR5]);
215
216 #ifndef FAST486_NO_FPU
217 DbgPrint("\nFPU Registers:\n"
218 "ST0 = %04X%016llX\tST1 = %04X%016llX\n"
219 "ST2 = %04X%016llX\tST3 = %04X%016llX\n"
220 "ST4 = %04X%016llX\tST5 = %04X%016llX\n"
221 "ST6 = %04X%016llX\tST7 = %04X%016llX\n"
222 "Status: %04X\tControl: %04X\tTag: %04X\n",
223 FPU_ST(0).Exponent | ((USHORT)FPU_ST(0).Sign << 15),
224 FPU_ST(0).Mantissa,
225 FPU_ST(1).Exponent | ((USHORT)FPU_ST(1).Sign << 15),
226 FPU_ST(1).Mantissa,
227 FPU_ST(2).Exponent | ((USHORT)FPU_ST(2).Sign << 15),
228 FPU_ST(2).Mantissa,
229 FPU_ST(3).Exponent | ((USHORT)FPU_ST(3).Sign << 15),
230 FPU_ST(3).Mantissa,
231 FPU_ST(4).Exponent | ((USHORT)FPU_ST(4).Sign << 15),
232 FPU_ST(4).Mantissa,
233 FPU_ST(5).Exponent | ((USHORT)FPU_ST(5).Sign << 15),
234 FPU_ST(5).Mantissa,
235 FPU_ST(6).Exponent | ((USHORT)FPU_ST(6).Sign << 15),
236 FPU_ST(6).Mantissa,
237 FPU_ST(7).Exponent | ((USHORT)FPU_ST(7).Sign << 15),
238 FPU_ST(7).Mantissa,
239 State->FpuStatus,
240 State->FpuControl,
241 State->FpuTag);
242 #endif
243
244 DbgPrint("\n<-- Fast486DumpState\n\n");
245 }
246
247 VOID
248 NTAPI
249 Fast486Continue(PFAST486_STATE State)
250 {
251 /* Call the internal function */
252 Fast486ExecutionControl(State, FAST486_CONTINUE);
253 }
254
255 VOID
256 NTAPI
257 Fast486StepInto(PFAST486_STATE State)
258 {
259 /* Call the internal function */
260 Fast486ExecutionControl(State, FAST486_STEP_INTO);
261 }
262
263 VOID
264 NTAPI
265 Fast486StepOver(PFAST486_STATE State)
266 {
267 /* Call the internal function */
268 Fast486ExecutionControl(State, FAST486_STEP_OVER);
269 }
270
271 VOID
272 NTAPI
273 Fast486StepOut(PFAST486_STATE State)
274 {
275 /* Call the internal function */
276 Fast486ExecutionControl(State, FAST486_STEP_OUT);
277 }
278
279 /* EOF */