//////////////////////////////////////////////////////////////////////////////
// Filename:          C:\School\Project\MPSoC\Multiprocs/drivers/shared_mem_if_v1_00_a/src/shared_mem_if.c
// Version:           1.00.a
// Description:       shared_mem_if Driver Source File
// Date:              Thu Apr 02 10:05:07 2009 (by Create and Import Peripheral Wizard)
//////////////////////////////////////////////////////////////////////////////

#define INIT_WRITE 0xC000
#define INIT_READ 0x4000
#define MEM_REQ_DONE_FLAG 0x2000
#define NO_MEM_REQ 0x0000

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

#include "shared_mem_if.h"

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

void write_to_shared_mem(Xuint32 baseaddr, Xuint16 address, Xuint16 data)
{	
	Xuint16 status;
	   
	SHARED_MEM_IF_mWriteSlaveReg0(baseaddr, data);
	SHARED_MEM_IF_mWriteSlaveReg1(baseaddr, address);
		
	//this signals that a write should be requested
	SHARED_MEM_IF_mWriteSlaveReg2(baseaddr, INIT_WRITE);
	
	//now wait until the memory request is finished
	status = SHARED_MEM_IF_mReadSlaveReg2(baseaddr);	
	while ((status & MEM_REQ_DONE_FLAG) == 0)
	{
		status = SHARED_MEM_IF_mReadSlaveReg2(baseaddr);		
	}
	
	SHARED_MEM_IF_mWriteSlaveReg2(baseaddr, NO_MEM_REQ);
}

Xuint16 read_from_shared_mem(Xuint32 baseaddr, Xuint16 address)
{	
	Xuint16 readVal;
      	
	SHARED_MEM_IF_mWriteSlaveReg1(baseaddr, address);
	
	//this signals that a write should be requested
	SHARED_MEM_IF_mWriteSlaveReg2(baseaddr, INIT_READ);
	
	//now wait until the memory request is finished
	while ((SHARED_MEM_IF_mReadSlaveReg2(baseaddr) & MEM_REQ_DONE_FLAG) == 0);	
	
	readVal = SHARED_MEM_IF_mReadSlaveReg0(baseaddr);
	SHARED_MEM_IF_mWriteSlaveReg2(baseaddr, NO_MEM_REQ);
	
	return readVal;
} 

void init_shared_mem(Xuint32 baseaddr)
{
	SHARED_MEM_IF_mReset(baseaddr);
}
 

