Skip to content

Commit 55f56a1

Browse files
committed
commit
1 parent 2fd0c36 commit 55f56a1

11 files changed

Lines changed: 1132 additions & 0 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CMakeCache.txt
2+
CMakeFiles/
3+
testcases/
4+
sample/
5+
Makefile
6+
cmake_install.cmake
7+
code

ALU.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "parser.cpp"
2+
class ReservationStationALU
3+
{
4+
const static int N=100;
5+
enum state {wait,calc,done};
6+
public:
7+
struct ReservationStation
8+
{
9+
int bid,bva;
10+
struct node
11+
{
12+
bool busy;
13+
int dest,vj,vk,qj,qk,op,id,ret;
14+
int st;
15+
node(){busy=false;dest=vj=vk=op=ret=id=0;qj=qk=-1;st=wait;}
16+
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;}
17+
} rs[N];
18+
ReservationStation(){bid=-2,bva=0;}
19+
void clear()
20+
{
21+
for(int i=0;i<N;i++) rs[i]=node();
22+
}
23+
void ins(int dest,int vj,int vk,int qj,int qk,int op,int id)
24+
{
25+
for(int i=0;i<N;i++)
26+
{
27+
if(!rs[i].busy)
28+
{
29+
rs[i]=node(dest,vj,vk,qj,qk,op,id);
30+
if(rs[i].qj==bid) rs[i].qj=-1,rs[i].vj=bva;
31+
if(rs[i].qk==bid) rs[i].qk=-1,rs[i].vk=bva;
32+
return;
33+
}
34+
}
35+
}
36+
void del(int pos) {rs[pos]=node();}
37+
int findReady()
38+
{
39+
for(int i=0;i<N;i++)
40+
{
41+
if(!rs[i].busy||rs[i].st!=wait) continue;
42+
if(rs[i].qj==-1&&rs[i].qk==-1) return i;
43+
}
44+
return -1;
45+
}
46+
int findDone(int st)
47+
{
48+
for(int i=st;i<N;i++)
49+
{
50+
if(!rs[i].busy) continue;
51+
if(rs[i].st==done) return i;
52+
}
53+
return -1;
54+
}
55+
int getx(int pos){return rs[pos].vj;}
56+
int gety(int pos){return rs[pos].vk;}
57+
int getop(int pos){return rs[pos].op;}
58+
int getid(int pos){return rs[pos].id;}
59+
int getret(int pos){return rs[pos].ret;}
60+
void nextState(int pos){rs[pos].st++;}
61+
} now,next;
62+
void nextClock(){now=next;next.bid=-2,next.bva=0;}
63+
void broadcast(int id,int va)
64+
{
65+
next.bid=id,next.bva=va;
66+
for(int i=0;i<N;i++)
67+
{
68+
if(next.rs[i].qj==id) next.rs[i].qj=-1,next.rs[i].vj=va;
69+
if(next.rs[i].qk==id) next.rs[i].qk=-1,next.rs[i].vk=va;
70+
}
71+
}
72+
void prepare(int pos,int va)
73+
{
74+
next.rs[pos].ret=va;
75+
next.rs[pos].st=done;
76+
if(next.rs[pos].dest) broadcast(next.rs[pos].id,va);
77+
}
78+
} RS;
79+
class ALU
80+
{
81+
int rem,res,pos;
82+
int rem2,res2,pos2;
83+
public:
84+
ALU(){rem=res=rem2=res2=0;pos=pos2=-1;}
85+
void clear() {rem2=res2=0;pos2=-1;}
86+
void nextClock(){rem=rem2,res=res2,pos=pos2;}
87+
bool busy(){return rem>0;}
88+
void calc()
89+
{
90+
if(busy())
91+
{
92+
prompt();
93+
return;
94+
}
95+
int tmp=RS.now.findReady();
96+
if(tmp==-1) return;
97+
int x=RS.now.getx(tmp),y=RS.now.gety(tmp),op=RS.now.getop(tmp);
98+
RS.next.nextState(tmp);
99+
rem2=1;pos2=tmp;
100+
switch(op)
101+
{
102+
case eq:res2=(x==y);break;
103+
case neq:res2=(x!=y);break;
104+
case le:res2=(x<y);break;
105+
case geq:res2=(x>=y);break;
106+
case leu:
107+
{
108+
unsigned int tx=x,ty=y;
109+
res2=(tx<ty);break;
110+
}
111+
case gequ:
112+
{
113+
unsigned int tx=x,ty=y;
114+
res2=(tx>=ty);break;
115+
}
116+
case add:res2=(x+y);break;
117+
case xori:res2=(x^y);break;
118+
case ori:res2=(x|y);break;
119+
case andi:res2=(x&y);break;
120+
case slli:res2=(x<<(y&31));break;
121+
case srli:
122+
{
123+
unsigned int tx=x;
124+
res2=(tx>>(y&31));break;
125+
}
126+
case srai:res2=(x>>(y&31));break;
127+
case sub:res2=(x-y);break;
128+
case mul:res2=(x*y);break;
129+
case dv:res2=(x/y);break;
130+
}
131+
prompt2();
132+
}
133+
void prompt()
134+
{
135+
if(!rem) return;
136+
rem2=rem-1;
137+
if(!rem2) RS.prepare(pos,res);
138+
}
139+
void prompt2()
140+
{
141+
if(!rem2) return;
142+
rem2--;
143+
if(!rem2) RS.prepare(pos2,res2);
144+
}
145+
} alu;

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(RISC_V)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
6+
add_executable(code naive.cpp)

LSB.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "parser.cpp"
2+
3+
class LoadStoreBuffer
4+
{
5+
const static int N=100;
6+
enum state {wait,calc,done};
7+
public:
8+
struct Buffer
9+
{
10+
struct node
11+
{
12+
bool busy;
13+
int dest,op,val,id;
14+
int st;
15+
node(){busy=false;dest=op=val=id=0;st=wait;}
16+
node(int dest,int op,int val,int id): dest(dest),op(op),val(val),id(id) {busy=true;st=wait;}
17+
} que[N];
18+
int h,t;
19+
Buffer() {h=t=0;}
20+
bool full(){return h==t&&que[h].busy==true;}
21+
bool empty(){return h==t&&que[h].busy==false;}
22+
void nextState(int pos){que[pos].st++;}
23+
bool isDone(int pos){return que[pos].st==done;}
24+
int getpos(){return h;}
25+
int nextpos(int now){return (now+1)%N;}
26+
int getdir(int pos){return que[pos].dest;}
27+
int getop(int pos){return que[pos].op;}
28+
int getv(int pos){return que[pos].val;}
29+
int getid(int pos){return que[pos].id;}
30+
int findNext(int pos)
31+
{
32+
if((h+1)%N!=t) return (h+1)%N;
33+
else return -1;
34+
}
35+
void pop()
36+
{
37+
que[h]=node();
38+
h=(h+1)%N;
39+
}
40+
void push(int dest,int op,int val,int id)
41+
{
42+
que[t]=node(dest,op,val,id);
43+
t=(t+1)%N;
44+
}
45+
} now,next;
46+
void nextClock(){now=next;}
47+
void prepare(int pos,int va)
48+
{
49+
next.que[pos].val=va;
50+
next.que[pos].st=done;
51+
}
52+
} LSB;
53+
class MemoryReadWriter
54+
{
55+
int rem,op,pos,dir,val;
56+
int rem2,op2,pos2,dir2,val2;
57+
public:
58+
MemoryReadWriter(){rem=op=pos=dir=val=rem2=op2=pos2=dir2=val2=0;}
59+
void nextClock(){rem=rem2,op=op2,pos=pos2,dir=dir2,val=val2;}
60+
bool busy(){return rem>0;}
61+
void calc()
62+
{
63+
if(busy())
64+
{
65+
prompt();
66+
return;
67+
}
68+
if(LSB.now.empty()) return;
69+
while(pos2!=LSB.now.t&&LSB.now.isDone(pos2)) pos2=LSB.now.nextpos(pos2);
70+
if(pos2==LSB.now.t) return;
71+
rem2=3;op2=LSB.now.getop(pos2);dir2=LSB.now.getdir(pos2);val2=LSB.now.getv(pos2);
72+
LSB.next.nextState(pos2);
73+
prompt2();
74+
}
75+
void prompt()
76+
{
77+
if(!rem) return;
78+
rem2=rem-1;
79+
if(!rem2)
80+
{
81+
dir%=MEM_MAX;
82+
if(op>0) for(int i=0;i<op;i++) mem[dir+i]=val>>(i*8)&255;
83+
else val2=getValxx(mem,dir,dir-op-1);
84+
LSB.prepare(pos,val2);
85+
}
86+
}
87+
void prompt2()
88+
{
89+
if(!rem2) return;
90+
rem2--;
91+
if(!rem2)
92+
{
93+
dir%=MEM_MAX;
94+
if(op>0) for(int i=0;i<op;i++) mem[dir+i]=val>>(i*8)&255;
95+
else val2=getValxx(mem,dir,dir-op-1);
96+
LSB.prepare(pos,val2);
97+
}
98+
}
99+
} mrw;

command.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include "parser.cpp"
4+
enum counter{stronglyNotTaken,weaklyNotTaken,weaklyTaken,stronglyTaken};
5+
class BranchPredictor
6+
{
7+
const static int N=5000,MOD=307;
8+
int freq[N];
9+
int getHash(int pc){return 1ll*pc*MOD%N;}
10+
public:
11+
BranchPredictor()
12+
{
13+
for(int i=0;i<N;i++) freq[i]=weaklyNotTaken;
14+
}
15+
bool getPredict(int pc)
16+
{
17+
int id=getHash(pc);
18+
if(freq[id]==weaklyTaken||freq[id]==stronglyTaken) return true;
19+
else return false;
20+
}
21+
void changeFreq(bool is,int pc)
22+
{
23+
int id=getHash(pc);
24+
if(is&&freq[id]!=stronglyTaken) freq[id]++;
25+
if(!is&&freq[id]!=stronglyNotTaken) freq[id]--;
26+
}
27+
} bP;
28+
class CommandQueue
29+
{
30+
bool used;
31+
int pc,pc2;
32+
public:
33+
CommandQueue(){pc=pc2=0;}
34+
void nextClock(){pc=pc2;used=false;}
35+
void modify(int v){pc2=v;used=true;}
36+
int getpc(){return pc;}
37+
int getCommand()
38+
{
39+
int ret=pc;
40+
if(used||getType1(pc)=='?') return -1;
41+
if(getType1(pc)=='B')
42+
{
43+
if(bP.getPredict(pc)) pc2+=getImm(pc);
44+
else pc2+=4;
45+
}
46+
else pc2+=4;
47+
return ret;
48+
}
49+
} cQ;

naive

49.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)