#ifndef __BIT_ACCESS_H__
#define __BIT_ACCESS_H__

#include "types.h"

// A macro that defines enumeration values for a bit field
// Specify the start and end bit positions

#define REG_BIT_DEFN(start, end) ((start << 16)|(end - start + 1))

template <typename RegType>
inline RegType bitRead(RegType reg, uint32_t bits)
{
  const uint32_t width  = bits & 0xff;
  const uint32_t bitno  = bits >> 16;
  reg >>= bitno;
  reg  &= ((1<<width)-1);
  return reg;
}

template <typename RegType>
inline void bitWrite(RegType& reg, uint32_t bits, uint32_t value)
{
	uint32_t          regval = reg;
	const uint32_t    width  = bits & 0xff;
	const uint32_t    bitno  = bits >> 16;
	regval &= ~(((1<<width)-1) << bitno);
	regval |=  value << bitno;
	reg = regval;
}

#endif // __BIT_ACCESS_h__
