2023-01-24 23:19:51 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// btb.sv
|
|
|
|
//
|
|
|
|
// Written: Ross Thomposn ross1728@gmail.com
|
|
|
|
// Created: February 15, 2021
|
|
|
|
// Modified: 24 January 2023
|
|
|
|
//
|
|
|
|
// Purpose: Branch Target Buffer (BTB). The BTB predicts the target address of all control flow instructions.
|
|
|
|
// It also guesses the type of instrution; jalr(r), return, jump (jr), or branch.
|
|
|
|
//
|
|
|
|
// Documentation: RISC-V System on Chip Design Chapter 10 (Figure ***)
|
|
|
|
//
|
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
|
|
|
module btb
|
|
|
|
#(parameter int Depth = 10
|
|
|
|
)
|
|
|
|
(input logic clk,
|
|
|
|
input logic reset,
|
2023-01-25 22:03:02 +00:00
|
|
|
input logic StallF, StallD, StallM, FlushD, FlushM,
|
|
|
|
input logic [`XLEN-1:0] PCNextF, PCF, PCD, PCE, // PC at various stages
|
|
|
|
output logic [`XLEN-1:0] PredPCF, // BTB's guess at PC
|
2023-01-26 01:39:18 +00:00
|
|
|
output logic [3:0] BTBPredInstrClassF, // BTB's guess at instruction class
|
2023-01-25 22:03:02 +00:00
|
|
|
output logic PredValidF, // BTB's guess is valid
|
2023-01-24 23:19:51 +00:00
|
|
|
// update
|
2023-01-25 22:03:02 +00:00
|
|
|
input logic PredictionInstrClassWrongE, // BTB's instruction class guess was wrong
|
|
|
|
input logic [`XLEN-1:0] IEUAdrE, // Branch/jump target address to insert into btb
|
|
|
|
input logic [3:0] InstrClassE // Instruction class to insert into btb
|
2023-01-24 23:19:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
localparam TotalDepth = 2 ** Depth;
|
|
|
|
logic [TotalDepth-1:0] ValidBits;
|
2023-01-25 19:02:20 +00:00
|
|
|
logic [Depth-1:0] PCNextFIndex, PCFIndex, PCDIndex, PCEIndex;
|
2023-01-24 23:19:51 +00:00
|
|
|
logic [`XLEN-1:0] ResetPC;
|
2023-01-25 19:02:20 +00:00
|
|
|
logic MatchF, MatchD, MatchE, MatchNextX, MatchXF;
|
|
|
|
logic [`XLEN+3:0] ForwardBTBPrediction, ForwardBTBPredictionF;
|
|
|
|
logic [`XLEN+3:0] TableBTBPredictionF;
|
2023-01-25 22:03:02 +00:00
|
|
|
logic [`XLEN-1:0] PredPCD;
|
|
|
|
logic [3:0] PredInstrClassD; // *** copy of reg outside module
|
|
|
|
logic UpdateEn;
|
2023-01-28 23:00:50 +00:00
|
|
|
logic TablePredValidF;
|
2023-01-25 22:03:02 +00:00
|
|
|
|
2023-01-24 23:19:51 +00:00
|
|
|
// hashing function for indexing the PC
|
|
|
|
// We have Depth bits to index, but XLEN bits as the input.
|
|
|
|
// bit 0 is always 0, bit 1 is 0 if using 4 byte instructions, but is not always 0 if
|
|
|
|
// using compressed instructions. XOR bit 1 with the MSB of index.
|
2023-01-25 19:02:20 +00:00
|
|
|
assign PCFIndex = {PCF[Depth+1] ^ PCF[1], PCF[Depth:2]};
|
|
|
|
assign PCDIndex = {PCD[Depth+1] ^ PCD[1], PCD[Depth:2]};
|
2023-01-24 23:19:51 +00:00
|
|
|
assign PCEIndex = {PCE[Depth+1] ^ PCE[1], PCE[Depth:2]};
|
2023-01-25 18:05:13 +00:00
|
|
|
|
2023-01-25 21:29:55 +00:00
|
|
|
// must output a valid PC and valid bit during reset. Because only PCF, not PCNextF is reset, PCNextF is invalid
|
|
|
|
// during reset. The BTB must produce a non X PC1NextF to allow the simulation to run.
|
|
|
|
// While thie mux could be included in IFU it is not necessary for the IROM/I$/bus.
|
|
|
|
// For now it is optimal to leave it here.
|
2023-01-24 23:19:51 +00:00
|
|
|
assign ResetPC = `RESET_VECTOR;
|
2023-01-25 19:02:20 +00:00
|
|
|
assign PCNextFIndex = reset ? ResetPC[Depth+1:2] : {PCNextF[Depth+1] ^ PCNextF[1], PCNextF[Depth:2]};
|
|
|
|
|
|
|
|
assign MatchF = PCNextFIndex == PCFIndex;
|
|
|
|
assign MatchD = PCNextFIndex == PCDIndex;
|
|
|
|
assign MatchE = PCNextFIndex == PCEIndex;
|
|
|
|
assign MatchNextX = MatchF | MatchD | MatchE;
|
2023-01-24 23:19:51 +00:00
|
|
|
|
2023-01-25 19:02:20 +00:00
|
|
|
flopenr #(1) MatchReg(clk, reset, ~StallF, MatchNextX, MatchXF);
|
|
|
|
|
2023-01-26 01:39:18 +00:00
|
|
|
assign ForwardBTBPrediction = MatchF ? {BTBPredInstrClassF, PredPCF} :
|
2023-01-25 22:03:02 +00:00
|
|
|
MatchD ? {PredInstrClassD, PredPCD} :
|
2023-01-25 19:02:20 +00:00
|
|
|
{InstrClassE, IEUAdrE} ;
|
|
|
|
|
|
|
|
flopenr #(`XLEN+4) ForwardBTBPredicitonReg(clk, reset, ~StallF, ForwardBTBPrediction, ForwardBTBPredictionF);
|
|
|
|
|
2023-01-26 01:39:18 +00:00
|
|
|
assign {BTBPredInstrClassF, PredPCF} = MatchXF ? ForwardBTBPredictionF : TableBTBPredictionF;
|
2023-01-25 19:02:20 +00:00
|
|
|
|
2023-01-24 23:19:51 +00:00
|
|
|
always_ff @ (posedge clk) begin
|
|
|
|
if (reset) begin
|
|
|
|
ValidBits <= #1 {TotalDepth{1'b0}};
|
2023-01-25 22:03:02 +00:00
|
|
|
end else if ((UpdateEn) & ~StallM & ~FlushM) begin
|
2023-01-25 21:16:53 +00:00
|
|
|
ValidBits[PCEIndex] <= #1 |InstrClassE;
|
2023-01-24 23:19:51 +00:00
|
|
|
end
|
2023-01-29 06:49:23 +00:00
|
|
|
if(~StallF | reset) TablePredValidF = ValidBits[PCNextFIndex];
|
2023-01-24 23:19:51 +00:00
|
|
|
end
|
|
|
|
|
2023-01-28 23:00:50 +00:00
|
|
|
assign PredValidF = MatchXF ? 1'b1 : TablePredValidF;
|
|
|
|
|
2023-01-25 22:03:02 +00:00
|
|
|
assign UpdateEn = |InstrClassE | PredictionInstrClassWrongE;
|
|
|
|
|
2023-01-25 18:05:13 +00:00
|
|
|
// An optimization may be using a PC relative address.
|
2023-01-24 23:22:00 +00:00
|
|
|
ram2p1r1wbe #(2**Depth, `XLEN+4) memory(
|
2023-01-25 19:02:20 +00:00
|
|
|
.clk, .ce1(~StallF | reset), .ra1(PCNextFIndex), .rd1(TableBTBPredictionF),
|
2023-01-25 22:03:02 +00:00
|
|
|
.ce2(~StallM & ~FlushM), .wa2(PCEIndex), .wd2({InstrClassE, IEUAdrE}), .we2(UpdateEn), .bwe2('1));
|
2023-01-24 23:19:51 +00:00
|
|
|
|
2023-01-26 01:39:18 +00:00
|
|
|
flopenrc #(`XLEN+4) BTBD(clk, reset, FlushD, ~StallD, {BTBPredInstrClassF, PredPCF}, {PredInstrClassD, PredPCD});
|
2023-01-25 19:02:20 +00:00
|
|
|
|
2023-01-24 23:19:51 +00:00
|
|
|
endmodule
|