project64/Source/Project64/N64 System/Mips/Sram.cpp

88 lines
2.3 KiB
C++
Raw Normal View History

2012-12-19 09:30:18 +00:00
/****************************************************************************
* *
* Project 64 - A Nintendo 64 emulator. *
* http://www.pj64-emu.com/ *
* Copyright (C) 2012 Project64. All rights reserved. *
* *
* License: *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
* *
****************************************************************************/
#include "stdafx.h"
CSram::CSram ( bool ReadOnly ) :
m_hFile(NULL),
m_ReadOnly(ReadOnly)
{
2015-03-29 17:19:28 +00:00
}
CSram::~CSram()
{
2015-03-29 17:19:28 +00:00
if (m_hFile)
{
CloseHandle(m_hFile);
m_hFile = NULL;
}
}
2015-05-02 22:14:19 +00:00
bool CSram::LoadSram()
2015-03-29 17:19:28 +00:00
{
CPath FileName;
FileName.SetDriveDirectory( g_Settings->LoadString(Directory_NativeSave).c_str());
FileName.SetName(g_Settings->LoadString(Game_GameName).c_str());
FileName.SetExtension("sra");
if (!FileName.DirectoryExists())
{
FileName.CreateDirectory();
}
m_hFile = CreateFile(FileName,m_ReadOnly ? GENERIC_READ : GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
2012-12-17 00:21:29 +00:00
WriteTraceF(TraceError,__FUNCTION__ ": Failed to open (%s), ReadOnly = %d, LastError = %X",(LPCTSTR)FileName, m_ReadOnly, GetLastError());
return false;
}
SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);
return true;
}
2015-03-29 17:19:28 +00:00
void CSram::DmaFromSram(BYTE * dest, int StartOffset, int len)
{
DWORD dwRead;
2015-03-29 17:19:28 +00:00
if (m_hFile == NULL)
{
if (!LoadSram())
{
return;
}
}
SetFilePointer(m_hFile,StartOffset,NULL,FILE_BEGIN);
ReadFile(m_hFile,dest,len,&dwRead,NULL);
}
2015-03-29 17:19:28 +00:00
void CSram::DmaToSram(BYTE * Source, int StartOffset, int len)
{
if (m_ReadOnly)
{
return;
}
2015-03-29 17:19:28 +00:00
if (m_hFile == NULL)
{
if (!LoadSram())
{
return;
}
}
DWORD dwWritten;
SetFilePointer(m_hFile,StartOffset,NULL,FILE_BEGIN);
WriteFile(m_hFile,Source,len,&dwWritten,NULL);
FlushFileBuffers(m_hFile);
}