2021-02-15 20:51:39 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// RASPredictor.sv
|
|
|
|
//
|
2023-01-25 23:18:07 +00:00
|
|
|
// Written: Ross Thomposn ross1728@gmail.com
|
|
|
|
// Created: 15 February 2021
|
|
|
|
// Modified: 25 January 2023
|
2021-02-15 20:51:39 +00:00
|
|
|
//
|
|
|
|
// Purpose: 2 bit saturating counter predictor with parameterized table depth.
|
|
|
|
//
|
2023-01-25 23:18:07 +00:00
|
|
|
// Documentation: RISC-V System on Chip Design Chapter 10 (Figure ***)
|
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2021-02-15 20:51:39 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-02-15 20:51:39 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-02-15 20:51:39 +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-15 20:51:39 +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-15 20:51:39 +00:00
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2023-01-29 21:24:20 +00:00
|
|
|
module RASPredictor #(parameter int StackSize = 16 )(
|
2023-01-27 22:27:04 +00:00
|
|
|
input logic clk,
|
2023-01-25 23:18:07 +00:00
|
|
|
input logic reset,
|
|
|
|
input logic StallF, StallD, StallE, StallM, FlushD, FlushE, FlushM,
|
|
|
|
input logic [3:0] WrongPredInstrClassD, // Prediction class is wrong
|
|
|
|
input logic [3:0] InstrClassD, InstrClassE, PredInstrClassF, // Instr class
|
|
|
|
input logic [`XLEN-1:0] PCLinkE, // PC of instruction after a jal
|
|
|
|
output logic [`XLEN-1:0] RASPCF // Top of the stack
|
2021-02-15 20:51:39 +00:00
|
|
|
);
|
|
|
|
|
2021-10-27 19:43:55 +00:00
|
|
|
logic CounterEn;
|
2021-02-15 20:51:39 +00:00
|
|
|
localparam Depth = $clog2(StackSize);
|
|
|
|
|
2023-02-10 15:06:51 +00:00
|
|
|
logic [Depth-1:0] NextPtr, Ptr, P1, M1, IncDecPtr;
|
2021-10-27 19:43:55 +00:00
|
|
|
logic [StackSize-1:0] [`XLEN-1:0] memory;
|
|
|
|
integer index;
|
2023-01-25 23:18:07 +00:00
|
|
|
|
2023-01-25 23:06:25 +00:00
|
|
|
logic PopF;
|
|
|
|
logic PushE;
|
2023-01-25 23:10:52 +00:00
|
|
|
logic RepairD;
|
2023-01-26 05:33:03 +00:00
|
|
|
logic IncrRepairD, DecRepairD;
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2023-01-25 23:18:07 +00:00
|
|
|
logic DecrementPtr;
|
2023-01-31 20:54:05 +00:00
|
|
|
logic FlushedRetDE;
|
|
|
|
logic WrongPredRetD;
|
|
|
|
|
2023-01-25 23:06:25 +00:00
|
|
|
|
2023-01-25 23:10:52 +00:00
|
|
|
assign PopF = PredInstrClassF[2] & ~StallD & ~FlushD;
|
2023-01-31 20:54:05 +00:00
|
|
|
assign PushE = InstrClassE[3] & ~StallM & ~FlushM;
|
2023-01-26 05:33:03 +00:00
|
|
|
|
2023-01-31 20:54:05 +00:00
|
|
|
assign WrongPredRetD = (WrongPredInstrClassD[2]) & ~StallE & ~FlushE;
|
2023-01-31 21:17:32 +00:00
|
|
|
assign FlushedRetDE = (~StallE & FlushE & InstrClassD[2]) | (~StallM & FlushM & InstrClassE[2]); // flushed ret
|
2023-01-26 05:33:03 +00:00
|
|
|
|
2023-01-31 20:54:05 +00:00
|
|
|
assign RepairD = WrongPredRetD | FlushedRetDE ;
|
|
|
|
|
|
|
|
assign IncrRepairD = FlushedRetDE | (WrongPredRetD & ~InstrClassD[2]); // Guessed it was a ret, but its not
|
|
|
|
|
|
|
|
assign DecRepairD = WrongPredRetD & InstrClassD[2]; // Guessed non ret but is a ret.
|
2023-01-25 23:06:25 +00:00
|
|
|
|
2023-01-25 23:18:07 +00:00
|
|
|
assign CounterEn = PopF | PushE | RepairD;
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2023-01-26 05:33:03 +00:00
|
|
|
assign DecrementPtr = (PopF | DecRepairD) & ~IncrRepairD;
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2023-02-10 15:06:51 +00:00
|
|
|
assign P1 = 1;
|
|
|
|
assign M1 = '1; // -1
|
|
|
|
mux2 #(Depth) PtrMux(P1, M1, DecrementPtr, IncDecPtr);
|
|
|
|
assign NextPtr = Ptr + IncDecPtr;
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2023-02-03 23:40:20 +00:00
|
|
|
flopenr #(Depth) PTR(clk, reset, CounterEn, NextPtr, Ptr);
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2021-02-20 02:09:07 +00:00
|
|
|
// RAS must be reset.
|
2021-10-27 14:57:11 +00:00
|
|
|
always_ff @ (posedge clk) begin
|
2021-02-20 02:09:07 +00:00
|
|
|
if(reset) begin
|
|
|
|
for(index=0; index<StackSize; index++)
|
2023-01-25 23:10:52 +00:00
|
|
|
memory[index] <= {`XLEN{1'b0}};
|
2023-01-13 21:56:10 +00:00
|
|
|
end else if(PushE) begin
|
2023-02-10 15:06:51 +00:00
|
|
|
memory[NextPtr] <= #1 PCLinkE;
|
2021-02-15 20:51:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-03 23:40:20 +00:00
|
|
|
assign RASPCF = memory[Ptr];
|
2021-02-15 20:51:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
endmodule
|
|
|
|
|
|
|
|
|
|
|
|
|