Debian Codegraph @

terps/unp64/scanners/intros.cpp

raw symbols SCIP
1 /* This is a cut-down version of UNP64 with only the bare minimum
2 * needed to decompress a number of Commodore 64 adventure games.
3 * It is distributed under the zlib License by kind permission of
4 * the original authors Magnus Lind and iAN CooG.
5 */
6
7 /*
8 UNP64 - generic Commodore 64 prg unpacker
9 (C) 2008-2022 iAN CooG/HVSC Crew^C64Intros
10 original source and idea: testrun.c, taken from exo20b7
11
12 Follows original disclaimer
13 */
14
15 /*
16 * Copyright (c) 2002 - 2023 Magnus Lind.
17 *
18 *
19 * This software is provided 'as-is', without any express or implied
20 * warranty. In no event will the authors be held liable for any damages
21 * arising from the use of this software.
22 *
23 * Permission is granted to anyone to use this software for any purpose,
24 * including commercial applications, and to alter it and redistribute it
25 * freely, subject to the following restrictions:
26 *
27 * 1. The origin of this software must not be misrepresented; you must not
28 * claim that you wrote the original software. If you use this software
29 * in a product, an acknowledgment in the product documentation would be
30 * appreciated but is not required.
31 * 2. Altered source versions must be plainly marked as such, and must not be
32 * misrepresented as being the original software.
33 * 3. This notice may not be removed or altered from any source distribution.
34 *
35 */
36
37 /*
38 C++ version based on code adapted to ScummVM by Avijeet Maurya
39 */
40
41 #include <cstring>
42
43 #include "exo_util.h"
44 #include "types.h"
45 #include "unp64.h"
46
47 namespace Unp64 {
48
49 void scnIntros(UnpStr *unp) {
50 unsigned char *mem;
51 int q = 0, p;
52 if (unp->_idFlag)
53 return;
54 mem = unp->_mem;
55 /* HIV virus */
56 if (unp->_depAdr == 0) {
57 if (u32eq(mem + 0x80b, 0x10DD002C) &&
58 u32eq(mem + 0x84f, 0x34A9EDDD) &&
59 u32eq(mem + 0x8a1, 0xA94D2D57) &&
60 u32eq(mem + 0x9bc, 0xF004B120)) {
61 unp->_forced = 0x859;
62 unp->_depAdr = 0x100;
63 unp->_retAdr = 0xa7ae;
64 unp->_strMem = 0x801;
65 unp->_endAdr = 0x2d;
66 unp->_idFlag = 1;
67 return;
68 }
69 }
70 /* BHP virus */
71 if (unp->_depAdr == 0) {
72 if (u32eq(mem + 0x821, 0xADA707F0) &&
73 u32eq(mem + 0x825, 0x32A55DA6) &&
74 u32eq(mem + 0x920, 0xD13D20D1) &&
75 u32eq(mem + 0xef5, 0x02CD4C20)) {
76 unp->_forced = 0x831;
77 unp->_depAdr = 0x2ab;
78 unp->_retAdr = 0xa7ae;
79 unp->_rtAFrc = 1;
80 unp->_strMem = 0x801;
81 unp->_endAdr = 0x2d;
82 unp->_idFlag = 1;
83 return;
84 }
85 }
86 /* Bula virus */
87 if (unp->_depAdr == 0) {
88 if (u32eq(mem + 0x810, 0x78084220) &&
89 u32eq(mem + 0x853, 0x4DA908F6) &&
90 u32eq(mem + 0xa00, 0xF00348AD) &&
91 u32eq(mem + 0xac4, 0x04B14C0D)) {
92 unp->_forced = 0x810;
93 unp->_depAdr = 0x340;
94 unp->_retAdr = 0xa7ae;
95 unp->_rtAFrc = 1;
96 unp->_strMem = 0x801;
97 unp->_endAdr = unp->_info->_end - 0x2fa;
98 unp->_idFlag = 1;
99 return;
100 }
101 }
102 /* Coder virus */
103 if (unp->_depAdr == 0) {
104 if (u32eq(mem + 0x81f, 0xD3018D08) &&
105 u32eq(mem + 0x823, 0xA9082D8D) &&
106 u32eq(mem + 0x89F, 0x78802C20) &&
107 u32eq(mem + 0x8AF, 0x6CE3974C)) {
108 unp->_forced = 0x812;
109 unp->_depAdr = 0x2b3;
110 unp->_retAdr = 0xa7ae;
111 unp->_rtAFrc = 1;
112 unp->_strMem = 0x801;
113 unp->_endAdr = 0x2d;
114 unp->_idFlag = 1;
115 return;
116 }
117 }
118 /* Boa virus */
119 if (unp->_depAdr == 0) {
120 if (u32eq(mem + 0x80b, 0x10A900A0) &&
121 u32eq(mem + 0x81b, 0xEDDD2008) &&
122 u32eq(mem + 0x85F, 0x0401004C) &&
123 u32eq(mem + 0x9F6, 0x04EE4C00)) {
124 unp->_forced = 0x80b;
125 unp->_depAdr = 0x100;
126 unp->_retAdr = 0xa7ae;
127 unp->_rtAFrc = 1;
128 unp->_strMem = 0x801;
129 unp->_endAdr = 0x2d;
130 unp->_idFlag = 1;
131 return;
132 }
133 }
134 /* Relax intros */
135 if (unp->_depAdr == 0) {
136 if (u32eq(mem + 0xB50, 0x0C632078) &&
137 u32eq(mem + 0xb54, 0xA90C5A20) &&
138 u32eq(mem + 0xc30, 0x4C0D6420) &&
139 u32eq(mem + 0xc34, 0x00A2EA31)) {
140 unp->_forced = 2070;
141 unp->_depAdr = 0x100;
142 unp->_retAdr = READ_LE_UINT16(&mem[0xbea]);
143 unp->_rtAFrc = 1;
144 unp->_strMem = 0x0801;
145 unp->_endAdr = 0x2d;
146 mem[0xbae] = 0x24; /* lda $c5 is not simulated in UNP64 */
147 unp->_idFlag = 1;
148 return;
149 }
150 }
151 if (unp->_depAdr == 0) {
152 if (u32eq(mem + 0x816, 0x2ABD00A2) &&
153 u32eq(mem + 0x81a, 0xCE009D08) &&
154 u32eq(mem + 0x826, 0xCE004CF1) &&
155 u32eq(mem + 0x9F9, 0x5A5A5A5A)) {
156 unp->_forced = 0x816;
157 unp->_depAdr = 0xcf7e; /* highmem unpacker always need to be managed */
158 unp->_retAdr = READ_LE_UINT16(&mem[0x9a9]);
159 unp->_rtAFrc = 1;
160 unp->_strMem = 0x0801;
161 unp->_endAdr = 0x2d;
162 unp->_idFlag = 1;
163 return;
164 }
165 }
166 /* F4CG */
167 if (unp->_depAdr == 0) {
168 if (u32eq(mem + 0x812, 0x20FDA320) &&
169 u32eq(mem + 0x890, 0x06309DD8) &&
170 u32eq(mem + 0x9BF, 0xA2A9D023) &&
171 u32eq(mem + 0xa29, 0xFFFF2CFF)) {
172 unp->_forced = 0x812;
173 unp->_depAdr = 0x110;
174 p = mem[0x8ee];
175 unp->_retAdr = (mem[0xe44] ^ p) | (mem[0xe45] ^ p) << 8;
176 unp->_strMem = 0x0801;
177 unp->_endAdr = mem[0x8f8] | mem[0x8fc] << 8;
178 mem[0xa2d] = 0x4c; /* indirect jmp $0110 using basic rom */
179 mem[0xa2e] = 0x10;
180 mem[0xa2f] = 0x01;
181 unp->_idFlag = 1;
182 return;
183 }
184 }
185 /* Triad */
186 if (unp->_depAdr == 0) {
187 for (p = 0x80d; p < 0x828; p++) {
188 if (u32eq(mem + p + 0x000, 0xA9D0228D) &&
189 u32eq(mem + p + 0x00c, 0x06979D0A) &&
190 u32eq(mem + p + 0x0a0, 0x03489D03) &&
191 u32eq(mem + p + 0x0a4, 0x9D0371BD)) {
192
193 for (q = 0; q < 5; q += 4) {
194 if (u32eq(mem + p + 0x1ec + q, 0x04409D04) &&
195 u32eq(mem + p + 0x210 + q, 0x607EFF60)) {
196 unp->_depAdr = 0x100;
197 break;
198 }
199 }
200 break;
201 }
202 }
203 if (unp->_depAdr) {
204 unp->_forced = p;
205 unp->_retAdr = READ_LE_UINT16(&mem[p + q + 0x28a]);
206 unp->_endAdr = mem[p + q + 0x19e] | mem[p + q + 0x1a4] << 8;
207 unp->_strMem = 0x801;
208 unp->_idFlag = 1;
209 return;
210 }
211 }
212 if (unp->_depAdr == 0) {
213 if (u32eq(mem + 0x81f, 0xA2D0228D) &&
214 u32eq(mem + 0x823, 0x0A5CBD28) &&
215 u32eq(mem + 0x8d7, 0x8509754C) &&
216 u32eq(mem + 0x975, 0x07A053A2)) {
217 unp->_depAdr = 0x100;
218 unp->_forced = 0x81f;
219 unp->_retAdr = READ_LE_UINT16(&mem[0xac3]);
220 unp->_endAdr = mem[0x9cd] | mem[0x9d3] << 8;
221 unp->_strMem = 0x801;
222 unp->_idFlag = 1;
223 return;
224 }
225 }
226 if (unp->_depAdr == 0) {
227 if (u32eq(mem + 0x821, 0xA9D0228D) &&
228 u32eq(mem + 0x82a, 0x0A3aBD28) &&
229 u32eq(mem + 0x8a7, 0x85093c4C) &&
230 u32eq(mem + 0x93c, 0x17108EC6)) {
231 unp->_depAdr = 0x100;
232 unp->_forced = 0x821;
233 unp->_retAdr = READ_LE_UINT16(&mem[0xa99]);
234 unp->_endAdr = mem[0x9bc] | mem[0x9c2] << 8;
235 unp->_strMem = 0x801;
236 unp->_idFlag = 1;
237 return;
238 }
239 }
240 /* Snacky/G*P */
241 if (unp->_depAdr == 0) {
242 if (u32eq(mem + 0x818, 0x018533A9) &&
243 u32eq(mem + 0x888, 0x08684C03) &&
244 u32eq(mem + 0x898, 0x40A9FAD0) &&
245 u32eq(mem + 0xa2d, 0x0A2E4C58)) {
246 for (q = 0xc3c; q < 0xc4f; q++) {
247 if ((mem[q] == 0xa9) &&
248 (mem[q + 2] == 0x85) &&
249 (mem[q + 3] == 0x01) &&
250 (mem[q + 4] == 0x4c)) {
251 unp->_forced = q;
252 unp->_endAdr = READ_LE_UINT16(&mem[q + 5]);
253 break;
254 }
255 if ((mem[q] == 0x84) &&
256 (mem[q + 1] == 0x01) &&
257 (mem[q + 2] == 0x4c)) {
258 unp->_forced = q;
259 unp->_endAdr = READ_LE_UINT16(&mem[q + 3]);
260 break;
261 }
262 }
263 unp->_depAdr = 0x340;
264 unp->_strMem = 0x801;
265 unp->_idFlag = 1;
266 return;
267 }
268 }
269 /* 711 introdes 3 */
270 if (unp->_depAdr == 0) {
271 if (u32eq(mem + 0x1118, 0x8D01A978) &&
272 u32eq(mem + 0x111c, 0x1A8DDC0D) &&
273 u32eq(mem + 0x1174, 0x11534C2A) &&
274 u32eq(mem + 0x2B30, 0x786001F0)) {
275 unp->_forced = 0x2b33;
276 /* it restores $3fff by saving it to $02 =) */
277 mem[2] = mem[0x3fff];
278 if (u32eq(mem + 0x2B4F, 0xE803409D)) {
279 unp->_depAdr = 0x340;
280 unp->_strMem = mem[0x2b65] | mem[0x2b69] << 8;
281 unp->_endAdr = unp->_info->_end - (mem[0x2b5d] | mem[0x2b61] << 8) + unp->_strMem;
282 p = 0x2b7f;
283 if (u32eq(mem + p - 1, 0x4CA65920)) {
284 mem[p - 1] = 0x2c;
285 p += 3;
286 }
287 unp->_retAdr = READ_LE_UINT16(&mem[p]);
288 }
289 unp->_idFlag = 1;
290 return;
291 }
292 }
293 /* BN 1872 intromaker(?), magic disk and so on */
294 if (unp->_depAdr == 0) {
295 if (u32eq(mem + 0x4800, 0x48F02078) &&
296 u32eq(mem + 0x4E67, 0x48004CFC) &&
297 u32eq(mem + 0x4851, 0x4E00BD78) &&
298 u32eq(mem + 0x4855, 0xE800FA9D)) {
299 unp->_forced = 0x4851;
300 unp->_depAdr = 0xfa;
301 unp->_endAdr = 0x2d;
302 unp->_strMem = 0x800;
303 unp->_idFlag = 1;
304 return;
305 }
306 }
307 /* generic excell/ikari reloc routine */
308 if (unp->_depAdr == 0) {
309 for (p = 0xe00; p < 0x3400; p++) {
310 if (u32eq(mem + p + 0x00, 0x01A90385) &&
311 u32eq(mem + p + 0x04, 0x08A90485) &&
312 u32eq(mem + p + 0x10, 0xE6F9D0C8)) {
313 for (q = p + 0x4e; q < p + 0x70; q++) {
314 if (u32eqmasked(mem + q, 0xffffff00, 0x00206C00)) {
315 q = 1;
316 break;
317 }
318 }
319 if (q != 1)
320 break;
321
322 if (mem[p - 0x100 + 0xec] == 0xa2 &&
323 mem[p - 0x100 + 0xee] == 0xbd &&
324 u32eq(mem + p - 0x100 + 0xe8, 0x018534A9) &&
325 u32eq(mem + p - 0x100 + 0xf1, 0xE804009D) &&
326 u32eq(mem + p - 0x100 + 0xf5, 0x004CF7D0)) {
327 unp->_forced = p - 0x100 + 0xe8;
328 unp->_depAdr = 0x400;
329 break;
330 }
331 if (mem[p - 0x100 + 0xc8] == 0xa2 &&
332 mem[p - 0x100 + 0xca] == 0xbd &&
333 u32eq(mem + (READ_LE_UINT16(&mem[p - 0x100 + 0xcb])) + 0x0f, 0x018534A9) &&
334 u32eq(mem + p - 0x100 + 0xcd, 0xA904009D) &&
335 u32eq(mem + p - 0x100 + 0xe3, 0x040F4CF8)) {
336 unp->_forced = p - 0x100 + 0xc8;
337 unp->_depAdr = 0x40f;
338 break;
339 }
340 }
341 }
342
343 if (unp->_depAdr) {
344 unp->_endAdr = mem[p + 0x22] | mem[p + 0x24] << 8;
345 unp->_strMem = 0x801;
346 for (q = p + 0x29; q < p + 0x39; q++) {
347 if ((mem[q] == 0xa9) || (mem[q] == 0xa2) || (mem[q] == 0xa0) ||
348 (mem[q] == 0x85) || (mem[q] == 0x86)) {
349 q++;
350 continue;
351 }
352 if ((mem[q] == 0x8d) || (mem[q] == 0x8e)) {
353 q += 2;
354 continue;
355 }
356 if ((mem[q] == 0x20) || (mem[q] == 0x4c)) {
357 unp->_retAdr = READ_LE_UINT16(&mem[q + 1]);
358 if ((unp->_retAdr == 0xa659) && (mem[q] == 0x20)) {
359 mem[q] = 0x2c;
360 unp->_retAdr = 0;
361 continue;
362 }
363 break;
364 }
365 }
366 unp->_idFlag = 1;
367 return;
368 }
369 }
370 /* ikari-06 by tridos/ikari */
371 if (unp->_depAdr == 0) {
372 if (u32eq(mem + 0xC00, 0xD0CAA5A2) &&
373 u32eq(mem + 0xC10, 0xE807A00C) &&
374 u32eq(mem + 0xC20, 0x201CFB20) &&
375 u32eq(mem + 0xfe8, 0x60BD80A2) &&
376 u32eq(mem + 0xd60, 0x8534A978)) {
377 unp->_forced = 0xfe8;
378 unp->_depAdr = 0x334;
379 unp->_endAdr = 0x2d;
380 unp->_strMem = 0x801;
381 unp->_retAdr = READ_LE_UINT16(&mem[0xda0]);
382 unp->_idFlag = 1;
383 return;
384 }
385 }
386 /* flt-01 version at $0801 */
387 if (unp->_depAdr == 0) {
388 if (u32eq(mem + 0x998, 0x78FF5B20) &&
389 u32eq(mem + 0x9a8, 0xBE8DFAB1) &&
390 u32eq(mem + 0xd3b, 0xFD152078) &&
391 u32eq(mem + 0xd4b, 0xD0CA033B) &&
392 u32eq(mem + 0xd5b, 0xA90DFD4C)) {
393 unp->_forced = 0xd3b;
394 unp->_depAdr = 0x33c;
395 unp->_endAdr = mem[0xb5c] | mem[0xb62] << 8;
396 unp->_strMem = 0x801;
397 unp->_retAdr = READ_LE_UINT16(&mem[0xb6b]);
398 unp->_idFlag = 1;
399 return;
400 }
401 }
402 /* s451-09 at $0801 */
403 if (unp->_depAdr == 0) {
404 if (u32eq(mem + 0x801, 0xE67800A0) &&
405 u32eq(mem + 0x805, 0xA62DA501) &&
406 u32eq(mem + 0x82a, 0xFD91FBB1) &&
407 u32eq(mem + 0x831, 0xC6E6D00F) &&
408 u32eq(mem + 0x841, 0x61BE0004)) {
409 /* first moves $0839-(eof) to $1000 */
410 p = mem[0x837];
411 if ((p == 0x3e) || (p == 0x48)) {
412 unp->_endAdr = mem[0x80f] | mem[0x811] << 8;
413 memmove(mem + 0x1000, mem + 0x839, (size_t)(unp->_endAdr - 0x1000));
414 if (p == 0x3e) {
415 unp->_strMem = READ_LE_UINT16(&mem[0x113c]);
416 } else /* if(p==0x48) */
417 {
418 unp->_strMem = READ_LE_UINT16(&mem[0x114c]);
419 }
420 unp->_depAdr = unp->_strMem;
421 unp->_retAdr = unp->_strMem;
422 unp->_forced = unp->_strMem;
423 unp->_idFlag = 1;
424 return;
425 }
426 }
427 }
428 /* Super_Titlemaker/DSCompware */
429 if (unp->_depAdr == 0) {
430 if (u32eq(mem + 0x820, 0x8536A978) &&
431 u32eq(mem + 0x830, 0x8570A9F9) &&
432 u32eq(mem + 0x860, 0xB100A0B5) &&
433 u32eq(mem + 0x864, 0xE60285B2)) {
434 unp->_endAdr = mem[0x854] | mem[0x852] << 8;
435 //unp->_endAdC=1;
436 unp->_depAdr = 0x851;
437 unp->_retAdr = READ_LE_UINT16(&mem[0x88a]);
438 unp->_strMem = unp->_retAdr;
439 unp->_forced = 0x820;
440 unp->_idFlag = 1;
441 return;
442 }
443 }
444 }
445
446 } // End of namespace Unp64