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 --- tests/cache_2_1.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/cache_2_1.cc (limited to 'tests/cache_2_1.cc') diff --git a/tests/cache_2_1.cc b/tests/cache_2_1.cc new file mode 100644 index 0000000..101c6c3 --- /dev/null +++ b/tests/cache_2_1.cc @@ -0,0 +1,21 @@ +#include "c11.h" +#include "cache.h" +#include "dram.h" +#include "storage.h" +#include + +/** + * one way associative, two level + */ +class C21 : public C11 +{ + public: + C21() : C11() + { + Storage *s; + + s = new Dram(this->m_delay); + s = new Cache(s, 5, this->c_delay); + this->c = new Cache(s, 7, this->c_delay); + } +}; -- cgit v1.2.3 From 432e6f63ab4742ebf34ba7d031e269af810aa93f Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 14 Apr 2025 20:19:32 -0400 Subject: Add two-level cache test --- inc/definitions.h | 2 +- tests/cache_1_1.cc | 6 ++--- tests/cache_2_1.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 10 deletions(-) (limited to 'tests/cache_2_1.cc') diff --git a/inc/definitions.h b/inc/definitions.h index db43426..8513361 100644 --- a/inc/definitions.h +++ b/inc/definitions.h @@ -20,7 +20,7 @@ * The number of bits to specify a memory line * The total number of lines in memory */ -#define MEM_WORD_SPEC 10 +#define MEM_WORD_SPEC 16 #define MEM_LINE_SPEC static_cast(MEM_WORD_SPEC - LINE_SPEC) #define MEM_WORDS static_cast(pow(2, MEM_WORD_SPEC)) #define MEM_LINES static_cast(pow(2, MEM_LINE_SPEC)) diff --git a/tests/cache_1_1.cc b/tests/cache_1_1.cc index e3677a4..7d16f76 100644 --- a/tests/cache_1_1.cc +++ b/tests/cache_1_1.cc @@ -11,7 +11,7 @@ TEST_CASE_METHOD(C11, "store 0th element in DELAY cycles", "[dram]") w = 0x11223344; // delay + 1 due to internal logic, when mem - // finishes handle_miss still returns 'blocked' + // finishes is_address_missing still returns '1' this->wait_then_do(this->m_delay + this->c_delay + 1, [this, w]() { return this->c->write_word(this->mem, w, 0b0); }); @@ -32,7 +32,7 @@ TEST_CASE_METHOD(C11, "store 0th, 1st element in DELAY cycles, with conflict", " w = 0x11223344; // delay + 1 due to internal logic, when mem - // finishes handle_miss still returns 'blocked' + // finishes is_address_missing still returns '1' for (i = 0; i < this->m_delay + this->c_delay + 1; ++i) { r = c->write_word(this->mem, w, 0b0); CHECK(!r); @@ -72,7 +72,7 @@ TEST_CASE_METHOD( w = 0x11223344; // delay + 1 due to internal logic, when mem - // finishes handle_miss still returns 'blocked' + // finishes is_address_missing still returns '1' this->wait_then_do(this->m_delay + this->c_delay + 1, [this, w]() { return this->c->write_word(this->mem, w, 0b0); }); diff --git a/tests/cache_2_1.cc b/tests/cache_2_1.cc index 101c6c3..5b5a072 100644 --- a/tests/cache_2_1.cc +++ b/tests/cache_2_1.cc @@ -3,19 +3,85 @@ #include "dram.h" #include "storage.h" #include +#include /** - * one way associative, two level + * One way associative, two level + * Assuming that each address is 14 bits (16384 word address space): + * LEVEL1: OFFSET=2, INDEX=5(32), TAG=7 + * LEVEL2: OFFSET=2, INDEX=7(128), TAG=5 */ class C21 : public C11 { public: C21() : C11() { - Storage *s; - - s = new Dram(this->m_delay); - s = new Cache(s, 5, this->c_delay); - this->c = new Cache(s, 7, this->c_delay); + this->c2 = new Cache(new Dram(this->m_delay), 7, this->c_delay); + this->c = new Cache(this->c2, 5, this->c_delay); } + + Cache *c2; }; + +// TEST_CASE_METHOD(C21, "store 32th, 33rd element in DELAY cycles", + +TEST_CASE_METHOD(C21, "store 32th, 96th element in DELAY cycles, evict to level 2", "[2level_cache]") +{ + int r; + signed int w; + CHECK(expected == actual); + + w = 0x11223344; + // delay + 1 due to internal logic, when mem + // finishes handle_miss still returns 'blocked' + this->wait_then_do(this->m_delay + (this->c_delay * 2) + 2, [this, w]() { + return this->c->write_word(this->mem, w, 0b10000000); + }); + + r = this->c->write_word(this->mem, w, 0b10000000); + CHECK(r); + + // check level 2 + // note this is write-back == no write + actual = this->c2->view(32, 1)[0]; + REQUIRE(expected == actual); + + // check level 1 + expected.at(0) = w; + actual = this->c->view(0, 1)[0]; + REQUIRE(expected == actual); + + // wait = evict + this->wait_then_do(this->c_delay + 1, [this, w]() { + return this->c->write_word(this->mem, w, 0b110000000); + }); + + // check level 2 + actual = this->c2->view(32, 1)[0]; + REQUIRE(expected == actual); + + // read in line + this->wait_then_do(this->m_delay + this->c_delay + 1, [this, w]() { + return this->c->write_word(this->mem, w, 0b110000000); + }); + + expected.at(0) = 0; + // perform write + this->wait_then_do(this->c_delay + 1, [this, w]() { + return this->c->write_word(this->mem, w, 0b110000000); + }); + + r = this->c->write_word(this->mem, w, 0b110000000); + CHECK(r); + + // check level 2 + actual = this->c2->view(96, 1)[0]; + REQUIRE(expected == actual); + expected.at(0) = w; + actual = this->c2->view(32, 1)[0]; + REQUIRE(expected == actual); + + // check level 1 + actual = this->c->view(0, 1)[0]; + REQUIRE(expected == actual); +} -- 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 'tests/cache_2_1.cc') 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