Skip to content

Commit

Permalink
Automatically choose load address of DSi device list
Browse files Browse the repository at this point in the history
  • Loading branch information
fincs committed Jan 1, 2024
1 parent d7eb62f commit ef137ee
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
11 changes: 10 additions & 1 deletion source/elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ void ElfReadHdr(FILE *fp, Elf32_Ehdr *hdr, Elf32_Phdr **phdr) {
* unsigned int *size, a pointer to place the data size at.
* unsigned int *wram_address,a pointer to map DSi exclusive ARM7 WRAM at.
* bool *has_overlays, a pointer to place the "has overlays" flag at.
* bool is_arm9, true if this is the arm9 binary.
* bool is_twl, true if we want to copy TWL sections.
*/
int CopyFromElf(char *elfFilename, unsigned int *entry,
unsigned int *ram_address, unsigned int *size,
unsigned int *wram_address, bool *has_overlays,
bool is_twl)
bool is_arm9, bool is_twl)
{
FILE *in;
Elf32_Ehdr header;
Expand Down Expand Up @@ -192,6 +193,14 @@ int CopyFromElf(char *elfFilename, unsigned int *entry,
if(wram_address && !*wram_address && p_headers[i].p_vaddr >= 0x03000000 && p_headers[i].p_vaddr < 0x037F8000)
*wram_address = p_headers[i].p_vaddr;

/* Automatically choose load address of DSi device list (right after all ARM7 WRAM segments). */
if(!is_arm9 && !is_twl) {
unsigned int end_address = (p_headers[i].p_vaddr + p_headers[i].p_memsz + 3) &~ 3;
if (end_address > deviceListRamAddress && end_address + 0x400 <= 0x0380F000) {
deviceListRamAddress = end_address;
}
}

/* Skip BSS segments. */
if(!p_headers[i].p_filesz)
continue;
Expand Down
1 change: 1 addition & 0 deletions source/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ int CopyFromElf(char *elfFilename,
unsigned int *size,
unsigned int *wram_address,
bool *has_overlays,
bool is_arm9,
bool is_twl);
void CopyOverlaysFromElf(const char* elfFilename, bool is_arm9);
void ElfReadHdr(FILE *fp, Elf32_Ehdr *hdr, Elf32_Phdr **phdr);
Expand Down
10 changes: 5 additions & 5 deletions source/ndscreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ void Create()
unsigned int size = 0;
bool has_overlays = false;
if (is_arm9_elf)
CopyFromElf(arm9filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false);
CopyFromElf(arm9filename, &entry_address, &ram_address, &size, NULL, &has_overlays, true, false);
else
CopyFromBin(arm9filename, 0, &size);
header.arm9_entry_address = entry_address;
Expand Down Expand Up @@ -499,7 +499,7 @@ void Create()
bool has_overlays = false;

if (is_arm7_elf)
CopyFromElf(arm7filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false);
CopyFromElf(arm7filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false, false);
else
CopyFromBin(arm7filename, &size);

Expand Down Expand Up @@ -652,7 +652,7 @@ void Create()

unsigned int ram_address = 0;
unsigned int size = 0;
CopyFromElf(arm9filename, NULL, &ram_address, &size, NULL, NULL, true);
CopyFromElf(arm9filename, NULL, &ram_address, &size, NULL, NULL, true, true);
if (!size)
{
sections--;
Expand All @@ -675,7 +675,7 @@ void Create()

unsigned int ram_address = 0;
unsigned int size = 0;
CopyFromElf(arm7filename, NULL, &ram_address, &size, &mbkArm7WramMapAddress, NULL, true);
CopyFromElf(arm7filename, NULL, &ram_address, &size, &mbkArm7WramMapAddress, NULL, false, true);
if (!size)
{
sections--;
Expand Down Expand Up @@ -771,7 +771,7 @@ void Create()
header.access_control = accessControl;
header.scfg_ext_mask = scfgExtMask;
header.appflags = appFlags;
header.device_list_ram_address = 0x02FFDC00;
header.device_list_ram_address = deviceListRamAddress;
header.offset_0x20C = 0x00010000;
header.tid_low = header.gamecode[3] | (header.gamecode[2]<<8) | (header.gamecode[1]<<16) | (header.gamecode[0]<<24);
header.tid_high = titleidHigh;
Expand Down
1 change: 1 addition & 0 deletions source/ndstool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ unsigned int scfgExtMask = 0x80040407; // enable access to everything
unsigned int accessControl = 0x00000138;
unsigned int mbkArm7WramMapAddress = 0;
unsigned int appFlags = 0x01;
unsigned int deviceListRamAddress = 0x03800000;


/*
Expand Down
1 change: 1 addition & 0 deletions source/ndstool.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extern unsigned int scfgExtMask;
extern unsigned int accessControl;
extern unsigned int appFlags;
extern unsigned int mbkArm7WramMapAddress;
extern unsigned int deviceListRamAddress;
extern char *title;
extern char *makercode;
extern char *gamecode;
Expand Down

0 comments on commit ef137ee

Please sign in to comment.