summaryrefslogtreecommitdiff
path: root/arm9/lib/src/FX_cp.c
blob: a8da0a8fe579ce1ab1a055025184b97e559386ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "global.h"
#include "main.h"
#include "fx.h"

s32 FX_GetDivResult();
s32 FX_GetSqrtResult();
void FX_DivAsync(s32 numerator, s32 denominator);
void FX_InvAsync(s32 x);

s32 FX_Div(s32 numerator, s32 denominator){
    FX_DivAsync(numerator, denominator);
    return FX_GetDivResult();
}

s32 FX_Inv(s32 x){
    FX_InvAsync(x);
    return FX_GetDivResult();
}

s32 FX_Sqrt(s32 x){
    if (x > 0)
    {
        SETREG16(HW_REG_SQRTCNT, 0x1);
        SETREG64(HW_REG_SQRT_PARAM, (s64)x << 32);
        return FX_GetSqrtResult();
    }
    else
    {
        return 0;
    }
}

s64 FX_GetDivResultFx64c(){
    while (READREG16(HW_REG_DIVCNT) & 0x8000);
    return READREG64(HW_REG_DIV_RESULT);
}

s32 FX_GetDivResult(){
    while (READREG16(HW_REG_DIVCNT) & 0x8000);
    return (READREG64(HW_REG_DIV_RESULT) + (1 << (0x14 - 1))) >> 0x14;
}

void FX_InvAsync(s32 x){
    SETREG16(HW_REG_DIVCNT, 0x1);
    SETREG64(HW_REG_DIV_NUMER, (s64)0x00001000 << 32);
    SETREG64(HW_REG_DIV_DENOM, (u32)x);
}

s32 FX_GetSqrtResult(){
    while (READREG16(HW_REG_SQRTCNT) & 0x8000);
    return (READREG32(HW_REG_SQRT_RESULT) + (1 << (0xA - 1))) >> 0xA;
}

void FX_DivAsync(s32 numerator, s32 denominator){
    SETREG16(HW_REG_DIVCNT, 0x1);
    SETREG64(HW_REG_DIV_NUMER, (s64)numerator << 32);
    SETREG64(HW_REG_DIV_DENOM, (u32)denominator);
}

 s32 FX_DivS32(s32 numerator, s32 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);
 }

 s32 FX_ModS32(s32 num, s32 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);
 }