2021-01-15 04:37:51 +00:00
///////////////////////////////////////////
// hazard.sv
//
// Written: David_Harris@hmc.edu 9 January 2021
// Modified:
//
// Purpose: Determine forwarding, stalls and flushes
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
2022-01-07 12:58:40 +00:00
// MIT LICENSE
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
2021-01-15 04:37:51 +00:00
//
2022-01-07 12:58:40 +00:00
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
2021-01-15 04:37:51 +00:00
//
2022-01-07 12:58:40 +00:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////
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
module hazard (
2021-02-02 18:53:13 +00:00
// Detect hazards
2022-06-02 16:37:59 +00:00
( * mark_debug = " true " * ) input logic BPPredWrongE , CSRWriteFencePendingDEM , RetM , TrapM ,
2022-01-07 04:30:00 +00:00
( * mark_debug = " true " * ) input logic LoadStallD , StoreStallD , MDUStallD , CSRRdStallD ,
2022-01-15 00:24:16 +00:00
( * mark_debug = " true " * ) input logic LSUStallM , IFUStallF ,
2021-12-12 21:05:50 +00:00
( * mark_debug = " true " * ) input logic FPUStallD , FStallD ,
( * mark_debug = " true " * ) input logic DivBusyE , FDivBusyE ,
( * mark_debug = " true " * ) input logic EcallFaultM , BreakpointFaultM ,
2022-06-02 16:37:59 +00:00
( * mark_debug = " true " * ) input logic wfiM , IntPendingM ,
2021-02-26 06:03:47 +00:00
// Stall & flush outputs
2021-12-12 21:05:50 +00:00
( * mark_debug = " true " * ) output logic StallF , StallD , StallE , StallM , StallW ,
( * mark_debug = " true " * ) output logic FlushF , FlushD , FlushE , FlushM , FlushW
2021-02-02 18:42:23 +00:00
) ;
2021-02-08 04:21:55 +00:00
logic StallFCause , StallDCause , StallECause , StallMCause , StallWCause ;
logic FirstUnstalledD , FirstUnstalledE , FirstUnstalledM , FirstUnstalledW ;
2021-04-02 20:32:15 +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-05-12 16:41:52 +00:00
// *** can stalls be pushed into earlier stages (e.g. no stall after Decode?)
2022-08-29 14:48:00 +00:00
// *** consider replacing CSRWriteFencePendingDEM with a flush rather than a stall.
2022-06-02 16:37:59 +00:00
assign StallFCause = CSRWriteFencePendingDEM & ~ ( TrapM | RetM | BPPredWrongE ) ;
2022-05-08 04:30:46 +00:00
// stall in decode if instruction is a load/mul/csr dependent on previous
assign StallDCause = ( LoadStallD | StoreStallD | MDUStallD | CSRRdStallD | FPUStallD | FStallD ) & ~ ( TrapM | RetM | BPPredWrongE ) ;
2022-08-22 20:28:51 +00:00
// assign StallECause = (DivBusyE | FDivBusyE) & ~(TrapM); // *** can we move to decode stage (KP?)
2022-07-07 23:01:33 +00:00
assign StallECause = ( DivBusyE ) & ~ ( TrapM ) ; // *** can we move to decode stage (KP?)
2022-05-08 04:30:46 +00:00
// WFI terminates if any enabled interrupt is pending, even if global interrupts are disabled. It could also terminate with TW trap
2022-08-22 20:28:51 +00:00
// assign StallMCause = (wfiM & (~TrapM & ~IntPendingM)); // | FDivBusyE;
2022-10-18 20:04:21 +00:00
assign StallMCause = ( ( wfiM | FDivBusyE ) & ( ~ TrapM & ~ IntPendingM ) ) ; //*** Ross: should FDivBusyE trigger StallECause rather than StallMCause similar to DivBusyE?
2022-01-15 00:24:16 +00:00
assign StallWCause = LSUStallM | IFUStallF ;
2021-03-04 19:35:46 +00:00
2022-06-08 16:28:09 +00:00
assign # 1 StallF = StallFCause | StallD ;
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
2022-01-02 21:47:21 +00:00
assign FirstUnstalledD = ~ StallD & StallF ;
assign FirstUnstalledE = ~ StallE & StallD ;
assign FirstUnstalledM = ~ StallM & StallE ;
assign FirstUnstalledW = ~ 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
2022-06-08 16:28:09 +00:00
assign # 1 FlushF = BPPredWrongE ;
assign # 1 FlushD = FirstUnstalledD | TrapM | RetM | BPPredWrongE ;
assign # 1 FlushE = FirstUnstalledE | TrapM | RetM | BPPredWrongE ; // *** why is BPPredWrongE here, but not needed in simple processor
assign # 1 FlushM = FirstUnstalledM | TrapM | RetM ;
2021-08-23 20:43:43 +00:00
// on Trap the memory stage should be flushed going into the W stage,
// except if the instruction causing the Trap is an ecall or ebreak.
2022-06-08 16:28:09 +00:00
assign # 1 FlushW = FirstUnstalledW | ( TrapM & ~ ( BreakpointFaultM | EcallFaultM ) ) ;
2021-01-15 04:37:51 +00:00
endmodule