diff options
author | bd <bdunahu@operationnull.com> | 2025-04-14 16:28:05 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-04-14 16:28:05 -0400 |
commit | ee433509972d9390a52f188e902eb74e55596822 (patch) | |
tree | a2bb8d9eb7220cbaf147c3af3fb837bec6736296 /inc/cache.h | |
parent | b91eb002d4e6b2dc0c51b03df57c5089659ac669 (diff) |
Allow multi-level cache by passing a size into the constructor
Diffstat (limited to 'inc/cache.h')
-rw-r--r-- | inc/cache.h | 56 |
1 files changed, 31 insertions, 25 deletions
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 <array> +#include <cmath> #include <functional> #include <ostream> +/** + * 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<signed int, LINE_SIZE>, int) override; - int - read_line(void *, int, std::array<signed int, LINE_SIZE> &) 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<std::array<int, 2>, L1_CACHE_LINES> - get_meta() const; + int write_word(void *, signed int, int) override; + int write_line(void *, std::array<signed int, LINE_SIZE>, int) override; + int read_line(void *, int, std::array<signed int, LINE_SIZE> &) 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<void(int index, int offset)> request_handler); + 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 @@ -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<std::array<int, 2>, L1_CACHE_LINES> meta; + std::vector<std::array<signed int, 2>> meta; }; #endif /* CACHE_H_INCLUDED */ |