Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions include/ctr/gte.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@
#include <ctr_math.h>
#include <psn00bsdk/include/inline_c.h>

enum GTE_ROW_INDEX
typedef enum GTE_ROW_INDEX
{
GTE_ROW_INDEX_0,
GTE_ROW_INDEX_1,
GTE_ROW_INDEX_2,
};
} GTE_ROW_INDEX;

enum GTE_MATRIX
typedef enum GTE_MATRIX
{
GTE_MATRIX_ROT,
GTE_MATRIX_LIGHT,
GTE_MATRIX_COLOR,
};
GTE_MATRIX_NONE,
} GTE_MATRIX;

enum GTE_VECTOR
typedef enum GTE_VECTOR
{
GTE_VECTOR_0,
GTE_VECTOR_1,
GTE_VECTOR_2,
GTE_VECTOR_IR,
};
GTE_VECTOR_0, /* SVec */
GTE_VECTOR_1, /* SVec */
GTE_VECTOR_2, /* SVec */
GTE_VECTOR_IR, /* Vec */
} GTE_VECTOR;

enum GTE_MAC
typedef enum GTE_MAC
{
GTE_MAC_0,
GTE_MAC_1,
GTE_MAC_2,
GTE_MAC_3,
GTE_MAC_VEC, /* 1-3 */
};
GTE_MAC_VEC, /* 1-3, Vec */
} GTE_MAC;

/* HELPERS */
#define _CAT(a, b) a##b
Expand All @@ -44,10 +45,10 @@ enum GTE_MAC
#define _gte_loadSVecMatrix_GTE_MATRIX_ROT_GTE_ROW_INDEX_0(v) gte_ldsvrtrow0(v)
#define _gte_loadSVecMatrix_GTE_MATRIX_LIGHT_GTE_ROW_INDEX_0(v) gte_ldsvllrow0(v)
#define _gte_loadSVecMatrix_GTE_MATRIX_COLOR_GTE_ROW_INDEX_0(v) gte_ldsvlcrow0(v)
#define _gte_loadSVec_GTE_VECTOR_0(v) gte_ldv0(v)
#define _gte_loadSVec_GTE_VECTOR_1(v) gte_ldv1(v)
#define _gte_loadSVec_GTE_VECTOR_2(v) gte_ldv2(v)
#define _gte_loadSVec_GTE_VECTOR_IR(v) gte_ldsv(v)
#define _gte_loadVec_GTE_VECTOR_0(v) gte_ldv0(v)
#define _gte_loadVec_GTE_VECTOR_1(v) gte_ldv1(v)
#define _gte_loadVec_GTE_VECTOR_2(v) gte_ldv2(v)
#define _gte_loadVec_GTE_VECTOR_IR(v) gte_ldlvl(v)
#define _gte_readMac_GTE_MAC_0(out) gte_stopz(out)
#define _gte_readMac_GTE_MAC_1(out) gte_stlvnl0(out)
#define _gte_readMac_GTE_MAC_2(out) gte_stlvnl1(out)
Expand All @@ -56,12 +57,12 @@ enum GTE_MAC
#define _gte_dotProduct_GTE_ROW_INDEX_0(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_1(out)
#define _gte_dotProduct_GTE_ROW_INDEX_1(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_2(out)
#define _gte_dotProduct_GTE_ROW_INDEX_2(out, matrixType, vecType) gte_mvmva(0, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_3(out)
#define _gte_mulMatrixVec(out, matrixType, vecType, shift) gte_mvmva(shift, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_VEC(out)

/* API */
#define gte_readMac(out, macType) CAT(_gte_readMac_, macType)(out)
#define gte_loadSVec(v, vecType) CAT(_gte_loadSVec_, vecType)(v)
#define gte_loadVec(v, vecType) CAT(_gte_loadVec_, vecType)(v)
#define gte_loadRowMatrix(v, rowIndex, matrixType) CAT3(_gte_loadSVecMatrix_, matrixType, _##rowIndex)(v)
#define gte_mulMatrixVec(out, matrixType, vecType) gte_mvmva(1, matrixType, vecType, 3, 0); _gte_readMac_GTE_MAC_VEC(out)
#define gte_mulMatrixVec(out, matrixType, vecType) _gte_mulMatrixVec(out, matrixType, vecType, 1)
#define gte_dotProduct(out, rowIndex, matrixType, vecType) CAT(_gte_dotProduct_, rowIndex)(out, matrixType, vecType)

#endif
6 changes: 3 additions & 3 deletions include/ctr/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ typedef int8_t s8;
#define MINUTES(x) ((s32)(((float)(x)) * MINUTE))
#define HOURS(x) ((s32)(((float)(x)) * HOUR))

#define ARR_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define OFFSETOF(TYPE, ELEMENT) ((unsigned int)&(((TYPE *)0)->ELEMENT))

#define nullptr ((void *) 0)
#define len(arr) (sizeof(arr) / sizeof(arr[0]))
#define force_inline static inline __attribute__((always_inline))

#define OFFSETOF(TYPE, ELEMENT) ((unsigned int)&(((TYPE *)0)->ELEMENT))

#endif
11 changes: 10 additions & 1 deletion include/ctr/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CTR_MATH_H

#include <ctr/macros.h>
#include <ctr/gte.h>

typedef union SVec2
{
Expand Down Expand Up @@ -97,12 +98,14 @@ typedef struct TrigTable
#define FP8_ONE (1 << FRACTIONAL_BITS_8)
#define FP8_INT(x) ((x) >> FRACTIONAL_BITS_8)
#define FP8_MULT(x, y) (((x) * (y)) >> FRACTIONAL_BITS_8)
#define FP8(x) ((int)(((float)x) * FP8_ONE))
#define FP8_DIV(x, y) (((x) << FRACTIONAL_BITS_8) / (y))
#define FP8(x) ((int)(((float)(x)) * FP8_ONE))

#define FRACTIONAL_BITS 12
#define FP_ONE (1 << FRACTIONAL_BITS)
#define FP_INT(x) ((x) >> FRACTIONAL_BITS)
#define FP_MULT(x, y) (((x) * (y)) >> FRACTIONAL_BITS)
#define FP_DIV(x, y) (((x) << FRACTIONAL_BITS) / (y))
#define FP(x) ((int)(((float)(x)) * FP_ONE))

force_inline s32 abs(s32 value) { return value < 0 ? -value : value; }
Expand All @@ -116,6 +119,12 @@ force_inline s32 clamp(s32 n, s32 lo, s32 hi)
}

s32 MATH_Sin(u32 angle);
s32 MATH_Cos(u32 angle);
u32 MATH_Sqrt(u32 n, u32 shift); // sqrt(n * 2^shift)
void MATH_GetInverseMatrixTransformation(Matrix* out, const Matrix* matrix);
s32 MATH_VectorLength(const SVec3* vector);
void MATH_VectorNormalize(SVec3* vector);
void MATH_CombineMatrixTransformation(Matrix* out, const Matrix* m, const Matrix* n);
void MATH_MatrixMultiplication(Matrix* out, const Matrix* m, const Matrix* n); // overwrites Rotation Matrix in the GTE with resulting matrix

#endif
12 changes: 10 additions & 2 deletions include/ctr/nd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
#include <ctr/macros.h>
#include <ctr/math.h>

void ND_Music_SetIntro();
void ND_LOAD_XnfFile(char* filename, u32 address, char* dummy);
s32 ND_SquareRoot0_stub(s32 n);
s32 ND_MATH_VectorLength(const SVec3* vector);
int ND_printf(const char* format, ...);
void ND_LOAD_InitCD();

/* MATH */
s32 ND_MATH_Sin(u32 angle);
s32 ND_MATH_Cos(u32 angle);
u32 ND_MATH_Sqrt(u32 n, u32 shift);
void ND_MATH_GetInverseMatrixTransformation(Matrix* out, const Matrix* matrix);
s32 ND_MATH_VectorLength(const SVec3* vector);
void ND_MATH_VectorNormalize(SVec3* vector);
void ND_MATH_CombineMatrixTransformation(Matrix* out, const Matrix* m, const Matrix* n);
void ND_MATH_MatrixMultiplication(Matrix* out, const Matrix* m, const Matrix* n);

#endif
15 changes: 15 additions & 0 deletions include/ctr/redux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef REDUX_H
#define REDUX_H

#include <ctr/macros.h>

force_inline void pcsx_putc(int c) { *((volatile char* const)0x1f802080) = c; }
force_inline void pcsx_debugbreak() { *((volatile char* const)0x1f802081) = 0; }
force_inline void pcsx_execSlot(uint8_t slot) { *((volatile uint8_t* const)0x1f802081) = slot; }
force_inline void pcsx_exit(int code) { *((volatile int16_t* const)0x1f802082) = code; }
force_inline void pcsx_message(const char* msg) { *((volatile const char** const)0x1f802084) = msg; }
force_inline void pcsx_checkKernel(int enable) { *((volatile char*)0x1f802088) = enable; }
force_inline int pcsx_isCheckingKernel() { return *((volatile char* const)0x1f802088) != 0; }
force_inline int pcsx_present() { return *((volatile uint32_t* const)0x1f802080) == 0x58534350; }

#endif
32 changes: 32 additions & 0 deletions include/ctr/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef TEST_H
#define TEST_H

#include <ctr/macros.h>
#include <ctr/math.h>
#include <ctr/redux.h>

void LoadTestPatches();

#define TEST_MATH_IMPL

#ifdef TEST_MATH_IMPL
void TEST_MATH_Sin(u32 angle, s32 res);
void TEST_MATH_Cos(u32 angle, s32 res);
void TEST_MATH_Sqrt(u32 n, u32 shift, u32 res);
void TEST_MATH_GetInverseMatrixTransformation(const Matrix* matrix, const Matrix* res);
void TEST_MATH_VectorLength(const SVec3* vector, s32 res);
void TEST_MATH_VectorNormalize(SVec3* vector, const SVec3* res);
void TEST_MATH_CombineMatrixTransformation(const Matrix* m, const Matrix* n, const Matrix* res);
void TEST_MATH_MatrixMultiplication(const Matrix* m, const Matrix* n, const Matrix* res);
#else
#define TEST_MATH_Sin(angle, res)
#define TEST_MATH_Cos(angle, res)
#define TEST_MATH_Sqrt(n, shift, res)
#define TEST_MATH_GetInverseMatrixTransformation(matrix, res)
#define TEST_MATH_VectorLength(vector, res)
#define TEST_MATH_VectorNormalize(vector, res)
#define TEST_MATH_CombineMatrixTransformation(m, n, res)
#define TEST_MATH_MatrixMultiplication(m, n, res)
#endif

#endif
5 changes: 1 addition & 4 deletions rewrite/buildList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ common, header, 0x8000B0B8, 0x0, src/hooks/dll/load_decomp.c

// decomp files
common, DLL, 0x80200000, 0x0, src/math.c src/prim.c, DECOMP.BIN

// decomp hooks
common, exe, ND_MATH_Sin, 0x0, src/hooks/math/Sin.s
common, exe, ND_MATH_VectorLength, 0x0, src/hooks/math/VectorLength.s
common, DLL, 0x80600000, 0x0, src/tests/test.c, TESTS.BIN
9 changes: 7 additions & 2 deletions rewrite/src/hooks/dll/load_decomp.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <ctr/macros.h>
#include <ctr/nd.h>
#include <ctr/test.h>

void LoadDecomp()
{
const u32 address = 0x80200000;
const u32 testAddr = 0x80600000;
const u32 decompAddr = 0x80200000;
char dummy;
ND_LOAD_InitCD();
ND_LOAD_XnfFile("\\DECOMP.BIN;1", address, &dummy);
ND_LOAD_XnfFile("\\TESTS.BIN;1", testAddr, &dummy);
LoadTestPatches();
ND_LOAD_XnfFile("\\DECOMP.BIN;1", decompAddr, &dummy);
}
3 changes: 0 additions & 3 deletions rewrite/src/hooks/math/Sin.s

This file was deleted.

3 changes: 0 additions & 3 deletions rewrite/src/hooks/math/VectorLength.s

This file was deleted.

141 changes: 136 additions & 5 deletions rewrite/src/math.c

Large diffs are not rendered by default.

Loading