-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRomTools_Common.cpp
More file actions
251 lines (217 loc) · 7.08 KB
/
RomTools_Common.cpp
File metadata and controls
251 lines (217 loc) · 7.08 KB
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
#include <Windows.h>
#include <stdio.h>
#include "RomTools_Common.h"
#include "Settings Api.h"
#include "Pif.h"
#include "xxhash64.h"
#include "FileHandler.h"
#include <string>
void CountryCodeToString(char string[], BYTE Country, int length) {
switch (Country) {
case '7': strncpy(string, "Beta", length); break;
case 'A': strncpy(string, "NTSC", length); break;
case 'B': strncpy(string, "Brazil", length); break;
case 'C': strncpy(string, "China", length); break;
case 'D': strncpy(string, "Germany", length); break;
case 'E': strncpy(string, "USA", length); break;
case 'F': strncpy(string, "France", length); break;
case 'G': strncpy(string, "Gateway (NTSC)", length); break;
case 'I': strncpy(string, "Italy", length); break;
case 'J': strncpy(string, "Japan", length); break;
case 'K': strncpy(string, "Korea", length); break;
case 'L': strncpy(string, "Lodgenet (PAL)", length); break;
case 'P': strncpy(string, "Europe", length); break;
case 'S': strncpy(string, "Spain", length); break;
case 'U': strncpy(string, "Australia", length); break;
case 'X': strncpy(string, "PAL", length); break;
case 'Y': strncpy(string, "PAL", length); break;
case ' ': strncpy(string, "None (PD by NAN)", length); break;
case 0: strncpy(string, "None (PD)", length); break;
default:
if (length > 20)
sprintf(string, "Unknown %c (%02X)", Country, Country);
break;
}
}
void CountryCodeToShortString(char string[], BYTE Country, int length) {
switch (Country) {
case '7': strncpy(string, "(Beta)", length); break;
case 'A': strncpy(string, "(NTSC)", length); break;
case 'B': strncpy(string, "(B)", length); break;
case 'C': strncpy(string, "(C)", length); break;
case 'D': strncpy(string, "(G)", length); break;
case 'E': strncpy(string, "(U)", length); break;
case 'F': strncpy(string, "(F)", length); break;
case 'I': strncpy(string, "(I)", length); break;
case 'J': strncpy(string, "(J)", length); break;
case 'K': strncpy(string, "(K)", length); break;
case 'P': strncpy(string, "(E)", length); break;
case 'S': strncpy(string, "(S)", length); break;
case 'U': strncpy(string, "(A)", length); break;
case 'X': strncpy(string, "(PAL)", length); break;
case 'Y': strncpy(string, "(PAL)", length); break;
case ' ': strncpy(string, "(PD by NAN)", length); break;
case 0: strncpy(string, "(PD)", length); break;
default:
if (length > 20)
sprintf(string, "(%02X)", Country);
break;
}
}
int GetRomRegion(BYTE* RomData) {
BYTE Country;
GetRomCountry(&Country, RomData);
return GetRomRegionByCode(Country);
}
int GetRomRegionByCode(BYTE CountryCode) {
switch (CountryCode) {
case 'D': // Germany
case 'F': // French
case 'I': // Italian
case 'P': // Europe
case 'L': // Lodgenet (PAL)
case 'S': // Spanish
case 'U': // Australia
case 'X': // PAL
case 'Y': // PAL
return PAL_Region;
case '7': // Beta
case 'A': // NTSC (Only 1080 JU?)
case 'B': // Brazil
case 'C': // China
case 'E': // USA
case 'G': // Gateway (NTSC)
case 'J': // Japan
case 'K': // Korea
case ' ': // PD
case 0: // PD
return NTSC_Region;
default:
return Unknown_Region;
}
}
void GetRomName(char* Name, BYTE* RomData) {
int count;
memcpy(Name, (void*)(RomData + 0x20), 20);
Name[20] = '\0';
// Byteswap the Internal Name
for (count = 0; count < 20; count += 4) {
Name[count] ^= Name[count + 3];
Name[count + 3] ^= Name[count];
Name[count] ^= Name[count + 3];
Name[count + 1] ^= Name[count + 2];
Name[count + 2] ^= Name[count + 1];
Name[count + 1] ^= Name[count + 2];
}
// Remove excess spaces at the end
for (count = 19; count >= 0; count--) {
if (Name[count] != '\0' && Name[count] != ' ')
break;
else
Name[count] = '\0';
}
}
void GetRomFullName(char* FullName, BYTE* RomData, char* FullPath) {
char Identifier[100], InternalName[25], ShortCountryCode[25];
BYTE Country;
std::string junk;
RomID(Identifier, RomData);
junk = ReadStr(RDS_NAME, Identifier, "Name", STR_EMPTY);
// We have an existing entry in the RDS, use that as the full name
if (!junk.empty()) {
strncpy(FullName, junk.c_str(), junk.length());
FullName[junk.length()] = '\0';
return;
}
// Generate a Name using the Internal Name and the Country Code
GetRomName(InternalName, RomData);
GetRomCountry(&Country, RomData);
CountryCodeToShortString(ShortCountryCode, Country, sizeof(ShortCountryCode));
if (strlen(InternalName) != 0 && strlen(ShortCountryCode) != 0) {
sprintf(FullName, "%s %s", InternalName, ShortCountryCode);
return;
}
// Attempts to read or generate a name have failed, use the file name itself
_splitpath(FullPath, NULL, NULL, FullName, NULL);
}
void GetRomCartID(char* ID, BYTE* RomData) {
ID[0] = *(RomData + 0x3F);
ID[1] = *(RomData + 0x3E);
ID[2] = '\0';
}
void GetRomReleaseVersion(int* ReleaseVersion, BYTE* RomData) {
*ReleaseVersion = (int)RomData[0x3C];
}
void GetRomSdkVersion(BYTE* SdkVersion, BYTE* RomData) {
SdkVersion[0] = RomData[0x0C];
SdkVersion[1] = RomData[0x0D];
}
void GetRomManufacturer(BYTE* Manufacturer, BYTE* RomData) {
*Manufacturer = *(BYTE*)(RomData + 0x38);
}
void GetRomCountry(BYTE* Country, BYTE* RomData) {
*Country = *(RomData + 0x3D);
}
void GetRomCRC1(DWORD* Crc1, BYTE* RomData) {
*Crc1 = *(DWORD*)(RomData + 0x10);
}
void GetRomCRC2(DWORD* Crc2, BYTE* RomData) {
*Crc2 = *(DWORD*)(RomData + 0x14);
}
enum CIC_CHIP GetRomCicChipID(BYTE* RomData) {
return GetCicChipID(RomData);
}
void GetRomCicChipString(BYTE* RomData, char String[], int length) {
BuildRomCicChipString(GetRomCicChipID(RomData), String, length, GetRomRegion(RomData));
}
void BuildRomCicChipString(enum CIC_CHIP ID, char String[], int length, int region) {
switch (ID) {
case CIC_NUS_6101:
region == PAL_Region ? strcpy(&String[0], "CIC-NUS-7102") : strcpy(&String[0], "CIC-NUS-6101");
break;
case CIC_NUS_6102:
region == PAL_Region ? strcpy(&String[0], "CIC-NUS-7101") : strcpy(&String[0], "CIC-NUS-6102");
break;
case CIC_NUS_6103:
region == PAL_Region ? strcpy(&String[0], "CIC-NUS-7103") : strcpy(&String[0], "CIC-NUS-6103");
break;
case CIC_NUS_6105:
region == PAL_Region ? strcpy(&String[0], "CIC-NUS-7105") : strcpy(&String[0], "CIC-NUS-6105");
break;
case CIC_NUS_6106:
region == PAL_Region ? strcpy(&String[0], "CIC-NUS-7106") : strcpy(&String[0], "CIC-NUS-6106");
break;
case CIC_NUS_5167:
strcpy(&String[0], "CIC-NUS-5167");
break;
case CIC_NUS_8303:
strcpy(&String[0], "CIC-NUS-8303");
break;
case CIC_NUS_DDUS:
strcpy(&String[0], "CIC-NUS-DDUS");
break;
case CIC_NUS_8401:
strcpy(&String[0], "CIC-NUS-8401");
break;
default:
snprintf(&String[0], length, "Unknown");
break;
}
}
void RomID(char* ID, BYTE* RomData) {
DWORD CRC1, CRC2;
BYTE Country;
GetRomCRC1(&CRC1, RomData);
GetRomCRC2(&CRC2, RomData);
GetRomCountry(&Country, RomData);
RomIDPreScanned(ID, &CRC1, &CRC2, &Country);
}
void RomIDPreScanned(char* ID, DWORD* CRC1, DWORD* CRC2, BYTE* Country) {
sprintf(ID, "%08X-%08X-C:%02X", *CRC1, *CRC2, *Country);
}
void RomHASH(char* ID, BYTE* FullRom, size_t rom_size) {
uint64_t hash = XXHash64::hash(FullRom, rom_size, 64);
sprintf(ID, "%llu", hash);
}
void RomCRC32(char* ID, BYTE* FullRom, size_t rom_size) {
}