-
Notifications
You must be signed in to change notification settings - Fork 4
/
wxMain.cpp
692 lines (622 loc) · 27.4 KB
/
wxMain.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
/************************************************************************
* Copyright (C) 2020-2021 Matthias Rosenthal
*
* 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 3 of the License.
*
* 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, see <https://www.gnu.org/licenses/>
***********************************************************************/
#include <cmath>
#include "wx/config.h"
#include "wx/textdlg.h"
#include "wx/clipbrd.h"
#include "wxMain.h"
#include "utils/utils_wx.h"
#include <boost/filesystem.hpp>
// sadly, there is no function in scintilla to prevent user modifications, but allow
// modifications by the program (SetReadOnly / SetEditable also prevent program's
// modifications). Therefore, we need to set the textCtrl to editable on each possible
// modification.
class wxSTCEditableLocker
{
private:
wxStyledTextCtrl *wxstc;
public:
wxSTCEditableLocker(wxStyledTextCtrl *wxstc) {
this->wxstc = wxstc;
wxstc->SetEditable(true);
}
~wxSTCEditableLocker() {
wxstc->SetEditable(false);
}
};
BEGIN_EVENT_TABLE(Dis8051Frame, wxFrame)
EVT_CLOSE(Dis8051Frame::OnClose)
EVT_MENU(idMenuQuit, Dis8051Frame::OnQuit)
EVT_MENU(idMenuAbout, Dis8051Frame::OnAbout)
EVT_MENU(idMenuSaveMetaFile, Dis8051Frame::OnSaveMetaFile)
EVT_MENU(idMenuGoto, Dis8051Frame::OnGoto)
EVT_MENU(idMenuFindPrevious, Dis8051Frame::OnFindBefore)
EVT_MENU(idMenuFindNext, Dis8051Frame::OnFindNext)
EVT_MENU(idMenuToggleCallAnnotation, Dis8051Frame::OnToggleCallAnnotation)
EVT_MENU(idMenuReadAddressFromCode, Dis8051Frame::OnReadAddressFromCode)
EVT_TIMER(idTimerUpdateUI, Dis8051Frame::OnTimerUpdateUI)
EVT_STC_UPDATEUI(idDisassemblyTextCtrl, Dis8051Frame::OnDisassemblyTextUpdated)
EVT_BUTTON(idButtonFindBefore, Dis8051Frame::OnFindBefore)
EVT_BUTTON(idButtonFindNext, Dis8051Frame::OnFindNext)
EVT_TEXT_ENTER(idTextCtrlComment, Dis8051Frame::OnCommentEnterPressed)
EVT_TEXT_ENTER(idTextCtrlFunction, Dis8051Frame::OnFunctionEnterPressed)
EVT_CHECKBOX(idCheckBoxFunctionShown, Dis8051Frame::OnToggleFunctionShown)
EVT_BUTTON(idButtonDeleteFunction, Dis8051Frame::OnFunctionDelete)
END_EVENT_TABLE()
Dis8051Frame::Dis8051Frame(wxFrame *frame, const wxString& title, std::string firmwareFile, std::string metaFile, Dis8051App *app)
: wxFrame(frame, -1, title),
timer_gui_update1(this, idTimerUpdateUI)
{
m_app = app;
gui_palette_init(c_gui_palette);
this->firmwareFile = firmwareFile;
this->metaFile = metaFile;
c_logger.reset(new logger::logger);
c_logger->LogInformation("started interactive 8051 disassembler, logging to log.txt");
c_logger->AddLogDisplay("log.txt");
disassembly = std::make_shared<disas8051::Disassembly>(c_logger.get());
std::string bufString;
if(utils::GetFileString(firmwareFile, &bufString, true)) {
c_logger->LogInformation("Loaded " + std::to_string(bufString.length()) + " bytes from " + firmwareFile);
disassembly->buf = utils::StdStringToUstring(bufString);
disas8051::Disassembler disassembler(c_logger.get());
if(disassembly->openMetaFile(metaFile) == 0) {
c_logger->LogInformation("Loaded metadata file " + metaFile);
}
else {
// create a new meta file
disassembly->functions[0] = disas8051::Function{"start"};
c_logger->LogInformation("Created new meta, starting at address 0");
}
c_logger->LogInformation("Disassembling...");
for(auto itFunction = disassembly->functions.begin(); itFunction != disassembly->functions.end(); itFunction++) {
disassembler.followCodePath(disassembly->buf, itFunction->first, disassembly.get());
}
c_logger->LogInformation("Resolving remapping calls...");
disassembly->resolveRemapCalls(&disassembler);
c_logger->LogInformation("Resolving comments...");
disassembly->resolveComments();
c_logger->LogInformation("Resolving function ends addresses...");
disassembly->resolveFunctionEndAddresses();
c_logger->LogInformation("Auto commenting...");
disassembly->autoComment();
c_logger->LogInformation("Processed firmware + metadata file.");
}
else {
c_logger->LogError("Could not open file " + firmwareFile);
wxMessageBox(wxString::Format(wxT("Error: Could not open firmware file %s"), wxString::FromUTF8(firmwareFile.c_str())));
}
// create a menu bar
wxMenuBar* mbar = new wxMenuBar();
wxMenu* fileMenu = new wxMenu(_T(""));
fileMenu->Append(idMenuSaveMetaFile, _("&Save metadata file\tCtrl-S"));
fileMenu->AppendSeparator();
fileMenu->Append(idMenuGoto, _("&Goto line\tCtrl-G"));
fileMenu->Append(idMenuFindNext, _("Find &next\tF3"));
fileMenu->Append(idMenuFindPrevious, _("Find &previous\tShift-F3"));
fileMenu->Append(idMenuToggleCallAnnotation, _("Toggle call annotation\tCtrl-K"), _("Toggle whether calling arrow from the current code to some function are displayed"));
fileMenu->Append(idMenuReadAddressFromCode, _("Clipboard address from code\tCtrl-R"), _("Queries an address and copies the 2 bytes from this address to clipboard"));
fileMenu->AppendSeparator();
fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
mbar->Append(fileMenu, _("&File"));
wxMenu* helpMenu = new wxMenu(_T(""));
helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
mbar->Append(helpMenu, _("&Help"));
SetMenuBar(mbar);
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
//wxPanel *panel_top = new wxPanel(this);
wxNotebook *notebook = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBK_DEFAULT);
wxPanel *panel_disassembly = new wxPanel(notebook);
notebook->AddPage(panel_disassembly, wxT("Disassembly"), false);
wxBoxSizer *wxsz_panel_disassembly = new wxBoxSizer(wxHORIZONTAL);
scroll_overview = new ScrollOverview(this, panel_disassembly, wxID_ANY);
wxsz_panel_disassembly->Add(scroll_overview, 0, wxALL | wxEXPAND, 2);
wxsw_main = new wxSplitterWindow(panel_disassembly, wxID_ANY);
wxPanel *panel_left = new wxPanel(wxsw_main);
wxBoxSizer *wxsz_left = new wxBoxSizer(wxVERTICAL);
wxSplitterWindow *wxsw_disassembly = new wxSplitterWindow(panel_left, wxID_ANY);
annotation_canvas = new AnnotationCanvas(this, wxsw_disassembly, wxID_ANY);
wxrt_disassembly = new wxStyledTextCtrl(wxsw_disassembly, idDisassemblyTextCtrl);
wxrt_disassembly->SetCaretLineVisible(true);
wxFont font(wxFontInfo().Family(wxFONTFAMILY_TELETYPE));
wxrt_disassembly->StyleSetFont((int)disas8051::Styles::DEFAULT, font);
wxrt_disassembly->StyleSetFont((int)disas8051::Styles::BUF_ADDRESS, font);
wxrt_disassembly->StyleSetForeground((int)disas8051::Styles::BUF_ADDRESS, wxColour(0, 255, 0));
wxrt_disassembly->StyleSetFont((int)disas8051::Styles::BYTES, font);
wxrt_disassembly->StyleSetForeground((int)disas8051::Styles::BYTES, wxColour(0, 100, 0));
wxrt_disassembly->StyleSetFont((int)disas8051::Styles::RET_INSTRUCTION, font);
wxrt_disassembly->StyleSetForeground((int)disas8051::Styles::RET_INSTRUCTION, wxColour(200, 0, 0));
wxrt_disassembly->StyleSetFont((int)disas8051::Styles::COMMENT, font);
wxrt_disassembly->StyleSetForeground((int)disas8051::Styles::COMMENT, wxColour(200, 0, 0));
annotation_canvas->lineHeight = font.GetPixelSize().GetHeight();
std::cout << "LineHeight: " << annotation_canvas->lineHeight << std::endl;
#ifdef _WIN32 // also 64-bit
annotation_canvas->lineHeight += 3; // no time to check why this is needed
#endif
c_logger->LogInformation("Generating text viewer content...");
disassembly->printToWx(wxrt_disassembly);
c_logger->LogInformation("Generated text viewer content.");
wxsw_disassembly->SplitVertically(annotation_canvas, wxrt_disassembly, 100);
wxsw_disassembly->SetMinimumPaneSize(10); // prevent unsplitting
wxsz_left->Add(wxsw_disassembly, 1, wxALL | wxEXPAND, 2);
wxStaticBoxSizer *wxsbz_current_line = new wxStaticBoxSizer(wxVERTICAL, panel_left, wxT("Current Line"));
wxBoxSizer *wxsz_comment = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *lbl_comment = new wxStaticText(panel_left, wxID_ANY, wxT("Comment: "));
wxsz_comment->Add(lbl_comment, 0, wxALL, 2);
wxtc_comment = new wxTextCtrl(panel_left, idTextCtrlComment, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
wxtc_comment->SetToolTip(wxT("press enter to create or edit a comment at the current instruction"));
wxsz_comment->Add(wxtc_comment, 1, wxALL, 2);
wxsbz_current_line->Add(wxsz_comment, 0, wxALL | wxEXPAND, 2);
wxBoxSizer *wxsz_function = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *lbl_function = new wxStaticText(panel_left, wxID_ANY, wxT("Function: "));
wxsz_function->Add(lbl_function, 0, wxALL, 2);
wxtc_function = new wxTextCtrl(panel_left, idTextCtrlFunction, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
wxtc_function->SetToolTip(wxT("press enter to create a function or rename a function at the current address"));
wxsz_function->Add(wxtc_function, 1, wxALL, 2);
wxc_function_shown = new wxCheckBox(panel_left, idCheckBoxFunctionShown, wxT("show"));
wxc_function_shown->SetToolTip(wxT("whether the function is shown in the function call graph"));
wxsz_function->Add(wxc_function_shown, 0, wxALL, 2);
wxButton *wxb_delete_function = new wxButton(panel_left, idButtonDeleteFunction, wxT("delete"));
wxsz_function->Add(wxb_delete_function, 0, wxALL, 2);
wxsbz_current_line->Add(wxsz_function, 0, wxALL | wxEXPAND, 2);
wxtc_function_blocks = new wxTextCtrl(panel_left, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
wxsbz_current_line->Add(wxtc_function_blocks, 0, wxALL | wxEXPAND, 2);
wxBoxSizer *wxsz_search = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *lbl_search = new wxStaticText(panel_left, wxID_ANY, wxT("Search: "));
wxsz_search->Add(lbl_search, 0, wxALL, 2);
wxtc_search = new wxTextCtrl(panel_left, idTextCtrlSearch);
wxsz_search->Add(wxtc_search, 1, wxALL, 2);
wxc_search_hex = new wxCheckBox(panel_left, wxID_ANY, wxT("hex"));
wxsz_search->Add(wxc_search_hex, 0, wxALL, 2);
wxButton *wxb_find_before = new wxButton(panel_left, idButtonFindBefore, wxT("Previous"));
wxsz_search->Add(wxb_find_before, 0, wxALL, 2);
wxButton *wxb_find_next = new wxButton(panel_left, idButtonFindNext, wxT("Next"));
wxsz_search->Add(wxb_find_next, 0, wxALL, 2);
wxsbz_current_line->Add(wxsz_search, 0, wxALL | wxEXPAND, 2);
wxtc_referalls = new wxTextCtrl(panel_left, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
wxsbz_current_line->Add(wxtc_referalls, 0, wxALL | wxEXPAND, 2);
wxsz_left->Add(wxsbz_current_line, 0, wxALL | wxEXPAND, 2);
panel_left->SetSizerAndFit(wxsz_left);
//wxPanel *panel_right = new wxPanel(wxsw_main);
function_graph = new FunctionGraph(this, wxsw_main, wxID_ANY);
function_graph->RefreshCache();
wxsw_main->SplitVertically(panel_left, function_graph, 500);
wxsw_main->SetMinimumPaneSize(10); // prevent unsplitting
wxsz_panel_disassembly->Add(wxsw_main, 1, wxALL | wxEXPAND, 2);
panel_disassembly->SetSizerAndFit(wxsz_panel_disassembly);
wxPanel *panel_log = new wxPanel(notebook);
notebook->AddPage( panel_log, wxT("Log"), false);
wxBoxSizer *wxsz_panel_log = new wxBoxSizer(wxVERTICAL);
wxTextCtrl* wxtc_log = new wxTextCtrl(panel_log, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(500, 200), wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
c_logger->AddLogDisplay(wxtc_log, &c_gui_palette);
wxsz_panel_log->Add(wxtc_log, 1, wxEXPAND | wxALL, 2);
panel_log->SetSizerAndFit(wxsz_panel_log);
notebook->SetSelection(0); // Activate the "Capture" tab
topSizer->Insert(0, notebook, wxSizerFlags(5).Expand().Border());
topSizer->Layout();
SetSizer(topSizer);
topSizer->Fit(this);
topSizer->SetSizeHints(this);
// create a status bar with some information about the used wxWidgets version
CreateStatusBar(2);
time_t now = time(NULL);
tm *info = localtime(&now);
if(info->tm_hour < 11)
SetStatusText(_("Good Morning"),0);
else if(info->tm_hour < 17)
SetStatusText(_("Good Afternoon"),0);
else
SetStatusText(_("Good Evening"),0);
SetStatusText(wxT("Copyright Matthias Rosenthal"), 1);
timer_gui_update1.Start(TIMER_GUI_UPDATE_INTERVAL_MS);
SetSize(wxSize(1200, 900));
wxrt_disassembly->SetEditable(false);
}
Dis8051Frame::~Dis8051Frame()
{
c_logger.reset();
gui_palette_end(c_gui_palette);
}
void Dis8051Frame::OnClose(wxCloseEvent &event)
{
std::cout << "stopping" << std::endl;
timer_gui_update1.Stop();
std::cout << "stopped." << std::endl;
Destroy();
}
void Dis8051Frame::OnQuit(wxCommandEvent &event)
{
std::cout << "stopping" << std::endl;
timer_gui_update1.Stop();
std::cout << "stopped." << std::endl;
Destroy();
}
void Dis8051Frame::OnAbout(wxCommandEvent &event)
{
wxString msg = wxT("Interactive 8051 disassembler\n"
"Currently targeted for ENE KB9012 (Keyboard Controller)\nCopyright Matthias Rosenthal\n") +
fn_wxbuildinfo(wxbuildinfof_long_f);
wxMessageBox(msg, _("Welcome to..."));
}
void Dis8051Frame::OnTimerUpdateUI(wxTimerEvent& event)
{
c_logger->updateLogDisplays();
}
void Dis8051Frame::OnSaveMetaFile(wxCommandEvent &event)
{
disassembly->saveMetaFile(metaFile);
messageGUI("Saved to " + metaFile);
}
void Dis8051Frame::OnDisassemblyTextUpdated(wxStyledTextEvent &event)
{
if(event.GetUpdated() & wxSTC_UPDATE_V_SCROLL) {
scroll_overview->Refresh();
annotation_canvas->Refresh();
}
if(event.GetUpdated() & wxSTC_UPDATE_SELECTION) {
OnDissassemblyCaretMove();
}
}
void Dis8051Frame::OnDissassemblyCaretMove()
{
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
if(itAddress != disassembly->addressByLine.end()) {
auto itInstruction = disassembly->instructions.find(itAddress->second);
if(itInstruction != disassembly->instructions.end()) {
wxtc_comment->SetValue(wxString::FromUTF8(itInstruction->second.comment.c_str()));
SetStatusText(wxString::FromUTF8(("Address: " + utils::Int_To_String_Hex(itAddress->second) +
", Bytes: " + fn_FormatToHex(disassembly->buf.substr(itAddress->second,
itInstruction->second.length), true)).c_str()), 0);
}
else {
wxtc_comment->SetValue(wxEmptyString);
SetStatusText(wxString::Format(wxT("Address: %5x"), (int)itAddress->second), 0);
}
auto itTarget = disassembly->jumpTargets.find(itAddress->second);
if(itTarget != disassembly->jumpTargets.end()) {
if(!itTarget->second.empty()) {
std::string references = "Referenced by " + getAddressDescription(itTarget->second[0]);
for(std::size_t i1 = 1; i1 < itTarget->second.size(); i1++) {
references += ", " + getAddressDescription(itTarget->second[i1]);
}
wxtc_referalls->SetValue(wxString::FromUTF8(references.c_str()));
}
else {
wxtc_referalls->SetValue(wxEmptyString);
}
}
else {
wxtc_referalls->SetValue(wxEmptyString);
}
auto itFunction = disassembly->findFunction(itAddress->second);
if(itFunction != disassembly->functions.end()) {
wxtc_function->SetValue(wxString::FromUTF8(itFunction->second.name.c_str()));
wxtc_function_blocks->SetValue(wxString::FromUTF8(itFunction->second.getBlockSummary().c_str()));
wxc_function_shown->SetValue(itFunction->second.isShownInGraph);
}
else {
wxtc_function->SetValue(wxEmptyString);
wxtc_function_blocks->SetValue(wxEmptyString);
wxc_function_shown->SetValue(false);
}
return;
}
wxtc_comment->SetValue(wxEmptyString);
wxtc_referalls->SetValue(wxEmptyString);
wxtc_function->SetValue(wxEmptyString);
}
void Dis8051Frame::OnOverviewLineClicked(int line)
{
wxrt_disassembly->SetFocus();
wxrt_disassembly->GotoLine(line);
}
void Dis8051Frame::OnFunctionClicked(uint64_t address, disas8051::Function *function)
{
int line = disassembly->findLineByAddress(address);
if(line < 0) {
c_logger->LogError("Line should have been existed");
return;
}
wxrt_disassembly->SetFocus();
wxrt_disassembly->GotoLine(line);
}
void Dis8051Frame::GetDisassemblyLinesOnScreen(int *lineBegin, int *lineEnd)
{
*lineBegin = wxrt_disassembly->GetFirstVisibleLine();
*lineEnd = *lineBegin + wxrt_disassembly->LinesOnScreen();
}
void Dis8051Frame::doSearch(bool previous)
{
if(wxc_search_hex->GetValue()) {
std::string hexstring = fn_wxUTF8String_ToUTF8String(wxtc_search->GetValue());
std::size_t ihexstring = 0;
disas8051::ustring searchbytes;
while(true) {
while((ihexstring < hexstring.size() && (hexstring[ihexstring] == ' '))) {
ihexstring++;
}
if(ihexstring == hexstring.size()) {
break;
}
if(ihexstring + 2 > hexstring.size()) {
messageGUI("invalid hexstring: it does not end with a block of two hex digits");
return;
}
if((!utils::is_hexchar(hexstring[ihexstring])) || (!utils::is_hexchar(hexstring[ihexstring+1]))) {
messageGUI("invalid hex digit");
return;
}
searchbytes.append(1, utils::Hex_To_uint8(hexstring[ihexstring], hexstring[ihexstring+1]));
ihexstring += 2;
}
if(searchbytes.empty()) {
messageGUI("empty search string");
return;
}
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
if(itAddress == disassembly->addressByLine.end()) {
messageGUI("no address corresponding to that line");
return;
}
std::size_t pos;
if(previous) {
if(itAddress->second <= 0) {
messageGUI("Not found");
return;
}
pos = disassembly->buf.rfind(searchbytes, itAddress->second - 1);
std::cout << "searching backwards from " << utils::Int_To_String_Hex(itAddress->second) << ": " << fn_FormatToHex(searchbytes, true) << std::endl;
}
else {
pos = disassembly->buf.find(searchbytes, itAddress->second + 1);
std::cout << "searching forwards from " << utils::Int_To_String_Hex(itAddress->second) << ": " << fn_FormatToHex(searchbytes, true) << std::endl;
}
if(pos == std::string::npos) {
messageGUI("Not found");
return;
}
int line = disassembly->findLineByAddress(pos);
if(line >= 0) {
wxrt_disassembly->GotoLine(line);
wxrt_disassembly->SetFocus();
}
else {
messageGUI("Could not find line corresponding to position found");
}
}
else {
wxrt_disassembly->SearchAnchor();
std::cout << "searching for \"" + fn_wxUTF8String_ToUTF8String(wxtc_search->GetValue()) << '"' << std::endl;
int pos;
if(previous) {
pos = wxrt_disassembly->SearchPrev(wxSTC_FIND_MATCHCASE, wxtc_search->GetValue());
}
else {
pos = wxrt_disassembly->SearchNext(wxSTC_FIND_MATCHCASE, wxtc_search->GetValue());
}
if(pos >= 0) {
std::cout << (previous ? 1 : 0) << " goto " << (pos + (previous ? 0 : 1)) << std::endl;
wxrt_disassembly->GotoPos(pos + (previous ? 0 : 1)); // without '+1' on next search, the next match would be the same
wxrt_disassembly->SetFocus();
}
else {
messageGUI("Not found");
}
}
}
void Dis8051Frame::OnFindBefore(wxCommandEvent &event)
{
doSearch(true);
}
void Dis8051Frame::OnFindNext(wxCommandEvent &event)
{
doSearch(false);
}
void Dis8051Frame::OnGoto(wxCommandEvent &event)
{
int address = queryAddressFromUser(wxT("Goto"));
if(address >= 0) {
int line = disassembly->findLineByAddress(address);
if(line >= 0) {
wxrt_disassembly->GotoLine(line);
wxrt_disassembly->SetFocus();
}
else {
messageGUI("Could not find corresponding line");
}
}
}
void Dis8051Frame::OnCommentEnterPressed(wxCommandEvent &event)
{
wxSTCEditableLocker disasLocker(wxrt_disassembly);
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
if(itAddress != disassembly->addressByLine.end()) {
auto itInstruction = disassembly->instructions.find(itAddress->second);
if(itInstruction != disassembly->instructions.end()) {
disassembly->updateInstructionComment(itAddress->second,
fn_wxUTF8String_ToUTF8String(wxtc_comment->GetValue()));
messageGUI("Updated comment");
}
else {
messageGUI("No instruction corresponding to that address");
}
}
else {
messageGUI("No address corresponding to that line");
}
}
void Dis8051Frame::OnFunctionEnterPressed(wxCommandEvent &event)
{
wxSTCEditableLocker disasLocker(wxrt_disassembly);
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
std::string name = fn_wxUTF8String_ToUTF8String(wxtc_function->GetValue());
if(itAddress != disassembly->addressByLine.end()) {
auto itFunction = disassembly->functions.find(itAddress->second);
if(itFunction != disassembly->functions.end()) {
if(name.empty()) {
messageGUI("Please press the delete button to confirm that you really want to delete that function");
}
else {
disassembly->updateFunctionName(itAddress->second, name);
function_graph->RefreshCache();
function_graph->Refresh();
messageGUI("Renamed function to " + name);
}
}
else {
if(name.empty()) {
messageGUI("No empty name allowed");
}
else {
disas8051::Function function;
function.posX = 10;
function.posY = 10;
function.isInterrupt = false;
function.name = name;
if(disassembly->instructions.find(itAddress->second) == disassembly->instructions.end()) {
int address = queryAddressFromUser(wxT("Enter address of the new function"), (int)itAddress->second);
if(address >= 0) {
disassembly->addFunction(address, function);
scroll_overview->Refresh(); // we disassembled some code
function_graph->RefreshCache();
function_graph->Refresh();
wxc_function_shown->SetValue(true);
messageGUI("Added function " + name);
}
}
else {
disassembly->addFunction(itAddress->second, function);
function_graph->RefreshCache();
function_graph->Refresh();
messageGUI("Added function " + name);
}
}
}
}
else {
messageGUI("No address");
}
}
void Dis8051Frame::OnToggleFunctionShown(wxCommandEvent &event)
{
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
if(itAddress != disassembly->addressByLine.end()) {
auto itFunction = disassembly->findFunction(itAddress->second);
if(itFunction != disassembly->functions.end()) {
itFunction->second.isShownInGraph = wxc_function_shown->GetValue();
function_graph->Refresh();
}
else {
messageGUI("No function on this address");
}
}
else {
messageGUI("No function on this line");
}
}
void Dis8051Frame::OnFunctionDelete(wxCommandEvent &event)
{
wxSTCEditableLocker disasLocker(wxrt_disassembly);
int currentLine = wxrt_disassembly->GetCurrentLine();
auto itAddress = disassembly->addressByLine.find(currentLine);
if(itAddress != disassembly->addressByLine.end()) {
auto itFunction = disassembly->findFunction(itAddress->second);
if(itFunction != disassembly->functions.end()) {
messageGUI("Deleted function " + itFunction->second.name);
disassembly->deleteFunction(itFunction->first);
wxtc_function->SetValue(wxEmptyString);
wxtc_function_blocks->SetValue(wxEmptyString);
function_graph->RefreshCache();
function_graph->Refresh();
}
else {
messageGUI("No function on this address");
}
}
else {
messageGUI("No function on this line");
}
}
void Dis8051Frame::OnToggleCallAnnotation(wxCommandEvent &event)
{
annotation_canvas->annotateCalls = !annotation_canvas->annotateCalls;
annotation_canvas->Refresh();
}
void Dis8051Frame::OnReadAddressFromCode(wxCommandEvent &event)
{
int address = queryAddressFromUser("Copy 16-Bit address from code to clipboard");
if(address >= 0) {
// Write some text to the clipboard
if (wxTheClipboard->Open())
{
std::string addressStr = fn_FormatToHex(disassembly->buf.substr(address, 1), true) +
fn_FormatToHex(disassembly->buf.substr(address+1, 1), true);
// This data objects are held by the clipboard,
// so do not delete them in the app.
wxTheClipboard->SetData(new wxTextDataObject(wxString::FromUTF8(addressStr.c_str())));
wxTheClipboard->Close();
messageGUI("Copied " + addressStr + " to clipboard.");
}
else {
messageGUI("Could not open clipboard.");
}
}
}
void Dis8051Frame::messageGUI(std::string msg)
{
SetStatusText(wxString::FromUTF8(msg.c_str()), 0);
c_logger->LogInformation(msg);
}
std::string Dis8051Frame::getAddressDescription(uint64_t address)
{
std::string str = disassembly->formatWithFunction(address);
if(disassembly->instructions[address].isCall()) {
str += " (call)";
}
return str;
}
std::basic_string<uint8_t> Uint16t_To_UString(uint16_t var)
{
return std::basic_string<uint8_t>(1, *((uint8_t*)&var))
+ std::basic_string<uint8_t>(1, *((uint8_t*)&var + 1));
}
int Dis8051Frame::queryAddressFromUser(wxString title, int defaultAddress)
{
wxTextEntryDialog dlg(this, wxT("Hex address"), title);
if(defaultAddress >= 0) {
dlg.SetValue(wxString::Format(wxT("%x"), defaultAddress));
}
if(dlg.ShowModal() == wxID_OK) {
utils::string_to_int_limited_converter convertobj;
uint64_t address = convertobj.ConvertToLLI_Hex_Limited(
fn_wxUTF8String_ToUTF8String(dlg.GetValue()), 0, disassembly->buf.length());
if(convertobj.MistakeHappened() || convertobj.LimitMistakeHappened()) {
messageGUI("Invalid address");
return -1;
}
else {
return address;
}
}
else {
return -1;
}
}