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

#include "Behavioural/Generic/Victim/SelfTest/include/test.h"
#include "Common/include/Test.h"
#include "Behavioural/include/Allocation.h"

#define NB_ITERATION  10
#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::generic::victim::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

  Victim * _Victim = new Victim (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_ACCESS_VAL    ," in_ACCESS_VAL    ",Tcontrol_t,_param->_nb_access);
  ALLOC1_SC_SIGNAL(out_ACCESS_ACK    ,"out_ACCESS_ACK    ",Tcontrol_t,_param->_nb_access);
  ALLOC1_SC_SIGNAL( in_ACCESS_ADDRESS," in_ACCESS_ADDRESS",Taddress_t,_param->_nb_access);
  ALLOC1_SC_SIGNAL( in_ACCESS_HIT    ," in_ACCESS_HIT    ",Tcontrol_t,_param->_nb_access);
  ALLOC1_SC_SIGNAL( in_ACCESS_ENTITY ," in_ACCESS_ENTITY ",Tentity_t ,_param->_nb_access);
  ALLOC1_SC_SIGNAL(out_ACCESS_VICTIM ,"out_ACCESS_VICTIM ",Tentity_t ,_param->_nb_access);
  
  /********************************************************
   * Instanciation
   ********************************************************/
  
  msg(_("<%s> : Instanciation of _Victim.\n"),name.c_str());

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

  INSTANCE1_SC_SIGNAL(_Victim, in_ACCESS_VAL    ,_param->_nb_access);
  INSTANCE1_SC_SIGNAL(_Victim,out_ACCESS_ACK    ,_param->_nb_access);
  if (_param->_have_port_address)
  INSTANCE1_SC_SIGNAL(_Victim, in_ACCESS_ADDRESS,_param->_nb_access);
  INSTANCE1_SC_SIGNAL(_Victim, in_ACCESS_HIT    ,_param->_nb_access);
  INSTANCE1_SC_SIGNAL(_Victim, in_ACCESS_ENTITY ,_param->_nb_access);
  INSTANCE1_SC_SIGNAL(_Victim,out_ACCESS_VICTIM ,_param->_nb_access);

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

      SC_START(1);
    }

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

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

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

  delete _Victim;

  delete in_CLOCK;
  delete in_NRESET;

  delete []  in_ACCESS_VAL    ;
  delete [] out_ACCESS_ACK    ;
  delete []  in_ACCESS_ADDRESS;
  delete []  in_ACCESS_HIT    ;
  delete []  in_ACCESS_ENTITY ;
  delete [] out_ACCESS_VICTIM ;
#endif

#ifdef STATISTICS
  delete _parameters_statistics;
#endif
}
