-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlock_step_run.cpp
316 lines (283 loc) · 10.5 KB
/
lock_step_run.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
313
314
315
316
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#define LOCKSTEP
#include "fyp18-riscv-emulator/src/emulator.h"
#undef SHOW_TERMINAL
#include "simulator/src/simulator.h"
#include <chrono>
#include <unistd.h>
#include <cstdlib>
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
using namespace std;
using namespace std::chrono;
#define LOGGING
#define DUMP_CONDITION 1 /* && (bench.tickcount > 15800955UL) */
#define PROBE_DOUBLE (0x08dbf180UL+0x0cUL) & (~7UL)
emulator golden_model;
struct keystroke_buffer {
unsigned char reader, writer, char_buffer[128];
};
/* void enable_raw_mode() {
termios term;
tcgetattr(0, &term);
term.c_lflag &= ~(ICANON | ECHO); // Disable echo as well
tcsetattr(0, TCSANOW, &term);
}
void disable_raw_mode() {
termios term;
tcgetattr(0, &term);
term.c_lflag |= ICANON | ECHO;
tcsetattr(0, TCSANOW, &term);
} */
// Define the function to be called when ctrl-c (SIGINT) is sent to process
void signal_callback_handler(int signum) {
golden_model.show_state();
disable_raw_mode();
tcflush(0, TCIFLUSH);
// Terminate program
exit(signum);
}
/* int kbhit()
{
int byteswaiting;
ioctl(0, FIONREAD, &byteswaiting);
return byteswaiting > 0;
} */
// emulator emu;
int main(int argc, char* argv[]) {
// Name of kernel image must be provided at run time
/* if (argc == 1) {
printf("Name of kerenl image must be provided at run time\n");
return 1;
} else if (argc > 2) {
printf("Too many arguments provided\n");
} */
/* if (!golden_model.load_kernel(argv[1])) {
printf("kernel loading failed\n");
return 1;
} */
simulator bench;
bench.init("fyp18-riscv-emulator/src/Image", argv[2], argv[3]);
printf("bench inititated!\n");
cout << endl;
// golden_model.load_dtb(argv[2], 0x7e00000UL);
// golden_model.load_bootrom(argv[3]);
// golden_model.load_symbols("resources/symbol_names.txt", "resources/symbol_pointers.bin");
char x;
// golden_model.init();
golden_model.init("fyp18-riscv-emulator/src/Image");
//golden_model.print_symbols();
/* golden_model.step();
golden_model.step(); */
#ifdef LOGGING
std::ofstream outFile("run.log"); // This will create or overwrite the file
// Check if the file is open
if (!outFile.is_open()) {
std::cerr << "Error opening the file." << std::endl;
return 1;
}
#endif
/* std::ifstream inputFile("resources/symbol_names.txt");
if (!inputFile.is_open()) {
std::cerr << "Failed to open the file." << std::endl;
return 1;
} */
std::vector<std::string> symbols;
std::string line;
/* while (std::getline(inputFile, line)) {
symbols.push_back(line);
} */
// inputFile.close();
// Now you can access and print the stored symbols
/* for (const std::string& storedLine : symbols) {
std::cout << storedLine << std::endl;
} */
unsigned long old_symbol = 1;
unsigned long mem_address, data;
unsigned long delta = 10000000;
//for (int i; i < 10; i++) {
printf("stepping\n");
// Use auto keyword to avoid typing long
// type definitions to get the timepoint
// at this instant use function now()
auto start = high_resolution_clock::now();
int timer_interr = 0;
signal(SIGINT, signal_callback_handler);
enable_raw_mode();
keystroke_buffer keys_rx;
keys_rx.reader = 0;
keys_rx.writer = 0;
unsigned long gprs[32];
bench.set_probe(PROBE_DOUBLE);
bench.step_nodump();
unsigned long sim_prev = 0x80100000UL;
while (1 || (bench.tickcount + bench.dump_tick) < 800351768UL) {
// golden_model.show_state();
//cin >> x;
if (kbhit()) {
// printf("detected input, %c\n", getchar());
keys_rx.char_buffer[keys_rx.writer++] = getchar();
keys_rx.reader += (keys_rx.reader == keys_rx.writer); // overflow
outFile << "keyhit\n";
}
// if (keys_rx.reader != keys_rx.writer) { keys_rx.reader += golden_model.load_rx_char(keys_rx.char_buffer[keys_rx.reader]); }
#ifdef LOGGING
/* if (golden_model.get_instruction() == 0x00100073)
break; */
//unsigned long current_symbol = golden_model.get_symbom_index(golden_model.get_pc(), old_symbol);
/* if (current_symbol != old_symbol)
{
outFile << setfill('0') << setw(8) << hex << golden_model.get_pc() << " " << symbols[current_symbol];// << "\n";
outFile << setfill('0') << setw(8) << hex << golden_model.get_csr_value(MIE) << "\n";
old_symbol = current_symbol;
} */
outFile << setfill('0') << setw(16) << dec << (bench.dump_tick) << " ";
outFile << setfill('0') << setw(16) << hex << golden_model.get_pc() << " ";
outFile << setfill('0') << setw(16) << hex << golden_model.get_instruction() << " ";
outFile << setfill('0') << setw(16) << hex << golden_model.fetch_long(PROBE_DOUBLE) << " ";
outFile << setfill('0') << setw(16) << hex << bench.get_probe() << " ";
outFile << setfill('0') << setw(16) << hex << golden_model.reg_file[15] << endl;
/* switch (golden_model.check_for_mem_access(&mem_address, &data))
{
case 1:
//if (mem_address == 0x80001000) { printf("0x%016lx\n", data); }
if (mem_address >= 0x80000001 || mem_address < 0x90000000) { break; }
outFile << "load access at " << setfill('0') << setw(16) << hex << mem_address;
outFile << " reading data " << setfill('0') << setw(16) << hex << data << "\n";
break;
case 2:
if (mem_address >= 0x80000001 || mem_address < 0x90000000) { break; }
outFile << "store access at " << setfill('0') << setw(16) << hex << mem_address;
outFile << " writing data " << setfill('0') << setw(16) << hex << data << "\n";
break;
case 3:
if (mem_address >= 0x80000001 || mem_address < 0x90000000) { break; }
outFile << "atomic access at " << setfill('0') << setw(16) << hex << mem_address;
outFile << " reading data " << setfill('0') << setw(16) << hex << data << "\n";
break;
default:
break;
} */
#endif
/* if (
golden_model.check_for_mem_access(&mem_address, &data) &&
(mem_address == 0x10000004) &&
(data == 0))
{
break;
} */
// golden_model.show_state();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
/* if (timer_interr == 0)
if (duration.count() > 10000000) { printf("Timer might be working"); timer_interr++; } */
if (duration.count() > 10000000) {
//printf("Timer might be working");
//start = high_resolution_clock::now();
//golden_model.set_mtip();
}
// golden_model.set_mtime(duration.count()*10);
//printf("%d\n", timer_interr);
// Write data to the file
//printf("Timer might be working");
/**
* bench.prev_pc - executed pc by cpu pipeline
* golden_model.get_pc - The next instruction to be executed
*
* This happens because we get bench.prev_pc after it is executed.
* This needs to be fixed to have a common semantic for both pc values
*/
if (bench.prev_pc != golden_model.get_pc()) {
cout << "PC mismatech emulator: " << hex << golden_model.get_pc();
cout << " emulator instruction: " << setfill('0') << setw(8) << hex << golden_model.get_instruction(); cout << " simulator: " << hex << bench.prev_pc << endl;
golden_model.show_state();
break;
}
/* if (1 || (bench.tickcount >= 856489UL)) {
cout << "time out reached" << endl;
golden_model.show_state(); break;
} */
if (bench.check_registers(golden_model.reg_file, golden_model.get_mstatus())) {
cout << "Register mismatch at register " << dec << bench.check_registers(golden_model.reg_file, golden_model.get_mstatus());
cout << " simulator value: " << setfill('0') << setw(16) << hex << bench.read_register(bench.check_registers(golden_model.reg_file, golden_model.get_mstatus())) << endl;
golden_model.show_state();
cout << dec << (bench.tickcount + bench.dump_tick) << endl;bench.step(); bench.step(); bench.step(); bench.step(); bench.step(); break;
}
if (bench.check_registersF(golden_model.freg_file) != 33) {
cout << "Register mismatch at fregister " << dec << bench.check_registersF(golden_model.freg_file);
printf(" simulator value: %016x\n",bench.read_registerF(bench.check_registersF(golden_model.freg_file)));
// cout << " simulator value: " << setfill('0') << setw(8) << bench.read_registerF(bench.check_registersF(golden_model.freg_file)) << endl;
golden_model.show_state();
cout << dec << (bench.tickcount + bench.dump_tick) << endl;bench.step(); bench.step(); bench.step(); bench.step(); bench.step(); break;
}
sim_prev = golden_model.get_pc();
int x = 1;
if (DUMP_CONDITION) {
x = bench.step();
} else {
x = bench.step_nodump();
}
if (x == 1) { break; }
// bench.step();
if (x == 0) {
if (golden_model.is_peripheral_read()) {
// cout << "peripheral read" << endl;
__uint32_t p_instruction = golden_model.get_instruction();
golden_model.step();
golden_model.set_register_with_value((p_instruction>>7)&0x1f, bench.get_register_value((p_instruction>>7)&0x1f));
} else{
golden_model.step();
}
while (
((golden_model.get_instruction() & 0x0000007f) == 0x73) &&
(golden_model.get_instruction() & 0x00007000)
)
{
golden_model.step();
}
} else if ((x == 2) && (golden_model.set_interrupts()))
{
cout << "Setting interrupts failed in emulator" << endl;
cout << "tickcount: " << dec << (bench.tickcount + bench.dump_tick) << endl;
golden_model.show_state();
break;
}
if (x == 2) {
if (DUMP_CONDITION) {
x = bench.step();
} else {
x = bench.step_nodump();
}
// printf("Taking interrupt\n");
// golden_model.show_state();
if (x == 1) { return 1; }
if (golden_model.is_peripheral_read()) {
// cout << "peripheral read" << endl;
__uint32_t p_instruction = golden_model.get_instruction();
golden_model.step();
golden_model.set_register_with_value((p_instruction>>7)&0x1f, bench.get_register_value((p_instruction>>7)&0x1f));
} else{
golden_model.step();
}
while (
((golden_model.get_instruction() & 0x0000007f) == 0x73) &&
(golden_model.get_instruction() & 0x00007000)
)
{
golden_model.step();
}
}
}
#ifdef LOGGING
outFile.close();
#endif
printf("Test failed: Time-out!\n");
printf("Total ticks: %ld \n", (bench.tickcount+bench.dump_tick));
disable_raw_mode();
tcflush(0, TCIFLUSH);
return 1;
}