From 9009d358f7959b1dd60b77fea181be04ae190ef3 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 10 Mar 2025 16:44:39 -0400 Subject: Add starter overloaded << operator for cache --- src/storage/cache.cc | 42 ++++++++++++++++++++++++++++++++++++++++-- src/storage/storage.cc | 2 +- 2 files changed, 41 insertions(+), 3 deletions(-) (limited to 'src/storage') 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 +#include +#include +#include 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, L1_CACHE_SIZE> *Cache::get_meta() +std::array, L1_CACHE_SIZE> Cache::get_meta() const { - return &(this->meta); + std::array, 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> data = + c.view(0, L1_CACHE_SIZE); + std::array, 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(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(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 std::vector> -Storage::view(int base, int lines) +Storage::view(int base, int lines) const { base = (base / LINE_SIZE) * LINE_SIZE; std::vector> ret(lines + 1); -- cgit v1.2.3