Create base- and subclasses for cache objects.
MODIFY
RISC-V-sim.pro
RISC-V-sim.pro
@@ -5,9 +5,15 @@ CONFIG -= qt
SOURCES += src/main.cpp \
src/parser.cpp \
- src/runner.cpp
+ src/runner.cpp \
+ src/cache.cpp \
+ src/dmcache.cpp \
+ src/facache.cpp
HEADERS += \
src/parser.h \
src/defines.h \
- src/runner.h
+ src/runner.h \
+ src/cache.h \
+ src/dmcache.h \
+ src/facache.h

ADD
src/cache.cpp
+#include "cache.h"
+
+#include <assert.h>
+
+Cache::Cache(int size, int missPenalty, int searchPenalty) {
+
+ // Assert cache size is a power of 2
+ assert((size & (size - 1)) == 0);
+
+ // Initialize cache of size, set to zeros
+ m_cache = std::vector<uint32_t>(size, 0);
+ m_missPenalty = missPenalty;
+ m_searchPenalty = searchPenalty;
+}
+
+void Cache::resize(int size) {
+ // Only resize if size is power of 2
+ if ((size & (size - 1)) == 0) {
+ m_cache.resize(size);
+ }
+}

ADD
src/cache.h
+#ifndef CACHE_H
+#define CACHE_H
+
+#include <cstdint>
+#include <vector>
+
+class Cache {
+public:
+ Cache(int size, int missPenalty = 0, int searchPenalty = 0);
+ virtual uint32_t readData(uint32_t address) const = 0;
+ virtual void writeData(uint32_t address) = 0;
+
+ void setMissPenalty(int cycles) { m_missPenalty = cycles; }
+ void setSearchPenalty(int cycles) { m_searchPenalty = cycles; }
+
+ void resize(int size);
+
+private:
+ std::vector<uint32_t> m_cache;
+ int m_missPenalty;
+ int m_searchPenalty;
+ uint32_t *m_memoryPtr;
+};
+
+#endif // CACHE_H

ADD
src/dmcache.cpp
+#include "dmcache.h"
+
+DMCache::DMCache(int size) : Cache(size) {}
+
+uint32_t DMCache::readData(uint32_t address) const { return 0; }
+
+void DMCache::writeData(uint32_t address) {}
+
+void DMCache::searchCache(uint32_t address) const {}

ADD
src/dmcache.h
+#ifndef DMCACHE_H
+#define DMCACHE_H
+
+#include "cache.h"
+#include <cstdint>
+
+// Direct mapped cache
+class DMCache : public Cache {
+public:
+ DMCache(int size);
+
+ uint32_t readData(uint32_t address) const override;
+ void writeData(uint32_t address) override;
+
+private:
+ void searchCache(uint32_t address) const;
+};
+
+#endif // DMCACHE_H

ADD
src/facache.cpp
+#include "facache.h"
+
+FACache::FACache(int size) : Cache(size) {}
+
+uint32_t FACache::readData(uint32_t address) const { return 0; }
+
+void FACache::writeData(uint32_t address) {}
+
+void FACache::searchCache(uint32_t address) const {}

ADD
src/facache.h
+#ifndef FACACHE_H
+#define FACACHE_H
+
+#include "cache.h"
+
+// Fully associative cache
+class FACache : public Cache {
+public:
+ FACache(int size);
+
+ uint32_t readData(uint32_t address) const override;
+ void writeData(uint32_t address) override;
+
+private:
+ void searchCache(uint32_t address) const;
+};
+
+#endif // FACACHE_H

Create base- and subclasses for cache objects.