#ifndef __REGISTER_BITOPS_H__
#define __REGISTER_BITOPS_H__

#include "stdlib.h"

#define REGISTER_BIT_DEFINITION(end, start) ((start << 16) | (end - start + 1))

template <typename RegType>
uint32_t bitRead(RegType reg, uint32_t bits)
{
	uint32_t regval = *regAddress(reg);
	const uint32_t width = bits & 0xff;
	const uint32_t bitno = bits >> 16;
	regval >>= bitno;
	regval &= ((1 << width) - 1);
	return regval;
}

template <typename RegType>
void bitWrite(RegType reg, uint32_t bits, uint32_t value)
{
	uint32_t regval = *regAddress(reg);
	const uint32_t width = bits & 0xff;
	const uint32_t bitno = bits >> 16;
	regval &= ~(((1 << width) - 1) << bitno);
	regval |= value << bitno;
	*regAddress(reg) = regval;
}

#endif // __REGISTER_BITOPS_H__
