Skip to content

Commit

Permalink
Machine::address_to_editor_blcok
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Jun 9, 2024
1 parent 5e1ee99 commit c3ddbb6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/assembler/simpleasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ bool SimpleAsm::process_line(
}
uint32_t *p = inst;
for (size_t l = 0; l < size; l += 4) {
address_to_blocknum.insert(address, line_number);
if (!fatal_occured) { mem->write_u32(address, *(p++), ae::INTERNAL); }
address += 4;
}
Expand Down
3 changes: 3 additions & 0 deletions src/assembler/simpleasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class SimpleAsm : public QObject {
SymbolTableDb *symtab {};
machine::Address address {};

public:
QMap<machine::Address, int> address_to_blocknum = {};

private:
QStringList include_stack;
machine::FrontendMemory *mem {};
Expand Down
6 changes: 6 additions & 0 deletions src/gui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,12 @@ void MainWindow::compile_source() {
}
if (!sasm.finish()) { error_occured = true; }


machine->address_to_editor_blcok = sasm.address_to_blocknum;
connect(
machine.data(), &machine::Machine::highlight_by_blocknum, editor,
&SrcEditor::highlightBlock);

if (error_occured) { show_messages(); }
}

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

void SrcEditor::highlightBlock(int blockNum) {
QList<QTextEdit::ExtraSelection> extraSelections;

if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;

QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
QTextBlock block = document()->findBlockByNumber(blockNum - 1);

// scroll to block and show it in editor middle
QFontMetrics fontMetrics(document()->defaultFont());
// calculate viewport line count
int viewportLineCount = viewport()->height() / fontMetrics.height();
QScrollBar *vScrollBar = verticalScrollBar();
vScrollBar->setValue(
vScrollBar->singleStep() * (block.firstLineNumber() - viewportLineCount / 2));

selection.cursor = QTextCursor(block);
selection.cursor.movePosition(
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
extraSelections.append(selection);
}

setExtraSelections(extraSelections);
}
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 line_num);

private slots:
void updateMargins(int newBlockCount);
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ void Machine::pause() {

void Machine::step_internal(bool skip_break) {
CTL_GUARD;
emit highlight_by_blocknum(address_to_editor_blcok.value(regs->read_pc()));

enum Status stat_prev = stat;
set_status(ST_BUSY);
emit tick();
Expand Down
10 changes: 7 additions & 3 deletions src/machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include "core.h"
#include "machineconfig.h"
#include "memory/backend/aclintmswi.h"
#include "memory/backend/aclintmtimer.h"
#include "memory/backend/aclintsswi.h"
#include "memory/backend/lcddisplay.h"
#include "memory/backend/peripheral.h"
#include "memory/backend/peripspiled.h"
#include "memory/backend/serialport.h"
#include "memory/backend/aclintmtimer.h"
#include "memory/backend/aclintmswi.h"
#include "memory/backend/aclintsswi.h"
#include "memory/cache/cache.h"
#include "memory/memory_bus.h"
#include "predictor.h"
Expand Down Expand Up @@ -92,6 +92,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 @@ -102,6 +103,9 @@ public slots:
private slots:
void step_timer();

public:
QMap<machine::Address, int> address_to_editor_blcok = {};

private:
void step_internal(bool skip_break = false);
MachineConfig machine_config;
Expand Down

0 comments on commit c3ddbb6

Please sign in to comment.