/*	
 *	clipsti.h
 *
 *	P/ECE STIC Emulator
 *
 *	CLiP - Common Library for P/ECE
 *	Copyright (C) 2001-2005 Naoyuki Sawa
 *
 *	* Wed May 18 20:00:00 JST 2005 Naoyuki Sawa
 *	- 嶌惉奐巒丅
 */
#ifndef __CLIP_STI_H__
#define __CLIP_STI_H__

/* ============
 * STIC偵偮偄偰
 * ============
 * - STIC偼丄Standard Television Interface Chip偺棯偱偡丅
 * - STIC偼丄懳墳婯奿偺堘偄偵傛傝丄擇偮偺昳庬偑偁傝傑偡丅
 *   丒AY-3-8900   4.000   MHz (CCIR婯奿)
 *   丒AY-3-8900-1 3.579545MHz (NTSC婯奿)
 *   Intellivision偵搵嵹偝傟偰偄傞偺偼丄AY-3-8900-1偱偡丅
 *   怣崋僞僀儈儞僌埲奜偺巇條偼丄偳偪傜傕摨偠傒偨偄偱偡丅
 */

/* ===============================================
 * Intellivision僴儞僪僐儞僩儘乕儔擖椡偺價僢僩掕媊
 * ===============================================
 * - 塃偺僴儞僪僐儞僩儘乕儔偑AY-3-8914偺億乕僩A($01FE)丄
 *   嵍偺僴儞僪僐儞僩儘乕儔偑AY-3-8914偺億乕僩B($01FF)偵愙懕偝傟偰偄傑偡丅
 * - 擖椡偼晧榑棟偱丄埲壓偺掕媊偺NOT抣偑幚嵺偺擖椡偲側傝傑偡丅
 * - 杮摉偼丄僴儞僪僐儞僩儘乕儔擖椡偼STIC偲偼娭學側偄偺偱偡偗傟偳丄
 *   懠偵掕媊偡傞応強偑柍偄偺偱丄偲傝偁偊偢偙偙偱掕媊偡傞偙偲偵偟傑偟偨丅
 *
 *	  +---- Top Action (same) ----+
 *	  |                           |
 *	  |     +---------------+     |
 *	  |   +-| +---+---+---+ |-+   |
 *	  +-> | | | 1 | 2 | 3 | | | <-+ 
 *	      +-| +---+---+---+ |-+     
 *	        | | 4 | 5 | 6 | |       
 *	        | +---+---+---+ |       
 *	        | | 7 | 8 | 9 | |       
 *	      +-| +---+---+---+ |-+     R
 *	L --> | | | C | 0 | E | | | <-- i
 *	e     +-| +---+---+---+ |-+     g
 *	f       +---------------+       h
 *	t       |    +-----+    |       t
 *	        |  +-+     +-+  |       
 *	A       | ++         ++ |       A
 *	c       | |           | |       c
 *	t       | ++         ++ |       t
 *	i       |  +-+     +-+  |       i
 *	o       |    +-----+    |       o
 *	n       +---------------+       n
 */
#define INTV_KEY_DOWN		((1<<0)              )
#define INTV_KEY_UP		((1<<2)              )
#define INTV_KEY_RIGHT		(       (1<<1)       )
#define INTV_KEY_LEFT		(       (1<<3)       )
#define INTV_KEY_DOWN_RIGHT	((1<<0)|(1<<1)|(1<<4))
#define INTV_KEY_DOWN_LEFT	((1<<0)|(1<<3)|(1<<4))
#define INTV_KEY_UP_RIGHT	((1<<2)|(1<<1)|(1<<4))
#define INTV_KEY_UP_LEFT	((1<<2)|(1<<3)|(1<<4))
//
#define INTV_KEY_3		((1<<0)|(1<<5))
#define INTV_KEY_2		((1<<0)|(1<<6))
#define INTV_KEY_1		((1<<0)|(1<<7))
#define INTV_KEY_6		((1<<1)|(1<<5))
#define INTV_KEY_5		((1<<1)|(1<<6))
#define INTV_KEY_4		((1<<1)|(1<<7))
#define INTV_KEY_9		((1<<2)|(1<<5))
#define INTV_KEY_8		((1<<2)|(1<<6))
#define INTV_KEY_7		((1<<2)|(1<<7))
#define INTV_KEY_ENTER		((1<<3)|(1<<5))
#define INTV_KEY_0		((1<<3)|(1<<6))
#define INTV_KEY_CLEAR		((1<<3)|(1<<7))
//
#define INTV_KEY_ACTION_TOP	((1<<5)|       (1<<7))
#define INTV_KEY_ACTION_LEFT	((1<<5)|(1<<6)       )
#define INTV_KEY_ACTION_RIGHT	(       (1<<6)|(1<<7))

/****************************************************************************
 *
 ****************************************************************************/

/* STIC_ASM僔儞儃儖掕媊傪撉傒崬傒傑偡丅
 * STIC_ASM僔儞儃儖偺枹掕媊/掕媊偵傛傞C/傾僙儞僽儔僐乕僪偺愗傝懼偊偼丄
 * C僜乕僗僼傽僀儖(*.c)偲傾僙儞僽儔僜乕僗僼傽僀儖(*.s)偐傜嶲徠偡傞偺偱丄
 * 暿偺僀儞僋儖乕僪僼傽僀儖偵暘偗偰偄傑偡丅
 * (C尵岅摿桳偺掕媊傪娷傓僀儞僋儖乕僪僼傽僀儖傪偦偺傑傑傾僙儞僽儔僜乕僗偵
 *  僀儞僋儖乕僪偡傞偲丄傾僙儞僽儖僄儔乕偵側偭偰偟傑偆偨傔丅)
 */
#include "clipstia.h"

/********** 偙傟埲崀偼彂偒姺偊側偄偱偔偩偝偄 **********/

/* P/ECE埲奜偱偼傾僙儞僽儔僐乕僪偼巊梡偱偒傑偣傫丅
 */
#ifndef PIECE
#undef STIC_ASM
#endif /*PIECE*/

/****************************************************************************
 *	
 ****************************************************************************/

#define STIC_CLOCK	NTSC_COLOR_SUBCARRIER
#define INTV_CLOCK	(STIC_CLOCK/4)	/* Intellivision CPU 儅僔儞僒僀僋儖 */

#ifdef PIECE
#define STIC_GRAYSCALE
#endif /*PIECE*/

/* 仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛 */
/* 仛傾僙儞僽儔儖乕僠儞偐傜捈愙嶲徠偟偰偄傞偺偱丄僼僅乕儅僢僩屌掕偱偡丅仛 */
/* 仛僼僅乕儅僢僩傪曄峏偟偨偲偒偼丄clipstia.h偺掕媊傕捛廬偟偰偔偩偝偄丅仛 */
/* 仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛仛 */

typedef struct _STICMOB {
	unsigned char flags;		/* + 0,1: {PRIO,VISB,INTR,YRES,XFLIP,YFLIP} */
	unsigned char color;		/* + 1,1: FG Color */
	unsigned char x_size;		/* + 2,1: XSIZE */
	unsigned char y_size;		/* + 3,1: YSIZ2,YSIZ4 */
	short x;			/* + 4,2: X Coordinate */
	short y;			/* + 6,2: Y Coordinate */
	short w;			/* + 8,2: {8,16} */
	short h;			/* +10,2: {4,8,16,32,64} */
	unsigned short card;		/* +12,2: GRAM/GROM Card # */
	unsigned short collision;	/* +14,2: COLL BORD,BG,MOB7-0 */
} STICMOB;				/* =16 */

/* STICMOB.flags */
#define STICMOB_PRIORITY	(1<<0)	/* PRIO */
#define STICMOB_VISIBLE		(1<<1)	/* VISB */
#define STICMOB_INTERACT	(1<<2)	/* INTR */
#define STICMOB_RESOLUTION	(1<<3)	/* YRES */
#define STICMOB_X_FLIP		(1<<4)	/* XFLIP */
#define STICMOB_Y_FLIP		(1<<5)	/* YFLIP */

typedef struct _STIC {
	const unsigned short* backtab/*[$0200-$02EF]*/;
	const unsigned char* graphics/*[$3000-$3AFF]*/;
	unsigned char* vbuff/*[160*96]*/;
	//
	unsigned char flags;		/* {STIC_*} */
	unsigned char x_delay;		/* 0..7 */
	unsigned char y_delay;		/* 0..7 */
	unsigned char border_color;
	unsigned char color_stack[4];
	STICMOB mob[8];
} STIC;

/* STIC.flags */
#define STIC_MODE			(1<<0)	/* 0 = Color Stack & Colored Squares Mode, 1 = Foreground/Background Mode */
#define STIC_BORDER_EXTENSION_LEFT	(1<<1)	/* Border extension left edge */
#define STIC_BORDER_EXTENSION_TOP	(1<<2)	/* Border extension top edge */

/****************************************************************************
 *	僌儘乕僶儖曄悢
 ****************************************************************************/

extern STIC stic;

#ifdef STIC_GRAYSCALE
extern const unsigned char stic_grayscale_table[16/*Color*/];
#else /*STIC_GRAYSCALE*/
extern const unsigned char stic_color_table[16/*Color*/][3/*R,G,B*/];
#endif /*STIC_GRAYSCALE*/

/****************************************************************************
 *	撪晹娭悢
 ****************************************************************************/

void stic_fill_background_card(int x, int y, int color);
void stic_draw_background_card(int x, int y, int card, int color);
void stic_draw_colored_squares(int x, int y, const char colors[/*4*/]);
void stic_draw_mob(const STICMOB* mob);
void stic_make_mob_graphics(const STICMOB* mob, unsigned short graphics[/*64*/]);
void stic_make_background_card_mask(int card, unsigned short mask[/*8*/]);
void stic_make_colored_squares_mask(const char colors[/*4*/], unsigned short mask[/*8*/]);
int stic_collision_graphics(int x1, int y1, const unsigned short* g1/*[64]*/, int x2, int y2, const unsigned short* g2/*[64]*/);
void stic_collision();

/****************************************************************************
 *	傾僾儕働乕僔儑儞梡娭悢
 ****************************************************************************/

void stic_reset(const unsigned short backtab[/*$0200-$02EF*/], const unsigned char graphics[/*$3000-$3AFF*/]);
int stic_read(int addr);
void stic_write(int addr, int data);
void stic_update(unsigned char vbuff[/*160*96*/]);
#ifdef STIC_GRAYSCALE
void stic_reduce(unsigned char dst[/*128*88*/], const unsigned char src[/*160*96*/]);
#endif /*STIC_GRAYSCALE*/

#endif /*__CLIP_STI_H__*/
