From 486d18df5ca93e043fdd14fac1d22b5fe40fb6f6 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 10 Mar 2025 14:53:59 -0400 Subject: Update cli method signatures, add some getters to cache and storage --- inc/cache.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'inc/cache.h') diff --git a/inc/cache.h b/inc/cache.h index e8b7030..b7b72cf 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -25,6 +25,13 @@ class Cache : public Storage int address, std::array &data) override; + /** + * Getter for the meta attribute. + * TODO this doesn't seem like good object-oriented practice. + * @return this->meta + */ + std::array, L1_CACHE_SIZE> *get_meta(); + private: /** * Fetches `address` from a lower level of storage if it is not already -- cgit v1.2.3 From 141494cb961b72a7ad56c3e754af43a07f1b8c23 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 --- inc/cache.h | 5 ++++- inc/storage.h | 2 +- src/cli/cli.cc | 8 +++----- src/storage/cache.cc | 42 ++++++++++++++++++++++++++++++++++++++++-- src/storage/storage.cc | 2 +- 5 files changed, 49 insertions(+), 10 deletions(-) (limited to 'inc/cache.h') 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 +#include 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, L1_CACHE_SIZE> *get_meta(); + std::array, L1_CACHE_SIZE> get_meta() const; private: /** @@ -49,4 +50,6 @@ class Cache : public Storage std::array, 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> view(int base, int lines); + std::vector> 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> data = - curr->view(base, lines); - - if (dynamic_cast(curr)) { - + Cache *c = dynamic_cast(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 +#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 From 85078b99e4cf652fcbd5f4d36b061674a2fe8aa6 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 10 Mar 2025 22:07:36 -0400 Subject: overload << operator for dram --- inc/cache.h | 2 +- inc/definitions.h | 2 +- inc/dram.h | 14 ++++++++++---- src/cli/cli.cc | 10 ++++++---- src/main.cc | 4 ++-- src/storage/cache.cc | 1 + src/storage/dram.cc | 29 +++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 12 deletions(-) (limited to 'inc/cache.h') diff --git a/inc/cache.h b/inc/cache.h index 0c9b3d7..04f6181 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -50,6 +50,6 @@ class Cache : public Storage std::array, L1_CACHE_SIZE> meta; }; -std::ostream &operator<<(std::ostream &os, const Cache &a); +std::ostream &operator<<(std::ostream &os, const Cache &c); #endif /* CACHE_H_INCLUDED */ diff --git a/inc/definitions.h b/inc/definitions.h index 98c4575..62ed0f6 100644 --- a/inc/definitions.h +++ b/inc/definitions.h @@ -16,7 +16,7 @@ * The number of bits to specify a memory line * calculated as: (/ (expt 2 15) 4) */ -#define MEM_SPEC 13 +#define MEM_SPEC 8 /** * The total number of words in memory */ diff --git a/inc/dram.h b/inc/dram.h index 20221b7..e8d3573 100644 --- a/inc/dram.h +++ b/inc/dram.h @@ -1,5 +1,7 @@ #ifndef DRAM_H #define DRAM_H +#include "definitions.h" +#include #include class Dram : public Storage @@ -16,9 +18,12 @@ class Dram : public Storage ~Dram(); Response write(Accessor accessor, signed int data, int address) override; - Response read(Accessor accessor, int address, std::array& data) override; + Response read( + Accessor accessor, + int address, + std::array &data) override; - private: + private: /** * Helper for `write`. */ @@ -26,8 +31,9 @@ class Dram : public Storage /** * Helper for `read`. */ - void do_read(std::array& data_line, int address); + void do_read(std::array &data_line, int address); }; -#endif /* DRAM_H_INCLUDED */ +std::ostream &operator<<(std::ostream &os, const Dram &d); +#endif /* DRAM_H_INCLUDED */ diff --git a/src/cli/cli.cc b/src/cli/cli.cc index 2126798..e968a42 100644 --- a/src/cli/cli.cc +++ b/src/cli/cli.cc @@ -5,6 +5,8 @@ #include "response.h" #include +static Logger *global_log = Logger::getInstance(); + Cli::Cli() { this->initialize(); @@ -83,10 +85,9 @@ void Cli::help() "specified address. Accessor must be one of: \"MEM\", \"FETCH\", " "\"L1CACHE\".\n" << " c - manually advances the clock\n" + << " f - advances the clock until one operation reports completion\n" << " r - side door function that resets the memory configuration and " "cycles\n" - << " u
- side door function that updates " - "the memory at the specified address with data provided\n" << " p - side door function that peeks " "the current status of the entire memory subsystem\n" << " h - Prints this help text\n" @@ -129,7 +130,8 @@ void Cli::peek(int level) if (c) { std::cout << *c; } else { - std::cout << "dram"; + std::cout << *dynamic_cast(curr); + ; } } @@ -173,7 +175,7 @@ void Cli::run() void Cli::initialize() { Logger *global_log = Logger::getInstance(); - global_log->log(INFO, "Resetting memory configuration.\n"); + global_log->log(INFO, "Resetting memory configuration."); if (this->cache == nullptr) delete this->cache; Dram *d = new Dram(MEM_SIZE, MEM_DELAY); diff --git a/src/main.cc b/src/main.cc index 8a24fdb..f2f416d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,6 +4,8 @@ #include #include +static Logger *global_log = Logger::getInstance(); + static std::string version_number = "v0.1"; static std::string banner = " _/_/_/ _/_/_/ _/_/_/ _/_/_/ \n" @@ -32,7 +34,6 @@ static void err() static void parseArguments(int argc, char **argv, bool &python) { - Logger *global_log = Logger::getInstance(); struct option long_options[] = { {"debug", no_argument, 0, 'd'}, {"no-python", no_argument, 0, 'p'}, @@ -64,7 +65,6 @@ static void parseArguments(int argc, char **argv, bool &python) int main(int argc, char **argv) { - Logger *global_log = Logger::getInstance(); print_version_number(); Cli cli; global_log->log(INFO, "Initializing..."); diff --git a/src/storage/cache.cc b/src/storage/cache.cc index d382c3d..6dd0b8a 100644 --- a/src/storage/cache.cc +++ b/src/storage/cache.cc @@ -100,6 +100,7 @@ std::ostream &operator<<(std::ostream &os, const Cache &c) c.view(0, L1_CACHE_SIZE); std::array, L1_CACHE_SIZE> meta = c.get_meta(); + cout << data.capacity(); os << " " << std::setfill(' ') << std::setw(L1_CACHE_SPEC + 2) << "INDEX" << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA" << " | " << std::setfill(' ') diff --git a/src/storage/dram.cc b/src/storage/dram.cc index 441f10b..5c6d719 100644 --- a/src/storage/dram.cc +++ b/src/storage/dram.cc @@ -1,7 +1,11 @@ #include "dram.h" #include "definitions.h" #include "response.h" +#include #include +#include +#include +#include Dram::Dram(int lines, int delay) { @@ -70,3 +74,28 @@ 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> data = + d.view(0, MEM_SIZE); + + cout << data.capacity(); + os << " " << std::setfill(' ') << std::setw(MEM_SPEC + 2) << "INDEX" + << " | " << std::setfill(' ') << std::setw((8 + 3) * 4 - 1) << "DATA" << '\n'; + for (int i = 0; i < MEM_SIZE; ++i) { + os << " 0b" << std::setw(MEM_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 << '\n'; + } + + std::cout.flags(default_flags); + std::cout.fill(default_fill); + return os; +} -- cgit v1.2.3