#include "p_apps.h"

static XCHAR _iReadBufLen, _iReadSdramHi, _iReadSdramLo;
static XCHAR _pReadBuf[256];
static XCHAR _pReadBuf2[256];

void ReadFileOpen(UCHAR iSdramHi)
{
	_iReadSdramHi = iSdramHi;
	_iReadSdramLo = 0;
	_iReadBufLen = 0;
}

void ReadFile(UCHAR iLength)
{
	DCHAR iLength2;

	if (iLength && iLength <= _iReadBufLen)
	{
		p_memcpy_x2x((PXCHAR)Sdram_pDataCache, _pReadBuf, iLength);
		_iReadBufLen -= iLength;
		if (_iReadBufLen)
		{
			p_memcpy_x2x(_pReadBuf, (PXCHAR)(_pReadBuf+iLength), _iReadBufLen);
		}
	}
	else
	{
		sdram_read(_iReadSdramHi, _iReadSdramLo, SRAM_DMA_HALF_DWORD);
		_iReadSdramLo ++;
		if (!_iReadSdramLo)
		{
			if (_iReadSdramHi < 0x1f)
			{
				_iReadSdramHi ++;
			}
		}

		p_memcpy_x2x(_pReadBuf2, (PXCHAR)Sdram_pDataCache, (UCHAR)256);
		p_memcpy_x2x((PXCHAR)Sdram_pDataCache, _pReadBuf, _iReadBufLen);
		iLength2 = iLength - _iReadBufLen;
		p_memcpy_x2x((PXCHAR)(Sdram_pDataCache+_iReadBufLen), _pReadBuf2, iLength2);
		_iReadBufLen = 256-iLength2;
		p_memcpy_x2x(_pReadBuf, (PXCHAR)(_pReadBuf2+iLength2), _iReadBufLen);
	}
}

