summaryrefslogtreecommitdiff
path: root/src/storage/dram.cc
blob: 23dedc00d13bbefd78bbdedb91779dc71cb572bf (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
#include "dram.h"
#include "definitions.h"
#include "response.h"
#include <algorithm>

Dram::Dram(int lines, int delay)
{
	this->data = new std::vector<std::array<signed int, LINE_SIZE>>;
	this->data->resize(lines);
	this->delay = delay;
	this->wait_time = this->delay;
	this->lower = nullptr;
	this->requester = IDLE;
}

Dram::~Dram() { delete this->data; }

void Dram::do_write(signed data, int address)
{
	int line = address / LINE_SIZE;
	int word = address % LINE_SIZE;

	this->data->at(line).at(word) = data;
}

Response Dram::write(Accessor accessor, signed int data, int address)
{
	Response r = WAIT;

	if (accessor == SIDE) {
		this->do_write(data, address);
		r = OK;
	} else {
		/* Do this first--then process the first cycle immediately. */
		if (this->requester == IDLE)
			this->requester = accessor;

		if (this->requester == accessor) {
			if (this->wait_time == 0) {
				this->do_write(data, address);
				r = OK;
			}
		}
	}

	return r;
}

void Dram::do_read(std::array<signed int, LINE_SIZE> &data_line, int address)
{
	int line = address / LINE_SIZE;
	data_line = this->data->at(line);
}

Response Dram::read(
	Accessor accessor, int address, std::array<signed int, LINE_SIZE> &data)
{
	Response r = WAIT;
	if (this->requester == IDLE)
		this->requester = accessor;
	if (this->requester == accessor) {
		if (this->wait_time == 0) {
			this->do_read(data, address);
			r = OK;
		}
	}
	return r;
}