2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2015-11-10 05:21:49 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2012-12-19 09:30:18 +00:00
|
|
|
* 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"
|
2016-04-18 21:29:24 +00:00
|
|
|
#include <Project64-core/N64System/Mips/Sram.h>
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Common/path.h>
|
2010-06-29 02:11:22 +00:00
|
|
|
|
2015-12-13 07:07:03 +00:00
|
|
|
CSram::CSram(bool ReadOnly) :
|
2016-04-18 21:29:24 +00:00
|
|
|
m_ReadOnly(ReadOnly)
|
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-05-02 22:14:19 +00:00
|
|
|
bool CSram::LoadSram()
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2016-04-18 21:29:24 +00:00
|
|
|
CPath FileName(g_Settings->LoadStringVal(Directory_NativeSave).c_str(), "");
|
2016-04-21 20:29:55 +00:00
|
|
|
if (g_Settings->LoadBool(Setting_UniqueSaveDir))
|
|
|
|
{
|
|
|
|
FileName.AppendDirectory(g_Settings->LoadStringVal(Game_UniqueSaveDir).c_str());
|
|
|
|
}
|
2015-12-13 07:07:03 +00:00
|
|
|
FileName.SetName(g_Settings->LoadStringVal(Game_GameName).c_str());
|
|
|
|
FileName.SetExtension("sra");
|
|
|
|
|
|
|
|
if (!FileName.DirectoryExists())
|
|
|
|
{
|
|
|
|
FileName.DirectoryCreate();
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:29:24 +00:00
|
|
|
if (!m_File.Open(FileName, (m_ReadOnly ? CFileBase::modeRead : CFileBase::modeReadWrite) | CFileBase::modeNoTruncate | CFileBase::modeCreate))
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
2016-04-18 21:29:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WriteTrace(TraceN64System, TraceError, "Failed to open (%s), ReadOnly = %d, LastError = %X", (const char *)FileName, m_ReadOnly, GetLastError());
|
|
|
|
#else
|
|
|
|
WriteTrace(TraceN64System, TraceError, "Failed to open (%s), ReadOnly = %d", (const char *)FileName, m_ReadOnly);
|
|
|
|
#endif
|
2015-12-13 07:07:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 21:29:24 +00:00
|
|
|
m_File.SeekToBegin();
|
2015-12-13 07:07:03 +00:00
|
|
|
return true;
|
2010-06-29 02:11:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 10:02:01 +00:00
|
|
|
void CSram::DmaFromSram(uint8_t * dest, int32_t StartOffset, int32_t len)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-12-13 07:07:03 +00:00
|
|
|
uint32_t i;
|
|
|
|
|
2016-04-18 21:29:24 +00:00
|
|
|
if (!m_File.IsOpen())
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
|
|
|
if (!LoadSram())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix Dezaemon 3D saves
|
|
|
|
StartOffset = ((StartOffset >> 3) & 0xFFFF8000) | (StartOffset & 0x7FFF);
|
|
|
|
|
2016-06-04 01:33:55 +00:00
|
|
|
if (((StartOffset & 3) == 0) && ((((uint32_t)dest) & 3) == 0))
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
2016-04-18 21:29:24 +00:00
|
|
|
m_File.Seek(StartOffset, CFile::begin);
|
|
|
|
m_File.Read(dest, len);
|
2015-12-13 07:07:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-04 01:33:55 +00:00
|
|
|
for (i = 0; i < len; i++)
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
2016-06-04 01:33:55 +00:00
|
|
|
m_File.Seek((StartOffset + i) ^ 3, CFile::begin);
|
|
|
|
m_File.Read((uint8_t*)(((uint32_t)dest + i) ^ 3), 1);
|
2015-12-13 07:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-29 02:11:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 10:02:01 +00:00
|
|
|
void CSram::DmaToSram(uint8_t * Source, int32_t StartOffset, int32_t len)
|
2015-03-29 17:19:28 +00:00
|
|
|
{
|
2015-12-13 07:07:03 +00:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (m_ReadOnly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:29:24 +00:00
|
|
|
if (!m_File.IsOpen())
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
|
|
|
if (!LoadSram())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix Dezaemon 3D saves
|
|
|
|
StartOffset = ((StartOffset >> 3) & 0xFFFF8000) | (StartOffset & 0x7FFF);
|
|
|
|
|
2016-06-04 01:33:55 +00:00
|
|
|
if (((StartOffset & 3) == 0) && ((((uint32_t)Source) & 3) == 0) && NULL != NULL)
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
2016-04-18 21:29:24 +00:00
|
|
|
m_File.Seek(StartOffset, CFile::begin);
|
|
|
|
m_File.Write(Source, len);
|
2015-12-13 07:07:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-04 01:33:55 +00:00
|
|
|
for (i = 0; i < len; i++)
|
2015-12-13 07:07:03 +00:00
|
|
|
{
|
2016-06-04 01:33:55 +00:00
|
|
|
m_File.Seek((StartOffset + i) ^ 3, CFile::begin);
|
|
|
|
m_File.Write((uint8_t*)(((uint32_t)Source + i) ^ 3), 1);
|
2015-12-13 07:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 21:56:32 +00:00
|
|
|
}
|