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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "id.h"
#include "cache.h"
#include "controller.h"
#include "dram.h"
#include "dum.h"
#include "instr.h"
#include "instrDTO.h"
#include <catch2/catch_test_macros.hpp>
class IDFixture
{
public:
IDFixture()
{
this->dr = new Dram(3);
this->c = new Cache(this->dr, 5, 0, 1);
this->dum = new DUM(nullptr);
this->d = new ID(dum);
this->ct = new Controller(this->d, this->c, true);
};
~IDFixture()
{
delete this->ct;
delete this->c;
};
InstrDTO *decode_bits(signed int raw)
{
InstrDTO *i = new InstrDTO();
i->slot_A = raw;
this->dum->set_curr_instr(i);
i = this->ct->advance(READY);
REQUIRE(i == nullptr);
i = this->ct->advance(READY);
REQUIRE(i != nullptr);
return i;
}
signed int encode_R_type(
signed int s3,
signed int s2,
signed int s1,
signed int opcode,
signed int type)
{
signed int t;
t = s3;
t = (t << REG_SIZE) + s2;
t = (t << REG_SIZE) + s1;
t = (t << R_OPCODE_SIZE) + opcode;
t = (t << TYPE_SIZE) + type;
return t;
}
signed int encode_I_type(
signed int s3,
signed int s2,
signed int s1,
signed int opcode,
signed int type)
{
signed int t;
t = s3;
t = (t << REG_SIZE) + s2;
t = (t << REG_SIZE) + s1;
t = (t << OPCODE_SIZE) + opcode;
t = (t << TYPE_SIZE) + type;
return t;
}
signed int encode_J_type(
signed int s2, signed int s1, signed int opcode, signed int type)
{
signed int t;
t = s2;
t = (t << REG_SIZE) + s1;
t = (t << OPCODE_SIZE) + opcode;
t = (t << TYPE_SIZE) + type;
return t;
}
Dram *dr;
Cache *c;
ID *d;
DUM *dum;
Controller *ct;
};
TEST_CASE_METHOD(IDFixture, "Parse invalid type", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_R_type(0b0, 0b1, 0b10, 0b11, 0b11);
i = this->decode_bits(t);
CHECK(i->mnemonic == NOP);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary r-type # one", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_R_type(0b101, 0b110, 0b111, 0b11, 0b0);
i = this->decode_bits(t);
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == 0x00000000);
CHECK(i->operands.integer.slot_three == 0x00000000);
CHECK(i->mnemonic == MUL);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary r-type # two", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_R_type(0b1000, 0b01000, 0b00100, 0b10, 0b0);
i = this->decode_bits(t);
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == 0x00000000);
CHECK(i->operands.integer.slot_three == 0x00000000);
CHECK(i->mnemonic == SUB);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary i-type # one", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_I_type(0xF, 0b101, 0b110, 0b0111, 0b1);
i = this->decode_bits(t);
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == 0x00000000);
CHECK(i->operands.integer.slot_three == 0xF);
CHECK(i->mnemonic == ANDI);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary i-type # two", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_I_type(0xCC, 0b10101, 0b00110, 0b11011, 0b1);
i = this->decode_bits(t);
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == 0x00000000);
CHECK(i->operands.integer.slot_three == 0xCC);
CHECK(i->mnemonic == STOREV);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary j-type # one", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_J_type(0x3456, 0b101, 0b0111, 0b10);
i = this->decode_bits(t);
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == 0x3456);
CHECK(i->mnemonic == BOF);
delete i;
}
TEST_CASE_METHOD(IDFixture, "Parse arbitrary j-type # two", "[id]")
{
signed int t;
InstrDTO *i;
t = this->encode_J_type(0xBBCCF, 0b00101, 0b0011, 0b10);
i = this->decode_bits(t);
t = 0xFFFBBCCF;
CHECK(i->operands.integer.slot_one == 0x00000000); // registers are empty
CHECK(i->operands.integer.slot_two == t);
CHECK(i->mnemonic == JAL);
delete i;
}
TEST_CASE_METHOD(IDFixture, "read does not conflict with read", "[id]")
{
signed int v;
Response r;
v = 0b1;
r = this->d->read_guard(v);
CHECK(v == 0b0);
REQUIRE(r == OK);
v = 0b1;
this->d->read_guard(v);
REQUIRE(v == 0b0);
}
|