summaryrefslogtreecommitdiff
path: root/tests/if.cc
blob: 185a52accde770d1a7f8fcc412b9ade036d3d8c3 (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
#include "if.h"
#include "cache.h"
#include "controller.h"
#include "dram.h"
#include "instrDTO.h"
#include <catch2/catch_test_macros.hpp>

class IFPipeFixture
{
  public:
	IFPipeFixture()
	{
		Dram *d;

		d = new Dram(this->m_delay);
		// 0xC00 is a nop
		p = {0xC000, 0xC001, 0xC002, 0xC003};
		d->load(p);

		this->c = new Cache(d, this->c_delay);
		this->f = new IF(nullptr);
		this->ct = new Controller(this->f, this->c, true);
	}
	~IFPipeFixture()
	{
		delete this->ct;
		delete this->c;
	};

	/**
	 * Fetch a clean line not in cache.
	 */
	InstrDTO *fetch_through()
	{
		InstrDTO *r;
		int i;

		for (i = 0; i < this->m_delay + 1; ++i) {
			r = this->ct->advance(OK);
			// check response
			CHECK(r == nullptr);
		}
		return this->fetch_cache();
	}

	/**
	 * Fetch a line in cache.
	 */
	InstrDTO *fetch_cache()
	{
		InstrDTO *r;
		int i;

		for (i = 0; i < this->c_delay; ++i) {
			r = this->ct->advance(OK);
			// check response
			CHECK(r == nullptr);
		}
		r = this->ct->advance(OK);
		// check response
		CHECK(r != nullptr);
		return r;
	}

	int m_delay = 3;
	int c_delay = 1;
	std::vector<signed int> p;
	Cache *c;
	IF *f;
	Controller *ct;
};

TEST_CASE_METHOD(IFPipeFixture, "fetch returns single instuction", "[if_pipe]")
{
	InstrDTO *i;
	int expected_cycles;

	expected_cycles = this->m_delay + this->c_delay + 2;
	i = this->fetch_through();
	CHECK(i->get_time_of(FETCH) == expected_cycles);
	REQUIRE(i->get_instr_bits() == this->p[0]);

	delete i;
}

TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]")
{
	InstrDTO *i;
	int expected_cycles;

	expected_cycles = this->m_delay + this->c_delay + 2;
	i = this->fetch_through();

	CHECK(i->get_time_of(FETCH) == expected_cycles);
	REQUIRE(i->get_instr_bits() == this->p[0]);
	delete i;

	expected_cycles += this->c_delay + 1;
	i = this->fetch_cache();

	CHECK(i->get_time_of(FETCH) == expected_cycles);
	REQUIRE(i->get_instr_bits() == this->p[1]);
	delete i;
}

TEST_CASE_METHOD(IFPipeFixture, "fetch waits with old instruction",
"[if_pipe]")
{
	InstrDTO *i;
	int j, expected_cycles, fetch_cycles;

	fetch_cycles = this->m_delay + this->c_delay + 2;
	expected_cycles = this->m_delay + (this->c_delay * 2) + 1;

	for (j = 0; j < this->m_delay + 1; ++j) {
		i = this->ct->advance(BLOCKED);
		// check response
		CHECK(i == nullptr);
	}
	for (j = 0; j < this->c_delay; ++j) {
		i = this->ct->advance(BLOCKED);
		// check response
		CHECK(i == nullptr);
	}
	for (j = 0; j < expected_cycles - fetch_cycles; ++j) {
		i = this->ct->advance(BLOCKED);
		// check response
		CHECK(i != nullptr);
	}

	i = this->ct->advance(OK);
	CHECK(i->get_time_of(FETCH) == expected_cycles);
	REQUIRE(i->get_instr_bits() == this->p[0]);

	delete i;
}