Skip to content

Commit fee0672

Browse files
authored
LIST namespace (#234)
* LIST namespace, need to do tests still. * Jitpool + feedback * testing changes, COLL still todo * Finished swapping tests to the stack system. * Final adjustments for tests? * Redhot feedback * some cleanup
1 parent 9c6f263 commit fee0672

16 files changed

Lines changed: 686 additions & 79 deletions

File tree

include/ctr/jitpool.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <ctr/list.h>
4+
5+
typedef struct JitPool
6+
{
7+
LinkedList free;
8+
LinkedList taken;
9+
10+
s32 maxItems;
11+
u32 itemSize;
12+
s32 poolSize;
13+
void* ptrPoolData;
14+
} JitPool;
15+
16+
void JitPool_Clear(JitPool* AP);
17+
void JitPool_Init(JitPool* AP, s32 maxItems, s32 itemSize);
18+
Item* JitPool_Add(JitPool* AP);
19+
void JitPool_Remove(JitPool* AP, Item* item);

include/ctr/list.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <ctr/macros.h>
4+
5+
typedef struct Item
6+
{
7+
struct Item* next;
8+
struct Item* prev;
9+
} Item;
10+
11+
typedef struct LinkedList
12+
{
13+
Item* first;
14+
Item* last;
15+
s32 count;
16+
} LinkedList;
17+
18+
void LIST_Clear(LinkedList* list);
19+
void LIST_AddFront(LinkedList* list, Item* item);
20+
void LIST_AddBack(LinkedList* list, Item* item);
21+
void* LIST_GetNextItem(Item* item);
22+
void* LIST_GetFirstItem(LinkedList* list);
23+
Item* LIST_RemoveMember(LinkedList* list, Item* item);
24+
Item* LIST_RemoveFront(LinkedList* list);
25+
Item* LIST_RemoveBack(LinkedList* list);
26+
void LIST_Init(LinkedList* list, Item* item, s32 itemSize, s32 numItems);
27+
28+
//not a real ND function, just a helper macro. The value of an "Item" is the memory right after the Item struct.
29+
#define LIST_GetItem(itemPtr) ((void*)(((Item*)itemPtr) + 1))

include/ctr/nd.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <ctr/math.h>
55
#include <ctr/rng.h>
66
#include <ctr/coll.h>
7+
#include <ctr/jitpool.h>
8+
#include <ctr/list.h>
79

810
void ND_LOAD_XnfFile(char* filename, u32 address, char* dummy);
911
void ND_LOAD_InitCD();
@@ -36,4 +38,29 @@ void ND_COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, con
3638
void ND_COLL_LoadVerticeData(CollDCache* cache);
3739
s32 ND_COLL_BarycentricTest(TestVertex* t, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3);
3840
void ND_COLL_TestTriangle(CollDCache* cache, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3);
39-
void ND_COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cache);
41+
void ND_COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cache);
42+
43+
/* LIST */
44+
void ND_LIST_Clear(LinkedList* list);
45+
void ND_LIST_AddFront(LinkedList* list, Item* item);
46+
void ND_LIST_AddBack(LinkedList* list, Item* item);
47+
void* ND_LIST_GetNextItem(Item* item);
48+
void* ND_LIST_GetFirstItem(LinkedList* list);
49+
Item* ND_LIST_RemoveMember(LinkedList* list, Item* item);
50+
Item* ND_LIST_RemoveFront(LinkedList* list);
51+
Item* ND_LIST_RemoveBack(LinkedList* list);
52+
void ND_LIST_Init(LinkedList* list, Item* item, s32 itemSize, s32 numItems);
53+
54+
/* MEMPACK */
55+
void* ND_MEMPACK_AllocMem(s32 size);
56+
57+
/* JITPOOL */
58+
void ND_JitPool_Clear(JitPool* AP);
59+
void ND_JitPool_Init(JitPool* AP, s32 maxItems, s32 itemSize);
60+
Item* ND_JitPool_Add(JitPool* AP);
61+
void ND_JitPool_Remove(JitPool* AP, Item* item);
62+
63+
/* MISC */
64+
//TODO: ensure that the signedness of s32 for both of these are correct
65+
void* memset(void* dest, u8 val, s32 len);
66+
void* memcpy(void* dest, const void* src, s32 count);

include/ctr/test.h

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
#include <ctr/math.h>
88
#include <ctr/rng.h>
99
#include <ctr/coll.h>
10+
#include <ctr/list.h>
11+
#include <ctr/jitpool.h>
12+
#include <ctr/test_backup.h>
1013

1114
extern const char* s_nameTestedFunc;
1215

1316
void TEST_WRAPPER();
14-
void LoadTestPatches();
17+
void TEST_LoadPatches();
18+
void TEST_Init();
19+
bool TEST_Memcmp(const void* expected, const void* actual, u32 n);
1520
u32 PatchFunction_Beg(u32* index, const char* funcName);
1621
void PatchFunction_End(u32 index);
17-
u32 PrintSVectorDiff(const SVec3* expected, const SVec3* ret);
18-
u32 PrintMatrixDiff(const Matrix* expected, const Matrix* ret, u32 cmpTrans);
22+
u32 TEST_PrintSVectorDiff(const SVec3* expected, const SVec3* ret);
23+
u32 TEST_PrintMatrixDiff(const Matrix* expected, const Matrix* ret, u32 cmpTrans);
1924

2025
force_inline void FlushCache()
2126
{
@@ -24,11 +29,31 @@ force_inline void FlushCache()
2429
((void (*)())0xa0)();
2530
}
2631

27-
#define BACKUP_ADDR 0x80400000
32+
#define TEST
2833

34+
#if defined(TEST)
35+
#define DYNAMIC_ASSERT(expected, actual, msg) \
36+
do { \
37+
if ((expected) != (actual)) { \
38+
printf("DYNAMIC_ASSERT FAILED (expected %d, got %d): %s\n", (u32)(expected), (u32)(actual), msg); \
39+
} \
40+
} while (0)
41+
#else
42+
#define DYNAMIC_ASSERT(expected, actual, msg)
43+
#endif
44+
45+
#if defined(TEST)
46+
#define STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
47+
#else
48+
#define STATIC_ASSERT(cond, msg)
49+
#endif
50+
51+
#if defined(TEST)
2952
#define TEST_MATH_IMPL
3053
#define TEST_RNG_IMPL
3154
#define TEST_COLL_IMPL
55+
#define TEST_LIST_IMPL
56+
#endif
3257

3358
#ifdef TEST_MATH_IMPL
3459
void TEST_MATH_Sin(u32 angle, s32 ret);
@@ -51,16 +76,30 @@ force_inline void FlushCache()
5176
#endif
5277

5378
#ifdef TEST_RNG_IMPL
79+
typedef struct BDATA_RNG_Rand
80+
{
81+
u32 e_seed; //backup of 0x8008d424
82+
} BDATA_RNG_Rand;
5483
void BACKUP_RNG_Rand();
84+
void RESTORE_RNG_Rand(BDATA_RNG_Rand* restore);
5585
void TEST_RNG_Rand();
86+
87+
typedef struct BDATA_RNG_RandInt
88+
{
89+
RNGSeed e_gameTracker_seed; //backup of e_gameTracker->seed
90+
} BDATA_RNG_RandInt;
5691
void BACKUP_RNG_RandInt();
92+
void RESTORE_RNG_RandInt(BDATA_RNG_RandInt* restore);
5793
void TEST_RNG_RandInt(u32 n, s32 ret);
94+
5895
void TEST_RNG_PseudoRand(u16 n, u16 ret);
5996
void TEST_RNG_Random(RNGSeed* seed, const RNGSeed* ret);
6097
#else
6198
#define BACKUP_RNG_Rand()
99+
#define RESTORE_RNG_Rand(restore)
62100
#define TEST_RNG_Rand()
63101
#define BACKUP_RNG_RandInt()
102+
#define RESTORE_RNG_RandInt(restore)
64103
#define TEST_RNG_RandInt(n, ret)
65104
#define TEST_RNG_PseudoRand(n, ret)
66105
#define TEST_RNG_Random(seed, ret)
@@ -70,11 +109,40 @@ force_inline void FlushCache()
70109
void TEST_COLL_ProjectPointToEdge(const SVec3* v1, const SVec3* v2, const SVec3* point, const SVec3* ret);
71110
void TEST_COLL_CalculateTrianglePlane(const CollDCache* cache, CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const CollVertex* ret);
72111
void TEST_COLL_LoadVerticeData(CollDCache* cache);
73-
void TEST_COLL_LoadQuadblockData_LowLOD(CollDCache* cache, const Quadblock* quadblock, const CollDCache* ret);
74-
void TEST_COLL_LoadQuadblockData_HighLOD(CollDCache* cache, const Quadblock* quadblock, const CollDCache* ret);
112+
113+
typedef struct BDATA_COLL_LoadQuadblockData_LowLOD
114+
{
115+
CollDCache cache; //backup of *cache
116+
} BDATA_COLL_LoadQuadblockData_LowLOD;
117+
void BACKUP_COLL_LoadQuadblockData_LowLOD(CollDCache* cache);
118+
void RESTORE_COLL_LoadQuadblockData_LowLOD(BDATA_COLL_LoadQuadblockData_LowLOD* restore, CollDCache* cache);
119+
void TEST_COLL_LoadQuadblockData_LowLOD(const Quadblock* quadblock, CollDCache* cache);
120+
121+
typedef struct BDATA_COLL_LoadQuadblockData_HighLOD
122+
{
123+
CollDCache cache; //backup of *cache
124+
} BDATA_COLL_LoadQuadblockData_HighLOD;
125+
void BACKUP_COLL_LoadQuadblockData_HighLOD(CollDCache* cache);
126+
void RESTORE_COLL_LoadQuadblockData_HighLOD(BDATA_COLL_LoadQuadblockData_HighLOD* restore, CollDCache* cache);
127+
void TEST_COLL_LoadQuadblockData_HighLOD(const Quadblock* quadblock, CollDCache* cache);
128+
75129
void TEST_COLL_BarycentricTest(TestVertex* t, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const SVec3* pos, s32 ret);
76-
void TEST_COLL_TestTriangle(CollDCache* cache, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3, const CollDCache* ret);
77-
void TEST_COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cache, const CollDCache* ret);
130+
131+
typedef struct BDATA_COLL_TestTriangle
132+
{
133+
CollDCache cache; //backup of *cache
134+
} BDATA_COLL_TestTriangle;
135+
void BACKUP_COLL_TestTriangle(CollDCache* cache);
136+
void RESTORE_COLL_TestTriangle(BDATA_COLL_TestTriangle* restore, CollDCache* cache);
137+
void TEST_COLL_TestTriangle(const CollVertex* v1, const CollVertex* v2, const CollVertex* v3, CollDCache* cache);
138+
139+
typedef struct BDATA_COLL_TestLeaf_Quadblock
140+
{
141+
CollDCache cache; //backup of *cache
142+
} BDATA_COLL_TestLeaf_Quadblock;
143+
void BACKUP_COLL_TestLeaf_Quadblock(CollDCache* cache);
144+
void RESTORE_COLL_TestLeaf_Quadblock(BDATA_COLL_TestLeaf_Quadblock* restore, CollDCache* cache);
145+
void TEST_COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cache);
78146
#else
79147
#define TEST_COLL_ProjectPointToEdge(out, v1, v2, point)
80148
#define TEST_COLL_CalculateTrianglePlane(cache, v1, v2, v3, ret)
@@ -84,4 +152,10 @@ force_inline void FlushCache()
84152
#define TEST_COLL_BarycentricTest(t, v1, v2, v3, pos, ret)
85153
#define TEST_COLL_TestTriangle(cache, v1, v2, v3, ret)
86154
#define TEST_COLL_TestLeaf_Quadblock(quadblock, cache, ret)
155+
#endif
156+
157+
#ifdef TEST_LIST_IMPL
158+
159+
#else
160+
87161
#endif

include/ctr/test_backup.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <ctr/macros.h>
4+
5+
#define BACKUP_STACK_BASE_ADDR ((u32)0x80400000u)
6+
#define BACKUP_STACK_HEAD_PTR ((volatile u32*)(BACKUP_STACK_BASE_ADDR + 0x0))
7+
#define BACKUP_STACK_DATA_BEGIN ((u8*)(BACKUP_STACK_BASE_ADDR + 0x8))
8+
#define BACKUP_STACK_DATA_END ((u8*)(0x80500000u))
9+
#define BACKUP_ALIGN ((u32)4)
10+
11+
void BACKUP_INIT(void);
12+
void* BACKUP_PUSH(const void* src, u32 size);
13+
void* BACKUP_PEEK(u32 depth, u32* out_size);
14+
s32 BACKUP_POP(void); //add variable for "how many times to pop"
15+
s32 BACKUP_POP_MULTIPLE(u32 count);

rewrite/src/exe/coll.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ static void _COLL_LoadQuadblockData_LowLOD(CollDCache* cache, const Quadblock* q
101101

102102
static void COLL_LoadQuadblockData_LowLOD(CollDCache* cache, const Quadblock* quadblock)
103103
{
104-
#ifdef TEST_COLL_IMPL
105-
*(CollDCache*)(BACKUP_ADDR) = *cache;
106-
#endif
104+
BACKUP_COLL_LoadQuadblockData_LowLOD(cache); //global state before
105+
107106
_COLL_LoadQuadblockData_LowLOD(cache, quadblock);
108-
TEST_COLL_LoadQuadblockData_LowLOD((CollDCache*)(BACKUP_ADDR), quadblock, cache);
107+
108+
BACKUP_COLL_LoadQuadblockData_LowLOD(cache); //global state after (result from decomp)
109+
TEST_COLL_LoadQuadblockData_LowLOD(quadblock, cache);
109110
}
110111

111112
/* Address: 0x8001f6f0 */
@@ -137,11 +138,12 @@ static void _COLL_LoadQuadblockData_HighLOD(CollDCache* cache, const Quadblock*
137138

138139
static void COLL_LoadQuadblockData_HighLOD(CollDCache* cache, const Quadblock* quadblock)
139140
{
140-
#ifdef TEST_COLL_IMPL
141-
*(CollDCache*)(BACKUP_ADDR) = *cache;
142-
#endif
141+
BACKUP_COLL_LoadQuadblockData_HighLOD(cache); //global state before
142+
143143
_COLL_LoadQuadblockData_HighLOD(cache, quadblock);
144-
TEST_COLL_LoadQuadblockData_HighLOD((CollDCache*)(BACKUP_ADDR), quadblock, cache);
144+
145+
BACKUP_COLL_LoadQuadblockData_HighLOD(cache); //global state after (result from decomp)
146+
TEST_COLL_LoadQuadblockData_HighLOD(quadblock, cache);
145147
}
146148

147149
/* Address: 0x8001f928 */
@@ -382,12 +384,12 @@ static void _COLL_TestTriangle(CollDCache* cache, const CollVertex* v1, const Co
382384

383385
static void COLL_TestTriangle(CollDCache* cache, const CollVertex* v1, const CollVertex* v2, const CollVertex* v3)
384386
{
385-
#ifdef TEST_COLL_IMPL
386-
const u32 backupAddr = BACKUP_ADDR + sizeof(CollDCache);
387-
*(CollDCache*)(backupAddr) = *cache;
388-
#endif
389-
_COLL_TestTriangle(cache, v1, v2, v3);
390-
TEST_COLL_TestTriangle((CollDCache*)(backupAddr), v1, v2, v3, cache);
387+
BACKUP_COLL_TestTriangle(cache); //global state before
388+
389+
_COLL_TestTriangle(cache, v1, v2, v3);
390+
391+
BACKUP_COLL_TestTriangle(cache); //global state after (result from decomp)
392+
TEST_COLL_TestTriangle(v1, v2, v3, cache);
391393
}
392394

393395
/* Address: 0x80020064 */
@@ -450,19 +452,13 @@ static void _COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cac
450452

451453
void COLL_TestLeaf_Quadblock(const Quadblock* quadblock, CollDCache* cache)
452454
{
453-
#ifdef TEST_COLL_IMPL
454-
*(CollDCache*)(BACKUP_ADDR) = *cache;
455-
#endif
456-
_COLL_TestLeaf_Quadblock(quadblock, cache);
457-
#ifdef TEST_COLL_IMPL
458-
const u32 retAddr = BACKUP_ADDR + sizeof(CollDCache);
459-
*(CollDCache*)(retAddr) = *cache;
460-
*cache = *(CollDCache*)(BACKUP_ADDR);
461-
#endif
462-
TEST_COLL_TestLeaf_Quadblock(quadblock, cache, (CollDCache*)(retAddr));
463-
#ifdef TEST_COLL_IMPL
464-
*cache = *(CollDCache*)(retAddr);
465-
#endif
455+
BACKUP_COLL_TestLeaf_Quadblock(cache); //global state before
456+
457+
_COLL_TestLeaf_Quadblock(quadblock, cache);
458+
459+
BACKUP_COLL_TestLeaf_Quadblock(cache); //global state after (result from decomp)
460+
TEST_COLL_TestLeaf_Quadblock(quadblock, cache);
461+
466462
/* This is a hand written assembly function that breaks the ABI,
467463
and some callers expect the argument registers to be untouched */
468464
__asm__ volatile("move $t9, %0" : : "r"((u32)quadblock));

rewrite/src/exe/jitpool.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <ctr/jitpool.h>
2+
#include <ctr/nd.h>
3+
4+
/* Address: 0x80030fdc */
5+
void JitPool_Clear(JitPool* AP)
6+
{
7+
Item* item = (Item*)AP->ptrPoolData;
8+
LIST_Clear(&AP->free);
9+
LIST_Clear(&AP->taken);
10+
for (s32 i = 0; i < AP->maxItems; i++)
11+
{
12+
LIST_AddFront(&AP->free, item);
13+
//oddly, if AP->itemSize is not aligned to 4 bytes, this will align it DOWN to the nearest 4 byte boundary.
14+
//will this cause clobbering?
15+
item = (struct Item*)((u32)&item->next + (AP->itemSize & 0xfffffffc));
16+
}
17+
}
18+
19+
/* Address: 0x8003105c */
20+
void JitPool_Init(JitPool* AP, s32 maxItems, s32 itemSize)
21+
{
22+
memset((void*)AP, '\0', sizeof(JitPool));
23+
AP->maxItems = maxItems;
24+
AP->itemSize = itemSize;
25+
AP->poolSize = maxItems * itemSize;
26+
void* poolData = ND_MEMPACK_AllocMem(maxItems * itemSize);
27+
AP->ptrPoolData = poolData;
28+
JitPool_Clear(AP);
29+
}
30+
31+
/* Address: 0x800310d4 */
32+
Item* JitPool_Add(JitPool* AP)
33+
{
34+
Item* item = AP->free.first;
35+
if (item != NULL) {
36+
LIST_RemoveMember(&AP->free, item);
37+
LIST_AddFront(&AP->taken, item);
38+
}
39+
return item;
40+
}
41+
42+
/* Address: 0x8003112c */
43+
void JitPool_Remove(JitPool* AP, Item* item)
44+
{
45+
LIST_RemoveMember(&AP->taken, item);
46+
LIST_AddFront(&AP->free, item);
47+
}

0 commit comments

Comments
 (0)