Skip to content

Commit

Permalink
Merge pull request #4544 from JosJuice/region-enum
Browse files Browse the repository at this point in the history
DiscIO: Add GetRegion function and Region enum
  • Loading branch information
Parlane committed Jan 12, 2017
2 parents 34c6672 + ec969da commit d346d4c
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 270 deletions.
23 changes: 12 additions & 11 deletions Source/Core/Core/Boot/Boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,32 +188,34 @@ bool CBoot::Load_BS2(const std::string& _rBootROMFilename)
// Use zlibs crc32 implementation to compute the hash
u32 ipl_hash = crc32(0L, Z_NULL, 0);
ipl_hash = crc32(ipl_hash, (const Bytef*)data.data(), (u32)data.size());
std::string ipl_region;
DiscIO::Region ipl_region;
switch (ipl_hash)
{
case USA_v1_0:
case USA_v1_1:
case USA_v1_2:
case BRA_v1_0:
ipl_region = USA_DIR;
ipl_region = DiscIO::Region::NTSC_U;
break;
case JAP_v1_0:
case JAP_v1_1:
ipl_region = JAP_DIR;
ipl_region = DiscIO::Region::NTSC_J;
break;
case PAL_v1_0:
case PAL_v1_2:
ipl_region = EUR_DIR;
ipl_region = DiscIO::Region::PAL;
break;
default:
PanicAlertT("IPL with unknown hash %x", ipl_hash);
ipl_region = DiscIO::Region::UNKNOWN_REGION;
break;
}

std::string BootRegion = _rBootROMFilename.substr(_rBootROMFilename.find_last_of(DIR_SEP) - 3, 3);
if (BootRegion != ipl_region)
const DiscIO::Region boot_region = SConfig::GetInstance().m_region;
if (ipl_region != DiscIO::Region::UNKNOWN_REGION && boot_region != ipl_region)
PanicAlertT("%s IPL found in %s directory. The disc might not be recognized",
ipl_region.c_str(), BootRegion.c_str());
SConfig::GetDirectoryForRegion(ipl_region),
SConfig::GetDirectoryForRegion(boot_region));

// Run the descrambler over the encrypted section containing BS1/BS2
CEXIIPL::Descrambler((u8*)data.data() + 0x100, 0x1AFE00);
Expand Down Expand Up @@ -255,7 +257,8 @@ bool CBoot::BootUp()
g_symbolDB.Clear();

// PAL Wii uses NTSC framerate and linecount in 60Hz modes
VideoInterface::Preset(_StartupPara.bNTSC || (_StartupPara.bWii && _StartupPara.bPAL60));
VideoInterface::Preset(DiscIO::IsNTSC(_StartupPara.m_region) ||
(_StartupPara.bWii && _StartupPara.bPAL60));

switch (_StartupPara.m_BootType)
{
Expand All @@ -275,8 +278,6 @@ bool CBoot::BootUp()
}

std::string game_id = DVDInterface::GetVolume().GetGameID();
if (game_id.size() >= 4)
VideoInterface::SetRegionReg(game_id.at(3));

std::vector<u8> tmd_buffer = pVolume.GetTMD();
if (!tmd_buffer.empty())
Expand Down Expand Up @@ -419,7 +420,7 @@ bool CBoot::BootUp()

// Poor man's bootup
if (_StartupPara.bWii)
SetupWiiMemory(DiscIO::Country::COUNTRY_UNKNOWN);
SetupWiiMemory();
else
EmulatedBS2_GC(true);

Expand Down
9 changes: 2 additions & 7 deletions Source/Core/Core/Boot/Boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
#include <cstdlib>
#include <string>

namespace DiscIO
{
enum class Country;
}

struct CountrySetting
struct RegionSetting
{
const std::string area;
const std::string video;
Expand Down Expand Up @@ -57,5 +52,5 @@ class CBoot
static bool Load_BS2(const std::string& _rBootROMFilename);
static void Load_FST(bool _bIsWii);

static bool SetupWiiMemory(DiscIO::Country country);
static bool SetupWiiMemory();
};
63 changes: 24 additions & 39 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ bool CBoot::EmulatedBS2_GC(bool skipAppLoader)
0x10000006,
0x8000002C); // Console type - DevKit (retail ID == 0x00000003) see YAGCD 4.2.1.1.2

PowerPC::HostWrite_U32(SConfig::GetInstance().bNTSC ? 0 : 1,
0x800000CC); // Fake the VI Init of the IPL (YAGCD 4.2.1.4)
const bool ntsc = DiscIO::IsNTSC(SConfig::GetInstance().m_region);
PowerPC::HostWrite_U32(ntsc ? 0 : 1, 0x800000CC); // Fake the VI Init of the IPL (YAGCD 4.2.1.4)

PowerPC::HostWrite_U32(0x01000000, 0x800000d0); // ARAM Size. 16MB main + 4/16/32MB external
// (retail consoles have no external ARAM)
Expand Down Expand Up @@ -113,13 +113,14 @@ bool CBoot::EmulatedBS2_GC(bool skipAppLoader)
DVDRead(apploader_offset + 0x20, 0x01200000, apploader_size + apploader_trailer, false);

// Setup pointers like real BS2 does
if (SConfig::GetInstance().bNTSC)
if (ntsc)
{
PowerPC::ppcState.gpr[1] = 0x81566550; // StackPointer, used to be set to 0x816ffff0
PowerPC::ppcState.gpr[2] = 0x81465cc0; // Global pointer to Small Data Area 2 Base (haven't
// seen anything use it...meh)
PowerPC::ppcState.gpr[13] =
0x81465320; // Global pointer to Small Data Area Base (Luigi's Mansion's apploader uses it)
// StackPointer, used to be set to 0x816ffff0
PowerPC::ppcState.gpr[1] = 0x81566550;
// Global pointer to Small Data Area 2 Base (haven't seen anything use it...meh)
PowerPC::ppcState.gpr[2] = 0x81465cc0;
// Global pointer to Small Data Area Base (Luigi's Mansion's apploader uses it)
PowerPC::ppcState.gpr[13] = 0x81465320;
}
else
{
Expand Down Expand Up @@ -181,28 +182,15 @@ bool CBoot::EmulatedBS2_GC(bool skipAppLoader)
return true;
}

bool CBoot::SetupWiiMemory(DiscIO::Country country)
bool CBoot::SetupWiiMemory()
{
static const CountrySetting SETTING_EUROPE = {"EUR", "PAL", "EU", "LE"};
static const CountrySetting SETTING_USA = {"USA", "NTSC", "US", "LU"};
static const CountrySetting SETTING_JAPAN = {"JPN", "NTSC", "JP", "LJ"};
static const CountrySetting SETTING_KOREA = {"KOR", "NTSC", "KR", "LKH"};
static const std::map<DiscIO::Country, const CountrySetting> country_settings = {
{DiscIO::Country::COUNTRY_EUROPE, SETTING_EUROPE},
{DiscIO::Country::COUNTRY_USA, SETTING_USA},
{DiscIO::Country::COUNTRY_JAPAN, SETTING_JAPAN},
{DiscIO::Country::COUNTRY_KOREA, SETTING_KOREA},
// TODO: Determine if Taiwan have their own specific settings.
// Also determine if there are other specific settings
// for other countries.
{DiscIO::Country::COUNTRY_TAIWAN, SETTING_JAPAN}};
auto entryPos = country_settings.find(country);
const CountrySetting& country_setting =
(entryPos != country_settings.end()) ?
entryPos->second :
(SConfig::GetInstance().bNTSC ?
SETTING_USA :
SETTING_EUROPE); // default to USA or EUR depending on game's video mode
static const std::map<DiscIO::Region, const RegionSetting> region_settings = {
{DiscIO::Region::NTSC_J, {"JPN", "NTSC", "JP", "LJ"}},
{DiscIO::Region::NTSC_U, {"USA", "NTSC", "US", "LU"}},
{DiscIO::Region::PAL, {"EUR", "PAL", "EU", "LE"}},
{DiscIO::Region::NTSC_K, {"KOR", "NTSC", "KR", "LKH"}}};
auto entryPos = region_settings.find(SConfig::GetInstance().m_region);
const RegionSetting& region_setting = entryPos->second;

SettingsHandler gen;
std::string serno;
Expand Down Expand Up @@ -233,15 +221,15 @@ bool CBoot::SetupWiiMemory(DiscIO::Country country)
INFO_LOG(BOOT, "Using serial number: %s", serno.c_str());
}

std::string model = "RVL-001(" + country_setting.area + ")";
gen.AddSetting("AREA", country_setting.area);
std::string model = "RVL-001(" + region_setting.area + ")";
gen.AddSetting("AREA", region_setting.area);
gen.AddSetting("MODEL", model);
gen.AddSetting("DVD", "0");
gen.AddSetting("MPCH", "0x7FFE");
gen.AddSetting("CODE", country_setting.code);
gen.AddSetting("CODE", region_setting.code);
gen.AddSetting("SERNO", serno);
gen.AddSetting("VIDEO", country_setting.video);
gen.AddSetting("GAME", country_setting.game);
gen.AddSetting("VIDEO", region_setting.video);
gen.AddSetting("GAME", region_setting.game);

File::CreateFullPath(settings_Filename);
{
Expand Down Expand Up @@ -317,7 +305,7 @@ bool CBoot::SetupWiiMemory(DiscIO::Country country)
Memory::Write_U32(0x80000000, 0x00003184); // GameID Address

// Fake the VI Init of the IPL
Memory::Write_U32(SConfig::GetInstance().bNTSC ? 0 : 1, 0x000000CC);
Memory::Write_U32(DiscIO::IsNTSC(SConfig::GetInstance().m_region) ? 0 : 1, 0x000000CC);

// Clear exception handler. Why? Don't we begin with only zeros?
for (int i = 0x3000; i <= 0x3038; i += 4)
Expand All @@ -336,10 +324,7 @@ bool CBoot::EmulatedBS2_Wii()
INFO_LOG(BOOT, "Faking Wii BS2...");

// Setup Wii memory
DiscIO::Country country_code = DiscIO::Country::COUNTRY_UNKNOWN;
if (DVDInterface::VolumeIsValid())
country_code = DVDInterface::GetVolume().GetCountry();
if (SetupWiiMemory(country_code) == false)
if (!SetupWiiMemory())
return false;

// Execute the apploader
Expand Down
7 changes: 1 addition & 6 deletions Source/Core/Core/Boot/Boot_WiiWAD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,8 @@ bool CBoot::Boot_WiiWAD(const std::string& _pFilename)
if (titleID == TITLEID_SYSMENU)
HLE_IPC_CreateVirtualFATFilesystem();
// setup Wii memory
if (!SetupWiiMemory(ContentLoader.GetCountry()))
if (!SetupWiiMemory())
return false;
// this sets a bit that is used to detect NTSC-J
if (ContentLoader.GetCountry() == DiscIO::Country::COUNTRY_JAPAN)
{
VideoInterface::SetRegionReg('J');
}
// DOL
const DiscIO::SNANDContent* pContent =
ContentLoader.GetContentByIndex(ContentLoader.GetBootIndex());
Expand Down
18 changes: 12 additions & 6 deletions Source/Core/Core/BootManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Common/IniFile.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"

#include "Core/BootManager.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
Expand All @@ -38,6 +39,9 @@
#include "Core/Host.h"
#include "Core/Movie.h"
#include "Core/NetPlayProto.h"

#include "DiscIO/Enums.h"

#include "VideoCommon/VideoBackendBase.h"

namespace BootManager
Expand Down Expand Up @@ -349,17 +353,19 @@ bool BootCore(const std::string& _rFilename)
g_SRAM_netplay_initialized = false;
}

const bool ntsc = DiscIO::IsNTSC(StartUp.m_region);

// Apply overrides
// Some NTSC GameCube games such as Baten Kaitos react strangely to language settings that would
// be invalid on an NTSC system
if (!StartUp.bOverrideGCLanguage && StartUp.bNTSC)
// Some NTSC GameCube games such as Baten Kaitos react strangely to
// language settings that would be invalid on an NTSC system
if (!StartUp.bOverrideGCLanguage && ntsc)
{
StartUp.SelectedLanguage = 0;
}

// Some NTSC Wii games such as Doc Louis's Punch-Out!! and 1942 (Virtual Console) crash if the
// PAL60 option is enabled
if (StartUp.bWii && StartUp.bNTSC)
// Some NTSC Wii games such as Doc Louis's Punch-Out!! and
// 1942 (Virtual Console) crash if the PAL60 option is enabled
if (StartUp.bWii && ntsc)
{
StartUp.bPAL60 = false;
}
Expand Down
60 changes: 29 additions & 31 deletions Source/Core/Core/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,31 +761,28 @@ void SConfig::LoadDefaults()
m_revision = 0;
}

static const char* GetRegionOfCountry(DiscIO::Country country)
const char* SConfig::GetDirectoryForRegion(DiscIO::Region region)
{
switch (country)
switch (region)
{
case DiscIO::Country::COUNTRY_USA:
return USA_DIR;

case DiscIO::Country::COUNTRY_TAIWAN:
case DiscIO::Country::COUNTRY_KOREA:
// TODO: Should these have their own Region Dir?
case DiscIO::Country::COUNTRY_JAPAN:
case DiscIO::Region::NTSC_J:
return JAP_DIR;

case DiscIO::Country::COUNTRY_AUSTRALIA:
case DiscIO::Country::COUNTRY_EUROPE:
case DiscIO::Country::COUNTRY_FRANCE:
case DiscIO::Country::COUNTRY_GERMANY:
case DiscIO::Country::COUNTRY_ITALY:
case DiscIO::Country::COUNTRY_NETHERLANDS:
case DiscIO::Country::COUNTRY_RUSSIA:
case DiscIO::Country::COUNTRY_SPAIN:
case DiscIO::Country::COUNTRY_WORLD:
case DiscIO::Region::NTSC_U:
return USA_DIR;

case DiscIO::Region::PAL:
return EUR_DIR;

case DiscIO::Country::COUNTRY_UNKNOWN:
case DiscIO::Region::NTSC_K:
// This function can't return a Korean directory name, because this
// function is only used for GameCube things (memory cards, IPL), and
// GameCube has no NTSC-K region. Since NTSC-K doesn't correspond to any
// GameCube region, let's return an arbitrary pick. Returning nullptr like
// with unknown regions would be inappropriate, because Dolphin expects
// to get valid memory card paths even when running an NTSC-K Wii game.
return JAP_DIR;

default:
return nullptr;
}
Expand Down Expand Up @@ -837,17 +834,18 @@ bool SConfig::AutoSetup(EBootBS2 _BootBS2)
// Check if we have a Wii disc
bWii = pVolume->GetVolumeType() == DiscIO::Platform::WII_DISC;

const char* retrieved_region_dir = GetRegionOfCountry(pVolume->GetCountry());
m_region = pVolume->GetRegion();
const char* retrieved_region_dir = GetDirectoryForRegion(m_region);
if (!retrieved_region_dir)
{
if (!PanicYesNoT("Your GCM/ISO file seems to be invalid (invalid country)."
"\nContinue with PAL region?"))
return false;
m_region = DiscIO::Region::PAL;
retrieved_region_dir = EUR_DIR;
}

set_region_dir = retrieved_region_dir;
bNTSC = set_region_dir == USA_DIR || set_region_dir == JAP_DIR;
}
else if (!strcasecmp(Extension.c_str(), ".elf"))
{
Expand All @@ -857,24 +855,24 @@ bool SConfig::AutoSetup(EBootBS2 _BootBS2)
// all GC homebrew to 50Hz.
// In the future, it probably makes sense to add a Region setting for homebrew somewhere in
// the emulator config.
bNTSC = bWii ? false : true;
set_region_dir = bNTSC ? USA_DIR : EUR_DIR;
m_region = bWii ? DiscIO::Region::PAL : DiscIO::Region::NTSC_U;
set_region_dir = bWii ? EUR_DIR : USA_DIR;
m_BootType = BOOT_ELF;
}
else if (!strcasecmp(Extension.c_str(), ".dol"))
{
CDolLoader dolfile(m_strFilename);
bWii = dolfile.IsWii();
// TODO: See the ELF code above.
bNTSC = bWii ? false : true;
set_region_dir = bNTSC ? USA_DIR : EUR_DIR;
m_region = bWii ? DiscIO::Region::PAL : DiscIO::Region::NTSC_U;
set_region_dir = bWii ? EUR_DIR : USA_DIR;
m_BootType = BOOT_DOL;
}
else if (!strcasecmp(Extension.c_str(), ".dff"))
{
bWii = true;
m_region = DiscIO::Region::NTSC_U;
set_region_dir = USA_DIR;
bNTSC = true;
m_BootType = BOOT_DFF;

std::unique_ptr<FifoDataFile> ddfFile(FifoDataFile::Load(m_strFilename, true));
Expand All @@ -899,9 +897,9 @@ bool SConfig::AutoSetup(EBootBS2 _BootBS2)
return false; // do not boot
}

const char* retrieved_region_dir = GetRegionOfCountry(ContentLoader.GetCountry());
m_region = ContentLoader.GetRegion();
const char* retrieved_region_dir = GetDirectoryForRegion(m_region);
set_region_dir = retrieved_region_dir ? retrieved_region_dir : EUR_DIR;
bNTSC = set_region_dir == USA_DIR || set_region_dir == JAP_DIR;

bWii = true;
m_BootType = BOOT_WII_NAND;
Expand Down Expand Up @@ -942,21 +940,21 @@ bool SConfig::AutoSetup(EBootBS2 _BootBS2)
break;

case BOOT_BS2_USA:
m_region = DiscIO::Region::NTSC_U;
set_region_dir = USA_DIR;
m_strFilename.clear();
bNTSC = true;
break;

case BOOT_BS2_JAP:
m_region = DiscIO::Region::NTSC_J;
set_region_dir = JAP_DIR;
m_strFilename.clear();
bNTSC = true;
break;

case BOOT_BS2_EUR:
m_region = DiscIO::Region::PAL;
set_region_dir = EUR_DIR;
m_strFilename.clear();
bNTSC = false;
break;
}

Expand Down
Loading

0 comments on commit d346d4c

Please sign in to comment.