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 *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-06-29 02:11:22 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
CSram::CSram ( bool ReadOnly ) :
|
2015-09-23 09:00:51 +00:00
|
|
|
m_ReadOnly(ReadOnly),
|
|
|
|
m_hFile(NULL)
|
2010-06-29 02:11:22 +00:00
|
|
|
{
|
2015-03-29 17:19:28 +00:00
|
|
|
|
2010-06-29 02:11:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 22:19:02 +00:00
|
|
|
CSram::~CSram()
|
2010-06-29 02:11:22 +00:00
|
|
|
{
|
2015-03-29 17:19:28 +00:00
|
|
|
if (m_hFile)
|
|
|
|
{
|
2010-06-29 02:11:22 +00:00
|
|
|
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
|
|
|
{
|
2010-06-29 02:11:22 +00:00
|
|
|
CPath FileName;
|
|
|
|
|
2012-11-17 01:02:04 +00:00
|
|
|
FileName.SetDriveDirectory( g_Settings->LoadString(Directory_NativeSave).c_str());
|
|
|
|
FileName.SetName(g_Settings->LoadString(Game_GameName).c_str());
|
2010-06-29 02:11:22 +00:00
|
|
|
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());
|
2010-06-29 02:11:22 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-06-29 02:11:22 +00:00
|
|
|
DWORD dwRead;
|
2015-08-01 08:48:38 +00:00
|
|
|
DWORD i;
|
|
|
|
BYTE tmp[4];
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
{
|
|
|
|
if (!LoadSram())
|
|
|
|
{
|
2010-06-29 02:11:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 08:48:38 +00:00
|
|
|
DWORD Offset = StartOffset & 3;
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-08-01 08:48:38 +00:00
|
|
|
if (Offset == 0)
|
|
|
|
{
|
|
|
|
SetFilePointer(m_hFile, StartOffset, NULL, FILE_BEGIN);
|
|
|
|
ReadFile(m_hFile, dest, len, &dwRead, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetFilePointer(m_hFile, StartOffset - Offset, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
ReadFile(m_hFile, tmp, 4, &dwRead, NULL);
|
|
|
|
for (i = 0; i < (4 - Offset); i++)
|
|
|
|
{
|
|
|
|
dest[i + Offset] = tmp[i];
|
|
|
|
}
|
|
|
|
for (i = 4 - Offset; i < len - Offset; i += 4)
|
|
|
|
{
|
|
|
|
ReadFile(m_hFile, tmp, 4, &dwRead, NULL);
|
|
|
|
switch (Offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
dest[i + 2] = tmp[0];
|
|
|
|
dest[i + 3] = tmp[1];
|
|
|
|
dest[i + 4] = tmp[2];
|
|
|
|
dest[i - 3] = tmp[3];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dest[i + 4] = tmp[0];
|
|
|
|
dest[i + 5] = tmp[1];
|
|
|
|
dest[i - 2] = tmp[2];
|
|
|
|
dest[i - 1] = tmp[3];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
dest[i + 6] = tmp[0];
|
|
|
|
dest[i - 1] = tmp[1];
|
|
|
|
dest[i] = tmp[2];
|
|
|
|
dest[i + 1] = tmp[3];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ReadFile(m_hFile, tmp, 4, &dwRead, NULL);
|
|
|
|
switch (Offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
dest[i - 3] = tmp[3];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dest[i - 2] = tmp[2];
|
|
|
|
dest[i - 1] = tmp[3];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
dest[i - 1] = tmp[1];
|
|
|
|
dest[i] = tmp[2];
|
|
|
|
dest[i + 1] = tmp[3];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-29 02:11:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
void CSram::DmaToSram(BYTE * Source, int StartOffset, int len)
|
|
|
|
{
|
2015-08-01 08:48:38 +00:00
|
|
|
DWORD dwWritten;
|
|
|
|
DWORD i;
|
|
|
|
BYTE tmp[4];
|
|
|
|
|
2010-06-29 02:11:22 +00:00
|
|
|
if (m_ReadOnly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-29 17:19:28 +00:00
|
|
|
if (m_hFile == NULL)
|
|
|
|
{
|
|
|
|
if (!LoadSram())
|
|
|
|
{
|
2010-06-29 02:11:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 08:48:38 +00:00
|
|
|
DWORD Offset = StartOffset & 3;
|
|
|
|
if (Offset == 0)
|
|
|
|
{
|
|
|
|
SetFilePointer(m_hFile, StartOffset, NULL, FILE_BEGIN);
|
|
|
|
WriteFile(m_hFile, Source, len, &dwWritten, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < (4 - Offset); i++)
|
|
|
|
{
|
|
|
|
tmp[i] = Source[i + Offset];
|
|
|
|
}
|
|
|
|
SetFilePointer(m_hFile, StartOffset - Offset, NULL, FILE_BEGIN);
|
|
|
|
WriteFile(m_hFile, tmp, (4 - Offset), &dwWritten, NULL);
|
|
|
|
|
|
|
|
SetFilePointer(m_hFile, Offset, NULL, FILE_CURRENT);
|
|
|
|
for (i = 4 - Offset; i < len - Offset; i += 4)
|
|
|
|
{
|
|
|
|
switch (Offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
tmp[0] = Source[i + 2];
|
|
|
|
tmp[1] = Source[i + 3];
|
|
|
|
tmp[2] = Source[i + 4];
|
|
|
|
tmp[3] = Source[i - 3];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp[0] = Source[i + 4];
|
|
|
|
tmp[1] = Source[i + 5];
|
|
|
|
tmp[2] = Source[i - 2];
|
|
|
|
tmp[3] = Source[i - 1];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
tmp[0] = Source[i + 6];
|
|
|
|
tmp[1] = Source[i - 1];
|
|
|
|
tmp[2] = Source[i];
|
|
|
|
tmp[3] = Source[i + 1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WriteFile(m_hFile, tmp, 4, &dwWritten, NULL);
|
|
|
|
}
|
|
|
|
switch (Offset)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
tmp[0] = Source[i - 3];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp[0] = Source[i - 2];
|
|
|
|
tmp[0] = Source[i - 1];
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
tmp[0] = Source[i - 1];
|
|
|
|
tmp[0] = Source[i];
|
|
|
|
tmp[0] = Source[i + 1];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SetFilePointer(m_hFile, 4 - Offset, NULL, FILE_CURRENT);
|
|
|
|
WriteFile(m_hFile, tmp, Offset, &dwWritten, NULL);
|
|
|
|
}
|
2010-06-29 02:11:22 +00:00
|
|
|
FlushFileBuffers(m_hFile);
|
|
|
|
}
|