2021-02-14 16:47:01 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// bpred.sv
|
|
|
|
//
|
2023-01-19 21:06:37 +00:00
|
|
|
// Written: Ross Thomposn ross1728@gmail.com
|
|
|
|
// Created: 12 February 2021
|
|
|
|
// Modified: 19 January 2023
|
2021-02-14 16:47:01 +00:00
|
|
|
//
|
2023-01-19 21:06:37 +00:00
|
|
|
// Purpose: Branch direction prediction and jump/branch target prediction.
|
|
|
|
// Prediction made during the fetch stage and corrected in the execution stage.
|
2021-02-14 16:47:01 +00:00
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2021-02-14 16:47:01 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-02-14 16:47:01 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-02-14 16:47:01 +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-02-14 16:47:01 +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-02-14 16:47:01 +00:00
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2023-02-28 21:21:56 +00:00
|
|
|
`define INSTR_CLASS_PRED 0
|
2023-01-26 01:39:18 +00:00
|
|
|
|
2022-12-23 03:36:49 +00:00
|
|
|
module bpred (
|
2023-01-24 14:14:31 +00:00
|
|
|
input logic clk, reset,
|
|
|
|
input logic StallF, StallD, StallE, StallM, StallW,
|
|
|
|
input logic FlushD, FlushE, FlushM, FlushW,
|
2023-01-24 13:42:34 +00:00
|
|
|
// Fetch stage
|
|
|
|
// the prediction
|
2023-01-24 14:14:31 +00:00
|
|
|
input logic [31:0] InstrD, // Decompressed decode stage instruction. Used to decode instruction class
|
|
|
|
input logic [`XLEN-1:0] PCNextF, // Next Fetch Address
|
|
|
|
input logic [`XLEN-1:0] PCPlus2or4F, // PCF+2/4
|
2023-02-28 21:37:25 +00:00
|
|
|
output logic [`XLEN-1:0] PC1NextF, // Branch Predictor predicted or corrected fetch address on miss prediction
|
2023-01-24 13:42:34 +00:00
|
|
|
output logic [`XLEN-1:0] NextValidPCE, // Address of next valid instruction after the instruction in the Memory stage
|
2022-12-20 04:51:55 +00:00
|
|
|
|
2023-01-24 13:42:34 +00:00
|
|
|
// Update Predictor
|
2023-01-24 14:14:31 +00:00
|
|
|
input logic [`XLEN-1:0] PCF, // Fetch stage instruction address
|
|
|
|
input logic [`XLEN-1:0] PCD, // Decode stage instruction address. Also the address the branch predictor took
|
|
|
|
input logic [`XLEN-1:0] PCE, // Execution stage instruction address
|
|
|
|
input logic [`XLEN-1:0] PCM, // Memory stage instruction address
|
2022-12-20 04:51:55 +00:00
|
|
|
|
2023-01-29 21:24:20 +00:00
|
|
|
input logic [31:0] PostSpillInstrRawF, // Instruction
|
|
|
|
|
2023-01-24 13:42:34 +00:00
|
|
|
// Branch and jump outcome
|
2023-02-10 16:33:10 +00:00
|
|
|
input logic InstrValidD, InstrValidE,
|
2023-02-10 21:45:56 +00:00
|
|
|
input logic BranchD, BranchE,
|
|
|
|
input logic JumpD, JumpE,
|
2023-01-29 21:24:20 +00:00
|
|
|
input logic PCSrcE, // Executation stage branch is taken
|
|
|
|
input logic [`XLEN-1:0] IEUAdrE, // The branch/jump target address
|
2023-02-20 21:39:42 +00:00
|
|
|
input logic [`XLEN-1:0] IEUAdrM, // The branch/jump target address
|
2023-01-29 21:24:20 +00:00
|
|
|
input logic [`XLEN-1:0] PCLinkE, // The address following the branch instruction. (AKA Fall through address)
|
2023-02-27 02:20:30 +00:00
|
|
|
output logic [3:0] InstrClassM, // The valid instruction class. 1-hot encoded as call, return, jr (not return), j, br
|
|
|
|
output logic JumpOrTakenBranchM, // The valid instruction class. 1-hot encoded as call, return, jr (not return), j, br
|
2022-12-23 03:36:49 +00:00
|
|
|
|
2023-01-24 13:42:34 +00:00
|
|
|
// Report branch prediction status
|
2023-02-27 06:39:19 +00:00
|
|
|
output logic BPWrongE, // Prediction is wrong
|
2023-02-28 21:37:25 +00:00
|
|
|
output logic BPWrongM, // Prediction is wrong
|
2023-02-25 01:51:47 +00:00
|
|
|
output logic BPDirPredWrongM, // Prediction direction is wrong
|
2023-01-24 13:42:34 +00:00
|
|
|
output logic BTBPredPCWrongM, // Prediction target wrong
|
|
|
|
output logic RASPredPCWrongM, // RAS prediction is wrong
|
|
|
|
output logic PredictionInstrClassWrongM // Class prediction is wrong
|
2023-01-29 21:24:20 +00:00
|
|
|
);
|
2021-02-14 16:47:01 +00:00
|
|
|
|
2023-02-27 03:28:36 +00:00
|
|
|
logic [1:0] BPDirPredF;
|
2021-02-14 16:47:01 +00:00
|
|
|
|
2023-02-27 03:28:36 +00:00
|
|
|
logic [`XLEN-1:0] BTAF, RASPCF;
|
2023-02-27 06:39:19 +00:00
|
|
|
logic BPPCWrongE;
|
2023-02-27 03:28:36 +00:00
|
|
|
logic AnyWrongPredInstrClassD, AnyWrongPredInstrClassE;
|
|
|
|
logic BPDirPredWrongE;
|
2021-03-24 02:49:16 +00:00
|
|
|
|
2023-02-27 03:28:36 +00:00
|
|
|
logic BPPCSrcF;
|
|
|
|
logic [`XLEN-1:0] BPPCF;
|
2023-02-28 21:37:25 +00:00
|
|
|
logic [`XLEN-1:0] PC0NextF;
|
2023-02-27 03:28:36 +00:00
|
|
|
logic [`XLEN-1:0] PCCorrectE;
|
|
|
|
logic [3:0] WrongPredInstrClassD;
|
|
|
|
|
|
|
|
logic BTBTargetWrongE;
|
|
|
|
logic RASTargetWrongE;
|
|
|
|
|
|
|
|
logic [`XLEN-1:0] BTAD;
|
|
|
|
|
|
|
|
logic BTBCallF, BTBReturnF, BTBJumpF, BTBBranchF;
|
|
|
|
logic BPBranchF, BPJumpF, BPReturnF, BPCallF;
|
|
|
|
logic BPBranchD, BPJumpD, BPReturnD, BPCallD;
|
|
|
|
logic ReturnD, CallD;
|
|
|
|
logic ReturnE, CallE;
|
|
|
|
logic BranchM, JumpM, ReturnM, CallM;
|
|
|
|
logic BranchW, JumpW, ReturnW, CallW;
|
|
|
|
logic WrongBPReturnD;
|
2023-02-27 23:37:29 +00:00
|
|
|
logic [`XLEN-1:0] BTAE;
|
2023-02-28 21:57:34 +00:00
|
|
|
|
|
|
|
|
2023-02-27 23:37:29 +00:00
|
|
|
|
2021-03-04 15:23:35 +00:00
|
|
|
// Part 1 branch direction prediction
|
2022-12-20 18:58:54 +00:00
|
|
|
// look into the 2 port Sram model. something is wrong.
|
2023-02-21 00:36:33 +00:00
|
|
|
if (`BPRED_TYPE == "BP_TWOBIT") begin:Predictor
|
2023-02-23 21:55:34 +00:00
|
|
|
twoBitPredictor #(`BPRED_SIZE) DirPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW,
|
|
|
|
.FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-25 01:51:47 +00:00
|
|
|
.PCNextF, .PCM, .BPDirPredF, .BPDirPredWrongE,
|
2023-02-23 22:19:03 +00:00
|
|
|
.BranchE, .BranchM, .PCSrcE);
|
2021-03-16 20:06:40 +00:00
|
|
|
|
2023-02-21 00:36:33 +00:00
|
|
|
end else if (`BPRED_TYPE == "BP_GSHARE") begin:Predictor
|
2023-02-14 00:52:52 +00:00
|
|
|
gshare #(`BPRED_SIZE) DirPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-25 04:55:51 +00:00
|
|
|
.PCNextF, .PCF, .PCD, .PCE, .PCM, .BPDirPredF, .BPDirPredWrongE,
|
2023-02-25 00:02:00 +00:00
|
|
|
.BPBranchF, .BranchD, .BranchE, .BranchM, .BranchW,
|
2023-02-21 00:36:33 +00:00
|
|
|
.PCSrcE);
|
2023-01-05 20:18:00 +00:00
|
|
|
|
2023-02-21 00:36:33 +00:00
|
|
|
end else if (`BPRED_TYPE == "BP_GLOBAL") begin:Predictor
|
2023-02-22 15:11:46 +00:00
|
|
|
gshare #(`BPRED_SIZE, 0) DirPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-25 04:55:51 +00:00
|
|
|
.PCNextF, .PCF, .PCD, .PCE, .PCM, .BPDirPredF, .BPDirPredWrongE,
|
2023-02-25 00:02:00 +00:00
|
|
|
.BPBranchF, .BranchD, .BranchE, .BranchM, .BranchW,
|
2023-02-19 06:17:37 +00:00
|
|
|
.PCSrcE);
|
2023-02-21 00:36:33 +00:00
|
|
|
|
|
|
|
end else if (`BPRED_TYPE == "BP_GSHARE_BASIC") begin:Predictor
|
|
|
|
gsharebasic #(`BPRED_SIZE) DirPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-25 01:51:47 +00:00
|
|
|
.PCNextF, .PCM, .BPDirPredF, .BPDirPredWrongE,
|
2023-02-23 22:19:03 +00:00
|
|
|
.BranchE, .BranchM, .PCSrcE);
|
2023-02-21 00:36:33 +00:00
|
|
|
|
|
|
|
end else if (`BPRED_TYPE == "BP_GLOBAL_BASIC") begin:Predictor
|
2023-02-22 15:11:46 +00:00
|
|
|
gsharebasic #(`BPRED_SIZE, 0) DirPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-25 01:51:47 +00:00
|
|
|
.PCNextF, .PCM, .BPDirPredF, .BPDirPredWrongE,
|
2023-02-23 22:19:03 +00:00
|
|
|
.BranchE, .BranchM, .PCSrcE);
|
2023-02-19 06:17:37 +00:00
|
|
|
|
2023-01-29 04:06:12 +00:00
|
|
|
end else if (`BPRED_TYPE == "BPLOCALPAg") begin:Predictor
|
2023-01-05 20:04:09 +00:00
|
|
|
// *** Fix me
|
|
|
|
/* -----\/----- EXCLUDED -----\/-----
|
2022-02-01 20:32:27 +00:00
|
|
|
localHistoryPredictor DirPredictor(.clk,
|
2022-12-11 22:28:11 +00:00
|
|
|
.reset, .StallF, .StallE,
|
2022-01-05 16:25:08 +00:00
|
|
|
.LookUpPC(PCNextF),
|
2023-02-25 01:51:47 +00:00
|
|
|
.Prediction(BPDirPredF),
|
2022-01-05 16:25:08 +00:00
|
|
|
// update
|
|
|
|
.UpdatePC(PCE),
|
|
|
|
.UpdateEN(InstrClassE[0] & ~StallE),
|
2022-02-01 20:32:27 +00:00
|
|
|
.PCSrcE,
|
2023-01-05 20:04:09 +00:00
|
|
|
.UpdatePrediction(InstrClassE[0]));
|
|
|
|
-----/\----- EXCLUDED -----/\----- */
|
2022-01-05 16:25:08 +00:00
|
|
|
end
|
2021-03-16 20:06:40 +00:00
|
|
|
|
2021-03-04 15:23:35 +00:00
|
|
|
// Part 2 Branch target address prediction
|
2023-02-09 00:24:38 +00:00
|
|
|
// BTB contains target address for all CFI
|
2021-02-14 16:47:01 +00:00
|
|
|
|
2023-02-20 04:13:50 +00:00
|
|
|
btb #(`BTB_SIZE)
|
2023-02-20 23:54:22 +00:00
|
|
|
TargetPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-27 03:28:36 +00:00
|
|
|
.PCNextF, .PCF, .PCD, .PCE, .PCM,
|
2023-02-27 23:37:29 +00:00
|
|
|
.BTAF, .BTAD, .BTAE,
|
2023-02-27 02:20:30 +00:00
|
|
|
.BTBIClassF({BTBCallF, BTBReturnF, BTBJumpF, BTBBranchF}),
|
2023-02-27 23:37:29 +00:00
|
|
|
.PredictionInstrClassWrongM, .AnyWrongPredInstrClassE,
|
2023-02-27 03:28:36 +00:00
|
|
|
.IEUAdrE, .IEUAdrM,
|
|
|
|
.InstrClassD({CallD, ReturnD, JumpD, BranchD}),
|
|
|
|
.InstrClassE({CallE, ReturnE, JumpE, BranchE}),
|
|
|
|
.InstrClassM({CallM, ReturnM, JumpM, BranchM}),
|
2023-02-27 02:20:30 +00:00
|
|
|
.InstrClassW({CallW, ReturnW, JumpW, BranchW}));
|
2023-01-31 05:55:52 +00:00
|
|
|
|
2023-02-28 21:21:56 +00:00
|
|
|
icpred #(`INSTR_CLASS_PRED) icpred(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .StallW, .FlushD, .FlushE, .FlushM, .FlushW,
|
2023-02-27 02:20:30 +00:00
|
|
|
.PostSpillInstrRawF, .InstrD, .BranchD, .BranchE, .JumpD, .JumpE, .BranchM, .BranchW, .JumpM, .JumpW,
|
2023-02-27 03:28:36 +00:00
|
|
|
.CallD, .CallE, .CallM, .CallW, .ReturnD, .ReturnE, .ReturnM, .ReturnW, .BTBCallF, .BTBReturnF, .BTBJumpF,
|
2023-02-28 21:57:34 +00:00
|
|
|
.BTBBranchF, .BPCallF, .BPReturnF, .BPJumpF, .BPBranchF, .PredictionInstrClassWrongM, .AnyWrongPredInstrClassE, .WrongBPReturnD);
|
2022-02-01 20:32:27 +00:00
|
|
|
|
2023-02-27 02:20:30 +00:00
|
|
|
// Part 3 RAS
|
|
|
|
RASPredictor RASPredictor(.clk, .reset, .StallF, .StallD, .StallE, .StallM, .FlushD, .FlushE, .FlushM,
|
|
|
|
.BPReturnF, .ReturnD, .ReturnE, .CallE,
|
|
|
|
.WrongBPReturnD, .RASPCF, .PCLinkE);
|
|
|
|
|
2022-12-23 02:33:38 +00:00
|
|
|
// Check the prediction
|
2023-02-07 20:01:59 +00:00
|
|
|
// if it is a CFI then check if the next instruction address (PCD) matches the branch's target or fallthrough address.
|
|
|
|
// if the class prediction is wrong a regular instruction may have been predicted as a taken branch
|
|
|
|
// this will result in PCD not being equal to the fall through address PCLinkE (PCE+4).
|
|
|
|
// The next instruction is always valid as no other flush would occur at the same time as the branch and not
|
|
|
|
// also flush the branch. This will change in a superscaler cpu.
|
2023-02-10 16:33:10 +00:00
|
|
|
// branch is wrong only if the PC does not match and both the Decode and Fetch stages have valid instructions.
|
2023-02-28 02:00:50 +00:00
|
|
|
assign BPWrongE = (PCCorrectE != PCD) & InstrValidE & InstrValidD;
|
2023-02-28 21:37:25 +00:00
|
|
|
flopenrc #(1) BPWrongMReg(clk, reset, FlushM, ~StallM, BPWrongE, BPWrongM);
|
2023-02-27 03:28:36 +00:00
|
|
|
|
2023-02-07 20:01:59 +00:00
|
|
|
// Output the predicted PC or corrected PC on miss-predict.
|
2023-02-27 03:28:36 +00:00
|
|
|
assign BPPCSrcF = (BPBranchF & BPDirPredF[1]) | BPJumpF;
|
|
|
|
mux2 #(`XLEN) pcmuxbp(BTAF, RASPCF, BPReturnF, BPPCF);
|
2023-02-07 20:01:59 +00:00
|
|
|
// Selects the BP or PC+2/4.
|
2023-02-28 21:37:25 +00:00
|
|
|
mux2 #(`XLEN) pcmux0(PCPlus2or4F, BPPCF, BPPCSrcF, PC0NextF);
|
2023-02-07 20:01:59 +00:00
|
|
|
// If the prediction is wrong select the correct address.
|
2023-02-28 21:37:25 +00:00
|
|
|
mux2 #(`XLEN) pcmux1(PC0NextF, PCCorrectE, BPWrongE, PC1NextF);
|
2023-02-07 20:01:59 +00:00
|
|
|
// Correct branch/jump target.
|
|
|
|
mux2 #(`XLEN) pccorrectemux(PCLinkE, IEUAdrE, PCSrcE, PCCorrectE);
|
2021-03-31 16:54:02 +00:00
|
|
|
|
2023-02-07 20:01:59 +00:00
|
|
|
// If the fence/csrw was predicted as a taken branch then we select PCF, rather PCE.
|
|
|
|
// Effectively this is PCM+4 or the non-existant PCLinkM
|
2023-02-28 21:37:25 +00:00
|
|
|
if(`INSTR_CLASS_PRED) mux2 #(`XLEN) pcmuxBPWrongInvalidateFlush(PCE, PCF, BPWrongM, NextValidPCE);
|
2023-02-07 20:01:59 +00:00
|
|
|
else assign NextValidPCE = PCE;
|
|
|
|
|
2023-02-23 20:39:31 +00:00
|
|
|
if(`ZICOUNTERS_SUPPORTED) begin
|
2023-02-27 03:28:36 +00:00
|
|
|
logic JumpOrTakenBranchE;
|
2023-02-27 23:37:29 +00:00
|
|
|
logic [`XLEN-1:0] RASPCD, RASPCE;
|
2023-02-27 03:28:36 +00:00
|
|
|
logic BTBPredPCWrongE, RASPredPCWrongE;
|
|
|
|
// performance counters
|
|
|
|
// 1. class (class wrong / minstret) (PredictionInstrClassWrongM / csr) // Correct now
|
|
|
|
// 2. target btb (btb target wrong / class[0,1,3]) (btb target wrong / (br + j + jal)
|
|
|
|
// 3. target ras (ras target wrong / class[2])
|
|
|
|
// 4. direction (br dir wrong / class[0])
|
|
|
|
|
|
|
|
// Unforuantely we can't use PCD to infer the correctness of the BTB or RAS because the class prediction
|
|
|
|
// could be wrong or the fall through address selected for branch predict not taken.
|
|
|
|
// By pipeline the BTB's PC and RAS address through the pipeline we can measure the accuracy of
|
|
|
|
// both without the above inaccuracies.
|
|
|
|
assign BTBPredPCWrongE = (BTAE != IEUAdrE) & (BranchE | JumpE & ~ReturnE) & PCSrcE;
|
|
|
|
assign RASPredPCWrongE = (RASPCE != IEUAdrE) & ReturnE & PCSrcE;
|
|
|
|
|
|
|
|
assign JumpOrTakenBranchE = (BranchE & PCSrcE) | JumpE;
|
|
|
|
|
|
|
|
flopenrc #(1) JumpOrTakenBranchMReg(clk, reset, FlushM, ~StallM, JumpOrTakenBranchE, JumpOrTakenBranchM);
|
|
|
|
|
|
|
|
flopenrc #(`XLEN) RASTargetDReg(clk, reset, FlushD, ~StallD, RASPCF, RASPCD);
|
|
|
|
flopenrc #(`XLEN) RASTargetEReg(clk, reset, FlushE, ~StallE, RASPCD, RASPCE);
|
|
|
|
flopenrc #(3) BPPredWrongRegM(clk, reset, FlushM, ~StallM,
|
|
|
|
{BPDirPredWrongE, BTBPredPCWrongE, RASPredPCWrongE},
|
|
|
|
{BPDirPredWrongM, BTBPredPCWrongM, RASPredPCWrongM});
|
|
|
|
|
2023-02-23 20:39:31 +00:00
|
|
|
end else begin
|
2023-02-27 03:28:36 +00:00
|
|
|
assign {BTBPredPCWrongM, RASPredPCWrongM, JumpOrTakenBranchM} = '0;
|
2023-02-23 20:39:31 +00:00
|
|
|
end
|
2023-02-23 21:55:34 +00:00
|
|
|
|
|
|
|
// **** Fix me
|
2023-02-27 02:20:30 +00:00
|
|
|
assign InstrClassM = {CallM, ReturnM, JumpM, BranchM};
|
2022-12-20 18:58:54 +00:00
|
|
|
|
2021-02-14 21:06:53 +00:00
|
|
|
endmodule
|