From ee433509972d9390a52f188e902eb74e55596822 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 14 Apr 2025 16:28:05 -0400 Subject: Allow multi-level cache by passing a size into the constructor --- inc/cache.h | 56 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'inc/cache.h') diff --git a/inc/cache.h b/inc/cache.h index 0f15536..325d46f 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -3,40 +3,45 @@ #include "definitions.h" #include "storage.h" #include +#include #include #include +/** + * Parse an address into a tag, index into the cache table, and a line + * offset. + * @param the address to be parsed + * @param the resulting tag + * @param the resulting index + * @param the resulting offset + */ +// clang-format off +#define GET_FIELDS(a, t, i, o) \ + *(t) = GET_MID_BITS(a, this->size + LINE_SPEC, MEM_WORD_SPEC); \ + *(i) = GET_MID_BITS(a, LINE_SPEC, this->size + LINE_SPEC); \ + *(o) = GET_LS_BITS(a, LINE_SPEC) +// clang-format on + class Cache : public Storage { public: /** - * Constructor. +nn * Constructor. * @param The number of `lines` contained in memory. The total number of * words is this number multiplied by LINE_SIZE. * @param The next lowest level in storage. Methods from this object are * called in case of a cache miss. + * @param The number of bits required to specify a line in this level of cache. * @param The number of clock cycles each access takes. * @return A new cache object. */ - Cache(Storage *lower, int delay); + Cache(Storage *lower, unsigned int size, int delay); ~Cache(); - int - write_word(void *, signed int, int) override; - int - write_line(void *, std::array, int) override; - int - read_line(void *, int, std::array &) override; - int - read_word(void *, int, signed int &) override; - - /** - * Getter for the meta attribute. - * TODO this doesn't seem like good object-oriented practice. - * @return this->meta - */ - std::array, L1_CACHE_LINES> - get_meta() const; + int write_word(void *, signed int, int) override; + int write_line(void *, std::array, int) override; + int read_line(void *, int, std::array &) override; + int read_word(void *, int, signed int &) override; private: /** @@ -47,8 +52,7 @@ class Cache : public Storage * @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); /** * Returns OK if `id` is allowed to complete its request this cycle. * Handles cache misses, wait times, and setting the current id this @@ -56,8 +60,7 @@ class Cache : public 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(void *id, int address); + int is_access_cleared(void *id, int address); /** * Helper for is_access_cleared. * Fetches `address` from a lower level of storage if it is not already @@ -65,15 +68,18 @@ class Cache : public Storage * @param the address that must be present in cache. * @param 0 if the address is currently in cache, 1 if it is being fetched. */ - int - is_address_missing(int address); + int is_address_missing(int address); + /** + * The number of bits required to specify a line in this level of cache. + */ + unsigned int size; /** * An array of metadata about elements in `data`. * If the first value of an element is negative, the corresponding * element in `data` is invalid. If the most second value of an element * is nonzero, the corresponding element in `data` is dirty. */ - std::array, L1_CACHE_LINES> meta; + std::vector> meta; }; #endif /* CACHE_H_INCLUDED */ -- cgit v1.2.3 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 --- inc/cache.h | 10 +--------- inc/dram.h | 28 +++++++--------------------- inc/storage.h | 22 ++++++++++++---------- src/cache.cc | 31 ++++++++++--------------------- src/dram.cc | 27 ++++++--------------------- src/storage.cc | 20 +++++++++++++++++++- 6 files changed, 55 insertions(+), 83 deletions(-) (limited to 'inc/cache.h') 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 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. diff --git a/inc/dram.h b/inc/dram.h index fc46b47..fbac620 100644 --- a/inc/dram.h +++ b/inc/dram.h @@ -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, int) override; - int - read_word(void *, int, signed int &) override; - int - read_line(void *, int, std::array &) override; + int write_word(void *, signed int, int) override; + int write_line(void *, std::array, int) override; + int read_word(void *, int, signed int &) override; + int read_line(void *, int, std::array &) override; /** * TODO This will accept a file at a later date. */ - void - load(std::vector program); + 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); - /** - * 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 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 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 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 &data) = 0; - virtual int - read_word(void *id, int address, signed int &data) = 0; + virtual int read_line(void *id, int address, std::array &data) = 0; + virtual int read_word(void *id, int address, signed int &data) = 0; /** * Sidedoor view of `lines` of memory starting at `base`. @@ -48,10 +44,16 @@ 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) const; + std::vector> 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. */ 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 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 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 -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 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 'inc/cache.h') 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 From 71f69927931e007d0bac13b9268b6a697b45c70a Mon Sep 17 00:00:00 2001 From: bd Date: Wed, 16 Apr 2025 11:23:46 -0400 Subject: Update GET_FIELDS to account for number of ways, constructors --- inc/cache.h | 18 +++++++++++++----- src/cache.cc | 29 ++++++++++++++++++----------- src/dram.cc | 5 +---- tests/c11.h | 2 +- tests/cache_2_1.cc | 4 ++-- 5 files changed, 35 insertions(+), 23 deletions(-) (limited to 'inc/cache.h') diff --git a/inc/cache.h b/inc/cache.h index 7409c02..6f06466 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -17,8 +17,8 @@ */ // clang-format off #define GET_FIELDS(a, t, i, o) \ - *(t) = GET_MID_BITS(a, this->size + LINE_SPEC, MEM_WORD_SPEC); \ - *(i) = GET_MID_BITS(a, LINE_SPEC, this->size + LINE_SPEC); \ + *(t) = GET_MID_BITS(a, this->size + LINE_SPEC - this->ways, MEM_WORD_SPEC); \ + *(i) = GET_MID_BITS(a, LINE_SPEC, this->size + LINE_SPEC - this->ways); \ *(o) = GET_LS_BITS(a, LINE_SPEC) // clang-format on @@ -32,10 +32,12 @@ nn * Constructor. * @param The next lowest level in storage. Methods from this object are * called in case of a cache miss. * @param The number of bits required to specify a line in this level of cache. + * @param The number of ways this line of cache uses, or the number of data addresses stored for + * certain address index. * @param The number of clock cycles each access takes. * @return A new cache object. */ - Cache(Storage *lower, unsigned int size, int delay); + Cache(Storage *lower, unsigned int size, unsigned int ways, int delay); ~Cache(); int write_word(void *, signed int, int) override; @@ -44,7 +46,8 @@ nn * Constructor. int read_word(void *, int, signed int &) override; private: - int process(void *id, int address, std::function request_handler) override; + 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 @@ -57,13 +60,18 @@ nn * Constructor. * The number of bits required to specify a line in this level of cache. */ unsigned int size; + /** + * The number of bits required to specify a way, or the number of data addresses stored for + * certain address index. + */ + unsigned int ways; /** * An array of metadata about elements in `data`. * If the first value of an element is negative, the corresponding * element in `data` is invalid. If the most second value of an element * is nonzero, the corresponding element in `data` is dirty. */ - std::vector> meta; + std::vector> meta; }; #endif /* CACHE_H_INCLUDED */ diff --git a/src/cache.cc b/src/cache.cc index 68047ed..08545fd 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -3,15 +3,18 @@ #include #include -Cache::Cache(Storage *lower, unsigned int size, int delay) : Storage(delay) +Cache::Cache(Storage *lower, unsigned int size, unsigned int ways, int delay) : Storage(delay) { int true_size; true_size = 1 << size; this->data->resize(true_size); - this->meta = std::vector>(true_size, {-1, -1}); - this->size = size; + this->meta = std::vector>(true_size, {-1, -1, -1}); this->lower = lower; + + this->size = size; + // store the number of bits which are moved into the tag field + this->ways = ways; } Cache::~Cache() @@ -58,13 +61,7 @@ Cache::read_word(void *id, int address, signed int &data) int Cache::process(void *id, int address, std::function request_handler) { - if (!preprocess(id)) - return 0; - - if (is_address_missing(address)) - return 0; - - if (!this->is_data_ready()) + if (!preprocess(id) || is_address_missing(address) || !this->is_data_ready()) return 0; int tag, index, offset; @@ -79,7 +76,7 @@ Cache::is_address_missing(int expected) { int r, q, tag, index, offset; std::array *actual; - std::array *meta; + std::array *meta; GET_FIELDS(expected, &tag, &index, &offset); r = 0; @@ -104,3 +101,13 @@ Cache::is_address_missing(int expected) return r; } + +// unsigned int +// Cache::get_true_index(unsigned int index) +// { +// } + +// unsigned int +// Cache::get_replacement_index(unsigned int index) +// { +// } diff --git a/src/dram.cc b/src/dram.cc index bbd18b7..53db16b 100644 --- a/src/dram.cc +++ b/src/dram.cc @@ -54,10 +54,7 @@ Dram::load(std::vector program) int Dram::process(void *id, int address, std::function request_handler) { - if (!preprocess(id)) - return 0; - - if (!this->is_data_ready()) + if (!preprocess(id) || !this->is_data_ready()) return 0; int line, word; diff --git a/tests/c11.h b/tests/c11.h index 6d63c77..e5599db 100644 --- a/tests/c11.h +++ b/tests/c11.h @@ -18,7 +18,7 @@ class C11 this->c_delay = 2; this->mem = new int(); this->fetch = new int(); - this->c = new Cache(new Dram(this->m_delay), 5, this->c_delay); + this->c = new Cache(new Dram(this->m_delay), 5, 0, this->c_delay); this->expected = {0, 0, 0, 0}; this->actual = this->c->view(0, 1)[0]; } diff --git a/tests/cache_2_1.cc b/tests/cache_2_1.cc index 5b5a072..cb48d2a 100644 --- a/tests/cache_2_1.cc +++ b/tests/cache_2_1.cc @@ -16,8 +16,8 @@ class C21 : public C11 public: C21() : C11() { - this->c2 = new Cache(new Dram(this->m_delay), 7, this->c_delay); - this->c = new Cache(this->c2, 5, this->c_delay); + this->c2 = new Cache(new Dram(this->m_delay), 7, 0, this->c_delay); + this->c = new Cache(this->c2, 5, 0, this->c_delay); } Cache *c2; -- cgit v1.2.3