cvw/pipelined/src/ifu/brpred/localHistoryPredictor.sv

134 lines
4.5 KiB
Systemverilog
Raw Normal View History

2021-04-01 16:52:40 +00:00
///////////////////////////////////////////
// locallHistoryPredictor.sv
//
// Written: Shreya Sanghai
// Email: ssanghai@hmc.edu
// Created: March 16, 2021
// Modified:
//
// Purpose: Global History Branch predictor with parameterized global history register
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-04-01 16:52:40 +00:00
//
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-04-01 16:52:40 +00:00
//
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-04-01 16:52:40 +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-04-01 16:52:40 +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.
////////////////////////////////////////////////////////////////////////////////////////////////
2021-04-01 16:52:40 +00:00
`include "wally-config.vh"
module localHistoryPredictor
#( parameter int m = 6, // 2^m = number of local history branches
parameter int k = 10 // number of past branches stored
)
2021-10-27 19:43:55 +00:00
(input logic clk,
input logic reset,
2022-12-11 22:28:11 +00:00
input logic StallF, StallE,
2021-04-01 16:52:40 +00:00
input logic [`XLEN-1:0] LookUpPC,
2021-10-27 19:43:55 +00:00
output logic [1:0] Prediction,
2021-04-01 16:52:40 +00:00
// update
input logic [`XLEN-1:0] UpdatePC,
2021-10-27 19:43:55 +00:00
input logic UpdateEN, PCSrcE,
input logic [1:0] UpdatePrediction
2021-04-01 16:52:40 +00:00
);
2021-10-27 19:43:55 +00:00
logic [2**m-1:0] [k-1:0] LHRNextF;
logic [k-1:0] LHRF, ForwardLHRNext, LHRFNext;
logic [m-1:0] LookUpPCIndex, UpdatePCIndex;
logic [1:0] PredictionMemory;
logic DoForwarding, DoForwardingF, DoForwardingPHT, DoForwardingPHTF;
logic [1:0] UpdatePredictionF;
2021-04-01 16:52:40 +00:00
assign LHRFNext = {PCSrcE, LHRF[k-1:1]};
assign UpdatePCIndex = {UpdatePC[m+1] ^ UpdatePC[1], UpdatePC[m:2]};
assign LookUpPCIndex = {LookUpPC[m+1] ^ LookUpPC[1], LookUpPC[m:2]};
2021-04-01 16:52:40 +00:00
// INCASE we do ahead pipelining
2022-12-20 16:36:45 +00:00
// ram2p1r1wb #(m,k) LHR(.clk(clk)),
// .reset(reset),
// .RA1(LookUpPCIndex), // need hashing function to get correct PC address
// .RD1(LHRF),
// .REN1(~StallF),
// .WA1(UpdatePCIndex),
// .WD1(LHRENExt),
// .WEN1(UpdateEN),
// .BitWEN1(2'b11));
2021-04-01 16:52:40 +00:00
2021-10-27 19:43:55 +00:00
genvar index;
2022-01-05 16:25:08 +00:00
for (index = 0; index < 2**m; index = index +1) begin:localhist
flopenr #(k) LocalHistoryRegister(.clk, .reset, .en(UpdateEN & (index == UpdatePCIndex)),
.d(LHRFNext), .q(LHRNextF[index]));
end
2021-04-01 16:52:40 +00:00
// need to forward when updating to the same address as reading.
// first we compare to see if the update and lookup addreses are the same
assign DoForwarding = LookUpPCIndex == UpdatePCIndex;
assign ForwardLHRNext = DoForwarding ? LHRFNext :LHRNextF[LookUpPCIndex];
2021-04-01 16:52:40 +00:00
// Make Prediction by reading the correct address in the PHT and also update the new address in the PHT
// LHR referes to the address that the past k branches points to in the prediction stage
// LHRE refers to the address that the past k branches points to in the exectution stage
2022-12-20 16:36:45 +00:00
ram2p1r1wb #(k, 2) PHT(.clk(clk),
2021-10-27 19:43:55 +00:00
.reset(reset),
2022-12-29 23:13:48 +00:00
.ra1(ForwardLHRNext),
.rd1(PredictionMemory),
.ren1(~StallF),
.wa2(LHRFNext),
.wd2(UpdatePrediction),
.wen2(UpdateEN),
.bwe2(2'b11));
2021-04-01 16:52:40 +00:00
assign DoForwardingPHT = LHRFNext == ForwardLHRNext;
2021-04-01 16:52:40 +00:00
// register the update value and the forwarding signal into the Fetch stage
// TODO: add stall logic ***
flopr #(1) DoForwardingReg(.clk(clk),
2021-10-27 19:43:55 +00:00
.reset(reset),
.d(DoForwardingPHT),
.q(DoForwardingPHTF));
2021-04-01 16:52:40 +00:00
flopr #(2) UpdatePredictionReg(.clk(clk),
2021-10-27 19:43:55 +00:00
.reset(reset),
.d(UpdatePrediction),
.q(UpdatePredictionF));
2021-04-01 16:52:40 +00:00
assign Prediction = DoForwardingPHTF ? UpdatePredictionF : PredictionMemory;
//pipeline for LHR
flopenrc #(k) LHRFReg(.clk(clk),
2021-10-27 19:43:55 +00:00
.reset(reset),
.en(~StallF),
2022-12-11 22:28:11 +00:00
.clear(1'b0),
2021-10-27 19:43:55 +00:00
.d(ForwardLHRNext),
.q(LHRF));
/*
flopenrc #(k) LHRDReg(.clk(clk),
.reset(reset),
.en(~StallD),
.clear(FlushD),
.d(LHRF),
.q(LHRD));
2021-04-01 16:52:40 +00:00
flopenrc #(k) LHREReg(.clk(clk),
.reset(reset),
.en(~StallE),
.clear(FlushE),
.d(LHRD),
.q(LHRE));
*/
2021-04-01 16:52:40 +00:00
endmodule