-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_run_sc.cpp
312 lines (276 loc) · 6.35 KB
/
cpu_run_sc.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <systemc.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include "Vcpu_top.h"
#include "Vcpu_top_cpu_top.h"
#include "Vcpu_top_io_port.h"
#include "Vcpu_top_core_top.h"
#include "Vcpu_top_core_top.h"
#include "Vcpu_top_core.h"
#include "Vcpu_top_regfile.h"
#include "Vcpu_top_bram_dpstrobe.h"
#include "Vcpu_top_timer.h"
#include "disasm.h"
class cpu_run_t : public sc_module
{
public:
Vcpu_top* dut;
uint32_t* ROM;
uint32_t* FD_PC;
uint32_t* FD_inst;
sc_in<bool> clk_tb;
sc_signal<bool> resetb_tb;
sc_signal<uint32_t> gpio0_tb;
bool test_passes, test_fails, test_halt;
uint32_t test_result_base_addr;
SC_HAS_PROCESS(cpu_run_t);
cpu_run_t(sc_module_name name, const std::string& path)
: sc_module(name)
, program(path)
, clk_tb("clk_tb")
, resetb_tb("resetb_tb")
, gpio0_tb("gpio0_tb")
{
SC_CTHREAD(test_thread, clk_tb.pos());
test_result_base_addr = 0;
dut = new Vcpu_top("dut");
dut->clk(clk_tb);
dut->resetb(resetb_tb);
dut->gpio0(gpio0_tb);
//ROM = dut->cpu_top->CT0->MMU0->rom0->ROM;
ROM = dut->cpu_top->RAM0->ram;
FD_PC = &(dut->cpu_top->CT0->CPU0->FD_PC);
FD_inst = &(dut->cpu_top->CT0->CPU0->im_do);
// FD_disasm_opcode =
// (char*)dut->cpu_top->CT0->CPU0->inst_dec->disasm_opcode;
}
std::string reverse(char* s) {
std::string str(s);
std::reverse(str.begin(), str.end());
return str;
}
void reset()
{
resetb_tb.write(false);
wait();
resetb_tb.write(true);
wait();
}
uint32_t get_memory_word(uint32_t i)
{
uint32_t word = 0;
word = dut->cpu_top->RAM0->ram[i];
return word;
}
void initialize_memory()
{
for (int i=0; i<1024; ++i) {
dut->cpu_top->RAM0->ram[i] = 0xAAAAAAAA;
}
}
void dump_memory();
void scan_memory_for_base_address();
void view_snapshot_pc()
{
std::cout << "(TT) Opcode=" << disasm(*FD_inst)
<< ", FD_PC=0x"
<< std::hex
<< *FD_PC
<< std::endl;
}
void view_snapshot_hex()
{
std::cout << "(TT) Opcode=" << disasm(*FD_inst)
<< ", FD_PC=0x"
<< std::hex
<< *FD_PC
<< ", sp = 0x" << std::hex << dut->cpu_top->CT0->CPU0->RF->data[2]
<< ", a5 = 0x" << std::hex << dut->cpu_top->CT0->CPU0->RF->data[15]
//<< ", ram_addr: " << std::hex << std::setfill('0') << std::setw(8) << dut->cpu_top->RAM0->addrb * 4
//<< ", ram_wdata: " << std::hex << std::setfill('0') << std::setw(8) << dut->cpu_top->RAM0->dinb
//<< ", ram_wstrb: " << std::hex << (unsigned int) dut->cpu_top->RAM0->web
//<< ", dm_be: " << std::hex << (unsigned int) dut->cpu_top->CT0->CPU0->dm_be
<< std::endl;
}
void view_snapshot_int()
{
std::cout << "(TT) Opcode=" << disasm(*FD_inst)
<< ", FD_PC=0x"
<< std::hex
<< *FD_PC
<< ", x1 = "
<< static_cast<int32_t>(dut->cpu_top->CT0->CPU0->RF->data[1])
<< std::endl;
}
void poll_io(void);
//void tb_handshake(void);
bool load_program(const std::string& path)
{
for (int i=0; i<1024; ++i) {
ROM[i] = 0;
}
ifstream f(path, std::ios::binary);
if (f.is_open()) {
f.seekg(0, f.end);
int size = f.tellg();
if (size == 0 || size > 4096) {
return false;
}
if (size % 4 != 0) {
return false;
}
f.seekg(0, f.beg);
auto buf = new char[size];
f.read(buf, size);
auto words = (uint32_t*) buf;
for (int i=0; i<size/4; ++i) {
ROM[i] = words[i];
}
f.close();
delete[] buf;
return true;
}
else {
return false;
}
}
void test_thread(void);
private:
std::string program;
};
void cpu_run_t::poll_io()
{
bool en = dut->cpu_top->IO0->io_en;
bool we = dut->cpu_top->IO0->io_we;
uint8_t addr8 = dut->cpu_top->IO0->io_addr;
{
// Testbench command
test_passes = false;
test_fails = false;
test_halt = false;
if (en && we) {
// IO domain address is 0x0
if (addr8 == 0) {
switch (dut->cpu_top->IO0->io_data_write) {
case 0:
scan_memory_for_base_address();
break;
case 1:
test_passes = true;
break;
case 2:
test_fails = true;
break;
case 3:
test_halt = true;
break;
default:
assert(false && "Invalid testbench command");
break;
}
}
}
}
}
// Handshake happens when 0x80000000 writes non-zero
//void cpu_run_t::tb_handshake()
//{
// while (true) {
// wait();
// }
//}
std::string reverse(const sc_bv<256>& bv)
{
char buf[32];
for (int i=0; i<31; ++i) {
buf[i] = bv.range(i*8+7, i*8).to_uint();
}
std::string op(buf);
std::reverse(op.begin(), op.end());
return op;
}
void cpu_run_t::scan_memory_for_base_address()
{
bool tail = false;
std::cout << "(II) Scanning Memory" << std::endl;
for (int i=1023; i>=0; --i) {
uint32_t word = get_memory_word(i);
if (!tail) {
if (word == 0xDEADDEAD) {
tail = true;
std::cout << "(II) Tail at 0x" << std::hex << i << std::endl;
}
}
else {
if (word != 0xFFFFFFFF) {
test_result_base_addr = (i+1) << 2;
std::cout << "(II) Base at 0x" << std::hex << i << std::endl;
return;
}
}
}
}
void cpu_run_t::dump_memory()
{
bool begin_dump = false;
//bool align_skipped = false;
bool align_skipped = true;
ofstream f("mem.log");
int i = test_result_base_addr >> 2;
assert(i < 1024);
for (; i<1024; ++i) {
uint32_t word = get_memory_word(i);
if (f.is_open()) {
f << std::setfill('0') << std::setw(8)
<< std::hex << word << std::endl;
}
if (word == 0xdeaddead) {
break;
}
std::cout << "(DD) "
<< std::setfill('0') << std::setw(8)
<< std::hex << word << std::endl;
}
f.close();
}
void cpu_run_t::test_thread()
{
std::string full_name = program + ".bin";
initialize_memory();
if (!load_program(full_name)) {
std::cerr << "Program load failed!" << std::endl;
exit(1);
}
reset();
for (int i=0; i<4096; ++i) {
poll_io();
view_snapshot_hex();
if (test_passes) {
std::cout << "A test passes!" << std::endl;
}
if (test_fails) {
std::cout << "A test fails at PC=0x" << std::hex << *FD_PC << std::endl;
}
if (test_halt) {
std::cout << "End of the test." << std::endl;
break;
}
wait();
}
// TODO: Dump memory
dump_memory();
sc_stop();
}
////////////////////////
int sc_main(int argc, char** argv)
{
Verilated::commandArgs(argc, argv);
assert(argc == 2);
auto tb = new cpu_run_t("cpu0", argv[1]);
sc_clock sysclk("sysclk", 10, SC_NS);
tb->clk_tb(sysclk);
sc_start();
delete tb;
exit(0);
}