-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator.hpp
More file actions
217 lines (199 loc) · 6.67 KB
/
simulator.hpp
File metadata and controls
217 lines (199 loc) · 6.67 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
#ifndef _SIMULATOR
#define _SIMULATOR
#define MEMORY_SIZE (33554432) // 2^25(1024*1024*32)だけ欲しい
#define INSTRUCTION_MEMORY_SIZE (1024*1024) //どのくらい必要?
#define REG_SIZE 32
#define FREG_SIZE 32
#define DEBUG 0
#define PRINT_STAT 1
//#define DEBUG2 0 // if 1 then showing specific registers
#define RM 0b000 // Round Mode(float)
#include <iostream>
#include <stdlib.h>
#include <string>
#include <map>
//#include "cache.hpp"
// とりあえず、direct-mapped
#define ADDRESS_BIT 27
// #define CACHE_SIZE 512
#define CACHE_LINE_NUM 1028
#define CACHE_LINE_SIZE 4 // 8 words
#define TAG_BIT 13
#define TAG_MASK 0x07FFC000
#define INDEX_BIT 10
#define INDEX_MASK 0x00003FF0
#define OFFSET_BIT 4
#define OFFSET_MASK 0x0000000F
using namespace std;
enum Register { zero, ra, sp, gp, tp, t0, t1, t2,
s0, s1, a0, a1, a2, a3, a4, a5,
a6, a7, s2, s3, s4, s5, s6, s7,
s8, s9, s10, s11, t3, t4, t5, t6};
enum fRegister { ft0, ft1, ft2, ft3, ft4, ft5, ft6, ft7,
fs0, fs1, fa0, fa1, fa2, fa3, fa4, fa5,
fa6, fa7, fs2, fs3, fs4, fs5, fs6, fs7,
fs8, fs9, fs10, fs11, ft8, ft9, ft10, ft11};
static char reg_name[REG_SIZE][16] = {
"%zero","%ra","%sp","%mc_hp",
"%in","%out","%a0","%a1",
"%a2","%a3","%a4","%a5",
"%a6","%a7","%a8","%a9",
"%a10","%a11","%a12","%fzero",
"%f0","%f1","%f2","%f3",
"%f4","%f5","%f6","%f7",
"%f8","%f9","%f10","%f11",
};
/*
static char freg_name[REG_SIZE][8] = {
"ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7",
"fs0", "fs1", "fa0", "fa1", "fa2", "fa3", "fa4", "fa5",
"fa6", "fa7", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
"fs8", "fs9", "fs10", "fs11", "ft8", "ft9", "ft10", "ft11",
};
*/
static char freg_name[REG_SIZE][8] = {
"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
"f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
};
static map<string, int> reg_num{
{ "zero", 0 }, { "ra", 1 }, { "sp", 2 }, { "gp", 3 },
{ "tp", 4 }, { "t0", 5 }, { "t1", 6 }, { "t2", 7 },
{ "s0", 8 }, { "s1", 9 }, { "a0", 10 }, { "a1", 11 },
{ "a2", 12 }, { "a3", 13 }, { "a4", 14 }, { "a5", 15 },
{ "a6", 16 }, { "a7", 17 }, { "s2", 18 }, { "s3", 19 },
{ "s4", 20 }, { "s5", 21 }, { "s6", 22 }, { "s7", 23 },
{ "s8", 24 }, { "s9", 25 }, { "s10", 26 }, { "s11", 27 },
{ "t3", 28 }, { "t4", 29 }, { "t5", 30 }, { "t6", 31 }
};
static map<string, int> freg_num{
{ "ft0", 0 },{ "ft1", 1 },{ "ft2", 2 },{ "ft3", 3 },
{ "ft4", 4 },{ "ft5", 5 },{ "ft6", 6 },{ "ft7", 7 },
{ "fs0", 8 },{ "fs1", 9 },{ "fa0", 10 },{ "fa1", 11 },
{ "fa2", 12 },{ "fa3", 13 },{ "fa4", 14 },{ "fa5", 15 },
{ "fa6", 16 },{ "fa7", 17 },{ "fs2", 18 },{ "fs3", 19 },
{ "fs4", 20 },{ "fs5", 21 },{ "fs6", 22 },{ "fs7", 23 },
{ "fs8", 24 },{ "fs9", 25 },{ "fs10", 26 },{ "fs11", 27 },
{ "ft8", 28 },{ "ft9", 29 },{ "ft10", 30 },{ "ft11", 31 }
};
typedef struct {
bool flg_p;
bool flg_a;
bool flg_r;
bool flg_s;
bool flg_g;
bool flg_R;
bool flg_m;
bool flg_e;
bool print_asm;
long long int start;
long long int goal;
int endpc;
int mem_s; // if print memory, show from memory address "mem_s"
bool reg_for_print[64];
} cmdline_args;
typedef struct {
long long int beq;
long long int bne;
long long int blt;
long long int bge;
long long int lw;
long long int sw;
long long int addi;
long long int slli;
long long int srli;
long long int add;
long long int sub;
long long int sll;
long long int lui;
long long int jal;
long long int jalr;
long long int fadd;
long long int fsub;
long long int fmul;
long long int fdiv;
long long int fhalf;
long long int fsqrt;
long long int fabs;
long long int fneg;
long long int fiszero;
long long int fisneg;
long long int fispos;
long long int fless;
long long int floor;
long long int ftoi;
long long int itof;
long long int exec_times[100000];
long long int cache_hit;
long long int cache_miss;
long long int reg_used[32];
} statistics;
typedef struct {
bool valid;
uint32_t tag; // 実際は下位13bitのみを使用
// uint32_t index; emu->cache[ind] の indでわかる。
uint32_t data[CACHE_LINE_SIZE];
} cache_line;
typedef struct {
uint32_t reg[REG_SIZE];
// uint32_t freg[FREG_SIZE];
int32_t pc;
uint32_t* memory;
uint32_t *instruction_memory;
int instruction_size;
long long int clks;
cmdline_args args;
cache_line *cache;
//masks mask;
statistics stats;
} Emulator;
inline void print_masked_num(uint32_t num, uint32_t mask_size){
for (int i=0; i<mask_size; i++){
cout << ((num & (1<<(mask_size-i-1))) != 0);
}
cout << endl;
}
inline bool cache_hit(Emulator* emu, uint32_t mem_address) {
// mem_address : 27bitとする。
uint32_t tag = (mem_address & TAG_MASK) >> (INDEX_BIT + OFFSET_BIT);
uint32_t index = (mem_address & INDEX_MASK) >> (OFFSET_BIT);
uint32_t offset = (mem_address & OFFSET_MASK);
// uint32_t offset = mem_address & OFFSET_MASK;
if (emu->args.print_asm){
cout << " tag : "; print_masked_num(tag, TAG_BIT);
cout << " index : "; print_masked_num(index, INDEX_BIT);
cout << " offset : "; print_masked_num(offset, OFFSET_BIT);
}
if (emu->cache[index].tag == tag && emu->cache[index].valid){
emu->stats.cache_hit += 1ll;
return true;
}
else{
emu->stats.cache_miss += 1ll;
return false;
}
}
inline void cache_save(Emulator* emu, uint32_t mem_address) {
uint32_t tag = (mem_address & TAG_MASK) >> (INDEX_BIT + OFFSET_BIT);
uint32_t index = (mem_address & INDEX_MASK) >> (OFFSET_BIT);
uint32_t offset = (mem_address & OFFSET_MASK);
emu->cache[index].valid = true;
emu->cache[index].tag = tag;
uint32_t mask_for_start_index = 0xFFFFFFFF << OFFSET_BIT;
uint32_t start_ind = (mem_address & mask_for_start_index) >> 2;
for (int i=0; i<CACHE_LINE_SIZE; i++) {
emu->cache[index].data[i] = emu->memory[start_ind + i];
}
}
uint32_t bin2int(string bin);
uint32_t hex2int(string hex);
void init_emulator(Emulator* emu);
void destroy_emulator(Emulator* emu);
int load_instructions(Emulator* emu, string file_path);
void print_reg(Emulator* emu);
void print_reg_for_debug(Emulator* emu);
void print_mem(Emulator* emu, int start);
void output_image(Emulator* emu);
//bool cache_hit(Emulator* emu, uint32_t mem_address);
#endif