/****************************************************************************
* $Id: bitstm.h,v 1.1 2006-01-17 14:27:51+01 rolf Exp rolf $
*****************************************************************************/
#ifndef _BITSTM_H_

#define _BITSTM_H_

#include <stddef.h>

typedef unsigned bsWord;	/* Preferred parameter type for BitStream functions */

/*
	Prozessor specific defines for accessing Bits & Bytes
*/

#define bsMODE8		/* 8/16/32 bit mode */

#ifdef bsMODE8
	typedef unsigned char	bsCell;		/* Memory access in 8-bit words */
	#define bsMSB			0x0080		/* Most Significant Bit for this type */
	#define bsCellSize		1			/* Address increment for 8-bit access, 80x86=1, RTX2010=1, TMS320=none */
	#define bsCellBits		8			/* Number of bits per cell */
#endif

#ifdef bsMODE16
	typedef unsigned short	bsCell;		/* Memory access in 16-bit words */
	#define bsMSB			0x8000		/* Most Significant Bit for this type */
	#define bsCellSize		2			/* Address increment for 16-bit access, 80x86=2, RTX2010=2, TMS320=1 */
	#define bsCellBits		16			/* Number of bits per cell */
#endif

#ifdef bsMODE32
	typedef unsigned int	bsCell;		/* Memory access in 32-bit words */
	#define bsMSB			0x80000000	/* Most Significant Bit for this type */
	#define bsCellSize		4			/* Address increment for 32-bit access, 80x86=4, RTX2010=none, TMS320=none */
	#define bsCellBits		32			/* Number of bits per cell */
#endif

typedef struct {
    bsCell   *bufPtr;		/* Buffer pointer */
    size_t   bufSize;		/* Buffer size in CELL's */
    bsCell  *curPtr;		/* Pointer to current CELL */
    bsCell   curBit;		/* Mask to current Bit in CELL */
} BitStream;

extern BitStream BS;                        /* Performance: One fixed bitstream, no multiple bitstreams in an application */

void bsNextBit (void);                      /* internal: increment pointer/mask by 1 Bit */

void bsInit (void *buf, size_t size);       /* Setup bitstream memory pointer */

void bsReset (void);                        /* Reset pointer to 1st Bit */
void bsClear (void);                        /* Clear BitStream from current position (0) */
int bsEnd (void);							/* TRUE: End of bitstream */
int bsSeek (unsigned n);					/* Seek bit position */
unsigned bsPos (void);                      /* Retrieve current bit position */
int bsSkip (unsigned n);					/* Skip n Bits  */

int bsPutBit (bsWord w);					/* Write Bit=0 (w==0) or Bit=1 (w!=0) to BS */
int bsGetBit (bsWord *w, bsWord mask);		/* Read one Bit from BS, mask it to word w */
											/* Without initializing *w !!! */

int bsPutWord (bsWord  w, bsWord mask);		/* Write multiple bits, Mask points to the 1st Bit (MSB) */
int bsGetWord (bsWord *w, bsWord mask);		/* Read multiple bits, Mask points to the 1st Bit (MSB) */
											/* With initializing *w !!! */

#endif
