/* **************************************************
 * *                                                *
 * *  DSP Info Library, Data mask related routines  *
 * *                                                *
 * **************************************************
 * File: libs/dsp/dspi_mask.c
 *     
 *   COPYRIGHT (c) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2006 BY
 *             VAISALA INC., 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 <string.h>

#define DSP_INFO_DRIVER 1
#include "sigtypes.h"
#include "dsp_lib.h"

/* =======================================================
 * Test to see if a specified bit is set in the data mask.
 */
UINT1 lDspMaskTest 
( volatile const struct dsp_data_mask *pDataMask_a,
  UINT1 iDataType_a )
{
  if (iDataType_a<32 ) return BTEST(pDataMask_a->iWord0,iDataType_a);
  if (iDataType_a<64 ) return BTEST(pDataMask_a->iWord1,iDataType_a-32);
  if (iDataType_a<96 ) return BTEST(pDataMask_a->iWord2,iDataType_a-64);
  if (iDataType_a<128) return BTEST(pDataMask_a->iWord3,iDataType_a-96);
  if (iDataType_a<160) return BTEST(pDataMask_a->iWord4,iDataType_a-128);
  return FALSE;
}

/* =======================================================
 * Set one data type in the mask.
 */
void DspMaskSet 
( struct dsp_data_mask *pDataMask_a,
  UINT1 iDataType_a )
{
  UINT4 iMask;
  if (iDataType_a<32 )
    {
      iMask = 1<<iDataType_a;
      pDataMask_a->iWord0 |= iMask;
      return;
    }
  if (iDataType_a<64 )
    {
      iMask = 1<<(iDataType_a-32);
      pDataMask_a->iWord1 |= iMask;
      return;
    }
  if (iDataType_a<96 )
    {
      iMask = 1<<(iDataType_a-64);
      pDataMask_a->iWord2 |= iMask;
      return;
    }
  if (iDataType_a<128 )
    {
      iMask = 1<<(iDataType_a-96);
      pDataMask_a->iWord3 |= iMask;
      return;
    }
  if (iDataType_a<160 )
    {
      iMask = 1<<(iDataType_a-128);
      pDataMask_a->iWord4 |= iMask;
      return;
    }
  return;
}

/* =======================================================
 * Clear one data type in the mask;
 */
void DspMaskClear 
( struct dsp_data_mask *pDataMask_a,
  UINT1 iDataType_a )
{
  UINT4 iMask;
  if (iDataType_a<32 )
    {
      iMask = 1<<iDataType_a;
      pDataMask_a->iWord0 &= ~iMask;
      return;
    }
  if (iDataType_a<64 )
    {
      iMask = 1<<(iDataType_a-32);
      pDataMask_a->iWord1 &= ~iMask;
      return;
    }
  if (iDataType_a<96 )
    {
      iMask = 1<<(iDataType_a-64);
      pDataMask_a->iWord2 &= ~iMask;
      return;
    }
  if (iDataType_a<128 )
    {
      iMask = 1<<(iDataType_a-96);
      pDataMask_a->iWord3 &= ~iMask;
      return;
    }
  if (iDataType_a<160 )
    {
      iMask = 1<<(iDataType_a-128);
      pDataMask_a->iWord4 &= ~iMask;
      return;
    }
}

/* =======================================================
 * And a complemented data mask into main
 */
void DspMaskAndNot
( volatile struct dsp_data_mask *pMainMask_a,
  const struct dsp_data_mask *pAndMask_a )
{
  pMainMask_a->iWord0 &= ~pAndMask_a->iWord0;
  pMainMask_a->iWord1 &= ~pAndMask_a->iWord1;
  pMainMask_a->iWord2 &= ~pAndMask_a->iWord2;
  pMainMask_a->iWord3 &= ~pAndMask_a->iWord3;
  pMainMask_a->iWord4 &= ~pAndMask_a->iWord4;
}

/* =======================================================
 * And a data mask into main
 */
void DspMaskAnd
( volatile struct dsp_data_mask *pMainMask_a,
  const struct dsp_data_mask *pAndMask_a )
{
  pMainMask_a->iWord0 &= pAndMask_a->iWord0;
  pMainMask_a->iWord1 &= pAndMask_a->iWord1;
  pMainMask_a->iWord2 &= pAndMask_a->iWord2;
  pMainMask_a->iWord3 &= pAndMask_a->iWord3;
  pMainMask_a->iWord4 &= pAndMask_a->iWord4;
}

/* =======================================================
 * Or a data mask into main
 */
void DspMaskOr
( struct dsp_data_mask *pMainMask_a,
  const struct dsp_data_mask *pOrMask_a )
{
  pMainMask_a->iWord0 |= pOrMask_a->iWord0;
  pMainMask_a->iWord1 |= pOrMask_a->iWord1;
  pMainMask_a->iWord2 |= pOrMask_a->iWord2;
  pMainMask_a->iWord3 |= pOrMask_a->iWord3;
  pMainMask_a->iWord4 |= pOrMask_a->iWord4;
}

/* =======================================================
 * Test to see if there is no bit is set in the data mask.
 */
UINT1 lDspMaskEmpty 
( const struct dsp_data_mask *pDataMask_a )
{
  UINT1 lResult;
  lResult = ( ( pDataMask_a->iWord0 == 0 ) &&
	      ( pDataMask_a->iWord1 == 0 ) &&
	      ( pDataMask_a->iWord2 == 0 ) &&
	      ( pDataMask_a->iWord3 == 0 ) &&
	      ( pDataMask_a->iWord4 == 0 ) );
  return lResult;
}

/* =======================================================
 * Test to see if 2 dsp masks are equal.  The Extended header
 * type is ignored.
 */
UINT1 lDspMaskEqual 
( volatile const struct dsp_data_mask *pDataMask1_a,
  const struct dsp_data_mask *pDataMask2_a )
{
  UINT1 lResult;
  lResult = ( ( pDataMask1_a->iWord0 == pDataMask2_a->iWord0) &&
	      ( pDataMask1_a->iWord1 == pDataMask2_a->iWord1) &&
	      ( pDataMask1_a->iWord2 == pDataMask2_a->iWord2) &&
	      ( pDataMask1_a->iWord3 == pDataMask2_a->iWord3) &&
	      ( pDataMask1_a->iWord4 == pDataMask2_a->iWord4) );
  return lResult;
}

/* =======================================================
 * Return a mask showing all the 1-byte data available from a DSP.
 */
void Dsp1ByteMask 
( struct dsp_data_mask *pDataMask_a )
{
  memset( pDataMask_a, 0, sizeof(*pDataMask_a) );

  DspMaskSet( pDataMask_a, DB_DBT);
  DspMaskSet( pDataMask_a, DB_DBZ);
  DspMaskSet( pDataMask_a, DB_VEL);
  DspMaskSet( pDataMask_a, DB_WIDTH);
  DspMaskSet( pDataMask_a, DB_ZDR);
  DspMaskSet( pDataMask_a, DB_KDP);
  DspMaskSet( pDataMask_a, DB_PHIDP);
  DspMaskSet( pDataMask_a, DB_RHOHV);
  DspMaskSet( pDataMask_a, DB_SQI);
  DspMaskSet( pDataMask_a, DB_LDRH);
  DspMaskSet( pDataMask_a, DB_RHOH);
  DspMaskSet( pDataMask_a, DB_PHIH);
  DspMaskSet( pDataMask_a, DB_LDRV);
  DspMaskSet( pDataMask_a, DB_RHOV);
  DspMaskSet( pDataMask_a, DB_PHIV);
  DspMaskSet( pDataMask_a, DB_FLAGS);
  DspMaskSet( pDataMask_a, DB_HCLASS);
}

/* =======================================================
 * Return a mask showing all the 2-byte data available from a DSP.
 */
void Dsp2ByteMask 
( struct dsp_data_mask *pDataMask_a )
{
  memset( pDataMask_a, 0, sizeof(*pDataMask_a) );

  DspMaskSet( pDataMask_a, DB_DBT2);
  DspMaskSet( pDataMask_a, DB_DBZ2);
  DspMaskSet( pDataMask_a, DB_VEL2);
  DspMaskSet( pDataMask_a, DB_WIDTH2);
  DspMaskSet( pDataMask_a, DB_ZDR2);
  DspMaskSet( pDataMask_a, DB_KDP2);
  DspMaskSet( pDataMask_a, DB_PHIDP2);
  DspMaskSet( pDataMask_a, DB_RHOHV2);
  DspMaskSet( pDataMask_a, DB_SQI2);
  DspMaskSet( pDataMask_a, DB_LDRH2);
  DspMaskSet( pDataMask_a, DB_RHOH2);
  DspMaskSet( pDataMask_a, DB_PHIH2);
  DspMaskSet( pDataMask_a, DB_LDRV2);
  DspMaskSet( pDataMask_a, DB_RHOV2);
  DspMaskSet( pDataMask_a, DB_PHIV2);
  DspMaskSet( pDataMask_a, DB_FLAGS2);
  DspMaskSet( pDataMask_a, DB_HCLASS2);
}
