-
Notifications
You must be signed in to change notification settings - Fork 0
/
HazardUnit.v
90 lines (83 loc) · 2.2 KB
/
HazardUnit.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
`timescale 1ns / 1ps
module HazardUnit(
IDEX_MemRead,
IDEX_Rt,
IFID_Rs,
IFID_Rt,
ID_PCSrc,
IDEX_PCSrc,
EX_need_branch,
PCWrite,
IFID_write,
IFID_flush,
IDEX_flush
);
input IDEX_MemRead;
input [4:0] IDEX_Rt;
input [4:0] IFID_Rs;
input [4:0] IFID_Rt;
input [2:0] ID_PCSrc;
input [2:0] IDEX_PCSrc;
input EX_need_branch;
output PCWrite;
output IFID_write;
output IFID_flush;
output IDEX_flush;
reg [2:0] PCWrite_request, IFID_write_request, IFID_flush_request, IDEX_flush_request;
assign PCWrite = PCWrite_request[0] & PCWrite_request[1] & PCWrite_request[2];
assign IFID_write = IFID_write_request[0] & IFID_write_request[1] & IFID_write_request[2];
assign IFID_flush = IFID_flush_request[0] | IFID_flush_request[1] | IFID_flush_request[2];
assign IDEX_flush = IDEX_flush_request[0] | IDEX_flush_request[1] | IDEX_flush_request[2];
// Load-use
always @(*) begin
if(IDEX_MemRead == 1'b1 &&
(IDEX_Rt == IFID_Rs || IDEX_Rt == IFID_Rt))
begin
PCWrite_request[0] = 1'b0;
IFID_write_request[0] = 1'b0;
IDEX_flush_request[0] = 1'b1;
IFID_flush_request[0] = 1'b0;
end
else
begin
PCWrite_request[0] = 1'b1;
IFID_write_request[0] = 1'b1;
IDEX_flush_request[0] = 1'b0;
IFID_flush_request[0] = 1'b0;
end
end
// Jump
always @(*) begin
if(ID_PCSrc == 3'b001 || ID_PCSrc == 3'b010 || ID_PCSrc == 3'b011 || ID_PCSrc == 3'b101)
begin
PCWrite_request[1] = 1'b1;
IFID_write_request[1] = 1'b1;
IDEX_flush_request[1] = 1'b0;
IFID_flush_request[1] = 1'b1;
end
else
begin
PCWrite_request[1] = 1'b1;
IFID_write_request[1] = 1'b1;
IDEX_flush_request[1] = 1'b0;
IFID_flush_request[1] = 1'b0;
end
end
// Branch
always @(*) begin
if(IDEX_PCSrc == 3'b100 && EX_need_branch == 1'b1)
begin
PCWrite_request[2] = 1'b1;
IFID_write_request[2] = 1'b1;
IDEX_flush_request[2] = 1'b1;
IFID_flush_request[2] = 1'b1;
end
else
begin
PCWrite_request[2] = 1'b1;
IFID_write_request[2] = 1'b1;
IDEX_flush_request[2] = 1'b0;
IFID_flush_request[2] = 1'b0;
end
end
endmodule