-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtomasulo.cpp
More file actions
340 lines (340 loc) · 9.61 KB
/
tomasulo.cpp
File metadata and controls
340 lines (340 loc) · 9.61 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "command.cpp"
#include "ALU.cpp"
#include "LSB.cpp"
#include <algorithm>
bool end;
int fail,success,CLOCK;
int reg[32],rely[32],CLEAR;
int reg2[32],rely2[32];
struct information
{
int dest,vj,vk,qj,qk,op,oth;
information(){dest=vj=vk=qj=qk=oth=0;}
information(int dest,int vj,int vk,int qj,int qk,int op): dest(dest),vj(vj),vk(vk),qj(qj),qk(qk),op(op) {oth=0;}
void print()
{
fprintf(stderr,"dest:%d vj:%d vk:%d qj:%d qk:%d op:%d\n",dest,vj,vk,qj,qk,op);
}
};
information decode(int pc)
{
int dest=getDest(pc),vj=0,vk=0,qj=-1,qk=-1,op=getOp(pc);
char typ=getType1(pc);
if(typ=='R'||typ=='B')
{
qj=getRs1(pc),qk=getRs2(pc);
if(rely[qj]!=-1)
{
if(rely2[qj]==-1) vj=reg2[qj],qj=-1;
else qj=rely[qj];
}
else vj=reg[qj],qj=-1;
if(rely[qk]!=-1)
{
if(rely2[qk]==-1) vk=reg2[qk],qk=-1;
else qk=rely[qk];
}
else vk=reg[qk],qk=-1;
}
else if(typ=='I'||typ=='S')
{
qj=getRs1(pc);
if(rely[qj]!=-1)
{
if(rely2[qj]==-1) vj=reg2[qj],qj=-1;
else qj=rely[qj];
}
else vj=reg[qj],qj=-1;
vk=getImm(pc);
}
else if(typ=='U'||typ=='J') vj=getImm(pc);
information ret(dest,vj,vk,qj,qk,op);
if(typ=='B') ret.oth=bP.getPredict(pc);
return ret;
}
class ReorderBuffer
{
const static int N=32;
enum state {execute,ready,commit};
public:
struct Buffer
{
struct node
{
bool busy,wait;
int dest,val;
int st,pc,oth;
node(){busy=wait=false;dest=val=pc=oth=0;st=execute;}
node(int pc,int dest,int oth): pc(pc),dest(dest),oth(oth) {val=0,st=execute,busy=true,wait=false;}
} que[N];
int h,t;
Buffer(){h=t=0;}
bool full(){return h==t&&que[h].busy;}
void clear() {for(int i=0;i<N;i++) que[i]=node();h=t=0;}
bool isExecute(){return que[h].st==execute;}
bool isReady(){return que[h].st==ready;}
bool isCommit(){return que[h].st==commit;}
bool isWait(){return que[h].wait;}
void letWait(){que[h].wait=true;}
void push(int pc)
{
information inf=decode(pc);
que[t]=node(pc,inf.dest,inf.oth);
int typ=getType2(pc);
if(inf.op==none) que[t].val=inf.vj,que[t].st=ready;
else
{
if(typ==LB||typ==LH||typ==LW||typ==LBU||typ==LHU) RS.next.ins(0,inf.vj,inf.vk,inf.qj,inf.qk,inf.op,t);
else RS.next.ins(inf.dest,inf.vj,inf.vk,inf.qj,inf.qk,inf.op,t);
}
if(inf.dest) rely2[inf.dest]=t;
t=(t+1)%N;
}
int toppc(){return que[h].pc;}
int topid(){return h;}
int topv(){return que[h].val;}
int topd(){return que[h].dest;}
int topoth(){return que[h].oth;}
void pop()
{
if(que[h].dest&&rely[que[h].dest]==h&&rely2[que[h].dest]==h) rely2[que[h].dest]=-1;
//printf("%d\n",que[h].pc);
//for(int i=0;i<32;i++) printf("%d ",reg[i]);printf("\n");
que[h]=node();
h=(h+1)%N;
}
} now,next;
void nextClock(){now=next;}
void fetchALU()
{
int tmp=-1;
while((tmp=RS.now.findDone(tmp+1))!=-1)
{
int res=RS.now.getret(tmp),pos=RS.now.getid(tmp);
//if(res==4952) fprintf(stderr,"pc:%d ",now.que[pos].pc),decode(now.que[pos].pc).print();
RS.next.del(tmp);
next.que[pos].val=res;
next.que[pos].st++;
}
}
void fetchLSB()
{
int tmp=LSB.now.getpos();
while(LSB.now.isDone(tmp))
{
int res=LSB.now.getv(tmp),pos=LSB.now.getid(tmp),op=LSB.now.getop(tmp);
LSB.next.pop();
if(op<0)
{
next.que[pos].val=res;
next.que[pos].st++;
next.que[pos].wait=false;
}
tmp=LSB.now.nextpos(tmp);
}
}
} RoB;
void branchClear()
{
CLEAR=1;
for(int i=0;i<32;i++) rely2[i]=-1;
RoB.next.clear();
RS.next.clear();
alu.clear();
}
void fetch()
{
if(CLEAR||RoB.now.full()) return;
int pc=cQ.getCommand();
if(pc==-1) return;
RoB.next.push(pc);
}
void execute()
{
if(CLEAR) {mrw.calc();return;}
alu.calc();
mrw.calc();
RoB.fetchALU();
RoB.fetchLSB();
}
void commit()
{
if(RoB.now.isExecute()||RoB.now.isWait()) return;
int pc=RoB.now.toppc(),dest=RoB.now.topd(),val=RoB.now.topv();
//if(pc==4260) system("pause");
//printf("pc=%d val=%d ",pc,val),decode(pc).print();
if(getValxx(mem,pc,pc+3)==0x0ff00513) {end=true;return;}
int typ=getType2(pc);
switch(typ)
{
case LUI:if(dest) reg2[dest]=val,RS.broadcast(RoB.now.topid(),reg2[dest]);RoB.next.pop();break;
case AUIPC:if(dest) reg2[dest]=cQ.getpc()+val,RS.broadcast(RoB.now.topid(),reg2[dest]);RoB.next.pop();break;
case JAL:
{
if(dest) reg2[dest]=pc+4;
cQ.modify(pc+val);
branchClear();
break;
}
case JALR:
{
if(dest) reg2[dest]=pc+4;
cQ.modify(val);
branchClear();
break;
}
case BEQ:
case BNE:
case BLT:
case BGE:
case BLTU:
case BGEU:
{
int is=RoB.now.topoth();
if(is!=val)
{
fail++;
if(val) cQ.modify(pc+getImm(pc));
else cQ.modify(pc+4);
bP.changeFreq(val,pc);
branchClear();
}
else success++,bP.changeFreq(val,pc),RoB.next.pop();
break;
}
case LB:
{
if(RoB.now.isReady()) LSB.next.push(val,-1,0,RoB.now.topid()),RoB.next.letWait();
else
{
if(dest) reg2[dest]=sext(val,8),RS.broadcast(RoB.now.topid(),reg2[dest]);
RoB.next.pop();
}
break;
}
case LH:
{
if(RoB.now.isReady()) LSB.next.push(val,-2,0,RoB.now.topid()),RoB.next.letWait();
else
{
if(dest) reg2[dest]=sext(val,16),RS.broadcast(RoB.now.topid(),reg2[dest]);
RoB.next.pop();
}
break;
}
case LW:
{
if(RoB.now.isReady()) LSB.next.push(val,-4,0,RoB.now.topid()),RoB.next.letWait();
else
{
if(dest) reg2[dest]=val,RS.broadcast(RoB.now.topid(),reg2[dest]);
RoB.next.pop();
}
break;
}
case LBU:
{
if(RoB.now.isReady()) LSB.next.push(val,-1,0,RoB.now.topid()),RoB.next.letWait();
else
{
if(dest) reg2[dest]=val,RS.broadcast(RoB.now.topid(),reg2[dest]);
RoB.next.pop();
}
break;
}
case LHU:
{
if(RoB.now.isReady()) LSB.next.push(val,-2,0,RoB.now.topid()),RoB.next.letWait();
else
{
if(dest) reg2[dest]=val,RS.broadcast(RoB.now.topid(),reg2[dest]);
RoB.next.pop();
}
break;
}
case SB:
{
LSB.next.push(val,1,reg[getRs2(pc)]&255,RoB.now.topid());
RoB.next.pop();
break;
}
case SH:
{
LSB.next.push(val,2,reg[getRs2(pc)]&65535,RoB.now.topid());
RoB.next.pop();
break;
}
case SW:
{
LSB.next.push(val,4,reg[getRs2(pc)],RoB.now.topid());
RoB.next.pop();
break;
}
case ADDI:
case SLTI:
case SLTIU:
case XORI:
case ORI:
case ANDI:
case SLLI:
case SRLI:
case SRAI:
case ADD:
case SUB:
case SLL:
case SLT:
case SLTU:
case XOR:
case SRL:
case SRA:
case OR:
case AND:if(dest) reg2[dest]=val,RS.broadcast(RoB.now.topid(),reg2[dest]);RoB.next.pop();break;
}
}
void nextClock()
{
CLOCK++;
CLEAR=0;
for(int i=0;i<32;i++) reg[i]=reg2[i],rely[i]=rely2[i];
RoB.nextClock();
RS.nextClock();
alu.nextClock();
LSB.nextClock();
mrw.nextClock();
cQ.nextClock();
}
int op[3]={0,1,2};
char FILENAME[18][50]={"testcases/array_test1.data","testcases/array_test2.data","testcases/basicopt1.data","testcases/bulgarian.data","testcases/expr.data",
"testcases/gcd.data","testcases/hanoi.data","testcases/lvalue2.data","testcases/magic.data","testcases/manyarguments.data","testcases/multiarray.data",
"testcases/naive.data","testcases/pi.data","testcases/qsort.data","testcases/queens.data","testcases/statement_test.data","testcases/superloop.data","testcases/tak.data"};
int main()
{
//freopen(FILENAME[12],"r",stdin);
//freopen("tomasulo.out","w",stdout);
CLOCK=0;
memset(mem,0,sizeof(mem));
memset(reg,0,sizeof(reg));
memset(reg2,0,sizeof(reg2));
memset(rely,-1,sizeof(rely));
memset(rely2,-1,sizeof(rely2));
getAllCommands();
while(1)
{
std::random_shuffle(op,op+3);
for(int i=0;i<3;i++)
{
if(op[i]==0) fetch();
if(op[i]==1) execute();
if(op[i]==2) commit();
}
nextClock();
if(end)
{
printf("%u\n",((unsigned int)reg[10])&255u);
//printf("%d %d %d %f\n",CLOCK,success,fail,success/1.0/(fail+success));
break;
}
//printf("%u\n",reg[10]);
}
return 0;
}