uBTB.scala 7.9 KB
Newer Older
1 2 3 4 5
package xiangshan.frontend

import chisel3._
import chisel3.util._
import utils._
Fa_wang's avatar
Fa_wang 已提交
6
import xiangshan._
7

J
jinyue110 已提交
8 9
import scala.math.min

10 11
trait MicroBTBPatameter{
    val nWays = 16
12
    val offsetSize = 20
13 14 15 16 17
}

class MicroBTB extends BasePredictor
    with MicroBTBPatameter
{
J
jinyue110 已提交
18 19 20
    val tagSize = VAddrBits - log2Ceil(PredictWidth) - 1

    class MicroBTBResp extends Resp
21
    {
L
Lingrui98 已提交
22 23
        val targets = Vec(PredictWidth, UInt(VAddrBits.W))
        val hits = Vec(PredictWidth, Bool())
24
        val takens = Vec(PredictWidth, Bool())
25
        val notTakens = Vec(PredictWidth, Bool())
26
        val is_RVC = Vec(PredictWidth, Bool())
27 28
    }

J
jinyue110 已提交
29
    class MicroBTBBranchInfo extends Meta
30
    {
J
jinyue110 已提交
31
        val writeWay = Vec(PredictWidth,UInt(log2Ceil(nWays).W))
32 33
        val hits = Vec(PredictWidth,Bool())
    }
J
jinyue110 已提交
34
    val out_ubtb_br_info = Wire(new MicroBTBBranchInfo)
35
    override val metaLen = out_ubtb_br_info.asUInt.getWidth
36

J
jinyue110 已提交
37
    class MicroBTBIO extends DefaultBasePredictorIO
38 39
    {
        val out = Output(new MicroBTBResp)   //
J
jinyue110 已提交
40
        val uBTBBranchInfo = Output(new MicroBTBBranchInfo)
41 42
    }

43
    override val io = IO(new MicroBTBIO)
J
jinyue110 已提交
44
    io.uBTBBranchInfo <> out_ubtb_br_info
45

Fa_wang's avatar
Fa_wang 已提交
46
    def getTag(pc: UInt) = (pc >> (log2Ceil(PredictWidth) + 1)).asUInt()
47 48 49 50 51 52 53 54 55
    def getBank(pc: UInt) = pc(log2Ceil(PredictWidth) ,1)
    def satUpdate(old: UInt, len: Int, taken: Bool): UInt = {
        val oldSatTaken = old === ((1 << len)-1).U
        val oldSatNotTaken = old === 0.U
        Mux(oldSatTaken && taken, ((1 << len)-1).U,
            Mux(oldSatNotTaken && !taken, 0.U,
            Mux(taken, old + 1.U, old - 1.U)))
    } 

J
jinyue110 已提交
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70
    class MicroBTBMeta extends XSBundle
    {
        val is_Br = Bool()
        val is_RVC = Bool()
        val valid = Bool()
        val pred = UInt(2.W)
        val tag = UInt(tagSize.W)
    }

    class MicroBTBEntry extends XSBundle
    {
        val offset = SInt(offsetSize.W)
    }

J
jinyue110 已提交
71 72
    val uBTBMeta = RegInit((0.U).asTypeOf(Vec(nWays, Vec(PredictWidth, new MicroBTBMeta))))
    val uBTB = Reg(Vec(nWays, Vec(PredictWidth, new MicroBTBEntry)))
73 74 75

    //uBTB read
    //tag is bank align
J
jinyue110 已提交
76 77 78
    val read_valid = io.pc.valid
    val read_req_tag = getTag(io.pc.bits)
    val read_req_basebank = getBank(io.pc.bits)
79
    val read_mask = circularShiftRight(io.inMask, PredictWidth, read_req_basebank)
J
jinyue110 已提交
80 81

    XSDebug(read_valid,"uBTB read req: pc:0x%x, tag:%x  basebank:%d\n",io.pc.bits,read_req_tag,read_req_basebank)
82 83 84 85
    
    class ReadRespEntry extends XSBundle
    {
        val is_RVC = Bool()
86
        val target = UInt(VAddrBits.W)
87 88
        val valid = Bool()
        val taken = Bool()
89
        val notTaken = Bool()
90 91 92
    }
    val read_resp = Wire(Vec(PredictWidth,new ReadRespEntry))

L
Lingrui98 已提交
93
    val read_bank_inOrder = VecInit((0 until PredictWidth).map(b => (read_req_basebank + b.U)(log2Up(PredictWidth)-1,0) ))
J
jinyue110 已提交
94
    val isInNextRow = VecInit((0 until PredictWidth).map(_.U < read_req_basebank))
95
    val read_hit_ohs = read_bank_inOrder.map{ b =>
96
        VecInit((0 until nWays) map {w => 
J
jinyue110 已提交
97
            Mux(isInNextRow(b),read_req_tag + 1.U,read_req_tag) === uBTBMeta(b)(w).tag
98
        })
99 100
    }

101

J
jinyue110 已提交
102 103 104
    val read_hit_vec = VecInit(read_hit_ohs.map{oh => ParallelOR(oh).asBool})
    val read_hit_ways = VecInit(read_hit_ohs.map{oh => PriorityEncoder(oh)})
    val read_hit =  ParallelOR(read_hit_vec).asBool
Fa_wang's avatar
Fa_wang 已提交
105
    val read_hit_way = PriorityEncoder(ParallelOR(read_hit_ohs.map(_.asUInt)))
106
    
J
jinyue110 已提交
107 108 109 110 111 112 113 114 115 116 117

    val  uBTBMeta_resp = VecInit((0 until PredictWidth).map(b => uBTBMeta(read_bank_inOrder(b))(read_hit_ways(b))))//uBTBMeta(i)(read_hit_ways(index))
    val  btb_resp = VecInit((0 until PredictWidth).map(b => uBTB(read_bank_inOrder(b))(read_hit_ways(b))))  

    for(i <- 0 until PredictWidth){
        // do not need to decide whether to produce results\
        read_resp(i).valid := uBTBMeta_resp(i).valid && read_hit_vec(i) && read_mask(i)
        read_resp(i).taken := read_resp(i).valid && uBTBMeta_resp(i).pred(1)
        read_resp(i).notTaken := read_resp(i).valid && !uBTBMeta_resp(i).pred(1)
        read_resp(i).target := ((io.pc.bits).asSInt + (i<<1).S + btb_resp(i).offset).asUInt
        read_resp(i).is_RVC := read_resp(i).valid && uBTBMeta_resp(i).is_RVC
L
Lingrui98 已提交
118 119

        out_ubtb_br_info.hits(i) := read_hit_vec(i)
120 121 122
    }

    //TODO: way alloc algorithm
J
jinyue110 已提交
123
    val alloc_way = {
J
jinyue110 已提交
124
        val r_uBTBMetas = Cat(VecInit(uBTBMeta.map(e => VecInit(e.map(_.tag)))).asUInt, (read_req_tag)(tagSize-1,0))
125
        val l = log2Ceil(nWays)
J
jinyue110 已提交
126
        val nChunks = (r_uBTBMetas.getWidth + l - 1) / l
127
        val chunks = (0 until nChunks) map { i =>
J
jinyue110 已提交
128
        r_uBTBMetas(min((i+1)*l, r_uBTBMetas.getWidth)-1, i*l)
129 130 131
        }
        chunks.reduce(_^_)
    }
J
jinyue110 已提交
132
    out_ubtb_br_info.writeWay.map(_:= Mux(read_hit,read_hit_way,alloc_way))
133
    XSDebug(read_valid,"uBTB read resp:   read_hit_vec:%d, read_hit_way:%d  alloc_way:%d\n",read_hit_vec.asUInt,read_hit_way,alloc_way)
J
jinyue110 已提交
134
    for(i <- 0 until PredictWidth) {
135 136
        XSDebug(read_valid,"bank(%d)   hit:%d   way:%d   valid:%d  is_RVC:%d  taken:%d   notTaken:%d   target:0x%x\n",
                                 i.U,read_hit_vec(i),read_hit_ways(i),read_resp(i).valid,read_resp(i).is_RVC,read_resp(i).taken,read_resp(i).notTaken,read_resp(i).target )
J
jinyue110 已提交
137
    }
138 139 140 141
    //response
    //only when hit and instruction valid and entry valid can output data
    for(i <- 0 until PredictWidth)
    {
142 143 144 145 146
        io.out.targets(i) := read_resp(i).target
        io.out.hits(i) := read_resp(i).valid
        io.out.takens(i) := read_resp(i).taken
        io.out.is_RVC(i) := read_resp(i).is_RVC
        io.out.notTakens(i) := read_resp(i).notTaken
147 148 149 150
    }

    //uBTB update 
    //backend should send fetch pc to update
L
Lingrui98 已提交
151 152 153
    val u = io.update.bits.ui
    val update_fetch_pc  = u.pc
    val update_idx = u.fetchIdx
Fa_wang's avatar
Fa_wang 已提交
154
    val update_br_offset = (update_idx << 1).asUInt()
155
    val update_br_pc = update_fetch_pc + update_br_offset
L
Lingrui98 已提交
156 157 158
    val update_write_way = u.brInfo.ubtbWriteWay
    val update_hits = u.brInfo.ubtbHits
    val update_taken = u.taken
159 160

    val update_bank = getBank(update_br_pc)
J
jinyue110 已提交
161
    val update_base_bank = getBank(update_fetch_pc)
162
    val update_tag = getTag(update_br_pc)
L
Lingrui98 已提交
163 164
    val update_taget_offset =  u.target.asSInt - update_br_pc.asSInt
    val update_is_BR_or_JAL = (u.pd.brType === BrType.branch) || (u.pd.brType === BrType.jal) 
165

L
Lingrui98 已提交
166
    val entry_write_valid = io.update.valid && u.isMisPred && update_is_BR_or_JAL
J
jinyue110 已提交
167
    val meta_write_valid = io.update.valid && update_is_BR_or_JAL
168
    //write btb target when miss prediction
J
jinyue110 已提交
169
    when(entry_write_valid)
170 171 172
    {
        uBTB(update_bank)(update_write_way).offset := update_taget_offset
    }
J
jinyue110 已提交
173
    //write the uBTBMeta
J
jinyue110 已提交
174
    when(meta_write_valid)
175 176
    {
        //commit update
L
Lingrui98 已提交
177 178
        uBTBMeta(update_bank)(update_write_way).is_Br := u.pd.brType === BrType.branch
        uBTBMeta(update_bank)(update_write_way).is_RVC := u.pd.isRVC
J
jinyue110 已提交
179
        (0 until PredictWidth).foreach{b =>  uBTBMeta(b)(update_write_way).valid := false.B}
180 181 182 183
        uBTBMeta(update_bank)(update_write_way).valid := true.B
        uBTBMeta(update_bank)(update_write_way).tag := update_tag
        uBTBMeta(update_bank)(update_write_way).pred := 
        Mux(!update_hits(update_bank),
J
jinyue110 已提交
184
            Mux(update_taken,3.U,0.U),
185 186 187
            satUpdate( uBTBMeta(update_bank)(update_write_way).pred,2,update_taken)
        )
    }
J
jinyue110 已提交
188
    XSDebug(meta_write_valid,"uBTB update: update fetch pc:0x%x  | real pc:0x%x  | update hits%b  | update_write_way:%d\n",update_fetch_pc,update_br_pc,update_hits,update_write_way)
J
jinyue110 已提交
189 190
   
   //bypass:read-after-write 
J
jinyue110 已提交
191
   for( b <- 0 until PredictWidth) {
J
jinyue110 已提交
192
        when(update_bank === b.U && meta_write_valid && read_valid
J
jinyue110 已提交
193 194
            && Mux(b.U < update_base_bank,update_tag===read_req_tag+1.U ,update_tag===read_req_tag))  //read and write is the same fetch-packet
        {
L
Lingrui98 已提交
195 196 197 198
            io.out.targets(b) := u.target
            io.out.takens(b) := u.taken
            io.out.is_RVC(b) := u.pd.isRVC
            io.out.notTakens(b) := (u.pd.brType === BrType.branch) && (!io.out.takens(b))
Fa_wang's avatar
Fa_wang 已提交
199
            XSDebug("uBTB bypass hit! :   hitpc:0x%x |  hitbanck:%d  |  out_target:0x%x\n",io.pc.bits+(b<<1).asUInt(),b.U, io.out.targets(b))
J
jinyue110 已提交
200 201
        }
    }
202
}