-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_unit.h
106 lines (100 loc) · 2.34 KB
/
control_unit.h
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
#ifndef CONTROL_UNIT_H
#define CONTROL_UNIT_H
using namespace std;
bool RegWrite = false, RegDst = false, Branch = false, ALUSrc = false, InstType = false,
MemWrite = false, MemtoReg = false, MemRead = false, Jump = false;
int ALUControl(int ALUop, int funct);
void resetConstrolUnitSignals();
void resetConstrolUnitSignals() {
RegWrite = false;
RegDst = false;
Branch = false;
ALUSrc = false;
InstType = false;
MemWrite = false;
MemtoReg = false;
MemRead = false;
Jump = false;
}
int controlUnit(int opcode, int funct) {
resetConstrolUnitSignals();
switch(opcode) {
case 0x0:
// R-Format
// jr operation
if(funct == 0x8){
Jump = true;
}
else {
RegDst = true;
RegWrite = true;
}
return ALUControl(2, funct);
case 0x23:
// lw operation
ALUSrc = true;
MemtoReg = true;
RegWrite = true;
MemRead = true;
return ALUControl(0, funct);
case 0x2b:
// sw operation
ALUSrc = true;
MemWrite = true;
return ALUControl(0, funct);
case 0x4:
// beq operation
Branch = true;
return ALUControl(1, funct);
case 0x3:
// jal operation
Jump = true;
return ALUControl(0, funct);
case 0x2:
// j operation
Jump = true;
return ALUControl(0, funct);
default:
// Error
return 10000;
}
}
int ALUControl(int ALUop, int funct) {
if(ALUop == 0){
// 0010 add
return 2;
}
else if(ALUop == 1){
// 0110 substract
return 6;
}
else if(ALUop == 2){
if(funct == 0x20){
// 0010 add
return 2;
}
else if(funct == 0x22){
// 0110 substract
return 6;
}
else if(funct == 0x24){
// 0000 AND
return 0;
}
else if(funct == 0x25){
// 0001 OR
return 1;
}
else if(funct == 0x27){
// NOR
return 0xc;
}
else {
// 0111 set on less than
return 7;
}
}
// Error
return 10000;
}
#endif