
#include <sys\types.h>
#include <libetc.h>
#include <libgte.h>
#include <libgpu.h>
#include <kernel.h>
#include "typedefs.h"
#include "gpu.h"

//#define PROFILE_ME


#ifdef PROFILE_ME
/*-------- #defines and macros ------+--------+--------+--------+-------+---*/

#define	TOP			25
#define	LEFT		25
#define HEIGHT		10
#define	GRIDSIZE	65
#define	DRAW_TOP	40
#define	HBL_MID_VAL	((525+625)/4)

/*-------- typedefs --------+--------+--------+--------+--------+-------+---*/
typedef struct
{
	short xgrid;
	char scalefactor;
	char nummarks;
} SCALE;

/*-------- Globals and Statics ------+--------+--------+--------+-------+---*/
static unsigned long EHbl;
static short divisor;
static short nummarks;
static short finalCount;
static POLY_G4	bargraph[2];
static POLY_F3	markers[9];
static POLY_F3	markers2[9];

static short drawCount;

static SCALE scales[] =
{
	{ 260, 0, 2 },          // Xgrid,ScaleFactor,NumMarks
	{ 130, 1, 3 },
	{ 65,  2, 5	}
};



/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
static void ProfileCallBack(void)
{
	drawCount = GetRCnt( RCntCNT1 ) >> divisor;
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
void	ProfileInit(int scale)
{
 	int		count;

	int grid = scales[scale].xgrid;
	nummarks = scales[scale].nummarks;
	divisor = scales[scale].scalefactor;

	EnterCriticalSection();
	EHbl = OpenEvent(RCntCNT1, EvSpINT, EvMdNOINTR, NULL);
	EnableEvent(EHbl);
	SetRCnt(RCntCNT1, 64000, RCntMdINTR);

	DrawSyncCallback(ProfileCallBack);

	ExitCriticalSection();

	for (count = 0; count < nummarks; count++ )
	{
	  	setPolyF3(&markers[count]);
		setRGB0(&markers[count],50,50,150);
		setXY3(&markers[count],
			LEFT+count*grid-4,TOP-5,
			LEFT+count*grid+4,TOP-5,
			LEFT+count*grid,TOP+5);

	  	setPolyF3(&markers2[count]);
		setRGB0(&markers2[count],50,50,150);
		setXY3(&markers2[count],
			LEFT+count*grid-4,DRAW_TOP+15,
			LEFT+count*grid+4,DRAW_TOP+15,
			LEFT+count*grid,DRAW_TOP+5);
	}

	setPolyG4(&bargraph[0]);
	setRGB0(&bargraph[0],0,200,0);
	setRGB1(&bargraph[0],200,0,0);
	setRGB2(&bargraph[0],0,200,0);
	setRGB3(&bargraph[0],200,0,0);
	setXY4(&bargraph[0], LEFT,TOP, 100,TOP, LEFT,TOP+HEIGHT, 100,TOP+HEIGHT);

	setPolyG4(&bargraph[1]);
	setRGB0(&bargraph[1],0,200,0);
	setRGB1(&bargraph[1],200,0,0);
	setRGB2(&bargraph[1],0,200,0);
	setRGB3(&bargraph[1],200,0,0);
	setXY4(&bargraph[1], LEFT,DRAW_TOP,
					100,DRAW_TOP, LEFT,DRAW_TOP+HEIGHT, 100,DRAW_TOP+HEIGHT);


}


/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
void	ProfileStartCount(void)
{
	ResetRCnt(RCntCNT1);
	StartRCnt(RCntCNT1);
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
short	ProfileStopCount(void)
{
	StopRCnt(RCntCNT1);
	finalCount = GetRCnt(RCntCNT1)>>divisor;
	return finalCount;
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
short	ProfileReadCount(void)
{
	finalCount = GetRCnt(RCntCNT1)>>divisor;
	return finalCount;
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
void	ProfileAddOT(unsigned long * ot)
{
	int		count;

	bargraph[0].x1 = finalCount+LEFT;
	bargraph[0].x3 = finalCount+LEFT;
	for (count = 0; count < nummarks; count++ )
	{
		addPrim(ot,&markers[count]);
	}


	addPrim(ot,&bargraph[0]);
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
void	ProfileAddDrawOT(unsigned long * ot)
{
	int		count;

	bargraph[1].x1 = drawCount+LEFT;
	bargraph[1].x3 = drawCount+LEFT;

	for (count = 0; count < nummarks; count++ )
	{
		addPrim(ot,&markers2[count]);
	}

	addPrim(ot,&bargraph[1]);
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
int 	ProfileDispMode(void)
{
	int 	count;

	VSync(0);
	ProfileInit(0);
	ProfileStartCount();
	VSync(0);
	count = ProfileStopCount();

	if (count < HBL_MID_VAL)
		return MODE_NTSC;
	else
		return MODE_PAL;
}

/*--------+--------+--------+--------+--------+--------+--------+-------+---*/
#endif
