IFU.scala 3.9 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 + 1)
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

  //
  val lateJump = bp1.io.lateJump
  val lateJumpLatch = RegInit(false.B) 
W
William Wang 已提交
36 37
  when(pcUpdate || bp1.io.flush) {
    lateJumpLatch := Mux(bp1.io.flush, false.B, lateJump && !lateJumpLatch)
38 39 40 41 42
  }
  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 49 50
  // Debug(){
  //   printf("[NPC] %x %x %x %x %x %x\n",lateJumpLatch, lateJumpTarget, lateJump, bp1.io.out.valid, pnpc, snpc)
  // }
51

52
  // val npc = Mux(io.redirect.valid, io.redirect.target, Mux(io.redirectRVC.valid, io.redirectRVC.target, snpc))
53
  val brIdx = Wire(UInt(4.W)) 
54 55
  // brIdx(0) -> branch at pc offset 0 (mod 4)
  // brIdx(1) -> branch at pc offset 2 (mod 4)
56
  // brIdx(2) -> branch at pc offset 6 (mod 8), and this inst is not rvc inst
57
  brIdx := Cat(npcIsSeq, Mux(io.redirect.valid, 0.U, pbrIdx))
58
  //TODO: BP will be disabled shortly after a redirect request
59

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

68 69 70 71
  when (pcUpdate) { 
    pc := npc 
    // printf("[IF1] pc=%x\n", pc)
  }
Z
Zihao Yu 已提交
72

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

W
William Wang 已提交
76
  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))
77
    size = "b11".U, cmd = SimpleBusCmd.read, wdata = 0.U, wmask = 0.U, user = Cat(0.U(1.W), brIdx(3,0), npc, pc))
Z
Zihao Yu 已提交
78
  io.imem.req.valid := io.out.ready
79
  //TODO: add ctrlFlow.exceptionVec
80
  io.imem.resp.ready := io.out.ready || io.flushVec(0)
81

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

85
  Debug(){
86
    when(io.imem.req.fire()){
87
      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)
88
    }
89
    when (io.out.fire()) {
90
          printf("[IFO] pc=%x inst=%x\n", io.out.bits.pc, io.out.bits.instr)
91
    }
92 93
  }

W
William Wang 已提交
94 95 96
  // 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 已提交
97 98 99
  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)
100
    io.out.bits.brIdx := x(AddrBits*2 + 3, AddrBits*2)
101
    io.out.bits.exceptionVec(instrPageFault) := x(AddrBits*2 + 4)
Z
Zihao Yu 已提交
102
  }
Z
Zihao Yu 已提交
103
  io.out.valid := io.imem.resp.valid && !io.flushVec(0)
104

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