Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exceptions: Expose custom exception handler functionality to ARM7 #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/nds.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -135,6 +135,7 @@ extern "C" {
#include <nds/debug.h>
#include <nds/decompress.h>
#include <nds/dma.h>
#include <nds/exceptions.h>
#include <nds/fifocommon.h>
#include <nds/input.h>
#include <nds/interrupts.h>
Expand All @@ -157,7 +158,6 @@ extern "C" {
# include <nds/arm9/camera.h>
# include <nds/arm9/console.h>
# include <nds/arm9/dynamicArray.h>
# include <nds/arm9/exceptions.h>
# include <nds/arm9/guitarGrip.h>
# include <nds/arm9/image.h>
# include <nds/arm9/input.h>
Expand Down
64 changes: 2 additions & 62 deletions include/nds/arm9/exceptions.h
Original file line number Diff line number Diff line change
@@ -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 <nds/cpu.h>
#include <nds/ndstypes.h>

/// 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"
75 changes: 75 additions & 0 deletions include/nds/exceptions.h
Original file line number Diff line number Diff line change
@@ -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 <nds/cpu.h>
#include <nds/ndstypes.h>

#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__
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <nds/asminc.h>

.syntax unified
#ifdef ARM9
.arch armv5te
.cpu arm946e-s
#endif

.arm

Expand All @@ -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]
Expand All @@ -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
Expand All @@ -73,7 +86,11 @@ exceptionC:

.global exceptionStack
exceptionStack:
#ifdef ARM9
.word 0x02ff4000
#else
.word 0x03810000
#endif

.global exceptionRegisters
exceptionRegisters:
Expand Down
File renamed without changes.