-
Notifications
You must be signed in to change notification settings - Fork 1
/
Instruction.cpp
101 lines (81 loc) · 1.68 KB
/
Instruction.cpp
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
#include <iostream>
#include "Instruction.h"
using namespace std;
Instruction::Instruction(){
instruct=0;
}
unsigned Instruction::fromto(int high,int low){
return (instruct<<(31-high))>>(31-high+low);
}
void Instruction::Decode(){
opcode=fromto(6,0);
rd=fromto(11,7);
rs1=fromto(19,15);
rs2=fromto(24,20);
funct3=fromto(14,12);
funct7=fromto(31,25);
funct2=fromto(26,25);
rs3=fromto(31,27);
shamt=rs2;
imm=fromto(31,20);
//cout<<instruct<<endl;
//cout<<opcode<<rd<<endl;
}
unsigned char Instruction::getOpcode(){
return opcode;
}
unsigned char Instruction::getRd(){
return rd;
}
unsigned char Instruction::getRs1(){
return rs1;
}
unsigned char Instruction::getRs2(){
return rs2;
}
unsigned char Instruction::getRs3(){
return rs3;
}
unsigned char Instruction::getFunct2(){
return funct2;
}
unsigned char Instruction::getFunct3(){
return funct3;
}
unsigned char Instruction::getFunct7(){
return funct7;
}
unsigned char Instruction::getShamt() {
return shamt;
}
unsigned Instruction::getImm(){
return imm;
}
unsigned Instruction::getInstr() {
return instruct;
}
void Instruction::setInstruct(unsigned inst) {
// printf("%x\n", inst);
// char *p = (char*)&inst;
// char tmp;
// tmp = *(p+3);
// *(p+3) = *p;
// *p = tmp;
// tmp = *(p+1);
// *(p+1) = *(p+2);
// *(p+2) = tmp;
instruct = inst;
// printf("%x\n", inst);
}
/*int main(){
unsigned test=0xbd8fc158;
Instruction a=Instruction(test);
a.Decode();
cout<<"opcode "<<int(a.getOpcode())<<endl;
cout<<"rs1 "<<int(a.getRs1())<<endl;
cout<<"rs2 "<<int(a.getRs2())<<endl;
cout<<"rd "<<int(a.getRd())<<endl;
cout<<"funct3 "<<int(a.getFunct3())<<endl;
cout<<"funct7 "<<int(a.getFunct7())<<endl;
return 0;
}*/