Skip to content

Commit

Permalink
GUI: highlight currently executed instruction in internal editor
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg authored and jdupak committed Jun 21, 2024
1 parent 68a1b3a commit 57b7a92
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
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
8 changes: 7 additions & 1 deletion src/gui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,9 @@ void MainWindow::compile_source() {

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

sasm.setup(mem, &symtab, machine::Address(0x00000200), machine->core()->get_xlen());
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 +765,10 @@ void MainWindow::compile_source() {
}
if (!sasm.finish()) { error_occured = true; }

connect(
machine.data(), &machine::Machine::highlight_by_blocknum, editor,
&SrcEditor::highlightBlock);

if (error_occured) { show_messages(); }
}

Expand Down
27 changes: 27 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 @@ -289,3 +290,29 @@ void SrcEditor::insertFromMimeData(const QMimeData *source) {
bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
return source->hasText();
}

void SrcEditor::highlightBlock(int block_num) {
QList<QTextEdit::ExtraSelection> extra_selections;

// set hightly style
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);

// select block
QTextBlock block = document()->findBlockByNumber(block_num - 1);
selection.cursor = QTextCursor(block);
selection.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
extra_selections.append(selection);

// 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));

setExtraSelections(extra_selections);
}
1 change: 1 addition & 0 deletions src/gui/windows/editor/srceditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SrcEditor : public QPlainTextEdit {

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

private slots:
void updateMargins(int newBlockCount);
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
4 changes: 4 additions & 0 deletions src/machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Machine : public QObject {
const CSR::ControlState *control_state();
const Memory *memory();
Memory *memory_rw();
const QMap<machine::Address, int> *address_to_blocknum();
QMap<machine::Address, int> *address_to_blocknum_rw();
const Cache *cache_program();
const Cache *cache_data();
const Cache *cache_level2();
Expand Down Expand Up @@ -92,6 +94,7 @@ public slots:
void restart();

signals:
void highlight_by_blocknum(int block_num);
void program_exit();
void program_trap(machine::SimulatorException &e);
void status_change(enum machine::Machine::Status st);
Expand All @@ -114,6 +117,7 @@ private slots:
* simulation reset without repeated ELF file loading.
*/
Memory *mem_program_only = nullptr;
QMap<machine::Address, int> *addr_to_blocknum = nullptr;
MemoryDataBus *data_bus = nullptr;
SerialPort *ser_port = nullptr;
PeripSpiLed *perip_spi_led = nullptr;
Expand Down

0 comments on commit 57b7a92

Please sign in to comment.