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

#include "Behavioural/Core/Multi_Front_end/Front_end/Prediction_unit/Direction/Meta_Predictor/Two_Level_Branch_Predictor/SelfTest/include/test.h"
#include "Behavioural/include/Allocation.h"

void test (string name,
	   morpheo::behavioural::core::multi_front_end::front_end::prediction_unit::direction::meta_predictor::two_level_branch_predictor::Parameters * _param)
{
  msg(_("<%s> : Simulation SystemC.\n"),name.c_str());

  if (setlocale (LC_ALL, "") == NULL)
    msg(_("setlocale ko.\n"));

#ifdef STATISTICS
  morpheo::behavioural::Parameters_Statistics * _parameters_statistics = new morpheo::behavioural::Parameters_Statistics (5,CYCLE_MAX);
#endif

  Tusage_t _usage = USE_ALL;

//   _usage = usage_unset(_usage,USE_SYSTEMC              );
//   _usage = usage_unset(_usage,USE_VHDL                 );
//   _usage = usage_unset(_usage,USE_VHDL_TESTBENCH       );
//   _usage = usage_unset(_usage,USE_VHDL_TESTBENCH_ASSERT);
//   _usage = usage_unset(_usage,USE_POSITION             );
//   _usage = usage_unset(_usage,USE_STATISTICS           );
//   _usage = usage_unset(_usage,USE_INFORMATION          );

  Two_Level_Branch_Predictor * _Two_Level_Branch_Predictor = new Two_Level_Branch_Predictor 
    (name.c_str(),
#ifdef STATISTICS
     _parameters_statistics,
#endif
     _param,
     _usage);
  
#ifdef SYSTEMC
  if (usage_is_set(_usage,USE_SYSTEMC))
    {
  /*********************************************************************
   * Déclarations des signaux
   *********************************************************************/
  string rename;

  sc_clock              *  in_CLOCK  = new sc_clock ("clock", 1.0, 0.5);	 
  sc_signal<Tcontrol_t> *  in_NRESET = new sc_signal<Tcontrol_t> ("NRESET");

  ALLOC1_SC_SIGNAL( in_PREDICT_VAL          ," in_PREDICT_VAL          ",Tcontrol_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL(out_PREDICT_ACK          ,"out_PREDICT_ACK          ",Tcontrol_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL( in_PREDICT_ADDRESS      ," in_PREDICT_ADDRESS      ",Taddress_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL(out_PREDICT_DIRECTION    ,"out_PREDICT_DIRECTION    ",Tcontrol_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL(out_PREDICT_HISTORY      ,"out_PREDICT_HISTORY      ",Thistory_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL( in_PREDICT_DIRECTION_VAL," in_PREDICT_DIRECTION_VAL",Tcontrol_t,_param->_nb_inst_predict);
  ALLOC1_SC_SIGNAL( in_PREDICT_DIRECTION    ," in_PREDICT_DIRECTION    ",Tcontrol_t,_param->_nb_inst_predict);
  
  ALLOC1_SC_SIGNAL( in_UPDATE_VAL           ," in_UPDATE_VAL           ",Tcontrol_t,_param->_nb_inst_update);
  ALLOC1_SC_SIGNAL(out_UPDATE_ACK           ,"out_UPDATE_ACK           ",Tcontrol_t,_param->_nb_inst_update);
  ALLOC1_SC_SIGNAL( in_UPDATE_ADDRESS       ," in_UPDATE_ADDRESS       ",Taddress_t,_param->_nb_inst_update);
  ALLOC1_SC_SIGNAL( in_UPDATE_HISTORY       ," in_UPDATE_HISTORY       ",Thistory_t,_param->_nb_inst_update);
  ALLOC1_SC_SIGNAL( in_UPDATE_DIRECTION     ," in_UPDATE_DIRECTION     ",Tcontrol_t,_param->_nb_inst_update);
  ALLOC1_SC_SIGNAL( in_UPDATE_MISS          ," in_UPDATE_MISS          ",Tcontrol_t,_param->_nb_inst_update);
  
  /********************************************************
   * Instanciation
   ********************************************************/
  
  msg(_("<%s> : Instanciation of _Two_Level_Branch_Predictor.\n"),name.c_str());

  (*(_Two_Level_Branch_Predictor->in_CLOCK))        (*(in_CLOCK));
  (*(_Two_Level_Branch_Predictor->in_NRESET))       (*(in_NRESET));

  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_PREDICT_VAL          ,_param->_nb_inst_predict);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor,out_PREDICT_ACK          ,_param->_nb_inst_predict);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_PREDICT_ADDRESS      ,_param->_nb_inst_predict);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor,out_PREDICT_DIRECTION    ,_param->_nb_inst_predict);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor,out_PREDICT_HISTORY      ,_param->_nb_inst_predict);
  if (_param->_update_on_prediction)
    {
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_PREDICT_DIRECTION_VAL,_param->_nb_inst_predict);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_PREDICT_DIRECTION    ,_param->_nb_inst_predict);
    }

  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_UPDATE_VAL           ,_param->_nb_inst_update);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor,out_UPDATE_ACK           ,_param->_nb_inst_update);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_UPDATE_ADDRESS       ,_param->_nb_inst_update);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_UPDATE_HISTORY       ,_param->_nb_inst_update);
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_UPDATE_DIRECTION     ,_param->_nb_inst_update);
  if (_param->_update_on_prediction)
  INSTANCE1_SC_SIGNAL(_Two_Level_Branch_Predictor, in_UPDATE_MISS          ,_param->_nb_inst_update);

  msg(_("<%s> : Start Simulation ............\n"),name.c_str());
    
  Time * _time = new Time();

  /********************************************************
   * Simulation - Begin
   ********************************************************/

  // Initialisation

  const uint32_t seed = 0;
//const uint32_t seed = static_cast<uint32_t>(time(NULL));

  srand(seed);

  const  int32_t percent_transaction_predict = 75;
  const  int32_t percent_transaction_update  = 75;

  SC_START(0);
  LABEL("Initialisation");

  for (uint32_t i=0; i<_param->_nb_inst_predict; ++i)
    in_PREDICT_VAL [i]->write(0);
  for (uint32_t i=0; i<_param->_nb_inst_update; ++i)
    in_UPDATE_VAL [i]->write(0);

  LABEL("Reset");
  in_NRESET->write(0);
  SC_START(5);
  in_NRESET->write(1);  

  LABEL("Loop of Test");
  
  Thistory_t bht;
  Thistory_t pht [_param->_pht_size_bank];
  
  for (uint32_t iteration=0; iteration<NB_ITERATION; iteration ++)
    {
      LABEL("Iteration %d",iteration);

      uint32_t bht_num_reg  = (_param->_have_bht)?(rand()%_param->_bht_nb_shifter):0;
      uint32_t pht_num_bank = (_param->_have_pht)?(rand()%_param->_pht_nb_bank   ):0;

      LABEL("  * bht_num_reg  : %d",bht_num_reg );
      LABEL("  * pht_num_bank : %d",pht_num_bank);

      {
        LABEL("Init BHT and PHT");

        bht = 0;
        for (uint32_t i=0; i<_param->_pht_size_bank; ++i)
          pht [i] = 0;
        
        uint32_t port = rand()%_param->_nb_inst_update;

        if (_param->_have_bht)
          {        
            bool find = false;
            while (not find)
              {
                bool val = ((rand()%2)<percent_transaction_update);
                in_UPDATE_VAL       [port]->write(val);
                in_UPDATE_DIRECTION [port]->write(0);
                
                if (_param->_update_on_prediction)
                  in_UPDATE_MISS      [port]->write(1); // miss
                in_UPDATE_HISTORY   [port]->write(0);
                in_UPDATE_ADDRESS   [port]->write(bht_num_reg);
                
                SC_START(0);
                
                if (val and out_UPDATE_ACK [port]->read())
                  {
                    LABEL("UPDATE[%d] - Transaction accepted",port);
                    find = true;
                  }
                
                SC_START(1);
              }
            in_UPDATE_VAL [port]->write(0);
          }

        if (_param->_have_pht)
          {        
            for (uint32_t i=0; i<_param->_pht_size_bank; ++i)
              {
                bool find = false;
                
                while (not find)
                  {
                    bool val = ((rand()%2)<percent_transaction_update);
                    in_UPDATE_VAL       [port]->write(val);
                    in_UPDATE_DIRECTION [port]->write(0);
                    
                    if (_param->_update_on_prediction)
                      in_UPDATE_MISS      [port]->write(1); // miss
                    in_UPDATE_HISTORY   [port]->write(i<<_param->_bht_history_rshift);
                    in_UPDATE_ADDRESS   [port]->write(pht_num_bank<<_param->_pht_address_bank_rshift);
                    
                    SC_START(0);
                    
                    if (val and out_UPDATE_ACK [port]->read())
                      {
                        LABEL("UPDATE[%d] - Transaction accepted",port);
                        find = true;
                      }
                    
                    SC_START(1);
                  }
                in_UPDATE_VAL [port]->write(0);
              }
          }
      }

//       if (1)
//         {
//           LABEL("Saturation all PHT");
          
//         }

      SC_START(1);
    }

  /********************************************************
   * Simulation - End
   ********************************************************/

  TEST_OK ("End of Simulation");
  delete _time;

  msg(_("<%s> : ............ Stop Simulation\n"),name.c_str());

  delete in_CLOCK;
  delete in_NRESET;

  DELETE1_SC_SIGNAL( in_PREDICT_VAL          ,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL(out_PREDICT_ACK          ,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL( in_PREDICT_ADDRESS      ,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL(out_PREDICT_DIRECTION    ,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL(out_PREDICT_HISTORY      ,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL( in_PREDICT_DIRECTION_VAL,_param->_nb_inst_predict);
  DELETE1_SC_SIGNAL( in_PREDICT_DIRECTION    ,_param->_nb_inst_predict);

  DELETE1_SC_SIGNAL( in_UPDATE_VAL           ,_param->_nb_inst_update);
  DELETE1_SC_SIGNAL(out_UPDATE_ACK           ,_param->_nb_inst_update);
  DELETE1_SC_SIGNAL( in_UPDATE_ADDRESS       ,_param->_nb_inst_update);
  DELETE1_SC_SIGNAL( in_UPDATE_HISTORY       ,_param->_nb_inst_update);
  DELETE1_SC_SIGNAL( in_UPDATE_DIRECTION     ,_param->_nb_inst_update);
  DELETE1_SC_SIGNAL( in_UPDATE_MISS          ,_param->_nb_inst_update);
    }
#endif

  delete _Two_Level_Branch_Predictor;
#ifdef STATISTICS
  delete _parameters_statistics;
#endif
}
