Skip to content

Commit

Permalink
Fix support for homebrew which give shared WRAM to arm9
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Feb 17, 2024
1 parent 4b98d29 commit f6e093e
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 15 deletions.
10 changes: 8 additions & 2 deletions hb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export GAME_ICON := $(CURDIR)/$(ASSETS)/icon.bmp

#.PHONY: sdengine bootloader bootstub BootStrap clean
.PHONY: all dist release nightly bootloader bootloaderInject bootstub BootStrap dldi/dsisd.dldi sdengine clean
.PHONY: all dist release nightly bootloader bootloaderInject bootstub BootStrap dldi/dsisd.dldi sdengine sdengine_alt clean

all: $(OUTPUT)

Expand Down Expand Up @@ -167,7 +167,7 @@ dldi/dsisd.dldi:
@$(MAKE) -C dldi

#---------------------------------------------------------------------------------
bootloader: $(DATA) sdengine
bootloader: $(DATA) sdengine sdengine_alt
@$(MAKE) -C bootloader
@dlditool dldi/dsisd.dldi nitrofiles/load.bin

Expand All @@ -184,6 +184,11 @@ sdengine: $(DATA)
@mkdir -p bootloader/$(DATA)
@$(MAKE) -C sdengine

#---------------------------------------------------------------------------------
sdengine_alt: $(DATA)
@mkdir -p bootloader/$(DATA)
@$(MAKE) -C sdengine_alt

#---------------------------------------------------------------------------------
#$(BUILD):
#@[ -d $@ ] || mkdir -p $@
Expand All @@ -196,6 +201,7 @@ clean:
@$(MAKE) -C arm7 clean
@$(MAKE) -C arm9 clean
@$(MAKE) -C sdengine clean
@$(MAKE) -C sdengine_alt clean
@$(MAKE) -C bootloader clean
@$(MAKE) -C bootloaderInject clean
@$(MAKE) -C bootstub clean
Expand Down
9 changes: 9 additions & 0 deletions hb/arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ int main(void) {

irqEnable( IRQ_VBLANK | IRQ_VCOUNT );

if (isDSiMode() && REG_SCFG_EXT == 0) {
u32 wordBak = *(vu32*)0x037C0000;
*(vu32*)0x037C0000 = 0x414C5253;
if (*(vu32*)0x037C0000 == 0x414C5253 && *(vu32*)0x037C8000 != 0x414C5253) {
*(u32*)0x02FFE1A0 = 0x080037C0;
}
*(vu32*)0x037C0000 = wordBak;
}

nocashMessage("init completed");

nocashMessage("wait for FIFO");
Expand Down
15 changes: 8 additions & 7 deletions hb/bootloader/source/arm7/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <nds/debug.h>

#include "sdengine_bin.h"
#include "sdengine_alt_bin.h"
#include "patch.h"
#include "hook.h"
#include "common.h"
Expand Down Expand Up @@ -63,11 +64,11 @@ static const u32 homebrewSig[5] = {
// interruptDispatcher.s jump_intr:
// Patch
static const u32 homebrewSigPatched[5] = {
0xE59F1008, // ldr r1, =0x3900010 @ my custom handler
0xE59F1008, // ldr r1, =0x3000010 @ my custom handler
0xE5012008, // str r2, [r1,#-8] @ irqhandler
0xE501F004, // str r0, [r1,#-4] @ irqsig
0xEA000000, // b got_handler
0x037C0010 // DCD 0x037C0010
0x00000010 // DCD 0x03000010
};

// Accelerator patch for IPC_SYNC v2007
Expand Down Expand Up @@ -101,9 +102,9 @@ static const u32 homebrewAccelSig2010[4] = {
};

static const u32 homebrewAccelSigPatched[2] = {
0x47104A00 , // LDR R2, =0x037C0020
0x47104A00 , // LDR R2, =0x03000020
// BX R2
0x037C0020
0x00000020
};

static const u16 swi00Sig[2] = {
Expand Down Expand Up @@ -333,7 +334,7 @@ int hookNds (const tNDSHeader* ndsHeader, u32* sdEngineLocation, u32* wordComman
hookLocation[1] = homebrewSigPatched[1];
hookLocation[2] = homebrewSigPatched[2];
hookLocation[3] = homebrewSigPatched[3];
hookLocation[4] = homebrewSigPatched[4];
hookLocation[4] = ((u32)sdEngineLocation)+homebrewSigPatched[4];
} else {
nocashMessage("ERR_HOOK");
return ERR_HOOK;
Expand All @@ -354,7 +355,7 @@ int hookNds (const tNDSHeader* ndsHeader, u32* sdEngineLocation, u32* wordComman
} else {
// patch the program
hookAccel[0] = homebrewAccelSigPatched[0];
hookAccel[1] = homebrewAccelSigPatched[1];
hookAccel[1] = ((u32)sdEngineLocation)+homebrewAccelSigPatched[1];

nocashMessage("ACCEL_IPC_OK");
}
Expand All @@ -373,7 +374,7 @@ int hookNds (const tNDSHeader* ndsHeader, u32* sdEngineLocation, u32* wordComman
hookSwi05((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize, hookAccel, sdEngineLocation);
}*/

tonccpy (sdEngineLocation, sdengine_bin, sdengine_bin_size);
tonccpy (sdEngineLocation, (sdEngineLocation == (u32*)SDENGINE_LOCATION_ALT) ? sdengine_alt_bin : sdengine_bin, (sdEngineLocation == (u32*)SDENGINE_LOCATION_ALT) ? sdengine_alt_bin_size : sdengine_bin_size);

sdEngineLocation[1] = (u32)wordCommandAddr;

Expand Down
9 changes: 6 additions & 3 deletions hb/bootloader/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ extern u32 srParamsFileCluster;
extern u32 ndsPreloaded;

u8 TWL_HEAD[0x1000] = {0};
static u32 sdEngineLocation = 0;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Firmware stuff
Expand Down Expand Up @@ -195,8 +196,8 @@ static void initMBK(void) {
*((vu32*)REG_MBK5)=0x9C989490;

// WRAM mapped to the 0x3700000 - 0x37FFFFF area
// WRAM-A mapped to the 0x37C0000 - 0x37FFFFF area : 256k
REG_MBK6=0x080037C0; // same as dsiware
// WRAM-A mapped to the 0x3000000 - 0x303FFFF area : 256k
REG_MBK6=0x00403000; // same as dsi-enhanced and certain dsiware
// WRAM-B mapped to the 0x3740000 - 0x37BFFFF area : 512k // why? only 256k real memory is there
REG_MBK7=0x07C03740; // same as dsiware
// WRAM-C mapped to the 0x3700000 - 0x373FFFF area : 256k
Expand Down Expand Up @@ -242,6 +243,8 @@ static void resetMemory_ARM7 (void)
REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_SEND_CLEAR;
REG_IPC_FIFO_CR = 0;

sdEngineLocation = (*(u32*)0x02FFE1A0 == 0x080037C0) ? SDENGINE_LOCATION_ALT : SDENGINE_LOCATION;

arm7clearRAM(); // clear exclusive IWRAM
if (ndsPreloaded) {
toncset((u32*)0x02200000, 0, 0x180000); // clear most of EWRAM (except pre-loaded ARM9 binary)
Expand Down Expand Up @@ -712,7 +715,7 @@ int arm7_main (void) {
}
u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEADER)[0x0A])+patchOffset+0x80);

hookNds(ndsHeader, (u32*)SDENGINE_LOCATION, wordCommandAddr);
hookNds(ndsHeader, (u32*)sdEngineLocation, wordCommandAddr);

if (!patchOffsetCache.bootloaderChecked) {
u32 bootloaderSignature[4] = {0xEA000002, 0x00000000, 0x00000001, 0x00000000};
Expand Down
4 changes: 2 additions & 2 deletions hb/common/include/locations.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#define LOAD_CRT0_LOCATION 0x06840000 // LCDC_BANK_C

#define SDENGINE_LOCATION 0x037C0000
#define SDENGINE_BUFFER_WRAM_LOCATION 0x037CC000
#define SDENGINE_LOCATION 0x03000000
#define SDENGINE_LOCATION_ALT 0x037C0000
#define BOOT_INJECT_LOCATION 0x037D0000
#define TEMP_MEM 0x02FFD000
#define TEMP_ARM9_START_ADDRESS (*(vu32*)0x02FFFFF4)
Expand Down
2 changes: 1 addition & 1 deletion hb/sdengine/sdengine.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ OUTPUT_ARCH(arm)

MEMORY {

vram : ORIGIN = 0x037C0000, LENGTH = 32K
vram : ORIGIN = 0x03000000, LENGTH = 32K
}

__vram_start = ORIGIN(vram);
Expand Down
134 changes: 134 additions & 0 deletions hb/sdengine_alt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# SPECS is the directory containing the important build and link files
#---------------------------------------------------------------------------------
export TARGET := sdengine_alt
BUILD := build
SOURCES := ../sdengine/source ../common/source ../common/source_general
DATA := ../sdengine/data
INCLUDES := ../sdengine/include ../common/include


#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -mthumb-interwork -march=armv4t

COMMON := -g -Wall -O2\
-mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer\
-ffast-math \
$(ARCH)

# ADD -DDEBUG here for logging/debug
COMMON += $(INCLUDE) -DARM7 -D_NO_FAT

CFLAGS := $(COMMON) -std=gnu99
CXXFLAGS := $(COMMON) -fno-rtti -fno-exceptions

ASFLAGS := -g $(ARCH) $(INCLUDE)
LDFLAGS = -nostartfiles -T ../sdengine.ld -g $(ARCH) -Wl,-Map,$(TARGET).map

LIBS :=

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBNDS)


#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT := $(CURDIR)/../bootloader/data/$(TARGET)

export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR := $(CURDIR)/$(BUILD)

CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)

export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

.PHONY: $(BUILD) clean all

#---------------------------------------------------------------------------------
all: $(BUILD)

$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).bin $(TARGET).elf


#---------------------------------------------------------------------------------
else

DEPENDS := $(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).bin : $(CURDIR)/$(TARGET).elf
$(CURDIR)/$(TARGET).elf : $(OFILES)


#---------------------------------------------------------------------------------
%.bin: %.elf
@$(OBJCOPY) -O binary $< $@
@echo built ... $(notdir $@)


-include $(DEPENDS)


#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
Loading

0 comments on commit f6e093e

Please sign in to comment.