Skip to content

Commit

Permalink
[3DS] Update to libctru 2.0
Browse files Browse the repository at this point in the history
Set USE_CTRULIB_2=1 to build with libctru 2.0. When unset, this code
is compatible with the older toolchain.

Some 2.0 changes addressed rare problems in earlier versions:

- Save / restore stack pointer for init / exit
  Otherwise, it could be outside of the range we deallocate.
- Run aptMainLoop in the audio driver to react correctly to sleep events

Other changes for 2.0:

- Remove ninjhax1 -- requires outdated APIs that have been removed
- Switch from __sync_arbiter to syncArbitrateAddress
- Use implicit gxCmdBuf
- Use gpuPresentBuffer for double buffering
  • Loading branch information
justinweiss committed Aug 27, 2020
1 parent 4177ea4 commit 9ae2514
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 85 deletions.
28 changes: 20 additions & 8 deletions Makefile.ctr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ BUILD_3DSX = 1
BUILD_3DS = 0
BUILD_CIA = 1
LIBCTRU_NO_DEPRECATION = 1
USE_CTRULIB_2 = 0

APP_TITLE = RetroArch 3DS
APP_DESCRIPTION = RetroArch 3DS
Expand Down Expand Up @@ -35,8 +36,7 @@ OBJ += ctr/exec-3dsx/exec_cia.o \
ctr/exec-3dsx/exec_3dsx.o \
ctr/exec-3dsx/mini-hb-menu/launch.o \
ctr/exec-3dsx/mini-hb-menu/loaders/rosalina.o \
ctr/exec-3dsx/mini-hb-menu/loaders/hax2.o \
ctr/exec-3dsx/mini-hb-menu/loaders/ninjhax1.o
ctr/exec-3dsx/mini-hb-menu/loaders/hax2.o

ifeq ($(APP_BIG_TEXT_SECTION), 1)
APP_USE_SVCHAX = 1
Expand Down Expand Up @@ -104,6 +104,18 @@ ifeq ($(strip $(CTRULIB)),)
CTRULIB = $(DEVKITPRO)/libctru
endif

ifeq ($(strip $(USE_CTRULIB_2)),1)
CFLAGS += -DUSE_CTRULIB_2
endif

ifeq ($(strip $(DEVKITTOOLS)),)
ifeq ($(strip $(USE_CTRULIB_2)),1)
DEVKITTOOLS = $(DEVKITPRO)/tools
else
DEVKITTOOLS = $(DEVKITARM)
endif
endif

APP_TITLE := $(shell echo "$(APP_TITLE)" | cut -c1-128)
APP_DESCRIPTION := $(shell echo "$(APP_DESCRIPTION)" | cut -c1-256)
APP_AUTHOR := $(shell echo "$(APP_AUTHOR)" | cut -c1-128)
Expand Down Expand Up @@ -225,13 +237,13 @@ else
endif

%.o: %.vsh %.gsh
$(DEVKITARM)/bin/picasso $^ -o $*.shbin
$(DEVKITARM)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
$(DEVKITTOOLS)/bin/picasso $^ -o $*.shbin
$(DEVKITTOOLS)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
rm $*.shbin

%.o: %.vsh
$(DEVKITARM)/bin/picasso $^ -o $*.shbin
$(DEVKITARM)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
$(DEVKITTOOLS)/bin/picasso $^ -o $*.shbin
$(DEVKITTOOLS)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
rm $*.shbin

%.o: %.cpp
Expand All @@ -252,15 +264,15 @@ endif
%.vsh:

$(TARGET).smdh: $(APP_ICON)
$(DEVKITARM)/bin/smdhtool --create "$(APP_TITLE)" "$(APP_DESCRIPTION)" "$(APP_AUTHOR)" $(APP_ICON) $@
$(DEVKITTOOLS)/bin/smdhtool --create "$(APP_TITLE)" "$(APP_DESCRIPTION)" "$(APP_AUTHOR)" $(APP_ICON) $@

$(TARGET).3dsx: $(TARGET).elf
ifeq ($(APP_BIG_TEXT_SECTION), 1)
cp pkg/ctr/big_text_section.xml $(TARGET).xml
else
rm -f $(TARGET).xml
endif
$(DEVKITARM)/bin/3dsxtool $< $@ $(_3DSXFLAGS)
$(DEVKITTOOLS)/bin/3dsxtool $< $@ $(_3DSXFLAGS)

$(TARGET).elf: ctr/3dsx_custom_crt0.o
$(LD) $(LDFLAGS) $(OBJ) $(LIBDIRS) $(LIBS) -o $@
Expand Down
28 changes: 20 additions & 8 deletions Makefile.ctr.salamander
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DEBUG = 0
BUILD_3DSX = 1
BUILD_3DS = 0
BUILD_CIA = 1
USE_CTRULIB_2 = 0

APP_TITLE = RetroArch 3DS
APP_DESCRIPTION = RetroArch 3DS
Expand Down Expand Up @@ -46,13 +47,24 @@ OBJ += ctr/exec-3dsx/exec_cia.o \
ctr/exec-3dsx/exec_3dsx.o \
ctr/exec-3dsx/mini-hb-menu/launch.o \
ctr/exec-3dsx/mini-hb-menu/loaders/rosalina.o \
ctr/exec-3dsx/mini-hb-menu/loaders/hax2.o \
ctr/exec-3dsx/mini-hb-menu/loaders/ninjhax1.o
ctr/exec-3dsx/mini-hb-menu/loaders/hax2.o

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

ifeq ($(strip $(USE_CTRULIB_2)),1)
CFLAGS += -DUSE_CTRULIB_2
endif

ifeq ($(strip $(DEVKITTOOLS)),)
ifeq ($(strip $(USE_CTRULIB_2)),1)
DEVKITTOOLS = $(DEVKITPRO)/tools
else
DEVKITTOOLS = $(DEVKITARM)
endif
endif

ifeq ($(strip $(CTRULIB)),)
CTRULIB = $(DEVKITPRO)/libctru
endif
Expand Down Expand Up @@ -154,13 +166,13 @@ else
endif

%.o: %.vsh %.gsh
$(DEVKITARM)/bin/picasso $^ -o $*.shbin
$(DEVKITARM)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
$(DEVKITTOOLS)/bin/picasso $^ -o $*.shbin
$(DEVKITTOOLS)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
rm $*.shbin

%.o: %.vsh
$(DEVKITARM)/bin/picasso $^ -o $*.shbin
$(DEVKITARM)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
$(DEVKITTOOLS)/bin/picasso $^ -o $*.shbin
$(DEVKITTOOLS)/bin/bin2s $*.shbin | $(PREFIX)as -o $@
rm $*.shbin

%.o: %.cpp
Expand All @@ -181,15 +193,15 @@ endif
%.vsh:

$(TARGET).smdh: $(APP_ICON)
$(DEVKITARM)/bin/smdhtool --create "$(APP_TITLE)" "$(APP_DESCRIPTION)" "$(APP_AUTHOR)" $(APP_ICON) $@
$(DEVKITTOOLS)/bin/smdhtool --create "$(APP_TITLE)" "$(APP_DESCRIPTION)" "$(APP_AUTHOR)" $(APP_ICON) $@

$(TARGET).3dsx: $(TARGET).elf
ifeq ($(APP_BIG_TEXT_SECTION), 1)
cp pkg/ctr/big_text_section.xml $(TARGET).xml
else
rm -f $(TARGET).xml
endif
$(DEVKITARM)/bin/3dsxtool $< $@ $(_3DSXFLAGS)
$(DEVKITTOOLS)/bin/3dsxtool $< $@ $(_3DSXFLAGS)

$(TARGET).elf: ctr/3dsx_custom_crt0.o
$(LD) $(LDFLAGS) $(OBJ) $(LIBDIRS) $(LIBS) -o $@
Expand Down
9 changes: 9 additions & 0 deletions audio/drivers/ctr_dsp_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ static ssize_t ctr_dsp_audio_write(void *data, const void *buf, size_t size)
{
do{
svcSleepThread(100000);

/* Run aptMainLoop to update APT state if DSP state
* changed, this prevents a hang on sleep. */
if(!aptMainLoop())
{
command_event(CMD_EVENT_QUIT, NULL);
return true;
}

sample_pos = ndspChnGetSamplePos(ctr->channel);
}while (((sample_pos - (ctr->pos + (size >>2))) & CTR_DSP_AUDIO_COUNT_MASK) > (CTR_DSP_AUDIO_COUNT >> 1)
|| (((ctr->pos - (CTR_DSP_AUDIO_COUNT >> 4) - sample_pos) & CTR_DSP_AUDIO_COUNT_MASK) > (CTR_DSP_AUDIO_COUNT >> 1)));
Expand Down
3 changes: 1 addition & 2 deletions ctr/ctr_linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ void* linearMemAlign(size_t size, size_t alignment)
void* linearAlloc(size_t size)
{
#if 0
extern PrintConsole* currentConsole;
if(currentConsole->consoleInitialised)
if(ctrConsole && ctrConsole->consoleInitialised)
{
printf("linearAlloc : 0x%08X\n", size);
DEBUG_HOLD();
Expand Down
9 changes: 9 additions & 0 deletions ctr/ctr_svchax.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ typedef struct
bool isNew3DS;
u32 kernel_fcram_mapping_offset;

#ifndef USE_CTRULIB_2
Handle arbiter;
#endif
volatile u32 alloc_address;
volatile u32 alloc_size;
u8* flush_buffer;
Expand Down Expand Up @@ -263,7 +265,9 @@ static void do_memchunkhax2(void)

svcCloseHandle(mch2.dummy_threads_lock);

#ifndef USE_CTRULIB_2
mch2.arbiter = __sync_get_arbiter();
#endif

svcControlMemory(&linear_buffer, 0, 0, 0x1000,
MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);
Expand Down Expand Up @@ -327,9 +331,14 @@ static void do_memchunkhax2(void)
(ThreadFunc)alloc_thread_entry, (u32)&mch2,
mch2.threads[MCH2_THREAD_COUNT_MAX - 1].stack_top, 0x3F, 1);

#ifdef USE_CTRULIB_2
while ((u32) syncArbitrateAddress((s32 *)mch2.alloc_address,
ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0) == 0xD9001814);
#else
while ((u32) svcArbitrateAddress(mch2.arbiter, mch2.alloc_address,
ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0,
0) == 0xD9001814);
#endif

GX_TextureCopy((void*)linear_buffer, 0, (void*)dst_memchunk, 0, 16, 8);
memcpy(flush_buffer, flush_buffer + 0x4000, 0x4000);
Expand Down
11 changes: 11 additions & 0 deletions ctr/ctr_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ u32 __heapBase;
u32 __stack_bottom;
u32 __stack_size_extra;

u32 __saved_stack;

extern u32 __linear_heap_size_hbl;
extern u32 __heap_size_hbl;
extern void* __service_ptr;
Expand Down Expand Up @@ -74,6 +76,11 @@ void __system_allocateHeaps(void)
/* Allocate the linear heap */
svcControlMemory(&__linear_heap, 0x0, 0x0, __linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);

#ifdef USE_CTRULIB_2
/* Mappable allocator init */
mappableInit(OS_MAP_AREA_BEGIN, OS_MAP_AREA_END);
#endif

/* Set up newlib heap */
fake_heap_end = (char*)0x13F00000;
}
Expand Down Expand Up @@ -240,6 +247,9 @@ void __system_initArgv(void)

void initSystem(void (*retAddr)(void))
{
register u32 sp_val __asm__("sp");
__saved_stack = sp_val;

__libctru_init(retAddr);
__appInit();
__system_initArgv();
Expand All @@ -250,6 +260,7 @@ void __attribute__((noreturn)) __ctru_exit(int rc)
{
__libc_fini_array();
__appExit();
asm ("mov sp, %[saved_stack] \n\t" : : [saved_stack] "r" (__saved_stack) : "sp");
__libctru_exit(rc);
}

Expand Down
7 changes: 0 additions & 7 deletions ctr/exec-3dsx/exec_3dsx.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "mini-hb-menu/common.h"

extern const loaderFuncs_s loader_Ninjhax1;
extern const loaderFuncs_s loader_Ninjhax2;
extern const loaderFuncs_s loader_Rosalina;

Expand Down Expand Up @@ -69,12 +68,6 @@ static int exec_3dsx_actual(const char* path,
launch_3dsx = loader_Ninjhax2.launchFile;
}

if (!inited)
{
inited = loader_Ninjhax1.init();
launch_3dsx = loader_Ninjhax1.launchFile;
}

if (inited)
{
osSetSpeedupEnable(false);
Expand Down
43 changes: 0 additions & 43 deletions ctr/exec-3dsx/mini-hb-menu/loaders/ninjhax1.c

This file was deleted.

Loading

0 comments on commit 9ae2514

Please sign in to comment.