Add cache configuration presets
MODIFY
src/cacheconfigwidget.cpp
src/cacheconfigwidget.cpp
@@ -10,18 +10,58 @@ CacheConfigWidget::CacheConfigWidget(QWidget* parent) : QWidget(parent), m_ui(ne
m_ui->setupUi(this);
}
-void CacheConfigWidget::setCache(CacheBase* cache) {
+void CacheConfigWidget::setCache(CacheSim* cache) {
m_cache = cache;
- connect(m_ui->ways, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheBase::setWays);
- connect(m_ui->blocks, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheBase::setBlocks);
- connect(m_ui->lines, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheBase::setLines);
-
- connect(m_ui->sizeBreakdownButton, &QPushButton::clicked, this, &CacheConfigWidget::showSizeBreakdown);
-
+ for (const auto& policy : s_cachePolicyStrings) {
+ m_ui->replacementPolicy->addItem(policy.second, QVariant::fromValue(policy.first));
+ }
m_ui->ways->setValue(m_cache->getWaysBits());
m_ui->lines->setValue(m_cache->getLineBits());
m_ui->blocks->setValue(m_cache->getBlockBits());
+
+ connect(m_ui->ways, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheSim::setWays);
+ connect(m_ui->blocks, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheSim::setBlocks);
+ connect(m_ui->lines, QOverload<int>::of(&QSpinBox::valueChanged), m_cache, &CacheSim::setLines);
+ connect(m_ui->sizeBreakdownButton, &QPushButton::clicked, this, &CacheConfigWidget::showSizeBreakdown);
+ connect(m_ui->replacementPolicy, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
+ m_cache->setReplacementPolicy(qvariant_cast<CacheReplPlcy>(m_ui->replacementPolicy->itemData(index)));
+ });
+
+ setupPresets();
+ m_ui->replacementPolicy->setCurrentIndex(0);
+}
+
+void CacheConfigWidget::setupPresets() {
+ std::vector<std::pair<QString, CachePreset>> presets;
+
+ presets.push_back({"32-entry 4-word direct-mapped", CachePreset{2, 5, 0}});
+ presets.push_back({"32-entry 4-word fully associative", CachePreset{2, 0, 5}});
+ presets.push_back({"32-entry 4-word 2-way set associative", CachePreset{2, 4, 1}});
+
+ for (const auto& preset : presets) {
+ m_ui->presets->addItem(preset.first, QVariant::fromValue<CachePreset>(preset.second));
+ }
+
+ connect(m_ui->presets, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
+ const CachePreset preset = qvariant_cast<CachePreset>(m_ui->presets->itemData(index));
+
+ // Block signal emmision from the value widgets; the cache sim is updated all at once internally, so no need to
+ // emit signals.
+ m_ui->ways->blockSignals(true);
+ m_ui->lines->blockSignals(true);
+ m_ui->blocks->blockSignals(true);
+ m_ui->ways->setValue(preset.ways);
+ m_ui->lines->setValue(preset.lines);
+ m_ui->blocks->setValue(preset.blocks);
+ m_ui->ways->blockSignals(false);
+ m_ui->lines->blockSignals(false);
+ m_ui->blocks->blockSignals(false);
+
+ m_cache->setPreset(preset);
+ });
+
+ m_ui->presets->setCurrentIndex(0);
}
void CacheConfigWidget::setCacheSize(unsigned size) {
@@ -34,7 +74,6 @@ void CacheConfigWidget::setHitRate(double hitrate) {
void CacheConfigWidget::showSizeBreakdown() {
QString sizeText;
- sizeText += "Cache size components:\n\n";
auto cacheSize = m_cache->getCacheSize();

MODIFY
src/cacheconfigwidget.h
src/cacheconfigwidget.h
#pragma once
#include <QWidget>
-#include "cachebase.h"
+#include "cachesim.h"
namespace Ripes {
@@ -11,20 +11,25 @@ class CacheConfigWidget;
class CacheConfigWidget : public QWidget {
Q_OBJECT
+
public:
CacheConfigWidget(QWidget* parent);
~CacheConfigWidget() override;
- void setCache(CacheBase* cache);
+ void setCache(CacheSim* cache);
public slots:
void setHitRate(double hitrate);
void setCacheSize(unsigned);
private:
+ void setupPresets();
void showSizeBreakdown();
- CacheBase* m_cache;
+ CacheSim* m_cache;
Ui::CacheConfigWidget* m_ui = nullptr;
};
} // namespace Ripes
+
+Q_DECLARE_METATYPE(Ripes::CacheReplPlcy);
+Q_DECLARE_METATYPE(Ripes::CachePreset);

MODIFY
src/cachegraphic.cpp
src/cachegraphic.cpp
namespace Ripes {
-CacheGraphic::CacheGraphic(CacheBase& cache) : QGraphicsObject(nullptr), m_cache(cache) {
- connect(&cache, &CacheBase::configurationChanged, this, &CacheGraphic::cacheParametersChanged);
- connect(&cache, &CacheBase::accessChanged, this, &CacheGraphic::updateHighlighting);
- connect(&cache, &CacheBase::dataChanged, this, &CacheGraphic::dataChanged);
+CacheGraphic::CacheGraphic(CacheSim& cache) : QGraphicsObject(nullptr), m_cache(cache) {
+ connect(&cache, &CacheSim::configurationChanged, this, &CacheGraphic::cacheParametersChanged);
+ connect(&cache, &CacheSim::accessChanged, this, &CacheGraphic::updateHighlighting);
+ connect(&cache, &CacheSim::dataChanged, this, &CacheGraphic::dataChanged);
cacheParametersChanged();
}

MODIFY
src/cachegraphic.h
src/cachegraphic.h
#include <QGraphicsItem>
#include <QObject>
#include <memory>
-#include "cachebase.h"
+#include "cachesim.h"
namespace Ripes {
class CacheGraphic : public QGraphicsObject {
public:
- CacheGraphic(CacheBase& cache);
+ CacheGraphic(CacheSim& cache);
QRectF boundingRect() const override {}
@@ -34,7 +34,7 @@ private:
void drawText(const QString& text, qreal x, qreal y);
QFont m_font = QFont("Inconsolata", 12);
- CacheBase& m_cache;
+ CacheSim& m_cache;
std::vector<std::unique_ptr<QGraphicsRectItem>> m_highlightingItems;

MODIFY
src/memorytab.cpp
src/memorytab.cpp
#include "memorytab.h"
#include "ui_memorytab.h"
-#include "cachebase.h"
+#include "cachesim.h"
#include "cachegraphic.h"
#include <QGraphicsItem>
@@ -20,10 +20,10 @@ MemoryTab::MemoryTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, par
m_ui->memoryViewerWidget->updateView();
auto* scene = new QGraphicsScene(this);
- auto* cacheSim = new CacheBase(this);
+ auto* cacheSim = new CacheSim(this);
m_ui->cacheConfig->setCache(cacheSim);
- connect(cacheSim, &CacheBase::hitRateChanged, m_ui->cacheConfig, &CacheConfigWidget::setHitRate);
+ connect(cacheSim, &CacheSim::hitRateChanged, m_ui->cacheConfig, &CacheConfigWidget::setHitRate);
auto* cacheGraphic = new CacheGraphic(*cacheSim);
m_ui->cacheView->setScene(scene);
scene->addItem(cacheGraphic);

MODIFY
src/memorytab.ui
src/memorytab.ui
<property name="windowTitle">
<string>Form</string>
</property>
- <layout class="QHBoxLayout" name="horizontalLayout_2">
- <item>
- <layout class="QHBoxLayout" name="horizontalLayout">
- <item>
+ <layout class="QGridLayout" name="gridLayout">
+ <item row="0" column="0">
+ <widget class="QSplitter" name="splitter">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
<widget class="MemoryViewerWidget" name="memoryViewerWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
</sizepolicy>
</property>
</widget>
- </item>
- <item>
+ <widget class="QWidget" name="">
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
<item>
<widget class="CacheConfigWidget" name="cacheConfig" native="true"/>
<widget class="QGraphicsView" name="cacheView"/>
</item>
</layout>
- </item>
- </layout>
+ </widget>
+ </widget>
</item>
</layout>
</widget>

Add cache configuration presets