/*
 * $Id$
 *
 * [ Description ]
 * 
 */

#include "Behavioural/Stage_1_Ifetch/Predictor/Meta_Predictor/Meta_Predictor_Glue/include/Meta_Predictor_Glue.h"

namespace morpheo                    {
namespace behavioural {
namespace stage_1_ifetch {
namespace predictor {
namespace meta_predictor {
namespace meta_predictor_glue {


#ifdef SYSTEMC
  Meta_Predictor_Glue::Meta_Predictor_Glue (sc_module_name name,
#else
  Meta_Predictor_Glue::Meta_Predictor_Glue (string name,
#endif
#ifdef STATISTICS
			      morpheo::behavioural::Parameters_Statistics             param_statistics,
#endif
			      morpheo::behavioural::stage_1_ifetch::predictor::meta_predictor::meta_predictor_glue::Parameters param ):
			      _name              (name)
			      ,_param            (param)
// #ifdef STATISTICS
// 			      ,_param_statistics (param_statistics)
// #endif
  {
    log_printf(FUNC,Meta_Predictor_Glue,"Meta_Predictor_Glue","Begin");

#ifdef STATISTICS
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","Allocation of statistics");

    // Allocation of statistics
    _stat = new Statistics (static_cast<string>(_name),
			    param_statistics          ,
			    param);
#endif

#ifdef VHDL_TESTBENCH
    // Creation of a testbench
    //  -> port
    //  -> clock's signals
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","Creation of a testbench");
    _vhdl_testbench = new Vhdl_Testbench (_name);
    vhdl_testbench_port           ();
    _vhdl_testbench->set_clock    ("in_CLOCK",false);
#endif

#ifdef VHDL
    // generate the vhdl
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","VHDL's generation");
    vhdl();
#endif

#ifdef SYSTEMC
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","Allocation");
    allocation ();

#if defined(STATISTICS) or defined(VHDL_TESTBENCH)
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - transition");

    SC_METHOD (transition);
    dont_initialize ();
    sensitive_pos << *(in_CLOCK);
#endif

    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - genMealy_predict_ack");
    SC_METHOD (genMealy_predict_ack);
    dont_initialize ();
    for (uint32_t i=0; i<_param._nb_prediction; i++)
      {
	sensitive << *(in_PREDICT_PREDICTOR_2_ACK [i]);
	
	if (_param._have_meta_predictor)
	  {
	    sensitive << *(in_PREDICT_PREDICTOR_0_ACK [i])
		      << *(in_PREDICT_PREDICTOR_1_ACK [i]);
	  }			           
      }

#ifdef SYSTEMCASS_SPECIFIC
    // List dependency information
    for (uint32_t i=0; i<_param._nb_prediction; i++)
      {
	(*(out_PREDICT_ACK [i])) (*(in_PREDICT_PREDICTOR_2_ACK [i]));
	
	if (_param._have_meta_predictor)
	  {
	(*(out_PREDICT_ACK [i])) (*(in_PREDICT_PREDICTOR_0_ACK [i]));
	(*(out_PREDICT_ACK [i])) (*(in_PREDICT_PREDICTOR_1_ACK [i]));
	  }			           
      }
#endif    

    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - genMealy_predict_history");
    SC_METHOD (genMealy_predict_history);
    dont_initialize ();
    for (uint32_t i=0; i<_param._nb_prediction; i++)
      {
	if (_param._predictor_2_have_bht)
	sensitive << *(in_PREDICT_PREDICTOR_2_BHT_HISTORY [i]);
	if (_param._predictor_2_have_pht)
	sensitive << *(in_PREDICT_PREDICTOR_2_PHT_HISTORY [i]);

	if (_param._have_meta_predictor)
	  {
	if (_param._predictor_1_have_bht)
	sensitive << *(in_PREDICT_PREDICTOR_1_BHT_HISTORY [i]);
	if (_param._predictor_1_have_pht)
	sensitive << *(in_PREDICT_PREDICTOR_1_PHT_HISTORY [i]);

	if (_param._predictor_0_have_bht)
	sensitive << *(in_PREDICT_PREDICTOR_0_BHT_HISTORY [i]);
	if (_param._predictor_0_have_pht)
	sensitive << *(in_PREDICT_PREDICTOR_0_PHT_HISTORY [i]);
	  }			           
      }

#ifdef SYSTEMCASS_SPECIFIC
    // List dependency information
    for (uint32_t i=0; i<_param._nb_prediction; i++)
      {
	if (_param._predictor_2_have_bht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_2_BHT_HISTORY [i]));
	if (_param._predictor_2_have_pht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_2_PHT_HISTORY [i]));

	if (_param._have_meta_predictor)
	  {
	if (_param._predictor_1_have_bht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_1_BHT_HISTORY [i]));
	if (_param._predictor_1_have_pht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_1_PHT_HISTORY [i]));

	if (_param._predictor_0_have_bht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_0_BHT_HISTORY [i]));
	if (_param._predictor_0_have_pht)
	(*(out_PREDICT_HISTORY    [i])) (*(in_PREDICT_PREDICTOR_0_PHT_HISTORY [i]));
	  }			           

	if (_param._predictor_2_have_bht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_2_BHT_HISTORY [i]));
	if (_param._predictor_2_have_pht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_2_PHT_HISTORY [i]));

	if (_param._have_meta_predictor)
	  {
	if (_param._predictor_1_have_bht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_1_BHT_HISTORY [i]));
	if (_param._predictor_1_have_pht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_1_PHT_HISTORY [i]));

	if (_param._predictor_0_have_bht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_0_BHT_HISTORY [i]));
	if (_param._predictor_0_have_pht)
	(*(out_PREDICT_DIRECTION  [i])) (*(in_PREDICT_PREDICTOR_0_PHT_HISTORY [i]));
	  }			           
      }
#endif    

    if (_param._have_meta_predictor)
      {
    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - genMealy_branch_complete_val");
    SC_METHOD (genMealy_branch_complete_val);
    dont_initialize ();
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	sensitive << *(in_BRANCH_COMPLETE_VAL       [i])
		  << *(in_BRANCH_COMPLETE_HISTORY   [i])
		  << *(in_BRANCH_COMPLETE_DIRECTION [i]);
      }

#ifdef SYSTEMCASS_SPECIFIC
    // List dependency information
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	(*(out_BRANCH_COMPLETE_PREDICTOR_2_VAL       [i])) (*(in_BRANCH_COMPLETE_VAL       [i]));
	(*(out_BRANCH_COMPLETE_PREDICTOR_2_VAL       [i])) (*(in_BRANCH_COMPLETE_HISTORY   [i]));
	(*(out_BRANCH_COMPLETE_PREDICTOR_2_VAL       [i])) (*(in_BRANCH_COMPLETE_DIRECTION [i]));

	(*(out_BRANCH_COMPLETE_PREDICTOR_2_DIRECTION [i])) (*(in_BRANCH_COMPLETE_VAL       [i]));
	(*(out_BRANCH_COMPLETE_PREDICTOR_2_DIRECTION [i])) (*(in_BRANCH_COMPLETE_HISTORY   [i]));
	(*(out_BRANCH_COMPLETE_PREDICTOR_2_DIRECTION [i])) (*(in_BRANCH_COMPLETE_DIRECTION [i]));
      }
#endif    
      }

    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - genMealy_branch_complete_ack");
    SC_METHOD (genMealy_branch_complete_ack);
    dont_initialize ();
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	sensitive << *(in_BRANCH_COMPLETE_PREDICTOR_2_ACK [i]);
	
	if (_param._have_meta_predictor)
	  {
	    sensitive << *(in_BRANCH_COMPLETE_PREDICTOR_0_ACK [i])
		      << *(in_BRANCH_COMPLETE_PREDICTOR_1_ACK [i]);
	  }			           
      }

#ifdef SYSTEMCASS_SPECIFIC
    // List dependency information
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	(*(out_BRANCH_COMPLETE_ACK [i])) (*(in_BRANCH_COMPLETE_PREDICTOR_2_ACK [i]));
	
	if (_param._have_meta_predictor)
	  {
	(*(out_BRANCH_COMPLETE_ACK [i])) (*(in_BRANCH_COMPLETE_PREDICTOR_0_ACK [i]));
	(*(out_BRANCH_COMPLETE_ACK [i])) (*(in_BRANCH_COMPLETE_PREDICTOR_1_ACK [i]));
	  }			           
      }
#endif    

    log_printf(INFO,Meta_Predictor_Glue,"Meta_Predictor_Glue","method - genMealy_branch_complete_history");
    SC_METHOD (genMealy_branch_complete_history);
    dont_initialize ();
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	sensitive << *(in_BRANCH_COMPLETE_HISTORY [i]);
      }

#ifdef SYSTEMCASS_SPECIFIC
    // List dependency information
    for (uint32_t i=0; i<_param._nb_branch_complete; i++)
      {
	if (_param._predictor_2_have_bht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_2_BHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
	if (_param._predictor_2_have_pht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_2_PHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
		
	if (_param._have_meta_predictor)
	  {
	    if (_param._predictor_1_have_bht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_1_BHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
	    if (_param._predictor_1_have_pht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_1_PHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
													   
	    if (_param._predictor_0_have_bht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_0_BHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
	    if (_param._predictor_0_have_pht)
	 (*(out_BRANCH_COMPLETE_PREDICTOR_0_PHT_HISTORY [i])) (*( in_BRANCH_COMPLETE_HISTORY    [i]));
	  }			           
      }
#endif    

#endif
    log_printf(FUNC,Meta_Predictor_Glue,"Meta_Predictor_Glue","End");
  };
  
  Meta_Predictor_Glue::~Meta_Predictor_Glue (void)
  {
    log_printf(FUNC,Meta_Predictor_Glue,"~Meta_Predictor_Glue","Begin");

#ifdef VHDL_TESTBENCH
    log_printf(INFO,Meta_Predictor_Glue,"~Meta_Predictor_Glue","Generate Testbench");
    // generate the test bench
    _vhdl_testbench->generate_file();
    delete _vhdl_testbench;
#endif

#ifdef STATISTICS
    log_printf(INFO,Meta_Predictor_Glue,"~Meta_Predictor_Glue","Generate Statistics");
    _stat->generate_file(statistics(0));
    
    delete _stat;
#endif

#ifdef SYSTEMC
    log_printf(INFO,Meta_Predictor_Glue,"~Meta_Predictor_Glue","Dealocation");
    deallocation ();
#endif

    log_printf(FUNC,Meta_Predictor_Glue,"~Meta_Predictor_Glue","End");
  };

}; // end namespace meta_predictor_glue
}; // end namespace meta_predictor
}; // end namespace predictor
}; // end namespace stage_1_ifetch
}; // end namespace behavioural
}; // end namespace morpheo              
