summaryrefslogtreecommitdiff
path: root/src/storage
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
commite24e3cd4d296599b9ef1b705846b1c868148b0fd (patch)
tree25646d98b4bfcf4b9a664eabfc2651c481984c1d /src/storage
parent357e7fb37caf58cdfcdf85f7553db9378ff16e0c (diff)
parentfde996690d77b81e445450671a0723f837de4eb3 (diff)
Merge pull request #23 from bdunahu/bdunahu
Memory simulator CLI function implementation
Diffstat (limited to 'src/storage')
-rw-r--r--src/storage/cache.cc47
-rw-r--r--src/storage/dram.cc28
-rw-r--r--src/storage/storage.cc6
3 files changed, 79 insertions, 2 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;
+}
diff --git a/src/storage/dram.cc b/src/storage/dram.cc
index 441f10b..e755c2a 100644
--- a/src/storage/dram.cc
+++ b/src/storage/dram.cc
@@ -2,6 +2,10 @@
#include "definitions.h"
#include "response.h"
#include <algorithm>
+#include <bits/stdc++.h>
+#include <bitset>
+#include <iostream>
+#include <iterator>
Dram::Dram(int lines, int delay)
{
@@ -70,3 +74,27 @@ Response Dram::read(
return r;
}
+
+std::ostream &operator<<(std::ostream &os, const Dram &d)
+{
+ const auto default_flags = std::cout.flags();
+ const auto default_fill = std::cout.fill();
+
+ std::vector<std::array<signed int, LINE_SIZE>> data = d.view(0, MEM_SIZE);
+
+ os << " " << std::setfill(' ') << std::setw(MEM_SPEC + 2) << "INDEX"
+ << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA"
+ << std::endl;
+ for (int i = 0; i < MEM_SIZE; ++i) {
+ os << " 0b" << std::setw(MEM_SPEC) << std::bitset<MEM_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 << std::endl;
+ }
+
+ std::cout.flags(default_flags);
+ std::cout.fill(default_fill);
+ return os;
+}
diff --git a/src/storage/storage.cc b/src/storage/storage.cc
index 61531d1..8e2e461 100644
--- a/src/storage/storage.cc
+++ b/src/storage/storage.cc
@@ -3,7 +3,7 @@
#include <algorithm>
std::vector<std::array<signed int, LINE_SIZE>>
-Storage::view(int base, int lines)
+Storage::view(int base, int lines) const
{
base = (base / LINE_SIZE) * LINE_SIZE;
std::vector<std::array<signed int, LINE_SIZE>> ret(lines + 1);
@@ -13,8 +13,12 @@ Storage::view(int base, int lines)
return ret;
}
+Storage *Storage::get_lower() { return this->lower; }
+
void Storage::resolve()
{
+ if (this->lower)
+ this->lower->resolve();
if (this->wait_time == 0) {
this->requester = IDLE;
this->wait_time = delay;