diff --git a/include/nds.h b/include/nds.h index 9fa2ccfa..24634ad0 100644 --- a/include/nds.h +++ b/include/nds.h @@ -110,9 +110,9 @@ /// /// @section debug_api Debugging /// - @ref nds/arm9/console.h "Debug via printf to DS screen or NO$GBA" -/// - @ref nds/arm9/exceptions.h "Exception handling" /// - @ref nds/arm9/sassert.h "Simple assert" /// - @ref nds/debug.h "Send message to NO$GBA" +/// - @ref nds/exceptions.h "Exception handling" #ifndef LIBNDS_NDS_H__ #define LIBNDS_NDS_H__ @@ -135,6 +135,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -157,7 +158,6 @@ extern "C" { # include # include # include -# include # include # include # include diff --git a/include/nds/arm9/exceptions.h b/include/nds/arm9/exceptions.h index aeb52dc9..5aae2a80 100644 --- a/include/nds/arm9/exceptions.h +++ b/include/nds/arm9/exceptions.h @@ -1,65 +1,5 @@ // SPDX-License-Identifier: Zlib -// SPDX-FileNotice: Modified from the original version by the BlocksDS project. // -// Copyright (C) 2005 Dave Murphy (WinterMute) +// Copyright (C) 2024 Adrian "asie" Siekierka -#ifndef LIBNDS_NDS_ARM9_EXCEPTIONS_H__ -#define LIBNDS_NDS_ARM9_EXCEPTIONS_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -/// @file nds/arm9/exceptions.h -/// -/// @brief Functions to handle hardware exceptions. -/// -/// Check https://www.problemkaputt.de/gbatek.htm#biosramusage for more -/// information. - -#include -#include - -/// NDS9 BIOS debug exception vector, or 0 for no handler (mirror) -#define EXCEPTION_VECTOR (*(VoidFn *)(0x2FFFD9C)) - -/// NDS9 BIOS debug exception stack top (mirror) -#define EXCEPTION_STACK_TOP ((uint32_t *)0x2FFFD9C) - -/// Pointer to the user exception handler, called from the exception handler of -/// libnds. -extern VoidFn exceptionC; - -/// Stack reserved for the user exception handler, allocated by the exception -/// handler of libnds. -extern u32 exceptionStack; - -/// Array with a copy of all the registers of when the exception occured. -extern s32 exceptionRegisters[]; - -/// Default exception handler of libnds -void enterException(void); - -/// Sets a custom hardware exception handler. -/// -/// @param handler -/// Exception handler routine. -void setExceptionHandler(VoidFn handler); - -/// Sets the default debug hardware exception handler. -/// -/// This handler prints a lot of information, like the state of the CPU -/// registers when the CPU crashed. -void defaultExceptionHandler(void); - -/// Sets the release hardware exception handler. -/// -/// This is similar to defaultExceptionHandler(), but it only prints a minimal -/// error message, and it uses a lot less code to do it. -void releaseExceptionHandler(void); - -#ifdef __cplusplus -} -#endif - -#endif // LIBNDS_NDS_ARM9_EXCEPTIONS_H__ +#include "../exceptions.h" diff --git a/include/nds/exceptions.h b/include/nds/exceptions.h new file mode 100644 index 00000000..9920ca06 --- /dev/null +++ b/include/nds/exceptions.h @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Zlib +// SPDX-FileNotice: Modified from the original version by the BlocksDS project. +// +// Copyright (C) 2005 Dave Murphy (WinterMute) + +#ifndef LIBNDS_NDS_EXCEPTIONS_H__ +#define LIBNDS_NDS_EXCEPTIONS_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/// @file nds/exceptions.h +/// +/// @brief Functions to handle hardware exceptions. +/// +/// Check https://www.problemkaputt.de/gbatek.htm#biosramusage for more +/// information. + +#include +#include + +#ifdef ARM9 +/// NDS9 BIOS debug exception vector, or 0 for no handler (mirror) +#define EXCEPTION_VECTOR (*(VoidFn *)(0x2FFFD9C)) + +/// NDS9 BIOS debug exception stack top (mirror) +#define EXCEPTION_STACK_TOP ((uint32_t *)0x2FFFD9C) +#else +/// NDS7 BIOS debug exception vector, or 0 for no handler (mirror) +#define EXCEPTION_VECTOR (*(VoidFn *)(0x380FFDC)) + +/// NDS7 BIOS debug exception stack top (mirror) +#define EXCEPTION_STACK_TOP ((uint32_t *)0x380FFDC) +#endif + +/// Pointer to the user exception handler, called from the exception handler of +/// libnds. +extern VoidFn exceptionC; + +/// Stack reserved for the user exception handler, allocated by the exception +/// handler of libnds. +extern u32 exceptionStack; + +/// Array with a copy of all the registers of when the exception occured. +extern s32 exceptionRegisters[]; + +/// Default exception handler of libnds +void enterException(void); + +/// Sets a custom hardware exception handler. +/// +/// @param handler +/// Exception handler routine. +void setExceptionHandler(VoidFn handler); + +#ifdef ARM9 +/// Sets the default debug hardware exception handler. +/// +/// This handler prints a lot of information, like the state of the CPU +/// registers when the CPU crashed. +void defaultExceptionHandler(void); + +/// Sets the release hardware exception handler. +/// +/// This is similar to defaultExceptionHandler(), but it only prints a minimal +/// error message, and it uses a lot less code to do it. +void releaseExceptionHandler(void); +#endif + +#ifdef __cplusplus +} +#endif + +#endif // LIBNDS_NDS_EXCEPTIONS_H__ diff --git a/source/arm9/system/exceptionHandler.s b/source/common/exception/exceptionHandler.s similarity index 90% rename from source/arm9/system/exceptionHandler.s rename to source/common/exception/exceptionHandler.s index 0ff99d69..109aea24 100644 --- a/source/arm9/system/exceptionHandler.s +++ b/source/common/exception/exceptionHandler.s @@ -7,8 +7,10 @@ #include .syntax unified +#ifdef ARM9 .arch armv5te .cpu arm946e-s +#endif .arm @@ -23,13 +25,19 @@ BEGIN_ASM_FUNC enterException ldr r13, =exceptionStack ldr r13, [r13] +#ifdef ARM9 // Re-enable MPU mrc CP15_REG1_CONTROL_REGISTER(r0) orr r0, r0, #CP15_CONTROL_PROTECTION_UNIT_ENABLE mcr CP15_REG1_CONTROL_REGISTER(r0) +#endif // BIOS exception stack +#ifdef ARM9 ldr r0, =0x02FFFD90 +#else + ldr r0, =0x0380FFD0 +#endif // Get r15 from BIOS exception stack ldr r2, [r0, #8] @@ -56,7 +64,12 @@ BEGIN_ASM_FUNC enterException // Get C function and call it ldr r12, =exceptionC ldr r12, [r12, #0] +#ifdef ARM9 blxne r12 +#else + mov lr, pc + bxne r12 +#endif // Restore registers ldr r12, =exceptionRegisters @@ -73,7 +86,11 @@ exceptionC: .global exceptionStack exceptionStack: +#ifdef ARM9 .word 0x02ff4000 +#else + .word 0x03810000 +#endif .global exceptionRegisters exceptionRegisters: diff --git a/source/arm9/system/exceptions.c b/source/common/exception/exceptions.c similarity index 100% rename from source/arm9/system/exceptions.c rename to source/common/exception/exceptions.c