| 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 "types.h" |
| 42 | #include "unp64.h" |
| 43 | #include "exo_util.h" |
| 44 | |
| 45 | namespace Unp64 { |
| 46 | |
| 47 | void scnECA(UnpStr *unp) { |
| 48 | uint8_t *mem; |
| 49 | int q, p; |
| 50 | if (unp->_idFlag) |
| 51 | return; |
| 52 | mem = unp->_mem; |
| 53 | if (unp->_depAdr == 0) { |
| 54 | // for(p=0x810;p<0x830;p+=0x4) |
| 55 | for (p = 0x80d; p < 0x830; p += 0x1) { |
| 56 | if (u32eq(mem + p + 0x08, (unsigned int)(0x2D9D0032 + p)) && |
| 57 | u32eq(mem + p + 0x3a, 0x2a2a2a2a) && |
| 58 | u32eq(mem + p + 0x0c, 0xF710CA00)) { |
| 59 | if (((*(unsigned int *)(mem + p + 0x00) & 0xf4fff000) == 0x8434A000) && |
| 60 | u32eq(mem + p + 0x04, 0xBD05A201)) { |
| 61 | unp->_forced = p + 1; |
| 62 | } else if (((*(unsigned int *)(mem + p + 0x00) & 0xffffff00) == 0x04A27800) && |
| 63 | u32eq(mem + p + 0x04, 0xBDE80186)) { |
| 64 | unp->_forced = p + 1; |
| 65 | } else if (((*(unsigned int *)(mem + p - 0x03) & 0xffffff00) == 0x04A27800) && |
| 66 | u32eq(mem + p + 0x04, 0xBDE80186)) { |
| 67 | unp->_forced = p - 2; |
| 68 | } else if (u32eq(mem + p - 0x03, 0x8D00a978)) { |
| 69 | unp->_forced = p - 2; |
| 70 | } |
| 71 | } |
| 72 | if (!unp->_forced) { |
| 73 | if (u32eq(mem + p + 0x3a, 0x2a2a2a2a) && |
| 74 | u32eq(mem + p + 0x02, 0x8534A978) && |
| 75 | mem[p - 3] == 0xa0) { |
| 76 | unp->_forced = p - 3; |
| 77 | if (mem[p + 0x0d6] == 0x20 && mem[p + 0x0d7] == 0xe0 && |
| 78 | mem[p + 0x0d8] == 0x03 && mem[p + 0x1da] == 0x5b && |
| 79 | mem[p + 0x1e7] == 0x59) { |
| 80 | /* antiprotection :D */ |
| 81 | mem[p + 0x0d6] = 0x4c; |
| 82 | mem[p + 0x0d7] = 0xae; |
| 83 | mem[p + 0x0d8] = 0xa7; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if (!unp->_forced) { /* FDT */ |
| 88 | if (u32eq(mem + p + 0x3a, 0x2a2a2a2a) && |
| 89 | u32eq(mem + p + 0x03, 0x8604A278) && |
| 90 | u32eq(mem + p + 0x0a, 0x2D950842)) { |
| 91 | unp->_forced = p + 3; |
| 92 | } |
| 93 | } |
| 94 | if (!unp->_forced) { |
| 95 | /* decibel hacks */ |
| 96 | if (u32eq(mem + p + 0x3a, 0x2a2a2a2a) && |
| 97 | u32eq(mem + p + 0x00, 0x9D085EBD) && |
| 98 | u32eq(mem + p - 0x06, 0x018534A9)) { |
| 99 | unp->_forced = p - 0x6; |
| 100 | } |
| 101 | } |
| 102 | if (unp->_forced) { |
| 103 | for (q = 0xd6; q < 0xde; q++) { |
| 104 | if (mem[p + q] == 0x20) { |
| 105 | if (u16eq(mem + p + q + 1, 0xa659) || |
| 106 | u16eq(mem + p + q + 1, 0xff81) || |
| 107 | u16eq(mem + p + q + 1, 0xe3bf) || |
| 108 | u16eq(mem + p + q + 1, 0xe5a0) || |
| 109 | u16eq(mem + p + q + 1, 0xe518)) { |
| 110 | mem[p + q] = 0x2c; |
| 111 | q += 2; |
| 112 | continue; |
| 113 | } else { |
| 114 | unp->_retAdr = READ_LE_UINT16(&mem[p + q + 1]); // mem[p + q + 1] | mem[p + q + 2] << 8; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | if (mem[p + q] == 0x4c) { |
| 119 | unp->_retAdr = READ_LE_UINT16(&mem[p + q + 1]); // mem[p + q + 1] | mem[p + q + 2] << 8; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | unp->_depAdr = READ_LE_UINT16(&mem[p + 0x30]); // mem[p + 0x30] | mem[p + 0x31] << 8; |
| 124 | // some use $2d, some $ae |
| 125 | for (q = 0xed; q < 0x108; q++) { |
| 126 | if (u32eq(mem + p + q, 0xA518F7D0)) { |
| 127 | unp->_endAdr = mem[p + q + 4]; |
| 128 | // if(unp->_DebugP) |
| 129 | // printf("EndAdr from $%02x\n",unp->_endAdr); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | /* |
| 134 | if anything it's unpacked to $d000-efff, it will be copied |
| 135 | to $e000-ffff as last action in unpacker before starting. |
| 136 | 0196 20 DA 01 JSR $01DA ; some have this jsr nopped, reloc doesn't |
| 137 | happen 0199 A9 37 LDA #$37 019b 85 01 STA $01 019d 58 CLI |
| 138 | 019e 20 00 0D JSR $0D00 ; retaddr can be either here or following |
| 139 | 01a1 4C AE A7 JMP $A7AE |
| 140 | 01da B9 00 EF LDA $EF00,Y |
| 141 | 01dd 99 00 FF STA $FF00,Y |
| 142 | 01e0 C8 INY |
| 143 | 01e1 D0 F7 BNE $01DA |
| 144 | 01e3 CE DC 01 DEC $01DC |
| 145 | 01e6 CE DF 01 DEC $01DF |
| 146 | 01e9 AD DF 01 LDA $01DF |
| 147 | 01ec C9 DF CMP #$DF ;<< not fixed, found as lower as $44 for |
| 148 | example 01ee D0 EA BNE $01DA 01f0 60 RTS Because of this, |
| 149 | $d000-dfff will be a copy of $e000-efff. So if $2d points to >= $d000, |
| 150 | SOMETIMES it's better save upto $ffff or: mem[$2d]|(mem[$2e]+$10)<<8 |
| 151 | Still it's not a rule and I don't know exactly when. |
| 152 | 17/06/09: Implemented but still experimental, so better check |
| 153 | extensively. use -v to know when it does the adjustments. 28/10/09: |
| 154 | whoops, was clearing ONLY $d000-dfff =) |
| 155 | */ |
| 156 | unp->_strMem = READ_LE_UINT16(&mem[p + 0x32]); // mem[p + 0x32] | mem[p + 0x33] << 8; |
| 157 | for (q = 0xcd; q < 0xd0; q++) { |
| 158 | if (u32eqmasked(mem + p + q, 0xffff00ff, 0xa9010020)) { |
| 159 | unp->_ecaFlg = READ_LE_UINT16(&mem[p + q + 1]); // mem[p + q + 1] | mem[p + q + 2] << 8; |
| 160 | for (q = 0x110; q < 0x11f; q++) { |
| 161 | if (u32eq(mem + p + q, 0x99EF00B9) && |
| 162 | mem[p + q + 0x12] == 0xc9) { |
| 163 | unp->_ecaFlg |= (mem[p + q + 0x13] - 0xf) << 24; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | /* radwar hack has a BRK here, fffe/f used as IRQ/BRK vector */ |
| 171 | if (mem[0x8e1] == 0) { |
| 172 | mem[0x8e1] = 0x6c; |
| 173 | mem[0x8e2] = 0xfe; |
| 174 | mem[0x8e3] = 0xff; |
| 175 | } |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (unp->_depAdr) { |
| 180 | unp->_idFlag = 1; |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | /* old packer, many old 1985 warez used this */ |
| 185 | if (unp->_depAdr == 0) { |
| 186 | if (u32eq(mem + 0x81b, 0x018534A9) && |
| 187 | u32eq(mem + 0x822, 0xAFC600A0) && |
| 188 | u32eq(mem + 0x826, 0xB1082DCE) && |
| 189 | u32eq(mem + 0x85b, 0x2A2A2A2A)) { |
| 190 | unp->_forced = 0x81b; |
| 191 | unp->_depAdr = 0x100; |
| 192 | unp->_strMem = READ_LE_UINT16(&mem[0x853]); // mem[0x853] | mem[0x854] << 8; |
| 193 | unp->_endAdr = mem[0x895]; |
| 194 | unp->_retAdr = READ_LE_UINT16(&mem[0x885]); // mem[0x885] | mem[0x886] << 8; |
| 195 | unp->_idFlag = 1; |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } // End of namespace Unp64 |