// Sdram.cpp: implementation of the CSdram class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CSdram.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSdram::CSdram()
{
	m_pBlock = NULL;
	m_pCache = NULL;
	m_pPage = NULL;
}

CSdram::~CSdram()
{
}

BOOL CSdram::Init(int iBlockSize, int iCacheSize)
{
	m_pCache = (unsigned char *)malloc(iCacheSize);
	if (m_pCache == NULL)
	{
		return FALSE;
	}

	m_pBlock = (unsigned char *)malloc(iBlockSize);
	if (m_pBlock == NULL)
	{
		free(m_pCache);
		return FALSE;
	}
	m_pPage = m_pBlock;
	return TRUE;
}

void CSdram::Free()
{
	if (m_pCache != NULL)
	{
		free(m_pCache);
		m_pCache = NULL;
	}
	if (m_pBlock != NULL)
	{
		free(m_pBlock);
		m_pBlock = NULL;
	}
}

void CSdram::SetPage(int iAddressHi, int iAddressLo)
{
	m_pPage = m_pBlock + (iAddressHi << 16) + (iAddressLo << 8);
}

void CSdram::SetBuffer(unsigned char iAddress, unsigned char * pData, int iLength)
{
	memcpy((char *)(m_pPage + iAddress), pData, iLength);
}

void CSdram::SetString(unsigned char iAddress, unsigned char * pData, int iLength)
{
	int i;
	memset((char *)(m_pPage + iAddress), 0, iLength);
	for (i = 0; pData[i] != 0; i ++)
	{
		m_pPage[iAddress+i] = pData[i];
	}
}
