summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arm7/asm/OS_irqHandler.s8
-rw-r--r--arm7/asm/OS_irqTable.s204
-rw-r--r--arm7/asm/itcm.s10
-rw-r--r--arm7/lib/include/OS_init.h1
-rw-r--r--arm7/lib/include/OS_irqTable.h24
-rw-r--r--arm7/lib/include/OS_thread.h11
-rw-r--r--arm7/lib/include/mmap.h4
-rw-r--r--arm7/lib/src/OS_irqTable.c125
-rw-r--r--arm7/lib/src/OS_thread.c12
-rw-r--r--arm9/lib/include/OS_irqTable.h6
-rw-r--r--arm9/lib/include/OS_mutex.h7
-rw-r--r--arm9/lib/include/mmap.h3
-rw-r--r--arm9/lib/src/OS_mutex.c142
-rw-r--r--arm9/lib/src/SND_bank.c2
-rw-r--r--arm9/lib/src/SND_main.c5
-rw-r--r--include/nitro/mmap_shared.h4
16 files changed, 332 insertions, 236 deletions
diff --git a/arm7/asm/OS_irqHandler.s b/arm7/asm/OS_irqHandler.s
index b05113f1..8639392c 100644
--- a/arm7/asm/OS_irqHandler.s
+++ b/arm7/asm/OS_irqHandler.s
@@ -1,7 +1,13 @@
.include "asm/macros.inc"
.include "global.inc"
- .section .text
+ .section .bss
+
+ .global OSi_IrqThreadQueue
+OSi_IrqThreadQueue: ;0x0380770C
+ .space 0x03807714 - 0x0380770C
+
+ .section .text
arm_func_start OS_IrqHandler
OS_IrqHandler: ; 0x037F853C
diff --git a/arm7/asm/OS_irqTable.s b/arm7/asm/OS_irqTable.s
deleted file mode 100644
index 36a550fe..00000000
--- a/arm7/asm/OS_irqTable.s
+++ /dev/null
@@ -1,204 +0,0 @@
- .include "asm/macros.inc"
- .include "global.inc"
-
- .section .data
-
- .global _038075E4
-_038075E4:
- .word 0x00090008
- .word 0x000B000A
- .word 0x00040003
- .word 0x00060005
- .word 0x00000000
-
- .global OS_IRQTable
-OS_IRQTable:
- .word OSi_IrqVBlank
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OSi_IrqTimer0
- .word OSi_IrqTimer1
- .word OSi_IrqTimer2
- .word OSi_IrqTimer3
- .word OS_IrqDummy
- .word OSi_IrqDma0
- .word OSi_IrqDma1
- .word OSi_IrqDma2
- .word OSi_IrqDma3
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
- .word OS_IrqDummy
-
- .section .bss
-
- .global OSi_IrqThreadQueue
-OSi_IrqThreadQueue: ;0x0380770C
- .space 0x03807714 - 0x0380770C
-
- .global OSi_IrqCallbackInfo
-OSi_IrqCallbackInfo: ;0x03807714
- .space 0x03807718 - 0x03807714
-
- .global _03807718
-_03807718: ;0x03807718
- .space 0x0380771C - 0x03807718
-
- .global _0380771C
-_0380771C: ;0x0380771C
- .space 0x03807744 - 0x0380771C
-
- .global _03807744
-_03807744: ;0x03807744
- .space 0x03807748 - 0x03807744
-
- .global _03807748
-_03807748: ;0x03807748
- .space 0x0380774C - 0x03807748
-
- .global _0380774C
-_0380774C: ;0x0380774C
- .space 0x03807774 - 0x0380774C
-
- .global _03807774
-_03807774: ;0x03807774
- .space 0x03807780 - 0x03807774
-
- .section .text
-
- arm_func_start OSi_IrqVBlank
-OSi_IrqVBlank: ; 0x037F86B4
- stmfd sp!, {lr}
- sub sp, sp, #4
- ldr r0, _037F8700 ; =OSi_IrqCallbackInfo
- ldr r2, [r0, #96] ; 0x60
- ldr r1, _037F8704 ; =0x027FFC3C
- ldr r0, [r1]
- add r0, r0, #1
- str r0, [r1]
- cmp r2, #0
- beq _037F86E4
- mov lr, pc
- bx r2
-_037F86E4:
- ldr r1, _037F8708 ; =0x0380FFF8
- ldr r0, [r1]
- orr r0, r0, #1
- str r0, [r1]
- add sp, sp, #4
- ldmia sp!, {lr}
- bx lr
-_037F8700: .word OSi_IrqCallbackInfo
-_037F8704: .word 0x027FFC3C
-_037F8708: .word 0x0380FFF8
-
- arm_func_start OSi_IrqTimer3
-OSi_IrqTimer3: ; 0x037F870C
- mov r0, #7
- ldr ip, _037F8718 ; =OSi_IrqCallback
- bx ip
-_037F8718: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqTimer2
-OSi_IrqTimer2: ; 0x037F871C
- mov r0, #6
- ldr ip, _037F8728 ; =OSi_IrqCallback
- bx ip
-_037F8728: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqTimer1
-OSi_IrqTimer1: ; 0x037F872C
- mov r0, #5
- ldr ip, _037F8738 ; =OSi_IrqCallback
- bx ip
-_037F8738: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqTimer0
-OSi_IrqTimer0: ; 0x037F873C
- mov r0, #4
- ldr ip, _037F8748 ; =OSi_IrqCallback
- bx ip
-_037F8748: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqDma3
-OSi_IrqDma3: ; 0x037F874C
- mov r0, #3
- ldr ip, _037F8758 ; =OSi_IrqCallback
- bx ip
-_037F8758: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqDma2
-OSi_IrqDma2: ; 0x037F875C
- mov r0, #2
- ldr ip, _037F8768 ; =OSi_IrqCallback
- bx ip
-_037F8768: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqDma1
-OSi_IrqDma1: ; 0x037F876C
- mov r0, #1
- ldr ip, _037F8778 ; =OSi_IrqCallback
- bx ip
-_037F8778: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqDma0
-OSi_IrqDma0: ; 0x037F877C
- mov r0, #0
- ldr ip, _037F8788 ; =OSi_IrqCallback
- bx ip
-_037F8788: .word OSi_IrqCallback
-
- arm_func_start OSi_IrqCallback
-OSi_IrqCallback: ; 0x037F878C
- stmdb sp!, {r4, r5, lr}
- sub sp, sp, #4
- mov r3, #1
- mov r2, r0, lsl #1
- ldr r1, _037F880C ; =_038075E4
- ldrh r1, [r1, r2]
- mov r5, r3, lsl r1
- mov r1, #12
- mul r4, r0, r1
- ldr r2, _037F8810 ; =OSi_IrqCallbackInfo
- ldr r1, [r2, r4]
- mov r0, #0
- str r0, [r2, r4]
- cmp r1, #0
- beq _037F87D8
- ldr r0, _037F8814 ; =_0380771C
- ldr r0, [r0, r4]
- mov lr, pc
- bx r1
-_037F87D8:
- ldr r1, _037F8818 ; =0x0380FFF8
- ldr r0, [r1]
- orr r0, r0, r5
- str r0, [r1]
- ldr r0, _037F881C ; =_03807718
- ldr r0, [r0, r4]
- cmp r0, #0
- bne _037F8800
- mov r0, r5
- bl OS_DisableIrqMask
-_037F8800:
- add sp, sp, #4
- ldmia sp!, {r4, r5, lr}
- bx lr
-_037F880C: .word _038075E4
-_037F8810: .word OSi_IrqCallbackInfo
-_037F8814: .word _0380771C
-_037F8818: .word 0x0380FFF8
-_037F881C: .word _03807718
-
- arm_func_start OS_IrqDummy
-OS_IrqDummy: ; 0x037F8820
- bx lr
diff --git a/arm7/asm/itcm.s b/arm7/asm/itcm.s
index 48ed9199..ac320e6c 100644
--- a/arm7/asm/itcm.s
+++ b/arm7/asm/itcm.s
@@ -20,10 +20,10 @@ _027E0014:
cmp r1, #100663296 ; 0x6000000
bne _027E00AC
ldr r3, _027E00CC ; =0x027E00DC
- ldr r0, _027E00D0 ; =0x0380A3F4
+ ldr r0, _027E00D0 ; =_0380A3F4
str r3, [r0]
add r2, r5, r4
- ldr r0, _027E00D4 ; =0x0380A3FC
+ ldr r0, _027E00D4 ; =_0380A3FC
str r2, [r0]
ldr r1, _027E00D8 ; =0x027FAFCC
add r0, r3, r2
@@ -31,7 +31,7 @@ _027E0014:
beq _027E0060
bl OS_Terminate
_027E0060:
- ldr r0, _027E00D0 ; =0x0380A3F4
+ ldr r0, _027E00D0 ; =_0380A3F4
ldr r3, [r0]
mov r2, #0
mov r1, r5, lsr #2
@@ -66,6 +66,6 @@ _027E00C0: .word 0x023801B0
_027E00C4: .word 0x023A92F8
_027E00C8: .word 0x023A931C
_027E00CC: .word 0x027E00DC
-_027E00D0: .word 0x0380A3F4
-_027E00D4: .word 0x0380A3FC
+_027E00D0: .word _0380A3F4
+_027E00D4: .word _0380A3FC
_027E00D8: .word 0x027FAFCC
diff --git a/arm7/lib/include/OS_init.h b/arm7/lib/include/OS_init.h
index 6b70d2d8..b4836234 100644
--- a/arm7/lib/include/OS_init.h
+++ b/arm7/lib/include/OS_init.h
@@ -11,6 +11,7 @@
#include "OS_spinLock.h"
#include "OS_context.h"
#include "OS_interrupt.h"
+#include "OS_irqTable.h"
#include "OS_timer.h"
#include "OS_tick.h"
diff --git a/arm7/lib/include/OS_irqTable.h b/arm7/lib/include/OS_irqTable.h
new file mode 100644
index 00000000..523b96ae
--- /dev/null
+++ b/arm7/lib/include/OS_irqTable.h
@@ -0,0 +1,24 @@
+#ifndef POKEDIAMOND_ARM7_OS_IRQTABLE_H
+#define POKEDIAMOND_ARM7_OS_IRQTABLE_H
+
+#include "consts.h"
+#include "OS_interrupt.h"
+
+void OS_IrqDummy(void);
+void OSi_IrqCallback(s32 index);
+void OSi_IrqDma0(void);
+void OSi_IrqDma1(void);
+void OSi_IrqDma2(void);
+void OSi_IrqDma3(void);
+void OSi_IrqTimer0(void);
+void OSi_IrqTimer1(void);
+void OSi_IrqTimer2(void);
+void OSi_IrqTimer3(void);
+void OSi_IrqVBlank(void);
+
+static inline void OS_SetIrqCheckFlag(OSIrqMask intr)
+{
+ *(vu32 *)HW_INTR_CHECK_BUF |= (u32)intr;
+}
+
+#endif //POKEDIAMOND_ARM7_OS_IRQTABLE_H
diff --git a/arm7/lib/include/OS_thread.h b/arm7/lib/include/OS_thread.h
index b1d3a01b..701acc94 100644
--- a/arm7/lib/include/OS_thread.h
+++ b/arm7/lib/include/OS_thread.h
@@ -4,20 +4,10 @@
#include "OS_context.h"
#include "nitro/OS_thread_shared.h"
-static s32 OSi_GetUnusedThreadId(void);
-static void OSi_InsertLinkToQueue(OSThreadQueue *queue, OSThread *thread);
-static OSThread *OSi_RemoveLinkFromQueue(OSThreadQueue *queue);
-static OSThread *OSi_RemoveSpecifiedLinkFromQueue(OSThreadQueue *queue, OSThread *thread);
OSMutex *OSi_RemoveMutexLinkFromQueue(OSMutexQueue *queue);
-static void OSi_InsertThreadToList(OSThread *thread);
-static void OSi_RemoveThreadFromList(OSThread *thread);
-static void OSi_RescheduleThread(void);
void OS_InitThread(void);
void OS_CreateThread(OSThread *thread, void (*func) (void *), void *arg, void *stack, u32 stackSize, u32 prio);
void OS_ExitThread(void);
-static void OSi_ExitThread_ArgSpecified(OSThread *thread, void *arg);
-static void OSi_ExitThread(void *arg);
-static void OSi_ExitThread_Destroy(void);
void OS_JoinThread(OSThread *thread);
BOOL OS_IsThreadTerminated(const OSThread *thread);
void OS_SleepThread(OSThreadQueue *queue);
@@ -27,7 +17,6 @@ OSThread *OS_SelectThread(void);
void OS_RescheduleThread(void);
BOOL OS_SetThreadPriority(OSThread *thread, u32 prio);
void OS_Sleep(u32 msec);
-static void OSi_SleepAlarmCallback(void *arg);
OSSwitchThreadCallback OS_SetSwitchThreadCallback(OSSwitchThreadCallback callback);
u32 OS_DisableScheduler(void);
u32 OS_EnableScheduler(void);
diff --git a/arm7/lib/include/mmap.h b/arm7/lib/include/mmap.h
index f43b4bc4..12558eeb 100644
--- a/arm7/lib/include/mmap.h
+++ b/arm7/lib/include/mmap.h
@@ -9,7 +9,6 @@
#define HW_PRV_WRAM_SIZE (HW_PRV_WRAM_END-HW_PRV_WRAM)
-#define HW_SVC_STACK_SIZE 0x40
#define HW_PRV_WRAM_SYSRV_SIZE 0x40
#define HW_MAIN_MEM_SUB_SIZE (HW_MAIN_MEM_SIZE - HW_MAIN_MEM_MAIN_SIZE - HW_MAIN_MEM_SHARED_SIZE)
@@ -23,8 +22,9 @@
#define HW_PRV_WRAM_SYSRV (HW_PRV_WRAM + HW_PRV_WRAM_SIZE - HW_PRV_WRAM_SYSRV_SIZE)
+#define HW_INTR_CHECK_BUF (HW_PRV_WRAM_SYSRV + 0x38)
+
#define HW_VBLANK_COUNT_BUF (HW_MAIN_MEM + 0x007ffc3c)
-#define HW_BUTTON_XY_BUF (HW_MAIN_MEM + 0x007FFFA8)
#define HW_LOCK_ID_FLAG_SUB (HW_MAIN_MEM + 0x007fffb8)
#endif //POKEDIAMOND_ARM7_MMAP_H
diff --git a/arm7/lib/src/OS_irqTable.c b/arm7/lib/src/OS_irqTable.c
new file mode 100644
index 00000000..91c08b2e
--- /dev/null
+++ b/arm7/lib/src/OS_irqTable.c
@@ -0,0 +1,125 @@
+#include "nitro/types.h"
+#include "function_target.h"
+#include "OS_irqTable.h"
+
+OSIrqFunction OS_IRQTable[25] = {
+ OSi_IrqVBlank,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OSi_IrqTimer0,
+ OSi_IrqTimer1,
+ OSi_IrqTimer2,
+ OSi_IrqTimer3,
+ OS_IrqDummy,
+ OSi_IrqDma0,
+ OSi_IrqDma1,
+ OSi_IrqDma2,
+ OSi_IrqDma3,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy,
+ OS_IrqDummy
+};
+
+OSIrqCallbackInfo OSi_IrqCallbackInfo[9] = {
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0},
+ {NULL, 0, 0}
+};
+
+static u16 OSi_IrqCallbackInfoIndex[9] = {
+ 8, 9, 10, 11, 3, 4, 5, 6, 0
+};
+
+ARM_FUNC void OS_IrqDummy(void)
+{
+ //do nothing
+}
+
+ARM_FUNC void OSi_IrqCallback(s32 index)
+{
+ OSIrqMask imask = (1UL << OSi_IrqCallbackInfoIndex[index]);
+ void (*callback) (void *) = OSi_IrqCallbackInfo[index].func;
+
+ OSi_IrqCallbackInfo[index].func = NULL;
+
+ if (callback)
+ {
+ (callback)(OSi_IrqCallbackInfo[index].arg);
+ }
+
+ OS_SetIrqCheckFlag(imask);
+
+ if (!OSi_IrqCallbackInfo[index].enable)
+ {
+ (void)OS_DisableIrqMask(imask);
+ }
+}
+
+ARM_FUNC void OSi_IrqDma0(void)
+{
+ OSi_IrqCallback(0);
+}
+
+ARM_FUNC void OSi_IrqDma1(void)
+{
+ OSi_IrqCallback(1);
+}
+
+ARM_FUNC void OSi_IrqDma2(void)
+{
+ OSi_IrqCallback(2);
+}
+ARM_FUNC void OSi_IrqDma3(void)
+{
+ OSi_IrqCallback(3);
+}
+
+ARM_FUNC void OSi_IrqTimer0(void)
+{
+ OSi_IrqCallback(4);
+}
+
+ARM_FUNC void OSi_IrqTimer1(void)
+{
+ OSi_IrqCallback(5);
+}
+
+ARM_FUNC void OSi_IrqTimer2(void)
+{
+ OSi_IrqCallback(6);
+}
+
+ARM_FUNC void OSi_IrqTimer3(void)
+{
+ OSi_IrqCallback(7);
+}
+
+ARM_FUNC void OSi_IrqVBlank(void)
+{
+ void (*callback) (void) = (void (*)(void))OSi_IrqCallbackInfo[8].func;
+
+ (*(u32 *)HW_VBLANK_COUNT_BUF)++;
+
+ if (callback)
+ {
+ (callback)();
+ }
+
+ OS_SetIrqCheckFlag(1UL << 0);
+}
diff --git a/arm7/lib/src/OS_thread.c b/arm7/lib/src/OS_thread.c
index be3f611d..6db0b4f7 100644
--- a/arm7/lib/src/OS_thread.c
+++ b/arm7/lib/src/OS_thread.c
@@ -29,6 +29,18 @@ static s32 OSi_ThreadIdCount = 0;
OSThread OSi_LauncherThread;
OSThread OSi_IdleThread;
+static s32 OSi_GetUnusedThreadId(void);
+static void OSi_InsertLinkToQueue(OSThreadQueue *queue, OSThread *thread);
+static OSThread *OSi_RemoveLinkFromQueue(OSThreadQueue *queue);
+static OSThread *OSi_RemoveSpecifiedLinkFromQueue(OSThreadQueue *queue, OSThread *thread);
+static void OSi_InsertThreadToList(OSThread *thread);
+static void OSi_RemoveThreadFromList(OSThread *thread);
+static void OSi_RescheduleThread(void);
+static void OSi_ExitThread_ArgSpecified(OSThread *thread, void *arg);
+static void OSi_ExitThread(void *arg);
+static void OSi_ExitThread_Destroy(void);
+static void OSi_SleepAlarmCallback(void *arg);
+
ARM_FUNC static s32 OSi_GetUnusedThreadId(void)
{
return ++OSi_ThreadIdCount;
diff --git a/arm9/lib/include/OS_irqTable.h b/arm9/lib/include/OS_irqTable.h
index 8cd7b7b3..10165a11 100644
--- a/arm9/lib/include/OS_irqTable.h
+++ b/arm9/lib/include/OS_irqTable.h
@@ -1,5 +1,5 @@
-#ifndef POKEDIAMOND_OS_IRQTABLE_H
-#define POKEDIAMOND_OS_IRQTABLE_H
+#ifndef POKEDIAMOND_ARM9_OS_IRQTABLE_H
+#define POKEDIAMOND_ARM9_OS_IRQTABLE_H
#include "consts.h"
#include "OS_interrupt.h"
@@ -20,4 +20,4 @@ static inline void OS_SetIrqCheckFlag(OSIrqMask intr)
*(vu32 *)HW_INTR_CHECK_BUF |= (u32)intr;
}
-#endif //POKEDIAMOND_OS_IRQTABLE_H
+#endif //POKEDIAMOND_ARM9_OS_IRQTABLE_H
diff --git a/arm9/lib/include/OS_mutex.h b/arm9/lib/include/OS_mutex.h
index 9da89c69..23e4337e 100644
--- a/arm9/lib/include/OS_mutex.h
+++ b/arm9/lib/include/OS_mutex.h
@@ -4,7 +4,14 @@
#include "OS_thread.h"
#include "OS_context.h"
#include "nitro/OS_mutex_shared.h"
+#include "nitro/types.h"
+void OS_InitMutex(OSMutex *mutex);
+void OS_LockMutex(OSMutex *mutex);
+void OS_UnlockMutex(OSMutex *mutex);
void OSi_UnlockAllMutex(OSThread * thread);
+BOOL OS_TryLockMutex(OSMutex *mutex);
+void OSi_EnqueueTail(OSThread *thread, OSMutex *mutex);
+void OSi_DequeueItem(OSThread *thread, OSMutex *mutex);
#endif //POKEDIAMOND_ARM9_OS_MUTEX_H
diff --git a/arm9/lib/include/mmap.h b/arm9/lib/include/mmap.h
index 143b609b..8b30ffc8 100644
--- a/arm9/lib/include/mmap.h
+++ b/arm9/lib/include/mmap.h
@@ -32,7 +32,6 @@ extern u32 SDK_AUTOLOAD_DTCM_START[];
#define HW_MAIN_MEM_EX_END (HW_MAIN_MEM + HW_MAIN_MEM_EX_SIZE)
#define HW_MAIN_MEM_SHARED (HW_MAIN_MEM_EX_END - HW_MAIN_MEM_SHARED_SIZE)
#define HW_DTCM_SVC_STACK_END (HW_DTCM + 0x00003fc0)
-#define HW_SVC_STACK_SIZE 0x00000040
#define HW_DTCM_SVC_STACK (HW_DTCM_SVC_STACK_END - HW_SVC_STACK_SIZE)
#define HW_DTCM_IRQ_STACK_END (HW_DTCM_SVC_STACK)
@@ -49,8 +48,6 @@ extern u32 SDK_AUTOLOAD_DTCM_START[];
#define HW_SHARED_LOCK_BUF (HW_MAIN_MEM + 0x007fffc0)
#define HW_CARD_LOCK_BUF (HW_MAIN_MEM + 0x007fffe0)
-#define HW_BUTTON_XY_BUF (HW_MAIN_MEM + 0x007fffa8)
-
#define OSi_MAIN_ARENA_HI_DEFAULT (HW_MAIN_MEM_MAIN_END)
#define OSi_MAINEX_ARENA_HI_DEFAULT (HW_MAIN_MEM + HW_MAIN_MEM_DEBUGGER_OFFSET)
#define HW_ITCM_ARENA_HI_DEFAULT (HW_ITCM + HW_ITCM_SIZE)
diff --git a/arm9/lib/src/OS_mutex.c b/arm9/lib/src/OS_mutex.c
new file mode 100644
index 00000000..5eb999a9
--- /dev/null
+++ b/arm9/lib/src/OS_mutex.c
@@ -0,0 +1,142 @@
+#include "nitro/types.h"
+#include "function_target.h"
+#include "OS_mutex.h"
+#include "OS_system.h"
+
+ARM_FUNC void OS_InitMutex(OSMutex *mutex)
+{
+ OS_InitThreadQueue(&mutex->queue);
+ mutex->thread = NULL;
+ mutex->count = 0;
+}
+
+ARM_FUNC void OS_LockMutex(OSMutex *mutex)
+{
+ OSIntrMode prevIntrMode = OS_DisableInterrupts();
+ OSThread *currentThread = OS_GetCurrentThread();
+
+ OSThread *ownerThread;
+ for (;;)
+ {
+ ownerThread = ((volatile OSMutex *)mutex)->thread;
+
+ if (ownerThread == NULL)
+ {
+ mutex->thread = currentThread;
+ mutex->count++;
+ OSi_EnqueueTail(currentThread, mutex);
+ break;
+ }
+ else if (ownerThread == currentThread)
+ {
+ mutex->count++;
+ break;
+ }
+ else
+ {
+ currentThread->mutex = mutex;
+ OS_SleepThread(&mutex->queue);
+ currentThread->mutex = NULL;
+ }
+ }
+
+ (void)OS_RestoreInterrupts(prevIntrMode);
+}
+
+ARM_FUNC void OS_UnlockMutex(OSMutex *mutex)
+{
+ OSIntrMode prevIntrMode = OS_DisableInterrupts();
+ OSThread *currentThread = OS_GetCurrentThread();
+
+ if (mutex->thread == currentThread && --mutex->count == 0)
+ {
+ OSi_DequeueItem(currentThread, mutex);
+ mutex->thread = NULL;
+
+ OS_WakeupThread(&mutex->queue);
+ }
+
+ (void)OS_RestoreInterrupts(prevIntrMode);
+}
+
+ARM_FUNC void OSi_UnlockAllMutex(OSThread *thread)
+{
+ OSMutex *mutex;
+ while (thread->mutexQueue.head)
+ {
+ mutex = OSi_RemoveMutexLinkFromQueue(&thread->mutexQueue);
+
+ mutex->count = 0;
+ mutex->thread = NULL;
+ OS_WakeupThread(&mutex->queue);
+ }
+}
+
+ARM_FUNC BOOL OS_TryLockMutex(OSMutex *mutex)
+{
+ OSIntrMode prevIntrMode = OS_DisableInterrupts();
+ OSThread *currentThread = OS_GetCurrentThread();
+ BOOL locked;
+
+ if (mutex->thread == NULL)
+ {
+ mutex->thread = currentThread;
+ mutex->count++;
+ OSi_EnqueueTail(currentThread, mutex);
+ locked = TRUE;
+ }
+ else if (mutex->thread == currentThread)
+ {
+ mutex->count++;
+ locked = TRUE;
+ }
+ else
+ {
+ locked = FALSE;
+ }
+
+ (void)OS_RestoreInterrupts(prevIntrMode);
+ return locked;
+}
+
+ARM_FUNC void OSi_EnqueueTail(OSThread *thread, OSMutex *mutex)
+{
+ OSMutex *prev = thread->mutexQueue.tail;
+
+ if (!prev)
+ {
+ thread->mutexQueue.head = mutex;
+ }
+ else
+ {
+ prev->link.next = mutex;
+ }
+
+ mutex->link.prev = prev;
+ mutex->link.next = NULL;
+ thread->mutexQueue.tail = mutex;
+}
+
+ARM_FUNC void OSi_DequeueItem(OSThread *thread, OSMutex *mutex)
+{
+ OSMutex *next = mutex->link.next;
+ OSMutex *prev = mutex->link.prev;
+
+ if (!next)
+ {
+ thread->mutexQueue.tail = prev;
+ }
+ else
+ {
+ next->link.prev = prev;
+ }
+
+ if(!prev)
+ {
+ thread->mutexQueue.head = next;
+ }
+ else
+ {
+ prev->link.next = next;
+ }
+}
diff --git a/arm9/lib/src/SND_bank.c b/arm9/lib/src/SND_bank.c
index 7f5bd94a..44075454 100644
--- a/arm9/lib/src/SND_bank.c
+++ b/arm9/lib/src/SND_bank.c
@@ -1,8 +1,6 @@
#include "SND_bank.h"
#include "OS_mutex.h"
-void OS_LockMutex(struct OSMutex *);
-void OS_UnlockMutex(struct OSMutex *);
void DC_StoreRange(const void *, u32);
/*
diff --git a/arm9/lib/src/SND_main.c b/arm9/lib/src/SND_main.c
index 34b839b1..f97b0873 100644
--- a/arm9/lib/src/SND_main.c
+++ b/arm9/lib/src/SND_main.c
@@ -8,11 +8,6 @@
static struct OSMutex sSndMutex;
static s32 sSndInitialized;
-// TODO remove these declarations once we have the functions in the headers
-void OS_InitMutex(struct OSMutex *);
-void OS_UnlockMutex(struct OSMutex *);
-void OS_LockMutex(struct OSMutex *);
-
ARM_FUNC void SND_Init(void) {
if (sSndInitialized)
return;
diff --git a/include/nitro/mmap_shared.h b/include/nitro/mmap_shared.h
index 60c0c1cb..d99044da 100644
--- a/include/nitro/mmap_shared.h
+++ b/include/nitro/mmap_shared.h
@@ -27,4 +27,8 @@
#define HW_INIT_LOCK_BUF (HW_MAIN_MEM + 0x007ffff0)
#define HW_MAIN_MEM_MAIN_END (HW_MAIN_MEM + HW_MAIN_MEM_MAIN_SIZE)
+#define HW_SVC_STACK_SIZE 0x00000040
+
+#define HW_BUTTON_XY_BUF (HW_MAIN_MEM + 0x007FFFA8)
+
#endif //POKEDIAMOND_MMAP_SHARED_H