-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathdebugger.cpp
2904 lines (2649 loc) · 96.7 KB
/
debugger.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2002 Ben Parnell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common.h"
#include "../../utils/xstring.h"
#include "../../types.h"
#include "debugger.h"
#include "../../x6502.h"
#include "../../fceu.h"
#include "../../debug.h"
#include "../../nsf.h"
#include "../../ppu.h"
#include "../../cart.h"
#include "../../ines.h"
#include "../../asm.h"
#include "tracer.h"
#include "memview.h"
#include "cheat.h"
#include "gui.h"
#include "ntview.h"
#include "cdlogger.h"
#include "ppuview.h"
#include "richedit.h"
// ################################## Start of SP CODE ###########################
#include "debuggersp.h"
extern Name* pageNames[32];
extern Name* ramBankNames;
extern bool ramBankNamesLoaded;
extern int pageNumbersLoaded[32];
extern int myNumWPs;
// ################################## End of SP CODE ###########################
extern int vblankScanLines;
extern int vblankPixel;
extern bool DebuggerWasUpdated;
int childwnd;
extern readfunc ARead[0x10000];
int DbgPosX,DbgPosY;
int DbgSizeX=-1,DbgSizeY=-1;
int WP_edit=-1;
int ChangeWait=0,ChangeWait2=0;
uint8 debugger_open=0;
HWND hDebug;
static HMENU hDebugcontext; //Handle to context menu
static HMENU hDebugcontextsub; //Handle to context sub menu
static HMENU hDisasmcontext; //Handle to context menu
static HMENU hDisasmcontextsub; //Handle to context sub menu
WNDPROC IDC_DEBUGGER_DISASSEMBLY_oldWndProc = 0;
// static HFONT hFont;
static SCROLLINFO si;
bool debuggerAutoload = false;
bool debuggerSaveLoadDEBFiles = true;
bool debuggerIDAFont = false;
unsigned int IDAFontSize = 16;
bool debuggerDisplayROMoffsets = false;
wchar_t* debug_wstr;
char* debug_cdl_str;
char* debug_str_decoration_comment;
char* debug_decoration_comment;
char* debug_decoration_comment_end_pos;
FINDTEXT newline;
FINDTEXT num;
int DefDbgRGB;
CHARFORMAT2 DefDbgChFmt;
struct DBGCOLORMENU {
COLORMENU menu;
CHARFORMAT2 *fmt;
} dbgcolormenu[] = {
{ "PC", PPCCF(DbgPC) },
{ NULL },
{ "Mnemonic", PPCCF(DbgMnem) },
{ NULL },
{ "Symbolic name", PPCCF(DbgSym) },
{ "Comment" , PPCCF(DbgComm) },
{ NULL },
{ "Operand" , PPCCF(DbgOper) },
{ "Operand note" , PPCCF(DbgOpNt) },
{ "Effective address", PPCCF(DbgEff) },
{ NULL },
{ "RTS Line", PPCCF(DbgRts) }
};
#define IDC_DEBUGGER_RESTORESIZE 1000
#define ID_COLOR_DEBUGGER 2000
bool ChangeColor(HWND hwnd, DBGCOLORMENU* item)
{
if (ChangeColor(hwnd, (COLORMENU*)item))
{
item->fmt->crTextColor = RGB(*item->menu.r, *item->menu.g, *item->menu.b);
return true;
}
return false;
}
// this is used to keep track of addresses that lines of Disassembly window correspond to
std::vector<uint16> disassembly_addresses;
// this is used to keep track of addresses in operands of each printed instruction
std::vector<std::vector<uint16>> disassembly_operands;
// this is used to autoscroll the Disassembly window while keeping relative position of the ">" pointer inside this window
unsigned int PC_pointerOffset = 0;
int PCLine = -1;
// this is used for dirty, but unavoidable hack, which is necessary to ensure the ">" pointer is visible when stepping/seeking to PC
bool PCPointerWasDrawn = false;
// and another hack...
int beginningOfPCPointerLine = -1; // index of the first char within debug_str[] string, where the ">" line starts
#define INVALID_START_OFFSET 1
#define INVALID_END_OFFSET 2
#define MAX_NAME_SIZE 200
#define MAX_CONDITION_SIZE 200
void UpdateOtherDebuggingDialogs()
{
//adelikat: This updates all the other dialogs such as ppu, nametable, logger, etc in one function, should be applied to all the step type buttons
NTViewDoBlit(0); //Nametable Viewer
UpdateLogWindow(); //Trace Logger
UpdateCDLogger(); //Code/Data Logger
PPUViewDoBlit(); //PPU Viewer
}
#define DISASM_DEFAULT_WIDTH (debuggerIDAFont ? 540 : 470)
#define DEBUGGER_MIN_HEIGHT_LEFT 120 // Minimum height for the left part
#define DEBUGGER_MIN_HEIGHT_RIGHT 590 // Minimun height for the right part.
#define DEBUGGER_MIN_WIDTH 360 // Minimum width for debugger
#define DEBUGGER_DEFAULT_HEIGHT 594 // default height for debugger
// owomomo: default width of the debugger is depend on the default width of disasm view, so it's not defined here.
void RestoreSize(HWND hwndDlg)
{
HDC hdc = GetDC(hwndDlg);
RECT wndRect, disasmRect;
GetWindowRect(hwndDlg, &wndRect);
GetWindowRect(GetDlgItem(hwndDlg, IDC_DEBUGGER_DISASSEMBLY), &disasmRect);
int default_width = (disasmRect.left - wndRect.left) + DISASM_DEFAULT_WIDTH + (wndRect.right - disasmRect.right);
int default_height = MulDiv(DEBUGGER_DEFAULT_HEIGHT, GetDeviceCaps(hdc, LOGPIXELSY), 96);
ReleaseDC(hwndDlg, hdc);
SetWindowPos(hwndDlg,HWND_TOP,DbgPosX,DbgPosY,default_width,default_height,SWP_SHOWWINDOW);
}
unsigned int NewBreakWindows(HWND hwndDlg, unsigned int num, bool enable)
{
char startOffsetBuffer[5] = {0};
char endOffsetBuffer[5] = {0};
unsigned int type = 0;
GetDlgItemText(hwndDlg, IDC_ADDBP_ADDR_START, startOffsetBuffer, sizeof(startOffsetBuffer));
GetDlgItemText(hwndDlg, IDC_ADDBP_ADDR_END, endOffsetBuffer, sizeof(endOffsetBuffer));
if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MEM_CPU))
type |= BT_C;
else if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MEM_PPU))
type |= BT_P;
else
type |= BT_S;
if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MODE_R))
type |= WP_R;
if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MODE_W))
type |= WP_W;
if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MODE_X))
type |= WP_X;
//this overrides all
if (IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MODE_F))
type = WP_F;
int start = offsetStringToInt(type, startOffsetBuffer);
if (start == -1)
{
return INVALID_START_OFFSET;
}
int end = offsetStringToInt(type, endOffsetBuffer);
if (*endOffsetBuffer && end == -1)
{
return INVALID_END_OFFSET;
}
// Handle breakpoint conditions
char name[MAX_NAME_SIZE] = {0};
GetDlgItemText(hwndDlg, IDC_ADDBP_NAME, name, MAX_NAME_SIZE);
char condition[MAX_CONDITION_SIZE] = {0};
GetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, condition, MAX_CONDITION_SIZE);
return NewBreak(name, start, end, type, condition, num, enable);
}
/**
* Adds a new breakpoint to the breakpoint list
*
* @param hwndDlg Handle of the debugger window
* @return 0 (success), 1 (Too many breakpoints), 2 (???), 3 (Invalid breakpoint condition)
**/
unsigned int AddBreak(HWND hwndDlg)
{
if (numWPs == MAXIMUM_NUMBER_OF_BREAKPOINTS)
{
return TOO_MANY_BREAKPOINTS;
}
unsigned val = NewBreakWindows(hwndDlg,numWPs,1);
if (val == 1)
{
return 2;
}
else if (val == 2)
{
return INVALID_BREAKPOINT_CONDITION;
}
numWPs++;
myNumWPs++;
return 0;
}
// This function is for "smart" scrolling...
// it attempts to scroll up one line by a whole instruction
int InstructionUp(int from)
{
int i = std::min(16, from), j;
while (i > 0)
{
j = i;
while (j > 0)
{
if (GetMem(from - j) == 0x00)
break; // BRK usually signifies data
if (opsize[GetMem(from - j)] == 0)
break; // invalid instruction!
if (opsize[GetMem(from - j)] > j)
break; // instruction is too long!
if (opsize[GetMem(from - j)] == j)
return (from - j); // instruction is just right! :D
j -= opsize[GetMem(from - j)];
}
i--;
}
// if we get here, no suitable instruction was found
if ((from >= 2) && (GetMem(from - 2) == 0x00))
return (from - 2); // if a BRK instruction is possible, use that
if (from)
return (from - 1); // else, scroll up one byte
return 0; // of course, if we can't scroll up, just return 0!
}
int InstructionDown(int from)
{
int tmp = opsize[GetMem(si.nPos)];
if ((tmp))
return from + tmp;
else
return from + 1; // this is data or undefined instruction
}
static void UpdateDialog(HWND hwndDlg) {
BOOL forbid = IsDlgButtonChecked(hwndDlg, IDC_ADDBP_MODE_F);
BOOL enable = !forbid;
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_R),enable);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_W),enable);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_X),enable);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MEM_CPU),enable);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MEM_PPU),enable);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MEM_SPR),enable);
//nah.. lets leave these checked
//CheckDlgButton(hwndDlg,IDC_ADDBP_MODE_R,BST_UNCHECKED);
//CheckDlgButton(hwndDlg,IDC_ADDBP_MODE_W,BST_UNCHECKED);
//CheckDlgButton(hwndDlg,IDC_ADDBP_MODE_X,BST_UNCHECKED);
//CheckDlgButton(hwndDlg,IDC_ADDBP_MEM_CPU,BST_UNCHECKED);
//CheckDlgButton(hwndDlg,IDC_ADDBP_MEM_PPU,BST_UNCHECKED);
//CheckDlgButton(hwndDlg,IDC_ADDBP_MEM_SPR,BST_UNCHECKED);
}
INT_PTR CALLBACK AddbpCallB(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
char str[8] = {0};
int tmp;
switch(uMsg)
{
case WM_INITDIALOG:
CenterWindow(hwndDlg);
SendDlgItemMessage(hwndDlg, IDC_ADDBP_ADDR_START, EM_SETLIMITTEXT, 4, 0);
SendDlgItemMessage(hwndDlg, IDC_ADDBP_ADDR_END, EM_SETLIMITTEXT, 4, 0);
// Don't limit address entry. See: debugcpp offsetStringToInt
//DefaultEditCtrlProc = (WNDPROC)SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_ADDBP_ADDR_START), GWLP_WNDPROC, (LONG_PTR)FilterEditCtrlProc);
//SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_ADDBP_ADDR_END), GWLP_WNDPROC, (LONG_PTR)FilterEditCtrlProc);
if (WP_edit >= 0)
{
SetWindowText(hwndDlg,"Edit Breakpoint...");
sprintf(str,"%04X",watchpoint[WP_edit].address);
SetDlgItemText(hwndDlg,IDC_ADDBP_ADDR_START,str);
sprintf(str,"%04X",watchpoint[WP_edit].endaddress);
if (strcmp(str,"0000") != 0) SetDlgItemText(hwndDlg,IDC_ADDBP_ADDR_END,str);
if (watchpoint[WP_edit].flags&WP_R) CheckDlgButton(hwndDlg, IDC_ADDBP_MODE_R, BST_CHECKED);
if (watchpoint[WP_edit].flags&WP_W) CheckDlgButton(hwndDlg, IDC_ADDBP_MODE_W, BST_CHECKED);
if (watchpoint[WP_edit].flags&WP_X) CheckDlgButton(hwndDlg, IDC_ADDBP_MODE_X, BST_CHECKED);
if (watchpoint[WP_edit].flags&WP_F) CheckDlgButton(hwndDlg, IDC_ADDBP_MODE_F, BST_CHECKED);
if (watchpoint[WP_edit].flags&BT_P) {
CheckDlgButton(hwndDlg, IDC_ADDBP_MEM_PPU, BST_CHECKED);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_X),FALSE);
}
else if (watchpoint[WP_edit].flags&BT_S) {
CheckDlgButton(hwndDlg, IDC_ADDBP_MEM_SPR, BST_CHECKED);
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_X),FALSE);
}
else CheckDlgButton(hwndDlg, IDC_ADDBP_MEM_CPU, BST_CHECKED);
UpdateDialog(hwndDlg);
// ################################## Start of SP CODE ###########################
SendDlgItemMessage(hwndDlg,IDC_ADDBP_CONDITION,EM_SETLIMITTEXT,200,0);
SendDlgItemMessage(hwndDlg,IDC_ADDBP_NAME,EM_SETLIMITTEXT,200,0);
if (watchpoint[WP_edit].cond)
{
SetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, watchpoint[WP_edit].condText);
}
else
{
SetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, "");
}
if (watchpoint[WP_edit].desc)
{
SetDlgItemText(hwndDlg, IDC_ADDBP_NAME, watchpoint[WP_edit].desc);
}
else
{
SetDlgItemText(hwndDlg, IDC_ADDBP_NAME, "");
}
// ################################## End of SP CODE ###########################
} else
{
CheckDlgButton(hwndDlg, IDC_ADDBP_MEM_CPU, BST_CHECKED);
// if lParam is not 0 then we should suggest to add PC breakpoint
if (lParam)
{
CheckDlgButton(hwndDlg, IDC_ADDBP_MODE_X, BST_CHECKED);
sprintf(str, "%04X", (unsigned int)lParam);
SetDlgItemText(hwndDlg,IDC_ADDBP_ADDR_START,str);
// also set the condition to only break at this Bank
auto bank = getBank(lParam);
if (bank > -1) {
sprintf(str, "K==#%02X", bank);
SetDlgItemText(hwndDlg, IDC_ADDBP_CONDITION, str);
}
}
}
break;
case WM_CLOSE:
case WM_QUIT:
break;
case WM_COMMAND:
switch(HIWORD(wParam)) {
case BN_CLICKED:
switch(LOWORD(wParam)) {
case IDC_ADDBP_MODE_F:
{
UpdateDialog(hwndDlg);
break;
}
case IDOK:
if (WP_edit >= 0) {
int tmp = NewBreakWindows(hwndDlg,WP_edit,(BOOL)(watchpoint[WP_edit].flags&WP_E));
if (tmp == 2 || tmp == INVALID_BREAKPOINT_CONDITION)
{
MessageBox(hwndDlg, "Invalid breakpoint condition", "Error", MB_OK | MB_ICONERROR);
break;
}
EndDialog(hwndDlg,1);
break;
}
if ((tmp=AddBreak(hwndDlg)) == TOO_MANY_BREAKPOINTS) {
MessageBox(hwndDlg, "Too many breakpoints, please delete one and try again", "Breakpoint Error", MB_OK | MB_ICONERROR);
goto endaddbrk;
}
if (tmp == 2) goto endaddbrk;
else if (tmp == INVALID_BREAKPOINT_CONDITION)
{
MessageBox(hwndDlg, "Invalid breakpoint condition", "Error", MB_OK | MB_ICONERROR);
break;
}
EndDialog(hwndDlg,1);
break;
case IDCANCEL:
endaddbrk:
EndDialog(hwndDlg,0);
break;
case IDC_ADDBP_MEM_CPU:
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_X),TRUE);
break;
case IDC_ADDBP_MEM_PPU:
case IDC_ADDBP_MEM_SPR:
EnableWindow(GetDlgItem(hwndDlg,IDC_ADDBP_MODE_X),FALSE);
break;
}
break;
}
break;
}
return FALSE; //TRUE;
}
void HighlightPC(HWND hWnd)
{
if (PCLine == -1)
return;
FINDTEXT ft;
ft.lpstrText = ">";
ft.chrg.cpMin = 0;
ft.chrg.cpMax = -1;
int start = SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_FINDTEXT, (WPARAM)FR_DOWN, (LPARAM)&ft);
if (start >= 0)
{
int old_start, old_end;
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_GETSEL, (WPARAM)&old_start, (LPARAM)&old_end);
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)start, (LPARAM)start+20);
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgPC));
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)old_start, (LPARAM)old_end);
}
}
void HighlightSyntax(HWND hWnd, int lines)
{
int wordbreak = 0;
int opbreak = 0;
int numpos = 0;
int old_start, old_end;
bool commentline;
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_GETSEL, (WPARAM)&old_start, (LPARAM)&old_end);
for (int line = 0; ; line++)
{
commentline = false;
wordbreak = SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_FINDWORDBREAK, (WPARAM)WB_RIGHT, (LPARAM)newline.chrg.cpMin + 21);
for (int ch = newline.chrg.cpMin; debug_wstr[ch] != 0; ch++)
{
if (debug_wstr[ch] == L'=' || debug_wstr[ch] == L'@' || debug_wstr[ch] == L'\n' || debug_wstr[ch] == L'-' || debug_wstr[ch] == L';')
{
opbreak = ch;
break;
}
}
if (debug_wstr[newline.chrg.cpMin] == L';')
commentline = true;
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)newline.chrg.cpMin + 20, (LPARAM)opbreak);
int oldline = newline.chrg.cpMin;
newline.chrg.cpMin = SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_FINDTEXT, (WPARAM)FR_DOWN, (LPARAM)&newline) + 1;
if(newline.chrg.cpMin == 0) break;
// symbolic address
if (debug_wstr[newline.chrg.cpMin - 2] == L':')
{
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)oldline, (LPARAM)newline.chrg.cpMin);
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgSym));
continue;
}
if (!commentline)
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgMnem));
// comment
if (opbreak < newline.chrg.cpMin)
{
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)opbreak, (LPARAM)newline.chrg.cpMin);
if (commentline)
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgComm));
else
{
if (debug_wstr[opbreak] == L'-')
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION,
(LPARAM)PPCF(DbgRts));
else
{
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgOpNt));
if (debug_wstr[opbreak] == L'@')
{
// effective address
FINDTEXT ft = { { opbreak, newline.chrg.cpMin }, "=" };
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, opbreak, SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_FINDTEXT, (WPARAM)FR_DOWN, (LPARAM)&ft));
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgEff));
}
}
}
}
if (commentline)
continue;
// operand
num.chrg.cpMin = wordbreak;
num.chrg.cpMax = wordbreak + 6;
numpos = SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_FINDTEXT, (WPARAM)FR_DOWN, (LPARAM)&num);
if (numpos != 0)
{
if (debug_wstr[numpos + 3] == L',' || debug_wstr[numpos + 3] == L')' || debug_wstr[numpos + 3] == L'\n'
|| debug_wstr[numpos + 3] == L' ' //zero 30-nov-2017 - in support of combined label/offset disassembly. not sure this is a good idea
)
wordbreak = numpos + 2;
else
wordbreak = numpos + 4;
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)numpos, (LPARAM)wordbreak + 1);
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)PPCF(DbgOper));
}
if (newline.chrg.cpMin == 0)
break;
}
SendDlgItemMessage(hWnd, IDC_DEBUGGER_DISASSEMBLY, EM_SETSEL, (WPARAM)old_start, (LPARAM)old_end);
}
void UpdateDisassembleView(HWND hWnd, UINT id, int lines, bool text = false)
{
// basic syntax highlighter and due richedit optimizations
int eventMask = SendDlgItemMessage(hWnd, id, EM_SETEVENTMASK, 0, 0);
SendDlgItemMessage(hWnd, id, WM_SETREDRAW, false, 0);
if (text)
SetDlgItemTextW(hWnd, id, debug_wstr);
HighlightSyntax(hWnd, lines);
HighlightPC(hWnd);
SendDlgItemMessage(hWnd, id, WM_SETREDRAW, true, 0);
InvalidateRect(GetDlgItem(hWnd, id), 0, true);
SendDlgItemMessage(hWnd, id, EM_SETEVENTMASK, 0, eventMask);
}
void Disassemble(HWND hWnd, int id, int scrollid, unsigned int addr)
{
wchar_t chr[40] = { 0 };
wchar_t debug_wbuf[2048] = { 0 };
int size;
uint8 opcode[3];
unsigned int instruction_addr;
disassembly_addresses.resize(0);
PCPointerWasDrawn = false;
beginningOfPCPointerLine = -1;
if (symbDebugEnabled)
{
loadNameFiles();
disassembly_operands.resize(0);
}
si.nPos = addr;
SetScrollInfo(GetDlgItem(hWnd,scrollid),SB_CTL,&si,TRUE);
//figure out how many lines we can draw
RECT rect;
GetClientRect(GetDlgItem(hWnd, id), &rect);
int lines = (rect.bottom-rect.top) / debugSystem->disasmFontHeight;
debug_wstr[0] = 0;
PCLine = -1;
unsigned int instructions_count = 0;
for (int i = 0; i < lines; i++)
{
// PC pointer
if (addr > 0xFFFF) break;
instruction_addr = addr;
if (symbDebugEnabled)
{
// insert Name and Comment lines if needed
Name* node = findNode(getNamesPointerForAddress(addr), addr);
if (node)
{
if (node->name)
{
swprintf(debug_wbuf, L"%S:\n", node->name);
wcscat(debug_wstr, debug_wbuf);
// we added one line to the disassembly window
disassembly_addresses.push_back(addr);
disassembly_operands.resize(i + 1);
i++;
}
if (node->comment)
{
// make a copy
strcpy(debug_str_decoration_comment, node->comment);
strcat(debug_str_decoration_comment, "\r\n");
// divide the debug_str_decoration_comment into strings (Comment1, Comment2, ...)
debug_decoration_comment = debug_str_decoration_comment;
debug_decoration_comment_end_pos = strstr(debug_decoration_comment, "\r\n");
while (debug_decoration_comment_end_pos)
{
debug_decoration_comment_end_pos[0] = 0; // set \0 instead of \r
debug_decoration_comment_end_pos[1] = 0; // set \0 instead of \n
swprintf(debug_wbuf, L"; %S\n", debug_decoration_comment);
wcscat(debug_wstr, debug_wbuf);
// we added one line to the disassembly window
disassembly_addresses.push_back(addr);
disassembly_operands.resize(i + 1);
i++;
debug_decoration_comment_end_pos += 2;
debug_decoration_comment = debug_decoration_comment_end_pos;
debug_decoration_comment_end_pos = strstr(debug_decoration_comment_end_pos, "\r\n");
}
}
}
}
if (addr == X.PC)
{
PC_pointerOffset = instructions_count;
PCPointerWasDrawn = true;
beginningOfPCPointerLine = wcslen(debug_wstr);
wcscat(debug_wstr, L">");
PCLine = instructions_count;
} else
{
wcscat(debug_wstr, L" ");
}
if (addr >= 0x8000)
{
if (debuggerDisplayROMoffsets && GetNesFileAddress(addr) != -1)
{
swprintf(chr, L" %06X: ", GetNesFileAddress(addr));
} else
{
swprintf(chr, L"%02X:%04X: ", getBank(addr), addr);
}
} else
{
swprintf(chr, L" :%04X: ", addr);
}
// Add address
wcscat(debug_wstr, chr);
disassembly_addresses.push_back(addr);
if (symbDebugEnabled)
disassembly_operands.resize(i + 1);
size = opsize[GetMem(addr)];
if (size == 0)
{
swprintf(chr, L"%02X UNDEFINED", GetMem(addr++));
wcscat(debug_wstr, chr);
} else
{
if ((addr + size) > 0xFFFF)
{
while (addr < 0xFFFF)
{
swprintf(chr, L"%02X OVERFLOW\n", GetMem(addr++));
wcscat(debug_wstr, chr);
}
break;
}
for (int j = 0; j < size; j++)
{
swprintf(chr, L"%02X ", opcode[j] = GetMem(addr++));
wcscat(debug_wstr, chr);
}
while (size < 3)
{
wcscat(debug_wstr, L" "); //pad output to align ASM
size++;
}
static char bufferForDisassemblyWithPlentyOfStuff[64+NL_MAX_NAME_LEN*10]; //"plenty"
char* _a = Disassemble(addr, opcode);
strcpy(bufferForDisassemblyWithPlentyOfStuff, _a);
if (symbDebugEnabled)
{
replaceNames(ramBankNames, bufferForDisassemblyWithPlentyOfStuff, &disassembly_operands[i]);
for(int p=0;p<ARRAY_SIZE(pageNames);p++)
if(pageNames[p] != NULL)
replaceNames(pageNames[p], bufferForDisassemblyWithPlentyOfStuff, &disassembly_operands[i]);
}
// special case: an RTS opcode
if (GetMem(instruction_addr) == 0x60)
{
// add "----------" to emphasize the end of subroutine
strcat(bufferForDisassemblyWithPlentyOfStuff, " ");
for (int j = strlen(bufferForDisassemblyWithPlentyOfStuff); j < (LOG_DISASSEMBLY_MAX_LEN - 1); ++j)
bufferForDisassemblyWithPlentyOfStuff[j] = '-';
bufferForDisassemblyWithPlentyOfStuff[LOG_DISASSEMBLY_MAX_LEN - 1] = 0;
}
// append the disassembly to current line
swprintf(debug_wbuf, L" %S", bufferForDisassemblyWithPlentyOfStuff);
wcscat(debug_wstr, debug_wbuf);
}
wcscat(debug_wstr, L"\n");
instructions_count++;
}
UpdateDisassembleView(hWnd, id, lines, true);
// fill the left panel data
debug_cdl_str[0] = 0;
if (cdloggerdataSize)
{
uint8 cdl_data;
lines = disassembly_addresses.size();
for (int i = 0; i < lines; ++i)
{
instruction_addr = GetNesFileAddress(disassembly_addresses[i]) - 16;
if (instruction_addr >= 0 && instruction_addr < cdloggerdataSize)
{
cdl_data = cdloggerdata[instruction_addr] & 3;
if (cdl_data == 3)
strcat(debug_cdl_str, "cd\r\n"); // both Code and Data
else if (cdl_data == 2)
strcat(debug_cdl_str, " d\r\n"); // Data
else if (cdl_data == 1)
strcat(debug_cdl_str, "c\r\n"); // Code
else
strcat(debug_cdl_str, "\r\n"); // not logged
} else
{
strcat(debug_cdl_str, "\r\n"); // cannot be logged
}
}
}
SetDlgItemText(hWnd, IDC_DEBUGGER_DISASSEMBLY_LEFT_PANEL, debug_cdl_str);
}
void PrintOffsetToSeekAndBookmarkFields(int offset)
{
if (offset >= 0 && hDebug)
{
char offsetBuffer[5];
sprintf(offsetBuffer, "%04X", offset);
// send the address to "Seek To" field
SetDlgItemText(hDebug, IDC_DEBUGGER_VAL_PCSEEK, offsetBuffer);
// send the address to "Bookmark Add" field
SetDlgItemText(hDebug, IDC_DEBUGGER_BOOKMARK, offsetBuffer);
}
}
char *DisassembleLine(int addr) {
static char str[64]={0},chr[25]={0};
char *c;
int size,j;
uint8 opcode[3];
sprintf(str, "%02X:%04X: ", getBank(addr),addr);
size = opsize[GetMem(addr)];
if (size == 0)
{
sprintf(chr, "%02X UNDEFINED", GetMem(addr++));
strcat(str,chr);
}
else {
if ((addr+size) > 0x10000) {
sprintf(chr, "%02X OVERFLOW", GetMem(addr));
strcat(str,chr);
}
else {
for (j = 0; j < size; j++) {
sprintf(chr, "%02X ", opcode[j] = GetMem(addr++));
strcat(str,chr);
}
while (size < 3) {
strcat(str," "); //pad output to align ASM
size++;
}
strcat(strcat(str," "),Disassemble(addr,opcode));
}
}
if ((c=strchr(str,'='))) *(c-1) = 0;
if ((c=strchr(str,'@'))) *(c-1) = 0;
return str;
}
char *DisassembleData(int addr, uint8 *opcode) {
static char str[64]={0},chr[25]={0};
char *c;
int size,j;
sprintf(str, "%02X:%04X: ", getBank(addr), addr);
size = opsize[opcode[0]];
if (size == 0)
{
sprintf(chr, "%02X UNDEFINED", opcode[0]);
strcat(str,chr);
} else
{
if ((addr+size) > 0x10000)
{
sprintf(chr, "%02X OVERFLOW", opcode[0]);
strcat(str,chr);
} else
{
for (j = 0; j < size; j++)
{
sprintf(chr, "%02X ", opcode[j]);
addr++;
strcat(str,chr);
}
while (size < 3)
{
strcat(str," "); //pad output to align ASM
size++;
}
strcat(strcat(str," "),Disassemble(addr,opcode));
}
}
if ((c=strchr(str,'='))) *(c-1) = 0;
if ((c=strchr(str,'@'))) *(c-1) = 0;
return str;
}
int GetEditHex(HWND hwndDlg, int id) {
char str[9];
int tmp;
GetDlgItemText(hwndDlg,id,str,9);
tmp = strtol(str,NULL,16);
return tmp;
}
int *GetEditHexData(HWND hwndDlg, int id){
static int data[31];
char str[60];
int i,j, k;
GetDlgItemText(hwndDlg,id,str,60);
memset(data,0,31*sizeof(int));
j=0;
for(i = 0;i < 60;i++){
if(str[i] == 0)break;
if((str[i] >= '0') && (str[i] <= '9'))j++;
if((str[i] >= 'A') && (str[i] <= 'F'))j++;
if((str[i] >= 'a') && (str[i] <= 'f'))j++;
}
j=j&1;
for(i = 0;i < 60;i++){
if(str[i] == 0)break;
k = -1;
if((str[i] >= '0') && (str[i] <= '9'))k=str[i]-'0';
if((str[i] >= 'A') && (str[i] <= 'F'))k=(str[i]-'A')+10;
if((str[i] >= 'a') && (str[i] <= 'f'))k=(str[i]-'a')+10;
if(k != -1){
if(j&1)data[j>>1] |= k;
else data[j>>1] |= k<<4;
j++;
}
}
data[j>>1]=-1;
return data;
}
void UpdateRegs(HWND hwndDlg) {
if (DebuggerWasUpdated) {
X.A = GetEditHex(hwndDlg,IDC_DEBUGGER_VAL_A);
X.X = GetEditHex(hwndDlg,IDC_DEBUGGER_VAL_X);
X.Y = GetEditHex(hwndDlg,IDC_DEBUGGER_VAL_Y);
X.PC = GetEditHex(hwndDlg,IDC_DEBUGGER_VAL_PC);
}
}
///indicates whether we're under the control of the debugger
bool inDebugger = false;
//this code enters the debugger when a breakpoint was hit
void FCEUD_DebugBreakpoint(int bp_num)
{
// log the Breakpoint Hit into Trace Logger log if needed
if (logging)
{
log_old_emu_paused = false; // force Trace Logger update
if (logging_options & LOG_MESSAGES)
{
char str_temp[500];
if (bp_num >= 0)
{
// normal breakpoint
sprintf(str_temp, "Breakpoint %u Hit at $%04X: ", bp_num, X.PC);
strcat(str_temp, BreakToText(bp_num));
//watchpoint[num].condText
OutputLogLine(str_temp);
} else if (bp_num == BREAK_TYPE_BADOP)
{
sprintf(str_temp, "Bad Opcode Breakpoint Hit at $%04X", X.PC);
OutputLogLine(str_temp);
} else if (bp_num == BREAK_TYPE_CYCLES_EXCEED)
{
sprintf(str_temp, "Breakpoint Hit at $%04X: cycles count %lu exceeds %lu", X.PC, (long)(timestampbase + timestamp - total_cycles_base), (long)break_cycles_limit);
OutputLogLine(str_temp);
} else if (bp_num == BREAK_TYPE_INSTRUCTIONS_EXCEED)
{
sprintf(str_temp, "Breakpoint Hit at $%04X: instructions count %lu exceeds %lu", X.PC, (long)total_instructions, (long)break_instructions_limit);
OutputLogLine(str_temp);
}
}
}
DoDebug(0);
UpdateOtherDebuggingDialogs(); // Keeps the debugging windows updating smoothly when stepping
// highlight the ">" line
if (bp_num != BREAK_TYPE_STEP)
if (beginningOfPCPointerLine >= 0)
SendMessage(GetDlgItem(hDebug, IDC_DEBUGGER_DISASSEMBLY), EM_SETSEL, beginningOfPCPointerLine + 1, beginningOfPCPointerLine + 8);
// highlight breakpoint
if (bp_num >= 0)
{
// highlight bp_num item in IDC_DEBUGGER_BP_LIST
SendDlgItemMessage(hDebug, IDC_DEBUGGER_BP_LIST, LB_SETCURSEL, (WPARAM)bp_num, 0);
EnableWindow(GetDlgItem(hDebug, IDC_DEBUGGER_BP_DEL), TRUE);
EnableWindow(GetDlgItem(hDebug, IDC_DEBUGGER_BP_EDIT), TRUE);
} else
{
// remove any selection from IDC_DEBUGGER_BP_LIST
SendDlgItemMessage(hDebug, IDC_DEBUGGER_BP_LIST, LB_SETCURSEL, (WPARAM)(-1), 0);
EnableWindow(GetDlgItem(hDebug, IDC_DEBUGGER_BP_DEL), FALSE);
EnableWindow(GetDlgItem(hDebug, IDC_DEBUGGER_BP_EDIT), FALSE);
// highlight IDC_DEBUGGER_VAL_CYCLES_COUNT or IDC_DEBUGGER_VAL_INSTRUCTIONS_COUNT if needed
if (bp_num == BREAK_TYPE_CYCLES_EXCEED)
SendMessage(GetDlgItem(hDebug, IDC_DEBUGGER_VAL_CYCLES_COUNT), EM_SETSEL, 0, -1);
else if (bp_num == BREAK_TYPE_INSTRUCTIONS_EXCEED)
SendMessage(GetDlgItem(hDebug, IDC_DEBUGGER_VAL_INSTRUCTIONS_COUNT), EM_SETSEL, 0, -1);
}
void win_debuggerLoop(); // HACK to let user interact with the Debugger while emulator isn't updating
win_debuggerLoop();
// since we unfreezed emulation, reset delta_cycles counter
ResetDebugStatisticsDeltaCounters();
}
void UpdateBreakpointsCaption()
{
char str[32];
// calculate the number of enabled breakpoints
int tmp = 0;
for (int i = 0; i < numWPs; i++)
if (watchpoint[i].flags & WP_E)
tmp++;
sprintf(str, "Breakpoints %02X of %02X", tmp, numWPs);
SetDlgItemText(hDebug, IDC_DEBUGGER_BREAKPOINTS, str);
}
void UpdateDebugger(bool jump_to_pc)
{
//don't do anything if the debugger is not visible
if(!hDebug)
return;
//but if the debugger IS visible, then focus it
ShowWindow(hDebug, SW_SHOWNORMAL);
SetForegroundWindow(hDebug);
char str[512] = {0}, str2[512] = {0}, chr[8];
int tmp, ret, i, starting_address;