/************************************************************************/
/* TITLE	: TVGRAF.C						*/
/* Function	: Process TV-GRAPHICS PACKS				*/
/*									*/
/* Project #	: JAGUAR CD+G						*/
/* Programmer	: Pradip K Fatehpuria	11/10/93			*/
/*									*/
/* COPYRIGHT(c) 1992,1993 Atari Computer Corporation			*/
/* UNATHORIZED REPRODUCTION, ADAPTATION, DISTRIBUTION, PERFORMANCE OR	*/
/* DISPLAY OF THIS COMPUTER PROGRAM OR THE ASSOCIATED AUDIOVISUAL WORK	*/
/* IS STRICTLY PROHIBITED.						*/
/* ALL RIGHTS RESERVED.							*/
/************************************************************************/

#include	"defs.h"
#include	"packbind.h"

/* -------------------------------------------------------------------- */

#define	INST_TV_PRESETMEMORY	0x0001
#define	INST_TV_PRESETBORDER	0x0002
#define	INST_TV_FONTFGBG	0x0006
#define	INST_TV_SCROLLPRESET	0x0014
#define	INST_TV_SCROLLCOPY	0x0018
#define	INST_TV_COLRTRANS	0x001C
#define	INST_TV_LOADCLUT0	0x001E
#define	INST_TV_LOADCLUT1	0x001F
#define	INST_TV_EXORFONT1	0x0026

/* -------------------------------------------------------------------- */

extern	unsigned short	*TvColTbl;
extern	unsigned short	*fntdata;

extern	short	TvChnlNo;

extern	short	BldObFlag;

extern	unsigned short	*CurPack;

extern	short	CDGType;

extern	short	XORFlag;

extern	unsigned short	ScrlColr;

extern	short	tvPH;
extern	short	tvPV;

extern	short	tvCOPH;
extern	short	tvCOPV;

extern	short	ScrlType;

extern	short	BrdColNo;

/* -------------------------------------------------------------------- */

extern	unsigned char	mess[];

/* -------------------------------------------------------------------- */

static	void	opntvgrf( short, short );

static	void	tvsetmem( void );
static	void	tvsetbrd( void );
static	void	tvfntfbg( void );
static	void	tvxorfnt( void );
static	void	tvscrlst( void );
static	void	tvscrlcp( void );
static	void	tvldclt0( void );
static	void	tvldclt1( void );
static	void	tvcoltrn( void );

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

/* -----------------------------------------------------------------------
 * Process TV-GRAPHICS instructions
 */

void	proctv()
{
static	unsigned short	func_items[] =
	{
	INST_TV_PRESETMEMORY	,
	INST_TV_PRESETBORDER	,
	INST_TV_FONTFGBG	,
	INST_TV_EXORFONT1	,
	INST_TV_SCROLLPRESET	,
	INST_TV_SCROLLCOPY	,
	INST_TV_LOADCLUT0	,
	INST_TV_LOADCLUT1	,
	INST_TV_COLRTRANS	,
	-1
	};
static	void	(*func_table[])( void ) =
	{
	tvsetmem,
	tvsetbrd,
	tvfntfbg,
	tvxorfnt,
	tvscrlst,
	tvscrlcp,
	tvldclt0,
	tvldclt1,
	tvcoltrn
	};

	procfunc(CurPack[1], func_items, func_table);
	return;
}

/* -----------------------------------------------------------------------
 */

static	void	opntvgrf(type, colno)
short	type;
register short	colno;
{
	if (CDGType & TYPE_TV_GRAPHICS)
		return;

	if (type)
	{
		defcolor();
		clrtvdsp(colno);
		clrtvgrf(colno);
	}
	settvgrf(TRUE);
	BldObFlag = TRUE;
	CDGType |= TYPE_TV_GRAPHICS;
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode PRESET-MEMORY instruction (1)
 */

static	void	tvsetmem()
{
register unsigned short	*pack = CurPack;
register short	colno = pack[4];

	colno |= (((pack[4] << 8) & 0x0F00) | (pack[4] & 0x000F));

	tvPH = 0;
	tvPV = 0;
	opntvgrf(TRUE, colno);
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode PRESET-BORDER instruction (2)
 */

static	void	tvsetbrd()
{
	BrdColNo = CurPack[0];

/* first build 32 bit colour from the 16 bit colour info available */
/* @@@@@ */

/* and then call a routine to set the border colour */
/* @@@@@ */

	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode WRITE-FONT FOREGROUND/BACKGROUND instruction (6)
 */

static	void	tvfntfbg()
{
register unsigned short	*pack = CurPack;
register unsigned char	*dp = (unsigned char *)fntdata;
register unsigned char	fg_col;
register unsigned char	bg_col;
register unsigned short	d;
register short	chno;
register short	x, y;
register short	i;

	opntvgrf(FALSE, 0);

	chno = (((pack[4] >> 2) & 0x00C0) | ((pack[5] >> 4) & 0x0003));
	if ((chno > 1) && (chno != TvChnlNo))
		return;

	bg_col = (pack[4] & 0x000F);
	fg_col = (pack[5] & 0x000F);

	x = (pack[7] & 0x003F);
	y = (pack[6] & 0x001F);

	for (i = 8 ; i < 20 ; i++)
	{
		d = pack[i];

		*dp++ = ((d & 0x20) ? fg_col : bg_col);
		*dp++ = ((d & 0x10) ? fg_col : bg_col);
		*dp++ = ((d & 0x08) ? fg_col : bg_col);
		*dp++ = ((d & 0x04) ? fg_col : bg_col);
		*dp++ = ((d & 0x02) ? fg_col : bg_col);
		*dp++ = ((d & 0x01) ? fg_col : bg_col);
	}
	bltfnt08(x,y,0);
	cp2tvdsp();
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode EXCLUSIVE-OR FONT instruction (38)
 */

static	void	tvxorfnt()
{
register unsigned short	*pack = CurPack;
register unsigned char	*dp = (unsigned char *)fntdata;
register unsigned char	fg_col;
register unsigned char	bg_col;
register unsigned short	d;
register short	chno;
register short	x, y;
register short	i;

	opntvgrf(FALSE, 0);

	chno = (((pack[4] >> 2) & 0x00C0) | ((pack[5] >> 4) & 0x0003));
	if ((chno > 1) && (chno != TvChnlNo))
		return;

	bg_col = (pack[4] & 0x000F);
	fg_col = (pack[5] & 0x000F);

	x = (pack[7] & 0x003F);
	y = (pack[6] & 0x001F);

	for (i = 8 ; i < 20 ; i++)
	{
		d = pack[i];

		*dp++ = ((d & 0x20) ? fg_col : bg_col);
		*dp++ = ((d & 0x10) ? fg_col : bg_col);
		*dp++ = ((d & 0x08) ? fg_col : bg_col);
		*dp++ = ((d & 0x04) ? fg_col : bg_col);
		*dp++ = ((d & 0x02) ? fg_col : bg_col);
		*dp++ = ((d & 0x01) ? fg_col : bg_col);
	}
	XORFlag = TRUE;

	bltfnt08(x,y,0);

	XORFlag = FALSE;
	cp2tvdsp();
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode SCROLL SCREEN WITH PRESET instruction (20)
 */

static	void	tvscrlst()
{
register unsigned short	*pack = CurPack;
register short	coph, copv;

	opntvgrf(FALSE, 0);

	ScrlColr = (((pack[4] << 8) & 0x0F00) | (pack[4] & 0x000F));

	tvPH = (pack[5] & 0x0007);
	tvPV = (pack[6] & 0x000F);

	coph = (pack[5] & 0x0030);
	copv = (pack[6] & 0x0030);

	tvCOPH = ((coph == COPH_RIGHT) ? 6 : ((coph == COPH_LEFT) ? -6 : 0));
	tvCOPV = ((copv == COPV_DOWN) ? 12 : ((copv == COPV_UP) ? -12 : 0));

	cp2tvdsp();

	tvCOPH = 0;
	tvCOPV = 0;
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode SCROLL SCREEN WITH COPY instruction (24)
 */

static	void	tvscrlcp()
{
register unsigned short	*pack = CurPack;
register short	coph, copv;

	opntvgrf(FALSE, 0);

	tvPH = (pack[5] & 0x0007);
	tvPV = (pack[6] & 0x000F);

	coph = (pack[5] & 0x0030);
	copv = (pack[6] & 0x0030);

	tvCOPH = ((coph == COPH_RIGHT) ? 6 : ((coph == COPH_LEFT) ? -6 : 0));
	tvCOPV = ((copv == COPV_DOWN) ? 12 : ((copv == COPV_UP) ? -12 : 0));
	ScrlType = TRUE;

	cp2tvdsp();

	ScrlType = FALSE;
	tvCOPH = 0;
	tvCOPV = 0;
	return;
}

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode	LOAD CLUT Colour-0..7 instruction (30)
 *			LOAD CLUT Colour-8..15 instruction (31)
 */

static	void	tvldclt0()	{ ldclut1(0);	return; }
static	void	tvldclt1()	{ ldclut1(8);	return; }

/* -----------------------------------------------------------------------
 * The TV-GRAPHICS mode DEFINE COLOUR TRANSPARENCY instruction (28)
 */

static	void	tvcoltrn()	{ ldtrans();	return; }

/************************************************************************/
/*	END OF FILE							*/
/************************************************************************/
