-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestbench.cpp
More file actions
254 lines (241 loc) · 12.2 KB
/
Copy pathtestbench.cpp
File metadata and controls
254 lines (241 loc) · 12.2 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "testbench.h"
#include <unordered_set>
const int BASIC_TESTS = 20;
void Testbench::test_basic_read_write() {
// test read
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Testing basic read write\n", (int)sc_time_stamp().to_double() / 1000);
printf("[T=%d ns] [Testbench] Basic Test 1: Read correctness\n", (int)sc_time_stamp().to_double() / 1000);
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Basic Test 1: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> rd_data = 0;
sc_uint<32> rd_addr = rand() % 0x2000;
sc_uint<32> gt_rd_data = (slave->memory)[rd_addr];
printf("[T=%d ns] [Testbench] Basic Test 1: Reading addr=0x%x\n", (int)sc_time_stamp().to_double() / 1000, rd_addr.to_uint());
// blocking read request
sc_time before = sc_time_stamp();
master->basic_read_request(rd_addr, rd_data);
sc_time after = sc_time_stamp();
// check timing
if(after - before > sc_time((READ_DELAY_CYCLES + 2) * CLK_PERIOD, SC_NS)) {
fprintf(stderr, "[T=%d ns] [Testbench] Basic Test 1 Round %d Failed: Basic read took more time than expected\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1);
sc_stop();
}
// check results
if(gt_rd_data != rd_data) {
fprintf(stderr, "[T=%d ns] [Testbench] Basic Test 1 Round %d Failed: %x != %x\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1, gt_rd_data.to_uint(), rd_data.to_uint());
sc_stop();
} else {
printf("[T=%d ns] [Testbench] Basic Test 1: Round %d passed\n\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
}
}
// test write
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Basic Test 2: Write correctness\n", (int)sc_time_stamp().to_double() / 1000);
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Basic Test 2: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> wr_data = rand() % 0x4000;
sc_uint<32> wr_addr = rand() % 0x2000;
printf("[T=%d ns] [Testbench] Basic Test 2: Writing addr=0x%x, data=0x%x\n", (int)sc_time_stamp().to_double() / 1000, wr_addr.to_uint(), wr_data.to_uint());
// blocking write request
sc_time before = sc_time_stamp();
master->basic_write_request(wr_addr, wr_data);
sc_time after = sc_time_stamp();
// check timing
if(after - before > sc_time((WRITE_DELAY_CYCLES + 2) * CLK_PERIOD, SC_NS)) {
fprintf(stderr, "[T=%d ns] [Testbench] Basic Test 2 Round %d Failed: Basic write took more time than expected\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1);
sc_stop();
}
// check results
if(slave->memory[wr_addr] != wr_data) {
std::cerr << "[T=" << sc_time_stamp() << "] [Testbench] Basic Test 2: Write Failed: " << slave->memory[wr_addr] << "!=" << wr_data << std::endl;
sc_stop();
} else {
printf("[T=%d ns] [Testbench] Basic Test 2: Round %d passed\n\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
}
}
// test passed
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Basic read and write test passed\n\n", (int)sc_time_stamp().to_double() / 1000);
score += 60;
basic_finished.notify();
}
void Testbench::spawned_read_request(int i, int test_id, sc_uint<32> rd_addr) {
sc_uint<32> gt_rd_data = (slave->memory)[rd_addr];
sc_uint<32> rd_data = 0;
sc_time before = sc_time_stamp();
master->pipelined_read_request(rd_addr, rd_data);
sc_time after = sc_time_stamp();
if(after != before) {
fprintf(stderr, "[T=%d ns] [Testbench] Pipelined Test %d Round %d Failed: Pipeline read should not block\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1);
sc_stop();
}
for (int i = 0; i < READ_DELAY_CYCLES + 2; i++)
wait(HCLK.posedge_event());
wait(1, SC_NS);
if(gt_rd_data != rd_data) {
fprintf(stderr, "[T=%d ns] [Testbench] Pipelined Test %d Round %d Failed: %x != %x\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1, gt_rd_data.to_uint(), rd_data.to_uint());
sc_stop();
} else {
printf("[T=%d ns] [Testbench] Pipelined Test %d: Round %d passed\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1);
}
}
void Testbench::spawned_write_request(int i, int test_id, sc_uint<32> wr_data, sc_uint<32> wr_addr) {
sc_time before = sc_time_stamp();
master->pipelined_write_request(wr_addr, wr_data);
sc_time after = sc_time_stamp();
if(after != before) {
fprintf(stderr, "[T=%d ns] [Testbench] Pipelined Test %d Round %d Failed: Pipeline write should not block\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1);
sc_stop();
}
for (int i = 0; i < WRITE_DELAY_CYCLES + 2; i++)
wait(HCLK.posedge_event());
wait(1, SC_NS);
sc_uint<32> gt_data = (slave->memory)[wr_addr];
if(gt_data != wr_data) {
fprintf(stderr, "[T=%d ns] [Testbench] Pipelined Test %d Round %d Failed: %x != %x\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1, gt_data.to_uint(), wr_data.to_uint());
sc_stop();
} else {
printf("[T=%d ns] [Testbench] Pipelined Test %d: Round %d passed\n\n",
(int)sc_time_stamp().to_double() / 1000, test_id, i + 1);
}
}
void Testbench::test_pipelined_read_write() {
wait(basic_finished);
// test read
wait(HCLK.posedge_event());
printf("\n");
printf("[T=%d ns] [Testbench] Testing pipelined read write\n", (int)sc_time_stamp().to_double() / 1000);
printf("[T=%d ns] [Testbench] Pipelined Test 1: Pipelined read correctness\n", (int)sc_time_stamp().to_double() / 1000);
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Pipelined Test 1: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> rd_addr = rand() % 0x2000;
printf("[T=%d ns] [Testbench] Pipelined Test 1: Reading addr=0x%x\n", (int)sc_time_stamp().to_double() / 1000, rd_addr.to_uint());
sc_spawn(
[&, i, rd_addr]() {
this->spawned_read_request(i, 1, rd_addr);
},
("test_read" + std::to_string(i)).c_str()
);
for (int i = 0; i < READ_DELAY_CYCLES + 1; i++)
wait(HCLK.posedge_event());
}
// test write
for (int i = 0; i < READ_DELAY_CYCLES + 1; i++)
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Pipelined Test 2: Pipelined write correctness\n", (int)sc_time_stamp().to_double() / 1000);
// gen distinct random write addresses
std::unordered_set<int> write_addresses;
while (write_addresses.size() < BASIC_TESTS) {
write_addresses.insert(rand() % 0x2000);
}
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Pipelined Test 2: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> wr_data = rand() % 0x4000;
sc_uint<32> wr_addr = *(std::next(write_addresses.begin(), i));
printf("[T=%d ns] [Testbench] Pipelined Test 2: Writing addr=0x%x, data=0x%x\n", (int)sc_time_stamp().to_double() / 1000, wr_addr.to_uint(), wr_data.to_uint());
sc_spawn(
[&, i, wr_data, wr_addr]() {
this->spawned_write_request(i, 2, wr_data, wr_addr);
},
("test_write_" + std::to_string(i)).c_str()
);
for (int i = 0; i < WRITE_DELAY_CYCLES + 1; i++)
wait(HCLK.posedge_event());
}
// test passed
for (int i = 0; i < WRITE_DELAY_CYCLES + 1; i++)
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Pipelined read and write test passed\n\n", (int)sc_time_stamp().to_double() / 1000);
score += 20;
pipelined_finished.notify();
}
void Testbench::test_burst_read_write() {
wait(pipelined_finished);
wait(HCLK.posedge_event());
// test burst read
printf("[T=%d ns] [Testbench] Testing burst read write\n", (int)sc_time_stamp().to_double() / 1000);
printf("[T=%d ns] [Testbench] Burst Test 1: Burst read correctness\n", (int)sc_time_stamp().to_double() / 1000);
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Burst Test 1: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> rd_addr = rand() % 0x2000;
int burst_length = 4 * (rand() % 4 + 1); // 4, 8, 12, 16
printf("[T=%d ns] [Testbench] Burst Test 1: Reading start addr=0x%x, burst length=%d\n",
(int)sc_time_stamp().to_double() / 1000, rd_addr.to_uint(), burst_length);
std::vector<sc_uint<32>> rd_data, gt_rd_data;
for (int j = 0; j < burst_length; j++) {
gt_rd_data.push_back(slave->memory[rd_addr + j]);
}
// blocking burst read request
sc_time before = sc_time_stamp();
master->burst_read_request(rd_addr, burst_length, rd_data);
sc_time after = sc_time_stamp();
// check timing
if(after - before > sc_time((burst_length + READ_DELAY_CYCLES + 1) * CLK_PERIOD, SC_NS)) {
fprintf(stderr, "[T=%d ns] [Testbench] Burst Test 1 Round %d Failed: Burst read took more time than expected\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1);
sc_stop();
}
// check results
if (rd_data.size() != burst_length) {
fprintf(stderr, "[T=%d ns] [Testbench] Burst Test 1 Round %d Failed: Read data size incorrect\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1);
sc_stop();
} else {
for (int j = 0; j < burst_length; j++) {
if (rd_data[j] != gt_rd_data[j]) {
fprintf(stderr, "[T=%d ns] [Testbench] Burst Test 1 Round %d Failed at burst index %d: %x != %x\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1, j, rd_data[j].to_uint(), gt_rd_data[j].to_uint());
sc_stop();
}
}
}
wait(HCLK.posedge_event());
}
// test burst write
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Burst Test 2: Burst write correctness\n", (int)sc_time_stamp().to_double() / 1000);
for (int i = 0; i < BASIC_TESTS; i++) {
printf("\n[T=%d ns] [Testbench] Burst Test 2: Round %d start\n", (int)sc_time_stamp().to_double() / 1000, i + 1);
sc_uint<32> wr_addr = rand() % 0x2000;
int burst_length = 4 * (rand() % 4 + 1); // 4, 8, 12, 16
std::vector<sc_uint<32>> wr_data, gt_data;
for (int j = 0; j < burst_length; j++) {
wr_data.push_back(rand() % 0x4000);
}
printf("[T=%d ns] [Testbench] Burst Test 2: Writing start addr=0x%x, burst length=%d\n",
(int)sc_time_stamp().to_double() / 1000, wr_addr.to_uint(), burst_length);
// blocking burst write request
sc_time before = sc_time_stamp();
master->burst_write_request(wr_addr, burst_length, wr_data);
sc_time after = sc_time_stamp();
// check timing
if(after - before > sc_time((burst_length + WRITE_DELAY_CYCLES + 1) * CLK_PERIOD, SC_NS)) {
fprintf(stderr, "[T=%d ns] [Testbench] Burst Test 2 Round %d Failed: Burst write took more time than expected\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1);
sc_stop();
}
// check results
for (int j = 0; j < burst_length; j++) {
sc_uint<32> gt_data = slave->memory[wr_addr + j];
if (wr_data[j] != gt_data) {
fprintf(stderr, "[T=%d ns] [Testbench] Burst Test 2 Round %d Failed at burst index %d: %x != %x\n\n",
(int)sc_time_stamp().to_double() / 1000, i + 1, j, wr_data[j].to_uint(), gt_data.to_uint());
sc_stop();
}
}
wait(HCLK.posedge_event());
}
wait(HCLK.posedge_event());
printf("[T=%d ns] [Testbench] Burst read and write test passed\n\n", (int)sc_time_stamp().to_double() / 1000);
score += 20;
sc_stop();
}