All files / src/web basic-panel.js

3.73% Statements 5/134
0% Branches 0/54
2.94% Functions 1/34
4.31% Lines 5/116

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                                          2x 2x 2x     2x                   2x                                                                                                                                                                                                                                                                                                                                                                                                                                                      
import {
    basicRom,
    findLine,
    firstStatement,
    holds,
    LanguageRomNumber,
    lineHolding,
    listStatements,
    readPointers,
    readResidentIntegers,
    readVariables,
    romTitle,
    startsStatement,
} from "../basic-state.js";
import { hexword } from "../hex.js";
import { bindGotoForm } from "./debug-dom.js";
import { setText } from "./dom-utils.js";
import { DebugGroup, MenuGroup, panelShell } from "./floating-panel.js";
import { keyLine } from "./panel-keys.js";
import { Centred, StartEdge } from "./panel-layout.js";
 
const MinPanelSize = { w: 260, h: 160 };
const LargestLineNumber = 32767;
const InTube = "BASIC runs in the second processor, whose memory this panel does not read.";
 
/** The BASIC panel's keys, answering only in the debugger; see panel-keys.js for the shape. */
export const BasicKeys = [
    {
        combo: "s",
        key: "s",
        run: "stepStatement",
        label: "step statement",
        description: "Run to BASIC's next statement",
    },
];
 
const basicHex = (value) => `&${hexword(value).toUpperCase()}`;
 
function element(tag, className, text) {
    const node = document.createElement(tag);
    node.className = className;
    node.textContent = text;
    return node;
}
 
/**
 * A floating panel showing BBC BASIC as it runs a program: where the program and its variables are, the line running
 * with its statement marked, and the variables. It can run the machine to a line, or on by a statement, each with a
 * hook on the CPU that lasts only as long as that run.
 */
export class BasicPanel {
    /** Whether the machine runs BBC BASIC: the Atom's BASIC is another language. */
    static fits(cpu) {
        return !cpu.model.isAtom;
    }
 
    /**
     * @param {object} options
     * @param {import("../6502.js").Cpu6502} options.cpu
     * @param {import("./floating-panel.js").PanelArrangement} options.arrangement
     * @param {import("./panel-keys.js").PanelKeys} options.keys
     * @param {(stopsHere: () => boolean) => void} options.runUntil runs the machine from the debugger until
     * `stopsHere()` holds before an instruction
     * @param {() => boolean} options.inDebugger whether the machine is stopped in the debugger, where its keys answer
     */
    constructor({ cpu, arrangement, keys, runUntil, inDebugger }) {
        this._cpu = cpu;
        this._peek = (address) => cpu.peekmemAsLanguage(address);
        this._runUntil = runUntil;
        /** @type {{bank: number, title: string, basic: ReturnType<typeof basicRom>} | null} */
        this._language = null;
        this._shown = { line: "", variables: "" };
        const { floating, body } = panelShell({
            id: "basic",
            title: "BASIC",
            feature: "basic",
            menuGroup: MenuGroup.software,
            group: DebugGroup,
            place: () => ({ x: Centred, y: StartEdge }),
            minSize: MinPanelSize,
            arrangement,
        });
        this.floating = floating;
        const { panel } = floating;
        panel.classList.add("debug-panel");
        const header = panel.querySelector(".panel-header");
        this._controls = this._makeControls();
        header.querySelector(".panel-close").before(...this._controls);
        body.innerHTML = `
            <div class="basic-absent" hidden></div>
            <div class="basic-present">
                <div class="basic-pointers"></div>
                <div class="basic-line"></div>
                <div class="basic-residents"></div>
                <div class="basic-variables"><table><tbody></tbody></table></div>
                <div class="basic-routines"></div>
                <div class="panel-keys"></div>
            </div>`;
        this._absent = body.querySelector(".basic-absent");
        this._present = body.querySelector(".basic-present");
        this._pointers = body.querySelector(".basic-pointers");
        this._line = body.querySelector(".basic-line");
        this._variables = body.querySelector(".basic-variables tbody");
        this._routines = body.querySelector(".basic-routines");
        this._residents = readResidentIntegers(this._peek).map(({ name }) => {
            const value = element("span", "basic-value", "");
            body.querySelector(".basic-residents").append(element("span", "basic-name", name), value);
            return value;
        });
        body.querySelector(".panel-keys").textContent = keyLine(BasicKeys);
 
        floating.whileOpen(() => this.update());
        keys.register({
            table: BasicKeys,
            actions: { stepStatement: () => this.stepStatement() },
            isLive: () => inDebugger() && this.floating.isOpen && this._basic() !== null,
            panel: this.floating,
        });
        this.update();
    }
 
    _makeControls() {
        const form = document.createElement("form");
        form.className = "goto-form";
        form.innerHTML = `<input type="text" class="form-control form-control-sm goto-addr panel-field"
            placeholder="Run to line" aria-label="Run to line" autocomplete="off" />`;
        bindGotoForm(
            form,
            (text) => this._lineNumbered(text),
            (line) => this.runToLine(line.number),
        );
        const step = element("button", "btn btn-sm btn-outline-light basic-step", "Step");
        step.type = "button";
        step.title = "Run to BASIC's next statement (s)";
        step.addEventListener("click", () => this.stepStatement());
        return [form, step];
    }
 
    /** The language ROM, read again only when another is the language. */
    _currentLanguage() {
        const bank = this._cpu.bankPagedBy(this._peek(LanguageRomNumber));
        const rom = this._cpu.romBank(bank);
        const title = romTitle(rom);
        if (this._language?.bank !== bank || this._language.title !== title)
            this._language = { bank, title, basic: basicRom(rom) };
        return this._language;
    }
 
    /** BASIC, when it is the language and runs in this machine, or null. */
    _basic() {
        if (this._cpu.hasTube) return null;
        return this._currentLanguage().basic;
    }
 
    /** Whether BASIC is about to start a statement of its program, and not of a command typed at its prompt. */
    _atProgramStatement({ basic, bank }) {
        const cpu = this._cpu;
        if (cpu.pc !== basic.statementStart || cpu.pagedBank !== bank) return false;
        const { page, top, reading } = readPointers(this._peek);
        return reading > page && reading < top && startsStatement(this._peek, reading);
    }
 
    _lineNumbered(text) {
        if (!this._basic() || !/^\d+$/.test(text) || Number(text) > LargestLineNumber) return undefined;
        return findLine(this._peek, readPointers(this._peek).page, Number(text)) ?? undefined;
    }
 
    /** BASIC as it is when a run starts, which the run keeps to should another language take over. */
    _runningBasic() {
        return this._basic() ? this._language : null;
    }
 
    stepStatement() {
        const running = this._runningBasic();
        if (!running) return;
        this._runUntil(() => this._atProgramStatement(running));
    }
 
    /**
     * Runs the machine until BASIC comes to line `number`: to its first statement, or to any of its statements from
     * elsewhere, as a procedure defined on it is called. The line is looked for at each statement, as the program can
     * be edited while it runs.
     */
    runToLine(number) {
        const running = this._runningBasic();
        const peek = this._peek;
        const lineNow = () => findLine(peek, readPointers(peek).page, number);
        const isIn = (line, address) => line !== null && holds(line, address);
        let wasInLine = isIn(lineNow(), readPointers(peek).reading);
        this._runUntil(() => {
            if (!this._atProgramStatement(running)) return false;
            const line = lineNow();
            const { reading } = readPointers(peek);
            const cameFromElsewhere = !wasInLine;
            wasInLine = isIn(line, reading);
            return wasInLine && (cameFromElsewhere || reading === firstStatement(peek, line));
        });
    }
 
    update() {
        const basic = this._basic();
        this._absent.hidden = !!basic;
        this._present.hidden = !basic;
        for (const control of this._controls) control.hidden = !basic;
        if (!basic) {
            this._absent.textContent = this._cpu.hasTube
                ? InTube
                : `The current language is ${this._language.title || "an unnamed ROM"}, not a BASIC this panel can read.`;
            return;
        }
        const peek = this._peek;
        const pointers = readPointers(peek);
        this._pointers.textContent = ["page", "top", "lomem", "himem"]
            .map((name) => `${name.toUpperCase()} ${basicHex(pointers[name])}`)
            .join("  ");
        this._showLine(lineHolding(peek, pointers.page, pointers.reading), pointers.reading, basic.keywords);
        readResidentIntegers(peek).forEach(({ value }, i) => {
            setText(this._residents[i], value);
        });
        this._showVariables(readVariables(peek));
    }
 
    _showLine(line, reading, keywords) {
        const statements = line ? listStatements(this._peek, line, keywords) : [];
        const running = statements.findIndex(({ from, to }) => reading >= from && reading <= to);
        const shown = JSON.stringify([line?.number, running, statements.map(({ text }) => text)]);
        if (shown === this._shown.line) return;
        this._shown.line = shown;
        if (!line) {
            this._line.replaceChildren(element("span", "basic-idle", "No program line running"));
            return;
        }
        const parts = statements.flatMap(({ text }, i) => [
            ...(i ? [":"] : []),
            element("span", i === running ? "basic-running" : "", text),
        ]);
        this._line.replaceChildren(element("span", "basic-line-number", String(line.number)), ...parts);
    }
 
    _showVariables({ variables, routines }) {
        const shown = JSON.stringify([variables, routines]);
        if (shown === this._shown.variables) return;
        this._shown.variables = shown;
        this._variables.replaceChildren(
            ...variables.map(({ name, value }) => {
                const row = document.createElement("tr");
                const valueCell = element("td", "basic-value", value);
                valueCell.title = value;
                row.append(element("td", "basic-name", name), valueCell);
                return row;
            }),
        );
        this._routines.textContent = routines.join(" ");
    }
}