forked from sm64pc/sm64ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
444 lines (387 loc) · 13.2 KB
/
memory.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <PR/ultratypes.h>
#include <string.h>
#include "sm64.h"
#define INCLUDED_FROM_MEMORY_C
#include "buffers/buffers.h"
#include "decompress.h"
#include "game_init.h"
#include "main.h"
#include "memory.h"
#include "segment_symbols.h"
#include "segments.h"
// round up to the next multiple
#define ALIGN4(val) (((val) + 0x3) & ~0x3)
#define ALIGN8(val) (((val) + 0x7) & ~0x7)
#define ALIGN16(val) (((val) + 0xF) & ~0xF)
struct MainPoolState {
u32 freeSpace;
struct MainPoolBlock *listHeadL;
struct MainPoolBlock *listHeadR;
void *prev;
};
struct MainPoolBlock {
struct MainPoolBlock *prev;
struct MainPoolBlock *next;
};
struct MemoryPool {
u32 totalSpace;
struct MemoryBlock *firstBlock;
struct MemoryBlock *freeList;
};
struct MemoryBlock {
struct MemoryBlock *next;
u32 size;
};
extern uintptr_t sSegmentTable[32];
extern u32 sPoolFreeSpace;
extern u8 *sPoolStart;
extern u8 *sPoolEnd;
extern struct MainPoolBlock *sPoolListHeadL;
extern struct MainPoolBlock *sPoolListHeadR;
/**
* Memory pool for small graphical effects that aren't connected to Objects.
* Used for colored text, paintings, and environmental snow and bubbles.
*/
struct MemoryPool *gEffectsMemoryPool;
uintptr_t sSegmentTable[32];
u32 sPoolFreeSpace;
u8 *sPoolStart;
u8 *sPoolEnd;
struct MainPoolBlock *sPoolListHeadL;
struct MainPoolBlock *sPoolListHeadR;
static struct MainPoolState *gMainPoolState = NULL;
uintptr_t set_segment_base_addr(s32 segment, void *addr) {
sSegmentTable[segment] = (uintptr_t) addr & 0x1FFFFFFF;
return sSegmentTable[segment];
}
void *get_segment_base_addr(s32 segment) {
return (void *) (sSegmentTable[segment] | 0x80000000);
}
void *segmented_to_virtual(const void *addr) {
return (void *) addr;
}
void *virtual_to_segmented(UNUSED u32 segment, const void *addr) {
return (void *) addr;
}
void move_segment_table_to_dmem(void) {
}
/**
* Initialize the main memory pool. This pool is conceptually a pair of stacks
* that grow inward from the left and right. It therefore only supports
* freeing the object that was most recently allocated from a side.
*/
void main_pool_init(void *start, void *end) {
sPoolStart = (u8 *) ALIGN16((uintptr_t) start) + 16;
sPoolEnd = (u8 *) ALIGN16((uintptr_t) end - 15) - 16;
sPoolFreeSpace = sPoolEnd - sPoolStart;
sPoolListHeadL = (struct MainPoolBlock *) (sPoolStart - 16);
sPoolListHeadR = (struct MainPoolBlock *) sPoolEnd;
sPoolListHeadL->prev = NULL;
sPoolListHeadL->next = NULL;
sPoolListHeadR->prev = NULL;
sPoolListHeadR->next = NULL;
}
/**
* Allocate a block of memory from the pool of given size, and from the
* specified side of the pool (MEMORY_POOL_LEFT or MEMORY_POOL_RIGHT).
* If there is not enough space, return NULL.
*/
void *main_pool_alloc(u32 size, u32 side) {
struct MainPoolBlock *newListHead;
void *addr = NULL;
size = ALIGN16(size) + 16;
if (size != 0 && sPoolFreeSpace >= size) {
sPoolFreeSpace -= size;
if (side == MEMORY_POOL_LEFT) {
newListHead = (struct MainPoolBlock *) ((u8 *) sPoolListHeadL + size);
sPoolListHeadL->next = newListHead;
newListHead->prev = sPoolListHeadL;
newListHead->next = NULL;
addr = (u8 *) sPoolListHeadL + 16;
sPoolListHeadL = newListHead;
} else {
newListHead = (struct MainPoolBlock *) ((u8 *) sPoolListHeadR - size);
sPoolListHeadR->prev = newListHead;
newListHead->next = sPoolListHeadR;
newListHead->prev = NULL;
sPoolListHeadR = newListHead;
addr = (u8 *) sPoolListHeadR + 16;
}
}
return addr;
}
/**
* Free a block of memory that was allocated from the pool. The block must be
* the most recently allocated block from its end of the pool.
* Return the amount of free space left in the pool.
*/
u32 main_pool_free(void *addr) {
struct MainPoolBlock *block = (struct MainPoolBlock *) ((u8 *) addr - 16);
struct MainPoolBlock *oldListHead = (struct MainPoolBlock *) ((u8 *) addr - 16);
if (oldListHead < sPoolListHeadL) {
while (oldListHead->next != NULL) {
oldListHead = oldListHead->next;
}
sPoolListHeadL = block;
sPoolListHeadL->next = NULL;
sPoolFreeSpace += (uintptr_t) oldListHead - (uintptr_t) sPoolListHeadL;
} else {
while (oldListHead->prev != NULL) {
oldListHead = oldListHead->prev;
}
sPoolListHeadR = block->next;
sPoolListHeadR->prev = NULL;
sPoolFreeSpace += (uintptr_t) sPoolListHeadR - (uintptr_t) oldListHead;
}
return sPoolFreeSpace;
}
/**
* Resize a block of memory that was allocated from the left side of the pool.
* If the block is increasing in size, it must be the most recently allocated
* block from the left side.
* The block does not move.
*/
void *main_pool_realloc(void *addr, u32 size) {
void *newAddr = NULL;
struct MainPoolBlock *block = (struct MainPoolBlock *) ((u8 *) addr - 16);
if (block->next == sPoolListHeadL) {
main_pool_free(addr);
newAddr = main_pool_alloc(size, MEMORY_POOL_LEFT);
}
return newAddr;
}
/**
* Return the size of the largest block that can currently be allocated from the
* pool.
*/
u32 main_pool_available(void) {
return sPoolFreeSpace - 16;
}
/**
* Push pool state, to be restored later. Return the amount of free space left
* in the pool.
*/
u32 main_pool_push_state(void) {
void *prevState = gMainPoolState;
u32 freeSpace = sPoolFreeSpace;
struct MainPoolBlock *lhead = sPoolListHeadL;
struct MainPoolBlock *rhead = sPoolListHeadR;
gMainPoolState = main_pool_alloc(sizeof(*gMainPoolState), MEMORY_POOL_LEFT);
gMainPoolState->freeSpace = freeSpace;
gMainPoolState->listHeadL = lhead;
gMainPoolState->listHeadR = rhead;
gMainPoolState->prev = prevState;
return sPoolFreeSpace;
}
/**
* Restore pool state from a previous call to main_pool_push_state. Return the
* amount of free space left in the pool.
*/
u32 main_pool_pop_state(void) {
sPoolFreeSpace = gMainPoolState->freeSpace;
sPoolListHeadL = gMainPoolState->listHeadL;
sPoolListHeadR = gMainPoolState->listHeadR;
gMainPoolState = gMainPoolState->prev;
return sPoolFreeSpace;
}
/**
* Perform a DMA read from ROM. The transfer is split into 4KB blocks, and this
* function blocks until completion.
*/
static void dma_read(u8 *dest, u8 *srcStart, u8 *srcEnd) {
u32 size = ALIGN16(srcEnd - srcStart);
memcpy(dest, srcStart, srcEnd - srcStart);
}
/**
* Perform a DMA read from ROM, allocating space in the memory pool to write to.
* Return the destination address.
*/
static void *dynamic_dma_read(u8 *srcStart, u8 *srcEnd, u32 side) {
void *dest;
u32 size = ALIGN16(srcEnd - srcStart);
dest = main_pool_alloc(size, side);
if (dest != NULL) {
dma_read(dest, srcStart, srcEnd);
}
return dest;
}
/**
* Allocate an allocation-only pool from the main pool. This pool doesn't
* support freeing allocated memory.
* Return NULL if there is not enough space in the main pool.
*/
struct AllocOnlyPool *alloc_only_pool_init(u32 size, u32 side) {
void *addr;
struct AllocOnlyPool *subPool = NULL;
size = ALIGN4(size);
addr = main_pool_alloc(size + sizeof(struct AllocOnlyPool), side);
if (addr != NULL) {
subPool = (struct AllocOnlyPool *) addr;
subPool->totalSpace = size;
subPool->usedSpace = 0;
subPool->startPtr = (u8 *) addr + sizeof(struct AllocOnlyPool);
subPool->freePtr = (u8 *) addr + sizeof(struct AllocOnlyPool);
}
return subPool;
}
/**
* Allocate from an allocation-only pool.
* Return NULL if there is not enough space.
*/
void *alloc_only_pool_alloc(struct AllocOnlyPool *pool, s32 size) {
void *addr = NULL;
size = ALIGN4(size);
if (size > 0 && pool->usedSpace + size <= pool->totalSpace) {
addr = pool->freePtr;
pool->freePtr += size;
pool->usedSpace += size;
}
return addr;
}
/**
* Resize an allocation-only pool.
* If the pool is increasing in size, the pool must be the last thing allocated
* from the left end of the main pool.
* The pool does not move.
*/
struct AllocOnlyPool *alloc_only_pool_resize(struct AllocOnlyPool *pool, u32 size) {
struct AllocOnlyPool *newPool;
size = ALIGN4(size);
newPool = main_pool_realloc(pool, size + sizeof(struct AllocOnlyPool));
if (newPool != NULL) {
pool->totalSpace = size;
}
return newPool;
}
/**
* Allocate a memory pool from the main pool. This pool supports arbitrary
* order for allocation/freeing.
* Return NULL if there is not enough space in the main pool.
*/
struct MemoryPool *mem_pool_init(u32 size, u32 side) {
void *addr;
struct MemoryBlock *block;
struct MemoryPool *pool = NULL;
size = ALIGN4(size);
addr = main_pool_alloc(size + ALIGN16(sizeof(struct MemoryPool)), side);
if (addr != NULL) {
pool = (struct MemoryPool *) addr;
pool->totalSpace = size;
pool->firstBlock = (struct MemoryBlock *) ((u8 *) addr + ALIGN16(sizeof(struct MemoryPool)));
pool->freeList = (struct MemoryBlock *) ((u8 *) addr + ALIGN16(sizeof(struct MemoryPool)));
block = pool->firstBlock;
block->next = NULL;
block->size = pool->totalSpace;
}
return pool;
}
/**
* Allocate from a memory pool. Return NULL if there is not enough space.
*/
void *mem_pool_alloc(struct MemoryPool *pool, u32 size) {
struct MemoryBlock *freeBlock = (struct MemoryBlock *) &pool->freeList;
void *addr = NULL;
size = ALIGN4(size) + sizeof(struct MemoryBlock);
while (freeBlock->next != NULL) {
if (freeBlock->next->size >= size) {
addr = (u8 *) freeBlock->next + sizeof(struct MemoryBlock);
if (freeBlock->next->size - size <= sizeof(struct MemoryBlock)) {
freeBlock->next = freeBlock->next->next;
} else {
struct MemoryBlock *newBlock = (struct MemoryBlock *) ((u8 *) freeBlock->next + size);
newBlock->size = freeBlock->next->size - size;
newBlock->next = freeBlock->next->next;
freeBlock->next->size = size;
freeBlock->next = newBlock;
}
break;
}
freeBlock = freeBlock->next;
}
return addr;
}
/**
* Free a block that was allocated using mem_pool_alloc.
*/
void mem_pool_free(struct MemoryPool *pool, void *addr) {
struct MemoryBlock *block = (struct MemoryBlock *) ((u8 *) addr - sizeof(struct MemoryBlock));
struct MemoryBlock *freeList = pool->freeList;
if (pool->freeList == NULL) {
pool->freeList = block;
block->next = NULL;
} else {
if (block < pool->freeList) {
if ((u8 *) pool->freeList == (u8 *) block + block->size) {
block->size += freeList->size;
block->next = freeList->next;
pool->freeList = block;
} else {
block->next = pool->freeList;
pool->freeList = block;
}
} else {
while (freeList->next != NULL) {
if (freeList < block && block < freeList->next) {
break;
}
freeList = freeList->next;
}
if ((u8 *) freeList + freeList->size == (u8 *) block) {
freeList->size += block->size;
block = freeList;
} else {
block->next = freeList->next;
freeList->next = block;
}
if (block->next != NULL && (u8 *) block->next == (u8 *) block + block->size) {
block->size = block->size + block->next->size;
block->next = block->next->next;
}
}
}
}
void *alloc_display_list(u32 size) {
void *ptr = NULL;
size = ALIGN8(size);
if (gGfxPoolEnd - size >= (u8 *) gDisplayListHead) {
gGfxPoolEnd -= size;
ptr = gGfxPoolEnd;
} else {
}
return ptr;
}
static struct MarioAnimDmaRelatedThing *func_802789F0(u8 *srcAddr) {
struct MarioAnimDmaRelatedThing *sp1C = dynamic_dma_read(srcAddr, srcAddr + sizeof(u32),
MEMORY_POOL_LEFT);
u32 size = sizeof(u32) + (sizeof(u8 *) - sizeof(u32)) + sizeof(u8 *) +
sp1C->count * sizeof(struct OffsetSizePair);
main_pool_free(sp1C);
sp1C = dynamic_dma_read(srcAddr, srcAddr + size, MEMORY_POOL_LEFT);
sp1C->srcAddr = srcAddr;
return sp1C;
}
void func_80278A78(struct MarioAnimation *a, void *b, struct Animation *target) {
if (b != NULL) {
a->animDmaTable = func_802789F0(b);
}
a->currentAnimAddr = NULL;
a->targetAnim = target;
}
s32 load_patchable_table(struct MarioAnimation *a, u32 index) {
s32 ret = FALSE;
struct MarioAnimDmaRelatedThing *sp20 = a->animDmaTable;
u8 *addr;
u32 size;
if (index < sp20->count) {
do {
addr = sp20->srcAddr + sp20->anim[index].offset;
size = sp20->anim[index].size;
} while (0);
if (a->currentAnimAddr != addr) {
dma_read((u8 *) a->targetAnim, addr, addr + size);
a->currentAnimAddr = addr;
ret = TRUE;
}
}
return ret;
}