1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.frontend 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import xiangshan.cache._ 24import xiangshan.cache.mmu._ 25import chisel3.experimental.verification 26import utils._ 27 28trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 29 def mmioBusWidth = 64 30 def mmioBusBytes = mmioBusWidth /8 31 def mmioBeats = FetchWidth * 4 * 8 / mmioBusWidth 32 def mmioMask = VecInit(List.fill(PredictWidth)(true.B)).asUInt 33 def mmioBusAligned(pc :UInt): UInt = align(pc, mmioBusBytes) 34} 35 36trait HasIFUConst extends HasXSParameter { 37 def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 38 // def groupAligned(pc: UInt) = align(pc, groupBytes) 39 // def packetAligned(pc: UInt) = align(pc, packetBytes) 40} 41 42class IfuToFtqIO(implicit p:Parameters) extends XSBundle { 43 val pdWb = Valid(new PredecodeWritebackBundle) 44} 45 46class FtqInterface(implicit p: Parameters) extends XSBundle { 47 val fromFtq = Flipped(new FtqToIfuIO) 48 val toFtq = new IfuToFtqIO 49} 50 51class ICacheInterface(implicit p: Parameters) extends XSBundle { 52 val toIMeta = Decoupled(new ICacheReadBundle) 53 val toIData = Decoupled(new ICacheReadBundle) 54 val toMissQueue = Vec(2,Decoupled(new ICacheMissReq)) 55 val fromIMeta = Input(new ICacheMetaRespBundle) 56 val fromIData = Input(new ICacheDataRespBundle) 57 val fromMissQueue = Vec(2,Flipped(Decoupled(new ICacheMissResp))) 58} 59 60class NewIFUIO(implicit p: Parameters) extends XSBundle { 61 val ftqInter = new FtqInterface 62 val icacheInter = new ICacheInterface 63 val toIbuffer = Decoupled(new FetchToIBuffer) 64 val iTLBInter = Vec(2, new BlockTlbRequestIO) 65} 66 67// record the situation in which fallThruAddr falls into 68// the middle of an RVI inst 69class LastHalfInfo(implicit p: Parameters) extends XSBundle { 70 val valid = Bool() 71 val middlePC = UInt(VAddrBits.W) 72 def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr 73} 74 75class IfuToPreDecode(implicit p: Parameters) extends XSBundle { 76 val data = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W)) 77 val startAddr = UInt(VAddrBits.W) 78 val fallThruAddr = UInt(VAddrBits.W) 79 val fallThruError = Bool() 80 val isDoubleLine = Bool() 81 val ftqOffset = Valid(UInt(log2Ceil(PredictWidth).W)) 82 val target = UInt(VAddrBits.W) 83 val pageFault = Vec(2, Bool()) 84 val accessFault = Vec(2, Bool()) 85 val instValid = Bool() 86 val lastHalfMatch = Bool() 87 val oversize = Bool() 88} 89 90class NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters 91{ 92 println(s"icache ways: ${nWays} sets:${nSets}") 93 val io = IO(new NewIFUIO) 94 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 95 val (toMeta, toData, meta_resp, data_resp) = (io.icacheInter.toIMeta, io.icacheInter.toIData, io.icacheInter.fromIMeta, io.icacheInter.fromIData) 96 val (toMissQueue, fromMissQueue) = (io.icacheInter.toMissQueue, io.icacheInter.fromMissQueue) 97 val (toITLB, fromITLB) = (VecInit(io.iTLBInter.map(_.req)), VecInit(io.iTLBInter.map(_.resp))) 98 99 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 100 101 def isLastInCacheline(fallThruAddr: UInt): Bool = fallThruAddr(blockOffBits - 1, 1) === 0.U 102 103 104 //--------------------------------------------- 105 // Fetch Stage 1 : 106 // * Send req to ICache Meta/Data 107 // * Check whether need 2 line fetch 108 //--------------------------------------------- 109 110 val f0_valid = fromFtq.req.valid 111 val f0_ftq_req = fromFtq.req.bits 112 val f0_situation = VecInit(Seq(isCrossLineReq(f0_ftq_req.startAddr, f0_ftq_req.fallThruAddr), isLastInCacheline(f0_ftq_req.fallThruAddr))) 113 val f0_doubleLine = f0_situation(0) || f0_situation(1) 114 val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.fallThruAddr)) 115 val f0_fire = fromFtq.req.fire() 116 117 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 118 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 119 120 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 121 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 122 123 val f3_redirect = WireInit(false.B) 124 f3_flush := fromFtq.redirect.valid 125 f2_flush := f3_flush || f3_redirect 126 f1_flush := f2_flush || from_bpu_f1_flush 127 f0_flush := f1_flush || from_bpu_f0_flush 128 129 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 130 131 //fetch: send addr to Meta/TLB and Data simultaneously 132 val fetch_req = List(toMeta, toData) 133 for(i <- 0 until 2) { 134 fetch_req(i).valid := f0_fire 135 fetch_req(i).bits.isDoubleLine := f0_doubleLine 136 fetch_req(i).bits.vSetIdx := f0_vSetIdx 137 } 138 139 fromFtq.req.ready := fetch_req(0).ready && fetch_req(1).ready && f1_ready && GTimer() > 500.U 140 141 //--------------------------------------------- 142 // Fetch Stage 2 : 143 // * Send req to ITLB and TLB Response (Get Paddr) 144 // * ICache Response (Get Meta and Data) 145 // * Hit Check (Generate hit signal and hit vector) 146 // * Get victim way 147 //--------------------------------------------- 148 149 //TODO: handle fetch exceptions 150 151 val tlbRespAllValid = WireInit(false.B) 152 153 val f1_valid = RegInit(false.B) 154 val f1_ftq_req = RegEnable(next = f0_ftq_req, enable=f0_fire) 155 val f1_situation = RegEnable(next = f0_situation, enable=f0_fire) 156 val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire) 157 val f1_vSetIdx = RegEnable(next = f0_vSetIdx, enable=f0_fire) 158 val f1_fire = f1_valid && tlbRespAllValid && f2_ready 159 160 f1_ready := f2_ready && tlbRespAllValid || !f1_valid 161 162 from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) 163 164 val preDecoder = Module(new PreDecode) 165 val (preDecoderIn, preDecoderOut) = (preDecoder.io.in, preDecoder.io.out) 166 167 //flush generate and to Ftq 168 val predecodeOutValid = WireInit(false.B) 169 170 when(f1_flush) {f1_valid := false.B} 171 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 172 .elsewhen(f1_fire) {f1_valid := false.B} 173 174 toITLB(0).valid := f1_valid 175 toITLB(0).bits.vaddr := align(f1_ftq_req.startAddr, blockBytes) 176 toITLB(0).bits.debug.pc := align(f1_ftq_req.startAddr, blockBytes) 177 178 toITLB(1).valid := f1_valid && f1_doubleLine 179 toITLB(1).bits.vaddr := align(f1_ftq_req.fallThruAddr, blockBytes) 180 toITLB(1).bits.debug.pc := align(f1_ftq_req.fallThruAddr, blockBytes) 181 182 toITLB.map{port => 183 port.bits.cmd := TlbCmd.exec 184 port.bits.roqIdx := DontCare 185 port.bits.debug.isFirstIssue := DontCare 186 } 187 188 fromITLB.map(_.ready := true.B) 189 190 val (tlbRespValid, tlbRespPAddr) = (fromITLB.map(_.valid), VecInit(fromITLB.map(_.bits.paddr))) 191 val (tlbRespMiss, tlbRespMMIO) = (fromITLB.map(port => port.bits.miss && port.valid), fromITLB.map(port => port.bits.mmio && port.valid)) 192 val (tlbExcpPF, tlbExcpAF) = (fromITLB.map(port => port.bits.excp.pf.instr && port.valid), fromITLB.map(port => port.bits.excp.af.instr && port.valid)) 193 194 tlbRespAllValid := tlbRespValid(0) && (tlbRespValid(1) || !f1_doubleLine) 195 196 val f1_pAddrs = tlbRespPAddr //TODO: Temporary assignment 197 val f1_pTags = VecInit(f1_pAddrs.map(get_phy_tag(_))) 198 val (f1_tags, f1_cacheline_valid, f1_datas) = (meta_resp.tags, meta_resp.valid, data_resp.datas) 199 val bank0_hit_vec = VecInit(f1_tags(0).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(0)(i) && way_tag === f1_pTags(0) }) 200 val bank1_hit_vec = VecInit(f1_tags(1).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(1)(i) && way_tag === f1_pTags(1) }) 201 val (bank0_hit,bank1_hit) = (ParallelOR(bank0_hit_vec) && !tlbExcpPF(0) && !tlbExcpAF(0), ParallelOR(bank1_hit_vec) && !tlbExcpPF(1) && !tlbExcpAF(1)) 202 val f1_hit = (bank0_hit && bank1_hit && f1_valid && f1_doubleLine) || (f1_valid && !f1_doubleLine && bank0_hit) 203 val f1_bank_hit_vec = VecInit(Seq(bank0_hit_vec, bank1_hit_vec)) 204 val f1_bank_hit = VecInit(Seq(bank0_hit, bank1_hit)) 205 206 val replacers = Seq.fill(2)(ReplacementPolicy.fromString(Some("random"),nWays,nSets/2)) 207 val f1_victim_masks = VecInit(replacers.zipWithIndex.map{case (replacer, i) => UIntToOH(replacer.way(f1_vSetIdx(i)))}) 208 209 val touch_sets = Seq.fill(2)(Wire(Vec(2, UInt(log2Ceil(nSets/2).W)))) 210 val touch_ways = Seq.fill(2)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W)))) ) 211 212 ((replacers zip touch_sets) zip touch_ways).map{case ((r, s),w) => r.access(s,w)} 213 214 val f1_hit_data = VecInit(f1_datas.zipWithIndex.map { case(bank, i) => 215 val bank_hit_data = Mux1H(f1_bank_hit_vec(i).asUInt, bank) 216 bank_hit_data 217 }) 218 219 220 //--------------------------------------------- 221 // Fetch Stage 3 : 222 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 223 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 224 // * cut cacheline(s) and send to PreDecode 225 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 226 //--------------------------------------------- 227 val f2_fetchFinish = Wire(Bool()) 228 229 val f2_valid = RegInit(false.B) 230 val f2_ftq_req = RegEnable(next = f1_ftq_req, enable = f1_fire) 231 val f2_situation = RegEnable(next = f1_situation, enable=f1_fire) 232 val f2_doubleLine = RegEnable(next = f1_doubleLine, enable=f1_fire) 233 val f2_fire = f2_valid && f2_fetchFinish && f3_ready 234 235 f2_ready := (f3_ready && f2_fetchFinish) || !f2_valid 236 237 when(f2_flush) {f2_valid := false.B} 238 .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B } 239 .elsewhen(f2_fire) {f2_valid := false.B} 240 241 242 val f2_pAddrs = RegEnable(next = f1_pAddrs, enable = f1_fire) 243 val f2_hit = RegEnable(next = f1_hit , enable = f1_fire) 244 val f2_bank_hit = RegEnable(next = f1_bank_hit, enable = f1_fire) 245 val f2_miss = f2_valid && !f2_hit 246 val (f2_vSetIdx, f2_pTags) = (RegEnable(next = f1_vSetIdx, enable = f1_fire), RegEnable(next = f1_pTags, enable = f1_fire)) 247 val f2_waymask = RegEnable(next = f1_victim_masks, enable = f1_fire) 248 //exception information 249 val f2_except_pf = RegEnable(next = VecInit(tlbExcpPF), enable = f1_fire) 250 val f2_except_af = RegEnable(next = VecInit(tlbExcpAF), enable = f1_fire) 251 val f2_except = VecInit((0 until 2).map{i => f2_except_pf(i) || f2_except_af(i)}) 252 val f2_has_except = f2_valid && (f2_except_af.reduce(_||_) || f2_except_pf.reduce(_||_)) 253 254 //instruction 255 val wait_idle :: wait_queue_ready :: wait_send_req :: wait_two_resp :: wait_0_resp :: wait_1_resp :: wait_one_resp ::wait_finish :: Nil = Enum(8) 256 val wait_state = RegInit(wait_idle) 257 258 fromMissQueue.map{port => port.ready := true.B} 259 260 val (miss0_resp, miss1_resp) = (fromMissQueue(0).fire(), fromMissQueue(1).fire()) 261 val (bank0_fix, bank1_fix) = (miss0_resp && !f2_bank_hit(0), miss1_resp && f2_doubleLine && !f2_bank_hit(1)) 262 263 val only_0_miss = f2_valid && !f2_hit && !f2_doubleLine && !f2_has_except 264 val (hit_0_miss_1 , miss_0_hit_1, miss_0_miss_1) = ( (f2_valid && !f2_bank_hit(1) && f2_bank_hit(0) && f2_doubleLine && !f2_has_except), 265 (f2_valid && !f2_bank_hit(0) && f2_bank_hit(1) && f2_doubleLine && !f2_has_except), 266 (f2_valid && !f2_bank_hit(0) && !f2_bank_hit(1) && f2_doubleLine && !f2_has_except), 267 ) 268 269 val hit_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && f2_bank_hit(0) 270 val miss_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && !f2_bank_hit(0) 271 //val fetch0_except_1 = hit_0_except_1 || miss_0_except_1 272 val except_0 = f2_valid && f2_except(0) 273 274 val f2_mq_datas = Reg(Vec(2, UInt(blockBits.W))) 275 276 when(fromMissQueue(0).fire) {f2_mq_datas(0) := fromMissQueue(0).bits.data} 277 when(fromMissQueue(1).fire) {f2_mq_datas(1) := fromMissQueue(1).bits.data} 278 279 switch(wait_state){ 280 is(wait_idle){ 281 when(miss_0_except_1){ 282 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 283 }.elsewhen( only_0_miss || miss_0_hit_1){ 284 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 285 }.elsewhen(hit_0_miss_1){ 286 wait_state := Mux(toMissQueue(1).ready, wait_queue_ready ,wait_idle ) 287 }.elsewhen( miss_0_miss_1 ){ 288 wait_state := Mux(toMissQueue(0).ready && toMissQueue(1).ready, wait_queue_ready ,wait_idle) 289 } 290 } 291 292 //TODO: naive logic for wait icache response 293 is(wait_queue_ready){ 294 wait_state := wait_send_req 295 } 296 297 is(wait_send_req) { 298 when(miss_0_except_1 || only_0_miss || hit_0_miss_1 || miss_0_hit_1){ 299 wait_state := wait_one_resp 300 }.elsewhen( miss_0_miss_1 ){ 301 wait_state := wait_two_resp 302 } 303 } 304 305 is(wait_one_resp) { 306 when( (miss_0_except_1 ||only_0_miss || miss_0_hit_1) && fromMissQueue(0).fire()){ 307 wait_state := wait_finish 308 }.elsewhen( hit_0_miss_1 && fromMissQueue(1).fire()){ 309 wait_state := wait_finish 310 } 311 } 312 313 is(wait_two_resp) { 314 when(fromMissQueue(0).fire() && fromMissQueue(1).fire()){ 315 wait_state := wait_finish 316 }.elsewhen( !fromMissQueue(0).fire() && fromMissQueue(1).fire() ){ 317 wait_state := wait_0_resp 318 }.elsewhen(fromMissQueue(0).fire() && !fromMissQueue(1).fire()){ 319 wait_state := wait_1_resp 320 } 321 } 322 323 is(wait_0_resp) { 324 when(fromMissQueue(0).fire()){ 325 wait_state := wait_finish 326 } 327 } 328 329 is(wait_1_resp) { 330 when(fromMissQueue(1).fire()){ 331 wait_state := wait_finish 332 } 333 } 334 335 is(wait_finish) { 336 when(f2_fire) {wait_state := wait_idle } 337 } 338 } 339 340 when(f2_flush) { wait_state := wait_idle } 341 342 (0 until 2).map { i => 343 if(i == 1) toMissQueue(i).valid := (hit_0_miss_1 || miss_0_miss_1) && wait_state === wait_queue_ready 344 else toMissQueue(i).valid := (only_0_miss || miss_0_hit_1 || miss_0_miss_1) && wait_state === wait_queue_ready 345 toMissQueue(i).bits.addr := f2_pAddrs(i) 346 toMissQueue(i).bits.vSetIdx := f2_vSetIdx(i) 347 toMissQueue(i).bits.waymask := f2_waymask(i) 348 toMissQueue(i).bits.clientID :=0.U 349 } 350 351 val miss_all_fix = (wait_state === wait_finish) 352 353 f2_fetchFinish := ((f2_valid && f2_hit) || miss_all_fix || hit_0_except_1 || except_0) 354 355 356 (touch_ways zip touch_sets).zipWithIndex.map{ case((t_w,t_s), i) => 357 t_s(0) := f1_vSetIdx(i) 358 t_w(0).valid := f1_bank_hit(i) 359 t_w(0).bits := OHToUInt(f1_bank_hit_vec(i)) 360 361 t_s(1) := f2_vSetIdx(i) 362 t_w(1).valid := f2_valid && !f2_bank_hit(i) 363 t_w(1).bits := OHToUInt(f2_waymask(i)) 364 } 365 366 val sec_miss_reg = RegInit(0.U.asTypeOf(Vec(4, Bool()))) 367 val reservedRefillData = Reg(Vec(2, UInt(blockBits.W))) 368 val f2_hit_datas = RegEnable(next = f1_hit_data, enable = f1_fire) 369 val f2_datas = Wire(Vec(2, UInt(blockBits.W))) 370 371 f2_datas.zipWithIndex.map{case(bank,i) => 372 if(i == 0) bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(2),reservedRefillData(1),Mux(sec_miss_reg(0),reservedRefillData(0), f2_mq_datas(i)))) 373 else bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(3),reservedRefillData(1),Mux(sec_miss_reg(1),reservedRefillData(0), f2_mq_datas(i)))) 374 } 375 376 val f2_jump_valids = Fill(PredictWidth, !preDecoderOut.cfiOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> (~preDecoderOut.cfiOffset.bits) 377 val f2_predecode_valids = VecInit(preDecoderOut.pd.map(instr => instr.valid)).asUInt & f2_jump_valids 378 379 def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={ 380 if(HasCExtension){ 381 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 382 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W))) 383 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1)) 384 (0 until PredictWidth + 1).foreach( i => 385 result(i) := dataVec(startPtr + i.U) 386 ) 387 result 388 } else { 389 val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 390 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 391 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2)) 392 (0 until PredictWidth).foreach( i => 393 result(i) := dataVec(startPtr + i.U) 394 ) 395 result 396 } 397 } 398 399 val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr ) 400 401 // deal with secondary miss in f1 402 val f2_0_f1_0 = ((f2_valid && !f2_bank_hit(0)) && f1_valid && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr))) 403 val f2_0_f1_1 = ((f2_valid && !f2_bank_hit(0)) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U))) 404 val f2_1_f1_0 = ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr) )) 405 val f2_1_f1_1 = ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U) )) 406 407 val isSameLine = f2_0_f1_0 || f2_0_f1_1 || f2_1_f1_0 || f2_1_f1_1 408 val sec_miss_sit = VecInit(Seq(f2_0_f1_0, f2_0_f1_1, f2_1_f1_0, f2_1_f1_1)) 409 val hasSecMiss = RegInit(false.B) 410 411 when(f2_flush){ 412 sec_miss_reg.map(sig => sig := false.B) 413 hasSecMiss := false.B 414 }.elsewhen(isSameLine && !f1_flush && f2_fire){ 415 sec_miss_reg.zipWithIndex.map{case(sig, i) => sig := sec_miss_sit(i)} 416 hasSecMiss := true.B 417 }.elsewhen((!isSameLine || f1_flush) && hasSecMiss && f2_fire){ 418 sec_miss_reg.map(sig => sig := false.B) 419 hasSecMiss := false.B 420 } 421 422 when((f2_0_f1_0 || f2_0_f1_1) && f2_fire){ 423 reservedRefillData(0) := f2_mq_datas(0) 424 } 425 426 when((f2_1_f1_0 || f2_1_f1_1) && f2_fire){ 427 reservedRefillData(1) := f2_mq_datas(1) 428 } 429 430 431 //--------------------------------------------- 432 // Fetch Stage 4 : 433 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 434 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 435 // * cut cacheline(s) and send to PreDecode 436 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 437 //--------------------------------------------- 438 val f3_valid = RegInit(false.B) 439 val f3_ftq_req = RegEnable(next = f2_ftq_req, enable=f2_fire) 440 val f3_situation = RegEnable(next = f2_situation, enable=f2_fire) 441 val f3_doubleLine = RegEnable(next = f2_doubleLine, enable=f2_fire) 442 val f3_fire = io.toIbuffer.fire() 443 444 when(f3_flush) {f3_valid := false.B} 445 .elsewhen(f2_fire && !f2_flush) {f3_valid := true.B } 446 .elsewhen(io.toIbuffer.fire()) {f3_valid := false.B} 447 448 f3_ready := io.toIbuffer.ready || !f2_valid 449 450 val f3_cut_data = RegEnable(next = f2_cut_data, enable=f2_fire) 451 val f3_except_pf = RegEnable(next = f2_except_pf, enable = f2_fire) 452 val f3_except_af = RegEnable(next = f2_except_af, enable = f2_fire) 453 val f3_hit = RegEnable(next = f2_hit , enable = f2_fire) 454 455 val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 456 val f3_lastHalfMatch = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr) 457 val f3_except = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)}) 458 val f3_has_except = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_)) 459 460 461 preDecoderIn.instValid := f3_valid && !f3_has_except 462 preDecoderIn.data := f3_cut_data 463 preDecoderIn.startAddr := f3_ftq_req.startAddr 464 preDecoderIn.fallThruAddr := f3_ftq_req.fallThruAddr 465 preDecoderIn.fallThruError := f3_ftq_req.fallThruError 466 preDecoderIn.isDoubleLine := f3_doubleLine 467 preDecoderIn.ftqOffset := f3_ftq_req.ftqOffset 468 preDecoderIn.target := f3_ftq_req.target 469 preDecoderIn.oversize := f3_ftq_req.oversize 470 preDecoderIn.lastHalfMatch := f3_lastHalfMatch 471 preDecoderIn.pageFault := f3_except_pf 472 preDecoderIn.accessFault := f3_except_af 473 474 475 // TODO: What if next packet does not match? 476 when (f3_flush) { 477 f3_lastHalf.valid := false.B 478 }.elsewhen (io.toIbuffer.fire()) { 479 f3_lastHalf.valid := preDecoderOut.hasLastHalf 480 f3_lastHalf.middlePC := preDecoderOut.realEndPC 481 } 482 483 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 484 485 io.toIbuffer.valid := f3_valid 486 io.toIbuffer.bits.instrs := preDecoderOut.instrs 487 io.toIbuffer.bits.valid := f3_predecode_range & preDecoderOut.instrRange.asUInt 488 io.toIbuffer.bits.pd := preDecoderOut.pd 489 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 490 io.toIbuffer.bits.pc := preDecoderOut.pc 491 io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i)} 492 io.toIbuffer.bits.foldpc := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth)) 493 io.toIbuffer.bits.ipf := preDecoderOut.pageFault 494 io.toIbuffer.bits.acf := preDecoderOut.accessFault 495 io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF 496 497 //Write back to Ftq 498 val finishFetchMaskReg = RegNext(f3_valid && !(f2_fire && !f2_flush)) 499 500 toFtq.pdWb.valid := !finishFetchMaskReg && f3_valid 501 toFtq.pdWb.bits.pc := preDecoderOut.pc 502 toFtq.pdWb.bits.pd := preDecoderOut.pd 503 toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := f3_predecode_range(i)} 504 toFtq.pdWb.bits.ftqIdx := f3_ftq_req.ftqIdx 505 toFtq.pdWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 506 toFtq.pdWb.bits.misOffset := preDecoderOut.misOffset 507 toFtq.pdWb.bits.cfiOffset := preDecoderOut.cfiOffset 508 toFtq.pdWb.bits.target := preDecoderOut.target 509 toFtq.pdWb.bits.jalTarget := preDecoderOut.jalTarget 510 toFtq.pdWb.bits.instrRange := preDecoderOut.instrRange 511 512 val predecodeFlush = preDecoderOut.misOffset.valid && f3_valid 513 val predecodeFlushReg = RegNext(predecodeFlush && !(f2_fire && !f2_flush)) 514 515 f3_redirect := !predecodeFlushReg && predecodeFlush 516 517 // Performance Counter 518 XSPerfAccumulate("req", io.toIbuffer.fire() ) 519 XSPerfAccumulate("miss", io.toIbuffer.fire() && !f3_hit ) 520 XSPerfAccumulate("frontendFlush", f3_redirect ) 521 XSPerfAccumulate("only_0_miss", only_0_miss ) 522 XSPerfAccumulate("hit_0_miss_1", hit_0_miss_1 ) 523 XSPerfAccumulate("miss_0_hit_1", miss_0_hit_1 ) 524 XSPerfAccumulate("miss_0_miss_1", miss_0_miss_1 ) 525 XSPerfAccumulate("crossLine", io.toIbuffer.fire() && f3_situation(0) ) 526 XSPerfAccumulate("lastInLin", io.toIbuffer.fire() && f3_situation(1) ) 527} 528