diff options
author | bd <bdunahu@operationnull.com> | 2025-04-14 23:10:18 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-04-14 23:10:18 -0400 |
commit | 661ba30d5233e1b95ac312e88cd51380e664933b (patch) | |
tree | bc9bba3e3e87025eebf86e6b5013485088cf0ccf | |
parent | 432e6f63ab4742ebf34ba7d031e269af810aa93f (diff) |
Initial refactor of is_access_cleared
-rw-r--r-- | inc/cache.h | 10 | ||||
-rw-r--r-- | inc/dram.h | 28 | ||||
-rw-r--r-- | inc/storage.h | 22 | ||||
-rw-r--r-- | src/cache.cc | 31 | ||||
-rw-r--r-- | src/dram.cc | 27 | ||||
-rw-r--r-- | src/storage.cc | 20 |
6 files changed, 55 insertions, 83 deletions
diff --git a/inc/cache.h b/inc/cache.h index 325d46f..3177f59 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -54,15 +54,7 @@ nn * Constructor. */ int process(void *id, int address, std::function<void(int index, int offset)> request_handler); /** - * Returns OK if `id` is allowed to complete its request this cycle. - * Handles cache misses, wait times, and setting the current id this - * storage is serving. - * @param the id asking for a resource - * @return 1 if the access can be carried out this function call, 0 otherwise. - */ - int is_access_cleared(void *id, int address); - /** - * Helper for is_access_cleared. + * Helper for process. * Fetches `address` from a lower level of storage if it is not already * present. The victim line is chosen/written back. * @param the address that must be present in cache. @@ -25,42 +25,28 @@ class Dram : public Storage Dram(int delay); ~Dram(); - int - write_word(void *, signed int, int) override; - int - write_line(void *, std::array<signed int, LINE_SIZE>, int) override; - int - read_word(void *, int, signed int &) override; - int - read_line(void *, int, std::array<signed int, LINE_SIZE> &) override; + int write_word(void *, signed int, int) override; + int write_line(void *, std::array<signed int, LINE_SIZE>, int) override; + int read_word(void *, int, signed int &) override; + int read_line(void *, int, std::array<signed int, LINE_SIZE> &) override; /** * TODO This will accept a file at a later date. */ - void - load(std::vector<signed int> program); + void load(std::vector<signed int> program); private: /** * Helper for all access methods. * Calls `request_handler` when `id` is allowed to complete its * request cycle. + * Handles wait times and setting the current id this storage is serving. * @param the source making the request * @param the address to write to * @param the function to call when an access should be completed * @return 1 if the access completed successfully, 0 otherwise */ - int - process(void *id, int address, std::function<void(int line, int word)> request_handler); - /** - * Returns OK if `id` is allowed to complete its request this cycle. - * Handles wait times, side door, and setting the current id this - * storage is serving. - * @param the source making the request - * @return 1 if the access can be completed this function call, 0 otherwise - */ - int - is_access_cleared(void *id); + int process(void *id, int address, std::function<void(int line, int word)> request_handler); /** * Given `address`, returns the line and word it is in. * @param an address diff --git a/inc/storage.h b/inc/storage.h index 81194da..1b71794 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -24,10 +24,8 @@ class Storage * @param the address to write to. * @return 1 if the request was completed, 0 otherwise. */ - virtual int - write_word(void *id, signed int data, int address) = 0; - virtual int - write_line(void *id, std::array<signed int, LINE_SIZE> data_line, int address) = 0; + virtual int write_word(void *id, signed int data, int address) = 0; + virtual int write_line(void *id, std::array<signed int, LINE_SIZE> data_line, int address) = 0; /** * Get the data line at `address`. @@ -36,10 +34,8 @@ class Storage * @param the data being returned * @return 1 if the request was completed, 0 otherwise */ - virtual int - read_line(void *id, int address, std::array<signed int, LINE_SIZE> &data) = 0; - virtual int - read_word(void *id, int address, signed int &data) = 0; + virtual int read_line(void *id, int address, std::array<signed int, LINE_SIZE> &data) = 0; + virtual int read_word(void *id, int address, signed int &data) = 0; /** * Sidedoor view of `lines` of memory starting at `base`. @@ -48,11 +44,17 @@ 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) const; + std::vector<std::array<signed int, LINE_SIZE>> view(int base, int lines) const; protected: /** + * Returns OK if `id` should complete its request this cycle. In the case it can, automatically + * clears the current requester. + * @param the id asking for a resource + * @return 1 if the access can be carried out this function call, 0 otherwise. + */ + int is_access_cleared(); + /** * The data currently stored in this level of storage. */ std::vector<std::array<signed int, LINE_SIZE>> *data; diff --git a/src/cache.cc b/src/cache.cc index 307d6d0..44a770d 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -59,35 +59,24 @@ int Cache::process(void *id, int address, std::function<void(int index, int offset)> request_handler) { int r; - r = this->is_access_cleared(id, address); - if (r) { - int tag, index, offset; - GET_FIELDS(address, &tag, &index, &offset); - request_handler(index, offset); - } - return r; -} -int -Cache::is_access_cleared(void *id, int address) -{ - /* Do this first--then process the first cycle immediately. */ + r = 0; if (id == nullptr) throw std::invalid_argument("Accessor cannot be nullptr."); if (this->current_request == nullptr) this->current_request = id; if (this->current_request == id) { if (is_address_missing(address)) - return 0; - else if (this->wait_time == 0) { - this->current_request = nullptr; - this->wait_time = delay; - return 1; - } else { - --this->wait_time; - } + r = 0; + else + r = this->is_access_cleared(); } - return 0; + if (r) { + int tag, index, offset; + GET_FIELDS(address, &tag, &index, &offset); + request_handler(index, offset); + } + return r; } int diff --git a/src/dram.cc b/src/dram.cc index 18c1a3e..264cadb 100644 --- a/src/dram.cc +++ b/src/dram.cc @@ -55,33 +55,18 @@ int Dram::process(void *id, int address, std::function<void(int line, int word)> request_handler) { int r; - r = this->is_access_cleared(id); - if (r) { - int line, word; - get_memory_index(address, line, word); - request_handler(line, word); - } - return r; -} -int -Dram::is_access_cleared(void *id) -{ - /* Do this first--then process the first cycle immediately. */ if (id == nullptr) throw std::invalid_argument("Accessor cannot be nullptr."); if (this->current_request == nullptr) this->current_request = id; - if (this->current_request == id) { - if (this->wait_time == 0) { - this->current_request = nullptr; - this->wait_time = delay; - return 1; - } else { - --this->wait_time; - } + r = (this->current_request == id) ? this->is_access_cleared() : 0; + if (r) { + int line, word; + get_memory_index(address, line, word); + request_handler(line, word); } - return 0; + return r; } void diff --git a/src/storage.cc b/src/storage.cc index 4ad916b..b6be578 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -2,7 +2,8 @@ #include "definitions.h" #include <algorithm> -Storage::Storage(int delay) { +Storage::Storage(int delay) +{ this->data = new std::vector<std::array<signed int, LINE_SIZE>>; this->delay = delay; this->lower = nullptr; @@ -18,3 +19,20 @@ Storage::view(int base, int lines) const std::copy(this->data->begin() + base, this->data->begin() + base + lines, ret.begin()); return ret; } + +int +Storage::is_access_cleared() +{ + int r; + + r = 0; + if (this->wait_time == 0) { + this->current_request = nullptr; + this->wait_time = delay; + r = 1; + } else { + --this->wait_time; + } + + return r; +} |