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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
|
#include <libultraship.h>
#include <libultra/types.h>
#include <align_asset_macro.h>
#include <macros.h>
#include <string.h>
#include <common_structs.h>
#include <segments.h>
#include <decode.h>
#include <stubs.h>
#include "memory.h"
#include "main.h"
#include "code_800029B0.h"
#include "math_util.h"
#include "courses/courseTable.h"
#include "courses/all_course_data.h"
#include "courses/all_course_packed.h"
#include "courses/all_course_model.h"
#include "courses/all_course_offsets.h"
#include "defines.h"
#include <course_offsets.h>
#include "engine/courses/Course.h"
#include <stdio.h>
#include "port/Game.h"
s32 sGfxSeekPosition;
s32 sPackedSeekPosition;
static u8 sMemoryPool[0xFFFFF]; // Stock memory pool size: 0xAB630
uintptr_t sPoolEnd = sMemoryPool + sizeof(sMemoryPool);
uintptr_t sPoolFreeSpace;
struct MainPoolBlock* sPoolListHeadL;
struct MainPoolBlock* sPoolListHeadR;
struct MainPoolState* gMainPoolState = NULL;
struct UnkStruct_802B8CD4 D_802B8CD4[] = { 0 };
s32 D_802B8CE4 = 0; // pad
s32 memoryPadding[2];
#define PRINT_MEMPOOL \
printf("\nPool Start: 0x%llX, Pool End: 0x%llX, size: 0x%llX\ngNextFreeMemoryAddress: 0x%llX\n\n", sMemoryPool, \
sMemoryPool + sizeof(sMemoryPool), (sMemoryPool + sizeof(sMemoryPool)) - sMemoryPool, \
gNextFreeMemoryAddress)
/**
* @brief Returns the address of the next available memory location and updates the memory pointer
* to reference the next location of available memory based provided size to allocate.
* @param size of memory to allocate.
* @return Address of free memory
*/
void* get_next_available_memory_addr(uintptr_t size) {
uintptr_t freeSpace = (uintptr_t) gNextFreeMemoryAddress;
size = ALIGN16(size);
gNextFreeMemoryAddress += size;
if (gNextFreeMemoryAddress > sPoolEnd) {
printf("[memory.c] get_next_available_memory_addr(): Memory Pool Out of Bounds! Out of memory!\n");
PRINT_MEMPOOL;
}
return (void*) freeSpace;
}
/**
* @brief Stores the physical memory addr for segmented memory in `gSegmentTable` using the segment number as an index.
*
* This function takes a segment number and a pointer to a memory address, and stores the address in the `gSegmentTable`
* array at the specified segment index. The stored address is truncated to a 29-bit value to ensure that it fits within
* the memory address. This allows converting between segmented memory and physical memory.
*
* @param segment A segment number from 0x0 to 0xF to set the base address.
* @param addr A pointer containing the physical memory address of the data.
* @return The stored base address, truncated to a 29-bit value.
*/
uintptr_t set_segment_base_addr(s32 segment, void* addr) {
gSegmentTable[segment] = (uintptr_t) addr & 0x1FFFFFFF;
return gSegmentTable[segment];
}
uintptr_t set_segment_base_addr_x64(s32 segment, void* addr) {
gSegmentTable[segment] = (uintptr_t) addr;
return gSegmentTable[segment];
}
/**
* @brief Returns the physical memory location of a segment.
* @param permits segment numbers from 0x0 to 0xF.
*/
void* get_segment_base_addr(s32 segment) {
return (void*) (gSegmentTable[segment] | 0x80000000);
}
/**
* @brief converts an RSP segment + offset address to a normal memory address
*/
void* segmented_to_virtual(const void* addr) {
return addr;
size_t segment = (uintptr_t) addr >> 24;
size_t offset = (uintptr_t) addr & 0x00FFFFFF;
return (void*) ((gSegmentTable[segment] + offset));
}
void* segment_offset_to_virtual(uint32_t segment, uint32_t offset) {
return (void*) (gSegmentTable[segment] + ((offset / 8) * sizeof(Gfx)));
}
void* segment_vtx_to_virtual(size_t offset) {
// printf("seg_vtx_to_virt: 0x%llX to 0x%llX\n", offset, (gSegmentTable[0x04] + offset));
return (void*) (gSegmentTable[0x04] + (offset));
}
void* segment5_to_virtual(size_t offset) {
// printf("seg_texture_to_virt: 0x%llX to 0x%llX\n", offset, (gSegmentTable[0x05] + offset));
return (void*) (gSegmentTable[0x05] + (offset));
}
void* segmented_texture_to_virtual(uintptr_t addr) {
uint32_t segment = SEGMENT_NUMBER(addr);
size_t offset = SEGMENT_OFFSET(addr);
// printf("seg_texture_to_virt: 0x%llX to 0x%llX\n", offset, (gSegmentTable[segment] + offset));
return (void*) (gSegmentTable[segment] + (offset));
}
void* segmented_uintptr_t_to_virtual(uintptr_t addr) {
uint32_t newAddr = (uint32_t) addr;
size_t segment = (uintptr_t) newAddr >> 24;
size_t offset = (uintptr_t) newAddr & 0x00FFFFFF;
uint32_t numCommands = offset / 8;
offset = numCommands * sizeof(Gfx);
// printf("seg_uintptr_t_to_virt: 0x%llX to 0x%llX\n", newAddr, (gSegmentTable[segment] + offset));
return (void*) ((gSegmentTable[segment] + offset));
}
Gfx* segmented_gfx_to_virtual(const void* addr) {
size_t segment = (uintptr_t) addr >> 24;
size_t offset = (uintptr_t) addr & 0x00FFFFFF;
uint32_t numCommands = offset / 8;
offset = numCommands * sizeof(Gfx);
// printf("seg_gfx_to_virt: 0x%llX to 0x%llX\n", addr, (gSegmentTable[segment] + offset));
return (Gfx*) ((gSegmentTable[segment] + offset));
}
void move_segment_table_to_dmem(void) {
s32 i;
for (i = 0; i < 16; i++) {
__gSPSegment(gDisplayListHead++, i, gSegmentTable[i]);
}
}
/**
* @brief Sets the starting location for allocating memory and calculates pool size.
*
* Default memory size, 701.984 Kilobytes.
*/
void initialize_memory_pool() {
uintptr_t poolStart = sMemoryPool;
// uintptr_t sPoolEnd = sMemoryPool + sizeof(sMemoryPool);
bzero(sMemoryPool, sizeof(sMemoryPool));
poolStart = ALIGN16(poolStart);
// Truncate to a 16-byte boundary.
sPoolEnd &= ~0xF;
gFreeMemorySize = (sPoolEnd - poolStart) - 0x10;
gNextFreeMemoryAddress = poolStart;
PRINT_MEMPOOL;
}
/**
* @brief Allocates memory and adjusts gFreeMemorySize.
*/
void* allocate_memory(size_t size) {
uintptr_t freeSpace;
size = ALIGN16(size);
gFreeMemorySize -= size;
if (gFreeMemorySize < 0) {
printf("[memory.c] allocate_memory(): gFreeMemorySize below zero!\n");
printf("gFreeMemorySize: 0x%X", gFreeMemorySize);
PRINT_MEMPOOL;
}
freeSpace = (uintptr_t) gNextFreeMemoryAddress;
gNextFreeMemoryAddress += size;
if (gNextFreeMemoryAddress > sPoolEnd) {
printf("[memory.c] allocate_memory(): Memory Pool Out of Bounds! Out of memory!\n");
PRINT_MEMPOOL;
}
return (void*) freeSpace;
}
UNUSED void func_802A7D54(s32 arg0, s32 arg1) {
gD_80150158[arg0].unk0 = arg0;
gD_80150158[arg0].unk8 = arg1;
}
/**
* @brief Allocate and DMA.
*/
void* load_data(uintptr_t startAddr, uintptr_t endAddr) {
void* allocated;
uintptr_t size = endAddr - startAddr;
allocated = allocate_memory(size);
if (allocated != 0) {
dma_copy((u8*) allocated, (u8*) startAddr, size);
}
return (void*) allocated;
}
UNUSED void main_pool_init(uintptr_t start, uintptr_t end) {
start = ALIGN16(start);
end = ALIGN16(end - 15);
sPoolFreeSpace = (end - start) - 16;
sPoolListHeadL = (struct MainPoolBlock*) start;
sPoolListHeadR = (struct MainPoolBlock*) end;
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.
*/
UNUSED void* main_pool_alloc(uintptr_t size, uintptr_t side) {
struct MainPoolBlock* newListHead;
void* addr = NULL;
size = ALIGN16(size) + 8;
if (sPoolFreeSpace >= size) {
sPoolFreeSpace -= size;
if (side == MEMORY_POOL_LEFT) {
newListHead = (struct MainPoolBlock*) ((u8*) sPoolListHeadL + size);
sPoolListHeadL->next = newListHead;
newListHead->prev = sPoolListHeadL;
addr = (u8*) sPoolListHeadL + 8;
sPoolListHeadL = newListHead;
} else {
newListHead = (struct MainPoolBlock*) ((u8*) sPoolListHeadR - size);
sPoolListHeadR->prev = newListHead;
newListHead->next = sPoolListHeadR;
sPoolListHeadR = newListHead;
addr = (u8*) sPoolListHeadR + 8;
}
}
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, otherwise all
* newer blocks are freed as well.
* Return the amount of free space left in the pool.
*/
UNUSED uintptr_t main_pool_free(void* addr) {
struct MainPoolBlock* block = (struct MainPoolBlock*) ((u8*) addr - 8);
struct MainPoolBlock* oldListHead = (struct MainPoolBlock*) ((u8*) addr - 8);
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;
}
// main_pool_realloc
UNUSED void* main_pool_realloc(void* addr, uintptr_t size) {
void* newAddr = NULL;
struct MainPoolBlock* block = (struct MainPoolBlock*) ((u8*) addr - 8);
if (block->next == sPoolListHeadL) {
main_pool_free(addr);
newAddr = main_pool_alloc(size, MEMORY_POOL_LEFT);
}
return newAddr;
}
UNUSED uintptr_t main_pool_available(void) {
return sPoolFreeSpace - 8;
}
UNUSED uintptr_t main_pool_push_state(void) {
struct MainPoolState* prevState = gMainPoolState;
uintptr_t 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.
*/
UNUSED uintptr_t main_pool_pop_state(void) {
sPoolFreeSpace = gMainPoolState->freeSpace;
sPoolListHeadL = gMainPoolState->listHeadL;
sPoolListHeadR = gMainPoolState->listHeadR;
gMainPoolState = gMainPoolState->prev;
return sPoolFreeSpace;
}
// similar to sm64 dma_read
UNUSED void* func_802A80B0(u8* dest, u8* srcStart, u8* srcEnd) {
void* addr;
uintptr_t size = srcStart - dest;
addr = main_pool_alloc(size, (uintptr_t) srcEnd);
if (addr != 0) {
osInvalDCache(addr, size);
osPiStartDma(&gDmaIoMesg, OS_MESG_PRI_NORMAL, OS_READ, (uintptr_t) dest, addr, size, &gDmaMesgQueue);
osRecvMesg(&gDmaMesgQueue, &gMainReceivedMesg, OS_MESG_BLOCK);
}
return addr;
}
// replaces call to dynamic_dma_read with dma_read.
UNUSED void* load_segment(s32 segment, u8* srcStart, u8* srcEnd, u8* side) {
void* addr = func_802A80B0(srcStart, srcEnd, side);
if (addr != NULL) {
set_segment_base_addr(segment, addr);
}
return addr;
}
// Similar to sm64 load_to_fixed_pool_addr?
UNUSED void* func_802A8190(s32 arg0, u8* arg1) {
// uintptr_t srcSize = ALIGN16(srcEnd - srcStart);
// uintptr_t destSize = ALIGN16((u8 *) sPoolListHeadR - destAddr);
void* addr;
uintptr_t temp_v0 = D_802B8CD4[arg0].unk4;
uintptr_t temp_v1 = D_802B8CD4[arg0].unk8;
uintptr_t temp_v2 = D_802B8CD4[arg0].unk2;
addr = func_802A80B0((u8*) temp_v0, (u8*) temp_v1, arg1);
// dest = main_pool_alloc(destSize, MEMORY_POOL_RIGHT);
if (addr != 0) {
set_segment_base_addr(temp_v2, addr);
}
return (void*) addr;
}
UNUSED void func_802A81EC(void) {
s32 temp_s0;
s16* phi_s1;
s32 phi_s0;
phi_s1 = (s16*) &D_802B8CD4;
phi_s0 = 0;
do {
if ((*phi_s1 & 1) != 0) {
func_802A8190(phi_s0, 0);
}
temp_s0 = phi_s0 + 1;
phi_s1 += 8;
phi_s0 = temp_s0;
} while (phi_s0 != 3);
}
UNUSED struct AllocOnlyPool* alloc_only_pool_init(uintptr_t size, uintptr_t 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 = (s32) addr + sizeof(struct AllocOnlyPool);
subPool->startPtr = 0;
subPool->freePtr = (u8*) addr + sizeof(struct AllocOnlyPool);
}
return subPool;
}
UNUSED uintptr_t func_802A82AC(s32 arg0) {
uintptr_t temp_v0;
uintptr_t phi_v1;
temp_v0 = D_801502A0 - arg0;
phi_v1 = 0;
if (temp_v0 >= (uintptr_t) gDisplayListHead) {
D_801502A0 = temp_v0;
phi_v1 = temp_v0;
}
return phi_v1;
}
/**
* @brief Returns pointer to mio0 compressed Vtx.
*/
u8* dma_compressed_vtx(u8* start, u8* end) {
u8* freeSpace;
uintptr_t size;
size = ALIGN16(end - start);
freeSpace = (u8*) gNextFreeMemoryAddress;
dma_copy(freeSpace, start, size);
gNextFreeMemoryAddress += size;
return freeSpace;
}
// unused mio0 decode func.
UNUSED uintptr_t func_802A8348(s32 arg0, s32 arg1, s32 arg2) {
uintptr_t offset;
UNUSED void* pad;
uintptr_t oldAddr;
void* newAddr;
offset = ALIGN16(arg1 * arg2);
oldAddr = gNextFreeMemoryAddress;
newAddr = (void*) (oldAddr + offset);
pad = &newAddr;
#ifdef TARGET_N64
osInvalDCache(newAddr, offset);
osPiStartDma(&gDmaIoMesg, 0, 0, (uintptr_t) &_other_texturesSegmentRomStart[SEGMENT_OFFSET(arg0)], newAddr, offset,
&gDmaMesgQueue);
osRecvMesg(&gDmaMesgQueue, &gMainReceivedMesg, 1);
#endif
func_80040030((u8*) newAddr, (u8*) oldAddr);
gNextFreeMemoryAddress += offset;
return oldAddr;
}
UNUSED u8* func_802A841C(u8* arg0, s32 arg1, s32 arg2) {
u8* temp_v0;
void* temp_a0;
temp_v0 = (u8*) gNextFreeMemoryAddress;
temp_a0 = temp_v0 + arg2;
arg1 = ALIGN16(arg1);
arg2 = ALIGN16(arg2);
osInvalDCache(temp_a0, arg1);
#ifdef TARGET_N64
osPiStartDma(&gDmaIoMesg, 0, 0, (uintptr_t) &_other_texturesSegmentRomStart[SEGMENT_OFFSET(arg0)], temp_a0, arg1,
&gDmaMesgQueue);
#endif
osRecvMesg(&gDmaMesgQueue, &gMainReceivedMesg, 1);
func_80040030((u8*) temp_a0, temp_v0);
gNextFreeMemoryAddress += arg2;
return temp_v0;
}
u8* dma_textures(const char* texture, size_t arg1, size_t arg2) {
u8* temp_v0;
void* temp_a0;
#ifdef TARGET_N64
temp_v0 = (u8*) gNextFreeMemoryAddress;
#else
u8* tex = (u8*) LOAD_ASSET(texture);
temp_v0 = (u8*) allocate_memory(arg2);
#endif
temp_a0 = temp_v0 + arg2;
arg1 = ALIGN16(arg1);
arg2 = ALIGN16(arg2);
#ifdef TARGET_N64
osInvalDCache((void*) temp_a0, arg1);
osPiStartDma(&gDmaIoMesg, 0, 0, (uintptr_t) &_other_texturesSegmentRomStart[SEGMENT_OFFSET(texture)],
(void*) temp_a0, arg1, &gDmaMesgQueue);
osRecvMesg(&gDmaMesgQueue, &gMainReceivedMesg, (int) 1);
mio0decode((u8*) temp_a0, temp_v0);
gNextFreeMemoryAddress += arg2;
#else
memcpy(temp_v0, tex, arg2);
// strcpy(temp_v0, texture);
#endif
return temp_v0;
}
uintptr_t MIO0_0F(u8* arg0, uintptr_t arg1, uintptr_t arg2) {
uintptr_t oldHeapEndPtr;
void* temp_v0;
arg1 = ALIGN16(arg1);
arg2 = ALIGN16(arg2);
oldHeapEndPtr = gHeapEndPtr;
temp_v0 = (void*) gNextFreeMemoryAddress;
osInvalDCache(temp_v0, arg1);
#ifdef TARGET_N64
osPiStartDma(&gDmaIoMesg, 0, 0, (uintptr_t) &_other_texturesSegmentRomStart[SEGMENT_OFFSET(arg0)], temp_v0, arg1,
&gDmaMesgQueue);
#endif
osRecvMesg(&gDmaMesgQueue, &gMainReceivedMesg, 1);
mio0decode((u8*) temp_v0, (u8*) oldHeapEndPtr);
gHeapEndPtr += arg2;
return oldHeapEndPtr;
}
void func_802A86A8(CourseVtx* data, Vtx* vtx, size_t arg1) {
CourseVtx* courseVtx = data;
s32 tmp = ALIGN16(arg1 * 0x10);
size_t i;
s8 temp_a0;
s8 temp_a3;
s8 flags;
#ifdef TARGET_N64
gHeapEndPtr -= tmp;
vtx = (Vtx*) gHeapEndPtr;
#endif
// s32 to uintptr_t comparison required for matching.
for (i = 0; i < arg1; i++) {
if (gIsMirrorMode) {
vtx->v.ob[0] = -courseVtx->ob[0];
} else {
vtx->v.ob[0] = courseVtx->ob[0];
}
vtx->v.ob[1] = (courseVtx->ob[1] * vtxStretchY);
temp_a0 = courseVtx->ca[0];
temp_a3 = courseVtx->ca[1];
flags = temp_a0 & 3;
flags |= (temp_a3 << 2) & 0xC;
vtx->v.ob[2] = courseVtx->ob[2];
vtx->v.tc[0] = courseVtx->tc[0];
vtx->v.tc[1] = courseVtx->tc[1];
vtx->v.cn[0] = (temp_a0 & 0xFC);
vtx->v.cn[1] = (temp_a3 & 0xFC);
vtx->v.cn[2] = courseVtx->ca[2];
vtx->v.flag = flags;
vtx->v.cn[3] = 0xFF;
vtx++;
courseVtx++;
}
}
void decompress_vtx(CourseVtx* arg0, u32 vertexCount) {
s32 size = ALIGN16(vertexCount * 0x18);
#ifdef TARGET_N64
u32 segment = SEGMENT_NUMBER2(arg0);
u32 offset = SEGMENT_OFFSET(arg0);
void* freeSpace;
u8* vtxCompressed = VIRTUAL_TO_PHYSICAL2(gSegmentTable[segment] + offset);
#else
void* freeSpace;
u8* vtxCompressed = arg0;
#endif
UNUSED s32 pad;
freeSpace = (void*) gNextFreeMemoryAddress;
gNextFreeMemoryAddress += size;
mio0decode(vtxCompressed, (u8*) freeSpace);
#ifdef TARGET_N64
func_802A86A8((CourseVtx*) freeSpace, vertexCount);
#endif
set_segment_base_addr(4, (void*) gHeapEndPtr);
}
UNUSED void func_802A8844(void) {
}
void unpack_lights(Gfx* arg0, UNUSED u8* arg1, s8 arg2) {
UNUSED s32 pad;
s32 a = (arg2 * 0x18) + 0x9000008;
s32 b = (arg2 * 0x18) + 0x9000000;
Gfx macro[] = { gsSPNumLights(NUMLIGHTS_1) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
arg0[sGfxSeekPosition].words.w0 = 0x3860010;
arg0[sGfxSeekPosition].words.w1 = a;
sGfxSeekPosition++;
arg0[sGfxSeekPosition].words.w0 = 0x3880010;
arg0[sGfxSeekPosition].words.w1 = b;
sGfxSeekPosition++;
}
void unpack_displaylist(Gfx* arg0, u8* args, UNUSED s8 opcode) {
uintptr_t temp_v0 = args[sPackedSeekPosition++];
uintptr_t temp_t7 = ((args[sPackedSeekPosition++]) << 8 | temp_v0) * 8;
arg0[sGfxSeekPosition].words.w0 = 0x06000000;
// Segment seven addr
arg0[sGfxSeekPosition].words.w1 = segment_offset_to_virtual(
0x07, temp_t7); // (0x07000000 + temp_t7); // (gSegmentTable[segment] + ( (offset / 8) * sizeof(Gfx) ) );
sGfxSeekPosition++;
}
// end displaylist
void unpack_end_displaylist(Gfx* arg0, UNUSED u8* arg1, UNUSED s8 arg2) {
arg0[sGfxSeekPosition].words.w0 = (uintptr_t) (uint8_t) G_ENDDL << 24;
arg0[sGfxSeekPosition].words.w1 = 0;
sGfxSeekPosition++;
}
void unpack_set_geometry_mode(Gfx* arg0, UNUSED u8* arg1, UNUSED s8 arg2) {
Gfx macro[] = { gsSPSetGeometryMode(G_CULL_BACK) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_clear_geometry_mode(Gfx* arg0, UNUSED u8* arg1, UNUSED s8 arg2) {
Gfx macro[] = { gsSPClearGeometryMode(G_CULL_BACK) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_cull_displaylist(Gfx* arg0, UNUSED u8* arg1, UNUSED s8 arg2) {
Gfx macro[] = { gsSPCullDisplayList(0, 7) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_combine_mode1(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetCombineMode(G_CC_MODULATERGBA, G_CC_MODULATERGBA) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_combine_mode2(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetCombineMode(G_CC_MODULATERGBDECALA, G_CC_MODULATERGBDECALA) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_combine_mode_shade(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetCombineMode(G_CC_SHADE, G_CC_SHADE) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_combine_mode4(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetCombineMode(G_CC_MODULATERGBDECALA, G_CC_MODULATERGBDECALA) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_combine_mode5(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetCombineMode(G_CC_DECALRGBA, G_CC_DECALRGBA) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_render_mode_opaque(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetRenderMode(G_RM_AA_ZB_OPA_SURF, G_RM_AA_ZB_OPA_SURF2) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_render_mode_tex_edge(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetRenderMode(G_RM_AA_ZB_TEX_EDGE, G_RM_AA_ZB_TEX_EDGE2) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_render_mode_translucent(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetRenderMode(G_RM_AA_ZB_XLU_SURF, G_RM_AA_ZB_XLU_SURF2) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_render_mode_opaque_decal(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetRenderMode(G_RM_AA_ZB_OPA_DECAL, G_RM_AA_ZB_OPA_DECAL) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_render_mode_translucent_decal(Gfx* arg0, UNUSED u8* arg1, UNUSED uintptr_t arg2) {
Gfx macro[] = { gsDPSetRenderMode(G_RM_AA_ZB_XLU_DECAL, G_RM_AA_ZB_XLU_DECAL) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_tile_sync(Gfx* gfx, u8* args, s8 opcode) {
Gfx tileSync[] = { gsDPTileSync() };
uintptr_t temp_a0;
uintptr_t lo;
uintptr_t hi;
s32 width;
s32 height;
s32 fmt;
s32 siz;
s32 line;
s32 tmem;
s32 cms;
s32 masks;
s32 cmt;
s32 maskt;
s32 lrs;
s32 lrt;
UNUSED s32 pad[4];
tmem = 0;
switch (opcode) {
case 26:
width = 32;
height = 32;
fmt = 0;
break;
case 44:
width = 32;
height = 32;
fmt = 0;
tmem = 256;
break;
case 27:
width = 64;
height = 32;
fmt = 0;
break;
case 28:
width = 32;
height = 64;
fmt = 0;
break;
case 29:
width = 32;
height = 32;
fmt = 3;
break;
case 30:
width = 64;
height = 32;
fmt = 3;
break;
case 31:
width = 32;
height = 64;
fmt = 3;
break;
}
// Set arguments
siz = G_IM_SIZ_16b_BYTES;
line = ((((width * 2) + 7) >> 3));
temp_a0 = args[sPackedSeekPosition++];
cms = temp_a0 & 0xF;
masks = (temp_a0 & 0xF0) >> 4;
temp_a0 = args[sPackedSeekPosition++];
cmt = temp_a0 & 0xF;
maskt = (temp_a0 & 0xF0) >> 4;
// Generate gfx
gfx[sGfxSeekPosition].words.w0 = tileSync->words.w0;
gfx[sGfxSeekPosition].words.w1 = tileSync->words.w1;
sGfxSeekPosition++;
lo = ((uintptr_t) (uint8_t) G_SETTILE << 24) | (fmt << 21) | (siz << 19) | (line << 9) | tmem;
hi = ((cmt) << 18) | ((maskt) << 14) | ((cms) << 8) | ((masks) << 4);
gfx[sGfxSeekPosition].words.w0 = lo;
gfx[sGfxSeekPosition].words.w1 = hi;
sGfxSeekPosition++;
lrs = (width - 1) << 2;
lrt = (height - 1) << 2;
lo = ((uintptr_t) (uint8_t) G_SETTILESIZE << 24);
hi = (lrs << 12) | lrt;
gfx[sGfxSeekPosition].words.w0 = lo;
gfx[sGfxSeekPosition].words.w1 = hi;
sGfxSeekPosition++;
}
uintptr_t get_texture(size_t offset) {
course_texture* textures = CM_GetProps()->textures;
size_t totalOffset = 0;
while (textures->addr) {
if (totalOffset == offset) {
return (textures->addr);
}
totalOffset += textures->data_size;
textures++;
}
printf("memory.c: get_texture()\nTEXTURE NOT FOUND DURING DISPLAYLIST EXTRACT\n");
printf("offset: 0x%X\n", offset);
return NULL;
}
void unpack_tile_load_sync(Gfx* gfx, u8* args, s8 opcode) {
UNUSED uintptr_t var;
Gfx tileSync[] = { gsDPTileSync() };
Gfx loadSync[] = { gsDPLoadSync() };
uintptr_t arg;
uintptr_t lo;
uintptr_t hi;
uintptr_t addr;
uintptr_t width;
uintptr_t height;
uintptr_t fmt;
uintptr_t siz;
uintptr_t tmem;
uintptr_t tile;
size_t offset;
switch (opcode) {
case 32:
width = 32;
height = 32;
fmt = 0;
break;
case 33:
width = 64;
height = 32;
fmt = 0;
break;
case 34:
width = 32;
height = 64;
fmt = 0;
break;
case 35:
width = 32;
height = 32;
fmt = 3;
break;
case 36:
width = 64;
height = 32;
fmt = 3;
break;
case 37:
width = 32;
height = 64;
fmt = 3;
break;
}
// Set arguments
// Waa?
var = args[sPackedSeekPosition];
// Generates a texture address.
offset = args[sPackedSeekPosition++] << 11;
sPackedSeekPosition++;
arg = args[sPackedSeekPosition++];
siz = G_IM_SIZ_16b;
tmem = (arg & 0xF);
tile = (arg & 0xF0) >> 4;
// Generate gfx
lo = ((uintptr_t) (uint8_t) G_SETTIMG_OTR_FILEPATH << 24) | (fmt << 21) | (siz << 19);
gfx[sGfxSeekPosition].words.w0 = lo;
gfx[sGfxSeekPosition].words.w1 = get_texture(offset);
sGfxSeekPosition++;
gfx[sGfxSeekPosition].words.w0 = tileSync->words.w0;
gfx[sGfxSeekPosition].words.w1 = tileSync->words.w1;
sGfxSeekPosition++;
lo = ((uintptr_t) (uint8_t) G_SETTILE << 24) | (fmt << 21) | (siz << 19) | tmem;
hi = tile << 24;
gfx[sGfxSeekPosition].words.w0 = lo;
gfx[sGfxSeekPosition].words.w1 = hi;
sGfxSeekPosition++;
gfx[sGfxSeekPosition].words.w0 = loadSync->words.w0;
gfx[sGfxSeekPosition].words.w1 = loadSync->words.w1;
sGfxSeekPosition++;
lo = (uintptr_t) (uint8_t) G_LOADBLOCK << 24;
hi = (tile << 24) | (MIN((width * height) - 1, 0x7FF) << 12) | CALC_DXT(width, G_IM_SIZ_16b_BYTES);
gfx[sGfxSeekPosition].words.w0 = lo;
gfx[sGfxSeekPosition].words.w1 = hi;
sGfxSeekPosition++;
}
void unpack_texture_on(Gfx* arg0, UNUSED u8* args, UNUSED s8 arg2) {
Gfx macro[] = { gsSPTexture(0xFFFF, 0xFFFF, 0, G_TX_RENDERTILE, G_ON) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_texture_off(Gfx* arg0, UNUSED u8* args, UNUSED s8 arg2) {
Gfx macro[] = { gsSPTexture(0x1, 0x1, 0, G_TX_RENDERTILE, G_OFF) };
arg0[sGfxSeekPosition].words.w0 = macro->words.w0;
arg0[sGfxSeekPosition].words.w1 = macro->words.w1;
sGfxSeekPosition++;
}
void unpack_vtx1(Gfx* gfx, u8* args, UNUSED s8 arg2) {
uintptr_t temp_t7;
uintptr_t temp_t7_2;
uintptr_t temp = args[sPackedSeekPosition++];
uintptr_t temp2 = ((args[sPackedSeekPosition++] << 8) | temp) * 0x10;
temp = args[sPackedSeekPosition++];
temp_t7 = temp & 0x3F;
temp = args[sPackedSeekPosition++];
temp_t7_2 = temp & 0x3F;
gfx[sGfxSeekPosition].words.w0 =
((uintptr_t) (uint8_t) G_VTX << 24) | (temp_t7_2 * 2 << 16) | (((temp_t7 << 10) + ((0x10 * temp_t7) - 1)));
gfx[sGfxSeekPosition].words.w1 = (uintptr_t) segment_vtx_to_virtual(temp2);
sGfxSeekPosition++;
}
void unpack_vtx2(Gfx* gfx, u8* args, s8 arg2) {
uintptr_t temp_t9;
uintptr_t temp_v1;
uintptr_t temp_v2;
temp_v1 = args[sPackedSeekPosition++];
temp_v2 = ((args[sPackedSeekPosition++] << 8) | temp_v1) * 0x10;
temp_t9 = arg2 - 50;
gfx[sGfxSeekPosition].words.w0 = ((uintptr_t) (uint8_t) G_VTX << 24) | ((temp_t9 << 10) + (((temp_t9) * 0x10) - 1));
gfx[sGfxSeekPosition].words.w1 = (uintptr_t) segment_vtx_to_virtual(temp_v2);
sGfxSeekPosition++;
}
void unpack_triangle(Gfx* gfx, u8* args, UNUSED s8 arg2) {
uintptr_t temp_v0;
uintptr_t phi_a0;
uintptr_t phi_a2;
uintptr_t phi_a3;
temp_v0 = args[sPackedSeekPosition++];
if (gIsMirrorMode) {
phi_a3 = temp_v0 & 0x1F;
phi_a2 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_a2 |= (temp_v0 & 3) * 8;
phi_a0 = (temp_v0 >> 2) & 0x1F;
} else {
phi_a0 = temp_v0 & 0x1F;
phi_a2 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_a2 |= (temp_v0 & 3) * 8;
phi_a3 = (temp_v0 >> 2) & 0x1F;
}
gfx[sGfxSeekPosition].words.w0 = ((uintptr_t) (uint8_t) G_TRI1 << 24);
gfx[sGfxSeekPosition].words.w1 = ((phi_a0 * 2) << 16) | ((phi_a2 * 2) << 8) | (phi_a3 * 2);
sGfxSeekPosition++;
}
void unpack_quadrangle(Gfx* gfx, u8* args, UNUSED s8 arg2) {
uintptr_t temp_v0;
uintptr_t phi_t0;
uintptr_t phi_a3;
uintptr_t phi_a0;
uintptr_t phi_t2;
uintptr_t phi_t1;
uintptr_t phi_a2;
temp_v0 = args[sPackedSeekPosition++];
if (gIsMirrorMode) {
phi_t0 = temp_v0 & 0x1F;
phi_a3 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_a3 |= (temp_v0 & 3) * 8;
phi_a0 = (temp_v0 >> 2) & 0x1F;
} else {
phi_a0 = temp_v0 & 0x1F;
phi_a3 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_a3 |= (temp_v0 & 3) * 8;
phi_t0 = (temp_v0 >> 2) & 0x1F;
}
temp_v0 = args[sPackedSeekPosition++];
if (gIsMirrorMode) {
phi_a2 = temp_v0 & 0x1F;
phi_t1 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_t1 |= (temp_v0 & 3) * 8;
phi_t2 = (temp_v0 >> 2) & 0x1F;
} else {
phi_t2 = temp_v0 & 0x1F;
phi_t1 = (temp_v0 >> 5) & 7;
temp_v0 = args[sPackedSeekPosition++];
phi_t1 |= (temp_v0 & 3) * 8;
phi_a2 = (temp_v0 >> 2) & 0x1F;
}
gfx[sGfxSeekPosition].words.w0 =
((uintptr_t) (uint8_t) G_TRI2 << 24) | ((phi_a0 * 2) << 16) | ((phi_a3 * 2) << 8) | (phi_t0 * 2);
gfx[sGfxSeekPosition].words.w1 = ((phi_t2 * 2) << 16) | ((phi_t1 * 2) << 8) | (phi_a2 * 2);
sGfxSeekPosition++;
}
void unpack_spline_3D(Gfx* gfx, u8* arg1, UNUSED s8 arg2) {
uintptr_t temp_v0;
uintptr_t phi_a0;
uintptr_t phi_t0;
uintptr_t phi_a3;
uintptr_t phi_a2;
temp_v0 = arg1[sPackedSeekPosition++];
if (gIsMirrorMode != 0) {
phi_a0 = temp_v0 & 0x1F;
phi_a2 = ((temp_v0 >> 5) & 7);
temp_v0 = arg1[sPackedSeekPosition++];
phi_a2 |= ((temp_v0 & 3) * 8);
phi_a3 = (temp_v0 >> 2) & 0x1F;
phi_t0 = ((temp_v0 >> 7) & 1);
temp_v0 = arg1[sPackedSeekPosition++];
phi_t0 |= (temp_v0 & 0xF) * 2;
} else {
phi_t0 = temp_v0 & 0x1F;
phi_a3 = ((temp_v0 >> 5) & 7);
temp_v0 = arg1[sPackedSeekPosition++];
phi_a3 |= ((temp_v0 & 3) * 8);
phi_a2 = (temp_v0 >> 2) & 0x1F;
phi_a0 = ((temp_v0 >> 7) & 1);
temp_v0 = arg1[sPackedSeekPosition++];
phi_a0 |= (temp_v0 & 0xF) * 2;
}
gfx[sGfxSeekPosition].words.w0 = ((uintptr_t) (uint8_t) G_QUAD << 24);
gfx[sGfxSeekPosition].words.w1 = ((phi_a0 * 2) << 24) | ((phi_t0 * 2) << 16) | ((phi_a3 * 2) << 8) | (phi_a2 * 2);
sGfxSeekPosition++;
}
UNUSED void func_802A9AEC(void) {
}
/**
* Unpacks course packed displaylists by iterating through each byte of the packed file.
* Each packed displaylist entry has an opcode and any number of arguments.
* The opcodes range from 0 to 87 which are used to run the relevant unpack function.
* The file pointer increments when arguments are used. This way,
* displaylist_unpack will always read an opcode and not an argument by accident.
*
* @warning opcodes that do not contain a definition in the switch are ignored. If an undefined opcode
* contained arguments the unpacker might try to unpack those arguments.
* This issue is prevented so long as the packed file adheres to correct opcodes and unpack code
* increments the file pointer the correct number of times.
*/
void displaylist_unpack(uintptr_t* data, uintptr_t finalDisplaylistOffset, u32 arg2) {
#ifdef TARGET_N64
uintptr_t segment = SEGMENT_NUMBER2(data);
uintptr_t offset = SEGMENT_OFFSET(data);
u8* packed_dl = VIRTUAL_TO_PHYSICAL2(gSegmentTable[segment] + offset);
#else
u8* packed_dl = finalDisplaylistOffset;
Gfx* gfx = data;
#endif
#ifdef TARGET_N64
Gfx* gfx;
u32 addr;
#endif
u8 opcode;
#ifdef TARGET_N64
finalDisplaylistOffset = ALIGN16(finalDisplaylistOffset) + 8;
gHeapEndPtr -= finalDisplaylistOffset;
addr = gHeapEndPtr;
gfx = (Gfx*) gHeapEndPtr;
#endif
sGfxSeekPosition = 0;
sPackedSeekPosition = 0;
while (true) {
// Seek to the next byte
opcode = packed_dl[sPackedSeekPosition++];
// Break when the eof has been reached denoted by opcode 0xFF
if (opcode == 0xFF) {
break;
}
switch (opcode) {
case 0x0:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x1:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x2:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x3:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x4:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x5:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x6:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x7:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x8:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x9:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xA:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xB:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xC:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xD:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xE:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0xF:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x10:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x11:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x12:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x13:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x14:
unpack_lights(gfx, packed_dl, opcode);
break;
case 0x15:
unpack_combine_mode1(gfx, packed_dl, arg2);
break;
case 0x16:
unpack_combine_mode2(gfx, packed_dl, arg2);
break;
case 0x17:
unpack_combine_mode_shade(gfx, packed_dl, arg2);
break;
case 0x2E:
unpack_combine_mode4(gfx, packed_dl, arg2);
break;
case 0x53:
unpack_combine_mode5(gfx, packed_dl, arg2);
break;
case 0x18:
unpack_render_mode_opaque(gfx, packed_dl, arg2);
break;
case 0x19:
unpack_render_mode_tex_edge(gfx, packed_dl, arg2);
break;
case 0x2F:
unpack_render_mode_translucent(gfx, packed_dl, arg2);
break;
case 0x54:
unpack_render_mode_opaque_decal(gfx, packed_dl, arg2);
break;
case 0x55:
unpack_render_mode_translucent_decal(gfx, packed_dl, arg2);
break;
case 0x1A:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x2C:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x1B:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x1C:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x1D:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x1E:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x1F:
unpack_tile_sync(gfx, packed_dl, opcode);
break;
case 0x20:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x21:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x22:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x23:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x24:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x25:
unpack_tile_load_sync(gfx, packed_dl, opcode);
break;
case 0x26:
unpack_texture_on(gfx, packed_dl, opcode);
break;
case 0x27:
unpack_texture_off(gfx, packed_dl, opcode);
break;
case 0x28:
unpack_vtx1(gfx, packed_dl, opcode);
break;
case 0x33:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x34:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x35:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x36:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x37:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x38:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x39:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3A:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3B:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3C:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3D:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3E:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x3F:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x40:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x41:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x42:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x43:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x44:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x45:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x46:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x47:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x48:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x49:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4A:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4B:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4C:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4D:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4E:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x4F:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x50:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x51:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x52:
unpack_vtx2(gfx, packed_dl, opcode);
break;
case 0x29:
unpack_triangle(gfx, packed_dl, opcode);
break;
case 0x58:
unpack_quadrangle(gfx, packed_dl, opcode);
break;
case 0x30:
unpack_spline_3D(gfx, packed_dl, opcode);
break;
case 0x2D:
unpack_cull_displaylist(gfx, packed_dl, opcode);
break;
case 0x2A:
unpack_end_displaylist(gfx, packed_dl, opcode);
break;
case 0x56:
unpack_set_geometry_mode(gfx, packed_dl, opcode);
break;
case 0x57:
unpack_clear_geometry_mode(gfx, packed_dl, opcode);
break;
case 0x2B:
unpack_displaylist(gfx, packed_dl, opcode);
break;
default:
// Skip unknown values
break;
}
}
#ifdef TARGET_N64
set_segment_base_addr(0x7, (void*) addr);
#endif
}
struct UnkStr_802AA7C8 {
u8* unk0;
uintptr_t unk4;
uintptr_t unk8;
uintptr_t unkC;
};
void decompress_textures(u32* arg0) {
#ifdef TARGET_N64
u32 segment = SEGMENT_NUMBER2(arg0);
u32 offset = SEGMENT_OFFSET(arg0);
struct UnkStr_802AA7C8* phi_s0 = (struct UnkStr_802AA7C8*) VIRTUAL_TO_PHYSICAL2(gSegmentTable[segment] + offset);
#else
struct UnkStr_802AA7C8* phi_s0 = (struct UnkStr_802AA7C8*) arg0;
#endif
struct UnkStr_802AA7C8* temp_s0;
uintptr_t temp_t2;
u8* temp_a0;
uintptr_t phi_v0;
uintptr_t sp20;
phi_v0 = 0;
temp_s0 = phi_s0;
while (true) {
temp_a0 = phi_s0->unk0;
if ((temp_a0) == 0) {
break;
}
phi_v0 += phi_s0->unk8;
phi_s0++;
}
phi_s0 = temp_s0;
gHeapEndPtr -= phi_v0;
sp20 = gHeapEndPtr;
while (true) {
temp_a0 = phi_s0->unk0;
if ((temp_a0) == 0) {
break;
}
MIO0_0F(temp_a0, phi_s0->unk4, phi_s0->unk8);
phi_s0++;
}
gHeapEndPtr = sp20;
temp_t2 = gHeapEndPtr;
set_segment_base_addr(0x5, (void*) temp_t2);
}
void* decompress_segments(u8* start, u8* end) {
return NULL;
UNUSED u32 pad;
u32 sp28;
u32 size = ALIGN16(end - start);
u8* heapEnd;
u32* freeSpace;
heapEnd = (u8*) gHeapEndPtr - size;
// sp20 = temp_a0;
dma_copy(heapEnd, start, size);
sp28 = *(u32*) (heapEnd + 4);
sp28 = ALIGN16(sp28);
freeSpace = (u32*) gNextFreeMemoryAddress;
mio0decode(heapEnd, (u8*) freeSpace);
gNextFreeMemoryAddress += sp28;
return (void*) freeSpace;
}
extern const course_texture mario_raceway_textures[30];
/* To help verify if ptrs are pointing within segments see gfx_pc.cpp gfx_step() */
uintptr_t vtxSegEnd;
uintptr_t dlSegEnd;
uintptr_t texSegEnd;
size_t texSegSize;
Gfx* testaaa;
u8* load_lakitu_tlut_x64(const char** textureList, size_t length) {
// Calculate lakitu texture size to allocate
size_t size = 0;
for (size_t i = 0; i < length; i++) {
size += ResourceGetTexSizeByName(textureList[i]);
}
u8* textures = (u8*) gNextFreeMemoryAddress;
gNextFreeMemoryAddress += size;
size_t offset = 0;
for (size_t i = 0; i < length; i++) {
u8* tex = (u8*) LOAD_ASSET_RAW(textureList[i]);
size_t texSize = ResourceGetTexSizeByName(textureList[i]);
// printf("\nTEX SIZE: %X\n\n", texSize);
memcpy(&textures[offset], tex, texSize);
offset += texSize;
}
return textures;
}
/**
* @brief Loads & DMAs course data. Vtx, textures, displaylists, etc.
* @param courseId
*/
void load_course(s32 courseId) {
printf("Loading Course %d\n", courseId);
gNextFreeMemoryAddress = gFreeMemoryResetAnchor;
CM_CleanWorld();
LoadCourse();
CM_Editor_SetLevelDimensions(gCourseMinX, gCourseMaxX, gCourseMinZ, gCourseMaxZ, gCourseMinY, gCourseMaxY);
}
|