Skip to content

Commit 953ce8b

Browse files
committed
GUI: highlight currently executed instruction in internal editor
1 parent 5e1ee99 commit 953ce8b

File tree

7 files changed

+61
-3
lines changed

7 files changed

+61
-3
lines changed

src/assembler/simpleasm.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ void SimpleAsm::setup(
7171
machine::FrontendMemory *mem,
7272
SymbolTableDb *symtab,
7373
machine::Address address,
74-
machine::Xlen xlen) {
74+
machine::Xlen xlen,
75+
QMap<machine::Address, int> *address_to_blocknum) {
7576
this->mem = mem;
7677
this->symtab = symtab;
7778
this->address = address;
7879
this->symtab->setSymbol("XLEN", static_cast<uint64_t>(xlen), sizeof(uint64_t));
80+
this->address_to_blocknum = address_to_blocknum;
7981
}
8082

8183
static const machine::BitArg wordArg = { { { 32, 0 } }, 0 };
@@ -521,6 +523,9 @@ bool SimpleAsm::process_line(
521523
}
522524
uint32_t *p = inst;
523525
for (size_t l = 0; l < size; l += 4) {
526+
if (address_to_blocknum != nullptr) {
527+
address_to_blocknum->insert(address, line_number);
528+
}
524529
if (!fatal_occured) { mem->write_u32(address, *(p++), ae::INTERNAL); }
525530
address += 4;
526531
}

src/assembler/simpleasm.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class SimpleAsm : public QObject {
5555
machine::FrontendMemory *mem,
5656
SymbolTableDb *symtab,
5757
machine::Address address,
58-
machine::Xlen xlen);
58+
machine::Xlen xlen,
59+
QMap<machine::Address, int> *address_to_blocknum = nullptr);
5960
bool process_line(
6061
const QString &line,
6162
const QString &filename = "",
@@ -78,6 +79,7 @@ class SimpleAsm : public QObject {
7879

7980
private:
8081
QStringList include_stack;
82+
QMap<machine::Address, int> *address_to_blocknum = nullptr;
8183
machine::FrontendMemory *mem {};
8284
machine::RelocExpressionList reloc;
8385
};

src/gui/mainwindow/mainwindow.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ void MainWindow::compile_source() {
750750

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

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

755755
int ln = 1;
756756
for (QTextBlock block = content->begin(); block.isValid(); block = block.next(), ln++) {
@@ -759,6 +759,10 @@ void MainWindow::compile_source() {
759759
}
760760
if (!sasm.finish()) { error_occured = true; }
761761

762+
connect(
763+
machine.data(), &machine::Machine::highlight_by_blocknum, editor,
764+
&SrcEditor::highlightBlock);
765+
762766
if (error_occured) { show_messages(); }
763767
}
764768

src/gui/windows/editor/srceditor.cpp

+27
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,29 @@ 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 block_num) {
295+
QList<QTextEdit::ExtraSelection> extra_selections;
296+
297+
// set hightly style
298+
QTextEdit::ExtraSelection selection;
299+
QColor lineColor = QColor(Qt::yellow).lighter(160);
300+
selection.format.setBackground(lineColor);
301+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
302+
303+
// select block
304+
QTextBlock block = document()->findBlockByNumber(block_num - 1);
305+
selection.cursor = QTextCursor(block);
306+
selection.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, block.length());
307+
extra_selections.append(selection);
308+
309+
// calculate viewport line count
310+
int viewport_line_count
311+
= viewport()->height() / QFontMetrics(document()->defaultFont()).height();
312+
// scroll to block and show it in editor middle
313+
QScrollBar *vScrollBar = verticalScrollBar();
314+
vScrollBar->setValue(
315+
vScrollBar->singleStep() * (block.firstLineNumber() - viewport_line_count / 2));
316+
317+
setExtraSelections(extra_selections);
318+
}

src/gui/windows/editor/srceditor.h

+1
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 block_num);
4546

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

src/machine/machine.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Machine::Machine(MachineConfig config, bool load_symtab, bool load_executable)
3434
mem = new Memory(*mem_program_only);
3535
} else {
3636
mem = new Memory(machine_config.get_simulated_endian());
37+
addr_to_blocknum = new QMap<machine::Address, int>();
3738
}
3839

3940
data_bus = new MemoryDataBus(machine_config.get_simulated_endian());
@@ -184,6 +185,8 @@ Machine::~Machine() {
184185
symtab = nullptr;
185186
delete predictor;
186187
predictor = nullptr;
188+
delete addr_to_blocknum;
189+
addr_to_blocknum = nullptr;
187190
}
188191

189192
const MachineConfig &Machine::config() {
@@ -211,6 +214,14 @@ Memory *Machine::memory_rw() {
211214
return mem;
212215
}
213216

217+
const QMap<machine::Address, int> *Machine::address_to_blocknum() {
218+
return addr_to_blocknum;
219+
}
220+
221+
QMap<machine::Address, int> *Machine::address_to_blocknum_rw() {
222+
return addr_to_blocknum;
223+
}
224+
214225
const Cache *Machine::cache_program() {
215226
return cch_program;
216227
}
@@ -331,6 +342,10 @@ void Machine::pause() {
331342

332343
void Machine::step_internal(bool skip_break) {
333344
CTL_GUARD;
345+
if (addr_to_blocknum != nullptr && addr_to_blocknum->contains(regs->read_pc())) {
346+
emit highlight_by_blocknum(addr_to_blocknum->value(regs->read_pc()));
347+
}
348+
334349
enum Status stat_prev = stat;
335350
set_status(ST_BUSY);
336351
emit tick();

src/machine/machine.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Machine : public QObject {
3636
const CSR::ControlState *control_state();
3737
const Memory *memory();
3838
Memory *memory_rw();
39+
const QMap<machine::Address, int> *address_to_blocknum();
40+
QMap<machine::Address, int> *address_to_blocknum_rw();
3941
const Cache *cache_program();
4042
const Cache *cache_data();
4143
const Cache *cache_level2();
@@ -92,6 +94,7 @@ public slots:
9294
void restart();
9395

9496
signals:
97+
void highlight_by_blocknum(int block_num);
9598
void program_exit();
9699
void program_trap(machine::SimulatorException &e);
97100
void status_change(enum machine::Machine::Status st);
@@ -114,6 +117,7 @@ private slots:
114117
* simulation reset without repeated ELF file loading.
115118
*/
116119
Memory *mem_program_only = nullptr;
120+
QMap<machine::Address, int> *addr_to_blocknum = nullptr;
117121
MemoryDataBus *data_bus = nullptr;
118122
SerialPort *ser_port = nullptr;
119123
PeripSpiLed *perip_spi_led = nullptr;

0 commit comments

Comments
 (0)