All files / src teletext.js

97.31% Statements 254/261
91.37% Branches 106/116
100% Functions 21/21
97.52% Lines 236/242

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            271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x   271x 271x 271x 271x   271x 271x 271x   271x       271x     271x     2428160x 86720x   2341440x 2341440x 2341440x 14048640x   2341440x         867200x       813x 813x 78048x   1560960x 867200x   693760x   1560960x                                 271x     208128x 693760x 2081280x           271x 26016x 17344x 17344x 17344x 17344x 17344x 17344x       271x     271x 26016x 17344x 17344x 17344x 17344x 17344x 17344x       271x                     537x 537x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x 137472x             56x         168x 168x 168x   56x                                                     36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x   36x         36x 36x 36x       114x 111x 4x   107x     3x         5607433x 5607433x   5607433x               3x 3x 3x 3x 3x   73x 73x   4x 4x     31x 31x 31x               109x 109x 109x 109x 109x   7x 7x           2x 2x 2x   2x 2x   8x 8x   9x 9x   1x 1x   5607433x 23x 23x   5607410x 5607410x     5607433x         9321394x 9321394x 9321394x 9321394x           2837x 2837x         2837x 1421x     1416x 1416x     1416x               396x           701794x 701794x         701794x 628558x     73236x 73236x 73236x 73236x 73236x 73236x 73236x 73236x 73236x 73236x   73236x   73236x 6661x   6661x 2x   6659x       73236x             123322x               5826322x   5826322x   5826322x 5826322x   5826322x 5826322x 5826322x 5607433x 218889x 322x 321x 321x     218567x         5826322x     5826322x   5826322x 5826322x 5826322x 5826322x       869x 869x 4x     869x 31x 31x 10x     869x               868x   868x 215x 215x 3440x     653x 653x   653x 10448x 10448x             1x 1x           1x 1x 1x 8x 8x           867x 867x      
import { makeChars } from "./teletext_data.js";
import { BbcDefaultPalette as BbcDefaultCollook } from "./bbc-palette.js";
import { makeFast32 } from "./binary.js";
 
export class Teletext {
    constructor() {
        this.prevCol = 0;
        this.col = 7;
        this.bg = 0;
        this.sep = false;
        this.dbl = this.oldDbl = this.secondHalfOfDouble = this.wasDbl = false;
        this.gfx = false;
        this.conceal = false;
        this.flash = false;
        this.flashTime = 0;
        this.heldChar = 0;
        this.holdChar = false;
        this.cellGlyphIndex = 0;
        this.cellFlash = false;
        this.cellConceal = false;
        this.cellPalette = 0;
        this.dataQueue = new Uint8Array(4);
        this.scanlineCounter = 0;
        this.levelDEW = false;
        this.levelDISPTMG = false;
        this.levelRA0 = false;
 
        this.normalGlyphs = makeFast32(new Uint32Array(96 * 20));
        this.graphicsGlyphs = makeFast32(new Uint32Array(96 * 20));
        this.separatedGlyphs = makeFast32(new Uint32Array(96 * 20));
        this.colour = makeFast32(new Uint32Array(256));
 
        this.nextGlyphs = this.normalGlyphs;
        this.curGlyphs = this.normalGlyphs;
        this.heldGlyphs = this.normalGlyphs;
 
        this.init();
    }
 
    init() {
        const charData = makeChars();
 
        // Build palette from BBC default colours.
        this.rebuildColours(BbcDefaultCollook);
 
        function getLoResGlyphRow(c, row) {
            if (row < 0 || row >= 20) {
                return 0;
            } else {
                let index = c * 60 + (row >>> 1) * 6;
                let result = 0;
                for (let x = 0; x < 6; ++x) {
                    result |= (charData[index++] * 3) << (x * 2);
                }
                return result;
            }
        }
 
        function combineRows(a, b) {
            return a | ((a >>> 1) & b & ~(b >>> 1)) | ((a << 1) & b & ~(b << 1));
        }
 
        function makeHiResGlyphs(dest, graphicsGlyphs) {
            let index = 0;
            for (let c = 0; c < 96; ++c) {
                for (let row = 0; row < 20; ++row) {
                    let data;
                    if (!graphicsGlyphs || !!(c & 32)) {
                        data = combineRows(getLoResGlyphRow(c, row), getLoResGlyphRow(c, row + (row & 1 ? 1 : -1)));
                    } else {
                        data = getLoResGlyphRow(c, row);
                    }
                    dest[index++] =
                        (data & 0x1) * 0x7 +
                        (data & 0x2) * 0x14 +
                        (data & 0x4) * 0x34 +
                        (data & 0x8) * 0xe0 +
                        (data & 0x10) * 0x280 +
                        (data & 0x20) * 0x680 +
                        (data & 0x40) * 0x1c00 +
                        (data & 0x80) * 0x5000 +
                        (data & 0x100) * 0xd000 +
                        (data & 0x200) * 0x38000 +
                        (data & 0x400) * 0xa0000 +
                        (data & 0x800) * 0x1a0000;
                }
            }
        }
 
        makeHiResGlyphs(this.normalGlyphs, false);
 
        function setGraphicsBlock(c, x, y, w, h, sep, n) {
            for (let yy = 0; yy < h; ++yy) {
                for (let xx = 0; xx < w; ++xx) {
                    charData[c * 60 + (y + yy) * 6 + (x + xx)] = sep && (xx === 0 || yy === h - 1) ? 0 : n;
                }
            }
        }
 
        // Build graphics character set
        for (let c = 0; c < 96; ++c) {
            if (!(c & 32)) {
                setGraphicsBlock(c, 0, 0, 3, 3, false, !!(c & 1));
                setGraphicsBlock(c, 3, 0, 3, 3, false, !!(c & 2));
                setGraphicsBlock(c, 0, 3, 3, 4, false, !!(c & 4));
                setGraphicsBlock(c, 3, 3, 3, 4, false, !!(c & 8));
                setGraphicsBlock(c, 0, 7, 3, 3, false, !!(c & 16));
                setGraphicsBlock(c, 3, 7, 3, 3, false, !!(c & 64));
            }
        }
 
        makeHiResGlyphs(this.graphicsGlyphs, true);
 
        // Build separated graphics character set
        for (let c = 0; c < 96; ++c) {
            if (!(c & 32)) {
                setGraphicsBlock(c, 0, 0, 3, 3, true, !!(c & 1));
                setGraphicsBlock(c, 3, 0, 3, 3, true, !!(c & 2));
                setGraphicsBlock(c, 0, 3, 3, 4, true, !!(c & 4));
                setGraphicsBlock(c, 3, 3, 3, 4, true, !!(c & 8));
                setGraphicsBlock(c, 0, 7, 3, 3, true, !!(c & 16));
                setGraphicsBlock(c, 3, 7, 3, 3, true, !!(c & 64));
            }
        }
 
        makeHiResGlyphs(this.separatedGlyphs, true);
    }
 
    /**
     * Rebuild the 256-entry colour lookup from a 16-entry ABGR collook palette.
     * Each entry encodes: bits 7-5 = background colour index (0-7),
     * bits 4-2 = foreground colour index (0-7), bits 1-0 = blend weight.
     * Reference: b-em mode7_gen_nula_lookup().
     * @param {Uint32Array} collook - 16-entry palette in ABGR format (0xffBBGGRR).
     */
    rebuildColours(collook) {
        const gamma = 1.0 / 2.2;
        for (let i = 0; i < 256; ++i) {
            const weight = (i & 3) / 3.0;
            const fgIndex = (i >> 2) & 7;
            const bgIndex = (i >> 5) & 7;
            const fgColour = collook[fgIndex];
            const bgColour = collook[bgIndex];
            const fgR = (fgColour & 0xff) / 255;
            const fgG = ((fgColour >> 8) & 0xff) / 255;
            const fgB = ((fgColour >> 16) & 0xff) / 255;
            const bgR = (bgColour & 0xff) / 255;
            const bgG = ((bgColour >> 8) & 0xff) / 255;
            const bgB = ((bgColour >> 16) & 0xff) / 255;
            const blendedR = Math.pow(fgR * weight + bgR * (1.0 - weight), gamma) * 240;
            const blendedG = Math.pow(fgG * weight + bgG * (1.0 - weight), gamma) * 240;
            const blendedB = Math.pow(fgB * weight + bgB * (1.0 - weight), gamma) * 240;
            this.colour[i] = blendedR | 0 | ((blendedG | 0) << 8) | ((blendedB | 0) << 16) | (0xff << 24);
        }
    }
 
    snapshotState() {
        // Encode glyph table references as strings for serialization
        let nextGlyphsName, curGlyphsName, heldGlyphsName;
        for (const [name, table] of [
            ["normal", this.normalGlyphs],
            ["graphics", this.graphicsGlyphs],
            ["separated", this.separatedGlyphs],
        ]) {
            if (this.nextGlyphs === table) nextGlyphsName = name;
            if (this.curGlyphs === table) curGlyphsName = name;
            if (this.heldGlyphs === table) heldGlyphsName = name;
        }
        return {
            prevCol: this.prevCol,
            col: this.col,
            bg: this.bg,
            sep: this.sep,
            dbl: this.dbl,
            oldDbl: this.oldDbl,
            secondHalfOfDouble: this.secondHalfOfDouble,
            wasDbl: this.wasDbl,
            gfx: this.gfx,
            conceal: this.conceal,
            flash: this.flash,
            flashTime: this.flashTime,
            heldChar: this.heldChar,
            holdChar: this.holdChar,
            dataQueue: [...this.dataQueue],
            scanlineCounter: this.scanlineCounter,
            levelDEW: this.levelDEW,
            levelDISPTMG: this.levelDISPTMG,
            levelRA0: this.levelRA0,
            nextGlyphs: nextGlyphsName,
            curGlyphs: curGlyphsName,
            heldGlyphs: heldGlyphsName,
        };
    }
 
    restoreState(state) {
        this.prevCol = state.prevCol;
        this.col = state.col;
        this.bg = state.bg;
        this.sep = state.sep;
        this.dbl = state.dbl;
        this.oldDbl = state.oldDbl;
        this.secondHalfOfDouble = state.secondHalfOfDouble;
        this.wasDbl = state.wasDbl;
        this.gfx = state.gfx;
        this.conceal = state.conceal ?? false;
        this.flash = state.flash;
        this.flashTime = state.flashTime;
        this.heldChar = state.heldChar;
        this.holdChar = state.holdChar;
        this.dataQueue.set(state.dataQueue);
        this.scanlineCounter = state.scanlineCounter;
        this.levelDEW = state.levelDEW;
        this.levelDISPTMG = state.levelDISPTMG;
        this.levelRA0 = state.levelRA0;
 
        const glyphMap = {
            normal: this.normalGlyphs,
            graphics: this.graphicsGlyphs,
            separated: this.separatedGlyphs,
        };
        this.nextGlyphs = glyphMap[state.nextGlyphs] || this.normalGlyphs;
        this.curGlyphs = glyphMap[state.curGlyphs] || this.normalGlyphs;
        this.heldGlyphs = glyphMap[state.heldGlyphs] || this.normalGlyphs;
    }
 
    setNextChars() {
        if (this.gfx) {
            if (this.sep) {
                this.nextGlyphs = this.separatedGlyphs;
            } else {
                this.nextGlyphs = this.graphicsGlyphs;
            }
        } else {
            this.nextGlyphs = this.normalGlyphs;
        }
    }
 
    handleControlCode(data) {
        const wasGfx = this.gfx;
        const wasHoldChar = this.holdChar;
 
        switch (data) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                this.gfx = false;
                this.col = data;
                this.conceal = false;
                this.setNextChars();
                break;
            case 8:
                this.flash = true;
                break;
            case 9:
                this.flash = false;
                break;
            case 12:
            case 13:
                this.dbl = !!(data & 1);
                Eif (this.dbl) this.wasDbl = true;
                break;
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
                this.gfx = true;
                this.col = data & 7;
                this.conceal = false;
                this.setNextChars();
                break;
            case 24:
                this.conceal = true;
                break;
            case 25:
                this.sep = false;
                this.setNextChars();
                break;
            case 26:
                this.sep = true;
                this.setNextChars();
                break;
            case 28:
                this.bg = 0;
                break;
            case 29:
                this.bg = this.col;
                break;
            case 30:
                this.holdChar = true;
                break;
            case 31:
                this.holdChar = false;
                break;
        }
        if (wasGfx && (wasHoldChar || this.holdChar) && this.dbl === this.oldDbl) {
            data = this.heldChar;
            this.curGlyphs = this.heldGlyphs;
        } else {
            this.heldChar = 0x20;
            data = 0x20;
        }
 
        return data;
    }
 
    fetchData(data) {
        // `copyWithin` over four bytes costs more than the moves it saves.
        this.dataQueue[0] = this.dataQueue[1];
        this.dataQueue[1] = this.dataQueue[2];
        this.dataQueue[2] = this.dataQueue[3];
        this.dataQueue[3] = data & 0x7f;
    }
 
    setDEW(level) {
        // The SAA5050 input pin "DEW" is connected to the 6845 output pin
        // "VSYNC" and it is used to track frames.
        const oldLevel = this.levelDEW;
        this.levelDEW = level;
 
        // Trigger on high -> low. This appears to be what the hardware does.
        // It needs to be this way for the scanline counter to stay in sync
        // if you set R6>R4.
        if (!oldLevel || level) {
            return;
        }
 
        this.scanlineCounter = 0;
        this.secondHalfOfDouble = false;
 
        // 3:1 flash ratio.
        if (++this.flashTime === 64) this.flashTime = 0;
    }
 
    // Flashing text starts off in sync with a slow cursor, extinguished together. Multiple MODE
    // changes gradually desynchronise the frame counters.
    // TODO(#1057) setDEW is reached a MOS-dependent number of times before Video.frameCount rises, so
    // the initial sync here holds under MOS 1.20 only.
    get hideFlashing() {
        return this.flashTime < 16;
    }
 
    setDISPTMG(level) {
        // The SAA5050 input pin "LOSE" is connected to the 6845 output pin
        // "DISPTMG" and it is used to track scanlines.
        const oldLevel = this.levelDISPTMG;
        this.levelDISPTMG = level;
 
        // Trigger on high -> low. This is probably what the hardware does as
        // we need to increment scanline at the end of the scanline, not the
        // beginning.
        if (!oldLevel || level) {
            return;
        }
 
        this.col = 7;
        this.bg = 0;
        this.holdChar = false;
        this.heldChar = 0x20;
        this.nextGlyphs = this.heldGlyphs = this.normalGlyphs;
        this.flash = false;
        this.sep = false;
        this.gfx = false;
        this.conceal = false;
        this.dbl = false;
 
        this.scanlineCounter++;
        // Check for end of character row.
        if (this.scanlineCounter === 10) {
            this.scanlineCounter = 0;
 
            if (this.secondHalfOfDouble) {
                this.secondHalfOfDouble = false;
            } else {
                this.secondHalfOfDouble = this.wasDbl;
            }
        }
 
        this.wasDbl = false;
    }
 
    setRA0(level) {
        // The SAA5050 input pin "CRS" is connected to the 6845 output pin
        // "RA0", via a signal inverter, and it is used to select between a
        // normal scanline and a calculated smoothing scanline.
        this.levelRA0 = level;
    }
 
    /**
     * Clock one character through the chip: take the oldest byte in the pipeline, apply it
     * to the character state, and latch how the resulting cell should look.
     */
    advance() {
        let data = this.dataQueue[0];
 
        this.oldDbl = this.dbl;
 
        this.prevCol = this.col;
        this.curGlyphs = this.nextGlyphs;
 
        let flashThisCell = this.flash;
        let concealThisCell = this.conceal;
        if (data < 0x20) {
            data = this.handleControlCode(data);
        } else if (this.gfx) {
            if (data & 0x20) {
                this.heldChar = data;
                this.heldGlyphs = this.curGlyphs;
            }
        } else {
            this.heldChar = 32;
        }
 
        // Flash (code 8) is "Set After" — flashThisCell retains the pre-control-code state.
        // Steady (code 9) is "Set At" — update so this cell stops flashing immediately.
        if (flashThisCell && !this.flash) flashThisCell = false;
 
        // Conceal (code 24) is "Set At", and a colour code only reveals from the cell after itself.
        if (this.conceal) concealThisCell = true;
 
        this.cellGlyphIndex = (data - 32) * 20;
        this.cellFlash = flashThisCell;
        this.cellConceal = concealThisCell;
        this.cellPalette = ((this.bg & 7) << 5) | ((this.prevCol & 7) << 2);
    }
 
    glyphScanline() {
        let scanline = this.scanlineCounter << 1;
        if (this.levelRA0) {
            scanline++;
        }
 
        if (this.oldDbl) {
            scanline = scanline >>> 1;
            if (this.secondHalfOfDouble) {
                scanline += 10;
            }
        }
        return scanline;
    }
 
    /**
     * Paint the cell most recently latched by `advance`. Everything else read here changes
     * per scanline or per field, not per character.
     */
    emit(buf, offset) {
        const scanline = this.glyphScanline();
 
        if (this.cellConceal || (this.cellFlash && this.hideFlashing) || (this.secondHalfOfDouble && !this.dbl)) {
            const backgroundColour = this.colour[this.cellPalette & 0xe0];
            for (let i = 0; i < 16; ++i) {
                buf[offset++] = backgroundColour;
            }
        } else {
            let chardef = this.curGlyphs[this.cellGlyphIndex + scanline];
            const paletteIndex = this.cellPalette;
 
            for (let pixel = 0; pixel < 16; ++pixel) {
                buf[offset + pixel] = this.colour[paletteIndex + (chardef & 3)];
                chardef >>>= 2;
            }
        }
    }
 
    // The second half of the cell `emit` last painted, for the ULA's 1MHz repaint.
    emitSecondHalf(buf, offset) {
        const scanline = this.glyphScanline();
        Iif (this.cellConceal || (this.cellFlash && this.hideFlashing) || (this.secondHalfOfDouble && !this.dbl)) {
            const backgroundColour = this.colour[this.cellPalette & 0xe0];
            for (let pixel = 8; pixel < 16; ++pixel) {
                buf[offset + pixel] = backgroundColour;
            }
        } else {
            let chardef = this.curGlyphs[this.cellGlyphIndex + scanline] >>> 16;
            const paletteIndex = this.cellPalette;
            for (let pixel = 8; pixel < 16; ++pixel) {
                buf[offset + pixel] = this.colour[paletteIndex + (chardef & 3)];
                chardef >>>= 2;
            }
        }
    }
 
    render(buf, offset) {
        this.advance();
        this.emit(buf, offset);
    }
}