/* **********************************************************************
 * *                                                                    *
 * *       Structure and Parameter Definitions for SIGMET DSPs          *
 * *                                                                    *
 * **********************************************************************
 * File: include/dsp.h
 *
 *               COPYRIGHT (c) 2001, 2003, 2004, 2005 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.
 */
#ifndef SIGMET_DSP_H
#define SIGMET_DSP_H 1

/* Timeout interval for all DSP physical I/O
 */
#define DSP_TIMEOUT_MS  (6000)	/* Miliseconds */

/* Some constants internal to the processor
 */
#define RVP7_LOG_SLOPE (1966)	/* Slope loaded (0.0300 dB/LSB) */
#define RVP7_ROMRVP  0x00000400	/* Location of RVP.OUT in the RVP7 ROM */

#define RVP7_MAX_XARGS   256	/* Maximum length of XARGS list for */
#define RVP8_MAX_XARGS  1200	/*   each processor family. */

/* PRF limits (in Hertz) for each type of processor.
 */
#define DSP_RVP6_MINPRF   160.0
#define DSP_RVP6_MAXPRF  3000.0

#define DSP_RVP7_MINPRF    50.0
#define DSP_RVP7_MAXPRF 20000.0	/* (In support of DPT1 mode) */

#define DSP_RVP8_MINPRF    50.0
#define DSP_RVP8_MAXPRF 20000.0

/* Parameters related to range bin selection and averaging
 */
#define DSP_MAXAVG       16	/* Largest permitted range averaging factor */

/* What kind of Dual/Multiple PRF scheme we are using.  Note that the
 * number of custom trigger sequences here should match the parameter
 * CUSTOMTRIGS.
 */
#define PRF_FIXED   (0)		/* Fixed trigger rate */
#define PRF_2_3     (1)		/* 2:3 PRF Ratio */
#define PRF_3_4     (2)		/* 3:4 PRF Ratio */
#define PRF_4_5     (3)		/* 4:5 PRF Ratio */
#define PRF_SEQ0    (4)		/* Custom sequence 0 */
#define PRF_SEQ1    (5)		/* Custom sequence 1 */
#define PRF_SEQ2    (6)		/* Custom sequence 2 */
#define PRF_SEQ3    (7)		/* Custom sequence 3 */

/* What kind of polarization transmission is being used.  These
 * bits are input to the PROC command, and antenna library where
 * relevent.
 */
#define POL_HORIZ_FIX    (0)	/* Horizontal fixed */
#define POL_VERT_FIX     (1)	/* Vertical fixed */
#define POL_ALTERNATING  (2)	/* Alternating between H and V */
#define POL_SIMULTANEOUS (3)	/* Simultaneous on both H and V */
#define POL_UNCHANGED    (7)    /* Do not change the polarization (in RCP02) */

/* Options for transmitted phase sequence.
 */
#define PHSEQ_FIXED   (0)	/* Unmodulated output */
#define PHSEQ_RANDOM  (1)	/* Random sequence */
#define PHSEQ_CUSTOM  (2)	/* Custom user defined sequence */
#define PHSEQ_SZ8_64  (3)	/* Sachi/Zrnic sequence */

/* Available output formats when the signal processor is
 * collecting Time Series / Power Spectrum data.
 */
#define TSOUT_TS8     (0)	/*  8-bit Time Series */
#define TSOUT_TS16    (1)	/* 16-bit Time Series */
#define TSOUT_SPEC    (2)	/* Power Spectrum */
#define TSOUT_HISNR   (3)	/* Packed TS16 w/higher SNR */

/* Possible choices of spectrum to view.  These values may be
 * combined only with the TSOUT_SPEC format choice.
 */
#define SPEC_MASK     (0xF0)
#define SPEC_RAW1     (0x00)	/* Raw first trip */
#define SPEC_WHITE1   (0x10)	/* Whitened first trip */
#define SPEC_CLEAN1   (0x20)	/* Cleaned first trip */
#define SPEC_FINAL1   (0x30)	/* Final first trip */
#define SPEC_RAW2     (0x40)	/* Raw second trip */
#define SPEC_WHITE2   (0x50)	/* Whitened second trip */
#define SPEC_CLEAN2   (0x60)	/* Cleaned second trip */
#define SPEC_FINAL2   (0x70)	/* Final second trip */
#define SPEC_RAWRES1  (0x80)	/* Raw residual first trip */
#define SPEC_RAWRES2  (0x90)	/* Raw residual second trip */

#define SPEC_BIT( OPT ) (1 << (((OPT) & SPEC_MASK ) >> 4))
#define SPEC_DEFAULT_BITS \
  ( SPEC_BIT( SPEC_RAW1   ) | SPEC_BIT( SPEC_WHITE1 ) | \
    SPEC_BIT( SPEC_CLEAN1 ) | SPEC_BIT( SPEC_FINAL1 ) | \
    SPEC_BIT( SPEC_RAW2   ) | SPEC_BIT( SPEC_WHITE2 ) | \
    SPEC_BIT( SPEC_CLEAN2 ) | SPEC_BIT( SPEC_FINAL2 ) )

/* Scope plotting definitions.
 */
#define MAXPLOTS 16		/* Max # of plotted lines (can't exceed 16) */
#define PLOTLEN 500		/* Length of data plots */

/* ==================================================
 * Operational Parameter Structure.  The first twenty words are loaded
 * directly by the SOPRM user command; remaining words are loaded via
 * XARGS.
 */
struct opprm {
  UINT2 isamp ;			/*  1: Sample size */
  UINT2 iflags ;		/*  2: Processing/threshold flags */
  SINT2 iz_slope ;		/*  3: LOG conversion Slope */
  SINT2 izns_thr ;		/*  4: LOG noise threshold */
  SINT2 iccr_thr ;		/*  5: Clutter correction threshold */
  UINT2 isqi_thr ;		/*  6: SQI threshold */
  SINT2 isig_thr ;		/*  7: Signal Power threshold */
  SINT2 iz_calib ;		/*  8: Calibration Reflectivity */
  UINT2 flg_agc  ;		/*  9: Flags / #pulses integrated for AGC */
  UINT2 filter   ;		/* 10: Filter delay & control */
  UINT2 dbt_tcf ;		/* 11: Uncorrected dBZ Threshold Control Flag */
  UINT2 dbz_tcf ;		/* 12: */
  UINT2 vel_tcf  ;		/* 13: */
  UINT2 wid_tcf  ;		/* 14: */
  BIN2  iAzOffset ;		/* 15: Additive offset for measured AZ angles */
  BIN2  iElOffset ;		/* 16: Additive offset for measured EL angles */
  UINT2 gas_atten ;		/* 17: intervening gas attenuation */
  UINT2 zdr_tcf ;		/* 18: Zdr threshold control flags */
  SINT2 zdr_offset ;		/* 19: Fixed offset to correct in Zdr (db*16) */
  UINT2 wavelength ;		/* 20: Radar wavelength in CM * 1000 */
  /* ------- Beginning of XARGS ------- */
  SINT2 ldr_offset ;		/*  1: Fixed offset to correct in LDR (dB*100) */
  UINT2 ibin_smooth ;		/*  2: Range smoothing (0:None, 1:pairs, etc) */
  UINT2 iPtCltDetect ;		/*  3: Point clutter detection parameters */
  UINT2 iPtCltCensor ;		/*  4: Point clutter censoring parameters */
  UINT2 iMismatchTSIgnore ;	/*  5: Bit mask of mismatch conditions to ignore */
  UINT2 iFlagsTS ;		/*  6: Flag bits related to timeseries data stream */
  UINT2 iCltSigPacks[2] ;	/* 7/8 Residual clutter noise margin packed info */
} ;
#define SOPRM_FIXED_ARGS 20	/* Number of fixed args */
#define SOPRM_TOTAL_ARGS 28	/* Total number of args defined */

/* Bits for Operational Parameter Flag word
 */
#define OPF_RNV      0x0001	/* Range normalization */
#define OPF_DSR      0x0002	/* Doppler speckle remover */
#define OPF_LSR      0x0004	/* LOG speckle remover */
#define OPF_END      0x0008	/* ENDRAY pulse at end of ray */
#define OPF_BEG      0x0010	/* ENDRAY pulse at beginning of ray */
#define OPF_2DPASS   0x0020	/* Final 2D speckle/unfold pass */
#define OPF_AGC      0x0040	/* Use AGC rather than STC */
#define OPF_R2       0x0080	/* Compute two-lag autocorrelations */
#define OPF_AVGCTH   0x0100	/* Range averaging clutter suppression */
#define OPF_16B      0x0200	/* Output individual params as 16-bits */
#define OPF_ANYSPEC  0x0400	/* Request that spectra size match sample size */
#define OPF_NHD      0x0800	/* Disable ray header in PROC command */
#define OPF_POL      0x3000	/* Two bits of polarization control */

#define POL_FROM_OPF(OPF) (((OPF) >> 12)  &  3)
#define OPF_FROM_POL(POL) (((POL)  &  3) << 12)

/* Bits for Operational Parameter Filter word
 */
#define OPFLT_RESET    0x0100	/* Reset filter state before waiting */
#define OPFLT_WIN      0x0E00	/* Window code in bits 9,10,11 */
#define OPFLT_CTRIGOK  0x1000	/* Allow custom triggers in normal algorithms */
#define OPFLT_UNFDIFF  0x2000	/* Unfold velocity using simple difference */

#define WIN_RECT          0	/* Window codes used within IRIS, as well */
#define WIN_HAMMING       1	/*   as within the signal processors. */
#define WIN_BLACKMAN      2
#define WIN_BLKMANEX      3
#define WIN_VONHANN       4
#define WIN_ADAPTIVE      5
#define NWINTYPES           6	/* Total number of window types */

#define WIN_FROM_OPFLT(OPFLT) (((OPFLT) >> 9)  & 7)
#define OPFLT_FROM_WIN(WIN  ) (((WIN  )  & 7) << 9)

/* Major mode of the signal processor (RVP6 and greater).  Values
 * for bit field within the opprm Flag/AGC word.
 */
#define PMODE_PPP     (0x0)	/* Pulse Pair Doppler */
#define PMODE_FFT     (0x1)	/* FFT Doppler */
#define PMODE_RPH     (0x2)	/* Random Phase Doppler */
#define PMODE_KNMI    (0x3)	/* KNMI custom LOG clutter filter */
#define PMODE_DPT1    (0x4)	/* Dual-PRT Type-1 (for low duty cycle) */
#define PMODE_DPT2    (0x5)	/* Dual-PRT Type-2 (for velocity unfolding) */
#define PMODE_BATCH   (0x6)	/* Legacy WSR88D batched trigger/unfolding */

#define PMODE_USER1   (0xC)	/* User-defined modes */
#define PMODE_USER2   (0xD)
#define PMODE_USER3   (0xE)
#define PMODE_USER4   (0xF)
#define PMODE_MAX_COUNT (16)     /* Maximum possible number of processing modes */

#define PMODE_FROM_OPF(OPF) (((OPF) & 0x0F00) >> 8)
#define PMODE_INTO_OPF(PMODE,OPF) ( ((OPF) & 0xF0FF) | (((PMODE) & 0xF) << 8) )

/* Threshold control flag bit definitions, and mask words having
 * that selected half of the bits set on.  These are used to
 * generate masks required by the DSPWTHRESH subroutine.
 */
#define TCFBIT_LOG 1
#define TCFBIT_CSR 2
#define TCFBIT_SQI 4
#define TCFBIT_WSP 8

#define TCF_LOG 0xAAAA
#define TCF_CSR 0xCCCC
#define TCF_SQI 0xF0F0
#define TCF_WSP 0xFF00

/* Bits for Operational Parameter iFlagsTS word (timeseries flags)
 */
#define OPTS_ZCAL       0x0001	/* Obtain calibration info from TS */
#define OPTS_NOISE      0x0002	/* Obtain noise levels from TS */
#define OPTS_NOSATLOG   0x0004	/* LOG threshold unaffected by saturated clutter */

/* Packed information for residual clutter LOG noise margin adjustment
 */
#define CLTSIG_DEFAULT  0x0000	/* Revert to RVP8 default values */
#define CLTSIG_LOADNEW  0x8000	/* Load new Beginning dB and Slope */
#define CLTSIG_PACK( BEGDB, SLOPE ) ( CLTSIG_LOADNEW |			\
    ( NINT(   1.0 * MAX( 0.0, MIN( 120.0, (BEGDB) ) ) ) << 8 ) |	\
    ( NINT( 100.0 * MAX( 0.0, MIN(   2.5, (SLOPE) ) ) )      ) )
#define CLTSIG_BEGDB( PACK ) ( ((FLT4)(((PACK) >> 8) & 0x7F)) /   1.0 )
#define CLTSIG_SLOPE( PACK ) ( ((FLT4)( (PACK)       & 0xFF)) / 100.0 )

/* ==================================================
 * Structure holding output data in response to the GPARM command
 */
#define DSP_GPARM_SIZE (128)

struct gparm {
  UINT2 irev_ser ;		/* 01: Rev/Serial # */
  UINT2 ibin_out_num ;		/* 02: # range bins output by processor */
  UINT2 iprt_mes ;		/* 03: Current trigger period */
  UINT2 itaga ;			/* 04: Tag bits 00 - 15 */
  UINT2 itagb ;			/* 05: Tag bits 16 - 31 */
  UINT2 log_nse ;		/* 06: Log Channel Noise */
  SINT2 i_nse_ ;		/* 07: I Channel Noise */
  SINT2 q_nse_ ;		/* 08: Q Channel Noise */
  UINT2 istat_l ;		/* 09: Latched status */
  UINT2 istat_i ;		/* 10: Immediate status */
  UINT2 idiag_a ;		/* 11: Diagnostic Results A */
  UINT2 idiag_b ;		/* 12: Diagnostic Results B */
  UINT2 isamp ;			/* 13: # pulses / ray */
  UINT2 itrg_cnt_a ;		/* 14: Low 16 bits of trigger count */
  UINT2 itrg_cnt_b ;		/* 15: High 8 bits of trigger count */
  UINT2 iaqbins ;		/* 16: # properly acquired bins */
  UINT2 iprbins ;		/* 17: # properly processed bins */
  UINT2 istat_i2 ;		/* 18: Immediate Status (second word) */
  UINT2 inse_rng ;		/* 19: Noise range in Km */
  UINT2 inse_prt ;		/* 20: Noise period */
  UINT2 ipwmin_0 ;		/* 21: Pulse width 0 min trigger period */
  UINT2 ipwmin_1 ;		/* 22: Pulse width 1 min trigger period */
  UINT2 ipwmin_2 ;		/* 23: Pulse width 2 min trigger period */
  UINT2 ipwmin_3 ;		/* 24: Pulse width 3 min trigger period */
  UINT2 ipw_bits ;		/* 25: Pulse width bit patterns */
  UINT2 ipw_now ;		/* 26: Current pulsewidth choice */
  UINT2 iprt_gen ;		/* 27: Current trigger generator period */
  UINT2 iprt_des ;		/* 28: Desired trigger generator period */
  UINT2 iprt_start ;		/* 29: Measured PRT at start of last ray */
  UINT2 iprt_end ;		/* 30: Measured PRT at end of last ray */
  UINT2 iflags ;		/* 31: Processing/threshold flags */
  SINT2 iz_slope ;		/* 32: LOG conversion Slope */
  SINT2 izns_thr ;		/* 33: LOG noise threshold */
  SINT2 iccr_thr ;		/* 34: Clutter correction threshold */
  UINT2 isqi_thr ;		/* 35: SQI threshold */
  SINT2 isig_thr ;		/* 36: Signal Power threshold */
  SINT2 iz_calib ;		/* 37: Calibration Reflectivity */
  UINT2 iqi_now ;		/* 38: (Q,I) current A/D sample */
  UINT2 iz_now ;		/* 39: LOG current A/D sample */
  UINT2 ibin_avg ;		/* 40: Range averaging choice (0:None, 1:Pairs, etc) */

  UINT2 idiag_c ;		/* 41: Reserved for future diagnostic bits */
  UINT2 idiag_d ;		/* 42: Reserved for future diagnostic bits */

  UINT2 iproc_hdr0 ;		/* 43: Bits 0-15 defining header of PROC rays */

  UINT2 isq_lo ;		/* 44: Noise sample average value of I squared, */
  SINT2 isq_hi ;		/* 45:   expressed as a 32-bit quantity. */
  UINT2 qsq_lo ;		/* 46: Same for Q... */
  SINT2 qsq_hi ;		/* 47:  */

  SINT2 zlin_noise ;		/* 48: Linearized LOG power noise sample mean */
  SINT2 zlin_rms  ;		/* 49: Linearized LOG power noise root-mean-square */

  SINT2 inse_hv_ratio ;		/* 50: Horiz/Vert noise ratio in dB*100 */

  SINT2 iafclevel ;		/* 51: Signed 16-bit AFC level */

  UINT2 intflt ;		/* 52: Interference filter (bits  3:0) */
				/*            Minor Version (bits  7:4) */
				/*       IFD Sat.Power Code (bits 11:8) */
  SINT2 intflt_p1 ;		/* 53: Interference filter parameter #1 */
  SINT2 intflt_p2 ;		/* 54: Interference filter parameter #1 */

  UINT2 istat_i3 ;		/* 55: Immediate Status (third word) */
  SINT2 itrigslew ;		/* 56: Burst tracking slew (usec * 100) */

  UINT2 iPolFlags ;		/* 57: Polarization flags */
#define POLFLAG_TX_H     0x0001	/*       Use H transmissions for (T,Z,V,W) */
#define POLFLAG_TX_V     0x0002	/*       Use V transmissions for (T,Z,V,W) */
#define POLFLAG_RX_CO    0x0004	/*       Use Co-Pol reception for (T,Z,V,W) */
#define POLFLAG_RX_CX    0x0008	/*       Use Cross-Pol reception for (T,Z,V,W) */
#define POLFLAG_CNOISE   0x0010	/*       Correct all polar params for noise */
#define POLFLAG_FILTER   0x0020	/*       Use filtered data for all polar params */
#define POLFLAG_PHISIGN  0x0040	/*       Sign convention for PHIdp */

  UINT2 iMaskSpacingCM ;	/* 58: Range mask spacing (cm) for current pulsewidth */

  UINT2 istat_i4 ;		/* 59: Immediate Status (fourth word) */

  UINT2 unused_word_60 ;	/* 60:  */
  UINT2 unused_word_61 ;	/* 61:  */
  UINT2 unused_word_62 ;	/* 62:  */
  UINT2 unused_word_63 ;	/* 63:  */
  UINT2 unused_word_64 ;	/* 64:  */
} ;

/* Bits in GPARM latched status word
 */
#define GLS_NTRIGNSE	 0x0001	/* No trigger during noise measurement */
#define GLS_FASTNSE	 0x0002	/* Trig too fast during noise measurement */
#define GLS_NTGPROC	 0x0004	/* No trigger during PROC command */
#define GLS_PRFVARIED	 0x0008	/* PRF varied from beginning to end of ray */
#define GLS_FIFOERROR	 0x0020	/* Buffer overflow during PROC command */
#define GLS_OLOST	 0x0040	/* Some user output words were lost */
#define GLS_NOISERROR	 0x0080	/* Error detected during noise measurement */
#define GLS_RMASK	 0x0200	/* Range mask error */
#define GLS_SIMULATE	 0x0400	/* Simulated data sequencing error */
#define GLS_PHSEQMES	 0x0800	/* Measured phase sequence is incorrect */
#define GLS_ZLINSE	 0x4000	/* Error in linear Z noise calculation */
#define GLS_PMODERR	 0x8000	/* Configuration error during PROC command */

#define MAXPRTVARYUSEC 10.0	/* Maximum allowable PRT (1/PRF) variation */

/* Bits in GPARM immediate status word (#1)
 */
#define GIS_NOTRIG       0x0001	/* No trigger */
#define GIS_SYLDERR      0x0002	/* Error loading sync angle table */
#define GIS_PWINFO       0x0004	/* PWINFO command is enabled */

#define GIS_SYBCD        0x0008	/* Sync angles are BCD */
#define GIS_SYEL         0x0010	/* Sync angles are for elevation axis */
#define GIS_SYENAB       0x0020	/* Sync enabled */
#define GIS_SYNOINT      0x0040	/* Sync wait loop not interruptible */
#define GIS_SYDYNAM      0x0080	/* Sync dynamically (angles end rays) */
#define GIS_SYOPBITS     0x00F8	/* The above four bits */

#define GIS_HASIAGC      0x0100	/* An AUX board with IAGC is connected */
#define GIS_16BITTS      0x0200	/* 16-bit time series is supported */

#define GIS_UMODE0	 0x0400	/* 2-bit unfolding mode */
#define GIS_UMODE1	 0x0800

#define GIS_NAUX0	 0x1000	/* Count of # of AUX boards attached */
#define GIS_NAUX1	 0x2000

#define GIS_SPEC	 0x4000	/* Power Spectrum output is supported */

/* Bits in GPARM immediate status word (#2)
 */
#define GI2S_FFT	 0x0001	/* FFT processing mode is supported */
#define GI2S_RPH	 0x0002	/* Random phase mode is supported */
#define GI2S_KNM	 0x0004	/* KNMI LOG filtering is supported */
#define GI2S_DPT1	 0x0008	/* Dual-PRT Type-1 is supported */
#define GI2S_UPLERR	 0x0010	/* Error in uplink to receiver */
#define GI2S_DNLERR	 0x0020	/* Downlink error / no signal detected */
				/* 0x0040 was: separate noise levels per PW */
#define GI2S_PLLERR	 0x0080	/* Error in user clock PLL in receiver */

#define GI2S_AFCBITS     0x0700	/* Current AFC status code */
#define GI2S_AFCLOWBIT   8

#define AFC_DISABLED  1
#define AFC_MANUAL    2
#define AFC_NOBURST   3
#define AFC_WAIT      4
#define AFC_LOCKED    5
#define AFC_TRACK     6

#define GI2S_IFDSWS	 0x0800	/* Receiver's switches not set to "run" */
#define GI2S_TBLANK	 0x1000	/* Trigger blanking is enabled */
#define GI2S_NOBURST	 0x2000	/* Missing signal at IFD burst input */
#define GI2S_DSPCOUNT	 0x4000	/* DSP count may be less than what's available */
#define GI2S_SLIPBURST	 0x8000	/* Burst pulse energy is not well centered */

/* Bits in GPARM immediate status word (#3)
 */
#define GI3S_BPTIMEADJ	 0x0001	/* Burst Pulse time adjustments are okay */
#define GI3S_BPFREQADJ	 0x0002	/* Burst Pulse frequency adjustments are okay */
#define GI3S_BPHUNTENA	 0x0004	/* Burst Pulse hunting is enabled */
#define GI3S_BPHUNTNOW	 0x0008	/* Hunting is running right now */
#define GI3S_BPHFAIL	 0x0010	/* Last hunt failed to find the burst */
#define GI3S_DPT2	 0x0020	/* Dual PRT (Type 2) is supported */
#define GI3S_PHSEQGEN	 0x0040	/* Could not generate the requested phase seq */
#define GI3S_TXCLK	 0x0080	/* Problem with digital transmitter clock */
#define GI3S_USER1	 0x0100	/* Extended user PMODEs are supported */
#define GI3S_USER2	 0x0200
#define GI3S_USER3	 0x0400
#define GI3S_USER4	 0x0800

/* Bits in GPARM immediate status word (#4)
 */
#define GI4S_ANYSPECINT  0x0001	/* Power spectra for params matches sample size */
#define GI4S_ANYSPECOUT  0x0002	/* PROC output spectra match sample size */
#define GI4S_ALTPATTERN  0x0004	/* Bits indicating that the trigger */
#define GI4S_ALTPERIOD   0x0008	/*   pattern and/or PRT were altered. */
#define GI4S_IQPACKHISNR 0x0010	/* Using High-SNR packed (I,Q) format */
#define GI4S_ALTTRUNC    0x0020	/* Out-of-Memory, triggers truncated */
#define GI4S_EXTERNALTS  0x0040	/* TimeSeries data source is external to the DSP */
#define GI4S_BATCH       0x0080	/* Batch processing mode is supported */
#define GI4S_CANTEXTRIG  0x0100	/* Major mode refuses to use external trigger */
#define GI4S_NSEFMTPACK  0x0200	/* Use packed format for i_nse and q_nse */
#define GI4S_RXPROTFLT   0x0400	/* Receiver protection fault */
#define GI4S_IFDCHANERR  0x0800	/* IFD dual-channel inconsistency */

/* Scale factors for encoding/decoding trigger periods
 */
#define TREQSCL 6000000.0	/* Scale factors for I/O encoding of */
#define TMESSCL 1200000.0	/*  requested and measured PRT/PRF. */

/* ------------------------------
 * Target simulator definitions.  Beginning with the RVP8, the signal
 * processor can simulate physical targets that are mixed in with the
 * realtime receiver (I,Q) values.
 */
#define DSPPHYSTARGSIMCOUNT  4	/* Max number of simulated targets */

struct dspPhysTargSim {		/* Parameters to simulate a physical target */
  UINT1 iFlags ;		/*   This simulated target is enabled */
#define DPTS_SIMCOPOL   0x01	/*     Apply signal to Co-Pol Rx channel */
#define DPTS_SIMCXPOL   0x02	/*     Apply signal to Cross-Pol Rx channel */
  char  ipad1x1[1] ;
  UINT2 iStartKM10 ;		/*   Starting range and span of the */
  UINT2 iLengthKM10 ;		/*     target in tenths of km. */
  SINT2 iPowerDB10 ;		/*   Power in tenths of dB relative to saturation */
  SINT2 iPowerSpanDB10 ;	/*     and delta over the range span. */
  SINT2 iDopplerHZ ;		/*   Target Doppler shift in tenths of Hz, */
  SINT2 iDopplerSpanHZ ;	/*     and delat over the range span. */
  char  ipad14x6[6] ;
} ;

/* ------------------------------
 * Extended parameter information structure.  These are elements that
 * really should be included in GPARM, but that structure has become
 * full.  The 'IO' form is used between the signal processor and the
 * DSP driver.
 */
#define DSPSITENAMELEN 16	/* Site name length within DSPs */
#define DSPTASKNAMELEN 16	/* Task name length within DSPs */

struct dspExParm {		/* Extended DSP parameter readback */
  char sSiteName		/*  Site name (NULL terminated) */
    [ DSPSITENAMELEN+1 ] ;
  char pad17x3[3] ;

  FLT4 fMaxRangeKM ;		/* Furthest range mask bin (km) */

  FLT4 fPulseWidthUSec ;	/* Pulsewidth in microseconds */
  FLT4 fBandWidthMHz ;		/* Tx/Rx Bandwidth in megaHertz */

  struct ymds_time rayUTC ;	/* UTC time of last processed ray */

  FLT4  fBurstFreqMHz ;		/* Measured frequency and power of the */
  FLT4  fBurstPowerDBm ;	/*   primary channel burst pulse. */

  UINT1 iBurstPhaseLock ;	/* Lock (I,Q) data to burst (one of BPLOCK_xxx) */
  UINT1 lBurstAmplCor ;	        /* Pulse-to-pulse amplitude correction being used */

  UINT2 iMismatchTS ;		/* RVP8 vs TimeSeries mismatched parameters */
#define MMTS_MAJORMODE  0x0001	/*   Major mode of the processor */
#define MMTS_BINCOUNT   0x0002	/*   Number of input bins (range mask bits) */
#define MMTS_UMODE      0x0004  /*   Dual-PRF unfolding mode */
#define MMTS_PWIDTH     0x0008  /*   Pulsewidth selection code */
#define MMTS_PHASEMOD   0x0010  /*   Phase modulation sequence */
#define MMTS_POLAR      0x0020  /*   Transmit Polarization */
#define MMTS_NOMPRF     0x0040  /*   Nominal PRF from last SETPWF opcode */
#define MMTS_BINPLACE   0x0080  /*   Placement of input bins (range mask bits) */
#define MMTS_RMASKRES   0x0100  /*   Range mask resolution */

				/* Desired RVP8 settings based on last ray */
  UINT2 iRayMajorMode ;		/*   Major mode for data acquisition */
  UINT2 iRayBinCount ;		/*   Overall input bin count */
  UINT1 iRayUmode ;		/*   Dual-PRF unfolding mode */
  UINT1 iRayPWidth ;		/*   Pulsewidth code */
  UINT1 iRayPhaseMod ;		/*   Tx phase modulation */
  UINT1 iRayPolar ;		/*   Tx polarization */
  FLT4  fRayNomPRF ;		/*   Nominal PRF (Hertz) */

  SINT2 iIFDTempCaseDegC[2] ;	/* Temperature of the IFD chassis and of */
  SINT2 iIFDTempFPGADegC[2] ;	/*   the onboard FPGA chip (Centigrade). */

  BIN2 iNoiseAZ, iNoiseEL ;	/* Mean AZ/EL of last noise sample */
} ;

struct dspExParmIO {		/* DSP I/O structure (APPEND TO END ONLY) */
  UINT2 iSiteName		/* Site name (packed in Lo/Hi order */
    [ DSPSITENAMELEN/2 ] ;	/*   within the 16-bit words. */

  UINT2 iMaxRangeKM ;		/* Kilometer and meter components of */
  UINT2 iMaxRangeM ;		/*   furthest bin in the range mask. */

  UINT2 iPwUSec ;		/* Pulsewidth microsecond and nanosecond */
  UINT2 iPwNSec ;		/*   component parts. */

  UINT2 iRayMsUTC ;		/* UTC last ray time, 16-bit miliseconds and */
  UINT2 iRayLoUTC, iRayHiUTC ;	/*   32-bit seconds. */

  UINT2 iBurstPhaseLock ;	/* Lock (I,Q) data to burst (one of BPLOCK_xxx) */
  UINT2 lBurstAmplCor ;    	/* Pulse-to-pulse amplitude correction being used */

  UINT2 iMismatchTS ;		/* RVP8 vs TimeSeries mismatched parameters */
  UINT2 iRayMajorMode ;		/*   Major mode for data acquisition */
  UINT2 iRayBinCount ;		/*   Overall input bin count */
  UINT2 iRayUmode ;		/*   Dual-PRF unfolding mode */
  UINT2 iRayPWidth ;		/*   Pulsewidth code */
  UINT2 iRayPhaseMod ;		/*   Tx phase modulation */
  UINT2 iRayPolar ;		/*   Tx polarization */
  UINT2 iRayHiNomPRF ;		/*   Nominal PRF (Hertz) */
  UINT2 iRayLoNomPRF ;		/*     (and Hertz / 2^16) */

  UINT2 iBandWidthKHz ;		/* Tx/Rx Bandwidth in kiloHertz */

  UINT2 iBurstFreq2KHz ;	/* Burst frequency (2KHz increments) */
  SINT2 iBurstPowerDBm100 ;	/* Burst power (hundredths of dBm) */

  SINT2 iIFDTempCaseDegC[2] ;	/* Temperature of the IFD chassis and of */
  SINT2 iIFDTempFPGADegC[2] ;	/*   the onboard FPGA chip (Centigrade). */

  UINT2 iNoiseAZ, iNoiseEL ;	/* Mean AZ/EL of last noise sample */
} ;

/* ==================================================
 * Standard command opcodes from host computer.  Normal ones
 * occupy lower 5-bits ; extended ones occupy middle 7-bits.
 */
#define OP_NOP       0x00
#define OP_LRMSK     0x01	/* Load range mask */
#define OP_SOPRM     0x02	/* Set operating parameters */
#define OP_IOTEST    0x03	/* Input/Output test */
#define OP_OTEST     0x04	/* Output only test */
#define OP_SNOISE    0x05	/* Sample receiver noise */
#define OP_PROC      0x06	/* Acquire/process data */
#define OP_LSTCB     0x07	/* Load STC and user range bits */
#define OP_LFILT     0x08	/* Load filter versus range */
#define OP_GPARM     0x09	/* Get processor parameters */
#define OP_LSIMUL    0x0A	/* Load simulated time series */
#define OP_LDBCOR    0x0B	/* Load LOG correction table */
#define OP_RESET     0x0C	/* Various resets */
#define OP_TRIGWF    0x0D	/* Set trigger waveforms */
#define OP_RNGOFF    0x0E	/* Set range offset */
#define OP_PWINFO    0x0F	/* Set pulse width bits & bounds */
#define OP_SETPWF    0x10	/* Set pulse width and PRF */
#define OP_LSYNC     0x11	/* Load angle sync info */
#define OP_SLED      0x12	/* Control panel LED */
#define OP_TTYOP     0x13	/* TTY simulated op */
#define OP_LDAGC     0x14	/* Load AGC lookup table */
#define OP_LDRNV     0x15	/* Load custom range norm table */
#define OP_RBACK     0x16	/* Read back internal params */
#define OP_IAGCTRL   0x17	/* Instantaneous AGC control */
#define OP_XARGS     0x18	/* Supply extended args to commands */

#define OP_EXTEND    0x1F
#define XOP_LFSPECS    0x00	/* Load clutter filter specifications */
#define XOP_KNMCMDS    0x01	/* Custom commands for KNMI filters */
#define XOP_CFGHDR     0x02	/* Configure header of PROC output rays */
#define XOP_CFGINTF    0x03	/* Configure interference filter */
#define XOP_BOOT       0x04	/* Load ROM image and reboot */
#define XOP_SETAFC     0x05	/* Set AFC level */
#define XOP_SETSLEW    0x06	/* Set trigger timing slew */
#define XOP_BPHUNT     0x07	/* Hunt for burst pulse */
#define XOP_CFGPHZ     0x08	/* Configure phase modulation */
#define XOP_UIQBITS    0x09	/* Set user bits for IQ headers*/
#define XOP_THRESH     0x0A	/* Set individual parameter thresholds */
#define XOP_TASKID     0x0B	/* Set task identification information */
#define XOP_PRFSECT    0x0C	/* Set trigger waveform within a sector */
#define XOP_TARGSIM    0x0D	/* Configure the internal target simulator */
#define XOP_BPOPTS     0x0E	/* Set Burst Pulse Processing Options */
#define XOP_USRINTR    0x7C	/* User defined opcodes for current major mode */
#define XOP_USRCONT    0x7D	/*   (Interrupting and Continuous side effects) */
#define XOP_LICENSE    0x7F	/* Enable licensed features */

#define XOPC(XOP) ( OP_EXTEND | ((XOP) << 5) )

/* Additional details of the PROC command
 */
#define OP_PROC_MODE    0x00000060 /* Processing mode bit field */
#define OP_PROC_SYNCDOP 0x00000020 /*   Synchronous Doppler */
#define OP_PROC_FREEDOP 0x00000040 /*   Free running Doppler */
#define OP_PROC_TS      0x00000060 /*   Time series */

#define OP_PROC_UMODE   0x00000300 /* Location of PRF unfolding choice */
#define OP_PROC_UMODE_SHIFT      8

#define OP_PROC_KDP     0x00000080 /* Parameter selection bits */
#define OP_PROC_ZDR     0x00000400
#define OP_PROC_WID     0x00000800
#define OP_PROC_VEL     0x00001000
#define OP_PROC_DBT     0x00002000
#define OP_PROC_DBZ     0x00004000
#define OP_PROC_ARCHIVE 0x00008000
#define OP_PROC_PHIDP   0x00010000
#define OP_PROC_RHOHV   0x00020000
#define OP_PROC_SQI     0x00040000
#define OP_PROC_LDRH    0x00080000
#define OP_PROC_RHOH    0x00100000
#define OP_PROC_PHIH    0x00200000
#define OP_PROC_LDRV    0x00400000
#define OP_PROC_RHOV    0x00800000
#define OP_PROC_PHIV    0x01000000
#define OP_PROC_FLAGS   0x02000000

/* Mask of universe of explicit data parameter bits
 */
#define OP_PROC_UBITS ( OP_PROC_DBZ   | OP_PROC_DBT   | OP_PROC_VEL  | \
                        OP_PROC_WID   | OP_PROC_ZDR   | OP_PROC_KDP  | \
                        OP_PROC_PHIDP | OP_PROC_RHOHV | OP_PROC_SQI  | \
                        OP_PROC_LDRH  | OP_PROC_RHOH  | OP_PROC_PHIH | \
                        OP_PROC_LDRV  | OP_PROC_RHOV  | OP_PROC_PHIV | \
                        OP_PROC_FLAGS )

/* Ray Header options for output rays generated by the PROC command
 * (selected by the CFGHDR command).
 */
#define DSP_HEADER_MAX_SIZE (2*78)  /* Maximum number of bytes output you can get */

#define DSP_HDR_TAGS    0x00000001 /* 32-bit TAGs from start and end */
#define DSP_HDR_PRT     0x00000002 /* Instantaneous PRT (end of ray) */
#define DSP_HDR_PULSES  0x00000004 /* Number of pulses in the ray */
#define DSP_HDR_TSTAMP  0x00000008 /* Millisecond time stamp (end of ray) */
#define DSP_HDR_GPARM   0x00000010 /* Complete GPARM structure */
#define DSP_HDR_FLAGS   0x00000020 /* Miscellaneous flags, see below */
#define DSP_HDR_UTC     0x00000040 /* UTC time plus miliseconds (start of ray) */
#define DSP_HDR_MMTS    0x00000080 /* Mismatch timeseries bits (MMTS_xxx) */
#define DSP_HDR_SYTIME  0x00000100 /* IFD system clock time (start of ray) */
#define DSP_HDR_TSVERS  0x00000200 /* Timeseries playback version number */
#define DSP_HDR_TASKID  0x00000400 /* Task ID encoded as struct rvp8TaskID_IO */

#define DSP_HDR_FLG_DPRFLOW  0x0001 /* Dual-PRF is in the low PRF state */
#define DSP_HDR_FLG_BLANKED  0x0002 /* Trigger is blanked for this ray */
#define DSP_HDR_FLG_PRFSECT  0x0004 /* This ray is from a special PRF sector */
#define DSP_HDR_FLG_ENDEDTS  0x0008 /* This ray resulted from a TS writer ending */
#define DSP_HDR_FLG_PRFSECT0 0x0010 /* When DSP_HDR_FLG_PRFSECT is set, */
#define DSP_HDR_FLG_PRFSECT1 0x0020 /*   these bits indicate which PRF  */
#define DSP_HDR_FLG_PRFSECT2 0x0040 /*   was actually selected. */

/* Opcodes for the OP_RBACK read back command
 */
#define RBACK_SOPRM      ( 0)	/* Operating parameters */
#define RBACK_RAYHIST    ( 1)	/* Ray history */
#define RBACK_LDSYNC     ( 2)	/* Angle SYNC table */
#define RBACK_LDAGC      ( 3)	/* AGC mapping table */
#define RBACK_LFILT      ( 4)	/* Filter selection vs range */
#define RBACK_LSTCB      ( 5)	/* STC waveform */
#define RBACK_LDRNV      ( 6)	/* Custom range normalization */
#define RBACK_TAGSAMPS   ( 7)	/* Samples of TAG input lines */
#define RBACK_MASKRES    (10)	/* Range mask bit spacing */
#define RBACK_UIQBITS    (11)	/* Custom 'UserIQ' bits */
#define RBACK_THRESH     (12)	/* Data threshold settings */
#define RBACK_EXPARM     (13)	/* Extended parameters */
#define RBACK_ADCLOHI    (14)	/* Min/Max analog ADC samps */
#define RBACK_LFSPECS    (15)	/* Clutter filter parameter definitions */

/* Other miscellaneous opcode parameters
 */
#define PRFSECT_MAXSECTS   8	/* Maximum number of PRF sectors */

#define BPL_PHASELOCK_OFF  0x0001 /* Force phase locking OFF */
#define BPL_PHASELOCK_ON   0x0002 /* Force phase locking ON */
#define BPL_AMPLCORR_OFF   0x0004 /* Force Amplitude Correction OFF */
#define BPL_AMPLCORR_ON    0x0008 /* Force Amplitude Correction ON */

/* User supplied bits (uninterpreted by the signal processor itself)
 * that are supplied via XOP_UIQBITS and read back via RBACK_UIQBITS.
 * The bits are guaranteed to be glitch-free to reader apps within
 * internal 32-bit integers, but not necessarilly across those 32-bit
 * boundaries.
 */
struct uiqbits {		/* Generic user bits */
  UINT4 iLong[2] ;		/*   No interpretation, just size */
} ;

#endif /* #ifndef SIGMET_DSP_H */
