diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4cb6f1e..5d38cb89 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,6 +20,7 @@ add_library(core STATIC DSi_AES.cpp DSi_Camera.cpp DSi_DSP.cpp + DSi_DSP_UCodes.cpp DSi_I2C.cpp DSi_NAND.cpp DSi_NDMA.cpp diff --git a/src/DSi_DSP.cpp b/src/DSi_DSP.cpp index 25abd474..4324e6ed 100644 --- a/src/DSi_DSP.cpp +++ b/src/DSi_DSP.cpp @@ -20,6 +20,7 @@ #include "DSi.h" #include "DSi_DSP.h" +#include "DSi_DSP_UCodes.h" #include "FIFO.h" #include "NDS.h" #include "Platform.h" @@ -113,6 +114,7 @@ DSi_DSP::DSi_DSP(melonDS::DSi& dsi) : DSi(dsi) TeakraCore = new Teakra::Teakra(); SCFG_RST = false; + CurrentUCodeID = UCodeID::UNKNOWN; // ???? //if (!TeakraCore) return false; @@ -188,6 +190,11 @@ bool DSi_DSP::IsRstReleased() const } void DSi_DSP::SetRstLine(bool release) { + if (!SCFG_RST && release) + { + CurrentUCodeID = IdentifyUCode(DSi); + } + SCFG_RST = release; Reset(); DSPTimestamp = DSi.ARM9Timestamp; // only start now! diff --git a/src/DSi_DSP.h b/src/DSi_DSP.h index f76b4202..16c2f38f 100644 --- a/src/DSi_DSP.h +++ b/src/DSi_DSP.h @@ -30,6 +30,7 @@ namespace Teakra { class Teakra; } namespace melonDS { class DSi; +enum class UCodeID; class DSi_DSP { public: @@ -74,6 +75,7 @@ private: u16 SNDExCnt; Teakra::Teakra* TeakraCore; + UCodeID CurrentUCodeID; bool SCFG_RST; diff --git a/src/DSi_DSP_UCodes.cpp b/src/DSi_DSP_UCodes.cpp new file mode 100644 index 00000000..6c6997b2 --- /dev/null +++ b/src/DSi_DSP_UCodes.cpp @@ -0,0 +1,95 @@ +/* + Copyright 2016-2024 melonDS team + + This file is part of melonDS. + + melonDS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#include "DSi.h" +#include "DSi_DSP_UCodes.h" +#include "CRC32.h" +#include "Platform.h" + +namespace melonDS +{ +using Platform::Log; +using Platform::LogLevel; + +// static block of zero'd memory for unmapped DSP memory +static const u8 DSPZeroPage[0x8000] = {}; + +UCodeID IdentifyUCode(melonDS::DSi& dsi) +{ + u32 crc = 0; + + // Hash NWRAM B, which contains the DSP program + // The hash should be in the DSP's memory view + for (u32 addr = 0; addr < 0x40000; addr += 0x8000) + { + const u8* ptr = dsi.NWRAMMap_B[2][(addr >> 15) & 0x7]; + if (!ptr) ptr = DSPZeroPage; + crc = CRC32(ptr, 0x8000, crc); + } + + switch (crc) + { + case 0x7867C94B: + Log(LogLevel::Debug, "Identified AAC Sound App DSP UCode\n"); + return UCodeID::AAC_SOUND_APP; + case 0x0CAFEF48: + Log(LogLevel::Debug, "Identified AAC SDK v0 DSP UCode\n"); + return UCodeID::AAC_SDK_V0; + case 0xCD2A8B1B: + Log(LogLevel::Debug, "Identified Graphics SDK v0 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V0; + case 0x7EEE19FE: + Log(LogLevel::Debug, "Identified G711 SDK v1 DSP UCode\n"); + return UCodeID::G711_SDK_V1; + case 0x7323B75B: + Log(LogLevel::Debug, "Identified Graphics SDK v1 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V1; + case 0xBD4B63B6: + Log(LogLevel::Debug, "Identified Graphics SDK v1 Patch DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V1_PATCH; + case 0x6056C6FF: + Log(LogLevel::Debug, "Identified G711 SDK v2 DSP UCode\n"); + return UCodeID::G711_SDK_V2; + case 0x448BB6A2: + Log(LogLevel::Debug, "Identified Graphics SDK v2 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V2; + case 0x2C281DAE: + Log(LogLevel::Debug, "Identified G711 SDK v3 DSP UCode\n"); + return UCodeID::G711_SDK_V3; + case 0x63CAEC33: + Log(LogLevel::Debug, "Identified Graphics SDK v3 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V3; + case 0x2A1D7F94: + Log(LogLevel::Debug, "Identified G711 SDK v4 DSP UCode\n"); + return UCodeID::G711_SDK_V4; + case 0x1451EB84: + Log(LogLevel::Debug, "Identified Graphics SDK v4 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V4; + case 0x4EBEB519: + Log(LogLevel::Debug, "Identified G711 SDK v5 DSP UCode\n"); + return UCodeID::G711_SDK_V5; + case 0x2C974FC8: + Log(LogLevel::Debug, "Identified Graphics SDK v5 DSP UCode\n"); + return UCodeID::GRAPHICS_SDK_V5; + default: + Log(LogLevel::Debug, "Unknown DSP UCode (CRC = %08X)\n", crc); + return UCodeID::UNKNOWN; + } +} + +} \ No newline at end of file diff --git a/src/DSi_DSP_UCodes.h b/src/DSi_DSP_UCodes.h new file mode 100644 index 00000000..41dee2eb --- /dev/null +++ b/src/DSi_DSP_UCodes.h @@ -0,0 +1,103 @@ +/* + Copyright 2016-2024 melonDS team + + This file is part of melonDS. + + melonDS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#ifndef DSI_DSP_UCODES_H +#define DSI_DSP_UCODES_H + +namespace melonDS +{ +class DSi; + +// DSP ucodes are COFF files embed into the DSi ROM in some fashion +// DSP ucodes all appear to simply be from the DSi's SDK (as evidenced by the build paths in the COFF) +// Presumingly, developers could not actually make their own ucodes, that was reserved for Nintendo +enum class UCodeID +{ + // AAC decoder in DSi Sound app + // COFF build timestamp: 8/4/2008 + // This appears to be from an extremely early version of the DSi's SDK + // This SDK was presumingly never available to 3rd party developers, as the build timestamp is before the DSi's release + // As such, nothing else appears to use this ucode + AAC_SOUND_APP, + + // AAC decoder present in v0 SDK + // COFF build timestamp: 10/21/2008 + // At least 1 DSiWare app (Futo Sutando Tsuki - Banbura DX Rajio) is known to contain this + AAC_SDK_V0, + + // Graphics ucode present in v0 SDK + // COFF build timestamp: 10/21/2008 + // As least 1 DSiWare app (Hobonichi Rosenzu 2010, rev 0) is known to contain this + GRAPHICS_SDK_V0, + + // G711 encoder/decoder present in v1 SDK + // COFF build timestamp: 2/13/2009 + // Appears to be a replacement for the AAC decoder + G711_SDK_V1, + + // Graphics ucode present in v1 SDK + // COFF build timestamp: 2/13/2009 + GRAPHICS_SDK_V1, + + // Graphics ucode present in v1 SDK + // COFF build timestamp: 4/3/2009 + // Appears to be a patch against the previous graphics ucode + // Known to sometimes be present alongside the SDK v1 G711 ucode + GRAPHICS_SDK_V1_PATCH, + + // G711 encoder/decoder present in v2 SDK + // COFF build timestamp: 4/8/2009 + G711_SDK_V2, + + // Graphics ucode present in v2 SDK + // COFF build timestamp: 4/8/2009 + GRAPHICS_SDK_V2, + + // G711 encoder/decoder present in v3 SDK + // COFF build timestamp: 9/16/2009 + G711_SDK_V3, + + // Graphics ucode present in v3 SDK + // COFF build timestamp: 9/16/2009 + GRAPHICS_SDK_V3, + + // G711 encoder/decoder present in v4 SDK + // COFF build timestamp: 11/9/2009 + G711_SDK_V4, + + // Graphics ucode present in v4 SDK + // COFF build timestamp: 11/9/2009 + GRAPHICS_SDK_V4, + + // G711 encoder/decoder present in v5 SDK + // COFF build timestamp: 1/13/2010 + G711_SDK_V5, + + // Graphics ucode present in v5 SDK + // COFF build timestamp: 1/13/2010 + GRAPHICS_SDK_V5, + + // Unknown ucode... + UNKNOWN = -1, +}; + +UCodeID IdentifyUCode(melonDS::DSi& dsi); + +} + +#endif // DSI_DSP_UCODES_H