uBTB.scala 13.4 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 13 14
    val lowerBitsSize = 20

    val extended_stat = false
15 16 17 18 19
}

class MicroBTB extends BasePredictor
    with MicroBTBPatameter
{
20 21 22
    // val tagSize = VAddrBits - log2Ceil(PredictWidth) - 1
    val untaggedBits = lowerBitsSize + 1
    val tagSize = VAddrBits - untaggedBits
J
jinyue110 已提交
23 24

    class MicroBTBResp extends Resp
25
    {
L
Lingrui98 已提交
26 27
        val targets = Vec(PredictWidth, UInt(VAddrBits.W))
        val hits = Vec(PredictWidth, Bool())
28
        val takens = Vec(PredictWidth, Bool())
29
        val brMask = Vec(PredictWidth, Bool())
30
        val is_RVC = Vec(PredictWidth, Bool())
31 32
    }

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

J
jinyue110 已提交
41
    class MicroBTBIO extends DefaultBasePredictorIO
42 43
    {
        val out = Output(new MicroBTBResp)   //
J
jinyue110 已提交
44
        val uBTBBranchInfo = Output(new MicroBTBBranchInfo)
45 46
    }

47
    override val debug = true
48
    override val io = IO(new MicroBTBIO)
J
jinyue110 已提交
49
    io.uBTBBranchInfo <> out_ubtb_br_info
50

51
    def getTag(pc: UInt) = (pc >> untaggedBits).asUInt()
52
    def getBank(pc: UInt) = pc(log2Ceil(PredictWidth) ,1)
J
jinyue110 已提交
53

54 55 56 57 58 59 60 61 62 63 64
    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
    {
65
        val lower = UInt(lowerBitsSize.W)
66 67
    }

L
Lingrui98 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    // 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
    //     }
    // }

95 96 97 98 99 100
    class MetaOutput extends XSBundle {
        val is_Br = Bool()
        val is_RVC = Bool()
        val pred = UInt(2.W)
    }

L
Lingrui98 已提交
101
    class UBTBMetaBank(nWays: Int) extends XSModule {
L
Lingrui98 已提交
102 103 104 105
        val io = IO(new Bundle {
            val wen = Input(Bool())
            val wWay = Input(UInt(log2Up(nWays).W))
            val wdata = Input(new MicroBTBMeta)
106 107 108 109 110 111
            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 已提交
112
        })
113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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 已提交
127
        when (io.wen) {
128
            mem.write(io.wWay, io.wdata)
L
Lingrui98 已提交
129 130 131
        }
    }

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

148 149 150 151
    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 已提交
152

153 154
    val uBTBMeta = VecInit(metas.map(m => m.rdata))
    val uBTB     = VecInit(datas.map(d => d.rdata))
155

L
Lingrui98 已提交
156 157 158 159 160
    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 }

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

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

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

L
Lingrui98 已提交
190
    (0 until PredictWidth).map(b => datas(b).rWay := read_hit_ways((b.U + PredictWidth.U - read_req_basebank)(log2Up(PredictWidth)-1, 0)))
191 192 193

    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 已提交
194 195 196

    for(i <- 0 until PredictWidth){
        // do not need to decide whether to produce results\
197
        read_resp(i).valid := read_hit_vec(i) && io.inMask(i)
J
jinyue110 已提交
198
        read_resp(i).taken := read_resp(i).valid && uBTBMeta_resp(i).pred(1)
199
        read_resp(i).is_Br  := read_resp(i).valid && uBTBMeta_resp(i).is_Br
200
        read_resp(i).target := Cat(io.pc.bits(VAddrBits-1, lowerBitsSize+1), btb_resp(i).asUInt, 0.U(1.W))
J
jinyue110 已提交
201
        read_resp(i).is_RVC := read_resp(i).valid && uBTBMeta_resp(i).is_RVC
L
Lingrui98 已提交
202 203

        out_ubtb_br_info.hits(i) := read_hit_vec(i)
204 205 206
    }

    //TODO: way alloc algorithm
207 208 209 210
    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)
211
        val l = log2Ceil(nWays)
212
        val nChunks = (tags.getWidth + l - 1) / l
213
        val chunks = (0 until nChunks) map { i =>
214
            tags(min((i+1)*l, tags.getWidth)-1, i*l)
215
        }
216 217 218
        way := Mux(all_valid,chunks.reduce(_^_),PriorityEncoder(~valids))
        way
    }
219 220 221 222 223

    // 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))
224
        
225 226 227 228
    // }

    val alloc_ways = read_bank_inOrder.map{ b => 
        Mux(metas(b).allocatable_way.valid, metas(b).allocatable_way.bits, LFSR64()(log2Ceil(nWays)-1,0))}
229
    (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 已提交
230

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

    //uBTB update 
    //backend should send fetch pc to update
L
Lingrui98 已提交
244
    val u = io.update.bits.ui
J
jinyue110 已提交
245 246 247 248
    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 已提交
249 250 251
    val update_write_way = u.brInfo.ubtbWriteWay
    val update_hits = u.brInfo.ubtbHits
    val update_taken = u.taken
252 253

    val update_bank = getBank(update_br_pc)
J
jinyue110 已提交
254
    val update_base_bank = getBank(update_fetch_pc)
255
    val update_tag = getTag(update_br_pc)
J
jinyue110 已提交
256
    val update_target = Mux(u.pd.isBr, u.brTarget, u.target)
257
    val update_target_lower = update_target(lowerBitsSize, 1)
L
Lingrui98 已提交
258
    val update_is_BR_or_JAL = (u.pd.brType === BrType.branch) || (u.pd.brType === BrType.jal) 
259 260 261
  
  
    val jalFirstEncountered = !u.isMisPred && !u.brInfo.btbHitJal && (u.pd.brType === BrType.jal)
262 263
    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
264
    //write btb target when miss prediction
L
Lingrui98 已提交
265 266
    // when(entry_write_valid)
    // {
267
    //     uBTB(update_write_way)(update_bank).offset := update_target_offset
L
Lingrui98 已提交
268
    // }
L
Lingrui98 已提交
269
    for (b <- 0 until PredictWidth) {
L
Lingrui98 已提交
270 271
        datas(b).wen := do_reset || (entry_write_valid && b.U === update_bank)
        datas(b).wWay := Mux(do_reset, reset_way, update_write_way)
272
        datas(b).wdata := Mux(do_reset, 0.U.asTypeOf(new MicroBTBEntry), update_target_lower.asTypeOf(new MicroBTBEntry))
L
Lingrui98 已提交
273 274
    }

L
Lingrui98 已提交
275

276

J
jinyue110 已提交
277
    //write the uBTBMeta
278
    (0 until PredictWidth).map(i => metas(i).rWay := update_write_way)
L
Lingrui98 已提交
279 280 281 282 283 284 285
    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),
286
                                    satUpdate( metas(update_bank).rpred,2,update_taken)
L
Lingrui98 已提交
287
                                )
L
Lingrui98 已提交
288 289

    for (b <- 0 until PredictWidth) {
L
Lingrui98 已提交
290 291 292
        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 已提交
293
    }
Z
zhanglinjuan 已提交
294

295
    if (BPUDebug && debug) {
L
Lingrui98 已提交
296 297 298 299 300 301 302
        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))
        }

303 304
        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 | update_lower 0x%x\n "
                    ,update_br_pc,update_hits,update_write_way,update_bank,update_br_idx,update_tag,update_target_lower(lowerBitsSize-1,0))
L
Lingrui98 已提交
305 306 307 308 309 310
        XSDebug(meta_write_valid, "uBTB update: update_taken:%d | old_pred:%b | new_pred:%b\n",
            update_taken, metas(update_bank).rpred,
            Mux(!update_hits,
                    Mux(update_taken,3.U,0.U),
                    satUpdate( metas(update_bank).rpred,2,update_taken)
                ))
311

L
Lingrui98 已提交
312
    }
313 314 315 316 317

    if (extended_stat) {
        val high_identical = update_target(VAddrBits-1, lowerBitsSize) =/= update_fetch_pc(VAddrBits-1, lowerBitsSize)
        XSDebug(io.update.valid, "extended_stat: identical %d\n", high_identical)
    }
J
jinyue110 已提交
318 319
   
   //bypass:read-after-write 
J
jinyue110 已提交
320 321 322 323 324 325 326 327 328 329 330
//    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))
//         }
//     }
331
}