/*
 *  Copyright 2011 OpenSourceStewardshipFoundation.org
 *  Licensed under GNU General Public License version 2
 *
 * Author: seanhalle@yahoo.com
 *
 */
#include "HWSim__PingPong__HWDef/HWSim__PingPong__HWDef.h"
 
/* is an expr resolves to an actual commPath struct instance
 */
#define setCommPathValuesTo( commPath, fromElIdx, outPort, toElIdx, inPort,\
                             timeFnPtr, dataPtr)\
do{\
   commPath->idxOfFromElem     = fromElIdx; \
   commPath->idxOfFromOutPort  = outPort; \
   commPath->idxOfToElem       = toElIdx; \
   commPath->idxOfToInPort     = inPort; \
   commPath->commTimeCalcFn     = timeFnPtr;\
   commPath->archSpecCommPathData  = dataPtr; \
 }while(0); //macro magic for namespace



HWSimActivityType* createPingPongActivityType (); 

HWSimElem* createAPingPongElem (HWSimNetlist *netlist);

/*This file constructs the netlist for the Hello World circuit, which is
 * used during design and implementation of the HWSim language
 *
 *It has two elements, each with one input port and one output port, and
 * a single activity-type.
 *
 *The elements are cross-coupled, so output port of one connects to input
 * port of the other.  The input port has a single trigger, which fires
 * the one activity-type.
 *
 *The activity does nothing, except send a NULL message on the output port.
 *The activity-sim-time and communication-sim-time are both constants.
 *
 *Note that elements are generic.  They are specialized by declaring
 * inports and outports, and by registering triggers that fire particular
 * activity-types.
 */
HWSimNetlist *createPingPongNetlist()
 { HWSimNetlist   *netlist;
   HWSimCommPath  **commPaths;
   HWSimElem **elems;
   int32 numElems;
   int32 numCommPaths;
   
   netlist = malloc( sizeof(HWSimNetlist) );
  	numElems= 2; 
   elems = malloc( numElems * sizeof(HWSimElem *) );
   netlist->numElems = numElems;
   netlist->elems    = elems;
   netlist->numActivityTypes = 1;
   netlist->activityTypes = malloc(netlist->numActivityTypes*sizeof(HWSimActivityType*));
	// Stefan: tocheck valgrind
   netlist->activityTypes[PING_PONG_ACTIVITY] = createPingPongActivityType();
	
   elems[0] = createAPingPongElem( netlist ); //use info from netlist
   elems[1] = createAPingPongElem( netlist ); 
      //on one of the elems, make reset trigger an action   
	//Stefan: FIXME
   elems[1]->inPorts[-1].triggeredActivityType =
              netlist->activityTypes[PING_PONG_ACTIVITY]; //Connect elems together
			  
	/*OutPorts and InPorts may have many commPaths attached.
    *  but an inPort only     
	 * has one kind of activity that all incoming communications trigger.  That
	 * activity can be zero time and then switch on the type of message then
	 * end with a continuation, where the continuation activity is chosen by the    
	 * switch. 
	 *So, a commPath only connects an out port to an in port
	 *The format is: sending TL-index, out-port, dest TL-index, in-port
	 */
   numCommPaths          = 2;
   commPaths             = malloc( numCommPaths * sizeof(HWSimCommPath *) );
   netlist->numCommPaths= numCommPaths;
   netlist->commPaths= commPaths;
      //TL 0, out-port 0 to TL 1, in-port 0
	commPaths[0]= malloc(sizeof(HWSimCommPath));
	setCommPathValuesTo(commPaths[0],0,0,1,0, commPath_TimeSpanCalc, NULL); 
      //TL 1, out-port 0 to TL 0, in-port 0
	commPaths[1]= malloc(sizeof(HWSimCommPath));
   setCommPathValuesTo(commPaths[1], 1,0,0,0, commPath_TimeSpanCalc, NULL);
   
   //TODO: decide how do in-out bidirectional commPaths -- thinking make it two
   // separate commPaths with guard between in-port and out-port

	return netlist;
 }

void
freePingPongNetlist (HWSimNetlist *netlist) 
{
	int i;
	for (i= 0; i<netlist->numCommPaths; i++) {
		free(netlist->commPaths[i]);
	}	
	free(netlist->commPaths);
	for (i= 0; i<netlist->numElems; i++) {
		free(&netlist->elems[i]->inPorts[-1]);
		free(netlist->elems[i]);
	}

	free(netlist->activityTypes);
	free(netlist->elems);
	free(netlist);
}
 
HWSimElem *
createAPingPongElem( HWSimNetlist *netlist )
 { HWSimElem *TL;
   TL = malloc( sizeof(HWSimElem) );
   TL->numInPorts  = 1;
   TL->numOutPorts = 1;
   TL->inPorts = HWSim_ext__make_inPortsArray( TL->numInPorts );
   TL->inPorts[-1].triggeredActivityType = IDLE_SPAN; //reset port
   TL->inPorts[0].triggeredActivityType  = netlist->activityTypes[PING_PONG_ACTIVITY];
	return TL;
 }
 
HWSimActivityType *
createPingPongActivityType( )
 { HWSimActivityType *pingPongActivityType;
   pingPongActivityType = malloc( sizeof(HWSimActivityType) );
   
   pingPongActivityType->hasBehavior   = true;
   pingPongActivityType->hasTiming     = true;
   pingPongActivityType->timingIsFixed = true;
   pingPongActivityType->fixedTime     = 10;
   pingPongActivityType->behaviorFn    = &pingPongElem_PingActivity_behavior;
   return pingPongActivityType;
 }
 
