/************************************************/
/*                                              */
/* File        : calbbits.h                     */
/* Description : CALB bits interpreter          */
/*                                              */
/* Authors: G.Cabibbo,A.Passeri                 */
/*                                              */
/* Created      : 11.02.1997                    */
/* Last modified: 11.02.1997                    */
/*                                              */
/************************************************/

#ifndef CALBBITS_H
#define CALBBITS_H

#include "/kloe/soft/onl/Common/v1/src/bits.h"
#include "calbhard.h"

/*********************************************************************/
/*                       bit values                                  */
/*********************************************************************/

#define CALBB_BIT_ON       0
#define CALBB_BIT_OFF      1

/********************************************************************/
/*                      Register bits                               */
/*                      if no mask specified, use 1                 */
/*                DO NOT USE DIRECTLY IF NOT REALLY NECESSARY       */
/********************************************************************/

/* control register  */

#define CALBB_CREG_ACQEN           0
#define CALBB_CREG_ZSEN            1
#define CALBB_CREG_BERREN          2
#define CALBB_CREG_ALU_SHIFT       3
#define CALBB_CREG_ALU_NBITS       3
#define CALBB_CREG_D12FORCE        6
#define CALBB_CREG_XRESEN          7


/* status register  */

#define CALBB_SREG_EFD             0
#define CALBB_SREG_HFD             1
#define CALBB_SREG_FFD             2
#define CALBB_SREG_EFHD            3
#define CALBB_SREG_HFHD            4
#define CALBB_SREG_FFHD            5
#define CALBB_SREG_XBERR           7


/*    FIFOs    */

#define CALBB_FIFO_NVD              31
#define CALBB_FIFO_SHIFT            0
#define CALBB_HFIFO_RANGE           17
#define CALBB_HFIFO_TRGNUM_SHIFT    0
#define CALBB_HFIFO_TRGNUM_RANGE    12
#define CALBB_HFIFO_NWORD_SHIFT     12
#define CALBB_HFIFO_NWORD_RANGE     5
#define CALBB_DFIFO_RANGE           18
#define CALBB_DFIFO_DATA_SHIFT      0
#define CALBB_DFIFO_DATA_RANGE      13
#define CALBB_DFIFO_CHAN_SHIFT      13
#define CALBB_DFIFO_CHAN_RANGE      5


/*********************************************************************/
/**************                types               *******************/
/*********************************************************************/

/*********************************************************************/
/*                       internal page                               */
/*********************************************************************/

typedef struct
        {
	  unsigned char acqen;     /* 0/1 */
	  unsigned char zsen;      /* 0/1 */
	  unsigned char berren;    /* 0/1 */
	  unsigned char alu;       /* 0,2,5 */
	  unsigned char d12force;  /* 0/1 */
	  unsigned char xresen;    /* 0/1 */
        } CALBB_CREG_bits;

typedef struct
        {
	  unsigned char efd;      /* 0/1 */
	  unsigned char hfd;      /* 0/1 */
	  unsigned char ffd;      /* 0/1 */
	  unsigned char efhd;     /* 0/1 */
	  unsigned char hfhd;     /* 0/1 */
	  unsigned char ffhd;     /* 0/1 */
          unsigned char xberr;    /* 0/1 */
        } CALBB_SREG_bits;


/*********************************************************************/
/*                         FIFO page                                 */
/*********************************************************************/

typedef struct
        {
	  unsigned char nvd;         /* 0/1 */
	  unsigned int fifodata;     /* 0..0x1ffff */
        } CALBB_HFIFO_bits;

typedef struct
        {
	  unsigned char nvd;         /* 0/1 */
	  unsigned int fifodata;     /* 0..0x3ffff */
        } CALBB_DFIFO_bits;

typedef struct
        {
	  unsigned int ntrig;         /* 0...4095 */
	  unsigned int nword;         /* 0..0x1f */
        } CALBB_HFIFO_unpack;

typedef struct
        {
	  unsigned int data;          /* 0...0x1fff */
	  unsigned int chan ;         /* 0..0x1f */
        } CALBB_DFIFO_unpack;

/*********************************************************************/
/**************           page types               *******************/
/*********************************************************************/

typedef struct
        {
	  unsigned char acqen;     /* 0/1 */
	  unsigned char zsen;      /* 0/1 */
	  unsigned char berren;    /* 0/1 */
	  unsigned char alu;       /* 0,2,5 */
	  unsigned char d12force;  /* 0/1 */
	  unsigned char xresen;    /* 0/1 */
	  unsigned char efd;       /* 0/1 */
	  unsigned char hfd;       /* 0/1 */
	  unsigned char ffd;       /* 0/1 */
	  unsigned char efhd;      /* 0/1 */
	  unsigned char hfhd;      /* 0/1 */
	  unsigned char ffhd;      /* 0/1 */
          unsigned char xberr;     /* 0/1 */
        } CALBB_PAGE_CSR_bits;


/*********************************************************************/
/**************      conversion routines           *******************/
/*********************************************************************/

/*********************************************************************/
/*                    control and status page                        */
/*********************************************************************/

/* control register */

#define calbb_cregtonr(/*CALBB_CREG_bits*/ creg)   /* IN  */         \
 (bits_pos_bit(creg.acqen,CALBB_CREG_ACQEN)       |   \
  bits_pos_bit(creg.zsen,CALBB_CREG_ZSEN)         |   \
  bits_pos_bit(creg.berren,CALBB_CREG_BERREN)     |   \
  bits_pos_bits(creg.alu,CALBB_CREG_ALU_SHIFT,CALBB_CREG_ALU_NBITS)   |   \
  bits_pos_bit(creg.d12force,CALBB_CREG_D12FORCE) |   \
  bits_pos_bit(creg.xresen,CALBB_CREG_XRESEN) )


#define calbb_nrtocreg(/* unsigned char */  nr, /* IN */              \
                       /*CALBB_CREG_bits*/ creg)   /* OUT  */         \
{                                                     \
  creg.acqen   =bits_get_bit(nr,CALBB_CREG_ACQEN);          \
  creg.zsen    =bits_get_bit(nr,CALBB_CREG_ZSEN);            \
  creg.berren  =bits_get_bit(nr,CALBB_CREG_BERREN);        \
  creg.alu     =bits_get_bits(nr,CALBB_CREG_ALU_SHIFT,CALBB_CREG_ALU_NBITS); \
  creg.d12force=bits_get_bit(nr,CALBB_CREG_D12FORCE);    \
  creg.xresen  =bits_get_bit(nr,CALBB_CREG_XRESEN);    \
}

		       
/* status register */

#define calbb_sregtonr(/*CALBB_SREG_bits*/ sreg)   /* IN  */         \
 (bits_pos_bit(sreg.efd,CALBB_SREG_EFD)     |   \
  bits_pos_bit(sreg.hfd,CALBB_SREG_HFD)     |   \
  bits_pos_bit(sreg.ffd,CALBB_SREG_FFD)     |   \
  bits_pos_bit(sreg.efhd,CALBB_SREG_EFHD)   |   \
  bits_pos_bit(sreg.hfhd,CALBB_SREG_HFHD)   |   \
  bits_pos_bit(sreg.ffhd,CALBB_SREG_FFHD)   |   \
  bits_pos_bit(sreg.xberr,CALBB_SREG_XBERR))


#define calbb_nrtosreg(/* unsigned char */  nr, /* IN */              \
                       /*CALBB_SREG_bits*/ sreg)   /* OUT  */         \
{                                                     \
  sreg.efd  =bits_get_bit(nr,CALBB_SREG_EFD);     \
  sreg.hfd  =bits_get_bit(nr,CALBB_SREG_HFD);     \
  sreg.ffd  =bits_get_bit(nr,CALBB_SREG_FFD);     \
  sreg.efhd =bits_get_bit(nr,CALBB_SREG_EFHD);    \
  sreg.hfhd =bits_get_bit(nr,CALBB_SREG_HFHD);    \
  sreg.ffhd =bits_get_bit(nr,CALBB_SREG_FFHD);    \
  sreg.xberr=bits_get_bit(nr,CALBB_SREG_XBERR);   \
}
		       		       

/*********************************************************************/
/*                         FIFO page                                 */
/*********************************************************************/

#define calbb_hfifotonr(/*CALBB_HFIFO_bits*/ hfifo)   /* IN  */  \
                      (bits_pos_bit(hfifo.nvd,CALBB_FIFO_NVD)   | \
		       bits_pos_bits(hfifo.fifodata,CALBB_FIFO_SHIFT,CALBB_HFIFO_RANGE))

#define calbb_nrtohfifo(/*unsigned int     */ nr,     /* IN  */  \
			/*CALBB_HFIFO_bits*/ hfifo) /* OUT */  \
        {                                                        \
	  hfifo.nvd = bits_get_bit(nr,CALBB_FIFO_NVD);        \
	  hfifo.fifodata = bits_get_bits(nr,CALBB_FIFO_SHIFT,CALBB_HFIFO_RANGE); \
	}

#define calbb_dfifotonr(/*CALBB_DFIFO_bits*/ dfifo)   /* IN  */  \
                      (bits_pos_bit(dfifo.nvd,CALBB_FIFO_NVD)   | \
		       bits_pos_bits(dfifo.fifodata,CALBB_FIFO_SHIFT,CALBB_DFIFO_RANGE))

#define calbb_nrtodfifo(/*unsigned int     */ nr,     /* IN  */  \
			/*CALBB_DFIFO_bits*/ dfifo) /* OUT */  \
        {                                                        \
	  dfifo.nvd = bits_get_bit(nr,CALBB_FIFO_NVD);        \
	  dfifo.fifodata = bits_get_bits(nr,CALBB_FIFO_SHIFT,CALBB_DFIFO_RANGE); \
	}

#define calbb_hfifo_unpack(/* unsigned int  */  hdata,  /* IN  */   \
                           /* CALBB_HFIFO_unpack */ hunpack)  /* OUT */    \
        {                                                         \
	  hunpack.ntrig = bits_get_bits(hdata,CALBB_HFIFO_TRGNUM_SHIFT,CALBB_HFIFO_TRGNUM_RANGE); \
          hunpack.nword = bits_get_bits(hdata,CALBB_HFIFO_NWORD_SHIFT,CALBB_HFIFO_NWORD_RANGE); \
        }

#define calbb_dfifo_unpack(/* unsigned int  */  ddata,  /* IN  */   \
                           /* CALBB_DFIFO_unpack */ dunpack)  /* OUT */    \
        { \
	  dunpack.data=bits_get_bits(ddata,CALBB_DFIFO_DATA_SHIFT,CALBB_DFIFO_DATA_RANGE); \
          dunpack.chan=bits_get_bits(ddata,CALBB_DFIFO_CHAN_SHIFT,CALBB_DFIFO_CHAN_RANGE); \
        }

/*********************************************************************/
/**************         get bit routines           *******************/
/*********************************************************************/

/* control */

#define calbb_get_acqen(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_CREG_ACQEN)

#define calbb_get_zsen(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_CREG_ZSEN)

#define calbb_get_berren(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_CREG_BERREN)

#define calbb_get_alu(/*unsigned char */ nr) /* IN */  \
        bits_get_bits(nr,CALBB_CREG_ALU_SHIFT,CALBB_CREG_ALU_NBITS)

#define calbb_get_d12force(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_CREG_D12FORCE)

#define calbb_get_xresen(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_CREG_XRESEN)


/* status */

#define calbb_get_efd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_EFD)

#define calbb_get_hfd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_HFD)

#define calbb_get_ffd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_FFD)

#define calbb_get_efhd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_EFHD)

#define calbb_get_hfhd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_HFHD)

#define calbb_get_ffhd(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_FFHD)

#define calbb_get_xberr(/*unsigned char */ nr) /* IN */  \
        bits_get_bit(nr,CALBB_SREG_XBERR)



/*********************************************************************/
/**************         set bit routines           *******************/
/*********************************************************************/

/* control */

#define calbb_set_acqen(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bit(nr,CALBB_CREG_ACQEN,value)

#define calbb_set_zsen(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bit(nr,CALBB_CREG_ZSEN,value)

#define calbb_set_berren(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bit(nr,CALBB_CREG_BERREN,value)

#define calbb_set_alu(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bits(nr,CALBB_CREG_ALU_SHIFT,CALBB_CREG_ALU_NBITS,value)

#define calbb_set_d12force(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bit(nr,CALBB_CREG_D12FORCE,value)

#define calbb_set_xresen(/*unsigned char */ nr,     /* IN */  \
			/*unsigned char */ value)  /* IN */  \
        bits_set_bit(nr,CALBB_CREG_XRESEN,value)


/*********************************************************************/
/**************      conversion routines           *******************/
/*********************************************************************/

#define calbb_bitstoregs_csr(/*CALBB_PAGE_CSR_bits*/ bits,  /* IN  */  \
				 /*CALBH_CSR_regs     */ regs)  /* OUT */  \
        { /* calbb_*tonr used in a strange way */	\
	  regs.creg = calbb_cregtonr(bits);		\
	  regs.sreg = calbb_sregtonr(bits);		\
        }

#define calbb_regstobits_csr(/*CALBH_CSR_regs     */ regs,  /* IN  */  \
				 /*CALBB_PAGE_CSR_bits*/ bits)  /* OUT */  \
        { /* calbb_nrto* user in a strange way */	\
	  calbb_nrtocreg(regs.creg,bits);		\
	  calbb_nrtosreg(regs.sreg,bits);		\
        }


#endif /* CALBBITS_H */