2021-02-15 20:51:39 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// SRAM2P1R1W
|
|
|
|
//
|
|
|
|
// Written: Ross Thomposn
|
|
|
|
// Email: ross1728@gmail.com
|
|
|
|
// Created: February 15, 2021
|
|
|
|
// Modified:
|
|
|
|
//
|
|
|
|
// Purpose: BTB model. Outputs type of instruction (currently 1 hot encoded. Probably want
|
|
|
|
// to encode to reduce storage), valid, target PC.
|
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
// MIT LICENSE
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
|
|
// software and associated documentation files (the "Software"), to deal in the Software
|
|
|
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
|
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
|
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
2021-02-15 20:51:39 +00:00
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or
|
|
|
|
// substantial portions of the Software.
|
2021-02-15 20:51:39 +00:00
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
|
|
// OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-02-15 20:51:39 +00:00
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
|
|
|
module BTBPredictor
|
|
|
|
#(parameter int Depth = 10
|
|
|
|
)
|
2021-10-27 19:43:55 +00:00
|
|
|
(input logic clk,
|
|
|
|
input logic reset,
|
|
|
|
input logic StallF, StallE,
|
2021-02-19 03:32:15 +00:00
|
|
|
input logic [`XLEN-1:0] LookUpPC,
|
2021-02-15 20:51:39 +00:00
|
|
|
output logic [`XLEN-1:0] TargetPC,
|
2021-10-27 19:43:55 +00:00
|
|
|
output logic [4:0] InstrClass,
|
|
|
|
output logic Valid,
|
2021-02-15 20:51:39 +00:00
|
|
|
// update
|
2021-10-27 19:43:55 +00:00
|
|
|
input logic UpdateEN,
|
2021-02-19 03:32:15 +00:00
|
|
|
input logic [`XLEN-1:0] UpdatePC,
|
2021-03-04 15:23:35 +00:00
|
|
|
input logic [`XLEN-1:0] UpdateTarget,
|
2021-10-27 19:43:55 +00:00
|
|
|
input logic [4:0] UpdateInstrClass,
|
|
|
|
input logic UpdateInvalid
|
2021-02-15 20:51:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
localparam TotalDepth = 2 ** Depth;
|
|
|
|
logic [TotalDepth-1:0] ValidBits;
|
2021-10-27 19:43:55 +00:00
|
|
|
logic [Depth-1:0] LookUpPCIndex, UpdatePCIndex, LookUpPCIndexQ, UpdatePCIndexQ;
|
|
|
|
logic UpdateENQ;
|
2021-03-24 01:20:23 +00:00
|
|
|
|
2021-02-15 20:51:39 +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.
|
|
|
|
assign UpdatePCIndex = {UpdatePC[Depth+1] ^ UpdatePC[1], UpdatePC[Depth:2]};
|
|
|
|
assign LookUpPCIndex = {LookUpPC[Depth+1] ^ LookUpPC[1], LookUpPC[Depth:2]};
|
|
|
|
|
2021-02-19 03:32:15 +00:00
|
|
|
|
|
|
|
flopenr #(Depth) UpdatePCIndexReg(.clk(clk),
|
2021-10-27 19:43:55 +00:00
|
|
|
.reset(reset),
|
|
|
|
.en(~StallE),
|
|
|
|
.d(UpdatePCIndex),
|
|
|
|
.q(UpdatePCIndexQ));
|
2021-02-15 20:51:39 +00:00
|
|
|
|
|
|
|
// The valid bit must be resetable.
|
|
|
|
always_ff @ (posedge clk) begin
|
|
|
|
if (reset) begin
|
|
|
|
ValidBits <= #1 {TotalDepth{1'b0}};
|
2021-03-24 01:06:45 +00:00
|
|
|
end else
|
2021-03-24 01:20:23 +00:00
|
|
|
if (UpdateENQ) begin
|
2021-03-24 02:49:16 +00:00
|
|
|
ValidBits[UpdatePCIndexQ] <= #1 ~ UpdateInvalid;
|
2021-02-15 20:51:39 +00:00
|
|
|
end
|
|
|
|
end
|
2021-03-24 01:06:45 +00:00
|
|
|
assign Valid = ValidBits[LookUpPCIndexQ];
|
|
|
|
|
|
|
|
|
2021-03-31 16:54:02 +00:00
|
|
|
flopenr #(1) UpdateENReg(.clk(clk),
|
2021-10-27 19:43:55 +00:00
|
|
|
.reset(reset),
|
|
|
|
.en(~StallF),
|
|
|
|
.d(UpdateEN),
|
|
|
|
.q(UpdateENQ));
|
2021-03-24 01:20:23 +00:00
|
|
|
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2021-02-19 03:32:15 +00:00
|
|
|
flopenr #(Depth) LookupPCIndexReg(.clk(clk),
|
2021-10-27 19:43:55 +00:00
|
|
|
.reset(reset),
|
|
|
|
.en(~StallF),
|
|
|
|
.d(LookUpPCIndex),
|
|
|
|
.q(LookUpPCIndexQ));
|
2021-02-19 03:32:15 +00:00
|
|
|
|
2021-03-24 01:06:45 +00:00
|
|
|
|
2021-02-19 03:32:15 +00:00
|
|
|
|
2021-02-15 20:51:39 +00:00
|
|
|
// the BTB contains the target address.
|
|
|
|
// Another optimization may be using a PC relative address.
|
2021-03-04 19:01:41 +00:00
|
|
|
// *** need to add forwarding.
|
2021-02-15 20:51:39 +00:00
|
|
|
|
2022-03-29 17:11:28 +00:00
|
|
|
// *** optimize for byte write enables
|
2021-03-30 18:57:40 +00:00
|
|
|
SRAM2P1R1W #(Depth, `XLEN+5) memory(.clk(clk),
|
2021-10-27 19:43:55 +00:00
|
|
|
.reset(reset),
|
|
|
|
.RA1(LookUpPCIndex),
|
|
|
|
.RD1({{InstrClass, TargetPC}}),
|
|
|
|
.REN1(~StallF),
|
|
|
|
.WA1(UpdatePCIndex),
|
|
|
|
.WD1({UpdateInstrClass, UpdateTarget}),
|
|
|
|
.WEN1(UpdateEN),
|
|
|
|
.BitWEN1({5'h1F, {`XLEN{1'b1}}})); // *** definitely not right.
|
2021-02-15 20:51:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
endmodule
|