| 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 | |
| 42 | #include <cstring> |
| 43 | #include <cstdio> |
| 44 | |
| 45 | #include "types.h" |
| 46 | #include "globals.h" |
| 47 | #include "6502_emu.h" |
| 48 | #include "exo_util.h" |
| 49 | #include "unp64.h" |
| 50 | |
| 51 | #include "unp64_interface.h" |
| 52 | |
| 53 | namespace Unp64 { |
| 54 | |
| 55 | void reinitUnp(void) { |
| 56 | _G(_unp)._idFlag = 0; |
| 57 | _G(_unp)._forced = 0; |
| 58 | _G(_unp)._strMem = 0x800; |
| 59 | _G(_unp)._retAdr = 0x800; |
| 60 | _G(_unp)._depAdr = 0; |
| 61 | _G(_unp)._endAdr = 0x10000; |
| 62 | _G(_unp)._rtAFrc = 0; |
| 63 | _G(_unp)._wrMemF = 0; |
| 64 | _G(_unp)._lfMemF = 0; |
| 65 | _G(_unp)._exoFnd = 0; |
| 66 | _G(_unp)._ecaFlg = 0; |
| 67 | _G(_unp)._fEndBf = 0; |
| 68 | _G(_unp)._fEndAf = 0; |
| 69 | _G(_unp)._fStrAf = 0; |
| 70 | _G(_unp)._fStrBf = 0; |
| 71 | _G(_unp)._mon1st = 0; |
| 72 | } |
| 73 | |
| 74 | int isBasicRun1(int pc) { |
| 75 | if (pc == 0xa7ae || pc == 0xa7ea || pc == 0xa7b1 || pc == 0xa474 || pc == 0xa533 || pc == 0xa871 || pc == 0xa888 || pc == 0xa8bc) |
| 76 | return 1; |
| 77 | else |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int isBasicRun2(int pc) { |
| 82 | if (isBasicRun1(pc) || ((pc >= 0xA57C) && (pc <= 0xA659)) || pc == 0xa660 || pc == 0xa68e) |
| 83 | return 1; |
| 84 | else |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int unp64cpp(uint8_t *compressed, size_t length, uint8_t *destinationBuffer, size_t *finalLength, const char *switches) { |
| 89 | |
| 90 | char settings[4][64]; |
| 91 | int numSettings = 0; |
| 92 | |
| 93 | if (switches != NULL) { |
| 94 | Unp64::size_t string_length = std::strlen(switches); |
| 95 | char string[100]; |
| 96 | if (string_length > 0 && string_length < 100) { |
| 97 | snprintf(string, sizeof string, "%s", switches); |
| 98 | char *setting = strtok(string, " "); |
| 99 | while (setting != NULL && numSettings < 4) { |
| 100 | snprintf(settings[numSettings], sizeof settings[numSettings], "%s", setting); |
| 101 | numSettings++; |
| 102 | setting = strtok(NULL, " "); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | CpuCtx r[1]; |
| 108 | LoadInfo info[1]; |
| 109 | char name[260] = {0}, forcedname[260] = {0}; |
| 110 | uint8_t mem[65536] = {0}, oldmem[65536] = {0}; |
| 111 | uint8_t vector[0x20] = {0x31, 0xEA, 0x66, 0xFE, 0x47, 0xFE, 0x4A, 0xF3, |
| 112 | 0x91, 0xF2, 0x0E, 0xF2, 0x50, 0xF2, 0x33, 0xF3, |
| 113 | 0x57, 0xF1, 0xCA, 0xF1, 0xED, 0xF6, 0x3E, 0xF1, |
| 114 | 0x2F, 0xF3, 0x66, 0xFE, 0xA5, 0xF4, 0xED, 0xF5}; |
| 115 | |
| 116 | uint8_t stack[0x100] = {0x33, 0x38, 0x39, 0x31, 0x31, 0x00, 0x30, 0x30, 0x30, 0x30, |
| 117 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 118 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 119 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 120 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 121 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 122 | 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 123 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 124 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 125 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 126 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 127 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 128 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, |
| 129 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 130 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 132 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 134 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 135 | 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 136 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 137 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 138 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7D, |
| 139 | 0xEA, 0x00, 0x00, 0x82, 0x22, 0x0E, 0xBC, 0x81, 0x64, 0xB8, |
| 140 | 0x0C, 0xBD, 0xBA, 0xB7, 0xBC, 0x03, 0x00, 0x46, 0xE1, 0xE9, |
| 141 | 0xA7, 0xA7, 0x79, 0xA6, 0x9C, 0xE3}; |
| 142 | |
| 143 | int iterMax = ITERMAX; |
| 144 | int p; |
| 145 | |
| 146 | memset(&_G(_unp), 0, sizeof(_G(_unp))); |
| 147 | reinitUnp(); |
| 148 | _G(_unp)._fStack = 1; |
| 149 | _G(_unp)._mem = mem; |
| 150 | _G(_unp)._r = r; |
| 151 | _G(_unp)._name = name; |
| 152 | _G(_unp)._info = info; |
| 153 | |
| 154 | p = 0; |
| 155 | |
| 156 | if (numSettings != 0) { |
| 157 | if (settings[0][0] == '-' && _G(_parsePar) && settings[0][1] == 'f') { |
| 158 | strToInt(settings[p] + 2, (int *)&_G(_unp)._filler); |
| 159 | if (_G(_unp)._filler) { |
| 160 | memset(mem + (_G(_unp)._filler >> 16), _G(_unp)._filler & 0xff, 0x10000 - (_G(_unp)._filler >> 16)); |
| 161 | } |
| 162 | p++; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | looprecurse: |
| 167 | info->_basicTxtStart = 0x801; |
| 168 | loadData(compressed, length, mem, info); |
| 169 | /* no start address from load */ |
| 170 | if (info->_run == -1) { |
| 171 | /* look for sys line */ |
| 172 | info->_run = findSys(mem + info->_basicTxtStart, 0x9e); |
| 173 | } |
| 174 | |
| 175 | scanners(&_G(_unp)); |
| 176 | if (_G(_unp)._idFlag == 2) |
| 177 | return 0; |
| 178 | |
| 179 | if ((_G(_unp)._recurs == 0) && (numSettings > 0)) { |
| 180 | while (p < numSettings) { |
| 181 | if (settings[p][0] == '-') { |
| 182 | switch (settings[p][1]) { |
| 183 | case '-': |
| 184 | p = numSettings; |
| 185 | break; |
| 186 | case 'e': |
| 187 | strToInt(settings[p] + 2, &_G(_unp)._forced); |
| 188 | _G(_unp)._forced &= 0xffff; |
| 189 | if (_G(_unp)._forced < 0x1) |
| 190 | _G(_unp)._forced = 0; |
| 191 | break; |
| 192 | case 'a': |
| 193 | _G(_unp)._strMem = 2; |
| 194 | _G(_unp)._endAdr = 0x10001; |
| 195 | _G(_unp)._fEndAf = 0; |
| 196 | _G(_unp)._fStrAf = 0; |
| 197 | _G(_unp)._strAdC = 0; |
| 198 | _G(_unp)._endAdC = 0; |
| 199 | _G(_unp)._monEnd = 0; |
| 200 | _G(_unp)._monStr = 0; |
| 201 | break; |
| 202 | case 'r': |
| 203 | strToInt(settings[p] + 2, &_G(_unp)._retAdr); |
| 204 | _G(_unp)._retAdr &= 0xffff; |
| 205 | break; |
| 206 | case 'R': |
| 207 | strToInt(settings[p] + 2, &_G(_unp)._retAdr); |
| 208 | _G(_unp)._retAdr &= 0xffff; |
| 209 | _G(_unp)._rtAFrc = 1; |
| 210 | break; |
| 211 | case 'd': |
| 212 | strToInt(settings[p] + 2, &_G(_unp)._depAdr); |
| 213 | _G(_unp)._depAdr &= 0xffff; |
| 214 | break; |
| 215 | case 't': |
| 216 | strToInt(settings[p] + 2, &_G(_unp)._endAdr); |
| 217 | _G(_unp)._endAdr &= 0xffff; |
| 218 | if (_G(_unp)._endAdr >= 0x100) |
| 219 | _G(_unp)._endAdr++; |
| 220 | break; |
| 221 | case 'u': |
| 222 | _G(_unp)._wrMemF = 1; |
| 223 | break; |
| 224 | case 'l': |
| 225 | _G(_unp)._lfMemF = info->_end; |
| 226 | break; |
| 227 | case 's': |
| 228 | _G(_unp)._fStack = 0; |
| 229 | break; |
| 230 | case 'x': |
| 231 | break; |
| 232 | case 'B': |
| 233 | //copyRoms[0][1] = 1; |
| 234 | break; |
| 235 | case 'K': |
| 236 | //copyRoms[1][1] = 1; |
| 237 | break; |
| 238 | case 'c': |
| 239 | _G(_unp)._recurs++; |
| 240 | break; |
| 241 | case 'm': // keep undocumented for now |
| 242 | strToInt(settings[p] + 2, &iterMax); |
| 243 | } |
| 244 | } |
| 245 | p++; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (_G(_unp)._idOnly) { |
| 250 | if (_G(_unp)._depAdr == 0) |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | if (_G(_unp)._wrMemF | _G(_unp)._lfMemF) { |
| 255 | memcpy(oldmem, mem, sizeof(oldmem)); |
| 256 | } |
| 257 | |
| 258 | if (_G(_unp)._forced) { |
| 259 | info->_run = _G(_unp)._forced; |
| 260 | } |
| 261 | |
| 262 | if (info->_run == -1) { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | if (_G(_unp)._strMem > _G(_unp)._retAdr) { |
| 267 | _G(_unp)._strMem = _G(_unp)._retAdr; |
| 268 | } |
| 269 | |
| 270 | mem[0] = 0x60; |
| 271 | r->_cycles = 0; |
| 272 | mem[1] = 0x37; |
| 273 | |
| 274 | if (((_G(_unp)._forced >= 0xa000) && (_G(_unp)._forced < 0xc000)) || (_G(_unp)._forced >= 0xd000)) |
| 275 | mem[1] = 0x38; |
| 276 | |
| 277 | /* some packers rely on basic pointers already set */ |
| 278 | mem[0x2b] = info->_basicTxtStart & 0xff; |
| 279 | mem[0x2c] = info->_basicTxtStart >> 8; |
| 280 | |
| 281 | if (info->_basicVarStart == -1) { |
| 282 | mem[0x2d] = info->_end & 0xff; |
| 283 | mem[0x2e] = info->_end >> 8; |
| 284 | } else { |
| 285 | mem[0x2d] = info->_basicVarStart & 0xff; |
| 286 | mem[0x2e] = info->_basicVarStart >> 8; |
| 287 | } |
| 288 | |
| 289 | mem[0x2f] = mem[0x2d]; |
| 290 | mem[0x30] = mem[0x2e]; |
| 291 | mem[0x31] = mem[0x2d]; |
| 292 | mem[0x32] = mem[0x2e]; |
| 293 | mem[0xae] = info->_end & 0xff; |
| 294 | mem[0xaf] = info->_end >> 8; |
| 295 | |
| 296 | /* CCS unpacker requires $39/$3a (current basic line number) set */ |
| 297 | mem[0x39] = mem[0x803]; |
| 298 | mem[0x3a] = mem[0x804]; |
| 299 | mem[0x52] = 0; |
| 300 | mem[0x53] = 3; |
| 301 | |
| 302 | if (_G(_unp)._fStack) { |
| 303 | memcpy(mem + 0x100, stack, |
| 304 | sizeof(stack)); /* stack as found on clean start */ |
| 305 | r->_sp = 0xf6; /* sys from immediate mode leaves $f6 in stackptr */ |
| 306 | } else { |
| 307 | r->_sp = 0xff; |
| 308 | } |
| 309 | |
| 310 | if (info->_start > (long int)(0x314 + sizeof(vector))) { |
| 311 | /* some packers use values in irq pointers to decrypt themselves */ |
| 312 | memcpy(mem + 0x314, vector, sizeof(vector)); |
| 313 | } |
| 314 | |
| 315 | mem[0x200] = 0x8a; |
| 316 | r->_mem = mem; |
| 317 | r->_pc = info->_run; |
| 318 | r->_flags = 0x20; |
| 319 | r->_a = 0; |
| 320 | r->_y = 0; |
| 321 | |
| 322 | if (info->_run > 0x351) /* temporary for XIP */ { |
| 323 | r->_x = 0; |
| 324 | } |
| 325 | |
| 326 | _G(_iter) = 0; |
| 327 | while ((_G(_unp)._depAdr ? r->_pc != _G(_unp)._depAdr : r->_pc >= _G(_unp)._retAdr)) { |
| 328 | if ((((mem[1] & 0x7) >= 6) && (r->_pc >= 0xe000)) || ((r->_pc >= 0xa000) && (r->_pc <= 0xbfff) && ((mem[1] & 0x7) > 6))) { |
| 329 | /* some packer relies on regs set at return from CLRSCR */ |
| 330 | if ((r->_pc == 0xe536) || (r->_pc == 0xe544) || (r->_pc == 0xff5b) || ((r->_pc == 0xffd2) && (r->_a == 0x93))) { |
| 331 | if (r->_pc != 0xffd2) { |
| 332 | r->_x = 0x01; |
| 333 | r->_y = 0x84; |
| 334 | |
| 335 | if (r->_pc == 0xff5b) |
| 336 | r->_a = 0x97; /* actually depends on $d012 */ |
| 337 | else |
| 338 | r->_a = 0xd8; |
| 339 | |
| 340 | r->_flags &= ~(128 | 2); |
| 341 | r->_flags |= (r->_a == 0 ? 2 : 0) | (r->_a & 128); |
| 342 | } |
| 343 | memset(mem + 0x400, 0x20, 1000); |
| 344 | } |
| 345 | /* intros */ |
| 346 | if ((r->_pc == 0xffe4) || (r->_pc == 0xf13e)) { |
| 347 | static int flipspe4 = -1; |
| 348 | static unsigned char fpressedchars[] = {0x20, 0, 0x4e, 0, 3, 0, 0x5f, 0, 0x11, 00, 0x0d, 0, 0x31, 0}; |
| 349 | flipspe4++; |
| 350 | |
| 351 | if (flipspe4 > ARRAYSIZE(fpressedchars)) |
| 352 | flipspe4 = 0; |
| 353 | |
| 354 | r->_a = fpressedchars[flipspe4]; |
| 355 | r->_flags &= ~(128 | 2); |
| 356 | r->_flags |= (r->_a == 0 ? 2 : 0) | (r->_a & 128); |
| 357 | } |
| 358 | |
| 359 | if (r->_pc == 0xfd15) { |
| 360 | r->_a = 0x31; |
| 361 | r->_x = 0x30; |
| 362 | r->_y = 0xff; |
| 363 | } |
| 364 | |
| 365 | if (r->_pc == 0xfda3) { |
| 366 | mem[0x01] = 0xe7; |
| 367 | r->_a = 0xd7; |
| 368 | r->_x = 0xff; |
| 369 | } |
| 370 | |
| 371 | if (r->_pc == 0xffbd) { |
| 372 | mem[0xB7] = r->_a; |
| 373 | mem[0xBB] = r->_x; |
| 374 | mem[0xBC] = r->_y; |
| 375 | } |
| 376 | |
| 377 | if ((r->_pc == 0xffd5) || (r->_pc == 0xf4a2)) { |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | if (isBasicRun1(r->_pc)) { |
| 382 | info->_run = findSys(mem + info->_basicTxtStart, 0x9e); |
| 383 | if (info->_run > 0) { |
| 384 | r->_sp = 0xf6; |
| 385 | r->_pc = info->_run; |
| 386 | } else { |
| 387 | mem[0] = 0x60; |
| 388 | r->_pc = 0; /* force a RTS instead of executing ROM code */ |
| 389 | } |
| 390 | } else { |
| 391 | mem[0] = 0x60; |
| 392 | r->_pc = 0; /* force a RTS instead of executing ROM code */ |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (nextInst(r) == 1) |
| 397 | return 0; |
| 398 | |
| 399 | _G(_iter)++; |
| 400 | if (_G(_iter) == iterMax) { |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | if (_G(_unp)._exoFnd && (_G(_unp)._endAdr == 0x10000) && (r->_pc >= 0x100) && (r->_pc <= 0x200) && (_G(_unp)._strMem != 2)) { |
| 405 | _G(_unp)._endAdr = r->_mem[0xfe] + (r->_mem[0xff] << 8); |
| 406 | if ((_G(_unp)._exoFnd & 0xff) == 0x30) { /* low byte of _endAdr, it's a lda $ff00,y */ |
| 407 | _G(_unp)._endAdr = (_G(_unp)._exoFnd >> 8) + (r->_mem[0xff] << 8); |
| 408 | } else if ((_G(_unp)._exoFnd & 0xff) == 0x32) { /* add 1 */ |
| 409 | _G(_unp)._endAdr = 1 + ((_G(_unp)._exoFnd >> 8) + (r->_mem[0xff] << 8)); |
| 410 | } |
| 411 | |
| 412 | if (_G(_unp)._endAdr == 0) |
| 413 | _G(_unp)._endAdr = 0x10001; |
| 414 | } |
| 415 | |
| 416 | if (_G(_unp)._fEndBf && (_G(_unp)._endAdr == 0x10000) && (r->_pc == _G(_unp)._depAdr)) { |
| 417 | _G(_unp)._endAdr = r->_mem[_G(_unp)._fEndBf] | r->_mem[_G(_unp)._fEndBf + 1] << 8; |
| 418 | _G(_unp)._endAdr++; |
| 419 | |
| 420 | if (_G(_unp)._endAdr == 0) |
| 421 | _G(_unp)._endAdr = 0x10001; |
| 422 | |
| 423 | _G(_unp)._fEndBf = 0; |
| 424 | } |
| 425 | |
| 426 | if (_G(_unp)._fStrBf && (_G(_unp)._strMem != 0x2) && (r->_pc == _G(_unp)._depAdr)) { |
| 427 | _G(_unp)._strMem = r->_mem[_G(_unp)._fStrBf] | r->_mem[_G(_unp)._fStrBf + 1] << 8; |
| 428 | _G(_unp)._fStrBf = 0; |
| 429 | } |
| 430 | |
| 431 | if (_G(_unp)._debugP) { |
| 432 | for (p = 0; p < 0x20; p += 2) { |
| 433 | if (*(unsigned short int *)(mem + 0x314 + p) != *(unsigned short int *)(vector + p)) { |
| 434 | *(unsigned short int *)(vector + p) = *(unsigned short int *)(mem + 0x314 + p); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | _G(_iter) = 0; |
| 441 | while (_G(_unp)._rtAFrc ? r->_pc != _G(_unp)._retAdr : r->_pc < _G(_unp)._retAdr) { |
| 442 | if (_G(_unp)._monEnd && r->_pc == _G(_unp)._depAdr) { |
| 443 | p = r->_mem[_G(_unp)._monEnd >> 16] | r->_mem[_G(_unp)._monEnd & 0xffff] << 8; |
| 444 | if (p > (_G(_unp)._endAdr & 0xffff)) { |
| 445 | _G(_unp)._endAdr = p; |
| 446 | } |
| 447 | } |
| 448 | if (_G(_unp)._monStr && r->_pc == _G(_unp)._depAdr) { |
| 449 | p = r->_mem[_G(_unp)._monStr >> 16] | r->_mem[_G(_unp)._monStr & 0xffff] << 8; |
| 450 | if (p > 0) { |
| 451 | if (_G(_unp)._mon1st == 0) { |
| 452 | _G(_unp)._strMem = p; |
| 453 | } |
| 454 | _G(_unp)._mon1st = (unsigned int)_G(_unp)._strMem; |
| 455 | _G(_unp)._strMem = (p < _G(_unp)._strMem ? p : _G(_unp)._strMem); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (r->_pc >= 0xe000) { |
| 460 | if (((mem[1] & 0x7) >= 6) && ((mem[1] & 0x7) <= 7)) { |
| 461 | mem[0] = 0x60; |
| 462 | r->_pc = 0; |
| 463 | } |
| 464 | } |
| 465 | if (nextInst(r) == 1) |
| 466 | return 0; |
| 467 | |
| 468 | if ((mem[r->_pc] == 0x40) && (_G(_unp)._rtiFrc == 1)) { |
| 469 | _G(_unp)._retAdr = r->_pc; |
| 470 | _G(_unp)._rtAFrc = 1; |
| 471 | if (_G(_unp)._retAdr < _G(_unp)._strMem) |
| 472 | _G(_unp)._strMem = 2; |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | _G(_iter)++; |
| 477 | if (_G(_iter) == iterMax) { |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | if ((r->_pc >= 0xa000) && (r->_pc <= 0xbfff) && ((mem[1] & 0x7) == 7)) { |
| 482 | if (isBasicRun2(r->_pc)) { |
| 483 | r->_pc = 0xa7ae; |
| 484 | break; |
| 485 | } else { |
| 486 | mem[0] = 0x60; |
| 487 | r->_pc = 0; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (r->_pc >= 0xe000) { |
| 492 | if (((mem[1] & 0x7) >= 6) && ((mem[1] & 0x7) <= 7)) { |
| 493 | if (r->_pc == 0xffbd) { |
| 494 | mem[0xB7] = r->_a; |
| 495 | mem[0xBB] = r->_x; |
| 496 | mem[0xBC] = r->_y; |
| 497 | } |
| 498 | /* return into IRQ handler, better stop here */ |
| 499 | if (((r->_pc >= 0xea31) && (r->_pc <= 0xeb76)) || (r->_pc == 0xffd5) || (r->_pc == 0xfce2)) { |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | if (r->_pc == 0xfda3) { |
| 504 | mem[0x01] = 0xe7; |
| 505 | r->_a = 0xd7; |
| 506 | r->_x = 0xff; |
| 507 | } |
| 508 | mem[0] = 0x60; |
| 509 | r->_pc = 0; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (_G(_unp)._fEndAf && _G(_unp)._monEnd) { |
| 515 | _G(_unp)._endAdC = (unsigned int)(mem[_G(_unp)._fEndAf] | mem[_G(_unp)._fEndAf + 1] << 8); |
| 516 | if ((int)_G(_unp)._endAdC > _G(_unp)._endAdr) |
| 517 | _G(_unp)._endAdr = (int)_G(_unp)._endAdC; |
| 518 | |
| 519 | _G(_unp)._endAdC = 0; |
| 520 | _G(_unp)._fEndAf = 0; |
| 521 | } |
| 522 | |
| 523 | if (_G(_unp)._fEndAf && (_G(_unp)._endAdr == 0x10000)) { |
| 524 | _G(_unp)._endAdr = r->_mem[_G(_unp)._fEndAf] | r->_mem[_G(_unp)._fEndAf + 1] << 8; |
| 525 | if (_G(_unp)._endAdr == 0) |
| 526 | _G(_unp)._endAdr = 0x10000; |
| 527 | else |
| 528 | _G(_unp)._endAdr++; |
| 529 | _G(_unp)._fEndAf = 0; |
| 530 | } |
| 531 | |
| 532 | if (_G(_unp)._fStrAf /*&&(_G(_unp)._strMem==0x800)*/) { |
| 533 | _G(_unp)._strMem = r->_mem[_G(_unp)._fStrAf] | r->_mem[_G(_unp)._fStrAf + 1] << 8; |
| 534 | _G(_unp)._strMem++; |
| 535 | _G(_unp)._fStrAf = 0; |
| 536 | } |
| 537 | |
| 538 | if (_G(_unp)._exoFnd && (_G(_unp)._strMem != 2)) { |
| 539 | _G(_unp)._strMem = r->_mem[0xfe] + (r->_mem[0xff] << 8); |
| 540 | |
| 541 | if ((_G(_unp)._exoFnd & 0xff) == 0x30) { |
| 542 | _G(_unp)._strMem += r->_y; |
| 543 | } else if ((_G(_unp)._exoFnd & 0xff) == 0x32) { |
| 544 | _G(_unp)._strMem += r->_y + 1; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | if (r->_pc == 0xfce2) { |
| 549 | if ((*(unsigned int *)(mem + 0x8004) == 0x38cdc2c3) && (mem[0x8008] == 0x30)) { |
| 550 | r->_pc = r->_mem[0x8000] + (r->_mem[0x8001] << 8); |
| 551 | } |
| 552 | } else if (r->_pc == 0xa7ae) { |
| 553 | info->_basicTxtStart = mem[0x2b] | mem[0x2c] << 8; |
| 554 | if (info->_basicTxtStart == 0x801) { |
| 555 | info->_run = findSys(mem + info->_basicTxtStart, 0x9e); |
| 556 | if (info->_run > 0) |
| 557 | r->_pc = info->_run; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (_G(_unp)._wrMemF) { |
| 562 | _G(_unp)._wrMemF = 0; |
| 563 | for (p = 0x800; p < 0x10000; p += 4) { |
| 564 | if (*(unsigned int *)(oldmem + p) == *(unsigned int *)(mem + p)) { |
| 565 | *(unsigned int *)(mem + p) = 0; |
| 566 | _G(_unp)._wrMemF = 1; |
| 567 | } |
| 568 | } |
| 569 | /* clean also the $fd30 table copy in RAM */ |
| 570 | if (memcmp(mem + 0xfd30, vector, sizeof(vector)) == 0) { |
| 571 | memset(mem + 0xfd30, 0, sizeof(vector)); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (_G(_unp)._lfMemF) { |
| 576 | for (p = 0xffff; p > 0x0800; p--) { |
| 577 | if (oldmem[--_G(_unp)._lfMemF] == mem[p]) |
| 578 | mem[p] = 0x0; |
| 579 | else { |
| 580 | if (p >= 0xffff) |
| 581 | _G(_unp)._lfMemF = 0 | _G(_unp)._ecaFlg; |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (*forcedname) { |
| 588 | snprintf(name, sizeof name, "%s", forcedname); |
| 589 | } else { |
| 590 | size_t ln = strlen(name); |
| 591 | if (ln > 248) {/* dirty hack in case name is REALLY long */ |
| 592 | name[248] = 0; |
| 593 | ln = 248; |
| 594 | } |
| 595 | |
| 596 | snprintf(name + ln, sizeof(name) - ln, ".%04x%s", r->_pc, ((_G(_unp)._wrMemF | _G(_unp)._lfMemF) ? ".clean" : "")); |
| 597 | } |
| 598 | |
| 599 | /* endadr is set to a ZP location? then use it as a pointer |
| 600 | todo: use __fEndAf instead, it can be used for any location, not only ZP. */ |
| 601 | if (_G(_unp)._endAdr && (_G(_unp)._endAdr < 0x100)) { |
| 602 | p = (mem[_G(_unp)._endAdr] | mem[_G(_unp)._endAdr + 1] << 8) & 0xffff; |
| 603 | _G(_unp)._endAdr = p; |
| 604 | } |
| 605 | |
| 606 | if (_G(_unp)._ecaFlg && (_G(_unp)._strMem != 2)) /* checkme */ { |
| 607 | if (_G(_unp)._endAdr >= ((_G(_unp)._ecaFlg >> 16) & 0xffff)) { |
| 608 | /* most of the times transfers $2000 byte from $d000-efff to $e000-ffff but there are exceptions */ |
| 609 | if (_G(_unp)._lfMemF) |
| 610 | memset(mem + ((_G(_unp)._ecaFlg >> 16) & 0xffff), 0, 0x1000); |
| 611 | _G(_unp)._endAdr += 0x1000; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (_G(_unp)._endAdr <= 0) |
| 616 | _G(_unp)._endAdr = 0x10000; |
| 617 | |
| 618 | if (_G(_unp)._endAdr > 0x10000) |
| 619 | _G(_unp)._endAdr = 0x10000; |
| 620 | |
| 621 | if (_G(_unp)._endAdr < _G(_unp)._strMem) |
| 622 | _G(_unp)._endAdr = 0x10000; |
| 623 | |
| 624 | if (_G(_unp)._endAdC & 0xffff) { |
| 625 | _G(_unp)._endAdr += (_G(_unp)._endAdC & 0xffff); |
| 626 | _G(_unp)._endAdr &= 0xffff; |
| 627 | } |
| 628 | |
| 629 | if (_G(_unp)._endAdC & EA_USE_A) { |
| 630 | _G(_unp)._endAdr += r->_a; |
| 631 | _G(_unp)._endAdr &= 0xffff; |
| 632 | } |
| 633 | |
| 634 | if (_G(_unp)._endAdC & EA_USE_X) { |
| 635 | _G(_unp)._endAdr += r->_x; |
| 636 | _G(_unp)._endAdr &= 0xffff; |
| 637 | } |
| 638 | |
| 639 | if (_G(_unp)._endAdC & EA_USE_Y) { |
| 640 | _G(_unp)._endAdr += r->_y; |
| 641 | _G(_unp)._endAdr &= 0xffff; |
| 642 | } |
| 643 | |
| 644 | if (_G(_unp)._strAdC & 0xffff) { |
| 645 | _G(_unp)._strMem += (_G(_unp)._strAdC & 0xffff); |
| 646 | _G(_unp)._strMem &= 0xffff; |
| 647 | /* only if ea_addff, no reg involved */ |
| 648 | if (((_G(_unp)._strAdC & 0xffff0000) == EA_ADDFF) && ((_G(_unp)._strMem & 0xff) == 0)) { |
| 649 | _G(_unp)._strMem += 0x100; |
| 650 | _G(_unp)._strMem &= 0xffff; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (_G(_unp)._strAdC & EA_USE_A) { |
| 655 | _G(_unp)._strMem += r->_a; |
| 656 | _G(_unp)._strMem &= 0xffff; |
| 657 | if (_G(_unp)._strAdC & EA_ADDFF) { |
| 658 | if ((_G(_unp)._strMem & 0xff) == 0xff) |
| 659 | _G(_unp)._strMem++; |
| 660 | |
| 661 | if (r->_a == 0) { |
| 662 | _G(_unp)._strMem += 0x100; |
| 663 | _G(_unp)._strMem &= 0xffff; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (_G(_unp)._strAdC & EA_USE_X) { |
| 669 | _G(_unp)._strMem += r->_x; |
| 670 | _G(_unp)._strMem &= 0xffff; |
| 671 | |
| 672 | if (_G(_unp)._strAdC & EA_ADDFF) { |
| 673 | if ((_G(_unp)._strMem & 0xff) == 0xff) |
| 674 | _G(_unp)._strMem++; |
| 675 | |
| 676 | if (r->_x == 0) { |
| 677 | _G(_unp)._strMem += 0x100; |
| 678 | _G(_unp)._strMem &= 0xffff; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if (_G(_unp)._strAdC & EA_USE_Y) { |
| 684 | _G(_unp)._strMem += r->_y; |
| 685 | _G(_unp)._strMem &= 0xffff; |
| 686 | |
| 687 | if (_G(_unp)._strAdC & EA_ADDFF) { |
| 688 | if ((_G(_unp)._strMem & 0xff) == 0xff) |
| 689 | _G(_unp)._strMem++; |
| 690 | |
| 691 | if (r->_y == 0) { |
| 692 | _G(_unp)._strMem += 0x100; |
| 693 | _G(_unp)._strMem &= 0xffff; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (_G(_unp)._endAdr <= 0) |
| 699 | _G(_unp)._endAdr = 0x10000; |
| 700 | |
| 701 | if (_G(_unp)._endAdr > 0x10000) |
| 702 | _G(_unp)._endAdr = 0x10000; |
| 703 | |
| 704 | if (_G(_unp)._endAdr < _G(_unp)._strMem) |
| 705 | _G(_unp)._endAdr = 0x10000; |
| 706 | |
| 707 | mem[_G(_unp)._strMem - 2] = _G(_unp)._strMem & 0xff; |
| 708 | mem[_G(_unp)._strMem - 1] = _G(_unp)._strMem >> 8; |
| 709 | |
| 710 | memcpy(destinationBuffer, mem + (_G(_unp)._strMem - 2), (size_t)(_G(_unp)._endAdr - _G(_unp)._strMem + 2)); |
| 711 | *finalLength = (size_t)(_G(_unp)._endAdr - _G(_unp)._strMem + 2); |
| 712 | |
| 713 | if (_G(_unp)._recurs) { |
| 714 | if (++_G(_unp)._recurs > RECUMAX) |
| 715 | return 1; |
| 716 | reinitUnp(); |
| 717 | goto looprecurse; |
| 718 | } |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | } // End of namespace Unp64 |
| 723 | |
| 724 | |
| 725 | int unp64(uint8_t *compressed, size_t length, uint8_t *destinationBuffer, size_t *finalLength, const char *settings) { |
| 726 | |
| 727 | Unp64::size_t scottlength = (Unp64::size_t)length; |
| 728 | Unp64::size_t scottfinallength; |
| 729 | |
| 730 | using namespace Unp64; |
| 731 | |
| 732 | g_globals = new Globals; |
| 733 | |
| 734 | int result = unp64cpp(compressed, scottlength, destinationBuffer, &scottfinallength, settings); |
| 735 | |
| 736 | *finalLength = scottfinallength; |
| 737 | |
| 738 | delete g_globals; |
| 739 | g_globals = nullptr; |
| 740 | |
| 741 | return result; |
| 742 | } |