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
|
#include "DisplayListFactory.h"
#include "DisplayListOverrides.h"
#include "utils/Decompressor.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <fstream>
#include "n64/gbi-otr.h"
#ifdef STANDALONE
#include <gfxd.h>
#endif
#define C0(pos, width) ((w0 >> (pos)) & ((1U << width) - 1))
#define ALIGN16(val) (((val) + 0xF) & ~0xF)
std::unordered_map<std::string, uint8_t> gF3DTable = {
{ "G_VTX", 0x04 },
{ "G_DL", 0x06 },
{ "G_MTX", 0x1 },
{ "G_ENDDL", 0xB8 },
{ "G_SETTIMG", 0xFD },
{ "G_MOVEMEM", 0x03 },
{ "G_MV_L0", 0x86 },
{ "G_MV_L1", 0x88 },
{ "G_MV_LIGHT", 0xA },
{ "G_TRI2", 0xB1 },
{ "G_QUAD", -1 }
};
std::unordered_map<std::string, uint8_t> gF3DExTable = {
{ "G_VTX", 0x04 },
{ "G_DL", 0x06 },
{ "G_MTX", 0x1 },
{ "G_ENDDL", 0xB8 },
{ "G_SETTIMG", 0xFD },
{ "G_MOVEMEM", 0x03 },
{ "G_MV_L0", 0x86 },
{ "G_MV_L1", 0x88 },
{ "G_MV_LIGHT", 0xA },
{ "G_TRI2", 0xB1 },
{ "G_QUAD", 0xB5 }
};
std::unordered_map<std::string, uint8_t> gF3DEx2Table = {
{ "G_VTX", 0x01 },
{ "G_DL", 0xDE },
{ "G_MTX", 0xDA },
{ "G_ENDDL", 0xDF },
{ "G_SETTIMG", 0xFD },
{ "G_MOVEMEM", 0xDC },
{ "G_MV_L0", 0x86 },
{ "G_MV_L1", 0x88 },
{ "G_MV_LIGHT", 0xA },
{ "G_TRI2", 0x06 },
{ "G_QUAD", 0x07 }
};
std::unordered_map<GBIVersion, std::unordered_map<std::string, uint8_t>> gGBITable = {
{ GBIVersion::f3d, gF3DTable },
{ GBIVersion::f3dex, gF3DExTable },
{ GBIVersion::f3dex2, gF3DEx2Table },
};
#define GBI(cmd) gGBITable[Companion::Instance->GetGBIVersion()][#cmd]
#ifdef STANDALONE
void GFXDSetGBIVersion(){
switch (Companion::Instance->GetGBIVersion()) {
case GBIVersion::f3d:
gfxd_target(gfxd_f3d);
break;
case GBIVersion::f3dex:
gfxd_target(gfxd_f3dex);
break;
case GBIVersion::f3db:
gfxd_target(gfxd_f3db);
break;
case GBIVersion::f3dex2:
gfxd_target(gfxd_f3dex2);
break;
case GBIVersion::f3dexb:
gfxd_target(gfxd_f3dexb);
break;
}
}
#endif
ExportResult DListHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
const auto symbol = GetSafeNode(node, "symbol", entryName);
if(Companion::Instance->IsOTRMode()){
write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
return std::nullopt;
}
write << "extern Gfx " << symbol << "[];\n";
return std::nullopt;
}
#ifdef STANDALONE
bool hasTable = false;
ExportResult DListCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
const auto cmds = std::static_pointer_cast<DListData>(raw)->mGfxs;
const auto symbol = GetSafeNode(node, "symbol", entryName);
auto offset = GetSafeNode<uint32_t>(node, "offset");
const auto searchTable = Companion::Instance->SearchTable(offset);
const auto sz = (sizeof(uint32_t) * cmds.size());
hasTable = searchTable.has_value();
size_t isize = cmds.size();
char out[0xFFFF] = {0};
gfxd_input_buffer(cmds.data(), sizeof(uint32_t) * cmds.size());
gfxd_output_buffer(out, sizeof(out));
gfxd_endian(gfxd_endian_host, sizeof(uint32_t));
gfxd_macro_fn([] {
auto gfx = static_cast<const N64Gfx*>(gfxd_macro_data());
const uint8_t opcode = (gfx->words.w0 >> 24) & 0xFF;
if(hasTable) {
gfxd_puts(fourSpaceTab fourSpaceTab);
} else {
gfxd_puts(fourSpaceTab);
}
// For mk64 only
if(opcode == GBI(G_QUAD) && Companion::Instance->GetGBIMinorVersion() == GBIMinorVersion::Mk64) {
GFXDOverride::Quadrangle(gfx);
// Prevents mix and matching of quadrangle commands. Forces 2TRI only.
} else if(opcode == GBI(G_TRI2)) {
GFXDOverride::Triangle2(gfx);
} else {
gfxd_macro_dflt();
}
gfxd_puts(",\n");
return 0;
});
gfxd_vtx_callback(GFXDOverride::Vtx);
gfxd_timg_callback(GFXDOverride::Texture);
gfxd_dl_callback(GFXDOverride::DisplayList);
gfxd_tlut_callback(GFXDOverride::Palette);
gfxd_lightsn_callback(GFXDOverride::Lights);
gfxd_light_callback(GFXDOverride::Light);
gfxd_vp_callback(GFXDOverride::Viewport);
gfxd_mtx_callback(GFXDOverride::Matrix);
GFXDSetGBIVersion();
if(searchTable.has_value()){
const auto [name, start, end, mode, index_size] = searchTable.value();
if(mode != TableMode::Append){
throw std::runtime_error("Reference mode is not supported for now");
}
if (index_size > -1) {
isize = index_size;
}
if(start == offset){
gfxd_puts(("Gfx " + name + "[][" + std::to_string(isize / 2) + "] = {\n").c_str());
gfxd_puts("\t{\n");
} else {
gfxd_puts("\t{\n");
}
gfxd_execute();
write << std::string(out);
if(end == offset){
write << fourSpaceTab << "}\n";
write << "};\n";
if (Companion::Instance->IsDebug()) {
write << "// count: " << std::to_string(sz / 8) << " Gfx\n";
}else {
write << "\n";
}
} else {
write << fourSpaceTab << "},\n";
}
} else {
gfxd_puts(("Gfx " + symbol + "[] = {\n").c_str());
gfxd_execute();
write << std::string(out);
write << "};\n";
if (Companion::Instance->IsDebug()) {
write << "// count: " << std::to_string(sz / 8) << " Gfx\n";
} else {
write << "\n";
}
}
return offset + sz;
}
void DebugDisplayList(uint32_t w0, uint32_t w1){
uint32_t dlist[] = {w0, w1};
gfxd_input_buffer(dlist, sizeof(dlist));
gfxd_output_fd(fileno(stdout));
gfxd_endian(gfxd_endian_host, sizeof(uint32_t));
gfxd_macro_fn([](){
gfxd_puts("> ");
gfxd_macro_dflt();
gfxd_puts("\n");
return 0;
});
gfxd_vtx_callback(GFXDOverride::Vtx);
gfxd_timg_callback(GFXDOverride::Texture);
gfxd_dl_callback(GFXDOverride::DisplayList);
gfxd_tlut_callback(GFXDOverride::Palette);
//gfxd_light_callback(GFXDOverride::Light);
GFXDSetGBIVersion();
gfxd_execute();
}
#endif
std::optional<std::tuple<std::string, YAML::Node>> SearchVtx(uint32_t ptr){
auto decs = Companion::Instance->GetNodesByType("VTX");
if(!decs.has_value()){
return std::nullopt;
}
for(auto& dec : decs.value()){
auto [name, node] = dec;
auto offset = GetSafeNode<uint32_t>(node, "offset");
auto count = GetSafeNode<uint32_t>(node, "count");
auto end = ALIGN16((count * sizeof(N64Vtx_t)));
if(ptr > offset && ptr < offset + end){
return std::make_tuple(GetSafeNode<std::string>(node, "symbol", name), node);
}
}
return std::nullopt;
}
ExportResult DListBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
const auto gbi = Companion::Instance->GetGBIVersion();
auto cmds = std::static_pointer_cast<DListData>(raw)->mGfxs;
auto writer = LUS::BinaryWriter();
WriteHeader(writer, Torch::ResourceType::DisplayList, 0);
writer.Write((int8_t) gbi);
while (writer.GetBaseAddress() % 8 != 0)
writer.Write(static_cast<int8_t>(0xFF));
auto bhash = CRC64((*replacement).c_str());
writer.Write(static_cast<uint32_t>((G_MARKER << 24)));
writer.Write(0xBEEFBEEF);
writer.Write(static_cast<uint32_t>(bhash >> 32));
writer.Write(static_cast<uint32_t>(bhash & 0xFFFFFFFF));
for(size_t i = 0; i < cmds.size(); i+=2){
auto w0 = cmds[i];
auto w1 = cmds[i + 1];
uint8_t opcode = w0 >> 24;
if(opcode == GBI(G_VTX)) {
size_t nvtx;
size_t didx;
switch (gbi) {
case GBIVersion::f3dex2:
nvtx = C0(12, 8);
didx = C0(1, 7) - C0(12, 8);
break;
case GBIVersion::f3dex:
case GBIVersion::f3dexb:
nvtx = C0(10, 6);
didx = C0(17, 7);
break;
default:
nvtx = (C0(0, 16)) / sizeof(N64Vtx_t);
didx = C0(16, 4);
break;
}
auto ptr = w1;
auto overlap = GFXDOverride::GetVtxOverlap(ptr);
if(overlap.has_value()){
auto ovnode = std::get<1>(overlap.value());
auto path = Companion::Instance->RelativePath(std::get<0>(overlap.value()));
uint64_t hash = CRC64(path.c_str());
if(hash == 0) {
throw std::runtime_error("Vtx hash is 0 for " + std::get<0>(overlap.value()));
}
SPDLOG_INFO("Found vtx: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, path);
auto offset = GetSafeNode<uint32_t>(ovnode, "offset");
auto count = GetSafeNode<uint32_t>(ovnode, "count");
auto diff = ASSET_PTR(ptr) - ASSET_PTR(offset);
N64Gfx value = gsSPVertexOTR(diff, nvtx, didx);
SPDLOG_INFO("gsSPVertexOTR({}, {}, {})", diff, nvtx, didx);
w0 = value.words.w0;
w1 = value.words.w1;
writer.Write(w0);
writer.Write(w1);
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find vtx at 0x{:X}", ptr);
}
auto dec = Companion::Instance->GetSafeStringByAddr(ptr, "VTX");
if(dec.has_value()){
uint64_t hash = CRC64(dec.value().c_str());
if(hash == 0) {
throw std::runtime_error("Vtx hash is 0 for " + dec.value());
}
SPDLOG_INFO("Found vtx: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, dec.value());
N64Gfx value = gsSPVertexOTR(0, nvtx, didx);
SPDLOG_INFO("gsSPVertex({}, {}, 0x{:X})", nvtx, didx, ptr);
w0 = value.words.w0;
w1 = value.words.w1;
writer.Write(w0);
writer.Write(w1);
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find vtx at 0x{:X}", ptr);
}
}
if(opcode == GBI(G_DL)) {
N64Gfx value;
auto ptr = w1;
auto dec = Companion::Instance->GetSafeStringByAddr(ptr, "GFX");
auto branch = (w0 >> 16) & G_DL_NO_PUSH;
// Export displaylist segment addresses as an index into a buffer of gfx
{
value = gsSPDisplayListOTRHash(ptr);
w0 = value.words.w0;
w1 = value.words.w1;
}
writer.Write(w0);
writer.Write(w1);
if(dec.has_value()){
uint64_t hash = CRC64(dec.value().c_str());
SPDLOG_INFO("Found display list: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, dec.value());
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find display list at 0x{:X}", ptr);
}
if(branch){
writer.Write(w0);
writer.Write(w1);
value = gsSPRawOpcode(GBI(G_ENDDL));
w0 = value.words.w0;
w1 = value.words.w1;
}
}
// TODO: Fix this opcode
if(opcode == GBI(G_MOVEMEM)) {
auto ptr = w1;
uint8_t index = 0;
uint8_t offset = 0;
bool hasOffset = false;
switch (gbi) {
case GBIVersion::f3d:
index = C0(16, 8);
offset = 0;
break;
case GBIVersion::f3dex:
index = (w0 >> 16) & 0xFF;
offset = C0(8, 8) * 8;
break;
default:
index = C0(0, 8);
offset = C0(8, 8) * 8;
break;
}
auto res = Companion::Instance->GetStringByAddr(ptr);
if(!res.has_value()){
res = Companion::Instance->GetStringByAddr(ptr - 0x8);
hasOffset = res.has_value();
if(!hasOffset){
SPDLOG_INFO("Could not find light {:X}", ptr);
// throw std::runtime_error("Could not find light");
}
}
w0 &= 0x00FFFFFF;
w0 += G_MOVEMEM_OTR_HASH << 24;
w1 = _SHIFTL(index, 24, 8) | _SHIFTL(offset, 16, 8) | _SHIFTL((uint8_t)(hasOffset ? 1 : 0), 8, 8);
writer.Write(w0);
writer.Write(w1);
if(res.has_value()){
uint64_t hash = CRC64(res.value().c_str());
SPDLOG_INFO("Found movemem: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, res.value());
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find light at 0x{:X}", ptr);
}
}
if(opcode == GBI(G_SETTIMG)) {
auto ptr = w1;
auto dec = Companion::Instance->GetSafeStringByAddr(ptr, "TEXTURE");
// Export texture segment addresses as segmented addresses
// if ((Companion::Instance->GetGBIMinorVersion() == GBIMinorVersion::Mk64) && ((SEGMENT_NUMBER(w1) == 0x03) || (SEGMENT_NUMBER(w1) == 0x05))) {
// w1 |= 1;
// writer.Write(w0);
// writer.Write(w1);
// } else
{
N64Gfx value = gsDPSetTextureOTRImage(C0(21, 3), C0(19, 2), C0(0, 10), ptr);
w0 = value.words.w0;
w1 = value.words.w1;
writer.Write(w0);
writer.Write(w1);
if(dec.has_value()){
uint64_t hash = CRC64(dec.value().c_str());
if(hash == 0){
throw std::runtime_error("Texture hash is 0 for " + dec.value());
}
SPDLOG_INFO("Found texture: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, dec.value());
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find texture at 0x{:X}", ptr);
}
}
}
if(opcode == GBI(G_MTX)) {
auto ptr = w1;
auto dec = Companion::Instance->GetSafeStringByAddr(ptr, "MTX");
w0 &= 0x00FFFFFF;
w0 += G_MTX_OTR << 24;
w1 = 0;
writer.Write(w0);
writer.Write(w1);
if(dec.has_value()){
uint64_t hash = CRC64(dec.value().c_str());
if(hash == 0){
throw std::runtime_error("Matrix hash is 0 for " + dec.value());
}
SPDLOG_INFO("Found matrix: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, dec.value());
w0 = hash >> 32;
w1 = hash & 0xFFFFFFFF;
} else {
SPDLOG_WARN("Could not find matrix at 0x{:X}", ptr);
}
}
writer.Write(w0);
writer.Write(w1);
}
writer.Finish(write);
return std::nullopt;
}
std::optional<std::shared_ptr<IParsedData>> DListFactory::parse(std::vector<uint8_t>& raw_buffer, YAML::Node& node) {
const auto gbi = Companion::Instance->GetGBIVersion();
auto count = GetSafeNode<int32_t>(node, "count", -1);
auto [_, segment] = Decompressor::AutoDecode(node, raw_buffer);
LUS::BinaryReader reader(segment.data, segment.size);
reader.SetEndianness(Torch::Endianness::Big);
std::vector<uint32_t> gfxs;
auto processing = true;
size_t length = 0;
while (processing){
auto w0 = reader.ReadUInt32();
auto w1 = reader.ReadUInt32();
uint8_t opcode = w0 >> 24;
if(opcode == GBI(G_ENDDL)) {
processing = false;
}
if(opcode == GBI(G_DL)) {
if (SEGMENT_NUMBER(node["offset"].as<uint32_t>()) == SEGMENT_NUMBER(w1)) {
std::optional<uint32_t> segment;
if ((w0 >> 16) & G_DL_NO_PUSH) {
SPDLOG_INFO("Branch List Command Found");
processing = false;
}
YAML::Node gfx;
gfx["type"] = "GFX";
gfx["offset"] = w1;
Companion::Instance->AddAsset(gfx);
}
}
// This opcode is generally used as part of multiple macros such as gsSPSetLights1.
// We need to process gsSPLight which is a subcommand inside G_MOVEMEM (0x03).
if(opcode == GBI(G_MOVEMEM)) {
// 0x03860000 or 0x03880000 subcommand will contain 0x86/0x88 for G_MV_L0 and G_MV_L1. Other subcommands also exist.
uint8_t subcommand = (w0 >> 16) & 0xFF;
uint8_t index = 0;
uint8_t offset = 0;
bool light = false;
switch (Companion::Instance->GetGBIVersion()) {
// If needing light generation on G_MV_L0 then we'll need to walk the DL ptr forward/backward to check for 0xBC
// Otherwise mk64 will break.
// PD: Mega, this works for sm64 too, why you didn't implement it? >:(
// PD: Im jk, <3
case GBIVersion::f3d:
case GBIVersion::f3dex:
/*
* Only generate lights on the second gsSPLight.
* gsSPSetLights1(name) outputs three macros:
*
* gsSPNumLights(NUMLIGHTS_1)
* gsSPLight(&name.l[0], G_MV_L0)
* gsSPLight(&name.a, G_MV_L1) <-- This ptr is used to generate the lights
*/
if (subcommand == GBI(G_MV_L1)) {
light = true;
}
case GBIVersion::f3dex2:
index = C0(0, 8);
offset = C0(8, 8) * 8;
// same thing as above; see macro gSPLight at gbi.h
if(index == GBI(G_MV_LIGHT) && offset == (2 * 24 + 24)) {
light = true;
}
break;
default:
throw std::runtime_error("Unsupported GBI version");
}
if(light){
YAML::Node lnode;
lnode["type"] = "LIGHTS";
lnode["offset"] = w1;
Companion::Instance->AddAsset(lnode);
}
}
if(opcode == GBI(G_VTX)) {
uint32_t nvtx;
switch (gbi) {
case GBIVersion::f3dex2:
nvtx = C0(12, 8);
break;
case GBIVersion::f3dex:
case GBIVersion::f3dexb:
nvtx = C0(10, 6);
break;
default:
nvtx = (C0(0, 16)) / sizeof(N64Vtx_t);
break;
}
const auto decl = Companion::Instance->GetNodeByAddr(w1);
if(!decl.has_value()){
auto adjPtr = Companion::Instance->PatchVirtualAddr(w1);
auto search = SearchVtx(adjPtr);
if(search.has_value()){
auto [path, vtx] = search.value();
SPDLOG_INFO("Path: {}", path);
auto lOffset = GetSafeNode<uint32_t>(vtx, "offset");
auto lCount = GetSafeNode<uint32_t>(vtx, "count");
auto lSize = ALIGN16(lCount * sizeof(N64Vtx_t));
if(adjPtr > lOffset && adjPtr <= lOffset + lSize){
SPDLOG_INFO("Found vtx at 0x{:X} matching last vtx at 0x{:X}", adjPtr, lOffset);
GFXDOverride::RegisterVTXOverlap(adjPtr, search.value());
}
} else {
YAML::Node vtx;
vtx["type"] = "VTX";
vtx["offset"] = adjPtr;
vtx["count"] = nvtx;
Companion::Instance->AddAsset(vtx);
}
} else {
SPDLOG_WARN("Found vtx at 0x{:X}", w1);
}
}
if(count != -1 && length++ >= count){
break;
}
gfxs.push_back(w0);
gfxs.push_back(w1);
}
return std::make_shared<DListData>(gfxs);
}
|