-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU.cpp
More file actions
156 lines (156 loc) · 4.34 KB
/
ALU.cpp
File metadata and controls
156 lines (156 loc) · 4.34 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
#include "parser.cpp"
class ReservationStationALU
{
const static int N=32,cdbSIZE=2;
enum state {wait,calc,done};
public:
struct ReservationStation
{
int bid[cdbSIZE],bva[cdbSIZE];
struct node
{
bool busy;
int dest,vj,vk,qj,qk,op,id,ret;
int st;
node(){busy=false;dest=vj=vk=op=ret=id=0;qj=qk=-1;st=wait;}
node(int dest,int vj,int vk,int qj,int qk,int op,int id): dest(dest),vj(vj),vk(vk),qj(qj),qk(qk),op(op),id(id) {busy=true;st=wait;ret=0;}
} rs[N];
ReservationStation()
{
for(int i=0;i<cdbSIZE;i++) bid[i]=-2,bva[i]=0;
}
void clear()
{
for(int i=0;i<N;i++) rs[i]=node();
for(int i=0;i<cdbSIZE;i++) bid[i]=-2,bva[i]=0;
}
void ins(int dest,int vj,int vk,int qj,int qk,int op,int id)
{
for(int i=0;i<N;i++)
{
if(!rs[i].busy)
{
rs[i]=node(dest,vj,vk,qj,qk,op,id);
for(int j=0;j<cdbSIZE;j++) if(rs[i].qj==bid[j]) rs[i].qj=-1,rs[i].vj=bva[j];
for(int j=0;j<cdbSIZE;j++) if(rs[i].qk==bid[j]) rs[i].qk=-1,rs[i].vk=bva[j];
return;
}
}
}
void del(int pos) {rs[pos]=node();}
int findReady()
{
for(int i=0;i<N;i++)
{
if(!rs[i].busy||rs[i].st!=wait) continue;
if(rs[i].qj==-1&&rs[i].qk==-1) return i;
}
return -1;
}
int findDone(int st)
{
for(int i=st;i<N;i++)
{
if(!rs[i].busy) continue;
if(rs[i].st==done) return i;
}
return -1;
}
int getx(int pos){return rs[pos].vj;}
int gety(int pos){return rs[pos].vk;}
int getop(int pos){return rs[pos].op;}
int getid(int pos){return rs[pos].id;}
int getret(int pos){return rs[pos].ret;}
void nextState(int pos){rs[pos].st++;}
} now,next;
void nextClock()
{
now=next;
for(int i=0;i<cdbSIZE;i++) next.bid[i]=-2,next.bva[i]=0;
}
void broadcast(int id,int va)
{
for(int i=0;i<cdbSIZE;i++)
{
if(next.bid[i]!=-2) continue;
next.bid[i]=id,next.bva[i]=va;
break;
}
for(int i=0;i<N;i++)
{
if(next.rs[i].qj==id) next.rs[i].qj=-1,next.rs[i].vj=va;
if(next.rs[i].qk==id) next.rs[i].qk=-1,next.rs[i].vk=va;
}
}
void prepare(int pos,int va)
{
next.rs[pos].ret=va;
next.rs[pos].st=done;
if(next.rs[pos].dest) broadcast(next.rs[pos].id,va);
}
} RS;
class ALU
{
int rem,res,pos;
int rem2,res2,pos2;
public:
ALU(){rem=res=rem2=res2=0;pos=pos2=-1;}
void clear() {rem2=res2=0;pos2=-1;}
void nextClock(){rem=rem2,res=res2,pos=pos2;}
bool busy(){return rem>0;}
void calc()
{
if(busy())
{
prompt();
return;
}
int tmp=RS.now.findReady();
if(tmp==-1) return;
int x=RS.now.getx(tmp),y=RS.now.gety(tmp),op=RS.now.getop(tmp);
RS.next.nextState(tmp);
rem2=1;pos2=tmp;
switch(op)
{
case eq:res2=(x==y);break;
case neq:res2=(x!=y);break;
case le:res2=(x<y);break;
case geq:res2=(x>=y);break;
case leu:
{
unsigned int tx=x,ty=y;
res2=(tx<ty);break;
}
case gequ:
{
unsigned int tx=x,ty=y;
res2=(tx>=ty);break;
}
case add:res2=(x+y);break;
case xori:res2=(x^y);break;
case ori:res2=(x|y);break;
case andi:res2=(x&y);break;
case slli:res2=(x<<(y&31));break;
case srli:
{
unsigned int tx=x;
res2=(tx>>(y&31));break;
}
case srai:res2=(x>>(y&31));break;
case sub:res2=(x-y);break;
}
prompt2();
}
void prompt()
{
if(!rem) return;
rem2=rem-1;
if(!rem2) RS.prepare(pos,res);
}
void prompt2()
{
if(!rem2) return;
rem2--;
if(!rem2) RS.prepare(pos2,res2);
}
} alu;