/***************************************************************************
 *
 * std.c
 *
 ***************************************************************************
 *
 * standard C lib functions missing from N64 libs
 *
 ***************************************************************************/

/*
 * includes
 */

/*
 * defines
 */

/*
 * typedefs
 */

/*
 * structures
 */

/*
 * globals
 */

int	fileOutHandle=-1;

/*
 * function prototypes
 */

/*
 * code
 */

#ifdef N64

int toupper(int c)
{
	if(c>='a' && c<='z') c-=32;
	return(c);
}

int tolower(int c)
{
	if(c>='A' && c<='Z') c+=32;
	return(c);
}

void printf(char *s,...)
{
}

void strcat(char *a,char *b)
{
	for(;(*a);a++);

	while(1)
	{
		*a=*b;
		if(!(*b)) break;
		a++; b++;
	}
}

void strncpy(char *a,char *b,int n)
{
	while(1)
	{
		*a=*b;
		n--;
		if(!n) { *a=0; break; }
		if(!(*b)) break;
		a++; b++;
	}
}

int stricmp(char *a,char *b)
{
	int i;

	while(1)
	{
		if(toupper(*a) < toupper(*b)) return(-1);
		else if(toupper(*a) > toupper(*b)) return(1);
		if(!(*a)) break;
		a++; b++;
	}

	return(0);
}

void KernelCritsecEnter(void)
{
}

void KernelCritsecExit(void)
{
}

void IntToStr(int a,char *s)
{
	int i,j;

	if(a<0)
	{
		*(s++)='-';
		a=-a;
	}

	for(j=1;j<=a;j*=10);
	if(a!=0) j/=10;
	for(;j>0;j/=10)
	{
		*(s++)='0' + (a/j);
		a-=(a/j)*j;
	}
	*(s++)=0;
}

void strupper(char *a)
{
	while(*a) { *a=toupper(*a); a++; }
}

unsigned short fast_sqrt(unsigned long v)
{
	long t,r,s;

	t=1<<30;
	r=0;

#define STEP(k)				\
		s = t + r;		\
		r >>= 1;		\
		if (s <= v)		\
		{			\
		        v -= s; 	\
		        r |= t; 	\
		}

	STEP(15);	t >>= 2;
	STEP(14);	t >>= 2;
	STEP(13);	t >>= 2;
	STEP(12);	t >>= 2;
	STEP(11);	t >>= 2;
	STEP(10);	t >>= 2;
	STEP(9);	t >>= 2;
	STEP(8);	t >>= 2;
	STEP(7);	t >>= 2;
	STEP(6);	t >>= 2;
	STEP(5);	t >>= 2;
	STEP(4);	t >>= 2;
	STEP(3);	t >>= 2;
	STEP(2);	t >>= 2;
	STEP(1);	t >>= 2;
	STEP(0);

	return (unsigned short) r;
}

#endif


void exit(int i)
{
	// WOLFF: This wasn't cool with the GCC compiler
	//	asm("break 0x407");

	asm("break 0");     
	while(1);
}

static int
	FatalReturn;

void FatalMessage(void)
{
	asm( "sw $31,FatalReturn" );
	exit(1);
}

void PrintToFile(char *string)
{
#ifdef DEBUG
	if(fileOutHandle==-1)
	{
		fileOutHandle=PCcreat("output.log",0);
	}
	PCwrite(fileOutHandle,string,strlen(string));
#endif
}


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

void *memset( void *mem, int val, unsigned len )
{
	char
		*ptr = (char *)mem;

	while (len)
	{
		*ptr++ = (char)val;
		len--;
	}

	return( mem );
}


char *strcpy( char *dst, const char *src )
{
	char
		*start = dst;

	while (*src)
		*dst++ = *src++;

	*dst++ = 0;

	return( start );
}


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/



/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

