All files / src/web fdc-panel.js

94.58% Statements 262/277
77.16% Branches 98/127
94.23% Functions 98/104
96.56% Lines 225/233

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 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675                                          2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       1923x 255x         572x       178x     338x       16x 14x 2x   14x   6x 3x   2x                 14x             2x         2x 20x 2x     15x 14x 13x 13x   2x   2x 10x       75x   75x 75x       2x                 15x 15x   10x                   15x   15x                         15x 15x                         15x 15x                         15x   15x                       4x       30x 30x                   22x 11x 11x     8x 4x   2x     13x   6x 6x   6x 6x 6x           3x 3x 3x               2x   2x           2x       6x 4x         16x 4x 4x 4x 1x       4x 4x 4x 1x 4x     2x                   2x                       16x 16x   2x   2x 6x       48x 48x     2x         16x 16x 16x 16x                   16x 16x           16x 16x                         16x 16x                         2x                         3x   2x 5x     2x 2x 2x 2x 1x 1x 1x   1x 1x 1x 1x           26x   2x 2x               1x 4x 1x   1x 1x           3x 3x 3x           1x         19x           2x             31x 31x                             42x 42x 6x 6x       2x       2x 1x 210x                                 21x 21x 21x                 21x 21x                                               21x 21x 21x 21x 21x             21x   21x 21x 21x 21x           22x   21x 42x   21x 21x       81x 26x         77x 22x         78x 23x     21x 63x 11x 11x 11x     21x 21x 21x 2x 9x   21x 1x 1x 1x   1x   21x   21x 21x 21x 21x 21x 21x       23x 4x 4x 40x 10x         32x 96x 96x         21x 20x 60x       33x 11x       48x         39x 39x 39x 39x 39x 16x 16x 16x 16x     39x 39x 39x 39x       5x 1x           31x 31x 31x             62x   31x 31x 31x        
import { hexbyte } from "../hex.js";
import {
    Command as IntelCommand,
    DriveOut,
    Result,
    State as IntelState,
    StatusFlag,
    TimerState as IntelTimerState,
} from "../intel-fdc.js";
import {
    Command as WdCommand,
    CommandBits as WdCommandBits,
    Control,
    State as WdState,
    Status,
    TimerState as WdTimerState,
} from "../wd-fdc.js";
import { decodedPanel, DecodedRegisters, fixed, lit, register, spelt } from "./decoded-registers.js";
import { Centred, StartEdge } from "./panel-layout.js";
import { readStored, writeStored } from "./stored-json.js";
 
const MinPanelSize = { w: 240, h: 160 };
const MaxLogLines = 10000;
const LogStorageKey = "fdcLog";
const ResultCodeMask = 0x1e;
const SectorCountMask = 0x1f;
const SectorSizeShift = 5;
const SmallestSectorBytes = 128;
const IntelParamCount = 5;
const WdCommandMask = 0xf0;
const Acronyms = new Set(["id", "crc", "dma", "fm", "mfm"]);
 
/** An enum's name for a value, as words: `syncingForIdWait` as "syncing for ID wait". */
function spellName(values, value) {
    const name = Object.keys(values).find((key) => values[key] === value) ?? `&${hexbyte(value)}`;
    return name
        .replaceAll("_", " ")
        .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
        .toLowerCase()
        .split(" ")
        .map((word) => (Acronyms.has(word) ? word.toUpperCase() : word))
        .join(" ");
}
 
const spellingsOf = (values) => Object.values(values).map((value) => spellName(values, value));
 
/** Lights for a register's named bits, in the order given. */
const bitLights = (value, bits) => bits.map(([mask, text, title]) => lit(text, title, value & mask));
 
// ---- The 8271 ----
 
const track = { title: "the track", describe: (value) => `track ${value}` };
const sector = { title: "the first sector", describe: (value) => `sector ${value}` };
const sizeAndCount = {
    title: "bits 5-7 the sector size, 128 bytes doubled that many times; bits 0-4 how many sectors",
    describe: (value) => `${value & SectorCountMask} × ${SmallestSectorBytes << (value >>> SectorSizeShift)} bytes`,
};
const gap = (n) => ({ title: `the length of gap ${n}`, describe: (value) => `gap ${n}: ${value}` });
const specialRegister = { title: "the special register", describe: (value) => `register &${hexbyte(value)}` };
 
const IntelParams = new Map([
    ...[
        IntelCommand.scanData,
        IntelCommand.scanDataAndDeleted,
        IntelCommand.writeData,
        IntelCommand.writeDeletedData,
        IntelCommand.readData,
        IntelCommand.readDataAndDeleted,
        IntelCommand.verify,
    ].map((command) => [command, [track, sector, sizeAndCount]]),
    [IntelCommand.readId, [track, null, { title: "how many IDs to read", describe: (value) => `${value} IDs` }]],
    [IntelCommand.format, [track, gap(3), sizeAndCount, gap(5), gap(1)]],
    [IntelCommand.seek, [track]],
    [IntelCommand.specify, [{ ...specialRegister, title: "the first register the next three parameters set" }]],
    [
        IntelCommand.writeSpecialRegister,
        [specialRegister, { title: "the value written", describe: (value) => `value &${hexbyte(value)}` }],
    ],
    [IntelCommand.readSpecialRegister, [specialRegister]],
]);
 
const IntelCommandNames = spellingsOf(IntelCommand);
const ResultCodes = Object.fromEntries(Object.entries(Result).filter(([, code]) => code & ResultCodeMask || !code));
const ResultNames = spellingsOf(ResultCodes);
 
function intelPhase({ state, timerState, awaitingParams }) {
    if (awaitingParams) return "waiting for parameters";
    if (timerState === IntelTimerState.seekStep) return "stepping";
    Iif (timerState === IntelTimerState.postSeek) return "settling";
    return spellName(IntelState, state);
}
const IntelPhases = ["waiting for parameters", ...spellingsOf(IntelState)];
 
const paramRow = (index) =>
    register(
        `P${index + 1}`,
        `Parameter ${index + 1}, as the 8271 holds it: some count down as the command runs`,
        8,
        (s) => s.params[index],
        (value, { command }) => {
            const param = IntelParams.get(command)?.[index];
            return param ? [fixed(param.describe(value), param.title)] : [];
        },
    );
 
const IntelSections = {
    command: [
        {
            heading: "Command",
            registers: [
                register(
                    "CMD",
                    "The command, without its drive select and parameter count bits",
                    8,
                    (s) => s.command,
                    (value) => [spelt(spellName(IntelCommand, value), "The command", IntelCommandNames)],
                ),
                ...Array.from({ length: IntelParamCount }, (_, index) => paramRow(index)),
            ],
        },
        {
            heading: "Status",
            registers: [
                register(
                    "STA",
                    "The status register",
                    8,
                    (s) => s.status,
                    (value) =>
                        bitLights(value, [
                            [StatusFlag.busy, "busy", "Busy with a command"],
                            [StatusFlag.commandFull, "cmd full", "The command register is full"],
                            [StatusFlag.paramFull, "param full", "The parameter register is full"],
                            [StatusFlag.resultReady, "result", "A result is ready to read"],
                            [StatusFlag.nmi, "NMI", "Asking for an NMI"],
                            [StatusFlag.needData, "data", "Waiting for a data byte to be read or written"],
                        ]),
                ),
                register(
                    "RES",
                    "The result of the last command",
                    8,
                    (s) => s.result,
                    (value) => [
                        spelt(spellName(ResultCodes, value & ResultCodeMask), "Bits 1-4: how it ended", ResultNames),
                        lit(
                            "deleted data",
                            "Bit 5: a sector read had the deleted data mark",
                            value & Result.flagDeletedData,
                        ),
                    ],
                ),
                register(
                    "PHASE",
                    "Where the 8271 is in the command",
                    8,
                    (s) => s.state,
                    (_, s) => [spelt(intelPhase(s), "What the 8271 is doing", IntelPhases)],
                ),
            ],
        },
    ],
    drive: [
        {
            heading: "Drive lines",
            registers: [
                register(
                    "OUT",
                    "The drive output lines",
                    8,
                    (s) => s.driveOut,
                    (value) =>
                        bitLights(value, [
                            [DriveOut.select_1, "sel 1", "Drive 1 selected"],
                            [DriveOut.select_0, "sel 0", "Drive 0 selected"],
                            [DriveOut.side, "side 1", "The upper side"],
                            [DriveOut.lowHeadCurrent, "low I", "Low head current, for the inner tracks"],
                            [DriveOut.loadHead, "load", "Load head: the head on the disc and the motor on"],
                            [DriveOut.direction, "in", "Step direction: towards the centre"],
                            [DriveOut.step, "step", "Step pulse"],
                            [DriveOut.writeEnable, "write", "Write enable"],
                        ]),
                ),
                ...[0, 1].map((drive) =>
                    register(
                        `TRK${drive}`,
                        `Where the 8271 believes drive ${drive}'s head is`,
                        8,
                        (s) => s.trackRegisters[drive],
                        (value) => [fixed(`track ${value}`, `The 8271's track register for drive ${drive}`)],
                    ),
                ),
            ],
        },
    ],
};
 
/** The drives a select pattern names, as words to follow a command's name. */
function drivesNamed(selected) {
    const drives = selected.flatMap((on, drive) => (on ? [drive] : []));
    Iif (!drives.length) return "";
    return drives.length === 1 ? `, drive ${drives[0]}` : `, drives ${drives.join(" and ")}`;
}
 
const headOn = (headTrack) => (headTrack === null ? "" : ` (head on track ${headTrack})`);
const hexBytes = (values) => values.map(hexbyte).join(" ");
 
const IntelValueResults = new Set([IntelCommand.readDriveStatus, IntelCommand.readSpecialRegister]);
 
function intelLogLine(entry) {
    switch (entry.kind) {
        case "command": {
            const { byte, command, select, params, headTrack } = entry;
            const meanings = (IntelParams.get(command) ?? [])
                .slice(0, params.length)
                .flatMap((param, index) => (param ? [param.describe(params[index])] : []));
            const where = drivesNamed([select & DriveOut.select_0, select & DriveOut.select_1]);
            return {
                text: `${spellName(IntelCommand, command)}${where}${meanings.length ? `: ${meanings.join(", ")}` : ""}${headOn(headTrack)}`,
                raw: `cmd ${hexbyte(byte)}${params.length ? ` params ${hexBytes(params)}` : ""}`,
            };
        }
        case "result": {
            const { command, status, result } = entry;
            const deleted = result & Result.flagDeletedData ? ", deleted data" : "";
            return {
                text: IntelValueResults.has(command)
                    ? `result &${hexbyte(result)}`
                    : `done: ${spellName(ResultCodes, result & ResultCodeMask)}${deleted}`,
                raw: `status ${hexbyte(status)} result ${hexbyte(result)}`,
            };
        }
        case "state":
            return { text: `state: ${spellName(IntelState, entry.from)} → ${spellName(IntelState, entry.to)}` };
        default:
            return { text: entry.text };
    }
}
 
// ---- The 1770 ----
 
const WdCommandNames = spellingsOf(WdCommand);
 
/** The type of the command itself: a force interrupt with the controller idle leaves its last command in place. */
function wdCommandType(command) {
    if (command < WdCommand.readSector) return 1;
    Eif (command < WdCommand.readAddress) return 2;
    return 3;
}
 
function wdCommandMeaning(value, { commandType: typeInForce, isVerify, isMulti, isSettle, isDeleted, stepRateMs }) {
    if (!typeInForce) return [];
    const commandType = wdCommandType(value);
    const meanings = [spelt(spellName(WdCommand, value), `A type ${commandType} command`, WdCommandNames)];
    if (commandType === 1)
        meanings.push(
            lit("verify", "Read an ID at the end of the seek to check the track", isVerify),
            fixed(`${stepRateMs} ms`, "The step rate"),
        );
    if (commandType === 2) meanings.push(lit("multi", "Go on to the next sector", isMulti));
    if (commandType !== 1) meanings.push(lit("settle", "Wait for the head to settle first", isSettle));
    if (value === WdCommand.writeSector || value === WdCommand.writeSectorMulti)
        meanings.push(lit("deleted", "Write a deleted data mark", isDeleted));
    return meanings;
}
 
const WdTypeIStatus = [
    [Status.motorOn, "motor", "The motor is on"],
    [Status.writeProtected, "WP", "The disc is write protected"],
    [Status.typeISpinUpDone, "spun up", "The motor has spun up"],
    [Status.recordNotFound, "seek err", "Seek error: the verify found no ID with the track number"],
    [Status.crcError, "CRC", "A CRC error in an ID"],
    [Status.typeITrack0, "track 0", "The head is over track 0"],
    [Status.typeIIndex, "index", "The index pulse"],
    [Status.busy, "busy", "Busy with a command"],
];
const WdTypeIIStatus = [
    [Status.motorOn, "motor", "The motor is on"],
    [Status.writeProtected, "WP", "A write was refused: the disc is write protected"],
    [Status.typeIIorIIIDeletedMark, "deleted", "The sector read had a deleted data mark"],
    [Status.recordNotFound, "not found", "The sector was not found"],
    [Status.crcError, "CRC", "A CRC error"],
    [Status.typeIIorIIILostByte, "lost", "A byte was lost: the host was too slow"],
    [Status.typeIIorIIIDrq, "DRQ", "A data byte waits to be read or written"],
    [Status.busy, "busy", "Busy with a command"],
];
 
function wdPhase({ state, timerState }) {
    Iif (state === WdState.timerWait) return spellName(WdTimerState, timerState);
    return spellName(WdState, state);
}
const WdPhases = [...spellingsOf(WdState), ...spellingsOf(WdTimerState)];
 
const plainRow = (name, title, field, text) =>
    register(
        name,
        title,
        8,
        (s) => s[field],
        (value) => [fixed(text(value), title)],
    );
 
const WdSections = {
    command: [
        {
            heading: "Command",
            registers: [
                register("CMD", "The command, without its flag bits", 8, (s) => s.command, wdCommandMeaning),
                plainRow("TR", "The track register", "track", (value) => `track ${value}`),
                plainRow("SR", "The sector register", "sector", (value) => `sector ${value}`),
                plainRow("DR", "The data register", "data", (value) => `${value}`),
            ],
        },
        {
            heading: "Status",
            registers: [
                register(
                    "STA",
                    "The status register, whose bits mean different things after a seek",
                    8,
                    (s) => s.status,
                    (value, { commandType }) => bitLights(value, commandType > 1 ? WdTypeIIStatus : WdTypeIStatus),
                ),
                register(
                    "PHASE",
                    "Where the controller is in the command",
                    8,
                    (s) => s.state,
                    (_, s) => [spelt(wdPhase(s), "What the controller is doing", WdPhases)],
                ),
            ],
        },
    ],
    drive: [
        {
            heading: "Drive lines",
            registers: [
                register(
                    "CTL",
                    "The drive control latch, as the controller sees it",
                    8,
                    (s) => s.control,
                    (value) => [
                        lit("reset", "The controller is held in reset", !(value & Control.reset)),
                        spelt(value & Control.density ? "FM" : "MFM", "Single density (FM) or double (MFM)", ["MFM"]),
                        lit("side 1", "The upper side", value & Control.side),
                        lit("sel 1", "Drive 1 selected", value & Control.drive1),
                        lit("sel 0", "Drive 0 selected", value & Control.drive0),
                    ],
                ),
            ],
        },
    ],
};
 
const WdErrorBits = {
    1: [
        [Status.recordNotFound, "seek error"],
        [Status.crcError, "CRC error"],
    ],
    2: [
        [Status.writeProtected, "write protected"],
        [Status.recordNotFound, "not found"],
        [Status.crcError, "CRC error"],
        [Status.typeIIorIIILostByte, "lost data"],
    ],
};
 
const wdRegisters = ({ track, sector, data }) => `tr ${track} sr ${sector} dr ${data}`;
 
const wdWhere = (control) =>
    `${drivesNamed([control & Control.drive0, control & Control.drive1])}, side ${control & Control.side ? 1 : 0}`;
 
function wdCommandDetails({ byte, track, sector, data, stepRateMs }) {
    const command = byte & WdCommandMask;
    const commandType = wdCommandType(command);
    Iif (command === WdCommand.forceInterrupt) return [];
    if (commandType === 1) {
        const details = command === WdCommand.seek ? [`to track ${data} from ${track}`] : [];
        Eif (byte & WdCommandBits.typeIVerify) details.push("verify");
        return [...details, `${stepRateMs} ms steps`];
    }
    Eif (commandType === 2) {
        const isWrite = command === WdCommand.writeSector || command === WdCommand.writeSectorMulti;
        const deleted = isWrite && byte & WdCommandBits.typeIIDeleted ? ["deleted data mark"] : [];
        return [`track ${track} sector ${sector}`, ...deleted];
    }
    return [];
}
 
function wdLogLine(entry) {
    switch (entry.kind) {
        case "command": {
            const details = wdCommandDetails(entry);
            return {
                text:
                    `${spellName(WdCommand, entry.byte & WdCommandMask)}${wdWhere(entry.control)}` +
                    `${details.length ? `: ${details.join(", ")}` : ""}${headOn(entry.headTrack)}`,
                raw: `cmd ${hexbyte(entry.byte)} ${wdRegisters(entry)}`,
            };
        }
        case "result": {
            const errors = WdErrorBits[Math.min(entry.commandType, 2)]
                .filter(([mask]) => entry.status & mask)
                .map(([, text]) => text);
            const deleted =
                entry.commandType > 1 && entry.status & Status.typeIIorIIIDeletedMark ? ", deleted data" : "";
            return {
                text: `done: ${errors.length ? errors.join(", ") : "ok"}${deleted}`,
                raw: `status ${hexbyte(entry.status)}`,
            };
        }
        case "control": {
            const { value } = entry;
            const reset = value & Control.reset ? "" : ", held in reset";
            return {
                text: `control${wdWhere(value)}, ${value & Control.density ? "FM" : "MFM"}${reset}`,
                raw: `wrote ${hexbyte(entry.written)}`,
            };
        }
        case "state":
            return {
                text: `state: ${spellName(WdState, entry.from)} → ${spellName(WdState, entry.to)}`,
                raw: wdRegisters(entry),
            };
        default:
            return { text: entry.text };
    }
}
 
// ---- Either ----
 
const HeadSection = {
    heading: "Head",
    registers: [
        register(
            "HEAD",
            "The track the drive's head is over",
            8,
            (s) => s.head.track,
            (_, { head }) => [
                spelt(
                    `drive ${head.drive} side ${head.side} track ${head.track}`,
                    "The drive selected, or the last one that was, and the side and physical track under its head",
                    ["drive 0 side 0 track 00"],
                ),
                lit("selected", "The controller selects the drive", head.selected),
                lit("spinning", "The disc is turning", head.spinning),
            ],
        ),
    ],
};
 
/** A drive's own events as log lines, for the drive toggle. */
function driveEvents(drive, driveIndex, log) {
    const say = (text) => log({ kind: "drive", text: `drive ${driveIndex}: ${text}` });
    return [
        ["startSpinning", () => say("motor on")],
        ["stopSpinning", () => say("motor off")],
        [
            "seekStart",
            ({ steps, stepMs }) =>
                say(
                    `stepping ${Math.abs(steps)} ${Math.abs(steps) === 1 ? "track" : "tracks"} ${steps > 0 ? "in" : "out"}, ${stepMs} ms a step`,
                ),
        ],
        ["seekEnd", () => say(`head on track ${drive.logicalTrack}`)],
        ["headStop", () => say(`head against its stop at track ${drive.logicalTrack}`)],
    ].map(([type, listener]) => ({ drive, type, listener }));
}
 
/**
 * A floating panel showing the disc controller, the 8271 or the 1770, decoded: its command and parameters, where it
 * is in the command, its status and result, the drive lines and the head. It logs the controller's commands, its state
 * changes and the drives' events while each is switched on, and the controller's warnings always.
 */
export class FdcPanel {
    /**
     * @param {object} options
     * @param {import("../6502.js").Cpu6502} options.cpu
     * @param {import("./floating-panel.js").PanelArrangement} options.arrangement
     * @param {{zoomToHead: (driveIndex: number) => void}} options.visualiser
     * @param {(() => Storage) | null} options.storage - where the log's switches are kept between visits, or null
     */
    constructor({ cpu, arrangement, visualiser, storage }) {
        this._fdc = cpu.fdc;
        this._storage = storage;
        const { floating, body, part } = decodedPanel({
            id: "fdc",
            title: "Disc controller",
            feature: "disc_controller",
            place: () => ({ x: Centred, y: StartEdge }),
            minSize: MinPanelSize,
            arrangement,
            draw: () => this.update(),
        });
        this.floating = floating;
        body.innerHTML = `
            <div class="fdc-registers"></div>
            <div>
                <div class="fdc-drive"></div>
                <button type="button" class="txt-btn fdc-zoom" tabindex="-1"
                    title="Open the disc surface on this side, zoomed in on the head">show the head on the disc</button>
            </div>
            <div class="fdc-log-area">
                <div class="decoded-heading fdc-log-heading">
                    Log
                    <button type="button" class="decoded-bit fdc-log-commands" tabindex="-1"
                        title="Log each command the controller is given, with its parameters and result">commands</button>
                    <button type="button" class="decoded-bit fdc-log-states" tabindex="-1"
                        title="Log each change of the controller's state; there are a great many">state changes</button>
                    <button type="button" class="decoded-bit fdc-log-drives" tabindex="-1"
                        title="Log the drives' motors starting and stopping, and their heads stepping">drives</button>
                    <input type="text" class="form-control form-control-sm panel-field fdc-log-filter"
                        aria-label="Show only the lines containing this" placeholder="filter" />
                    <button type="button" class="txt-btn fdc-log-copy" tabindex="-1"
                        title="Copy the lines shown to the clipboard">copy</button>
                    <button type="button" class="txt-btn fdc-log-clear" tabindex="-1">clear</button>
                </div>
                <div class="fdc-log"></div>
            </div>`;
        const { chip } = this._fdc.inspect();
        const sections = chip === "8271" ? IntelSections : WdSections;
        this._logLine = chip === "8271" ? intelLogLine : wdLogLine;
        const [commandSection, ...otherSections] = sections.command;
        this._registers = [
            new DecodedRegisters(part("fdc-registers"), [
                { ...commandSection, heading: `${chip} command` },
                ...otherSections,
            ]),
            new DecodedRegisters(part("fdc-drive"), [...sections.drive, HeadSection]),
        ];
        part("fdc-zoom").addEventListener("click", () => visualiser.zoomToHead(this._fdc.inspect().lastSelectedDrive));
 
        this._log = part("fdc-log");
        this._followingLog = true;
        this._followedTo = 0;
        this._log.addEventListener("scroll", () => {
            const log = this._log;
            // Lines may have come between following them and this event, so only a scroll back up stops following.
            this._followingLog =
                log.scrollTop >= this._followedTo || log.scrollTop + log.clientHeight >= log.scrollHeight - 1;
        });
        this._fdc.onLog = (entry) => this._appendLog(entry);
 
        this._driveListeners = this._fdc.drives.flatMap((drive, index) =>
            driveEvents(drive, index, (entry) => this._appendLog(entry)),
        );
        this._loggingDrives = false;
        this._logToggles = [
            {
                name: "commands",
                button: part("fdc-log-commands"),
                isOn: () => this._fdc.logCommands,
                set: (on) => (this._fdc.logCommands = on),
            },
            {
                name: "states",
                button: part("fdc-log-states"),
                isOn: () => this._fdc.logStateChanges,
                set: (on) => (this._fdc.logStateChanges = on),
            },
            {
                name: "drives",
                button: part("fdc-log-drives"),
                isOn: () => this._loggingDrives,
                set: (on) => this._logDrives(on),
            },
        ];
        for (const toggle of this._logToggles)
            toggle.button.addEventListener("click", () => {
                toggle.set(!toggle.isOn());
                this._showLogging();
                this._saveLogging();
            });
 
        this._filter = "";
        const filter = part("fdc-log-filter");
        filter.addEventListener("input", () => {
            this._filter = filter.value.toLowerCase();
            for (const line of this._log.children) line.hidden = !this._shows(line);
        });
        filter.addEventListener("keydown", (event) => {
            Eif (event.key === "Escape") {
                filter.value = "";
                filter.dispatchEvent(new Event("input"));
            }
            Iif (event.key === "Enter") filter.blur();
        });
        const copy = part("fdc-log-copy");
        // The clipboard is only there on a secure origin.
        copy.hidden = !navigator.clipboard;
        copy.addEventListener("click", () => this._copyLog());
        part("fdc-log-clear").addEventListener("click", () => this._log.replaceChildren());
        this._restoreLogging();
        this._showLogging();
        this.update();
    }
 
    _logDrives(on) {
        if (on === this._loggingDrives) return;
        this._loggingDrives = on;
        for (const { drive, type, listener } of this._driveListeners) {
            if (on) drive.addEventListener(type, listener);
            else drive.removeEventListener(type, listener);
        }
    }
 
    _showLogging() {
        for (const { button, isOn } of this._logToggles) {
            button.classList.toggle("lit", isOn());
            button.setAttribute("aria-pressed", String(isOn()));
        }
    }
 
    _restoreLogging() {
        if (!this._storage) return;
        const saved = readStored(this._storage, LogStorageKey, "the disc controller's log switches");
        for (const { name, set } of this._logToggles) set(saved?.[name] === true);
    }
 
    _saveLogging() {
        const saved = Object.fromEntries(this._logToggles.map(({ name, isOn }) => [name, isOn()]));
        writeStored(this._storage, LogStorageKey, saved, "the disc controller's log switches");
    }
 
    _shows(line) {
        return !this._filter || line.textContent.toLowerCase().includes(this._filter);
    }
 
    /** @param {import("../fdc.js").FdcLogEntry | {kind: "drive", text: string}} entry */
    _appendLog(entry) {
        const { text, raw } = this._logLine(entry);
        const isWarning = entry.kind === "warning";
        const line = document.createElement("div");
        line.textContent = isWarning ? `warning: ${text}` : text;
        if (raw) {
            const rawPart = document.createElement("span");
            rawPart.className = "fdc-log-raw";
            rawPart.textContent = raw;
            line.append("  ", rawPart);
        }
        // console.log(line.textContent);
        line.classList.toggle("fdc-log-warning", isWarning);
        line.hidden = !this._shows(line);
        this._log.append(line);
        Iif (this._log.childElementCount > MaxLogLines) this._log.firstElementChild.remove();
    }
 
    _copyLog() {
        const shown = [...this._log.children].filter((line) => !line.hidden).map((line) => line.textContent);
        navigator.clipboard
            .writeText(shown.join("\n"))
            .catch((error) => console.warn(`Could not copy the disc controller's log: ${error}`));
    }
 
    update() {
        const state = this._fdc.inspect();
        const drive = this._fdc.drives[state.lastSelectedDrive];
        state.head = {
            drive: state.lastSelectedDrive,
            selected: state.selectedDrive !== null,
            side: drive.isSideUpper ? 1 : 0,
            track: drive.track,
            spinning: drive.spinning,
        };
        for (const registers of this._registers) registers.update(state);
        // Scrolled here, once a frame, rather than as each line comes, which would lay the page out for every one.
        Eif (this._followingLog) {
            this._log.scrollTop = this._log.scrollHeight;
            this._followedTo = this._log.scrollTop;
        }
    }
}