2016-01-27 09:49:26 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <Project64-core/N64System/Mips/FlashRam.h>
|
|
|
|
#include <Project64-core/N64System/SystemGlobals.h>
|
|
|
|
#include <Project64-core/N64System/Mips/MemoryVirtualMem.h>
|
|
|
|
#include <Common/path.h>
|
|
|
|
|
|
|
|
CFlashram::CFlashram(bool ReadOnly) :
|
2021-04-12 11:35:39 +00:00
|
|
|
m_FlashRamPointer(nullptr),
|
2016-07-12 21:56:32 +00:00
|
|
|
m_FlashFlag(FLASHRAM_MODE_NOPES),
|
|
|
|
m_FlashStatus(0),
|
|
|
|
m_FlashRAM_Offset(0),
|
|
|
|
m_ReadOnly(ReadOnly)
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CFlashram::~CFlashram()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFlashram::DmaFromFlashram(uint8_t * dest, int32_t StartOffset, int32_t len)
|
|
|
|
{
|
2016-04-12 20:55:54 +00:00
|
|
|
uint8_t FlipBuffer[0x10000];
|
|
|
|
|
2016-01-27 09:49:26 +00:00
|
|
|
switch (m_FlashFlag)
|
|
|
|
{
|
|
|
|
case FLASHRAM_MODE_READ:
|
2016-04-11 08:28:18 +00:00
|
|
|
if (!m_File.IsOpen() && !LoadFlashram())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2016-04-11 08:28:18 +00:00
|
|
|
return;
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
2016-04-12 15:50:57 +00:00
|
|
|
if (len > sizeof(FlipBuffer))
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
g_Notify->DisplayError(stdstr_f("%s: DmaFromFlashram FlipBuffer to small (len: %d)", __FUNCTION__, len).c_str());
|
|
|
|
}
|
2016-04-12 15:50:57 +00:00
|
|
|
len = sizeof(FlipBuffer);
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
if ((len & 3) != 0)
|
|
|
|
{
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
g_Notify->DisplayError(stdstr_f("%s: Unaligned flash RAM read?", __FUNCTION__).c_str());
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(FlipBuffer, 0, sizeof(FlipBuffer));
|
|
|
|
StartOffset = StartOffset << 1;
|
2016-04-11 08:28:18 +00:00
|
|
|
m_File.Seek(StartOffset, CFile::begin);
|
|
|
|
m_File.Read(FlipBuffer, len);
|
|
|
|
|
|
|
|
for (int32_t count = m_File.GetLength(); count < len; count++)
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
FlipBuffer[count] = 0xFF;
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:28:18 +00:00
|
|
|
for (int32_t count = 0; count < len; count += 4)
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2016-04-11 08:28:18 +00:00
|
|
|
*(uint32_t *)(dest + count) = *(uint32_t *)&FlipBuffer[count];
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FLASHRAM_MODE_STATUS:
|
|
|
|
if (StartOffset != 0 && len != 8)
|
|
|
|
{
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
g_Notify->DisplayError(stdstr_f("%s: Reading m_FlashStatus not being handled correctly\nStart: %X len: %X", __FUNCTION__, StartOffset, len).c_str());
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 15:50:57 +00:00
|
|
|
*((uint32_t *)(dest)+0) = (uint32_t)((m_FlashStatus >> 32) & 0xFFFFFFFF);
|
2016-01-27 09:49:26 +00:00
|
|
|
*((uint32_t *)(dest)+1) = (uint32_t)(m_FlashStatus & 0xFFFFFFFF);
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
g_Notify->DisplayError(stdstr_f("%s: Start: %X, Offset: %X len: %X", __FUNCTION__, dest - g_MMU->Rdram(), StartOffset, len).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFlashram::DmaToFlashram(uint8_t * Source, int32_t StartOffset, int32_t len)
|
|
|
|
{
|
|
|
|
switch (m_FlashFlag)
|
|
|
|
{
|
|
|
|
case FLASHRAM_MODE_WRITE:
|
|
|
|
m_FlashRamPointer = Source;
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
g_Notify->DisplayError(stdstr_f("%s: Start: %X, Offset: %X len: %X", __FUNCTION__, Source - g_MMU->Rdram(), StartOffset, len).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CFlashram::ReadFromFlashStatus(uint32_t PAddr)
|
|
|
|
{
|
|
|
|
switch (PAddr)
|
|
|
|
{
|
|
|
|
case 0x08000000: return (uint32_t)(m_FlashStatus >> 32);
|
|
|
|
default:
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
|
|
|
g_Notify->DisplayError(stdstr_f("%s: PAddr (%X)", __FUNCTION__, PAddr).c_str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (uint32_t)(m_FlashStatus >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFlashram::LoadFlashram()
|
|
|
|
{
|
2016-09-29 11:30:00 +00:00
|
|
|
CPath FileName(g_Settings->LoadStringVal(Directory_NativeSave).c_str(), stdstr_f("%s.fla", g_Settings->LoadStringVal(Game_GameName).c_str()).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());
|
|
|
|
}
|
2018-11-20 03:41:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
FileName.NormalizePath(CPath(CPath::MODULE_DIRECTORY));
|
|
|
|
#endif
|
2016-01-27 09:49:26 +00:00
|
|
|
|
|
|
|
if (!FileName.DirectoryExists())
|
|
|
|
{
|
|
|
|
FileName.DirectoryCreate();
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:28:18 +00:00
|
|
|
if (!m_File.Open(FileName, (m_ReadOnly ? CFileBase::modeRead : CFileBase::modeReadWrite) | CFileBase::modeNoTruncate | CFileBase::modeCreate))
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2016-04-11 08:28:18 +00:00
|
|
|
WriteTrace(TraceN64System, TraceError, "Failed to open (%s), ReadOnly = %d", (const char *)FileName, m_ReadOnly);
|
2016-01-27 09:49:26 +00:00
|
|
|
g_Notify->DisplayError(GS(MSG_FAIL_OPEN_FLASH));
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-11 08:28:18 +00:00
|
|
|
m_File.SeekToBegin();
|
2016-01-27 09:49:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFlashram::WriteToFlashCommand(uint32_t FlashRAM_Command)
|
|
|
|
{
|
2016-04-12 15:50:57 +00:00
|
|
|
uint8_t EmptyBlock[16 * sizeof(int64_t)];
|
2016-01-27 09:49:26 +00:00
|
|
|
|
|
|
|
switch (FlashRAM_Command & 0xFF000000)
|
|
|
|
{
|
|
|
|
case 0xD2000000:
|
|
|
|
switch (m_FlashFlag)
|
|
|
|
{
|
|
|
|
case FLASHRAM_MODE_NOPES: break;
|
|
|
|
case FLASHRAM_MODE_READ: break;
|
|
|
|
case FLASHRAM_MODE_STATUS: break;
|
|
|
|
case FLASHRAM_MODE_ERASE:
|
|
|
|
memset(EmptyBlock, 0xFF, sizeof(EmptyBlock));
|
2016-04-11 08:28:18 +00:00
|
|
|
if (!m_File.IsOpen() && !LoadFlashram())
|
|
|
|
{
|
|
|
|
return;
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
2016-12-12 19:13:10 +00:00
|
|
|
if (!m_ReadOnly)
|
|
|
|
{
|
|
|
|
m_File.Seek(m_FlashRAM_Offset, CFile::begin);
|
|
|
|
m_File.Write(EmptyBlock, sizeof(EmptyBlock));
|
|
|
|
}
|
2016-01-27 09:49:26 +00:00
|
|
|
break;
|
|
|
|
case FLASHRAM_MODE_WRITE:
|
2016-04-11 08:28:18 +00:00
|
|
|
if (!m_File.IsOpen() && !LoadFlashram())
|
|
|
|
{
|
|
|
|
return;
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
{
|
2016-04-12 15:50:57 +00:00
|
|
|
uint8_t FlipBuffer[sizeof(EmptyBlock)];
|
2016-01-27 09:49:26 +00:00
|
|
|
uint8_t * FlashRamPointer = m_FlashRamPointer;
|
|
|
|
|
|
|
|
memset(FlipBuffer, 0, sizeof(FlipBuffer));
|
2016-04-12 15:50:57 +00:00
|
|
|
memcpy(&FlipBuffer[0], FlashRamPointer, sizeof(EmptyBlock));
|
2016-01-27 09:49:26 +00:00
|
|
|
|
2016-12-12 19:13:10 +00:00
|
|
|
if (!m_ReadOnly)
|
|
|
|
{
|
|
|
|
m_File.Seek(m_FlashRAM_Offset, CFile::begin);
|
|
|
|
m_File.Write(FlipBuffer, sizeof(EmptyBlock));
|
|
|
|
}
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-05-18 11:51:36 +00:00
|
|
|
g_Notify->DisplayError(stdstr_f("Writing %X to flash RAM command register\nm_FlashFlag: %d", FlashRAM_Command, m_FlashFlag).c_str());
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
m_FlashFlag = FLASHRAM_MODE_NOPES;
|
|
|
|
break;
|
|
|
|
case 0xE1000000:
|
|
|
|
m_FlashFlag = FLASHRAM_MODE_STATUS;
|
|
|
|
m_FlashStatus = 0x1111800100C2001E;
|
|
|
|
break;
|
|
|
|
case 0xF0000000:
|
2016-03-21 13:59:13 +00:00
|
|
|
case 0x00000000:
|
2016-01-27 09:49:26 +00:00
|
|
|
m_FlashFlag = FLASHRAM_MODE_READ;
|
|
|
|
m_FlashStatus = 0x11118004F0000000;
|
|
|
|
break;
|
|
|
|
case 0x4B000000:
|
2016-04-12 15:50:57 +00:00
|
|
|
m_FlashRAM_Offset = (FlashRAM_Command & 0xFFFF) * sizeof(EmptyBlock);
|
2016-01-27 09:49:26 +00:00
|
|
|
break;
|
|
|
|
case 0x78000000:
|
|
|
|
m_FlashFlag = FLASHRAM_MODE_ERASE;
|
|
|
|
m_FlashStatus = 0x1111800800C2001E;
|
|
|
|
break;
|
|
|
|
case 0xB4000000:
|
2021-05-18 11:51:36 +00:00
|
|
|
m_FlashFlag = FLASHRAM_MODE_WRITE;
|
2016-01-27 09:49:26 +00:00
|
|
|
break;
|
|
|
|
case 0xA5000000:
|
2016-04-12 15:50:57 +00:00
|
|
|
m_FlashRAM_Offset = (FlashRAM_Command & 0xFFFF) * sizeof(EmptyBlock);
|
2016-01-27 09:49:26 +00:00
|
|
|
m_FlashStatus = 0x1111800400C2001E;
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-15 21:23:21 +00:00
|
|
|
if (HaveDebugger())
|
2016-01-27 09:49:26 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
g_Notify->DisplayError(stdstr_f("Writing %X to flash RAM command register", FlashRAM_Command).c_str());
|
2016-01-27 09:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 21:56:32 +00:00
|
|
|
}
|