/******************************************************************************

  Copyright (C), 2001-2012, Texas Instrument.

 ******************************************************************************
  File Name     : Robust_L1_MP.c
  Version       : Initial Draft
  Author        : Brighton Feng
  Created       : 2012/10/13
  Last Modified :
  Description   : L1 memory protection example
  Function List :
              L1D_MP_test
              L1P_MP_test
  History       :
  1.Date        : 2012/10/13
    Author      : Brighton Feng
    Modification: Created file

******************************************************************************/

#include <stdio.h>
#include "KeyStone_common.h"
#define USE_EDMA_ACCESS_L1 	1
/*----------------------------------------------*
 * external variables                           *
 *----------------------------------------------*/
/*----------------------------------------------*
 * external routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * internal routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * project-wide global variables                *
 *----------------------------------------------*/

/*----------------------------------------------*
 * module-wide global variables                 *
 *----------------------------------------------*/

/*----------------------------------------------*
 * constants                                    *
 *----------------------------------------------*/

/*----------------------------------------------*
 * macros                                       *
 *----------------------------------------------*/

/*----------------------------------------------*
 * routines' implementations                    *
 *----------------------------------------------*/
/*****************************************************************************
 Prototype    : L1P_MP_test
 Description  : write to L1P cache to trigger excpetions
 Input        : None
 Output       : None
 Return Value : 
 
  History        :
  1.Date         : 2012/10/13
    Author       : Brighton Feng
    Modification : Created function

*****************************************************************************/
void L1P_MP_test()
{
	puts("===================-L1P memory protection test-==================");

	puts("!!!DMA write to L1D at 0x00E00000...");
#if USE_EDMA_ACCESS_L1
	EDMA_copy(GLOBAL_ADDR(0x00800000),
			  GLOBAL_ADDR(0x00E00000),
			  32, EDMA_CC2_CH0, DMA_NO_WAIT);
#else
	IDMA_copy(0x00800000, 0x00E00000, 32, DMA_NO_WAIT);
#endif
	/*DMA access has some latency, and normally, exception is 
	captured about 15~45 cycles after the access violation.
	Dummy operations added here to wait for DMA access and excpetion*/
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");/*NRP should be around the address of this instruction*/
	asm(" nop 5");	asm(" nop 5");
#if USE_EDMA_ACCESS_L1
	EDMA_wait(EDMA_CC2_CH0);
#else
	IDMA_wait();
#endif

	printf("\n!!!DSP core write to gpCGEM_regs->L1PMPPA[0] register at 0x%x without unlocking...\n",
		&gpCGEM_regs->L1PMPPA[0]);
	gpCGEM_regs->L1PMPPA[0] = 0xFFFFFFFF;

	/*normally, exception is captured about 10~100 cycles after the 
	access violation. Dummy operations added here to wait for it.*/
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5"); 	/*NRP should be around the address of this instruction*/
	asm(" nop 5"); 	
}

/*****************************************************************************
 Prototype    : L1D_MP_test
 Description  : write to L1D cache to trigger excpetions
 Input        : None
 Output       : None
 Return Value : 
 
  History        :
  1.Date         : 2012/10/13
    Author       : Brighton Feng
    Modification : Created function

*****************************************************************************/
void L1D_MP_test()
{
	puts("===================-L1D memory protection test-==================");

	puts("!!!DMA write to L1D at 0x00F00000...");
#if USE_EDMA_ACCESS_L1
	EDMA_copy(GLOBAL_ADDR(0x00800000),
			  GLOBAL_ADDR(0x00F00000),
			  32, EDMA_CC2_CH0, DMA_NO_WAIT);
#else
	IDMA_copy(0x00800000, 0x00F00000, 32, DMA_NO_WAIT);
#endif

	/*DMA access has some latency, and normally, exception is 
	captured about 15~45 cycles after the access violation.
	Dummy operations added here to wait for DMA access and excpetion*/
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");
	asm(" nop 5");	asm(" nop 5");/*NRP should be around the address of this instruction*/
	asm(" nop 5");	asm(" nop 5");
#if USE_EDMA_ACCESS_L1
	EDMA_wait(EDMA_CC2_CH0);
#else
	IDMA_wait();
#endif

	puts("\n!!!DSP core write to L1D at 0x00F00100...");
	*(Uint32 *)(0x00F00100)= 0xBFBFBFBF;

	/*normally, exception is captured about 10~100 cycles after the 
	access violation. Dummy operations added here to wait for it.*/
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5"); 	/*NRP should be around the address of this instruction*/
	asm(" nop 5"); 	

	printf("\n!!!DSP core write to gpCGEM_regs->L1DMPPA[0] register at 0x%x without unlocking...\n",
		&gpCGEM_regs->L1DMPPA[0]);
	gpCGEM_regs->L1DMPPA[0] = 0xFFFFFFFF;

	/*normally, exception is captured about 10~100 cycles after the 
	access violation. Dummy operations added here to wait for it.*/
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5");
	asm(" nop 5"); 	/*NRP should be around the address of this instruction*/
	asm(" nop 5"); 	
}



