summaryrefslogtreecommitdiff
path: root/ZAPD/ZRom.cpp
blob: f6016ae9944d41addefb5b8def0bc88285a2859c (plain)
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
#include "ZRom.h"
#include "Utils/BitConverter.h"
#include <Utils/DiskFile.h>
#include "Utils/Directory.h"
#include "yaz0/yaz0.h"

#ifdef __linux__
#include <byteswap.h>
#endif
#include <Globals.h>

namespace fs = std::filesystem;

#define DMA_ENTRY_SIZE 16

#if defined(_MSC_VER)
#define __bswap_32 _byteswap_ulong
#define bswap_32 _byteswap_ulong
#endif
#if defined __APPLE__
#define __bswap32 __builtin_bswap32
#define bswap32 __builtin_bswap32
#endif

// ROM DMA Table Start
#define OOT_OFF_NTSC_10_RC 0x7430
#define OOT_OFF_NTSC_10 0x7430
#define OOT_OFF_NTSC_11 0x7430
#define OOT_OFF_PAL_10 0x7950
#define OOT_OFF_NTSC_12 0x7960
#define OOT_OFF_PAL_11 0x7950
#define OOT_OFF_JP_GC 0x7170
#define OOT_OFF_JP_MQ 0x7170
#define OOT_OFF_US_GC 0x7170
#define OOT_OFF_US_MQ 0x7170
#define OOT_OFF_PAL_GC_DBG1 0x12F70
#define OOT_OFF_PAL_MQ_DBG 0x12F70
#define OOT_OFF_PAL_GC_DBG2 0x12F70
#define OOT_OFF_PAL_GC 0x7170
#define OOT_OFF_PAL_MQ 0x7170
#define OOT_OFF_JP_GC_CE 0x7170
#define OOT_OFF_CN_IQUE 0xB7A0
#define OOT_OFF_TW_IQUE 0xB240

#define MM_OFF_US_10 0x1A500
#define MM_OFF_US_GC 0x1AE90
#define MM_OFF_PAL_10 0x1A650
#define MM_OFF_PAL_11 0x1A8D0
#define MM_OFF_JP_GC 0x1AE90
#define MM_OFF_PAL_GC 0x1AE90
#define MM_OFF_JP_10 0x1C110
#define MM_OFF_JP_11 0x1C050
#define MM_OFF_DBG 0x24F60

#define OOT_NTSC_10 0xEC7011B7
#define OOT_NTSC_11 0xD43DA81F
#define OOT_NTSC_12 0x693BA2AE
#define OOT_PAL_10 0xB044B569
#define OOT_PAL_11 0xB2055FBD
#define OOT_NTSC_JP_GC_CE 0xF7F52DB8
#define OOT_NTSC_JP_GC 0xF611F4BA
#define OOT_NTSC_US_GC 0xF3DD35BA
#define OOT_PAL_GC 0x09465AC3
#define OOT_NTSC_JP_MQ 0xF43B45BA
#define OOT_NTSC_US_MQ 0xF034001A
#define OOT_PAL_MQ 0x1D4136F3
#define OOT_PAL_GC_DBG1 0x871E1C92  // 03-21-2002 build
#define OOT_PAL_GC_DBG2 0x87121EFE  // 03-13-2002 build
#define OOT_PAL_GC_MQ_DBG 0x917D18F6
#define OOT_IQUE_TW 0x3D81FB3E
#define OOT_IQUE_CN 0xB1E1E07B
#define UNKNOWN 0xFFFFFFFF

#define MM_NTSC_10 0x5354631C
#define MM_NTSC_10_UNCOMPRESSED 0xDA6983E7
#define MM_PAL_10 0xE97955C6
#define MM_PAL_11 0x0A5D8F83
#define MM_NTSC_GC 0xB443EB08
#define MM_NTSC_JP_GC 0x8473D0C1
#define MM_PAL_GC 0x6AECEC4F

bool ZRom::IsMQ() {
    int crc = BitConverter::ToInt32BE(romData, 0x10);
    switch (crc) {
        case OOT_NTSC_10:
        case OOT_NTSC_11:
        case OOT_NTSC_12:
        case OOT_PAL_10:
        case OOT_PAL_11:
        case OOT_NTSC_JP_GC:
        case OOT_NTSC_JP_GC_CE:
        case OOT_NTSC_US_GC:
        case OOT_PAL_GC:
        case OOT_PAL_GC_DBG1:
        case OOT_PAL_GC_DBG2:
        case OOT_IQUE_CN:
        case OOT_IQUE_TW:
        // MM - Always not MQ
        case MM_NTSC_10:
        case MM_NTSC_10_UNCOMPRESSED:
		case MM_PAL_10:
		case MM_PAL_11:
		case MM_NTSC_GC:
		case MM_PAL_GC:
		case MM_NTSC_JP_GC:
        default:
            return false;
        case OOT_NTSC_JP_MQ:
        case OOT_NTSC_US_MQ:
        case OOT_PAL_MQ:
        case OOT_PAL_GC_MQ_DBG:
            return true;
    }
}

ZRom::ZRom(std::string romPath)
{
	RomVersion version;
	romData = DiskFile::ReadAllBytes(romPath);

	BitConverter::RomToBigEndian(romData.data(), romData.size());

	version.crc = BitConverter::ToInt32BE(romData, 0x10);

	switch (version.crc)
	{
	case OOT_NTSC_10:
		version.version = "N64 NTSC 1.0";
		version.listPath = "ntsc_oot.txt";
		version.offset = OOT_OFF_NTSC_10;
		break;
	case OOT_NTSC_11:
		version.version = "N64 NTSC 1.1";
		version.listPath = "ntsc_oot.txt";
		version.offset = OOT_OFF_NTSC_11;
		break;
	case OOT_NTSC_12:
		version.version = "N64 NTSC 1.2";
		version.listPath = "ntsc_12_oot.txt";
		version.offset = OOT_OFF_NTSC_12;
		break;
	case OOT_PAL_10:
		version.version = "N64 PAL 1.0";
		version.listPath = "pal_oot.txt";
		version.offset = OOT_OFF_PAL_10;
		break;
	case OOT_PAL_11:
		version.version = "N64 PAL 1.1";
		version.listPath = "pal_oot.txt";
		version.offset = OOT_OFF_PAL_11;
		break;
	case OOT_NTSC_JP_GC:
		version.version = "JP GameCube (MQ Disk)";
		version.listPath = "gamecube.txt";
		version.offset = OOT_OFF_JP_GC;
		break;
	case OOT_NTSC_JP_GC_CE:
		version.version = "GameCube (Collectors Edition Disk)";
		version.listPath = "gamecube.txt";
		version.offset = OOT_OFF_JP_GC_CE;
		break;
	case OOT_NTSC_JP_MQ:
		version.version = "JP Master Quest";
		version.listPath = "gamecube.txt";
		version.offset = OOT_OFF_JP_MQ;
		break;
	case OOT_NTSC_US_MQ:
		version.version = "NTSC Master Quest";
		version.listPath = "gamecube.txt";
		version.offset = OOT_OFF_JP_MQ;
		break;
	case OOT_NTSC_US_GC:
		version.version = "NTSC GameCube";
		version.listPath = "gamecube.txt";
		version.offset = OOT_OFF_US_MQ;
		break;
	case OOT_PAL_GC:
		version.version = "PAL GameCube";
		version.listPath = "gamecube_pal.txt";
		version.offset = OOT_OFF_PAL_GC;
		break;
	case OOT_PAL_MQ:
		version.version = "PAL Master Quest";
		version.listPath = "gamecube_pal.txt";
		version.offset = OOT_OFF_PAL_MQ;
		break;
	case OOT_PAL_GC_DBG1:
		version.version = "GameCube Debug 1.0";
		version.listPath = "dbg.txt";
		version.offset = OOT_OFF_PAL_GC_DBG1;
		break;
	case OOT_PAL_GC_DBG2:
		version.version = "GameCube Debug 2.0";
		version.listPath = "dbg.txt";
		version.offset = OOT_OFF_PAL_GC_DBG2;
		break;
	case OOT_PAL_GC_MQ_DBG:
		version.version = "GameCube MQ-Debug";
		version.listPath = "dbg.txt";
		version.offset = OOT_OFF_PAL_MQ_DBG;
		break;
	case OOT_IQUE_CN:
		version.version = "OoT IQue";
		version.listPath = "ique.txt";
		version.offset = OOT_OFF_CN_IQUE;
		break;
	case OOT_IQUE_TW:
		version.version = "TW IQue";
		version.listPath = "ique.txt";
		version.offset = OOT_OFF_TW_IQUE;
		break;
	case MM_NTSC_10:
		version.version = "MM US 1.0";
		version.listPath = "mm.txt";
		version.offset = MM_OFF_US_10;
		break;
	case MM_NTSC_10_UNCOMPRESSED:
		version.version = "MM US 1.0";
		version.listPath = "mm.txt";
		version.offset = MM_OFF_US_10;
		break;
	case MM_NTSC_GC:
		version.version = "MM US GC";
		version.listPath = "mm_gc.txt";
		version.offset = MM_OFF_US_GC;
		break;
	case MM_NTSC_JP_GC:
		version.version = "MM JP GC";
		version.listPath = "mm_gc_jp.txt";
		version.offset = MM_OFF_JP_GC;
		break;
	case MM_PAL_10:
		version.version = "MM PAL 1.0";
		version.listPath = "mm_pal.txt";
		version.offset = MM_OFF_PAL_10;
		break;
	case MM_PAL_11:
		version.version = "MM PAL 1.1";
		version.listPath = "mm_pal.txt";
		version.offset = MM_OFF_PAL_11;
		break;
	case MM_PAL_GC:
		version.version = "MM PAL GC";
		version.listPath = "mm_gc_pal.txt";
		version.offset = MM_OFF_PAL_GC;
		break;
	}

	auto path = StringHelper::Sprintf("%s/%s", Globals::Instance->fileListPath.string().c_str(), version.listPath.c_str());
	auto txt = DiskFile::ReadAllText(path);
	std::vector<std::string> lines = StringHelper::Split(txt, "\n");

    std::vector<uint8_t> decompressedData(1);

	for (unsigned int i = 0; i < lines.size(); i++)
	{
		lines[i] = StringHelper::Strip(lines[i], "\r");
		bool yarCompressed = false;
		const int romOffset = version.offset + (DMA_ENTRY_SIZE * i);

		const int virtStart = BitConverter::ToInt32BE(romData, romOffset + 0);
		const int virtEnd = BitConverter::ToInt32BE(romData, romOffset + 4);
		const int physStart = BitConverter::ToInt32BE(romData, romOffset + 8);
		const int physEnd = BitConverter::ToInt32BE(romData, romOffset + 12);
			// File Deleted
		if (physEnd == 0xFFFFFFFF && physStart == 0xFFFFFFFF)
		{
			// MM has some other checks that we might need to do
			//if (virtEnd - virtStart == 0)
			continue;
		}

		const bool compressed = physEnd != 0;
		int size = compressed ? physEnd - physStart : virtEnd - virtStart;

		auto outData = std::vector<uint8_t>();
		outData.resize(size);
		memcpy(outData.data(), romData.data() + physStart, size);
		// An ifdef is used here because at this point the XMLs haven't been parsed, and we don't
		// know if this is MM or OOT
#ifdef GAME_MM
		// PAL filelists differ For yar file locations, TODO: Find way to avoid special casing numbers
		if (version.crc == MM_PAL_10 || version.crc == MM_PAL_11 || version.crc == MM_PAL_GC)
		{
			if ((i >= 17 && i <= 28) || i == 30)
			{
				yarCompressed = true;
			}
		}
		else
		{
			if ((i >= 15 && i <= 20) || i == 22)
			{
				yarCompressed = true;
			}
		}
#endif

		if (compressed)
		{
			int decSize = virtEnd - virtStart;
			decompressedData = std::vector<uint8_t>();
			decompressedData.resize(decSize);
			yaz0_decode(outData.data(), decompressedData.data(), decSize);
			files[lines[i]] = decompressedData;
		}
		else if (yarCompressed)
		{
			//int decSize = virtEnd - virtStart;
			decompressedData = std::vector<uint8_t>();
			decompressedData.resize(1024*1024); //TODO FIX THIS PLEASE
			yaz0_decodeYarArchive(outData.data(), decompressedData.data(), size);
			files[lines[i]] = decompressedData;
		}
		else
			files[lines[i]] = outData;

		//DiskFile::WriteAllBytes(StringHelper::Sprintf("baserom/%s", lines[i].c_str()), files[lines[i]]);
	}
}

std::vector<uint8_t> ZRom::GetFile(std::string fileName)
{
	return files[fileName];
}