-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhazard.v
97 lines (89 loc) · 2.26 KB
/
hazard.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2017/11/22 10:23:13
// Design Name:
// Module Name: hazard
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module hazard(
//fetch stage
output wire stallF,
//decode stage
input wire[4:0] rsD,rtD,
input wire branchD,
output wire forwardaD,forwardbD,
output wire stallD,
//execute stage
input wire[4:0] rsE,rtE,
input wire[4:0] writeregE,
input wire regwriteE,
input wire memtoregE,
output reg[1:0] forwardaE,forwardbE,
output wire flushE,
//mem stage
input wire[4:0] writeregM,
input wire regwriteM,
input wire memtoregM,
//write back stage
input wire[4:0] writeregW,
input wire regwriteW
);
wire lwstallD,branchstallD;
//forwarding sources to D stage (branch equality)
assign forwardaD = (rsD != 0 & rsD == writeregM & regwriteM);
assign forwardbD = (rtD != 0 & rtD == writeregM & regwriteM);
//forwarding sources to E stage (ALU)
always @(*) begin
forwardaE = 2'b00;
forwardbE = 2'b00;
if(rsE != 0) begin
/* code */
if(rsE == writeregM & regwriteM) begin
/* code */
forwardaE = 2'b10;
end else if(rsE == writeregW & regwriteW) begin
/* code */
forwardaE = 2'b01;
end
end
if(rtE != 0) begin
/* code */
if(rtE == writeregM & regwriteM) begin
/* code */
forwardbE = 2'b10;
end else if(rtE == writeregW & regwriteW) begin
/* code */
forwardbE = 2'b01;
end
end
end
//stalls
assign #1 lwstallD = memtoregE & (rtE == rsD | rtE == rtD);
assign #1 branchstallD = branchD &
(regwriteE &
(writeregE == rsD | writeregE == rtD) |
memtoregM &
(writeregM == rsD | writeregM == rtD));
assign #1 stallD = lwstallD | branchstallD;
assign #1 stallF = stallD;
//stalling D stalls all previous stages
assign #1 flushE = stallD;
//stalling D flushes next stage
// Note: not necessary to stall D stage on store
// if source comes from load;
// instead, another bypass network could
// be added from W to M
endmodule