summaryrefslogtreecommitdiff
path: root/src/cache.cc
blob: c8879b5a5aeef2474d50e5244a1fc1e3210c8ee1 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Memory subsystem for the RISC-V[ECTOR] mini-ISA
// Copyright (C) 2025 Siddarth Suresh
// Copyright (C) 2025 bdunahu

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include "cache.h"
#include "definitions.h"
#include <cstdlib>
#include <iostream>
#include <iterator>

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<std::array<signed int, 3>>(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()
{
	delete this->lower;
	delete this->data;
}

unsigned int
Cache::get_size() { return this->size; }

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;
	});
}

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)
{
	address = WRAP_ADDRESS(address);
	if (!preprocess(id) || priming_address(address) || !this->is_data_ready())
		return 0;

	int tag, index, offset;
	GET_FIELDS(address, &tag, &index, &offset);
	index = this->get_true_index(index);
	request_handler(index, offset);

	return 1;
}

int
Cache::priming_address(int address)
{
	int tag, index, offset;
	int r1, r2;
	std::array<signed int, LINE_SIZE> *evict;
	std::array<int, 3> *meta;

	r1 = 0;
	GET_FIELDS(address, &tag, &index, &offset);
	index = this->get_true_index(index);

	if (this->is_address_missing(index, tag)) {
		r1 = 1;

		index = this->get_replacement_index(index);
		meta = &this->meta.at(index);
		evict = &this->data->at(index);

		// handle eviction of dirty cache lines
		if (meta->at(1) >= 0) {
			r2 = this->lower->write_line(
				this, *evict, ((index << LINE_SPEC) + (meta->at(0) << (this->size + LINE_SPEC))));
			if (r2)
				meta->at(1) = -1;
		} else {
			r2 = this->lower->read_line(this, address, *evict);
			if (r2) {
				meta->at(0) = tag;
			}
		}
	}

	return r1;
}

int
Cache::is_address_missing(int index, int tag)
{
	int i;

	for (i = 0; i < (1 << this->ways); ++i)
		if (this->meta.at(index + i).at(0) == tag)
			return i;
	return -1;
}

int
Cache::get_true_index(int index)
{
	return index * (1 << this->ways);
}

int
Cache::get_replacement_index(int index)
{
	return index + (rand() % (1 << this->ways));
}