/***************************************************************************
 *
 * sram.c
 *
 ***************************************************************************
 *
 * N64 SRAM access
 *
 ***************************************************************************/

/*
 * includes
 */

#include "ult_64.h"
#include "sram.h"
#include "dma.h"

/*
 * defines
 */

/*
 * typedefs
 */

/*
 * structures
 */

/*
 * globals
 */

OSPiHandle	_sramHandle;

/*
 * function prototypes
 */

/*
 * code
 */


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

OSPiHandle *osSramInit(void)
{
	u32		saveMask;

	/*
	 * ensure not already initted
	 */

	if(_sramHandle.baseAddress==PHYS_TO_K1(SRAM_START_ADDR))
	{
		return(&_sramHandle);
	}

	/*
	 * fill in structure
	 */

	_sramHandle.type=DEVICE_TYPE_SRAM;
	_sramHandle.baseAddress=PHYS_TO_K1(SRAM_START_ADDR);
	_sramHandle.latency=(u8)SRAM_latency;
	_sramHandle.pulse=(u8)SRAM_pulse;
	_sramHandle.pageSize=(u8)SRAM_pageSize;
	_sramHandle.relDuration=(u8)SRAM_relDuration;
	_sramHandle.domain=1;
	memset(&(_sramHandle.transferInfo),0,sizeof(_sramHandle.transferInfo));

	/*
	 * put handle into PiTable
	 */

	saveMask=__osDisableInt();
	_sramHandle.next=__osPiTable;
	__osPiTable=&_sramHandle;
	__osRestoreInt(saveMask);
	return(&_sramHandle);

}

/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

int SRAMRead(unsigned char *data)
{
	int chan,rc;

	osInvalDCache(data,32768);
	rc=DmaStart(sramHandle,OS_READ,SRAM_START_ADDR,(unsigned long)data,32768);

	if(chan<0) return(1);

	DmaWait(chan);

	return(0);
}


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

int SRAMWrite(unsigned char *data)
{
	int chan,rc;

	osWritebackDCache(data,32768);
	rc=DmaStart(sramHandle,OS_WRITE,SRAM_START_ADDR,(unsigned long)data,32768);

	if(chan<0) return(1);

	DmaWait(chan);
	return(0);
}

/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/


/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/



/***************************************************************************
 *
 *
 *
 ***************************************************************************
 *
 *
 *
 ***************************************************************************/

