/*

This source file contains user functions for easy disassembling of DMA chains.

It automatically determines which type of disassembly is required (by looking
at the DMA channel).

It will return an error status. The calling application should check for this using
IsFatal(err), and if it returns true, then it should dump the output/error
buffers.

*/

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header.
#include "PS2/PS2Strip/DMADebug/Disassemble.h"

// Engine independent.
#include "Debugging/Asura_Assert.h"

// PS2Strip.
#include "PS2/PS2Strip/PS2Strip_PS2Stuff.h"

// DMADebug.
#include "PS2/PS2Strip/DMADebug/Debug.h"

// B E G I N   N A M E S P A C E /////////////////////////////////////////////

namespace DMADebug
{

// F U N C T I O N   I M P L E M E N T A T I O N S ///////////////////////////

static int debug( PS2STRIP_DMA_CHAN_ID eChanID )
{
	int err = DMA_ERR_NO_ERROR;
	if(eChanID == PS2STRIP_DMA_CHAN_ID_VIF0 || eChanID == PS2STRIP_DMA_CHAN_ID_VIF1) {
		err |= VIF_disassemble();
	} else if(eChanID == PS2STRIP_DMA_CHAN_ID_GIF) {
		err |= GIF_disassemble();
	} else {
		do {
			err |= DMA_emulateDMACycle();
		} while(!IsFatal(err) && gDmaTransferQWord != NULL);
	}

//	if(err != DMA_ERR_NO_ERROR)
//    {
//		puts("\n\n===== Errors occurred ===== \n");
//		puts(gOutputBuffer);
//		Exit(1);
//	}

	return err;
}

int DIS_dmaSend( PS2STRIP_DMA_CHAN_ID eChanID, u_int chcr, const char* options )
{
	int err = DMA_ERR_NO_ERROR;

	DEBUG_init(options);
	err |= DMA_startDmaTransfer(eChanID, chcr);
	if(!IsFatal(err)) {
		err |= debug(eChanID);
	}
	return err;
}

int DIS_sceDmaSend( PS2STRIP_DMA_CHAN_ID eChanID, void* tag, const char* options )
{
	int err = DMA_ERR_NO_ERROR;
	DEBUG_init(options);
	err |= DMA_sceDmaSend( eChanID, tag );
	return debug(eChanID);
}

int DIS_sceDmaSendN( PS2STRIP_DMA_CHAN_ID eChanID, void* tag, int numqwords, const char* options )
{
	int err = DMA_ERR_NO_ERROR;
	DEBUG_init(options);
	err |= DMA_sceDmaSendN( eChanID, tag, numqwords );
	return debug(eChanID);
}

// E N D   N A M E S P A C E /////////////////////////////////////////////////

}

