forked from 21-cpu-group2/sim_binary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
301 lines (283 loc) · 9.96 KB
/
simulator.cpp
File metadata and controls
301 lines (283 loc) · 9.96 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
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iterator>
#include "simulator.hpp"
uint32_t bin2int(string bin) {
// std::stoiがうまく機能しないので自分で実装。
int ofst = (bin.at(1) == 'b') ? 2 : 0;
uint32_t ret = 0x00000000;
for (int i=0; i<bin.size()-ofst; i++){
ret <<= 1;
if (bin.at(i + ofst) == '1'){
ret += 1;
}
}
return ret;
}
uint32_t hex2int(string hex) {
// std::stoiがうまく機能しないので自分で実装。
int ofst = (hex.at(1) == 'x') ? 2 : 0;
uint32_t ret = 0x00000000;
for (int i=0; i<hex.size()-ofst; i++){
ret <<= 4;
if ((uint32_t)(hex.at(i + ofst) - '0') < 10){ // num
ret += (uint32_t)(hex.at(i + ofst) - '0');
}
else {
ret += (uint32_t)(hex.at(i + ofst) - 'A') + (uint32_t)10;
}
}
return ret;
}
void init_emulator(Emulator* emu){
for (int i=0; i<REG_SIZE; i++){
emu->reg[i] = 0x00000000;
}
//////////////////////////////////////////
emu->pc = 0x00000000;
//メモリを動的に確保
emu->memory = (uint32_t*)malloc(sizeof(uint32_t) * MEMORY_SIZE);
emu->instruction_memory = (uint32_t*)malloc(sizeof(uint32_t) * INSTRUCTION_MEMORY_SIZE);
emu->cache = (cache_line*)malloc(sizeof(cache_line) * CACHE_LINE_NUM);
if (emu->memory == NULL || emu->instruction_memory == NULL){
printf("error : cannot allocate memory\n");
exit(1);
}
memset(emu->memory, 0, sizeof(uint32_t) * MEMORY_SIZE);
memset(emu->instruction_memory, 0, sizeof(uint32_t) * INSTRUCTION_MEMORY_SIZE);
memset(emu->cache, 0, sizeof(cache_line) * CACHE_LINE_NUM);
for (int i=0; i<CACHE_LINE_NUM; i++) {
emu->cache[i].valid = false;
}
emu->instruction_size = 0x00000000;
emu->clks = 0ll;
emu->args.flg_p = false;
emu->args.flg_a = false;
emu->args.flg_r = false;
emu->args.flg_s = false;
emu->args.flg_g = false;
emu->args.flg_R = false;
emu->args.flg_m = false;
emu->args.flg_e = false;
emu->args.print_asm = false;
emu->args.start = 0;
emu->args.goal = 0;
emu->args.mem_s = 0;
emu->args.endpc = 0;
for (int i=0; i<REG_SIZE; i++){
emu->args.reg_for_print[i] = false;
}
emu->stats.beq = 0ll;
emu->stats.bne = 0ll;
emu->stats.blt = 0ll;
emu->stats.bge = 0ll;
emu->stats.lw = 0ll;
emu->stats.sw = 0ll;
emu->stats.addi = 0ll;
emu->stats.slli = 0ll;
emu->stats.srli = 0ll;
emu->stats.add = 0ll;
emu->stats.sub = 0ll;
emu->stats.sll = 0ll;
emu->stats.lui = 0ll;
emu->stats.jal = 0ll;
emu->stats.jalr = 0ll;
emu->stats.fadd = 0ll;
emu->stats.fsub = 0ll;
emu->stats.fmul = 0ll;
emu->stats.fdiv = 0ll;
emu->stats.fhalf = 0ll;
emu->stats.fsqrt = 0ll;
emu->stats.fabs = 0ll;
emu->stats.fneg = 0ll;
emu->stats.fiszero = 0ll;
emu->stats.fisneg = 0ll;
emu->stats.fispos = 0ll;
emu->stats.fless = 0ll;
emu->stats.floor = 0ll;
emu->stats.ftoi = 0ll;
emu->stats.itof = 0ll;
for (int i=0; i<100000; i++){
emu->stats.exec_times[i] = 0ll;
}
emu->stats.cache_hit = 0ll;
emu->stats.cache_miss = 0ll;
for (int i=0; i<32; i++){
emu->stats.reg_used[i] = 0ll;
}
// memory <- input_data
int input_start = 50000; // 200000 / 4
ifstream ifs("data/contest.txt");
if (ifs.fail()){
cout << "cannot open the file \"contest.txt\"" << endl;
return ;
}
string str;
while (getline(ifs, str)){
uint32_t n = (uint32_t)stoul(str, nullptr, 10);
emu->memory[input_start++] = n;
}
if (DEBUG) {
for (int i=0; i<emu->instruction_size; i++){
cout << hex << "0x" << emu->instruction_memory[i] << endl;
}
}
// uint32_t address_mask = 0x07FFFFFF;
// emu->mask.tag_mask = (0xFFFFFFFF << (BLOCK_SIZE_BIT + BLOCK_NUM_BIT)) & address_mask;
// emu->mask.index_mask = ((0xFFFFFFFF << (BLOCK_SIZE_BIT)) ^ emu->mask.tag_mask) & address_mask;
// emu->mask.offset_mask = ((0xFFFFFFFF ^ emu->mask.tag_mask) ^ emu->mask.index_mask) & address_mask;
}
void destroy_emulator(Emulator* emu) {
free(emu->memory);
free(emu->instruction_memory);
free(emu);
}
int load_instructions(Emulator* emu, string file_path){
// read machine code and keep in instruction-memory
ifstream ifs(file_path);
if (ifs.fail()){
return 1;
}
string str;
bool fst = true;
while (getline(ifs, str)){
// if (fst) {
// uint32_t pc_init = (uint32_t)stoi(str, nullptr, 10);
// emu->pc = pc_init;
// fst = false;
// continue;
// }
if (str.at(0) == '/' && str.at(1) == '/'){
continue;
}
uint32_t inst = bin2int(str);
emu->instruction_memory[emu->instruction_size++] = inst;
}
return 0;
if (DEBUG) {
for (int i=0; i<emu->instruction_size; i++){
cout << hex << "0x" << emu->instruction_memory[i] << endl;
}
}
}
void print_reg(Emulator* emu){
// 普通のレジスタの表示
cout << "----------------------------------------------------------------------------------------------------------------" << endl;
for (int i=0; i<REG_SIZE; i++){
cout << setfill(' ') << right << setw(8) << reg_name[i] << " : " <<
"0x" << hex << setfill('0') << right << setw(8) << emu->reg[i];
cout << " ";
if (i % 4 == 3) {cout << endl;}
}
cout << "----------------------------------------------------------------------------------------------------------------" << endl;
// cout << setfill(' ') << right << setw(8) << "pc" << " : " <<
// "0x" << hex << setfill('0') << right << setw(8) << emu->pc << endl;
// cout << "----------------------------------------------------------------------------------------------------------------" << endl;
}
void print_reg_for_debug(Emulator* emu){
//cout << "----------------------------------------------------------------------------------------------------------------" << endl;
for (int i=0; i<REG_SIZE; i++){
if (emu->args.reg_for_print[i]){
cout << dec << setfill('0') << right << setw(8) << emu->reg[i];
cout << " ";
}
}
cout << dec << setfill('0') << right << setw(8) << emu->pc << endl;
}
void print_mem(Emulator* emu, int start){
// startから 128 byte 表示
// string address_hex;
// cin >> address_hex;
// uint32_t start = hex2int(address_hex);
start /= 4;
cout << "----------------------------------------------------------------------------------------------------------------" << endl;
for (int i=0; i<4; i++){
cout << "" << hex << setfill(' ') << right << setw(10) << "address " << " : ";
cout << " " << hex << setfill(' ') << left << setw(8) << "data" ;
cout << " ";
}
cout << endl;
for (int i=0; i<32; i++){
cout << "0x" << hex << setfill('0') << right << setw(8) << (start + i) * 4 << " : ";
cout << "0x" << hex << setfill('0') << right << setw(8) << emu->memory[start+i] ;
cout << " ";
if (i % 4 == 3) {cout << endl;}
}
cout << "----------------------------------------------------------------------------------------------------------------" << endl;
}
// void output_image(Emulator* emu){
// // %out ?γ???? : 300000
// // %out ?ν?λ?? : 6591488 if 512 * 512
// // 693248 if 128 * 128
// int out_start = 300000;
// int out_goal = 693248; // if 128 * 128
// // int out_goal = 6591488; // if 512 * 512
// ofstream writing_file;
// FILE *fp;
// fp = fopen("output.ppm", "w");
// for (int i=out_start; i<out_goal; i += 4){
// if (emu->memory[i/4] == 538981200){
// fprintf(fp, "P3");
// }
// else if (emu->memory[i/4] == 538976266){ // 0x2020200A SPC SPC SPC LF
// fprintf(fp, "\n");
// }
// else if (emu->memory[i/4] == 538976288){ // 0x20202020 SPC SPC SPC SPC
// fprintf(fp, " ");
// }
// else{
// fprintf(fp, "%03d", (emu->memory[i/4]));
// }
// }
// return;
// }
void output_image(Emulator* emu){
// %out の開始時 : 300000
// %out の終了時 : 6591488 if 512 * 512
// 693248 if 128 * 128
int out_start = 300000;
// int out_goal = 496624; // if 128 * 128
// int out_goal = 1872896; // if 256 * 256
int out_goal = 3445744; // if 512 * 512
ofstream writing_file;
FILE *fp;
fp = fopen("output.ppm", "w");
for (int i=out_start; i<out_goal; i += 4){
uint32_t temp = emu->memory[i/4];
uint32_t t1, t2, t3, t4;
t1 = (temp & 0xFF000000) >> 24;
t2 = (temp & 0x00FF0000) >> 16;
t3 = (temp & 0x0000FF00) >> 8;
t4 = (temp & 0x000000FF);
fprintf(fp, "%c", t1);
fprintf(fp, "%c", t2);
fprintf(fp, "%c", t3);
fprintf(fp, "%c", t4);
}
return;
}
// void cache_save(Emulator* emu, uint32_t mem_address) {
// uint32_t block_address = (mem_address & emu->mask.index_mask) >> BLOCK_SIZE_BIT;
// uint32_t tag = (mem_address & emu->mask.tag_mask) >> (BLOCK_SIZE_BIT + BLOCK_NUM_BIT);
// //uint32_t index = (mem_address & )
// emu->cache[block_address].valid = true;
// emu->cache[block_address].tag = tag;
// // emu->cache[block_address].index = block_address;
// uint32_t start_ind = (mem_address >> BLOCK_SIZE_BIT) << (BLOCK_SIZE_BIT - 2);
// for (int i=0; i<BLOCK_SIZE/4; i++) {
// emu->cache[block_address].data[i] = emu->memory[start_ind + i];
// }
// }
// bool cache_hit(Emulator* emu, uint32_t mem_address) {
// // 暫定的に
// uint32_t block_address = (mem_address & emu->mask.index_mask) >> BLOCK_SIZE_BIT;
// uint32_t tag = (mem_address & emu->mask.tag_mask) >> (BLOCK_SIZE_BIT + BLOCK_NUM_BIT);
// if (emu->cache[block_address].tag == tag){
// return true;
// }
// return false;
// }