CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/GEDebugger/CtrlDisplayListView.cpp
Views: 1401
1
#include <algorithm>
2
#include <tchar.h>
3
#include "Common/Data/Encoding/Utf8.h"
4
#include "Common/StringUtils.h"
5
#include "Common/System/Display.h"
6
#include "Windows/GEDebugger/CtrlDisplayListView.h"
7
#include "Windows/GEDebugger/GEDebugger.h"
8
#include "Windows/MainWindow.h"
9
#include "Windows/InputBox.h"
10
#include "Windows/W32Util/ContextMenu.h"
11
#include "Windows/main.h"
12
#include "Core/Config.h"
13
#include "GPU/Debugger/Breakpoints.h"
14
#include "GPU/GPUState.h"
15
16
LPCTSTR CtrlDisplayListView::windowClass = _T("CtrlDisplayListView");
17
18
void CtrlDisplayListView::registerClass()
19
{
20
WNDCLASSEX wndClass;
21
22
wndClass.cbSize = sizeof(wndClass);
23
wndClass.lpszClassName = windowClass;
24
wndClass.hInstance = GetModuleHandle(0);
25
wndClass.lpfnWndProc = wndProc;
26
wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
27
wndClass.hIcon = 0;
28
wndClass.lpszMenuName = 0;
29
wndClass.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);
30
wndClass.style = 0;
31
wndClass.cbClsExtra = 0;
32
wndClass.cbWndExtra = sizeof(CtrlDisplayListView*);
33
wndClass.hIconSm = 0;
34
35
RegisterClassEx(&wndClass);
36
}
37
38
CtrlDisplayListView::CtrlDisplayListView(HWND _wnd)
39
: wnd(_wnd)
40
{
41
SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR) this);
42
SetWindowLong(wnd, GWL_STYLE, GetWindowLong(wnd,GWL_STYLE) | WS_VSCROLL);
43
SetScrollRange(wnd, SB_VERT, -1,1,TRUE);
44
45
instructionSize = 4;
46
47
// In small window mode, g_dpi_scale may have been adjusted.
48
const float fontScale = 1.0f / g_display.dpi_scale_real_y;
49
int fontHeight = g_Config.iFontHeight * fontScale;
50
int charWidth = g_Config.iFontWidth * fontScale;
51
52
rowHeight = fontHeight + 2;
53
54
font = CreateFont(fontHeight,charWidth,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,
55
L"Lucida Console");
56
boldfont = CreateFont(fontHeight,charWidth,0,0,FW_DEMIBOLD,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,
57
L"Lucida Console");
58
59
pixelPositions.addressStart = 16;
60
pixelPositions.opcodeStart = pixelPositions.addressStart + 19*charWidth;
61
62
hasFocus = false;
63
validDisplayList = false;
64
}
65
66
CtrlDisplayListView::~CtrlDisplayListView() {
67
DeleteObject(font);
68
DeleteObject(boldfont);
69
}
70
71
CtrlDisplayListView *CtrlDisplayListView::getFrom(HWND hwnd)
72
{
73
return (CtrlDisplayListView*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
74
}
75
76
LRESULT CALLBACK CtrlDisplayListView::wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
77
{
78
CtrlDisplayListView *win = CtrlDisplayListView::getFrom(hwnd);
79
80
switch(msg) {
81
case WM_NCCREATE:
82
// Allocate a new CustCtrl structure for this window.
83
win = new CtrlDisplayListView(hwnd);
84
85
// Continue with window creation.
86
return win != NULL;
87
case WM_NCDESTROY:
88
SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
89
delete win;
90
break;
91
case WM_SIZE:
92
win->redraw();
93
break;
94
case WM_PAINT:
95
win->onPaint(wParam,lParam);
96
break;
97
case WM_SETFOCUS:
98
SetFocus(hwnd);
99
win->hasFocus=true;
100
win->redraw();
101
break;
102
case WM_KILLFOCUS:
103
win->hasFocus=false;
104
win->redraw();
105
break;
106
case WM_VSCROLL:
107
win->onVScroll(wParam,lParam);
108
break;
109
case WM_MOUSEWHEEL:
110
if (GET_WHEEL_DELTA_WPARAM(wParam) > 0)
111
{
112
win->scrollWindow(-3);
113
} else if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) {
114
win->scrollWindow(3);
115
}
116
break;
117
case WM_LBUTTONDOWN:
118
win->onMouseDown(wParam,lParam,1);
119
break;
120
case WM_RBUTTONDOWN:
121
win->onMouseDown(wParam,lParam,2);
122
break;
123
case WM_LBUTTONUP:
124
win->onMouseUp(wParam,lParam,1);
125
break;
126
case WM_RBUTTONUP:
127
win->onMouseUp(wParam,lParam,2);
128
break;
129
case WM_KEYDOWN:
130
case WM_SYSKEYDOWN:
131
win->onKeyDown(wParam,lParam);
132
return 0;
133
case WM_GETDLGCODE:
134
if (lParam && ((MSG*)lParam)->message == WM_KEYDOWN)
135
{
136
switch (wParam)
137
{
138
case VK_TAB:
139
return DLGC_WANTMESSAGE;
140
default:
141
return DLGC_WANTCHARS|DLGC_WANTARROWS;
142
}
143
}
144
return DLGC_WANTCHARS|DLGC_WANTARROWS;
145
}
146
147
return DefWindowProc(hwnd, msg, wParam, lParam);
148
}
149
150
void CtrlDisplayListView::redraw()
151
{
152
GetClientRect(wnd, &rect);
153
visibleRows = rect.bottom/rowHeight;
154
155
RedrawWindow(wnd, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_INTERNALPAINT | RDW_ALLCHILDREN);
156
}
157
158
159
void CtrlDisplayListView::onPaint(WPARAM wParam, LPARAM lParam)
160
{
161
if (!validDisplayList || !gpuDebug)
162
return;
163
164
PAINTSTRUCT ps;
165
HDC actualHdc = BeginPaint(wnd, &ps);
166
HDC hdc = CreateCompatibleDC(actualHdc);
167
HBITMAP hBM = CreateCompatibleBitmap(actualHdc, rect.right-rect.left, rect.bottom-rect.top);
168
SelectObject(hdc, hBM);
169
170
SetBkMode(hdc, TRANSPARENT);
171
172
HPEN nullPen=CreatePen(0,0,0xffffff);
173
HPEN condPen=CreatePen(0,0,0xFF3020);
174
HBRUSH nullBrush=CreateSolidBrush(0xffffff);
175
HBRUSH currentBrush=CreateSolidBrush(0xFFEfE8);
176
177
HPEN oldPen=(HPEN)SelectObject(hdc,nullPen);
178
HBRUSH oldBrush=(HBRUSH)SelectObject(hdc,nullBrush);
179
HFONT oldFont = (HFONT)SelectObject(hdc,(HGDIOBJ)font);
180
181
HICON breakPoint = (HICON)LoadIcon(GetModuleHandle(0),(LPCWSTR)IDI_STOP);
182
183
auto disasm = gpuDebug->DissassembleOpRange(windowStart, windowStart + (visibleRows + 2) * instructionSize);
184
185
for (int i = 0; i < visibleRows+2; i++)
186
{
187
unsigned int address=windowStart + i*instructionSize;
188
bool stall = address == list.stall;
189
190
int rowY1 = rowHeight*i;
191
192
// draw background
193
COLORREF backgroundColor = stall ? 0xCCCCFF : 0xFFFFFF;
194
COLORREF textColor = 0x000000;
195
196
if (address >= selectRangeStart && address < selectRangeEnd)
197
{
198
if (hasFocus)
199
{
200
backgroundColor = address == curAddress ? 0xFF8822 : 0xFF9933;
201
textColor = 0xFFFFFF;
202
} else {
203
backgroundColor = 0xC0C0C0;
204
}
205
}
206
207
HBRUSH backgroundBrush = CreateSolidBrush(backgroundColor);
208
HPEN backgroundPen = CreatePen(0,0,backgroundColor);
209
SelectObject(hdc,backgroundBrush);
210
SelectObject(hdc,backgroundPen);
211
Rectangle(hdc,0,rowY1,rect.right,rowY1+rowHeight);
212
213
SelectObject(hdc,currentBrush);
214
SelectObject(hdc,nullPen);
215
216
DeleteObject(backgroundBrush);
217
DeleteObject(backgroundPen);
218
219
// display address/symbol
220
if (GPUBreakpoints::IsAddressBreakpoint(address))
221
{
222
textColor = 0x0000FF;
223
int yOffset = std::max(-1,(rowHeight-14+1)/2);
224
DrawIconEx(hdc,2,rowY1+1+yOffset,breakPoint,32,32,0,0,DI_NORMAL);
225
}
226
SetTextColor(hdc,textColor);
227
228
GPUDebugOp op = i < (int)disasm.size() ? disasm[i] : GPUDebugOp();
229
230
char addressText[64];
231
snprintf(addressText,sizeof(addressText),"%08X %08X",op.pc,op.op);
232
TextOutA(hdc,pixelPositions.addressStart,rowY1+2,addressText,(int)strlen(addressText));
233
234
if (address == list.pc)
235
{
236
TextOut(hdc,pixelPositions.opcodeStart-8,rowY1,L"\x25A0",1);
237
}
238
239
const char* opcode = op.desc.c_str();
240
SelectObject(hdc,stall ? boldfont : font);
241
TextOutA(hdc,pixelPositions.opcodeStart,rowY1+2,opcode,(int)strlen(opcode));
242
SelectObject(hdc,font);
243
}
244
245
SelectObject(hdc,oldFont);
246
SelectObject(hdc,oldPen);
247
SelectObject(hdc,oldBrush);
248
249
// copy bitmap to the actual hdc
250
BitBlt(actualHdc, 0, 0, rect.right, rect.bottom, hdc, 0, 0, SRCCOPY);
251
DeleteObject(hBM);
252
DeleteDC(hdc);
253
254
DeleteObject(nullPen);
255
DeleteObject(condPen);
256
257
DeleteObject(nullBrush);
258
DeleteObject(currentBrush);
259
260
DestroyIcon(breakPoint);
261
262
EndPaint(wnd, &ps);
263
}
264
265
void CtrlDisplayListView::toggleBreakpoint()
266
{
267
SendMessage(GetParent(wnd),WM_GEDBG_TOGGLEPCBREAKPOINT,curAddress,0);
268
}
269
270
void CtrlDisplayListView::PromptBreakpointCond() {
271
std::string expression;
272
GPUBreakpoints::GetAddressBreakpointCond(curAddress, &expression);
273
if (!InputBox_GetString(GetModuleHandle(NULL), wnd, L"Expression", expression, expression))
274
return;
275
276
std::string error;
277
if (!GPUBreakpoints::SetAddressBreakpointCond(curAddress, expression, &error))
278
MessageBox(wnd, ConvertUTF8ToWString(error).c_str(), L"Invalid expression", MB_OK | MB_ICONEXCLAMATION);
279
}
280
281
void CtrlDisplayListView::onMouseDown(WPARAM wParam, LPARAM lParam, int button)
282
{
283
if (!validDisplayList || !gpuDebug) {
284
return;
285
}
286
287
int y = HIWORD(lParam);
288
289
int line = y/rowHeight;
290
u32 newAddress = windowStart + line*instructionSize;
291
292
bool extend = KeyDownAsync(VK_SHIFT);
293
if (button == 1)
294
{
295
if (newAddress == curAddress && hasFocus)
296
{
297
toggleBreakpoint();
298
}
299
} else if (button == 2)
300
{
301
// Maintain the current selection if right clicking into it.
302
if (newAddress >= selectRangeStart && newAddress < selectRangeEnd)
303
extend = true;
304
}
305
306
setCurAddress(newAddress,extend);
307
308
SetFocus(wnd);
309
redraw();
310
}
311
312
void CtrlDisplayListView::onMouseUp(WPARAM wParam, LPARAM lParam, int button)
313
{
314
if (!validDisplayList || !gpuDebug) {
315
return;
316
}
317
318
if (button == 2)
319
{
320
HMENU menu = GetContextMenu(ContextMenuID::DISPLAYLISTVIEW);
321
EnableMenuItem(menu, ID_GEDBG_SETCOND, GPUBreakpoints::IsAddressBreakpoint(curAddress) ? MF_ENABLED : MF_GRAYED);
322
323
switch (TriggerContextMenu(ContextMenuID::DISPLAYLISTVIEW, wnd, ContextPoint::FromEvent(lParam)))
324
{
325
case ID_DISASM_GOTOINMEMORYVIEW:
326
if (memoryWindow)
327
memoryWindow->Goto(curAddress);
328
break;
329
case ID_DISASM_TOGGLEBREAKPOINT:
330
toggleBreakpoint();
331
redraw();
332
break;
333
case ID_GEDBG_SETCOND:
334
PromptBreakpointCond();
335
break;
336
case ID_DISASM_COPYINSTRUCTIONDISASM:
337
{
338
int space = 256 * (selectRangeEnd - selectRangeStart) / instructionSize;
339
char *temp = new char[space];
340
341
char *p = temp, *end = temp + space;
342
for (u32 pos = selectRangeStart; pos < selectRangeEnd && p < end; pos += instructionSize)
343
{
344
GPUDebugOp op = gpuDebug->DissassembleOp(pos);
345
p += snprintf(p, end - p, "%s\r\n", op.desc.c_str());
346
}
347
348
W32Util::CopyTextToClipboard(wnd, temp);
349
delete [] temp;
350
}
351
break;
352
case ID_DISASM_COPYADDRESS:
353
{
354
char temp[16];
355
snprintf(temp,sizeof(temp),"%08X",curAddress);
356
W32Util::CopyTextToClipboard(wnd, temp);
357
}
358
break;
359
case ID_DISASM_SETPCTOHERE:
360
{
361
gpuDebug->ResetListPC(list.id,curAddress);
362
list.pc = curAddress;
363
redraw();
364
}
365
break;
366
case ID_GEDBG_SETSTALLADDR:
367
{
368
gpuDebug->ResetListStall(list.id,curAddress);
369
list.stall = curAddress;
370
redraw();
371
}
372
break;
373
case ID_DISASM_COPYINSTRUCTIONHEX:
374
{
375
int space = 24 * (selectRangeEnd - selectRangeStart) / instructionSize;
376
char *temp = new char[space];
377
378
char *p = temp, *end = temp + space;
379
for (u32 pos = selectRangeStart; pos < selectRangeEnd && p < end; pos += instructionSize)
380
p += snprintf(p, end - p, "%08X\r\n", Memory::ReadUnchecked_U32(pos));
381
382
W32Util::CopyTextToClipboard(wnd, temp);
383
delete [] temp;
384
}
385
break;
386
case ID_DISASM_RUNTOHERE:
387
{
388
SendMessage(GetParent(wnd),WM_GEDBG_RUNTOWPARAM,curAddress,0);
389
redraw();
390
}
391
break;
392
case ID_GEDBG_GOTOPC:
393
setCurAddress(list.pc);
394
scrollAddressIntoView();
395
redraw();
396
break;
397
case ID_GEDBG_GOTOADDR:
398
{
399
std::string expression = StringFromFormat("%08x", curAddress);
400
if (!InputBox_GetString(GetModuleHandle(NULL), wnd, L"Address", expression, expression, InputBoxFlags::Selected)) {
401
break;
402
}
403
uint32_t newAddress = curAddress;
404
if (!GPUDebugExecExpression(gpuDebug, expression.c_str(), newAddress)) {
405
MessageBox(wnd, ConvertUTF8ToWString(getExpressionError()).c_str(), L"Invalid expression", MB_OK | MB_ICONEXCLAMATION);
406
break;
407
}
408
if (!Memory::IsValidAddress(newAddress)) {
409
MessageBox(wnd, L"Address not in valid memory", L"Invalid address", MB_OK | MB_ICONEXCLAMATION);
410
break;
411
}
412
413
setCurAddress(newAddress);
414
scrollAddressIntoView();
415
redraw();
416
}
417
break;
418
}
419
return;
420
}
421
422
redraw();
423
}
424
425
void CtrlDisplayListView::onVScroll(WPARAM wParam, LPARAM lParam)
426
{
427
switch (wParam & 0xFFFF)
428
{
429
case SB_LINEDOWN:
430
windowStart += instructionSize;
431
break;
432
case SB_LINEUP:
433
windowStart -= instructionSize;
434
break;
435
case SB_PAGEDOWN:
436
windowStart += visibleRows*instructionSize;
437
break;
438
case SB_PAGEUP:
439
windowStart -= visibleRows*instructionSize;
440
break;
441
default:
442
return;
443
}
444
redraw();
445
}
446
447
void CtrlDisplayListView::onKeyDown(WPARAM wParam, LPARAM lParam)
448
{
449
u32 windowEnd = windowStart+visibleRows*instructionSize;
450
451
switch (wParam & 0xFFFF)
452
{
453
case VK_DOWN:
454
setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT));
455
scrollAddressIntoView();
456
break;
457
case VK_UP:
458
setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT));
459
scrollAddressIntoView();
460
break;
461
case VK_NEXT:
462
if (curAddress != windowEnd - instructionSize && curAddressIsVisible()) {
463
setCurAddress(windowEnd - instructionSize, KeyDownAsync(VK_SHIFT));
464
scrollAddressIntoView();
465
} else {
466
setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT));
467
scrollAddressIntoView();
468
}
469
break;
470
case VK_PRIOR:
471
if (curAddress != windowStart && curAddressIsVisible()) {
472
setCurAddress(windowStart, KeyDownAsync(VK_SHIFT));
473
scrollAddressIntoView();
474
} else {
475
setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT));
476
scrollAddressIntoView();
477
}
478
break;
479
case VK_LEFT:
480
gotoAddr(list.pc);
481
return;
482
case VK_SPACE:
483
toggleBreakpoint();
484
break;
485
case VK_F10:
486
case VK_F11:
487
SendMessage(GetParent(wnd),WM_GEDBG_STEPDISPLAYLIST,0,0);
488
break;
489
}
490
redraw();
491
}
492
493
void CtrlDisplayListView::scrollAddressIntoView()
494
{
495
u32 windowEnd = windowStart + visibleRows * instructionSize;
496
497
if (curAddress < windowStart)
498
windowStart = curAddress;
499
else if (curAddress >= windowEnd)
500
windowStart = curAddress - visibleRows * instructionSize + instructionSize;
501
}
502
503
bool CtrlDisplayListView::curAddressIsVisible()
504
{
505
u32 windowEnd = windowStart + visibleRows * instructionSize;
506
return curAddress >= windowStart && curAddress < windowEnd;
507
}
508
509