// BKMSTD11.cpp: implementation of the CBKMSTD11 class.
//


#include "pch.h"
#include "resource.h"
#include "BKMSTD11.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
IMPLEMENT_DYNAMIC(CBKMSTD11, CBKMPIDevice)

CBKMSTD11::CBKMSTD11()
    = default;

CBKMSTD11::~CBKMSTD11()
    = default;

bool CBKMSTD11::InitDevice(const uint16_t addrStart)
{
	// инициализация карты памяти
	return OnInitDevice();
}

bool CBKMSTD11::OnInitDevice()
{
	static const CBKMPIDevice::BoardRomList_t m_arrMSTD11RomList[MSTD11RomListSize] =
	{
		// инициализируем список загружаемых ПЗУ
		{ IDS_INI_BK11_RE2_203_MSTD, BRD_11_TESTS_BNK }
	};
	// инициализация карты памяти
	return InitMemoryModules(m_arrMSTD11RomList, MSTD11RomListSize);
}


bool CBKMSTD11::GetByteT(const uint16_t addr, uint8_t *pValue, int &nTC)
{
	if (0160000 <= addr && addr < m_nBKPortsIOArea)
	{
		nTC = m_pMemoryMap[BRD_11_TESTS_BNK].nTimingCorrection;
		*pValue = m_pMemory[addr];
		return true;
	}

	nTC = 0;
	*pValue = 0;
	return false;
}

bool CBKMSTD11::GetWordT(const uint16_t addr, uint16_t *pValue, int &nTC)
{
	if (0160000 <= addr && addr < m_nBKPortsIOArea)
	{
		nTC = m_pMemoryMap[BRD_11_TESTS_BNK].nTimingCorrection;
		*pValue = *(reinterpret_cast<uint16_t *>(&m_pMemory[addr & 0177776]));
		return true;
	}

	nTC = 0;
	*pValue = 0;
	return false;
}


bool CBKMSTD11::SetByteTInternal(const uint16_t addr, uint8_t value, int &nTC)
{
	if (0160000 <= addr && addr < m_nBKPortsIOArea)
	{
		nTC = m_pMemoryMap[BRD_11_TESTS_BNK].nTimingCorrection;
		m_pMemory[addr] = value;
		return true;
	}

	nTC = 0;
	return false;
}

bool CBKMSTD11::SetWordTInternal(const uint16_t addr, uint16_t value, int &nTC)
{
	if (0160000 <= addr && addr < m_nBKPortsIOArea)
	{
		nTC = m_pMemoryMap[BRD_11_TESTS_BNK].nTimingCorrection;
		*(reinterpret_cast<uint16_t *>(&m_pMemory[addr & 0177776])) = value;
		return true;
	}

	nTC = 0;
	return false;
}

void CBKMSTD11::MemoryManager()
{
	CBKMPIDevice::MemoryManager();
	m_pMemoryMap[BRD_11_TESTS_BNK].bReadable = !!(m_nROMPresent & (1 << BRD_11_TESTS_BNK));
	m_pMemoryMap[BRD_11_TESTS_BNK].nTimingCorrection = ROM_TIMING_CORR_VALUE;
}


bool CBKMSTD11::RestoreMemory(DWORD id, CMSFManager *pMsf)
{
	if (pMsf->IsLoad())
	{
		return pMsf->GetBlockBaseMemory(id, GetMainMemory(), m_nMemorySize);
	}

	return pMsf->SetBlockBaseMemoryEx(id, GetMainMemory(), m_nMemorySize, true);
}

bool CBKMSTD11::RestoreMemoryMap(DWORD id, CMSFManager *pMsf)
{
	if (pMsf->IsLoad())
	{
		return pMsf->GetBlockMemMap(id, m_pMemoryMap.data(), DEC_BANKS_NUM, m_nROMPresent);
	}

	return pMsf->SetBlockMemMap(id, m_pMemoryMap.data(), DEC_BANKS_NUM, m_nROMPresent);
}

