-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
146 lines (140 loc) · 5 KB
/
control.py
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
class Control:
def __init__(self,Environment):
self.type=0
self.isBranch=0
self.OP2Select=0
self.RFWrite=0
self.ALUOp=""
self.ResultSelect=0
self.BranchTargetSelect=0
self.Env = Environment
def Type_gen(self):
if self.Env.opcode == 51:
self.type = "R"
elif self.Env.opcode == 19 or self.Env.opcode == 3 or self.Env.opcode == 103:
self.type = "I"
elif self.Env.opcode == 35:
self.type = "S"
elif self.Env.opcode == 55 or self.Env.opcode == 23:
self.type = "U"
elif self.Env.opcode == 111:
self.type = "J"
elif self.Env.opcode == 99:
self.type = "B"
def OP2Select_gen(self):
if self.type == "R" or self.type == "B":
self.OP2Select = 0
elif self.type == "I":
self.OP2Select = 1
elif self.type == "S":
self.OP2Select = 2
def ALUOp_gen(self):
if self.type == "R":
if self.Env.funct3 == 0:
if self.Env.funct7 == 0:
self.ALUOp = "add"
elif self.Env.funct7 == 32:
self.ALUOp = "sub"
elif self.Env.funct3 == 4:
self.ALUOp = "xor"
elif self.Env.funct3 == 6:
self.ALUOp = "or"
elif self.Env.funct3 == 7:
self.ALUOp = "and"
elif self.Env.funct3 == 1:
self.ALUOp = "sll"
elif self.Env.funct3 == 5:
if self.Env.funct7 == 0:
self.ALUOp = "srl"
elif self.Env.funct7 == 32:
self.ALUOp = "sra"
elif self.Env.funct3 == 2:
self.ALUOp = "slt"
else:
self.ALUOp="error"
elif self.type == "I" and self.Env.opcode == 19:
if self.Env.funct3 == 0:
self.ALUOp = "addi"
elif self.Env.funct3 == 6:
self.ALUOp = "ori"
elif self.Env.funct3 == 7:
self.ALUOp = "andi"
else:
self.ALUOp="error"
elif self.type == "I" and self.Env.opcode == 3:
if self.Env.funct3 == 0:
self.ALUOp = "lb"
elif self.Env.funct3 == 1:
self.ALUOp = "lh"
elif self.Env.funct3 == 2:
self.ALUOp = "lw"
else:
self.ALUOp="error"
elif self.type == "S" and self.Env.opcode == 35:
if self.Env.funct3 == 0:
self.ALUOp = "sb"
elif self.Env.funct3 == 1:
self.ALUOp = "sh"
elif self.Env.funct3 == 2:
self.ALUOp = "sw"
else:
self.ALUOp="error"
elif self.type == "I" and self.Env.opcode == 103:
if self.Env.funct3 == 0:
self.ALUOp = "jalr"
else:
self.ALUOp="error"
elif self.type == "B" :
if self.Env.funct3 == 0:
self.ALUOp = "beq"
elif self.Env.funct3 == 1:
self.ALUOp = "bne"
elif self.Env.funct3 == 4:
self.ALUOp = "blt"
elif self.Env.funct3 == 5:
self.ALUOp = "bge"
else:
self.ALUOp="error"
elif self.type == "U" :
if self.Env.opcode == 55:
self.ALUOp = "lui"
elif self.Env.opcode == 23:
self.ALUOp = "auipc"
else:
self.ALUOp="error"
elif self.type == "J":
if self.Env.opcode == 111:
self.ALUOp = "jal"
else:
self.ALUOp="error"
def BranchTargetSelect_gen(self):
if self.type == "J":
self.BranchTargetSelect = 1 #immJ
else:
self.BranchTargetSelect = 0 #immB
def ResultSelect_gen(self):
if self.type == "R":
self.ResultSelect = 0 #AluResult
elif self.type == "I" and self.Env.opcode==19:
self.ResultSelect = 0 #AluResult
elif self.type == "I" and self.Env.opcode==3:
self.ResultSelect = 1 #LoadData
elif self.type == "J":
self.ResultSelect = 2 #PC+4
elif self.type == "I" and self.Env.opcode==103:
self.ResultSelect = 2 #PC+4
elif self.type == "U" and self.Env.opcode==55:
self.ResultSelect = 3 #ImmU
elif self.type == "U" and self.Env.opcode==23:
self.ResultSelect = 4 #ImmU+PC
else:
self.ResultSelect = 5 #No WriteBack required
def isBranch_gen(self,result):
if self.type=="B" and result==1:
self.isBranch=1
elif self.type=="J":
self.isBranch=1
elif self.ALUOp=="jalr":
self.isBranch=2
else:
self.isBranch=0