summaryrefslogtreecommitdiff
path: root/memory/cache.h
diff options
context:
space:
mode:
authorJulian Blake Kongslie2022-11-11 16:28:55 -0800
committerJulian Blake Kongslie2022-11-11 16:28:55 -0800
commitdb82579d3c023c441c895d26d32de3fa039eafa4 (patch)
treee42ee62e50a5910dac54f6b57489876303b3d516 /memory/cache.h
parentSlightly more ergonomic support for events without data (diff)
downloadnanosim-db82579d3c023c441c895d26d32de3fa039eafa4.tar.xz
Starting to write a real memory subsystem for biggolf.
Diffstat (limited to 'memory/cache.h')
-rw-r--r--memory/cache.h96
1 files changed, 96 insertions, 0 deletions
diff --git a/memory/cache.h b/memory/cache.h
new file mode 100644
index 0000000..8554910
--- /dev/null
+++ b/memory/cache.h
@@ -0,0 +1,96 @@
1#pragma once
2
3#include <array>
4#include <cstdint>
5
6#include "memory/dram.h"
7#include "memory/line.h"
8
9namespace memory {
10 template<unsigned int SETS_LOG2, unsigned int WAYS> struct inline_cache {
11 static constexpr std::uint64_t SETS = 1 << SETS_LOG2;
12 static constexpr std::uint64_t LINE_SET_OFFSET_MASK = SETS - 1;
13
14 std::uint64_t last_serial = 0;
15
16 struct tag {
17 std::uint64_t serial = 0;
18 std::uint64_t line_address;
19 infra::transaction transaction;
20 };
21
22 typedef std::array<tag, WAYS> set_tags;
23 typedef std::array<line, WAYS> set_data;
24
25 std::array<set_tags, SETS> tags;
26 std::array<set_data, SETS> data;
27
28 void handle_response(const dram::response &r) {
29 auto set_address = r.line_address & LINE_SET_OFFSET_MASK;
30 auto &stags = tags[set_address];
31 auto &sdata = data[set_address];
32 for (unsigned int i = 0; i < WAYS; ++i) {
33 auto &stag = stags[i];
34 if (stag.serial && stag.line_address == r.line_address) {
35 handle_response(r, stag, sdata[i]);
36 return;
37 }
38 }
39 std::uint64_t min_serial = ~(std::uint64_t)0;
40 unsigned int victim;
41 for (unsigned int i = 0; i < WAYS; ++i) {
42 auto &stag = stags[i];
43 if (stag.serial < min_serial) {
44 min_serial = stag.serial;
45 victim = i;
46 }
47 }
48 handle_response(r, stags[victim], sdata[victim]);
49 }
50
51 void handle_response(const dram::response &r, tag &t, line &d) {
52 t.serial = ++last_serial;
53 t.line_address = r.line_address;
54 t.transaction = r.transaction;
55 d = r.data;
56 }
57
58 std::optional<tag> probe(std::uint64_t address) {
59 auto line_address = address >> LINE_BYTES_LOG2;
60 auto set_address = line_address & LINE_SET_OFFSET_MASK;
61 auto &stags = tags[set_address];
62 for (unsigned int i = 0; i < WAYS; ++i) {
63 auto &stag = stags[i];
64 if (stag.serial && stag.line_address == line_address)
65 return stag;
66 }
67 return {};
68 }
69
70 std::optional<tag> fetchline(line &dst, std::uint64_t address) {
71 auto line_address = address >> LINE_BYTES_LOG2;
72 auto set_address = line_address & LINE_SET_OFFSET_MASK;
73 auto &stags = tags[set_address];
74 auto &sdata = data[set_address];
75 for (unsigned int i = 0; i < WAYS; ++i) {
76 auto &stag = stags[i];
77 if (stag.serial && stag.line_address == line_address) {
78 stag.serial = ++last_serial;
79 dst = sdata[i];
80 return stag;
81 }
82 }
83 return {};
84 }
85
86 std::optional<tag> fetch(unsigned int &dst, std::uint64_t address) {
87 line data;
88 if (auto tag = fetchline(data, address); tag.has_value()) {
89 auto line_offset = address & LINE_BYTE_OFFSET_MASK;
90 dst = data[line_offset];
91 return tag;
92 }
93 return {};
94 }
95 };
96}