Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | 1x 1x 1x 1x 19x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 10x 2x 2x 1x 1x 5x 5x 5x 52x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 1x 1x 2x 1x 7x 7x 1x 4x 4x 4x 2x 2x 1x 8x 8x 8x 152x 40x 40x 2x 40x 20x 152x 8x 1x 4x 4x 20x 4x 32x 12x 20x 16x 4x 20x 4x 4x 66x 4x 4x 4x 20x 8x 8x 2x 4x 2x 8x 4x 70x 70x 70x 70x 4x 20x 12x 12x 12x 209x 209x 192x 17x 17x 17x 6x 6x 6x 12x 6x 6x 6x 6x 6x 8x 4x 1x 1x 1x 2x 5x 4x 1x 1x 1x 1x | import fs from "fs"; import Path from "path"; interface PackArgs { bankOffset?: number; // Sets the first bank to use (default 1) outputPath?: string; filter?: number; // Only repack files from specified bank (default repack all banks) extension?: string; // Replace the file extension for output files mbc1?: boolean; // Use MBC1 hardware (skip banks 0x20, 0x40 and 0x60) additional?: number; // Reserve N additional banks at end of cart for batteryless saving (default 0) verbose?: boolean; reserve?: Record<number, number>; } interface PackResult { cartSize: number; maxBank: number; } export interface ObjectBankData { size: number; bank: number; } export interface ObjectData { banks: ObjectBankData[]; filename: string; contents: string; } export interface BankReplacement { from: number; to: number; } export interface Bank { objects: [number, ObjectBankData][]; } export interface ObjectPatch { filename: string; contents: string; replacements: BankReplacement[]; } const BANK_SIZE = 16384; const toHex = (value: number, length: number): string => { return value.toString(16).toUpperCase().padStart(length, "0"); }; export const parseSize = (line: string): ObjectBankData => { const split = line.split(" "); const bankSplit = split[1].split("_"); const size = parseInt(split[3], 16); const bank = parseInt(bankSplit[2]); return { size, bank }; }; export const parseSizes = (contents: string): ObjectBankData[] => { const banks: ObjectBankData[] = []; const lines = contents.split("\n"); for (const line of lines) { if (line.includes("A _CODE_")) { const parsedSize = parseSize(line); banks.push(parsedSize); } } return banks; }; export const replaceBank = ( objectString: string, originalBank: number, bankNo: number ): string => { let newString = objectString; const lines = objectString.split("\n"); // Find banked functions for (const line of lines) { if (line.startsWith("S b_")) { const split = line.substring(4).split(" "); const fnName = split[0]; const fnDef = `S _${fnName}`; // If symbol has pair if (newString.includes(fnDef)) { const findBankedFnDef = `b_${fnName} Def[0]*${toHex(originalBank, 6)}`; const replaceBankedFnDef = `b_${fnName} Def${toHex(bankNo, 6)}`; newString = newString.replace( new RegExp(findBankedFnDef, "g"), replaceBankedFnDef ); } } } const findCode = `CODE_${originalBank}`; const replaceCode = `CODE_${bankNo}`; const replacedString = newString.replace( new RegExp(findCode, "g"), replaceCode ); const re = new RegExp(`__bank_([^ ]*) Def[0]*${toHex(originalBank, 6)}`, "g"); const result = replacedString.replace(re, (_, capture) => { return `__bank_${capture} Def${toHex(bankNo, 6)}`; }); return result; }; export const replaceAllBanks = ( objectString: string, replacements: BankReplacement[] ): string => { return replacements.reduce((memo, replacement) => { return replaceBank(memo, replacement.from, replacement.to); }, objectString); }; export const toCartSize = (maxBank: number): number => { const power = Math.ceil(Math.log(maxBank + 1) / Math.log(2)); return Math.pow(2, power); }; export const toOutputFilename = ( originalFilename: string, outputPath: string, ext: string ): string => { const fileStem = Path.basename(originalFilename).replace(/\.[^.]*/, ""); const newFilename = `${fileStem}.${ext}`; if (outputPath.length > 0) { // Store output in dir specified by output_path return Path.join(outputPath, newFilename); } else { return Path.join(Path.dirname(originalFilename), newFilename); } }; const getBankReplacements = ( index: number, packed: Bank[], mbc1: boolean ): BankReplacement[] => { const replacements: BankReplacement[] = []; // Write packed files back to disk let bankNo = 1; for (const bin of packed) { for (const object of bin.objects) { if (mbc1) { if (bankNo === 0x20 || bankNo === 0x40 || bankNo === 0x60) { bankNo += 1; } } if (object[0] === index) { replacements.push({ from: object[1].bank, to: bankNo, }); } } bankNo += 1; } return replacements; }; export const packObjectData = ( objects: ObjectData[], filter: number, bankOffset: number, mbc1: boolean, reserve: Record<number, number> ): ObjectPatch[] => { const banks: Bank[] = []; const areas: [number, ObjectBankData][] = objects .map((x, i) => x.banks.map((y) => [i, y] as [number, ObjectBankData])) .flat(); // Sort objects by descending size areas.sort((a, b) => { if (b[1].size > a[1].size) { return 1; } else if (b[1].size < a[1].size) { return -1; } return 0; }); const maxSize = Math.max(...areas.map((a) => a[1].size)); Iif (BANK_SIZE < maxSize) { const oversizedObjects = objects .filter((object) => { return object.banks.some((bank) => bank.size > BANK_SIZE); }) .map((object) => ` ${Path.basename(object.filename)}`) .join("\n"); throw new Error( `Object files too large to fit in bank.\n${oversizedObjects}` ); } // Add the extra banks first const arr = Array(bankOffset) .fill(null) .map(() => ({ objects: [] })); banks.push(...arr); // Pack fixed areas if (filter !== 0) { for (const area of areas) { if (area[1].bank !== filter) { const sizeDiff: number = area[1].bank - banks.length; if (sizeDiff > 0) { // Add the extra banks first const arr = Array(sizeDiff) .fill(null) .map(() => ({ objects: [] })); banks.push(...arr); } banks[area[1].bank - 1].objects.push([...area]); } } } // Check fixed areas are within max size // for (const bank of banks) { for (let bankIndex = 0; bankIndex < banks.length; bankIndex++) { const bank = banks[bankIndex]; const size = bank.objects.reduce((a, b) => a + b[1].size, 0); const reserved = reserve[bankIndex + 1] ?? 0; Iif (size + reserved > BANK_SIZE) { throw new Error( `Bank overflow in ${ bankIndex + 1 }. Size was ${size} bytes where max allowed is ${BANK_SIZE} bytes` ); } } // Pack unfixed areas for (const area of areas) { if (filter === 0 || area[1].bank === filter) { let stored = false; // Find first fit in existing banks let bankNo = 0; for (const bank of banks) { bankNo += 1; // Skip until at bank_offset if (bankNo < bankOffset) { continue; } // Calculate current size of bank const res = bank.objects.reduce((a, b) => a + b[1].size, 0); const reserved = reserve[bankNo] ?? 0; // If can fit store it here if (res + area[1].size + reserved <= BANK_SIZE) { bank.objects.push([...area]); stored = true; break; } } // No room in existing banks, create a new bank if (!stored) { const nextReserved = reserve[bankNo + 1] ?? 0; Iif (area[1].size + nextReserved > BANK_SIZE) { throw new Error(`Oversized ${area[1].size}`); } const newBank: Bank = { objects: [] }; newBank.objects.push([...area]); banks.push(newBank); } } } // Convert packed data into object patch const patch = objects.map((x, i) => ({ filename: x.filename, contents: x.contents, replacements: getBankReplacements(i, banks, mbc1), })); return patch; }; export const getPatchMaxBank = (packed: ObjectPatch[]): number => { let max = 0; for (const patch of packed) { for (const replacement of patch.replacements) { if (replacement.to > max) { max = replacement.to; } } } return max; }; /// Read an object file into a struct containing the information required /// to pack the data into banks export const toObjectData = (filename: string): Promise<ObjectData> => { return new Promise((resolve, reject) => { fs.readFile(filename, "utf8", (err, contents) => { Iif (err) { return reject(err); } const banks = parseSizes(contents); return resolve({ filename: filename, contents: contents, banks, }); }); }); }; export const writeAsync = ( filename: string, contents: string ): Promise<void> => { return new Promise((resolve, reject) => { fs.writeFile(filename, contents, (err) => { Iif (err) { return reject(err); } return resolve(); }); }); }; export const gbspack = async ( inputFiles: string[], args: PackArgs ): Promise<PackResult> => { const bankOffset = args.bankOffset ?? 0; const outputPath = args.outputPath ?? ""; const filter = args.filter ?? 0; const additional = args.additional ?? 0; const ext = args.extension ?? "o"; const mbc1 = args.mbc1 ?? false; const verbose = args.verbose ?? false; const reserve = args.reserve ?? {}; Iif (verbose) { console.log(`Starting at bank=${bankOffset}`); console.log(`Processing ${inputFiles.length} files`); console.log(`Using extension .${ext}`); Iif (outputPath.length > 0) { console.log(`Output path=${outputPath}`); } Iif (mbc1) { console.log("Using MBC1 hardware"); } } // Convert input files to ObjectData[] const objects: ObjectData[] = []; for (const filename of inputFiles) { Iif (verbose) { console.log(`Processing file: ${filename}`); } const object = await toObjectData(filename); objects.push(object); } // Pack object data into banks const packed = packObjectData(objects, filter, bankOffset, mbc1, reserve); const maxBankNo = getPatchMaxBank(packed) + additional; for (const patch of packed) { const outputFilename = toOutputFilename(patch.filename, outputPath, ext); Iif (verbose) { console.log(`Writing file ${outputFilename}`); } const newContents = replaceAllBanks(patch.contents, patch.replacements); Iif (verbose) { console.log(`Write ${outputFilename}`); } await writeAsync(outputFilename, newContents); } Iif (verbose) { console.log("Done"); } return { cartSize: toCartSize(maxBankNo), maxBank: maxBankNo, }; }; |