2021-01-15 04:37:51 +00:00
///////////////////////////////////////////
// hazard.sv
//
// Written: David_Harris@hmc.edu 9 January 2021
// Modified:
//
2023-01-15 02:56:46 +00:00
// Purpose: Determine stalls and flushes
2021-01-15 04:37:51 +00:00
//
2023-01-15 02:56:46 +00:00
// Documentation: RISC-V System on Chip Design Chapter 4, Figure 13.54
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-01-15 04:37:51 +00:00
//
2023-01-10 19:35:20 +00:00
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-01-15 04:37:51 +00:00
//
2023-01-10 19:35:20 +00:00
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-01-15 04:37:51 +00:00
//
2023-01-10 19:35:20 +00:00
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
// may obtain a copy of the License at
2021-01-15 04:37:51 +00:00
//
2023-01-10 19:35:20 +00:00
// https://solderpad.org/licenses/SHL-2.1/
//
// Unless required by applicable law or agreed to in writing, any work distributed under the
// License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
2022-01-07 12:58:40 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////
2021-01-15 04:37:51 +00:00
2021-01-23 15:48:12 +00:00
`include " wally-config.vh "
2021-01-15 04:37:51 +00:00
2023-01-21 00:47:36 +00:00
module hazard (
2021-02-02 18:53:13 +00:00
// Detect hazards
2023-02-27 06:39:19 +00:00
input logic BPWrongE , CSRWriteFenceM , RetM , TrapM ,
2023-01-21 00:47:36 +00:00
input logic LoadStallD , StoreStallD , MDUStallD , CSRRdStallD ,
input logic LSUStallM , IFUStallF ,
input logic FCvtIntStallD , FPUStallD ,
input logic DivBusyE , FDivBusyE ,
input logic EcallFaultM , BreakpointFaultM ,
input logic WFIStallM ,
2021-02-26 06:03:47 +00:00
// Stall & flush outputs
2023-01-21 00:47:36 +00:00
output logic StallF , StallD , StallE , StallM , StallW ,
output logic FlushD , FlushE , FlushM , FlushW
2021-02-02 18:42:23 +00:00
) ;
2023-01-15 02:56:46 +00:00
logic StallFCause , StallDCause , StallECause , StallMCause , StallWCause ;
2023-03-14 18:09:50 +00:00
logic LatestUnstalledD , LatestUnstalledE , LatestUnstalledM , LatestUnstalledW ;
2023-01-15 02:56:46 +00:00
logic FlushDCause , FlushECause , FlushMCause , FlushWCause ;
2022-12-15 16:05:17 +00:00
2021-01-15 04:37:51 +00:00
// stalls and flushes
// loads: stall for one cycle if the subsequent instruction depends on the load
// branches and jumps: flush the next two instructions if the branch is taken in EXE
// CSR Writes: stall all instructions after the CSR until it completes, except that PC must change when branch is resolved
// this also applies to other privileged instructions such as M/S/URET, ECALL/EBREAK
// Exceptions: flush entire pipeline
// Ret instructions: occur in M stage. Might be possible to move earlier, but be careful about hazards
2021-01-30 06:43:49 +00:00
// General stall and flush rules:
// A stage must stall if the next stage is stalled
// If any stages are stalled, the first stage that isn't stalled must flush.
2022-12-19 17:41:41 +00:00
// Flush causes
// Traps (TrapM) flush the entire pipeline.
// However, breakpoint and ecall traps must finish the writeback stage (commit their results) because these instructions complete before trapping.
// Trap returns (RetM) also flush the entire pipeline after the RetM (all stages except W) because all the subsequent instructions must be discarded.
// Similarly, CSR writes and fences flush all subsequent instructions and refetch them in light of the new operating modes and cache/TLB contents
// Branch misprediction is found in the Execute stage and must flush the next two instructions.
// However, an active division operation resides in the Execute stage, and when the BP incorrectly mispredicts the divide as a taken branch, the divde must still complete
2023-02-27 06:39:19 +00:00
assign FlushDCause = TrapM | RetM | CSRWriteFenceM | BPWrongE ;
assign FlushECause = TrapM | RetM | CSRWriteFenceM | ( BPWrongE & ~ ( DivBusyE | FDivBusyE ) ) ;
2022-12-15 16:05:17 +00:00
assign FlushMCause = TrapM | RetM | CSRWriteFenceM ;
2023-02-02 14:52:06 +00:00
assign FlushWCause = TrapM ;
2022-12-15 15:53:35 +00:00
2022-12-19 17:41:41 +00:00
// Stall causes
// Most data depenency stalls are identified in the decode stage
// Division stalls in the execute stage
2022-12-23 21:10:37 +00:00
// Flushing any stage has priority over the corresponding stage stall.
2022-12-19 17:41:41 +00:00
// Even if the register gave clear priority over enable, various FSMs still need to disable the stall, so it's best to gate the stall here with flush
// The IFU and LSU stall the entire pipeline on a cache miss, bus access, or other long operation.
// The IFU stalls the entire pipeline rather than just Fetch to avoid complications with instructions later in the pipeline causing Exceptions
// A trap could be asserted at the start of a IFU/LSU stall, and should flush the memory operation
2022-12-15 15:53:35 +00:00
assign StallFCause = '0 ;
2022-12-19 15:28:45 +00:00
assign StallDCause = ( LoadStallD | StoreStallD | MDUStallD | CSRRdStallD | FCvtIntStallD | FPUStallD ) & ~ FlushDCause ;
2022-12-15 18:56:18 +00:00
assign StallECause = ( DivBusyE | FDivBusyE ) & ~ FlushECause ;
2022-12-23 21:10:37 +00:00
assign StallMCause = WFIStallM & ~ FlushMCause ;
// Need to gate IFUStallF when the equivalent FlushFCause = FlushDCause = 1.
2022-12-23 21:13:15 +00:00
// assign StallWCause = ((IFUStallF & ~FlushDCause) | LSUStallM) & ~FlushWCause;
2022-12-23 21:10:37 +00:00
// Because FlushWCause is a strict subset of FlushDCause, FlushWCause is factored out.
assign StallWCause = ( IFUStallF & ~ FlushDCause ) | ( LSUStallM & ~ FlushWCause ) ;
2022-12-15 15:53:35 +00:00
2022-12-19 17:41:41 +00:00
// Stall each stage for cause or if the next stage is stalled
2023-03-31 15:33:46 +00:00
// coverage off: StallFCause is always 0
2022-06-08 16:28:09 +00:00
assign # 1 StallF = StallFCause | StallD ;
2023-03-31 15:33:46 +00:00
// coverage on
2022-06-08 16:28:09 +00:00
assign # 1 StallD = StallDCause | StallE ;
assign # 1 StallE = StallECause | StallM ;
assign # 1 StallM = StallMCause | StallW ;
assign # 1 StallW = StallWCause ;
2021-02-08 04:21:55 +00:00
2023-01-15 02:56:46 +00:00
// detect the first stage that is not stalled
2023-03-14 18:09:50 +00:00
assign LatestUnstalledD = ~ StallD & StallF ;
assign LatestUnstalledE = ~ StallE & StallD ;
assign LatestUnstalledM = ~ StallM & StallE ;
assign LatestUnstalledW = ~ StallW & StallM ;
2021-02-08 04:21:55 +00:00
// Each stage flushes if the previous stage is the last one stalled (for cause) or the system has reason to flush
2023-03-14 18:09:50 +00:00
assign # 1 FlushD = LatestUnstalledD | FlushDCause ;
assign # 1 FlushE = LatestUnstalledE | FlushECause ;
assign # 1 FlushM = LatestUnstalledM | FlushMCause ;
assign # 1 FlushW = LatestUnstalledW | FlushWCause ;
2021-01-15 04:37:51 +00:00
endmodule