diff options
author | bd <bdunahu@operationnull.com> | 2025-03-10 16:44:39 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-03-10 16:44:39 -0400 |
commit | 9009d358f7959b1dd60b77fea181be04ae190ef3 (patch) | |
tree | bfd4d2a5303d15f625b4d945800c0c7c55a2bac7 | |
parent | cdcaec3c1e62b47fcd270df651cd0e343e46f0e7 (diff) |
Add starter overloaded << operator for cache
-rw-r--r-- | inc/cache.h | 5 | ||||
-rw-r--r-- | inc/storage.h | 2 | ||||
-rw-r--r-- | src/cli/cli.cc | 8 | ||||
-rw-r--r-- | src/storage/cache.cc | 42 | ||||
-rw-r--r-- | src/storage/storage.cc | 2 |
5 files changed, 49 insertions, 10 deletions
diff --git a/inc/cache.h b/inc/cache.h index b7b72cf..0c9b3d7 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -3,6 +3,7 @@ #include "definitions.h" #include "storage.h" #include <array> +#include <ostream> class Cache : public Storage { @@ -30,7 +31,7 @@ class Cache : public Storage * TODO this doesn't seem like good object-oriented practice. * @return this->meta */ - std::array<std::array<int, 2>, L1_CACHE_SIZE> *get_meta(); + std::array<std::array<int, 2>, L1_CACHE_SIZE> get_meta() const; private: /** @@ -49,4 +50,6 @@ class Cache : public Storage std::array<std::array<int, 2>, L1_CACHE_SIZE> meta; }; +std::ostream &operator<<(std::ostream &os, const Cache &a); + #endif /* CACHE_H_INCLUDED */ diff --git a/inc/storage.h b/inc/storage.h index 0ab16e4..3f113d3 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -45,7 +45,7 @@ class Storage * @return A matrix of data values, where each row is a line and each column * is a word. */ - std::vector<std::array<signed int, LINE_SIZE>> view(int base, int lines); + std::vector<std::array<signed int, LINE_SIZE>> view(int base, int lines) const; /** * Advances to the next job if the current job is completed. */ diff --git a/src/cli/cli.cc b/src/cli/cli.cc index fea44d7..d1631b4 100644 --- a/src/cli/cli.cc +++ b/src/cli/cli.cc @@ -120,11 +120,9 @@ void Cli::view(int level, int base, int lines) curr = curr->get_lower(); } - std::vector<std::array<signed int, LINE_SIZE>> data = - curr->view(base, lines); - - if (dynamic_cast<const Cache *>(curr)) { - + Cache *c = dynamic_cast<Cache *>(curr); + if (c) { + std::cout << *c; } else { std::cout << "dram"; } diff --git a/src/storage/cache.cc b/src/storage/cache.cc index 55c8cfa..45ef0ab 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) { @@ -81,7 +84,42 @@ 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() +std::array<std::array<int, 2>, L1_CACHE_SIZE> Cache::get_meta() const { - return &(this->meta); + 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 + 1) << " ADDRESS |" + << std::setfill(' ') << std::setw(11) << "0" << std::setfill(' ') + << std::setw(11) << "1" << std::setfill(' ') << std::setw(11) << "2" + << std::setfill(' ') << std::setw(11) << "3" + << " |" << std::setfill(' ') + << std::setw(MEM_SPEC - LINE_SPEC - L1_CACHE_SPEC + 4) << "TAG " + << "| D \n"; + 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) << '\n'; + } + + 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 16ad63f..62f3699 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); |