Skip to content

Commit

Permalink
Flesh out DLDI driver patching (#166)
Browse files Browse the repository at this point in the history
Fixes #164
  • Loading branch information
asiekierka authored Nov 18, 2024
1 parent a377c95 commit cbc4da2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/dldi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
along with NooDS. If not, see <https://www.gnu.org/licenses/>.
*/

#include <cstring>
#include "dldi.h"
#include "core.h"
#include "settings.h"
Expand All @@ -31,7 +32,7 @@ Dldi::~Dldi()
void Dldi::patchRom(uint8_t *rom, uint32_t offset, uint32_t size)
{
// Scan the ROM for DLDI drivers and patch them if found
for (size_t i = 0; i < size; i += 0x40)
for (uint32_t i = 0; i < size; i += 4)
{
// Check for the DLDI magic number
if (U8TO32(rom, i) != 0xBF8DA5ED)
Expand All @@ -46,9 +47,23 @@ void Dldi::patchRom(uint8_t *rom, uint32_t offset, uint32_t size)
goto next;
}

// Ensure there's room to patch the DLDI driver
if (rom[i + 0x0F] < 0x08) // Space in ROM
{
LOG("Not enough space to patch DLDI driver at ROM offset 0x%X\n", offset + i);
break;
}

// Patch the DLDI driver to use the HLE functions
rom[i + 0x0F] = 0x0E; // Size of driver in terms of 1 << n (16KB)
rom[i + 0x0C] = 0x01; // DLDI driver version
rom[i + 0x0D] = 0x08; // Size of driver in terms of 1 << n (256 bytes)
rom[i + 0x0E] = 0x00; // Sections to adjust
strcpy((char*)&rom[i + 0x10], "NooDS DLDI"); // Long driver name
uint32_t address = U8TO32(rom, i + 0x40); // Address of driver
U32TO8(rom, i + 0x44, address + 0x98); // End of driver code
U32TO8(rom, i + 0x58, address + 0x98); // Start of BSS area
U32TO8(rom, i + 0x5C, address + 0x98); // End of BSS area
memcpy(&rom[i + 0x60], "NOOD", 4); // Short driver name
U32TO8(rom, i + 0x64, 0x00000023); // Feature flags (read, write, NDS slot)
U32TO8(rom, i + 0x68, address + 0x80); // Address of startup()
U32TO8(rom, i + 0x6C, address + 0x84); // Address of isInserted()
Expand Down

0 comments on commit cbc4da2

Please sign in to comment.