/* **********************************
 * *                                *  
 * *           DSP Driver           *
 * *                                *  
 * **********************************  
 * File: libs/dsp/dsp_start.c
 *
 * COPYRIGHT (c) 1988, 1989, 1990, 1992, 1994, 1997, 1998, 1999, 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 <string.h>

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

/* ========================================================================
 * This subroutine initializes the DSP, and does the read/write test.
 *
 * Entry parameters:
 *     dspsetup_a - DSP setup structure
 *     idsp_a     - DSP number, pass by value
 */
Dsp *DspStart
( MESSAGE *pStatus_a )
{
  SINT4 ii, iactcnt ; Dsp *pdsp ;

  /* Initialize the driver package
   */
  pdsp = DspOpenForIo( pStatus_a ) ;
  if( NULL == pdsp ) return( pdsp ) ;
     
  /* Verify that the byte ordering seems to be okay.
   */
  *pStatus_a = DspResetFifo( pdsp ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;
  *pStatus_a = DspCheckByteOrder( pdsp ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  /* Run I/O test to further check the interface
   */
  {
    UINT2 ipattern[16], jpattern[16] ;
    for( ii=0 ; ii < 16 ; ii++ ) ipattern[ii] = ~( 1 << ii ) ;

    *pStatus_a = dspw_pat( pdsp, ipattern ) ;
    if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

    *pStatus_a = dsp_read( pdsp, jpattern, 16, &iactcnt ) ;
    if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

    for ( ii=0 ; ii < 16 ; ii++ ) {
      if( ipattern[ii] != jpattern[ii] ) {
	printf( "I/O data mismatch: Bit:%2d  Wr:%4.4X  Rd:%4.4X  Err:%4.4X\n",
		ii, ipattern[ii], jpattern[ii], ipattern[ii]^jpattern[ii] ) ;
	*pStatus_a = USER_ERROR_SIG ;
      }
    }
    if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;
  }

  /* Check whether there are any code upgrades to install.  Do not
   * complain if no new code is available, or if the versions already
   * match.
   */
  {
    char spath[PATHNAME_SIZE], *sptr = getenv( "IRIS_CONFIG" ) ;
    if( NULL == sptr ) {
      sig_signal_va( USER_NO_ENV, 1, "IRIS_CONFIG") ;
      *pStatus_a = USER_NO_ENV ;

    } else {
      strncpy( spath, sptr, sizeof(spath)-20 ) ;
      spath[ sizeof(spath)-20 ] = '\0' ;
      strcat ( spath, "dsp.rom" ) ;

      *pStatus_a = dspw_romload( pdsp, spath, TRUE, TRUE ) ;

      if( DSP_NEWCODE == SIGBASE(*pStatus_a) ) {
	sig_signal( *pStatus_a ) ; *pStatus_a = SS_NORMAL ;
      }
    }
    if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;
  }  

  /* Load up the PRF constraints into the DSP
   */
  *pStatus_a = DspWritePwInfo( pdsp ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  /* Set the DSP to not be in angle sync mode: (added by jmh 27 May
   * 1992)
   */
  *pStatus_a = dspw_syn_mode( pdsp, ANGSYN_NONE, 0 ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  /* Set the DSP to not be in extended header mode.  (Added by jmh 28
   * April 1998)
   */
  *pStatus_a = dspw_header_cfg( pdsp, DSP_HDR_TAGS ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  /* Verify that the range masks are consistently defined between IRIS
   * and the DSP.
   */
  *pStatus_a = DspCheckMaskSpacing( pdsp ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  /* Verify that the max PRFs are consistently defined between IRIS
   * and the DSP. (Added by jmh 6 Jan 2003)
   */
  *pStatus_a = DspCheckPrfLimits( pdsp ) ;
  if( *pStatus_a != SS_NORMAL ) return( pdsp ) ;

  return( pdsp ) ;
}
