Initial commit for the memory model
MODIFY
RISC-V-sim.pro
RISC-V-sim.pro
@@ -20,7 +20,8 @@ SOURCES += src/main.cpp \
src/cachesetupwidget.cpp \
src/cachebase.cpp \
src/runnercache.cpp \
- src/cacheinspector.cpp
+ src/cacheinspector.cpp \
+ src/memorymodel.cpp
HEADERS += \
src/parser.h \
@@ -37,7 +38,8 @@ HEADERS += \
src/cachesetupwidget.h \
src/cachebase.h \
src/runnercache.h \
- src/cacheinspector.h
+ src/cacheinspector.h \
+ src/memorymodel.h
FORMS += \
src/mainwindow.ui \

ADD
src/memorymodel.cpp
+#include "memorymodel.h"
+
+MemoryModel::MemoryModel(memory *memoryPtr, QObject *parent)
+ : QStandardItemModel(parent) {
+ m_memoryPtr = memoryPtr;
+
+ // Create initial view
+ // i is a byte index
+ for (int i = 0; i < m_addressRadius * 2 + 1; i++) {
+ auto lineAddress = m_centralAddress + m_addressRadius - i * 4;
+ auto iter = m_memoryPtr->find(lineAddress);
+ if (iter != m_memoryPtr->end()) {
+ setItem(i, 0, new QStandardItem(iter->first));
+ setItem(i, 1, new QStandardItem(iter->second));
+ }
+ for (int j = 1; j < 4; j++) {
+ // Locate the remaining 3 bytes in the word
+ if ((iter = m_memoryPtr->find(lineAddress + j)) != m_memoryPtr->end()) {
+ setItem(i, j + 1, new QStandardItem(iter->second));
+ } else {
+ // Memory is not initialized, set model data value to 0
+ setItem(i, j + 1, 0);
+ }
+ }
+ }
+
+ // Set headers
+ setHeaderData(0, Qt::Horizontal, "Address");
+ setHeaderData(1, Qt::Horizontal, "+0");
+ setHeaderData(2, Qt::Horizontal, "+1");
+ setHeaderData(3, Qt::Horizontal, "+2");
+ setHeaderData(4, Qt::Horizontal, "+3");
+}

ADD
src/memorymodel.h
+#ifndef MEMORYMODEL_H
+#define MEMORYMODEL_H
+
+#include "defines.h"
+
+#include <QStandardItemModel>
+
+#include <unordered_map>
+
+class MemoryModel : public QStandardItemModel {
+ Q_OBJECT
+
+public:
+ explicit MemoryModel(memory *memoryPtr, QObject *parent = nullptr);
+
+ // Custom functionality
+ void setCentralAddress(uint32_t address);
+ void jumpToAddress(uint32_t address);
+ void moveSelection(int dir);
+
+ void setAddressCount(int count);
+
+private:
+ // instead of direct access to the memory, we should have a pointer
+ // to the runner, which can return requested values, or null, if memory
+ // doesnt exist
+ memory *m_memoryPtr;
+
+ uint32_t m_centralAddress = 0; // Address at the center of the model
+ int m_addressRadius = 5; // amount of addresses in each direction, from
+ // centralAddress
+};
+
+#endif // MEMORYMODEL_H

MODIFY
src/memorytab.cpp
src/memorytab.cpp
@@ -13,7 +13,10 @@ MemoryTab::MemoryTab(QWidget *parent)
void MemoryTab::init() {
Q_ASSERT(m_memoryPtr != nullptr && m_regPtr != nullptr);
initializeMemoryView();
+ initializeRegisterView();
+}
+void MemoryTab::initializeRegisterView() {
// Initialize register ABInames
QStringList ABInames = QStringList() << "zero"
<< "ra"
@@ -104,6 +107,7 @@ void MemoryTab::updateRegisterWidget(int n) {
}
void MemoryTab::initializeMemoryView() {
+ /*
std::vector<std::pair<uint32_t, uint8_t>> sortedMemory(m_memoryPtr->begin(),
m_memoryPtr->end());
std::sort(sortedMemory.begin(), sortedMemory.end(),
@@ -124,5 +128,11 @@ void MemoryTab::initializeMemoryView() {
m_ui->memoryView->setItem(i, 1, val);
i++;
}
+*/
+ m_model = new MemoryModel(m_memoryPtr);
+ m_ui->memoryView->setModel(m_model);
+ m_ui->memoryView->horizontalHeader()->setStretchLastSection(true);
+ m_ui->memoryView->verticalHeader()->hide();
}
+
MemoryTab::~MemoryTab() { delete m_ui; }

MODIFY
src/memorytab.h
src/memorytab.h
#include "defines.h"
#include <QWidget>
+
#include <unordered_map>
+#include "memorymodel.h"
+
namespace Ui {
class MemoryTab;
}
@@ -28,8 +31,10 @@ public slots:
private:
void initializeMemoryView();
+ void initializeRegisterView();
Ui::MemoryTab *m_ui;
+ MemoryModel *m_model;
memory *m_memoryPtr;
std::vector<uint32_t> *m_regPtr;

MODIFY
src/memorytab.ui
src/memorytab.ui
<rect>
<x>0</x>
<y>0</y>
- <width>658</width>
+ <width>785</width>
<height>415</height>
</rect>
</property>
<rect>
<x>0</x>
<y>0</y>
- <width>179</width>
- <height>342</height>
+ <width>231</width>
+ <height>335</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<rect>
<x>0</x>
<y>0</y>
- <width>445</width>
- <height>342</height>
+ <width>520</width>
+ <height>335</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<layout class="QVBoxLayout" name="memoryLayout">
<item>
- <widget class="QTableWidget" name="memoryView"/>
+ <widget class="QTableView" name="memoryView"/>
</item>
</layout>
</item>

Initial commit for the memory model