Skip to content

Commit acd03b7

Browse files
committed
draft
1 parent 3fc9693 commit acd03b7

File tree

8 files changed

+87
-24
lines changed

8 files changed

+87
-24
lines changed

src/assembler/simpleasm.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "simpleasm.h"
22

3+
#include "common/logging.h"
34
#include "machine/memory/address.h"
45
#include "machine/memory/memory_utils.h"
56

@@ -9,6 +10,8 @@
910
#include <QObject>
1011
#include <QString>
1112

13+
LOG_CATEGORY("ddd.fff");
14+
1215
using namespace fixmatheval;
1316
using machine::Address;
1417
using ae = machine::AccessEffects; // For enum values, type is obvious from
@@ -520,7 +523,9 @@ bool SimpleAsm::process_line(
520523
return false;
521524
}
522525
uint32_t *p = inst;
526+
LOG("address: {%x} -> start_line: {%d}, size: {%d}", address, line_number, size);
523527
for (size_t l = 0; l < size; l += 4) {
528+
lintToAddress.insert(address, line_number);
524529
if (!fatal_occured) { mem->write_u32(address, *(p++), ae::INTERNAL); }
525530
address += 4;
526531
}
@@ -543,6 +548,7 @@ bool SimpleAsm::process_file(const QString &filename, QString *error_ptr) {
543548
if ((line.count() > 0) && (line.at(line.count() - 1) == '\n')) {
544549
line.truncate(line.count() - 1);
545550
}
551+
LOG("address: {%x} -> start_line: {%d}", address, line);
546552
if (!process_line(line, filename, ln, error_ptr)) { res = false; }
547553
}
548554
srcfile.close();

src/assembler/simpleasm.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class SimpleAsm : public QObject {
4848
~SimpleAsm() override;
4949

5050
public:
51-
static uint64_t
52-
string_to_uint64(const QString &str, int base, int *chars_taken = nullptr);
51+
static uint64_t string_to_uint64(const QString &str, int base, int *chars_taken = nullptr);
5352
void clear();
5453
void setup(
5554
machine::FrontendMemory *mem,
@@ -61,8 +60,7 @@ class SimpleAsm : public QObject {
6160
const QString &filename = "",
6261
int line_number = 0,
6362
QString *error_ptr = nullptr);
64-
virtual bool
65-
process_file(const QString &filename, QString *error_ptr = nullptr);
63+
virtual bool process_file(const QString &filename, QString *error_ptr = nullptr);
6664
bool finish(QString *error_ptr = nullptr);
6765

6866
protected:
@@ -76,6 +74,9 @@ class SimpleAsm : public QObject {
7674
SymbolTableDb *symtab {};
7775
machine::Address address {};
7876

77+
public:
78+
QMap<machine::Address, int> lintToAddress = {};
79+
7980
private:
8081
QStringList include_stack;
8182
machine::FrontendMemory *mem {};

src/gui/mainwindow/mainwindow.cpp

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
#include "windows/editor/editordock.h"
2-
#include "windows/editor/editortab.h"
3-
4-
#include <QProcessEnvironment>
5-
#include <QtWidgets>
6-
#include <qactiongroup.h>
7-
#include <qwidget.h>
1+
#include "mainwindow.h"
82

93
#include "assembler/fixmatheval.h"
104
#include "assembler/simpleasm.h"
5+
#include "common/logging.h"
116
#include "dialogs/about/aboutdialog.h"
127
#include "dialogs/gotosymbol/gotosymboldialog.h"
138
#include "dialogs/savechanged/savechangeddialog.h"
149
#include "extprocess.h"
1510
#include "helper/async_modal.h"
16-
#include "mainwindow.h"
1711
#include "os_emulation/ossyscall.h"
1812
#include "textsignalaction.h"
19-
#include "common/logging.h"
13+
#include "windows/editor/editordock.h"
14+
#include "windows/editor/editortab.h"
2015

2116
#include <QFileInfo>
2217
#include <QMessageBox>
2318
#include <QMetaObject>
19+
#include <QProcessEnvironment>
2420
#include <QTextDocument>
21+
#include <QtWidgets>
22+
#include <qactiongroup.h>
23+
#include <qwidget.h>
2524

2625
LOG_CATEGORY("gui.mainwindow");
2726

@@ -266,8 +265,9 @@ void MainWindow::create_core(
266265

267266
set_speed(); // Update machine speed to current settings
268267

269-
const static machine::ExceptionCause ecall_variats[] = {machine::EXCAUSE_ECALL_ANY,
270-
machine::EXCAUSE_ECALL_M, machine::EXCAUSE_ECALL_S, machine::EXCAUSE_ECALL_U};
268+
const static machine::ExceptionCause ecall_variats[]
269+
= { machine::EXCAUSE_ECALL_ANY, machine::EXCAUSE_ECALL_M, machine::EXCAUSE_ECALL_S,
270+
machine::EXCAUSE_ECALL_U };
271271

272272
if (config.osemu_enable()) {
273273
auto *osemu_handler = new osemu::OsSyscallExceptionHandler(
@@ -300,6 +300,8 @@ void MainWindow::create_core(
300300
connect(machine.data(), &machine::Machine::status_change, this, &MainWindow::machine_status);
301301
connect(machine.data(), &machine::Machine::program_exit, this, &MainWindow::machine_exit);
302302
connect(machine.data(), &machine::Machine::program_trap, this, &MainWindow::machine_trap);
303+
connect(
304+
machine.data()->core(), &machine::Core::mySignal, this, &MainWindow::hightlightByAddress);
303305
// Connect signal from break to machine pause
304306
connect(
305307
machine->core(), &machine::Core::stop_on_exception_reached, machine.data(),
@@ -334,6 +336,14 @@ void MainWindow::new_machine() {
334336
ndialog->show();
335337
}
336338

339+
void MainWindow::hightlightByAddress(machine::Address addr) {
340+
auto lineNum = lineToAddress.value(addr);
341+
printf(
342+
"))))))))))))))))))))))))))))))))))c))))))))))))))))))))))))addr: %x, block: %d\n", addr,
343+
lineNum);
344+
editor_tabs->get_current_editor()->highlightBlock(lineNum);
345+
}
346+
337347
void MainWindow::machine_reload(bool force_memory_reset, bool force_elf_load) {
338348
if (machine == nullptr) { return new_machine(); }
339349
bool load_executable = force_elf_load || machine->executable_loaded();
@@ -408,13 +418,13 @@ void MainWindow::print_action() {
408418
void MainWindow::show_##NAME() { \
409419
show_dockwidget(&*NAME, DEFAULT_AREA, true, false); \
410420
} \
411-
void MainWindow::reset_state_##NAME() { \
412-
show_dockwidget(&*NAME, DEFAULT_AREA, DEFAULT_VISIBLE, true); \
413-
}
421+
void MainWindow::reset_state_##NAME() { \
422+
show_dockwidget(&*NAME, DEFAULT_AREA, DEFAULT_VISIBLE, true); \
423+
}
414424

415425
SHOW_HANDLER(registers, Qt::TopDockWidgetArea, true)
416426
SHOW_HANDLER(program, Qt::LeftDockWidgetArea, true)
417-
SHOW_HANDLER(memory, Qt::RightDockWidgetArea, true )
427+
SHOW_HANDLER(memory, Qt::RightDockWidgetArea, true)
418428
SHOW_HANDLER(cache_program, Qt::RightDockWidgetArea, false)
419429
SHOW_HANDLER(cache_data, Qt::RightDockWidgetArea, false)
420430
SHOW_HANDLER(cache_level2, Qt::RightDockWidgetArea, false)
@@ -524,8 +534,11 @@ void MainWindow::closeEvent(QCloseEvent *event) {
524534
}
525535
}
526536

527-
void MainWindow::show_dockwidget(QDockWidget *dw, Qt::DockWidgetArea area,
528-
bool defaultVisible, bool resetState) {
537+
void MainWindow::show_dockwidget(
538+
QDockWidget *dw,
539+
Qt::DockWidgetArea area,
540+
bool defaultVisible,
541+
bool resetState) {
529542
if (dw == nullptr) { return; }
530543
if (resetState) {
531544
if (dw->isFloating()) {
@@ -617,8 +630,7 @@ void MainWindow::message_selected(
617630
central_widget_tabs->setCurrentWidget(editor_tabs.data());
618631
if (!editor_tabs->set_cursor_to(file, line, column)) {
619632
editor_tabs->open_file_if_not_open(file, false);
620-
if (!editor_tabs->set_cursor_to(file, line, column))
621-
return;
633+
if (!editor_tabs->set_cursor_to(file, line, column)) return;
622634
}
623635

624636
// Highlight the line
@@ -632,7 +644,7 @@ void MainWindow::message_selected(
632644
}
633645

634646
bool SimpleAsmWithEditorCheck::process_file(const QString &filename, QString *error_ptr) {
635-
EditorTab* tab = mainwindow->editor_tabs->find_tab_by_filename(filename);
647+
EditorTab *tab = mainwindow->editor_tabs->find_tab_by_filename(filename);
636648
if (tab == nullptr) { return Super::process_file(filename, error_ptr); }
637649
SrcEditor *editor = tab->get_editor();
638650
QTextDocument *doc = editor->document();
@@ -759,6 +771,10 @@ void MainWindow::compile_source() {
759771
}
760772
if (!sasm.finish()) { error_occured = true; }
761773

774+
lineToAddress = sasm.lintToAddress;
775+
// TODO
776+
// save lintToAddress in MainWindow?
777+
762778
if (error_occured) { show_messages(); }
763779
}
764780

src/gui/mainwindow/mainwindow.h

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class MainWindow : public QMainWindow {
6060

6161
public slots:
6262
// Actions signals
63+
void hightlightByAddress(machine::Address addr);
6364
void new_machine();
6465
void machine_reload(bool force_memory_reset = false, bool force_elf_load = false);
6566
void print_action();
@@ -115,6 +116,9 @@ public slots:
115116
protected:
116117
void closeEvent(QCloseEvent *cancel) override;
117118

119+
public:
120+
QMap<machine::Address, int> lineToAddress = {};
121+
118122
private:
119123
Box<Ui::MainWindow> ui {};
120124

src/gui/windows/editor/srceditor.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,34 @@ void SrcEditor::insertFromMimeData(const QMimeData *source) {
289289
bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
290290
return source->hasText();
291291
}
292+
293+
void SrcEditor::highlightBlock(int blockNum) {
294+
QList<QTextEdit::ExtraSelection> extraSelections;
295+
296+
if (!isReadOnly()) {
297+
QTextEdit::ExtraSelection selection;
298+
299+
QColor lineColor = QColor(Qt::yellow).lighter(160);
300+
selection.format.setBackground(lineColor);
301+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
302+
QTextBlock block = document()->findBlockByLineNumber(blockNum - 1);
303+
selection.cursor = QTextCursor(block);
304+
selection.cursor.movePosition(
305+
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
306+
extraSelections.append(selection);
307+
}
308+
309+
setExtraSelections(extraSelections);
310+
}
311+
312+
// TODO: delete this
313+
void SrcEditor::mousePressEvent(QMouseEvent *event) {
314+
QPlainTextEdit::mousePressEvent(event); // 先调用父类的鼠标点击事件
315+
316+
// 计算鼠标点击的行
317+
QTextCursor cursor = this->cursorForPosition(event->pos());
318+
int blockNum = cursor.blockNumber();
319+
320+
// 高亮点击行
321+
highlightBlock(blockNum);
322+
}

src/gui/windows/editor/srceditor.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ class SrcEditor : public QPlainTextEdit {
3636
void resizeEvent(QResizeEvent *event) override;
3737
void insertFromMimeData(const QMimeData *source) override;
3838
bool canInsertFromMimeData(const QMimeData *source) const override;
39+
void mousePressEvent(QMouseEvent *event) override;
3940

4041
signals:
4142
void file_name_change();
4243

4344
public slots:
4445
void setShowLineNumbers(bool visible);
46+
void highlightBlock(int line_num);
4547

4648
private slots:
4749
void updateMargins(int newBlockCount);

src/machine/core.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Core::Core(Registers *regs,
4949
}
5050

5151
void Core::step(bool skip_break) {
52+
emit mySignal(regs->read_pc());
53+
5254
state.cycle_count++;
5355
do_step(skip_break);
5456
emit step_done(state);

src/machine/core.h

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Core : public QObject {
9393
signals:
9494
void stop_on_exception_reached();
9595
void step_done(const CoreState &);
96+
void mySignal(Address addr);
9697

9798
protected:
9899
virtual void do_step(bool skip_break) = 0;

0 commit comments

Comments
 (0)