Work on DM cache funtionality
MODIFY
src/cache.cpp
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);
-
+Cache::Cache(int missPenalty, int searchPenalty) {
// 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);
- }
+void Cache::assertSize(int size) const {
+ // Assert cache size is a power of 2
+ assert((size & (size - 1)) == 0);
}

MODIFY
src/cache.h
src/cache.h
class Cache {
public:
- Cache(int size, int missPenalty = 0, int searchPenalty = 0);
- virtual uint32_t readData(uint32_t address) const = 0;
+ Cache(int missPenalty = 0, int searchPenalty = 0);
+ virtual uint32_t readData(uint32_t address) = 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);
+ virtual void resize(int size) = 0;
+
+protected:
+ void assertSize(int size) const;
+ int m_readCount = 0;
+ int m_missCount = 0;
private:
- std::vector<uint32_t> m_cache;
int m_missPenalty;
int m_searchPenalty;
uint32_t *m_memoryPtr;

MODIFY
src/dmcache.cpp
src/dmcache.cpp
#include "dmcache.h"
-DMCache::DMCache(int size) : Cache(size) {}
+DMCache::DMCache(int size) : Cache() {
+ assertSize(size);
-uint32_t DMCache::readData(uint32_t address) const { return 0; }
+ // Initialize cache to all zeros and invalidated
+ for (auto i = 0; i < size; i++) {
+ // Initialize cache line to 0, and invalid
+ m_cache[i] = std::pair<bool, uint32_t>(false, 0);
+ }
+}
+
+uint32_t DMCache::readData(uint32_t address) {
+ m_readCount++;
+
+ auto it = m_cache.find(address);
+ if (it != m_cache.end() && it->second.first) {
+ // Cache hit
+ return it->second.second;
+ } else {
+ // Cache miss
+ m_missCount++;
+ }
+}
void DMCache::writeData(uint32_t address) {}
void DMCache::searchCache(uint32_t address) const {}
+
+void Cache::resize(int size) {
+ assertSize(size);
+ // do resize...
+}

MODIFY
src/dmcache.h
src/dmcache.h
#include "cache.h"
#include <cstdint>
+#include <map>
+
// Direct mapped cache
class DMCache : public Cache {
public:
DMCache(int size);
- uint32_t readData(uint32_t address) const override;
+ uint32_t readData(uint32_t address) override;
void writeData(uint32_t address) override;
private:
void searchCache(uint32_t address) const;
+
+ // Cache is a map of an (up to) 32-bit tag, and a pair of a valid-bit and the
+ // actual data
+ std::map<uint32_t, std::pair<bool, uint32_t>> m_cache;
};
#endif // DMCACHE_H

MODIFY
src/facache.cpp
src/facache.cpp
FACache::FACache(int size) : Cache(size) {}
-uint32_t FACache::readData(uint32_t address) const { return 0; }
+uint32_t FACache::readData(uint32_t address) {
+ m_readCount++;
+
+ return 0;
+}
void FACache::writeData(uint32_t address) {}

MODIFY
src/facache.h
src/facache.h
@@ -8,7 +8,7 @@ class FACache : public Cache {
public:
FACache(int size);
- uint32_t readData(uint32_t address) const override;
+ uint32_t readData(uint32_t address) override;
void writeData(uint32_t address) override;
private:

Work on DM cache funtionality