-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathn64split.c
2396 lines (2284 loc) · 84.2 KB
/
n64split.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
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
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <zlib.h>
#include "config.h"
#include "libblast.h"
#include "libmio0.h"
#include "libsfx.h"
#include "mipsdisasm.h"
#include "n64graphics.h"
#include "strutils.h"
#include "utils.h"
#define N64SPLIT_VERSION "0.4a"
#define GLOBALS_FILE "globals.inc"
#define MACROS_FILE "macros.inc"
typedef struct _arg_config
{
char input_file[FILENAME_MAX];
char config_file[FILENAME_MAX];
char output_dir[FILENAME_MAX];
float model_scale;
bool raw_texture; // TODO: this should be the default path once n64graphics is updated
bool large_texture;
bool large_texture_depth;
bool keep_going;
bool merge_pseudo;
} arg_config;
typedef enum {
N64_ROM_INVALID,
N64_ROM_Z64,
N64_ROM_V64,
} n64_rom_format;
// default configuration
static const arg_config default_args =
{
.input_file = "",
.config_file = "",
.output_dir = "",
.model_scale = 1024.0f,
.raw_texture = false,
.large_texture = false,
.large_texture_depth = 16,
.keep_going = false,
.merge_pseudo = false,
};
// static files
#include "n64split.makefile.h"
#include "n64split.collision.mtl.h"
const char asm_header[] =
"# %s disassembly and split file\n"
"# generated by n64split v%s - N64 ROM splitter\n"
"\n"
"# assembler directives\n"
".set noat # allow manual use of $at\n"
".set noreorder # don't insert nops after branches\n"
"\n"
".include \"" GLOBALS_FILE "\"\n"
"\n";
static void print_spaces(FILE *fp, int count)
{
int i;
for (i = 0; i < count; i++) {
fputc(' ', fp);
}
}
static n64_rom_format n64_rom_type(unsigned char *buf, unsigned int length)
{
const unsigned char bs[] = {0x37, 0x80, 0x40, 0x12}; // byte-swapped
const unsigned char be[] = {0x80, 0x37, 0x12, 0x40}; // big-endian
if (length >= (8 * MB)) {
if (!memcmp(buf, bs, sizeof(bs))) {
return N64_ROM_V64;
}
if (!memcmp(buf, be, sizeof(be))) {
return N64_ROM_Z64;
}
}
return N64_ROM_INVALID;
}
static void gzip_decode_file(char *gzfilename, int offset, char *binfilename)
{
#define CHUNK 0x4000
FILE *file;
FILE *fout;
z_stream strm = {0};
unsigned char in[CHUNK];
unsigned char out[CHUNK];
// TODO: add some error checking
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
inflateInit2(&strm, 16+MAX_WBITS);
file = fopen(gzfilename, "rb");
fout = fopen(binfilename, "wb");
fseek(file, offset, SEEK_SET);
while (1) {
int bytes_read;
bytes_read = fread(in, sizeof(char), sizeof(in), file);
strm.avail_in = bytes_read;
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
inflate(&strm, Z_NO_FLUSH);
fwrite(out, sizeof(char), CHUNK - strm.avail_out, fout);
} while (strm.avail_out == 0);
if (feof(file)) {
inflateEnd(&strm);
break;
}
}
fclose(file);
fclose(fout);
}
static int config_section_lookup(rom_config *config, unsigned int addr, char *label, int is_end)
{
int i;
// check for ROM offsets
switch (is_end) {
case 0:
for (i = 0; i < config->section_count; i++) {
// TODO: hack until mario_animation gets moved or AT() is used
if (config->sections[i].start == addr && addr != 0x4EC000) {
if (config->sections[i].label[0] != '\0') {
sprintf(label, "%s", config->sections[i].label);
} else {
sprintf(label, "%s_%06X", config_section2str(config->sections[i].type), addr);
}
INFO("Found 0 %06X: %s\n", addr, label);
return 0;
}
}
break;
case 1:
for (i = 0; i < config->section_count; i++) {
if (config->sections[i].end == addr) {
if (config->sections[i].label[0] != '\0') {
sprintf(label, "%s_end", config->sections[i].label);
} else {
sprintf(label, "%s_%06X", config_section2str(config->sections[i].type), addr);
}
INFO("Found 1 %06X: %s\n", addr, label);
return 0;
}
}
break;
default:
break;
}
sprintf(label, "0x%X", addr);
return -1;
}
static void write_behavior(FILE *out, unsigned char *data, rom_config *config, int s, disasm_state *state)
{
char label[128];
unsigned int a, i;
unsigned int len;
unsigned int val;
int beh_i;
split_section *sec;
split_section *beh;
sec = &config->sections[s];
beh = sec->children;
a = sec->start;
beh_i = 0;
while (a < sec->end) {
if (beh_i < sec->child_count) {
unsigned int offset = a - sec->start;
if (offset == beh[beh_i].start) {
fprintf(out, "%s: # %04X\n", beh[beh_i].label, beh[beh_i].start);
beh_i++;
} else if (offset > beh[beh_i].start) {
ERROR("Warning: skipped behavior %04X \"%s\"\n", beh[beh_i].start, beh[beh_i].label);
beh_i++;
}
}
switch (data[a]) {
case 0x02:
case 0x04:
case 0x0C:
case 0x13:
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x23:
case 0x27:
case 0x2A:
case 0x2E:
case 0x2F:
case 0x31:
case 0x33:
case 0x36:
case 0x37:
len = 8;
break;
case 0x1C:
case 0x29:
case 0x2B:
case 0x2C:
len = 12;
break;
case 0x30:
len = 20;
break;
default:
len = 4;
break;
}
val = read_u32_be(&data[a]);
fprintf(out, ".word 0x%08X", val);
switch(data[a]) {
case 0x0C: // behavior 0x0C is a function pointer
val = read_u32_be(&data[a+4]);
disasm_label_lookup(state, val, label);
fprintf(out, ", %s\n", label);
break;
case 0x02: // jump to another behavior
case 0x04: // jump to segmented address
case 0x1C: // sub-objects
case 0x29: // sub-objects
case 0x2C: // sub-objects
for (i = 4; i < len-4; i += 4) {
val = read_u32_be(&data[a+i]);
fprintf(out, ", 0x%08X", val);
}
val = read_u32_be(&data[a+len-4]);
disasm_label_lookup(state, val, label);
fprintf(out, ", %s\n", label);
break;
default:
for (i = 4; i < len; i += 4) {
val = read_u32_be(&data[a+i]);
fprintf(out, ", 0x%08X", val);
}
fprintf(out, "\n");
break;
}
a += len;
}
}
typedef struct
{
int length;
const char *macro;
} geo_command;
static geo_command geo_table[] =
{
/* 0x00 */ {0x08, "geo_branch_and_link"},
/* 0x01 */ {0x04, "geo_end"},
/* 0x02 */ {0x08, "geo_branch"},
/* 0x03 */ {0x04, "geo_return"},
/* 0x04 */ {0x04, "geo_open_node"},
/* 0x05 */ {0x04, "geo_close_node"},
/* 0x06 */ {0x04, "geo_todo_06"},
/* 0x07 */ {0x04, "geo_update_node_flags"},
/* 0x08 */ {0x0C, "geo_node_screen_area"},
/* 0x09 */ {0x04, "geo_todo_09"},
/* 0x0A */ {0x08, "geo_camera_frustum"}, // 8-12 variable
/* 0x0B */ {0x04, "geo_node_start"},
/* 0x0C */ {0x04, "geo_zbuffer"},
/* 0x0D */ {0x08, "geo_render_range"},
/* 0x0E */ {0x08, "geo_switch_case"},
/* 0x0F */ {0x14, "geo_todo_0F"},
/* 0x10 */ {0x10, "geo_translate_rotate"}, // variable
/* 0x11 */ {0x08, "geo_todo_11"}, // variable
/* 0x12 */ {0x08, "geo_todo_12"}, // variable
/* 0x13 */ {0x0C, "geo_dl_translated"},
/* 0x14 */ {0x08, "geo_billboard"},
/* 0x15 */ {0x08, "geo_display_list"},
/* 0x16 */ {0x08, "geo_shadow"},
/* 0x17 */ {0x04, "geo_todo_17"},
/* 0x18 */ {0x08, "geo_asm"},
/* 0x19 */ {0x08, "geo_background"},
/* 0x1A */ {0x08, "geo_nop_1A"},
/* 0x1B */ {0x04, "geo_todo_1B"},
/* 0x1C */ {0x0C, "geo_todo_1C"},
/* 0x1D */ {0x08, "geo_scale"}, // variable
/* 0x1E */ {0x08, "geo_nop_1E"},
/* 0x1F */ {0x10, "geo_nop_1F"},
/* 0x20 */ {0x04, "geo_start_distance"},
};
static void write_geolayout(FILE *out, unsigned char *data, unsigned int start, unsigned int end, disasm_state *state)
{
const int INDENT_AMOUNT = 3;
const int INDENT_START = INDENT_AMOUNT;
char label[128];
unsigned int a = start;
unsigned int tmp;
int indent;
int cmd_len;
int print_label = 1;
indent = INDENT_START;
fprintf(out, ".include \"macros.inc\"\n"
".include \"geo_commands.inc\"\n\n"
".section .geo, \"a\"\n\n");
while (a < end) {
unsigned cmd = data[a];
if (print_label) {
fprintf(out, "glabel geo_layout_X_%06X # %04X\n", a, a);
print_label = 0;
}
if ((cmd == 0x01 || cmd == 0x05) && indent > INDENT_AMOUNT) {
indent -= INDENT_AMOUNT;
}
print_spaces(out, indent);
if (cmd < DIM(geo_table)) {
if (cmd != 0x10) { // special case 0x10 since multiple pseudo
fprintf(out, "%s", geo_table[cmd].macro);
}
} else {
ERROR("Unknown geo layout command: 0x%02X\n", cmd);
}
cmd_len = geo_table[cmd].length;
switch (cmd) {
case 0x00: // 00 00 00 00 [SS SS SS SS]: branch and store
tmp = read_u32_be(&data[a+4]);
fprintf(out, " geo_layout_%08X # 0x%08X", tmp, tmp);
break;
case 0x01: // 01 00 00 00: terminate
case 0x03: // 03 00 00 00: return from branch
// no params
fprintf(out, "\n");
indent = INDENT_START;
print_label = 1;
break;
case 0x04: // 04 00 00 00: open node
case 0x05: // 05 00 00 00: close node
case 0x0B: // 05 00 00 00: start geo layout
case 0x17: // 17 00 00 00: set up object rendering
case 0x1A: // 1A 00 00 00 00 00 00 00: no op
case 0x1E: // 1E 00 00 00 00 00 00 00: no op
case 0x1F: // 1E 00 00 00 00 00 00 00 00 00 00 00: no op
// no params
break;
case 0x02: // 02 [AA] 00 00 [SS SS SS SS]
tmp = read_u32_be(&data[a+4]);
fprintf(out, " %d, geo_layout_%08X # 0x%08X", data[a+1], tmp, tmp);
break;
case 0x08: // 08 00 00 [AA] [XX XX] [YY YY] [WW WW] [HH HH]
fprintf(out, " %d, %d, %d, %d, %d", data[a+3],
read_s16_be(&data[a+4]), read_s16_be(&data[a+6]),
read_s16_be(&data[a+8]), read_s16_be(&data[a+10]));
break;
case 0x09: // 09 00 00 [AA]
fprintf(out, " %d", data[a+3]);
break;
case 0x0A: // 0A [AA] [BB BB] [NN NN] [FF FF] {EE EE EE EE}: set camera frustum
fprintf(out, " %d, %d, %d", read_s16_be(&data[a+2]), read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
if (data[a+1] > 0) {
cmd_len += 4;
disasm_label_lookup(state, read_u32_be(&data[a+8]), label);
fprintf(out, ", %s", label);
}
break;
case 0x0C: // 0C [AA] 00 00: enable/disable Z-buffer
fprintf(out, " %d", data[a+1]);
break;
case 0x0D: // 0D 00 00 00 [AA AA] [BB BB]: set render range
fprintf(out, " %d, %d", read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
break;
case 0x0E: // 0E 00 [NN NN] [AA AA AA AA]: switch/case
fprintf(out, " %d, geo_switch_case_%08X", read_s16_be(&data[a+2]), read_u32_be(&data[a+4]));
break;
case 0x0F: // 0F 00 [TT TT] [XX XX] [YY YY] [ZZ ZZ] [UU UU] [VV VV] [WW WW] [AA AA AA AA]
fprintf(out, " %d, %d, %d, %d, %d, %d, %d", read_s16_be(&data[a+2]),
read_s16_be(&data[a+4]), read_s16_be(&data[a+6]), read_s16_be(&data[a+8]),
read_s16_be(&data[a+10]), read_s16_be(&data[a+12]), read_s16_be(&data[a+14]));
disasm_label_lookup(state, read_u32_be(&data[a+0x10]), label);
fprintf(out, ", %s", label);
break;
case 0x10: // 10 [AA] [BB BB] [XX XX] [YY YY] [ZZ ZZ] [RX RX] [RY RY] [RZ RZ] {SS SS SS SS}: translate & rotate
{
unsigned char params = data[a+1];
unsigned char field_type = (params & 0x70) >> 4;
unsigned char layer = params & 0xF;
switch (field_type) {
case 0: // 10 [0L] 00 00 [TX TX] [TY TY] [TZ TZ] [RX RX] [RY RY] [RZ RZ] {SS SS SS SS}: translate & rotate
fprintf(out, "geo_translate_rotate %d, %d, %d, %d, %d, %d, %d", layer,
read_s16_be(&data[a+4]), read_s16_be(&data[a+6]), read_s16_be(&data[a+8]),
read_s16_be(&data[a+10]), read_s16_be(&data[a+12]), read_s16_be(&data[a+14]));
cmd_len = 16;
break;
case 1: // 10 [1L] [TX TX] [TY TY] [TZ TZ] {SS SS SS SS}: translate
fprintf(out, "geo_translate %d, %d, %d, %d", layer,
read_s16_be(&data[a+2]), read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
cmd_len = 8;
break;
case 2: // 10 [2L] [RX RX] [RY RY] [RZ RZ] {SS SS SS SS}: rotate
fprintf(out, "geo_rotate %d, %d, %d, %d", layer,
read_s16_be(&data[a+2]), read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
cmd_len = 8;
break;
case 3: // 10 [3L] [RY RY] {SS SS SS SS}: rotate Y
fprintf(out, "geo_rotate_y %d, %d", layer, read_s16_be(&data[a+2]));
cmd_len = 4;
break;
}
if (params & 0x80) {
tmp = read_u32_be(&data[a+cmd_len]);
fprintf(out, ", seg%X_dl_%08X", (tmp >> 24) & 0xFF, tmp);
cmd_len += 4;
}
break;
}
case 0x11: // 11 [P][L] [XX XX] [YY YY] [ZZ ZZ] {SS SS SS SS}: ? scene graph node, optional DL
case 0x12: // 12 [P][L] [XX XX] [YY YY] [ZZ ZZ] {SS SS SS SS}: ? scene graph node, optional DL
case 0x14: // 14 [P][L] [XX XX] [YY YY] [ZZ ZZ] {SS SS SS SS}: billboard model
fprintf(out, " 0x%02X, %d, %d, %d", data[a+1] & 0xF, read_s16_be(&data[a+2]),
read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
if (data[a+1] & 0x80) {
disasm_label_lookup(state, read_u32_be(&data[a+8]), label);
fprintf(out, ", %s", label);
cmd_len += 4;
}
break;
case 0x13: // 13 [LL] [XX XX] [YY YY] [ZZ ZZ] [AA AA AA AA]: scene graph node with layer and translation
fprintf(out, " 0x%02X, %d, %d, %d", data[a+1],
read_s16_be(&data[a+2]), read_s16_be(&data[a+4]), read_s16_be(&data[a+6]));
tmp = read_u32_be(&data[a+8]);
if (tmp != 0x0) {
fprintf(out, ", seg%X_dl_%08X", data[a+8], tmp);
}
break;
case 0x15: // 15 [LL] 00 00 [AA AA AA AA]: load display list
fprintf(out, " 0x%02X, seg%X_dl_%08X", data[a+1], data[a+4], read_u32_be(&data[a+4]));
break;
case 0x16: // 16 00 00 [AA] 00 [BB] [CC CC]: start geo layout with shadow
fprintf(out, " 0x%02X, 0x%02X, %d", data[a+3], data[a+5], read_s16_be(&data[a+6]));
break;
case 0x18: // 18 00 [XX XX] [AA AA AA AA]: load polygons from asm
case 0x19: // 19 00 [TT TT] [AA AA AA AA]: set background/skybox
disasm_label_lookup(state, read_u32_be(&data[a+4]), label);
fprintf(out, " %d, %s", read_s16_be(&data[a+2]), label);
break;
case 0x1B: // 1B 00 [XX XX]: ??
fprintf(out, " %d", read_s16_be(&data[a+2]));
break;
case 0x1C: // 1C [PP] [XX XX] [YY YY] [ZZ ZZ] [AA AA AA AA]
disasm_label_lookup(state, read_u32_be(&data[a+8]), label);
fprintf(out, " 0x%02X, %d, %d, %d, %s", data[a+1], read_s16_be(&data[a+2]),
read_s16_be(&data[a+4]), read_s16_be(&data[a+6]), label);
break;
case 0x1D: // 1D [P][L] 00 00 [MM MM MM MM] {SS SS SS SS}: scale model
fprintf(out, " 0x%02X, %d", data[a+1] & 0xF, read_u32_be(&data[a+4]));
if (data[a+1] & 0x80) {
disasm_label_lookup(state, read_u32_be(&data[a+8]), label);
fprintf(out, ", %s", label);
cmd_len += 4;
}
break;
case 0x20: // 20 00 [AA AA]: start geo layout with rendering area
fprintf(out, " %d", read_s16_be(&data[a+2]));
break;
default:
ERROR("Unknown geo layout command: 0x%02X\n", cmd);
break;
}
fprintf(out, "\n");
switch (cmd) {
case 0x04: // open_node
case 0x08: // node_screen_area
//case 0x0B: // node_start
case 0x16: // geo_shadow
case 0x20: // start_distance
indent += INDENT_AMOUNT;
default:
break;
}
if (cmd == 0x01 || cmd == 0x03) { // end or return
a += cmd_len;
cmd_len = 0;
while (a < end && 0 == read_u32_be(&data[a])) {
fprintf(out, ".word 0x0\n");
a += 4;
}
}
a += cmd_len;
}
}
static void write_level(FILE *out, unsigned char *data, rom_config *config, int s, disasm_state *state)
{
char start_label[128];
char end_label[128];
char dst_label[128];
split_section *sec;
unsigned int ptr_start;
unsigned int ptr_end;
unsigned int dst;
unsigned int a;
int i;
int beh_i;
sec = &config->sections[s];
beh_i = -1;
// see if there is a behavior section
for (i = 0; i < config->section_count; i++) {
if (config->sections[i].type == TYPE_SM64_BEHAVIOR) {
beh_i = i;
break;
}
}
a = sec->start;
while (a < sec->end) {
// length = 0 ends level script
if (data[a+1] == 0) {
break;
}
switch (data[a]) {
case 0x00: // load and jump from ROM into a RAM segment
case 0x01: // load and jump from ROM into a RAM segment
case 0x17: // copy uncompressed data from ROM to a RAM segment
case 0x18: // decompress MIO0 data from ROM and copy it into a RAM segment
case 0x1A: // decompress MIO0 data from ROM and copy it into a RAM segment (for texture only segments?)
ptr_start = read_u32_be(&data[a+4]);
ptr_end = read_u32_be(&data[a+8]);
config_section_lookup(config, ptr_start, start_label, 0);
config_section_lookup(config, ptr_end, end_label, 1);
fprintf(out, ".word 0x");
for (i = 0; i < 4; i++) {
fprintf(out, "%02X", data[a+i]);
}
if (0 == strcmp("behavior_data", start_label)) {
fprintf(out, ", __load_%s, __load_%s", start_label, end_label);
} else {
fprintf(out, ", %s, %s", start_label, end_label);
}
for (i = 12; i < data[a+1]; i++) {
if ((i & 0x3) == 0) {
fprintf(out, ", 0x");
}
fprintf(out, "%02X", data[a+i]);
}
fprintf(out, "\n");
break;
case 0x11: // call function
case 0x12: // call function
ptr_start = read_u32_be(&data[a+0x4]);
disasm_label_lookup(state, ptr_start, start_label);
fprintf(out, ".word 0x%08X, %s # %08X\n", read_u32_be(&data[a]), start_label, ptr_start);
break;
case 0x16: // load ASM into RAM
dst = read_u32_be(&data[a+0x4]);
ptr_start = read_u32_be(&data[a+0x8]);
ptr_end = read_u32_be(&data[a+0xc]);
// TODO: differentiate between start/end
disasm_label_lookup(state, dst, dst_label);
config_section_lookup(config, ptr_start, start_label, 0);
config_section_lookup(config, ptr_end, end_label, 1);
fprintf(out, ".word 0x");
for (i = 0; i < 4; i++) {
fprintf(out, "%02X", data[a+i]);
}
fprintf(out, ", %s, %s, %s\n", dst_label, start_label, end_label);
break;
case 0x25: // load mario object with behavior
case 0x24: // load object with behavior
fprintf(out, ".word 0x%08X", read_u32_be(&data[a]));
for (i = 4; i < data[a+1]-4; i+=4) {
fprintf(out, ", 0x%08X", read_u32_be(&data[a+i]));
}
dst = read_u32_be(&data[a+i]);
if (beh_i >= 0) {
unsigned int offset = dst & 0xFFFFFF;
split_section *beh = config->sections[beh_i].children;
for (i = 0; i < config->sections[beh_i].child_count; i++) {
if (offset == beh[i].start) {
fprintf(out, ", %s", beh[i].label);
break;
}
}
if (i >= config->sections[beh_i].child_count) {
ERROR("Error: cannot find behavior %04X needed at offset %X\n", offset, a);
}
} else {
fprintf(out, ", 0x%08X", dst);
}
fprintf(out, "\n");
break;
default:
fprintf(out, ".word 0x%08X", read_u32_be(&data[a]));
for (i = 4; i < data[a+1]; i+=4) {
fprintf(out, ", 0x%08X", read_u32_be(&data[a+i]));
}
fprintf(out, "\n");
break;
}
a += data[a+1];
}
// align to next 16-byte boundary
if (a & 0x0F) {
fprintf(out, "# begin %s alignment 0x%X\n", sec->label, a);
fprintf(out, ".byte ");
fprint_hex_source(out, &data[a], ALIGN(a, 16) - a);
fprintf(out, "\n");
a = ALIGN(a, 16);
}
// remaining is geo layout script
fprintf(out, "# begin %s geo 0x%X\n", sec->label, a);
write_geolayout(out, &data[sec->start], a - sec->start, sec->end - sec->start, state);
}
static void parse_music_sequences(FILE *out, unsigned char *data, split_section *sec, arg_config *args, strbuf *makeheader)
{
#define MUSIC_SUBDIR "music"
typedef struct {
unsigned start;
unsigned length;
} sequence;
typedef struct {
unsigned revision;
unsigned count;
sequence *seq;
} sequence_bank;
char music_dir[FILENAME_MAX];
char m64_file[FILENAME_MAX];
char m64_file_rel[FILENAME_MAX];
char seq_name[128];
sequence_bank seq_bank = {0};
unsigned i;
sprintf(music_dir, "%s/%s", args->output_dir, MUSIC_SUBDIR);
make_dir(music_dir);
seq_bank.revision = read_u16_be(&data[sec->start]);
seq_bank.count = read_u16_be(&data[sec->start+2]);
if (seq_bank.count > 0) {
seq_bank.seq = malloc(seq_bank.count * sizeof(*seq_bank.seq));
for (i = 0; i < seq_bank.count; i++) {
seq_bank.seq[i].start = read_u32_be(&data[sec->start+i*8+4]);
seq_bank.seq[i].length = read_u32_be(&data[sec->start+i*8+8]);
}
}
fprintf(out, "\n# music sequence table\n");
fprintf(out, "music_sequence_table_header:\n");
fprintf(out, ".hword %d, (music_sequence_table_end - music_sequence_table) / 8\n", seq_bank.revision);
fprintf(out, "music_sequence_table:\n");
for (i = 0; i < seq_bank.count; i++) {
sprintf(seq_name, "seq_%02X", i);
fprintf(out, ".word (%s - music_sequence_table_header), (%s_end - %s) # 0x%05X, 0x%04X\n",
seq_name, seq_name, seq_name, seq_bank.seq[i].start, seq_bank.seq[i].length);
}
fprintf(out, "music_sequence_table_end:\n");
fprintf(out, "\n.align 4, 0x01\n");
for (i = 0; i < seq_bank.count; i++) {
sprintf(seq_name, "seq_%02X", i);
fprintf(out, "\n%s:", seq_name);
sprintf(m64_file, "%s/%s.m64", music_dir, seq_name);
write_file(m64_file, &data[sec->start + seq_bank.seq[i].start], seq_bank.seq[i].length);
sprintf(m64_file_rel, "%s/%s.m64", MUSIC_SUBDIR, seq_name);
fprintf(out, "\n.incbin \"%s\"\n", m64_file_rel);
// append to Makefile
strbuf_sprintf(makeheader, " \\\n$(MUSIC_DIR)/%s.m64", seq_name);
fprintf(out, "%s_end:\n", seq_name);
}
// free used memory
if (seq_bank.count > 0) {
free(seq_bank.seq);
}
}
static void parse_instrument_set(FILE *out, unsigned char *data, split_section *sec)
{
unsigned *instrument_set;
unsigned count;
unsigned i, cur;
count = sec->child_count;
// each sequence has its own instrument set defined by offsets table
instrument_set = malloc(count * sizeof(*instrument_set));
for (i = 0; i < count; i++) {
instrument_set[i] = read_u16_be(&data[sec->start + 2*i]);
}
fprintf(out, "\ninstrument_sets:\n");
for (i = 0; i < count; i++) {
fprintf(out, ".hword instrument_set_%02X - instrument_sets # 0x%04X\n", i, instrument_set[i]);
}
// output each instrument set
cur = 0;
for (i = 2*count; sec->start + i < sec->end; i++) {
unsigned char val = data[sec->start + i];
if (instrument_set[cur] == i) {
fprintf(out, "\ninstrument_set_%02X:\n.byte 0x%02X", cur, val);
cur++;
} else {
fprintf(out, ", 0x%02X", val);
}
}
fprintf(out, "\ninstrument_sets_end:\n");
}
static void generate_globals(arg_config *args, rom_config *config)
{
char globalfilename[FILENAME_MAX];
FILE *fglobal;
sprintf(globalfilename, "%s/%s", args->output_dir, GLOBALS_FILE);
fglobal = fopen(globalfilename, "w");
if (fglobal == NULL) {
ERROR("Error opening %s\n", globalfilename);
exit(3);
}
fprintf(fglobal, "# globally accessible functions and data\n"
"# these will be accessible by C code and show up in the .map file\n\n");
for (int i = 0; i < config->label_count; i++) {
fprintf(fglobal, ".global %s\n", config->labels[i].name);
}
fprintf(fglobal, "\n");
fclose(fglobal);
}
static void generate_macros(arg_config *args)
{
char incfilename[FILENAME_MAX];
FILE *finc;
sprintf(incfilename, "%s/%s", args->output_dir, MACROS_FILE);
finc = fopen(incfilename, "w");
if (finc == NULL) {
ERROR("Error opening %s\n", incfilename);
exit(3);
}
fprintf(finc, "# common macros\n"
"\n"
"# F3D vertex\n"
".macro vertex \\x, \\y, \\z, \\u, \\v, \\r=0xFF, \\g=0xFF, \\b=0xFF, \\a=0xFF\n"
" .hword \\x, \\y, \\z, 0, \\u, \\v\n .byte \\r, \\g, \\b, \\a\n"
".endm\n");
fclose(finc);
}
static void parse_sound_banks(FILE *out, unsigned char *data, split_section *secCtl, split_section *secTbl, arg_config *args, strbuf *makeheader)
{
#define SOUNDS_SUBDIR "sounds"
// TODO: unused parameters
(void)out;
(void)makeheader;
char sound_dir[FILENAME_MAX];
char sfx_file[FILENAME_MAX];
unsigned i, j, sound_count;
sfx_initialize_key_table();
sprintf(sound_dir, "%s/%s", args->output_dir, SOUNDS_SUBDIR);
make_dir(sound_dir);
sound_data_header sound_data = read_sound_data(data, secTbl->start);
sound_bank_header sound_banks = read_sound_bank(data, secCtl->start);
sound_count = 0;
for (i = 0; i < sound_banks.bank_count; i++) {
for (j = 0; j < sound_banks.banks[i].instrument_count; j++) {
if(sound_banks.banks[i].sounds[j].wav_prev != NULL) {
sprintf(sfx_file, "Bank%uSound%uPrev", i, j);
extract_raw_sound(sound_dir, sfx_file, sound_banks.banks[i].sounds[j].wav_prev, sound_banks.banks[i].sounds[j].key_base_prev, sound_data.data[i], 16000);
sound_count++;
}
if(sound_banks.banks[i].sounds[j].wav != NULL) {
sprintf(sfx_file, "Bank%uSound%u", i, j);
extract_raw_sound(sound_dir, sfx_file, sound_banks.banks[i].sounds[j].wav, sound_banks.banks[i].sounds[j].key_base, sound_data.data[i], 16000);
sound_count++;
}
if(sound_banks.banks[i].sounds[j].wav_sec != NULL) {
sprintf(sfx_file, "Bank%uSound%uSec", i, j);
extract_raw_sound(sound_dir, sfx_file, sound_banks.banks[i].sounds[j].wav_sec, sound_banks.banks[i].sounds[j].key_base_sec, sound_data.data[i], 16000);
sound_count++;
}
}
// Todo: add percussion export here
}
// free used memory
if (sound_banks.bank_count > 0) {
free(sound_banks.banks);
}
INFO("Successfully exported sounds:\n");
INFO(" # of banks: %u\n", sound_banks.bank_count);
INFO(" # of sounds: %u\n", sound_count);
}
static void generate_geo_macros(arg_config *args)
{
char macrofilename[FILENAME_MAX];
FILE *fmacro;
sprintf(macrofilename, "%s/geo_commands.inc", args->output_dir);
fmacro = fopen(macrofilename, "w");
if (fmacro == NULL) {
ERROR("Error opening %s\n", macrofilename);
exit(3);
}
fprintf(fmacro,
"# geo layout macros\n"
"\n"
"# 0x00: Branch and store return address\n"
"# 0x04: scriptTarget, segment address of geo layout\n"
".macro geo_branch_and_link scriptTarget\n"
" .byte 0x00, 0x00, 0x00, 0x00\n"
" .word \\scriptTarget\n"
".endm\n"
"\n"
"# 0x01: Terminate geo layout\n"
"# 0x01-0x03: unused\n"
".macro geo_end\n"
" .byte 0x01, 0x00, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x02: Branch\n"
"# 0x01: if 1, store next geo layout address on stack\n"
"# 0x02-0x03: unused\n"
"# 0x04: scriptTarget, segment address of geo layout\n"
".macro geo_branch type, scriptTarget\n"
" .byte 0x02, \\type, 0x00, 0x00\n"
" .word \\scriptTarget\n"
".endm\n"
"\n"
"# 0x03: Return from branch\n"
"# 0x01-0x03: unused\n"
".macro geo_return\n"
" .byte 0x03, 0x00, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x04: Open node\n"
"# 0x01-0x03: unused\n"
".macro geo_open_node\n"
" .byte 0x04, 0x00, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x05: Close node\n"
"# 0x01-0x03: unused\n"
".macro geo_close_node\n"
" .byte 0x05, 0x00, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x06: TODO\n"
"# 0x01: unused\n"
"# 0x02: s16, index of some array\n"
".macro geo_todo_06 param\n"
" .byte 0x06, 0x00\n"
" .hword \\param\n"
".endm\n"
"\n"
"# 0x07: Update current scene graph node flags\n"
"# 0x01: u8 operation (0 = reset, 1 = set, 2 = clear)\n"
"# 0x02: s16 bits\n"
".macro geo_update_node_flags operation, flagBits\n"
" .byte 0x07, \\operation\n"
" .hword \\flagBits\n"
".endm\n"
"\n"
"# 0x08: Create screen area scene graph node\n"
"# 0x01: unused\n"
"# 0x02: s16 num entries (+2) to allocate\n"
"# 0x04: s16 x\n"
"# 0x06: s16 y\n"
"# 0x08: s16 width\n"
"# 0x0A: s16 height\n"
".macro geo_node_screen_area numEntries, x, y, width, height\n"
" .byte 0x08, 0x00\n"
" .hword \\numEntries\n"
" .hword \\x, \\y, \\width, \\height\n"
".endm\n"
"\n"
"# 0x09: TODO Create ? scene graph node\n"
"# 0x02: s16 ?\n"
".macro geo_todo_09 param\n"
" .byte 0x09, 0x00\n"
" .hword \\param\n"
".endm\n"
"\n"
"# 0x0A: Create camera frustum scene graph node\n"
"# 0x01: u8 if nonzero, enable function field\n"
"# 0x02: s16 field of view\n"
"# 0x04: s16 near\n"
"# 0x06: s16 far\n"
"# 0x08: [GraphNodeFunc function]\n"
".macro geo_camera_frustum fov, near, far, function=0\n"
" .byte 0x0A\n"
" .if (\\function != 0)\n"
" .byte 0x01\n"
" .else\n"
" .byte 0x00\n"
" .endif\n"
" .hword \\fov, \\near, \\far\n"
" .if (\\function != 0)\n"
" .word \\function\n"
" .endif\n"
".endm\n"
"\n"
"# 0x0B: Create a root scene graph node\n"
"# 0x01-0x03: unused\n"
".macro geo_node_start\n"
" .byte 0x0B, 0x00, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x0C: Create zbuffer-toggling scene graph node\n"
"# 0x01: u8 enableZBuffer (1 = on, 0 = off)\n"
"# 0x02-0x03: unused\n"
".macro geo_zbuffer enable\n"
" .byte 0x0C, \\enable, 0x00, 0x00\n"
".endm\n"
"\n"
"# 0x0D: Create render range scene graph node\n"
"# 0x01-0x03: unused\n"
"# 0x04: s16 minDistance\n"
"# 0x06: s16 maxDistance\n"
".macro geo_render_range minDistance, maxDistance\n"
" .byte 0x0D, 0x00, 0x00, 0x00\n"
" .hword \\minDistance, \\maxDistance\n"
".endm\n"
"\n"
"# 0x0E: Create switch-case scene graph node\n"
"# 0x01: unused\n"
"# 0x02: s16 numCases\n"
"# 0x04: GraphNodeFunc caseSelectorFunc\n"
".macro geo_switch_case count, function\n"
" .byte 0x0E, 0x00\n"
" .hword \\count\n"
" .word \\function\n"
".endm\n"
"\n"
"# 0x0F: TODO Create ? scene graph node\n"
"# 0x01: unused\n"
"# 0x02: s16 ?\n"
"# 0x04: s16 unkX\n"
"# 0x06: s16 unkY\n"
"# 0x08: s16 unkZ\n"
"# 0x0A: s16 unkX_2\n"
"# 0x0C: s16 unkY_2\n"
"# 0x0E: s16 unkZ_2\n"
"# 0x10: GraphNodeFunc function\n"
".macro geo_todo_0F unknown, x1, y1, z1, x2, y2, z2, function\n"
" .byte 0x0F, 0x00\n"
" .hword \\unknown, \\x1, \\y1, \\z1, \\x2, \\y2, \\z2\n"
" .word \\function\n"
".endm\n"
"\n"
"# 0x10: Create translation & rotation scene graph node with optional display list\n"
"# Four different versions of 0x10\n"
"# cmd+0x01: u8 params\n"
"# 0b1000_0000: if set, enable displayList field and drawingLayer\n"
"# 0b0111_0000: fieldLayout (determines how rest of data is formatted\n"
"# 0b0000_1111: drawingLayer\n"
"#\n"
"# fieldLayout = 0: Translate & Rotate\n"
"# 0x04: s16 xTranslation\n"
"# 0x06: s16 xTranslation\n"
"# 0x08: s16 xTranslation\n"
"# 0x0A: s16 xRotation\n"
"# 0x0C: s16 xRotation\n"
"# 0x0E: s16 xRotation\n"
"# 0x10: [u32 displayList: if MSbit of params set, display list segmented address]\n"
".macro geo_translate_rotate layer, tx, ty, tz, rx, ry, rz, displayList=0\n"
" .byte 0x10\n"
" .if (\\displayList != 0)\n"
" .byte 0x00 | \\layer | 0x80\n"
" .else\n"
" .byte 0x00 | \\layer\n"
" .endif\n"
" .hword 0x0000\n"
" .hword \\tx, \\ty, \\tz\n"
" .hword \\rx, \\ry, \\rz\n"
" .if (\\displayList != 0)\n"
" .word \\displayList\n"
" .endif\n"
".endm\n"