Skip to content

Commit

Permalink
HW: Remove global state from functions in Sram.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Sep 19, 2022
1 parent 720b3f5 commit 9963637
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/EXI/EXI.cpp
Expand Up @@ -106,7 +106,7 @@ void Init()
{
if (!g_SRAM_netplay_initialized)
{
InitSRAM();
InitSRAM(&g_SRAM, SConfig::GetInstance().m_strSRAM);
}

CEXIMemoryCard::Init();
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp
Expand Up @@ -136,7 +136,7 @@ CEXIIPL::CEXIIPL()
// you please
g_SRAM.settings.language = Config::Get(Config::MAIN_GC_LANGUAGE);
g_SRAM.settings.rtc_bias = 0;
FixSRAMChecksums();
FixSRAMChecksums(&g_SRAM);
}

CEXIIPL::~CEXIIPL()
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp
Expand Up @@ -141,7 +141,7 @@ CEXIMemoryCard::CEXIMemoryCard(const Slot slot, bool gci_folder,
m_memory_card_size = m_memory_card->GetCardId() * SIZE_TO_Mb;
std::array<u8, 20> header{};
m_memory_card->Read(0, static_cast<s32>(header.size()), header.data());
SetCardFlashID(header.data(), m_card_slot);
SetCardFlashID(&g_SRAM, header.data(), m_card_slot);
}

std::pair<std::string /* path */, bool /* migrate */>
Expand Down
27 changes: 13 additions & 14 deletions Source/Core/Core/HW/Sram.cpp
Expand Up @@ -9,7 +9,6 @@
#include "Common/MsgHandler.h"
#include "Common/Swap.h"

#include "Core/ConfigManager.h"
#include "Core/HW/EXI/EXI.h"

// English
Expand Down Expand Up @@ -58,24 +57,24 @@ const SRAM sram_dump_german = {{
}};
#endif

void InitSRAM()
void InitSRAM(Sram* sram, const std::string& filename)
{
File::IOFile file(SConfig::GetInstance().m_strSRAM, "rb");
File::IOFile file(filename, "rb");
if (file)
{
if (!file.ReadArray(&g_SRAM, 1))
if (!file.ReadArray(sram, 1))
{
ERROR_LOG_FMT(EXPANSIONINTERFACE, "EXI IPL-DEV: Could not read all of SRAM");
g_SRAM = sram_dump;
*sram = sram_dump;
}
}
else
{
g_SRAM = sram_dump;
*sram = sram_dump;
}
}

void SetCardFlashID(const u8* buffer, ExpansionInterface::Slot card_slot)
void SetCardFlashID(Sram* sram, const u8* buffer, ExpansionInterface::Slot card_slot)
{
u8 card_index;
switch (card_slot)
Expand All @@ -96,25 +95,25 @@ void SetCardFlashID(const u8* buffer, ExpansionInterface::Slot card_slot)
for (int i = 0; i < 12; i++)
{
rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16);
csum += g_SRAM.settings_ex.flash_id[card_index][i] = buffer[i] - ((u8)rand & 0xff);
csum += sram->settings_ex.flash_id[card_index][i] = buffer[i] - ((u8)rand & 0xff);
rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16);
rand &= (u64)0x0000000000007fffULL;
}
g_SRAM.settings_ex.flash_id_checksum[card_index] = csum ^ 0xFF;
sram->settings_ex.flash_id_checksum[card_index] = csum ^ 0xFF;
}

void FixSRAMChecksums()
void FixSRAMChecksums(Sram* sram)
{
// 16bit big-endian additive checksum
u16 checksum = 0;
u16 checksum_inv = 0;
for (auto p = reinterpret_cast<u16*>(&g_SRAM.settings.rtc_bias);
p != reinterpret_cast<u16*>(&g_SRAM.settings_ex); p++)
for (auto p = reinterpret_cast<u16*>(&sram->settings.rtc_bias);
p != reinterpret_cast<u16*>(&sram->settings_ex); p++)
{
u16 value = Common::FromBigEndian(*p);
checksum += value;
checksum_inv += ~value;
}
g_SRAM.settings.checksum = checksum;
g_SRAM.settings.checksum_inv = checksum_inv;
sram->settings.checksum = checksum;
sram->settings.checksum_inv = checksum_inv;
}
7 changes: 4 additions & 3 deletions Source/Core/Core/HW/Sram.h
Expand Up @@ -34,6 +34,7 @@ distribution.
#pragma once

#include <array>
#include <string>

#include "Common/CommonTypes.h"
#include "Common/Swap.h"
Expand Down Expand Up @@ -133,9 +134,9 @@ static_assert(sizeof(Sram) == 0x44);

#pragma pack(pop)

void InitSRAM();
void SetCardFlashID(const u8* buffer, ExpansionInterface::Slot card_slot);
void FixSRAMChecksums();
void InitSRAM(Sram* sram, const std::string& filename);
void SetCardFlashID(Sram* sram, const u8* buffer, ExpansionInterface::Slot card_slot);
void FixSRAMChecksums(Sram* sram);

extern Sram g_SRAM;
extern bool g_SRAM_netplay_initialized;
2 changes: 1 addition & 1 deletion Source/Core/Core/NetPlayServer.cpp
Expand Up @@ -1440,7 +1440,7 @@ bool NetPlayServer::StartGame()
if (!g_SRAM_netplay_initialized)
{
SConfig::GetInstance().m_strSRAM = File::GetUserPath(F_GCSRAM_IDX);
InitSRAM();
InitSRAM(&g_SRAM, SConfig::GetInstance().m_strSRAM);
g_SRAM_netplay_initialized = true;
}
sf::Packet srampac;
Expand Down

0 comments on commit 9963637

Please sign in to comment.