-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch_unit.v
195 lines (190 loc) · 3.58 KB
/
branch_unit.v
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
module branch_unit(
input [31:0] rs1,
input [31:0] rs2,
input [31:0] instr,
input [31:0] address,
input [31:0] alu_fb,
input [1:0] branch_dhazard,
input clk,
output reg flushF,
output reg flushD,
output reg miss_predict,
output reg [31:0] target);
// Chu thich:
// "flush" tich cuc muc CAO, noi voi Clear cua FLIPFLOP.
// "mis_predict" tich cuc muc CAO, noi voi MUX truoc PC.
// Mo ta hoat dong:
// Luon KHONG NHAY.
// <=> Du doan DIEU KIEN NHAY la SAI.
// De kiem tra dieu kien nhay thuc te, rs1 so sanh voi rs2 o stage EX.
// => Neu thuc te dieu kien nhay DUNG.
// => Doan sai.
// => Neu thuc te dieu kien nhay SAI.
// => Doan dung.
// Neu "Doan sai", mis_predict tich cuc. FLUSH toan bo, nhay den PC moi.
// Neu "Doan dung", PC = PC + 4, thuc hien tiep nhu binh thuong.
reg last_flush;
always @(*)
begin
if (instr[6:2] == 5'b11000)
begin
if (branch_dhazard == 0) begin
if (instr[14:12] == 3'b000) // Branch Equal
begin
if (rs1 == rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b001) // Branch Not Equal
begin
if (rs1 != rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b100) // Branch Less Than
begin
if (rs1 < rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b101) // Branch Less Than Unsigned
begin
if ($unsigned(rs1) < $unsigned(rs2))
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else begin
miss_predict = 0;
flushF = 0;
flushD = 0;
end
end
else if (branch_dhazard == 1) begin
if (instr[14:12] == 3'b000) // Branch Equal
begin
if (alu_fb == rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b001) // Branch Not Equal
begin
if (alu_fb != rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b100) // Branch Less Than
begin
if (alu_fb < rs2)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b101) // Branch Less Than Unsigned
begin
if ($unsigned(alu_fb) < $unsigned(rs2))
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else begin
miss_predict = 0;
flushF = 0;
flushD = 0;
end
end
else if (branch_dhazard == 2) begin
if (instr[14:12] == 3'b000) // Branch Equal
begin
if (rs1 == alu_fb)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b001) // Branch Not Equal
begin
if (rs1 != alu_fb)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b100) // Branch Less Than
begin
if (rs1 < alu_fb)
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else if (instr[14:12] == 3'b101) // Branch Less Than Unsigned
begin
if ($unsigned(rs1) < $unsigned(alu_fb))
begin
target = address;
miss_predict = 1;
flushF = 1;
flushD = 1;
end
end
else begin
miss_predict = 0;
flushF = 0;
flushD = 0;
end
end
end
else begin
target = 0;
miss_predict = 0;
last_flush = flushF;
flushF = 0;
flushD = 0;
end
end
always @(negedge clk)
begin
if (last_flush == 1 && flushF == 1)
begin
flushF = 0;
flushD = 0;
end
last_flush = flushF;
end
endmodule