/********************************************************************/
/* Kamui function test program										*/
/* Test of Bump map texture											*/
/********************************************************************/
/*   COPYRIGHT (C) NEC Corporation 1998                             */
/*   All rights reserved by NEC Corporation. This program must be   */
/*   used solely for the purpose for which it was furnished by NEC  */
/*   Corporation. No part of this program may be reproduced or      */
/*   disclosed to others, in any form, without the prior written    */
/*   permission of NEC Corporation.                                 */
/*   Use of copyright notice does not evidence publication of this  */
/*   program.                                                       */
/*                                                                  */
/*   Author   : Takeshi Muto / NEC Software, Ltd.                   */
/*   Created  : April 28 1998 by NEC Software, Ltd.                 */
/*   Modified :                                                     */
/********************************************************************/

#include	"kamui.h"				/* for Kamui Call */
#include	"dispstr.h"				/* for String Screen */

#define		_DISPLAY_STRING_
/* if defined then display strings on screen */

/* Size of vertex buffer. */
#define VBUFFER_SIZE    8*3*2000

extern const unsigned char b256_01[];
extern const unsigned char vllogo[];


/****************************************************************************/
/**	Valuables for KAMUI *****************************************************/
/****************************************************************************/
	KMSTATUS			status;
	KMVERSIONINFO		VersionInfo;			/* KAMUI Version information */
	KMSURFACEDESC		PrimarySurfaceDesc;		/* Primary frame buffer */
	KMSURFACEDESC		BackSurfaceDesc;		/* Back-buffer frame buffer */
	KMSURFACEDESC		TexSurfaceDesc1;		/* texture surface */
	KMSURFACEDESC		TexSurfaceDesc2;		/* texture surface */
	KMVERTEXBUFFDESC	VertexBufferDesc;		/* Vertex-buffer */
	KMVERTEXCONTEXT		VertexContextB1;		/* Vertex context for BUMP */
	KMVERTEXCONTEXT		VertexContextB2;		/* Vertex context for BUMP */
	KMVERTEXCONTEXT		VertexContextB3;		/* Vertex context for BUMP */
	KMVERTEXCONTEXT		VertexContextB4;		/* Vertex context for BUMP */
	KMVERTEXCONTEXT		VertexContextP1;		/* Vertex context for Twiddled RGB565 */
	KMVERTEXCONTEXT		VertexContextP2;		/* Vertex context for Twiddled RGB565 */
	KMVERTEXCONTEXT		VertexContextP3;		/* Vertex context for Twiddled RGB565 */
	KMVERTEXCONTEXT		VertexContextP4;		/* Vertex context for Twiddled RGB565 */
	KMVERTEX_03			V[4];					/* Vertex */
	PKMDWORD			pTexture01;				/* Pointer to texture */
	KMTEXTURETYPE		nTextureType;			/* texture type definition */

	unsigned char		TexWork[131072 + 16];		/* work area for kmGetTexture */

/* System Configuration */

PKMSURFACEDESC          psurface[2];            /* array of pointers to surfaces */
KMPACKEDARGB            border;

KMSYSTEMCONFIGSTRUCT kmsc = {
		sizeof(KMSYSTEMCONFIGSTRUCT),
		KM_CONFIGFLAG_ENABLE_CLEAR_FRAMEBUFFER,	/* set any special flags = clear frame buffer */
        psurface,                               /* pointer to array of surface pointers */
        2,                                      /* number of buffers = 2 */
        640,480,                                /* width and height of surface */
        KM_DSPBPP_RGB565,                       /* bits per pixel */
        0x100000,                               /* allocate amount of VRAM for texture = 4MB */
        &VertexBufferDesc,                      /* pointer to vertex buffer descriptor */
        0,                                      /* pointer to vertex buffer - ** malloc now on application side */
		2*VBUFFER_SIZE,							/* size in DWORDS of vertex buffer - must be multiple of 32 */
        {50.f,0.f,50.f,0.f,0.f},                /* percentage of vbuf to use for each type of vertex */
        0,0,0,0,0,0,0,                          /* reserved */
        KM_VERTEXBUFMODEL_NORMAL                /* latency model = 3VB */
	};

/****************************************************************************/
/**	Check KAMUI function status *********************************************/
/****************************************************************************/
void	Check_Status(KMSTATUS status, char * func)
{
	switch(status) {
	case KMSTATUS_SUCCESS:
		/* No message is displayed when SUCCESS */
		break;
	case KMSTATUS_INVALID_DISPLAY_MODE:
		PRINTOUT(("%s:KMSTATUS_INVALID_DISPLAY_MODE\n",func)); break;
	case KMSTATUS_INVALID_VIDEO_MODE:
		PRINTOUT(("%s:KMSTATUS_INVALID_VIDEO_MODE\n",func)); break;
	case KMSTATUS_NOT_ENOUGH_MEMORY:
		PRINTOUT(("%s:KMSTATUS_NOT_ENOUGH_MEMORY\n",func)); break;
	case KMSTATUS_INVALID_TEXTURE_TYPE:
		PRINTOUT(("%s:KMSTATUS_INVALID_TEXTURE_TYPE\n",func)); break;
	case KMSTATUS_INVALID_MIPMAPED_TEXTURE:
		PRINTOUT(("%s:KMSTATUS_INVALID_MIPMAPED_TEXTURE\n",func)); break;
	case KMSTATUS_CANT_FLIP_SURFACE:
		PRINTOUT(("%s:KMSTATUS_CANT_FLIP_SURFACE\n",func)); break;
	case KMSTATUS_INVALID_ADDRESS:
		PRINTOUT(("%s:KMSTATUS_INVALID_ADDRESS\n",func)); break;
	case KMSTATUS_HARDWARE_NOT_PRESENTED:
		PRINTOUT(("%s:KMSTATUS_HARDWARE_NOT_PRESENTED\n",func)); break;
	case KMSTATUS_INVALID_SETTING:
		PRINTOUT(("%s:KMSTATUS_INVALID_SETTING\n",func)); break;
	case KMSTATUS_INVALID_VERTEX_TYPE:
		PRINTOUT(("%s:KMSTATUS_INVALID_VERTEX_TYPE\n",func)); break;
	case KMSTATUS_NOT_IMPLEMENTED:
		PRINTOUT(("%s:KMSTATUS_NOT_IMPREMENTED\n",func)); break;
	case KMSTATUS_INVALID_PARAMETER:
		PRINTOUT(("%s:KMSTATUS_INVALID_PARAMETER\n",func)); break;
	default:
		PRINTOUT(("%s:???Undefined status : %d\n", func, status));
		break;
	}
}

/****************************************************************************/
/**	Compare byte data *******************************************************/
/****************************************************************************/
int	comparebyte(unsigned char * pData1, unsigned char * pData2, int len)
{
	int	result = 0;
	int	i;
	unsigned char	data1,data2;

	for(i = 0; i < len; i++) {
		data1 = *(pData1 + i);
		data2 = *(pData2 + i);
		if(data1 != data2) result++;
	}
	return	result;
}

/****************************************************************************/
/**	Initialize Vertex Context ***********************************************/
/****************************************************************************/
void	InitVertexContext(PKMVERTEXCONTEXT pVertexContext)
{
	/* 頂点CONTEXTの生成 */
	pVertexContext->RenderState	= KM_DEPTHMODE  		| KM_CULLINGMODE				/* ISP/TSP IW */
								| KM_SCREENCOORDINATION	| KM_SHADINGMODE
								| KM_MODIFIER 			| KM_ZWRITEDISABLE				/* TSP CW */
								| KM_SRCBLENDINGMODE	| KM_DSTBLENDINGMODE
								| KM_SRCSELECT			| KM_DSTSELECT
								| KM_FOGMODE			| KM_USESPECULAR
								| KM_USEALPHA			| KM_IGNORETEXTUREALPHA
								| KM_FLIPUV 			| KM_CLAMPUV
								| KM_FILTERMODE			| KM_SUPERSAMPLE
								| KM_MIPMAPDADJUST 		| KM_TEXTURESHADINGMODE
								| KM_PARAMTYPE 			| KM_LISTTYPE 					/* Param CW */
								| KM_COLORTYPE			| KM_USESPECULAR
								| KM_UVFORMAT;

	/* for ParameterControlWord */
	pVertexContext->ParamType			= KM_POLYGON;
	pVertexContext->ListType			= KM_OPAQUE_POLYGON;
/*	pVertexContext->ColorType			= KM_FLOATINGCOLOR;*/
	pVertexContext->ColorType			= KM_PACKEDCOLOR;
	pVertexContext->bUseSpecular		= FALSE;
	pVertexContext->UVFormat			= KM_32BITUV;

	/* for ISP/TSP InstructionWord */
	pVertexContext->DepthMode			= KM_GREATER;
	pVertexContext->CullingMode			= KM_NOCULLING;
	pVertexContext->ScreenCoordination	= KM_SCREEN;
	pVertexContext->ShadingMode			= KM_NOTEXTUREGOURAUD;
	pVertexContext->SelectModifier		= KM_NOMODIFIER;
	pVertexContext->bZWriteDisable		= FALSE;			/* ARC1にはない */

	/* for TSP ControlWord */
	pVertexContext->SRCBlendingMode		= KM_ONE;
	pVertexContext->DSTBlendingMode		= KM_ZERO;
	pVertexContext->bSRCSel				= FALSE;			/* ARC1にはない */
	pVertexContext->bDSTSel				= FALSE;			/* ARC1にはない */
	pVertexContext->FogMode				= KM_NOFOG;			/* 11はリザーブ */
	pVertexContext->bUseSpecular		= FALSE;
	pVertexContext->bUseAlpha			= FALSE;
	pVertexContext->FlipUV				= KM_NOFLIP;
	pVertexContext->ClampUV				= KM_NOCLAMP;
	pVertexContext->FilterMode			= KM_POINT_SAMPLE;
	pVertexContext->bSuperSample		= FALSE;
	pVertexContext->MipMapAdjust		= 0x00000004;		/* D=1.00 */
	pVertexContext->TextureShadingMode	= KM_MODULATE;

	/* for Texture ControlBit */

	/* No effect on ARC1 */
	pVertexContext->bColorClamp		= FALSE;
	pVertexContext->PaletteBank		= 0;

	/* for GlobalColorData */
	pVertexContext->fFaceColorAlpha		= 1.0f;		/* Face Color Alpha */
	pVertexContext->fFaceColorRed		= 1.0f;		/* Face Color Red */
	pVertexContext->fFaceColorGreen		= 1.0f;		/* Face Color Green */
	pVertexContext->fFaceColorBlue		= 1.0f;		/* Face Color Blue */
	pVertexContext->fOffsetColorAlpha	= 1.0f;		/* Specular Color Alpha */
	pVertexContext->fOffsetColorRed		= 1.0f;		/* Specular Color Red */
	pVertexContext->fOffsetColorGreen	= 1.0f;		/* Specular Color Green */
	pVertexContext->fOffsetColorBlue	= 1.0f;		/* Specular Color Blue */

	kmProcessVertexRenderState(pVertexContext);
}

/****************************************************************************/
/**	Background surface setting **********************************************/
/****************************************************************************/
void	InitBackGround(void)
{
	KMSTATUS			status;
	KMVERTEXCONTEXT		BackVertexContext;		/* Vertex context */
	KMVERTEX_01			BV[3];					/* pointer to Vertex */
	PVOID				pBV[3];					/* pointer to Vertex */

	/* Clear VertexContext */

	memset(&BackVertexContext,0,sizeof(KMVERTEXCONTEXT));


	/* Setup VertexContext */
	BackVertexContext.RenderState	= KM_DEPTHMODE  		| KM_CULLINGMODE				/* ISP/TSP IW */
									| KM_SCREENCOORDINATION	| KM_SHADINGMODE
									| KM_MODIFIER 			| KM_ZWRITEDISABLE				/* TSP CW */
									| KM_SRCBLENDINGMODE
									| KM_SRCSELECT			| KM_DSTSELECT
									| KM_FOGMODE			| KM_USESPECULAR
									| KM_USEALPHA			| KM_IGNORETEXTUREALPHA
									| KM_FLIPUV 			| KM_CLAMPUV
									| KM_FILTERMODE			| KM_SUPERSAMPLE
									| KM_MIPMAPDADJUST 		| KM_TEXTURESHADINGMODE
									| KM_PARAMTYPE 			| KM_LISTTYPE 					/* Param CW */
									| KM_COLORTYPE			| KM_USESPECULAR
									| KM_UVFORMAT;

	/* for ParameterControlWord */
	BackVertexContext.ParamType				= KM_POLYGON;
	BackVertexContext.ListType				= KM_OPAQUE_POLYGON;
	BackVertexContext.ColorType				= KM_FLOATINGCOLOR;
	BackVertexContext.bUseSpecular			= FALSE;
	BackVertexContext.UVFormat				= KM_32BITUV;

	/* for ISP/TSP InstructionWord */
	BackVertexContext.DepthMode				= KM_GREATER;
	BackVertexContext.CullingMode			= KM_NOCULLING;
	BackVertexContext.ScreenCoordination	= KM_SCREEN;
	BackVertexContext.ShadingMode			= KM_NOTEXTUREGOURAUD;
	BackVertexContext.SelectModifier		= KM_NOMODIFIER;
	BackVertexContext.bZWriteDisable		= FALSE;			/* not effective on ARC1 */

	/* for TSP ControlWord */
	BackVertexContext.SRCBlendingMode		= KM_ONE;
/*--
//	BackVertexContext.DSTBlendingMode		= KM_ZERO;	// must not set this parameter on background plane
--*/
	BackVertexContext.bSRCSel				= FALSE;			/* not effective on ARC1 */
	BackVertexContext.bDSTSel				= FALSE;			/* not effective on ARC1 */
	BackVertexContext.FogMode				= KM_NOFOG;			/* 11 is reserved */
	BackVertexContext.bUseSpecular			= FALSE;
	BackVertexContext.bUseAlpha				= FALSE;
	BackVertexContext.ClampUV				= KM_NOCLAMP;
	BackVertexContext.FilterMode			= KM_POINT_SAMPLE;
	BackVertexContext.bSuperSample			= FALSE;
	BackVertexContext.MipMapAdjust			= 0x00000004;		/* D=1.00 */
	BackVertexContext.TextureShadingMode	= KM_MODULATE;

	/* for Texture ControlBit */

	/* No effect on ARC1 */
	BackVertexContext.bColorClamp			= FALSE;
	BackVertexContext.PaletteBank			= 0;

	/* for GlobalColorData */
	BackVertexContext.fFaceColorAlpha		= 1.0f;		/* Face Color Alpha */
	BackVertexContext.fFaceColorRed			= 1.0f;		/* Face Color Red */
	BackVertexContext.fFaceColorGreen		= 1.0f;		/* Face Color Green */
	BackVertexContext.fFaceColorBlue		= 1.0f;		/* Face Color Blue */
	BackVertexContext.fOffsetColorAlpha		= 1.0f;		/* Specular Color Alpha */
	BackVertexContext.fOffsetColorRed		= 1.0f;		/* Specular Color Red */
	BackVertexContext.fOffsetColorGreen		= 1.0f;		/* Specular Color Green */
	BackVertexContext.fOffsetColorBlue		= 1.0f;		/* Specular Color Blue */

	status = kmProcessVertexRenderState(&BackVertexContext);
	Check_Status(status, "kmProcessVertexRenderState");
	status = kmSetBackGroundRenderState(&BackVertexContext);
	Check_Status(status, "kmSetBackGroundRenderState");

	/*-----------------------------------------------------------------------------------*/
	/* set VERTEX DATA */

	/* Vertex #1 */
	BV[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	BV[0].fX				=  2.0f;
	BV[0].fY				= 260.0f;
#if defined(_STRICT_UNION_)
	BV[0].u.fZ				=  0.2f;
#else
	BV[0].fZ				=  0.2f;
#endif
	BV[0].fBaseAlpha		=  1.0f;
	BV[0].fBaseRed			=  0.0f;
	BV[0].fBaseGreen		=  0.0f;
	BV[0].fBaseBlue			=  1.0f;

	/* Vertex #2 */
	BV[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	BV[1].fX				= 150.0f;
	BV[1].fY				=  2.0f;
#if defined(_STRICT_UNION_)
	BV[1].u.fZ				=  0.2f;
#else
	BV[1].fZ				=  0.2f;
#endif
	BV[1].fBaseAlpha		=  1.0f;
	BV[1].fBaseRed			=  1.0f;
	BV[1].fBaseGreen		=  0.0f;
	BV[1].fBaseBlue			=  0.0f;

	/* Vertex #3 */
	BV[2].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
	BV[2].fX				= 560.0f;
	BV[2].fY				= 260.0f;
#if defined(_STRICT_UNION_)
	BV[2].u.fZ				=  0.2f;
#else
	BV[2].fZ				=  0.2f;
#endif
	BV[2].fBaseAlpha		=  1.0f;
	BV[2].fBaseRed			=  0.0f;
	BV[2].fBaseGreen		=  1.0f;
	BV[2].fBaseBlue			=  0.0f;

	pBV[0] = &BV[0];
	pBV[1] = &BV[1];
	pBV[2] = &BV[2];

	/*-----------------------------------------------------------------------------------*/
	/* set VERTEX */
	status = kmSetBackGroundPlane(	pBV,					/* pVertex */
									KM_VERTEXTYPE_01,		/* VertexType */
									sizeof(KMVERTEX_01)		/* StructSize */
									);
	Check_Status(status, "kmSetBackGroundPlane");
}

/****************************************************************************/
/**	Set vertex data to Kamui ***********************************************/
/****************************************************************************/
void	SetVertex(KMVERTEXCONTEXT *VertexContext)
{
	status = kmSetVertexRenderState(VertexContext);
	Check_Status(status, "kmSetVertexRenderState");

	status = kmStartVertexStrip(&VertexBufferDesc);
	Check_Status(status, "kmStartVertexStrip");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V[0],					/* pVertex */
							KM_VERTEXTYPE_03,		/* VertexType */
							sizeof(KMVERTEX_03)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#1)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V[1],					/* pVertex */
							KM_VERTEXTYPE_03,		/* VertexType */
							sizeof(KMVERTEX_03)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#2)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V[2],					/* pVertex */
							KM_VERTEXTYPE_03,		/* VertexType */
							sizeof(KMVERTEX_03)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#3)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V[3],					/* pVertex */
							KM_VERTEXTYPE_03,		/* VertexType */
							sizeof(KMVERTEX_03)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#4)");

}

/****************************************************************************/
/**	Main ********************************************************************/
/****************************************************************************/
int	main(void)
{
	int		i, j;
	int		len, nn;			/* for test of kmGetTexture */
	float	x1, y1, x2, y2;
	int		D_V0Alpha	= 1;
	int		D_V0Red		= 2;
	int		D_V0Green	= 3;
	int		D_V0Blue	= 2;

/****************************************************************************/
/** Vertex Data *************************************************************/
/****************************************************************************/
	/* Vertex #1 */
	V[0].ParamControlWord			= KM_VERTEXPARAM_NORMAL;
	V[0].fX							= 120.0f;
	V[0].fY							= 440.0f;
#if defined(_STRICT_UNION_)
	V[0].u.fZ						=  0.5f;
#else
	V[0].fZ							=  0.5f;
#endif
	V[0].fU							=  0.0f;
	V[0].fV							=  0.0f;
	V[0].uBaseRGB.byte.bAlpha		=  255;
	V[0].uBaseRGB.byte.bRed			=  0;
	V[0].uBaseRGB.byte.bGreen		=  0;
	V[0].uBaseRGB.byte.bBlue		=  0;
	V[0].uOffsetRGB.byte.bAlpha		=  0;	/* K1 for BUMP map : ignored */
	V[0].uOffsetRGB.byte.bRed		=  0;	/* K2 for BUMP map : ignored */
	V[0].uOffsetRGB.byte.bGreen		=  0;	/* K3 for BUMP map : ignored */
	V[0].uOffsetRGB.byte.bBlue		=  0;	/* Q  for BUMP map : ignored */

	/* Vertex #2 */
	V[1].ParamControlWord			= KM_VERTEXPARAM_NORMAL;
	V[1].fX							= 120.0f;
	V[1].fY							=  40.0f;
#if defined(_STRICT_UNION_)
	V[1].u.fZ						=  0.5f;
#else
	V[1].fZ							=  0.5f;
#endif
	V[1].fU							=  0.0f;
	V[1].fV							=  1.0f;
	V[1].uBaseRGB.byte.bAlpha		=  255;
	V[1].uBaseRGB.byte.bRed			=  0;
	V[1].uBaseRGB.byte.bGreen		=  0;
	V[1].uBaseRGB.byte.bBlue		=  0;
	V[1].uOffsetRGB.byte.bAlpha		=  0;	/* K1 for BUMP map : ignored */
	V[1].uOffsetRGB.byte.bRed		=  0;	/* K2 for BUMP map : ignored */
	V[1].uOffsetRGB.byte.bGreen		=  0;	/* K3 for BUMP map : ignored */
	V[1].uOffsetRGB.byte.bBlue		=  0;	/* Q  for BUMP map : ignored */

	/* Vertex #3 */
	V[2].ParamControlWord			= KM_VERTEXPARAM_NORMAL;
	V[2].fX							= 520.0f;
	V[2].fY							= 440.0f;
#if defined(_STRICT_UNION_)
	V[2].u.fZ						=  0.5f;
#else
	V[2].fZ							=  0.5f;
#endif
	V[2].fU							=  1.0f;
	V[2].fV							=  0.0f;
	V[2].uBaseRGB.byte.bAlpha		=  255;
	V[2].uBaseRGB.byte.bRed			=  0;
	V[2].uBaseRGB.byte.bGreen		=  0;
	V[2].uBaseRGB.byte.bBlue		=  0;
	V[2].uOffsetRGB.byte.bAlpha		=  128;	/* K1 for BUMP map */
	V[2].uOffsetRGB.byte.bRed		=   34;	/* K2 for BUMP map */
	V[2].uOffsetRGB.byte.bGreen		=  123;	/* K3 for BUMP map */
	V[2].uOffsetRGB.byte.bBlue		=    0;	/* Q  for BUMP map */

	/* Vertex #4 */
	V[3].ParamControlWord			= KM_VERTEXPARAM_ENDOFSTRIP;
	V[3].fX							= 520.0f;
	V[3].fY							=  40.0f;
#if defined(_STRICT_UNION_)
	V[3].u.fZ						=  0.5f;
#else
	V[3].fZ							=  0.5f;
#endif
	V[3].fU							=  1.0f;
	V[3].fV							=  1.0f;
	V[3].uBaseRGB.byte.bAlpha		=  255;
	V[3].uBaseRGB.byte.bRed			=  0;
	V[3].uBaseRGB.byte.bGreen		=  0;
	V[3].uBaseRGB.byte.bBlue		=  0;
	V[3].uOffsetRGB.byte.bAlpha		=  128;	/* K1 for BUMP map */
	V[3].uOffsetRGB.byte.bRed		=   34;	/* K2 for BUMP map */
	V[3].uOffsetRGB.byte.bGreen		=  123;	/* K3 for BUMP map */
	V[3].uOffsetRGB.byte.bBlue		=    0;	/* Q  for BUMP map */

/****************************************************************************/
/** Kamu Test Main **********************************************************/
/****************************************************************************/

	txt_ClearScreen();	/* Clear text screen */
	PRINTOUT(("** KAMUI TEXTURE TEST 18 : Bump map **\n"));

	/*-----------------------------------------------------------------------------------*/
	/* Initialize Hardware */

    /* Replaced by sbInitSystem. */
    /* status = kmInitDevice(KM_NTSC); */
    /* Check_Status(status, "kmInitDevice"); */

	kmGetVersionInfo(&VersionInfo);
	PRINTOUT(("MajorVersion      0x0%08x\n",VersionInfo.kmMajorVersion));
	PRINTOUT(("LocalVersion      0x0%08x\n",VersionInfo.kmLocalVersion));
	PRINTOUT(("kmFrameBufferSize 0x0%08x\n",VersionInfo.kmFrameBufferSize));

    sbInitSystem (KM_DSPMODE_NTSCNI640x480, KM_DSPBPP_RGB565, 1);
    /* sbInitSystem (KM_DSPMODE_VGA, KM_DSPBPP_RGB565, 1); */

	border.dwPacked	=	0x00000000;
	kmSetBorderColor(border);
    Check_Status(status, "kmSetBorderColor");

    psurface[0]         =   &PrimarySurfaceDesc;
    psurface[1]         =   &BackSurfaceDesc;

    /* generate vertex buffer */
	kmsc.pVertexBuffer	=	(void *)((((int)syMalloc(2*sizeof(KMDWORD)*VBUFFER_SIZE)&0x3FFFFFE0)|0xA0000000)+32);

	/* Set system configuration */
    status  =   kmSetSystemConfiguration(&kmsc);
    Check_Status(status, "kmSetSystemConfiguration");

    /*-----------------------------------------------------------------------------------*/
	/* set display mode */
    /* status = kmSetDisplayMode(  KM_DSPMODE_NTSCNI640x480, */       /* nDisplayMode */
                                /* KM_DSPBPP_RGB565, */  /* nBpp */
                                /* TRUE, */              /* bDither */
                                /* FALSE */              /* bAntiAlias */
                                /* ); */
    /* Check_Status(status, "kmSetDisplayMode"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create vertex buffer */
    /* status = kmCreateVertexBuffer(&VertexBufferDesc, */       /* pBufferDesc */
                                    /* 131072, */                /* Opaque-Polygon 500KB */
                                    /* 0, */                     /* Opaque-Modifier */
                                    /* 131072, */                /* Trans-Polygon 500KB */
                                    /* 0 */                      /* Trans-Modifier */
                                    /* ); */
    /* Check_Status(status, "kmCreateVertexBuffer"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create primary frame buffer surface */
    /* status = kmCreateFrameBufferSurface(&PrimarySurfaceDesc, */   /* pSurfaceDesc */
                                /* 640, */   /* nWidth */
                                /* 480, */   /* nHeight */
                                /* FALSE, */ /* bStripBuffer */
                                /* FALSE */  /* bBufferClear */
                                /* ); */
    /* Check_Status(status, "kmCreateFrameBufferSurface(Primary)"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create backbuffer frame buffer surface */
    /* status = kmCreateFrameBufferSurface(&BackSurfaceDesc, */  /* pSurfaceDesc */
                                /* 640, */   /* nWidth */
                                /* 480, */   /* nHeight */
                                /* FALSE, */ /* bStripBuffer */
                                /* FALSE */  /* bBufferClear */
                                /* ); */
    /* Check_Status(status, "kmCreateFrameBufferSurface(Back-buffer)"); */

	/*-----------------------------------------------------------------------------------*/
	/* set display surface */
    /* status = kmActivateFrameBuffer( &PrimarySurfaceDesc, */   /* pSurfaceDesc */
                                    /* &BackSurfaceDesc, */      /* pSurfaceDesc */
                                    /* FALSE, */                 /* bStripBuffer */
                                    /* TRUE */                   /* bWaitVSync */
                                    /* ); */
    /* Check_Status(status, "kmActivateFrameBuffer"); */

	/*-----------------------------------------------------------------------------------*/
	/* Other initializes */
	txt_InitDisplayString();	/* Initialize string data */
	InitBackGround();			/* set background plane */

	/*-----------------------------------------------------------------------------------*/
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED | KM_TEXTURE_BUMP;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexSurfaceDesc1,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		256,					/*	IN	KMINT32			nWidth */
		256,					/*	IN	KMINT32			nHeight */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface");

	nTextureType = KM_TEXTURE_TWIDDLED | KM_TEXTURE_RGB565;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexSurfaceDesc2,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		256,					/*	IN	KMINT32			nWidth */
		256,					/*	IN	KMINT32			nHeight */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface");

	/*-----------------------------------------------------------------------------------*/
	/* Load and set texture */
	/* BUMP */
	pTexture01 = (unsigned long *)&b256_01;
	status = kmLoadTexture(&TexSurfaceDesc1,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #1");

	/* RGB565 */
	pTexture01 = (unsigned long *)&vllogo;
	status = kmLoadTexture(&TexSurfaceDesc2,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #2");

	/*-----------------------------------------------------------------------------------*/
	/* set VERTEXCONTEXT */

	/********************************/
	/* BUMP polygon (1) Bump Opaque */
	/********************************/
	/* Set Context for BUMP */
	memset( &VertexContextB1, 0, sizeof(VertexContextB1));
	InitVertexContext( &VertexContextB1);
	VertexContextB1.RenderState	= 0;
	/* Set shading mode */
	/* BUMP mapping needs FLAT shading mode */
	VertexContextB1.RenderState	|= KM_SHADINGMODE;
	VertexContextB1.ShadingMode	= KM_TEXTUREFLAT;
	/* Set texture surface */
	VertexContextB1.pTextureSurfaceDesc	= &TexSurfaceDesc1;
	/* Set clamp */
	VertexContextB1.RenderState	|= KM_CLAMPUV;
	VertexContextB1.ClampUV	= KM_CLAMP_UV;
	/* Set texture shading mode */
	VertexContextB1.RenderState	|= KM_TEXTURESHADINGMODE;
	VertexContextB1.TextureShadingMode	= KM_DECAL_ALPHA;
	/* Set offset color mode */
	VertexContextB1.RenderState	|= KM_USESPECULAR;
	VertexContextB1.bUseSpecular	= TRUE;
	/* Process Context */
	status = kmProcessVertexRenderState(&VertexContextB1);
	Check_Status(status, "kmProcessVertexRenderState");

	/*************************************/
	/* BUMP polygon (2) Bump translucent */
	/*************************************/
	/* Set Context for BUMP */
	memset( &VertexContextB2, 0, sizeof(VertexContextB2));
	InitVertexContext( &VertexContextB2);
	VertexContextB2.RenderState	= 0;
	/* Set shading mode */
	/* BUMP mapping needs FLAT shading mode */
	VertexContextB2.RenderState	|= KM_SHADINGMODE;
	VertexContextB2.ShadingMode	= KM_TEXTUREFLAT;
	/* Set texture surface */
	VertexContextB2.pTextureSurfaceDesc	= &TexSurfaceDesc1;
	/* Set clamp */
	VertexContextB2.RenderState	|= KM_CLAMPUV;
	VertexContextB2.ClampUV	= KM_CLAMP_UV;
	/* Set offset color mode */
	VertexContextB2.RenderState	|= KM_USESPECULAR;
	VertexContextB2.bUseSpecular	= TRUE;
	/* Set to translucent */
	VertexContextB2.RenderState	|= KM_SRCBLENDINGMODE;
	VertexContextB2.SRCBlendingMode	= KM_SRCALPHA;
	VertexContextB2.RenderState	|= KM_DSTBLENDINGMODE;
	VertexContextB2.DSTBlendingMode	= KM_INVSRCALPHA;
	VertexContextB2.RenderState	|= KM_LISTTYPE;
	VertexContextB2.ListType  = KM_TRANS_POLYGON;
	VertexContextB2.RenderState	|= KM_USEALPHA;
	VertexContextB2.bUseAlpha = TRUE;
	VertexContextB2.RenderState	|= KM_TEXTURESHADINGMODE;
	VertexContextB2.TextureShadingMode	= KM_MODULATE_ALPHA;
	/* Process Context */
	status = kmProcessVertexRenderState(&VertexContextB2);
	Check_Status(status, "kmProcessVertexRenderState");

	/****************************************/
	/* Non bumped polygon (1) Normal Opaque */
	/****************************************/
	/* Set Context for Texture */
	memset( &VertexContextP1, 0, sizeof(VertexContextP1));
	InitVertexContext( &VertexContextP1);
	VertexContextP1.RenderState	= 0;
	/* Set shading mode */
	VertexContextP1.RenderState	|= KM_SHADINGMODE;
	VertexContextP1.ShadingMode	= KM_TEXTUREFLAT;
	/* Set texture surface */
	VertexContextP1.pTextureSurfaceDesc	= &TexSurfaceDesc2;
	/* Set clamp */
	VertexContextP1.RenderState	|= KM_CLAMPUV;
	VertexContextP1.ClampUV	= KM_CLAMP_UV;
	/* Process Context */
	status = kmProcessVertexRenderState(&VertexContextP1);
	Check_Status(status, "kmProcessVertexRenderState");

	/*********************************************/
	/* Non bumped polygon (2) Normal Translucent */
	/*********************************************/
	/* Set Context for Texture */
	memset( &VertexContextP2, 0, sizeof(VertexContextP2));
	InitVertexContext( &VertexContextP2);
	VertexContextP2.RenderState	= 0;
	/* Set shading mode */
	VertexContextP2.RenderState	|= KM_SHADINGMODE;
	VertexContextP2.ShadingMode	= KM_TEXTUREFLAT;
	/* Set texture surface */
	VertexContextP2.pTextureSurfaceDesc	= &TexSurfaceDesc2;
	/* Set clamp */
	VertexContextP2.RenderState	|= KM_CLAMPUV;
	VertexContextP2.ClampUV	= KM_CLAMP_UV;
	/* Set to translucent */
	VertexContextP2.RenderState	|= KM_SRCBLENDINGMODE;
	VertexContextP2.SRCBlendingMode	= KM_SRCALPHA;
	VertexContextP2.RenderState	|= KM_DSTBLENDINGMODE;
	VertexContextP2.DSTBlendingMode	= KM_INVSRCALPHA;
	VertexContextP2.RenderState	|= KM_LISTTYPE;
	VertexContextP2.ListType  = KM_TRANS_POLYGON;
	VertexContextP2.RenderState	|= KM_USEALPHA;
	VertexContextP2.bUseAlpha = TRUE;
	VertexContextP2.RenderState	|= KM_TEXTURESHADINGMODE;
	VertexContextP2.TextureShadingMode	= KM_MODULATE_ALPHA;
	/* Process Context */
	status = kmProcessVertexRenderState(&VertexContextP2);
	Check_Status(status, "kmProcessVertexRenderState");

	/*-----------------------------------------------------------------------------------*/
	/* test of kmGetTexture */
	status = kmGetTexture((PKMDWORD)(TexWork + 16), &TexSurfaceDesc1);
	Check_Status(status, "kmGetTexture #1");
#if defined(_STRICT_UNION_)
	len = TexSurfaceDesc1.uSize.dwTextureSize;
#else
	len = TexSurfaceDesc1.dwTextureSize;
#endif
	nn  = comparebyte(b256_01 + 16, TexWork + 16, len);
	if(nn == 0) {
		PRINTOUT(("* kmGetTexture : OK.\n"));
	}
	else {
		PRINTOUT(("* kmGetTexture : Error. different is %d\n",nn));
	}

	/*-----------------------------------------------------------------------------------*/
	/* Start rendering loop */
	while(1){

	/*-----------------------------------------------------------------------------------*/
	/* Change Q parameter */
	V[2].uOffsetRGB.byte.bBlue += 1;
	if(V[2].uOffsetRGB.byte.bBlue > 255) {
		V[2].uOffsetRGB.byte.bBlue = 0;
	}
	V[3].uOffsetRGB.byte.bBlue = V[2].uOffsetRGB.byte.bBlue;

	txt_SetCursor(0,5);
	PRINTOUT((" K1 : %4d\n",V[2].uOffsetRGB.byte.bAlpha));
	PRINTOUT((" K2 : %4d\n",V[2].uOffsetRGB.byte.bRed));
	PRINTOUT((" K3 : %4d\n",V[2].uOffsetRGB.byte.bGreen));
	PRINTOUT((" Q  : %4d\n",V[2].uOffsetRGB.byte.bBlue));

	/*********************************/
	/* Left Up : Bumped polygon only */
	/*********************************/
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon coodination */
	V[0].fX							= 330.0f;
	V[0].fY							= 230.0f;
	V[1].fX							= 330.0f;
	V[1].fY							= 030.0f;
	V[2].fX							= 530.0f;
	V[2].fY							= 230.0f;
	V[3].fX							= 530.0f;
	V[3].fY							= 030.0f;
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon color */
	V[0].uBaseRGB.byte.bAlpha		=  255;
	V[0].uBaseRGB.byte.bRed			=  0;
	V[0].uBaseRGB.byte.bGreen		=  0;
	V[0].uBaseRGB.byte.bBlue		=  0;
	V[1].uBaseRGB.byte.bAlpha		=  255;
	V[1].uBaseRGB.byte.bRed			=  0;
	V[1].uBaseRGB.byte.bGreen		=  0;
	V[1].uBaseRGB.byte.bBlue		=  0;
	V[2].uBaseRGB.byte.bAlpha		=  255;
	V[2].uBaseRGB.byte.bRed			=  0;
	V[2].uBaseRGB.byte.bGreen		=  0;
	V[2].uBaseRGB.byte.bBlue		=  0;
	V[3].uBaseRGB.byte.bAlpha		=  255;
	V[3].uBaseRGB.byte.bRed			=  0;
	V[3].uBaseRGB.byte.bGreen		=  0;
	V[3].uBaseRGB.byte.bBlue		=  0;
	/*-----------------------------------------------------------------------------------*/
	/* set vertex data */
	SetVertex(&VertexContextB1);	/* set bump polygon */

	/*****************************************************************/
	/* Left Down : Trans Bumped polygon and Opaque No-bumped polygon */
	/*****************************************************************/
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon coodination */
	V[0].fX							= 110.0f;
	V[0].fY							= 450.0f;
	V[1].fX							= 110.0f;
	V[1].fY							= 250.0f;
	V[2].fX							= 310.0f;
	V[2].fY							= 450.0f;
	V[3].fX							= 310.0f;
	V[3].fY							= 250.0f;
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon color for Normal polygon (White) */
	V[0].uBaseRGB.byte.bAlpha		=  128;
	V[0].uBaseRGB.byte.bRed			=  255;
	V[0].uBaseRGB.byte.bGreen		=  255;
	V[0].uBaseRGB.byte.bBlue		=  255;
	V[1].uBaseRGB.byte.bAlpha		=  128;
	V[1].uBaseRGB.byte.bRed			=  255;
	V[1].uBaseRGB.byte.bGreen		=  255;
	V[1].uBaseRGB.byte.bBlue		=  255;
	V[2].uBaseRGB.byte.bAlpha		=  128;
	V[2].uBaseRGB.byte.bRed			=  255;
	V[2].uBaseRGB.byte.bGreen		=  255;
	V[2].uBaseRGB.byte.bBlue		=  255;
	V[3].uBaseRGB.byte.bAlpha		=  128;
	V[3].uBaseRGB.byte.bRed			=  255;
	V[3].uBaseRGB.byte.bGreen		=  255;
	V[3].uBaseRGB.byte.bBlue		=  255;
	/*-----------------------------------------------------------------------------------*/
	/* set vertex data */
	SetVertex(&VertexContextP1);	/* set no-bumped polygon */
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon color for BUMP (Black) */
	V[0].uBaseRGB.byte.bAlpha		=  255;
	V[0].uBaseRGB.byte.bRed			=  0;
	V[0].uBaseRGB.byte.bGreen		=  0;
	V[0].uBaseRGB.byte.bBlue		=  0;
	V[1].uBaseRGB.byte.bAlpha		=  255;
	V[1].uBaseRGB.byte.bRed			=  0;
	V[1].uBaseRGB.byte.bGreen		=  0;
	V[1].uBaseRGB.byte.bBlue		=  0;
	V[2].uBaseRGB.byte.bAlpha		=  255;
	V[2].uBaseRGB.byte.bRed			=  0;
	V[2].uBaseRGB.byte.bGreen		=  0;
	V[2].uBaseRGB.byte.bBlue		=  0;
	V[3].uBaseRGB.byte.bAlpha		=  255;
	V[3].uBaseRGB.byte.bRed			=  0;
	V[3].uBaseRGB.byte.bGreen		=  0;
	V[3].uBaseRGB.byte.bBlue		=  0;
	/*-----------------------------------------------------------------------------------*/
	/* set vertex data */
	SetVertex(&VertexContextB2);	/* set bump polygon */

	/******************************************************************/
	/* Right Down : Opaque Bumped polygon and Trans No-bumped polygon */
	/******************************************************************/
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon coodination */
	V[0].fX							= 330.0f;
	V[0].fY							= 450.0f;
	V[1].fX							= 330.0f;
	V[1].fY							= 250.0f;
	V[2].fX							= 530.0f;
	V[2].fY							= 450.0f;
	V[3].fX							= 530.0f;
	V[3].fY							= 250.0f;
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon color for Normal polygon (White) */
	V[0].uBaseRGB.byte.bAlpha		=  128;
	V[0].uBaseRGB.byte.bRed			=  255;
	V[0].uBaseRGB.byte.bGreen		=  255;
	V[0].uBaseRGB.byte.bBlue		=  255;
	V[1].uBaseRGB.byte.bAlpha		=  128;
	V[1].uBaseRGB.byte.bRed			=  255;
	V[1].uBaseRGB.byte.bGreen		=  255;
	V[1].uBaseRGB.byte.bBlue		=  255;
	V[2].uBaseRGB.byte.bAlpha		=  128;
	V[2].uBaseRGB.byte.bRed			=  255;
	V[2].uBaseRGB.byte.bGreen		=  255;
	V[2].uBaseRGB.byte.bBlue		=  255;
	V[3].uBaseRGB.byte.bAlpha		=  128;
	V[3].uBaseRGB.byte.bRed			=  255;
	V[3].uBaseRGB.byte.bGreen		=  255;
	V[3].uBaseRGB.byte.bBlue		=  255;
	/*-----------------------------------------------------------------------------------*/
	/* set vertex data */
	SetVertex(&VertexContextP2);	/* set no-bumped polygon */
	/*-----------------------------------------------------------------------------------*/
	/* Set Polygon color for BUMP (Black) */
	V[0].uBaseRGB.byte.bAlpha		=  255;
	V[0].uBaseRGB.byte.bRed			=  0;
	V[0].uBaseRGB.byte.bGreen		=  0;
	V[0].uBaseRGB.byte.bBlue		=  0;
	V[1].uBaseRGB.byte.bAlpha		=  255;
	V[1].uBaseRGB.byte.bRed			=  0;
	V[1].uBaseRGB.byte.bGreen		=  0;
	V[1].uBaseRGB.byte.bBlue		=  0;
	V[2].uBaseRGB.byte.bAlpha		=  255;
	V[2].uBaseRGB.byte.bRed			=  0;
	V[2].uBaseRGB.byte.bGreen		=  0;
	V[2].uBaseRGB.byte.bBlue		=  0;
	V[3].uBaseRGB.byte.bAlpha		=  255;
	V[3].uBaseRGB.byte.bRed			=  0;
	V[3].uBaseRGB.byte.bGreen		=  0;
	V[3].uBaseRGB.byte.bBlue		=  0;
	/*-----------------------------------------------------------------------------------*/
	/* set vertex data */
	SetVertex(&VertexContextB1);	/* set bump polygon */

	/*-----------------------------------------------------------------------------------*/
	/* Rendering start */
#if defined(_DISPLAY_STRING_)
	txt_DispScreen(VertexBufferDesc);	/* Display screen */
#endif
	status = kmRender();				/* Render !! */
	Check_Status(status, "kmRender");

	/*-----------------------------------------------------------------------------------*/
	/* Flip screen */
	status = kmFlipFrameBuffer();
	Check_Status(status, "kmFlipFrameBuffer");

	/*-----------------------------------------------------------------------------------*/
	/* Loop */
	}	/* end of while(1) */

	/*-----------------------------------------------------------------------------------*/
	/* Free vertex buffer */
	status = kmDiscardVertexBuffer(&VertexBufferDesc);
	Check_Status(status, "kmDiscadeVertexBuffer");

	PRINTOUT(("kmDiscadeVertexBuffer\n"));

	/*-----------------------------------------------------------------------------------*/
	/* Free texture Surface */
	status = kmFreeTexture(&TexSurfaceDesc1);
	Check_Status(status, "kmFreeTexture");

	/*-----------------------------------------------------------------------------------*/
	/* End display */
	status = kmStopDisplayFrameBuffer();
	Check_Status(status, "kmStopDisplayFrameBuffer");

	PRINTOUT(("** End of test program **\n"));
	return  0;
}
