summaryrefslogtreecommitdiff
path: root/arm9/lib/src
diff options
context:
space:
mode:
authorMade <made111@gmx.de>2020-05-08 23:06:05 +0200
committerMade <made111@gmx.de>2020-05-08 23:06:05 +0200
commitae744a2ea5a31f19330aedd1e264c88b97873c1c (patch)
treea30d34cbfba4e518eb6a7d4a52154ed86d41a428 /arm9/lib/src
parent6d3d11491cf6e8289822d2c5ba9cd7e309a5ce11 (diff)
parentf76cec09f8a2995244256d4b097d9eed2bebd34a (diff)
Merge branch 'master' of https://github.com/martmists/pokediamond
Diffstat (limited to 'arm9/lib/src')
-rw-r--r--arm9/lib/src/FX_cp.c48
-rw-r--r--arm9/lib/src/FX_vec.c44
-rw-r--r--arm9/lib/src/OS_arena.c2
-rw-r--r--arm9/lib/src/OS_emulator.c18
-rw-r--r--arm9/lib/src/OS_init.c2
-rw-r--r--arm9/lib/src/OS_interrupt.c79
-rw-r--r--arm9/lib/src/OS_reset.c63
-rw-r--r--arm9/lib/src/OS_system.c1
-rw-r--r--arm9/lib/src/OS_tcm.c13
-rw-r--r--arm9/lib/src/OS_terminate_proc.c21
10 files changed, 241 insertions, 50 deletions
diff --git a/arm9/lib/src/FX_cp.c b/arm9/lib/src/FX_cp.c
index 2ca9d720..08443dc8 100644
--- a/arm9/lib/src/FX_cp.c
+++ b/arm9/lib/src/FX_cp.c
@@ -16,8 +16,8 @@ ARM_FUNC fx32 FX_Inv(fx32 x){
ARM_FUNC fx32 FX_Sqrt(fx32 x){
if (x > 0)
{
- SETREG16(HW_REG_SQRTCNT, 0x1);
- SETREG64(HW_REG_SQRT_PARAM, (fx64)x << 32);
+ reg_CP_SQRTCNT = 0x1;
+ reg_CP_SQRT_PARAM = (fx64)x << 32;
return FX_GetSqrtResult();
}
else
@@ -27,44 +27,44 @@ ARM_FUNC fx32 FX_Sqrt(fx32 x){
}
ARM_FUNC fx64c FX_GetDivResultFx64c(){
- while (READREG16(HW_REG_DIVCNT) & 0x8000);
- return READREG64(HW_REG_DIV_RESULT);
+ while (reg_CP_DIVCNT & 0x8000);
+ return reg_CP_DIV_RESULT;
}
ARM_FUNC fx32 FX_GetDivResult(){
- while (READREG16(HW_REG_DIVCNT) & 0x8000);
- return (READREG64(HW_REG_DIV_RESULT) + (1 << (0x14 - 1))) >> 0x14;
+ while (reg_CP_DIVCNT & 0x8000);
+ return (reg_CP_DIV_RESULT + (1 << (0x14 - 1))) >> 0x14;
}
ARM_FUNC void FX_InvAsync(fx32 x){
- SETREG16(HW_REG_DIVCNT, 0x1);
- SETREG64(HW_REG_DIV_NUMER, (fx64)0x00001000 << 32);
- SETREG64(HW_REG_DIV_DENOM, (u32)x);
+ reg_CP_DIVCNT = 0x1;
+ reg_CP_DIV_NUMER = (fx64)0x00001000 << 32;
+ reg_CP_DIV_DENOM = (u32)x;
}
ARM_FUNC fx32 FX_GetSqrtResult(){
- while (READREG16(HW_REG_SQRTCNT) & 0x8000);
- return (READREG32(HW_REG_SQRT_RESULT) + (1 << (0xA - 1))) >> 0xA;
+ while (reg_CP_SQRTCNT & 0x8000);
+ return (reg_CP_SQRT_RESULT + (1 << (0xA - 1))) >> 0xA;
}
ARM_FUNC void FX_DivAsync(fx32 numerator, fx32 denominator){
- SETREG16(HW_REG_DIVCNT, 0x1);
- SETREG64(HW_REG_DIV_NUMER, (fx64)numerator << 32);
- SETREG64(HW_REG_DIV_DENOM, (u32)denominator);
+ reg_CP_DIVCNT = 0x1;
+ reg_CP_DIV_NUMER = (fx64)numerator << 32;
+ reg_CP_DIV_DENOM = (u32)denominator;
}
ARM_FUNC fx32 FX_DivS32(fx32 numerator, fx32 denominator){
- SETREG16(HW_REG_DIVCNT, 0x0);
- SETREG32(HW_REG_DIV_NUMER, (u32)numerator); //32bit write for some reason
- SETREG64(HW_REG_DIV_DENOM, (u32)denominator);
- while (READREG16(HW_REG_DIVCNT) & 0x8000);
- return READREG32(HW_REG_DIV_RESULT);
+ reg_CP_DIVCNT = 0x0;
+ *(REGType32 *)REG_DIV_NUMER_ADDR = (u32)numerator; //32bit write for some reason
+ reg_CP_DIV_DENOM = (u32)denominator;
+ while (reg_CP_DIVCNT & 0x8000);
+ return *(REGType32 *)REG_DIV_RESULT_ADDR;
}
ARM_FUNC fx32 FX_ModS32(fx32 num, fx32 mod){
- SETREG16(HW_REG_DIVCNT, 0x0);
- SETREG32(HW_REG_DIV_NUMER, (u32)num); //32bit write for some reason
- SETREG64(HW_REG_DIV_DENOM, (u32)mod);
- while (READREG16(HW_REG_DIVCNT) & 0x8000);
- return READREG32(HW_REG_DIVREM_RESULT);
+ reg_CP_DIVCNT = 0x0;
+ *(REGType32 *)REG_DIV_NUMER_ADDR = (u32)num; //32bit write for some reason
+ reg_CP_DIV_DENOM = (u32)mod;
+ while (reg_CP_DIVCNT & 0x8000);
+ return *(REGType32 *)REG_DIVREM_RESULT_ADDR;
}
diff --git a/arm9/lib/src/FX_vec.c b/arm9/lib/src/FX_vec.c
index af36fe89..95805f33 100644
--- a/arm9/lib/src/FX_vec.c
+++ b/arm9/lib/src/FX_vec.c
@@ -55,10 +55,10 @@ ARM_FUNC fx32 VEC_Mag(struct Vecx32 *a){
fx64 l2 = (fx64)a->x * a->x;
l2 += (fx64)a->y * a->y;
l2 += (fx64)a->z * a->z;
- SETREG16(HW_REG_SQRTCNT, 0x1);
- SETREG64(HW_REG_SQRT_PARAM, l2 * 4);
- while (READREG16(HW_REG_SQRTCNT) & 0x8000); //wait for coprocessor to finish
- return ((fx32)READREG32(HW_REG_SQRT_RESULT) + 1) >> 1;
+ reg_CP_SQRTCNT = 0x1;
+ reg_CP_SQRT_PARAM = l2 * 4;
+ while (reg_CP_SQRTCNT & 0x8000); //wait for coprocessor to finish
+ return ((fx32)reg_CP_SQRT_RESULT + 1) >> 1;
}
ARM_FUNC void VEC_Normalize(struct Vecx32 *a, struct Vecx32 *dst){
@@ -66,15 +66,15 @@ ARM_FUNC void VEC_Normalize(struct Vecx32 *a, struct Vecx32 *dst){
l2 += (fx64)a->y * a->y;
l2 += (fx64)a->z * a->z;
//1/sqrt(l) is computed by calculating sqrt(l)*(1/l)
- SETREG16(HW_REG_DIVCNT, 0x2);
- SETREG64(HW_REG_DIV_NUMER, 0x0100000000000000);
- SETREG64(HW_REG_DIV_DENOM, l2);
- SETREG16(HW_REG_SQRTCNT, 0x1);
- SETREG64(HW_REG_SQRT_PARAM, l2 * 4);
- while (READREG16(HW_REG_SQRTCNT) & 0x8000); //wait for sqrt to finish
- fx32 sqrtresult = READREG32(HW_REG_SQRT_RESULT);
- while (READREG16(HW_REG_DIVCNT) & 0x8000); //wait for division to finish
- l2 = READREG64(HW_REG_DIV_RESULT);
+ reg_CP_DIVCNT = 0x2;
+ reg_CP_DIV_NUMER = 0x0100000000000000;
+ reg_CP_DIV_DENOM = l2;
+ reg_CP_SQRTCNT = 0x1;
+ reg_CP_SQRT_PARAM = l2 * 4;
+ while (reg_CP_SQRTCNT & 0x8000); //wait for sqrt to finish
+ fx32 sqrtresult = reg_CP_SQRT_RESULT;
+ while (reg_CP_DIVCNT & 0x8000); //wait for division to finish
+ l2 = reg_CP_DIV_RESULT;
l2 = sqrtresult * l2;
dst->x = (l2 * a->x + (1LL << (0x2D - 1))) >> 0x2D;
dst->y = (l2 * a->y + (1LL << (0x2D - 1))) >> 0x2D;
@@ -86,15 +86,15 @@ ARM_FUNC void VEC_Fx16Normalize(struct Vecx16 *a, struct Vecx16 *dst){
l2 += a->y * a->y;
l2 += a->z * a->z;
//1/sqrt(l) is computed by calculating sqrt(l)*(1/l)
- SETREG16(HW_REG_DIVCNT, 0x2);
- SETREG64(HW_REG_DIV_NUMER, 0x0100000000000000);
- SETREG64(HW_REG_DIV_DENOM, l2);
- SETREG16(HW_REG_SQRTCNT, 0x1);
- SETREG64(HW_REG_SQRT_PARAM, l2 * 4);
- while (READREG16(HW_REG_SQRTCNT) & 0x8000); //wait for sqrt to finish
- fx32 sqrtresult = READREG32(HW_REG_SQRT_RESULT);
- while (READREG16(HW_REG_DIVCNT) & 0x8000); //wait for division to finish
- l2 = READREG64(HW_REG_DIV_RESULT);
+ reg_CP_DIVCNT = 0x2;
+ reg_CP_DIV_NUMER = 0x0100000000000000;
+ reg_CP_DIV_DENOM = l2;
+ reg_CP_SQRTCNT = 0x1;
+ reg_CP_SQRT_PARAM = l2 * 4;
+ while (reg_CP_SQRTCNT & 0x8000); //wait for sqrt to finish
+ fx32 sqrtresult = reg_CP_SQRT_RESULT;
+ while (reg_CP_DIVCNT & 0x8000); //wait for division to finish
+ l2 = reg_CP_DIV_RESULT;
l2 = sqrtresult * l2;
dst->x = (l2 * a->x + (1LL << (0x2D - 1))) >> 0x2D;
dst->y = (l2 * a->y + (1LL << (0x2D - 1))) >> 0x2D;
diff --git a/arm9/lib/src/OS_arena.c b/arm9/lib/src/OS_arena.c
index eb04e60b..ae05b49c 100644
--- a/arm9/lib/src/OS_arena.c
+++ b/arm9/lib/src/OS_arena.c
@@ -5,8 +5,8 @@
#include "consts.h"
#include "OS_arena.h"
#include "OS_protectionRegion.h"
+#include "OS_emulator.h"
-extern u32 OS_GetConsoleType();
extern BOOL OSi_MainExArenaEnabled;
extern BOOL OSi_Initialized; // TODO: located at 0x021d36f0
void SDK_MAIN_ARENA_LO(); // TODO: technically this should be defined in the lcf
diff --git a/arm9/lib/src/OS_emulator.c b/arm9/lib/src/OS_emulator.c
new file mode 100644
index 00000000..1be0e163
--- /dev/null
+++ b/arm9/lib/src/OS_emulator.c
@@ -0,0 +1,18 @@
+//
+// Created by red031000 on 2020-05-05.
+//
+
+#include "OS_emulator.h"
+#include "function_target.h"
+
+extern u32 OSi_ConsoleTypeCache; //todo fix bss
+
+ARM_FUNC BOOL OS_IsRunOnEmulator() {
+ return FALSE;
+}
+
+ARM_FUNC u32 OS_GetConsoleType() {
+ OSi_ConsoleTypeCache = OS_CONSOLE_NITRO | OS_CONSOLE_DEV_CARD | OS_CONSOLE_SIZE_4MB;
+
+ return OSi_ConsoleTypeCache;
+}
diff --git a/arm9/lib/src/OS_init.c b/arm9/lib/src/OS_init.c
index 90b01ffa..7467c672 100644
--- a/arm9/lib/src/OS_init.c
+++ b/arm9/lib/src/OS_init.c
@@ -7,14 +7,12 @@
extern void PXI_Init();
extern void OS_InitLock();
-extern void OS_InitIrqTable();
extern void OS_SetIrqStackChecker();
extern void OS_InitException();
extern void MI_Init();
extern void OS_InitVAlarm();
extern void OSi_InitVramExclusive();
extern void OS_InitThread();
-extern void OS_InitReset();
extern void CTRDG_Init();
extern void CARD_Init();
extern void PM_Init();
diff --git a/arm9/lib/src/OS_interrupt.c b/arm9/lib/src/OS_interrupt.c
new file mode 100644
index 00000000..f7a6d005
--- /dev/null
+++ b/arm9/lib/src/OS_interrupt.c
@@ -0,0 +1,79 @@
+//
+// Created by red031000 on 2020-05-07.
+//
+
+#include "consts.h"
+#include "function_target.h"
+#include "OS_interrupt.h"
+#include "OS_thread.h"
+
+#pragma optimize_for_size on
+
+extern OSThreadQueue OSi_IrqThreadQueue;
+extern OSIrqMask OS_EnableIrqMask(OSIrqMask intr);
+
+ARM_FUNC void OS_InitIrqTable() {
+ OS_InitThreadQueue(&OSi_IrqThreadQueue);
+}
+
+ARM_FUNC void OS_SetIrqFunction(OSIrqMask intrBit, OSIrqFunction function) {
+ s32 i;
+ OSIrqCallbackInfo *info;
+
+ for (i = 0; i < 0x16; i++) {
+ if (intrBit & 1) {
+ info = NULL;
+
+ if (8 <= i && i <= 11) {
+ info = &OSi_IrqCallbackInfo[i - 8];
+ }
+ else if (3 <= i && i <= 6) {
+ info = &OSi_IrqCallbackInfo[i - 3 + 4];
+ }
+ else {
+ OS_IRQTable[i] = function;
+ }
+
+ if (info) {
+ info->func = (void (*)(void *))function;
+ info->arg = 0;
+ info->enable = TRUE;
+ }
+ }
+ intrBit >>= 1;
+ }
+}
+
+ARM_FUNC OSIrqFunction OS_GetIrqFunction(OSIrqMask intrBit) {
+ s32 i = 0;
+ OSIrqFunction *funcPtr = &OS_IRQTable[0];
+
+ do {
+ if (intrBit & 1)
+ {
+ if (8 <= i && i <= 11) {
+ i = i - 8;
+ return (void (*)(void))OSi_IrqCallbackInfo[i].func;
+ }
+ else if (3 <= i && i <= 6) {
+ i++;
+ return (void (*)(void))OSi_IrqCallbackInfo[i].func;
+ }
+
+ return *funcPtr;
+ }
+ intrBit >>= 1;
+ funcPtr++;
+ i++;
+ } while (i < 0x16);
+ return 0;
+}
+
+ARM_FUNC void OSi_EnterDmaCallback(u32 dmaNo, void (*callback) (void *), void *arg)
+{
+ OSIrqMask mask = 1UL << (dmaNo + 8);
+ OSi_IrqCallbackInfo[dmaNo].func = callback;
+ OSi_IrqCallbackInfo[dmaNo].arg = arg;
+
+ OSi_IrqCallbackInfo[dmaNo].enable = OS_EnableIrqMask(mask) & mask;
+}
diff --git a/arm9/lib/src/OS_reset.c b/arm9/lib/src/OS_reset.c
new file mode 100644
index 00000000..28d815a9
--- /dev/null
+++ b/arm9/lib/src/OS_reset.c
@@ -0,0 +1,63 @@
+//
+// Created by red031000 on 2020-05-06.
+//
+
+#include "function_target.h"
+#include "OS_reset.h"
+#include "MB_mb.h"
+#include "OS_terminate_proc.h"
+
+extern u16 OSi_IsInitReset;
+extern vu16 OSi_IsResetOccurred;
+extern void PXI_Init();
+extern u32 PXI_IsCallbackReady(u32 param1, u32 param2);
+extern void PXI_SetFifoRecvCallback(u32 param1, void* callback);
+extern u32 PXI_SendWordByFifo(u32 param1, u32 data, u32 param2);
+extern void CARD_LockRom(u16 lockId);
+extern u32 OS_GetLockID();
+extern void MI_StopDma(u32 dma);
+extern void OS_SetIrqMask(u32 mask);
+extern void OS_ResetRequestIrqMask(u32 mask);
+extern void OSi_DoResetSystem(); //in itcm, should technically be in this file
+
+ARM_FUNC void OS_InitReset() {
+ if (OSi_IsInitReset) {
+ return;
+ }
+ OSi_IsInitReset = TRUE;
+ PXI_Init();
+ while (!PXI_IsCallbackReady(PXI_FIFO_TAG_OS, PXI_PROC_ARM7)) { }
+
+ PXI_SetFifoRecvCallback(PXI_FIFO_TAG_OS, OSi_CommonCallback);
+}
+
+ARM_FUNC static void OSi_CommonCallback(PXIFifoTag tag, u32 data, BOOL err) {
+#pragma unused(tag, err) //needed because otherwise -W all errors
+ u16 command = (u16)((data & OS_PXI_COMMAND_MASK) >> OS_PXI_COMMAND_SHIFT);
+ if (command == OS_PXI_COMMAND_RESET)
+ {
+ OSi_IsResetOccurred = TRUE;
+ return;
+ }
+ OS_Terminate();
+}
+
+ARM_FUNC static void OSi_SendToPxi(u16 data) {
+ while (PXI_SendWordByFifo(PXI_FIFO_TAG_OS, (u32)data << 0x8, FALSE)) {}
+}
+
+ARM_FUNC void OS_ResetSystem(u32 parameter) {
+ if (MB_IsMultiBootChild()) {
+ OS_Terminate();
+ }
+ CARD_LockRom((u16)OS_GetLockID());
+ MI_StopDma(0);
+ MI_StopDma(1);
+ MI_StopDma(2);
+ MI_StopDma(3);
+ OS_SetIrqMask(0x40000);
+ OS_ResetRequestIrqMask((u32)~0);
+ *(u32 *)HW_RESET_PARAMETER_BUF = parameter;
+ OSi_SendToPxi(OS_PXI_COMMAND_RESET);
+ OSi_DoResetSystem(); //oh boy this is in itcm, that's gonna be fun to deal with Kappa
+}
diff --git a/arm9/lib/src/OS_system.c b/arm9/lib/src/OS_system.c
index c2b08681..e5dfcb47 100644
--- a/arm9/lib/src/OS_system.c
+++ b/arm9/lib/src/OS_system.c
@@ -2,7 +2,6 @@
// Created by mart on 4/23/20.
//
-#include "function_target.h"
#include "OS_system.h"
ARM_FUNC asm OSIntrMode OS_EnableInterrupts() {
diff --git a/arm9/lib/src/OS_tcm.c b/arm9/lib/src/OS_tcm.c
new file mode 100644
index 00000000..5a168c58
--- /dev/null
+++ b/arm9/lib/src/OS_tcm.c
@@ -0,0 +1,13 @@
+//
+// Created by red031000 on 2020-05-05.
+//
+
+#include "OS_tcm.h"
+#include "function_target.h"
+
+ARM_FUNC asm u32 OS_GetDTCMAddress() {
+ mrc p15, 0x0, r0, c9, c1, 0x0
+ ldr r1, =OSi_TCM_REGION_BASE_MASK
+ and r0, r0, r1
+ bx lr
+}
diff --git a/arm9/lib/src/OS_terminate_proc.c b/arm9/lib/src/OS_terminate_proc.c
new file mode 100644
index 00000000..eb267c6b
--- /dev/null
+++ b/arm9/lib/src/OS_terminate_proc.c
@@ -0,0 +1,21 @@
+//
+// Created by red031000 on 2020-05-07.
+//
+
+#include "types.h"
+#include "OS_terminate_proc.h"
+#include "function_target.h"
+#include "OS_system.h"
+
+ARM_FUNC void OS_Terminate() {
+ while (TRUE) {
+ (void)OS_DisableInterrupts();
+ OS_Halt();
+ }
+}
+
+ARM_FUNC asm void OS_Halt() {
+ mov r0, #0x0
+ mcr p15, 0x0, r0, c7, c0, 0x4
+ bx lr
+}