summaryrefslogtreecommitdiff
path: root/src/storage/cache.cc
diff options
context:
space:
mode:
authorSiddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com>2025-03-11 11:28:25 -0400
committerGitHub <noreply@github.com>2025-03-11 11:28:25 -0400
commit33c7c78b1c65c375d0291fd435e02ddc9d35681b (patch)
tree25646d98b4bfcf4b9a664eabfc2651c481984c1d /src/storage/cache.cc
parent66edce63597093cf5f3afa5b577fd9e3ecae0ef6 (diff)
parent202f9a05d449ddc1160584c4e8a87f397f248e94 (diff)
Merge pull request #23 from bdunahu/bdunahu
Memory simulator CLI function implementation
Diffstat (limited to 'src/storage/cache.cc')
-rw-r--r--src/storage/cache.cc47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/storage/cache.cc b/src/storage/cache.cc
index 2031367..1a8a10b 100644
--- a/src/storage/cache.cc
+++ b/src/storage/cache.cc
@@ -3,6 +3,9 @@
#include "response.h"
#include "utils.h"
#include <bits/stdc++.h>
+#include <bitset>
+#include <iostream>
+#include <iterator>
Cache::Cache(Storage *lower, int delay)
{
@@ -16,7 +19,11 @@ Cache::Cache(Storage *lower, int delay)
this->wait_time = this->delay;
}
-Cache::~Cache() { delete this->data; }
+Cache::~Cache()
+{
+ delete this->lower;
+ delete this->data;
+}
Response Cache::write(Accessor accessor, signed int data, int address)
{
@@ -76,3 +83,41 @@ void Cache::fetch_resource(int expected)
this->is_waiting = (r == OK) ? false : true;
}
+
+std::array<std::array<int, 2>, L1_CACHE_SIZE> Cache::get_meta() const
+{
+ std::array<std::array<int, 2>, L1_CACHE_SIZE> ret;
+ std::copy(std::begin(this->meta), std::end(this->meta), std::begin(ret));
+ return ret;
+}
+
+std::ostream &operator<<(std::ostream &os, const Cache &c)
+{
+ const auto default_flags = std::cout.flags();
+ const auto default_fill = std::cout.fill();
+
+ std::vector<std::array<signed int, LINE_SIZE>> data =
+ c.view(0, L1_CACHE_SIZE);
+ std::array<std::array<int, 2>, L1_CACHE_SIZE> meta = c.get_meta();
+
+ os << " " << std::setfill(' ') << std::setw(L1_CACHE_SPEC + 2) << "INDEX"
+ << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA"
+ << " | " << std::setfill(' ')
+ << std::setw(MEM_SPEC - LINE_SPEC - L1_CACHE_SPEC + 2) << "TAG"
+ << " | D" << std::endl;
+ for (int i = 0; i < L1_CACHE_SIZE; ++i) {
+ os << " 0b" << std::setw(L1_CACHE_SPEC) << std::bitset<L1_CACHE_SPEC>(i)
+ << " | ";
+ for (int j = 0; j < LINE_SIZE; ++j) {
+ os << "0x" << std::setfill('0') << std::setw(8) << std::hex
+ << data.at(i).at(j) << " ";
+ }
+ os << "| 0x" << std::setfill(' ')
+ << std::bitset<MEM_SPEC - LINE_SPEC - L1_CACHE_SPEC>(meta.at(i)[0])
+ << " | " << (int)(meta.at(i)[0] >= 0) << std::endl;
+ }
+
+ std::cout.flags(default_flags);
+ std::cout.fill(default_fill);
+ return os;
+}