Skip to content

Highlight currently executed instruction in internal editor #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/assembler/simpleasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ void SimpleAsm::setup(
machine::FrontendMemory *mem,
SymbolTableDb *symtab,
machine::Address address,
machine::Xlen xlen) {
machine::Xlen xlen,
QMap<machine::Address, int> *address_to_blocknum) {
this->mem = mem;
this->symtab = symtab;
this->address = address;
this->symtab->setSymbol("XLEN", static_cast<uint64_t>(xlen), sizeof(uint64_t));
this->address_to_blocknum = address_to_blocknum;
}

static const machine::BitArg wordArg = { { { 32, 0 } }, 0 };
Expand Down Expand Up @@ -521,6 +523,7 @@ bool SimpleAsm::process_line(
}
uint32_t *p = inst;
for (size_t l = 0; l < size; l += 4) {
if (address_to_blocknum != nullptr) { address_to_blocknum->insert(address, line_number); }
if (!fatal_occured) { mem->write_u32(address, *(p++), ae::INTERNAL); }
address += 4;
}
Expand Down
4 changes: 3 additions & 1 deletion src/assembler/simpleasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class SimpleAsm : public QObject {
machine::FrontendMemory *mem,
SymbolTableDb *symtab,
machine::Address address,
machine::Xlen xlen);
machine::Xlen xlen,
QMap<machine::Address, int> *address_to_blocknum = nullptr);
bool process_line(
const QString &line,
const QString &filename = "",
Expand All @@ -78,6 +79,7 @@ class SimpleAsm : public QObject {

private:
QStringList include_stack;
QMap<machine::Address, int> *address_to_blocknum = nullptr;
machine::FrontendMemory *mem {};
machine::RelocExpressionList reloc;
};
Expand Down
38 changes: 34 additions & 4 deletions src/gui/mainwindow/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<iconset>
<normaloff>
:/icons/gui.png</normaloff>
:/icons/gui.png
:/icons/gui.png
</iconset>
</property>
<property name="dockNestingEnabled">
Expand Down Expand Up @@ -54,7 +54,7 @@
<x>0</x>
<y>0</y>
<width>900</width>
<height>32</height>
<height>31</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand Down Expand Up @@ -127,9 +127,11 @@
</widget>
<widget class="QMenu" name="menuOptions">
<property name="title">
<string>Options</string>
<string>&amp;Options</string>
</property>
<addaction name="actionEditorShowLineNumbers"/>
<addaction name="actionEditorHighlightPC"/>
<addaction name="actionEditorFollowPC"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuMachine"/>
Expand All @@ -146,7 +148,7 @@
<bool>false</bool>
</property>
<property name="allowedAreas">
<set>Qt::TopToolBarArea</set>
<set>Qt::ToolBarArea::TopToolBarArea</set>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
Expand Down Expand Up @@ -629,6 +631,34 @@
<property name="toolTip">
<string>Show line number in the code editor.</string>
</property>
</action>
<action name="actionEditorHighlightPC">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Highlight Curent Instruction</string>
</property>
<property name="toolTip">
<string>Highlight currently the currently executed instruction in the internal editor.</string>
</property>
</action>
<action name="actionEditorFollowPC">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Follow Current Instruction</string>
</property>
<property name="toolTip">
<string>Always focus the currently executed instruction in the internal editor.</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
Expand Down
17 changes: 14 additions & 3 deletions src/gui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ MainWindow::MainWindow(QSettings *settings, QWidget *parent)
connect(
ui->actionEditorShowLineNumbers, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_show_line_numbers);
connect(
ui->actionEditorHighlightPC, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_highlight_pc);
connect(
ui->actionEditorFollowPC, &QAction::triggered, editor_tabs.data(),
&EditorDock::set_follow_pc);

bool line_numbers_visible = settings->value("EditorShowLineNumbers", true).toBool();
editor_tabs->set_show_line_numbers(line_numbers_visible);
Expand Down Expand Up @@ -252,14 +258,13 @@ void MainWindow::create_core(
bool keep_memory) {
// Create machine
auto *new_machine = new machine::Machine(config, true, load_executable);

if (keep_memory && (machine != nullptr)) {
new_machine->memory_rw()->reset(*machine->memory());
*new_machine->address_to_blocknum_rw() = *machine->address_to_blocknum();
}

// Remove old machine
machine.reset(new_machine);

// Create machine view
auto focused_index = central_widget_tabs->currentIndex();
corescene.reset();
Expand Down Expand Up @@ -306,6 +311,7 @@ void MainWindow::create_core(
connect(
machine->core(), &machine::Core::stop_on_exception_reached, machine.data(),
&machine::Machine::pause);
connect(machine.data(), &machine::Machine::highlight_by_blocknum, this, &MainWindow::handle_highlight_by_blocknum);

// Setup docks
registers->connectToMachine(machine.data());
Expand Down Expand Up @@ -754,7 +760,10 @@ void MainWindow::compile_source() {

connect(&sasm, &SimpleAsm::report_message, this, &MainWindow::report_message);

sasm.setup(mem, &symtab, machine::Address(0x00000200), machine->core()->get_xlen());
machine->address_to_blocknum_rw()->clear();
sasm.setup(
mem, &symtab, machine::Address(0x00000200), machine->core()->get_xlen(),
machine->address_to_blocknum_rw());

int ln = 1;
for (QTextBlock block = content->begin(); block.isValid(); block = block.next(), ln++) {
Expand All @@ -763,6 +772,8 @@ void MainWindow::compile_source() {
}
if (!sasm.finish()) { error_occured = true; }

connect(this, &MainWindow::highlight_by_blocknum, editor, &SrcEditor::highlightBlock);

if (error_occured) { show_messages(); }
}

Expand Down
4 changes: 4 additions & 0 deletions src/gui/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MainWindow : public QMainWindow {
QString text,
QString hint);
void clear_messages();
void highlight_by_blocknum(int blocknum);

public slots:
// Actions signals
Expand Down Expand Up @@ -111,6 +112,9 @@ public slots:
int column,
const QString &text,
const QString &hint);
void handle_highlight_by_blocknum(int blocknum) {
emit highlight_by_blocknum(blocknum);
}

protected:
void closeEvent(QCloseEvent *cancel) override;
Expand Down
16 changes: 16 additions & 0 deletions src/gui/windows/editor/editordock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ void EditorDock::set_show_line_numbers(bool visible) {
}
}

void EditorDock::set_highlight_pc(bool enable) {
enable_hightlight = enable;
settings->setValue("editorEnableHighlisht", enable);
for (int i = 0; i < this->count(); i++) {
get_tab(i)->set_enable_highlight(enable);
}
}

void EditorDock::set_follow_pc(bool enable) {
enable_focus_change = enable;
settings->setValue("editorEnableFocusChange", enable);
for (int i = 0; i < this->count(); i++) {
get_tab(i)->set_enable_focus_change(enable);
}
}

void EditorDock::tabCountChanged() {
Super::tabCountChanged();
emit editor_available_changed(count() > 0);
Expand Down
4 changes: 4 additions & 0 deletions src/gui/windows/editor/editordock.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class EditorDock : public HidingTabWidget {

public slots:
void set_show_line_numbers(bool visible);
void set_highlight_pc(bool enable);
void set_follow_pc(bool enable);

void open_file_dialog();
void save_tab(int index);
Expand All @@ -59,6 +61,8 @@ public slots:
private:
QSharedPointer<QSettings> settings;
bool line_numbers_visible = true;
bool enable_hightlight = true;
bool enable_focus_change = true;
size_t unknown_editor_counter = 1;
};

Expand Down
8 changes: 8 additions & 0 deletions src/gui/windows/editor/editortab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ void EditorTab::set_show_line_number(bool visible) {
editor->setShowLineNumbers(visible);
}

void EditorTab::set_enable_highlight(bool enable) {
editor->setEnableHighlight(enable);
}

void EditorTab::set_enable_focus_change(bool enable) {
editor->setEnableFocusChange(enable);
}

void EditorTab::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
elide_file_name();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/windows/editor/editortab.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class EditorTab : public QWidget {

public slots:
void set_show_line_number(bool visible);
void set_enable_highlight(bool enable);
void set_enable_focus_change(bool enable);

protected:
void resizeEvent(QResizeEvent *event) override;
Expand Down
47 changes: 47 additions & 0 deletions src/gui/windows/editor/srceditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QFile>
#include <QFileInfo>
#include <QPainter>
#include <QScrollBar>
#include <QTextCursor>
#include <QTextDocumentWriter>
#include <qglobal.h>
Expand Down Expand Up @@ -282,10 +283,56 @@ void SrcEditor::setShowLineNumbers(bool show) {
updateMargins(0);
}

void SrcEditor::setEnableHighlight(bool enable) {
if (!enable) {
// clear old styles
QList<QTextEdit::ExtraSelection> extra_selections;
setExtraSelections(extra_selections);
}
enable_highlight = enable;
}

void SrcEditor::setEnableFocusChange(bool enable) {
enable_focus_change = enable;
}

void SrcEditor::insertFromMimeData(const QMimeData *source) {
if (source->hasText()) { insertPlainText(source->text()); }
}

bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
return source->hasText();
}

void SrcEditor::highlightBlock(int block_num) {
QTextBlock block = document()->findBlockByNumber(block_num - 1);

if (enable_highlight) {
// set hightly style
QList<QTextEdit::ExtraSelection> extra_selections;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);

// move cursor
selection.cursor = QTextCursor(block);
// select until the end of block
selection.cursor.movePosition(
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
// select an extra \n
selection.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 1);
extra_selections.append(selection);
setExtraSelections(extra_selections);
}

if (enable_focus_change) {
// calculate viewport line count
int viewport_line_count
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
// scroll to block and show it in editor middle
QScrollBar *vScrollBar = verticalScrollBar();
vScrollBar->setValue(
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));
}
}
5 changes: 5 additions & 0 deletions src/gui/windows/editor/srceditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class SrcEditor : public QPlainTextEdit {

public slots:
void setShowLineNumbers(bool visible);
void setEnableHighlight(bool enable);
void setEnableFocusChange(bool enable);
void highlightBlock(int block_num);

private slots:
void updateMargins(int newBlockCount);
Expand All @@ -51,6 +54,8 @@ private slots:
::Box<QSyntaxHighlighter> highlighter {};
LineNumberArea *line_number_area;
bool line_numbers_visible = true;
bool enable_highlight = true;
bool enable_focus_change = true;
QString fname;
QString tname;
bool saveAsRequiredFl {};
Expand Down
15 changes: 15 additions & 0 deletions src/machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Machine::Machine(MachineConfig config, bool load_symtab, bool load_executable)
mem = new Memory(*mem_program_only);
} else {
mem = new Memory(machine_config.get_simulated_endian());
addr_to_blocknum = new QMap<machine::Address, int>();
}

data_bus = new MemoryDataBus(machine_config.get_simulated_endian());
Expand Down Expand Up @@ -184,6 +185,8 @@ Machine::~Machine() {
symtab = nullptr;
delete predictor;
predictor = nullptr;
delete addr_to_blocknum;
addr_to_blocknum = nullptr;
}

const MachineConfig &Machine::config() {
Expand Down Expand Up @@ -211,6 +214,14 @@ Memory *Machine::memory_rw() {
return mem;
}

const QMap<machine::Address, int> *Machine::address_to_blocknum() {
return addr_to_blocknum;
}

QMap<machine::Address, int> *Machine::address_to_blocknum_rw() {
return addr_to_blocknum;
}

const Cache *Machine::cache_program() {
return cch_program;
}
Expand Down Expand Up @@ -331,6 +342,10 @@ void Machine::pause() {

void Machine::step_internal(bool skip_break) {
CTL_GUARD;
if (addr_to_blocknum != nullptr && addr_to_blocknum->contains(regs->read_pc())) {
emit highlight_by_blocknum(addr_to_blocknum->value(regs->read_pc()));
}

enum Status stat_prev = stat;
set_status(ST_BUSY);
emit tick();
Expand Down
Loading
Loading