cvw/src/ifu/bpred/gshare.sv

115 lines
4.4 KiB
Systemverilog
Raw Normal View History

2023-01-06 06:12:08 +00:00
///////////////////////////////////////////
2023-02-21 00:45:45 +00:00
// gshare.sv
2023-01-06 06:12:08 +00:00
//
2023-02-21 00:45:45 +00:00
// Written: Ross Thompson
// Email: ross1728@gmail.com
// Created: 16 March 2021
// Adapted from ssanghai@hmc.edu (Shreya Sanghai)
// Modified: 20 February 2023
2023-01-06 06:12:08 +00:00
//
2023-02-21 00:45:45 +00:00
// Purpose: gshare and Global History Branch predictors
2023-01-06 06:12:08 +00:00
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2023-01-06 06:12:08 +00:00
//
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2023-01-06 06:12:08 +00:00
//
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2023-01-06 06:12:08 +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
2023-01-06 06:12:08 +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.
2023-01-06 06:12:08 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////
`include "wally-config.vh"
module gshare #(parameter k = 10,
parameter integer TYPE = 1) (
2023-01-27 22:27:04 +00:00
input logic clk,
input logic reset,
input logic StallF, StallD, StallE, StallM, StallW,
input logic FlushD, FlushE, FlushM, FlushW,
2023-02-25 01:51:47 +00:00
output logic [1:0] BPDirPredF,
output logic BPDirPredWrongE,
2023-01-27 22:27:04 +00:00
// update
2023-02-25 00:02:00 +00:00
input logic [`XLEN-1:0] PCNextF, PCF, PCD, PCE, PCM, PCW,
input logic BPBranchF, BranchD, BranchE, BranchM, BranchW, PCSrcE
2023-01-27 22:27:04 +00:00
);
2023-01-06 06:12:08 +00:00
2023-02-25 00:02:00 +00:00
logic MatchF, MatchD, MatchE, MatchM, MatchW;
2023-02-25 00:20:42 +00:00
logic MatchX;
2023-02-25 01:56:55 +00:00
logic [1:0] TableBPDirPredF, BPDirPredD, BPDirPredE, FwdNewDirPredF;
2023-02-25 01:51:47 +00:00
logic [1:0] NewBPDirPredE, NewBPDirPredM, NewBPDirPredW;
2023-01-06 06:12:08 +00:00
2023-02-25 00:02:00 +00:00
logic [k-1:0] IndexNextF, IndexF, IndexD, IndexE, IndexM, IndexW;
logic [k-1:0] GHRF, GHRD, GHRE, GHRM;
logic [k-1:0] GHRNextM, GHRNextF;
2023-01-06 06:12:08 +00:00
logic PCSrcM;
if(TYPE == 1) begin
assign IndexNextF = GHRNextF ^ {PCNextF[k+1] ^ PCNextF[1], PCNextF[k:2]};
assign IndexF = GHRF ^ {PCF[k+1] ^ PCF[1], PCF[k:2]};
assign IndexD = GHRD ^ {PCD[k+1] ^ PCD[1], PCD[k:2]};
assign IndexE = GHRE ^ {PCE[k+1] ^ PCE[1], PCE[k:2]};
assign IndexM = GHRM ^ {PCM[k+1] ^ PCM[1], PCM[k:2]};
end else if(TYPE == 0) begin
assign IndexNextF = GHRNextF;
assign IndexF = GHRF;
assign IndexD = GHRD;
assign IndexE = GHRE;
assign IndexM = GHRM;
end
2023-02-25 00:02:00 +00:00
flopenrc #(k) IndexWReg(clk, reset, FlushW, ~StallW, IndexM, IndexW);
2023-02-25 00:02:00 +00:00
assign MatchD = BranchD & ~FlushE & (IndexF == IndexD);
assign MatchE = BranchE & ~FlushM & (IndexF == IndexE);
assign MatchM = BranchM & ~FlushW & (IndexF == IndexM);
assign MatchW = BranchW & ~FlushW & (IndexF == IndexW);
assign MatchX = MatchD | MatchE | MatchM | MatchW;
2023-02-25 01:56:55 +00:00
assign FwdNewDirPredF = MatchD ? {2{BPDirPredD[1]}} :
2023-02-25 01:51:47 +00:00
MatchE ? {NewBPDirPredE} :
MatchM ? {NewBPDirPredM} :
NewBPDirPredW ;
2023-01-06 06:12:08 +00:00
2023-02-25 01:56:55 +00:00
assign BPDirPredF = MatchX ? FwdNewDirPredF : TableBPDirPredF;
ram2p1r1wbe #(2**k, 2) PHT(.clk(clk),
2023-01-06 06:12:08 +00:00
.ce1(~StallF), .ce2(~StallM & ~FlushM),
.ra1(IndexNextF),
2023-02-25 01:51:47 +00:00
.rd1(TableBPDirPredF),
.wa2(IndexM),
2023-02-25 01:51:47 +00:00
.wd2(NewBPDirPredM),
2023-02-23 22:19:03 +00:00
.we2(BranchM),
2023-01-06 06:12:08 +00:00
.bwe2(1'b1));
2023-02-25 01:51:47 +00:00
flopenrc #(2) PredictionRegD(clk, reset, FlushD, ~StallD, BPDirPredF, BPDirPredD);
flopenrc #(2) PredictionRegE(clk, reset, FlushE, ~StallE, BPDirPredD, BPDirPredE);
2023-01-06 06:12:08 +00:00
2023-02-25 01:51:47 +00:00
satCounter2 BPDirUpdateE(.BrDir(PCSrcE), .OldState(BPDirPredE), .NewState(NewBPDirPredE));
flopenrc #(2) NewPredictionRegM(clk, reset, FlushM, ~StallM, NewBPDirPredE, NewBPDirPredM);
flopenrc #(2) NewPredictionRegW(clk, reset, FlushW, ~StallW, NewBPDirPredM, NewBPDirPredW);
2023-01-06 06:12:08 +00:00
2023-02-25 01:51:47 +00:00
assign BPDirPredWrongE = PCSrcE != BPDirPredE[1] & BranchE;
2023-01-06 06:12:08 +00:00
2023-02-25 01:51:47 +00:00
assign GHRNextF = BPBranchF ? {BPDirPredF[1], GHRF[k-1:1]} : GHRF;
assign GHRF = BranchD ? {BPDirPredD[1], GHRD[k-1:1]} : GHRD;
2023-02-23 22:19:03 +00:00
assign GHRD = BranchE ? {PCSrcE, GHRE[k-1:1]} : GHRE;
assign GHRE = BranchM ? {PCSrcM, GHRM[k-1:1]} : GHRM;
2023-01-06 06:12:08 +00:00
assign GHRNextM = {PCSrcM, GHRM[k-1:1]};
2023-01-06 06:12:08 +00:00
2023-02-23 22:19:03 +00:00
flopenr #(k) GHRReg(clk, reset, ~StallW & ~FlushW & BranchM, GHRNextM, GHRM);
flopenrc #(1) PCSrcMReg(clk, reset, FlushM, ~StallM, PCSrcE, PCSrcM);
2023-01-06 06:12:08 +00:00
endmodule