aboutsummaryrefslogtreecommitdiff
path: root/src/DSi_SD.cpp
diff options
context:
space:
mode:
authorJesse Talavera-Greenberg <jesse@jesse.tg>2023-10-11 11:20:05 -0400
committerGitHub <noreply@github.com>2023-10-11 17:20:05 +0200
commitd4e51f80601f57399db49f1010c45427bd2bf3c4 (patch)
tree206985e7bedfc1755941eeba26d6605d4e03fa0a /src/DSi_SD.cpp
parentb2fcff97c186cc9db263089acd4810ea7d58517d (diff)
Refactor DSi_NAND (#1844)
* Refactor diskio's contents - Change ff_disk_read_cb/write_cb into a std::function instead of a raw pointer - Add const specifiers as needed * Refactor DSi_NAND to manage the file system's mounted lifetime with RAII * Split NANDMount into NANDMount and NANDImage - NANDImage is used for information about the NAND that doesn't require decryption or filesystem access - NANDMount is used to actually access the file system - Both classes manage their respective resources (the NAND file handle and the NAND's mount) with RAII - Also split the file loading into another function that I will remove in a later PR * Make NANDMount immovable * Remove NAND-loading code that I had sectioned off into a function - Incomplete copypasta - I must have gotten distracted * Tidy up NANDImage's initialization - Don't unmount the disk image if the constructor fails (that's NANDMount's job now) - Only assign CurFile if the constructor succeeds * Add some const-correctness * Move DSi NAND initialization to the frontend - The NANDImage is now installed via a unique_ptr in DSi * Remove Platform::DSi_NANDPath - Not Config::DSiNANDPath; that can still be configured as usual - The core no longer needs to care
Diffstat (limited to 'src/DSi_SD.cpp')
-rw-r--r--src/DSi_SD.cpp39
1 files changed, 15 insertions, 24 deletions
diff --git a/src/DSi_SD.cpp b/src/DSi_SD.cpp
index 4f287b1..158ae1e 100644
--- a/src/DSi_SD.cpp
+++ b/src/DSi_SD.cpp
@@ -20,6 +20,7 @@
#include <string.h>
#include "DSi.h"
#include "DSi_SD.h"
+#include "DSi_NAND.h"
#include "DSi_NWifi.h"
#include "Platform.h"
@@ -137,11 +138,8 @@ void DSi_SDHost::Reset()
else
sd = nullptr;
- std::string nandpath = Platform::GetConfigString(Platform::DSi_NANDPath);
- std::string instnand = nandpath + Platform::InstanceFileSuffix();
-
- mmc = new DSi_MMCStorage(this, true, instnand);
- mmc->SetCID(DSi::eMMC_CID);
+ mmc = new DSi_MMCStorage(this, *DSi::NANDImage);
+ mmc->SetCID(DSi::NANDImage->GetEMMCID().data());
Ports[0] = sd;
Ports[1] = mmc;
@@ -768,14 +766,9 @@ void DSi_SDHost::CheckSwapFIFO()
#define MMC_DESC (Internal?"NAND":"SDcard")
-DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, bool internal, const std::string& filename)
- : DSi_SDDevice(host)
+DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, DSi_NAND::NANDImage& nand)
+ : DSi_SDDevice(host), Internal(true), NAND(&nand), SD(nullptr)
{
- Internal = internal;
- File = Platform::OpenLocalFile(filename, FileMode::ReadWriteExisting);
-
- SD = nullptr;
-
ReadOnly = false;
}
@@ -783,7 +776,7 @@ DSi_MMCStorage::DSi_MMCStorage(DSi_SDHost* host, bool internal, const std::strin
: DSi_SDDevice(host)
{
Internal = internal;
- File = nullptr;
+ NAND = nullptr;
SD = new FATStorage(filename, size, readonly, sourcedir);
SD->Open();
@@ -798,10 +791,8 @@ DSi_MMCStorage::~DSi_MMCStorage()
SD->Close();
delete SD;
}
- if (File)
- {
- CloseFile(File);
- }
+
+ // Do not close the NANDImage, it's not owned by this object
}
void DSi_MMCStorage::Reset()
@@ -925,7 +916,7 @@ void DSi_MMCStorage::SendCMD(u8 cmd, u32 param)
case 12: // stop operation
SetState(0x04);
- if (File) FileFlush(File);
+ if (NAND) FileFlush(NAND->GetFile());
RWCommand = 0;
Host->SendResponse(CSR, true);
return;
@@ -1052,10 +1043,10 @@ u32 DSi_MMCStorage::ReadBlock(u64 addr)
{
SD->ReadSectors((u32)(addr >> 9), 1, data);
}
- else if (File)
+ else if (NAND)
{
- FileSeek(File, addr, FileSeekOrigin::Start);
- FileRead(&data[addr & 0x1FF], 1, len, File);
+ FileSeek(NAND->GetFile(), addr, FileSeekOrigin::Start);
+ FileRead(&data[addr & 0x1FF], 1, len, NAND->GetFile());
}
return Host->DataRX(&data[addr & 0x1FF], len);
@@ -1082,10 +1073,10 @@ u32 DSi_MMCStorage::WriteBlock(u64 addr)
{
SD->WriteSectors((u32)(addr >> 9), 1, data);
}
- else if (File)
+ else if (NAND)
{
- FileSeek(File, addr, FileSeekOrigin::Start);
- FileWrite(&data[addr & 0x1FF], 1, len, File);
+ FileSeek(NAND->GetFile(), addr, FileSeekOrigin::Start);
+ FileWrite(&data[addr & 0x1FF], 1, len, NAND->GetFile());
}
}
}