//////////////////////////////////////////////////////////////////////////////
// Filename:          C:\School\Project\MPSoC\Multiprocs/drivers/shared_mem_if_v1_00_a/src/shared_mem_if_selftest.c
// Version:           1.00.a
// Description:       Contains a diagnostic self-test function for the shared_mem_if driver
// Date:              Thu Apr 02 10:05:07 2009 (by Create and Import Peripheral Wizard)
//////////////////////////////////////////////////////////////////////////////


/***************************** Include Files *******************************/

#include "shared_mem_if.h"

/************************** Constant Definitions ***************************/


/************************** Variable Definitions ****************************/


/************************** Function Definitions ***************************/

/**
 *
 * Run a self-test on the driver/device. Note this may be a destructive test if
 * resets of the device are performed.
 *
 * If the hardware system is not built correctly, this function may never
 * return to the caller.
 *
 * @param   baseaddr_p is the base address of the SHARED_MEM_IF instance to be worked on.
 *
 * @return
 *
 *    - XST_SUCCESS   if all self-test code passed
 *    - XST_FAILURE   if any self-test code failed
 *
 * @note    Caching must be turned off for this function to work.
 * @note    Self test may fail if data memory and device are not on the same bus.
 *
 */
XStatus SHARED_MEM_IF_SelfTest(void * baseaddr_p)
{
   int     Index;
   Xuint32 baseaddr;
   Xuint8  Reg8Value;
   Xuint16 Reg16Value;
   Xuint32 Reg32Value;
   Xuint64 Reg64Value;
	Xuint16 currAddr;
	Xuint16 currData;

   /*
    * Assert the argument
    */
   XASSERT_NONVOID(baseaddr_p != XNULL);
   baseaddr = (Xuint32) baseaddr_p;

   xil_printf("******************************\n\r");
   xil_printf("* User Peripheral Self Test\n\r");
   xil_printf("******************************\n\n\r");

   /*
    * Reset the device to get it back to its default state and get module identification value
    */
   xil_printf("RST/MIR test...\n\r");
   SHARED_MEM_IF_mReset(baseaddr);
   xil_printf("   - write 0x%08x to software reset register \n\r", IPIF_RESET);
   Reg32Value = SHARED_MEM_IF_mReadMIR(baseaddr);
   if ( Reg32Value == 0x30220301 )
   {
      xil_printf("   - read 0x%08x (expected) from module identification register \n\r", Reg32Value);
      xil_printf("   - RST/MIR write/read passed\n\n\r");
   }
   else
   {
      xil_printf("   - read 0x%08x (unexpected) from module identification register, expect 0x30220301 \n\r", Reg32Value);
      xil_printf("   - RST/MIR write/read failed\n\n\r");
      return XST_FAILURE;
   }

   /*
    * Write to user logic slave module register(s) and read back
    */
	currAddr = 0;
	currData = 0xA5A5;
	
	for (; currAddr < 5; currAddr++)
	{
		xil_printf("Writing 0x%04x to address 0x%04x\n\n\r", currData, currAddr);	
		write_to_shared_mem(baseaddr, currAddr, currData);
		currData++;
	}
	
	currAddr--;
	
	for (; currAddr > 0; currAddr--)
	{
		currData = read_from_shared_mem(baseaddr, currAddr);
		xil_printf("Read 0x%04x from address 0x%04x\n\n\r", currData, currAddr);
	}
	
	currData = read_from_shared_mem(baseaddr, currAddr);
	xil_printf("Read 0x%04x from address 0x%04x\n\n\r", currData, currAddr);
	
   xil_printf("   - slave register write/read passed\n\n\r");

   return XST_SUCCESS;
}
