Generic Pointers for MPLAB-C18 Compiler

by Isaac Marino Bavaresco

//==============================================================================
// Copyright (c) 2007-2009, Isaac Marino Bavaresco
// All rights reserved
// isaacbavaresco@yahoo.com.br
//==============================================================================
#ifndef		__GENERICPOINTER_H__
#define		__GENERICPOINTER_H__
//==============================================================================
#define	RAM						far ram
#define	ROM						const far rom
#define	GENERIC					far rom

/** Returns the RAM pointer 'p' converted to a generic pointer to void pointing to the same address.*/
#define MAKE_GEN_PTR_RAM(p)		((void GENERIC*)((unsigned short long)(p)|0x800000))
/** Returns the RAM pointer 'p' converted to a generic pointer to type 't' pointing to the same address.*/
#define MAKE_GEN_PTR_RAM_T(p,t)	((t GENERIC*)((unsigned short long)(p)|0x800000))
/** Returns the ROM pointer 'p' converted to a generic pointer to void pointing to the same address.
NOTE: not necessary, supplied just for completeness. Will waste program words if used.*/
#define MAKE_GEN_PTR_ROM(p)		((void GENERIC*)((unsigned short long)(p)&0x7fffff))
/** Returns the ROM pointer 'p' converted to a generic pointer to type 't' pointing to the same address.
NOTE: not necessary, supplied just for completeness. Will waste program words if used.*/
#define MAKE_GEN_PTR_ROM_T(p,t)	((t GENERIC*)((unsigned short long)(p)&0x7fffff))

/** Reads one char from generic pointer 'p'*/
#define READ_GEN_PTR(p)			(((unsigned char RAM*)&(p))[2]&0x80?*(char RAM*)(p):*(char ROM*)(p))
/** Reads one elemet of type 't' from generic pointer 'p'*/
#define READ_GEN_PTR_T(p,t)		(((unsigned char RAM*)&(p))[2]&0x80?*(t RAM*)(p):*(t ROM*)(p))

/** Writes one char to the address pointed to by generic pointer 'p'*/
#define WRITE_GEN_PTR(p,c)		{if(((unsigned char RAM*)&(p))[2]&0x80)*(char RAM*)(p)=(c);}
/** Writes one element of type 't' to the address pointed to by generic pointer 'p'*/
#define WRITE_GEN_PTR_T(p,t,c)	{if(((unsigned char RAM*)&(p))[2]&0x80)*(t RAM*)(p)=(c);}

/** Returns non-zero if generic pointer 'p' points to RAM*/
#define IS_GEN_PTR_TO_RAM(p)	(((unsigned char RAM*)&(p))[2]&0x80)
/** Returns non-zero if generic pointer 'p' points to ROM*/
#define IS_GEN_PTR_TO_ROM(p)	(!(((unsigned char RAM*)&(p))[2]&0x80))
//==============================================================================
#endif	//	__GENERICPOINTER_H__
//==============================================================================