Skip to content

Commit

Permalink
PowerPC: Convert CPUCore enum into an enum class
Browse files Browse the repository at this point in the history
Makes the enum values strongly-typed and prevents the identifiers from
polluting the PowerPC namespace. This also cleans up the parameters of
some functions where we were accepting an ambiguous int type and
expecting the correct values to be passed in.

Now those parameters accept a PowerPC::CPUCore type only, making it
immediately obvious which values should be passed in. It also turns out
we were storing these core types into other structures as plain ints,
which have also been corrected.

As this type is used directly with the configuration code, we need to
provide our own overloaded insertion (<<) and extraction (>>) operators
in order to make it compatible with it. These are fairly trivial to
implement, so there's no issue here.

A minor adjustment to TryParse() was required, as our generic function
was doing the following:

N tmp = 0;

which is problematic, as custom types may not be able to have that
assignment performed (e.g. strongly-typed enums), so we change this to:

N tmp;

which is sufficient, as the value is attempted to be initialized
immediately under that statement.
  • Loading branch information
lioncash committed Jun 15, 2018
1 parent 5860670 commit 6f473b9
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Source/Android/jni/MainAndroid.cpp
Expand Up @@ -445,7 +445,7 @@ JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetUserDi
JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_DefaultCPUCore(JNIEnv* env,
jobject obj)
{
return PowerPC::DefaultCPUCore();
return static_cast<jint>(PowerPC::DefaultCPUCore());
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetProfiling(JNIEnv* env,
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Common/StringUtil.h
Expand Up @@ -55,14 +55,14 @@ static bool TryParse(const std::string& str, N* const output)
// separators
iss.imbue(std::locale("C"));

N tmp = 0;
N tmp;
if (iss >> tmp)
{
*output = tmp;
return true;
}
else
return false;

return false;
}

template <typename N>
Expand Down
17 changes: 7 additions & 10 deletions Source/Core/Core/BootManager.cpp
Expand Up @@ -28,16 +28,12 @@
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"

#include "Common/Config/Config.h"
#include "Core/Boot/Boot.h"
#include "Core/Config/MainSettings.h"
#include "Core/Config/SYSCONFSettings.h"
#include "Core/ConfigLoaders/BaseConfigLoader.h"
#include "Core/ConfigLoaders/GameConfigLoader.h"
#include "Core/ConfigLoaders/MovieConfigLoader.h"
#include "Core/ConfigLoaders/NetPlayConfigLoader.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
Expand All @@ -47,6 +43,7 @@
#include "Core/HW/WiimoteReal/WiimoteReal.h"
#include "Core/Movie.h"
#include "Core/NetPlayProto.h"
#include "Core/PowerPC/PowerPC.h"

#include "DiscIO/Enums.h"

Expand Down Expand Up @@ -88,7 +85,7 @@ struct ConfigCache
bool bDSPHLE;
bool bHLE_BS2;
int iSelectedLanguage;
int iCPUCore;
PowerPC::CPUCore cpu_core;
int Volume;
float m_EmulationSpeed;
float m_OCFactor;
Expand Down Expand Up @@ -118,7 +115,7 @@ void ConfigCache::SaveConfig(const SConfig& config)
bDSPHLE = config.bDSPHLE;
bHLE_BS2 = config.bHLE_BS2;
iSelectedLanguage = config.SelectedLanguage;
iCPUCore = config.iCPUCore;
cpu_core = config.cpu_core;
Volume = config.m_Volume;
m_EmulationSpeed = config.m_EmulationSpeed;
strBackend = config.m_strVideoBackend;
Expand Down Expand Up @@ -159,7 +156,7 @@ void ConfigCache::RestoreConfig(SConfig* config)
config->bDSPHLE = bDSPHLE;
config->bHLE_BS2 = bHLE_BS2;
config->SelectedLanguage = iSelectedLanguage;
config->iCPUCore = iCPUCore;
config->cpu_core = cpu_core;

// Only change these back if they were actually set by game ini, since they can be changed while a
// game is running.
Expand Down Expand Up @@ -255,7 +252,7 @@ bool BootCore(std::unique_ptr<BootParameters> boot)
core_section->Get("FastDiscSpeed", &StartUp.bFastDiscSpeed, StartUp.bFastDiscSpeed);
core_section->Get("DSPHLE", &StartUp.bDSPHLE, StartUp.bDSPHLE);
core_section->Get("GFXBackend", &StartUp.m_strVideoBackend, StartUp.m_strVideoBackend);
core_section->Get("CPUCore", &StartUp.iCPUCore, StartUp.iCPUCore);
core_section->Get("CPUCore", &StartUp.cpu_core, StartUp.cpu_core);
core_section->Get("HLE_BS2", &StartUp.bHLE_BS2, StartUp.bHLE_BS2);
core_section->Get("GameCubeLanguage", &StartUp.SelectedLanguage, StartUp.SelectedLanguage);
if (core_section->Get("EmulationSpeed", &StartUp.m_EmulationSpeed, StartUp.m_EmulationSpeed))
Expand Down Expand Up @@ -317,7 +314,7 @@ bool BootCore(std::unique_ptr<BootParameters> boot)
StartUp.bCPUThread = Config::Get(Config::MAIN_CPU_THREAD);
StartUp.bDSPHLE = Config::Get(Config::MAIN_DSP_HLE);
StartUp.bFastDiscSpeed = Config::Get(Config::MAIN_FAST_DISC_SPEED);
StartUp.iCPUCore = Config::Get(Config::MAIN_CPU_CORE);
StartUp.cpu_core = Config::Get(Config::MAIN_CPU_CORE);
StartUp.bSyncGPU = Config::Get(Config::MAIN_SYNC_GPU);
if (!StartUp.bWii)
StartUp.SelectedLanguage = Config::Get(Config::MAIN_GC_LANGUAGE);
Expand All @@ -343,7 +340,7 @@ bool BootCore(std::unique_ptr<BootParameters> boot)
StartUp.bDSPHLE = g_NetPlaySettings.m_DSPHLE;
StartUp.bEnableMemcardSdWriting = g_NetPlaySettings.m_WriteToMemcard;
StartUp.bCopyWiiSaveNetplay = g_NetPlaySettings.m_CopyWiiSave;
StartUp.iCPUCore = g_NetPlaySettings.m_CPUcore;
StartUp.cpu_core = g_NetPlaySettings.m_CPUcore;
StartUp.SelectedLanguage = g_NetPlaySettings.m_SelectedLanguage;
StartUp.bOverrideGCLanguage = g_NetPlaySettings.m_OverrideGCLanguage;
StartUp.m_DSPEnableJIT = g_NetPlaySettings.m_DSPEnableJIT;
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/Config/MainSettings.cpp
Expand Up @@ -16,7 +16,8 @@ namespace Config
// Main.Core

const ConfigInfo<bool> MAIN_SKIP_IPL{{System::Main, "Core", "SkipIPL"}, true};
const ConfigInfo<int> MAIN_CPU_CORE{{System::Main, "Core", "CPUCore"}, PowerPC::DefaultCPUCore()};
const ConfigInfo<PowerPC::CPUCore> MAIN_CPU_CORE{{System::Main, "Core", "CPUCore"},
PowerPC::DefaultCPUCore()};
const ConfigInfo<bool> MAIN_FASTMEM{{System::Main, "Core", "Fastmem"}, true};
const ConfigInfo<bool> MAIN_DSP_HLE{{System::Main, "Core", "DSPHLE"}, true};
const ConfigInfo<int> MAIN_TIMING_VARIANCE{{System::Main, "Core", "TimingVariance"}, 40};
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/Core/Config/MainSettings.h
Expand Up @@ -8,12 +8,17 @@

#include "Common/Config/Config.h"

namespace PowerPC
{
enum class CPUCore;
}

namespace Config
{
// Main.Core

extern const ConfigInfo<bool> MAIN_SKIP_IPL;
extern const ConfigInfo<int> MAIN_CPU_CORE;
extern const ConfigInfo<PowerPC::CPUCore> MAIN_CPU_CORE;
extern const ConfigInfo<bool> MAIN_FASTMEM;
// Should really be in the DSP section, but we're kind of stuck with bad decisions made in the past.
extern const ConfigInfo<bool> MAIN_DSP_HLE;
Expand Down
11 changes: 7 additions & 4 deletions Source/Core/Core/ConfigLoaders/MovieConfigLoader.cpp
Expand Up @@ -8,10 +8,8 @@
#include <memory>
#include <string>

#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"
#include "Common/FileUtil.h"

#include "Core/Config/GraphicsSettings.h"
#include "Core/Config/MainSettings.h"
Expand All @@ -20,14 +18,19 @@
#include "Core/Movie.h"
#include "VideoCommon/VideoConfig.h"

namespace PowerPC
{
enum class CPUCore;
}

namespace ConfigLoaders
{
static void LoadFromDTM(Config::Layer* config_layer, Movie::DTMHeader* dtm)
{
config_layer->Set(Config::MAIN_CPU_THREAD, dtm->bDualCore);
config_layer->Set(Config::MAIN_DSP_HLE, dtm->bDSPHLE);
config_layer->Set(Config::MAIN_FAST_DISC_SPEED, dtm->bFastDiscSpeed);
config_layer->Set(Config::MAIN_CPU_CORE, static_cast<int>(dtm->CPUCore));
config_layer->Set(Config::MAIN_CPU_CORE, static_cast<PowerPC::CPUCore>(dtm->CPUCore));
config_layer->Set(Config::MAIN_SYNC_GPU, dtm->bSyncGPU);
config_layer->Set(Config::MAIN_GFX_BACKEND, dtm->videoBackend.data());

Expand All @@ -50,7 +53,7 @@ void SaveToDTM(Movie::DTMHeader* dtm)
dtm->bDualCore = Config::Get(Config::MAIN_CPU_THREAD);
dtm->bDSPHLE = Config::Get(Config::MAIN_DSP_HLE);
dtm->bFastDiscSpeed = Config::Get(Config::MAIN_FAST_DISC_SPEED);
dtm->CPUCore = Config::Get(Config::MAIN_CPU_CORE);
dtm->CPUCore = static_cast<u8>(Config::Get(Config::MAIN_CPU_CORE));
dtm->bSyncGPU = Config::Get(Config::MAIN_SYNC_GPU);
const std::string video_backend = Config::Get(Config::MAIN_GFX_BACKEND);

Expand Down
10 changes: 5 additions & 5 deletions Source/Core/Core/ConfigManager.cpp
Expand Up @@ -218,7 +218,7 @@ void SConfig::SaveCoreSettings(IniFile& ini)

core->Set("SkipIPL", bHLE_BS2);
core->Set("TimingVariance", iTimingVariance);
core->Set("CPUCore", iCPUCore);
core->Set("CPUCore", cpu_core);
core->Set("Fastmem", bFastmem);
core->Set("CPUThread", bCPUThread);
core->Set("DSPHLE", bDSPHLE);
Expand Down Expand Up @@ -505,11 +505,11 @@ void SConfig::LoadCoreSettings(IniFile& ini)

core->Get("SkipIPL", &bHLE_BS2, true);
#ifdef _M_X86
core->Get("CPUCore", &iCPUCore, PowerPC::CORE_JIT64);
core->Get("CPUCore", &cpu_core, PowerPC::CPUCore::JIT64);
#elif _M_ARM_64
core->Get("CPUCore", &iCPUCore, PowerPC::CORE_JITARM64);
core->Get("CPUCore", &cpu_core, PowerPC::CPUCore::JITARM64);
#else
core->Get("CPUCore", &iCPUCore, PowerPC::CORE_INTERPRETER);
core->Get("CPUCore", &cpu_core, PowerPC::CPUCore::Interpreter);
#endif
core->Get("Fastmem", &bFastmem, true);
core->Get("DSPHLE", &bDSPHLE, true);
Expand Down Expand Up @@ -763,7 +763,7 @@ void SConfig::LoadDefaults()
#endif
#endif

iCPUCore = PowerPC::DefaultCPUCore();
cpu_core = PowerPC::DefaultCPUCore();
iTimingVariance = 40;
bCPUThread = false;
bSyncGPUOnSkipIdleHack = true;
Expand Down
8 changes: 7 additions & 1 deletion Source/Core/Core/ConfigManager.h
Expand Up @@ -23,6 +23,7 @@ enum class Region;
struct Partition;
class Volume;
} // namespace DiscIO

namespace IOS
{
namespace ES
Expand All @@ -31,6 +32,11 @@ class TMDReader;
}
} // namespace IOS

namespace PowerPC
{
enum class CPUCore;
} // namespace PowerPC

// DSP Backend Types
#define BACKEND_NULLSOUND _trans("No Audio Output")
#define BACKEND_ALSA "ALSA"
Expand Down Expand Up @@ -75,7 +81,7 @@ struct SConfig
bool bAutomaticStart = false;
bool bBootToPause = false;

int iCPUCore; // Uses the values of PowerPC::CPUCore
PowerPC::CPUCore cpu_core;

bool bJITNoBlockCache = false;
bool bJITNoBlockLinking = false;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Core.cpp
Expand Up @@ -530,7 +530,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot)
Fifo::Prepare();

// Setup our core, but can't use dynarec if we are compare server
if (core_parameter.iCPUCore != PowerPC::CORE_INTERPRETER &&
if (core_parameter.cpu_core != PowerPC::CPUCore::Interpreter &&
(!core_parameter.bRunCompareServer || core_parameter.bRunCompareClient))
{
PowerPC::SetMode(PowerPC::CoreMode::JIT);
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/CPU.cpp
Expand Up @@ -45,7 +45,7 @@ static bool s_state_system_request_stepping = false;
static bool s_state_cpu_step_instruction = false;
static Common::Event* s_state_cpu_step_instruction_sync = nullptr;

void Init(int cpu_core)
void Init(PowerPC::CPUCore cpu_core)
{
PowerPC::Init(cpu_core);
s_state = State::Stepping;
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/Core/HW/CPU.h
Expand Up @@ -9,6 +9,11 @@ namespace Common
class Event;
}

namespace PowerPC
{
enum class CPUCore;
}

namespace CPU
{
enum class State
Expand All @@ -19,7 +24,7 @@ enum class State
};

// Init
void Init(int cpu_core);
void Init(PowerPC::CPUCore cpu_core);

// Shutdown
void Shutdown();
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/HW.cpp
Expand Up @@ -45,7 +45,7 @@ void Init()
DSP::Init(SConfig::GetInstance().bDSPHLE);
DVDInterface::Init();
GPFifo::Init();
CPU::Init(SConfig::GetInstance().iCPUCore);
CPU::Init(SConfig::GetInstance().cpu_core);
SystemTimers::Init();

if (SConfig::GetInstance().bWii)
Expand Down
12 changes: 11 additions & 1 deletion Source/Core/Core/NetPlayClient.cpp
Expand Up @@ -12,6 +12,7 @@
#include <mutex>
#include <sstream>
#include <thread>
#include <type_traits>

#include <mbedtls/md5.h>

Expand All @@ -35,6 +36,7 @@
#include "Core/HW/WiimoteReal/WiimoteReal.h"
#include "Core/IOS/USB/Bluetooth/BTEmu.h"
#include "Core/Movie.h"
#include "Core/PowerPC/PowerPC.h"
#include "InputCommon/GCAdapter.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/VideoConfig.h"
Expand Down Expand Up @@ -412,7 +414,15 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
packet >> m_current_game;
packet >> g_NetPlaySettings.m_CPUthread;
packet >> g_NetPlaySettings.m_CPUcore;

{
std::underlying_type_t<PowerPC::CPUCore> core;
if (packet >> core)
g_NetPlaySettings.m_CPUcore = static_cast<PowerPC::CPUCore>(core);
else
g_NetPlaySettings.m_CPUcore = PowerPC::CPUCore::CachedInterpreter;
}

packet >> g_NetPlaySettings.m_EnableCheats;
packet >> g_NetPlaySettings.m_SelectedLanguage;
packet >> g_NetPlaySettings.m_OverrideGCLanguage;
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/Core/NetPlayProto.h
Expand Up @@ -9,10 +9,15 @@
#include "Common/CommonTypes.h"
#include "Core/HW/EXI/EXI_Device.h"

namespace PowerPC
{
enum class CPUCore;
}

struct NetSettings
{
bool m_CPUthread;
int m_CPUcore;
PowerPC::CPUCore m_CPUcore;
bool m_EnableCheats;
int m_SelectedLanguage;
bool m_OverrideGCLanguage;
Expand Down
9 changes: 5 additions & 4 deletions Source/Core/Core/NetPlayServer.cpp
Expand Up @@ -11,6 +11,7 @@
#include <mutex>
#include <string>
#include <thread>
#include <type_traits>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -815,10 +816,10 @@ bool NetPlayServer::StartGame()

// tell clients to start game
sf::Packet spac;
spac << (MessageId)NP_MSG_START_GAME;
spac << static_cast<MessageId>(NP_MSG_START_GAME);
spac << m_current_game;
spac << m_settings.m_CPUthread;
spac << m_settings.m_CPUcore;
spac << static_cast<std::underlying_type_t<PowerPC::CPUCore>>(m_settings.m_CPUcore);
spac << m_settings.m_EnableCheats;
spac << m_settings.m_SelectedLanguage;
spac << m_settings.m_OverrideGCLanguage;
Expand All @@ -832,8 +833,8 @@ bool NetPlayServer::StartGame()
spac << m_settings.m_OCFactor;
spac << m_settings.m_EXIDevice[0];
spac << m_settings.m_EXIDevice[1];
spac << (u32)g_netplay_initial_rtc;
spac << (u32)(g_netplay_initial_rtc >> 32);
spac << static_cast<u32>(g_netplay_initial_rtc);
spac << static_cast<u32>(g_netplay_initial_rtc >> 32);

SendAsyncToClients(std::move(spac));

Expand Down

0 comments on commit 6f473b9

Please sign in to comment.