/*#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <memory.h>

#include <libsn.h>
*/

#include "glover.h"

static char		buffer[256];
static char		line[84];
/*
int debugPrintf(char* fmt, ...)
{
    va_list	arglist;
    long	r1, r2, r3, r4, r5, r6;
    int		len;

	va_start(arglist, fmt);
    r1 = va_arg(arglist, long);
    r2 = va_arg(arglist, long);
    r3 = va_arg(arglist, long);
    r4 = va_arg(arglist, long);
    r5 = va_arg(arglist, long);
    r6 = va_arg(arglist, long);
	va_end(arglist);
    len = sprintf(buffer, fmt, r1, r2, r3, r4, r5, r6);
#if GOLDCD == NO
	//if(buffer
	buffer[len] = 13;		// rqd for the analyser devkit, which just does N/L, not C/R on PCWrite
	buffer[len+1] = 10;

    PCwrite(-1, buffer, len+1);
#else
	printf(buffer);
#endif
    return len;
}
*/

int __line;
char __file[16];

#define ENDLINE 79

int debugPrintf2(char* fmt, ...)
{
    va_list	arglist;
    long	r1, r2, r3, r4, r5, r6;
    int		len, lenInfo, lenLeft, lineCtr;
	int i;

	lenInfo=sprintf(buffer, "%10s [%4d] ", __file,__line);

	va_start(arglist, fmt);
    r1 = va_arg(arglist, long);
    r2 = va_arg(arglist, long);
    r3 = va_arg(arglist, long);
    r4 = va_arg(arglist, long);
    r5 = va_arg(arglist, long);
    r6 = va_arg(arglist, long);
	va_end(arglist);
    len = sprintf(buffer+lenInfo, fmt, r1, r2, r3, r4, r5, r6)+lenInfo;

	lenLeft=len;

#if GOLDCD == NO

	lineCtr=0;
	for(i=0; i<lenLeft; i++)
	{
		if(lineCtr==ENDLINE)
		{
			line[lineCtr++] = 13;		// rqd for the analyser devkit, which just does N/L, not C/R on PCWrite
			line[lineCtr++] = 10;
			line[lineCtr++]=13;
			line[lineCtr]=10;

			PCwrite(-1, line, lineCtr);

			//lineCtr=0;
			lineCtr=lenInfo;
			memset(line, ' ', lenInfo);

			if(buffer[i]==' ')
				i++;
		}

		line[lineCtr++]=buffer[i];

	}

	line[lineCtr++]=13;
	line[lineCtr]=10;


    PCwrite(-1, line, lineCtr);
#else
	printf(buffer);
#endif
    return lenLeft;
}


int psxPrintf(char* fmt, ...)
{
    va_list	arglist;
    long	r1, r2, r3, r4, r5, r6;
	int len;

	va_start(arglist, fmt);
    r1 = va_arg(arglist, long);
    r2 = va_arg(arglist, long);
    r3 = va_arg(arglist, long);
    r4 = va_arg(arglist, long);
    r5 = va_arg(arglist, long);
    r6 = va_arg(arglist, long);
	va_end(arglist);
	len=sprintf(buffer, fmt, r1, r2, r3, r4, r5, r6);

#if GOLDCD == NO
	buffer[len++]=0xd;
	buffer[len]=0xa;
    PCwrite(-1, buffer, len);
#else
	printf(buffer);
#endif

	return 0xfec0ff;
}

void debugAssert(void)
{
//	psxPrintf("\nIf you are an artist, you can ignore this 'crash' by pressing X on your PSX\n");
//	psxPrintf("controller.  PLEASE NOTE that 'Beards' will not accept any responsibility\n");
//	psxPrintf("for any errors which occur AFTER the first ASSERT failure.\n");

	psxPrintf("Skippable assert: Continue with X, but expect problems\n");
	for(;;)
	{
		padHandler();
		if(MENUSELECT(0))
			break;
	}
}
