/* **********************************************************************
 * *                                                                    *
 * *     Test Tag Line inputs to the RVP7                               *
 * *                                                                    *
 * **********************************************************************
 * File: utils/dsp/tags.C
 *
 *             COPYRIGHT (c) 1997, 1999, 2000, 2002, 2003 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 <stdlib.h>

#include "sigtypes.h"
#include "dsp.h"
#include "user_lib.h"
#include "dsp_lib.h"

/* The 32 lines must be grounded in increasing order, only one line at a
 * time...
 */
int main( int argc_a, char **argv_a )
{
  MESSAGE istatus;
  Dsp *pdsp;
  SINT4 iv, iw, it, ii, ij;
  struct gparm Gparm;

  /* ------------------------------------------------------------------------
   * Attempt to open the device:
   */
  pdsp = DspOpenForIo( &istatus );
  if (!pdsp) goto L900;

  istatus = DspResetFifo( pdsp ) ;
  if( istatus != SS_NORMAL ) goto L900 ;

  istatus = DspCheckByteOrder( pdsp ) ;
  if( istatus != SS_NORMAL ) goto L900 ;

  printf("Ground TAGs, beginning with TAG0.\n");

  /* Run the test
   */
  iv = -1;
  iw = -2;
  ii =  0;

  while (TRUE)
    {
      if( iv >- 0 ) exit(EXIT_SUCCESS);
      sig_microSleep(40000);

      istatus = dspr_gparm( pdsp, &Gparm, GPARM_LSTATUS_CLEAR );
      if (istatus!=SS_NORMAL) goto L900;

      ij = Gparm.itagb;
      it = Gparm.itaga;
      it = ( it & 0x0000ffff ) | ( ij << 16 );

      if( it == iw )
	{
	  printf("Bit%3d Okay...\007\n", ii );
	  iv = iw;
	  iw = iw+iw+1;
	  ii = ii+1;
	}
      else if( (it!=-1) && ( it!=iv) )
	{
	  printf("Error expecting bit%3d.   Read:%08X\n", ii, it );
	}
    }

  /* Errors go here: */
L900:
  sig_signal( istatus );
  exit(EXIT_FAILURE);
}
