Skip to content

Commit

Permalink
decompile math_util
Browse files Browse the repository at this point in the history
  • Loading branch information
RevoSucks committed Sep 3, 2023
1 parent 9116960 commit a67fbd2
Show file tree
Hide file tree
Showing 6 changed files with 1,527 additions and 6 deletions.
1,424 changes: 1,424 additions & 0 deletions include/trig_tables.inc.c

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion splat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ segments:
- [0x75F0, asm] # stage loader
- [0x8EC0, c, crash_screen] # crash handler
- [0x9D20, c, profiler]
- [0xAF00, asm] # camera code
- [0xAF00, c, math_util]
- [0xB130, c, hal_libc]
- [0xB230, c, gb_tower]
- [0xC030, c] # extra sprites loader
Expand Down Expand Up @@ -331,6 +331,8 @@ segments:
- [0x6A3B0, .data, profiler]
- [0x6A3D0, .data, gb_tower]
- [0x6A3E0, data, rom_data_6A3E0]
- [0x6A3F0, .data, math_util]
- [0x6FC00, data, rom_data_6FC00]
- [0x7A180, .data, libleo/driverominit]
- [0x7A190, data, rom_data_7A190]
- [0x7A2A0, .data, libultra/os/initialize]
Expand Down
6 changes: 2 additions & 4 deletions src/controller.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include <ultra64.h>
#include "controller.h"
#include "gb_tower.h"

// TODO: Identify/move to header
extern s16 func_8000A360(float, float);
#include "math_util.h"

struct Controller gControllers[4];
OSMesgQueue gSIEventMesgQueue;
Expand Down Expand Up @@ -77,7 +75,7 @@ void Cont_AdjustAnalogStick(struct Controller* controller) {
}

if (controller->stickMag > 0.0f) {
controller->unkE = func_8000A360(-controller->stickY, controller->stickX);
controller->unkE = MathUtil_Atan2s(-controller->stickY, controller->stickX);
}
}

Expand Down
77 changes: 77 additions & 0 deletions src/math_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <ultra64.h>
#include "math_util.h"

// this file might be the leftovers of math_util.c from sm64/mk64.

#include "trig_tables.inc.c"

/**
* Helper function for atan2s. Does a look up of the arctangent of y/x assuming
* the resulting angle is in range [0, 0x2000] (1/8 of a circle).
*/
u16 MathUtil_Atan2Lookup(f32 y, f32 x) {
u16 ret;

if (x == 0) {
ret = gArctanTable[0];
} else {
ret = gArctanTable[(s32)(y / x * 1024 + 0.5f)];
}
return ret;
}

/**
* Compute the angle from (0, 0) to (x, y) as a s16. Given that terrain is in
* the xz-plane, this is commonly called with (z, x) to get a yaw angle.
*/
s16 MathUtil_Atan2s(f32 y, f32 x) {
u16 ret;

if (x >= 0) {
if (y >= 0) {
if (y >= x) {
ret = MathUtil_Atan2Lookup(x, y);
} else {
ret = 0x4000 - MathUtil_Atan2Lookup(y, x);
}
} else {
y = -y;
if (y < x) {
ret = 0x4000 + MathUtil_Atan2Lookup(y, x);
} else {
ret = 0x8000 - MathUtil_Atan2Lookup(x, y);
}
}
} else {
x = -x;
if (y < 0) {
y = -y;
if (y >= x) {
ret = 0x8000 + MathUtil_Atan2Lookup(x, y);
} else {
ret = 0xC000 - MathUtil_Atan2Lookup(y, x);
}
} else {
if (y < x) {
ret = 0xC000 + MathUtil_Atan2Lookup(y, x);
} else {
ret = -MathUtil_Atan2Lookup(x, y);
}
}
}
return ret;
}

/**
* Gets a random 16-bit value determined by the guRandom call.
*/
s32 MathUtil_Random16(void) {
return guRandom() & 0xFFFF; // would prototype this as u16, but doesnt match with other calls.
}

/**
* Gets a random float between 0.0 to 1.0.
*/
f32 MathUtil_Random_ZeroOne(void) {
return MathUtil_Random16() / 65536.0f;
}
13 changes: 13 additions & 0 deletions src/math_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _MATH_UTIL_H_
#define _MATH_UTIL_H_

extern f32 gSineTable[];
extern f32 gCosineTable[0x1000];
extern s16 gArctanTable[0x401];

u16 MathUtil_Atan2Lookup(f32 y, f32 x);
s16 MathUtil_Atan2s(f32 y, f32 x);
s32 MathUtil_Random16(void);
f32 MathUtil_Random_ZeroOne(void);

#endif // _MATH_UTIL_H_
9 changes: 8 additions & 1 deletion tools/symbol_addrs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,11 @@ mem_pool_alloc = 0x80002AF8;
mem_pool_free = 0x80002BD0;
Util_DrawMemProfiler = 0x8000310C;
Util_Malloc = 0x80002FDC;
Util_Free = 0x80003004;
Util_Free = 0x80003004;
MathUtil_Atan2Lookup = 0x8000A300;
MathUtil_Atan2s = 0x8000A360;
gArctanTable = 0x8006E7F0;
gSineTable = 0x800697F0;
gCosineTable = 0x8006A7F0;
MathUtil_Random16 = 0x8000A4D4;
MathUtil_Random_ZeroOne = 0x8000A4F8;

0 comments on commit a67fbd2

Please sign in to comment.