#include "stdio.h"
#include "verilog_test.h" 

extern void BmeNIreset(void);
extern void BmeNIconfigUtopia(unsigned char level, unsigned char extendedMode, unsigned char removeHec, unsigned char cellSize);
extern void BmeNIconfigCh(unsigned int portNum, int fastChnlNum, int ilvChnlNum);
extern void BmeNIenableCh(unsigned char port);
extern void testPIOWrite_diag (int BmeNum, unsigned long addr, unsigned long data);
extern void InitDma (int BmeNum);

void x2cloop (UINT32 loopCount) 
{ 

/*  UINT32 ccr_status=0;
  UINT32 data=0;
  int i;*/
  UINT32 temp0=0,temp1=0;

  printf("\n Initialize BME NI");
  BmeNIreset();

  printf("\n Config BME Utopia");
  BmeNIconfigUtopia(0,0,0,53);

  printf("\n Config BME Utopia Channel");
  BmeNIconfigCh(0,0,-1);

  testPIOWrite_diag (1, 0xe0040100,0x0);
//    testPIORead (1, 0xe0040100);

  printf("\n Initialize DMA!");
  InitDma(1);

  printf("\n Enable BME Utopia Channel");
  BmeNIenableCh(0);

  while (loopCount)
  {
//	  printf ("\nWaiting for data in XFIFO... ");
    /*Waiting for FIFO condition to be True, Block = 8, Size = 53, Fullness= 1*/
    wait_for_fifo_data(0x8, 0xffff, 0xe0040000, 0xe0040100, 0x0, 53, 1);

    printf(".");

    /*Starting DMA from XFIFO to CFIFO*/
    /*Polling bit 6 of DMA_CCR register to see if DMA is active*/

    temp1=0xe0030000 & 0xfffffffc;
    pio_read (temp1,&temp0);
    temp0=temp0 & 0x40;

    while ((temp0&0x40) == 0x40)
    pio_read (temp1,&temp0);

//	  printf ("DMA from XFIFO to CFIFO\n");

    /*Programming DMA Channel Registers, SAR, DAR, ECR, CCR*/
    pio_write( 0xe0030080, 0xc0000000);   // SAR

    pio_write( 0xe00300c0, 0xc6000000);   // DAR

    pio_write( 0xe0030040, 0x80000000);   // EVR

    pio_write( 0xe0030000, 0x00341601);   // CCR

    /*Polling bit 8 of DMA_CCR register to see if DMA is done*/
    pio_read (0xe0030000, (&temp0));
    temp0&=0x100;

    while((temp0 & 0x100) ==0x0 )
	    pio_read (0xe0030000, (&temp0));

    loopCount--;
  }

	/* Read CFIFO/XFIFO rd/wr pointers */
	printf ("\nRead CFIFO/XFIFO rd/wr pointers");
	pio_read (0xe0040100, (&temp0));
	printf ("\nXFIFO: 0xe0040100 0x%lx",temp0);
	pio_read (0xe0068100, (&temp0));
	printf ("\nCFIFO: 0xe0068100 0x%lx",temp0);

}
/*end of test*/

