/*
 * $Id: Parameters.cpp 111 2009-02-27 18:37:40Z rosiere $
 *
 * [ Description ]
 * 
 */

#include "Behavioural/Core/Multi_Front_end/Front_end/Prediction_unit/Direction/Meta_Predictor/Two_Level_Branch_Predictor/include/Parameters.h"
#include "Common/include/BitManipulation.h"

namespace morpheo {
namespace behavioural {
namespace core {
namespace multi_front_end {
namespace front_end {
namespace prediction_unit {
namespace direction {
namespace meta_predictor {
namespace two_level_branch_predictor {


#undef  FUNCTION
#define FUNCTION "Two_Level_Branch_Predictor::Parameters"
  Parameters::Parameters (uint32_t nb_inst_predict       ,
                          uint32_t nb_inst_update        ,
                          uint32_t size_address          ,
                          bool     have_bht              ,
                          uint32_t bht_size_shifter      ,
                          uint32_t bht_nb_shifter        ,
                          bool     have_pht              ,
                          uint32_t pht_size_counter      ,
                          uint32_t pht_nb_counter        ,
                          uint32_t pht_size_address_share,
                          bool     update_on_prediction  ,
                          bool is_toplevel)
  {
    log_begin(Two_Level_Branch_Predictor,FUNCTION);

    _nb_inst_predict         = nb_inst_predict       ;
    _nb_inst_update          = nb_inst_update        ;
    _size_address            = size_address          ;
    _have_bht                = have_bht              ;
    _bht_size_shifter        = (have_bht)?(bht_size_shifter):0;
    _bht_nb_shifter          = (have_bht)?(bht_nb_shifter  ):0;
    _have_pht                = have_pht              ;
    _pht_size_counter        = (have_pht)?(pht_size_counter      ):0;
    _pht_nb_counter          = (have_pht)?(pht_nb_counter        ):0;
    _pht_size_address_share  = (have_bht and have_pht)?(pht_size_address_share):0;
    _update_on_prediction    = update_on_prediction  ;
                             
    _bht_size_address        = (_have_bht)?log2(_bht_nb_shifter):0;
    _pht_size_address        = (_have_pht)?log2(_pht_nb_counter):0;
                             
    test();                  

    _size_history            = _bht_size_shifter + _pht_size_counter;
                             
    _bht_history_mask        = gen_mask<Thistory_t>(_bht_size_shifter);
    _bht_history_rshift      = 0;
    _pht_history_mask        = gen_mask<Thistory_t>(_pht_size_counter);
    _pht_history_rshift      = _bht_size_shifter;
                             
    log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _size_history             : %d",_size_history  );
    log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _bht_history_mask         : 0x%x",_bht_history_mask  );
    log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _bht_history_rshift       : %d",_bht_history_rshift);
    log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_history_mask         : 0x%x",_pht_history_mask  );
    log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_history_rshift       : %d",_pht_history_rshift);

    if (_have_bht)
      {
        _bht_address_mask        = gen_mask<Taddress_t>(_bht_size_address);

        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _bht_address_mask         : 0x%x",_bht_address_mask  );
      }

    if (_have_pht)
      {
        _pht_counter_max         = (1<<_pht_size_counter)-1;
        
        _pht_nb_bank             = (_pht_nb_counter - (1<<_bht_size_shifter))+1;
        _pht_size_bank           = _pht_nb_counter / _pht_nb_bank;

        _pht_address_share_mask  = gen_mask<Taddress_t>(_pht_size_address_share);
        _pht_address_share_lshift= _bht_size_shifter-_pht_size_address_share;
        _pht_address_bank_mask   = gen_mask<Taddress_t>(log2(_pht_nb_bank));
        _pht_address_bank_rshift = _pht_size_address_share;

        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_nb_bank              : %d"  ,_pht_nb_bank   );
        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_size_bank            : %d"  ,_pht_size_bank );
        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_address_share_mask   : 0x%x",_pht_address_share_mask  );
        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_address_share_lshift : %d"  ,_pht_address_share_lshift);
        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_address_bank_mask    : 0x%x",_pht_address_bank_mask   );
        log_printf(TRACE,Two_Level_Branch_Predictor,FUNCTION,"  * _pht_address_bank_rshift  : %d"  ,_pht_address_bank_rshift );
      }

    if (is_toplevel)
      copy();

    log_end(Two_Level_Branch_Predictor,FUNCTION);
  };
  
// #undef  FUNCTION
// #define FUNCTION "Two_Level_Branch_Predictor::Parameters (copy)"
//   Parameters::Parameters (Parameters & param)
//   {
//     log_begin(Two_Level_Branch_Predictor,FUNCTION);
//     test();
//     log_end(Two_Level_Branch_Predictor,FUNCTION);
//   };

#undef  FUNCTION
#define FUNCTION "Two_Level_Branch_Predictor::~Parameters"
  Parameters::~Parameters (void) 
  {
    log_begin(Two_Level_Branch_Predictor,FUNCTION);
    log_end(Two_Level_Branch_Predictor,FUNCTION);
  };

#undef  FUNCTION
#define FUNCTION "Two_Level_Branch_Predictor::copy"
  void Parameters::copy (void) 
  {
    log_begin(Two_Level_Branch_Predictor,FUNCTION);
    log_end(Two_Level_Branch_Predictor,FUNCTION);
  };

}; // end namespace two_level_branch_predictor
}; // end namespace meta_predictor
}; // end namespace direction
}; // end namespace prediction_unit
}; // end namespace front_end
}; // end namespace multi_front_end
}; // end namespace core

}; // end namespace behavioural
}; // end namespace morpheo              
