summaryrefslogtreecommitdiff
path: root/src/cache.cc
blob: 7092bc5005aa736d801fcf192021763a6a2a2b34 (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
#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;
}

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

unsigned int
Cache::is_address_missing(unsigned int index, unsigned 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;
}

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

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