-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstruction.cpp
104 lines (93 loc) · 2.83 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
102
103
104
#include <cassert>
#include "instruction.h"
void Instruction::init(InstructionType _type, RegisterType _arg1, RegisterType _arg2, RegisterType _arg3,
int _immidiate, int _address) {
type = _type;
arg1 = _arg1;
arg2 = _arg2;
arg3 = _arg3;
immidiate = _immidiate;
address = _address;
A = B = 0;
aluOut = 0;
loadMemData = 0;
fetchedAtCycle = 0;
srcCycle1 = 0;
srcCycle2 = 0;
}
Instruction::Instruction() {
init(NOP, R0, R0, R0, 0, 0);
}
Instruction::Instruction(InstructionType _type, int _address) {
init(_type, R0, R0, R0, 0, _address);
}
Instruction::Instruction(InstructionType _type, RegisterType _arg1, int _immidiate, int _address) {
init(_type, _arg1, R0, R0, _immidiate, _address);
}
Instruction::Instruction(InstructionType _type, RegisterType _arg1, RegisterType _arg2,
int _immidiate, int _address) {
init(_type, _arg1, _arg2, R0, _immidiate, _address);
}
Instruction::Instruction(InstructionType _type, RegisterType _arg1, RegisterType _arg2,
RegisterType _arg3, int _address) {
init(_type, _arg1, _arg2, _arg3, 0, _address);
}
Instruction::Instruction(Instruction& inst) {
init(inst.type, inst.arg1, inst.arg2, inst.arg3, inst.immidiate, inst.address);
A = inst.A;
B = inst.B;
aluOut = inst.aluOut;
loadMemData = inst.loadMemData;
fetchedAtCycle = inst.fetchedAtCycle;
srcCycle1 = inst.srcCycle1;
srcCycle2 = inst.srcCycle2;
}
Instruction& Instruction::operator=(Instruction& rhs) {
if (this == &rhs)
return *this;
init(rhs.type, rhs.arg1, rhs.arg2, rhs.arg3, rhs.immidiate, rhs.address);
A = rhs.A;
B = rhs.B;
aluOut = rhs.aluOut;
loadMemData = rhs.loadMemData;
fetchedAtCycle = rhs.fetchedAtCycle;
srcCycle1 = rhs.srcCycle1;
srcCycle2 = rhs.srcCycle2;
return *this;
}
void Instruction::clear() {
init(NOP, R0, R0, R0, 0, 0);
}
// Returns whether dest instruction is data dependent on src
// This should be used for detecting hazard between in ID stage
// instruction and later stage instruction
bool Instruction::isDataDependent(Instruction& src, Instruction& dest) {
//check if src is type that change a register
if(src.isAluImm() || src.isAluReg() || src.isLoad()) {
//check if dest uses arg2 and/or arg3
if(dest.isAluReg() || dest.isAluImm() || dest.isLoad()) {
if(dest.arg2 == src.arg1) {
dest.setSrcCycle1(src.getFetchedAtCycle());
return true;
}
if(dest.arg3 == src.arg1) {
dest.setSrcCycle2(src.getFetchedAtCycle());
return true;
}
}
//check if dest uses arg1 and/or arg2
if(dest.isStore() || dest.isBranch()){
if(dest.arg1 == src.arg1) {
dest.setSrcCycle1(src.getFetchedAtCycle());
return true;
}
if(dest.arg2 == src.arg1) {
dest.setSrcCycle2(src.getFetchedAtCycle());
return true;
}
}
}
return false;
}
Instruction::~Instruction() {
}