From 661ba30d5233e1b95ac312e88cd51380e664933b Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 14 Apr 2025 23:10:18 -0400 Subject: Initial refactor of is_access_cleared --- src/storage.cc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/storage.cc') 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 -Storage::Storage(int delay) { +Storage::Storage(int delay) +{ this->data = new std::vector>; 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; +} -- cgit v1.2.3 From 2140a23041d3920e001457f2c2acb0853094963d Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 14 Apr 2025 23:27:54 -0400 Subject: Simplify cache/dram process functions --- inc/storage.h | 2 +- src/cache.cc | 31 ++++++++++++++++--------------- src/dram.cc | 21 ++++++++++++--------- src/storage.cc | 2 +- 4 files changed, 30 insertions(+), 26 deletions(-) (limited to 'src/storage.cc') diff --git a/inc/storage.h b/inc/storage.h index 1b71794..972e24a 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -53,7 +53,7 @@ class Storage * @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(); + int is_data_ready(); /** * The data currently stored in this level of storage. */ diff --git a/src/cache.cc b/src/cache.cc index 44a770d..ee4b00c 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -58,25 +58,26 @@ Cache::read_word(void *id, int address, signed int &data) int Cache::process(void *id, int address, std::function request_handler) { - int r; - - 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)) - r = 0; - else - r = this->is_access_cleared(); - } - if (r) { - int tag, index, offset; - GET_FIELDS(address, &tag, &index, &offset); - request_handler(index, offset); - } - return r; + + if (this->current_request != id) + return 0; + + if (is_address_missing(address)) + return 0; + + if (!this->is_data_ready()) + return 0; + + int tag, index, offset; + GET_FIELDS(address, &tag, &index, &offset); + request_handler(index, offset); + + return 1; } int diff --git a/src/dram.cc b/src/dram.cc index 264cadb..5e7e57a 100644 --- a/src/dram.cc +++ b/src/dram.cc @@ -54,19 +54,22 @@ Dram::load(std::vector program) int Dram::process(void *id, int address, std::function request_handler) { - int r; - if (id == nullptr) throw std::invalid_argument("Accessor cannot be nullptr."); + if (this->current_request == nullptr) this->current_request = id; - 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 r; + + if (this->current_request != id) + return 0; + + if (!this->is_data_ready()) + return 0; + + int line, word; + get_memory_index(address, line, word); + request_handler(line, word); + return 1; } void diff --git a/src/storage.cc b/src/storage.cc index b6be578..17c902d 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -21,7 +21,7 @@ Storage::view(int base, int lines) const } int -Storage::is_access_cleared() +Storage::is_data_ready() { int r; -- cgit v1.2.3 From a7620015acc2401165b4587cbb6c9a118d944493 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 14 Apr 2025 23:40:25 -0400 Subject: Add preprocess method to storage, removing nearly all duplication --- inc/cache.h | 10 +--------- inc/dram.h | 12 +----------- inc/storage.h | 18 ++++++++++++++++++ src/cache.cc | 8 +------- src/dram.cc | 8 +------- src/storage.cc | 13 +++++++++++++ 6 files changed, 35 insertions(+), 34 deletions(-) (limited to 'src/storage.cc') 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 request_handler); + int process(void *id, int address, std::function request_handler) override; /** * Helper for process. * Fetches `address` from a lower level of storage if it is not already diff --git a/inc/dram.h b/inc/dram.h index fbac620..2a4e358 100644 --- a/inc/dram.h +++ b/inc/dram.h @@ -36,17 +36,7 @@ class Dram : public Storage void load(std::vector 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 request_handler); + int process(void *id, int address, std::function 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 #include +#include #include #include @@ -47,6 +48,23 @@ class Storage std::vector> view(int base, int lines) const; 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 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. 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 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 program) int Dram::process(void *id, int address, std::function 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 +#include Storage::Storage(int delay) { @@ -20,6 +21,18 @@ Storage::view(int base, int lines) const return ret; } +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() { -- cgit v1.2.3