-
Notifications
You must be signed in to change notification settings - Fork 1
/
DisassemblyWindow.cs
385 lines (331 loc) · 12.4 KB
/
DisassemblyWindow.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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.Drawing.Drawing2D;
namespace Emu6502
{
public struct DecodedInstr
{
public int Address;
public byte[] Bytes;
public string String;
}
public partial class DisassemblyWindow : Control
{
public Nes Nes { get { return nes; } set { nes = value; Disassemble(); } }
private Nes nes = null;
private Font fnt;
public int InstrNum { get; set; }
public int StartAddress { get; set; }
private List<DecodedInstr> decodedInstrs = new List<DecodedInstr>();
private Image bpImage;
private Image arrowImage;
public int SelectedIndex; // TODO: Property
public int Scroll = 0; // TODO: Property
private bool mouseDown = false;
private Dictionary<ushort, int> decodeMap = new Dictionary<ushort, int>();
public DisassemblyWindow()
{
InitializeComponent();
Init();
}
public DisassemblyWindow(Nes nes)
{
InitializeComponent();
Init();
Nes = nes;
}
private void Init()
{
this.DoubleBuffered = true;
this.TabStop = true;
this.Enabled = true;
try
{
bpImage = new Bitmap("breakpoint.png");
arrowImage = new Bitmap("arrow.png");
}
catch
{
}
fnt = new Font("Courier New", 10.0f);
}
public void SelectAddress(ushort address)
{
if (decodeMap.ContainsKey(address))
{
SelectedIndex = decodeMap[address];
MakeSelectionVisible();
Invalidate();
}
}
public void Disassemble()
{
if (nes == null)
return;
decodedInstrs.Clear();
decodeMap.Clear();
int addr = 0x8000;
while (addr <= 0xFFFF)
{
int count = 0x10000 - addr;
StringBuilder sb = new StringBuilder();
int bytes;
if (Disassembler.DisassembleOne(nes.Mem, addr, count, sb, out bytes))
{
DecodedInstr instr = new DecodedInstr();
instr.Address = addr;
instr.Bytes = new byte[bytes];
for (int i = 0; i < bytes; ++i)
instr.Bytes[i] = nes.Mem.Read(addr + i);
instr.String = sb.ToString();
decodedInstrs.Add(instr);
decodeMap[(ushort)addr] = decodedInstrs.Count - 1;
addr += bytes;
}
else
{
byte value = nes.Mem.Read(addr);
Disassembler.DB(sb, addr, value);
DecodedInstr instr = new DecodedInstr();
instr.Address = addr;
instr.Bytes = new byte[1];
instr.Bytes[0] = value;
instr.String = sb.ToString();
decodedInstrs.Add(instr);
decodeMap[(ushort)addr] = decodedInstrs.Count - 1;
addr += 1;
}
}
Console.WriteLine("Decoded {0} instrs for disassembler", decodedInstrs.Count);
}
private int GetLineHeight(Graphics g)
{
SizeF size = g.MeasureString("X", fnt);
return (int)Math.Ceiling(size.Height);
}
public void MakeSelectionVisible()
{
int lineHeight = GetLineHeight(CreateGraphics());
int lineTop = lineHeight * SelectedIndex;
int lineBottom = lineHeight * (SelectedIndex + 1);
if (lineTop - Scroll <= 0)
{
Scroll = lineTop;
Invalidate();
}
else if (lineBottom - Scroll >= ClientSize.Height)
{
Scroll = lineTop - ClientSize.Height + lineHeight;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (nes == null)
return;
Graphics g = pe.Graphics;
g.Clear(Color.White);
Size clientSize = this.ClientSize;
//Console.WriteLine(clientRect.Height);
// TODO: Start depending on where scrollbound is? I dunno.
int y = -Scroll;
int lineHeight = GetLineHeight(g);
for(int i=StartAddress; i < decodedInstrs.Count; ++i)
{
if (y < -lineHeight)
{
y += lineHeight;
continue;
}
DecodedInstr inst = decodedInstrs[i];
bool isBP = nes.Cpu.Breakpoints.GetBreakpoint((ushort)inst.Address) != null;
bool isPC = nes.Cpu.PC == inst.Address;
bool isSelected = (i == SelectedIndex);
Color bg = Color.White;
Color fg = Color.Black;
if (isPC)
{
bg = Color.FromArgb(0xFF, 0xEE, 0x00);
}
else if (isBP)
{
bg = Color.DarkRed;
fg = Color.White;
}
if(bg != Color.White)
g.FillRectangle(new SolidBrush(bg), new Rectangle(0, y, clientSize.Width, lineHeight));
g.DrawString(inst.String, fnt, new SolidBrush(fg), 20, y);
Pen p = new Pen(Color.Black);
p.DashStyle = DashStyle.Dot;
if (isSelected)
g.DrawRectangle(p, new Rectangle(0, y, clientSize.Width-1, lineHeight));
if (isBP || isPC)
{
if (bpImage != null && arrowImage != null)
{
int bpX = (int)Math.Round((20 - bpImage.Width) / 2.0);
int bpY = (int)Math.Round(y + (lineHeight - bpImage.Height) / 2.0);
RectangleF destRect = new RectangleF(bpX, bpY, bpImage.Width, bpImage.Height);
if (isBP)
g.DrawImage(bpImage, destRect);
if (isPC)
g.DrawImage(arrowImage, destRect);
}
}
y += lineHeight;
if (y > clientSize.Height)
break;
}
}
private void SelectPosition(int x, int y)
{
if (y < 0)
y = 0;
if (y >= ClientSize.Height)
y = ClientSize.Height - 1;
Graphics g = CreateGraphics();
int lineHeight = GetLineHeight(g);
int index = (y + Scroll)/ lineHeight;
if (index != SelectedIndex && index >= 0 && index < decodedInstrs.Count)
{
SelectedIndex = index;
Invalidate();
}
MakeSelectionVisible();
}
protected override void OnMouseDown(MouseEventArgs e)
{
Focus();
mouseDown = true;
SelectPosition(e.X, e.Y);
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
mouseDown = false;
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if(mouseDown)
SelectPosition(e.X, e.Y);
base.OnMouseMove(e);
}
protected override bool IsInputKey(Keys keyData)
{
if ((keyData & Keys.KeyCode) == Keys.Up ||
(keyData & Keys.KeyCode) == Keys.Down)
return true;
return base.IsInputKey(keyData);
}
public void Find(string text)
{
char[] splitChars = new char[] { '\n', '\r' };
string[] lines = text.Split(splitChars);
List<string> cleaned = new List<string>();
foreach (string line in lines)
{
string line2 = line.Trim();
if (line2 != "")
cleaned.Add(line2.ToUpper());
}
//MessageBox.Show(String.Join("\r\n", cleaned.ToArray()));
if (cleaned.Count == 0)
return;
int i = this.SelectedIndex;
i++;
while ((decodedInstrs.Count - i) >= cleaned.Count)
{
bool success = true;
for (int j = 0; j < cleaned.Count; j++)
{
DecodedInstr instr = decodedInstrs[i+j];
string targetStr = instr.String.ToUpper();
if (targetStr.IndexOf(cleaned[j]) < 0)
{
success = false;
break;
}
}
if (success)
{
this.SelectedIndex = i;
Invalidate();
MakeSelectionVisible();
break;
}
else
++i;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
switch(e.KeyCode & Keys.KeyCode)
{
case Keys.F9:
if (SelectedIndex >= 0 & SelectedIndex < decodedInstrs.Count)
{
ushort addr = (ushort)decodedInstrs[SelectedIndex].Address;
if (nes.Cpu.Breakpoints.GetBreakpoint(addr) != null)
nes.Cpu.Breakpoints.ClearBreakpoint(addr);
else
nes.Cpu.Breakpoints.SetBreakpoint(addr);
Invalidate();
e.Handled = true;
e.SuppressKeyPress = true;
}
break;
case Keys.PageUp:
{
int lineHeight = GetLineHeight(CreateGraphics());
int page = (int)Math.Ceiling((double)ClientSize.Height / lineHeight);
SelectedIndex = Math.Max(0, SelectedIndex - page);
MakeSelectionVisible();
Invalidate();
e.Handled = true;
e.SuppressKeyPress = true;
}
break;
case Keys.PageDown:
{
int lineHeight = GetLineHeight(CreateGraphics());
int page = (int)Math.Ceiling((double)ClientSize.Height / lineHeight);
SelectedIndex = Math.Min(decodedInstrs.Count-1, SelectedIndex + page);
MakeSelectionVisible();
Invalidate();
e.Handled = true;
e.SuppressKeyPress = true;
}
break;
case Keys.Down:
if (SelectedIndex < decodedInstrs.Count)
{
++SelectedIndex;
MakeSelectionVisible();
Invalidate();
}
e.Handled = true;
e.SuppressKeyPress = true;
break;
case Keys.Up:
if (SelectedIndex > 0)
{
--SelectedIndex;
MakeSelectionVisible();
Invalidate();
}
e.Handled = true;
e.SuppressKeyPress = true;
break;
}
base.OnKeyDown(e);
}
}
}