// ------------------------------------------------------------------------------------------------
// MyPrintf.c
//
// This has a function to printf to the debugger window
//
// COPYRIGHT (C) SEGA OF AMERICA, INC. 1998
// All rights reserved.
//
// Information and source code samples contained herein are 
// provided "as-is", without representations or warranties, and 
// are subject to change without notice.
//
// SEGA OF AMERICA, INC. cannot support modifications or 
// derivative works created from the sample source code provided. 
// Developers may use, reproduce, and modify portions or entire 
// sections of the sample source code for the purposes of creating
// Dreamcast applications.  
//
// Sample source code contained herein may not be appropriate 
// for use under all circumstances.  Please consult Developer 
// documentation provided along with your Dreamcast unit for
// additional information and suggestions.
//
//
// Author: Ralph "Cooksey" Thomas
//
// Tabs:	4
//
// Rev:		05/05/99	File Originated
//						
// ------------------------------------------------------------------------------------------------

#include <shinobi.h>
#include <ac.h>
#include <acassert.h>
#include <stdarg.h>
#include <stdio.h>
#include "MyPrintf.h"


// ------------------------------------------------------------------------------------------------
// For a binary/retail type build that uses any of the My...() group of functions comment
// out this constant. If this is left in for a retail build it will fail as there is no
// "console" window to print to...
// ------------------------------------------------------------------------------------------------

#define WRITE_TO_DEBUGGER_CONSOLE

extern const char* audio64Build;

// STARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MyShowAmError
// TERSE DESCRIPTION Shows the AM error number and text message
// 
// FORMAT
// 
// #include <ac.h>
// #include <MyPrintf.h>
// 
// void MyShowAmError(AM_ERROR_PTR theError)
// 
// PARAMETERS
// 
// AM_ERROR_PTR theError,	a pointer to the AM error structure.
// 
// RETURN VALUE
// 
// void
// 
// FUNCTION
// 
// Shows the AM error number and text message in the debugger console window.
//
// ENDDOC -----------------------------------------------------------------------------------------

void MyShowAmError(AM_ERROR_PTR theError)
{
if(!theError)
	return;

if(theError->number==0)
	return;

MyDebugf("****************************************************************************************");
MyDebugf("AM Error #: %u",theError->number);
MyDebugf("Message: %s",theError->message);
MyDebugf("****************************************************************************************");
}

// STARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MyShowAcError
// TERSE DESCRIPTION Shows the AC error number and text message
// 
// FORMAT
// 
// #include <ac.h>
// #include <MyPrintf.h>
// 
// void MyShowAcError(AC_ERROR_PTR theError)
// 
// PARAMETERS
// 
// AC_ERROR_PTR theError,	a pointer to the AC error structure.
// 
// RETURN VALUE
// 
// void
// 
// FUNCTION
// 
// Shows the AC error number and text message in the debugger console window.
//
// ENDDOC -----------------------------------------------------------------------------------------


void MyShowAcError(AC_ERROR_PTR theError)
{
if(!theError)
	return;

if(theError->number==0)
	return;
	
MyDebugf("****************************************************************************************");
MyDebugf("AC Error #: %u",theError->number);
MyDebugf("Message: %s",theError->message);
MyDebugf("****************************************************************************************");
}

// STARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MyShowDriverRev
// TERSE DESCRIPTION Shows the revision numbers for the lib and driver.
// 
// FORMAT
// 
// #include <ac.h>
// #include <MyPrintf.h>
// 
// void MyShowDriverRev(volatile KTU32 *driverImage)
// 
// PARAMETERS
// 
// volatile KTU32 *driverImage,	an image in SH4 memory of the driver to be installed.
// 
// RETURN VALUE
// 
// void
// 
// FUNCTION
// 
// This will show the library revision, the driver revision necessary for the library and the 
// revision of the driver passed in as its argument.
//
// ENDDOC -----------------------------------------------------------------------------------------


void MyShowDriverRev(volatile KTU32 *driverImage)
{
KTU8 	majorVersion	=	0;
KTU8 	minorVersion	=	0;
KTCHAR 	localVersion	=	0;

acSystemGetDriverRevision((KTU8 *)driverImage,&majorVersion,&minorVersion,&localVersion);

MyDebugf("----------------------------------------------------------------------------------------");
MyDebugf("Lib Revision       -%s",audio64Build);
MyDebugf("Lib Expects Driver - Major: %u Minor: %u Local: %c",AC_MAJOR_REVISION,AC_MINOR_REVISION,AC_LOCAL_REVISION);
MyDebugf("Driver Revision    - Major: %u Minor: %u Local: %c",majorVersion,minorVersion,localVersion);
MyDebugf("----------------------------------------------------------------------------------------");
}


// STARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MyDcPrintf
// TERSE DESCRIPTION printf's to the Dreamcast 
// 
// FORMAT
// 
// #include <ac.h>
// #include <MyPrintf.h>
// 
// void MyDcPrintf(const char *format,...)
// 
// PARAMETERS
// 
// Standard printf args.
// 
// RETURN VALUE
// 
// void
// 
// FUNCTION
// 
// Formatted printing to the dreamcast display, this will cause an error if the 
// string is too long...
//
// Note: This is based on the njPrint() call so it needs to be called in the main loop for
// anything to appear on the screen...
// 
// ENDDOC -----------------------------------------------------------------------------------------

void MyDcPrintf(short x,short y,const char *format,...)
{
va_list argptr;
char buffer[255];

va_start(argptr, format);
vsprintf(buffer, format, argptr);
va_end(argptr);

njPrint(NJM_LOCATION(x,y),buffer);
}


// STARTDOC ---------------------------------------------------------------------------------------
// FUNCTION NAME MyDebugf
// TERSE DESCRIPTION printf's to the Codescape log window.
// 
// FORMAT
// 
// #include <ac.h>
// #include <MyPrintf.h>
// 
// void MyDebugf(const char *format,...)
// 
// PARAMETERS
// 
// Standard printf args.
// 
// RETURN VALUE
// 
// void
// 
// FUNCTION
// 
// Formatted printing to the debugger log window
// 
// ENDDOC -----------------------------------------------------------------------------------------

void MyDebugf(const char *format,...)
{
va_list argptr;
char buffer[255];

if(format==KTNULL)
	{
	return;
	}

va_start(argptr, format);
vsprintf(buffer, format, argptr);
va_end(argptr);

#ifdef WRITE_TO_DEBUGGER_CONSOLE
debug_write(1,buffer,strlen(buffer));
#endif
}


