All files / src bem-snapshot.js

95.72% Statements 224/234
82.55% Branches 71/86
100% Functions 14/14
97.23% Lines 211/217

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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607              3x     3x                                                 9x 7x 7x                 26x 26x 26x   26x 19x 2x           20x 20x                                               3x     10x                           36x                                                         36x 18x 18x 18x   36x       18x 18x 18x 18x   18x 72x 72x 72x 72x 18x 18x   72x 72x 72x 72x   18x   18x                                 7x     7x 7x   7x 7x 1x     6x           6x 6x 6x   6x 6x 6x   6x 6x 6x 6x 6x   6x 6x 6x 6x 6x 6x 24x 24x 24x 24x   6x 6x   6x                                 113x 113x 113x 113x 113x 113x     113x       96x 96x 96x 96x         3x 3x                                 3x 3x   3x 3x   3x                     17x 12x       12x 1x   11x 2x         9x 9x     9x 1x       8x 8x 52x       8x 8x   8x 3x   5x 5x 3x         5x 5x 5x 2x           3x                                                                                       68x 17x 17x 17x       17x 17x   17x 17x 97x 97x 97x   97x   20x 20x 20x     97x 97x 97x 97x         17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 13x       13x         17x 17x 4x       17x 17x 17x 17x   4x 4x 4x 4x 4x 4x 4x 4x 4x     17x         8x                                               8x 4x 4x 4x 4x       8x                                         8x 4x                         8x 8x 8x 8x 4x 4x 4x   4x 4x 4x 64x     64x 64x 64x 64x 64x           8x 8x 8x 4x 4x 4x 4x                     8x 8x 4x 4x 4x 4x 4x 4x 4x 16x 16x 16x 16x   4x 4x 4x     8x                          
// B-em snapshot format parser (versions 1 and 3).
// v1 (BEMSNAP1): Fixed-size 327,885 byte packed struct. Reference: beebjit state.c
// v3 (BEMSNAP3): Section-based with key+size headers, zlib-compressed memory. Reference: b-em savestate.c
 
import { volumeTable, buildVideoState, buildSnapshot } from "./snapshot-helpers.js";
import { inflate } from "./archive.js";
 
const BemV1Size = 327885;
 
// v1 struct offsets
const V1Off = {
    signature: 0,
    model: 8,
    a: 9,
    x: 10,
    y: 11,
    flags: 12,
    s: 13,
    pc: 14,
    nmi: 16,
    interrupt: 17,
    cycles: 18,
    fe30: 22,
    fe34: 23,
    ram: 24,
    rom: 24 + 65536,
    sysvia: 24 + 65536 + 262144,
};
 
/**
 * Check if an ArrayBuffer looks like a b-em snapshot (any version).
 * @param {ArrayBuffer} buffer
 * @returns {boolean}
 */
export function isBemSnapshot(buffer) {
    if (buffer.byteLength < 8) return false;
    const sig = String.fromCharCode(...new Uint8Array(buffer, 0, 7));
    return sig === "BEMSNAP";
}
 
/**
 * Parse a b-em snapshot (v1 or v3) into a jsbeeb snapshot object.
 * @param {ArrayBuffer} buffer
 * @returns {object} jsbeeb snapshot
 */
export async function parseBemSnapshot(buffer) {
    Iif (buffer.byteLength < 8) throw new Error("File too small to be a b-em snapshot");
    const bytes = new Uint8Array(buffer);
    const sig = String.fromCharCode(...bytes.slice(0, 8));
 
    if (sig === "BEMSNAP1") return parseBemV1(buffer);
    if (sig === "BEMSNAP3") return parseBemV3(buffer);
    throw new Error(`Unsupported b-em snapshot version: "${sig}"`);
}
 
// ── Shared helpers ──────────────────────────────────────────────────
 
function readViaFromBytes(data, offset) {
    const view = new DataView(data.buffer, data.byteOffset + offset);
    return {
        ora: data[offset],
        orb: data[offset + 1],
        ira: data[offset + 2],
        irb: data[offset + 3],
        // +4, +5 are port read values (ignored on load, matching b-em via_loadstate)
        ddra: data[offset + 6],
        ddrb: data[offset + 7],
        sr: data[offset + 8],
        acr: data[offset + 9],
        pcr: data[offset + 10],
        ifr: data[offset + 11],
        ier: data[offset + 12],
        t1l: view.getInt32(13, true),
        t2l: view.getInt32(17, true),
        t1c: view.getInt32(21, true),
        t2c: view.getInt32(25, true),
        t1hit: data[offset + 29],
        t2hit: data[offset + 30],
        ca1: data[offset + 31],
        ca2: data[offset + 32],
    };
}
 
const ViaDataSize = 33; // 13 bytes + 4*4 timer ints + 4 booleans
 
function readCpuFromBytes(data) {
    return {
        a: data[0],
        x: data[1],
        y: data[2],
        flags: data[3],
        s: data[4],
        pc: data[5] | (data[6] << 8),
        nmi: data[7],
        interrupt: data[8],
        cycles: data[9] | (data[10] << 8) | (data[11] << 16) | (data[12] << 24),
    };
}
 
function convertViaState(bemVia, ic32) {
    const result = {
        ora: bemVia.ora,
        orb: bemVia.orb,
        ira: bemVia.ira,
        irb: bemVia.irb,
        ddra: bemVia.ddra,
        ddrb: bemVia.ddrb,
        sr: bemVia.sr,
        acr: bemVia.acr,
        pcr: bemVia.pcr,
        ifr: bemVia.ifr,
        ier: bemVia.ier,
        t1l: bemVia.t1l,
        t2l: bemVia.t2l,
        t1c: bemVia.t1c,
        t2c: bemVia.t2c,
        t1hit: bemVia.acr & 0x40 ? false : !!bemVia.t1hit,
        t2hit: !!bemVia.t2hit,
        portapins: 0xff,
        portbpins: 0xff,
        ca1: !!bemVia.ca1,
        ca2: !!bemVia.ca2,
        cb1: false,
        cb2: false,
        justhit: 0,
        t1_pb7: (bemVia.orb >> 7) & 1,
        lastPolltime: 0,
        taskOffset: 1,
    };
    if (ic32 !== undefined) {
        result.IC32 = ic32;
        result.capsLockLight = !(ic32 & 0x40);
        result.shiftLockLight = !(ic32 & 0x80);
    }
    return result;
}
 
function convertSoundState(snLatch, snCount, snStat, snVol, snNoise, snShift) {
    const registers = new Uint16Array(4);
    const counter = new Float32Array(4);
    const outputBit = [false, false, false, false];
    const volume = new Float32Array(4);
 
    for (let i = 0; i < 4; ++i) {
        const snChannel = 3 - i;
        let period = snLatch[snChannel] >> 6;
        let count = snCount[snChannel] >> 6;
        if (i === 3) {
            period >>= 1;
            count >>= 1;
        }
        registers[i] = period;
        counter[i] = count;
        outputBit[i] = snStat[snChannel] < 16;
        volume[i] = volumeTable[snVol[snChannel] & 0x0f];
    }
    registers[3] = snNoise & 0x07;
 
    return {
        registers,
        counter,
        outputBit,
        volume,
        lfsr: snShift,
        latchedRegister: 0,
        residual: 0,
        sineOn: false,
        sineStep: 0,
        sineTime: 0,
    };
}
 
// ── V1 parser (BEMSNAP1, fixed struct) ──────────────────────────────
 
function parseBemV1(buffer) {
    Iif (buffer.byteLength !== BemV1Size) {
        throw new Error(`Invalid BEM v1 snapshot size: expected ${BemV1Size}, got ${buffer.byteLength}`);
    }
    const bytes = new Uint8Array(buffer);
    const view = new DataView(buffer);
 
    const bemModel = bytes[V1Off.model];
    if (bemModel !== 3 && bemModel !== 4) {
        throw new Error(`Unsupported BEM v1 model: ${bemModel} (only BBC Model B supported)`);
    }
 
    const cpuState = {
        ...readCpuFromBytes(bytes.slice(V1Off.a)),
        fe30: bytes[V1Off.fe30],
        fe34: bytes[V1Off.fe34],
    };
 
    const ram = new Uint8Array(128 * 1024);
    ram.set(bytes.slice(V1Off.ram, V1Off.ram + 65536));
    const roms = bytes.slice(V1Off.rom, V1Off.rom + 262144);
 
    const sysVia = readViaFromBytes(bytes, V1Off.sysvia);
    const sysViaIC32 = bytes[V1Off.sysvia + ViaDataSize];
    const userVia = readViaFromBytes(bytes, V1Off.sysvia + ViaDataSize + 1);
 
    const ulaOff = V1Off.sysvia + ViaDataSize + 1 + ViaDataSize;
    const ulaControl = bytes[ulaOff];
    const ulaPalette = bytes.slice(ulaOff + 1, ulaOff + 17);
    const crtcOff = ulaOff + 17;
    const crtcRegs = bytes.slice(crtcOff, crtcOff + 18);
 
    const soundOff = crtcOff + 18 + 7 + 4 + 1 + 4;
    const snLatch = [],
        snCount = [],
        snStat = [],
        snVol = [];
    for (let i = 0; i < 4; i++) {
        snLatch.push(view.getUint32(soundOff + i * 4, true));
        snCount.push(view.getUint32(soundOff + 16 + i * 4, true));
        snStat.push(view.getUint32(soundOff + 32 + i * 4, true));
        snVol.push(bytes[soundOff + 48 + i]);
    }
    const snNoise = bytes[soundOff + 52];
    const snShift = view.getUint16(soundOff + 53, true);
 
    return buildSnapshot(
        "b-em",
        "B",
        cpuState,
        ram,
        roms,
        convertViaState(sysVia, sysViaIC32),
        convertViaState(userVia),
        buildVideoState(ulaControl, ulaPalette, crtcRegs),
        convertSoundState(snLatch, snCount, snStat, snVol, snNoise, snShift),
    );
}
 
// ── V3 parser (BEMSNAP3, section-based) ─────────────────────────────
 
// Variable-length integer encoding used by b-em v3
function readVar(data, pos) {
    let value = 0;
    let shift = 0;
    while (pos.offset < data.length) {
        const byte = data[pos.offset++];
        value |= (byte & 0x7f) << shift;
        Eif (byte & 0x80) break;
        shift += 7;
    }
    return value;
}
 
function readString(data, pos) {
    const len = readVar(data, pos);
    const str = String.fromCharCode(...data.slice(pos.offset, pos.offset + len));
    pos.offset += len;
    return str;
}
 
// b-em writes the tube ULA as a raw struct dump. Version 2 is all single-byte fields, so it has
// no padding and can be read directly; version 1 used native ints and is not supported here.
const BemTubeUlaVersion = 2;
const BemTubeUlaOffsets = {
    ph1: 0,
    ph2: 24,
    ph3: 25,
    ph4: 27,
    hp1: 29, // phl (byte 28) is a latch jsbeeb does not model
    hp2: 30,
    hp3: 31,
    hp4: 33,
    hstat: 35, // hpl (byte 34) likewise unmodelled
    pstat: 39,
    r1stat: 43,
    ph1head: 45, // ph1tail (byte 44) matters only to b-em, which appends there
    ph1count: 46,
    ph3pos: 47,
    hp3pos: 48,
};
const BemTubeUlaSize = 49;
const BemPh1Size = 24;
// 9 bytes of registers, then RAM, then ROM.
const BemTubeRegisterBytes = 9;
const BemTubeRamSize = 65536;
// b-em's other co-processors are different CPUs, and its "6502 Turbo" has 16MB of RAM.
const BemSupportedTubes = ["6502 Internal", "6502 External"];
 
/**
 * Convert b-em's tube sections into jsbeeb tube state.
 * @param {object} sections - parsed v3 sections
 * @param {string|null} tubeName - co-processor name from the model section
 * @returns {Promise<object|null>} jsbeeb tube state, or null if no usable co-processor
 */
async function parseBemTube(sections, tubeName) {
    // b-em names the model's co-processor even when it failed to start one, so naming a tube but
    // carrying no state for it is valid. Carrying only one of the two sections is not.
    if (!sections["T"] && !sections["P"]) return null;
    Iif (!sections["T"] || !sections["P"]) {
        throw new Error("Truncated b-em snapshot: it holds only one of the two co-processor sections");
    }
    // b-em only writes these sections for a co-processor it started, which it always names.
    if (!tubeName) {
        throw new Error("Corrupt b-em snapshot: it holds co-processor state but names no co-processor");
    }
    if (!BemSupportedTubes.includes(tubeName)) {
        throw new Error(
            `Unsupported b-em co-processor "${tubeName}": jsbeeb only emulates the 64K 65C02 second processor`,
        );
    }
 
    const ulaSection = sections["T"].data;
    Iif (ulaSection[0] !== BemTubeUlaVersion) {
        throw new Error(`Unsupported b-em tube ULA state version ${ulaSection[0]}`);
    }
    if (ulaSection.length < 2 + BemTubeUlaSize) {
        throw new Error(
            `Truncated b-em tube ULA state: expected ${2 + BemTubeUlaSize} bytes, got ${ulaSection.length}`,
        );
    }
    const romPaged = !!ulaSection[1];
    const ula = ulaSection.slice(2, 2 + BemTubeUlaSize);
    const at = (field) => ula[BemTubeUlaOffsets[field]];
 
    // b-em keeps the R1 FIFO as a circular buffer; jsbeeb shifts its contents down on read, so
    // the bytes have to be linearised into the order jsbeeb expects to read them.
    const ph1Count = at("ph1count");
    const ph1Head = at("ph1head");
    // Out-of-range counts would restore a FIFO jsbeeb's own reads could never have produced.
    if (ph1Count > BemPh1Size || ph1Head >= BemPh1Size || at("ph3pos") > 2 || at("hp3pos") > 2) {
        throw new Error("Corrupt b-em tube ULA state: FIFO counts out of range");
    }
    const ph1 = new Uint8Array(BemPh1Size);
    for (let i = 0; i < ph1Count; i++) {
        ph1[i] = ula[BemTubeUlaOffsets.ph1 + ((ph1Head + i) % BemPh1Size)];
    }
 
    // Those names come from a config file, so size is the real check. It cannot be exact: the
    // trailing ROM's size comes from that same config, and b-em records it as zero if unset.
    const parasite = inflate(sections["P"].data);
    const romBytes = parasite.length - BemTubeRegisterBytes - BemTubeRamSize;
    if (romBytes < 0 || romBytes > BemTubeRamSize) {
        throw new Error(
            `Unsupported b-em co-processor: expected ${BemTubeRamSize} bytes of parasite RAM, ` +
                `but the state section holds ${parasite.length - BemTubeRegisterBytes}`,
        );
    }
 
    return {
        a: parasite[2],
        x: parasite[3],
        y: parasite[4],
        p: parasite[5] | 0x30,
        s: parasite[6],
        pc: parasite[7] | (parasite[8] << 8),
        // b-em's oldnmi is the NMI level as of the parasite's last instruction, so it may lag the
        // line its ULA state implies. b-em's skipint has no jsbeeb equivalent and is dropped.
        nmiLevel: !!parasite[1],
        nmiEdge: false,
        takeInt: false,
        cycles: 0,
        romPaged,
        memory: parasite.slice(BemTubeRegisterBytes, BemTubeRegisterBytes + BemTubeRamSize),
        // The parasite ROM is loaded from file at boot, so b-em's copy is not needed.
        ula: {
            internalStatusRegister: at("r1stat"),
            hostStatus: ula.slice(BemTubeUlaOffsets.hstat, BemTubeUlaOffsets.hstat + 4),
            parasiteStatus: ula.slice(BemTubeUlaOffsets.pstat, BemTubeUlaOffsets.pstat + 4),
            parasiteToHostData: [
                ph1,
                Uint8Array.of(at("ph2")),
                ula.slice(BemTubeUlaOffsets.ph3, BemTubeUlaOffsets.ph3 + 2),
                Uint8Array.of(at("ph4")),
            ],
            hostToParasiteData: [
                Uint8Array.of(at("hp1")),
                Uint8Array.of(at("hp2")),
                ula.slice(BemTubeUlaOffsets.hp3, BemTubeUlaOffsets.hp3 + 2),
                Uint8Array.of(at("hp4")),
            ],
            parasiteToHostFifoByteCount1: ph1Count,
            parasiteToHostFifoByteCount3: at("ph3pos"),
            hostToParasiteFifoByteCount3: at("hp3pos"),
        },
    };
}
 
/**
 * Read the co-processor name from b-em's model section, if one was fitted.
 * @returns {string|null}
 */
function readBemTubeName(data, pos) {
    for (let i = 0; i < 4; i++) readString(data, pos); // os, cmos, rom setup, fdc names
    pos.offset += 6; // model flag bytes
    const fitted = data[pos.offset++];
    return fitted ? readString(data, pos) : null;
}
 
async function parseBemV3(buffer) {
    const bytes = new Uint8Array(buffer);
    let offset = 8; // Skip "BEMSNAP3" signature
 
    const sections = {};
    while (offset < bytes.length) {
        let key = bytes[offset];
        let size = bytes[offset + 1] | (bytes[offset + 2] << 8);
        let headerSize = 3;
 
        if (key & 0x80) {
            // Extended size (4 bytes) for compressed sections
            key &= 0x7f;
            size |= (bytes[offset + 3] << 16) | (bytes[offset + 4] << 24);
            headerSize = 5;
        }
 
        const sectionData = bytes.slice(offset + headerSize, offset + headerSize + size);
        const keyChar = String.fromCharCode(key);
        sections[keyChar] = { data: sectionData, compressed: headerSize === 5 };
        offset += headerSize + size;
    }
 
    // Parse model section to determine jsbeeb model name.
    // Use jsbeeb synonyms (from models.js) so findModel() resolves them.
    let modelName = "B";
    let tubeName = null;
    Eif (sections["m"]) {
        const pos = { offset: 0 };
        const data = sections["m"].data;
        readVar(data, pos); // curmodel index (skip)
        const name = readString(data, pos);
        tubeName = readBemTubeName(data, pos);
        if (name.includes("Master")) {
            Iif (name.includes("ADFS")) modelName = "MasterADFS";
            else Iif (name.includes("ANFS")) modelName = "MasterANFS";
            else modelName = "Master";
        } else Iif (name.includes("1770")) {
            if (name.includes("ADFS")) modelName = "B1770A";
            else modelName = "B1770";
        } else {
            modelName = "B";
        }
    }
 
    // Parse CPU
    let cpuState = { a: 0, x: 0, y: 0, flags: 0x30, s: 0xff, pc: 0, nmi: 0, interrupt: 0, cycles: 0 };
    if (sections["6"]) {
        cpuState = readCpuFromBytes(sections["6"].data);
    }
 
    // Parse memory (zlib-compressed in v3)
    const ram = new Uint8Array(128 * 1024);
    let roms = null;
    const memSection = sections["M"];
    if (memSection) {
        // Decompressed layout: 2 bytes (fe30, fe34) + 64KB RAM + 256KB ROM
        const memData = inflate(memSection.data);
        cpuState.fe30 = memData[0];
        cpuState.fe34 = memData[1];
        const ramStart = 2;
        const ramSize = 64 * 1024;
        ram.set(memData.slice(ramStart, ramStart + ramSize));
        const romStart = ramStart + ramSize;
        Eif (memData.length > romStart) {
            roms = memData.slice(romStart, romStart + 262144);
        }
    }
    return finishV3Parse(modelName, cpuState, ram, roms, sections, await parseBemTube(sections, tubeName));
}
 
function finishV3Parse(modelName, cpuState, ram, roms, sections, tube) {
    // Parse system VIA
    let sysvia = convertViaState(
        {
            ora: 0,
            orb: 0,
            ira: 0,
            irb: 0,
            ddra: 0,
            ddrb: 0,
            sr: 0,
            acr: 0,
            pcr: 0,
            ifr: 0,
            ier: 0,
            t1l: 0x1fffe,
            t2l: 0x1fffe,
            t1c: 0x1fffe,
            t2c: 0x1fffe,
            t1hit: 1,
            t2hit: 1,
            ca1: 0,
            ca2: 0,
        },
        0,
    );
    if (sections["S"]) {
        const data = sections["S"].data;
        const via = readViaFromBytes(data, 0);
        const ic32 = data.length > ViaDataSize ? data[ViaDataSize] : 0;
        sysvia = convertViaState(via, ic32);
    }
 
    // Parse user VIA
    let uservia = convertViaState({
        ora: 0,
        orb: 0,
        ira: 0,
        irb: 0,
        ddra: 0,
        ddrb: 0,
        sr: 0,
        acr: 0,
        pcr: 0,
        ifr: 0,
        ier: 0,
        t1l: 0x1fffe,
        t2l: 0x1fffe,
        t1c: 0x1fffe,
        t2c: 0x1fffe,
        t1hit: 1,
        t2hit: 1,
        ca1: 0,
        ca2: 0,
    });
    if (sections["U"]) {
        uservia = convertViaState(readViaFromBytes(sections["U"].data, 0));
    }
 
    // Parse Video ULA
    // v3 section layout (97 bytes):
    //   1: ula_ctrl
    //  16: ula_palbak[16] (raw palette register values)
    //  64: nula_collook[16] (4 bytes each: R, G, B, A in Allegro RGBA format)
    //   1: nula_pal_write_flag
    //   1: nula_pal_first_byte
    //   8: nula_flash[8]
    //   1: nula_palette_mode
    //   ... (more NULA state follows)
    let ulaControl = 0;
    let ulaPalette = new Uint8Array(16);
    let nulaCollook = null;
    if (sections["V"]) {
        const data = sections["V"].data;
        ulaControl = data[0];
        ulaPalette = data.slice(1, 17);
        // Parse NULA collook if section is large enough (v3 has 97 bytes)
        Eif (data.length >= 81) {
            nulaCollook = new Int32Array(16);
            for (let c = 0; c < 16; c++) {
                const off = 17 + c * 4;
                // b-em stores as R, G, B, A (Allegro format)
                // jsbeeb uses ABGR format (Uint32 on little-endian = canvas RGBA)
                const r = data[off];
                const g = data[off + 1];
                const b = data[off + 2];
                const a = data[off + 3];
                nulaCollook[c] = (a << 24) | (b << 16) | (g << 8) | r;
            }
        }
    }
 
    // Parse CRTC (18 regs + optional 7 counter bytes: vc, sc, hc, ma_lo, ma_hi, maback_lo, maback_hi)
    let crtcRegs = new Uint8Array(18);
    let crtcCounters = null;
    if (sections["C"]) {
        const data = sections["C"].data;
        crtcRegs = data.slice(0, 18);
        Eif (data.length >= 25) {
            crtcCounters = {
                vc: data[18],
                sc: data[19],
                hc: data[20],
                ma: data[21] | (data[22] << 8),
                maback: data[23] | (data[24] << 8),
            };
        }
    }
 
    // Parse sound
    let soundChip = convertSoundState([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], 0, 1 << 14);
    if (sections["s"]) {
        const data = sections["s"].data;
        const view = new DataView(data.buffer, data.byteOffset);
        const snLatch = [],
            snCount = [],
            snStat = [],
            snVol = [];
        for (let i = 0; i < 4; i++) {
            snLatch.push(view.getUint32(i * 4, true));
            snCount.push(view.getUint32(16 + i * 4, true));
            snStat.push(view.getUint32(32 + i * 4, true));
            snVol.push(data[48 + i]);
        }
        const snNoise = data[52];
        const snShift = view.getUint16(53, true);
        soundChip = convertSoundState(snLatch, snCount, snStat, snVol, snNoise, snShift);
    }
 
    return buildSnapshot(
        "b-em",
        modelName,
        cpuState,
        ram,
        roms,
        sysvia,
        uservia,
        buildVideoState(ulaControl, ulaPalette, crtcRegs, nulaCollook, crtcCounters),
        soundChip,
        tube,
    );
}