-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHazardUnit.v.bak
152 lines (129 loc) · 5.78 KB
/
HazardUnit.v.bak
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
`include "definations.v"
//****check for rt and rs in other instructions if they are wrong.like in load instruction rs i being used and rt is destination in mips
//****CHECK FOR RT IN STORE AND LOAD IN ALL BELOW FOR CORRECTION
module StallUnit(input [31:0] instr_D,instr_E,instr_M,
//*** no need for busy and start in risc_vtop
output reg stall);
//FOR MEMORY STORE WE ALWAYS HAVE RS2 DESTINATION
always@(*)
begin
if(load(instr_E)) //load is in execution phase.In pipeline we have to check if destination of this has to do something with decode instruction variables
begin
//check working here I have to check each instruction for RT,RS and rd in mips in comparizon to risc v rs1.rs2 and rd
if(instr_E[`RD] == instr_D[`RS2])//check above its load(rsand rt).we have to check here RT here conceptually
stall <= 1'b1;
else if(instr_E[`RD] == instr_D[`RS1])
stall <= 1'b1;
else if(cal_r(instr_D) && instr_E[`RD] == instr_D[`RS2])
stall <= 1'b1;
else if((cal_r(instr_D) || cal_i(instr_D) || load(instr_D) || store(instr_D)) && instr_E[`RD] == instr_D[`RS1])
stall <= 1'b1;
else
stall <= 1'b0;
end // if (load(instr_E))
else if(cal_i(instr_E)) //cal_i_E
begin
if(instr_E[`RD] == instr_D[`RS2])
stall <= 1'b1;
else if(instr_E[`RD] == instr_D[`RS1])
stall <= 1'b1;
else
stall <= 1'b0;
end
else if(cal_r(instr_E) && instr_E[`RD] != 5'b0 )
begin
if(instr_E[`RD] == instr_D[`RS2])
stall <= 1'b1;
else if(instr_E[`RD] == instr_D[`RS1])
stall <= 1'b1;
else
stall <= 1'b0;
end
else if(load(instr_M))
begin
if(instr_M[`RS2] == instr_D[`RS2])
stall <= 1'b1;
else if(instr_M[`RS2] == instr_D[`RS1])
stall <= 1'b1;
else
stall <= 1'b0;
end
else
stall <= 1'b0;
end // always @ begin
function load;
input [31:0] instr ;
load = (`LB || `LBU || `LH || `LHU || `LW);
endfunction // StallUnit
function cal_r;//include sll srl sra exclude mul div
input [31:0] instr;
cal_r = (`ADD || `SUB || `SLT || `SLTU || `SLL || `SRL || `SRA || `AND || `OR || `XOR);
endfunction // StallUnit
function cal_i;//include lui
input [31:0] instr;
cal_i = (`ADDI || `ORI || `XORI || `LUI || `SLTI || `SLTIU||`SLLI||`SRLI||`SRAI||`ANDI);
endfunction // StallUnit
function store;
input [31:0] instr;
store = (`SB || `SH || `SW);
endfunction // StallUnit
endmodule // StallUnit
module HazardUnit(input [31:0] instr_D,instr_E,instr_M,instr_W,
output reg [1:0] FowardRS1D,FowardRS2D,FowardRS1E,FowardRS2E,//RSD=DECODING INSTRUCTION NEED OF RS1 SO FORWARD AND ALSO SAMEFOR OTHERS
output reg FowardRDM);
always @(*) //for Foward RS1E
//between memory and execute for rs1
//if(cal_r(instr_E) || cal_i(instr_E) || load(instr_E) || store(instr_E) || )//cal_i cal_r lw sw E
if((cal_r(instr_M) && instr_M[`RS2] != 5'b0) && instr_M[`RS2] == instr_E[`RS1]) //cal_r M
FowardRS1E <= 2'b01;
else if(cal_i(instr_M) && instr_M[`RS2] == instr_E[`RS1]) //cal_i M
FowardRS1E <= 2'b01;
else if((cal_r(instr_W) && instr_W[`RD] != 5'b0) && instr_W[`RD] == instr_E[`RS1]) //cal_r W
FowardRS1E <= 2'b11;
else if(cal_i(instr_W) && instr_W[`RD] == instr_E[`RS1]) //cal_i W
FowardRS1E <= 2'b11;
else if(load(instr_W) && instr_W[`RD] == instr_E[`RS1]) //load W****IMP FOR LOAD
FowardRS1E <= 2'b11;
else
FowardRS1E <= 2'b00;
always @(*) //for Foward RS2E
//between memory/WRITE TO REGISTER and execute for for RS2
//if(cal_r(instr_E) || store(instr_E) || muldiv(instr_E))//cal_i sw E
if((cal_r(instr_M) && instr_M[`RS2] != 5'b0) && instr_M[`RS2] == instr_E[`RS2]) //cal_r M
FowardRS2E <= 2'b01;
else if(cal_i(instr_M) && instr_M[`RS2] == instr_E[`RS2]) //cal_i M
FowardRS2E <= 2'b01;
else if((cal_r(instr_W) && instr_W[`RD] != 5'b0 ) && instr_W[`RD] == instr_E[`RS2]) //cal_r W
FowardRS2E <= 2'b11;
else if(cal_i(instr_W) && instr_W[`RD] == instr_E[`RS2]) //cal_i W
FowardRS2E <= 2'b11;
else if(load(instr_W) && instr_W[`RD] == instr_E[`RS2]) //load W
FowardRS2E <= 2'b11;
else
FowardRS2E <= 2'b00;
always @(*) //for Foward RDM
//BETWEEN MEMORY AND WRITE TO REGISTER
//if(store(instr_M)) //sw M
if((cal_r(instr_W) && instr_W[`RD] != 5'b0 ) && instr_W[`RD] == instr_M[`RS2]) //cal_r W
FowardRDM <= 1'b1;
else if((cal_i(instr_W) || load(instr_W)) && instr_W[`RD] == instr_M[`RS2]) //cal_i W
FowardRDM <= 1'b1;
else
FowardRDM <= 1'b0;
function load;
input [31:0] instr;
load = (`LB || `LBU || `LH || `LHU || `LW);
endfunction // StallUnit
function cal_r;//include sll srl sra exclude mul div
input [31:0] instr;
cal_r = (`ADD || `SUB || `SLT || `SLTU || `SLL || `SRL || `SRA || `AND || `OR || `XOR);
endfunction // StallUnit
function cal_i;//include lui
input [31:0] instr;
cal_i = (`ADDI || `ORI || `XORI || `LUI || `SLTI || `SLTIU||`SLLI||`SRLI||`SRAI||`ANDI);
endfunction // StallUnit
function store;
input [31:0] instr;
store = (`SB || `SH || `SW);
endfunction // StallUnit
endmodule // HazardUnit