
/*
 * A very simple program residing in NAPA/SPLASH or DDR
 */

#include <diag.h>
#include "iop.h"


#define MAX_DATA 64
int global_data[MAX_DATA];

/*
 * This is shared between the mainline and the interrupt service
 * routine.  When it goes non-zero, we know that the interrupt arrived
 * and has been serviced.
 */
volatile int intrSvcDone = 0;
                                                                                                 
int
f (int *global_data);
int
fchk (int *global_data);
int 
compare_memory (unsigned int *src, unsigned int *dest, unsigned int words);
void 
init_memory (unsigned int *src, unsigned int words);
void 
wait_interrupt (void);

extern void
aesStartDma(unsigned int *src, unsigned int *dst, unsigned int numBlocks);

extern int
aesWait4Done(int timeout);

extern
void
interrupt_service (void);

extern
void
setupISR(void*);

/*
 * Note that we lay out the source and destination words leaving a
 * bit of space for other data items.
 */
#define TEST_DMA_SZ             0xc00  // in bytes
#define WORD_SZ                 (sizeof(int) / sizeof(char))
#define TEST_DMA_BLOCK_SZ       16  // 16 bytes-block DMA transfers

#define AES_DMA_TIME		100000


void
cpuDmaTest(unsigned int *src, unsigned int *dest, unsigned int words)
{
    unsigned int dma_bl;

    dma_bl = (words * WORD_SZ) / TEST_DMA_BLOCK_SZ;

    // Initialise the source block with data pattern.
    init_memory (src, words);
    //init_memory (dest - 1, words + 1); ///

    // Initialize the address past the last dest byte
    init_memory ((unsigned int *)((unsigned int)dest + TEST_DMA_SZ), 1);

    /* Flush memory */
    ahbMemFlush(IOS_WB_IOP);

    /* Invalidate AHM AES */
    ahbMemRBInvalidate(IOS_RB_AES);

    // setup ISR
    setupISR(interrupt_service);
  
    // Set up the device to interrupt.
    device_setup(src, dest, dma_bl); ///

    f(global_data);

    wait_interrupt(); ///

    //flushInvalidDCache();

    /* Flush memory */
    ahbMemFlush(IOS_WB_AES);
    ahbMemFlush(IOS_WB_IOP);

    /* Invalidate IOD */
    ahbMemRBInvalidate(IOS_RB_IOD);

    if (fchk(global_data) && compare_memory (src, dest, words))
        simPass(0);
    else
        simFail(0);
}


int
f (int *global_data)
{
    int i;

    for (i = 0; i < MAX_DATA / 2; i++) 
    {
        global_data[i] = i;
        global_data[MAX_DATA - 1 - i] = MAX_DATA - 1 - i;
    }

    return 0;
}

/*
 * Check f() results:
 * pass - return 1
 * fail - return 0
 *
 */

int
fchk (int *global_data)
{
    int i;

    for (i = 0; i < MAX_DATA / 2; i++) 
    {
        if (global_data[i] != i 
            || global_data[MAX_DATA - 1 - i] != MAX_DATA - 1 - i)
            return 0;
    }
    return 1;
}

/*
 * Initialise the source block with data pattern.
 */
void
init_memory (unsigned int *src, unsigned int words)
{
    int i;

    for (i = 0; i < words; i++) {
        *src++ = ((i + 1) << 16 | i);
    }

    return;
}

/*
 * Compare source and destination data words for identical contents.
 */
int
compare_memory (unsigned int *src, unsigned int *dest, unsigned int words)
{
    int i;

    for (i = 0; i < words; i++) {
        if (*src++ != *dest++) {
            trace_it(src);
            trace_it(dest);
            trace_it(i);
            return(0);
        }
    }
    if (*dest != (1 << 16))
    {
        trace_it(dest);
        return(0);
    }

    return (1);
}

/*
 * Just loop waiting for the interrupt to arrive.
 */
void
wait_interrupt (void)
{
    int i;

    i = 0;

    while (intrSvcDone == 0) {
        i++;

        /*
         * Take a look at the interrupt status register.
         */
        handle_status_register ();
    }

    return;
}


/* eof */
