Implement cache size calculation
MODIFY
src/cachebase.cpp
src/cachebase.cpp
@@ -20,6 +20,36 @@ void CacheBase::updateCacheLineLRU(CacheLine& line, unsigned lruIdx) {
line[lruIdx].lru = 0;
}
+CacheSize CacheBase::getCacheSize() const {
+ CacheSize size;
+
+ const int entries = getLines() * getWays();
+
+ // Valid bits
+ unsigned componentBits = entries; // 1 bit per entry
+ size.components.push_back("Valid bits: " + QString::number(componentBits));
+ size.bits += componentBits;
+
+ if (m_policy == CacheReplPlcy::LRU) {
+ // LRU bits
+ componentBits = getWaysBits() * entries;
+ size.components.push_back("LRU bits: " + QString::number(componentBits));
+ size.bits += componentBits;
+ }
+
+ // Tag bits
+ componentBits = bitcount(m_tagMask) * entries;
+ size.components.push_back("Tag bits: " + QString::number(componentBits));
+ size.bits += componentBits;
+
+ // Data bits
+ componentBits = 32 * entries * getBlocks();
+ size.components.push_back("Data bits: " + QString::number(componentBits));
+ size.bits += componentBits;
+
+ return size;
+}
+
void CacheBase::updateCacheValue(uint32_t address) {
const unsigned lineIdx = getAccessLineIdx();
const unsigned blockIdx = getAccessBlockIdx();

MODIFY
src/cachebase.h
src/cachebase.h
@@ -12,6 +12,11 @@ enum class CacheReplPlcy { Random, LRU };
static std::map<CacheReplPlcy, QString> CachePolicyStrings{{CacheReplPlcy::Random, "Random"},
{CacheReplPlcy::LRU, "LRU"}};
+struct CacheSize {
+ unsigned bits = 0;
+ std::vector<QString> components;
+};
+
class CacheBase : public QObject {
Q_OBJECT
public:
@@ -29,7 +34,7 @@ public:
CacheReplPlcy getReplacementPolicy() const { return m_policy; }
double getHitRate() const;
- int getSizeInBits();
+ CacheSize getCacheSize() const;
int getBlockBits() const { return m_blocks; }
int getWaysBits() const { return m_ways; }

MODIFY
src/cacheconfigwidget.cpp
src/cacheconfigwidget.cpp
#include "cacheconfigwidget.h"
#include "ui_cacheconfigwidget.h"
+#include <QMessageBox>
#include <QSpinBox>
namespace Ripes {
@@ -16,15 +17,36 @@ void CacheConfigWidget::setCache(CacheBase* cache) {
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);
+
m_ui->ways->setValue(m_cache->getWaysBits());
m_ui->lines->setValue(m_cache->getLineBits());
m_ui->blocks->setValue(m_cache->getBlockBits());
}
+void CacheConfigWidget::setCacheSize(unsigned size) {
+ m_ui->size->setText(QString::number(size));
+}
+
void CacheConfigWidget::setHitRate(double hitrate) {
m_ui->hitrate->setText(QString::number(hitrate));
}
+void CacheConfigWidget::showSizeBreakdown() {
+ QString sizeText;
+ sizeText += "Cache size components:\n\n";
+
+ auto cacheSize = m_cache->getCacheSize();
+
+ for (const auto& component : cacheSize.components) {
+ sizeText += component + "\n";
+ }
+
+ sizeText += "\nTotal: " + QString::number(cacheSize.bits) + " Bits";
+
+ QMessageBox::information(this, "Cache size breakdown", sizeText);
+}
+
CacheConfigWidget::~CacheConfigWidget() {
delete m_ui;
}

MODIFY
src/cacheconfigwidget.h
src/cacheconfigwidget.h
@@ -19,8 +19,10 @@ public:
public slots:
void setHitRate(double hitrate);
+ void setCacheSize(unsigned);
private:
+ void showSizeBreakdown();
CacheBase* m_cache;
Ui::CacheConfigWidget* m_ui = nullptr;
};

Implement cache size calculation