-
Notifications
You must be signed in to change notification settings - Fork 0
/
hazard.v
50 lines (42 loc) · 1.85 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
`timescale 1ps/1ps
module hazard(input regwriteW, regwriteM, memtoregM,
input [4:0] writeregW, writeregM, writeregE,
input regwriteE, memtoregE, branchD,
input [4:0] rsE, rtE, rsD, rtD,
input jalD, jalE, jalM, aluormultE, prodv, jumpD,
output [1:0] forwardAE, forwardBE,
output forwardAD, forwardBD, stallD, stallF, flushE);
wire lwstall, branchstall, jalstall, multstall;
reg [1:0] forwardAE_temp, forwardBE_temp;
//forwarding data hazards at execute stage
assign forwardAE = forwardAE_temp;
assign forwardBE = forwardBE_temp;
always @ * begin
//for forwarding to srcA
if((rsE != 0) & (rsE == writeregM) & regwriteM)
forwardAE_temp = 2'b10;
else if ((rsE != 0) & (rsE == writeregW) & regwriteW)
forwardAE_temp = 2'b01;
else
forwardAE_temp = 2'b00;
//for forwarding to srcB
if((rtE != 0) & (rtE == writeregM) & regwriteM)
forwardBE_temp = 2'b10;
else if ((rtE != 0) & (rtE == writeregW) & regwriteW)
forwardBE_temp = 2'b01;
else
forwardBE_temp = 2'b00;
end
//forwarding data hazards at decode stage
assign forwardAD = (rsD != 0) & (rsD == writeregM) & regwriteM;
assign forwardBD = (rtD != 0) & (rtD == writeregM) & regwriteM;
//control stalls
assign lwstall = ((rsD == rtE) | (rtD == rtE)) & memtoregE;
assign branchstall = (branchD & regwriteE & (writeregE == rsD | writeregE == rtD)) |
(branchD & memtoregM & (writeregM == rsD | writeregM == rtD));
assign jalstall = jalE | jalM;
assign multstall = aluormultE & ~prodv;
assign flushE = lwstall | branchstall | (jumpD & ~jalD) | jalstall;
assign stallD = flushE | jalstall | multstall;
assign stallF = stallD;
endmodule