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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { readFile } from "fs-extra"; import { decBin, decHexVal } from "shared/lib/helpers/8bit"; import { ungzip } from "node-gzip"; const MIN_VGM_VERSION = 0x161; type CompileOutputFmt = "c" | "asm"; type CompiledSoundOutput = { output: string; channelMuteMask: number; }; interface VGMOptions { delay: number; noInit?: boolean; noNR1X?: boolean; noNR2X?: boolean; noNR3X?: boolean; noNR4X?: boolean; noNR5X?: boolean; noWave?: boolean; } export const compileVGM = async ( filename: string, fmt: CompileOutputFmt = "c" ): Promise<CompiledSoundOutput> => { const decHex = ((fmt?: CompileOutputFmt) => (v: number) => { const prefix = fmt === "asm" ? "$" : "0x"; return `${prefix}${decHexVal(v)}`; })(fmt); const binPrefix = fmt === "asm" ? "%" : "0b"; const options: VGMOptions = { delay: 0, }; const disabledChannels = new Set<number>(); if (options.noNR1X) { disabledChannels.add(0); } else if (options.noNR2X) { disabledChannels.add(1); } else if (options.noNR3X) { disabledChannels.add(2); } else Iif (options.noNR4X) { disabledChannels.add(3); } let file = await readFile(filename); Iif (filename.toLowerCase().endsWith(".vgz")) { file = await ungzip(file); } let ptr = 0; const read = () => { const data = file[ptr]; ptr++; return data; }; const readSlice = (size: number) => { const data = [...file.slice(ptr, ptr + size)]; ptr += size; return data; }; const seek = (offset: number, whence = 0) => { if (whence === 0) { ptr = offset; } else Iif (whence === 1) { ptr += offset; } }; let channelMuteMask = 0; Iif (unpackString(readSlice(4)) !== "Vgm ") { throw new Error("Invalid file format"); } seek(0x08); const vgmVersion = unpackInt(readSlice(4)); Iif (vgmVersion < MIN_VGM_VERSION) { throw new Error("VGM version too low"); } Iif (vgmVersion === 0) { throw new Error("VGM must contain GB data"); } seek(0x34); const offset = unpackInt(readSlice(4)); seek(offset - 4, 1); let data = read(); let output = ""; let row: Record<number, Record<number, number>> = {}; while (data) { if (data === 0x66) { output += `1,${binPrefix}${decBin(7)}`; break; } else if (data === 0xb3) { let addr = read(); const data = read(); addr += 0x10; if (inRange(addr, 0x10, 0x16)) { setDefault(row, 0, {})[addr - 0x10] = data; } else if (inRange(addr, 0x16, 0x20)) { setDefault(row, 1, {})[addr - 0x15] = data; } else if (inRange(addr, 0x1a, 0x1f)) { setDefault(row, 2, {})[addr - 0x1a] = data; } else if (inRange(addr, 0x20, 0x24)) { setDefault(row, 3, {})[addr - 0x1f] = data; } else if (inRange(addr, 0x24, 0x27)) { setDefault(row, 4, {})[addr - 0x24] = data; } else if (inRange(addr, 0x30, 0x40)) { setDefault(row, 5, {})[addr - 0x30] = data; } else { throw new Error(`Invalid register address: ${addr}`); } } else if ( data === 0x61 || data === 0x62 || (data >= 0x70 && data <= 0x7f) ) { let delay = 0; Iif (data === 0x61) { const n = unpackInt(readSlice(2)); const frames = Math.round((n / 65535) * 90); delay += frames; } Iif (data >= 0x70 && data <= 0x7f) { delay += data - 0x6f; } let result = ""; let count = 0; // NR5x regs: let ch = pop(row, 4); Iif (ch !== undefined && !options.noNR5X) { let val = pop(ch, 3) ?? -1; Iif (val !== -1 && !options.noInit) { count += 1; result += `${binPrefix}00100100,${decHex(val)},`; } let mask = 4; let tmp = ""; for (let i = 0; i < 2; i++) { val = pop(ch, i) ?? -1; Iif (val !== -1) { mask |= 1 << (7 - i); tmp += `${decHex(val)},`; } } Iif (mask !== 4) { count += 1; result += `${binPrefix}${decBin(mask)},${tmp}`; } } // AUD3WAVE[] ch = pop(row, 5); Iif (ch !== undefined && !options.noWave) { const mask = 5; let tmp = ""; for (let i = 0; i < 16; i++) { const val = pop(ch, i) ?? 0; tmp += `${decHex(val)},`; } count += 1; result += `${binPrefix}${decBin(mask)},${tmp}`; } // NR1x, NR2x, NR3x, NR4x regs for (let j = 0; j < 4; j++) { ch = pop(row, j); Iif (ch !== undefined && !disabledChannels.has(j)) { let mask = j; let tmp = ""; for (let i = 0; i < 5; i++) { const val = pop(ch, i) ?? -1; Iif (val !== -1) { mask |= 1 << (7 - i); tmp += `${decHex(val)},`; } } Iif (mask !== j && (mask & 0b00001000) !== 0) { count += 1; result += `${binPrefix}${decBin(mask)},${tmp}`; channelMuteMask |= 1 << j; } } } //optional delay count |= Math.max(0, options.delay + delay - 1) << 4; // output result result = `${decHex(count)},${result}`; Iif (fmt === "c") { result += "\n"; } output += result; row = {}; } else { throw new Error(`Unsupported command ${decHex(data)}`); } data = read(); } return { output, channelMuteMask }; }; const unpackString = (data: number[]): string => { return data.map((c) => String.fromCharCode(c)).join(""); }; const unpackInt = (data: number[]): number => { // Little endian let value = 0; for (let i = data.length - 1; i >= 0; i--) { value <<= 8; value += data[i]; } return value; }; const inRange = (val: number, min: number, max: number) => { return val >= min && val < max; }; const setDefault = <T, K extends keyof T>(obj: T, key: K, value: T[K]) => { Iif (obj[key] !== undefined) { return obj[key]; } obj[key] = value; return value; }; const pop = <T, K extends keyof T>(obj: T, key: K) => { Iif (obj[key] !== undefined) { const value = obj[key]; delete obj[key]; return value; } }; |