From d6244f70f278a7c5dd8f90ec96488cdc3680797f Mon Sep 17 00:00:00 2001 From: crediar Date: Wed, 16 Jul 2025 19:55:59 +0200 Subject: [PATCH] Added Triforce support --- Data/Sys/GameSettings/GEYJ6E.ini | 4 + Data/Sys/GameSettings/GEYP6E.ini | 4 + Data/Sys/GameSettings/GGGE6E.ini | 5 + Data/Sys/GameSettings/GHAE6E.ini | 5 + Data/Sys/GameSettings/GHZW6E.ini | 4 + Data/Sys/GameSettings/GJAP6E.ini | 4 + Data/Sys/GameSettings/GLKJ6E.ini | 4 + Data/Sys/GameSettings/GLLP6E.ini | 4 + Source/Core/Common/CommonPaths.h | 3 + Source/Core/Common/FileUtil.cpp | 1 + Source/Core/Common/FileUtil.h | 3 +- Source/Core/Common/Logging/Log.h | 5 + Source/Core/Common/Logging/LogManager.cpp | 5 + Source/Core/Core/Boot/Boot.cpp | 19 +- Source/Core/Core/Boot/Boot_BS2Emu.cpp | 15 +- Source/Core/Core/CMakeLists.txt | 4 + Source/Core/Core/Config/MainSettings.cpp | 17 + Source/Core/Core/ConfigManager.cpp | 30 +- Source/Core/Core/ConfigManager.h | 6 +- Source/Core/Core/Core.cpp | 8 + Source/Core/Core/HW/DVD/AMMediaboard.cpp | 1850 +++++++++++++ Source/Core/Core/HW/DVD/AMMediaboard.h | 237 ++ Source/Core/Core/HW/DVD/DVDInterface.cpp | 43 +- Source/Core/Core/HW/EXI/EXI.cpp | 24 +- Source/Core/Core/HW/EXI/EXI.h | 1 - .../Core/Core/HW/EXI/EXI_DeviceBaseboard.cpp | 305 ++- Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.h | 46 +- Source/Core/Core/HW/GCPadEmu.cpp | 18 + Source/Core/Core/HW/GCPadEmu.h | 10 +- Source/Core/Core/HW/Memmap.h | 1 - Source/Core/Core/HW/SI/SI.h | 1 + Source/Core/Core/HW/SI/SI_Device.cpp | 4 + Source/Core/Core/HW/SI/SI_Device.h | 2 - .../Core/Core/HW/SI/SI_DeviceAMBaseboard.cpp | 2383 +++++++++++++++++ Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.h | 309 +++ .../Core/Core/HW/SI/SI_DeviceGCController.h | 3 + Source/Core/Core/HW/VideoInterface.cpp | 4 +- Source/Core/Core/NetPlayClient.cpp | 33 +- Source/Core/Core/State.cpp | 2 +- Source/Core/Core/TitleDatabase.cpp | 28 +- Source/Core/Core/TitleDatabase.h | 3 +- Source/Core/DiscIO/Enums.h | 9 +- Source/Core/DiscIO/Volume.h | 1 - Source/Core/DiscIO/VolumeDisc.cpp | 107 +- Source/Core/DiscIO/VolumeDisc.h | 24 + Source/Core/DiscIO/VolumeGC.cpp | 35 +- Source/Core/DiscIO/VolumeGC.h | 14 +- Source/Core/DolphinLib.props | 4 + .../Config/GamecubeControllersWidget.cpp | 2 + Source/Core/DolphinQt/Config/InfoWidget.cpp | 6 - .../DolphinQt/Config/Mapping/GCPadEmu.cpp | 15 +- .../Config/Mapping/MappingWindow.cpp | 16 +- Source/Core/DolphinQt/MainWindow.cpp | 44 + Source/Core/DolphinQt/MenuBar.cpp | 4 + Source/Core/DolphinQt/MenuBar.h | 1 + .../Core/DolphinQt/Settings/GameCubePane.cpp | 43 +- Source/Core/InputCommon/GCPadStatus.h | 9 + Source/Core/UICommon/GameFile.cpp | 5 +- Source/Core/UICommon/GameFile.h | 2 - Source/Core/UICommon/GameFileCache.cpp | 2 +- Source/Core/UICommon/UICommon.cpp | 1 + 61 files changed, 5627 insertions(+), 174 deletions(-) create mode 100644 Data/Sys/GameSettings/GEYJ6E.ini create mode 100644 Data/Sys/GameSettings/GEYP6E.ini create mode 100644 Data/Sys/GameSettings/GGGE6E.ini create mode 100644 Data/Sys/GameSettings/GHAE6E.ini create mode 100644 Data/Sys/GameSettings/GHZW6E.ini create mode 100644 Data/Sys/GameSettings/GJAP6E.ini create mode 100644 Data/Sys/GameSettings/GLKJ6E.ini create mode 100644 Data/Sys/GameSettings/GLLP6E.ini create mode 100644 Source/Core/Core/HW/DVD/AMMediaboard.cpp create mode 100644 Source/Core/Core/HW/DVD/AMMediaboard.h create mode 100644 Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.cpp create mode 100644 Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.h diff --git a/Data/Sys/GameSettings/GEYJ6E.ini b/Data/Sys/GameSettings/GEYJ6E.ini new file mode 100644 index 0000000000..c410c05bd7 --- /dev/null +++ b/Data/Sys/GameSettings/GEYJ6E.ini @@ -0,0 +1,4 @@ +# GEYJ6E - Virtua Striker 2002 (Japan) + +[Core] +FPRF = True diff --git a/Data/Sys/GameSettings/GEYP6E.ini b/Data/Sys/GameSettings/GEYP6E.ini new file mode 100644 index 0000000000..ccfcf0a9e4 --- /dev/null +++ b/Data/Sys/GameSettings/GEYP6E.ini @@ -0,0 +1,4 @@ +# GEYP6E - Virtua Striker 2002 (Export, Type3) + +[Core] +FPRF = True diff --git a/Data/Sys/GameSettings/GGGE6E.ini b/Data/Sys/GameSettings/GGGE6E.ini new file mode 100644 index 0000000000..8391b80aca --- /dev/null +++ b/Data/Sys/GameSettings/GGGE6E.ini @@ -0,0 +1,5 @@ +# GGGE6E - F-ZERO AX + +[Core] +FPRF = True + diff --git a/Data/Sys/GameSettings/GHAE6E.ini b/Data/Sys/GameSettings/GHAE6E.ini new file mode 100644 index 0000000000..4f6821b253 --- /dev/null +++ b/Data/Sys/GameSettings/GHAE6E.ini @@ -0,0 +1,5 @@ +# GHAE6E - F-ZERO AX (Monster) + +[Core] +FPRF = True + diff --git a/Data/Sys/GameSettings/GHZW6E.ini b/Data/Sys/GameSettings/GHZW6E.ini new file mode 100644 index 0000000000..afd22fb56c --- /dev/null +++ b/Data/Sys/GameSettings/GHZW6E.ini @@ -0,0 +1,4 @@ +# GHZW6E - VIRTUA STRIKER 4 (Asia) + +[Core] +FPRF = True diff --git a/Data/Sys/GameSettings/GJAP6E.ini b/Data/Sys/GameSettings/GJAP6E.ini new file mode 100644 index 0000000000..071cb0d5b9 --- /dev/null +++ b/Data/Sys/GameSettings/GJAP6E.ini @@ -0,0 +1,4 @@ +# GJAP6E - VIRTUA STRIKER 4 (Export) + +[Core] +FPRF = True diff --git a/Data/Sys/GameSettings/GLKJ6E.ini b/Data/Sys/GameSettings/GLKJ6E.ini new file mode 100644 index 0000000000..9b981daee6 --- /dev/null +++ b/Data/Sys/GameSettings/GLKJ6E.ini @@ -0,0 +1,4 @@ +# GLKJ6E - VIRTUA STRIKER 4 Ver.2006 (Japan) + +[Core] +FPRF = True diff --git a/Data/Sys/GameSettings/GLLP6E.ini b/Data/Sys/GameSettings/GLLP6E.ini new file mode 100644 index 0000000000..b607f28de4 --- /dev/null +++ b/Data/Sys/GameSettings/GLLP6E.ini @@ -0,0 +1,4 @@ +# GLLP6E - VIRTUA STRIKER 4 Ver.2006 (Export) + +[Core] +FPRF = True diff --git a/Source/Core/Common/CommonPaths.h b/Source/Core/Common/CommonPaths.h index d0efa66006..474890c4d8 100644 --- a/Source/Core/Common/CommonPaths.h +++ b/Source/Core/Common/CommonPaths.h @@ -41,11 +41,13 @@ #define USA_DIR "USA" #define JAP_DIR "JAP" #define JPN_DIR "JPN" +#define DEV_DIR "DEV" // Subdirs in the User dir returned by GetUserPath(D_USER_IDX) #define GC_USER_DIR "GC" #define GBA_USER_DIR "GBA" #define WII_USER_DIR "Wii" +#define TRI_USER_DIR "Triforce" #define CONFIG_DIR "Config" #define GAMESETTINGS_DIR "GameSettings" #define MAPS_DIR "Maps" @@ -155,6 +157,7 @@ // Subdirs in Sys #define GC_SYS_DIR "GC" #define WII_SYS_DIR "Wii" +#define TRI_SYS_DIR "Triforce" // Subdirs in Config #define GRAPHICSMOD_CONFIG_DIR "GraphicMods" diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 05be18b610..1dbbb4ac41 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -838,6 +838,7 @@ static void RebuildUserDirectories(unsigned int dir_index) case D_USER_IDX: s_user_paths[D_GCUSER_IDX] = s_user_paths[D_USER_IDX] + GC_USER_DIR DIR_SEP; s_user_paths[D_WIIROOT_IDX] = s_user_paths[D_USER_IDX] + WII_USER_DIR DIR_SEP; + s_user_paths[D_TRIUSER_IDX] = s_user_paths[D_USER_IDX] + TRI_USER_DIR DIR_SEP; s_user_paths[D_CONFIG_IDX] = s_user_paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; s_user_paths[D_GAMESETTINGS_IDX] = s_user_paths[D_USER_IDX] + GAMESETTINGS_DIR DIR_SEP; s_user_paths[D_MAPS_IDX] = s_user_paths[D_USER_IDX] + MAPS_DIR DIR_SEP; diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index 49f6847768..cfe6eddffb 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -26,7 +26,8 @@ enum { D_USER_IDX, D_GCUSER_IDX, - D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory + D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory + D_TRIUSER_IDX, D_SESSION_WIIROOT_IDX, // may point to minimal temporary directory for determinism D_CONFIG_IDX, // global settings D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default diff --git a/Source/Core/Common/Logging/Log.h b/Source/Core/Common/Logging/Log.h index 9e55f779cd..64d353af00 100644 --- a/Source/Core/Common/Logging/Log.h +++ b/Source/Core/Common/Logging/Log.h @@ -27,6 +27,8 @@ enum class LogType : int DSP_MAIL, DSPINTERFACE, DVDINTERFACE, + AMMEDIABOARD, + AMMEDIABOARD_NET, DYNA_REC, EXPANSIONINTERFACE, FILEMON, @@ -58,6 +60,9 @@ enum class LogType : int PROCESSORINTERFACE, POWERPC, SERIALINTERFACE, + SERIALINTERFACE_AMBB, + SERIALINTERFACE_CARD, + SERIALINTERFACE_JVSIO, SP1, SYMBOLS, VIDEO, diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 0755dc92ab..6c15608420 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -111,6 +111,8 @@ LogManager::LogManager() m_log[LogType::DSP_MAIL] = {"DSPMails", "DSP Mails"}; m_log[LogType::DSPINTERFACE] = {"DSP", "DSP Interface"}; m_log[LogType::DVDINTERFACE] = {"DVD", "DVD Interface"}; + m_log[LogType::AMMEDIABOARD] = {"AMMB", "AMMB Interface"}; + m_log[LogType::AMMEDIABOARD_NET] = {"AMMB_NET", "AMMB - Network"}; m_log[LogType::DYNA_REC] = {"JIT", "JIT Dynamic Recompiler"}; m_log[LogType::EXPANSIONINTERFACE] = {"EXI", "Expansion Interface"}; m_log[LogType::FILEMON] = {"FileMon", "File Monitor"}; @@ -142,6 +144,9 @@ LogManager::LogManager() m_log[LogType::PROCESSORINTERFACE] = {"PI", "Processor Interface"}; m_log[LogType::POWERPC] = {"PowerPC", "PowerPC IBM CPU"}; m_log[LogType::SERIALINTERFACE] = {"SI", "Serial Interface"}; + m_log[LogType::SERIALINTERFACE_AMBB] = {"SI_AMBB", "AMBB Interface"}; + m_log[LogType::SERIALINTERFACE_CARD] = {"SI_CARD", "CARD Interface"}; + m_log[LogType::SERIALINTERFACE_JVSIO] = {"SI_JVS", "JVS-I/O"}; m_log[LogType::SP1] = {"SP1", "Serial Port 1"}; m_log[LogType::SYMBOLS] = {"SYMBOLS", "Symbols"}; m_log[LogType::VIDEO] = {"Video", "Video Backend"}; diff --git a/Source/Core/Core/Boot/Boot.cpp b/Source/Core/Core/Boot/Boot.cpp index 1f87d06281..2ccf0cdcc4 100644 --- a/Source/Core/Core/Boot/Boot.cpp +++ b/Source/Core/Core/Boot/Boot.cpp @@ -36,6 +36,7 @@ #include "Core/ConfigManager.h" #include "Core/FifoPlayer/FifoPlayer.h" #include "Core/HLE/HLE.h" +#include "Core/HW/DVD/AMMediaboard.h" #include "Core/HW/DVD/DVDInterface.h" #include "Core/HW/EXI/EXI_DeviceIPL.h" #include "Core/HW/Memmap.h" @@ -373,7 +374,8 @@ bool CBoot::Load_BS2(Core::System& system, const std::string& boot_rom_filename) constexpr u32 PAL_v1_0 = 0x4F319F43; // DOL-101(EUR) (PAL Revision 1.2) constexpr u32 PAL_v1_2 = 0xAD1B7F16; - constexpr u32 Triforce = 0xD1883221; // The Triforce's special IPL + // Triforce Arcade IPL (DEV Revision 1.0) + constexpr u32 Triforce = 0xD1883221; // Load the IPL ROM dump, limited to 2MiB which is the size of the official IPLs. constexpr size_t max_ipl_size = 2 * 1024 * 1024; @@ -407,6 +409,7 @@ bool CBoot::Load_BS2(Core::System& system, const std::string& boot_rom_filename) break; case Triforce: known_ipl = true; + break; default: PanicAlertFmtT("The IPL file is not a known good dump. (CRC32: {0:x})", ipl_hash); break; @@ -457,6 +460,14 @@ bool CBoot::Load_BS2(Core::System& system, const std::string& boot_rom_filename) ppc_state.spr[SPR_DBAT3L] = 0xfff00001; SetupBAT(system, /*is_wii*/ false); + if (ipl_hash == Triforce) + { + HLE::Patch(system, 0x813048B8, "OSReport"); + HLE::Patch(system, 0x8130095C, "OSReport"); // Apploader + + AMMediaboard::FirmwareMap(true); + } + ppc_state.pc = 0x81200150; system.GetPowerPC().MSRUpdated(); @@ -490,6 +501,12 @@ bool CBoot::BootUp(Core::System& system, const Core::CPUThreadGuard& guard, { SConfig& config = SConfig::GetInstance(); + // Triforce systems are region free and always must use the NTSC video mode + if (system.IsTriforce()) + { + config.m_region = DiscIO::Region::NTSC_J; + } + // PAL Wii uses NTSC framerate and linecount in 60Hz modes system.GetVideoInterface().Preset(DiscIO::IsNTSC(config.m_region) || (system.IsWii() && Config::Get(Config::SYSCONF_PAL60))); diff --git a/Source/Core/Core/Boot/Boot_BS2Emu.cpp b/Source/Core/Core/Boot/Boot_BS2Emu.cpp index 3f88b29e3b..31ce5c8c8b 100644 --- a/Source/Core/Core/Boot/Boot_BS2Emu.cpp +++ b/Source/Core/Core/Boot/Boot_BS2Emu.cpp @@ -21,6 +21,7 @@ #include "Core/Core.h" #include "Core/Debugger/BranchWatch.h" #include "Core/HLE/HLE.h" +#include "Core/HW/DVD/AMMediaboard.h" #include "Core/HW/DVD/DVDInterface.h" #include "Core/HW/EXI/EXI_DeviceIPL.h" #include "Core/HW/Memmap.h" @@ -228,12 +229,20 @@ bool CBoot::RunApploader(Core::System& system, const Core::CPUThreadGuard& guard ppc_state.pc = ppc_state.gpr[3]; branch_watch.SetRecordingActive(guard, resume_branch_watch); - // Blank out session key (https://debugmo.de/2008/05/part-2-dumping-the-media-board/) - if (volume.GetVolumeType() == DiscIO::Platform::Triforce) + + if (system.IsTriforce()) { auto& memory = system.GetMemory(); + const u32 disc_size = volume.GetDataSize(); - memory.Memset(0, 0, 12); + // Load game into RAM, like on the actual Triforce + u8* dimm = AMMediaboard::InitDIMM(disc_size); + + volume.Read(0, disc_size, dimm, DiscIO::PARTITION_NONE); + + // Triforce disc register obfuscation + AMMediaboard::InitKeys(memory.Read_U32(0), memory.Read_U32(4), memory.Read_U32(8)); + AMMediaboard::FirmwareMap(false); } return true; diff --git a/Source/Core/Core/CMakeLists.txt b/Source/Core/Core/CMakeLists.txt index 85e446e2a0..e0cad85863 100644 --- a/Source/Core/Core/CMakeLists.txt +++ b/Source/Core/Core/CMakeLists.txt @@ -181,6 +181,8 @@ add_library(core HW/DSPLLE/DSPSymbols.h HW/DVD/DVDInterface.cpp HW/DVD/DVDInterface.h + HW/DVD/AMMediaboard.cpp + HW/DVD/AMMediaboard.h HW/DVD/DVDMath.cpp HW/DVD/DVDMath.h HW/DVD/DVDThread.cpp @@ -274,6 +276,8 @@ add_library(core HW/SI/SI_DeviceGCSteeringWheel.h HW/SI/SI_DeviceKeyboard.cpp HW/SI/SI_DeviceKeyboard.h + HW/SI/SI_DeviceAMBaseboard.cpp + HW/SI/SI_DeviceAMBaseboard.h HW/SI/SI_DeviceNull.cpp HW/SI/SI_DeviceNull.h HW/SI/SI.cpp diff --git a/Source/Core/Core/Config/MainSettings.cpp b/Source/Core/Core/Config/MainSettings.cpp index d11d48c898..3d370c8b1e 100644 --- a/Source/Core/Core/Config/MainSettings.cpp +++ b/Source/Core/Core/Config/MainSettings.cpp @@ -20,6 +20,7 @@ #include "Common/Version.h" #include "Core/AchievementManager.h" #include "Core/Config/DefaultLocale.h" +#include "Core/Core.h" #include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/GCMemcard/GCMemcard.h" @@ -27,6 +28,7 @@ #include "Core/HW/Memmap.h" #include "Core/HW/SI/SI_Device.h" #include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" #include "Core/USBUtils.h" #include "DiscIO/Enums.h" #include "VideoCommon/VideoBackendBase.h" @@ -672,6 +674,9 @@ const char* GetDirectoryForRegion(DiscIO::Region region, RegionDirectoryStyle st ASSERT_MSG(BOOT, false, "NTSC-K is not a valid GameCube region"); return style == RegionDirectoryStyle::Legacy ? JAP_DIR : JPN_DIR; + case DiscIO::Region::DEV: + return DEV_DIR; + default: ASSERT_MSG(BOOT, false, "Default case should not be reached"); return EUR_DIR; @@ -725,6 +730,7 @@ std::string GetMemcardPath(std::string configured_filename, ExpansionInterface:: constexpr std::string_view us_region = "." USA_DIR; constexpr std::string_view jp_region = "." JAP_DIR; constexpr std::string_view eu_region = "." EUR_DIR; + constexpr std::string_view dv_region = "." DEV_DIR; std::optional path_region = std::nullopt; if (name.ends_with(us_region)) { @@ -741,6 +747,11 @@ std::string GetMemcardPath(std::string configured_filename, ExpansionInterface:: name = name.substr(0, name.size() - eu_region.size()); path_region = DiscIO::Region::PAL; } + else if (name.ends_with(dv_region)) + { + name = name.substr(0, name.size() - dv_region.size()); + path_region = DiscIO::Region::DEV; + } const DiscIO::Region used_region = region ? *region : (path_region ? *path_region : Config::Get(Config::MAIN_FALLBACK_REGION)); @@ -784,6 +795,7 @@ std::string GetGCIFolderPath(std::string configured_folder, ExpansionInterface:: constexpr std::string_view us_region = "/" USA_DIR; constexpr std::string_view jp_region = "/" JPN_DIR; constexpr std::string_view eu_region = "/" EUR_DIR; + constexpr std::string_view dv_region = "/" DEV_DIR; std::string_view base_path = configured_folder; std::optional path_region = std::nullopt; if (base_path.ends_with(us_region)) @@ -801,6 +813,11 @@ std::string GetGCIFolderPath(std::string configured_folder, ExpansionInterface:: base_path = base_path.substr(0, base_path.size() - eu_region.size()); path_region = DiscIO::Region::PAL; } + else if (base_path.ends_with(dv_region)) + { + base_path = base_path.substr(0, base_path.size() - dv_region.size()); + path_region = DiscIO::Region::DEV; + } const DiscIO::Region used_region = region ? *region : (path_region ? *path_region : Config::Get(Config::MAIN_FALLBACK_REGION)); diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index 4268b41481..8404cfff07 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -142,12 +142,6 @@ const std::string SConfig::GetTitleDescription() const return m_title_description; } -std::string SConfig::GetTriforceID() const -{ - std::lock_guard lock(m_metadata_lock); - return m_triforce_id; -} - u64 SConfig::GetTitleID() const { std::lock_guard lock(m_metadata_lock); @@ -163,7 +157,7 @@ u16 SConfig::GetRevision() const void SConfig::ResetRunningGameMetadata() { std::lock_guard lock(m_metadata_lock); - SetRunningGameMetadata("00000000", "", "", 0, 0, DiscIO::Region::Unknown); + SetRunningGameMetadata("00000000", "", 0, 0, DiscIO::Region::Unknown); } void SConfig::SetRunningGameMetadata(const DiscIO::Volume& volume, @@ -172,14 +166,14 @@ void SConfig::SetRunningGameMetadata(const DiscIO::Volume& volume, std::lock_guard lock(m_metadata_lock); if (partition == volume.GetGamePartition()) { - SetRunningGameMetadata(volume.GetGameID(), volume.GetGameTDBID(), volume.GetTriforceID(), + SetRunningGameMetadata(volume.GetGameID(), volume.GetGameTDBID(), volume.GetTitleID().value_or(0), volume.GetRevision().value_or(0), volume.GetRegion()); } else { SetRunningGameMetadata(volume.GetGameID(partition), volume.GetGameTDBID(partition), - volume.GetTriforceID(), volume.GetTitleID(partition).value_or(0), + volume.GetTitleID(partition).value_or(0), volume.GetRevision(partition).value_or(0), volume.GetRegion()); } } @@ -197,28 +191,25 @@ void SConfig::SetRunningGameMetadata(const IOS::ES::TMDReader& tmd, DiscIO::Plat !Core::System::GetInstance().GetDVDInterface().UpdateRunningGameMetadata(tmd_title_id)) { // If not launching a disc game, just read everything from the TMD. - SetRunningGameMetadata(tmd.GetGameID(), tmd.GetGameTDBID(), "", tmd_title_id, - tmd.GetTitleVersion(), tmd.GetRegion()); + SetRunningGameMetadata(tmd.GetGameID(), tmd.GetGameTDBID(), tmd_title_id, tmd.GetTitleVersion(), + tmd.GetRegion()); } } void SConfig::SetRunningGameMetadata(const std::string& game_id) { std::lock_guard lock(m_metadata_lock); - SetRunningGameMetadata(game_id, "", "", 0, 0, DiscIO::Region::Unknown); + SetRunningGameMetadata(game_id, "", 0, 0, DiscIO::Region::Unknown); } void SConfig::SetRunningGameMetadata(const std::string& game_id, const std::string& gametdb_id, - std::string triforce_id, u64 title_id, u16 revision, - DiscIO::Region region) + u64 title_id, u16 revision, DiscIO::Region region) { std::lock_guard lock(m_metadata_lock); const bool was_changed = m_game_id != game_id || m_gametdb_id != gametdb_id || - m_triforce_id != triforce_id || m_title_id != title_id || - m_revision != revision; + m_title_id != title_id || m_revision != revision; m_game_id = game_id; m_gametdb_id = gametdb_id; - m_triforce_id = triforce_id; m_title_id = title_id; m_revision = revision; @@ -251,7 +242,7 @@ void SConfig::SetRunningGameMetadata(const std::string& game_id, const std::stri const Core::TitleDatabase title_database; auto& system = Core::System::GetInstance(); const DiscIO::Language language = GetLanguageAdjustedForRegion(system.IsWii(), region); - m_title_name = title_database.GetTitleName(m_gametdb_id, m_triforce_id, language); + m_title_name = title_database.GetTitleName(m_gametdb_id, language); m_title_description = title_database.Describe(m_gametdb_id, language); NOTICE_LOG_FMT(CORE, "Active title: {}", m_title_description); Host_TitleChanged(); @@ -458,6 +449,9 @@ bool SConfig::SetPathsAndGameMetadata(Core::System& system, const BootParameters if (m_region == DiscIO::Region::Unknown) m_region = Config::Get(Config::MAIN_FALLBACK_REGION); + if (system.IsTriforce()) + m_region = DiscIO::Region::DEV; + // Set up paths const std::string region_dir = Config::GetDirectoryForRegion(Config::ToGameCubeRegion(m_region)); m_strSRAM = File::GetUserPath(F_GCSRAM_IDX); diff --git a/Source/Core/Core/ConfigManager.h b/Source/Core/Core/ConfigManager.h index b2e27892b9..39dbc6d39a 100644 --- a/Source/Core/Core/ConfigManager.h +++ b/Source/Core/Core/ConfigManager.h @@ -54,7 +54,6 @@ struct SConfig // files std::string m_strBootROM; std::string m_strSRAM; - std::string m_debugger_game_id; // TODO: remove this as soon as the ticket view hack in IOS/ES/Views is dropped. @@ -64,7 +63,6 @@ struct SConfig const std::string GetGameTDBID() const; const std::string GetTitleName() const; const std::string GetTitleDescription() const; - std::string GetTriforceID() const; u64 GetTitleID() const; u16 GetRevision() const; void ResetRunningGameMetadata(); @@ -121,15 +119,13 @@ private: static void ReloadTextures(Core::System& system); void SetRunningGameMetadata(const std::string& game_id, const std::string& gametdb_id, - std::string triforce_id, u64 title_id, u16 revision, - DiscIO::Region region); + u64 title_id, u16 revision, DiscIO::Region region); static SConfig* m_Instance; mutable std::recursive_mutex m_metadata_lock; std::string m_game_id; std::string m_gametdb_id; - std::string m_triforce_id; std::string m_title_name; std::string m_title_description; u64 m_title_id; diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index bdae112865..0ddf5b11d1 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -51,6 +51,7 @@ #include "Core/HLE/HLE.h" #include "Core/HW/CPU.h" #include "Core/HW/DSP.h" +#include "Core/HW/DVD/AMMediaboard.h" #include "Core/HW/EXI/EXI.h" #include "Core/HW/GBAPad.h" #include "Core/HW/GCKeyboard.h" @@ -288,6 +289,13 @@ void Stop(Core::System& system) // - Hammertime! // Stop the CPU INFO_LOG_FMT(CONSOLE, "{}", StopMessage(true, "Stop CPU")); system.GetCPU().Stop(); + + const ExpansionInterface::EXIDeviceType type = Config::Get(Config::MAIN_SERIAL_PORT_1); + + if ((type == ExpansionInterface::EXIDeviceType::Baseboard)) + { + AMMediaboard::Shutdown(); + } } void DeclareAsCPUThread() diff --git a/Source/Core/Core/HW/DVD/AMMediaboard.cpp b/Source/Core/Core/HW/DVD/AMMediaboard.cpp new file mode 100644 index 0000000000..1d6b77ae63 --- /dev/null +++ b/Source/Core/Core/HW/DVD/AMMediaboard.cpp @@ -0,0 +1,1850 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Core/HW/DVD/AMMediaboard.h" + +#include +#include + +#include + +#include "Common/Buffer.h" +#include "Common/CommonTypes.h" +#include "Common/FileUtil.h" +#include "Common/IOFile.h" +#include "Common/Logging/Log.h" + +#include "Core/Boot/Boot.h" +#include "Core/BootManager.h" +#include "Core/ConfigManager.h" +#include "Core/Core.h" +#include "Core/HLE/HLE.h" +#include "Core/HW/DVD/DVDInterface.h" +#include "Core/HW/DVD/DVDThread.h" +#include "Core/HW/EXI/EXI.h" +#include "Core/HW/EXI/EXI_DeviceBaseboard.h" +#include "Core/HW/MMIO.h" +#include "Core/HW/Memmap.h" +#include "Core/HW/SI/SI.h" +#include "Core/HW/SI/SI_Device.h" +#include "Core/IOS/Network/Socket.h" +#include "Core/Movie.h" +#include "Core/System.h" + +#include "DiscIO/DirectoryBlob.h" + +#if defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__) or defined(__NetBSD__) or \ + defined(__HAIKU__) + +#include + +#define closesocket close +#define ioctlsocket ioctl + +#define WSAEWOULDBLOCK 10035L +#define SOCKET_ERROR (-1) + +typedef int SOCKET; + +#define INVALID_SOCKET (SOCKET)(~0) + +static int WSAGetLastError(void) +{ + switch (errno) + { + case EWOULDBLOCK: + return WSAEWOULDBLOCK; + default: + break; + } + + return errno; +} + +#endif + +namespace AMMediaboard +{ + +static bool s_firmware_map = false; +static bool s_test_menu = false; +static SOCKET s_namco_cam = 0; +static u32 s_timeouts[3] = {20000, 20000, 20000}; +static u32 s_last_error = SSC_SUCCESS; + +static u32 s_GCAM_key_a = 0; +static u32 s_GCAM_key_b = 0; +static u32 s_GCAM_key_c = 0; + +static File::IOFile s_netcfg; +static File::IOFile s_netctrl; +static File::IOFile s_extra; +static File::IOFile s_backup; +static File::IOFile s_dimm; + +Common::UniqueBuffer s_dimm_disc; + +static u8 s_firmware[2 * 1024 * 1024]; +static u32 s_media_buffer_32[192]; +static u8* const s_media_buffer = reinterpret_cast(s_media_buffer_32); +static u8 s_network_command_buffer[0x4FFE00]; +static u8 s_network_buffer[512 * 1024]; +static u8 s_allnet_buffer[4096]; +static u8 s_allnet_settings[0x8500]; + +constexpr char s_allnet_reply[] = { + "uri=http://" + "sega.com&host=sega.com&nickname=sega&name=sega&year=2025&month=08&day=16&hour=21&minute=10&" + "second=12&place_id=1234&setting=0x123®ion0=jap®ion_name0=japan®ion_name1=usa®ion_" + "name2=asia®ion_name3=export&end"}; + +// Sockets FDs are required to go from 0 to 63. +// Games use the FD as indexes so we have to workaround it. + +static SOCKET s_sockets[64]; + +static SOCKET socket_(int af, int type, int protocol) +{ + for (u32 i = 1; i < 64; ++i) + { + if (s_sockets[i] == SOCKET_ERROR) + { + s_sockets[i] = socket(af, type, protocol); + return i; + } + } + + // Out of sockets + return SOCKET_ERROR; +} + +static SOCKET accept_(int fd, sockaddr* addr, int* len) +{ + for (u32 i = 1; i < 64; ++i) + { + if (s_sockets[i] == SOCKET_ERROR) + { + s_sockets[i] = accept(fd, addr, (socklen_t*)len); + if (s_sockets[i] == SOCKET_ERROR) + return SOCKET_ERROR; + return i; + } + } + + // Out of sockets + return SOCKET_ERROR; +} + +static inline void PrintMBBuffer(u32 address, u32 length) +{ + const auto& system = Core::System::GetInstance(); + const auto& memory = system.GetMemory(); + + for (u32 i = 0; i < length; i += 0x10) + { + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: {:08x} {:08x} {:08x} {:08x}", memory.Read_U32(address + i), + memory.Read_U32(address + i + 4), memory.Read_U32(address + i + 8), + memory.Read_U32(address + i + 12)); + } +} + +void FirmwareMap(bool on) +{ + s_firmware_map = on; +} + +void InitKeys(u32 key_a, u32 key_b, u32 key_c) +{ + s_GCAM_key_a = key_a; + s_GCAM_key_b = key_b; + s_GCAM_key_c = key_c; +} + +static File::IOFile OpenOrCreateFile(const std::string& filename) +{ + // Try opening for read/write first + if (File::Exists(filename)) + return File::IOFile(filename, "rb+"); + + // Create new file + return File::IOFile(filename, "wb+"); +} + +void Init() +{ + std::ranges::fill(s_media_buffer_32, 0); + std::ranges::fill(s_network_buffer, 0); + std::ranges::fill(s_network_command_buffer, 0); + std::ranges::fill(s_firmware, -1); + std::ranges::fill(s_sockets, SOCKET_ERROR); + std::ranges::fill(s_allnet_buffer, 0); + std::ranges::fill(s_allnet_settings, 0); + + s_firmware_map = false; + s_test_menu = false; + + s_last_error = SSC_SUCCESS; + + s_GCAM_key_a = 0; + s_GCAM_key_b = 0; + s_GCAM_key_c = 0; + + const std::string base_path = File::GetUserPath(D_TRIUSER_IDX); + + s_netcfg = OpenOrCreateFile(base_path + "trinetcfg.bin"); + s_netctrl = OpenOrCreateFile(base_path + "trinetctrl.bin"); + s_extra = OpenOrCreateFile(base_path + "triextra.bin"); + s_dimm = OpenOrCreateFile(base_path + "tridimm_" + SConfig::GetInstance().GetGameID() + ".bin"); + s_backup = OpenOrCreateFile(base_path + "backup_" + SConfig::GetInstance().GetGameID() + ".bin"); + + if (!s_netcfg.IsOpen()) + PanicAlertFmt("Failed to open/create: {}", base_path + "trinetcfg.bin"); + if (!s_netctrl.IsOpen()) + PanicAlertFmt("Failed to open/create: {}", base_path + "trinetctrl.bin"); + if (!s_extra.IsOpen()) + PanicAlertFmt("Failed to open/create: {}", base_path + "triextra.bin"); + if (!s_dimm.IsOpen()) + PanicAlertFmt("Failed to open/create: {}", base_path + "tridimm.bin"); + if (!s_backup.IsOpen()) + PanicAlertFmt("Failed to open/create: {}", base_path + "backup.bin"); + + // This is the firmware for the Triforce + const std::string sega_boot_filename = base_path + "segaboot.gcm"; + + if (!File::Exists(sega_boot_filename)) + { + PanicAlertFmt("Failed to open segaboot.gcm({}), which is required for test menus.", + sega_boot_filename.c_str()); + return; + } + + File::IOFile sega_boot(sega_boot_filename, "rb+"); + if (!sega_boot.IsOpen()) + { + PanicAlertFmt("Failed to read: {}", sega_boot_filename); + return; + } + + const u64 length = std::min(sega_boot.GetSize(), sizeof(s_firmware)); + sega_boot.ReadBytes(s_firmware, length); + + s_test_menu = true; +} + +u8* InitDIMM(u32 size) +{ + if (size == 0) + return nullptr; + + if (!s_dimm_disc.size()) + { + s_dimm_disc.reset(size); + if (!s_dimm_disc.size()) + { + PanicAlertFmt("Failed to allocate DIMM memory."); + return nullptr; + } + } + + s_firmware_map = 0; + return s_dimm_disc.data(); +} + +static s32 NetDIMMAccept(int fd, sockaddr* addr, int* len) +{ + SOCKET clientSock = INVALID_SOCKET; + fd_set readfds; + + FD_ZERO(&readfds); + FD_SET(fd, &readfds); + + timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 10000; // 10 milliseconds + + const int result = select(fd + 1, &readfds, nullptr, nullptr, &timeout); + if (result > 0 && FD_ISSET(fd, &readfds)) + { + clientSock = accept_(fd, addr, len); + if (clientSock != INVALID_SOCKET) + { + s_last_error = SSC_SUCCESS; + return clientSock; + } + else + { + s_last_error = SOCKET_ERROR; + return SOCKET_ERROR; + } + } + else if (result == 0) + { + // Timeout + s_last_error = SSC_EWOULDBLOCK; + } + else + { + // select() failed + s_last_error = SOCKET_ERROR; + } + + return SOCKET_ERROR; +} + +static s32 NetDIMMConnect(int fd, sockaddr_in* addr, int len) +{ + // All.Net Connect IP + if (addr->sin_addr.s_addr == inet_addr("192.168.150.16")) + { + addr->sin_addr.s_addr = inet_addr("127.0.0.1"); + } + + // CyCraft Connect IP + if (addr->sin_addr.s_addr == inet_addr("192.168.11.111")) + { + addr->sin_addr.s_addr = inet_addr("127.0.0.1"); + } + + // NAMCO Camera ( IPs are: 192.168.29.104-108 ) + if ((addr->sin_addr.s_addr & 0xFFFFFF00) == 0xC0A81D00) + { + addr->sin_addr.s_addr = inet_addr("127.0.0.1"); + + // BUG: An invalid family value is being used + addr->sin_family = htons(AF_INET); + s_namco_cam = fd; + } + + // Key of Avalon Client + if (addr->sin_addr.s_addr == inet_addr("192.168.13.1")) + { + addr->sin_addr.s_addr = inet_addr("10.0.0.45"); + } + + addr->sin_family = Common::swap16(addr->sin_family); + + u_long val = 1; + // Set socket to non-blocking + ioctlsocket(fd, FIONBIO, &val); + + int ret = connect(fd, reinterpret_cast(addr), len); + const int err = WSAGetLastError(); + + if (ret == SOCKET_ERROR && err == WSAEWOULDBLOCK) + { + fd_set writefds; + FD_ZERO(&writefds); + FD_SET(fd, &writefds); + + timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = s_timeouts[0]; + + ret = select(fd + 1, nullptr, &writefds, nullptr, &timeout); + if (ret > 0 && FD_ISSET(fd, &writefds)) + { + int so_error = 0; + socklen_t optlen = sizeof(so_error); + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, reinterpret_cast(&so_error), &optlen) == 0 && + so_error == 0) + { + s_last_error = SSC_SUCCESS; + ret = 0; + } + else + { + s_last_error = SOCKET_ERROR; + ret = SOCKET_ERROR; + } + } + else if (ret == 0) + { + // Timeout + s_last_error = SSC_EWOULDBLOCK; + ret = SOCKET_ERROR; + } + else + { + // select() failed + s_last_error = SOCKET_ERROR; + ret = SOCKET_ERROR; + } + } + else if (ret == SOCKET_ERROR) + { + // Immediate failure (e.g. WSAECONNREFUSED) + s_last_error = ret; + } + else + { + s_last_error = SSC_SUCCESS; + } + + // Restore blocking mode + val = 0; + ioctlsocket(fd, FIONBIO, &val); + + return ret; +} + +static void FileWriteData(File::IOFile* file, u32 seek_pos, const u8* data, size_t length) +{ + file->Seek(seek_pos, File::SeekOrigin::Begin); + file->WriteBytes(data, length); + file->Flush(); +} + +u32 ExecuteCommand(std::array& DICMDBUF, u32* DIIMMBUF, u32 address, u32 length) +{ + auto& system = Core::System::GetInstance(); + auto& memory = system.GetMemory(); + + DICMDBUF[0] ^= s_GCAM_key_a; + DICMDBUF[1] ^= s_GCAM_key_b; + DICMDBUF[2] ^= s_GCAM_key_c; + + const u32 seed = DICMDBUF[0] >> 16; + + s_GCAM_key_a *= seed; + s_GCAM_key_b *= seed; + s_GCAM_key_c *= seed; + + // Key setup for Triforce IPL: + // These RAM offsets always hold the key for the next command. Since two dummy commands are sent + // before any real ones, you can just use the key from RAM without missing a real command. + + if (s_GCAM_key_a == 0) + { + if (memory.Read_U32(0)) + { + HLE::Patch(system, 0x813048B8, "OSReport"); + HLE::Patch(system, 0x8130095C, "OSReport"); // Apploader + + InitKeys(memory.Read_U32(0), memory.Read_U32(4), memory.Read_U32(8)); + } + } + + const u32 command = DICMDBUF[0] << 24; + const u32 offset = DICMDBUF[1] << 2; + + DEBUG_LOG_FMT(AMMEDIABOARD, + "GC-AM: {:08x} {:08x} DMA=addr:{:08x},len:{:08x} Keys: {:08x} {:08x} {:08x}", + command, offset, address, length, s_GCAM_key_a, s_GCAM_key_b, s_GCAM_key_c); + + // Test mode + if (offset == 0x0002440) + { + // Set by OSResetSystem + if (memory.Read_U32(0x811FFF00) == 1) + { + // Don't map firmware while in SegaBoot + if (memory.Read_U32(0x8006BF70) != 0x0A536567) + { + s_firmware_map = 1; + } + } + } + + switch (AMMBDICommand(command >> 24)) + { + case AMMBDICommand::Inquiry: + if (s_firmware_map) + { + s_firmware_map = false; + } + + // Returned value is used to set the protocol version. + switch (GetGameType()) + { + default: + *DIIMMBUF = Version1; + return 0; + case VirtuaStriker4_2006: + case KeyOfAvalon: + case MarioKartGP: + case MarioKartGP2: + case FirmwareUpdate: + *DIIMMBUF = Version2; + return 0; + } + break; + case AMMBDICommand::Read: + if ((offset & 0x8FFF0000) == 0x80000000) + { + switch (offset) + { + case MediaBoardStatus1: + memory.Write_U16(Common::swap16(0x0100), address); + break; + case MediaBoardStatus2: + memset(memory.GetSpanForAddress(address).data(), 0, length); + break; + case MediaBoardStatus3: + memset(memory.GetSpanForAddress(address).data(), 0xFF, length); + // DIMM size (512MB) + memory.Write_U32(Common::swap32(0x20000000), address); + // GCAM signature + memory.Write_U32(0x4743414D, address + 4); + break; + case 0x80000100: + memory.Write_U32(Common::swap32((u32)0x001F1F1F), address); + break; + case FirmwareStatus1: + memory.Write_U32(Common::swap32((u32)0x01FA), address); + break; + case FirmwareStatus2: + memory.Write_U32(Common::swap32((u32)1), address); + break; + case 0x80000160: + memory.Write_U32(0x00001E00, address); + break; + case 0x80000180: + memory.Write_U32(0, address); + break; + case 0x800001A0: + memory.Write_U32(0xFFFFFFFF, address); + break; + default: + PrintMBBuffer(address, length); + PanicAlertFmtT("Unhandled Media Board Read:{0:08x}", offset); + break; + } + return 0; + } + + // Network configuration + if (offset == 0x00000000 && length == 0x80) + { + s_netcfg.Seek(0, File::SeekOrigin::Begin); + s_netcfg.ReadBytes(memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // media crc check on/off + if (offset == DIMMExtraSettings && length == 0x20) + { + s_extra.Seek(0, File::SeekOrigin::Begin); + s_extra.ReadBytes(memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // DIMM memory (8MB) + if (offset >= DIMMMemory && offset <= 0x1F800000) + { + const u32 dimm_offset = offset - DIMMMemory; + s_dimm.Seek(dimm_offset, File::SeekOrigin::Begin); + s_dimm.ReadBytes(memory.GetSpanForAddress(address).data(), length); + return 0; + } + + if (offset >= AllNetBuffer && offset < 0x89011000) + { + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read All.Net Buffer ({:08x},{})", offset, length); + + // Fake reply + memcpy(memory.GetSpanForAddress(address).data(), s_allnet_reply, sizeof(s_allnet_reply)); + return 0; + } + + if (offset >= DIMMCommandVersion1 && offset < 0x1F900040) + { + const u32 dimm_offset = offset - DIMMCommandVersion1; + memcpy(memory.GetSpanForAddress(address).data(), s_media_buffer + dimm_offset, length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read MEDIA BOARD COMM AREA (1) ({:08x},{})", offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if (offset >= NetworkBufferAddress4 && offset < 0x89240000) + { + const u32 dimm_offset = offset - NetworkBufferAddress4; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK BUFFER (4) ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_buffer + dimm_offset, length); + return 0; + } + + if (offset >= NetworkBufferAddress5 && offset < 0x1FB10000) + { + const u32 dimm_offset = offset - NetworkBufferAddress5; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK BUFFER (5) ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_buffer + dimm_offset, length); + return 0; + } + + if (offset >= NetworkCommandAddress && offset < NetworkBufferAddress2) + { + const u32 dimm_offset = offset - NetworkCommandAddress; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK COMMAND BUFFER ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_command_buffer + dimm_offset, + length); + return 0; + } + + if (offset >= NetworkCommandAddress2 && offset < 0x89060200) + { + const u32 dimm_offset = offset - NetworkCommandAddress2; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK COMMAND BUFFER (2) ({:08x},{})", offset, + length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_command_buffer + dimm_offset, + length); + return 0; + } + + if (offset >= NetworkBufferAddress1 && offset < 0x1FA10000) + { + const u32 dimm_offset = offset - NetworkBufferAddress1; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK BUFFER (1) ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_buffer + dimm_offset, length); + return 0; + } + + if (offset >= NetworkBufferAddress2 && offset < 0x1FD10000) + { + const u32 dimm_offset = offset - NetworkBufferAddress2; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK BUFFER (2) ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_buffer + dimm_offset, length); + return 0; + } + + if (offset >= NetworkBufferAddress3 && offset < 0x89110000) + { + const u32 dimm_offset = offset - NetworkBufferAddress3; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read NETWORK BUFFER (3) ({:08x},{})", offset, length); + memcpy(memory.GetSpanForAddress(address).data(), s_network_buffer + dimm_offset, length); + return 0; + } + + if (offset >= DIMMCommandVersion2 && offset < 0x84000060) + { + const u32 dimm_offset = offset - DIMMCommandVersion2; + memcpy(memory.GetSpanForAddress(address).data(), s_media_buffer + dimm_offset, length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read MEDIA BOARD COMM AREA (2) ({:08x},{})", offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if (offset == DIMMCommandExecute2) + { + const AMMBCommand ammb_command = + static_cast(*reinterpret_cast(s_media_buffer + 0x202)); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Execute command: (2){0:04X}", + static_cast(ammb_command)); + + memcpy(s_media_buffer, s_media_buffer + 0x200, 0x20); + memset(s_media_buffer + 0x200, 0, 0x20); + s_media_buffer[0x204] = 1; + + switch (ammb_command) + { + case AMMBCommand::Unknown_001: + s_media_buffer_32[1] = 1; + break; + case AMMBCommand::GetNetworkFirmVersion: + s_media_buffer_32[1] = 0x1305; // Version: 13.05 + s_media_buffer[6] = 1; // Type: VxWorks + break; + case AMMBCommand::GetSystemFlags: + s_media_buffer[4] = 1; + s_media_buffer[6] = NANDMaskBoardNAND; + s_media_buffer[7] = 1; + break; + // Empty reply + case AMMBCommand::Unknown_103: + break; + case AMMBCommand::Unknown_104: + s_media_buffer[4] = 1; + break; + case AMMBCommand::Accept: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + int ret = -1; + sockaddr addr; + int len = 0; + + // Handle optional parameters + if (s_media_buffer_32[3] == 0 || s_media_buffer_32[4] == 0) + { + ret = NetDIMMAccept(fd, nullptr, nullptr); + } + else + { + const u32 addr_off = s_media_buffer_32[3] - NetworkCommandAddress2; + const u32 len_off = s_media_buffer_32[4] - NetworkCommandAddress2; + + ret = NetDIMMAccept(fd, &addr, &len); + if (len) + { + memcpy((s_network_command_buffer + addr_off), &addr, len); + memcpy((s_network_command_buffer + len_off), &len, sizeof(int)); + } + } + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: accept( {}({}) ):{}\n", fd, s_media_buffer_32[2], + ret); + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::Bind: + { + sockaddr_in addr; + + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + const u32 off = s_media_buffer_32[3] - NetworkCommandAddress2; + const u32 len = s_media_buffer_32[4]; + + memcpy((void*)&addr, s_network_command_buffer + off, sizeof(sockaddr_in)); + + addr.sin_family = Common::swap16(addr.sin_family); + + // Triforce titles typically rely on hardcoded IP addresses. + // This behavior has been modified to bind to the wildcard address instead. + // + // addr.sin_addr.s_addr = htonl(addr.sin_addr.s_addr); + + addr.sin_addr.s_addr = INADDR_ANY; + + const int ret = bind(fd, (const sockaddr*)&addr, len); + const int err = WSAGetLastError(); + + if (ret < 0) + PanicAlertFmt("Socket Bind Failed with{0}", err); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: bind( {}, ({},{:08x}:{}), {} ):{} ({})\n", fd, + addr.sin_family, addr.sin_addr.s_addr, Common::swap16(addr.sin_port), len, + ret, err); + + s_media_buffer_32[1] = ret; + s_last_error = SSC_SUCCESS; + break; + } + case AMMBCommand::Closesocket: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + + const int ret = closesocket(fd); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: closesocket( {}({}) ):{}\n", fd, + s_media_buffer_32[2], ret); + + s_sockets[SocketCheck(s_media_buffer_32[2])] = SOCKET_ERROR; + + s_media_buffer_32[1] = ret; + s_last_error = SSC_SUCCESS; + break; + } + case AMMBCommand::Connect: + { + sockaddr_in addr; + + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + const u32 off = s_media_buffer_32[3] - NetworkCommandAddress2; + const u32 len = s_media_buffer_32[4]; + + memcpy((void*)&addr, s_network_command_buffer + off, sizeof(sockaddr_in)); + + const int ret = NetDIMMConnect(fd, &addr, len); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: connect( {}({}), ({},{}:{}), {} ):{}\n", fd, + s_media_buffer_32[2], addr.sin_family, inet_ntoa(addr.sin_addr), + Common::swap16(addr.sin_port), len, ret); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::InetAddr: + { + const u32 ip = inet_addr((char*)s_network_command_buffer); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: InetAddr( {} )\n", + (char*)s_network_command_buffer); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = Common::swap32(ip); + break; + } + case AMMBCommand::Listen: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + const u32 backlog = s_media_buffer_32[3]; + + const int ret = listen(fd, backlog); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: listen( {}, {} ):{:d}\n", fd, backlog, ret); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::Recv: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + const u32 off = s_media_buffer_32[3]; + u32 len = s_media_buffer_32[4]; + + if (len >= sizeof(s_network_buffer)) + { + len = sizeof(s_network_buffer); + } + + u8* buffer = reinterpret_cast(s_network_buffer + off); + + if (off >= NetworkBufferAddress4 && off < NetworkBufferAddress4 + sizeof(s_network_buffer)) + { + buffer = reinterpret_cast(s_network_buffer + off - NetworkBufferAddress4); + } + else + { + PanicAlertFmt("RECV: Buffer overrun:{0} {1} ", off, len); + } + + const int ret = recv(fd, reinterpret_cast(buffer), len, 0); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: recv( {}, 0x{:08x}, {} ):{} {}\n", fd, off, len, + ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::Send: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + u32 off = s_media_buffer_32[3]; + const u32 len = s_media_buffer_32[4]; + + if (off >= NetworkBufferAddress3 && off < NetworkBufferAddress3 + sizeof(s_network_buffer)) + { + off -= NetworkBufferAddress3; + } + else + { + ERROR_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: send(error) unhandled destination:{:08x}\n", off); + } + + const int ret = send(fd, reinterpret_cast(s_network_buffer + off), len, 0); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: send( {}({}), 0x{:08x}, {} ): {} {}\n", fd, + s_media_buffer_32[2], off, len, ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::Socket: + { + // Protocol is not sent + const u32 af = s_media_buffer_32[2]; + const u32 type = s_media_buffer_32[3]; + + const SOCKET fd = socket_(af, type, IPPROTO_TCP); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: socket( {}, {}, IPPROTO_TCP ):{}\n", af, type, fd); + + s_media_buffer[1] = 0; + s_media_buffer_32[1] = fd; + break; + } + case AMMBCommand::Select: + { + u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2] - 1)]; + + // BUG: NAMCAM is hardcoded to call this with socket ID 0x100 which might be some magic + // thing? A valid value is needed so we use the socket from the connect. + + if (AMMediaboard::GetGameType() == MarioKartGP2) + { + if (s_media_buffer_32[2] == 256) + { + fd = s_namco_cam; + } + } + + fd_set* readfds = nullptr; + fd_set* writefds = nullptr; + fd_set* exceptfds = nullptr; + timeval timeout = {}; + timeval* timeout_src = nullptr; + + // Only one of 3, 4, 5 is ever set alongside 6 + if (s_media_buffer_32[3] && s_media_buffer_32[6]) + { + const u32 ROffset = s_media_buffer_32[6] - NetworkCommandAddress2; + readfds = reinterpret_cast(s_network_command_buffer + ROffset); + FD_ZERO(readfds); + FD_SET(fd, readfds); + + timeout_src = reinterpret_cast(s_network_command_buffer + s_media_buffer_32[3] - + NetworkCommandAddress2); + } + else if (s_media_buffer_32[4] && s_media_buffer_32[6]) + { + const u32 WOffset = s_media_buffer_32[6] - NetworkCommandAddress2; + writefds = reinterpret_cast(s_network_command_buffer + WOffset); + FD_ZERO(writefds); + FD_SET(fd, writefds); + + timeout_src = reinterpret_cast(s_network_command_buffer + s_media_buffer_32[4] - + NetworkCommandAddress2); + } + else if (s_media_buffer_32[5] && s_media_buffer_32[6]) + { + const u32 EOffset = s_media_buffer_32[6] - NetworkCommandAddress2; + exceptfds = reinterpret_cast(s_network_command_buffer + EOffset); + FD_ZERO(exceptfds); + FD_SET(fd, exceptfds); + + timeout_src = reinterpret_cast(s_network_command_buffer + s_media_buffer_32[5] - + NetworkCommandAddress2); + } + + // Copy timeout if set + if (timeout_src) + { + std::memcpy(&timeout, timeout_src, sizeof(timeval)); + } + + // BUG: The game sets timeout to two seconds + if (AMMediaboard::GetGameType() == KeyOfAvalon) + { + timeout.tv_sec = 0; + timeout.tv_usec = 1800; + } + + const int ret = + select(fd + 1, readfds, writefds, exceptfds, timeout_src ? &timeout : nullptr); + + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT( + AMMEDIABOARD_NET, + "GC-AM: select( {}({}), 0x{:08x} 0x{:08x} 0x{:08x} 0x{:08x} ):{} {} {}:{} \n", fd, + s_media_buffer_32[2], s_media_buffer_32[3], s_media_buffer_32[4], s_media_buffer_32[5], + s_media_buffer_32[6], ret, err, timeout.tv_sec, timeout.tv_usec); + // hexdump( s_network_command_buffer, 0x40 ); + + s_media_buffer[1] = 0; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::SetSockOpt: + { + const SOCKET fd = (SOCKET)(s_sockets[SocketCheck(s_media_buffer_32[2])]); + const int level = (int)(s_media_buffer_32[3]); + const int optname = (int)(s_media_buffer_32[4]); + const char* optval = + (char*)(s_network_command_buffer + s_media_buffer_32[5] - NetworkCommandAddress2); + const int optlen = (int)(s_media_buffer_32[6]); + + const int ret = setsockopt(fd, level, optname, optval, optlen); + + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, + "GC-AM: setsockopt( {:d}, {:04x}, {}, {:p}, {} ):{:d} ({})\n", fd, level, + optname, optval, optlen, ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::SetTimeOuts: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + const u32 timeoutA = s_media_buffer_32[3]; + const u32 timeoutB = s_media_buffer_32[4]; + const u32 timeoutC = s_media_buffer_32[5]; + + s_timeouts[0] = timeoutA; + s_timeouts[1] = timeoutB; + s_timeouts[2] = timeoutC; + + int ret = 0; + + if (fd != INVALID_SOCKET) + { + ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeoutB, sizeof(int)); + if (ret < 0) + { + ret = WSAGetLastError(); + } + else + { + ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeoutC, sizeof(int)); + if (ret < 0) + ret = WSAGetLastError(); + } + } + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: SetTimeOuts( {}, {}, {}, {} ):{}\n", fd, timeoutA, + timeoutB, timeoutC, ret); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + break; + } + case AMMBCommand::GetParambyDHCPExec: + { + const u32 value = s_media_buffer_32[2]; + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: GetParambyDHCPExec({})\n", value); + + s_media_buffer[1] = 0; + s_media_buffer_32[1] = 0; + break; + } + case AMMBCommand::ModifyMyIPaddr: + { + const u32 NetBufferOffset = *(u32*)(s_media_buffer + 8) - NetworkCommandAddress2; + + const char* IP = (char*)(s_network_command_buffer + NetBufferOffset); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: modifyMyIPaddr({})\n", IP); + break; + } + case AMMBCommand::GetLastError: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[2])]; + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: GetLastError( {}({}) ):{}\n", fd, + s_media_buffer_32[2], s_last_error); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = s_last_error; + } + break; + case AMMBCommand::InitLink: + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: InitLink"); + break; + case AMMBCommand::AllNetInit: + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: AllNetInit"); + break; + default: + ERROR_LOG_FMT(AMMEDIABOARD, "GC-AM: Command:{0:04x}", static_cast(ammb_command)); + ERROR_LOG_FMT(AMMEDIABOARD, "GC-AM: Command Unhandled!"); + break; + } + + s_media_buffer[3] |= 0x80; // Command complete flag + + memset(memory.GetSpanForAddress(address).data(), 0, length); + + ExpansionInterface::GenerateInterrupt(0x10); + return 0; + } + + if (offset >= DIMMCommandVersion2_2 && offset <= 0x89000200) + { + const u32 dimm_offset = offset - DIMMCommandVersion2_2; + memcpy(memory.GetSpanForAddress(address).data(), s_media_buffer + dimm_offset, length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Read MEDIA BOARD COMM AREA (3) ({:08x})", dimm_offset); + PrintMBBuffer(address, length); + return 0; + } + + // DIMM memory (8MB) + if (offset >= DIMMMemory2 && offset <= 0xFF800000) + { + const u32 dimm_offset = offset - DIMMMemory2; + s_dimm.Seek(dimm_offset, File::SeekOrigin::Begin); + s_dimm.ReadBytes(memory.GetSpanForAddress(address).data(), length); + return 0; + } + + if (offset >= AllNetSettings && offset <= 0x1F000000) + { + const u32 allnet_offset = offset - AllNetSettings; + memcpy(memory.GetSpanForAddress(address).data(), s_allnet_settings + allnet_offset, length); + return 0; + } + + if (offset == NetworkControl && length == 0x20) + { + s_netctrl.Seek(0, File::SeekOrigin::Begin); + s_netctrl.ReadBytes(memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // Max GC disc offset + if (offset >= 0x57058000) + { + PanicAlertFmtT("Unhandled Media Board Read:{0:08x}", offset); + return 0; + } + + if (s_firmware_map) + { + memcpy(memory.GetSpanForAddress(address).data(), s_firmware + offset, length); + return 0; + } + + if (s_dimm_disc.size()) + { + memcpy(memory.GetSpanForAddress(address).data(), s_dimm_disc.data() + offset, length); + return 0; + } + + return 1; + break; + case AMMBDICommand::Write: + + // These two magic writes allow a new firmware to be programmed + if ((offset == FirmwareMagicWrite1) && (length == 0x20)) + { + s_firmware_map = true; + return 0; + } + + if ((offset == FirmwareMagicWrite2) && (length == 0x20)) + { + s_firmware_map = true; + return 0; + } + + if (s_firmware_map) + { + // Firmware memory (2MB) + if ((offset >= 0x00400000) && (offset <= 0x600000)) + { + const u32 fwoffset = offset - 0x00400000; + memcpy(s_firmware + fwoffset, memory.GetSpanForAddress(address).data(), length); + return 0; + } + } + + // Network configuration + if ((offset == 0x00000000) && (length == 0x80)) + { + FileWriteData(&s_netcfg, 0, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // media crc check on/off + if ((offset == DIMMExtraSettings) && (length == 0x20)) + { + FileWriteData(&s_extra, 0, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // Backup memory (8MB) + if ((offset >= BackupMemory) && (offset <= 0x00800000)) + { + FileWriteData(&s_backup, 0, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // DIMM memory (8MB) + if ((offset >= DIMMMemory) && (offset <= 0x1F800000)) + { + const u32 dimm_offset = offset - DIMMMemory; + FileWriteData(&s_dimm, dimm_offset, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + if ((offset >= AllNetBuffer) && (offset <= 0x89011000)) + { + const u32 allnet_offset = offset - AllNetBuffer; + memcpy(s_allnet_buffer + allnet_offset, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + if ((offset >= NetworkCommandAddress) && (offset < 0x1F801240)) + { + const u32 dimm_offset = offset - NetworkCommandAddress; + + memcpy(s_network_command_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), + length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write NETWORK COMMAND BUFFER ({:08x},{})", dimm_offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= NetworkCommandAddress2) && (offset <= 0x890601FF)) + { + const u32 dimm_offset = offset - NetworkCommandAddress2; + + memcpy(s_network_command_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), + length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write NETWORK COMMAND BUFFER (2) ({:08x},{})", dimm_offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= NetworkBufferAddress1) && (offset <= 0x1FA1FFFF)) + { + const u32 dimm_offset = offset - NetworkBufferAddress1; + + memcpy(s_network_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write NETWORK BUFFER (1) ({:08x},{})", dimm_offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= NetworkBufferAddress2) && (offset <= 0x1FD0FFFF)) + { + const u32 dimm_offset = offset - NetworkBufferAddress2; + + memcpy(s_network_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write NETWORK BUFFER (2) ({:08x},{})", dimm_offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= NetworkBufferAddress3) && (offset <= 0x8910FFFF)) + { + const u32 dimm_offset = offset - NetworkBufferAddress3; + + memcpy(s_network_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write NETWORK BUFFER (3) ({:08x},{})", dimm_offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= DIMMCommandVersion1) && (offset <= 0x1F90003F)) + { + const u32 dimm_offset = offset - DIMMCommandVersion1; + memcpy(s_media_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write MEDIA BOARD COMM AREA (1) ({:08x},{})", offset, + length); + PrintMBBuffer(address, length); + return 0; + } + + if ((offset >= DIMMCommandVersion2) && (offset <= 0x8400005F)) + { + const u32 dimm_offset = offset - DIMMCommandVersion2; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write MEDIA BOARD COMM AREA (2) ({:08x},{})", offset, + length); + PrintMBBuffer(address, length); + + const u8 cmd_flag = memory.Read_U8(address); + + if (dimm_offset == 0x40 && cmd_flag == 1) + { + const AMMBCommand ammb_command = + static_cast(*reinterpret_cast(s_media_buffer + 0x22)); + + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Execute command (2):{0:04X}", + static_cast(ammb_command)); + + memset(s_media_buffer, 0, 0x20); + + // Counter/Command + s_media_buffer_32[0] = s_media_buffer_32[8] | 0x80000000; // Set command okay flag + + // Handle command + switch (ammb_command) + { + case AMMBCommand::Unknown_000: + s_media_buffer_32[1] = 1; + break; + case AMMBCommand::GetDIMMSize: + s_media_buffer_32[1] = 0x20000000; + break; + case AMMBCommand::GetMediaBoardStatus: + s_media_buffer_32[1] = LoadedGameProgram; + s_media_buffer_32[2] = 100; + break; + // SegaBoot version: 3.09 + case AMMBCommand::GetSegaBootVersion: + // Version + s_media_buffer[4] = 0x03; + s_media_buffer[5] = 0x09; + // Unknown + s_media_buffer[6] = 1; + s_media_buffer_32[2] = 1; + s_media_buffer_32[4] = 0xFF; + break; + case AMMBCommand::GetSystemFlags: + s_media_buffer[4] = 1; + s_media_buffer[5] = GDROM; + // Enable development mode (Sega Boot) + // This also allows region free booting + s_media_buffer[6] = 1; + s_media_buffer[8] = 0; // Access Count + break; + case AMMBCommand::GetMediaBoardSerial: + memcpy(s_media_buffer + 4, "A89E-27A50364511", 16); + break; + case AMMBCommand::Unknown_104: + s_media_buffer[4] = 1; + break; + default: + PanicAlertFmtT("Unhandled Media Board Command:{0:04x}", static_cast(ammb_command)); + break; + } + + memset(s_media_buffer + 0x20, 0, 0x20); + + ExpansionInterface::GenerateInterrupt(0x04); + return 0; + } + else + { + memcpy(s_media_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + } + return 0; + } + + if ((offset >= DIMMCommandVersion2_2) && (offset <= 0x89000200)) + { + const u32 dimm_offset = offset - DIMMCommandVersion2_2; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write MEDIA BOARD COMM AREA (3) ({:08x})", dimm_offset); + PrintMBBuffer(address, length); + + memcpy(s_media_buffer + dimm_offset, memory.GetSpanForAddress(address).data(), length); + + return 0; + } + + // Firmware Write + if ((offset >= FirmwareAddress) && (offset <= 0x84818000)) + { + const u32 dimm_offset = offset - FirmwareAddress; + INFO_LOG_FMT(AMMEDIABOARD, "GC-AM: Write Firmware ({:08x})", dimm_offset); + PrintMBBuffer(address, length); + return 0; + } + + // DIMM memory (8MB) + if ((offset >= DIMMMemory2) && (offset <= 0xFF800000)) + { + const u32 dimm_offset = offset - DIMMMemory2; + FileWriteData(&s_dimm, dimm_offset, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + if ((offset == NetworkControl) && (length == 0x20)) + { + FileWriteData(&s_netctrl, 0, memory.GetSpanForAddress(address).data(), length); + return 0; + } + + // Max GC disc offset + if (offset >= 0x57058000) + { + PrintMBBuffer(address, length); + PanicAlertFmtT("Unhandled Media Board Write:{0:08x}", offset); + } + break; + case AMMBDICommand::Execute: + { + const AMMBCommand ammb_command = static_cast(s_media_buffer_32[8] >> 16); + if (ammb_command == AMMBCommand::Unknown_000) + break; + + if (offset == 0 && length == 0) + { + memset(s_media_buffer, 0, 0x20); + + // Counter/Command + s_media_buffer_32[0] = s_media_buffer_32[8] | 0x80000000; // Set command okay flag + + switch (ammb_command) + { + case AMMBCommand::Unknown_000: + s_media_buffer_32[1] = 1; + break; + case AMMBCommand::GetDIMMSize: + s_media_buffer_32[1] = 0x20000000; + break; + case AMMBCommand::GetMediaBoardStatus: + { + // Fake loading the game to have a chance to enter test mode + static u32 status = LoadingGameProgram; + static u32 progress = 80; + + s_media_buffer_32[1] = status; + s_media_buffer_32[2] = progress; + if (progress < 100) + { + progress++; + } + else + { + status = LoadedGameProgram; + } + } + break; + // SegaBoot version: 3.11 + case AMMBCommand::GetSegaBootVersion: + // Version + s_media_buffer[4] = 0x03; + s_media_buffer[5] = 0x11; + // Unknown + s_media_buffer[6] = 1; + s_media_buffer_32[2] = 1; + s_media_buffer_32[4] = 0xFF; + break; + case AMMBCommand::GetSystemFlags: + s_media_buffer[4] = 1; + s_media_buffer[5] = GDROM; + // Enable development mode (Sega Boot) + // This also allows region free booting + s_media_buffer[6] = 1; + s_media_buffer[8] = 0; // Access Count + break; + case AMMBCommand::GetMediaBoardSerial: + memcpy(s_media_buffer + 4, "A89E-27A50364511", 16); + break; + case AMMBCommand::Unknown_104: + s_media_buffer[4] = 1; + break; + case AMMBCommand::NetworkReInit: + break; + case AMMBCommand::TestHardware: + // Test type + + // 0x01: Media board + // 0x04: Network + + // DEBUG_LOG_FMT(AMMEDIABOARD, "GC-AM: 0x301: ({:08x})", s_media_buffer_32[9] ) + // ); + + // Pointer to a memory address that is directly displayed on screen as a string + // DEBUG_LOG_FMT(AMMEDIABOARD, "GC-AM: ({:08x})", s_media_buffer_32[10]) + // ); + + // On real system it shows the status about the DIMM/GD-ROM here + // We just show "TEST OK" + memory.Write_U32(0x54534554, s_media_buffer_32[12]); + memory.Write_U32(0x004B4F20, s_media_buffer_32[12] + 4); + + s_media_buffer_32[1] = s_media_buffer_32[9]; + break; + case AMMBCommand::Closesocket: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[10])]; + + const int ret = closesocket(fd); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: closesocket( {}({}) ):{}\n", fd, + s_media_buffer_32[10], ret); + + s_sockets[SocketCheck(s_media_buffer_32[10])] = SOCKET_ERROR; + + s_media_buffer_32[1] = ret; + s_last_error = SSC_SUCCESS; + } + break; + case AMMBCommand::Connect: + { + sockaddr_in addr; + + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[10])]; + const u32 off = s_media_buffer_32[11] - NetworkCommandAddress; + const u32 len = s_media_buffer_32[12]; + + memcpy((void*)&addr, s_network_command_buffer + off, sizeof(sockaddr_in)); + + const int ret = NetDIMMConnect(fd, &addr, len); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: connect( {}({}), ({},{}:{}), {} ):{}\n", fd, + s_media_buffer_32[10], addr.sin_family, inet_ntoa(addr.sin_addr), + Common::swap16(addr.sin_port), len, ret); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + } + break; + case AMMBCommand::Recv: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[10])]; + const u32 off = s_media_buffer_32[11]; + u32 len = s_media_buffer_32[12]; + + if (len >= sizeof(s_network_buffer)) + { + len = sizeof(s_network_buffer); + } + + char* buffer = (char*)(s_network_buffer + off); + + if (off >= NetworkBufferAddress5 && off < NetworkBufferAddress5 + sizeof(s_network_buffer)) + { + buffer = (char*)(s_network_buffer + off - NetworkBufferAddress5); + } + else + { + PanicAlertFmt("RECV: Buffer overrun:{0} {1} ", off, len); + } + + const int ret = recv(fd, buffer, len, 0); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: recv( {}, 0x{:08x}, {} ):{} {}\n", fd, off, len, + ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + } + break; + case AMMBCommand::Send: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[10])]; + u32 off = s_media_buffer_32[11]; + const u32 len = s_media_buffer_32[12]; + + if (off >= NetworkBufferAddress1 && off < NetworkBufferAddress1 + sizeof(s_network_buffer)) + { + off -= NetworkBufferAddress1; + } + else + { + ERROR_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: send(error) unhandled destination:{:08x}\n", off); + } + + const int ret = send(fd, (char*)(s_network_buffer + off), len, 0); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: send( {}({}), 0x{:08x}, {} ): {} {}\n", fd, + s_media_buffer_32[10], off, len, ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + } + break; + case AMMBCommand::Socket: + { + // Protocol is not sent + const u32 af = s_media_buffer_32[10]; + const u32 type = s_media_buffer_32[11]; + + const SOCKET fd = socket_(af, type, IPPROTO_TCP); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: socket( {}, {}, 6 ):{}\n", af, type, fd); + + s_media_buffer[1] = 0; + s_media_buffer_32[1] = fd; + } + break; + case AMMBCommand::Select: + { + const u32 fd = s_sockets[SocketCheck(s_media_buffer_32[10] - 1)]; + + fd_set* readfds = nullptr; + fd_set* writefds = nullptr; + fd_set* exceptfds = nullptr; + timeval timeout = {}; + timeval* timeout_src = nullptr; + + // Only one of 11, 12, 13 is ever set alongside 14 + if (s_media_buffer_32[11] && s_media_buffer_32[14]) + { + const u32 ROffset = s_media_buffer_32[14] - NetworkCommandAddress; + readfds = reinterpret_cast(s_network_command_buffer + ROffset); + FD_ZERO(readfds); + FD_SET(fd, readfds); + + timeout_src = reinterpret_cast(s_network_command_buffer + + s_media_buffer_32[11] - NetworkCommandAddress); + } + else if (s_media_buffer_32[12] && s_media_buffer_32[14]) + { + const u32 WOffset = s_media_buffer_32[14] - NetworkCommandAddress; + writefds = reinterpret_cast(s_network_command_buffer + WOffset); + FD_ZERO(writefds); + FD_SET(fd, writefds); + + timeout_src = reinterpret_cast(s_network_command_buffer + + s_media_buffer_32[12] - NetworkCommandAddress); + } + else if (s_media_buffer_32[13] && s_media_buffer_32[14]) + { + const u32 EOffset = s_media_buffer_32[14] - NetworkCommandAddress; + exceptfds = reinterpret_cast(s_network_command_buffer + EOffset); + FD_ZERO(exceptfds); + FD_SET(fd, exceptfds); + + timeout_src = reinterpret_cast(s_network_command_buffer + + s_media_buffer_32[13] - NetworkCommandAddress); + } + + // Copy timeout if set + if (timeout_src) + { + std::memcpy(&timeout, timeout_src, sizeof(timeval)); + } + + // BUG?: F-Zero AX Monster calls select with a two second timeout + // for unknown reasons, which slows down the game a lot + if (AMMediaboard::GetGameType() == FZeroAXMonster) + { + timeout.tv_sec = 0; + timeout.tv_usec = 1800; + } + + const int ret = + select(fd + 1, readfds, writefds, exceptfds, timeout_src ? &timeout : nullptr); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, + "GC-AM: select( {}({}), 0x{:08x} 0x{:08x} 0x{:08x} 0x{:08x} ):{} {} \n", fd, + s_media_buffer_32[10], s_media_buffer_32[11], s_media_buffer_32[14], + s_media_buffer_32[15], s_media_buffer_32[16], ret, err); + // hexdump( NetworkCMDBuffer, 0x40 ); + + s_media_buffer[1] = 0; + s_media_buffer_32[1] = ret; + } + break; + case AMMBCommand::SetSockOpt: + { + const SOCKET fd = (SOCKET)(s_sockets[SocketCheck(s_media_buffer_32[10])]); + const int level = (int)(s_media_buffer_32[11]); + const int optname = (int)(s_media_buffer_32[12]); + const char* optval = + (char*)(s_network_command_buffer + s_media_buffer_32[13] - NetworkCommandAddress); + const int optlen = (int)(s_media_buffer_32[14]); + + const int ret = setsockopt(fd, level, optname, optval, optlen); + const int err = WSAGetLastError(); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, + "GC-AM: setsockopt( {:d}, {:04x}, {}, {:p}, {} ):{:d} ({})\n", fd, level, + optname, optval, optlen, ret, err); + + s_media_buffer[1] = s_media_buffer[8]; + s_media_buffer_32[1] = ret; + } + break; + case AMMBCommand::ModifyMyIPaddr: + { + const u32 NetBufferOffset = s_media_buffer_32[10] - NetworkCommandAddress; + + const char* IP = (char*)(s_network_command_buffer + NetBufferOffset); + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: modifyMyIPaddr({})\n", IP); + } + break; + // Empty reply + case AMMBCommand::InitLink: + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: 0x601"); + break; + case AMMBCommand::Unknown_605: + NOTICE_LOG_FMT(AMMEDIABOARD, "GC-AM: 0x605"); + break; + case AMMBCommand::SetupLink: + { + sockaddr_in addra; + sockaddr_in addrb; + + addra.sin_addr.s_addr = s_media_buffer_32[12]; + addrb.sin_addr.s_addr = s_media_buffer_32[13]; + + u16 size = s_media_buffer[0x24] | s_media_buffer[0x25] << 8; + u16 port = Common::swap16(s_media_buffer[0x27] | s_media_buffer[0x26] << 8); + u16 unknown = s_media_buffer[0x2D] | s_media_buffer[0x2C] << 8; + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: SetupLink:"); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: Size: ({}) ", size); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: Port: ({})", port); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM:LinkNum:({:02x})", s_media_buffer[0x28]); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: ({:02x})", s_media_buffer[0x2A]); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: ({:04x})", unknown); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: IP: ({})", inet_ntoa(addra.sin_addr)); // IP ? + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: IP: ({})", + inet_ntoa(addrb.sin_addr)); // Target IP + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: ({:08x})", + Common::swap32(s_media_buffer_32[14])); // some RAM address + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: ({:08x})", + Common::swap32(s_media_buffer_32[15])); // some RAM address + + s_media_buffer_32[1] = 0; + } + break; + // This sends a UDP packet to previously defined Target IP/Port + case AMMBCommand::SearchDevices: + { + u16 unknown = s_media_buffer[0x25] | s_media_buffer[0x24] << 8; + u16 off = s_media_buffer[0x26] | s_media_buffer[0x27] << 8; + u32 addr = s_media_buffer_32[10]; + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: SearchDevices: ({})", unknown); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: Offset: ({:04x})", off); + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: ({:08x})", addr); + + const u8* data = (u8*)(s_network_buffer + off + addr - NetworkBufferAddress2); + + for (u32 i = 0; i < 0x20; i += 0x10) + { + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: {:08x} {:08x} {:08x} {:08x}", *(u32*)(data + i), + *(u32*)(data + i + 4), *(u32*)(data + i + 8), *(u32*)(data + i + 12)); + } + + s_media_buffer_32[1] = 0; + } + break; + case AMMBCommand::Unknown_608: + { + const u32 ip = s_media_buffer_32[10]; + u16 port = Common::swap16(s_media_buffer[6] | s_media_buffer[7] << 8); + u16 flag = s_media_buffer[10] | s_media_buffer[11] << 8; + + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: 0x608( {} {} {} )", ip, port, flag); + } + break; + case AMMBCommand::Unknown_614: + NOTICE_LOG_FMT(AMMEDIABOARD_NET, "GC-AM: 0x614"); + break; + default: + ERROR_LOG_FMT(AMMEDIABOARD, "GC-AM: execute buffer UNKNOWN:{0:04x}", + static_cast(ammb_command)); + break; + } + + memset(s_media_buffer + 0x20, 0, 0x20); + return 0; + } + + PanicAlertFmtT("Unhandled Media Board Execute:{0:04x}", static_cast(ammb_command)); + break; + } + default: + PanicAlertFmtT("Unhandled Media Board Command:{0:02x}", command); + break; + } + + return 0; +} + +u32 GetMediaType() +{ + switch (GetGameType()) + { + default: + case FZeroAX: + case VirtuaStriker3: + case VirtuaStriker4: + case VirtuaStriker4_2006: + case GekitouProYakyuu: + case KeyOfAvalon: + return GDROM; + break; + + case MarioKartGP: + case MarioKartGP2: + case FZeroAXMonster: + return NAND; + break; + } + // Never reached +} + +u32 GetGameType() +{ + u64 game_id = 0; + + if (SConfig::GetInstance().GetGameID().length() > 6) + { + game_id = 0x32323232; + } + else + { + // Convert game ID into hex + sscanf(SConfig::GetInstance().GetGameID().c_str(), "%s", (char*)&game_id); + } + + // This is checking for the real game IDs (See boot.id within the game) + switch ((Common::swap32((u32)game_id) >> 8) & 0xFFFF) + { + // SBGG - F-ZERO AX + case 0x4747: + return FZeroAX; + // SBHA - F-ZERO AX (Monster) + case 0x4841: + return FZeroAXMonster; + // SBKJ/SBKP - MARIOKART ARCADE GP + case 0x4B50: + case 0x4B5A: + return MarioKartGP; + // SBNJ/SBNL - MARIOKART ARCADE GP2 + case 0x4E4A: + case 0x4E4C: + return MarioKartGP2; + // SBEJ/SBEY - Virtua Striker 2002 + case 0x454A: + case 0x4559: + return VirtuaStriker3; + // SBLJ/SBLK/SBLL - VIRTUA STRIKER 4 Ver.2006 + case 0x4C4A: + case 0x4C4B: + case 0x4C4C: + return VirtuaStriker4_2006; + // SBHJ/SBHN/SBHZ - VIRTUA STRIKER 4 VER.A + case 0x484A: + case 0x484E: + case 0x485A: + // SBJA/SBJJ - VIRTUA STRIKER 4 + case 0x4A41: + case 0x4A4A: + return VirtuaStriker4; + // SBFX/SBJN - Key of Avalon + case 0x4658: + case 0x4A4E: + return KeyOfAvalon; + // SBGX - Gekitou Pro Yakyuu (DIMM Upgrade 3.17) + case 0x4758: + return GekitouProYakyuu; + default: + PanicAlertFmtT("Unknown game ID:{0:08x}, using default controls.", game_id); + // GSBJ/G12U - VIRTUA STRIKER 3 + // RELS/RELJ - SegaBoot (does not have a boot.id) + case 0x5342: + case 0x3132: + case 0x454C: + return VirtuaStriker3; + // S000 - Firmware update + case 0x3030: + return FirmwareUpdate; + } + // Never reached +} + +bool GetTestMenu() +{ + return s_test_menu; +} + +void Shutdown() +{ + s_netcfg.Close(); + s_netctrl.Close(); + s_extra.Close(); + s_backup.Close(); + s_dimm.Close(); + + if (s_dimm_disc.size()) + { + s_dimm_disc.clear(); + } + + // Close all sockets + for (u32 i = 1; i < 64; ++i) + { + if (s_sockets[i] != SOCKET_ERROR) + { + closesocket(s_sockets[i]); + } + } +} + +} // namespace AMMediaboard diff --git a/Source/Core/Core/HW/DVD/AMMediaboard.h b/Source/Core/Core/HW/DVD/AMMediaboard.h new file mode 100644 index 0000000000..3cfa79fef6 --- /dev/null +++ b/Source/Core/Core/HW/DVD/AMMediaboard.h @@ -0,0 +1,237 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include + +#include "Common/CommonTypes.h" + +class PointerWrap; + +namespace Core +{ +class System; +} + +namespace File +{ +class IOFile; +} + +enum GameType +{ + FZeroAX = 1, + FZeroAXMonster, + MarioKartGP, + MarioKartGP2, + VirtuaStriker3, + VirtuaStriker4, + VirtuaStriker4_2006, + GekitouProYakyuu, + KeyOfAvalon, + FirmwareUpdate, +}; + +enum MediaType +{ + GDROM = 1, + NAND, +}; + +enum MediaBoardType +{ + NANDMaskBoardHDD = 0, + NANDMaskBoardMask = 1, + NANDMaskBoardNAND = 2, + DIMMBoardType3 = 4, +}; + +enum MediaBoardStatus +{ + Initializing = 0, + CheckingNetwork = 1, + SystemDisc = 2, + TestingGameProgram = 3, + LoadingGameProgram = 4, + LoadedGameProgram = 5, + Error = 6, +}; + +enum InquiryType +{ + Version1 = 0x21484100, + Version2 = 0x29484100, +}; + +#define SocketCheck(x) (x <= 0x3F ? x : 0) + +namespace AMMediaboard +{ + +enum class AMMBDICommand : u16 +{ + Inquiry = 0x12, + Read = 0xA8, + Write = 0xAA, + Execute = 0xAB, +}; + +enum class AMMBCommand : u16 +{ + Unknown_000 = 0x000, + GetDIMMSize = 0x001, + + GetMediaBoardStatus = 0x100, + GetSegaBootVersion = 0x101, + GetSystemFlags = 0x102, + GetMediaBoardSerial = 0x103, + Unknown_104 = 0x104, + + NetworkReInit = 0x204, + + TestHardware = 0x301, + + // Network used by Mario Kart GPs + Accept = 0x401, + Bind = 0x402, + Closesocket = 0x403, + Connect = 0x404, + GetIPbyDNS = 0x405, + InetAddr = 0x406, + Ioctl = 0x407, // Unused + Listen = 0x408, + Recv = 0x409, + Send = 0x40A, + Socket = 0x40B, + Select = 0x40C, + Shutdown = 0x40D, // Unused + SetSockOpt = 0x40E, + GetSockOpt = 0x40F, // Unused + SetTimeOuts = 0x410, + GetLastError = 0x411, + RouteAdd = 0x412, // Unused + RouteDelete = 0x413, // Unused + GetParambyDHCPExec = 0x414, + ModifyMyIPaddr = 0x415, + Recvfrom = 0x416, // Unused + Sendto = 0x417, // Unused + RecvDimmImage = 0x418, // Unused + SendDimmImage = 0x419, // Unused + + // Network used by F-Zero AX + InitLink = 0x601, + Unknown_605 = 0x605, + SetupLink = 0x606, + SearchDevices = 0x607, + Unknown_608 = 0x608, + Unknown_614 = 0x614, + + // All.Net used by Virtua Striker 4 2006 (Japan) + AllNetInit = 0x700, + Unknown_701 = 0x701, + Unknown_702 = 0x702, + Unknown_703 = 0x703, + Unknown_704 = 0x704, + + // NETDIMM Commands + Unknown_001 = 0x001, + GetNetworkFirmVersion = 0x101, + Unknown_103 = 0x103, +}; + +enum MediaBoardAddress : u32 +{ + MediaBoardStatus1 = 0x80000000, + MediaBoardStatus2 = 0x80000020, + MediaBoardStatus3 = 0x80000040, + + FirmwareStatus1 = 0x80000120, + FirmwareStatus2 = 0x80000140, + + BackupMemory = 0x000006A0, + + DIMMMemory = 0x1F000000, + DIMMMemory2 = 0xFF000000, + + DIMMExtraSettings = 0x1FFEFFE0, + + AllNetSettings = 0x1EFF8000, + + NetworkControl = 0xFFFF0000, + + DIMMCommandVersion1 = 0x1F900000, + DIMMCommandVersion2 = 0x84000000, + DIMMCommandVersion2_2 = 0x89000000, + DIMMCommandExecute2 = 0x88000000, + + NetworkCommandAddress = 0x1F800200, + NetworkCommandAddress2 = 0x89040200, + + NetworkBufferAddress1 = 0x1FA00000, + NetworkBufferAddress2 = 0x1FD00000, + NetworkBufferAddress3 = 0x89100000, + NetworkBufferAddress4 = 0x89180000, + NetworkBufferAddress5 = 0x1FB00000, + + AllNetBuffer = 0x89010000, + + FirmwareAddress = 0x84800000, + + FirmwareMagicWrite1 = 0x00600000, + FirmwareMagicWrite2 = 0x00700000, +}; + +// Mario Kart GP2 has a complete list of them +// but in japanese +// They somewhat match WSA errors codes +enum SocketStatusCodes +{ + SSC_E_4 = -4, // Failure (abnormal argument) + SSC_E_3 = -3, // Success (unsupported command) + SSC_E_2 = + -2, // Failure (failed to send, abnormal argument, or communication condition violation) + SSC_E_1 = -1, // Failure (error termination) + + SSC_EINTR = 4, // An interrupt occurred before data reception was completed + SSC_EBADF = 9, // Invalid descriptor + SSC_E_11 = 11, // Send operation was blocked on a non-blocking mode socket + SSC_EACCES = 13, // The socket does not support broadcast addresses, but the destination address + // is a broadcast address + SSC_EFAULT = + 14, // The name argument specifies a location other than an address used by the process. + SSC_E_23 = 23, // System file table is full. + SSC_AEMFILE = 24, // Process descriptor table is full. + SSC_EMSGSIZE = + 36, // Socket tried to send message without splitting, but message size is too large. + SSC_EAFNOSUPPORT = 47, // Address prohibited for use on this socket. + SSC_EADDRINUSE = 48, // Address already in use. + SSC_EADDRNOTAVAIL = 49, // Prohibited address. + SSC_E_50 = 50, // Non-socket descriptor. + SSC_ENETUNREACH = 51, // Cannot access specified network. + SSC_ENOBUFS = 55, // Insufficient buffer + SSC_EISCONN = 56, // Already connected socket + SSC_ENOTCONN = 57, // No connection for connection-type socket + SSC_ETIMEDOUT = 60, // Timeout + SSC_ECONNREFUSED = 61, // Connection request forcibly rejected + SSC_EHOSTUNREACH = 65, // Remote host cannot be reached + SSC_EHOSTDOWN = 67, // Remote host is down + SSC_EWOULDBLOCK = 70, // Socket is in non-blocking mode and connection has not been completed + SSC_E_69 = 69, // Socket is in non-blocking mode and a previously issued Connect command has not + // been completed + SSC_SUCCESS = 70, +}; + +void Init(); +void FirmwareMap(bool on); +u8* InitDIMM(u32 size); +void InitKeys(u32 KeyA, u32 KeyB, u32 KeyC); +u32 ExecuteCommand(std::array& DICMDBUF, u32* DIIMMBUF, u32 Address, u32 Length); +u32 GetGameType(); +u32 GetMediaType(); +bool GetTestMenu(); +void Shutdown(); +}; // namespace AMMediaboard diff --git a/Source/Core/Core/HW/DVD/DVDInterface.cpp b/Source/Core/Core/HW/DVD/DVDInterface.cpp index efb279cfac..e88a61084b 100644 --- a/Source/Core/Core/HW/DVD/DVDInterface.cpp +++ b/Source/Core/Core/HW/DVD/DVDInterface.cpp @@ -26,6 +26,7 @@ #include "Core/CoreTiming.h" #include "Core/DolphinAnalytics.h" #include "Core/HW/AudioInterface.h" +#include "Core/HW/DVD/AMMediaboard.h" #include "Core/HW/DVD/DVDMath.h" #include "Core/HW/DVD/DVDThread.h" #include "Core/HW/EXI/EXI_DeviceIPL.h" @@ -281,6 +282,16 @@ void DVDInterface::Init() u64 userdata = PackFinishExecutingCommandUserdata(ReplyType::DTK, DIInterruptType::TCINT); core_timing.ScheduleEvent(0, m_finish_executing_command, userdata); + + if (m_system.IsTriforce()) + { + AMMediaboard::Init(); + + // The Triforce IPL expects the cover to be closed + m_DICVR.Hex = 0; + m_DICFG.Hex |= 8; /* The Triforce IPL checks this bit + to set the physical memory to either 50MB(unset) or 24MB(set) */ + } } // Resets state on the MN102 chip in the drive itself, but not the DI registers exposed on the @@ -310,7 +321,7 @@ void DVDInterface::ResetDrive(bool spinup) else if (!spinup) { // Wii hardware tests indicate that this is used when ejecting and inserting a new disc, or - // performing a reset without spinup. + // performing a reset without spin up. SetDriveState(DriveState::DiscChangeDetected); } else @@ -697,11 +708,16 @@ bool DVDInterface::CheckReadPreconditions() SetDriveError(DriveError::MotorStopped); return false; } - if (m_drive_state == DriveState::DiscIdNotRead) + + // SegaBoot doesn't read the Disc ID + if (!m_system.IsTriforce()) { - ERROR_LOG_FMT(DVDINTERFACE, "Disc id not read."); - SetDriveError(DriveError::NoDiscID); - return false; + if (m_drive_state == DriveState::DiscIdNotRead) + { + ERROR_LOG_FMT(DVDINTERFACE, "Disc id not read."); + SetDriveError(DriveError::NoDiscID); + return false; + } } return true; } @@ -753,12 +769,19 @@ void DVDInterface::ExecuteCommand(ReplyType reply_type) DIInterruptType interrupt_type = DIInterruptType::TCINT; bool command_handled_by_thread = false; - // Swaps endian of Triforce DI commands, and zeroes out random bytes to prevent unknown read - // subcommand errors - auto& dvd_thread = m_system.GetDVDThread(); - if (dvd_thread.HasDisc() && dvd_thread.GetDiscType() == DiscIO::Platform::Triforce) + if (m_system.IsTriforce()) { - // TODO(C++23): Use std::byteswap and a bitwise AND for increased clarity + if (!AMMediaboard::ExecuteCommand(m_DICMDBUF, &m_DIIMMBUF, m_DIMAR, m_DILENGTH)) + { + // Transfer is done + m_DICR.TSTART = 0; + m_DIMAR += m_DILENGTH; + m_DILENGTH = 0; + GenerateDIInterrupt(DIInterruptType::TCINT); + m_error_code = DriveError::None; + return; + } + // Normal read command pass on to normal handling m_DICMDBUF[0] <<= 24; } diff --git a/Source/Core/Core/HW/EXI/EXI.cpp b/Source/Core/Core/HW/EXI/EXI.cpp index d676dc4845..144fdd57b9 100644 --- a/Source/Core/Core/HW/EXI/EXI.cpp +++ b/Source/Core/Core/HW/EXI/EXI.cpp @@ -66,18 +66,6 @@ void ExpansionInterfaceManager::AddMemoryCard(Slot slot) m_channels[SlotToEXIChannel(slot)]->AddDevice(memorycard_device, SlotToEXIDevice(slot)); } -void ExpansionInterfaceManager::AddSP1Device() -{ - EXIDeviceType sp1_device = EXIDeviceType::Baseboard; - auto& system = Core::System::GetInstance(); - if (system.IsTriforce()) - { - sp1_device = Config::Get(Config::MAIN_SERIAL_PORT_1); - } - - m_channels[0]->AddDevice(sp1_device, SlotToEXIDevice(Slot::SP1)); -} - u8 SlotToEXIChannel(Slot slot) { switch (slot) @@ -157,9 +145,15 @@ void ExpansionInterfaceManager::Init(const Sram* override_sram) AddMemoryCard(slot); m_channels[0]->AddDevice(EXIDeviceType::MaskROM, 1); - AddSP1Device(); - m_channels[SlotToEXIChannel(Slot::SP1)]->AddDevice(Config::Get(Config::MAIN_SERIAL_PORT_1), - SlotToEXIDevice(Slot::SP1)); + + // Automatically connect the Triforce Baseboard in Triforce mode + EXIDeviceType sp1_device = Config::Get(Config::MAIN_SERIAL_PORT_1); + if (m_system.IsTriforce()) + { + sp1_device = EXIDeviceType::Baseboard; + } + + m_channels[SlotToEXIChannel(Slot::SP1)]->AddDevice(sp1_device, SlotToEXIDevice(Slot::SP1)); m_channels[SlotToEXIChannel(Slot::SP2)]->AddDevice(Config::Get(Config::MAIN_SERIAL_PORT_2), SlotToEXIDevice(Slot::SP2)); diff --git a/Source/Core/Core/HW/EXI/EXI.h b/Source/Core/Core/HW/EXI/EXI.h index f0ea5fbfb5..7ded905b98 100644 --- a/Source/Core/Core/HW/EXI/EXI.h +++ b/Source/Core/Core/HW/EXI/EXI.h @@ -89,7 +89,6 @@ public: private: void AddMemoryCard(Slot slot); - void AddSP1Device(); static void ChangeDeviceCallback(Core::System& system, u64 userdata, s64 cycles_late); static void UpdateInterruptsCallback(Core::System& system, u64 userdata, s64 cycles_late); diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.cpp index 7df13f5020..a489d97a72 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.cpp @@ -1,20 +1,137 @@ -// Copyright 2013 Dolphin Emulator Project +// Copyright 2025 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include "Core/HW/EXI/EXI_DeviceBaseboard.h" -#include "Common/ChunkFile.h" +#include +#include +#include +#include +#include + +#include + +#include "Common/Buffer.h" #include "Common/CommonTypes.h" +#include "Common/Config/Config.h" +#include "Common/FileUtil.h" +#include "Common/IOFile.h" +#include "Common/IniFile.h" #include "Common/Logging/Log.h" +#include "Core/Boot/Boot.h" +#include "Core/BootManager.h" +#include "Core/Config/MainSettings.h" +#include "Core/Config/SYSCONFSettings.h" +#include "Core/ConfigLoaders/BaseConfigLoader.h" +#include "Core/ConfigLoaders/NetPlayConfigLoader.h" +#include "Core/ConfigManager.h" +#include "Core/Core.h" +#include "Core/HW/DVD/AMMediaboard.h" +#include "Core/HW/DVD/DVDInterface.h" +#include "Core/HW/EXI/EXI.h" +#include "Core/HW/EXI/EXI_Device.h" +#include "Core/HW/MMIO.h" +#include "Core/HW/Memmap.h" +#include "Core/HW/SI/SI.h" +#include "Core/HW/SI/SI_Device.h" +#include "Core/HW/Sram.h" +#include "Core/HW/WiimoteReal/WiimoteReal.h" +#include "Core/Movie.h" +#include "Core/NetPlayProto.h" +#include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" +#include "Core/WiiRoot.h" + +#include "DiscIO/Enums.h" + +static bool s_interrupt_set = false; +static u32 s_irq_timer = 0; +static u32 s_irq_status = 0; + +static u16 CheckSum(u8* data, u32 length) +{ + u16 check = 0; + + for (u32 i = 0; i < length; i++) + { + check += data[i]; + } + + return check; +} + namespace ExpansionInterface { -CEXIBaseboard::CEXIBaseboard(Core::System& system) : IEXIDevice(system) + +void GenerateInterrupt(int flag) { + auto& system = Core::System::GetInstance(); + + s_interrupt_set = true; + s_irq_timer = 0; + s_irq_status = flag; + + system.GetExpansionInterface().UpdateInterrupts(); +} + +CEXIBaseboard::CEXIBaseboard(Core::System& system) : IEXIDevice(system), m_position(0) +{ + std::string backup_filename(fmt::format("{}tribackup_{}.bin", File::GetUserPath(D_TRIUSER_IDX), + SConfig::GetInstance().GetGameID())); + + m_backup = File::IOFile(backup_filename, "rb+"); + if (!m_backup.IsOpen()) + { + m_backup = File::IOFile(backup_filename, "wb+"); + } + + // Some games share the same ID Client/Server + if (!m_backup.IsGood()) + { + PanicAlertFmt("Failed to open {}\nFile might be in use.", backup_filename.c_str()); + + std::srand(static_cast(std::time(nullptr))); + + backup_filename = fmt::format("{}tribackup_tmp_{}{}.bin", File::GetUserPath(D_TRIUSER_IDX), + rand(), SConfig::GetInstance().GetGameID()); + + m_backup = File::IOFile(backup_filename, "wb+"); + } + + // Virtua Striker 4 and Gekitou Pro Yakyuu need a higher FIRM version + // Which is read from the backup data?! + if (AMMediaboard::GetGameType() == VirtuaStriker4 || + AMMediaboard::GetGameType() == GekitouProYakyuu) + { + if (m_backup.GetSize()) + { + Common::UniqueBuffer data(m_backup.GetSize()); + m_backup.ReadBytes(data.data(), data.size()); + + // Set FIRM version + reinterpret_cast(data[0x12]) = 0x1703; + reinterpret_cast(data[0x212]) = 0x1703; + + // Update checksum + reinterpret_cast(data[0x0A]) = Common::swap16(CheckSum(&data[0xC], 0x1F4)); + reinterpret_cast(data[0x20A]) = Common::swap16(CheckSum(&data[0x20C], 0x1F4)); + + m_backup.Seek(0, File::SeekOrigin::Begin); + m_backup.WriteBytes(data.data(), data.size()); + m_backup.Flush(); + } + } +} + +CEXIBaseboard::~CEXIBaseboard() +{ + m_backup.Close(); } void CEXIBaseboard::SetCS(int cs) { + DEBUG_LOG_FMT(SP1, "AM-BB: ChipSelect={}", cs); if (cs) m_position = 0; } @@ -24,33 +141,197 @@ bool CEXIBaseboard::IsPresent() const return true; } -void CEXIBaseboard::TransferByte(u8& byte) +bool CEXIBaseboard::IsInterruptSet() { - if (m_position == 0) + if (s_interrupt_set) { - m_command = byte; + DEBUG_LOG_FMT(SP1, "AM-BB: IRQ"); + if (++s_irq_timer > 12) + s_interrupt_set = false; + return 1; } else { - switch (m_command) + return 0; + } +} + +void CEXIBaseboard::DMAWrite(u32 addr, u32 size) +{ + const auto& system = Core::System::GetInstance(); + const auto& memory = system.GetMemory(); + + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: Backup DMA Write: {:08x} {:x}", addr, size); + + m_backup.Seek(m_backup_offset, File::SeekOrigin::Begin); + + m_backup.WriteBytes(memory.GetSpanForAddress(addr).data(), size); + + m_backup.Flush(); +} + +void CEXIBaseboard::DMARead(u32 addr, u32 size) +{ + const auto& system = Core::System::GetInstance(); + const auto& memory = system.GetMemory(); + + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: Backup DMA Read: {:08x} {:x}", addr, size); + + m_backup.Seek(m_backup_offset, File::SeekOrigin::Begin); + + m_backup.Flush(); + + m_backup.ReadBytes(memory.GetSpanForAddress(addr).data(), size); +} + +void CEXIBaseboard::TransferByte(u8& byte) +{ + DEBUG_LOG_FMT(SP1, "AM-BB: > {:02x}", byte); + if (m_position < 4) + { + m_command[m_position] = byte; + byte = 0xFF; + } + + if ((m_position >= 2) && (m_command[0] == 0 && m_command[1] == 0)) + { + // Read serial ID + byte = "\x06\x04\x10\x00"[(m_position - 2) & 3]; + } + else if (m_position == 3) + { + u32 checksum = (m_command[0] << 24) | (m_command[1] << 16) | (m_command[2] << 8); + u32 bit = 0x80000000UL; + u32 check = 0x8D800000UL; + while (bit >= 0x100) { - case 0x00: + if (checksum & bit) + checksum ^= check; + check >>= 1; + bit >>= 1; + } + + if (m_command[3] != (checksum & 0xFF)) + DEBUG_LOG_FMT(SP1, "AM-BB: cs: {:02x}, w: {:02x}", m_command[3], checksum & 0xFF); + } + else if (m_position == 4) + { + switch (m_command[0]) { - static constexpr std::array ID = {0x06, 0x04, 0x10, 0x00}; - byte = ID[(m_position - 2) & 3]; + case BackupOffsetSet: + m_backup_offset = (m_command[1] << 8) | m_command[2]; + DEBUG_LOG_FMT(SP1, "AM-BB: COMMAND: BackupOffsetSet:{:04x}", m_backup_offset); + m_backup.Seek(m_backup_offset, File::SeekOrigin::Begin); + byte = 0x01; + break; + case BackupWrite: + DEBUG_LOG_FMT(SP1, "AM-BB: COMMAND: BackupWrite:{:04x}-{:02x}", m_backup_offset, + m_command[1]); + m_backup.WriteBytes(&m_command[1], 1); + m_backup.Flush(); + byte = 0x01; + break; + case BackupRead: + DEBUG_LOG_FMT(SP1, "AM-BB: COMMAND: BackupRead :{:04x}", m_backup_offset); + byte = 0x01; + break; + case DMAOffsetLengthSet: + m_backup_dma_offset = (m_command[1] << 8) | m_command[2]; + m_backup_dma_length = m_command[3]; + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: DMAOffsetLengthSet :{:04x} {:02x}", m_backup_dma_offset, + m_backup_dma_length); + byte = 0x01; + break; + case ReadISR: + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: ReadISR :{:02x} {:02x}:{:02x} {:02x}", m_command[1], + m_command[2], 4, s_irq_status); + byte = 0x04; + break; + case WriteISR: + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: WriteISR :{:02x} {:02x}", m_command[1], m_command[2]); + s_irq_status &= ~(m_command[2]); + byte = 0x04; + break; + // 2 byte out + case ReadIMR: + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: ReadIMR :{:02x} {:02x}", m_command[1], m_command[2]); + byte = 0x04; + break; + case WriteIMR: + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: WriteIMR :{:02x} {:02x}", m_command[1], m_command[2]); + byte = 0x04; + break; + case WriteLANCNT: + NOTICE_LOG_FMT(SP1, "AM-BB: COMMAND: WriteLANCNT :{:02x} {:02x}", m_command[1], m_command[2]); + if ((m_command[1] == 0) && (m_command[2] == 0)) + { + s_interrupt_set = true; + s_irq_timer = 0; + s_irq_status = 0x02; + } + if ((m_command[1] == 2) && (m_command[2] == 1)) + { + s_irq_status = 0; + } + byte = 0x08; + break; + default: + byte = 4; + ERROR_LOG_FMT(SP1, "AM-BB: COMMAND: {:02x} {:02x} {:02x}", m_command[0], m_command[1], + m_command[2]); break; } + } + else if (m_position > 4) + { + switch (m_command[0]) + { + // 1 byte out + case BackupRead: + m_backup.Flush(); + m_backup.ReadBytes(&byte, 1); + break; + case DMAOffsetLengthSet: + byte = 0x01; + break; + // 2 byte out + case ReadISR: + if (m_position == 6) + { + byte = s_irq_status; + s_interrupt_set = false; + } + else + { + byte = 0x04; + } + break; + // 2 byte out + case ReadIMR: + if (m_position == 5) + byte = 0xFF; + if (m_position == 6) + byte = 0x81; + break; default: - ERROR_LOG_FMT(EXPANSIONINTERFACE, "EXI BASEBOARD: Unhandled command {:#x} {:#x}", m_command, - m_position); + ERROR_LOG_FMT(SP1, "Unknown AM-BB command: {:02x}", m_command[0]); + break; } } + else + { + byte = 0xFF; + } + + DEBUG_LOG_FMT(SP1, "AM-BB < {:02x}", byte); m_position++; } void CEXIBaseboard::DoState(PointerWrap& p) { p.Do(m_position); + p.Do(s_interrupt_set); p.Do(m_command); } + } // namespace ExpansionInterface diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.h b/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.h index 4c534428c9..6b32da191c 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.h +++ b/Source/Core/Core/HW/EXI/EXI_DeviceBaseboard.h @@ -1,27 +1,59 @@ -// Copyright 2013 Dolphin Emulator Project +// Copyright 2025 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #pragma once +#include "Common/CommonTypes.h" +#include "Common/IOFile.h" #include "Core/HW/EXI/EXI_Device.h" -class PointerWrap; +namespace Core +{ +class System; +} namespace ExpansionInterface { +void GenerateInterrupt(int flag); + class CEXIBaseboard : public IEXIDevice { public: - CEXIBaseboard(Core::System& system); + explicit CEXIBaseboard(Core::System& system); + virtual ~CEXIBaseboard(); + void SetCS(int cs) override; + bool IsInterruptSet() override; bool IsPresent() const override; void DoState(PointerWrap& p) override; + void DMAWrite(u32 addr, u32 size) override; + void DMARead(u32 addr, u32 size) override; private: - // STATE_TO_SAVE - u32 m_position = 0; - u32 m_command = 0; + enum Command + { + BackupOffsetSet = 0x01, + BackupWrite = 0x02, + BackupRead = 0x03, - void TransferByte(u8& byte) override; + DMAOffsetLengthSet = 0x05, + + ReadISR = 0x82, + WriteISR = 0x83, + ReadIMR = 0x86, + WriteIMR = 0x87, + + WriteLANCNT = 0xFF, + }; + + u32 m_position; + u32 m_backup_dma_offset; + u32 m_backup_dma_length; + u8 m_command[4]; + u16 m_backup_offset; + File::IOFile m_backup; + +protected: + void TransferByte(u8& _uByte) override; }; } // namespace ExpansionInterface diff --git a/Source/Core/Core/HW/GCPadEmu.cpp b/Source/Core/Core/HW/GCPadEmu.cpp index 0058bc74e4..35cfeda2a8 100644 --- a/Source/Core/Core/HW/GCPadEmu.cpp +++ b/Source/Core/Core/HW/GCPadEmu.cpp @@ -34,6 +34,7 @@ static const u16 trigger_bitmasks[] = { static const u16 dpad_bitmasks[] = {PAD_BUTTON_UP, PAD_BUTTON_DOWN, PAD_BUTTON_LEFT, PAD_BUTTON_RIGHT}; +static const u8 triforce_bitmask[] = {SWITCH_TEST, SWITCH_SERVICE, SWITCH_COIN}; GCPad::GCPad(const unsigned int index) : m_index(index) { @@ -68,6 +69,13 @@ GCPad::GCPad(const unsigned int index) : m_index(index) m_dpad->AddInput(Translatability::Translate, named_direction); } + // triforce + groups.emplace_back(m_triforce = new ControllerEmu::Buttons(TRIFORCE_GROUP)); + for (const char* named_button : {TEST_BUTTON, SERVICE_BUTTON, COIN_BUTTON}) + { + m_triforce->AddInput(Translatability::DoNotTranslate, named_button); + } + // Microphone groups.emplace_back(m_mic = new ControllerEmu::Buttons(MIC_GROUP)); m_mic->AddInput(Translatability::Translate, _trans("Button")); @@ -119,6 +127,8 @@ ControllerEmu::ControlGroup* GCPad::GetGroup(PadGroup group) return m_mic; case PadGroup::Options: return m_options; + case PadGroup::Triforce: + return m_triforce; default: return nullptr; } @@ -150,6 +160,9 @@ GCPadStatus GCPad::GetInput() const // dpad m_dpad->GetState(&pad.button, dpad_bitmasks, m_input_override_function); + // triforce + m_triforce->GetState(&pad.switches, triforce_bitmask, m_input_override_function); + // sticks const auto main_stick_state = m_main_stick->GetState(m_input_override_function); pad.stickX = MapFloat(main_stick_state.x, GCPadStatus::MAIN_STICK_CENTER_X, 1); @@ -202,6 +215,11 @@ void GCPad::LoadDefaults(const ControllerInterface& ciface) m_dpad->SetControlExpression(2, "`F`"); // Left m_dpad->SetControlExpression(3, "`H`"); // Right + // Triforce + m_triforce->SetControlExpression(0, "`1`"); // Test + m_triforce->SetControlExpression(1, "`2`"); // Service + m_triforce->SetControlExpression(2, "`3`"); // Coin + // C Stick m_c_stick->SetControlExpression(0, "`I`"); // Up m_c_stick->SetControlExpression(1, "`K`"); // Down diff --git a/Source/Core/Core/HW/GCPadEmu.h b/Source/Core/Core/HW/GCPadEmu.h index 2555ee05b1..6e244d139f 100644 --- a/Source/Core/Core/HW/GCPadEmu.h +++ b/Source/Core/Core/HW/GCPadEmu.h @@ -29,7 +29,8 @@ enum class PadGroup Triggers, Rumble, Mic, - Options + Options, + Triforce, }; class GCPad : public ControllerEmu::EmulatedController @@ -61,6 +62,7 @@ public: static constexpr const char* RUMBLE_GROUP = _trans("Rumble"); static constexpr const char* MIC_GROUP = _trans("Microphone"); static constexpr const char* OPTIONS_GROUP = _trans("Options"); + static constexpr const char* TRIFORCE_GROUP = _trans("Triforce"); static constexpr const char* A_BUTTON = "A"; static constexpr const char* B_BUTTON = "B"; @@ -69,6 +71,11 @@ public: static constexpr const char* Z_BUTTON = "Z"; static constexpr const char* START_BUTTON = "Start"; + // Special Triforce buttons + static constexpr const char* TEST_BUTTON = "Test"; + static constexpr const char* SERVICE_BUTTON = "Service"; + static constexpr const char* COIN_BUTTON = "Coin"; + // i18n: The left trigger button (labeled L on real controllers) static constexpr const char* L_DIGITAL = _trans("L"); // i18n: The right trigger button (labeled R on real controllers) @@ -87,6 +94,7 @@ private: ControllerEmu::ControlGroup* m_rumble; ControllerEmu::Buttons* m_mic; ControllerEmu::ControlGroup* m_options; + ControllerEmu::Buttons* m_triforce; ControllerEmu::SettingValue m_always_connected_setting; diff --git a/Source/Core/Core/HW/Memmap.h b/Source/Core/Core/HW/Memmap.h index ce45d73034..4b33776a62 100644 --- a/Source/Core/Core/HW/Memmap.h +++ b/Source/Core/Core/HW/Memmap.h @@ -123,7 +123,6 @@ public: // If the specified range is within a single valid memory region, returns a pointer to the start // of the corresponding range in host memory. Otherwise, returns nullptr. u8* GetPointerForRange(u32 address, size_t size) const; - void CopyFromEmu(void* data, u32 address, size_t size) const; void CopyToEmu(u32 address, const void* data, size_t size); void Memset(u32 address, u8 value, size_t size); diff --git a/Source/Core/Core/HW/SI/SI.h b/Source/Core/Core/HW/SI/SI.h index 731b9b5c19..0a10b54a6a 100644 --- a/Source/Core/Core/HW/SI/SI.h +++ b/Source/Core/Core/HW/SI/SI.h @@ -56,6 +56,7 @@ public: void RemoveEvent(int device_number); void UpdateDevices(); + u32 GetInLength(void) const { return m_com_csr.INLNGTH; } void RemoveDevice(int device_number); void AddDevice(SIDevices device, int device_number); diff --git a/Source/Core/Core/HW/SI/SI_Device.cpp b/Source/Core/Core/HW/SI/SI_Device.cpp index 451523624b..8614bb156c 100644 --- a/Source/Core/Core/HW/SI/SI_Device.cpp +++ b/Source/Core/Core/HW/SI/SI_Device.cpp @@ -17,6 +17,7 @@ #ifdef HAS_LIBMGBA #include "Core/HW/SI/SI_DeviceGBAEmu.h" #endif +#include "Core/HW/SI/SI_DeviceAMBaseboard.h" #include "Core/HW/SI/SI_DeviceGCAdapter.h" #include "Core/HW/SI/SI_DeviceGCController.h" #include "Core/HW/SI/SI_DeviceGCSteeringWheel.h" @@ -149,6 +150,7 @@ bool SIDevice_IsGCController(SIDevices type) case SIDEVICE_GC_TARUKONGA: case SIDEVICE_DANCEMAT: case SIDEVICE_GC_STEERING: + case SIDEVICE_AM_BASEBOARD: return true; default: return false; @@ -191,6 +193,8 @@ std::unique_ptr SIDevice_Create(Core::System& system, const SIDevices return std::make_unique(system, device, port_number); case SIDEVICE_AM_BASEBOARD: + return std::make_unique(system, device, port_number); + case SIDEVICE_NONE: default: return std::make_unique(system, device, port_number); diff --git a/Source/Core/Core/HW/SI/SI_Device.h b/Source/Core/Core/HW/SI/SI_Device.h index 234c475e5f..c3171f42e8 100644 --- a/Source/Core/Core/HW/SI/SI_Device.h +++ b/Source/Core/Core/HW/SI/SI_Device.h @@ -97,8 +97,6 @@ enum SIDevices : int SIDEVICE_GC_STEERING, SIDEVICE_DANCEMAT, SIDEVICE_GC_TARUKONGA, - // Was used for Triforce in the past, but the implementation is no longer in Dolphin. - // It's kept here so that values below will stay constant. SIDEVICE_AM_BASEBOARD, SIDEVICE_WIIU_ADAPTER, SIDEVICE_GC_GBA_EMULATED, diff --git a/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.cpp b/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.cpp new file mode 100644 index 0000000000..59cba0c532 --- /dev/null +++ b/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.cpp @@ -0,0 +1,2383 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Core/HW/SI/SI_DeviceAMBaseboard.h" + +#include +#include +#include +#include +#include + +#include + +#include "Common/Buffer.h" +#include "Common/ChunkFile.h" +#include "Common/CommonTypes.h" +#include "Common/Config/Config.h" +#include "Common/FileUtil.h" +#include "Common/Hash.h" +#include "Common/IOFile.h" +#include "Common/IniFile.h" +#include "Common/Logging/Log.h" +#include "Common/MsgHandler.h" +#include "Common/Swap.h" + +#include "Core/Boot/Boot.h" +#include "Core/BootManager.h" +#include "Core/Config/MainSettings.h" +#include "Core/Config/SYSCONFSettings.h" +#include "Core/ConfigLoaders/BaseConfigLoader.h" +#include "Core/ConfigLoaders/NetPlayConfigLoader.h" +#include "Core/ConfigManager.h" +#include "Core/Core.h" +#include "Core/CoreTiming.h" +#include "Core/HW/DVD/AMMediaboard.h" +#include "Core/HW/DVD/DVDInterface.h" +#include "Core/HW/EXI/EXI.h" +#include "Core/HW/GCPad.h" +#include "Core/HW/MMIO.h" +#include "Core/HW/Memmap.h" +#include "Core/HW/ProcessorInterface.h" +#include "Core/HW/SI/SI.h" +#include "Core/HW/SI/SI_Device.h" +#include "Core/HW/SI/SI_DeviceGCController.h" +#include "Core/HW/Sram.h" +#include "Core/HW/SystemTimers.h" +#include "Core/HW/WiimoteReal/WiimoteReal.h" +#include "Core/Movie.h" +#include "Core/NetPlayProto.h" +#include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" +#include "Core/WiiRoot.h" + +#include "DiscIO/Enums.h" + +#include "InputCommon/GCPadStatus.h" + +namespace SerialInterface +{ + +JVSIOMessage::JVSIOMessage() +{ + m_ptr = 0; + m_last_start = 0; +} + +void JVSIOMessage::Start(int node) +{ + m_last_start = m_ptr; + const u8 hdr[3] = {0xE0, (u8)node, 0}; + m_csum = 0; + AddData(hdr, 3, 1); +} + +void JVSIOMessage::AddData(const u8* dst, size_t len, int sync = 0) +{ + while (len--) + { + const u8 c = *dst++; + if (!sync && ((c == 0xE0) || (c == 0xD0))) + { + m_msg[m_ptr++] = 0xD0; + m_msg[m_ptr++] = c - 1; + } + else + { + m_msg[m_ptr++] = c; + } + + if (!sync) + m_csum += c; + sync = 0; + if (m_ptr >= 0x80) + PanicAlertFmt("JVSIOMessage overrun!"); + } +} + +void JVSIOMessage::AddData(const void* data, size_t len) +{ + AddData((const u8*)data, len); +} + +void JVSIOMessage::AddData(const char* data) +{ + AddData(data, strlen(data)); +} + +void JVSIOMessage::AddData(u32 n) +{ + const u8 cs = n; + AddData(&cs, 1); +} + +void JVSIOMessage::End() +{ + const u32 len = m_ptr - m_last_start; + m_msg[m_last_start + 2] = len - 2; // assuming len <0xD0 + AddData(m_csum + len - 2); +} + +static u8 CheckSumXOR(u8* Data, u32 Length) +{ + u8 check = 0; + + for (u32 i = 0; i < Length; i++) + { + check ^= Data[i]; + } + + return check; +} + +// Reply has to be delayed due a bug in the parser +static void swap_buffers(u8* buffer, u32* buffer_length) +{ + static u8 last[2][0x80]; + static u32 lastptr[2]; + + memcpy(last[1], buffer, 0x80); // Save current buffer + memcpy(buffer, last[0], 0x80); // Load previous buffer + memcpy(last[0], last[1], 0x80); // Update history + + lastptr[1] = *buffer_length; // Swap lengths + *buffer_length = lastptr[0]; + lastptr[0] = lastptr[1]; +} + +static const char s_cdr_program_version[] = {" Version 1.22,2003/09/19,171-8213B"}; +static const char s_cdr_boot_version[] = {" Version 1.04,2003/06/17,171-8213B"}; +static const u8 s_cdr_card_data[] = { + 0x00, 0x6E, 0x00, 0x00, 0x01, 0x00, 0x00, 0x06, 0x00, 0x00, 0x07, 0x00, 0x00, 0x0B, + 0x00, 0x00, 0x0E, 0x00, 0x00, 0x10, 0x00, 0x00, 0x17, 0x00, 0x00, 0x19, 0x00, 0x00, + 0x1A, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x20, 0x00, + 0x00, 0x22, 0x00, 0x00, 0x23, 0x00, 0x00, 0x24, 0x00, 0x00, 0x27, 0x00, 0x00, 0x28, + 0x00, 0x00, 0x2C, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x34, 0x00, 0x00, 0x35, 0x00, 0x00, + 0x37, 0x00, 0x00, 0x38, 0x00, 0x00, 0x39, 0x00, 0x00, 0x3D, 0x00}; + +const static u8 s_region_flags[] = "\x00\x00\x30\x00" + // "\x01\xfe\x00\x00" // JAPAN + "\x02\xfd\x00\x00" // USA + //"\x03\xfc\x00\x00" // export + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; +// AM-Baseboard device on SI +CSIDevice_AMBaseboard::CSIDevice_AMBaseboard(Core::System& system, SIDevices device, + int device_number) + : ISIDevice(system, device, device_number) +{ + std::ranges::fill(m_coin, 0); + + // Setup IC-card + m_ic_card_state = 0x20; + m_ic_card_status = ICCARDStatus::Okay; + m_ic_card_session = 0x23; + + m_ic_write_size = 0; + m_ic_write_offset = 0; + + memset(m_ic_write_buffer, 0, sizeof(m_ic_write_buffer)); + memset(m_ic_card_data, 0, sizeof(m_ic_card_data)); + + // Card ID + m_ic_card_data[0x20] = 0x95; + m_ic_card_data[0x21] = 0x71; + + if (AMMediaboard::GetGameType() == KeyOfAvalon) + { + m_ic_card_data[0x22] = 0x26; + m_ic_card_data[0x23] = 0x40; + } + else if (AMMediaboard::GetGameType() == VirtuaStriker4) + { + m_ic_card_data[0x22] = 0x44; + m_ic_card_data[0x23] = 0x00; + } + + // Use count + m_ic_card_data[0x28] = 0xFF; + m_ic_card_data[0x29] = 0xFF; + + // Setup CARD + m_card_memory_size = 0; + m_card_is_inserted = false; + + m_card_offset = 0; + m_card_command = 0; + m_card_clean = 0; + + m_card_write_length = 0; + m_card_wrote = 0; + + m_card_read_length = 0; + m_card_read = 0; + + m_card_bit = 0; + m_card_shutter = true; // Open + m_card_state_call_count = 0; + + // Serial + m_wheel_init = 0; + + m_motor_init = 0; + m_motor_force_y = 0; + + m_fzdx_seatbelt = true; + m_fzdx_motion_stop = false; + m_fzdx_sensor_right = false; + m_fzdx_sensor_left = false; + + m_rx_reply = 0xF0; + + m_fzcc_seatbelt = true; + m_fzcc_sensor = false; + m_fzcc_emergency = false; + m_fzcc_service = false; + + memset(m_motor_reply, 0, sizeof(m_motor_reply)); +} + +constexpr u32 SI_XFER_LENGTH_MASK = 0x7f; + +// Translate [0,1,2,...,126,127] to [128,1,2,...,126,127] +constexpr s32 ConvertSILengthField(u32 field) +{ + return ((field - 1) & SI_XFER_LENGTH_MASK) + 1; +} + +void CSIDevice_AMBaseboard::ICCardSendReply(ICCommand* iccommand, u8* buffer, u32* length) +{ + iccommand->status = Common::swap16(iccommand->status); + + const u8 crc = CheckSumXOR(iccommand->data + 2, iccommand->pktlen - 1); + + for (u32 i = 0; i < iccommand->pktlen + 1; ++i) + { + buffer[(*length)++] = iccommand->data[i]; + } + + buffer[(*length)++] = crc; +} + +int CSIDevice_AMBaseboard::RunBuffer(u8* buffer, int request_length) +{ + const auto& serial_interface = m_system.GetSerialInterface(); + u32 buffer_length = ConvertSILengthField(serial_interface.GetInLength()); + + // Debug logging + ISIDevice::RunBuffer(buffer, buffer_length); + + u32 buffer_position = 0; + while (buffer_position < buffer_length) + { + BaseBoardCommand command = static_cast(buffer[buffer_position]); + buffer_position++; + + switch (command) + { + case BaseBoardCommand::GCAM_Reset: // Returns ID and dip switches + { + const u32 id = Common::swap32(SI_AM_BASEBOARD | 0x100); + std::memcpy(buffer, &id, sizeof(id)); + return sizeof(id); + } + break; + case BaseBoardCommand::GCAM_Command: + { + u32 checksum = 0; + for (u32 i = 0; i < buffer_length; ++i) + checksum += buffer[i]; + + u8 data_out[0x80]{}; + u32 data_offset = 0; + + static u32 dip_switch_1 = 0xFE; + static u32 dip_switch_0 = 0xFF; + + data_out[data_offset++] = 1; + data_out[data_offset++] = 1; + + u8* data_in = buffer + 2; + const u8* data_in_end = buffer + buffer[buffer_position] + 2; + + while (data_in < data_in_end) + { + const u32 gcam_command = *data_in++; + switch (GCAMCommand(gcam_command)) + { + case GCAMCommand::StatusSwitches: + { + const u8 status = *data_in++; + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x10, {:02x} (READ STATUS&SWITCHES)", + status); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x2; + + // We read Test/Service from the JVS-I/O SwitchesInput instead + // + // const GCPadStatus PadStatus = Pad::GetStatus(ISIDevice::m_device_number); + // baseboard test/service switches + // if (PadStatus.button & PAD_BUTTON_Y) // Test + // dip_switch_0 &= ~0x80; + // if (PadStatus.button & PAD_BUTTON_X) // Service + // dip_switch_0 &= ~0x40; + + // Horizontal Scanning Frequency switch + // Required for F-Zero AX booting via Sega Boot + if (AMMediaboard::GetGameType() == FZeroAX || + AMMediaboard::GetGameType() == FZeroAXMonster) + { + dip_switch_0 &= ~0x20; + } + + // Disable camera in MKGP1/2 + if (AMMediaboard::GetGameType() == MarioKartGP || + AMMediaboard::GetGameType() == MarioKartGP2) + { + dip_switch_0 &= ~0x10; + } + + data_out[data_offset++] = dip_switch_0; + data_out[data_offset++] = dip_switch_1; + break; + } + case GCAMCommand::SerialNumber: + { + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x11, {:02x} (READ SERIAL NR)", + *data_in++); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 16; + memcpy(data_out + data_offset, "AADE-01B98394904", 16); + + data_offset += 16; + break; + } + case GCAMCommand::Unknown_12: + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x12, {:02x} {:02x}", data_in[0], + data_in[1]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; + + data_in += 2; + break; + case GCAMCommand::Unknown_14: + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x14, {:02x} {:02x}", data_in[0], + data_in[1]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; + + data_in += 2; + break; + case GCAMCommand::FirmVersion: + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x15, {:02x} (READ FIRM VERSION)", + *data_in++); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x02; + // 00.26 + data_out[data_offset++] = 0x00; + data_out[data_offset++] = 0x26; + break; + case GCAMCommand::FPGAVersion: + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x16, {:02x} (READ FPGA VERSION)", + *data_in++); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x02; + // 07.06 + data_out[data_offset++] = 0x07; + data_out[data_offset++] = 0x06; + break; + case GCAMCommand::RegionSettings: + { + // Used by SegaBoot for region checks (dev mode skips this check) + // In some games this also controls the displayed language + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command 0x1F, {:02x} {:02x} {:02x} {:02x} {:02x} (REGION)", + data_in[0], data_in[1], data_in[2], data_in[3], data_in[4]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x14; + + for (int i = 0; i < 0x14; ++i) + data_out[data_offset++] = s_region_flags[i]; + + data_in += 5; + } + break; + // No reply + // Note: Always sends three bytes even though size is set to two + case GCAMCommand::Unknown_21: + { + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x21, {:02x}, {:02x}, {:02x}, {:02x}", + data_in[0], data_in[1], data_in[2], data_in[3]); + data_in += 4; + } + break; + // No reply + // Note: Always sends six bytes + case GCAMCommand::Unknown_22: + { + DEBUG_LOG_FMT( + SERIALINTERFACE_AMBB, + "GC-AM: Command 0x22, {:02x}, {:02x}, {:02x}, {:02x}, {:02x}, {:02x}, {:02x}", + data_in[0], data_in[1], data_in[2], data_in[3], data_in[4], data_in[5], data_in[6]); + data_in += data_in[0] + 1; + } + break; + case GCAMCommand::Unknown_23: + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x23, {:02x} {:02x}", data_in[0], + data_in[1]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; + + data_in += 2; + break; + case GCAMCommand::Unknown_24: + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x24, {:02x} {:02x}", data_in[0], + data_in[1]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; + + data_in += 2; + break; + case GCAMCommand::SerialA: + { + u32 length = *data_in++; + if (length) + { + INFO_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command 0x31, {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} " + "{:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}", + length, data_in[0], data_in[1], data_in[2], data_in[3], data_in[4], + data_in[5], data_in[6], data_in[7], data_in[8], data_in[9], data_in[10], + data_in[11], data_in[12]); + + // Serial - Wheel + if (AMMediaboard::GetGameType() == MarioKartGP || + AMMediaboard::GetGameType() == MarioKartGP2) + { + INFO_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command 0x31, (WHEEL) {:02x}{:02x} {:02x}{:02x} {:02x} {:02x} " + "{:02x} {:02x} {:02x} {:02x}", + data_in[0], data_in[1], data_in[2], data_in[3], data_in[4], data_in[5], + data_in[6], data_in[7], data_in[8], data_in[9]); + + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x03; + + switch (m_wheel_init) + { + case 0: + data_out[data_offset++] = 'E'; // Error + data_out[data_offset++] = '0'; + data_out[data_offset++] = '0'; + m_wheel_init++; + break; + case 1: + data_out[data_offset++] = 'C'; // Power Off + data_out[data_offset++] = '0'; + data_out[data_offset++] = '6'; + // Only turn on when a wheel is connected + if (serial_interface.GetDeviceType(1) == SerialInterface::SIDEVICE_GC_STEERING) + { + m_wheel_init++; + } + break; + case 2: + data_out[data_offset++] = 'C'; // Power On + data_out[data_offset++] = '0'; + data_out[data_offset++] = '1'; + break; + default: + break; + } + + // u16 CenteringForce= ptr(6); + // u16 FrictionForce = ptr(8); + // u16 Roll = ptr(10); + + data_in += length; + break; + } + + // Serial - Unknown + if (AMMediaboard::GetGameType() == GekitouProYakyuu) + { + u32 serial_command = *(u32*)(data_in); + + if (serial_command == 0x00001000) + { + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x03; + data_out[data_offset++] = 1; + data_out[data_offset++] = 2; + data_out[data_offset++] = 3; + } + + data_in += length; + break; + } + + // Serial IC-CARD / Serial Deck Reader + if (AMMediaboard::GetGameType() == VirtuaStriker4 || + AMMediaboard::GetGameType() == VirtuaStriker4_2006 || + AMMediaboard::GetGameType() == KeyOfAvalon) + { + u32 serial_command = data_in[1]; + + ICCommand icco; + + // Set default reply + icco.pktcmd = gcam_command; + icco.pktlen = 7; + icco.fixed = 0x10; + icco.command = serial_command; + icco.flag = 0; + icco.length = 2; + icco.status = 0; + icco.extlen = 0; + + // Check for rest of data from the write pages command + if (m_ic_write_size && m_ic_write_offset) + { + const u32 size = data_in[1]; + + char logptr[1024]; + char* log = logptr; + + for (u32 i = 0; i < (u32)(data_in[1] + 2); ++i) + { + log += sprintf(log, "%02X ", data_in[i]); + } + + INFO_LOG_FMT(SERIALINTERFACE_CARD, "Command: {}", logptr); + + INFO_LOG_FMT( + SERIALINTERFACE_CARD, + "GC-AM: Command 25 (IC-CARD) Write Pages: Off:{:x} Size:{:x} PSize:{:x}", + m_ic_write_offset, m_ic_write_size, size); + + memcpy(m_ic_write_buffer + m_ic_write_offset, data_in + 2, size); + + m_ic_write_offset += size; + + if (m_ic_write_offset > m_ic_write_size) + { + m_ic_write_offset = 0; + + const u16 page = m_ic_write_buffer[5]; + const u16 count = m_ic_write_buffer[7]; + + memcpy(m_ic_card_data + page * 8, m_ic_write_buffer + 10, count * 8); + + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 25 (IC-CARD) Write Pages:{} Count:{}({:x})", page, + count, size); + + icco.command = WritePages; + + ICCardSendReply(&icco, data_out, &data_offset); + } + data_in += length; + break; + } + + switch (ICCARDCommand(serial_command)) + { + case ICCARDCommand::GetStatus: + icco.status = m_ic_card_state; + + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (IC-CARD) Get Status:{:02x}", m_ic_card_state); + break; + case ICCARDCommand::SetBaudrate: + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (IC-CARD) Set Baudrate"); + break; + case ICCARDCommand::FieldOn: + m_ic_card_state |= 0x10; + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (IC-CARD) Field On"); + break; + case ICCARDCommand::InsertCheck: + icco.status = m_ic_card_status; + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (IC-CARD) Insert Check:{:02x}", m_ic_card_status); + break; + case ICCARDCommand::AntiCollision: + icco.extlen = 8; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + // Card ID + icco.extdata[0] = 0x00; + icco.extdata[1] = 0x00; + icco.extdata[2] = 0x54; + icco.extdata[3] = 0x4D; + icco.extdata[4] = 0x50; + icco.extdata[5] = 0x00; + icco.extdata[6] = 0x00; + icco.extdata[7] = 0x00; + + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (IC-CARD) Anti Collision"); + break; + case ICCARDCommand::SelectCard: + icco.extlen = 8; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + // Session + icco.extdata[0] = 0x00; + icco.extdata[1] = m_ic_card_session; + icco.extdata[2] = 0x00; + icco.extdata[3] = 0x00; + icco.extdata[4] = 0x00; + icco.extdata[5] = 0x00; + icco.extdata[6] = 0x00; + icco.extdata[7] = 0x00; + + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (IC-CARD) Select Card:{}", + m_ic_card_session); + break; + case ICCARDCommand::ReadPage: + case ICCARDCommand::ReadUseCount: + { + const u16 page = Common::swap16(*(u16*)(data_in + 6)); + + icco.extlen = 8; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + memcpy(icco.extdata, m_ic_card_data + page * 8, 8); + + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 31 (IC-CARD) Read Page:{}", + page); + break; + } + case ICCARDCommand::WritePage: + { + const u16 page = Common::swap16(*(u16*)(data_in + 8)); + + // Write only one page + if (page == 4) + { + icco.status = 0x80; + } + else + { + memcpy(m_ic_card_data + page * 8, data_in + 10, 8); + } + + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (IC-CARD) Write Page:{}", + page); + break; + } + case ICCARDCommand::DecreaseUseCount: + { + const u16 page = Common::swap16(*(u16*)(data_in + 6)); + + icco.extlen = 2; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + *(u16*)(m_ic_card_data + 0x28) = *(u16*)(m_ic_card_data + 0x28) - 1; + + // Counter + icco.extdata[0] = m_ic_card_data[0x28]; + icco.extdata[1] = m_ic_card_data[0x29]; + + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 31 (IC-CARD) Decrease Use Count:{}", page); + break; + } + case ICCARDCommand::ReadPages: + { + const u16 page = Common::swap16(*(u16*)(data_in + 6)); + const u16 count = Common::swap16(*(u16*)(data_in + 8)); + + const u32 offs = page * 8; + u32 cnt = count * 8; + + // Limit read size to not overwrite the reply buffer + if (cnt > (u32)0x50 - data_offset) + { + cnt = 5 * 8; + } + + icco.extlen = cnt; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + memcpy(icco.extdata, m_ic_card_data + offs, cnt); + + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 31 (IC-CARD) Read Pages:{} Count:{}", page, count); + break; + } + case ICCARDCommand::WritePages: + { + const u16 pksize = length; + const u16 size = Common::swap16(*(u16*)(data_in + 2)); + const u16 page = Common::swap16(*(u16*)(data_in + 6)); + const u16 count = Common::swap16(*(u16*)(data_in + 8)); + + // We got a complete packet + if (pksize - 5 == size) + { + if (page == 4) // Read Only Page, must return error + { + icco.status = 0x80; + } + else + { + memcpy(m_ic_card_data + page * 8, data_in + 13, count * 8); + } + + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (IC-CARD) Write Pages:{} Count:{}({:x})", page, + count, size); + } + // VirtuaStriker 4 splits the writes over multiple packets + else + { + memcpy(m_ic_write_buffer, data_in + 2, pksize); + m_ic_write_offset += pksize; + m_ic_write_size = size; + } + break; + } + default: + // Handle Deck Reader commands + serial_command = data_in[0]; + icco.command = serial_command; + icco.flag = 0; + switch (CDReaderCommand(serial_command)) + { + case CDReaderCommand::ProgramVersion: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Program Version"); + + icco.extlen = (u32)strlen(s_cdr_program_version); + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + memcpy(icco.extdata, s_cdr_program_version, icco.extlen); + break; + case CDReaderCommand::BootVersion: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Boot Version"); + + icco.extlen = (u32)strlen(s_cdr_boot_version); + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + memcpy(icco.extdata, s_cdr_boot_version, icco.extlen); + break; + case CDReaderCommand::ShutterGet: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Shutter Get"); + + icco.extlen = 4; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + icco.extdata[0] = 0; + icco.extdata[1] = 0; + icco.extdata[2] = 0; + icco.extdata[3] = 0; + break; + case CDReaderCommand::CameraCheck: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Camera Check"); + + icco.extlen = 6; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + icco.extdata[0] = 0x23; + icco.extdata[1] = 0x28; + icco.extdata[2] = 0x45; + icco.extdata[3] = 0x29; + icco.extdata[4] = 0x45; + icco.extdata[5] = 0x29; + break; + case CDReaderCommand::ProgramChecksum: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Program Checksum"); + + icco.extlen = 4; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + icco.extdata[0] = 0x23; + icco.extdata[1] = 0x28; + icco.extdata[2] = 0x45; + icco.extdata[3] = 0x29; + break; + case CDReaderCommand::BootChecksum: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Boot Checksum"); + + icco.extlen = 4; + icco.length += icco.extlen; + icco.pktlen += icco.extlen; + + icco.extdata[0] = 0x23; + icco.extdata[1] = 0x28; + icco.extdata[2] = 0x45; + icco.extdata[3] = 0x29; + break; + case CDReaderCommand::SelfTest: + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (DECK READER) Self Test"); + icco.flag = 0x00; + break; + case CDReaderCommand::SensLock: + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (DECK READER) Sens Lock"); + icco.flag = 0x01; + break; + case CDReaderCommand::SensCard: + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (DECK READER) Sens Card"); + break; + case CDReaderCommand::ShutterCard: + INFO_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (DECK READER) Shutter Card"); + break; + case CDReaderCommand::ReadCard: + INFO_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command 0x31 (DECK READER) Read Card"); + + icco.fixed = 0xAA; + icco.flag = 0xAA; + icco.extlen = sizeof(s_cdr_card_data); + icco.length = 0x72; + icco.status = Common::swap16(icco.extlen); + + icco.pktlen += icco.extlen; + + memcpy(icco.extdata, s_cdr_card_data, sizeof(s_cdr_card_data)); + + break; + default: + WARN_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command 0x31 (IC-Card) {:02x} {:02x} {:02x} {:02x} {:02x} " + "{:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}", + data_in[2], data_in[3], data_in[4], data_in[5], data_in[6], + data_in[7], data_in[8], data_in[9], data_in[10], data_in[11], + data_in[12], data_in[13]); + break; + } + break; + } + + ICCardSendReply(&icco, data_out, &data_offset); + + data_in += length; + break; + } + } + + u32 command_offset = 0; + while (command_offset < length) + { + // All commands are OR'd with 0x80 + // Last byte is checksum which we don't care about + const u32 serial_command = + Common::swap32(*(u32*)(data_in + command_offset)) ^ 0x80000000; + + if (AMMediaboard::GetGameType() == FZeroAX || + AMMediaboard::GetGameType() == FZeroAXMonster) + { + INFO_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command 0x31 (MOTOR) Length:{:02x} Command:{:04x}({:02x})", + length, (serial_command >> 8) & 0xFFFF, serial_command >> 24); + } + else + { + INFO_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x31 (SERIAL) Command:{:06x}", + serial_command); + + if (/*command == 0xf43200 || */ serial_command == 0x801000) + { + // u32 PC = m_system.GetPowerPC().GetPPCState().pc; + + // INFO_LOG_FMT(SERIALINTERFACE_AMBB, "GCAM: PC:{:08x}", PC); + + // m_system.GetPowerPC().GetBreakPoints().Add(PC + 8, true, true, std::nullopt); + + data_out[data_offset++] = 0x31; + data_out[data_offset++] = 0x02; + data_out[data_offset++] = 0xFF; + data_out[data_offset++] = 0x01; + } + } + + command_offset += 4; + + if (AMMediaboard::GetGameType() == FZeroAX || + AMMediaboard::GetGameType() == FZeroAXMonster) + { + // Status + m_motor_reply[command_offset + 2] = 0; + m_motor_reply[command_offset + 3] = 0; + + // Error + m_motor_reply[command_offset + 4] = 0; + + switch (serial_command >> 24) + { + case 0: + break; + case 1: // Set Maximum? + break; + case 2: + break; + + // 0x00-0x40: left + // 0x40-0x80: right + case 4: // Move Steering Wheel + // Left + if (serial_command & 0x010000) + { + m_motor_force_y = -((s16)serial_command & 0xFF00); + } + else // Right + { + m_motor_force_y = (serial_command - 0x4000) & 0xFF00; + } + + m_motor_force_y *= 2; + + // FFB + if (m_motor_init == 2) + { + if (serial_interface.GetDeviceType(1) == SerialInterface::SIDEVICE_GC_STEERING) + { + const GCPadStatus PadStatus = Pad::GetStatus(1); + if (PadStatus.isConnected) + { + const ControlState mapped_strength = (double)(m_motor_force_y >> 8) / 127.f; + + Pad::Rumble(1, mapped_strength); + INFO_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command 0x31 (MOTOR) mapped_strength:{}", + mapped_strength); + } + } + } + break; + case 6: // nice + case 9: + default: + break; + // Switch back to normal controls + case 7: + m_motor_init = 2; + break; + // Reset + case 0x7F: + m_motor_init = 1; + memset(m_motor_reply, 0, sizeof(m_motor_reply)); + break; + } + + // Checksum + m_motor_reply[command_offset + 5] = m_motor_reply[command_offset + 2] ^ + m_motor_reply[command_offset + 3] ^ + m_motor_reply[command_offset + 4]; + } + } + + if (length == 0) + { + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; + } + else + { + if (m_motor_init) + { + // Motor + m_motor_reply[0] = gcam_command; + m_motor_reply[1] = length; // Same out as in size + + memcpy(data_out + data_offset, m_motor_reply, m_motor_reply[1] + 2); + data_offset += m_motor_reply[1] + 2; + } + } + + data_in += length; + break; + } + case GCAMCommand::SerialB: + { + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 32 (CARD-Interface)"); + const u32 length = *data_in++; + if (length) + { + // Send Card Reply + if (length == 1 && data_in[0] == 0x05) + { + if (m_card_read_length) + { + data_out[data_offset++] = gcam_command; + u32 read_length = m_card_read_length - m_card_read; + + if (AMMediaboard::GetGameType() == FZeroAX) + { + if (read_length > 0x2F) + read_length = 0x2F; + } + + data_out[data_offset++] = read_length; // 0x2F (max size per packet) + + memcpy(data_out + data_offset, m_card_read_packet + m_card_read, read_length); + + data_offset += read_length; + m_card_read += read_length; + + if (m_card_read >= m_card_read_length) + m_card_read_length = 0; + + data_in += length; + break; + } + + data_out[data_offset++] = gcam_command; + const u32 command_length_offset = data_offset; + data_out[data_offset++] = 0x00; // len + + data_out[data_offset++] = 0x02; // + const u32 checksum_start = data_offset; + + data_out[data_offset++] = 0x00; // 0x00 len + + data_out[data_offset++] = m_card_command; // 0x01 command + + switch (CARDCommand(m_card_command)) + { + case CARDCommand::Init: + data_out[data_offset++] = 0x00; // 0x02 + data_out[data_offset++] = 0x30; // 0x03 + break; + case CARDCommand::GetState: + data_out[data_offset++] = 0x20 | m_card_bit; // 0x02 + + // bit 0: Please take your card + // bit 1: Endless waiting causes UNK_E to be called + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::Read: + data_out[data_offset++] = 0x02; // 0x02 + data_out[data_offset++] = 0x53; // 0x03 + break; + case CARDCommand::IsPresent: + data_out[data_offset++] = 0x22; // 0x02 + data_out[data_offset++] = 0x30; // 0x03 + break; + case CARDCommand::Write: + data_out[data_offset++] = 0x02; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::SetPrintParam: + data_out[data_offset++] = 0x00; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::RegisterFont: + data_out[data_offset++] = 0x00; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::WriteInfo: + data_out[data_offset++] = 0x02; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::Eject: + if (AMMediaboard::GetGameType() == FZeroAX) + { + data_out[data_offset++] = 0x01; // 0x02 + } + else + { + data_out[data_offset++] = 0x31; // 0x02 + } + data_out[data_offset++] = 0x30; // 0x03 + break; + case CARDCommand::Clean: + data_out[data_offset++] = 0x02; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + case CARDCommand::Load: + data_out[data_offset++] = 0x02; // 0x02 + data_out[data_offset++] = 0x30; // 0x03 + break; + case CARDCommand::SetShutter: + data_out[data_offset++] = 0x00; // 0x02 + data_out[data_offset++] = 0x00; // 0x03 + break; + } + + data_out[data_offset++] = 0x30; // 0x04 + data_out[data_offset++] = 0x00; // 0x05 + + data_out[data_offset++] = 0x03; // 0x06 + + data_out[checksum_start] = data_offset - checksum_start; // 0x00 len + + data_out[data_offset] = 0; // 0x07 + for (u32 i = 0; i < data_out[checksum_start]; ++i) + data_out[data_offset] ^= data_out[checksum_start + i]; + + data_offset++; + + data_out[command_length_offset] = data_out[checksum_start] + 2; + } + else + { + for (u32 i = 0; i < length; ++i) + m_card_buffer[m_card_offset + i] = data_in[i]; + + m_card_offset += length; + + // Check if we got a complete command + if (m_card_buffer[0] == 0x02) + { + if (m_card_buffer[1] == m_card_offset - 2) + { + if (m_card_buffer[m_card_offset - 2] == 0x03) + { + m_card_command = m_card_buffer[2]; + + switch (CARDCommand(m_card_command)) + { + case CARDCommand::Init: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD Init"); + + m_card_write_length = 0; + m_card_bit = 0; + m_card_memory_size = 0; + m_card_state_call_count = 0; + break; + case CARDCommand::GetState: + { + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD GetState({:02X})", + m_card_bit); + + if (m_card_memory_size == 0) + { + const std::string card_filename( + fmt::format("{}tricard_{}.bin", File::GetUserPath(D_TRIUSER_IDX), + SConfig::GetInstance().GetGameID())); + + if (File::Exists(card_filename)) + { + File::IOFile card(card_filename, "rb+"); + m_card_memory_size = (u32)card.GetSize(); + + card.ReadBytes(m_card_memory, m_card_memory_size); + card.Close(); + + m_card_is_inserted = true; + } + } + + if (AMMediaboard::GetGameType() == FZeroAX && m_card_memory_size) + { + m_card_state_call_count++; + if (m_card_state_call_count > 10) + { + if (m_card_bit & 2) + m_card_bit &= ~2; + else + m_card_bit |= 2; + + m_card_state_call_count = 0; + } + } + + if (m_card_clean == 1) + { + m_card_clean = 2; + } + else if (m_card_clean == 2) + { + const std::string card_filename( + fmt::format("{}tricard_{}.bin", File::GetUserPath(D_TRIUSER_IDX), + SConfig::GetInstance().GetGameID())); + + if (File::Exists(card_filename)) + { + m_card_memory_size = (u32)File::GetSize(card_filename); + if (m_card_memory_size) + { + if (AMMediaboard::GetGameType() == FZeroAX) + { + m_card_bit = 2; + } + else + { + m_card_bit = 1; + } + } + } + m_card_clean = 0; + } + break; + } + case CARDCommand::IsPresent: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD IsPresent"); + break; + case CARDCommand::RegisterFont: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD RegisterFont"); + break; + case CARDCommand::Load: + { + u8 mode = m_card_buffer[6]; + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD Load({:02X})", + mode); + break; + } + case CARDCommand::Clean: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD Clean"); + m_card_clean = 1; + break; + case CARDCommand::Read: + { + const u8 mode = m_card_buffer[6]; + const u8 bitmode = m_card_buffer[7]; + const u8 track = m_card_buffer[8]; + + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command CARD Read({:02X},{:02X},{:02X})", mode, + bitmode, track); + + // Prepare read packet + memset(m_card_read_packet, 0, 0xDB); + u32 packet_offset = 0; + + const std::string card_filename( + fmt::format("{}tricard_{}.bin", File::GetUserPath(D_TRIUSER_IDX), + SConfig::GetInstance().GetGameID())); + + if (File::Exists(card_filename)) + { + File::IOFile card(card_filename, "rb+"); + if (m_card_memory_size == 0) + { + m_card_memory_size = (u32)card.GetSize(); + } + + card.ReadBytes(m_card_memory, m_card_memory_size); + card.Close(); + + m_card_is_inserted = true; + } + + m_card_read_packet[packet_offset++] = 0x02; // SUB CMD + m_card_read_packet[packet_offset++] = 0x00; // SUB CMDLen + + m_card_read_packet[packet_offset++] = 0x33; // CARD CMD + + if (m_card_is_inserted) // CARD Status + { + m_card_read_packet[packet_offset++] = 0x31; + } + else + { + m_card_read_packet[packet_offset++] = 0x30; + } + + m_card_read_packet[packet_offset++] = 0x30; + m_card_read_packet[packet_offset++] = 0x30; + + // Data reply + memcpy(m_card_read_packet + packet_offset, m_card_memory, m_card_memory_size); + packet_offset += m_card_memory_size; + + m_card_read_packet[packet_offset++] = 0x03; + + m_card_read_packet[1] = packet_offset - 1; // SUB CMDLen + + for (u32 i = 0; i < packet_offset - 1; ++i) + m_card_read_packet[packet_offset] ^= m_card_read_packet[1 + i]; + + packet_offset++; + + m_card_read_length = packet_offset; + m_card_read = 0; + break; + } + case CARDCommand::Write: + { + const u8 mode = m_card_buffer[6]; + const u8 bitmode = m_card_buffer[7]; + const u8 track = m_card_buffer[8]; + + m_card_memory_size = m_card_buffer[1] - 9; + + memcpy(m_card_memory, m_card_buffer + 9, m_card_memory_size); + + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, + "GC-AM: Command CARD Write: {:02X} {:02X} {:02X} {}", mode, + bitmode, track, m_card_memory_size); + + const std::string card_filename( + File::GetUserPath(D_TRIUSER_IDX) + "tricard_" + + SConfig::GetInstance().GetGameID().c_str() + ".bin"); + + File::IOFile card(card_filename, "wb+"); + card.WriteBytes(m_card_memory, m_card_memory_size); + card.Close(); + + m_card_bit = 2; + + m_card_state_call_count = 0; + break; + } + case CARDCommand::SetPrintParam: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD SetPrintParam"); + break; + case CARDCommand::WriteInfo: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD WriteInfo"); + break; + case CARDCommand::Erase: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD Erase"); + break; + case CARDCommand::Eject: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD Eject"); + if (AMMediaboard::GetGameType() != FZeroAX) + { + m_card_bit = 0; + } + break; + case CARDCommand::SetShutter: + NOTICE_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: Command CARD SetShutter"); + if (AMMediaboard::GetGameType() != FZeroAX) + { + m_card_bit = 0; + } + // Close + if (m_card_buffer[6] == 0x30) + { + m_card_shutter = false; + } + // Open + else if (m_card_buffer[6] == 0x31) + { + m_card_shutter = true; + } + break; + default: + ERROR_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: CARD:Unhandled command!"); + ERROR_LOG_FMT(SERIALINTERFACE_CARD, "GC-AM: CARD:[{:08X}]", m_card_command); + // hexdump( m_card_buffer, m_card_offset ); + break; + } + m_card_offset = 0; + } + } + } + + data_out[data_offset++] = 0x32; + data_out[data_offset++] = 0x01; // len + data_out[data_offset++] = 0x06; // OK + } + } + else + { + data_out[data_offset++] = gcam_command; + data_out[data_offset++] = 0x00; // len + } + data_in += length; + break; + } + case GCAMCommand::JVSIOA: + case GCAMCommand::JVSIOB: + { + DEBUG_LOG_FMT( + SERIALINTERFACE_JVSIO, + "GC-AM: Command {:02x}, {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} (JVS IO)", + gcam_command, data_in[0], data_in[1], data_in[2], data_in[3], data_in[4], data_in[5], + data_in[6]); + + JVSIOMessage message; + + static int delay = 0; + + const u8* frame = &data_in[0]; + const u8 nr_bytes = frame[3]; // Byte after E0 xx + const u32 frame_len = nr_bytes + 3; // Header(2) + length byte + payload + checksum + + u8 jvs_buf[0x80]; + memcpy(jvs_buf, frame, frame_len); + + // Extract node and payload pointers + u8 node = jvs_buf[2]; + u8* jvs_io = jvs_buf + 4; // First payload byte + const u8* jvs_end = jvs_buf + frame_len; // One byte before checksum + + message.Start(0); + message.AddData(1); + + // Now iterate over the payload + while (jvs_io < jvs_end) + { + int jvsio_command = *jvs_io++; + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO:node={}, command={:02x}", node, + jvsio_command); + + switch (JVSIOCommand(jvsio_command)) + { + case JVSIOCommand::IOID: + message.AddData(StatusOkay); + switch (AMMediaboard::GetGameType()) + { + case FZeroAX: + // Specific version that enables DX mode on AX machines, all this does is enable the + // motion of a chair + message.AddData("SEGA ENTERPRISES,LTD.;837-13844-01 I/O CNTL BD2 ;"); + break; + case FZeroAXMonster: + case MarioKartGP: + case MarioKartGP2: + default: + message.AddData("namco ltd.;FCA-1;Ver1.01;JPN,Multipurpose + Rotary Encoder"); + break; + case VirtuaStriker3: + case VirtuaStriker4: + case VirtuaStriker4_2006: + message.AddData("SEGA ENTERPRISES,LTD.;I/O BD JVS;837-13551;Ver1.00"); + break; + } + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x10, BoardID"); + message.AddData((u32)0); + break; + case JVSIOCommand::CommandRevision: + message.AddData(StatusOkay); + message.AddData(0x11); + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x11, CommandRevision"); + break; + case JVSIOCommand::JVRevision: + message.AddData(StatusOkay); + message.AddData(0x20); + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x12, JVRevision"); + break; + case JVSIOCommand::CommunicationVersion: + message.AddData(StatusOkay); + message.AddData(0x10); + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x13, CommunicationVersion"); + break; + + // Slave features: + // + // Inputs: + // 0x01: Switch input: players, buttons + // 0x02: Coin input: slots + // 0x03: Analog input: channels, bits + // 0x04: Rotary input: channels + // 0x05: Keycode input: 0,0,0 ? + // 0x06: Screen position input: X bits, Y bits, channels + // + // Outputs: + // 0x10: Card system: slots + // 0x11: Medal hopper: channels + // 0x12: GPO-out: slots + // 0x13: Analog output: channels + // 0x14: Character output: width, height, type + // 0x15: Backup + case JVSIOCommand::CheckFunctionality: + message.AddData(StatusOkay); + switch (AMMediaboard::GetGameType()) + { + case FZeroAX: + case FZeroAXMonster: + // 2 Player (12bit) (p2=paddles), 1 Coin slot, 6 Analog-in + // message.AddData((void *)"\x01\x02\x0C\x00", 4); + // message.AddData((void *)"\x02\x01\x00\x00", 4); + // message.AddData((void *)"\x03\x06\x00\x00", 4); + // message.AddData((void *)"\x00\x00\x00\x00", 4); + // + // DX Version: 2 Player (22bit) (p2=paddles), 2 Coin slot, 8 Analog-in, + // 22 Driver-out + message.AddData((void*)"\x01\x02\x12\x00", 4); + message.AddData((void*)"\x02\x02\x00\x00", 4); + message.AddData((void*)"\x03\x08\x0A\x00", 4); + message.AddData((void*)"\x12\x16\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + case VirtuaStriker3: + // 2 Player (13bit), 2 Coin slot, 4 Analog-in, 1 CARD, 8 Driver-out + message.AddData((void*)"\x01\x02\x0D\x00", 4); + message.AddData((void*)"\x02\x02\x00\x00", 4); + message.AddData((void*)"\x10\x01\x00\x00", 4); + message.AddData((void*)"\x12\x08\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + case GekitouProYakyuu: + // 2 Player (13bit), 2 Coin slot, 4 Analog-in, 1 CARD, 8 Driver-out + message.AddData((void*)"\x01\x02\x0D\x00", 4); + message.AddData((void*)"\x02\x02\x00\x00", 4); + message.AddData((void*)"\x03\x04\x00\x00", 4); + message.AddData((void*)"\x10\x01\x00\x00", 4); + message.AddData((void*)"\x12\x08\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + case VirtuaStriker4: + case VirtuaStriker4_2006: + // 2 Player (13bit), 1 Coin slot, 4 Analog-in, 1 CARD + message.AddData((void*)"\x01\x02\x0D\x00", 4); + message.AddData((void*)"\x02\x01\x00\x00", 4); + message.AddData((void*)"\x03\x04\x00\x00", 4); + message.AddData((void*)"\x10\x01\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + case KeyOfAvalon: + // 1 Player (15bit), 1 Coin slot, 3 Analog-in, Touch, 1 CARD, 1 Driver-out + // (Unconfirmed) + message.AddData((void*)"\x01\x01\x0F\x00", 4); + message.AddData((void*)"\x02\x01\x00\x00", 4); + message.AddData((void*)"\x03\x03\x00\x00", 4); + message.AddData((void*)"\x06\x10\x10\x01", 4); + message.AddData((void*)"\x10\x01\x00\x00", 4); + message.AddData((void*)"\x12\x01\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + case MarioKartGP: + case MarioKartGP2: + default: + // 1 Player (15bit), 1 Coin slot, 3 Analog-in, 1 CARD, 1 Driver-out + message.AddData((void*)"\x01\x01\x0F\x00", 4); + message.AddData((void*)"\x02\x01\x00\x00", 4); + message.AddData((void*)"\x03\x03\x00\x00", 4); + message.AddData((void*)"\x10\x01\x00\x00", 4); + message.AddData((void*)"\x12\x01\x00\x00", 4); + message.AddData((void*)"\x00\x00\x00\x00", 4); + break; + } + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x14, CheckFunctionality"); + break; + case JVSIOCommand::MainID: + while (*jvs_io++) + { + }; + message.AddData(StatusOkay); + break; + case JVSIOCommand::SwitchesInput: + { + u32 player_count = *jvs_io++; + u32 player_byte_count = *jvs_io++; + + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x20, SwitchInputs: {} {}", + player_count, player_byte_count); + + message.AddData(StatusOkay); + + GCPadStatus PadStatus = Pad::GetStatus(0); + + // Test button + if (PadStatus.switches & SWITCH_TEST) + { + // Trying to access the test menu without SegaBoot present will cause a crash + if (AMMediaboard::GetTestMenu()) + { + message.AddData(0x80); + } + else + { + PanicAlertFmt("Test menu is disabled due missing SegaBoot"); + } + } + else + { + message.AddData((u32)0x00); + } + + for (u32 i = 0; i < player_count; ++i) + { + u8 player_data[3] = {0, 0, 0}; + + // Service button + if (PadStatus.switches & SWITCH_SERVICE) + player_data[0] |= 0x40; + + switch (AMMediaboard::GetGameType()) + { + // Controller configuration for F-Zero AX (DX) + case FZeroAX: + if (i == 0) + { + if (m_fzdx_seatbelt) + { + player_data[0] |= 0x01; + } + + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Boost + if (PadStatus.button & PAD_BUTTON_A) + player_data[0] |= 0x02; + // View Change 1 + if (PadStatus.button & PAD_BUTTON_RIGHT) + player_data[0] |= 0x20; + // View Change 2 + if (PadStatus.button & PAD_BUTTON_LEFT) + player_data[0] |= 0x10; + // View Change 3 + if (PadStatus.button & PAD_BUTTON_UP) + player_data[0] |= 0x08; + // View Change 4 + if (PadStatus.button & PAD_BUTTON_DOWN) + player_data[0] |= 0x04; + player_data[1] = m_rx_reply & 0xF0; + } + else if (i == 1) + { + // Paddle left + if (PadStatus.button & PAD_BUTTON_X) + player_data[0] |= 0x20; + // Paddle right + if (PadStatus.button & PAD_BUTTON_Y) + player_data[0] |= 0x10; + + if (m_fzdx_motion_stop) + { + player_data[0] |= 2; + } + if (m_fzdx_sensor_right) + { + player_data[0] |= 4; + } + if (m_fzdx_sensor_left) + { + player_data[0] |= 8; + } + + player_data[1] = m_rx_reply << 4; + } + break; + // Controller configuration for F-Zero AX MonsterRide + case FZeroAXMonster: + if (i == 0) + { + if (m_fzcc_sensor) + { + player_data[0] |= 0x01; + } + + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Boost + if (PadStatus.button & PAD_BUTTON_A) + player_data[0] |= 0x02; + // View Change 1 + if (PadStatus.button & PAD_BUTTON_RIGHT) + player_data[0] |= 0x20; + // View Change 2 + if (PadStatus.button & PAD_BUTTON_LEFT) + player_data[0] |= 0x10; + // View Change 3 + if (PadStatus.button & PAD_BUTTON_UP) + player_data[0] |= 0x08; + // View Change 4 + if (PadStatus.button & PAD_BUTTON_DOWN) + player_data[0] |= 0x04; + + player_data[1] = m_rx_reply & 0xF0; + } + else if (i == 1) + { + // Paddle left + if (PadStatus.button & PAD_BUTTON_X) + player_data[0] |= 0x20; + // Paddle right + if (PadStatus.button & PAD_BUTTON_Y) + player_data[0] |= 0x10; + + if (m_fzcc_seatbelt) + { + player_data[0] |= 2; + } + if (m_fzcc_service) + { + player_data[0] |= 4; + } + if (m_fzcc_emergency) + { + player_data[0] |= 8; + } + } + break; + // Controller configuration for Virtua Striker 3 games + case VirtuaStriker3: + PadStatus = Pad::GetStatus(i); + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Shoot + if (PadStatus.button & PAD_BUTTON_B) + player_data[0] |= 0x01; + // Short Pass + if (PadStatus.button & PAD_BUTTON_A) + player_data[1] |= 0x80; + // Long Pass + if (PadStatus.button & PAD_BUTTON_X) + player_data[0] |= 0x02; + // Left + if (PadStatus.button & PAD_BUTTON_LEFT) + player_data[0] |= 0x08; + // Up + if (PadStatus.button & PAD_BUTTON_UP) + player_data[0] |= 0x20; + // Right + if (PadStatus.button & PAD_BUTTON_RIGHT) + player_data[0] |= 0x04; + // Down + if (PadStatus.button & PAD_BUTTON_DOWN) + player_data[0] |= 0x10; + break; + // Controller configuration for Virtua Striker 4 games + case VirtuaStriker4: + case VirtuaStriker4_2006: + { + PadStatus = Pad::GetStatus(i); + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Long Pass + if (PadStatus.button & PAD_BUTTON_X) + player_data[0] |= 0x01; + // Short Pass + if (PadStatus.button & PAD_BUTTON_A) + player_data[0] |= 0x02; + // Shoot + if (PadStatus.button & PAD_BUTTON_B) + player_data[1] |= 0x80; + // Dash + if (PadStatus.button & PAD_BUTTON_Y) + player_data[1] |= 0x40; + // Tactics (U) + if (PadStatus.button & PAD_BUTTON_LEFT) + player_data[0] |= 0x20; + // Tactics (M) + if (PadStatus.button & PAD_BUTTON_UP) + player_data[0] |= 0x08; + // Tactics (D) + if (PadStatus.button & PAD_BUTTON_RIGHT) + player_data[0] |= 0x04; + + if (i == 0) + { + player_data[0] |= 0x10; // IC-Card Switch ON + + // IC-Card Lock + if (PadStatus.button & PAD_BUTTON_DOWN) + player_data[1] |= 0x20; + } + } + break; + // Controller configuration for Gekitou Pro Yakyuu + case GekitouProYakyuu: + PadStatus = Pad::GetStatus(i); + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // A + if (PadStatus.button & PAD_BUTTON_B) + player_data[0] |= 0x01; + // B + if (PadStatus.button & PAD_BUTTON_A) + player_data[0] |= 0x02; + // Gekitou + if (PadStatus.button & PAD_TRIGGER_L) + player_data[1] |= 0x80; + // Left + if (PadStatus.button & PAD_BUTTON_LEFT) + player_data[0] |= 0x08; + // Up + if (PadStatus.button & PAD_BUTTON_UP) + player_data[0] |= 0x20; + // Right + if (PadStatus.button & PAD_BUTTON_RIGHT) + player_data[0] |= 0x04; + // Down + if (PadStatus.button & PAD_BUTTON_DOWN) + player_data[0] |= 0x10; + break; + // Controller configuration for Mario Kart and other games + default: + case MarioKartGP: + case MarioKartGP2: + { + // Start + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Item button + if (PadStatus.button & PAD_BUTTON_A) + player_data[1] |= 0x20; + // VS-Cancel button + if (PadStatus.button & PAD_BUTTON_B) + player_data[1] |= 0x02; + } + break; + case KeyOfAvalon: + { + // Debug On + if (PadStatus.button & PAD_BUTTON_START) + player_data[0] |= 0x80; + // Switch 1 + if (PadStatus.button & PAD_BUTTON_A) + player_data[0] |= 0x04; + // Switch 2 + if (PadStatus.button & PAD_BUTTON_B) + player_data[0] |= 0x08; + // Toggle inserted card + if (PadStatus.button & PAD_TRIGGER_L) + { + m_ic_card_status ^= ICCARDStatus::NoCard; + } + } + break; + } + + for (u32 j = 0; j < player_byte_count; ++j) + message.AddData(player_data[j]); + } + break; + } + case JVSIOCommand::CoinInput: + { + const u32 slots = *jvs_io++; + message.AddData(StatusOkay); + for (u32 i = 0; i < slots; i++) + { + GCPadStatus PadStatus = Pad::GetStatus(i); + if ((PadStatus.switches & SWITCH_COIN) && !m_coin_pressed[i]) + { + m_coin[i]++; + } + m_coin_pressed[i] = PadStatus.switches & SWITCH_COIN; + message.AddData((m_coin[i] >> 8) & 0x3f); + message.AddData(m_coin[i] & 0xff); + } + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x21, CoinInput: {}", slots); + break; + } + case JVSIOCommand::AnalogInput: + { + message.AddData(StatusOkay); + + const u32 analogs = *jvs_io++; + GCPadStatus PadStatus = Pad::GetStatus(0); + + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x22, AnalogInput: {}", + analogs); + + switch (AMMediaboard::GetGameType()) + { + case FZeroAX: + case FZeroAXMonster: + // Steering + if (m_motor_init == 1) + { + if (m_motor_force_y > 0) + { + message.AddData(0x80 - (m_motor_force_y >> 8)); + } + else + { + message.AddData((m_motor_force_y >> 8)); + } + message.AddData((u8)0); + + message.AddData(PadStatus.stickY); + message.AddData((u8)0); + } + else + { + // The center for the Y axis is expected to be 78h this adjusts that + message.AddData(PadStatus.stickX - 12); + message.AddData((u8)0); + + message.AddData(PadStatus.stickY); + message.AddData((u8)0); + } + + // Unused + message.AddData((u8)0); + message.AddData((u8)0); + message.AddData((u8)0); + message.AddData((u8)0); + + // Gas + message.AddData(PadStatus.triggerRight); + message.AddData((u8)0); + + // Brake + message.AddData(PadStatus.triggerLeft); + message.AddData((u8)0); + + message.AddData((u8)0x80); // Motion Stop + message.AddData((u8)0); + + message.AddData((u8)0); + message.AddData((u8)0); + + break; + case VirtuaStriker4: + case VirtuaStriker4_2006: + { + message.AddData(-PadStatus.stickY); + message.AddData((u8)0); + message.AddData(PadStatus.stickX); + message.AddData((u8)0); + + PadStatus = Pad::GetStatus(1); + + message.AddData(-PadStatus.stickY); + message.AddData((u8)0); + message.AddData(PadStatus.stickX); + message.AddData((u8)0); + } + break; + default: + case MarioKartGP: + case MarioKartGP2: + // Steering + message.AddData(PadStatus.stickX); + message.AddData((u8)0); + + // Gas + message.AddData(PadStatus.triggerRight); + message.AddData((u8)0); + + // Brake + message.AddData(PadStatus.triggerLeft); + message.AddData((u8)0); + break; + } + break; + } + case JVSIOCommand::PositionInput: + { + const u32 channel = *jvs_io++; + + const GCPadStatus PadStatus = Pad::GetStatus(0); + + if (PadStatus.button & PAD_TRIGGER_R) + { + // Tap at center of screen (~320,240) + message.AddData((void*)"\x01\x00\x8C\x01\x95", + 5); // X=320 (0x0140), Y=240 (0x00F0) + } + else + { + message.AddData((void*)"\x01\xFF\xFF\xFF\xFF", 5); + } + + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x25, PositionInput:{}", + channel); + break; + } + case JVSIOCommand::CoinSubOutput: + { + const u32 slot = *jvs_io++; + const u8 coinh = *jvs_io++; + const u8 coinl = *jvs_io++; + + m_coin[slot] -= (coinh << 8) | coinl; + + message.AddData(StatusOkay); + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x30, CoinSubOutput: {}", slot); + break; + } + case JVSIOCommand::GeneralDriverOutput: + { + const u32 bytes = *jvs_io++; + + if (bytes) + { + message.AddData(StatusOkay); + + // The lamps are controlled via this + if (AMMediaboard::GetGameType() == MarioKartGP) + { + const u32 status = *jvs_io++; + if (status & 4) + { + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 32, Item Button ON"); + } + else + { + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 32, Item Button OFF"); + } + if (status & 8) + { + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 32, Cancel Button ON"); + } + else + { + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 32, Cancel Button OFF"); + } + break; + } + + Common::UniqueBuffer buf(bytes); + + for (u32 i = 0; i < bytes; ++i) + { + buf[i] = *jvs_io++; + } + + INFO_LOG_FMT( + SERIALINTERFACE_JVSIO, + "JVS-IO: Command 0x32, GPO: {:02x} {:02x} {} {:02x}{:02x}{:02x} ({:02x})", + delay, m_rx_reply, bytes, buf[0], buf[1], buf[2], + Common::swap16(*reinterpret_cast(&buf[1])) >> 2); + + // Handling of the motion seat used in F-Zero AXs DX version + switch (Common::swap16(*reinterpret_cast(&buf[1])) >> 2) + { + case 0x70: + delay++; + if ((delay % 10) == 0) + { + m_rx_reply = 0xFB; + } + break; + case 0xF0: + m_rx_reply = 0xF0; + break; + default: + case 0xA0: + case 0x60: + break; + } + } + break; + } + case JVSIOCommand::CoinAddOutput: + { + const u32 slot = *jvs_io++; + const u8 coinh = *jvs_io++; + const u8 coinl = *jvs_io++; + + m_coin[slot] += (coinh << 8) | coinl; + + message.AddData(StatusOkay); + DEBUG_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0x35, CoinAddOutput: {}", slot); + break; + } + case JVSIOCommand::NAMCOCommand: + { + const u32 namco_command = *jvs_io++; + + if (namco_command == 0x18) + { // ID check + jvs_io += 4; + message.AddData(StatusOkay); + message.AddData(0xff); + } + else + { + message.AddData(StatusOkay); + ERROR_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Unknown:{:02x}", namco_command); + } + break; + } + case JVSIOCommand::Reset: + if (*jvs_io++ == 0xD9) + { + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0xF0, Reset"); + delay = 0; + m_wheel_init = 0; + m_ic_card_state = 0x20; + } + message.AddData(StatusOkay); + + dip_switch_1 |= 1; + break; + case JVSIOCommand::SetAddress: + node = *jvs_io++; + NOTICE_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Command 0xF1, SetAddress: node={}", + node); + message.AddData(node == 1); + dip_switch_1 &= ~1; + break; + default: + ERROR_LOG_FMT(SERIALINTERFACE_JVSIO, "JVS-IO: Unhandled: node={}, command={:02x}", + node, jvsio_command); + break; + } + } + + message.End(); + + data_out[data_offset++] = gcam_command; + + const u8* buf = message.m_msg; + const u32 len = message.m_ptr; + data_out[data_offset++] = len; + + for (u32 i = 0; i < len; ++i) + data_out[data_offset++] = buf[i]; + + data_in += frame[0] + 1; + break; + } + case GCAMCommand::Unknown_60: + NOTICE_LOG_FMT(SERIALINTERFACE_AMBB, "GC-AM: Command 0x60, {:02x} {:02x} {:02x}", + data_in[0], data_in[1], data_in[2]); + data_in += data_in[0] + 1; + break; + default: + ERROR_LOG_FMT(SERIALINTERFACE_AMBB, + "GC-AM: Command {:02x} (unknown) {:02x} {:02x} {:02x} {:02x} {:02x}", + gcam_command, data_in[0], data_in[1], data_in[2], data_in[3], data_in[4]); + break; + } + } + + memset(buffer, 0, buffer_length); + + data_in = buffer; + data_out[1] = data_offset - 2; + checksum = 0; + char logptr[1024]; + char* log = logptr; + + for (int i = 0; i < 0x7F; ++i) + { + checksum += data_in[i] = data_out[i]; + log += sprintf(log, "%02X", data_in[i]); + } + data_in[0x7f] = ~checksum; + DEBUG_LOG_FMT(SERIALINTERFACE_AMBB, "Command send back: {}", logptr); + + swap_buffers(buffer, &buffer_length); + + buffer_position = buffer_length; + break; + } + default: + { + ERROR_LOG_FMT(SERIALINTERFACE, "Unknown SI command (0x{:08x})", (u32)command); + PanicAlertFmt("SI: Unknown command"); + buffer_position = buffer_length; + } + break; + } + } + + return buffer_position; +} + +u32 CSIDevice_AMBaseboard::MapPadStatus(const GCPadStatus& pad_status) +{ + // Thankfully changing mode does not change the high bits ;) + u32 hi = 0; + hi = pad_status.stickY; + hi |= pad_status.stickX << 8; + hi |= (pad_status.button | PAD_USE_ORIGIN) << 16; + return hi; +} + +CSIDevice_AMBaseboard::EButtonCombo +CSIDevice_AMBaseboard::HandleButtonCombos(const GCPadStatus& pad_status) +{ + // Keep track of the special button combos (embedded in controller hardware... :( ) + EButtonCombo temp_combo; + if ((pad_status.button & 0xff00) == (PAD_BUTTON_Y | PAD_BUTTON_X | PAD_BUTTON_START)) + temp_combo = COMBO_ORIGIN; + else if ((pad_status.button & 0xff00) == (PAD_BUTTON_B | PAD_BUTTON_X | PAD_BUTTON_START)) + temp_combo = COMBO_RESET; + else + temp_combo = COMBO_NONE; + + if (temp_combo != m_last_button_combo) + { + m_last_button_combo = temp_combo; + if (m_last_button_combo != COMBO_NONE) + m_timer_button_combo_start = m_system.GetCoreTiming().GetTicks(); + } + + if (m_last_button_combo != COMBO_NONE) + { + const u64 current_time = m_system.GetCoreTiming().GetTicks(); + const u32 ticks_per_second = m_system.GetSystemTimers().GetTicksPerSecond(); + if (u32(current_time - m_timer_button_combo_start) > ticks_per_second * 3) + { + if (m_last_button_combo == COMBO_RESET) + { + INFO_LOG_FMT(SERIALINTERFACE, "PAD - COMBO_RESET"); + m_system.GetProcessorInterface().ResetButton_Tap(); + } + else if (m_last_button_combo == COMBO_ORIGIN) + { + INFO_LOG_FMT(SERIALINTERFACE, "PAD - COMBO_ORIGIN"); + SetOrigin(pad_status); + } + + m_last_button_combo = COMBO_NONE; + return temp_combo; + } + } + + return COMBO_NONE; +} + +// GetData + +// Return true on new data (max 7 Bytes and 6 bits ;) +// [00?SYXBA] [1LRZUDRL] [x] [y] [cx] [cy] [l] [r] +// |\_ ERR_LATCH (error latched - check SISR) +// |_ ERR_STATUS (error on last GetData or SendCmd?) +DataResponse CSIDevice_AMBaseboard::GetData(u32& hi, u32& low) +{ + GCPadStatus pad_status = GetPadStatus(); + + if (!pad_status.isConnected) + return DataResponse::ErrorNoResponse; + + if (HandleButtonCombos(pad_status) == COMBO_ORIGIN) + pad_status.button |= PAD_GET_ORIGIN; + + hi = MapPadStatus(pad_status); + + // Low bits are packed differently per mode + if (m_mode == 0 || m_mode == 5 || m_mode == 6 || m_mode == 7) + { + low = (pad_status.analogB >> 4); // Top 4 bits + low |= ((pad_status.analogA >> 4) << 4); // Top 4 bits + low |= ((pad_status.triggerRight >> 4) << 8); // Top 4 bits + low |= ((pad_status.triggerLeft >> 4) << 12); // Top 4 bits + low |= ((pad_status.substickY) << 16); // All 8 bits + low |= ((pad_status.substickX) << 24); // All 8 bits + } + else if (m_mode == 1) + { + low = (pad_status.analogB >> 4); // Top 4 bits + low |= ((pad_status.analogA >> 4) << 4); // Top 4 bits + low |= (pad_status.triggerRight << 8); // All 8 bits + low |= (pad_status.triggerLeft << 16); // All 8 bits + low |= ((pad_status.substickY >> 4) << 24); // Top 4 bits + low |= ((pad_status.substickX >> 4) << 28); // Top 4 bits + } + else if (m_mode == 2) + { + low = pad_status.analogB; // All 8 bits + low |= pad_status.analogA << 8; // All 8 bits + low |= ((pad_status.triggerRight >> 4) << 16); // Top 4 bits + low |= ((pad_status.triggerLeft >> 4) << 20); // Top 4 bits + low |= ((pad_status.substickY >> 4) << 24); // Top 4 bits + low |= ((pad_status.substickX >> 4) << 28); // Top 4 bits + } + else if (m_mode == 3) + { + // Analog A/B are always 0 + low = pad_status.triggerRight; // All 8 bits + low |= (pad_status.triggerLeft << 8); // All 8 bits + low |= (pad_status.substickY << 16); // All 8 bits + low |= (pad_status.substickX << 24); // All 8 bits + } + else if (m_mode == 4) + { + low = pad_status.analogB; // All 8 bits + low |= pad_status.analogA << 8; // All 8 bits + // triggerLeft/Right are always 0 + low |= pad_status.substickY << 16; // All 8 bits + low |= pad_status.substickX << 24; // All 8 bits + } + + return DataResponse::Success; +} + +void CSIDevice_AMBaseboard::SendCommand(u32 command, u8 poll) +{ + UCommand controller_command(command); + + if (static_cast(controller_command.command) == EDirectCommands::CMD_WRITE) + { + const u32 type = controller_command.parameter1; // 0 = stop, 1 = rumble, 2 = stop hard + + // get the correct pad number that should rumble locally when using netplay + const int pad_num = NetPlay_InGamePadToLocalPad(m_device_number); + + if (pad_num < 4) + { + const SIDevices device = m_system.GetSerialInterface().GetDeviceType(pad_num); + if (type == 1) + CSIDevice_GCController::Rumble(pad_num, 1.0, device); + else + CSIDevice_GCController::Rumble(pad_num, 0.0, device); + } + + if (poll == 0) + { + m_mode = controller_command.parameter2; + INFO_LOG_FMT(SERIALINTERFACE, "PAD {} set to mode {}", m_device_number, m_mode); + } + } + else if (controller_command.command != 0x00) + { + // Costis sent 0x00 in some demos :) + ERROR_LOG_FMT(SERIALINTERFACE, "Unknown direct command ({:#x})", command); + PanicAlertFmt("SI: Unknown direct command"); + } +} + +void CSIDevice_AMBaseboard::SetOrigin(const GCPadStatus& pad_status) +{ + m_origin.origin_stick_x = pad_status.stickX; + m_origin.origin_stick_y = pad_status.stickY; + m_origin.substick_x = pad_status.substickX; + m_origin.substick_y = pad_status.substickY; + m_origin.trigger_left = pad_status.triggerLeft; + m_origin.trigger_right = pad_status.triggerRight; +} + +void CSIDevice_AMBaseboard::HandleMoviePadStatus(Movie::MovieManager& movie, int device_number, + GCPadStatus* pad_status) +{ + movie.SetPolledDevice(); + if (NetPlay_GetInput(device_number, pad_status)) + { + } + else if (movie.IsPlayingInput()) + { + movie.PlayController(pad_status, device_number); + movie.InputUpdate(); + } + else if (movie.IsRecordingInput()) + { + movie.RecordInput(pad_status, device_number); + movie.InputUpdate(); + } + else + { + movie.CheckPadStatus(pad_status, device_number); + } +} + +GCPadStatus CSIDevice_AMBaseboard::GetPadStatus() +{ + GCPadStatus pad_status = {}; + + // For netplay, the local controllers are polled in GetNetPads(), and + // the remote controllers receive their status there as well + if (!NetPlay::IsNetPlayRunning()) + { + pad_status = Pad::GetStatus(m_device_number); + } + + // Our GCAdapter code sets PAD_GET_ORIGIN when a new device has been connected. + // Watch for this to calibrate real controllers on connection. + if (pad_status.button & PAD_GET_ORIGIN) + SetOrigin(pad_status); + + return pad_status; +} + +} // namespace SerialInterface diff --git a/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.h b/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.h new file mode 100644 index 0000000000..82d46b48ea --- /dev/null +++ b/Source/Core/Core/HW/SI/SI_DeviceAMBaseboard.h @@ -0,0 +1,309 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "Core/HW/GCPad.h" +#include "Core/HW/SI/SI_Device.h" +#include "InputCommon/GCPadStatus.h" + +namespace Movie +{ +class MovieManager; +} + +namespace SerialInterface +{ + +// "JAMMA Video Standard" I/O +class JVSIOMessage +{ +public: + u32 m_ptr; + u32 m_last_start; + u32 m_csum; + u8 m_msg[0x80]; + + JVSIOMessage(); + void Start(int node); + void AddData(const u8* dst, size_t len, int sync); + void AddData(const void* data, size_t len); + void AddData(const char* data); + void AddData(u32 n); + void End(); +}; + +// Triforce (GC-AM) baseboard +class CSIDevice_AMBaseboard : public ISIDevice +{ +private: + enum BaseBoardCommand + { + GCAM_Reset = 0x00, + GCAM_Command = 0x70, + }; + + enum GCAMCommand + { + StatusSwitches = 0x10, + SerialNumber = 0x11, + Unknown_12 = 0x12, + Unknown_14 = 0x14, + FirmVersion = 0x15, + FPGAVersion = 0x16, + RegionSettings = 0x1F, + + Unknown_21 = 0x21, + Unknown_22 = 0x22, + Unknown_23 = 0x23, + Unknown_24 = 0x24, + + SerialA = 0x31, + SerialB = 0x32, + + JVSIOA = 0x40, + JVSIOB = 0x41, + + Unknown_60 = 0x60, + }; + + enum JVSIOCommand + { + IOID = 0x10, + CommandRevision = 0x11, + JVRevision = 0x12, + CommunicationVersion = 0x13, + CheckFunctionality = 0x14, + MainID = 0x15, + + SwitchesInput = 0x20, + CoinInput = 0x21, + AnalogInput = 0x22, + RotaryInput = 0x23, + KeyCodeInput = 0x24, + PositionInput = 0x25, + GeneralSwitchInput = 0x26, + + PayoutRemain = 0x2E, + Retrans = 0x2F, + CoinSubOutput = 0x30, + PayoutAddOutput = 0x31, + GeneralDriverOutput = 0x32, + AnalogOutput = 0x33, + CharacterOutput = 0x34, + CoinAddOutput = 0x35, + PayoutSubOutput = 0x36, + GeneralDriverOutput2 = 0x37, + GeneralDriverOutput3 = 0x38, + + NAMCOCommand = 0x70, + + Reset = 0xF0, + SetAddress = 0xF1, + ChangeComm = 0xF2, + }; + + enum JVSIOStatusCode + { + StatusOkay = 1, + UnsupportedCommand = 2, + ChecksumError = 3, + AcknowledgeOverflow = 4, + }; + + enum CARDCommand + { + Init = 0x10, + GetState = 0x20, + Read = 0x33, + IsPresent = 0x40, + Write = 0x53, + SetPrintParam = 0x78, + RegisterFont = 0x7A, + WriteInfo = 0x7C, + Erase = 0x7D, + Eject = 0x80, + Clean = 0xA0, + Load = 0xB0, + SetShutter = 0xD0, + }; + + enum ICCARDCommand + { + GetStatus = 0x10, + SetBaudrate = 0x11, + FieldOn = 0x14, + FieldOff = 0x15, + InsertCheck = 0x20, + AntiCollision = 0x21, + SelectCard = 0x22, + ReadPage = 0x24, + WritePage = 0x25, + DecreaseUseCount = 0x26, + ReadUseCount = 0x33, + ReadPages = 0x34, + WritePages = 0x35, + }; + + enum ICCARDStatus + { + Okay = 0, + NoCard = 0x8000, + Unknown = 0x800E, + BadCard = 0xFFFF, + }; + + enum CDReaderCommand + { + ShutterAuto = 0x61, + BootVersion = 0x62, + SensLock = 0x63, + SensCard = 0x65, + FirmwareUpdate = 0x66, + ShutterGet = 0x67, + CameraCheck = 0x68, + ShutterCard = 0x69, + ProgramChecksum = 0x6B, + BootChecksum = 0x6D, + ShutterLoad = 0x6F, + ReadCard = 0x72, + ShutterSave = 0x73, + SelfTest = 0x74, + ProgramVersion = 0x76, + }; + + union ICCommand + { + u8 data[81 + 4 + 4 + 4]; + + struct + { + u32 pktcmd : 8; + u32 pktlen : 8; + u32 fixed : 8; + u32 command : 8; + u32 flag : 8; + u32 length : 8; + u32 status : 16; + + u8 extdata[81]; + u32 extlen; + }; + }; + + u16 m_coin[2]; + u32 m_coin_pressed[2]; + + u8 m_ic_card_data[2048]; + u16 m_ic_card_state; + + u16 m_ic_card_status; + u16 m_ic_card_session; + u8 m_ic_write_buffer[512]; + u32 m_ic_write_offset; + u32 m_ic_write_size; + + u8 m_card_memory[0xD0]; + u8 m_card_read_packet[0xDB]; + u8 m_card_buffer[0x100]; + u32 m_card_memory_size; + bool m_card_is_inserted; + u32 m_card_command; + u32 m_card_clean; + u32 m_card_write_length; + u32 m_card_wrote; + u32 m_card_read_length; + u32 m_card_read; + u32 m_card_bit; + bool m_card_shutter; + u32 m_card_state_call_count; + u8 m_card_offset; + + u32 m_wheel_init; + + u32 m_motor_init; + u8 m_motor_reply[64]; + s16 m_motor_force_y; + + // F-Zero AX (DX) + bool m_fzdx_seatbelt; + bool m_fzdx_motion_stop; + bool m_fzdx_sensor_right; + bool m_fzdx_sensor_left; + u8 m_rx_reply; + + // F-Zero AX (CyCraft) + bool m_fzcc_seatbelt; + bool m_fzcc_sensor; + bool m_fzcc_emergency; + bool m_fzcc_service; + + void ICCardSendReply(ICCommand* iccommand, u8* buffer, u32* length); + +protected: + struct SOrigin + { + u16 button; + u8 origin_stick_x; + u8 origin_stick_y; + u8 substick_x; + u8 substick_y; + u8 trigger_left; + u8 trigger_right; + u8 unk_4; + u8 unk_5; + }; + + enum EButtonCombo + { + COMBO_NONE = 0, + COMBO_ORIGIN, + COMBO_RESET + }; + + // struct to compare input against + // Set on connection to perfect neutral values + // (standard pad only) Set on button combo to current input state + SOrigin m_origin = {}; + + // PADAnalogMode + // Dunno if we need to do this, game/lib should set it? + u8 m_mode = 0x3; + + // Timer to track special button combos: + // y, X, start for 3 seconds updates origin with current status + // Technically, the above is only on standard pad, wavebird does not support it for example + // b, x, start for 3 seconds triggers reset (PI reset button interrupt) + u64 m_timer_button_combo_start = 0; + // Type of button combo from the last/current poll + EButtonCombo m_last_button_combo = COMBO_NONE; + +public: + // constructor + CSIDevice_AMBaseboard(Core::System& system, SIDevices device, int device_number); + + // run the SI Buffer + int RunBuffer(u8* buffer, int request_length) override; + + // return true on new data + DataResponse GetData(u32& hi, u32& low) override; + + // send a command directly + void SendCommand(u32 command, u8 poll) override; + + virtual GCPadStatus GetPadStatus(); + virtual u32 MapPadStatus(const GCPadStatus& pad_status); + virtual EButtonCombo HandleButtonCombos(const GCPadStatus& pad_status); + + static void HandleMoviePadStatus(Movie::MovieManager& movie, int device_number, + GCPadStatus* pad_status); + + // Send and Receive pad input from network + static bool NetPlay_GetInput(int pad_num, GCPadStatus* status); + static int NetPlay_InGamePadToLocalPad(int pad_num); + +protected: + void SetOrigin(const GCPadStatus& pad_status); +}; + +} // namespace SerialInterface diff --git a/Source/Core/Core/HW/SI/SI_DeviceGCController.h b/Source/Core/Core/HW/SI/SI_DeviceGCController.h index 19b5bdb14b..f4dc5768c9 100644 --- a/Source/Core/Core/HW/SI/SI_DeviceGCController.h +++ b/Source/Core/Core/HW/SI/SI_DeviceGCController.h @@ -3,8 +3,11 @@ #pragma once +#include #include +#include "Common/CommonTypes.h" +#include "Common/Flag.h" #include "Core/HW/GCPad.h" #include "Core/HW/SI/SI_Device.h" #include "InputCommon/GCPadStatus.h" diff --git a/Source/Core/Core/HW/VideoInterface.cpp b/Source/Core/Core/HW/VideoInterface.cpp index af1ebb7bd1..3d46a345ee 100644 --- a/Source/Core/Core/HW/VideoInterface.cpp +++ b/Source/Core/Core/HW/VideoInterface.cpp @@ -163,7 +163,9 @@ void VideoInterfaceManager::Preset(bool _bNTSC) // Say component cable is plugged m_dtv_status.component_plugged = Config::Get(Config::SYSCONF_PROGRESSIVE_SCAN); - m_dtv_status.ntsc_j = region == DiscIO::Region::NTSC_J; + + // Triforce IPL requires the DTV NTSC-J flag to be set. + m_dtv_status.ntsc_j = m_system.IsTriforce() || region == DiscIO::Region::NTSC_J; m_fb_width.Hex = 0; m_border_hblank.Hex = 0; diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index cb6ad2293e..028799d56f 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -52,6 +52,7 @@ #include "Core/HW/GCPad.h" #include "Core/HW/SI/SI.h" #include "Core/HW/SI/SI_Device.h" +#include "Core/HW/SI/SI_DeviceAMBaseboard.h" #include "Core/HW/SI/SI_DeviceGCController.h" #include "Core/HW/Sram.h" #include "Core/HW/WiiSave.h" @@ -1883,6 +1884,8 @@ void NetPlayClient::UpdateDevices() auto& si = Core::System::GetInstance().GetSerialInterface(); for (auto player_id : m_pad_map) { + const SerialInterface::SIDevices si_device = Config::Get(Config::GetInfoForSIDevice(local_pad)); + if (m_gba_config[pad].enabled && player_id > 0) { si.ChangeDevice(SerialInterface::SIDEVICE_GC_GBA_EMULATED, pad); @@ -1890,8 +1893,6 @@ void NetPlayClient::UpdateDevices() else if (player_id == m_local_player->pid) { // Use local controller types for local controllers if they are compatible - const SerialInterface::SIDevices si_device = - Config::Get(Config::GetInfoForSIDevice(local_pad)); if (SerialInterface::SIDevice_IsGCController(si_device)) { si.ChangeDevice(si_device, pad); @@ -1909,7 +1910,8 @@ void NetPlayClient::UpdateDevices() } else if (player_id > 0) { - si.ChangeDevice(SerialInterface::SIDEVICE_GC_CONTROLLER, pad); + if (si_device != SerialInterface::SIDEVICE_AM_BASEBOARD) + si.ChangeDevice(SerialInterface::SIDEVICE_GC_CONTROLLER, pad); } else { @@ -2772,6 +2774,16 @@ bool SerialInterface::CSIDevice_GCController::NetPlay_GetInput(int pad_num, GCPa return false; } +bool SerialInterface::CSIDevice_AMBaseboard::NetPlay_GetInput(int pad_num, GCPadStatus* status) +{ + std::lock_guard lk(NetPlay::crit_netplay_client); + + if (NetPlay::netplay_client) + return NetPlay::netplay_client->GetNetPads(pad_num, NetPlay::s_si_poll_batching, status); + + return false; +} + bool NetPlay::NetPlay_GetWiimoteData(const std::span& entries) { std::lock_guard lk(crit_netplay_client); @@ -2835,12 +2847,21 @@ u64 ExpansionInterface::CEXIIPL::NetPlay_GetEmulatedTime() // called from ---CPU--- thread // return the local pad num that should rumble given a ingame pad num -int SerialInterface::CSIDevice_GCController::NetPlay_InGamePadToLocalPad(int numPAD) +int SerialInterface::CSIDevice_GCController::NetPlay_InGamePadToLocalPad(int pad_num) { std::lock_guard lk(NetPlay::crit_netplay_client); if (NetPlay::netplay_client) - return NetPlay::netplay_client->InGamePadToLocalPad(numPAD); + return NetPlay::netplay_client->InGamePadToLocalPad(pad_num); - return numPAD; + return pad_num; +} +int SerialInterface::CSIDevice_AMBaseboard::NetPlay_InGamePadToLocalPad(int pad_num) +{ + std::lock_guard lk(NetPlay::crit_netplay_client); + + if (NetPlay::netplay_client) + return NetPlay::netplay_client->InGamePadToLocalPad(pad_num); + + return pad_num; } diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index fd71ba9df2..98180a8736 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -95,7 +95,7 @@ struct CompressAndDumpStateArgs static Common::WorkQueueThreadSP s_compress_and_dump_thread; // Don't forget to increase this after doing changes on the savestate system -constexpr u32 STATE_VERSION = 176; // Last changed in PR 13768 +constexpr u32 STATE_VERSION = 177; // Last changed in PR 13844 // Increase this if the StateExtendedHeader definition changes constexpr u32 EXTENDED_HEADER_VERSION = 1; // Last changed in PR 12217 diff --git a/Source/Core/Core/TitleDatabase.cpp b/Source/Core/Core/TitleDatabase.cpp index 523b8b500e..41ad0e0e7e 100644 --- a/Source/Core/Core/TitleDatabase.cpp +++ b/Source/Core/Core/TitleDatabase.cpp @@ -91,32 +91,8 @@ TitleDatabase::TitleDatabase() TitleDatabase::~TitleDatabase() = default; const std::string& TitleDatabase::GetTitleName(const std::string& gametdb_id, - const std::string& triforce_id, DiscIO::Language language) const { - if (triforce_id != "") - { - const Map& map = *m_triforce_title_maps.at(DiscIO::Language::English); - auto it = map.find(triforce_id); - if (it != map.end()) - return it->second; - - // This code has been commented out as there is currently only a english title map, and all - // Triforce games are detected as Japanese. - - // if (language != DiscIO::Language::English) - //{ - // const Map& english_triforce_map = *m_triforce_title_maps.at(DiscIO::Language::English); - // it = english_triforce_map.find(triforce_id); - // if (it != english_triforce_map.end()) - // return it->second; - //} - - // it = m_base_map.find(triforce_id); - // if (it != m_base_map.end()) - // return it->second; - } - auto it = m_user_title_map.find(gametdb_id); if (it != m_user_title_map.end()) return it->second; @@ -149,12 +125,12 @@ const std::string& TitleDatabase::GetChannelName(u64 title_id, DiscIO::Language const std::string id{ {static_cast((title_id >> 24) & 0xff), static_cast((title_id >> 16) & 0xff), static_cast((title_id >> 8) & 0xff), static_cast(title_id & 0xff)}}; - return GetTitleName(id, "", language); + return GetTitleName(id, language); } std::string TitleDatabase::Describe(const std::string& gametdb_id, DiscIO::Language language) const { - const std::string& title_name = GetTitleName(gametdb_id, "", language); + const std::string& title_name = GetTitleName(gametdb_id, language); if (title_name.empty()) return gametdb_id; return fmt::format("{} ({})", title_name, gametdb_id); diff --git a/Source/Core/Core/TitleDatabase.h b/Source/Core/Core/TitleDatabase.h index dff9a56dfe..d1163d80a6 100644 --- a/Source/Core/Core/TitleDatabase.h +++ b/Source/Core/Core/TitleDatabase.h @@ -25,8 +25,7 @@ public: // Get a user friendly title name for a GameTDB ID. // This falls back to returning an empty string if none could be found. - const std::string& GetTitleName(const std::string& gametdb_id, const std::string& triforce_id, - DiscIO::Language language) const; + const std::string& GetTitleName(const std::string& gametdb_id, DiscIO::Language language) const; // Same as above, but takes a title ID instead of a GameTDB ID, and only works for channels. const std::string& GetChannelName(u64 title_id, DiscIO::Language language) const; diff --git a/Source/Core/DiscIO/Enums.h b/Source/Core/DiscIO/Enums.h index 9b5e274de9..62ad9e846a 100644 --- a/Source/Core/DiscIO/Enums.h +++ b/Source/Core/DiscIO/Enums.h @@ -19,7 +19,7 @@ enum class Platform WiiDisc, WiiWAD, ELFOrDOL, - NumberOfPlatforms + NumberOfPlatforms, }; enum class Country @@ -38,7 +38,7 @@ enum class Country Taiwan, World, Unknown, - NumberOfCountries + NumberOfCountries, }; // This numbering matches Nintendo's GameCube/Wii region numbering. @@ -48,7 +48,8 @@ enum class Region NTSC_U = 1, // Mainly North America PAL = 2, // Mainly Europe and Oceania Unknown = 3, // Nintendo uses this to mean region free, but we also use it for unknown regions - NTSC_K = 4 // South Korea (Wii only) + NTSC_K = 4, // South Korea (Wii only) + DEV = 5, // Triforce has no regions }; // Languages 0 - 9 match Nintendo's Wii language numbering. @@ -66,7 +67,7 @@ enum class Language SimplifiedChinese = 7, // Not selectable on any unmodded retail Wii TraditionalChinese = 8, // Not selectable on any unmodded retail Wii Korean = 9, - Unknown + Unknown, }; std::string GetName(Country country, bool translate); diff --git a/Source/Core/DiscIO/Volume.h b/Source/Core/DiscIO/Volume.h index 3f80cd2e3d..3d161811e4 100644 --- a/Source/Core/DiscIO/Volume.h +++ b/Source/Core/DiscIO/Volume.h @@ -96,7 +96,6 @@ public: } virtual std::string GetGameID(const Partition& partition = PARTITION_NONE) const = 0; virtual std::string GetGameTDBID(const Partition& partition = PARTITION_NONE) const = 0; - virtual std::string GetTriforceID() const { return ""; } virtual std::string GetMakerID(const Partition& partition = PARTITION_NONE) const = 0; virtual std::optional GetRevision(const Partition& partition = PARTITION_NONE) const = 0; virtual std::string GetInternalName(const Partition& partition = PARTITION_NONE) const = 0; diff --git a/Source/Core/DiscIO/VolumeDisc.cpp b/Source/Core/DiscIO/VolumeDisc.cpp index 882bd3ac96..a915df1224 100644 --- a/Source/Core/DiscIO/VolumeDisc.cpp +++ b/Source/Core/DiscIO/VolumeDisc.cpp @@ -12,6 +12,7 @@ #include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/Filesystem.h" +#include "DiscIO/VolumeGC.h" namespace DiscIO { @@ -19,6 +20,55 @@ std::string VolumeDisc::GetGameID(const Partition& partition) const { char id[6]; + if (GetVolumeType() == Platform::Triforce) + { + // Triforce games have their Game ID stored in the boot.id file + const BootID* boot_id = static_cast(this)->GetTriforceBootID(); + + // Construct game ID from the BTID + id[0] = 'G'; + + memcpy(id + 1, boot_id->gameId + 2, 2); + + switch (boot_id->regionFlags) + { + default: + case 0x02: // JAPAN + id[3] = 'J'; + break; + case 0x08: // ASIA + id[3] = 'W'; + break; + case 0x0E: // USA + id[3] = 'E'; + break; + case 0x0C: // EXPORT + id[3] = 'P'; + break; + } + + // There only seem to be three different makers here, + // so we can just check the first char to difference between them. + // + // NAMCO CORPORATION, SEGA CORPORATION and Hitmaker co,ltd. + + switch (boot_id->manufacturer[0]) + { + case 'N': // NAMCO CORPORATION + id[4] = '8'; + id[5] = '2'; + break; + default: + case 'H': // Hitmaker co,ltd. + case 'S': // SEGA CORPORATION + id[4] = '6'; + id[5] = 'E'; + break; + } + + return DecodeString(id); + } + if (!Read(0, sizeof(id), reinterpret_cast(id), partition)) return std::string(); @@ -27,8 +77,35 @@ std::string VolumeDisc::GetGameID(const Partition& partition) const Country VolumeDisc::GetCountry(const Partition& partition) const { - // The 0 that we use as a default value is mapped to Country::Unknown and Region::Unknown - const u8 country_byte = ReadSwapped(3, partition).value_or(0); + u8 country_byte = 0; + + if (GetVolumeType() == Platform::Triforce) + { + const BootID* boot_id = static_cast(this)->GetTriforceBootID(); + + switch (boot_id->regionFlags) + { + default: + case 0x02: // JAPAN + country_byte = 'J'; + break; + case 0x08: // ASIA + country_byte = 'W'; + break; + case 0x0E: // USA + country_byte = 'E'; + break; + case 0x0C: // EXPORT + country_byte = 'P'; + break; + } + } + else + { + // The 0 that we use as a default value is mapped to Country::Unknown and Region::Unknown + country_byte = ReadSwapped(3, partition).value_or(0); + } + const Region region = GetRegion(); const std::optional revision = GetRevision(); @@ -51,6 +128,24 @@ std::string VolumeDisc::GetMakerID(const Partition& partition) const { char maker_id[2]; + // Triforce games have their maker stored in the boot.id file as an actual string + if (GetVolumeType() == Platform::Triforce) + { + const BootID* boot_id = static_cast(this)->GetTriforceBootID(); + + switch (boot_id->manufacturer[0]) + { + case 'S': // SEGA CORPORATION + case 'H': // Hitmaker co,ltd + return DecodeString("6E"); + case 'N': // NAMCO CORPORATION + return DecodeString("82"); + default: + break; + } + // Fall back to normal maker from header + } + if (!Read(0x4, sizeof(maker_id), reinterpret_cast(&maker_id), partition)) return std::string(); @@ -66,6 +161,14 @@ std::optional VolumeDisc::GetRevision(const Partition& partition) const std::string VolumeDisc::GetInternalName(const Partition& partition) const { char name[0x60]; + + // Triforce games have their Title stored in the boot.id file + if (GetVolumeType() == Platform::Triforce) + { + const BootID* boot_id = static_cast(this)->GetTriforceBootID(); + return DecodeString(boot_id->gameName); + } + if (!Read(0x20, sizeof(name), reinterpret_cast(&name), partition)) return std::string(); diff --git a/Source/Core/DiscIO/VolumeDisc.h b/Source/Core/DiscIO/VolumeDisc.h index 17c76ae689..f263c40c16 100644 --- a/Source/Core/DiscIO/VolumeDisc.h +++ b/Source/Core/DiscIO/VolumeDisc.h @@ -15,6 +15,30 @@ namespace DiscIO class VolumeDisc : public Volume { public: + struct BootID + { + u32 magic; // 0x00 (Always BTID) + uint32_t unknown0[3]; + uint32_t unknown1[4]; + char mediaboardType[4]; // 0x20 (GCAM for Triforce) + uint32_t unknown2; + uint16_t year; // 0x28 + uint8_t month; // 0x2A + uint8_t day; // 0x2B + uint8_t videoMode; // 0x2C unknown bitmask, resolutions + horizontal/vertical + uint8_t unknown3; + uint8_t type3Compatible; // 0x2E (Type-3 compatible titles have this set to 1) + uint8_t unknown4; + char gameId[8]; // 0x30 + uint32_t regionFlags; // 0x38 + uint32_t unknown6[9]; + char manufacturer[0x20]; // 0x60 + char gameName[0x20]; // 0x80 + char gameExecutable[0x20]; // 0xA0 + char testExecutable[0x20]; // 0xC0 + char creditTypes[8][0x20]; // 0xE0 + }; + std::string GetGameID(const Partition& partition = PARTITION_NONE) const override; Country GetCountry(const Partition& partition = PARTITION_NONE) const override; std::string GetMakerID(const Partition& partition = PARTITION_NONE) const override; diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index ad3d8ec74e..52dacd59a4 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -45,13 +45,11 @@ VolumeGC::VolumeGC(std::unique_ptr reader) std::unique_ptr file_info = tmp_fs->FindFileInfo("boot.id"); if (!file_info) return; - BootID triforce_header; const u64 file_size = ReadFile(*this, PARTITION_NONE, file_info.get(), - reinterpret_cast(&triforce_header), sizeof(BootID)); - if (file_size >= 4 && triforce_header.magic == BTID_MAGIC) + reinterpret_cast(&m_triforce_header), sizeof(BootID)); + if (file_size >= 4 && m_triforce_header.magic == BTID_MAGIC) { m_is_triforce = true; - m_triforce_id = triforce_header.id; } } } @@ -90,16 +88,23 @@ std::string VolumeGC::GetGameTDBID(const Partition& partition) const return GetGameID(partition); } -std::string VolumeGC::GetTriforceID() const -{ - if (m_is_triforce) - return (std::string(m_triforce_id.data(), m_triforce_id.size())); - else - return ""; -} - Region VolumeGC::GetRegion() const { + if (m_is_triforce) + { + switch (m_triforce_header.regionFlags) + { + default: + case 0x02: // JAPAN + case 0x08: // ASIA + return Region::NTSC_J; + case 0x0E: // USA + return Region::NTSC_U; + case 0x0C: // EXPORT + return Region::PAL; + } + } + return RegionCodeToRegion(m_reader->ReadSwapped(0x458)); } @@ -184,6 +189,12 @@ std::array VolumeGC::GetSyncHash() const VolumeGC::ConvertedGCBanner VolumeGC::LoadBannerFile() const { + // There is at least one Triforce game that has an opening.bnr file but from a different game. + if (m_is_triforce) + { + return {}; + } + GCBanner banner_file; const u64 file_size = ReadFile(*this, PARTITION_NONE, "opening.bnr", reinterpret_cast(&banner_file), sizeof(GCBanner)); diff --git a/Source/Core/DiscIO/VolumeGC.h b/Source/Core/DiscIO/VolumeGC.h index ff768c2e2a..2f85a75535 100644 --- a/Source/Core/DiscIO/VolumeGC.h +++ b/Source/Core/DiscIO/VolumeGC.h @@ -34,7 +34,6 @@ public: const Partition& partition = PARTITION_NONE) const override; const FileSystem* GetFileSystem(const Partition& partition = PARTITION_NONE) const override; std::string GetGameTDBID(const Partition& partition = PARTITION_NONE) const override; - std::string GetTriforceID() const override; std::map GetShortNames() const override; std::map GetLongNames() const override; std::map GetShortMakers() const override; @@ -50,9 +49,10 @@ public: DataSizeType GetDataSizeType() const override; u64 GetRawSize() const override; const BlobReader& GetBlobReader() const override; - std::array GetSyncHash() const override; + const BootID* GetTriforceBootID() const { return &m_triforce_header; } + private: static constexpr u32 GC_BANNER_WIDTH = 96; static constexpr u32 GC_BANNER_HEIGHT = 32; @@ -77,13 +77,6 @@ private: // (only one for BNR1 type) }; - struct BootID - { - u32 magic; // "BTID" - u32 padding[11]; - std::array id; - }; - struct ConvertedGCBanner { ConvertedGCBanner(); @@ -112,8 +105,9 @@ private: std::unique_ptr m_reader; + BootID m_triforce_header; + bool m_is_triforce; - std::array m_triforce_id; }; } // namespace DiscIO diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index d030f8821b..7ed0b580e3 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -279,6 +279,7 @@ + @@ -322,6 +323,7 @@ + @@ -971,6 +973,7 @@ + @@ -1017,6 +1020,7 @@ + diff --git a/Source/Core/DolphinQt/Config/GamecubeControllersWidget.cpp b/Source/Core/DolphinQt/Config/GamecubeControllersWidget.cpp index d23db46eb6..f995a67a1a 100644 --- a/Source/Core/DolphinQt/Config/GamecubeControllersWidget.cpp +++ b/Source/Core/DolphinQt/Config/GamecubeControllersWidget.cpp @@ -42,6 +42,7 @@ static constexpr std::array s_gc_types = { #endif SIDeviceName{SerialInterface::SIDEVICE_GC_GBA, _trans("GBA (TCP)")}, SIDeviceName{SerialInterface::SIDEVICE_GC_KEYBOARD, _trans("Keyboard Controller")}, + SIDeviceName{SerialInterface::SIDEVICE_AM_BASEBOARD, _trans("Triforce Baseboard")}, }; static std::optional ToGCMenuIndex(const SerialInterface::SIDevices sidevice) @@ -132,6 +133,7 @@ void GamecubeControllersWidget::OnGCPadConfigure(size_t index) case SerialInterface::SIDEVICE_NONE: case SerialInterface::SIDEVICE_GC_GBA: return; + case SerialInterface::SIDEVICE_AM_BASEBOARD: case SerialInterface::SIDEVICE_GC_CONTROLLER: type = MappingWindow::Type::MAPPING_GCPAD; break; diff --git a/Source/Core/DolphinQt/Config/InfoWidget.cpp b/Source/Core/DolphinQt/Config/InfoWidget.cpp index fb1e3d0112..4ee3f63bd6 100644 --- a/Source/Core/DolphinQt/Config/InfoWidget.cpp +++ b/Source/Core/DolphinQt/Config/InfoWidget.cpp @@ -122,12 +122,6 @@ QGroupBox* InfoWidget::CreateGameDetails() m_game.GetMakerID() + ")"); layout->addRow(tr("Name:"), internal_name); - if (m_game.GetPlatform() == DiscIO::Platform::Triforce) - { - const auto triforce_id_string = QString::fromStdString(m_game.GetTriforceID()); - auto* const triforce_id = CreateValueDisplay(triforce_id_string); - layout->addRow(tr("Triforce ID:"), triforce_id); - } layout->addRow(tr("Game ID:"), game_id); layout->addRow(tr("Country:"), country); layout->addRow(tr("Maker:"), maker); diff --git a/Source/Core/DolphinQt/Config/Mapping/GCPadEmu.cpp b/Source/Core/DolphinQt/Config/Mapping/GCPadEmu.cpp index e530f495a5..7ae4a68083 100644 --- a/Source/Core/DolphinQt/Config/Mapping/GCPadEmu.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/GCPadEmu.cpp @@ -8,6 +8,10 @@ #include "Core/HW/GCPad.h" #include "Core/HW/GCPadEmu.h" +#include "Core/HW/SI/SI.h" +#include "Core/HW/SI/SI_DeviceAMBaseboard.h" + +#include "Core/Config/MainSettings.h" #include "InputCommon/InputConfig.h" @@ -22,8 +26,15 @@ void GCPadEmu::CreateMainLayout() layout->addWidget(CreateGroupBox(tr("Buttons"), Pad::GetGroup(GetPort(), PadGroup::Buttons)), 0, 0); - layout->addWidget(CreateGroupBox(tr("D-Pad"), Pad::GetGroup(GetPort(), PadGroup::DPad)), 1, 0, -1, - 1); + layout->addWidget(CreateGroupBox(tr("D-Pad"), Pad::GetGroup(GetPort(), PadGroup::DPad)), 1, 0); + + if (Config::Get(Config::GetInfoForSIDevice(0)) == + SerialInterface::SIDevices::SIDEVICE_AM_BASEBOARD) + { + layout->addWidget(CreateGroupBox(tr("Triforce"), Pad::GetGroup(GetPort(), PadGroup::Triforce)), + 2, 0); + } + layout->addWidget( CreateGroupBox(tr("Control Stick"), Pad::GetGroup(GetPort(), PadGroup::MainStick)), 0, 1, -1, 1); diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp b/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp index ddb9d39ee6..d2151a0f06 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp @@ -26,6 +26,9 @@ #include "Common/IniFile.h" #include "Common/StringUtil.h" +#include "Core/HW/SI/SI.h" +#include "Core/HW/SI/SI_DeviceAMBaseboard.h" + #include "DolphinQt/Config/Mapping/FreeLookGeneral.h" #include "DolphinQt/Config/Mapping/FreeLookRotation.h" #include "DolphinQt/Config/Mapping/GBAPadEmu.h" @@ -458,8 +461,17 @@ void MappingWindow::SetMappingType(MappingWindow::Type type) case Type::MAPPING_GC_DANCEMAT: case Type::MAPPING_GCPAD: widget = new GCPadEmu(this); - setWindowTitle(tr("GameCube Controller at Port %1").arg(GetPort() + 1)); - AddWidget(tr("GameCube Controller"), widget); + if (Config::Get(Config::GetInfoForSIDevice(GetPort())) == + SerialInterface::SIDevices::SIDEVICE_AM_BASEBOARD) + { + setWindowTitle(tr("Triforce Baseboard at Port %1").arg(GetPort() + 1)); + AddWidget(tr("Triforce Baseboard"), widget); + } + else + { + setWindowTitle(tr("GameCube Controller at Port %1").arg(GetPort() + 1)); + AddWidget(tr("GameCube Controller"), widget); + } break; case Type::MAPPING_GC_MICROPHONE: widget = new GCMicrophone(this); diff --git a/Source/Core/DolphinQt/MainWindow.cpp b/Source/Core/DolphinQt/MainWindow.cpp index 9f25e66ad4..090b085954 100644 --- a/Source/Core/DolphinQt/MainWindow.cpp +++ b/Source/Core/DolphinQt/MainWindow.cpp @@ -250,7 +250,12 @@ MainWindow::MainWindow(Core::System& system, std::unique_ptr boo }); #endif + connect(m_cheats_manager, &CheatsManager::OpenGeneralSettings, this, + &MainWindow::ShowGeneralWindow); + #ifdef USE_RETRO_ACHIEVEMENTS + connect(m_cheats_manager, &CheatsManager::OpenAchievementSettings, this, + &MainWindow::ShowAchievementSettings); connect(m_game_list, &GameList::OpenAchievementSettings, this, &MainWindow::ShowAchievementSettings); #endif // USE_RETRO_ACHIEVEMENTS @@ -511,6 +516,8 @@ void MainWindow::CreateComponents() }); connect(m_breakpoint_widget, &BreakpointWidget::ShowMemory, m_memory_widget, &MemoryWidget::SetAddress); + connect(m_cheats_manager, &CheatsManager::ShowMemory, m_memory_widget, &MemoryWidget::SetAddress); + connect(m_cheats_manager, &CheatsManager::RequestWatch, request_watch); } void MainWindow::ConnectMenuBar() @@ -1151,6 +1158,43 @@ void MainWindow::StartGame(std::unique_ptr&& parameters) if (!NKitWarningDialog::ShowUnlessDisabled()) return; } + + /* + When booting Triforce games, we need to ensure that the hardware is set up correctly. + */ + const auto volume_type = + std::get(parameters->parameters).volume->GetVolumeType(); + + const bool triforce_hardware_sp1 = + Config::Get(Config::MAIN_SERIAL_PORT_1) == ExpansionInterface::EXIDeviceType::Baseboard; + const bool triforce_hardware_port_1 = Config::Get(Config::GetInfoForSIDevice(0)) == + SerialInterface::SIDevices::SIDEVICE_AM_BASEBOARD; + + if (volume_type == DiscIO::Platform::Triforce) + { + if (!triforce_hardware_sp1 || !triforce_hardware_port_1) + { + ModalMessageBox::critical( + this, tr("Error"), + tr("To boot a Triforce game, SP1 and Port 1 must be set to Triforce Baseboard."), + QMessageBox::Ok); + HideRenderWidget(); + return; + } + } + else + { + /* + Some Triforce tools don't include a boot.id file, but they can still be launched. + */ + if (triforce_hardware_sp1 || triforce_hardware_port_1) + { + ModalMessageBox::warning( + this, tr("Warning"), + tr("Non-Triforce games cannot be booted with Triforce hardware attached."), + QMessageBox::Ok); + } + } } // If we're running, only start a new game once we've stopped the last. diff --git a/Source/Core/DolphinQt/MenuBar.cpp b/Source/Core/DolphinQt/MenuBar.cpp index cef4a6a28c..04c1b3eecd 100644 --- a/Source/Core/DolphinQt/MenuBar.cpp +++ b/Source/Core/DolphinQt/MenuBar.cpp @@ -319,6 +319,9 @@ void MenuBar::AddToolsMenu() m_pal_ipl = gc_ipl->addAction(tr("PAL"), this, [this] { emit BootGameCubeIPL(DiscIO::Region::PAL); }); + m_dev_ipl = gc_ipl->addAction(tr("Triforce"), this, + [this] { emit BootGameCubeIPL(DiscIO::Region::Unknown); }); + tools_menu->addAction(tr("Memory Card Manager"), this, [this] { emit ShowMemcardManager(); }); tools_menu->addSeparator(); @@ -1091,6 +1094,7 @@ void MenuBar::UpdateToolsMenu(const Core::State state) m_ntscj_ipl->setEnabled(is_uninitialized && File::Exists(Config::GetBootROMPath(JAP_DIR))); m_ntscu_ipl->setEnabled(is_uninitialized && File::Exists(Config::GetBootROMPath(USA_DIR))); m_pal_ipl->setEnabled(is_uninitialized && File::Exists(Config::GetBootROMPath(EUR_DIR))); + m_dev_ipl->setEnabled(is_uninitialized && File::Exists(Config::GetBootROMPath(DEV_DIR))); m_wad_install_action->setEnabled(is_uninitialized); m_import_backup->setEnabled(is_uninitialized); m_check_nand->setEnabled(is_uninitialized); diff --git a/Source/Core/DolphinQt/MenuBar.h b/Source/Core/DolphinQt/MenuBar.h index 9f51179e2a..2cd56f4e54 100644 --- a/Source/Core/DolphinQt/MenuBar.h +++ b/Source/Core/DolphinQt/MenuBar.h @@ -227,6 +227,7 @@ private: QAction* m_ntscj_ipl; QAction* m_ntscu_ipl; QAction* m_pal_ipl; + QAction* m_dev_ipl; QMenu* m_manage_nand_menu; QAction* m_import_backup; QAction* m_check_nand; diff --git a/Source/Core/DolphinQt/Settings/GameCubePane.cpp b/Source/Core/DolphinQt/Settings/GameCubePane.cpp index 772ade109e..975130531e 100644 --- a/Source/Core/DolphinQt/Settings/GameCubePane.cpp +++ b/Source/Core/DolphinQt/Settings/GameCubePane.cpp @@ -151,6 +151,7 @@ void GameCubePane::CreateWidgets() EXIDeviceType::EthernetIPC, #endif EXIDeviceType::ModemTapServer, + EXIDeviceType::Baseboard, }) { m_slot_combos[ExpansionInterface::Slot::SP1]->addItem(tr(fmt::format("{:n}", device).c_str()), @@ -418,6 +419,10 @@ void GameCubePane::OnConfigPressed(ExpansionInterface::Slot slot) dialog.exec(); return; } + case ExpansionInterface::EXIDeviceType::Baseboard: + { + return; + } default: PanicAlertFmt("Unknown settings pressed for {}", device); return; @@ -452,19 +457,23 @@ bool GameCubePane::SetMemcard(ExpansionInterface::Slot slot, const QString& file const std::string jp_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::NTSC_J); const std::string us_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::NTSC_U); const std::string eu_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::PAL); - const bool raw_path_valid = raw_path == jp_path || raw_path == us_path || raw_path == eu_path; + const std::string dv_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::DEV); + const bool raw_path_valid = + raw_path == jp_path || raw_path == us_path || raw_path == eu_path || raw_path == dv_path; if (!raw_path_valid) { // TODO: We could try to autodetect the card region here and offer automatic renaming. - ModalMessageBox::critical(this, tr("Error"), - tr("The filename %1 does not conform to Dolphin's region code format " - "for memory cards. Please rename this file to either %2, %3, or " - "%4, matching the region of the save files that are on it.") - .arg(QString::fromStdString(PathToFileName(raw_path))) - .arg(QString::fromStdString(PathToFileName(us_path))) - .arg(QString::fromStdString(PathToFileName(eu_path))) - .arg(QString::fromStdString(PathToFileName(jp_path)))); + ModalMessageBox::critical( + this, tr("Error"), + tr("The filename %1 does not conform to Dolphin's region code format " + "for memory cards. Please rename this file to either %2, %3, %4, or " + "%5, matching the region of the save files that are on it.") + .arg(QString::fromStdString(PathToFileName(raw_path))) + .arg(QString::fromStdString(PathToFileName(us_path))) + .arg(QString::fromStdString(PathToFileName(eu_path))) + .arg(QString::fromStdString(PathToFileName(jp_path))) + .arg(QString::fromStdString(PathToFileName(dv_path)))); return false; } @@ -556,8 +565,9 @@ bool GameCubePane::SetGCIFolder(ExpansionInterface::Slot slot, const QString& pa const std::string default_jp_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::NTSC_J); const std::string default_us_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::NTSC_U); const std::string default_eu_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::PAL); - const bool is_default_path = - raw_path == default_jp_path || raw_path == default_us_path || raw_path == default_eu_path; + const std::string default_dv_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::DEV); + const bool is_default_path = raw_path == default_jp_path || raw_path == default_us_path || + raw_path == default_eu_path || raw_path == default_dv_path; bool path_changed; if (is_default_path) @@ -574,7 +584,9 @@ bool GameCubePane::SetGCIFolder(ExpansionInterface::Slot slot, const QString& pa const std::string jp_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::NTSC_J); const std::string us_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::NTSC_U); const std::string eu_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::PAL); - const bool raw_path_valid = raw_path == jp_path || raw_path == us_path || raw_path == eu_path; + const std::string dv_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::DEV); + const bool raw_path_valid = + raw_path == jp_path || raw_path == us_path || raw_path == eu_path || raw_path == dv_path; if (!raw_path_valid) { @@ -582,12 +594,13 @@ bool GameCubePane::SetGCIFolder(ExpansionInterface::Slot slot, const QString& pa ModalMessageBox::critical( this, tr("Error"), tr("The folder %1 does not conform to Dolphin's region code format " - "for GCI folders. Please rename this folder to either %2, %3, or " - "%4, matching the region of the save files that are in it.") + "for GCI folders. Please rename this folder to either %2, %3, %4, or " + "%5, matching the region of the save files that are in it.") .arg(QString::fromStdString(PathToFileName(raw_path))) .arg(QString::fromStdString(PathToFileName(us_path))) .arg(QString::fromStdString(PathToFileName(eu_path))) - .arg(QString::fromStdString(PathToFileName(jp_path)))); + .arg(QString::fromStdString(PathToFileName(jp_path))) + .arg(QString::fromStdString(PathToFileName(dv_path)))); return false; } diff --git a/Source/Core/InputCommon/GCPadStatus.h b/Source/Core/InputCommon/GCPadStatus.h index 74849e5594..b6bbf804b4 100644 --- a/Source/Core/InputCommon/GCPadStatus.h +++ b/Source/Core/InputCommon/GCPadStatus.h @@ -28,6 +28,13 @@ enum PadButton PAD_BUTTON_START = 0x1000, }; +enum SwitchButton +{ + SWITCH_TEST = 0x0001, + SWITCH_SERVICE = 0x0002, + SWITCH_COIN = 0x0004, +}; + struct GCPadStatus { u16 button = 0; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits @@ -39,6 +46,8 @@ struct GCPadStatus u8 triggerRight = 0; // 0 <= triggerRight <= 255 u8 analogA = 0; // 0 <= analogA <= 255 u8 analogB = 0; // 0 <= analogB <= 255 + // Triforce + u8 switches = 0; bool isConnected = true; static const u8 MAIN_STICK_CENTER_X = 0x80; diff --git a/Source/Core/UICommon/GameFile.cpp b/Source/Core/UICommon/GameFile.cpp index 94ddf1f59c..4acab314d1 100644 --- a/Source/Core/UICommon/GameFile.cpp +++ b/Source/Core/UICommon/GameFile.cpp @@ -128,7 +128,6 @@ GameFile::GameFile(std::string path) : m_file_path(std::move(path)) m_internal_name = volume->GetInternalName(); m_game_id = volume->GetGameID(); m_gametdb_id = volume->GetGameTDBID(); - m_triforce_id = volume->GetTriforceID(); m_title_id = volume->GetTitleID().value_or(0); m_maker_id = volume->GetMakerID(); m_revision = volume->GetRevision().value_or(0); @@ -309,7 +308,6 @@ void GameFile::DoState(PointerWrap& p) p.Do(m_internal_name); p.Do(m_game_id); p.Do(m_gametdb_id); - p.Do(m_triforce_id); p.Do(m_title_id); p.Do(m_maker_id); @@ -498,8 +496,7 @@ const std::string& GameFile::GetName(const Core::TitleDatabase& title_database) if (IsModDescriptor()) return GetName(Variant::LongAndPossiblyCustom); - const std::string& database_name = - title_database.GetTitleName(m_gametdb_id, m_triforce_id, GetConfigLanguage()); + const std::string& database_name = title_database.GetTitleName(m_gametdb_id, GetConfigLanguage()); return database_name.empty() ? GetName(Variant::LongAndPossiblyCustom) : database_name; } diff --git a/Source/Core/UICommon/GameFile.h b/Source/Core/UICommon/GameFile.h index f015fa9725..e43950143b 100644 --- a/Source/Core/UICommon/GameFile.h +++ b/Source/Core/UICommon/GameFile.h @@ -76,7 +76,6 @@ public: const std::string& GetInternalName() const { return m_internal_name; } const std::string& GetGameID() const { return m_game_id; } const std::string& GetGameTDBID() const { return m_gametdb_id; } - std::string GetTriforceID() const { return m_triforce_id; } u64 GetTitleID() const { return m_title_id; } const std::string& GetMakerID() const { return m_maker_id; } u16 GetRevision() const { return m_revision; } @@ -160,7 +159,6 @@ private: std::string m_internal_name; std::string m_game_id; std::string m_gametdb_id; - std::string m_triforce_id; u64 m_title_id{}; std::string m_maker_id; diff --git a/Source/Core/UICommon/GameFileCache.cpp b/Source/Core/UICommon/GameFileCache.cpp index 37a130599d..898e818852 100644 --- a/Source/Core/UICommon/GameFileCache.cpp +++ b/Source/Core/UICommon/GameFileCache.cpp @@ -23,7 +23,7 @@ namespace UICommon { -static constexpr u32 CACHE_REVISION = 26; // Last changed in PR 10084 +static constexpr u32 CACHE_REVISION = 27; // Last changed in PR 13844 std::vector FindAllGamePaths(std::span directories_to_scan, bool recursive_scan) diff --git a/Source/Core/UICommon/UICommon.cpp b/Source/Core/UICommon/UICommon.cpp index 43973a02e0..c56d1e1d09 100644 --- a/Source/Core/UICommon/UICommon.cpp +++ b/Source/Core/UICommon/UICommon.cpp @@ -280,6 +280,7 @@ void CreateDirectories() File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + USA_DIR DIR_SEP); File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + EUR_DIR DIR_SEP); File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + JAP_DIR DIR_SEP); + File::CreateFullPath(File::GetUserPath(D_TRIUSER_IDX)); File::CreateFullPath(File::GetUserPath(D_HIRESTEXTURES_IDX)); File::CreateFullPath(File::GetUserPath(D_GRAPHICSMOD_IDX)); File::CreateFullPath(File::GetUserPath(D_MAPS_IDX));