OpenCores
URL https://opencores.org/ocsvn/edge/edge/trunk

Subversion Repositories edge

[/] [edge/] [trunk/] [HW/] [Verilog/] [hazard_unit.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 heshamelma
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Hazard unit for Edge Core                                   //
4
//                                                              //
5
//  This file is part of the Edge project                       //
6
//  http://www.opencores.org/project,edge                       //
7
//                                                              //
8
//  Description                                                 //
9
//  Hazard unit is responsible for detecting different pipline  //
10
//  hazards, and solving these hazards either by forwarding or  //
11
//  stalling the pipeline.                                      //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Hesham AL-Matary, heshamelmatary@gmail.com            //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2014 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
//////////////////////////////////////////////////////////////////
42
 
43
module hazard_unit
44
#(
45
  parameter N=32, M=5
46
)
47
(
48
  input CLK,
49
  input[M-1:0] rsE, rtE, /* Source registers to ALU at EX stage */
50
  input[M-1:0] rsD, rtD, /* Source registers to ALU at EX stage */
51
  input[M-1:0] DestRegE, DestRegM, DestRegW, /* Destination registers at mem
52
  and   wb stages */
53
  input RegWriteE, RegWriteM, RegWriteW, /* Whether instruction writes to RF or
54
  not */
55
  input loadE, /* load instruction */
56
  input MemWriteD, MemWriteE, /* Store */
57
  input[2:0] PCSrcM, /* PCplus4 or not */
58
  output reg[1:0] ForwardAE, ForwardBE, /* Forward signals to muxes at ALU
59
stages   */
60
  output reg StallF, StallD, StallE, StallM, FlushE, StallW, /* Stall control
61
  signals */
62
  output reg FlushD, FlushM, FlushF,
63
  input StallDataMemory
64
);
65
 
66
reg lwStall;
67
reg FlushControl;
68
reg stStall; /* Store stall right after load */
69
reg RWHazard; /* Read and Write at the same clock cycle */
70
reg[1:0] FetchCounter = 0;
71
reg FetchStall = 0; /* Two clock cycles for fetch to handle BRAM Read latency */
72
reg[31:0] ClockCycleCount = 0;
73
wire FetchStallwire = (FetchStall == 1)? 1'b1 : 1'b0;
74
reg StoresInRowStall = 0;
75
 
76
always @*
77
begin
78
  lwStall = 1'b0;
79
  stStall = 1'b0;
80
  RWHazard = 1'b0;
81
  ForwardAE = 2'b00;
82
  ForwardBE = 2'b00;
83
  StallF = 1'b0;
84
  StallD = 1'b0;
85
  FlushM = 1'b0;
86
  FlushD = 1'b0;
87
  FlushE = 1'b0;
88
  FlushControl = 1'b0;
89
  StoresInRowStall = 1'b0;
90
 
91
  if(rsE != 5'd0 && rsE == DestRegM && RegWriteM)
92
    ForwardAE = 2'b10;
93
  else if(rsE != 5'd0 && rsE == DestRegW && RegWriteW)
94
    ForwardAE = 2'b01;
95
  else
96
    ForwardAE = 2'b00;
97
 
98
  if(rtE != 5'd0 && rtE == DestRegM && RegWriteM)
99
    ForwardBE = 2'b10;
100
  else if(rtE != 5'd0 && rtE == DestRegW && RegWriteW)
101
    ForwardBE = 2'b01;
102
  else
103
    ForwardBE = 2'b00;
104
 
105
  /* load stall */
106
  if(loadE && (rsD == rtE || rtD == rtE))
107
    lwStall = 1'b1;
108
  else
109
    lwStall = 1'b0;
110
 
111
  /* Store stall */
112
  if
113
  (
114
    (RegWriteM && MemWriteD && DestRegM == rtD) ||
115
    (RegWriteE
116
    && MemWriteD && DestRegE == rtD)
117
  )
118
    stStall = 1'b1;
119
 
120
  /* Stall for one clock cycle if there is two stores in row */
121
  if(MemWriteD && MemWriteE)
122
  begin
123
    StoresInRowStall = 1;
124
  end
125
 
126
  /* Branch detected */
127
  if(PCSrcM != 3'b000)
128
      FlushControl = 1'b1;
129
 
130
  /* Stall one clock cycle if there is w/r to a register in the same time */
131
  if(
132
     (DestRegW == rsD || DestRegW == rtD) &&
133
     DestRegW !=0 &&
134
     StallDataMemory !=1
135
    )
136
      RWHazard = 1'b1;
137
 
138
  if(
139
      (DestRegM == rsD || DestRegM == rtD)
140
      && DestRegM !=0 &&
141
      MemWriteE &&
142
      StallDataMemory != 1
143
    )
144
      RWHazard = 1'b1;
145
 
146
  StallF = (lwStall == 1'b1 || stStall || RWHazard ||
147
  StoresInRowStall)? 1'b1:1'b0;
148
 
149
  StallD = (lwStall == 1'b1 || stStall || RWHazard ||
150
  StoresInRowStall)? 1'b1:1'b0;
151
 
152
  StallE = (lwStall == 1'b1)? 1'b1:1'b0;
153
  StallM = (lwStall == 1'b1)? 1'b1:1'b0;
154
  StallW = 0;
155
  FlushF = (FlushControl);
156
  FlushE =
157
  (lwStall || stStall || FlushControl || StoresInRowStall ||
158
   RWHazard)  ? 1'b1:1'b0;
159
  FlushD = ((FlushControl == 1'b1))? 1'b1:1'b0;
160
  FlushM = (FlushControl == 1'b1)? 1'b1:1'b0;
161
 
162
end
163
 
164
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.