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