/* OpenAmiga.org
 *  This file is part of the OpenAmiga "xfdmaster library project"
 *
 * For more information look at:
 *   http://openamiga.org/?function=viewproject&projectid=67
 */

#ifndef MEMIO_H
#define MEMIO_H

/* Useful macros */
#define READ_X(ptr,offs,type) (*(const type *)((CONST_APTR)(ptr) + (offs)))
#define READ_BYTE(ptr,offs) READ_X(ptr,offs,BYTE)
#define READ_UBYTE(ptr,offs) READ_X(ptr,offs,UBYTE)
#define READ_WORD(ptr,offs) READ_X(ptr,offs,WORD)
#define READ_UWORD(ptr,offs) READ_X(ptr,offs,UWORD)
#define READ_LONG(ptr,offs) READ_X(ptr,offs,LONG)
#define READ_ULONG(ptr,offs) READ_X(ptr,offs,ULONG)
#define READ_APTR(ptr,offs) READ_X(ptr,offs,APTR)
#define READ_X_INC(ptr,type) ({ \
	type res = *(const type *)(ptr); \
	(ptr) = (APTR)(ptr) + sizeof(type); \
	res; \
	})
#define READ_BYTE_INC(ptr) READ_X_INC(ptr,BYTE)
#define READ_UBYTE_INC(ptr) READ_X_INC(ptr,UBYTE)
#define READ_WORD_INC(ptr) READ_X_INC(ptr,WORD)
#define READ_UWORD_INC(ptr) READ_X_INC(ptr,UWORD)
#define READ_LONG_INC(ptr) READ_X_INC(ptr,LONG)
#define READ_ULONG_INC(ptr) READ_X_INC(ptr,ULONG)
#define READ_APTR_INC(ptr) READ_X_INC(ptr,APTR)
#define WRITE_X(ptr,offs,val,type) (*(type *)((APTR)(ptr) + (offs)) = (val))
#define WRITE_BYTE(ptr,offs,val) WRITE_X(ptr,offs,val,BYTE)
#define WRITE_UBYTE(ptr,offs,val) WRITE_X(ptr,offs,val,UBYTE)
#define WRITE_WORD(ptr,offs,val) WRITE_X(ptr,offs,val,WORD)
#define WRITE_UWORD(ptr,offs,val) WRITE_X(ptr,offs,val,UWORD)
#define WRITE_LONG(ptr,offs,val) WRITE_X(ptr,offs,val,LONG)
#define WRITE_ULONG(ptr,offs,val) WRITE_X(ptr,offs,val,ULONG)
#define WRITE_APTR(ptr,offs,val) WRITE_X(ptr,offs,val,APTR)
#define WRITE_X_INC(ptr,val,type) ({ \
	type res = *(type *)(ptr) = (val); \
	(ptr) = (APTR)(ptr) + sizeof(type); \
	res; \
	})
#define WRITE_BYTE_INC(ptr,val) WRITE_X_INC(ptr,val,BYTE)
#define WRITE_UBYTE_INC(ptr,val) WRITE_X_INC(ptr,val,UBYTE)
#define WRITE_WORD_INC(ptr,val) WRITE_X_INC(ptr,val,WORD)
#define WRITE_UWORD_INC(ptr,val) WRITE_X_INC(ptr,val,UWORD)
#define WRITE_LONG_INC(ptr,val) WRITE_X_INC(ptr,val,LONG)
#define WRITE_ULONG_INC(ptr,val) WRITE_X_INC(ptr,val,ULONG)
#define WRITE_APTR_INC(ptr,val) WRITE_X_INC(ptr,val,APTR)

#endif

