uBTB.scala 13.8 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 brMask = 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 debug = true
44
    override val io = IO(new MicroBTBIO)
J
jinyue110 已提交
45
    io.uBTBBranchInfo <> out_ubtb_br_info
46

Fa_wang's avatar
Fa_wang 已提交
47
    def getTag(pc: UInt) = (pc >> (log2Ceil(PredictWidth) + 1)).asUInt()
48
    def getBank(pc: UInt) = pc(log2Ceil(PredictWidth) ,1)
J
jinyue110 已提交
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63
    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)
    }

L
Lingrui98 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    // val uBTBMeta = RegInit((0.U).asTypeOf(Vec(nWays, Vec(PredictWidth, new MicroBTBMeta))))
    // val uBTB = Reg(Vec(nWays, Vec(PredictWidth, new MicroBTBEntry)))

    // class UBTBMem[T <: Data](gen: T, nWays: Int) extends XSModule {
    //     class UBTBBundleR[T <: Data](private val gen: T, val way: Int) extends Bundle {
    //         val data = Output(Vec(way, gen))
    //     }
    //     class UBTBReadBus[T <: Data](private val gen: T, val way: Int) {
    //         val resp = Output(new UBTBBundleR(gen, way))
    //     }
    //     class UBTBWriteBus[T <: Data](private val gen: T, val set: Int, val way: Int) extends Bundle {
    //         val req = 
    //     }
    //     val io = IO(new Bundle {
    //         val wen = Input(Bool())
    //         val wWay = Input(UInt(log2Up(nWays).W))
    //         val wRow = Input(UInt(log2Up(PredictWidth).W))
    //         val wdata = Input(new T)
    //         val entries = Output(Vec(nWays, Vec(PredictWidth, gen)))
    //     })
    //     val mem = RegInit((0.U).asTypeOf(Vec(nWays, Vec(PredictWidth, new T))))
    //     io.entries := mem
    //     when (io.wen) {
    //         mem(wWay)(wRow) := wdata
    //     }
    // }

91 92 93 94 95 96
    class MetaOutput extends XSBundle {
        val is_Br = Bool()
        val is_RVC = Bool()
        val pred = UInt(2.W)
    }

L
Lingrui98 已提交
97
    class UBTBMetaBank(nWays: Int) extends XSModule {
L
Lingrui98 已提交
98 99 100 101
        val io = IO(new Bundle {
            val wen = Input(Bool())
            val wWay = Input(UInt(log2Up(nWays).W))
            val wdata = Input(new MicroBTBMeta)
102 103 104 105 106 107
            val rtag = Input(UInt(tagSize.W))
            val rdata = Output(new MetaOutput)
            val hit_ohs = Output(Vec(nWays, Bool()))
            val allocatable_way = Valid(UInt(log2Up(nWays).W))
            val rWay = Input(UInt(log2Up(nWays).W))
            val rpred = Output(UInt(2.W))
L
Lingrui98 已提交
108
        })
109 110 111 112 113 114 115 116 117 118 119 120 121 122
        val mem = Mem(nWays, new MicroBTBMeta)
        val rentries = VecInit((0 until nWays) map (i => mem(i)))
        val hit_ohs = VecInit(rentries map (e => e.valid && e.tag === io.rtag))
        val hit_way = PriorityEncoder(hit_ohs)
        val hit_entry = rentries(hit_way)
        io.hit_ohs := hit_ohs
        io.rdata.is_Br  := hit_entry.is_Br
        io.rdata.is_RVC := hit_entry.is_RVC
        io.rdata.pred   := hit_entry.pred
        val entry_emptys = VecInit(rentries.map(e => !e.valid))
        val allocatable = ParallelOR(entry_emptys)
        io.allocatable_way.bits := PriorityEncoder(entry_emptys)
        io.allocatable_way.valid := allocatable
        io.rpred := rentries(io.rWay).pred
L
Lingrui98 已提交
123
        when (io.wen) {
124
            mem.write(io.wWay, io.wdata)
L
Lingrui98 已提交
125 126 127
        }
    }

L
Lingrui98 已提交
128
    class UBTBDataBank(nWays: Int) extends XSModule {
L
Lingrui98 已提交
129 130 131 132
        val io = IO(new Bundle {
            val wen = Input(Bool())
            val wWay = Input(UInt(log2Up(nWays).W))
            val wdata = Input(new MicroBTBEntry)
133 134
            val rWay = Input(UInt(log2Up(nWays).W))
            val rdata = Output(new MicroBTBEntry)
L
Lingrui98 已提交
135
        })
136 137 138
        val mem = Mem(nWays, new MicroBTBEntry)
        val rentries = VecInit((0 until nWays) map (i => mem(i)))
        io.rdata := rentries(io.rWay)
L
Lingrui98 已提交
139
        when (io.wen) {
140
            mem.write(io.wWay, io.wdata)
L
Lingrui98 已提交
141 142 143
        }
    }

144 145 146 147
    val metaBanks = Seq.fill(PredictWidth)(Module(new UBTBMetaBank(nWays)))
    val dataBanks = Seq.fill(PredictWidth)(Module(new UBTBDataBank(nWays)))
    val metas = VecInit(metaBanks.map(_.io))
    val datas = VecInit(dataBanks.map(_.io))
L
Lingrui98 已提交
148

149 150
    val uBTBMeta = VecInit(metas.map(m => m.rdata))
    val uBTB     = VecInit(datas.map(d => d.rdata))
151

L
Lingrui98 已提交
152 153 154 155 156
    val do_reset = RegInit(true.B)
    val reset_way = RegInit(0.U(log2Ceil(nWays).W))
    when (do_reset) { reset_way := reset_way + 1.U }
    when (reset_way === nWays.U) { do_reset := false.B }

157 158
    //uBTB read
    //tag is bank align
J
jinyue110 已提交
159 160 161
    val read_valid = io.pc.valid
    val read_req_tag = getTag(io.pc.bits)
    val read_req_basebank = getBank(io.pc.bits)
Z
zhanglinjuan 已提交
162
    // val read_mask = circularShiftLeft(io.inMask, PredictWidth, read_req_basebank)
J
jinyue110 已提交
163

164 165 166 167
    
    class ReadRespEntry extends XSBundle
    {
        val is_RVC = Bool()
168
        val target = UInt(VAddrBits.W)
169 170
        val valid = Bool()
        val taken = Bool()
171
        val is_Br = Bool()
172 173 174
    }
    val read_resp = Wire(Vec(PredictWidth,new ReadRespEntry))

L
Lingrui98 已提交
175
    val read_bank_inOrder = VecInit((0 until PredictWidth).map(b => (read_req_basebank + b.U)(log2Up(PredictWidth)-1,0) ))
J
jinyue110 已提交
176
    val isInNextRow = VecInit((0 until PredictWidth).map(_.U < read_req_basebank))
177 178 179
    
    (0 until PredictWidth).map{ b => metas(b).rtag := Mux(isInNextRow(b),read_req_tag + 1.U,read_req_tag) }
    val read_hit_ohs = read_bank_inOrder.map{ b => metas(b).hit_ohs }
J
jinyue110 已提交
180 181
    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)})
182 183
    // val read_hit =  ParallelOR(read_hit_vec).asBool
    // val read_hit_way = PriorityEncoder(ParallelOR(read_hit_ohs.map(_.asUInt)))
184
    
J
jinyue110 已提交
185

186 187 188 189
    (0 until PredictWidth).map(b => datas(b).rWay := read_hit_ways(PredictWidth.U - read_bank_inOrder(b)))

    val  uBTBMeta_resp = VecInit((0 until PredictWidth).map(b => metas(read_bank_inOrder(b)).rdata))
    val  btb_resp = VecInit((0 until PredictWidth).map(b => datas(read_bank_inOrder(b)).rdata))  
J
jinyue110 已提交
190 191 192

    for(i <- 0 until PredictWidth){
        // do not need to decide whether to produce results\
193
        read_resp(i).valid := read_hit_vec(i) && io.inMask(i)
J
jinyue110 已提交
194
        read_resp(i).taken := read_resp(i).valid && uBTBMeta_resp(i).pred(1)
195
        read_resp(i).is_Br  := read_resp(i).valid && uBTBMeta_resp(i).is_Br
J
jinyue110 已提交
196 197
        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 已提交
198 199

        out_ubtb_br_info.hits(i) := read_hit_vec(i)
200 201 202
    }

    //TODO: way alloc algorithm
203 204 205 206
    def alloc_way(valids:UInt ,meta_tags:UInt,req_tag:UInt) = {
        val way = Wire(UInt(log2Up(BtbWays).W))
        val all_valid = valids.andR.asBool
        val tags = Cat(meta_tags,req_tag)
207
        val l = log2Ceil(nWays)
208
        val nChunks = (tags.getWidth + l - 1) / l
209
        val chunks = (0 until nChunks) map { i =>
210
            tags(min((i+1)*l, tags.getWidth)-1, i*l)
211
        }
212 213 214
        way := Mux(all_valid,chunks.reduce(_^_),PriorityEncoder(~valids))
        way
    }
215 216 217 218 219

    // val alloc_ways = read_bank_inOrder.map{ b => 
    //     alloc_way(VecInit(uBTBMeta.map(w => w(b).valid)).asUInt,
    //               VecInit(uBTBMeta.map(w => w(b).tag)).asUInt,
    //               Mux(isInNextRow(b).asBool,read_req_tag + 1.U,read_req_tag))
220
        
221 222 223 224
    // }

    val alloc_ways = read_bank_inOrder.map{ b => 
        Mux(metas(b).allocatable_way.valid, metas(b).allocatable_way.bits, LFSR64()(log2Ceil(nWays)-1,0))}
225
    (0 until PredictWidth).map(i => out_ubtb_br_info.writeWay(i) := Mux(read_hit_vec(i).asBool,read_hit_ways(i),alloc_ways(i)))
L
Lingrui98 已提交
226

227 228 229 230
    //response
    //only when hit and instruction valid and entry valid can output data
    for(i <- 0 until PredictWidth)
    {
231 232 233 234
        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
235
        io.out.brMask(i) := read_resp(i).is_Br
236 237 238 239
    }

    //uBTB update 
    //backend should send fetch pc to update
L
Lingrui98 已提交
240
    val u = io.update.bits.ui
J
jinyue110 已提交
241 242 243 244
    val update_br_pc  = u.pc
    val update_br_idx = u.fetchIdx
    val update_br_offset = (update_br_idx << 1).asUInt()
    val update_fetch_pc = update_br_pc - update_br_offset
L
Lingrui98 已提交
245 246 247
    val update_write_way = u.brInfo.ubtbWriteWay
    val update_hits = u.brInfo.ubtbHits
    val update_taken = u.taken
248 249

    val update_bank = getBank(update_br_pc)
J
jinyue110 已提交
250
    val update_base_bank = getBank(update_fetch_pc)
251
    val update_tag = getTag(update_br_pc)
J
jinyue110 已提交
252 253
    val update_target = Mux(u.pd.isBr, u.brTarget, u.target)
    val update_taget_offset =  update_target.asSInt - update_br_pc.asSInt
L
Lingrui98 已提交
254
    val update_is_BR_or_JAL = (u.pd.brType === BrType.branch) || (u.pd.brType === BrType.jal) 
255 256 257
  
  
    val jalFirstEncountered = !u.isMisPred && !u.brInfo.btbHitJal && (u.pd.brType === BrType.jal)
258 259
    val entry_write_valid = io.update.valid && (u.isMisPred || !u.isMisPred && u.pd.isBr || jalFirstEncountered)//io.update.valid //&& update_is_BR_or_JAL
    val meta_write_valid = io.update.valid && (u.isMisPred || !u.isMisPred && u.pd.isBr || jalFirstEncountered)//io.update.valid //&& update_is_BR_or_JAL
260
    //write btb target when miss prediction
L
Lingrui98 已提交
261 262 263 264
    // when(entry_write_valid)
    // {
    //     uBTB(update_write_way)(update_bank).offset := update_taget_offset
    // }
L
Lingrui98 已提交
265
    for (b <- 0 until PredictWidth) {
L
Lingrui98 已提交
266 267 268
        datas(b).wen := do_reset || (entry_write_valid && b.U === update_bank)
        datas(b).wWay := Mux(do_reset, reset_way, update_write_way)
        datas(b).wdata := Mux(do_reset, 0.U.asTypeOf(new MicroBTBEntry), update_taget_offset.asTypeOf(new MicroBTBEntry))
L
Lingrui98 已提交
269 270
    }

L
Lingrui98 已提交
271

272

J
jinyue110 已提交
273
    //write the uBTBMeta
274
    (0 until PredictWidth).map(i => metas(i).rWay := update_write_way)
L
Lingrui98 已提交
275 276 277 278 279 280 281
    val update_write_meta = Wire(new MicroBTBMeta)
    update_write_meta.is_Br  := u.pd.brType === BrType.branch
    update_write_meta.is_RVC := u.pd.isRVC
    update_write_meta.valid  := true.B
    update_write_meta.tag    := update_tag
    update_write_meta.pred   := Mux(!update_hits,
                                    Mux(update_taken,3.U,0.U),
282
                                    satUpdate( metas(update_bank).rpred,2,update_taken)
L
Lingrui98 已提交
283
                                )
L
Lingrui98 已提交
284 285

    for (b <- 0 until PredictWidth) {
L
Lingrui98 已提交
286 287 288
        metas(b).wen := do_reset || (meta_write_valid && b.U === update_bank)
        metas(b).wWay := Mux(do_reset, reset_way, update_write_way)
        metas(b).wdata := Mux(do_reset, 0.U.asTypeOf(new MicroBTBMeta), update_write_meta)
L
Lingrui98 已提交
289
    }
L
Lingrui98 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303
    // when(meta_write_valid)
    // {
    //     //commit update
    //     uBTBMeta(update_write_way)(update_bank).is_Br := u.pd.brType === BrType.branch
    //     uBTBMeta(update_write_way)(update_bank).is_RVC := u.pd.isRVC
    //     //(0 until PredictWidth).foreach{b =>  uBTBMeta(update_write_way)(b).valid := false.B}
    //     uBTBMeta(update_write_way)(update_bank).valid := true.B
    //     uBTBMeta(update_write_way)(update_bank).tag := update_tag
    //     uBTBMeta(update_write_way)(update_bank).pred := 
    //     Mux(!update_hits,
    //         Mux(update_taken,3.U,0.U),
    //         satUpdate( uBTBMeta(update_write_way)(update_bank).pred,2,update_taken)
    //     )
    // }
Z
zhanglinjuan 已提交
304

305
    if (BPUDebug && debug) {
L
Lingrui98 已提交
306 307 308 309 310 311
        // XSDebug(read_valid,"uBTB read req: pc:0x%x, tag:%x  basebank:%d\n",io.pc.bits,read_req_tag,read_req_basebank)
        // XSDebug(read_valid,"uBTB read resp:   read_hit_vec:%b, \n",read_hit_vec.asUInt)
        // for(i <- 0 until PredictWidth) {
        //     XSDebug(read_valid,"bank(%d)   hit:%d   way:%d   valid:%d  is_RVC:%d  taken:%d   isBr:%d   target:0x%x  alloc_way:%d\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).is_Br,read_resp(i).target,out_ubtb_br_info.writeWay(i))
        // }
L
Lingrui98 已提交
312

L
Lingrui98 已提交
313 314 315 316 317 318 319
        // XSDebug(meta_write_valid,"uBTB update: update | pc:0x%x  | update hits:%b | | update_write_way:%d  | update_bank: %d| update_br_index:%d | update_tag:%x | upadate_offset 0x%x\n "
        //             ,update_br_pc,update_hits,update_write_way,update_bank,update_br_idx,update_tag,update_taget_offset(offsetSize-1,0))
        // XSDebug(meta_write_valid, "uBTB update: update_taken:%d | old_pred:%b | new_pred:%b\n",
        //     update_taken, uBTBMeta(update_write_way)(update_bank).pred,
        //     Mux(!update_hits,
        //         Mux(update_taken,3.U,0.U),
        //         satUpdate( uBTBMeta(update_write_way)(update_bank).pred,2,update_taken)))
320

L
Lingrui98 已提交
321
    }
J
jinyue110 已提交
322 323
   
   //bypass:read-after-write 
J
jinyue110 已提交
324 325 326 327 328 329 330 331 332 333 334
//    for( b <- 0 until PredictWidth) {
//         when(update_bank === b.U && meta_write_valid && read_valid
//             && 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
//         {
//             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))
//             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))
//         }
//     }
335
}