-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHazard_Unit.vhd
101 lines (91 loc) · 3.15 KB
/
Hazard_Unit.vhd
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
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use work.pro_types.all;
entity Hazard_Unit is
port
(
--WriteRegW and WriteRegM are the destination registers for the instructions being in that stages
--RxE and RyE are the input registers for the instructionz being in ALU
WriteRegW, WriteRegM, RsE, RtE, RsD, RtD: in bit_4;
Inst_type: in bit_2;
RegWriteW,RegWriteM : in std_logic ;
MemtoRegE : in std_logic;
ForwardBE, ForwardAE, ForwardWD, ForwardBranch : out bit_2;
StallF,StallD,FlushE : out std_logic
);
end entity Hazard_Unit;
architecture arc_hazardUnitPipeline of Hazard_Unit is
signal lwstall : std_logic ;
begin
process(RtE,RsE,WriteRegM,RegWriteM,WriteRegW,RegWriteW)
--Memory values are of higher priority because they are the most up to date ones
-- In other(losfer) words : if the value of an input register exists both in Memory and Writeback stage of
-- 2 other instructions the tie breaks in favor of the Memory
begin
ForwardAE <= "00";
ForwardBE <= "00";
ForwardWD <= "00";
ForwardBranch <= "00";
--check srcA
--forwarding for arithmetic operations instructions and move instruction which Rt is the first oprand
if(Inst_type(1)='1') then
if((RegWriteM='1') AND(RtE = WriteRegM)) then
ForwardAE <= "10";--forward Memory
else if ((RegWriteW='1') AND(RtE = WriteRegW)) then
ForwardAE <= "01";-- Forward Writeback
end if;
end if;
--some other instructions which Rs is the first oprand
else if((RegWriteM='1') AND(RsE = WriteRegM)) then
ForwardAE <= "10";--forward Memory
else if ((RegWriteW='1') AND (RsE = WriteRegW)) then
ForwardAE <= "01";-- Forward Writeback
end if;
end if;
end if;
--check srcB
if (Inst_type="10")then
if ((RsE = WriteRegM) AND (RegWriteM='1')) then
ForwardBE <= "10";--forward Memory
else if ((RsE = WriteRegW) AND (RegWriteW='1')) then
ForwardBE <= "01";-- Forward Writeback
end if;
end if;
end if;
--check WD for ST
if (Inst_type="00")then
if ((RtE = WriteRegM) AND (RegWriteM='1')) then
ForwardWD <= "10";--forward Memory
else if ((RtE = WriteRegW) AND (RegWriteW='1')) then
ForwardWD <= "01";-- Forward Writeback
end if;
end if;
end if;
--check Branch
if(Inst_type="00") then
if((RegWriteM='1') AND(RtD = WriteRegM)) then
ForwardBranch <= "10";--forward Memory
end if;
-- else if ((RegWriteW='1') AND(RtD = WriteRegW)) then
-- ForwardBranch <= "01";-- Forward Writeback
-- end if;
end if;
end process;
process(MemtoRegE,RtD,RtE,RsD) is
-- Solving Data Hazards with Stalls, LOAD instruction
-- RtE is th Register where the value will be loaded to
begin
if(((RtD=RtE)OR(RsD=RtE)) AND (MemtoRegE = '1')) then
lwstall <= '1';
else
lwstall <='0';
end if;
end process;
process(lwstall)
begin
StallF <= lwstall;
StallD <= lwstall;
FlushE <= lwstall;
end process;
end;