-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy.cpp
More file actions
291 lines (275 loc) · 9 KB
/
easy.cpp
File metadata and controls
291 lines (275 loc) · 9 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
#include<bits/stdc++.h>
using namespace std;
int Clock=0;
unsigned int reg[32],pc;
unsigned char mem[500000];
unsigned int Get0x(char *s,int l,int r){
unsigned int ans=0;
for(int i=l;i<=r;i++){
if('0'<=s[i]&&s[i]<='9')ans=(ans<<4)|(s[i]-'0');
else ans=(ans<<4)|(s[i]-'A'+10);
}
return ans;
}
void GetData(){
char tmp[20];
int pos;
while(~scanf("%s",tmp)){
if(tmp[0]=='@')pos=Get0x(tmp,1,strlen(tmp)-1);
else {
mem[pos]=Get0x(tmp,0,strlen(tmp)-1);
// printf("! %d %x\n",pos,mem[pos]);
pos++;
}
}
// exit(0);
}
enum Ordertype{
LUI,AUIPC, //U类型 用于操作长立即数的指令 0~1
ADD,SUB,SLL,SLT,SLTU,XOR,SRL,SRA,OR,AND, //R类型 寄存器间操作指令 2~11
JALR,LB,LH,LW,LBU,LHU,ADDI,SLTI,SLTIU,XORI,ORI,ANDI,SLLI,SRLI,SRAI, //I类型 短立即数和访存Load操作指令 12~26
SB,SH,SW, //S类型 访存Store操作指令 27~29
JAL, //J类型 用于无条件跳转的指令 30
BEQ,BNE,BLT,BGE,BLTU,BGEU, //B类型 用于有条件跳转的指令 31~36
END
};
string GGG[]={"LUI","AUIPC",
"ADD","SUB","SLL","SLT","SLTU","XOR","SRL","SRA","OR","AND",
"JALR","LB","LH","LW","LBU","LHU","ADDI","SLTI","SLTIU","XORI","ORI","ANDI","SLLI","SRLI","SRAI",
"SB","SH","SW",
"JAL",
"BEQ","BNE","BLT","BGE","BLTU","BGEU",
"END"};
class Order{
public:
Ordertype type;
unsigned int rd,rs1,rs2,imm;
Order(){}
Order(Ordertype _type,unsigned int _rd,unsigned int _rs1,unsigned int _rs2, unsigned int _imm):type(_type),rd(_rd),rs1(_rs1),rs2(_rs2),imm(_imm){}
};
Order Decode(int pos){
unsigned int s=(unsigned int)mem[pos]+((unsigned int)mem[pos+1]<<8)+((unsigned int)mem[pos+2]<<16)+((unsigned int)mem[pos+3]<<24);
Order order;
if(s==(int)0x0ff00513){
order.type=END;
return order;
}
int type1=s&0x7f;
int type2=(s>>12)&0x7;
int type3=(s>>25)&0x7f;
order.rd=(s>>7)&0x1f;
order.rs1=(s>>15)&0x1f;
order.rs2=(s>>20)&0x1f;
// printf("%x %x %x %x %x %x\n",type3,order.rs2,order.rs1,type2,order.rd,type1);
if(type1==0x37||type1==0x17){//U类型
if(type1==0x37)order.type=LUI;
if(type1==0x17)order.type=AUIPC;
order.imm=(s>>12)<<12;
}
if(type1==0x33){//R类型
if(type2==0x0){
if(type3==0x00)order.type=ADD;
if(type3==0x20)order.type=SUB;
}
if(type2==0x1)order.type=SLL;
if(type2==0x2)order.type=SLT;
if(type2==0x3)order.type=SLTU;
if(type2==0x4)order.type=XOR;
if(type2==0x5){
if(type3==0x00)order.type=SRL;
if(type3==0x20)order.type=SRA;
}
if(type2==0x6)order.type=OR;
if(type2==0x7)order.type=AND;
}
if(type1==0x67||type1==0x03||type1==0x13){//I类型
if(type1==0x67)order.type=JALR;
if(type1==0x03){
if(type2==0x0)order.type=LB;
if(type2==0x1)order.type=LH;
if(type2==0x2)order.type=LW;
if(type2==0x4)order.type=LBU;
if(type2==0x5)order.type=LHU;
}
if(type1==0x13){
if(type2==0x0)order.type=ADDI;
if(type2==0x2)order.type=SLTI;
if(type2==0x3)order.type=SLTIU;
if(type2==0x4)order.type=XORI;
if(type2==0x6)order.type=ORI;
if(type2==0x7)order.type=ANDI;
if(type2==0x1)order.type=SLLI;
if(type2==0x5){
if(type3==0x00)order.type=SRLI;
if(type3==0x20)order.type=SRAI;
}
}
order.imm=(s>>20);
}
if(type1==0x23){//S类型
if(type2==0x0)order.type=SB;
if(type2==0x1)order.type=SH;
if(type2==0x2)order.type=SW;
order.imm=((s>>25)<<5) | ((s>>7)&0x1f);
}
if(type1==0x6f){//J类型
order.type=JAL;
order.imm=(((s>>12)&0xff)<<12) | (((s>>20)&0x1)<<11) | (((s>>21)&0x3ff)<<1) | (((s>>31)&1)<<20);
}
if(type1==0x63){//B类型
if(type2==0x0)order.type=BEQ;
if(type2==0x1)order.type=BNE;
if(type2==0x4)order.type=BLT;
if(type2==0x5)order.type=BGE;
if(type2==0x6)order.type=BLTU;
if(type2==0x7)order.type=BGEU;
order.imm=(((s>>7)&0x1)<<11) | (((s>>8)&0xf)<<1) | (((s>>25)&0x3f)<<5) | (((s>>31)&1)<<12);
}
if(order.type==JALR||order.type==LB||order.type==LH||order.type==LW||order.type==LBU||order.type==LHU){
if(order.imm>>11)order.imm|=0xfffff000;
}
if(order.type==ADDI||order.type==SLTI||order.type==SLTIU||order.type==XORI||order.type==ORI||order.type==ANDI){
if(order.imm>>11)order.imm|=0xfffff000;
}
if(order.type==SB||order.type==SH||order.type==SW){
if(order.imm>>11)order.imm|=0xfffff000;
}
if(order.type==JAL){
if(order.imm>>20)order.imm|=0xfff00000;
}
if(order.type==BEQ||order.type==BNE||order.type==BLT||order.type==BGE||order.type==BLTU||order.type==BGEU){
if(order.imm>>12)order.imm|=0xffffe000;
}
// if(Clock>=6135){
// cout<<"!!!"<<pos<<endl;
// printf("%x\n",s);
// cout<<GGG[order.type]<<endl;
// printf("%u %u %u %u\n",order.rd,order.rs1,order.rs2,order.imm);
// }
return order;
}
void work(Order order){
// printf("%x %d\n",pc,order.type);
if(order.type==LUI)reg[order.rd]=order.imm,pc+=4;
if(order.type==AUIPC)reg[order.rd]=pc+order.imm,pc+=4;
if(order.type==ADD)reg[order.rd]=reg[order.rs1]+reg[order.rs2],pc+=4;
if(order.type==SUB)reg[order.rd]=reg[order.rs1]-reg[order.rs2],pc+=4;
if(order.type==SLL)reg[order.rd]=reg[order.rs1]<<(reg[order.rs2]&0x1f),pc+=4;
if(order.type==SLT)reg[order.rd]=((int)reg[order.rs1]<(int)reg[order.rs2])?1:0,pc+=4;
if(order.type==SLTU)reg[order.rd]=(reg[order.rs1]<reg[order.rs2])?1:0,pc+=4;
if(order.type==XOR)reg[order.rd]=reg[order.rs1]^reg[order.rs2],pc+=4;
if(order.type==SRL)reg[order.rd]=reg[order.rs1]>>(reg[order.rs2]&0x1f),pc+=4;
if(order.type==SRA)reg[order.rd]=(int)reg[order.rs1]>>(reg[order.rs2]&0x1f),pc+=4;
if(order.type==OR)reg[order.rd]=reg[order.rs1]|reg[order.rs2],pc+=4;
if(order.type==AND)reg[order.rd]=reg[order.rs1]®[order.rs2],pc+=4;
if(order.type==JALR){
unsigned int t=pc+4;
// cout<<"$$$$"<<reg[order.rs1]<<" "<<order.imm<<endl;
pc=(reg[order.rs1]+order.imm)&(~1);
reg[order.rd]=t;
}
if(order.type==LB){
int pos=reg[order.rs1]+order.imm;
reg[order.rd]=(char)mem[pos];
pc+=4;
}
if(order.type==LH){
int pos=reg[order.rs1]+order.imm;
reg[order.rd]=(short)((unsigned short)mem[pos]+((unsigned short)mem[pos+1]<<8));
pc+=4;
}
if(order.type==LW){
int pos=reg[order.rs1]+order.imm;
reg[order.rd]=(int)((unsigned int)mem[pos]+((unsigned int)mem[pos+1]<<8)+((unsigned int)mem[pos+2]<<16)+((unsigned int)mem[pos+3]<<24));
pc+=4;
}
if(order.type==LBU){
int pos=reg[order.rs1]+order.imm;
reg[order.rd]=mem[pos];
pc+=4;
}
if(order.type==LHU){
int pos=reg[order.rs1]+order.imm;
reg[order.rd]=(unsigned short)mem[pos]+((unsigned short)mem[pos+1]<<8);
pc+=4;
}
if(order.type==ADDI)reg[order.rd]=reg[order.rs1]+order.imm,pc+=4;
if(order.type==SLTI)reg[order.rd]=((int)reg[order.rs1]<(int)order.imm)?1:0,pc+=4;
if(order.type==SLTIU)reg[order.rd]=(reg[order.rs1]<order.imm)?1:0,pc+=4;
if(order.type==XORI)reg[order.rd]=reg[order.rs1]^order.imm,pc+=4;
if(order.type==ORI)reg[order.rd]=reg[order.rs1]|order.imm,pc+=4;
if(order.type==ANDI)reg[order.rd]=reg[order.rs1]&order.imm,pc+=4;
if(order.type==SLLI)reg[order.rd]=reg[order.rs1]<<order.imm,pc+=4;
if(order.type==SRLI)reg[order.rd]=reg[order.rs1]>>order.imm,pc+=4;
if(order.type==SRAI)reg[order.rd]=(int)reg[order.rs1]>>order.imm,pc+=4;
if(order.type==SB){
int pos=reg[order.rs1]+order.imm;
// cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Store "<<pos<<" "<<reg[order.rs2]<<endl;
mem[pos]=reg[order.rs2]&0xff;
pc+=4;
}
if(order.type==SH){
int pos=reg[order.rs1]+order.imm;
// cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Store "<<pos<<" "<<reg[order.rs2]<<endl;
mem[pos]=reg[order.rs2]&0xff,mem[pos+1]=(reg[order.rs2]>>8)&0xff;
pc+=4;
}
if(order.type==SW){
int pos=reg[order.rs1]+order.imm;
// cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Store "<<pos<<" "<<reg[order.rs2]<<endl;
mem[pos]=reg[order.rs2]&0xff,mem[pos+1]=(reg[order.rs2]>>8)&0xff,mem[pos+2]=(reg[order.rs2]>>16)&0xff,mem[pos+3]=(reg[order.rs2]>>24)&0xff;
pc+=4;
}
if(order.type==JAL){
// printf("imm=%x\n",order.imm);
reg[order.rd]=pc+4,pc+=order.imm;
}
if(order.type==BEQ){
if(reg[order.rs1]==reg[order.rs2])pc+=order.imm;
else pc+=4;
}
if(order.type==BNE){
if(reg[order.rs1]!=reg[order.rs2])pc+=order.imm;
else pc+=4;
}
if(order.type==BLT){
if((int)reg[order.rs1]<(int)reg[order.rs2])pc+=order.imm;
else pc+=4;
}
if(order.type==BGE){
if((int)reg[order.rs1]>=(int)reg[order.rs2])pc+=order.imm;
else pc+=4;
}
if(order.type==BLTU){
if(reg[order.rs1]<reg[order.rs2])pc+=order.imm;
else pc+=4;
}
if(order.type==BGEU){
if(reg[order.rs1]>=reg[order.rs2])pc+=order.imm;
else pc+=4;
}
reg[0]=0;
// printf("%d %d %d %d !!! ",order.rd,order.rs1,order.rs2,order.imm);
// if(Clock>=6135&&Clock<=6160){
// if(Clock==6000){
// cout<<Clock<<endl;
// for(int i=0;i<32;i++)cout<<reg[i]<<" ";
// cout<<endl;
// }
}
int main(){
GetData();
pc=0;
while(1){
Clock++;
// cout<<"clock="<<Clock<<endl;
Order order=Decode(pc);
if(order.type==END)break;
work(order);
// cout<<endl;
// if(Clock==10000)exit(0);
}
cout<<Clock<<endl;
printf("%u\n",reg[10]&255u);
return 0;
}