@@ -72,18 +72,26 @@ class BHTResp(implicit p: Parameters) extends BtbBundle()(p) {
7272// - each counter corresponds with the address of the fetch packet ("fetch pc").
7373// - updated when a branch resolves (and BTB was a hit for that branch).
7474// The updating branch must provide its "fetch pc".
75- class BHT (params : BHTParams )(implicit val p : Parameters ) extends HasCoreParameters {
75+ class BHT (params : BHTParams , historyLengthConfig : UInt , historyBitsConfig : UInt )(implicit val p : Parameters ) extends HasCoreParameters {
7676 def index (addr : UInt , history : UInt ) = {
77- def hashHistory (hist : UInt ) = if (params.historyLength == params.historyBits) hist else {
78- val k = math.sqrt(3 )/ 2
79- val i = BigDecimal (k * math.pow(2 , params.historyLength)).toBigInt
80- (i.U * hist)(params.historyLength- 1 , params.historyLength- params.historyBits)
77+ def hashHistory (hist : UInt ) = {
78+ Mux (historyBitsConfig >= historyLengthConfig,
79+ hist,
80+ {
81+ val k = math.sqrt(3 )/ 2
82+ val i = (BigDecimal (k * math.pow(2 , params.historyLength)).toBigInt.U ) >> (params.historyLength.U - historyLengthConfig)
83+ val product = i * hist
84+ (product >> (historyLengthConfig - historyBitsConfig)) & ((1 .U << historyLengthConfig) - 1 .U )
85+ }
86+ )
8187 }
8288 def hashAddr (addr : UInt ) = {
8389 val hi = addr >> log2Ceil(fetchBytes)
8490 hi(log2Ceil(params.nEntries)- 1 , 0 ) ^ (hi >> log2Ceil(params.nEntries))(1 , 0 )
8591 }
86- hashAddr(addr) ^ (hashHistory(history) << (log2Up(params.nEntries) - params.historyBits))
92+ val slicedInputHistory = history >> (params.historyLength.U - historyLengthConfig)
93+ val hashValue = hashHistory(slicedInputHistory)
94+ hashAddr(addr) ^ (hashValue << (log2Up(params.nEntries).U - historyBitsConfig))
8795 }
8896 def get (addr : UInt ): BHTResp = {
8997 val res = Wire (new BHTResp )
@@ -114,6 +122,8 @@ class BHT(params: BHTParams)(implicit val p: Parameters) extends HasCoreParamete
114122 private val table = Mem (params.nEntries, UInt (params.counterLength.W ))
115123 val history = RegInit (0 .U (params.historyLength.W ))
116124
125+ val slicedHistory = history >> (params.historyLength.U - historyLengthConfig)
126+
117127 private val reset_waddr = RegInit (0 .U ((params.nEntries.log2+ 1 ).W ))
118128 private val resetting = ! reset_waddr(params.nEntries.log2)
119129 private val wen = WireInit (resetting)
@@ -192,6 +202,8 @@ class BTB(implicit p: Parameters) extends BtbModule {
192202 val ras_update = Flipped (Valid (new RASUpdate ))
193203 val ras_head = Valid (UInt (vaddrBits.W ))
194204 val flush = Input (Bool ())
205+ val historyLengthConfig = Input (UInt (4 .W ))
206+ val historyBitsConfig = Input (UInt (4 .W ))
195207 })
196208
197209 val idxs = Reg (Vec (entries, UInt ((matchBits - log2Up(coreInstBytes)).W )))
@@ -299,7 +311,7 @@ class BTB(implicit p: Parameters) extends BtbModule {
299311 }
300312
301313 if (btbParams.bhtParams.nonEmpty) {
302- val bht = new BHT (btbParams.bhtParams.get)
314+ val bht = new BHT (btbParams.bhtParams.get, io.historyLengthConfig, io.historyBitsConfig )
303315 val isBranch = (idxHit & cfiType.map(_ === CFIType .branch).asUInt).orR
304316 val res = bht.get(io.req.bits.addr)
305317 when (io.bht_advance.valid) {
0 commit comments