-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.cpp
291 lines (277 loc) · 8 KB
/
memory.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
#include "./headers/memory.h"
extern clk_tick sys_clk;
extern memory main_mem;
extern config *CPU_cfg;
extern FU_CDB fCDB;
extern vector<int*> clk_wait_list;
extern instr_queue *instr_Q;
extern branchCtrl brcUnit;
extern ROB *CPU_ROB;
memory::memory(int sz)
{
buf = new memCell[sz];
for (int i = 0; i < sz; i++)
buf[i].f = 0.;
size = sz;
Lfront = Lrear = 0;
Sfront = Srear = 0;
LDQ_lock = SDQ_lock = PTHREAD_MUTEX_INITIALIZER;
}
memory::~memory()
{
delete []buf;
}
bool memory::store(LSQEntry entry)
{
ROBEntry *R = CPU_ROB->get_entry(entry.rob_i);
R->output.commit = R->output.mem = sys_clk.get_prog_cyc();
instr_timeline_output(R);
msg_log("Begin to store value to Addr="+to_string(entry.addr), 3);
for (int i = 0; ; i++)
{
msg_log("Storing value to Addr="+to_string(entry.addr), 3);
if (i == CPU_cfg->ld_str->mem_time - 1)
break;
at_falling_edge(mem_next_vdd);
if (i == 0)
CPU_ROB->ptr_advance();
at_rising_edge(mem_next_vdd);
}
buf[entry.addr] = entry.val;
at_falling_edge(mem_next_vdd);
if (CPU_cfg->ld_str->mem_time == 1) //In this case the ROB pointer was not advanced in the loop
CPU_ROB->ptr_advance();
Sfront = (++Sfront)%Q_LEN;
msg_log("Value stored ROB = " + to_string(entry.rob_i), 2);
return true;
}
bool memory::load(LSQEntry entry)
{
ROBEntry *R = CPU_ROB->get_entry(entry.rob_i);
int found = -1;
bool no_blocking_SD = true;
memCell ret;
for (int i = Srear; is_prev_index(Sfront, i, Sfront, Srear);)
{
i = (--i)%Q_LEN;
if (is_prev_index(Stor_Q[i].rob_i, entry.rob_i, CPU_ROB->get_front(), CPU_ROB->get_rear()))
{
if (Stor_Q[i].addr == -1)
{
no_blocking_SD = false;
break;
}
else
{
if (Stor_Q[i].addr == entry.addr && found == -1)
{
if (Stor_Q[i].ready)
found = i;
else
{
no_blocking_SD = false;
break;
}
}
}
}
}
if (no_blocking_SD)
{
R->output.mem = sys_clk.get_prog_cyc();
if (found >= 0)
{
msg_log("Forward store found, forwarding, Addr="+to_string(Stor_Q[found].addr), 3);
ret = Stor_Q[found].val;
}
else
{
msg_log("Begin LD, ROB = " + to_string(entry.rob_i), 3);
for (int i = 0; ; i++)
{
if (squash_p.flag)
{
squash_p.flag = false;
if (is_prev_index(brcUnit.squash_ROB_i(), entry.rob_i, squash_p.R_f, squash_p.R_r))
{
at_falling_edge(mem_next_vdd);
return true;
}
}
if (i == CPU_cfg->ld_str->mem_time - 1)
{
ret = buf[entry.addr];
break;
}
at_falling_edge(mem_next_vdd);
at_rising_edge(mem_next_vdd);
msg_log("Loading value from Addr="+to_string(entry.addr), 3);
}
}
fCDB.enQ(&ret, entry.rob_i);
at_falling_edge(mem_next_vdd);
Lfront = (++Lfront)%Q_LEN;
msg_log("Value loaded ROB = " + to_string(entry.rob_i), 2);
}
else
{
msg_log("Previous SD not ready, caller ROB = " + to_string(Load_Q[Lfront].rob_i), 3);
at_falling_edge(mem_next_vdd);
}
return true;
}
LSQEntry* memory::LD_enQ(int rbi, int addr)
{
pthread_mutex_lock(&LDQ_lock);
if ((Lrear+1)%Q_LEN == Lfront)
{
pthread_mutex_unlock(&LDQ_lock);
throw -2;
}
int index = Lrear;
Lrear = (++Lrear)%Q_LEN;
Load_Q[index].rob_i = rbi;
Load_Q[index].addr = addr;
Load_Q[index].code = LD;
Load_Q[index].ready = false;
pthread_mutex_unlock(&LDQ_lock);
return Load_Q + index;
}
LSQEntry* memory::SD_enQ(int rbi, int Qj, int addr, memCell val)
{
pthread_mutex_lock(&SDQ_lock);
if ((Srear+1)%Q_LEN == Sfront)
{
pthread_mutex_unlock(&SDQ_lock);
throw -1;
}
int index = Srear;
Srear = (++Srear)%Q_LEN;
Stor_Q[index].rob_i = rbi;
Stor_Q[index].addr = addr;
Stor_Q[index].SD_source = Qj;
Stor_Q[index].code = LD;
Stor_Q[index].val = val;
Stor_Q[index].ready = false;
pthread_mutex_unlock(&SDQ_lock);
return Stor_Q + index;
}
bool memory::setMem(valType type, int addr, void* val)
{
if (addr>=size){
err_log("Mem set out of range");
return false;
}
if (type == INTGR)
buf[addr].i = *(int*)val;
else
buf[addr].f = *(float*)val;
return true;
}
memCell memory::getMem(int addr)
{
return buf[addr];
}
bool is_prev_index(int i, int j, int front, int rear)
{
if (i == j) return false;
if (rear < front)
{
rear += CPU_ROB->size;
i += i<front?CPU_ROB->size:0;
j += j<front?CPU_ROB->size:0;
}
return j - i > 0;
}
void memory::squash(int ROB_i)
{
int R_f = CPU_ROB->get_front();
int R_r = CPU_ROB->get_rear();
pthread_mutex_lock(&LDQ_lock);
for (int i = Lrear; ;)
{
int j = (i-1)%Q_LEN;
if (j == Lfront || !is_prev_index(ROB_i, Load_Q[j].rob_i, R_f, R_r))
break;
Lrear = j;
i = (--i)%Q_LEN;
}
pthread_mutex_unlock(&LDQ_lock);
pthread_mutex_lock(&SDQ_lock);
for (int i = Srear; ;)
{
int j = (i-1)%Q_LEN;
if (j == Srear || !is_prev_index(ROB_i, Stor_Q[j].rob_i, R_f, R_r))
break;
Srear = j;
i = (--i)%Q_LEN;
}
pthread_mutex_unlock(&SDQ_lock);
msg_log("After squash, Store front = " + to_string(Sfront) + " rear = " + to_string(Srear), 3);
squash_p.R_f = CPU_ROB->get_front();
squash_p.R_r = CPU_ROB->get_rear();
squash_p.ROB_i = brcUnit.squash_ROB_i();
squash_p.flag = true;
}
void memory::SD_Q_automat()
{
SDQ_next_vdd = 0;
while (true)
{
at_rising_edge(SDQ_next_vdd);
pthread_mutex_lock(&SDQ_lock);
int CDB_i = fCDB.get_source();
for (int i = Sfront; i != Srear;)
{
if (Stor_Q[i].SD_source == CDB_i)
{
msg_log("SD queue entry " + to_string(i) + " got the value, source ROB = " + to_string(CDB_i), 3);
fCDB.get_val(&Stor_Q[i].val);
Stor_Q[i].SD_source = -1;
}
i = (++i)%Q_LEN;
}
pthread_mutex_unlock(&SDQ_lock);
at_falling_edge(SDQ_next_vdd);
for (int i = Sfront; i != Srear;)
{
if (Stor_Q[i].addr != -1)
{
Stor_Q[i].ready = true;
msg_log("Stor_Q entry i = " + to_string(i) + " ready, ROB = " + to_string(Stor_Q[Sfront].rob_i), 3);
}
i = (++i)%Q_LEN;
}
}
}
void memory::mem_automat()
{
mem_next_vdd = 0;
while (true)
{
at_rising_edge(mem_next_vdd);
if (Sfront == Srear && Lfront == Lrear && sys_clk.is_instr_ended())
sys_clk.end_mem();
if (squash_p.flag)
{
squash_p.flag = false;
at_falling_edge(mem_next_vdd);
}
else
{
if (Sfront != Srear && Stor_Q[Sfront].ready && CPU_ROB->get_front() == Stor_Q[Sfront].rob_i)
store(Stor_Q[Sfront]);
else if(Lfront != Lrear && Load_Q[Lfront].ready)
load(Load_Q[Lfront]);
else
at_falling_edge(mem_next_vdd);
}
}
}
void init_main_mem()
{
while(pthread_create(&main_mem.mem_handle, NULL, [](void *arg)->void*{main_mem.mem_automat(); return NULL;}, NULL));
while(pthread_create(&main_mem.SDQ_handle, NULL, [](void *arg)->void*{main_mem.SD_Q_automat(); return NULL;}, NULL));
clk_wait_list.push_back(&main_mem.mem_next_vdd);
clk_wait_list.push_back(&main_mem.SDQ_next_vdd);
}