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

#include "Behavioural/Core/Multi_OOO_Engine/OOO_Engine/Rename_unit/Register_translation_unit/Register_translation_unit_Glue/SelfTest/include/test.h"
#include "Common/include/Test.h"
#include "Behavioural/include/Allocation.h"

#define NB_ITERATION  256
#define CYCLE_MAX     (128*NB_ITERATION)

#define LABEL(str...)							\
  {									\
    msg (_("{%d} "),static_cast<uint32_t>(sc_simulation_time()));	\
    msg (str);								\
    msg (_("\n"));							\
  } while(0)

#define SC_START(cycle_offset)                                                       \
  do									             \
    {									             \
      /*cout << "SC_START (begin)" << endl;*/				             \
									             \
      uint32_t cycle_current = static_cast<uint32_t>(sc_simulation_time());          \
      if (cycle_offset != 0)						             \
	{								             \
	  cout << "##########[ cycle "<< cycle_current+cycle_offset << " ]" << endl; \
	}								             \
									             \
      if (cycle_current > CYCLE_MAX)					             \
	{								             \
	  TEST_KO("Maximal cycles Reached");				             \
	}								             \
									             \
      sc_start(cycle_offset);						             \
									             \
      /*cout << "SC_START (end  )" << endl;*/				             \
    } while(0)

void test (string name,
	   morpheo::behavioural::core::multi_ooo_engine::ooo_engine::rename_unit::register_translation_unit::register_translation_unit_glue::Parameters * _param)
{
  msg(_("<%s> : Simulation SystemC.\n"),name.c_str());

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

  Register_translation_unit_Glue * _Register_translation_unit_Glue = new Register_translation_unit_Glue (name.c_str(),
#ifdef STATISTICS
					     _parameters_statistics,
#endif
					     _param);
  
#ifdef 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_INSERT_RENAME_VAL    ," in_INSERT_RENAME_VAL    ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL(out_INSERT_RENAME_ACK    ,"out_INSERT_RENAME_ACK    ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL(out_INSERT_INSERT_VAL    ,"out_INSERT_INSERT_VAL    ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_INSERT_INSERT_ACK    ," in_INSERT_INSERT_ACK    ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL(out_INSERT_RAT_INSERT_VAL,"out_INSERT_RAT_INSERT_VAL",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_INSERT_RAT_RENAME_ACK," in_INSERT_RAT_RENAME_ACK",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_INSERT_RAT_INSERT_ACK," in_INSERT_RAT_INSERT_ACK",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL(out_INSERT_FREE_LIST_VAL ,"out_INSERT_FREE_LIST_VAL ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_INSERT_FREE_LIST_ACK ," in_INSERT_FREE_LIST_ACK ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL(out_INSERT_STAT_LIST_VAL ,"out_INSERT_STAT_LIST_VAL ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_INSERT_STAT_LIST_ACK ," in_INSERT_STAT_LIST_ACK ",Tcontrol_t,_param->_nb_inst_insert);
  ALLOC1_SC_SIGNAL( in_RETIRE_VAL           ," in_RETIRE_VAL           ",Tcontrol_t,_param->_nb_inst_retire);
  ALLOC1_SC_SIGNAL(out_RETIRE_ACK           ,"out_RETIRE_ACK           ",Tcontrol_t,_param->_nb_inst_retire);
  ALLOC1_SC_SIGNAL(out_RETIRE_RAT_VAL       ,"out_RETIRE_RAT_VAL       ",Tcontrol_t,_param->_nb_inst_retire);
  ALLOC1_SC_SIGNAL( in_RETIRE_RAT_ACK       ," in_RETIRE_RAT_ACK       ",Tcontrol_t,_param->_nb_inst_retire);
  ALLOC1_SC_SIGNAL(out_RETIRE_STAT_LIST_VAL ,"out_RETIRE_STAT_LIST_VAL ",Tcontrol_t,_param->_nb_inst_retire);
  ALLOC1_SC_SIGNAL( in_RETIRE_STAT_LIST_ACK ," in_RETIRE_STAT_LIST_ACK ",Tcontrol_t,_param->_nb_inst_retire);
  
  /********************************************************
   * Instanciation
   ********************************************************/
  
  msg(_("<%s> : Instanciation of _Register_translation_unit_Glue.\n"),name.c_str());

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

  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_RENAME_VAL    ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_INSERT_RENAME_ACK    ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_INSERT_INSERT_VAL    ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_INSERT_ACK    ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_INSERT_RAT_INSERT_VAL,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_RAT_RENAME_ACK,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_RAT_INSERT_ACK,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_INSERT_FREE_LIST_VAL ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_FREE_LIST_ACK ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_INSERT_STAT_LIST_VAL ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_INSERT_STAT_LIST_ACK ,_param->_nb_inst_insert);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_RETIRE_VAL           ,_param->_nb_inst_retire);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_RETIRE_ACK           ,_param->_nb_inst_retire);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_RETIRE_RAT_VAL       ,_param->_nb_inst_retire);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_RETIRE_RAT_ACK       ,_param->_nb_inst_retire);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue,out_RETIRE_STAT_LIST_VAL ,_param->_nb_inst_retire);
  INSTANCE1_SC_SIGNAL(_Register_translation_unit_Glue, in_RETIRE_STAT_LIST_ACK ,_param->_nb_inst_retire);

  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);

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

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

  LABEL("Loop of Test");

  for (uint32_t iteration=0; iteration<NB_ITERATION; iteration ++)
    {
      LABEL("Iteration %d",iteration);

      for (uint32_t i=0; i<_param->_nb_inst_insert;i++)
	{
	  in_INSERT_RENAME_VAL    [i]->write(rand()%2);
	  in_INSERT_INSERT_ACK    [i]->write(rand()%2);
	  in_INSERT_RAT_RENAME_ACK[i]->write(rand()%2);
	  in_INSERT_RAT_INSERT_ACK[i]->write(rand()%2);
	  in_INSERT_FREE_LIST_ACK [i]->write(rand()%2);
	  in_INSERT_STAT_LIST_ACK [i]->write(rand()%2);
	}
      
      for (uint32_t i=0; i<_param->_nb_inst_retire;i++)
	{
	  in_RETIRE_VAL           [i]->write(rand()%2);
	  in_RETIRE_RAT_ACK       [i]->write(rand()%2);
	  in_RETIRE_STAT_LIST_ACK [i]->write(rand()%2);
	}

      SC_START(0);

      for (uint32_t i=0; i<_param->_nb_inst_insert;i++)
	{
	  TEST(Tcontrol_t,out_INSERT_RENAME_ACK     [i]->read(),(in_INSERT_INSERT_ACK     [i]->read() and
								 in_INSERT_RAT_RENAME_ACK [i]->read() and
								 in_INSERT_RAT_INSERT_ACK [i]->read() and
								 in_INSERT_FREE_LIST_ACK  [i]->read() and
								 in_INSERT_STAT_LIST_ACK  [i]->read() ));
	  TEST(Tcontrol_t,out_INSERT_INSERT_VAL     [i]->read(),(in_INSERT_RENAME_VAL     [i]->read() and
								 in_INSERT_RAT_RENAME_ACK [i]->read() and
								 in_INSERT_RAT_INSERT_ACK [i]->read() and
								 in_INSERT_FREE_LIST_ACK  [i]->read() and
								 in_INSERT_STAT_LIST_ACK  [i]->read() ));
	  TEST(Tcontrol_t,out_INSERT_RAT_INSERT_VAL [i]->read(),(in_INSERT_RENAME_VAL     [i]->read() and
								 in_INSERT_INSERT_ACK     [i]->read() and
								 in_INSERT_RAT_RENAME_ACK [i]->read() and
								 in_INSERT_FREE_LIST_ACK  [i]->read() and
								 in_INSERT_STAT_LIST_ACK  [i]->read() ));
	  TEST(Tcontrol_t,out_INSERT_FREE_LIST_VAL  [i]->read(),(in_INSERT_RENAME_VAL     [i]->read() and
								 in_INSERT_INSERT_ACK     [i]->read() and
								 in_INSERT_RAT_RENAME_ACK [i]->read() and
								 in_INSERT_RAT_INSERT_ACK [i]->read() and
								 in_INSERT_STAT_LIST_ACK  [i]->read() ));
	  TEST(Tcontrol_t,out_INSERT_STAT_LIST_VAL  [i]->read(),(in_INSERT_RENAME_VAL     [i]->read() and
								 in_INSERT_INSERT_ACK     [i]->read() and
								 in_INSERT_RAT_RENAME_ACK [i]->read() and
								 in_INSERT_RAT_INSERT_ACK [i]->read() and
								 in_INSERT_FREE_LIST_ACK  [i]->read() ));
	}
      
      for (uint32_t i=0; i<_param->_nb_inst_retire;i++)
	{
	  TEST(Tcontrol_t,out_RETIRE_ACK           [i]->read(),(in_RETIRE_RAT_ACK       [i]->read() and
								in_RETIRE_STAT_LIST_ACK [i]->read()));
	  TEST(Tcontrol_t,out_RETIRE_RAT_VAL       [i]->read(),(in_RETIRE_VAL           [i]->read() and
								in_RETIRE_STAT_LIST_ACK [i]->read()));
	  TEST(Tcontrol_t,out_RETIRE_STAT_LIST_VAL [i]->read(),(in_RETIRE_VAL           [i]->read() and
								in_RETIRE_RAT_ACK       [i]->read()));
	}

      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;

  delete []  in_INSERT_RENAME_VAL    ;
  delete [] out_INSERT_RENAME_ACK    ;
  delete [] out_INSERT_INSERT_VAL    ;
  delete []  in_INSERT_INSERT_ACK    ;
  delete [] out_INSERT_RAT_INSERT_VAL;
  delete []  in_INSERT_RAT_RENAME_ACK;
  delete []  in_INSERT_RAT_INSERT_ACK;
  delete [] out_INSERT_FREE_LIST_VAL ;
  delete []  in_INSERT_FREE_LIST_ACK ;
  delete [] out_INSERT_STAT_LIST_VAL ;
  delete []  in_INSERT_STAT_LIST_ACK ;
  delete []  in_RETIRE_VAL           ;
  delete [] out_RETIRE_ACK           ;
  delete [] out_RETIRE_RAT_VAL       ;
  delete []  in_RETIRE_RAT_ACK       ;
  delete [] out_RETIRE_STAT_LIST_VAL ;
  delete []  in_RETIRE_STAT_LIST_ACK ;

#endif

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