Skip to content

Commit

Permalink
Fixed GBA mode switch.
Browse files Browse the repository at this point in the history
* libnds's swiSwitchToGBAMode ASM function was broken. They used r0
instead of r2 so it didn't work. It must have been correct when GBA
Exploader was last compiled more then a decade ago, but now it's broken
so this ASM function had to be reimplemented. GBA Mode switch now
appears to function as intended.
* As I currently lack a compatible slot-2 flashcart (SuperCard Lite
doesn't work even after trying a few hardcoded card types in main.c)
further testing can't be done on the rest of the code. It appears to
work but I won't know for sure until someone with compatible hardware
tests it.
  • Loading branch information
ApacheThunder committed May 9, 2024
1 parent 11ea610 commit 6c10c47
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ checkarm9:
$(TARGET).nds : arm7/$(TARGET).elf arm9/$(TARGET).elf
@ndstool -c $@ -7 arm7/$(TARGET).elf -9 arm9/$(TARGET).elf \
-b $(CURDIR)/logo32.bmp "GBA ExpLoader;Version $(VERSTRING);By Rudolph;" \
-g GBEX 01 "GBAEXPLOADER" -z 80040000 -u 00030004 -a 00000138 -p 0001 \
-g #### 01 "GBAEXPLOADER" -z 80040000 -u 00030004 -a 00000138 -p 0001 \

data:
@mkdir -p data
Expand Down
4 changes: 1 addition & 3 deletions arm7/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions


ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=ds_arm7.specs -g $(ARCH) -Wl,-Map,$(notdir $*).map
# LDFLAGS = -specs=ds_arm7_iwram.specs -g $(ARCH) -Wl,-Map,$(notdir $*).map

LDFLAGS = -specs=ds_arm7.specs -g $(ARCH) -Wl,-Map,$(notdir $*).map

LIBS := -lnds7

Expand Down
15 changes: 15 additions & 0 deletions arm7/source/biosCalls.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.TEXT
.ARM

@---------------------------------------------------------------------------------------
.GLOBAL swiSwitchToGBAModeFixed
.func swiSwitchToGBAModeFixed
@---------------------------------------------------------------------------------------
swiSwitchToGBAModeFixed:
mov r2,#0x40
swi 0x1f0000

.endfunc

.end

18 changes: 9 additions & 9 deletions arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ extern void ret_menu7_R4(void);
extern void ret_menu7_Gen(void);
extern void ret_menu7_mse(void);

// libnds messed up the original SWI bios call ASM.
// They used r0 instead of r2. This reimplementation fixes that issue for now.
extern void swiSwitchToGBAModeFixed();

void gbaMode() {
vu32 vr;

REG_IME = IME_DISABLE;
for(vr = 0; vr < 0x1000; vr++); // Wait ARM9

if (((*(vu32*)0x027FFCE4 >> 3) & 0x01) == 0x01) {
writePowerManagement(0, PM_BACKLIGHT_BOTTOM | PM_SOUND_AMP);
} else {
writePowerManagement(0, PM_BACKLIGHT_TOP | PM_SOUND_AMP);
if (PersonalData->gbaScreen) {
writePowerManagement(PM_CONTROL_REG, PM_BACKLIGHT_BOTTOM | PM_SOUND_AMP);
} else {
writePowerManagement(PM_CONTROL_REG, PM_BACKLIGHT_TOP | PM_SOUND_AMP);
}

swiSwitchToGBAMode();
// asm("mov r2, #0x40");
// asm("swi 0x1F0000");

swiSwitchToGBAModeFixed();
while(1);
}

Expand Down
14 changes: 5 additions & 9 deletions arm9/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,28 @@ static void resetToSlot2() {
swiSoftReset();
}

void gbaMode() {
ITCM_CODE void gbaMode() {

/*if(strncmp(GBA_HEADER.gamecode, "PASS", 4) == 0) {
if(strncmp(GBA_HEADER.gamecode, "PASS", 4) == 0) {
resetARM9Memory();
resetToSlot2();
}*/
}

videoSetMode(0);
videoSetModeSub(0);

// vramSetMainBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_BG, VRAM_C_MAIN_BG, VRAM_D_MAIN_BG);
vramSetPrimaryBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_BG, VRAM_C_MAIN_BG, VRAM_D_MAIN_BG);
// vramSetMainBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_BG, VRAM_C_ARM7, VRAM_D_ARM7);

if(PersonalData->gbaScreen) { lcdMainOnBottom(); } else { lcdMainOnTop(); }

// FIFOSend(IPC_CMD_GBAMODE);
fifoSendValue32(FIFO_USER_01, 1);

gba_frame();

// FIFOSend(IPC_CMD_GBAMODE);
fifoSendValue32(FIFO_USER_01, 1);
sysSetBusOwners(ARM7_OWNS_CARD, ARM7_OWNS_ROM);
fifoSendValue32(FIFO_USER_01, 1);
REG_IME = 0;
while(1)swiWaitForVBlank();
while(1);
}


Expand Down
Loading

0 comments on commit 6c10c47

Please sign in to comment.