diff options
author | bd <bdunahu@operationnull.com> | 2025-04-14 23:40:25 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-04-14 23:40:25 -0400 |
commit | a7620015acc2401165b4587cbb6c9a118d944493 (patch) | |
tree | 5bbe698fe42a7d32578df6170e464508a78a8e50 | |
parent | 2140a23041d3920e001457f2c2acb0853094963d (diff) |
Add preprocess method to storage, removing nearly all duplication
-rw-r--r-- | inc/cache.h | 10 | ||||
-rw-r--r-- | inc/dram.h | 12 | ||||
-rw-r--r-- | inc/storage.h | 18 | ||||
-rw-r--r-- | src/cache.cc | 8 | ||||
-rw-r--r-- | src/dram.cc | 8 | ||||
-rw-r--r-- | src/storage.cc | 13 |
6 files changed, 35 insertions, 34 deletions
diff --git a/inc/cache.h b/inc/cache.h index 3177f59..7409c02 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -44,15 +44,7 @@ nn * Constructor. int read_word(void *, int, signed int &) override; private: - /** - * Helper for all access methods. - * Calls `request_handler` when `id` is allowed to complete its - * request cycle. - * @param the source making the request - * @param the address to write to - * @param the function to call when an access should be completed - */ - int process(void *id, int address, std::function<void(int index, int offset)> request_handler); + int process(void *id, int address, std::function<void(int index, int offset)> request_handler) override; /** * Helper for process. * Fetches `address` from a lower level of storage if it is not already @@ -36,17 +36,7 @@ class Dram : public Storage 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); + int process(void *id, int address, std::function<void(int line, int word)> request_handler) override; /** * Given `address`, returns the line and word it is in. * @param an address diff --git a/inc/storage.h b/inc/storage.h index 972e24a..1bf5805 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -3,6 +3,7 @@ #include "definitions.h" #include <algorithm> #include <array> +#include <functional> #include <map> #include <vector> @@ -48,6 +49,23 @@ class Storage protected: /** + * Helper for all access methods. + * Calls `request_handler` when `id` is allowed to complete its + * request cycle. + * May include extra checks depending on storage device. + * @param the source making the request + * @param the address to write to + * @param the function to call when an access should be completed + */ + virtual int + process(void *id, int address, std::function<void(int index, int offset)> request_handler) = 0; + /** + * Helper for process. Given `id`, returns 0 if the request should trivially be ignored. + * @param the source making the request + * @return 0 if the request should not be completed, 1 if it should be evaluated further. + */ + int preprocess(void *id); + /** * 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 diff --git a/src/cache.cc b/src/cache.cc index ee4b00c..68047ed 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -58,13 +58,7 @@ Cache::read_word(void *id, int address, signed int &data) int Cache::process(void *id, int address, std::function<void(int index, int offset)> request_handler) { - 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 (!preprocess(id)) return 0; if (is_address_missing(address)) diff --git a/src/dram.cc b/src/dram.cc index 5e7e57a..bbd18b7 100644 --- a/src/dram.cc +++ b/src/dram.cc @@ -54,13 +54,7 @@ Dram::load(std::vector<signed int> program) int Dram::process(void *id, int address, std::function<void(int line, int word)> request_handler) { - 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 (!preprocess(id)) return 0; if (!this->is_data_ready()) diff --git a/src/storage.cc b/src/storage.cc index 17c902d..ee2e7e7 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -1,6 +1,7 @@ #include "storage.h" #include "definitions.h" #include <algorithm> +#include <stdexcept> Storage::Storage(int delay) { @@ -21,6 +22,18 @@ Storage::view(int base, int lines) const } int +Storage::preprocess(void *id) +{ + if (id == nullptr) + throw std::invalid_argument("Accessor cannot be nullptr."); + + if (this->current_request == nullptr) + this->current_request = id; + + return this->current_request == id; +} + +int Storage::is_data_ready() { int r; |