#ifdef BUP_DIAGS
#include <stdio.h>
#ifdef VXWORKS
#include <vxWorks.h>
#endif
#include "apposmacro.h"
#define UINT32 unsigned int

extern int printDiags;
extern int bup_bme;

//#define DEBUG_MSG

int bup_DoDmaCO (int SrcAddr, int DestAddr, int NumBytes, int ChnNo)
{
	int i = 0;
	UINT32 data1 = 0x0;
	UINT32 CcrReg;
	int fail = 0;

	testPIOWrite (bup_bme, (0xe0030040 | (ChnNo << 2)), 0x80000000);	/* set software DMA request*/
	testPIOWrite (bup_bme, (0xe0030080 | (ChnNo << 2)), SrcAddr);	   /* set source address*/
	testPIOWrite (bup_bme, (0xe00300c0 | (ChnNo << 2)), DestAddr);   /* set dest address*/
	CcrReg = 0x00001011 | ((NumBytes-1) << 16);
	testPIOWrite (bup_bme, (0xe0030000 | (ChnNo << 2)), CcrReg);		/* start DMA*/
	while (!(data1 & 0x100))				 /* polling for DMA done bit*/
	{
		data1 = testPIORead1 (bup_bme, (0xe0030000 | (ChnNo << 2)));
		i++;
		if (i == 300)
		{
			i = 0;
			printf ("********* Time out: DMA FAILED ********\n");
			fail = 1;
			break;
		}
	}
	return fail;
}
//----------------------------------------------------------------------------------------

int bup_BasicDmaTestCO ()
{
	int i = 0;
	int testfail = 0;
	int StartData;
	int NumBytes, ChnNo;

	UINT32 DestAddr [27] = {
		0xc0000000, 0xc1000000, 0xc1800000, 0xc2000000, 0xc5000000, 0xd1000000, 0xd0000000, 0xc6000000,
		0xca000000, 0xc9000000, 0xc3000000, 0xc4000000, 0xcc000000, 0xcd000000, 0xcb000000, 0xcb000000,
//      0xca000000, 0xc9000000, 0xc3000000, 0xc4000000, 0xcc000000, 0xcd000000, 0xcb000000, 0xc7000000,
		0xd2000000, 0xd2002000, 0xd2002180, 0xc6800000, 0xc3400000, 0xc3800000, 0xc3c00000, 0xc4400000,
		0xc4800000, 0xc4c00000, 0xc0800000};

	while (i < 27)
	{
		StartData = rand ();
		ChnNo = (StartData & 0x3);
		if ((DestAddr[i]&0xffffe000) == 0xd2002000)
			NumBytes = 96;
		else
			NumBytes = 256;
		bup_WriteSdramCO (0x40000000, 256, StartData);	 /*write SDRAM 1k locations*/
		printf ("DMA -> Source Addr:0x40000000, Dest Addr:0x%x, NumBytes:%d, Chn No:%d\n",DestAddr[i],NumBytes,ChnNo);
		testfail |= bup_DoDmaCO (0x40000000, DestAddr[i], NumBytes, ChnNo);
		testfail |= bup_DoDmaCO (DestAddr[i], 0x40001000, NumBytes, ChnNo);
		testfail |= bup_ReadSdramCO (0x40001000, NumBytes, StartData);
		i++;
	}


	if (testfail)
	{
		printf ("***********************************************\n"); 
		printf ("\tBasic DMA test FAILED\n");
		printf ("***********************************************\n"); 
	}
	else if (printDiags)
	{
		printf ("***********************************************\n"); 
		printf ("\tBasic DMA test PASSED  \n");
		printf ("***********************************************\n"); 
	}
	return testfail;
}
#endif
