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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include "cache.h"
#include "definitions.h"
#include <bits/stdc++.h>
#include <iterator>
Cache::Cache(Storage *lower, int delay) : Storage(delay)
{
this->data->resize(L1_CACHE_LINES);
this->lower = lower;
this->meta.fill({-1, -1});
}
Cache::~Cache()
{
delete this->lower;
delete this->data;
}
int
Cache::write_word(void *id, signed int data, int address)
{
return process(id, address, [&](int index, int offset) {
this->data->at(index).at(offset) = data;
this->meta[index].at(1) = 1;
});
}
int
Cache::write_line(void *id, std::array<signed int, LINE_SIZE> data_line, int address)
{
return process(id, address, [&](int index, int offset) {
(void)offset;
this->data->at(index) = data_line;
this->meta[index].at(1) = 1;
});
}
// TODO: tests for multi level cache
int
Cache::read_line(void *id, int address, std::array<signed int, LINE_SIZE> &data_line)
{
return process(id, address, [&](int index, int offset) {
(void)offset;
data_line = this->data->at(index);
});
}
int
Cache::read_word(void *id, int address, signed int &data)
{
return process(
id, address, [&](int index, int offset) { data = this->data->at(index).at(offset); });
}
int
Cache::process(void *id, int address, std::function<void(int index, int offset)> 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. */
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;
}
}
return 0;
}
int
Cache::is_address_missing(int expected)
{
int r, q, tag, index, offset;
std::array<signed int, LINE_SIZE> *actual;
std::array<int, 2> *meta;
GET_FIELDS(expected, &tag, &index, &offset);
r = 0;
meta = &this->meta.at(index);
actual = &this->data->at(index);
if (meta->at(0) != tag) {
r = 1;
if (meta->at(1) >= 0) {
q = this->lower->write_line(
this, *actual,
((index << LINE_SPEC) + (meta->at(0) << (L1_CACHE_LINE_SPEC + LINE_SPEC))));
if (q) {
meta->at(1) = -1;
}
} else {
q = this->lower->read_line(this, expected, *actual);
if (q) {
meta->at(0) = tag;
}
}
}
return r;
}
std::array<std::array<int, 2>, L1_CACHE_LINES>
Cache::get_meta() const
{
std::array<std::array<int, 2>, L1_CACHE_LINES> ret;
std::copy(std::begin(this->meta), std::end(this->meta), std::begin(ret));
return ret;
}
|