IFU.scala 2.0 KB
Newer Older
Z
Zihao Yu 已提交
1
package noop
Z
Zihao Yu 已提交
2 3 4

import chisel3._
import chisel3.util._
5
import chisel3.util.experimental.BoringUtils
Z
Zihao Yu 已提交
6

7
import utils._
8
import bus.simplebus._
Z
Zihao Yu 已提交
9

Z
Zihao Yu 已提交
10
trait HasResetVector {
11
  val resetVector = 0x80100000L//TODO: set reset vec
Z
Zihao Yu 已提交
12 13
}

Z
Zihao Yu 已提交
14
class IFU extends NOOPModule with HasResetVector {
Z
Zihao Yu 已提交
15
  val io = IO(new Bundle {
Z
Zihao Yu 已提交
16
    val imem = new SimpleBusUC(userBits = AddrBits*2)
Z
Zihao Yu 已提交
17 18
    val out = Decoupled(new CtrlFlowIO)
    val redirect = Flipped(new RedirectIO)
19
    val flushVec = Output(UInt(4.W))
20
    val bpFlush = Output(Bool())
Z
Zihao Yu 已提交
21 22
  })

Z
Zihao Yu 已提交
23
  // pc
Z
Zihao Yu 已提交
24
  val pc = RegInit(resetVector.U(AddrBits.W))
Z
Zihao Yu 已提交
25
  val pcUpdate = io.redirect.valid || io.imem.req.fire()
26
  val snpc = pc + 4.U  // sequential next pc
27 28

  val bp1 = Module(new BPU1)
29 30
  // predicted next pc
  val pnpc = bp1.io.out.target
Z
Zihao Yu 已提交
31
  val npc = Mux(io.redirect.valid, io.redirect.target, Mux(bp1.io.out.valid, pnpc, snpc))
32

33
  bp1.io.in.pc.valid := io.imem.req.fire() // only predict when Icache accepts a request
34
  bp1.io.in.pc.bits := npc  // predict one cycle early
Z
Zihao Yu 已提交
35
  bp1.io.flush := io.redirect.valid
36

Z
Zihao Yu 已提交
37
  when (pcUpdate) { pc := npc }
Z
Zihao Yu 已提交
38

Z
Zihao Yu 已提交
39
  io.flushVec := Mux(io.redirect.valid, "b1111".U, 0.U)
40
  io.bpFlush := false.B
Z
Zihao Yu 已提交
41

42
  io.imem.req.bits.apply(addr = Cat(pc(AddrBits-1,2),0.U(2.W)), //cache will treat it as Cat(pc(63,3),0.U(3.W))
Z
Zihao Yu 已提交
43
    size = "b11".U, cmd = SimpleBusCmd.read, wdata = 0.U, wmask = 0.U, user = Cat(npc, pc))
Z
Zihao Yu 已提交
44
  io.imem.req.valid := io.out.ready
45
  io.imem.resp.ready := io.out.ready || io.flushVec(0)
46

Z
Zihao Yu 已提交
47
  io.out.bits := DontCare
Z
Zihao Yu 已提交
48
    //inst path only uses 32bit inst, get the right inst according to pc(2)
Z
Zihao Yu 已提交
49
  io.out.bits.instr := (if (XLEN == 64) io.imem.resp.bits.rdata.asTypeOf(Vec(2, UInt(32.W)))(io.out.bits.pc(2))
Z
Zihao Yu 已提交
50
                       else io.imem.resp.bits.rdata)
Z
Zihao Yu 已提交
51 52 53 54
  io.imem.resp.bits.user.map{ case x =>
    io.out.bits.pc := x(AddrBits-1,0)
    io.out.bits.pnpc := x(AddrBits*2-1,AddrBits)
  }
Z
Zihao Yu 已提交
55
  io.out.valid := io.imem.resp.valid && !io.flushVec(0)
56

57 58
  BoringUtils.addSource(BoolStopWatch(io.imem.req.valid, io.imem.resp.fire()), "perfCntCondMimemStall")
  BoringUtils.addSource(io.flushVec.orR, "perfCntCondMifuFlush")
Z
Zihao Yu 已提交
59
}