IFU.scala 3.7 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 = 0x80000000L//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 {
W
William Wang 已提交
16

17
    val imem = new SimpleBusUC(userBits = AddrBits*2 + 4)
W
William Wang 已提交
18
    // val pc = Input(UInt(AddrBits.W))
Z
Zihao Yu 已提交
19
    val out = Decoupled(new CtrlFlowIO)
W
William Wang 已提交
20

Z
Zihao Yu 已提交
21
    val redirect = Flipped(new RedirectIO)
22
    val flushVec = Output(UInt(4.W))
23
    val bpFlush = Output(Bool())
Z
Zihao Yu 已提交
24 25
  })

Z
Zihao Yu 已提交
26
  // pc
Z
Zihao Yu 已提交
27
  val pc = RegInit(resetVector.U(AddrBits.W))
28
  val pcUpdate = io.redirect.valid || io.imem.req.fire()
29
  val snpc = Mux(pc(1), pc + 2.U, pc + 4.U)  // sequential next pc
30 31

  val bp1 = Module(new BPU1)
32 33 34 35 36 37 38 39 40 41 42

  //
  val lateJump = bp1.io.lateJump
  val lateJumpLatch = RegInit(false.B) 
  when(io.out.fire() || bp1.io.flush) {
    lateJumpLatch := Mux(bp1.io.flush, false.B, lateJump)
  }
  val lateJumpTarget = RegEnable(bp1.io.out.target, lateJump)
  val lateJumpForceSeq = lateJump && bp1.io.out.valid
  val lateJumpForceTgt = lateJumpLatch && !bp1.io.flush

43
  // predicted next pc
44
  val pnpc = Mux(lateJump, snpc, bp1.io.out.target)
W
William Wang 已提交
45
  val pbrIdx = bp1.io.brIdx
46
  val npc = Mux(io.redirect.valid, io.redirect.target, Mux(lateJumpLatch, lateJumpTarget, Mux(bp1.io.out.valid, pnpc, snpc)))
47
  val npcIsSeq = Mux(io.redirect.valid , false.B, Mux(lateJumpLatch, false.B, Mux(lateJump, true.B, Mux(bp1.io.out.valid, false.B, true.B))))
48
  // val npc = Mux(io.redirect.valid, io.redirect.target, Mux(io.redirectRVC.valid, io.redirectRVC.target, snpc))
49
  val brIdx = Wire(UInt(4.W)) 
50 51
  // brIdx(0) -> branch at pc offset 0 (mod 4)
  // brIdx(1) -> branch at pc offset 2 (mod 4)
52
  // brIdx(2) -> branch at pc offset 6 (mod 8), and this inst is not rvc inst
53
  brIdx := Cat(npcIsSeq, Mux(io.redirect.valid, 0.U, pbrIdx))
54
  //TODO: BP will be disabled shortly after a redirect request
55

56
  bp1.io.in.pc.valid := io.imem.req.fire() // only predict when Icache accepts a request
57
  bp1.io.in.pc.bits := npc  // predict one cycle early
58
  // bp1.io.flush := io.redirect.valid 
59
  bp1.io.flush := io.redirect.valid
Z
Zihao Yu 已提交
60 61 62
  //val bp2 = Module(new BPU2)
  //bp2.io.in.bits := io.out.bits
  //bp2.io.in.valid := io.imem.resp.fire()
63

64 65 66 67
  when (pcUpdate) { 
    pc := npc 
    // printf("[IF1] pc=%x\n", pc)
  }
Z
Zihao Yu 已提交
68

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

W
William Wang 已提交
72
  io.imem.req.bits.apply(addr = Cat(pc(AddrBits-1,1),0.U(1.W)), //cache will treat it as Cat(pc(63,3),0.U(3.W))
73
    size = "b11".U, cmd = SimpleBusCmd.read, wdata = 0.U, wmask = 0.U, user = Cat(brIdx(3,0), npc, pc))
Z
Zihao Yu 已提交
74
  io.imem.req.valid := io.out.ready
W
William Wang 已提交
75

76
  io.imem.resp.ready := io.out.ready || io.flushVec(0)
77

78 79
  Debug(){
    when(io.imem.req.fire()){
80
      printf("[IFI] pc=%x user=%x %x %x %x\n", io.imem.req.bits.addr, io.imem.req.bits.user.getOrElse(0.U), io.redirect.valid, pbrIdx, brIdx)
81 82 83
    }
  }

Z
Zihao Yu 已提交
84
  io.out.bits := DontCare
Z
Zihao Yu 已提交
85
    //inst path only uses 32bit inst, get the right inst according to pc(2)
86

87 88
  Debug(){
    when (io.out.fire()) {
89
          printf("[IFO] pc=%x inst=%x\n", io.out.bits.pc, io.out.bits.instr)
90
    }
91 92
  }

W
William Wang 已提交
93 94 95
  // io.out.bits.instr := (if (XLEN == 64) io.imem.resp.bits.rdata.asTypeOf(Vec(2, UInt(32.W)))(io.out.bits.pc(2))
                      //  else io.imem.resp.bits.rdata)
  io.out.bits.instr := io.imem.resp.bits.rdata
Z
Zihao Yu 已提交
96 97 98
  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)
99
    io.out.bits.brIdx := x(AddrBits*2 + 3, AddrBits*2)
Z
Zihao Yu 已提交
100
  }
Z
Zihao Yu 已提交
101
  io.out.valid := io.imem.resp.valid && !io.flushVec(0)
102

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