#ifndef __ENDIAN_H__
#define __ENDIAN_H__

#include <p24Fxxxx.h>

#define LITTLE_ENDIAN		1
#define BIG_ENDIAN			2

#define ENDIAN 				LITTLE_ENDIAN
// Multibyte quantities are stored in "little endian" format for MPLAB C30
// which means the least significant byte is stored at the lowest address
// As an example, the long value of 0x12345678 is stored at address 0x100 as
// 0x100	0x101	0x102	0x103
// 0x78		0x56	0x34	0x12
//
// Indirect 8 Bit 8080 Mode (For Big Endian only)
// AD_MODE		1
// CNF2			0
// CNF1			1
// CNF0			1
		
// CNF3			configure GPIO as outputs
// CNF4			1	(default for ssd1928 Eval board version 1E)
// CNF6			0	(MCLK = PLL_CLK, default for ssd1928 Eval board version 1E)


#if (ENDIAN==BIG_ENDIAN)
#define SWAP16(x)		((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00))
#define SWAP32(x)		((((x) >> 24) & 0x000000ff) | \
			 			(((x) >> 8) &  0x0000ff00) | \
			 			(((x) << 8) &  0x00ff0000) | \
			 			(((x) << 24) & 0xff000000))
#else
#define SWAP16(x)		x
#define SWAP32(x)		x
#endif


// these macros are for swapping regardless of endian
#define SWAPSHORT(x)	((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00))
#define SWAPLONG(x)	((((x) >> 24) & 0x000000ff) | \
			 (((x) >> 8) &  0x0000ff00) | \
			 (((x) << 8) &  0x00ff0000) | \
			 (((x) << 24) & 0xff000000))
#endif	/* _ENDIAN_H_ */
