From 791c5ae11d9c77694873786299ed73d1e5426427 Mon Sep 17 00:00:00 2001 From: Minseong Jang Date: Wed, 4 Sep 2024 17:17:50 +0900 Subject: [PATCH] Define helper struct for branch prediction --- .../src/cpu/branch_predictor/mod.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/hazardflow-designs/src/cpu/branch_predictor/mod.rs b/hazardflow-designs/src/cpu/branch_predictor/mod.rs index 19d1796..b02b2f6 100644 --- a/hazardflow-designs/src/cpu/branch_predictor/mod.rs +++ b/hazardflow-designs/src/cpu/branch_predictor/mod.rs @@ -8,11 +8,43 @@ pub use bht::*; pub use btb::*; pub use pre_decode::*; +use super::MemRespWithAddr; +use crate::std::*; + /// Number of BHT entries. pub const BHT_ENTRIES: usize = 128; /// Number of BTB entries. pub const BTB_ENTRIES: usize = 32; +/// Branch predictor with BHT and BTB. +#[derive(Debug, Default, Clone, Copy)] +pub struct Bp { + /// BHT. + pub bht: Bht, + + /// BTB. + pub btb: Btb, +} + +impl Bp { + /// Returns the branch prediction result. + pub fn predict(self, imem_resp: MemRespWithAddr) -> BpResult { + BpResult { + pre_decode: pre_decode(imem_resp.data.into_u()), + bht: self.bht.predict(imem_resp.addr), + btb: self.btb.predict(imem_resp.addr).unwrap_or(imem_resp.addr + 4), + } + } + + /// Updates the branch predictor. + pub fn update(self, bp_update: BpUpdate) -> Self { + match bp_update { + BpUpdate::Bht { pc, taken } => Self { bht: self.bht.update(pc, taken), btb: self.btb }, + BpUpdate::Btb { pc, target } => Self { bht: self.bht, btb: self.btb.update(pc, target) }, + } + } +} + /// Branch prediction results. #[derive(Debug, Clone, Copy)] pub struct BpResult {