109c6f1ddSLingrui98/*************************************************************************************** 2e3da8badSTang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 409c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 509c6f1ddSLingrui98* 609c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 709c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 809c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 909c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 1009c6f1ddSLingrui98* 1109c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1209c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1309c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1409c6f1ddSLingrui98* 1509c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1609c6f1ddSLingrui98***************************************************************************************/ 1709c6f1ddSLingrui98package xiangshan.frontend 1809c6f1ddSLingrui98 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.util._ 2209c6f1ddSLingrui98import xiangshan._ 2350780602SJeniusimport xiangshan.frontend.icache._ 2409c6f1ddSLingrui98import utils._ 253c02ee8fSwakafaimport utility._ 26*88895b11Sxu_zhimport xiangshan.cache.mmu.TlbResp 27*88895b11Sxu_zhimport xiangshan.backend.fu.PMPRespBundle 28*88895b11Sxu_zh 29c2ad24ebSLingrui98import scala.math._ 30d2b20d1aSTang Haojinimport java.util.ResourceBundle.Control 31d2b20d1aSTang Haojin 32d2b20d1aSTang Haojinclass FrontendTopDownBundle(implicit p: Parameters) extends XSBundle { 33d2b20d1aSTang Haojin val reasons = Vec(TopDownCounters.NumStallReasons.id, Bool()) 34d2b20d1aSTang Haojin val stallWidth = UInt(log2Ceil(PredictWidth).W) 35d2b20d1aSTang Haojin} 3609c6f1ddSLingrui98 37b37e4b45SLingrui98class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters { 38c5c5edaeSJenius 39c5c5edaeSJenius //fast path: Timing critical 4009c6f1ddSLingrui98 val startAddr = UInt(VAddrBits.W) 4134a88126SJinYue val nextlineStart = UInt(VAddrBits.W) 42c5c5edaeSJenius val nextStartAddr = UInt(VAddrBits.W) 43c5c5edaeSJenius //slow path 4409c6f1ddSLingrui98 val ftqIdx = new FtqPtr 4509c6f1ddSLingrui98 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 4609c6f1ddSLingrui98 47d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 48d2b20d1aSTang Haojin 496ce52296SJinYue def crossCacheline = startAddr(blockOffBits - 1) === 1.U 506ce52296SJinYue 5109c6f1ddSLingrui98 def fromFtqPcBundle(b: Ftq_RF_Components) = { 5209c6f1ddSLingrui98 this.startAddr := b.startAddr 53b37e4b45SLingrui98 this.nextlineStart := b.nextLineAddr 54b37e4b45SLingrui98 when (b.fallThruError) { 55fd3aa057SYuandongliang val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.nextLineAddr, b.startAddr) 56b37e4b45SLingrui98 val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 57b37e4b45SLingrui98 this.nextStartAddr := 58b37e4b45SLingrui98 Cat(nextBlockHigher, 59b37e4b45SLingrui98 startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W), 60b37e4b45SLingrui98 startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits), 61b37e4b45SLingrui98 0.U(instOffsetBits.W) 62b37e4b45SLingrui98 ) 6309c6f1ddSLingrui98 } 6409c6f1ddSLingrui98 this 6509c6f1ddSLingrui98 } 6609c6f1ddSLingrui98 override def toPrintable: Printable = { 67b37e4b45SLingrui98 p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" + 68b37e4b45SLingrui98 p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 6909c6f1ddSLingrui98 p" offset: ${ftqOffset.bits}\n" 7009c6f1ddSLingrui98 } 7109c6f1ddSLingrui98} 7209c6f1ddSLingrui98 73f22cf846SJeniusclass FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 74c5c5edaeSJenius val startAddr = UInt(VAddrBits.W) 75c5c5edaeSJenius val nextlineStart = UInt(VAddrBits.W) 76b92f8445Sssszwic val ftqIdx = new FtqPtr 77c5c5edaeSJenius def crossCacheline = startAddr(blockOffBits - 1) === 1.U 78b004fa13SJenius def fromFtqPcBundle(b: Ftq_RF_Components) = { 79b004fa13SJenius this.startAddr := b.startAddr 80b004fa13SJenius this.nextlineStart := b.nextLineAddr 81b004fa13SJenius this 82b004fa13SJenius } 83f22cf846SJenius} 84f22cf846SJenius 8550780602SJeniusclass IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 8650780602SJenius val icacheReady = Output(Bool()) 8750780602SJenius val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 88d2b20d1aSTang Haojin val topdownIcacheMiss = Output(Bool()) 89d2b20d1aSTang Haojin val topdownItlbMiss = Output(Bool()) 9050780602SJenius} 9150780602SJenius 92f22cf846SJeniusclass FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 93f56177cbSJenius val pcMemRead = Vec(5, new FtqICacheInfo) 94dc270d3bSJenius val readValid = Vec(5, Bool()) 95c5c5edaeSJenius} 96c5c5edaeSJenius 97c5c5edaeSJenius 9809c6f1ddSLingrui98class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 9909c6f1ddSLingrui98 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 10009c6f1ddSLingrui98 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 10109c6f1ddSLingrui98 val ftqIdx = new FtqPtr 10209c6f1ddSLingrui98 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 10309c6f1ddSLingrui98 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 10409c6f1ddSLingrui98 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 10509c6f1ddSLingrui98 val target = UInt(VAddrBits.W) 10609c6f1ddSLingrui98 val jalTarget = UInt(VAddrBits.W) 10709c6f1ddSLingrui98 val instrRange = Vec(PredictWidth, Bool()) 10809c6f1ddSLingrui98} 10909c6f1ddSLingrui98 1101d1e6d4dSJeniusclass mmioCommitRead(implicit p: Parameters) extends XSBundle { 1111d1e6d4dSJenius val mmioFtqPtr = Output(new FtqPtr) 1121d1e6d4dSJenius val mmioLastCommit = Input(Bool()) 1131d1e6d4dSJenius} 1141d1e6d4dSJenius 1156b46af8dSMuziobject ExceptionType { 116*88895b11Sxu_zh def none : UInt = "b00".U 117*88895b11Sxu_zh def pf : UInt = "b01".U // instruction page fault 118*88895b11Sxu_zh def gpf : UInt = "b10".U // instruction guest page fault 119*88895b11Sxu_zh def af : UInt = "b11".U // instruction access fault 120*88895b11Sxu_zh def width : Int = 2 121*88895b11Sxu_zh 122*88895b11Sxu_zh // raise pf/gpf/af according to itlb response 123*88895b11Sxu_zh def fromTlbResp(resp: TlbResp, useDup: Int = 0): UInt = { 124*88895b11Sxu_zh require(useDup >= 0 && useDup < resp.excp.length) 125*88895b11Sxu_zh assert( 126*88895b11Sxu_zh PopCount(VecInit(resp.excp(useDup).af.instr, resp.excp(useDup).pf.instr, resp.excp(useDup).gpf.instr)) <= 1.U, 127*88895b11Sxu_zh "tlb resp has more than 1 exception, af=%d, pf=%d, gpf=%d", 128*88895b11Sxu_zh resp.excp(useDup).af.instr, resp.excp(useDup).pf.instr, resp.excp(useDup).gpf.instr 129*88895b11Sxu_zh ) 130*88895b11Sxu_zh // itlb is guaranteed to respond at most one exception, so we don't worry about priority here. 131*88895b11Sxu_zh MuxCase(none, Seq( 132*88895b11Sxu_zh resp.excp(useDup).pf.instr -> pf, 133*88895b11Sxu_zh resp.excp(useDup).gpf.instr -> gpf, 134*88895b11Sxu_zh resp.excp(useDup).af.instr -> af 135*88895b11Sxu_zh )) 136*88895b11Sxu_zh } 137*88895b11Sxu_zh 138*88895b11Sxu_zh // raise af if pmp check failed 139*88895b11Sxu_zh def fromPMPResp(resp: PMPRespBundle): UInt = { 140*88895b11Sxu_zh Mux(resp.instr, af, none) 141*88895b11Sxu_zh } 142*88895b11Sxu_zh 143*88895b11Sxu_zh // raise af if meta/data array ecc check failed or l2 cache respond with tilelink corrupt 144*88895b11Sxu_zh def fromECC(corrupt: Bool): UInt = { 145*88895b11Sxu_zh Mux(corrupt, af, none) 146*88895b11Sxu_zh } 147*88895b11Sxu_zh 148*88895b11Sxu_zh /**Generates exception mux tree 149*88895b11Sxu_zh * 150*88895b11Sxu_zh * Exceptions that are further to the left in the parameter list have higher priority 151*88895b11Sxu_zh * @example 152*88895b11Sxu_zh * {{{ 153*88895b11Sxu_zh * val itlb_exception = ExceptionType.fromTlbResp(io.itlb.resp.bits) 154*88895b11Sxu_zh * // so as pmp_exception, meta_corrupt 155*88895b11Sxu_zh * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 156*88895b11Sxu_zh * Mux( 157*88895b11Sxu_zh * itlb_exception =/= none, 158*88895b11Sxu_zh * itlb_exception, 159*88895b11Sxu_zh * Mux(pmp_exception =/= none, pmp_exception, meta_corrupt) 160*88895b11Sxu_zh * ) 161*88895b11Sxu_zh * }}} 162*88895b11Sxu_zh */ 163*88895b11Sxu_zh def merge(exceptions: UInt*): UInt = { 164*88895b11Sxu_zh// // recursively generate mux tree 165*88895b11Sxu_zh// if (exceptions.length == 1) { 166*88895b11Sxu_zh// require(exceptions.head.getWidth == width) 167*88895b11Sxu_zh// exceptions.head 168*88895b11Sxu_zh// } else { 169*88895b11Sxu_zh// Mux(exceptions.head =/= none, exceptions.head, merge(exceptions.tail: _*)) 170*88895b11Sxu_zh// } 171*88895b11Sxu_zh // use MuxCase with default 172*88895b11Sxu_zh exceptions.foreach(e => require(e.getWidth == width)) 173*88895b11Sxu_zh val mapping = exceptions.init.map(e => (e =/= none) -> e) 174*88895b11Sxu_zh val default = exceptions.last 175*88895b11Sxu_zh MuxCase(default, mapping) 176*88895b11Sxu_zh } 177*88895b11Sxu_zh 178*88895b11Sxu_zh /**Generates exception mux tree for multi-port exception vectors 179*88895b11Sxu_zh * 180*88895b11Sxu_zh * Exceptions that are further to the left in the parameter list have higher priority 181*88895b11Sxu_zh * @example 182*88895b11Sxu_zh * {{{ 183*88895b11Sxu_zh * val itlb_exception = VecInit((0 until PortNumber).map(i => ExceptionType.fromTlbResp(io.itlb(i).resp.bits))) 184*88895b11Sxu_zh * // so as pmp_exception, meta_corrupt 185*88895b11Sxu_zh * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 186*88895b11Sxu_zh * VecInit((0 until PortNumber).map(i => Mux( 187*88895b11Sxu_zh * itlb_exception(i) =/= none, 188*88895b11Sxu_zh * itlb_exception(i), 189*88895b11Sxu_zh * Mux(pmp_exception(i) =/= none, pmp_exception(i), meta_corrupt(i)) 190*88895b11Sxu_zh * )) 191*88895b11Sxu_zh * }}} 192*88895b11Sxu_zh */ 193*88895b11Sxu_zh def merge(exceptionVecs: Vec[UInt]*): Vec[UInt] = { 194*88895b11Sxu_zh// // recursively generate mux tree 195*88895b11Sxu_zh// if (exceptionVecs.length == 1) { 196*88895b11Sxu_zh// exceptionVecs.head.foreach(e => require(e.getWidth == width)) 197*88895b11Sxu_zh// exceptionVecs.head 198*88895b11Sxu_zh// } else { 199*88895b11Sxu_zh// require(exceptionVecs.head.length == exceptionVecs.last.length) 200*88895b11Sxu_zh// VecInit((exceptionVecs.head zip merge(exceptionVecs.tail: _*)).map{ case (high, low) => 201*88895b11Sxu_zh// Mux(high =/= none, high, low) 202*88895b11Sxu_zh// }) 203*88895b11Sxu_zh// } 204*88895b11Sxu_zh // merge port-by-port 205*88895b11Sxu_zh val length = exceptionVecs.head.length 206*88895b11Sxu_zh exceptionVecs.tail.foreach(vec => require(vec.length == length)) 207*88895b11Sxu_zh VecInit((0 until length).map{ i => 208*88895b11Sxu_zh merge(exceptionVecs.map(_(i)): _*) 209*88895b11Sxu_zh }) 210*88895b11Sxu_zh } 2116b46af8dSMuzi} 2126b46af8dSMuzi 21309c6f1ddSLingrui98class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 21409c6f1ddSLingrui98 val instrs = Vec(PredictWidth, UInt(32.W)) 21509c6f1ddSLingrui98 val valid = UInt(PredictWidth.W) 2162a3050c2SJay val enqEnable = UInt(PredictWidth.W) 21709c6f1ddSLingrui98 val pd = Vec(PredictWidth, new PreDecodeInfo) 21809c6f1ddSLingrui98 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 21909c6f1ddSLingrui98 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 22009c6f1ddSLingrui98 val ftqPtr = new FtqPtr 22109c6f1ddSLingrui98 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 2226b46af8dSMuzi val exceptionType = Vec(PredictWidth, UInt(ExceptionType.width.W)) 22309c6f1ddSLingrui98 val crossPageIPFFix = Vec(PredictWidth, Bool()) 22472951335SLi Qianruo val triggered = Vec(PredictWidth, new TriggerCf) 225d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 22609c6f1ddSLingrui98} 22709c6f1ddSLingrui98 228c2ad24ebSLingrui98// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 229c2ad24ebSLingrui98// val io = IO(new Bundle { 230c2ad24ebSLingrui98// val set 231c2ad24ebSLingrui98// }) 232c2ad24ebSLingrui98// } 23309c6f1ddSLingrui98// Move from BPU 234c2ad24ebSLingrui98abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 235c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 236c2ad24ebSLingrui98} 237c2ad24ebSLingrui98 238c2ad24ebSLingrui98class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 23909c6f1ddSLingrui98 val predHist = UInt(HistoryLength.W) 24009c6f1ddSLingrui98 241c2ad24ebSLingrui98 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 242c2ad24ebSLingrui98 val g = Wire(new ShiftingGlobalHistory) 24309c6f1ddSLingrui98 g.predHist := (hist << shift) | taken 24409c6f1ddSLingrui98 g 24509c6f1ddSLingrui98 } 24609c6f1ddSLingrui98 247c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 248eeb5ff92SLingrui98 require(br_valids.length == numBr) 249eeb5ff92SLingrui98 require(real_taken_mask.length == numBr) 250eeb5ff92SLingrui98 val last_valid_idx = PriorityMux( 251eeb5ff92SLingrui98 br_valids.reverse :+ true.B, 252eeb5ff92SLingrui98 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 253eeb5ff92SLingrui98 ) 254eeb5ff92SLingrui98 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 255eeb5ff92SLingrui98 val smaller = Mux(last_valid_idx < first_taken_idx, 256eeb5ff92SLingrui98 last_valid_idx, 257eeb5ff92SLingrui98 first_taken_idx 258eeb5ff92SLingrui98 ) 259eeb5ff92SLingrui98 val shift = smaller 260eeb5ff92SLingrui98 val taken = real_taken_mask.reduce(_||_) 261eeb5ff92SLingrui98 update(shift, taken, this.predHist) 262eeb5ff92SLingrui98 } 263eeb5ff92SLingrui98 264c2ad24ebSLingrui98 // static read 265935edac4STang Haojin def read(n: Int): Bool = predHist.asBools(n) 266c2ad24ebSLingrui98 267c2ad24ebSLingrui98 final def === (that: ShiftingGlobalHistory): Bool = { 26809c6f1ddSLingrui98 predHist === that.predHist 26909c6f1ddSLingrui98 } 27009c6f1ddSLingrui98 271c2ad24ebSLingrui98 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 272c2ad24ebSLingrui98} 27309c6f1ddSLingrui98 274c2ad24ebSLingrui98// circular global history pointer 275c2ad24ebSLingrui98class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 276c2ad24ebSLingrui98 p => p(XSCoreParamsKey).HistoryLength 277c2ad24ebSLingrui98){ 278c2ad24ebSLingrui98} 279c7fabd05SSteve Gou 280c7fabd05SSteve Gouobject CGHPtr { 281c7fabd05SSteve Gou def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 282c7fabd05SSteve Gou val ptr = Wire(new CGHPtr) 283c7fabd05SSteve Gou ptr.flag := f 284c7fabd05SSteve Gou ptr.value := v 285c7fabd05SSteve Gou ptr 286c7fabd05SSteve Gou } 287c7fabd05SSteve Gou def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 288c7fabd05SSteve Gou apply(!ptr.flag, ptr.value) 289c7fabd05SSteve Gou } 290c7fabd05SSteve Gou} 291c7fabd05SSteve Gou 292c2ad24ebSLingrui98class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 293c2ad24ebSLingrui98 val buffer = Vec(HistoryLength, Bool()) 294c2ad24ebSLingrui98 type HistPtr = UInt 295c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 296c2ad24ebSLingrui98 this 297c2ad24ebSLingrui98 } 298c2ad24ebSLingrui98} 299c2ad24ebSLingrui98 300dd6c0695SLingrui98class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 301c2ad24ebSLingrui98 extends XSBundle with HasBPUConst { 302dd6c0695SLingrui98 require(compLen >= 1) 303c2ad24ebSLingrui98 require(len > 0) 304c2ad24ebSLingrui98 // require(folded_len <= len) 305dd6c0695SLingrui98 require(compLen >= max_update_num) 306dd6c0695SLingrui98 val folded_hist = UInt(compLen.W) 307dd6c0695SLingrui98 30867402d75SLingrui98 def need_oldest_bits = len > compLen 309dd6c0695SLingrui98 def info = (len, compLen) 310c2ad24ebSLingrui98 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 311c2ad24ebSLingrui98 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 312c2ad24ebSLingrui98 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 313c2ad24ebSLingrui98 def oldest_bit_start = oldest_bit_pos_in_folded.head 314c2ad24ebSLingrui98 315dd6c0695SLingrui98 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 316c2ad24ebSLingrui98 // TODO: wrap inc for histPtr value 317dd6c0695SLingrui98 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 318c2ad24ebSLingrui98 } 319c2ad24ebSLingrui98 320ab890bfeSLingrui98 def circular_shift_left(src: UInt, shamt: Int) = { 321c2ad24ebSLingrui98 val srcLen = src.getWidth 322c2ad24ebSLingrui98 val src_doubled = Cat(src, src) 323ab890bfeSLingrui98 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 324ab890bfeSLingrui98 shifted 325c2ad24ebSLingrui98 } 326c2ad24ebSLingrui98 32767402d75SLingrui98 // slow path, read bits from ghr 328ab890bfeSLingrui98 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 32967402d75SLingrui98 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 33067402d75SLingrui98 update(oldest_bits, num, taken) 33167402d75SLingrui98 } 33267402d75SLingrui98 33367402d75SLingrui98 33467402d75SLingrui98 // fast path, use pre-read oldest bits 33567402d75SLingrui98 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 336c2ad24ebSLingrui98 // do xors for several bitsets at specified bits 337c2ad24ebSLingrui98 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 338c2ad24ebSLingrui98 val res = Wire(Vec(len, Bool())) 339c2ad24ebSLingrui98 // println(f"num bitsets: ${bitsets.length}") 340c2ad24ebSLingrui98 // println(f"bitsets $bitsets") 341c2ad24ebSLingrui98 val resArr = Array.fill(len)(List[Bool]()) 342c2ad24ebSLingrui98 for (bs <- bitsets) { 343c2ad24ebSLingrui98 for ((n, b) <- bs) { 344c2ad24ebSLingrui98 resArr(n) = b :: resArr(n) 345c2ad24ebSLingrui98 } 346c2ad24ebSLingrui98 } 347c2ad24ebSLingrui98 // println(f"${resArr.mkString}") 348c2ad24ebSLingrui98 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 349c2ad24ebSLingrui98 for (i <- 0 until len) { 350c2ad24ebSLingrui98 // println(f"bit[$i], ${resArr(i).mkString}") 351c2ad24ebSLingrui98 if (resArr(i).length == 0) { 352dd6c0695SLingrui98 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 353c2ad24ebSLingrui98 } 354c2ad24ebSLingrui98 res(i) := resArr(i).foldLeft(false.B)(_^_) 355c2ad24ebSLingrui98 } 356c2ad24ebSLingrui98 res.asUInt 357c2ad24ebSLingrui98 } 358c2ad24ebSLingrui98 35967402d75SLingrui98 val new_folded_hist = if (need_oldest_bits) { 36067402d75SLingrui98 val oldest_bits = ob 36167402d75SLingrui98 require(oldest_bits.length == max_update_num) 362c2ad24ebSLingrui98 // mask off bits that do not update 363c2ad24ebSLingrui98 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 364ab890bfeSLingrui98 case (ob, i) => ob && (i < num).B 365c2ad24ebSLingrui98 } 366c2ad24ebSLingrui98 // if a bit does not wrap around, it should not be xored when it exits 367c2ad24ebSLingrui98 val oldest_bits_set = (0 until max_update_num).filter(oldest_bit_wrap_around).map(i => (oldest_bit_pos_in_folded(i), oldest_bits_masked(i))) 368c2ad24ebSLingrui98 369c2ad24ebSLingrui98 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 370c2ad24ebSLingrui98 371c2ad24ebSLingrui98 // only the last bit could be 1, as we have at most one taken branch at a time 372ab890bfeSLingrui98 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 373c2ad24ebSLingrui98 // if a bit does not wrap around, newest bits should not be xored onto it either 374e992912cSLingrui98 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 375c2ad24ebSLingrui98 376c2ad24ebSLingrui98 // println(f"new bits set ${newest_bits_set.map(_._1)}") 377c2ad24ebSLingrui98 // 378c2ad24ebSLingrui98 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 379ab890bfeSLingrui98 case (fb, i) => fb && !(num >= (len-i)).B 380c2ad24ebSLingrui98 }) 381c2ad24ebSLingrui98 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 382c2ad24ebSLingrui98 383c2ad24ebSLingrui98 // do xor then shift 384c2ad24ebSLingrui98 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 385ab890bfeSLingrui98 circular_shift_left(xored, num) 38667402d75SLingrui98 } else { 38767402d75SLingrui98 // histLen too short to wrap around 38867402d75SLingrui98 ((folded_hist << num) | taken)(compLen-1,0) 389c2ad24ebSLingrui98 } 39067402d75SLingrui98 391c2ad24ebSLingrui98 val fh = WireInit(this) 392c2ad24ebSLingrui98 fh.folded_hist := new_folded_hist 393c2ad24ebSLingrui98 fh 394c2ad24ebSLingrui98 } 39509c6f1ddSLingrui98} 39609c6f1ddSLingrui98 39767402d75SLingrui98class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 39867402d75SLingrui98 val bits = Vec(max_update_num*2, Bool()) 39967402d75SLingrui98 // def info = (len, compLen) 40067402d75SLingrui98 def getRealOb(brNumOH: UInt): Vec[Bool] = { 40167402d75SLingrui98 val ob = Wire(Vec(max_update_num, Bool())) 40267402d75SLingrui98 for (i <- 0 until max_update_num) { 40367402d75SLingrui98 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 40467402d75SLingrui98 } 40567402d75SLingrui98 ob 40667402d75SLingrui98 } 40767402d75SLingrui98} 40867402d75SLingrui98 40967402d75SLingrui98class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 41067402d75SLingrui98 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 41167402d75SLingrui98 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 41267402d75SLingrui98 require(gen.toSet.toList.equals(gen)) 41367402d75SLingrui98 def getObWithInfo(info: Tuple2[Int, Int]) = { 41467402d75SLingrui98 val selected = afhob.filter(_.len == info._1) 41567402d75SLingrui98 require(selected.length == 1) 41667402d75SLingrui98 selected(0) 41767402d75SLingrui98 } 41867402d75SLingrui98 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 41967402d75SLingrui98 val hisLens = afhob.map(_.len) 42067402d75SLingrui98 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 42167402d75SLingrui98 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 42267402d75SLingrui98 for (ob <- afhob) { 42367402d75SLingrui98 for (i <- 0 until numBr*2) { 42467402d75SLingrui98 val pos = ob.len - i - 1 42567402d75SLingrui98 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 42667402d75SLingrui98 require(bit_found.length == 1) 42767402d75SLingrui98 ob.bits(i) := bit_found(0)._2 42867402d75SLingrui98 } 42967402d75SLingrui98 } 43067402d75SLingrui98 } 43167402d75SLingrui98} 43267402d75SLingrui98 43367402d75SLingrui98class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 43467402d75SLingrui98 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 43567402d75SLingrui98 // println(gen.mkString) 43667402d75SLingrui98 require(gen.toSet.toList.equals(gen)) 43767402d75SLingrui98 def getHistWithInfo(info: Tuple2[Int, Int]) = { 43867402d75SLingrui98 val selected = hist.filter(_.info.equals(info)) 43967402d75SLingrui98 require(selected.length == 1) 44067402d75SLingrui98 selected(0) 44167402d75SLingrui98 } 44267402d75SLingrui98 def autoConnectFrom(that: AllFoldedHistories) = { 44367402d75SLingrui98 require(this.hist.length <= that.hist.length) 44467402d75SLingrui98 for (h <- this.hist) { 44567402d75SLingrui98 h := that.getHistWithInfo(h.info) 44667402d75SLingrui98 } 44767402d75SLingrui98 } 44867402d75SLingrui98 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 44967402d75SLingrui98 val res = WireInit(this) 45067402d75SLingrui98 for (i <- 0 until this.hist.length) { 45167402d75SLingrui98 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 45267402d75SLingrui98 } 45367402d75SLingrui98 res 45467402d75SLingrui98 } 45567402d75SLingrui98 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 45667402d75SLingrui98 val res = WireInit(this) 45767402d75SLingrui98 for (i <- 0 until this.hist.length) { 45867402d75SLingrui98 val fh = this.hist(i) 45967402d75SLingrui98 if (fh.need_oldest_bits) { 46067402d75SLingrui98 val info = fh.info 46167402d75SLingrui98 val selectedAfhob = afhob.getObWithInfo(info) 46267402d75SLingrui98 val ob = selectedAfhob.getRealOb(lastBrNumOH) 46367402d75SLingrui98 res.hist(i) := this.hist(i).update(ob, shift, taken) 46467402d75SLingrui98 } else { 46567402d75SLingrui98 val dumb = Wire(Vec(numBr, Bool())) // not needed 46667402d75SLingrui98 dumb := DontCare 46767402d75SLingrui98 res.hist(i) := this.hist(i).update(dumb, shift, taken) 46867402d75SLingrui98 } 46967402d75SLingrui98 } 47067402d75SLingrui98 res 47167402d75SLingrui98 } 47267402d75SLingrui98 47367402d75SLingrui98 def display(cond: Bool) = { 47467402d75SLingrui98 for (h <- hist) { 47567402d75SLingrui98 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 47667402d75SLingrui98 } 47767402d75SLingrui98 } 47867402d75SLingrui98} 47967402d75SLingrui98 48009c6f1ddSLingrui98class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 48109c6f1ddSLingrui98 def tagBits = VAddrBits - idxBits - instOffsetBits 48209c6f1ddSLingrui98 48309c6f1ddSLingrui98 val tag = UInt(tagBits.W) 48409c6f1ddSLingrui98 val idx = UInt(idxBits.W) 48509c6f1ddSLingrui98 val offset = UInt(instOffsetBits.W) 48609c6f1ddSLingrui98 48709c6f1ddSLingrui98 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 48809c6f1ddSLingrui98 def getTag(x: UInt) = fromUInt(x).tag 48909c6f1ddSLingrui98 def getIdx(x: UInt) = fromUInt(x).idx 49009c6f1ddSLingrui98 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 49109c6f1ddSLingrui98 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 49209c6f1ddSLingrui98} 493eeb5ff92SLingrui98 494b37e4b45SLingrui98trait BasicPrediction extends HasXSParameter { 495b37e4b45SLingrui98 def cfiIndex: ValidUndirectioned[UInt] 496b37e4b45SLingrui98 def target(pc: UInt): UInt 497b37e4b45SLingrui98 def lastBrPosOH: Vec[Bool] 498b37e4b45SLingrui98 def brTaken: Bool 499b37e4b45SLingrui98 def shouldShiftVec: Vec[Bool] 500b37e4b45SLingrui98 def fallThruError: Bool 501b37e4b45SLingrui98} 502935edac4STang Haojin 503b166c0eaSEaston Man// selectByTaken selects some data according to takenMask 5042bf6e0ecSEaston Man// allTargets should be in a Vec, like [taken0, taken1, ..., not taken, not hit] 505b166c0eaSEaston Manobject selectByTaken { 506b166c0eaSEaston Man def apply[T <: Data](takenMask: Vec[Bool], hit: Bool, allTargets: Vec[T]): T = { 507b166c0eaSEaston Man val selVecOH = 508b166c0eaSEaston Man takenMask.zipWithIndex.map { case (t, i) => !takenMask.take(i).fold(false.B)(_ || _) && t && hit } :+ 509b166c0eaSEaston Man (!takenMask.asUInt.orR && hit) :+ !hit 510b166c0eaSEaston Man Mux1H(selVecOH, allTargets) 511b166c0eaSEaston Man } 512b166c0eaSEaston Man} 513b166c0eaSEaston Man 514b37e4b45SLingrui98class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 515eeb5ff92SLingrui98 val br_taken_mask = Vec(numBr, Bool()) 51609c6f1ddSLingrui98 517eeb5ff92SLingrui98 val slot_valids = Vec(totalSlot, Bool()) 51809c6f1ddSLingrui98 519eeb5ff92SLingrui98 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 520b30c10d6SLingrui98 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 521a229ab6cSLingrui98 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 522a229ab6cSLingrui98 val fallThroughAddr = UInt(VAddrBits.W) 523b37e4b45SLingrui98 val fallThroughErr = Bool() 524fd3aa057SYuandongliang val multiHit = Bool() 52509c6f1ddSLingrui98 52609c6f1ddSLingrui98 val is_jal = Bool() 52709c6f1ddSLingrui98 val is_jalr = Bool() 52809c6f1ddSLingrui98 val is_call = Bool() 52909c6f1ddSLingrui98 val is_ret = Bool() 530f4ebc4b2SLingrui98 val last_may_be_rvi_call = Bool() 531eeb5ff92SLingrui98 val is_br_sharing = Bool() 53209c6f1ddSLingrui98 53309c6f1ddSLingrui98 // val call_is_rvc = Bool() 53409c6f1ddSLingrui98 val hit = Bool() 53509c6f1ddSLingrui98 536209a4cafSSteve Gou val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 537209a4cafSSteve Gou 538eeb5ff92SLingrui98 def br_slot_valids = slot_valids.init 539eeb5ff92SLingrui98 def tail_slot_valid = slot_valids.last 540eeb5ff92SLingrui98 541eeb5ff92SLingrui98 def br_valids = { 542b37e4b45SLingrui98 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 543eeb5ff92SLingrui98 } 544eeb5ff92SLingrui98 545eeb5ff92SLingrui98 def taken_mask_on_slot = { 546eeb5ff92SLingrui98 VecInit( 547eeb5ff92SLingrui98 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 548b30c10d6SLingrui98 tail_slot_valid && ( 549b30c10d6SLingrui98 is_br_sharing && br_taken_mask.last || !is_br_sharing 550b30c10d6SLingrui98 ) 551eeb5ff92SLingrui98 ) 552eeb5ff92SLingrui98 ) 553eeb5ff92SLingrui98 } 554eeb5ff92SLingrui98 555b37e4b45SLingrui98 def real_slot_taken_mask(): Vec[Bool] = { 556b37e4b45SLingrui98 VecInit(taken_mask_on_slot.map(_ && hit)) 557b37e4b45SLingrui98 } 558b37e4b45SLingrui98 559b37e4b45SLingrui98 // len numBr 560b37e4b45SLingrui98 def real_br_taken_mask(): Vec[Bool] = { 561b37e4b45SLingrui98 VecInit( 562b37e4b45SLingrui98 taken_mask_on_slot.map(_ && hit).init :+ 563b37e4b45SLingrui98 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 564b37e4b45SLingrui98 ) 565b37e4b45SLingrui98 } 566b37e4b45SLingrui98 567b37e4b45SLingrui98 // the vec indicating if ghr should shift on each branch 568b37e4b45SLingrui98 def shouldShiftVec = 569b37e4b45SLingrui98 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 570e3da8badSTang Haojin v && !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B)}) 571b37e4b45SLingrui98 572b37e4b45SLingrui98 def lastBrPosOH = 573b37e4b45SLingrui98 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 574b37e4b45SLingrui98 (0 until numBr).map(i => 575b37e4b45SLingrui98 br_valids(i) && 576e3da8badSTang Haojin !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 577b37e4b45SLingrui98 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 578b37e4b45SLingrui98 hit 579b37e4b45SLingrui98 ) 580b37e4b45SLingrui98 ) 581b37e4b45SLingrui98 58286d9c530SLingrui98 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 583b37e4b45SLingrui98 584b37e4b45SLingrui98 def target(pc: UInt): UInt = { 585b166c0eaSEaston Man selectByTaken(taken_mask_on_slot, hit, allTarget(pc)) 586b166c0eaSEaston Man } 587b166c0eaSEaston Man 5882bf6e0ecSEaston Man // allTarget return a Vec of all possible target of a BP stage 5892bf6e0ecSEaston Man // in the following order: [taken_target0, taken_target1, ..., fallThroughAddr, not hit (plus fetch width)] 590b166c0eaSEaston Man // 591b166c0eaSEaston Man // This exposes internal targets for timing optimization, 592b166c0eaSEaston Man // since usually targets are generated quicker than taken 593b166c0eaSEaston Man def allTarget(pc: UInt): Vec[UInt] = { 594b166c0eaSEaston Man VecInit(targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)) 595b37e4b45SLingrui98 } 596b37e4b45SLingrui98 597b37e4b45SLingrui98 def fallThruError: Bool = hit && fallThroughErr 598fd3aa057SYuandongliang def ftbMultiHit: Bool = hit && multiHit 599b37e4b45SLingrui98 600b37e4b45SLingrui98 def hit_taken_on_jmp = 601b37e4b45SLingrui98 !real_slot_taken_mask().init.reduce(_||_) && 602b37e4b45SLingrui98 real_slot_taken_mask().last && !is_br_sharing 603b37e4b45SLingrui98 def hit_taken_on_call = hit_taken_on_jmp && is_call 604b37e4b45SLingrui98 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 605b37e4b45SLingrui98 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 606b37e4b45SLingrui98 607b37e4b45SLingrui98 def cfiIndex = { 608b37e4b45SLingrui98 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 609b37e4b45SLingrui98 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 610b37e4b45SLingrui98 // when no takens, set cfiIndex to PredictWidth-1 611b37e4b45SLingrui98 cfiIndex.bits := 612b37e4b45SLingrui98 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 613b37e4b45SLingrui98 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 614b37e4b45SLingrui98 cfiIndex 615b37e4b45SLingrui98 } 616b37e4b45SLingrui98 617eeb5ff92SLingrui98 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 61809c6f1ddSLingrui98 61947c003a9SEaston Man def fromFtbEntry( 62047c003a9SEaston Man entry: FTBEntry, 62147c003a9SEaston Man pc: UInt, 62247c003a9SEaston Man last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 62347c003a9SEaston Man last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 62447c003a9SEaston Man ) = { 625eeb5ff92SLingrui98 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 62647c003a9SEaston Man targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 627b30c10d6SLingrui98 jalr_target := targets.last 628a229ab6cSLingrui98 offsets := entry.getOffsetVec 629eeb5ff92SLingrui98 is_jal := entry.tailSlot.valid && entry.isJal 630eeb5ff92SLingrui98 is_jalr := entry.tailSlot.valid && entry.isJalr 631eeb5ff92SLingrui98 is_call := entry.tailSlot.valid && entry.isCall 632eeb5ff92SLingrui98 is_ret := entry.tailSlot.valid && entry.isRet 633f4ebc4b2SLingrui98 last_may_be_rvi_call := entry.last_may_be_rvi_call 634eeb5ff92SLingrui98 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 635209a4cafSSteve Gou predCycle.map(_ := GTimer()) 636a229ab6cSLingrui98 637a60a2901SLingrui98 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 638b37e4b45SLingrui98 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 639fd3aa057SYuandongliang fallThroughErr := startLower >= endLowerwithCarry || endLowerwithCarry > (startLower + (PredictWidth).U) 64047c003a9SEaston Man fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 641a229ab6cSLingrui98 } 64209c6f1ddSLingrui98 64309c6f1ddSLingrui98 def display(cond: Bool): Unit = { 644eeb5ff92SLingrui98 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 64509c6f1ddSLingrui98 } 64609c6f1ddSLingrui98} 64709c6f1ddSLingrui98 648803124a6SLingrui98class SpeculativeInfo(implicit p: Parameters) extends XSBundle 649803124a6SLingrui98 with HasBPUConst with BPUUtils { 650803124a6SLingrui98 val histPtr = new CGHPtr 651c89b4642SGuokai Chen val ssp = UInt(log2Up(RasSize).W) 652deb3a97eSGao-Zeyu val sctr = UInt(RasCtrSize.W) 653c89b4642SGuokai Chen val TOSW = new RASPtr 654c89b4642SGuokai Chen val TOSR = new RASPtr 655c89b4642SGuokai Chen val NOS = new RASPtr 656c89b4642SGuokai Chen val topAddr = UInt(VAddrBits.W) 657803124a6SLingrui98} 658803124a6SLingrui98 659b37e4b45SLingrui98class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 660b37e4b45SLingrui98 with HasBPUConst with BPUUtils { 661adc0b8dfSGuokai Chen val pc = Vec(numDup, UInt(VAddrBits.W)) 662adc0b8dfSGuokai Chen val valid = Vec(numDup, Bool()) 663adc0b8dfSGuokai Chen val hasRedirect = Vec(numDup, Bool()) 66409c6f1ddSLingrui98 val ftq_idx = new FtqPtr 665adc0b8dfSGuokai Chen val full_pred = Vec(numDup, new FullBranchPrediction) 666b37e4b45SLingrui98 66709c6f1ddSLingrui98 668adc0b8dfSGuokai Chen def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 669b166c0eaSEaston Man def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).target(pc)}) 670b166c0eaSEaston Man def allTargets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).allTarget(pc)}) 671adc0b8dfSGuokai Chen def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 672adc0b8dfSGuokai Chen def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 673adc0b8dfSGuokai Chen def brTaken = VecInit(full_pred.map(_.brTaken)) 674adc0b8dfSGuokai Chen def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 675adc0b8dfSGuokai Chen def fallThruError = VecInit(full_pred.map(_.fallThruError)) 676fd3aa057SYuandongliang def ftbMultiHit = VecInit(full_pred.map(_.ftbMultiHit)) 677eeb5ff92SLingrui98 678adc0b8dfSGuokai Chen def taken = VecInit(cfiIndex.map(_.valid)) 679adc0b8dfSGuokai Chen 680adc0b8dfSGuokai Chen def getTarget = targets(pc) 681b166c0eaSEaston Man def getAllTargets = allTargets(pc) 68209c6f1ddSLingrui98 68309c6f1ddSLingrui98 def display(cond: Bool): Unit = { 684adc0b8dfSGuokai Chen XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 685adc0b8dfSGuokai Chen full_pred(0).display(cond) 68609c6f1ddSLingrui98 } 68709c6f1ddSLingrui98} 68809c6f1ddSLingrui98 68909c6f1ddSLingrui98class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 690b37e4b45SLingrui98 val s1 = new BranchPredictionBundle 691b37e4b45SLingrui98 val s2 = new BranchPredictionBundle 692cb4f77ceSLingrui98 val s3 = new BranchPredictionBundle 69309c6f1ddSLingrui98 694c4a59f19SYuandongliang val s1_uftbHit = Bool() 695c4a59f19SYuandongliang val s1_uftbHasIndirect = Bool() 696c4a59f19SYuandongliang val s1_ftbCloseReq = Bool() 697c4a59f19SYuandongliang 698c2d1ec7dSLingrui98 val last_stage_meta = UInt(MaxMetaLength.W) 6993711cf36S小造xu_zh val last_stage_spec_info = new Ftq_Redirect_SRAMEntry 700c2d1ec7dSLingrui98 val last_stage_ftb_entry = new FTBEntry 701c2d1ec7dSLingrui98 702d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 703d2b20d1aSTang Haojin 704b37e4b45SLingrui98 def selectedResp ={ 705b37e4b45SLingrui98 val res = 70609c6f1ddSLingrui98 PriorityMux(Seq( 707adc0b8dfSGuokai Chen ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 708adc0b8dfSGuokai Chen ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 709adc0b8dfSGuokai Chen (s1.valid(3) -> s1) 71009c6f1ddSLingrui98 )) 711b37e4b45SLingrui98 res 712b37e4b45SLingrui98 } 713adc0b8dfSGuokai Chen def selectedRespIdxForFtq = 71409c6f1ddSLingrui98 PriorityMux(Seq( 715adc0b8dfSGuokai Chen ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 716adc0b8dfSGuokai Chen ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 717adc0b8dfSGuokai Chen (s1.valid(3) -> BP_S1) 71809c6f1ddSLingrui98 )) 719cb4f77ceSLingrui98 def lastStage = s3 72009c6f1ddSLingrui98} 72109c6f1ddSLingrui98 722c2d1ec7dSLingrui98class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 72309c6f1ddSLingrui98 724803124a6SLingrui98class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 725803124a6SLingrui98 val pc = UInt(VAddrBits.W) 726803124a6SLingrui98 val spec_info = new SpeculativeInfo 727803124a6SLingrui98 val ftb_entry = new FTBEntry() 728803124a6SLingrui98 729803124a6SLingrui98 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 730803124a6SLingrui98 val br_taken_mask = Vec(numBr, Bool()) 731cc2d1573SEaston Man val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 732803124a6SLingrui98 val jmp_taken = Bool() 73309c6f1ddSLingrui98 val mispred_mask = Vec(numBr+1, Bool()) 734edc18578SLingrui98 val pred_hit = Bool() 73509c6f1ddSLingrui98 val false_hit = Bool() 73609c6f1ddSLingrui98 val new_br_insert_pos = Vec(numBr, Bool()) 73709c6f1ddSLingrui98 val old_entry = Bool() 73809c6f1ddSLingrui98 val meta = UInt(MaxMetaLength.W) 739abdbe4b7SLingrui98 val full_target = UInt(VAddrBits.W) 740edc18578SLingrui98 val from_stage = UInt(2.W) 74186d9c530SLingrui98 val ghist = UInt(HistoryLength.W) 74209c6f1ddSLingrui98 743803124a6SLingrui98 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 744803124a6SLingrui98 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 745803124a6SLingrui98 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 746803124a6SLingrui98 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 747803124a6SLingrui98 748c89b4642SGuokai Chen def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 749c89b4642SGuokai Chen def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 750c89b4642SGuokai Chen 751803124a6SLingrui98 def display(cond: Bool) = { 75209c6f1ddSLingrui98 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 75309c6f1ddSLingrui98 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 75409c6f1ddSLingrui98 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 75509c6f1ddSLingrui98 XSDebug(cond, p"--------------------------------------------\n") 75609c6f1ddSLingrui98 } 75709c6f1ddSLingrui98} 75809c6f1ddSLingrui98 75909c6f1ddSLingrui98class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 76009c6f1ddSLingrui98 // override def toPrintable: Printable = { 76109c6f1ddSLingrui98 // p"-----------BranchPredictionRedirect----------- " + 76209c6f1ddSLingrui98 // p"-----------cfiUpdate----------- " + 76309c6f1ddSLingrui98 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 76409c6f1ddSLingrui98 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 76509c6f1ddSLingrui98 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 76609c6f1ddSLingrui98 // p"------------------------------- " + 7679aca92b9SYinan Xu // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 76809c6f1ddSLingrui98 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 76909c6f1ddSLingrui98 // p"[ftqOffset] ${ftqOffset} " + 77009c6f1ddSLingrui98 // p"[level] ${level}, [interrupt] ${interrupt} " + 77109c6f1ddSLingrui98 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 77209c6f1ddSLingrui98 // p"[stFtqOffset] ${stFtqOffset} " + 77309c6f1ddSLingrui98 // p"\n" 77409c6f1ddSLingrui98 77509c6f1ddSLingrui98 // } 77609c6f1ddSLingrui98 777d2b20d1aSTang Haojin // TODO: backend should pass topdown signals here 778d2b20d1aSTang Haojin // must not change its parent since BPU has used asTypeOf(this type) from its parent class 779d2b20d1aSTang Haojin require(isInstanceOf[Redirect]) 780d2b20d1aSTang Haojin val BTBMissBubble = Bool() 781d2b20d1aSTang Haojin def ControlRedirectBubble = debugIsCtrl 782d2b20d1aSTang Haojin // if mispred br not in ftb, count as BTB miss 783d2b20d1aSTang Haojin def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 784d2b20d1aSTang Haojin def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 785d2b20d1aSTang Haojin def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 786d2b20d1aSTang Haojin def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 787d2b20d1aSTang Haojin def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 788d2b20d1aSTang Haojin def MemVioRedirectBubble = debugIsMemVio 789d2b20d1aSTang Haojin def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 790d2b20d1aSTang Haojin 791d2b20d1aSTang Haojin def connectRedirect(source: Redirect): Unit = { 792d2b20d1aSTang Haojin for ((name, data) <- this.elements) { 793d2b20d1aSTang Haojin if (source.elements.contains(name)) { 794d2b20d1aSTang Haojin data := source.elements(name) 795d2b20d1aSTang Haojin } 796d2b20d1aSTang Haojin } 797d2b20d1aSTang Haojin } 798d2b20d1aSTang Haojin 79909c6f1ddSLingrui98 def display(cond: Bool): Unit = { 80009c6f1ddSLingrui98 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 80109c6f1ddSLingrui98 XSDebug(cond, p"-----------cfiUpdate----------- \n") 80209c6f1ddSLingrui98 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 803c2ad24ebSLingrui98 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 80409c6f1ddSLingrui98 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 80509c6f1ddSLingrui98 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 80609c6f1ddSLingrui98 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 80709c6f1ddSLingrui98 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 80809c6f1ddSLingrui98 XSDebug(cond, p"------------------------------- \n") 8099aca92b9SYinan Xu XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 81009c6f1ddSLingrui98 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 81109c6f1ddSLingrui98 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 81209c6f1ddSLingrui98 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 81309c6f1ddSLingrui98 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 81409c6f1ddSLingrui98 XSDebug(cond, p"---------------------------------------------- \n") 81509c6f1ddSLingrui98 } 81609c6f1ddSLingrui98} 817