| 1 | #include "MainMemoryViewer.h" |
| 2 | #include "HexViewer.h" |
| 3 | #include "CPURegs.h" |
| 4 | #include "CPURegsViewer.h" |
| 5 | #include "SymbolTable.h" |
| 6 | #include "Convert.h" |
| 7 | #include <QComboBox> |
| 8 | #include <QVBoxLayout> |
| 9 | #include <QHBoxLayout> |
| 10 | #include <QLineEdit> |
| 11 | #include <iostream> |
| 12 | |
| 13 | static const int linkRegisters[] = { |
| 14 | CpuRegs::REG_BC, CpuRegs::REG_DE, CpuRegs::REG_HL, |
| 15 | CpuRegs::REG_IX, CpuRegs::REG_IY, |
| 16 | CpuRegs::REG_BC2, CpuRegs::REG_DE2, CpuRegs::REG_HL2, |
| 17 | CpuRegs::REG_PC, CpuRegs::REG_SP |
| 18 | }; |
| 19 | |
| 20 | MainMemoryViewer::MainMemoryViewer(QWidget* parent) |
| 21 | : QWidget(parent) |
| 22 | { |
| 23 | // create selection list, address edit line and viewer |
| 24 | addressSourceList = new QComboBox(); |
| 25 | addressSourceList->setEditable(false); |
| 26 | addressSourceList->addItem("Address:"); |
| 27 | for (int lr : linkRegisters) { |
| 28 | QString txt = QString("Linked to "); |
| 29 | txt.append(CpuRegs::regNames[lr]); |
| 30 | addressSourceList->addItem(txt); |
| 31 | } |
| 32 | |
| 33 | addressValue = new QLineEdit(); |
| 34 | addressValue->setText(hexValue(0, 4)); |
| 35 | //addressValue->setEditable(false); |
| 36 | |
| 37 | hexView = new HexViewer(); |
| 38 | hexView->setUseMarker(true); |
| 39 | hexView->setIsEditable(true); |
| 40 | hexView->setIsInteractive(true); |
| 41 | hexView->setDisplayMode(HexViewer::FILL_WIDTH_POWEROF2); |
| 42 | auto* hbox = new QHBoxLayout(); |
| 43 | hbox->setMargin(0); |
| 44 | hbox->addWidget(addressSourceList); |
| 45 | hbox->addWidget(addressValue); |
| 46 | |
| 47 | auto* vbox = new QVBoxLayout(); |
| 48 | vbox->setMargin(0); |
| 49 | vbox->addLayout(hbox); |
| 50 | vbox->addWidget(hexView); |
| 51 | setLayout(vbox); |
| 52 | |
| 53 | isLinked = false; |
| 54 | linkedId = 0; |
| 55 | regsViewer = nullptr; |
| 56 | symTable = nullptr; |
| 57 | |
| 58 | connect(hexView, &HexViewer::locationChanged, |
| 59 | this, &MainMemoryViewer::hexViewChanged); |
| 60 | connect(addressValue, &QLineEdit::returnPressed, |
| 61 | this, &MainMemoryViewer::addressValueChanged); |
| 62 | connect(addressSourceList, qOverload<int>(&QComboBox::currentIndexChanged), |
| 63 | this, &MainMemoryViewer::addressSourceListChanged); |
| 64 | } |
| 65 | |
| 66 | void MainMemoryViewer::settingsChanged() |
| 67 | { |
| 68 | hexView->settingsChanged(); |
| 69 | } |
| 70 | |
| 71 | void MainMemoryViewer::setLocation(int addr) |
| 72 | { |
| 73 | addressValue->setText(hexValue(addr, 4)); |
| 74 | hexView->setLocation(addr); |
| 75 | } |
| 76 | |
| 77 | void MainMemoryViewer::setDebuggable(const QString& name, int size) |
| 78 | { |
| 79 | hexView->setDebuggable(name, size); |
| 80 | } |
| 81 | |
| 82 | void MainMemoryViewer::setRegsView(CPURegsViewer* viewer) |
| 83 | { |
| 84 | regsViewer = viewer; |
| 85 | } |
| 86 | |
| 87 | void MainMemoryViewer::setSymbolTable(SymbolTable* symtable) |
| 88 | { |
| 89 | symTable = symtable; |
| 90 | } |
| 91 | |
| 92 | void MainMemoryViewer::refresh() |
| 93 | { |
| 94 | hexView->refresh(); |
| 95 | } |
| 96 | |
| 97 | void MainMemoryViewer::hexViewChanged(int addr) |
| 98 | { |
| 99 | addressValue->setText(hexValue(addr, 4)); |
| 100 | } |
| 101 | |
| 102 | void MainMemoryViewer::addressValueChanged() |
| 103 | { |
| 104 | auto addr = stringToValue<uint16_t>(addressValue->text()); |
| 105 | if (!addr && symTable) { |
| 106 | // try finding a label |
| 107 | Symbol *s = symTable->getAddressSymbol(addressValue->text()); |
| 108 | if (!s) s = symTable->getAddressSymbol(addressValue->text(), Qt::CaseInsensitive); |
| 109 | if (s) addr = s->value(); |
| 110 | } |
| 111 | |
| 112 | if (addr) hexView->setLocation(*addr); |
| 113 | } |
| 114 | |
| 115 | void MainMemoryViewer::registerChanged(int id, int value) |
| 116 | { |
| 117 | if (!isLinked || (id != linkedId)) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | addressValue->setText(hexValue(value, 4)); |
| 122 | hexView->setLocation(value); |
| 123 | //hexView->refresh(); |
| 124 | } |
| 125 | |
| 126 | void MainMemoryViewer::addressSourceListChanged(int index) |
| 127 | { |
| 128 | if (index == 0) { |
| 129 | isLinked = false; |
| 130 | addressValue->setReadOnly(false); |
| 131 | hexView->setIsInteractive(true); |
| 132 | } else { |
| 133 | isLinked = true; |
| 134 | linkedId = linkRegisters[index - 1]; |
| 135 | addressValue->setReadOnly(true); |
| 136 | hexView->setIsInteractive(false); |
| 137 | if (regsViewer) { |
| 138 | setLocation(regsViewer->readRegister(linkedId)); |
| 139 | } |
| 140 | } |
| 141 | } |