-
Notifications
You must be signed in to change notification settings - Fork 1
/
Debugger.cs
338 lines (285 loc) · 10.9 KB
/
Debugger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Runtime.InteropServices;
using System.IO;
namespace Emu6502
{
public partial class Debugger : Form
{
private Nes nes;
private PpuOutput outputWindow;
private Stopwatch sw = new Stopwatch();
public Debugger()
{
InitializeComponent();
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = Directory.GetCurrentDirectory();// @"p:\csharp\emu6502\roms";
if (dlg.ShowDialog() != DialogResult.OK)
{
Application.Exit();
return;
}
nes = new Nes(dlg.FileName);
UpdateScreen();
UpdateTitle();
Application.Idle += new EventHandler(Application_Idle);
outputWindow = new PpuOutput(nes);
outputWindow.Show(this);
//outputWindow.Hide();
disassembly2.Nes = nes;
disassembly2.Update();
disassembly2.SelectAddress(nes.Cpu.PC);
sw.Start();
}
private void UpdateTitle()
{
if (nes.Cpu.Paused)
this.Text = "Debugger [Paused]";
else
this.Text = "Debugger [Running]";
}
private static bool AppStillIdle() {
pkMessage msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
[StructLayout(LayoutKind.Sequential)]
public struct pkMessage {
public IntPtr hWnd;
public Message msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out pkMessage msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
int counter = 0;
void Application_Idle(object sender, EventArgs e)
{
while(AppStillIdle())
{
if(nes.Cpu.Paused)
return;
const float deadZone = 0.3f;
GamePadState state = GamePad.GetState(PlayerIndex.One);
nes.Controller1.A = state.Buttons.A == (Microsoft.Xna.Framework.Input.ButtonState.Pressed);
nes.Controller1.B = state.Buttons.X == (Microsoft.Xna.Framework.Input.ButtonState.Pressed);
nes.Controller1.Start = state.Buttons.Start == (Microsoft.Xna.Framework.Input.ButtonState.Pressed);
nes.Controller1.Select = state.Buttons.Back == (Microsoft.Xna.Framework.Input.ButtonState.Pressed);
nes.Controller1.Left = state.ThumbSticks.Left.X < -deadZone;
nes.Controller1.Right = state.ThumbSticks.Left.X > deadZone;
nes.Controller1.Up = state.ThumbSticks.Left.Y > deadZone;
nes.Controller1.Down = state.ThumbSticks.Left.Y < -deadZone;
double deltaT = (double)sw.ElapsedTicks / (double)Stopwatch.Frequency;
deltaT *= 5;
sw.Reset();
sw.Start();
if(deltaT > 0.1f)
deltaT = 0.1f;
double clockRate = 341 * 262 * 60;
int cycles = (int)Math.Round(clockRate * deltaT);
//Console.WriteLine("{0:0} ms = {1} cycles", deltaT * 1000.0, cycles);
//bool render=false;
/*while (!nes.Cpu.Paused && cycles > 0)
{
bool tmpRender = false;
cycles = nes.Run(cycles, out tmpRender);
if(tmpRender)
render = true;
}*/
nes.RunOneFrame();
if (nes.Cpu.Paused)
{
UpdateTitle();
UpdateScreen();
disassembly2.SelectAddress(nes.Cpu.PC);
return;
}
//if(render)
outputWindow.Invalidate();
counter++;
if (counter > 10)
{
outputWindow.Text = String.Format("NES Emulator - {0:0.0} FPS", nes.FPS);
counter = 0;
}
}
}
private string DecodeReg(string name, byte value)
{
StringBuilder sb = new StringBuilder();
sb.Append(name);
sb.AppendFormat(" ${0:X2} ", value);
for (int i = 7; i >= 4; --i)
sb.Append((value & (1 << i)) != 0 ? "1" : "0");
sb.Append(" ");
for (int i = 3; i >= 0; --i)
sb.Append((value & (1 << i)) != 0 ? "1" : "0");
if (value > 127)
sb.AppendFormat(" {0}/{1}", (int)value, (int)(sbyte)value);
else
sb.AppendFormat(" {0}", (int)value);
return sb.ToString();
}
public void UpdateScreen()
{
pcLabel.Text = String.Format("PC ${0:X4} Cy={1}", nes.Cpu.PC, nes.TotalCpuCycles);
spLabel.Text = String.Format("SP ${0:X2}", nes.Cpu.SP);
aLabel.Text = DecodeReg("A", nes.Cpu.A);
xLabel.Text = DecodeReg("X", nes.Cpu.X);
yLabel.Text = DecodeReg("Y", nes.Cpu.Y);
cFlag.Checked = nes.Cpu.C;
zFlag.Checked = nes.Cpu.Z;
iFlag.Checked = nes.Cpu.I;
vFlag.Checked = nes.Cpu.V;
nFlag.Checked = nes.Cpu.N;
ivectorLabel.Text = String.Format("Reset: ${0:X4} = ${1:X4}\r\n" +
" NMI: ${2:X4} = ${3:X4}\r\n" +
" IRQ: ${4:X4} = ${5:X4}\r\n",
Chip6502.ResetAddr, nes.Cpu.ReadWord(Chip6502.ResetAddr),
Chip6502.NMIAddr, nes.Cpu.ReadWord(Chip6502.NMIAddr),
Chip6502.IRQAddr, nes.Cpu.ReadWord(Chip6502.IRQAddr));
//string dis = Disassembler.Disassemble(nes.Mem, nes.Cpu.PC - 16, 128);
//disassembly.Text = dis;
disassembly2.Invalidate();
}
private void fileExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void debugStepOver_Click(object sender, EventArgs e)
{
nes.Cpu.SingleStep = true;
nes.Cpu.Paused = false;
UpdateTitle();
}
private void nFlag_CheckedChanged(object sender, EventArgs e)
{
nes.Cpu.N = nFlag.Checked;
}
private void vFlag_CheckedChanged(object sender, EventArgs e)
{
nes.Cpu.V = vFlag.Checked;
}
private void iFlag_CheckedChanged(object sender, EventArgs e)
{
nes.Cpu.I = iFlag.Checked;
}
private void zFlag_CheckedChanged(object sender, EventArgs e)
{
nes.Cpu.Z = zFlag.Checked;
}
private void cFlag_CheckedChanged(object sender, EventArgs e)
{
nes.Cpu.C = cFlag.Checked;
}
private void interruptReset_Click(object sender, EventArgs e)
{
nes.Reset();
nes.Cpu.Paused = true;
UpdateScreen();
UpdateTitle();
disassembly2.SelectAddress(nes.Cpu.PC);
}
private void interruptNMI_Click(object sender, EventArgs e)
{
nes.Cpu.NMI();
nes.Cpu.Paused = true;
UpdateScreen();
UpdateTitle();
disassembly2.SelectAddress(nes.Cpu.PC);
}
private void interruptIRQ_Click(object sender, EventArgs e)
{
nes.Cpu.IRQ();
nes.Cpu.Paused = true;
UpdateScreen();
UpdateTitle();
disassembly2.SelectAddress(nes.Cpu.PC);
}
private void debugGo_Click(object sender, EventArgs e)
{
if (nes.Cpu.Paused)
nes.Cpu.Paused = false;
else
{
nes.Cpu.Paused = true;
UpdateScreen();
disassembly2.SelectAddress(nes.Cpu.PC);
}
UpdateTitle();
}
private void debugBreakpoints_Click(object sender, EventArgs e)
{
Breakpoints bp = new Breakpoints(nes);
bp.ShowDialog();
// TODO: Blah, cleaner stuff for this
UpdateScreen();
}
private void Debugger_FormClosed(object sender, FormClosedEventArgs e)
{
//nes.Cpu.SaveEncounteredInstructions();
}
private void redisasm_Click(object sender, EventArgs e)
{
disassembly2.Disassemble();
disassembly2.SelectAddress(nes.Cpu.PC);
}
private void gotoBtn_Click(object sender, EventArgs e)
{
AddressEntryDlg dlg = new AddressEntryDlg();
if (dlg.ShowDialog() == DialogResult.OK)
{
disassembly2.SelectAddress((ushort)dlg.Address);
}
}
private void showPCBtn_Click(object sender, EventArgs e)
{
disassembly2.SelectAddress(nes.Cpu.PC);
}
private void ppuDebug_Click(object sender, EventArgs e)
{
PpuDebug dbg = new PpuDebug(this.nes.Ppu);
dbg.Show();
}
private void findBtn_Click(object sender, EventArgs e)
{
FindDlg dlg = new FindDlg();
if (dlg.ShowDialog() == DialogResult.OK)
{
disassembly2.Find(dlg.Result);
}
}
private void eventsBtn_Click(object sender, EventArgs e)
{
List<string> eventNames = new List<string>(nes.EventCounters.Keys);
eventNames.Sort();
string result = "";
foreach (string name in eventNames)
result += String.Format("{0}={1}\r\n", name, nes.EventCounters[name]);
MessageBox.Show(result);
}
private void clearEvents_Click(object sender, EventArgs e)
{
nes.EventCounters.Clear();
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void Debugger_Load(object sender, EventArgs e)
{
//outputWindow.Focus();
SetForegroundWindow(outputWindow.Handle);
}
}
}