Skip to content

Commit c3ddbb6

Browse files
committed
Machine::address_to_editor_blcok
1 parent 5e1ee99 commit c3ddbb6

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

src/assembler/simpleasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ bool SimpleAsm::process_line(
521521
}
522522
uint32_t *p = inst;
523523
for (size_t l = 0; l < size; l += 4) {
524+
address_to_blocknum.insert(address, line_number);
524525
if (!fatal_occured) { mem->write_u32(address, *(p++), ae::INTERNAL); }
525526
address += 4;
526527
}

src/assembler/simpleasm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class SimpleAsm : public QObject {
7676
SymbolTableDb *symtab {};
7777
machine::Address address {};
7878

79+
public:
80+
QMap<machine::Address, int> address_to_blocknum = {};
81+
7982
private:
8083
QStringList include_stack;
8184
machine::FrontendMemory *mem {};

src/gui/mainwindow/mainwindow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,12 @@ void MainWindow::compile_source() {
759759
}
760760
if (!sasm.finish()) { error_occured = true; }
761761

762+
763+
machine->address_to_editor_blcok = sasm.address_to_blocknum;
764+
connect(
765+
machine.data(), &machine::Machine::highlight_by_blocknum, editor,
766+
&SrcEditor::highlightBlock);
767+
762768
if (error_occured) { show_messages(); }
763769
}
764770

src/gui/windows/editor/srceditor.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <QFile>
1212
#include <QFileInfo>
1313
#include <QPainter>
14+
#include <QScrollBar>
1415
#include <QTextCursor>
1516
#include <QTextDocumentWriter>
1617
#include <qglobal.h>
@@ -289,3 +290,31 @@ void SrcEditor::insertFromMimeData(const QMimeData *source) {
289290
bool SrcEditor::canInsertFromMimeData(const QMimeData *source) const {
290291
return source->hasText();
291292
}
293+
294+
void SrcEditor::highlightBlock(int blockNum) {
295+
QList<QTextEdit::ExtraSelection> extraSelections;
296+
297+
if (!isReadOnly()) {
298+
QTextEdit::ExtraSelection selection;
299+
300+
QColor lineColor = QColor(Qt::yellow).lighter(160);
301+
selection.format.setBackground(lineColor);
302+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
303+
QTextBlock block = document()->findBlockByNumber(blockNum - 1);
304+
305+
// scroll to block and show it in editor middle
306+
QFontMetrics fontMetrics(document()->defaultFont());
307+
// calculate viewport line count
308+
int viewportLineCount = viewport()->height() / fontMetrics.height();
309+
QScrollBar *vScrollBar = verticalScrollBar();
310+
vScrollBar->setValue(
311+
vScrollBar->singleStep() * (block.firstLineNumber() - viewportLineCount / 2));
312+
313+
selection.cursor = QTextCursor(block);
314+
selection.cursor.movePosition(
315+
QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
316+
extraSelections.append(selection);
317+
}
318+
319+
setExtraSelections(extraSelections);
320+
}

src/gui/windows/editor/srceditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SrcEditor : public QPlainTextEdit {
4242

4343
public slots:
4444
void setShowLineNumbers(bool visible);
45+
void highlightBlock(int line_num);
4546

4647
private slots:
4748
void updateMargins(int newBlockCount);

src/machine/machine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ void Machine::pause() {
331331

332332
void Machine::step_internal(bool skip_break) {
333333
CTL_GUARD;
334+
emit highlight_by_blocknum(address_to_editor_blcok.value(regs->read_pc()));
335+
334336
enum Status stat_prev = stat;
335337
set_status(ST_BUSY);
336338
emit tick();

src/machine/machine.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include "core.h"
55
#include "machineconfig.h"
6+
#include "memory/backend/aclintmswi.h"
7+
#include "memory/backend/aclintmtimer.h"
8+
#include "memory/backend/aclintsswi.h"
69
#include "memory/backend/lcddisplay.h"
710
#include "memory/backend/peripheral.h"
811
#include "memory/backend/peripspiled.h"
912
#include "memory/backend/serialport.h"
10-
#include "memory/backend/aclintmtimer.h"
11-
#include "memory/backend/aclintmswi.h"
12-
#include "memory/backend/aclintsswi.h"
1313
#include "memory/cache/cache.h"
1414
#include "memory/memory_bus.h"
1515
#include "predictor.h"
@@ -92,6 +92,7 @@ public slots:
9292
void restart();
9393

9494
signals:
95+
void highlight_by_blocknum(int block_num);
9596
void program_exit();
9697
void program_trap(machine::SimulatorException &e);
9798
void status_change(enum machine::Machine::Status st);
@@ -102,6 +103,9 @@ public slots:
102103
private slots:
103104
void step_timer();
104105

106+
public:
107+
QMap<machine::Address, int> address_to_editor_blcok = {};
108+
105109
private:
106110
void step_internal(bool skip_break = false);
107111
MachineConfig machine_config;

0 commit comments

Comments
 (0)