-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSB.cpp
More file actions
99 lines (98 loc) · 2.7 KB
/
LSB.cpp
File metadata and controls
99 lines (98 loc) · 2.7 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
#include "parser.cpp"
class LoadStoreBuffer
{
const static int N=32;
enum state {wait,calc,done};
public:
struct Buffer
{
struct node
{
bool busy;
int dest,op,val,id;
int st;
node(){busy=false;dest=op=val=id=0;st=wait;}
node(int dest,int op,int val,int id): dest(dest),op(op),val(val),id(id) {busy=true;st=wait;}
} que[N];
int h,t;
Buffer() {h=t=0;}
bool full(){return h==t&&que[h].busy==true;}
bool empty(){return h==t&&que[h].busy==false;}
void nextState(int pos){que[pos].st++;}
bool isDone(int pos){return que[pos].st==done;}
int getpos(){return h;}
int nextpos(int now){return (now+1)%N;}
int getdir(int pos){return que[pos].dest;}
int getop(int pos){return que[pos].op;}
int getv(int pos){return que[pos].val;}
int getid(int pos){return que[pos].id;}
int findNext(int pos)
{
if((h+1)%N!=t) return (h+1)%N;
else return -1;
}
void pop()
{
que[h]=node();
h=(h+1)%N;
}
void push(int dest,int op,int val,int id)
{
que[t]=node(dest,op,val,id);
t=(t+1)%N;
}
} now,next;
void nextClock(){now=next;}
void clear(){now=next=Buffer();}
void prepare(int pos,int va)
{
next.que[pos].val=va;
next.que[pos].st=done;
}
} LSB;
class MemoryReadWriter
{
int rem,op,pos,dir,val;
int rem2,op2,pos2,dir2,val2;
public:
MemoryReadWriter(){rem=op=pos=dir=val=rem2=op2=pos2=dir2=val2=0;}
void nextClock(){rem=rem2,op=op2,pos=pos2,dir=dir2,val=val2;}
void clear(){rem=op=pos=dir=val=rem2=op2=pos2=dir2=val2=0;}
bool busy(){return rem>0;}
void calc()
{
if(busy())
{
prompt();
return;
}
if(LSB.now.empty()) return;
while(pos2!=LSB.now.t&&LSB.now.isDone(pos2)) pos2=LSB.now.nextpos(pos2);
if(pos2==LSB.now.t) return;
rem2=3;op2=LSB.now.getop(pos2);dir2=LSB.now.getdir(pos2);val2=LSB.now.getv(pos2);
LSB.next.nextState(pos2);
prompt2();
}
void prompt()
{
if(!rem) return;
rem2=rem-1;
if(!rem2)
{
if(op>0) for(int i=0;i<op;i++) mem[dir+i]=val>>(i*8)&255;
else val2=getValxx(mem,dir,dir-op-1);
LSB.prepare(pos,val2);
}
}
void prompt2()
{
if(!rem2) return;
rem2--;
if(!rem2)
{
if(op>0) for(int i=0;i<op;i++) mem[dir+i]=val>>(i*8)&255;
else val2=getValxx(mem,dir,dir-op-1);
LSB.prepare(pos,val2);
}
}
} mrw;