//	ＩＯＣＳＣＡＬＬ　ダイレクトコール関数
//	関数の実体のアドレスを使う時はiocslib.hを使うこと。
//	（このヘッダーを使うとIOCSCALLが直接展開されるため、関数に実体がないから）
//　未完成版（一部の関数の動作がおかしいことは確認されている）

#ifdef	__IOCSLIB_H
/* 先にIOCSLIB.Hがインクルードされている時は有効にしない */
#define	___AIG_IOCSLIBM___	1
#endif

#ifndef	___AIG_IOCSLIBM___
#define	___AIG_IOCSLIBM___	1
#define	__IOCSLIB_H		/* IOCSLIB.Hもインクルードしたことにする */

#include "deftype.h"

#ifndef __GNUC__
	// XCのときはIOCSLIB.Hへ
	#include "iocslib.h"
#else
// GCCでのみ有効
#include	"iocsclass.h"

/* IOCS ｽｰﾊﾟｰﾊﾟｲｻﾞﾓｰﾄﾞ用最適化 */
#if 0
#define B_XMAX()   (*(unsigned short*)0x00000970)	/* IOCS work */
#define B_YMAX()   (*(unsigned short*)0x00000972)	/* IOCS work */
#define B_XPOS()   (*(unsigned short*)0x00000974)	/* IOCS work */
#define B_YPOS()   (*(unsigned short*)0x00000976)	/* IOCS work */

#define B_SFTSNS() (*(unsigned short*)0x00000810)	/* IOCS $02 */
#define BITSNS(k)  (*(unsigned char*)(0x00000800+k))	/* IOCS $04 */

#define B_COLOR(c)	{*(unsigned  char*)0x00000994=c;}	/* IOCS $22 */
#define B_LOCATE(x,y)	{*(unsigned short*)0x00000974=x,   \
			 *(unsigned short*)0x00000976=y;}	/* IOCS $23 */
// 以上defs.h(MY.X)より
#endif

/*アクセスランプを反転する*/
#define	TIMER_LED()						\
	asm volatile ("	bset	#0,$00e8a01b			\
		eori.b	#%0000_0111,$00e8a001			\
	");

/*******************IOCS************************/

#define B_KEYINP()						\
	({register long	_d0 asm("d0");				\
	_d0=0x00;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define B_KEYSNS()						\
	({register long	_d0 asm("d0");				\
	_d0=0x01;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define B_SFTSNS()						\
	({register long	_d0 asm("d0");				\
	_d0=0x02;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

// KEY_INIT $03

#define	BITSNS(gr)						\
	({register long	_d0 asm("d0");				\
	register long	_d1 asm("d1");				\
	_d0=0x04;						\
	_d1=(gr);						\
	asm volatile ("trap #15"				\
		:"=d"(_d0)					\
		:"0"(_d0),"d"(_d1)				\
		:"d0","d1");					\
	_d0;							\
	})

#define TVCTRL(mode)			\
	({register int   _D0 asm("d0");	\
	register int   _D1 asm("d1");	\
	_D1=(mode);			\
	_D0=0x0c;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define CRTMOD(mode)			\
	({register int   _D0 asm("d0");	\
	register int   _D1 asm("d1");	\
	_D1=(mode);			\
	_D0=0x10;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	_D0;				\
	})

// $1e〜
#define B_CURON()			\
	({register int   _D0 asm("d0");	\
	_D0=0x1e;			\
	asm volatile ("trap #15"::"g"(_D0):"d0");	\
	})

#define B_CUROFF()			\
	({register int   _D0 asm("d0");	\
	_D0=0x1f;			\
	asm volatile ("trap #15 "::"g"(_D0):"d0");	\
	})

#define B_PUTC(c)			\
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	_D1=(c);			\
	_D0=0x20;			\
	asm volatile ("trap #15 *B_PUTC":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	_D0;				\
	})

#define B_PRINT(s)			\
	({register int   _D0 asm("d0");	\
	register char *_A1 asm("a1");	\
	_A1=(s);			\
	_D0=0x21;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_A1):"a1");	\
	_D0;				\
	})

#define B_COLOR(c)			\
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	_D1=(c);			\
	_D0=0x22;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	_D0;				\
	})

#if 0
#define B_LOCATE(x,y)
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	register short _D1 asm("d2");	\
	_D1=(x);			\
	_D2=(y);			\
	_D0=0x23;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1),"g"(_D2));	\
	_D0;				\
	})
#endif

#define B_DOWN_S()			\
	({register int   _D0 asm("d0");	\
	_D0=0x24;			\
	asm volatile ("trap #15"::"g"(_D0):"d0");	\
	})

#define B_UP_S()			\
	({register int   _D0 asm("d0");	\
	_D0=0x25;			\
	asm volatile ("trap #15"::"g"(_D0):"d0");	\
	})

#define B_UP(n)				\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x26;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	})

#define B_DOWN(n)			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x27;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	})

#define B_RIGHT(n)			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x28;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	})

#define B_LEFT(n)			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x29;			\
	asm volatile ("trap #15":"=g"(_D0):"g"(_D0),"g"(_D1));	\
	})

#define B_CLR_ED()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=0;				\
	_D0=0x2a;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_CLR_ST()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=1;				\
	_D0=0x2a;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_CLR_AL()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=2;				\
	_D0=0x2a;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_ERA_ED()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=0;				\
	_D0=0x2b;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_ERA_ST()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=1;				\
	_D0=0x2b;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_ERA_AL()			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=2;				\
	_D0=0x2b;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_INS(n)			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x2c;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_DEL(n)			\
	({register int   _D0 asm("d0");	\
	register char  _D1 asm("d1");	\
	_D1=(n);			\
	_D0=0x2d;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	})

#define B_CONSOL(x1,y1,x2,y2)		\
	({register int   _D0 asm("d0");	\
	register int   _D1 asm("d1");	\
	register int   _D2 asm("d2");	\
	_D1=(x1)*65536+(y1);		\
	_D2=(x2)*65536+(y2);		\
	_D0=0x2e;			\
	asm volatile ("trap #15 *B_CONSOL"::"g"(_D0),"g"(_D1),"g"(_D2):"d0","d1","d2");	\
	})

/* B_PUTMES $2f */

#define	MS_INIT()						\
	({register long	_d0 asm("d0");				\
	_d0=0x70;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	})

#define	MS_CURON()						\
	({register long	_d0 asm("d0");				\
	_d0=0x71;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	})

#define	MS_CUROF()						\
	({register long	_d0 asm("d0");				\
	_d0=0x72;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	})

#define	MS_STAT()						\
	({register long	_d0 asm("d0");				\
	_d0=0x73;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define	MS_GETDT()						\
	({register long	_d0 asm("d0");				\
	_d0=0x74;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define	MS_CURGT()						\
	({register long	_d0 asm("d0");				\
	_d0=0x75;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define MS_CURST(p)			\
	({register int   _D0 asm("d0");	\
	register int   _D1 asm("d1");	\
	_D1=(p);			\
	_D0=0x76;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	_d0;							\
	})


#define DENSNS()						\
	({register long	_d0 asm("d0");				\
	_d0=0x7e;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	})

#define ONTIME()						\
	({register long	_d0 asm("d0");				\
	_d0=0x7f;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define BOOTINF()						\
	({register long	_d0 asm("d0");				\
	_d0=0x8e;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define ROMVER()						\
	({register long	_d0 asm("d0");				\
	_d0=0x8f;						\
	asm volatile ("trap #15":"=d"(_d0):"0"(_d0):"d0");	\
	_d0;							\
	})

#define SFTJIS(c)			\
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	_D1=(c);			\
	_D0=0xa0;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	_D0;				\
	})

#define JISSFT(c)			\
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	_D1=(c);			\
	_D0=0xa1;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	_D0;				\
	})

#define AKCONV(c)			\
	({register int   _D0 asm("d0");	\
	register short _D1 asm("d1");	\
	_D1=(c);			\
	_D0=0xa2;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1):"d0");	\
	_D0;				\
	})

#define OS_CURON()			\
	({register short _D0 asm("d0");	\
	_D0=0xae;			\
	asm volatile ("trap #15"::"g"(_D0):"d0");	\
	})

#define OS_CUROF()			\
	({register short _D0 asm("d0");	\
	_D0=0xaf;			\
	asm volatile ("trap #15"::"g"(_D0):"d0");	\
	})

#define TXXLINE(s)			\
	({register short _D0 asm("d0");	\
	register struct XLINEPTR *_A1 asm("a1");	\
	_A1=(s);			\
	_D0=0xd3;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_A1));	\
	})

#define TXYLINE(s)			\
	({register short _D0 asm("d0");	\
	register struct YLINEPTR *_A1 asm("a1");	\
	_A1=(s);			\
	_D0=0xd4;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_A1));	\
	})

#define TXRASCPY(sor_dest,num,mode)	\
	({register short _D0 asm("d0");	\
	register int   _D1 asm("d1");	\
	register int   _D2 asm("d2");	\
	register int   _D3 asm("d3");	\
	_D1=(sor_dest);			\
	_D2=(num);			\
	_D3=(mode);			\
	_D0=0xdf;			\
	asm volatile ("trap #15"::"g"(_D0),"g"(_D1),"g"(_D2),"g"(_D3):"d0");	\
	})
// 以上defs.h(MY.X)より

#ifdef	__cplusplus
extern	"C" {
#endif
// プロトタイプのみ有効
// 以上にないもの（実現出来ないもの／未実装）はライブラリ関数を使う(iocslib.hと同じ)
	int	TRAP15(struct REGS *, struct REGS *);
//	int	B_KEYINP(void);
//	int	B_KEYSNS(void);
//	int	B_SFTSNS(void);
//	int	BITSNS(int);
	void	SKEYSET(int);
//	void	TVCTRL(int);
	void	LEDMOD(int, int);
	int	TGUSEMD(int, int);
	int	DEFCHR(int, int, UBYTE *);
//	int	CRTMOD(int);
	int	CONTRAST(int);
	int	HSVTORGB(int, int, int);
	int	TPALET(int, int);
	int	TPALET2(int, int);
	void	TCOLOR(int);
	UBYTE*	FNTADR(int, int);	// 追加
	int	FNTGET(int, int, struct FNTBUF *);
	void	TEXTGET(int, int, struct FNTBUF *);
	void	TEXTPUT(int, int, struct FNTBUF *);
	void	CLIPPUT(int, int, struct FNTBUF *, struct CLIPXY *);
	void	SCROLL(int, int, int);
//	void	B_CURON(void);
//	void	B_CUROFF(void);
//	int	B_PUTC(int);
//	int	B_PRINT(UBYTE *);
//	int	B_COLOR(int);
	int	B_LOCATE(int, int);
//	void	B_DOWN_S(void);
//	void	B_UP_S(void);
//	void	B_UP(int);
//	void	B_DOWN(int);
//	void	B_RIGHT(int);
//	void	B_LEFT(int);
//	void	B_CLR_ED(void);
//	void	B_CLR_ST(void);
//	void	B_CLR_AL(void);
//	void	B_ERA_ED(void);
//	void	B_ERA_ST(void);
//	void	B_ERA_AL(void);
//	void	B_INS(int);
//	void	B_DEL(int);
//	int	B_CONSOL(int, int, int, int);
	int	B_PUTMES(int, int, int, int, UBYTE *);
	int	SET232C(int);
	int	LOF232C(void);
	int	INP232C(void);
	int	ISNS232C(void);
	int	OSNS232C(void);
	void	OUT232C(int);
	int	JOYGET(int);
	int	INIT_PRN(int, int);
	int	SNSPRN(void);
	void	OUTLPT(int);
	void	OUTPRN(int);
	int	B_SEEK(int, int);
	int	B_VERIFY(int, int, int, UBYTE *);
	int	B_READDI(int, int, int, UBYTE *);
	int	B_DSKINI(int, UBYTE *, int);
	int	B_DRVSNS(int);
	int	B_WRITE(int, int, int, UBYTE *);
	int	B_READ(int, int, int, UBYTE *);
	int	B_RECALI(int);
	int	B_ASSIGN(int, int, int, UBYTE *);
	int	B_WRITED(int, int, int, UBYTE *);
	int	B_READID(int, int, UBYTE *);
	int	B_BADFMT(int, int, int);
	int	B_READDL(int, int, int, UBYTE *);
	int	B_FORMAT(int, int, int, UBYTE *);
	int	B_DRVCHK(int, int);
	int	B_EJECT(int);
	int	BINDATEBCD(int);
	void	BINDATESET(int);
	int	TIMEBCD(int);
	void	TIMESET(int);
	int	BINDATEGET(void);
	int	DATEBIN(int);
	int	TIMEGET(void);
	int	TIMEBIN(int);
	int	DATECNV(UBYTE *);
	int	TIMECNV(UBYTE *);
	int	DATEASC(int, UBYTE *);
	int	TIMEASC(int, UBYTE *);
	void	DAYASC(int, UBYTE *);
	int	ALARMMOD(int);
	int	ALARMSET(int, int, int);
	int	ALARMGET(int *, int *, int *);
	void	ADPCMOUT(UBYTE *, int, int);
	void	ADPCMINP(UBYTE *, int, int);
	void	ADPCMAOT(struct CHAIN *, int, int);
	void	ADPCMAIN(struct CHAIN *, int, int);
	void	ADPCMLOT(struct CHAIN2 *, int);
	void	ADPCMLIN(struct CHAIN2 *, int);
	int	ADPCMSNS(void);
	void	ADPCMMOD(int);
	void	OPMSET(int, int);
	int	OPMSNS(void);
	int	OPMINTST(UBYTE *);
	int	TIMERDST(UBYTE *, int, int);
	int	VDISPST(UBYTE *, int, int);
	int	CRTCRAS(UBYTE *, int);
	int	HSYNCST(UBYTE *);
	int	PRNINTST(UBYTE *);
//	void	MS_INIT(void);
//	void	MS_CURON(void);
//	void	MS_CUROF(void);
//	int	MS_STAT(void);
//	int	MS_GETDT(void);
//	int	MS_CURGT(void);
//	int	MS_CURST(int, int);
	int	MS_LIMIT(int, int, int, int);
	int	MS_OFFTM(int, int);
	int	MS_ONTM(int, int);
	void	MS_PATST(int, struct PATST *);
	void	MS_SEL(int);
	void	MS_SEL2(WORD *);
	int	SKEY_MOD(int, int, int);
//	void	DENSNS(void);
//	int	ONTIME(void);
	int	B_INTVCS(int, int);
	int	B_SUPER(int);
	int	B_BPEEK(UBYTE *);
	int	B_WPEEK(UWORD *);
	int	B_LPEEK(ULONG *);
	void	B_MEMSTR(UBYTE *, UBYTE *, int);
	void	B_BPOKE(UBYTE *, int);
	void	B_WPOKE(UWORD *, int);
	void	B_LPOKE(ULONG *, int);
	void	B_MEMSET(UBYTE *, UBYTE *, int);
	void	DMAMOVE(UBYTE *, UBYTE *, int, int);
	void	DMAMOV_A(struct CHAIN *, UBYTE *, int, int);
	void	DMAMOV_L(struct CHAIN2 *, UBYTE *, int);
	int	DMAMODE(void);
//	int	BOOTINF(void);
//	int	ROMVER(void);
	void	G_CLR_ON(void);
	int	GPALET(int, int);
//	int	SFTJIS(int);
//	int	JISSFT(int);
//	int	AKCONV(int, int);
	int	RMACNV(int, UBYTE *, UBYTE *);
	int	DAKJOB(UBYTE *);
	int	HANJOB(UBYTE *);
//	void	OS_CURON(void);
//	void	OS_CUROF(void);
	int	APAGE(int);
	int	VPAGE(int);
	int	HOME(int, int, int);
	int	WINDOW(int, int, int, int);
	int	WIPE(void);
	int	PSET(struct PSETPTR *);
	int	POINT(struct POINTPTR *);
	int	LINE(struct LINEPTR *);
	int	BOX(struct BOXPTR *);
	int	FILL(struct FILLPTR *);
	int	CIRCLE(struct CIRCLEPTR *);
	int	PAINT(struct PAINTPTR *);
	int	SYMBOL(struct SYMBOLPTR *);
	int	GETGRM(struct GETPTR *);
	int	PUTGRM(struct PUTPTR *);
	int	SP_INIT(void);
	int	SP_ON(void);
	void	SP_OFF(void);
	int	SP_CGCLR(int);
	int	SP_DEFCG(int, int, UBYTE *);
	int	SP_GTPCG(int, int, UBYTE *);
	int	SP_REGST(int, int, int, int, int);
	int	SP_REGGT(int, int *, int *, int *, int *);
	int	BGSCRLST(int, int, int);
	int	BGSCRLGT(int, int *, int *);
	int	BGCTRLST(int, int, int);
	int	BGCTRLGT(int);
	int	BGTEXTCL(int, int);
	int	BGTEXTST(int, int, int, int);
	int	BGTEXTGT(int, int, int);
	int	SPALET(int, int, int);
//	void	TXXLINE(struct XLINEPTR *);
//	void	TXYLINE(struct YLINEPTR *);
	void	TXBOX(struct TBOXPTR *);
	void	TXFILL(struct TXFILLPTR *);
	void	TXREV(struct TREVPTR *);
//	void	TXRASCPY(int, int, int);
	void	ABORTRST(void);
	void	IPLERR(void);
	void	ABORTJOB(void);
	// V2.11追加
	void	B_CURMOD(int);
	void	B_CURPAT(int);
	void	B_CURPAT1(void);
	void	B_CURDEF(UBYTE *);
	void	B_SCROLL(int);
	
	int	DRAWMODE(int);
	
	void	TXLINE(struct TLINEPTR);
	
	void	CACHE_MD(int);
	int	CACHE_ST(void);
	int	MPU_STAT(void);


#ifdef	__cplusplus
}
#endif

#endif		// __GNUC__

#endif		// ___AIG_IOCSLIBM___
