1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#pragma once
#include <array>
#include <cstdint>
#include "memory/dram.h"
#include "memory/line.h"
namespace memory {
template<unsigned int SETS_LOG2, unsigned int WAYS> struct inline_cache {
static constexpr std::uint64_t SETS = 1 << SETS_LOG2;
static constexpr std::uint64_t LINE_SET_OFFSET_MASK = SETS - 1;
std::uint64_t last_serial = 0;
struct tag {
std::uint64_t serial = 0;
std::uint64_t line_address;
infra::transaction transaction;
};
typedef std::array<tag, WAYS> set_tags;
typedef std::array<line, WAYS> set_data;
std::array<set_tags, SETS> tags;
std::array<set_data, SETS> data;
void handle_response(const dram::response &r) {
auto set_address = r.line_address & LINE_SET_OFFSET_MASK;
auto &stags = tags[set_address];
auto &sdata = data[set_address];
for (unsigned int i = 0; i < WAYS; ++i) {
auto &stag = stags[i];
if (stag.serial && stag.line_address == r.line_address) {
handle_response(r, stag);
return;
}
}
std::uint64_t min_serial = ~(std::uint64_t)0;
unsigned int victim;
for (unsigned int i = 0; i < WAYS; ++i) {
auto &stag = stags[i];
if (stag.serial < min_serial) {
min_serial = stag.serial;
victim = i;
}
}
handle_response(r, stags[victim], sdata[victim]);
}
void handle_response(const dram::response &r, tag &t) {
t.serial = ++last_serial;
t.transaction = r.transaction;
}
void handle_response(const dram::response &r, tag &t, line &d) {
handle_response(r, t);
t.line_address = r.line_address;
d = r.data;
}
void opportunistic_store(std::uint64_t address, unsigned int d) {
auto line_address = address >> LINE_BYTES_LOG2;
auto set_address = line_address & LINE_SET_OFFSET_MASK;
auto &stags = tags[set_address];
auto &sdata = data[set_address];
for (unsigned int i = 0; i < WAYS; ++i) {
auto &stag = stags[i];
if (stag.serial && stag.line_address == line_address) {
sdata[i][address & LINE_BYTE_OFFSET_MASK] = d;
return;
}
}
}
std::optional<tag> probe(std::uint64_t address) {
auto line_address = address >> LINE_BYTES_LOG2;
auto set_address = line_address & LINE_SET_OFFSET_MASK;
auto &stags = tags[set_address];
for (unsigned int i = 0; i < WAYS; ++i) {
auto &stag = stags[i];
if (stag.serial && stag.line_address == line_address)
return stag;
}
return {};
}
std::optional<tag> fetchline(line &dst, std::uint64_t address) {
auto line_address = address >> LINE_BYTES_LOG2;
auto set_address = line_address & LINE_SET_OFFSET_MASK;
auto &stags = tags[set_address];
auto &sdata = data[set_address];
for (unsigned int i = 0; i < WAYS; ++i) {
auto &stag = stags[i];
if (stag.serial && stag.line_address == line_address) {
stag.serial = ++last_serial;
dst = sdata[i];
return stag;
}
}
return {};
}
std::optional<tag> fetch(unsigned int &dst, std::uint64_t address) {
line data;
if (auto tag = fetchline(data, address); tag.has_value()) {
auto line_offset = address & LINE_BYTE_OFFSET_MASK;
dst = data[line_offset];
return tag;
}
return {};
}
};
}
|