-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisassembler.cpp
1249 lines (1106 loc) · 37.2 KB
/
Disassembler.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
//============================================================================
// Copyright 2015 by Joseph Forgione
// This file is part of VCC (Virtual Color Computer).
//
// VCC 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 3 of the License, or
// (at your option) any later version.
//
// VCC 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 VCC (Virtual Color Computer). If not, see
// <http://www.gnu.org/licenses/>.
//
// Disassembly Display - Part of the Debugger package for VCC
// Author: Ed Jaquay
//============================================================================
#include <Windows.h>
#include <Windowsx.h>
#include <commdlg.h>
#include <Richedit.h>
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include "defines.h"
#include "vcc.h"
#include "resource.h"
#include "logger.h"
#include "tcc1014mmu.h"
#include "Debugger.h"
#include "DebuggerUtils.h"
#include "OpDecoder.h"
#include "Disassembler.h"
#include "Audio.h"
#define HEXSTR(i,w) Debugger::ToHexString(i,w,true)
#define MAXLINES 0x1000
namespace VCC {
/**************************************************/
/* Local Function Templates */
/**************************************************/
// Dialogs
INT_PTR CALLBACK DisassemblerDlgProc(HWND,UINT,WPARAM,LPARAM);
INT_PTR CALLBACK BreakpointsDlgProc(HWND,UINT,WPARAM,LPARAM);
// Functions used to subclass controls
LRESULT CALLBACK SubEditDlgProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK SubTextDlgProc(HWND,UINT,WPARAM,LPARAM);
WNDPROC AddrDlgProc;
WNDPROC TextDlgProc;
// Handler for address and count edit boxes
BOOL ProcEditDlg(WNDPROC,HWND,UINT,WPARAM,LPARAM);
// Handler for disassembly text richedit control
BOOL ProcTextDlg(WNDPROC,HWND,UINT,WPARAM,LPARAM);
// Addressing mode and converters
void ToggleAddrMode();
int CpuBlock(int);
int CpuToReal(int);
int RealToCpu(int);
int HexToUint(char *);
// Disassembler and helpers
void DecodeAddr();
void Disassemble(unsigned short, unsigned short);
int DecodeModHdr(unsigned short, unsigned short, std::string *mhdr);
// Establish text line
int FindAdrLine(int);
void SetCurrentLine();
// Breakpoint control
void RemoveHaltpoint(int);
void SetHaltpoint(bool);
void RefreshHPlist();
void ListHaltpoints();
void FindHaltpoints();
bool IsBreakpoint(int);
void TrackPC();
void UnHilitePC();
void HiliteLine(int,int,int);
// Information and error line
void PutStatTxt(const char *, int);
void SetInfoStat();
int errDisplayTimer = 0;
// String functions used for decode
std::string PadRight(std::string const&,size_t);
std::string OpFDB(int,std::string,std::string,std::string);
std::string OpFCB(int,std::string,std::string);
std::string FmtLine(int,std::string,std::string,std::string,std::string);
/**************************************************/
/* Static Variables */
/**************************************************/
CriticalSection Section_;
HINSTANCE hVccInst;
// Dialog and control handles
HWND hDismDlg = NULL; // Disassembler Dialog
HWND hBrkpDlg = NULL; // Breakpoints Dialog
HWND hEdtAddr = NULL; // From editbox
HWND hEdtAPPY = NULL; // Apply button
HWND hDisText = NULL; // Richedit20 box for output
HWND hInfText = NULL; // Info and error text box
HWND hRealBtn = NULL;
// RealAdrMode indicates what addressing was used for decode
bool RealAdrMode = FALSE;
// Os9Decode indicates block and offset used for start address
bool Os9Decode = FALSE;
// Line highlight status
int TrackedPC = -1;
int PClinePos = 0;
bool TrackingEnabled = true;
// MMUregs at time of the last decode
MMUState MMUregs;
// Text colors [black, red, blue, magneta]
COLORREF Colors[4] = {RGB(0,0,0),RGB(255,0,0),RGB(0,0,255),RGB(195,0,195)};
// Default Info/Text line content, current brush and color
HBRUSH hInfoBrush = NULL;
int InfoColorNum = 0;
// Decode data
std::string sDecoded = {}; // Disassembly string
int NumDisLines = 0; // Number of lines in disassembly;
int DisLinePos[MAXLINES]; // Start positions of disassembly lines
int DisLineAdr[MAXLINES]; // Instruction addresses of disassembly lines
int CurrentLineNum=-1; // CurrentLineNum selected. (-1 == none)
// mHaltpoints container.
// A new style breakpoint is named 'Haltpoint' to avoid conflict
// with the breakpoints structure used by the source code debugger.
// The real address and instruction at the breakpoint are saved.
// Haltpoints are stored in Haltpoints std::map container. Haltpoints
// are active when the cpu is in run state and are inactive
// when the cpu is halted.
struct Haltpoint
{
int addr; // Real address
int lpos; // Char index into text
unsigned char instr;
bool placed;
bool exists;
};
std::map<int,Haltpoint> mHaltpoints{};
void ApplyHaltPoint(Haltpoint &,bool);
// Help text
char DbgHelp[] =
"'Real Address' checkbox sets Real vs CPU Addressing.\n"
"'Address' edit box is where the address to decode is set.\n"
"'Decode' (or Enter key) decodes from the set address.\n"
"'Os9 mode' checkbox selects OS9 module disassembly.\n"
"'Auto Track PC' checkbox selects PC tracking (default is set).\n"
"PC tracking is only active when VCC is paused.\n\n"
"In Real mode Address can be a block num followed by offset\n"
"and they will be converted to an absolute real address\n\n"
"The following hot keys can be used:\n\n"
" 'P' Pause the CPU.\n"
" 'G' Go - unpause the CPU.\n"
" 'S' Step to next instruction while paused.\n"
" 'B' Set breakpoint at selected line.\n"
" 'R' Remove breakpoint at selected line.\n"
" 'L' Start Breakpoints list window.\n"
" 'M' Toggle between real and CPU address mode.\n"
" 'T' Toggle CPU tracking ON/OFF.\n"
" 'I' Shows Processor State window.\n"
" 'K' Removes (kills) all breakpoints.\n";
/**************************************************/
/* Create Disassembler Dialog Window */
/**************************************************/
void
OpenDisassemblerWindow(HINSTANCE hInstance, HWND hParent)
{
hVccInst = hInstance;
if (hDismDlg == NULL) {
hDismDlg = CreateDialog ( hInstance,
MAKEINTRESOURCE(IDD_DISASSEMBLER),
hParent,
DisassemblerDlgProc );
ShowWindow(hDismDlg, SW_SHOWNORMAL);
}
SetFocus(hEdtAddr);
}
/**************************************************/
/* Dialog Processing */
/**************************************************/
INT_PTR CALLBACK DisassemblerDlgProc
(HWND hDlg,UINT msg,WPARAM wPrm,LPARAM lPrm)
{
switch (msg) {
case WM_INITDIALOG:
// Grab control handles
hEdtAddr = GetDlgItem(hDlg, IDC_EDIT_PC_ADDR);
hEdtAPPY = GetDlgItem(hDlg, IDAPPLY);
hDisText = GetDlgItem(hDlg, IDC_DISASSEMBLY_TEXT);
hInfText = GetDlgItem(hDlg, IDC_ERROR_TEXT);
// Hook (subclass) text controls
AddrDlgProc = (WNDPROC) GetWindowLongPtr(hEdtAddr,GWLP_WNDPROC);
TextDlgProc = (WNDPROC) GetWindowLongPtr(hDisText,GWLP_WNDPROC);
SetWindowLongPtr(hEdtAddr,GWLP_WNDPROC,(LONG_PTR) SubEditDlgProc);
SetWindowLongPtr(hDisText,GWLP_WNDPROC,(LONG_PTR) SubTextDlgProc);
// Set Consolas font (fixed) in disassembly edit box
CHARFORMAT disfmt;
disfmt.cbSize = sizeof(disfmt);
SendMessage(hDisText,EM_GETCHARFORMAT,(WPARAM) SCF_DEFAULT,(LPARAM) &disfmt);
strcpy(disfmt.szFaceName,"Consolas");
disfmt.yHeight=180;
SendMessage(hDisText,EM_SETCHARFORMAT,(WPARAM) SCF_DEFAULT,(LPARAM) &disfmt);
SendMessage(hDisText,EM_SETBKGNDCOLOR,0,(LPARAM)RGB(255,255,255));
// Inital settings
SetWindowTextA(hDisText,"");
TrackingEnabled = true;
Button_SetCheck(GetDlgItem(hDlg,IDC_BTN_AUTO),BST_CHECKED);
SetInfoStat();
// Number of lines in disassembly;
NumDisLines = 0;
// Start a timer for showing current status
SetTimer(hDlg, IDT_BRKP_TIMER, 250, (TIMERPROC) NULL);
break;
case WM_PAINT:
// Give edit control focus
SetFocus(hEdtAddr);
SendMessage(hEdtAddr,EM_SETSEL,0,-1); //Select contents
return FALSE;
case WM_CTLCOLORSTATIC:
if ((HWND)lPrm == hInfText) {
HDC hdc = (HDC) wPrm;
if (hInfoBrush==NULL)
hInfoBrush = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
SetTextColor(hdc,Colors[InfoColorNum]);
SetBkColor(hdc,GetSysColor(COLOR_3DFACE));
return (INT_PTR) hInfoBrush;
}
break;
case WM_COMMAND:
switch (LOWORD(wPrm)) {
case IDCLOSE:
case WM_DESTROY:
KillHaltpoints();
EmuState.Debugger.Enable_Halt(false);
DestroyWindow(hDlg);
hDismDlg = NULL;
return FALSE;
case IDAPPLY:
DecodeAddr();
SetFocus(hDisText);
return TRUE;
case IDC_BTN_OS9:
if (IsDlgButtonChecked(hDismDlg,IDC_BTN_OS9)==BST_CHECKED) {
Os9Decode = TRUE;
} else {
Os9Decode = FALSE;
}
SetFocus(hEdtAddr);
return TRUE;
case IDC_BTN_REAL:
if (IsDlgButtonChecked(hDlg,IDC_BTN_REAL) == BST_CHECKED)
if (EmuState.Debugger.IsHalted() && TrackingEnabled)
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_REAL),BST_UNCHECKED);
else
RealAdrMode = TRUE;
else
RealAdrMode = FALSE;
SetInfoStat();
SetFocus(hEdtAddr);
return TRUE;
case IDC_BTN_AUTO:
if (IsDlgButtonChecked(hDlg,IDC_BTN_AUTO)==BST_CHECKED) {
TrackingEnabled = TRUE;
} else {
TrackingEnabled = FALSE;
}
SetInfoStat();
SetFocus(hEdtAddr);
return TRUE;
case IDC_BTN_HELP:
MessageBox(hDismDlg,DbgHelp,"Usage",0);
SetFocus(hEdtAddr);
return TRUE;
}
break;
case WM_TIMER:
if (errDisplayTimer > 0) {
errDisplayTimer--;
if (errDisplayTimer == 0)
SetInfoStat();
}
if (EmuState.Debugger.IsHalted() && TrackingEnabled)
TrackPC();
else
UnHilitePC();
return TRUE;
}
return FALSE;
}
/***************************************************/
/* Process edit control messages */
/***************************************************/
LRESULT CALLBACK SubEditDlgProc(HWND hCtl,UINT msg,WPARAM wPrm,LPARAM lPrm)
{
char ch;
switch (msg) {
case WM_CHAR:
ch = toupper(wPrm);
// Hex digits made uppercase in edit text
if (strchr("0123456789ABCDEF ",ch)) {
wPrm = ch;
break;
}
// Hot keys are sent to disassembly text window
if (strchr("GPSLMIKT",ch)) {
SendMessage(hDisText,msg,ch,lPrm);
SetFocus(hDisText);
return TRUE;
}
// Other characters
switch (ch) {
// Enter does the disassembly
case VK_RETURN:
DecodeAddr();
SetFocus(hDisText);
SetCurrentLine();
return TRUE;
// keys used by edit control
case VK_DELETE:
case VK_BACK:
case VK_LEFT:
case VK_RIGHT:
break;
default:
return TRUE;
}
break;
case WM_KEYDOWN:
switch (wPrm) {
case VK_DOWN:
case VK_UP:
SetFocus(hDisText);
SendMessage(hDisText,msg,wPrm,lPrm);
return TRUE;
}
break;
case WM_KEYUP:
switch (wPrm) {
case VK_DOWN:
case VK_UP:
SetFocus(hDisText);
SendMessage(hDisText,msg,wPrm,lPrm);
return TRUE;
}
break;
}
// Everything else sent to original control processing
if (hCtl == hEdtAddr)
return CallWindowProc(AddrDlgProc,hCtl,msg,wPrm,lPrm);
return TRUE;
}
/***************************************************/
/* Process Disassembly window messages */
/***************************************************/
LRESULT CALLBACK SubTextDlgProc(HWND hCtl,UINT msg,WPARAM wPrm,LPARAM lPrm)
{
switch (msg) {
case WM_CHAR:
switch (toupper(wPrm)) {
case 'B':
SetHaltpoint(true);
return TRUE;
// Remove Breakpoint
case 'R':
SetHaltpoint(false);
return TRUE;
// Show Procesor State Window
case 'I':
SendMessage(GetWindow(hDismDlg,GW_OWNER),
WM_COMMAND,ID_PROCESSOR_STATE,0);
SetFocus(hEdtAddr);
return TRUE;
// Remove all haltpoints
case 'K':
KillHaltpoints();
return TRUE;
// List haltpoints
case 'L':
ListHaltpoints();
return TRUE;
case 'M':
if (!(EmuState.Debugger.IsHalted() && TrackingEnabled))
ToggleAddrMode();
return TRUE;
// Toggle pause go
case 'G':
if (EmuState.Debugger.IsHalted()) {
PauseAudio(0);
EmuState.Debugger.QueueRun();
}
return TRUE;
case 'P':
if (!EmuState.Debugger.IsHalted()) {
EmuState.Debugger.QueueHalt();
PauseAudio(1);
}
return TRUE;
// Step
case 'S':
if (EmuState.Debugger.IsHalted() && TrackingEnabled)
EmuState.Debugger.QueueStep();
UnHilitePC();
return TRUE;
case 'T':
if (TrackingEnabled) {
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_AUTO),BST_UNCHECKED);
TrackingEnabled = false;
} else {
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_AUTO),BST_CHECKED);
TrackingEnabled = true;
}
return TRUE;
case VK_TAB:
SetFocus(hEdtAddr);
return TRUE;
}
break;
case WM_LBUTTONUP:
SetCurrentLine();
break;
case WM_KEYUP:
switch (wPrm) {
case VK_PRIOR:
case VK_NEXT:
case VK_DOWN:
case VK_UP:
SetCurrentLine();
break;
}
break;
}
// Forward messages to original control
return CallWindowProc(TextDlgProc,hCtl,msg,wPrm,lPrm);
}
/**************************************************/
/* Breakpoints list Dialog Processing */
/**************************************************/
INT_PTR CALLBACK BreakpointsDlgProc
(HWND hDlg,UINT msg,WPARAM wPrm,LPARAM lPrm)
{
int sel;
HWND hList;
char buf[64];
switch (msg) {
case WM_COMMAND:
switch (LOWORD(wPrm)) {
case IDCLOSE:
case WM_DESTROY:
DestroyWindow(hDlg);
hBrkpDlg = NULL;
break;
case IDC_BTN_DEL_BREAKPOINT:
hList = GetDlgItem(hDlg,IDC_LIST_BREAKPOINTS);
sel = SendMessage(hList,LB_GETCURSEL,0,0);
SendMessage(hList,LB_GETTEXT, sel, (LPARAM) &buf);
buf[6]='\0';
RemoveHaltpoint(HexToUint(buf));
RefreshHPlist();
return true;
break;
}
}
return false;
}
/**************************************************/
/* Put Info text */
/**************************************************/
void PutStatTxt(const char * txt, int cnum) {
InfoColorNum = cnum & 3;
if (InfoColorNum > 0) errDisplayTimer = 8;
SendMessage(hDismDlg,WM_CTLCOLORSTATIC,
(WPARAM) GetDC(hInfText),(LPARAM) hInfText);
SetWindowTextA(hInfText,txt);
}
/**************************************************/
/* Set Info text */
/**************************************************/
void SetInfoStat() {
if (EmuState.Debugger.IsHalted() && TrackingEnabled) {
PutStatTxt("Disassembly is tracking the PC",0);
} else if (RealAdrMode) {
PutStatTxt("Enter address or block and offset",0);
} else {
PutStatTxt("Enter Hex address",0);
}
}
/***************************************************/
/* Toggle address mode between CPU and Real */
/***************************************************/
void ToggleAddrMode()
{
char buf[16];
int addr;
int tmpadr;
// Get real or CPU address of top line
GetWindowText(hEdtAddr, buf, 8);
addr = HexToUint(buf);
// Map address to CPU if possible
if (RealAdrMode) {
// Convert to CPU address
tmpadr = RealToCpu(addr);
if (tmpadr < 0) {
PutStatTxt("Address not Mapped by MMU",1);
return;
}
addr = tmpadr;
RealAdrMode = false;
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_REAL),BST_UNCHECKED);
// Map address to real
} else {
// Convert to Real address
addr = CpuToReal(addr);
RealAdrMode = true;
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_REAL),BST_CHECKED);
}
SetWindowText(hEdtAddr,HEXSTR(addr,0).c_str());
DecodeAddr();
}
/***************************************************/
/* Convert Cpu address to real address */
/***************************************************/
int CpuToReal(int cpuadr) {
int block = CpuBlock(cpuadr);
int offset = cpuadr & 0x1FFF;
return offset + block * 0x2000;
}
/***************************************************/
/* Get block from CPU address */
/***************************************************/
int CpuBlock(int cpuadr)
{
int mmublk = (cpuadr>>13) & 7;
if (MMUregs.ActiveTask == 0) {
return MMUregs.Task0[mmublk];
} else {
return MMUregs.Task1[mmublk];
}
}
/***************************************************/
/* Convert real address to cpu address */
/***************************************************/
int RealToCpu(int realaddr)
{
int offset = realaddr & 0x1FFF;
int block = (unsigned) realaddr >> 13;
int cpuadr;
int i;
for (i=0; i<8; i++) {
if (MMUregs.ActiveTask == 0) {
if (MMUregs.Task0[i] == block) break;
} else {
if (MMUregs.Task1[i] == block) break;
}
}
if (i < 8) {
cpuadr = i * 0x2000 + offset;
} else {
cpuadr = -1;
}
return cpuadr;
}
/*******************************************************/
/* Hilite a disassembly line */
/* */
/* lpos is line position in disassembly text */
/* cnum 0-3 black, red, blue, magneta */
/* flags.0 make bold */
/* flags.1 select line */
/*******************************************************/
void HiliteLine(int lpos, int cnum, int flags) {
DWORD SelMin;
DWORD SelMax;
// If select line save current focus else save selection
if (!(flags & 2))
SendMessage(hDisText,EM_GETSEL,(WPARAM) &SelMin,(LPARAM) &SelMax);
CHARFORMATA fmt;
fmt.cbSize = sizeof(fmt);
fmt.crTextColor = Colors[cnum & 3];
// Flags bit 0 make bold
if (flags & 1) {
fmt.dwEffects = CFE_BOLD;
} else {
fmt.dwEffects = 0;
}
fmt.dwMask = CFM_COLOR | CFM_BOLD;
SendMessage(hDisText,EM_SETSEL,lpos,lpos+36);
SendMessage(hDisText,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM) &fmt);
// Flags bit 1 select line
if (flags & 2) {
SetFocus(hDisText);
SendMessage(hDisText,EM_SETSEL,lpos,lpos);
// Move away from screen top or bottom if possible
int top = SendMessage(hDisText,EM_GETFIRSTVISIBLELINE,0,0);
int sel = SendMessage(hDisText,EM_LINEFROMCHAR,-1,0);
if (sel == (top + 32)) {
SendMessage(hDisText,EM_LINESCROLL,0,(LPARAM) 1);
} else if (sel > (top + 32)) {
SendMessage(hDisText,EM_LINESCROLL,0,(LPARAM) 4);
} else if ((sel < (top + 8)) & (top > 0)) {
SendMessage(hDisText,EM_LINESCROLL,0,(LPARAM) -4);
}
} else {
SendMessage(hDisText,EM_SETSEL,(WPARAM) SelMin,(LPARAM) SelMax);
}
}
/*******************************************************/
/* Find address match in disassembly */
/*******************************************************/
int FindAdrLine(int adr)
{
// If someone is bored this could be a binary search
for (int line=0; line < NumDisLines; line++) {
if (DisLineAdr[line] == adr) return line;
}
return MAXLINES; // Indicates failure
}
/*******************************************************/
/* Test for breakpoint at real address */
/*******************************************************/
bool IsBreakpoint(int realaddr)
{
return (mHaltpoints.count(realaddr) == 1);
}
/*******************************************************/
/* Find and highlight all haltpoints */
/*******************************************************/
void FindHaltpoints()
{
std::map<int, Haltpoint>::iterator it = mHaltpoints.begin();
while (it != mHaltpoints.end()) {
int adr = it->first;
// If decoded with CPU addressing convert haltpoint to CPU address
if (!RealAdrMode) {
adr = RealToCpu(adr);
if (!MemCheckWrite(adr)) return; // In case unwritable
}
if (adr >= 0) {
// Search for match in disassembly
int line = FindAdrLine(adr);
if (line < MAXLINES) {
Haltpoint hp = it->second;
hp.lpos = DisLinePos[line];
HiliteLine(hp.lpos,1,1);
}
}
it++;
}
}
/**************************************************/
/* Track the Current PC. This gets called on */
/* timer if tracking enabled and VCC is paused */
/**************************************************/
void TrackPC()
{
// Get the halted PC
CPUState state = CPUGetState();
int CPUadr = state.PC;
// If PC already highlighted just return
if (TrackedPC == CPUadr) return;
// Turn off Real addressing and set cpu address in address field
RealAdrMode = false;
Button_SetCheck(GetDlgItem(hDismDlg,IDC_BTN_REAL),BST_UNCHECKED);
SetWindowText(hEdtAddr,HEXSTR(CPUadr,0).c_str());
// Remove highlight from previous PC line
UnHilitePC();
// Search for match in disassembly
int line = FindAdrLine(CPUadr);
// If CPU found highlight it
if (line < MAXLINES) {
TrackedPC = CPUadr;
PClinePos = DisLinePos[line];
// Landed on breakpoint?
if (IsBreakpoint(CpuToReal(CPUadr))) {
HiliteLine(PClinePos,3,3); // Magneta
} else {
HiliteLine(PClinePos,2,3); // Blue
}
// Not found disassemble starting from PC
// PC should get painted on next timer event
} else {
DecodeAddr();
TrackedPC = -1;
}
}
/**************************************************/
/* Remove PC highlite */
/**************************************************/
void UnHilitePC()
{
if (TrackedPC >= 0) {
if (IsBreakpoint(CpuToReal(TrackedPC))) {
HiliteLine(PClinePos,1,1);
} else {
HiliteLine(PClinePos,0,0);
}
TrackedPC = -1;
}
}
/***************************************************/
/* Apply or Unapply a single Halt Point */
/***************************************************/
void ApplyHaltPoint(Haltpoint &hp,bool flag)
{
SectionLocker lock(Section_);
if (flag) {
if (!hp.placed) {
// Be sure really not placed
int instr = GetMem(hp.addr);
if (instr != 0x15) {
hp.instr = (unsigned char) instr;
SetMem(hp.addr,0x15);
}
hp.placed = true;
}
} else {
if (hp.placed) {
SetMem(hp.addr, hp.instr);
hp.placed = false;
}
}
return;
}
/***************************************************/
/* Delete Halt Point at real address */
/***************************************************/
void RemoveHaltpoint(int realaddr)
{
Haltpoint hp = mHaltpoints[realaddr];
if (hp.exists) {
HiliteLine(hp.lpos,0,0);
ApplyHaltPoint(hp,false);
}
mHaltpoints.erase(realaddr);
}
/***************************************************/
/* Set Halt Point at Current Line */
/***************************************************/
void SetHaltpoint(bool flag)
{
HWND hDisText = NULL; // Richedit20 box for output
std::string s;
Haltpoint hp;
int realaddr;
if ((CurrentLineNum < 0) || (CurrentLineNum >= MAXLINES))
return;
int lpos = DisLinePos[CurrentLineNum];
int addr = DisLineAdr[CurrentLineNum];
if (RealAdrMode) {
realaddr = addr;
} else {
if (MemCheckWrite(addr)) {
realaddr = CpuToReal(addr);
} else {
PutStatTxt("Can't set breakpoint here",1);
return;
}
}
// Create or fetch existing haltpoint
hp = mHaltpoints[realaddr];
// Update Haltpoint
if (flag) {
hp.lpos = lpos;
hp.exists = true;
hp.addr = realaddr;
ApplyHaltPoint(hp,true);
mHaltpoints[realaddr] = hp;
HiliteLine(lpos,1,1);
EmuState.Debugger.Enable_Halt(true);
// Remove Haltpoint
} else {
RemoveHaltpoint(realaddr);
}
// Refresh setpoints listbox
RefreshHPlist();
// Timer will apply PC highlight as required
if (PClinePos == hp.lpos) UnHilitePC();
return;
}
/*******************************************************/
/* Refresh haltpoints in ListBox */
/*******************************************************/
void RefreshHPlist()
{
if (hBrkpDlg == NULL) return;
SendDlgItemMessage(hBrkpDlg,IDC_LIST_BREAKPOINTS,LB_RESETCONTENT,0,0);
if (mHaltpoints.size() > 0) {
std::map<int, Haltpoint>::iterator it = mHaltpoints.begin();
while (it != mHaltpoints.end()) {
int realaddr = it->first;
Haltpoint hp = it->second;
std::string s = HEXSTR(realaddr,6)+"\t "+HEXSTR(hp.instr,2);
SendDlgItemMessage(hBrkpDlg, IDC_LIST_BREAKPOINTS,
LB_ADDSTRING, 0, (LPARAM) s.c_str());
it++;
}
}
}
/*******************************************************/
/* List haltpoints in DialogBox */
/*******************************************************/
void ListHaltpoints()
{
// Create breakpoints list dialog if required
if (hBrkpDlg == NULL) {
hBrkpDlg = CreateDialog ( hVccInst,
MAKEINTRESOURCE(IDD_BPLISTDIALOG),
hDismDlg, BreakpointsDlgProc );
ShowWindow(hBrkpDlg, SW_SHOWNORMAL);
SetFocus(hBrkpDlg);
}
RefreshHPlist();
return;
}
/*******************************************************/
/* Remove all haltpoints (public) */
/*******************************************************/
void KillHaltpoints()
{
std::map<int, Haltpoint>::iterator it = mHaltpoints.begin();
while (it != mHaltpoints.end()) {
int realaddr = it->first;
Haltpoint hp = it->second;
ApplyHaltPoint(hp,false);
HiliteLine(hp.lpos,0,0);
mHaltpoints.erase(realaddr);
it++;
}
UnHilitePC();
RefreshHPlist();
}
/*******************************************************/
/* Apply all haltpoints (public) */
/* Install HALTs or restore original opcodes */
/*******************************************************/
void ApplyHaltpoints(bool flag)
{
// Iterate over all defined haltpoints
std::map<int, Haltpoint>::iterator it = mHaltpoints.begin();
while (it != mHaltpoints.end()) {
int realaddr = it->first;
Haltpoint hp = it->second;
ApplyHaltPoint(hp,flag);
mHaltpoints[realaddr] = hp;
it++;
}
return;
}
/*********************************************************************/
/* Find position of current line in disassembly string and set caret */
/*********************************************************************/
void SetCurrentLine()
{
//TODO: This screws with scrolling
// Ignore find if text is selected
CHARRANGE range;
SendMessage(hDisText,EM_EXGETSEL,0,(LPARAM) &range);
if (range.cpMin!=range.cpMax) return;
// Find selected line number
int lnum = SendMessage(hDisText,EM_LINEFROMCHAR,range.cpMin,0);
int lpos = DisLinePos[lnum];
// If line number has changed set caret to start of line
if (lnum != CurrentLineNum) {
CurrentLineNum = lnum;
SendMessage(hDisText,EM_SETSEL,lpos,lpos);
}
//DEBUG
//int topline = SendMessage(hCtl,EM_GETFIRSTVISIBLELINE,0,0);
//PrintLogC("%d %d %X %d\n",lnum,DisLineAdr[lnum],lpos,topline);
}
/**************************************************/
/* Get user specified address and disassemble */
/**************************************************/
void DecodeAddr()
{
unsigned int adr;
unsigned int blk;
char buf[24];
GetWindowText(hEdtAddr, buf, 12);
char *p = buf;
// In real address mode allow user to optionally input a block
// number followed by an offset as per OS9 mdir -e command and
// convert these to the real address.
adr = blk = strtoul(p, &p, 16); // Convert hex to addr or block number
if (*p != 0) {
// Check for valid address mode
if (!RealAdrMode || (blk > 0x3FF)) {
PutStatTxt("Invalid address mode",1);
return;
}