summaryrefslogtreecommitdiff
path: root/arm9/lib/src/FX_cp.c
diff options
context:
space:
mode:
authorPikalaxALT <pikalaxalt@gmail.com>2020-05-04 16:43:58 -0400
committerPikalaxALT <pikalaxalt@gmail.com>2020-05-04 16:43:58 -0400
commita829c7c9ca642a3a42d7dbb92fe912ef74f0a52d (patch)
tree558f1e8f54a704e15624124e6f339144b14c5cd3 /arm9/lib/src/FX_cp.c
parent19b0fc98c068e6e9067a78a272adddc34e8da51c (diff)
parente2b04933a93cc1e66d8e792afdd8395e203ad26b (diff)
Merge branch 'master' of github.com:martmists/pokediamond into pikalax_work
Diffstat (limited to 'arm9/lib/src/FX_cp.c')
-rw-r--r--arm9/lib/src/FX_cp.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/arm9/lib/src/FX_cp.c b/arm9/lib/src/FX_cp.c
new file mode 100644
index 00000000..2ca9d720
--- /dev/null
+++ b/arm9/lib/src/FX_cp.c
@@ -0,0 +1,70 @@
+#include "global.h"
+#include "main.h"
+#include "fx.h"
+
+
+ARM_FUNC fx32 FX_Div(fx32 numerator, fx32 denominator){
+ FX_DivAsync(numerator, denominator);
+ return FX_GetDivResult();
+}
+
+ARM_FUNC fx32 FX_Inv(fx32 x){
+ FX_InvAsync(x);
+ return FX_GetDivResult();
+}
+
+ARM_FUNC fx32 FX_Sqrt(fx32 x){
+ if (x > 0)
+ {
+ SETREG16(HW_REG_SQRTCNT, 0x1);
+ SETREG64(HW_REG_SQRT_PARAM, (fx64)x << 32);
+ return FX_GetSqrtResult();
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+ARM_FUNC fx64c FX_GetDivResultFx64c(){
+ while (READREG16(HW_REG_DIVCNT) & 0x8000);
+ return READREG64(HW_REG_DIV_RESULT);
+}
+
+ARM_FUNC fx32 FX_GetDivResult(){
+ while (READREG16(HW_REG_DIVCNT) & 0x8000);
+ return (READREG64(HW_REG_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);
+}
+
+ARM_FUNC fx32 FX_GetSqrtResult(){
+ while (READREG16(HW_REG_SQRTCNT) & 0x8000);
+ return (READREG32(HW_REG_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);
+}
+
+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);
+}
+
+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);
+}