/*****************************************************************************
 Binary Blob Parser configuration file syntax.  
 
 The basic unit of configuration is a field.  A field is a chunk of bytes
 in the blob that are to be interperated into a more meaningful form.  
 
 For each field the following must be defined:
 
   * ty  - The data type, may be one of:
           
           B - Just raw bytes
           U - Unsigned Integer
           S - Signed Integer
           F - An IEEE floating point value
           C - Character Data
 
   * off - The offset in bytes to the 0-th order byte of the field.
           NOTE: For big endian fields this *NOT* the start of the field in
           the byte stream!
   
   * bo  - The order for reading higher order bytes.  This parameter may have
           the value 'BE' if the most significant byte is at lower offsets 
           (i.e. the "big end" is first in the stream, or 'LE' if the most 
           significant byte is at higher offsets (i.e. the "little end is
           first.)
   
   * sz  - The number bytes to read out of the blob.  This may be any
	        value > 0 for fields of type B (bytes).  For fields any other
			  type the maximum size is 8 bytes. 

   
 The is a special field named 'default' that may be used to specify default
 field parameters.  For example if most of a fields in the binary blob to be
 parsed are little-endian, 2 byte items, one could specify this once as 
 follows:
 
    default{ 
       bo = LE
       sz = 2
    }
    
 Defaults can be altered at anytime through out the configuration file 
 by specifing the default{ } field again.  Note, 'default' is always defined.
 unless otherwise specified the defaults are:
 
    default{ bo=BE sz=1 off=0 ty=U sft=0 fmt="%llu" } 
 
 To unset a default parameter, do not include a value after the '=' sign.
 Extended parameters may be included in the 'default' field.
 

 Bit fields
 ----------
 To extract bit fields, first specify byte or bytes containing the bit field,
 then, specify a shift and offset.  For example to get the 7th and 8th bits
 out of 2 byte little endian field one could use:
 
 
    MY_FIELD{ bo=LE ty=U off=0 sz=2 sft=7 msk=0x3 }
    
  This would extract the 2 byte little endian field, down shift 7 bits and 
  then mask off the lower 2 bits.  Note that shifts are applied before masks.
  
  The bit field parameters are:
  
     * sft - How many bits to down shift, if this value is NEGATIVE then
		       it's absolute vale is the number of bits to up shift.
     
     * msk - The hex pattern to 'and' with the shifted field
 
 
 Text Conversion
 ---------------
 Binary values must be converted to strings for display.  Multiple methods are
 supported to handle this.  The simplest is just to specify a C style format
 string that matches the type of data.  The 'fmt' parameter handles this:
 
    PKT_TYPE{ty=U off=2 sft=6 msk=3 desc="Packet Type" fmt="%02lX"}
             
 Another conversion method is to enumerate text strings associated with each
 input value using the 'enum' parameter in the following manner:  
 
    PKT_TYPE{
       off=2 sft=6 msk=0x3 desc="Packet Type"
       enum=[0:"Unknown", 1:"HRS", 2:"LRS", 3:"HSK"]
    }
 
 
 The text conversion parameters are:
 
      * fmt - Printf sytle formating for display, this must match the 'ty' 
              field unless a float convert polynomial is given.  Not that
              the size parameter 'l' is needed for all integers and that
              all floats are stored internally as doubles.
     
      * enum - A dictionary mapping values to text strings.  The basic format
              is [ val0:text0, val1:text1' val2:text2 ... ]
      
      * cal - A list of polynomial coefficients. TODO: Add example
      
      * unit - A text string for the physical units of the value, for example
              "V" for volts.
 
 Alarms
 ------
 A list of values that set an alarm indicator, the list is in the order:
   
     Error Low, Warning Low, Warning High, Error High
     
 Example:
 
 "PS_VOLTAGE"{off=18 ty=S fmt="%6.3f" cal=(0,.01968) 
  /*    E_LO, W_LO, W_HI, E_HI  * /
  alrm=(1.40, 1.45, 1.55, 1.60) unit="V"
  desc="+1.5Volts Y180"
}
    
 
 

 Extended Parameters
 -------------------
 Parameters unknown to the library may be included in field definitions these
 are merely stored for later retrevial by applications than know how to 
 interperate them.  Parameter names must match the regular expression:
 
  [_a-zA-Z0-9][_a-zA-Z0-9\-]*

  Typical use of extended parameters is to include screen positioning
  information.

*/

/****************************************************************************/
/* Parameters */

/* A lookup table for which parsers to use, the application software has to
   know how to use these parameters, there is nothing built into the generic
   parser */
parameters{
   HSK_SWITCH = [
      0xD0:hskmp_gen  0xD1:hskmp_pss
      0xE0:hskmp_gen  0xE1:hskmp_cs_errs
      0xF0:hskmp_gen  0xF1:hskmp_br_rom
      0xF2:hskmp_br_rsv
      0xF3:hskmp_br_ver
      0xF4:hskmp_br_time 
      0xF5:hskmp_cs1
      0xF7:hskmp_fs8
      0xF8:hskmp_fs16
   ]
}

/*****************************************************************************/
/* General HSK parsing */

default{ 
   bo = BE      /* Default to big endian for the order of multi-byte fields */
   ty = U       /* Default to unsigned as the type */
   sz = 1       /* Default to one byte items */
   fmt = "%llu" /* Default to decimal integer formats */
}

parser hsk_static{
   desc="Inital 64 bytes of a housekeeping packet"
   expects=64
   
   CRC{ desc="Cyclic Redundancy Check" fmt="%04llX" off=0 sz=2 }
   
   SRC{desc="Packet Source" enum=[0:"Unknown" 1:"HRS" 2:"LRS" 3:"HS"]
      off=2 sft=6 msk=0x3 
      area=(0,0,4,1) table=(0,4,2,0) span_b="<b><i>" span_e="</i></b>"
   }
   
   SEQ{desc="Monotonically increasing sequence count" fmt="%5llu"
      off=3 sz=2 msk=0x3FFF 
   }
   
   TQF{desc="Time Source Flag" enum=[0:"External" 1:"Internal"]
      off=7 msk=0x1
   }
   
   TIME{desc="Packet time (bit 0 is TQF)" fmt="0x%08llX"
      off=7 sz=4
   }
   
   VERS{desc="Housekeeping Version Number" off=8 msk=0x1F
      enum=[
         0:   "Never Implemented"
         1:   "V1 9/2008"
         2:   "V2 when?"
         3:   "V3 when?"
         4:   "V4 when?"
         5:   "V5 when?"
         31: "31 11/2008"
      ]
   }
   
   MODE{desc="Housekeeping mode" enum=[0:"Panic" 1:"ROM" 2:"Science"]
      off=8 sft=5 msk=0x3
   }
   
   CE_CRC{off=9  desc="Command Error, CRC errors"}
   CE_CIP{off=10 desc="Command Error, CIP Sync Errors"}
   CE_TYP{off=11 desc="Command Error, CIP Type Errors"}
   CE_CMD{off=63 desc="Command Error, CMD Destination"}
   
   C_UDP{ off=13 sz=2 desc="Command Counts, UDP records"}
   C_CRC{ off=15 sz=2 desc="Command Counts, WAVES CRC groups"}
   C_CMD{ off=17 sz=2 desc="Command Counts, WAVES Commands"}
   C_CIP{ off=18 desc="Command Counts, CIP blocks"}
   
   CK_REF{off=20 desc="Clock Errors, missing 1/2Hz edge"}
   CK_MSG{off=21 desc="Clock Errors, missing message"}
   CK_PLL{off=22 desc="Clock Errors, DPLL restart"}
   CK_STS{off=23 fmt="0x%02llX" desc="Clock Status Flags"}
   
   LD_CNT{off=25 sz=2 desc="Load Record Count"}
   LD_SPR{off=26 fmt="0x%02llX" desc="Spare"}
   LD_STS{off=27 fmt="0x%02llX" desc="Load Status"}
   
   IP_CKS_Err{off=28  desc="IP Checksum Errors"}
   UDP_CKS_Err{off=29 desc="UDP Checksum Errors"}
   
   SUB_SEC{off=31 fmt="0x%04llX" desc="Sub Seconds"}
   
   IEB_STS{off=46 fmt="%02llX" desc="IEB Status Word"}
   
/* Alter the default to shorten this section */
default{off=46 desc="IEB Status" msk=0x01}
   IEB_CRC    {sft=7 enum=[0:"OK"         1:"BAD"]       }
   IEB_Name   {sft=6 enum=[0:" "          1:"Mismatch"]  }
   IEBExec    {sft=5 enum=[0:" "          1:"Failure"]   } 
   IEB_PC     {sft=4 enum=[0:" "          1:"Overrun"]   }
   IEB_State  {sft=3 enum=[0:"Idle"       1:"Running"]   }
   IEB_6        {sft=2 enum=[0:" "          1:"ERROR"]     }
   IEB_Memory {sft=1 enum=[0:"Loaded"     1:"NOT Loaded"]}
   IEB_Handler{sft=0 enum=[0:"NOT Loaded" 1:"Loaded"]    }
   
default{off=47 desc="POST Status" msk=0x01 }
   POST_MMU  {      enum=[0:"MMU-OK"           1:"MMU-Fail"]           }
   POST_MEM  {sft=1 enum=[0:"Main_Memory-OK"   1:"Main_Memory-Fail"]   }
   POST_BLK_1{sft=1 enum=[0:"Bulk_Region-1-OK" 1:"Bulk_Region-1-Fail"] }
   POST_BLK_2{sft=3 enum=[0:"Bulk_Region-2-OK" 1:"Bulk_Region-2-Fail"] }
   POST_BLK_3{sft=4 enum=[0:"Bulk_Region-3-OK" 1:"Bulk_Region-3-Fail"] }
   POST_CRC  {sft=5 enum=[0:"CRC-OK"           1:"CRC-Fail"]           }
   
/* Unset the defaults used above */
rmdefault{ off desc msk}
   
   WBR_LOCK   {off=35 ty=S fmt="%6.3f" cal=(0,.00784) desc="WBR_LOCK" }
   HFR_LOCK   {off=32 ty=S fmt="%6.3f" cal=(0,.00784) desc="HFR_LOCK" }
   WBR_LOG_ATT{off=36 ty=S fmt="%6.3f" cal=(0,.00784) desc="WBR_LOG_A"}
   HFR_LOG_ATT{off=33 ty=S fmt="%6.3f" cal=(0,.00784) desc="HFR_LOG_A"}
   WBR_LOG_OUT{off=37 ty=S fmt="%6.3f" cal=(0,.00784) desc="WBR_LOG_O"}
   HFR_LOG_OUT{off=34 ty=S fmt="%6.3f" cal=(0,.00784) desc="HFR_LOG_O"}
   
   Y180_RUN{off=45 ty=S fmt="%6lld" desc="AN_RUN"}
   FFTE_RUN{off=38 ty=S fmt="%6lld" desc="AN_FRUN"}
   
default{ty=S fmt="%6.3f" unit="V"}
                                           /* E_LO, W_LO, W_HI, E_HI */
   "+1.5V_Y180"{off=42 cal=(0,.01968) alrm=(1.40,1.45,1.55,1.60) desc="+1.5V Y180"}
   "+1.5V_FFT" {off=39 cal=(0,.01968) alrm=(1.40,1.45,1.55,1.60) desc="+1.5V FFT" }
   "+3.3V_Y180"{off=44 cal=(0,.0325)  alrm=(3.10,3.20,3.40,3.50) desc="+3.3V Y180"}
   "+3.3V_FFT" {off=41 cal=(0,.0325)  alrm=(3.10,3.20,3.40,3.50) desc="+3.3V FFT" }
   "+1.8V_MEM" {off=40 cal=(0,.01968) alrm=(1.70,1.75,1.85,1.90) desc="+1.5V FFT"}
   "+1.5V_REF" {off=43 cal=(0,.01968) alrm=(1.40,1.45,1.55,1.60) 
                desc="Simulate +1.8 monitor"}
rmdefault{fmt}
default  {ty=U unit="V"}             
   
   "+5.5U"{off=48 fmt="%6.3f" cal=(0,.05413) desc="Power Supply"}
   "+12.0"{off=50 fmt="%6.2f" cal=(0,.11811) desc="Power Supply"}
   "-5.5U"{off=49 fmt="%6.3f" cal=(0,.05413) desc="Power Supply"}
   "-12.0"{off=51 fmt="%6.2f" cal=(0,.11811) desc="Power Supply"}
   "+3.6U"{off=52 fmt="%6.3f" cal=(0,.03543) desc="Power Supply"}
   "+15.0"{off=54 fmt="%6.2f" cal=(0,.14763) desc="Power Supply"}
   "+1.8U"{off=53 fmt="%6.3f" cal=(0,.01968) desc="Power Supply"}
   "-15.0"{off=55 fmt="%6.2f" cal=(0,.14763) desc="Power Supply"}
   "+5.00"{off=56 fmt="%6.3f" cal=(0,.04921) desc="Power Supply"}
   "+13U" {off=59 fmt="%6.2f" cal=(0,.12795) desc="Power Supply"}
   "-5.00"{off=57 fmt="%6.3f" cal=(0,.04921) desc="Power Supply"}
   "-13U" {off=60 fmt="%6.2f" cal=(0,.12795) desc="Power Supply"}
   "+20U" {off=61 fmt="%6.2f" cal=(0,.19685) desc="Power Supply"}
   "+2.50"{off=58 fmt="%6.3f" cal=(0,.02460) desc="Power Supply"}
   "-20U" {off=62 fmt="%6.2f" cal=(0,.19685) desc="Power Supply"}
}

/****************************************************************************/
/* Generic Micropacket */

parser hskmp_gen{
	expects=16
   desc = "Micropacket"
   TYPE{off=0 desc="Micropacket Type"}
}

/****************************************************************************/
/* 0xD1 */

parser hskmp_pss{
   desc="Power Switch Status"
	expects=16
   
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   ERRS{off=4 bo=LE sz=2 desc="Number of errors at power on"}
   LFR{off=6 sz=2 bo=LE enum=[0:"LFR Off" 1:"LFR On" DEF:"Error"] 
       desc="LFR Power Status"}
   HFR{off=8 sz=2 bo=LE fmt="0x%04llX" desc="HFR Power"}
   HFWBR{off=8 sz=2 bo=LE fmt="0x%04llX" desc="HFWBR Power"}
}

/*****************************************************************************/
/* 0xE1 */

parser hskmp_cs_errs{
   desc="Command Status: Error Counters"
	expects=16
   
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   RUNNING{off=4 sz=2 bo=LE fmt="0x%04llX" desc="Running"}
   PAR_ERR{off=6 sz=2 bo=LE fmt="0x%04llX" desc="Command Parity Errors"}
   FRM_ERR{off=8 sz=2 bo=LE fmt="0x%04llX" desc="Command Framing Errors"}
   OVR_ERR{off=10 sz=2 bo=LE fmt="0x%04llX" desc="Command Overflow Errors"}
   DMA_ERR{off=12 sz=2 bo=LE fmt="0x%04llX" desc="Command DMA Transfer Errors"}
   UST_ERR{off=14 sz=2 bp=LE fmt="0x%04llX" desc="Command DMA Transfer Errors"}
}


/*****************************************************************************/
/* 0xF1 */

parser hskmp_br_rom{
   desc="Reset Status"
	expects=16
   
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   F_ADR{off=4 sz=3 bo=LE fmt="0x%06llX" desc="Fault Address"}
   T_ADR{off=7 sz=3 bo=LE fmt="0x%06llX" desc="Trap Address"}
   
default{msk=0x1 off=10}
   Y180_FLT {sft=7 enum=[0:""   1:"FAULT"] desc="Y180 Fault"}
   Y180_TRP {sft=6 enum=[0:""   1:"TRAP"] desc="Y180 Trap"}
   WDT_RST  {sft=5 enum=[0:""   1:"RESET"] desc="Watchdog Reset"}
   WDT_RUN  {sft=4 enum=[0:""   1:"RUNNING"] desc="Watchdog Running"}
   SC_R_B_LV{sft=3 enum=[0:"LO" 1:"HI"] desc="SC Reset B Level"}
   SC_R_B   {sft=2 enum=[0:""   1:"RESET"] desc="SC Reset"}
   SC_R_A_LV{sft=1 enum=[0:"LO" 1:"HI"] desc="SC Reset B Level"}
   SC_R_A   {sft=0 enum=[0:""   1:"RESET"] desc="SC Reset"}
rmdefault{msk off}

   SID_IND{off=11 desc="Side Select Indicator"
      enum=[
         0x00:"Failed"
         0x31:"Def A"    0x32:"Def B"  0x33:"Def Both"
         0x41:"Rst A"    0x42:"Rst B"
         0x61:"Cmd A"    0x62:"Cmd B"
         0x64:"??"
         0x44:"DTCI"
      ]
   }
   
   TSK_VER{off=12 sz=4 ty=C desc="Hsk Task Version"}
}

/*****************************************************************************/
/* 0xF2 */

parser hskmp_br_rsv{
   desc="ROM Status"
	expects=16
         
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   
   IEB_C{off=4 sz=4 bo=LE desc="Block Count from IEB loader in boot ROM"}
   IEB_S{off=6 fmt="0x%02llX" desc="Status from IEB loader in the boot ROM"}
   
default{msk=0x1 off=7}
   CRC_FLT {sft=5 enum=[0:"" 1:"FAULT"] desc="CRC Hardware Fault"}
   DAT_BLK {sft=4 enum=[0:"" 1:"FAULT"] desc="Mem Region 3 fail (DATA Bulk)"}
   IEB_BLK {sft=3 enum=[0:"" 1:"FAULT"] desc="Mem Region 2 fail (IEB Bulk)"}
   PGM_BLK {sft=2 enum=[0:"" 1:"FAULT"] desc="Mem Region 1 fail (Program Bulk)"}
   MAIN_BLK{sft=1 enum=[0:"" 1:"FAULT"] desc="Mem Region 0 fail (Main Mem)"}
   MMU_FLT {sft=0 enum=[0:"" 1:"FAULT"] desc="MMU Test Failure"}
rmdefault{msk off}

   F_ADR{off=9 sz=3 bo=LE fmt="0x%06llX" desc="Fault Address"}
   T_ADR{off=12 sz=3 bo=LE fmt="0x%06llX" desc="Trap Address"}
   RESET{off=15 fmt="0x%02llX" desc="ROM Reset Status"}
}

/*****************************************************************************/
/* 0xF3 */

parser hskmp_br_ver{
   desc="Hardware Version, Kernel Version"
	expects=16
         
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   Y180_VER{off=4 sz=3 ty=C desc="Y180 Hardware Version"}
   WVFE_VER{off=8 sz=3 ty=C desc="FFT Engine Hardware Version"}
   KRN_VER{off=11 sz=5 ty=C desc="Kernel Version String"}
}

/*****************************************************************************/
/* 0xF4 */

parser hskmp_br_time{
   desc="Software build date"
	expects=16
   
   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}
   BLD_ID{off=4 sz=12 ty=C desc="HEXBUILD Date and Time String"}
}

/*****************************************************************************/
/* 0xF5 */

parser hskmp_cs1{
   desc="Clock Statistics"
	expects=16

   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}         
   LOST_REF{off=4 sz=2 bo=LE desc="Lost External Reference Clock Counter"}
   LOST_MSG{off=6 sz=2 bo=LE desc="Lost Time Message"}
   EXTR_MSG{off=8 sz=2 bo=LE desc="Extra Time Messages"}
   PH_GAIN{off=10 sz=2 bo=LE desc="Phase Gain"}
   FR_GAIN{off=12 sz=2 bo=LE desc="Frequency Gain"}
   SUB_SEC{off=14 sz=2 bo=BE desc="Sub Seconds field"}
}

/*****************************************************************************/
parser hskmp_fs8{
   desc="8-bit Fast Scan"
	expects=16

   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}         
   
   R01{off=4 desc="A/D Read 1"}
   R02{off=5 desc="A/D Read 2"}
   R03{off=6 desc="A/D Read 3"}
   R04{off=7 desc="A/D Read 4"}
   R05{off=8 desc="A/D Read 5"}
   R06{off=9 desc="A/D Read 6"}
   R07{off=10 desc="A/D Read 7"}
   R08{off=11 desc="A/D Read 8"}
   R09{off=12 desc="A/D Read 9"}
   R10{off=13 desc="A/D Read 10"}
   R11{off=14 desc="A/D Read 11"}
   R12{off=15 desc="A/D Read 12"}
}

/*****************************************************************************/
parser hskmp_fs16{
   desc="16-bit Fast Scan"
	expects=16

   TYPE{off=0 desc="Micropacket Type"}
   SEQ{off=1 desc="Sequence"}
   RTI{off=2 sz=2 fmt="0x%04llX" desc="RTI"}         
   
   R01{off=4 sz=2 bo=LE desc="A/D Read 1"}
   R02{off=6 sz=2 bo=LE desc="A/D Read 2"}
   R03{off=8 sz=2 bo=LE desc="A/D Read 3"}
   R04{off=10 sz=2 bo=LE desc="A/D Read 4"}
   R05{off=12 sz=2 bo=LE desc="A/D Read 5"}
   R06{off=14 sz=2 bo=LE desc="A/D Read 6"}
}


