-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_mem.c
305 lines (281 loc) · 7.86 KB
/
sim_mem.c
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
/* 046267 Computer Architecture - Spring 2017 - HW #1 */
/* Main memory simulator implementation */
#include "sim_api.h"
uint32_t prog_start; // the addr of the code block
uint32_t data_start; // the addr of the data block
SIM_cmd instructions[100]; // where the instructions are kept
int32_t data[100]; // where the data is kept
uint32_t ticks; // the current clk tick
uint32_t read_tick; // the clk tick of the first attempt to read
typedef struct {
uint32_t addr;
int32_t val;
bool valid;
uint32_t ticks; // for LRU
} cache_line;
cache_line cache[8];
uint32_t get_start(char *line) {
line = strtok(line, "\n");
strtok(line, "@");
line = strtok(NULL, "@");
return (uint32_t) strtol(line, NULL, 0);
}
void get_data(char *line, int data_i) {
line = strtok(line, "\n");
data[data_i] = (int32_t) strtol(line, NULL, 0);
}
int get_dst(char *dst) {
strtok(dst, ",");
strtok(dst, "$");
dst = strtok(NULL, "$");
return atoi(dst);
}
int get_dst_br(char *dst) {
strtok(dst, "\n");
strtok(dst, "$");
dst = strtok(NULL, "$");
return atoi(dst);
}
int get_src1(char *src1) {
strtok(src1, ",");
src1 = strtok(NULL, ",");
strtok(src1, "$");
src1 = strtok(NULL, "$");
return atoi(src1);
}
int get_src2(char *src2) {
strtok(src2, ",");
strtok(NULL, ",");
src2 = strtok(NULL, ",");
strtok(src2, "$");
src2 = strtok(NULL, "$");
src2 = strtok(src2, "\n");
return atoi(src2);
}
int get_src2_imm(char *src2, int inst_num) {
strtok(src2, ",");
strtok(NULL, ",");
src2 = strtok(NULL, ",");
if (strchr(src2, '$') == NULL) {
strtok(src2, " ");
instructions[inst_num].isSrc2Imm = 1;
} else {
strtok(src2, "$");
src2 = strtok(NULL, "$");
//assert(instructions[inst_num].isSrc2Imm == 0);
}
src2 = strtok(src2, "\n");
if (strchr(src2, 'x') == NULL) {
return atoi(src2);
} else {
return (uint32_t) strtol(src2, NULL, 0);
}
}
void add_sub_branch(char *line, int inst_num) {
char dst[50];
memset(dst, '\0', sizeof(dst));
strcpy(dst, line);
instructions[inst_num].dst = get_dst(dst);
char src1[50];
memset(src1, '\0', sizeof(src1));
strcpy(src1, line);
instructions[inst_num].src1 = get_src1(src1);
char src2[50];
memset(src2, '\0', sizeof(src2));
strcpy(src2, line);
instructions[inst_num].src2 = get_src2_imm(src2, inst_num);
}
void halt(char *line, int inst_num) {
char dst[50];
memset(dst, '\0', sizeof(dst));
strcpy(dst, line);
instructions[inst_num].dst = get_dst(dst);
}
void load_store(char *line, int inst_num) {
char dst[50];
memset(dst, '\0', sizeof(dst));
strcpy(dst, line);
instructions[inst_num].dst = get_dst(dst);
char src1[50];
memset(src1, '\0', sizeof(src1));
strcpy(src1, line);
instructions[inst_num].src1 = get_src1(src1);
char src2[50];
memset(src2, '\0', sizeof(src2));
strcpy(src2, line);
instructions[inst_num].src2 = get_src2_imm(src2, inst_num);
}
void branch(char *line, int inst_num) {
char dst[50];
memset(dst, '\0', sizeof(dst));
strcpy(dst, line);
instructions[inst_num].dst = get_dst_br(dst);
}
void get_inst(char *line, int inst_num) {
char command[50];
memset(command, '\0', sizeof(command));
strcpy(command, line);
strtok(command, " ");
int opc = 0;
while (strcmp(command, cmdStr[opc]) != 0) {
++opc;
}
instructions[inst_num].opcode = opc;
switch (opc) {
case CMD_NOP: // NOP
break;
case CMD_ADD:
case CMD_SUB:
case CMD_ADDI:
case CMD_SUBI:
add_sub_branch(line, inst_num);
break;
case CMD_LOAD:
case CMD_STORE:
load_store(line, inst_num);
break;
case CMD_BR:
branch(line, inst_num);
break;
case CMD_BREQ:
case CMD_BRNEQ:
add_sub_branch(line, inst_num);
break;
case CMD_HALT:
halt(line, inst_num);
break;
}
}
int SIM_MemReset(const char *memImgFname) {
FILE *img = fopen(memImgFname, "r");
char line[1024];
if (img == 0) {
return -1; // can't open img file
}
while (fgets(line, 1024, img) != NULL) {
if (line[0] == '#' || line[0] == '\n') // comment or empty line
{
continue;
} else if (line[0] == 'I' && line[1] == '@') // start of code block
{
prog_start = get_start(line);
int inst = 0;
fgets(line, 1024, img);
// get next instructions
while (line[0] != '\n' && line[0] != '#' && line[0] != 'D') {
get_inst(line, inst);
++inst;
if (fgets(line, 1024, img) == NULL) //EOF
{
break;
}
}
} else if (line[0] == 'D' && line[1] == '@') // start of data block
{
data_start = get_start(line);
int data_i = 0;
fgets(line, 1024, img);
while (line[0] != '\n' && line[0] != '#' && line[0] != 'I') {
get_data(line, data_i);
++data_i;
if (fgets(line, 1024, img) == NULL) {
break;
}
}
}
}
fclose(img);
return 0;
}
void SIM_MemClkTick() {
++ticks;
}
int cache_lookup(uint32_t addr) {
int i;
for (i = 0; i < 8; ++i) {
if (cache[i].addr == addr) {
return i;
}
}
return -1;
}
void insert_to_cache(uint32_t addr) {
int i;
int addr_i = addr - data_start;
addr_i = addr_i / 4;
// insert if there is an empty space
for (i = 0; i < 8; ++i) {
if (cache[i].valid == 0) {
cache[i].addr = addr;
cache[i].val = data[addr_i];
cache[i].valid = 1;
cache[i].ticks = ticks;
return;
}
}
// no empty space, find LRU
int remove = -1;
int max_ticks = 0;
for (i = 0; i < 8; ++i) {
if ((ticks - cache[i].ticks) > max_ticks) {
max_ticks = (ticks - cache[i].ticks);
remove = i;
}
}
// insert instead of LRU
cache[remove].addr = addr;
cache[remove].val = data[addr_i];
cache[remove].ticks = ticks;
cache[remove].valid = 1;
}
int SIM_MemDataRead(uint32_t addr, int32_t *dst) {
// init read tick
if (read_tick == 0) {
read_tick = ticks;
}
// calc location in data array
int addr_i = addr - data_start;
addr_i = addr_i / 4;
// first attempt to read
if (read_tick == ticks) {
// find in cache
int i = cache_lookup(addr);
if (i != -1) {
//assert(cache[i].val == data[addr_i]);
*dst = cache[i].val;
cache[i].ticks = ticks;
//assert(cache[i].valid == 1);
read_tick = 0; // init for next read
return 0;
} else {
insert_to_cache(addr);
}
}
if ((ticks - read_tick) < 3) {
return -1;
}
*dst = data[addr_i];
read_tick = 0; // init for next read
return 0;
}
void SIM_MemDataWrite(uint32_t addr, int32_t val) {
int addr_i = addr - data_start;
addr_i = addr_i / 4; // addr is aligned to 4 byte
data[addr_i] = val;
int i = cache_lookup(addr);
// if it is in cache then update the cache
if (i != -1) {
cache[i].val = val;
cache[i].ticks = ticks; // for LRU
//assert(cache[i].valid == 1);
}
}
void SIM_MemInstRead(uint32_t addr, SIM_cmd *dst) {
addr = addr - prog_start;
addr = addr / 4; // addr is aligned to 4 byte
dst->opcode = instructions[addr].opcode;
dst->dst = instructions[addr].dst;
dst->src1 = instructions[addr].src1;
dst->src2 = instructions[addr].src2;
dst->isSrc2Imm = instructions[addr].isSrc2Imm;
}