Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the SRAM's representation in NDSCartArgs #1914

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/NDSCart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ CartRetail::CartRetail(std::unique_ptr<u8[]>&& rom, u32 len, u32 chipid, bool ba
{ // Copy in what we can, truncate the rest.
SRAM = std::make_unique<u8[]>(SRAMLength);
memset(SRAM.get(), 0xFF, SRAMLength);
memcpy(SRAM.get(), sram.get(), std::min(sramlen, SRAMLength));

if (sram)
{ // If we have anything to copy, that is.
memcpy(SRAM.get(), sram.get(), std::min(sramlen, SRAMLength));
}
}
}

Expand Down Expand Up @@ -1650,7 +1654,8 @@ std::unique_ptr<CartCommon> ParseROM(std::unique_ptr<u8[]>&& romdata, u32 romlen
}

std::unique_ptr<CartCommon> cart;
auto [sram, sramlen] = args ? std::move(*args->SRAM) : std::make_pair(nullptr, 0);
std::unique_ptr<u8[]> sram = args ? std::move(args->SRAM) : nullptr;
u32 sramlen = args ? args->SRAMLength : 0;
if (homebrew)
cart = std::make_unique<CartHomebrew>(std::move(cartrom), cartromsize, cartid, romparams, args ? std::move(args->SDCard) : std::nullopt);
else if (gametitle[0] == 0 && !strncmp("SD/TF-NDS", gametitle + 1, 9) && gamecode == 0x414D5341)
Expand Down
9 changes: 7 additions & 2 deletions src/NDSCart.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ struct NDSCartArgs
std::optional<FATStorageArgs> SDCard = std::nullopt;

/// Save RAM to load into the cartridge.
/// If \c nullopt, then the cart's SRAM buffer will be empty.
/// If \c nullptr, then the cart's SRAM buffer will be empty.
/// Ignored for homebrew ROMs.
std::optional<std::pair<std::unique_ptr<u8[]>, u32>> SRAM = std::nullopt;
std::unique_ptr<u8[]> SRAM = nullptr;

/// The length of the buffer in SRAM.
/// If 0, then the cart's SRAM buffer will be empty.
/// Ignored for homebrew ROMs.
u32 SRAMLength = 0;
};

// CartCommon -- base code shared by all cart types
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/qt_sdl/ROMManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,8 @@ bool LoadROM(EmuThread* emuthread, QStringList filepath, bool reset)
// the ROM is homebrew or not.
// So this is the card we *would* load if the ROM were homebrew.
.SDCard = GetDLDISDCardArgs(),

.SRAM = std::make_pair(std::move(savedata), savelen),
.SRAM = std::move(savedata),
.SRAMLength = savelen,
};

auto cart = NDSCart::ParseROM(std::move(filedata), filelen, std::move(cartargs));
Expand Down
Loading