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 | import storage from "./storage"; import { createRgbAsm, createRgbAsmModule, createRgbLink, createRgbLinkModule, createRgbFix, createRgbFixModule, } from "./WasmModuleWrapper"; type CompileDoneCallback = ( romFile?: Uint8Array, startAddress?: number, addressToLineMap?: AddressToLineMap ) => void; type CompileLogCallback = (message: string | null) => void; type AddressToLineMap = Record<string, [string, number]>; type CompileErrorType = "error" | "warning"; interface CompileError { type: CompileErrorType; error: string; line: number; message: string; } type EmscriptenModule = { FS: { mkdir: (path: string) => void; writeFile: (path: string, value: string | Uint8Array) => void; }; }; let busy = false; let repeat = false; let startDelayTimer: ReturnType<typeof setTimeout>; let doneCallback: CompileDoneCallback; let logCallback: CompileLogCallback | null; let errorList: Array<CompileError> = []; let romSymbols: Array<string | null> = []; let ramSymbols: Array<string | null> = []; let linkOptions: string[] = []; const lineNumberRegex = /([\w.]+)[\w.:~]*\(([0-9]+)\)/gi; const symRegex = /^\s*\$([0-9a-f]+) = ([\w.]+)/; const sectionTypeBankRegex = /^\s*(\w+) bank #(\d+)/; const sectionRegex = /^\s*SECTION: \$([0-9a-f]+)-\$([0-9a-f]+)/; const slackRegex = /^\s*SLACK: \$([0-9a-f]+) bytes/; /* see: https://gist.github.com/surma/b2705b6cca29357ebea1c9e6e15684cc https://github.com/webpack/webpack/issues/7352 */ const locateFile = (module: unknown) => (path: string) => { Iif (path.endsWith(".wasm")) { return module; } return path; }; function logFunction(str: string) { Iif (logCallback) { logCallback(str); } Iif ( str.startsWith("error: ") || str.startsWith("ERROR: ") || str.startsWith("warning: ") ) { let type: CompileErrorType = "error"; Iif (str.startsWith("warning: ")) type = "warning"; const lineNumberMatch = str.matchAll(lineNumberRegex); for (const m of lineNumberMatch) { const errorLine = parseInt(m[2], 0); errorList.push({ type: type, error: m[1], line: errorLine, message: str, }); } } } function trigger() { Iif (typeof startDelayTimer != "undefined") { clearTimeout(startDelayTimer); } startDelayTimer = setTimeout(startCompile, 500); } async function startCompile() { Iif (logCallback) logCallback(null); errorList = []; romSymbols = []; ramSymbols = []; try { const objFiles = Object.keys(storage.getFiles()) .filter((name) => name.endsWith(".asm")) .map(runRgbAsm); const files = await Promise.all(objFiles); const [romFile, mapFile] = await runRgbLink(files); const fixedRomFile = await runRgbFix(romFile); buildDone(fixedRomFile, mapFile); } catch (e) { console.log(e); buildFailed(); } } async function runRgbAsm(target: string): Promise<Uint8Array> { logFunction(`Running: rgbasm -E ${target} -o output.o -Wall`); const module = await createRgbAsm({ locateFile: locateFile(createRgbAsmModule), arguments: ["-E", target, "-o", "output.o", "-Wall"], preRun: (m: EmscriptenModule) => { const FS = m.FS; FS.mkdir("include"); for (const [key, value] of Object.entries(storage.getFiles())) { FS.writeFile(key, value); } }, print: logFunction, printErr: logFunction, quit: () => { throw new Error("Compilation failed"); }, }); Iif (repeat) { throw new Error(); } const FS = module.FS; return FS.readFile("output.o"); } async function runRgbLink( objFiles: Uint8Array[] ): Promise<[Uint8Array, string]> { const args = ["-o", "output.gb", "--map", "output.map"].concat(linkOptions); objFiles.forEach((_, idx) => { args.push(`${idx}.o`); }); logFunction(`Running: rgblink ${args.join(" ")}`); const module = await createRgbLink({ locateFile: locateFile(createRgbLinkModule), arguments: args, preRun: (m: EmscriptenModule) => { const FS = m.FS; objFiles.forEach((_, idx) => { FS.writeFile(`${idx}.o`, objFiles[idx]); }); }, print: logFunction, printErr: logFunction, }); Iif (repeat) { throw new Error(); } const FS = module.FS; const romFile = FS.readFile("output.gb"); const mapFile = FS.readFile("output.map", { encoding: "utf8" }); return [romFile, mapFile]; } async function runRgbFix(inputRomFile: Uint8Array): Promise<Uint8Array> { logFunction("Running: rgbfix -v output.gb -p 0xff"); const module = await createRgbFix({ locateFile: locateFile(createRgbFixModule), arguments: ["-v", "output.gb", "-p", "0xff"], preRun: (m: EmscriptenModule) => { const FS = m.FS; FS.writeFile("output.gb", inputRomFile); }, print: logFunction, printErr: logFunction, }); const FS = module.FS; return FS.readFile("output.gb"); } function buildFailed() { logFunction("Build failed"); if (repeat) { repeat = false; trigger(); } else { busy = false; doneCallback(); } } function buildDone(romFile: Uint8Array, mapFile: string) { if (repeat) { repeat = false; trigger(); } else { busy = false; let startAddress = 0x100; const addrToLine: Record<string, [string, number]> = {}; let sectionType = ""; let bankNumber = 0; for (const line of mapFile.split("\n")) { let m; if ((m = symRegex.exec(line))) { let addr = parseInt(m[1], 16); let sym = m[2]; if (sym.startsWith("__SEC_")) { sym = sym.substr(6); let file = sym.substr(sym.indexOf("_") + 1); file = file.substr(file.indexOf("_") + 1); const lineNumber = parseInt(sym.split("_")[1], 16); addr = (addr & 0x3fff) | (bankNumber << 14); addrToLine[addr] = [file, lineNumber]; } else if ( sym === "emustart" || sym === "emuStart" || sym === "emu_start" ) { startAddress = addr; } else if (addr < 0x8000) { addr = (addr & 0x3fff) | (bankNumber << 14); romSymbols[addr] = sym; } else { ramSymbols[addr] = sym; } } else if ((m = sectionRegex.exec(line))) { let startAddress = parseInt(m[1], 16); let endAddress = parseInt(m[2], 16) + 1; if (startAddress < 0x8000) { startAddress = (startAddress & 0x3fff) | (bankNumber << 14); endAddress = (endAddress & 0x3fff) | (bankNumber << 14); romSymbols[startAddress] = null; romSymbols[endAddress] = null; } else { ramSymbols[startAddress] = null; ramSymbols[endAddress] = null; } } else if ((m = sectionTypeBankRegex.exec(line))) { sectionType = m[1]; bankNumber = parseInt(m[2], 0); } else Iif ((m = slackRegex.exec(line))) { const space = parseInt(m[1], 16); let total = 0x4000; if (sectionType.startsWith("WRAM")) total = 0x1000; else Iif (sectionType.startsWith("HRAM")) total = 127; logFunction( `Space left: ${sectionType}[${bankNumber}]: ${space} (${( (space / total) * 100 ).toFixed(1)}%)` ); } } logFunction("Build done"); doneCallback(romFile, startAddress, addrToLine); } } const compiler = { compile: ( options: string[], onCompileDone: CompileDoneCallback, onCompileLog?: CompileLogCallback ) => { doneCallback = onCompileDone; linkOptions = options ?? []; logCallback = onCompileLog ?? null; if (busy) { repeat = true; } else { busy = true; trigger(); } }, getErrors: () => errorList, getRomSymbols: () => romSymbols, getRamSymbols: () => ramSymbols, }; export default compiler; |