-
Notifications
You must be signed in to change notification settings - Fork 0
/
HazardUnit.v
65 lines (58 loc) · 2.21 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
`timescale 1ns / 1ps
module HazardUnit #(parameter WIDTH=32)
(
input clk,
input [4:0] RsD,
input [4:0] RtD,
input [4:0] RsE,
input [4:0] RtE,
input [4:0] WriteRegE,
input MemtoRegE,
input RegWriteE,
input [4:0] WriteRegM,
input MemtoRegM,
input RegWriteM,
input [4:0] WriteRegW,
input RegWriteW,
input PCSrcD,
input BranchED,
input BranchNED,
input Branch2RegD,
input Branch2ValueD,
output StallF,
output StallD,
output ForwardAD,
output ForwardBD,
output ForwardADN1,
output ForwardBDN1,
output ForwardADN2,
output ForwardBDN2,
output FlushE,
output [1:0] ForwardAE,
output [1:0] ForwardBE
);
reg [1:0] countreg;
// Yahan clock lao fir counter lagake 1 ya 2 cycle wait karo
assign ForwardAD = ((RsD==WriteRegM)&RegWriteM)?1'b1:1'b0;
assign ForwardBD = ((RtD==WriteRegM)&RegWriteM)?1'b1:1'b0;
assign ForwardAE = ((RsE==WriteRegM)&RegWriteM)?2'b10:((RsE==WriteRegW)&RegWriteW)?2'b01:2'b00;
assign ForwardBE = ((RtE==WriteRegM)&RegWriteM)?2'b10:((RtE==WriteRegW)&RegWriteW)?2'b01:2'b00;
assign ForwardADN1 = ((BranchED|BranchNED|Branch2RegD|Branch2ValueD)&(RsD==WriteRegE)&RegWriteE)?1'b1:1'b0;
assign ForwardBDN1 = ((BranchED|BranchNED|Branch2RegD|Branch2ValueD)&(RtD==WriteRegE)&RegWriteE)?1'b1:1'b0;
assign ForwardADN2 = ((BranchED|BranchNED|Branch2RegD|Branch2ValueD)&(RsD==WriteRegW)&RegWriteW)?1'b1:1'b0;
assign ForwardBDN2 = ((BranchED|BranchNED|Branch2RegD|Branch2ValueD)&(RtD==WriteRegW)&RegWriteW)?1'b1:1'b0;
assign StallF=((RsD==WriteRegE|RtD==WriteRegE)&MemtoRegE)?1'b1:(PCSrcD==1&(countreg==2'b00|countreg==2'b01))?1'b1:1'b0;
assign StallD=((RsD==WriteRegE|RtD==WriteRegE)&MemtoRegE)?1'b1:(PCSrcD==1&(countreg==2'b00|countreg==2'b01|countreg==2'b10))?1'b1:1'b0;
assign FlushE=((RsD==WriteRegE|RtD==WriteRegE)&MemtoRegE)?1'b1:(PCSrcD==1&(countreg==2'b00|countreg==2'b01|countreg==2'b10))?1'b1:1'b0;
initial
countreg=2'b00;
always @(posedge clk)
begin
case(countreg)
2'b00:if(PCSrcD==1)countreg<=2'b01;
2'b01:if(PCSrcD==1)countreg<=2'b10;
2'b10:if(PCSrcD==1)countreg<=2'b11;
2'b11:if(PCSrcD==1)countreg<=2'b00;
endcase
end
endmodule // HazardUnit