/* **********************************************************************
 * *                                                                    *
 * *   BITE SIMULATOR PROGRAM INTERFACE ROUTINES                        *
 * *                                                                    *
 * **********************************************************************
 * File: libs/antenna/bitsim_driver.c
 *
 *                  COPYRIGHT (c) 1991, 1999, 2002  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */
#include <stdio.h>

#include "sigtypes.h"
#include "setup.h"
#include "antenna_lib.h"
#include "antenna_int.h"
#include "antsim_global.h"

/* ------------------------------
 * Complement a specifed bit in the BITE simulator which will be transmitted in
 * future transmissions.  If the unit argument is out of range, then all
 * data bits are reset to zero, and the header and footer are set correctly.
 * Note that this allows bits to be set that will invalidate the transmission.
 */
void  bitesim_fault( UINT1 iunit_a, UINT1 ibyte_a, UINT1 ibit_a )
{
  if (iunit_a<BITE_MAX_UNITS)
    {
      if (ibyte_a<BITE_MAX_LENGTH)
	{
	  if (ibit_a<8)
	    {
	      volatile struct bitesim_unit_info *pBite=&(pantsim_c->binfo.Units[iunit_a]);

	      pBite->sbite_reception[ibyte_a] =	
		IBINV( pBite->sbite_reception[ibyte_a], ibit_a );
	      pBite->lSendUpdate = TRUE;
	    }
	}
    }
  else
    {
      UINT1 iunit_count, iunit, ibyte_count, ibyte;
      SINT4 iField;

      iunit_count = (UINT1)pantsim_c->bitex.iunit_cnt;
      if (iunit_count>BITE_MAX_UNITS) iunit_count = BITE_MAX_UNITS;

      for (iunit=0; iunit<iunit_count; iunit++)
	{
	  volatile struct bitesim_unit_info *pBite=&(pantsim_c->binfo.Units[iunit]);
	  ibyte_count = (UINT1)pantsim_c->bitex.units[iunit].iunit_length;
	  if (ibyte_count>BITE_MAX_LENGTH) ibyte_count = BITE_MAX_LENGTH;
	  if (pantsim_c->bitex.units[iunit].lQBite)
	    pBite->sbite_reception[0] = ANT_SYNC_QBITE;
	  else
	    pBite->sbite_reception[0] = ANT_SYNC_BITE;
	  pBite->sbite_reception[1]   = pantsim_c->bitex.units[iunit].iunit_id ;
	  for (ibyte=2; ibyte<(ibyte_count-1); ibyte++ )
	    {
	      pBite->sbite_reception[ibyte] = 0;
	    }
	  pBite->sbite_reception[ibyte_count-1] = 0xFF;
	  pBite->lSendUpdate = TRUE;
	  
	}
      for (iField=0; iField<BITE_NUM_FIELD_DEFS; iField++)
	pantsim_c->binfo.iQBiteValues[iField] = 0;

    }
}

/* ------------------------------
 * Get the activity counts for the number of bite units specified.
 * Returns the number of valid ones.
 */
SINT4 bitesim_get_activity( SINT4 in_count_a[], SINT4 out_count_a[], 
			SINT4 bite_count_a )
{
  SINT4 iUnit, iMaxUnits;


  iMaxUnits = MIN( bite_count_a, BITE_MAX_UNITS );
  for (iUnit=0; iUnit<iMaxUnits; iUnit++)
    {
      in_count_a[iUnit]  = pantsim_c->binfo.Units[iUnit].iin_cnt;
      out_count_a[iUnit] = pantsim_c->binfo.Units[iUnit].iout_cnt;
    }

  if (iMaxUnits > (SINT4)(UINT4)pantsim_c->bitex.iunit_cnt)
    iMaxUnits = (SINT4)(UINT4)pantsim_c->bitex.iunit_cnt;

  return iMaxUnits;
}
