All files / src ppia.js

90.1% Statements 164/182
76.19% Branches 32/42
73.58% Functions 39/53
91.46% Lines 150/164

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                                                                              39x 39x 39x 39x       39x   39x 39x 39x 39x 39x 39x 39x 39x         40x                         133x   66x     67x   133x                                       92802x     92802x 92801x   92788x 92788x 92788x       1x     2x   2x 2x   10x     10x           8x 8x 8x   7x 7x   1x 1x   8x 8x 8x           107422x 107421x   48602x 48602x   44187x 44187x 44187x 44187x 441870x   44187x 44187x 44187x     14631x     14631x       14631x     14631x 14631x               14631x     1x         141390x 141390x 141390x       44187x 44187x 44187x       14776x 14776x 14776x                                 39x   39x 39x 624x     39x   39x 39x         39x 39x 39x 39x 39x   39x     39x       39x         3x 48x 768x     3x         1x 1x                     1x                                             7x 7x         1x 1x         2x 2x           8x   8x 71x 550x 3x       5x                   141390x             14776x 14776x 9x 9x         141390x                                                   5x 5x 5x                           5x                 2x 2x 2x               7x 7x       1x 1x         2x       3x       2x       1x       4x 4x 4x         10x 10x 5x 5x 5x 5x           5x 5x 5x 4x           2x               32x             2x 2x 2x 2x 2x 2x 2x 2x 2x 32x     2x 2x      
import { getKeyMapAtom } from "./keymap-atom.js";
 
// 8255 Programmable Peripheral Interface Adapter for the Acorn Atom.
// Reference: http://mdfs.net/Docs/Comp/Acorn/Atom/atap25.htm
// Memory map: http://mdfs.net/Docs/Comp/Acorn/Atom/MemoryMap
//
// Port A - 0xB000 (output)
//   bits 0-3: Keyboard row select
//   bits 4-7: MC6847 graphics mode
//
// Port B - 0xB001 (input)
//   bits 0-5: Keyboard column (active low)
//   bit 6:    CTRL key (low when pressed)
//   bit 7:    SHIFT key (low when pressed)
//
// Port C - 0xB002 (mixed I/O)
//   Output bits:
//     0: Tape output
//     1: Enable 2.4 kHz to cassette output
//     2: Loudspeaker
//     3: Colour Set Select (CSS)
//   Input bits:
//     4: 2.4 kHz input
//     5: Cassette input
//     6: REPT key (low when pressed)
//     7: 60 Hz VSync signal (low during flyback)
//
// Keyboard matrix (active low, active when key pressed):
//   Port A row →    9   8   7   6   5   4   3   2   1   0
//   Port B col ↓
//        ~b0 :     SPC  [   \   ]   ^  LCK <-> ^-v Lft Rgt
//        ~b1 :     Dwn Up  CLR ENT CPY DEL  0   1   2   3
//        ~b2 :      4   5   6   7   8   9   :   ;   <   =
//        ~b3 :      >   ?   @   A   B   C   D   E   F   G
//        ~b4 :      H   I   J   K   L   M   N   O   P   Q
//        ~b5 :      R   S   T   U   V   W   X   Y   Z  ESC
//        ~b6 :                                          Ctrl
//        ~b7 :                                          Shift
 
const PORTA = 0x0,
    PORTB = 0x1,
    PORTC = 0x2,
    CREG = 0x3; // control register
 
class PPIA {
    constructor(cpu) {
        this.cpu = cpu;
 
        this.latcha = 0;
        this.latchb = 0;
        this.latchc = 0;
        this.portapins = 0;
        this.portbpins = 0;
        this.portcpins = 0;
        this.creg = 0;
        this.speaker = 0;
    }
 
    reset() {
        //http://members.casema.nl/hhaydn/8255_pin.html
        this.latcha = this.latchb = this.latchc = 0x00;
    }
 
    setVBlankInt(level) {
        // level == 1 when in the vsync
        // FE66_wait_for_flyback_start will loop until bit 7 (copied into N register using BIT)
        // of B002 is not 0 (i.e until BPL fails when bit 7 is 1)
        // then
        // FE6B_wait_for_flyback will loop until bit 7 of B002 is (copied into N register using BIT)
        // of B002 is not 1 (i.e until BMI fails when bit 7 is 0)
 
        //60 Hz sync signal - normally 1 during the frame, but goes 0 at start of flyback (at the end of a frame).
        //opposite of the 'level'
        if (!level) {
            // set bit 7 to 1 - in frame
            this.latchc |= 0x80;
        } else {
            // set bit 7 to 0 - in vsync
            this.latchc &= ~0x80;
        }
        this.recalculatePortCPins();
    }
 
    /*
 Port C - #B002
        Output bits:      Function:
             0          Tape output
             1          Enable 2.4 kHz to cassette output
             2          Loudspeaker
             3          Not used
 
        Input bits:       Function:
             4          2.4 kHz input
             5          Cassette input
             6          REPT key (low when pressed)
             7          60 Hz sync signal (low during flyback)
 The port C output lines, bits 0 to 3, may be used for user applications when the cassette interface is not being used.
 
 */
    write(addr, val) {
        val |= 0;
        // The 8255 only decodes A0-A1; addresses 4-7 mirror 0-3.
        // Addresses 8-15 are outside the 8255's decode range.
        if ((addr & 0xf) >= 0x8) return;
        switch (addr & 0x3) {
            case PORTA:
                this.latcha = val;
                this.recalculatePortAPins();
                break;
 
            case PORTB:
                // Port B is input-only; writes are ignored.
                break;
 
            case PORTC:
                this.latchc = (this.latchc & 0xf0) | (val & 0x0f);
 
                this.recalculatePortCPins();
                break;
            case CREG: {
                this.creg = val & 0xff;
                // 8255 CREG: D7=1 is mode-set (ignored; Atom uses fixed port directions),
                // D7=0 is Bit Set/Reset (BSR) for individual port C output bits.
                if (val & 0x80) break; // mode-set: no action needed
 
                // BSR: set or clear a single port C output bit.
                // NOTE: Simplified: only handles bits 2 (speaker) and 3 (CSS),
                // and rebuilds the lower nibble. Works because the Atom ROM
                // only BSR-toggles these two bits.
                let speaker = this.latchc & 0x04;
                let css = this.latchc & 0x08;
                switch (val & 0xe) {
                    case 0x4: // port C pin 2 (speaker)
                        speaker = (val & 1) << 2;
                        break;
                    case 0x6: // port C pin 3 (CSS)
                        css = (val & 1) << 3;
                        break;
                }
                this.latchc = (this.latchc & 0xf0) | css | speaker;
                this.recalculatePortCPins();
                break;
            }
        }
    }
 
    read(addr) {
        if ((addr & 0xf) >= 0x8) return addr >>> 8; // open bus
        switch (addr & 0x3) {
            case PORTA:
                this.recalculatePortAPins();
                return this.portapins;
            case PORTB: {
                this.recalculatePortBPins();
                const keyrow = this.portapins & 0x0f;
                const n = this.keys[keyrow];
                let r = 0xff; // all keys unpressed
                for (let b = 0; b <= 9; b++) r &= ~(n[b] << b);
                // CTRL and SHIFT are always readable regardless of row selection
                const ctrl_shift = (this.keys[0][7] << 7) | (this.keys[0][6] << 6);
                r &= ~(ctrl_shift & 0xc0);
                return r;
            }
            case PORTC: {
                this.recalculatePortCPins();
 
                // Force HZIN (bit 4) high
                this.portcpins = (this.portcpins & 0xef) | (1 << 4);
 
                // Read back full port C state: output bits 0-3 from latch,
                // input bits 4-7 from pins, with REPT key overlay on bit 6.
                let val = this.portcpins;
 
                // REPT key: bit 6 is LOW when pressed
                const rept_key = (!this.keys[1][6] << 6) & 0x40;
                val = (val & ~0x40) | rept_key;
 
                // Track cassette input transitions. The Atom ROM tape routines:
                //   0xfc0a - OSBGET: get byte from tape (every 3.34ms)
                //   0xfcd2 - test tape input pulse (every 0.033ms / 33 cycles)
                //   0xfcc2 - count duration of tape pulse (<8 loops = '1', >=8 = '0')
                //   0xfe6e, 0xfe9d, 0xfe69 - flyback/VSync routines
                // Between each receiveBit, fcd2 is called ~6 times (33 cycles each).
                return val;
            }
            default:
                return addr >>> 8; // CREG and unmapped registers return open bus
        }
    }
 
    recalculatePortAPins() {
        this.portapins = this.latcha;
        this.drivePortA();
        this.portAUpdated();
    }
 
    recalculatePortBPins() {
        this.portbpins = this.latchb;
        this.drivePortB();
        this.portBUpdated();
    }
 
    recalculatePortCPins() {
        this.portcpins = this.latchc;
        this.drivePortC();
        this.portCUpdated();
    }
 
    // No-op hooks for subclass override
    drivePortA() {}
    drivePortB() {}
    drivePortC() {}
    portAUpdated() {}
    portBUpdated() {}
    portCUpdated() {}
}
 
// On the atom, the PPIA does the keyboard, speaker and tape.
// On the BBC, sysVIA does the keyboard and pokes the soundchip
// and the ACIA does the tape
export class AtomPPIA extends PPIA {
    constructor(cpu, initialLayout, scheduler) {
        super(cpu);
 
        this.keys = [];
        for (let i = 0; i < 16; ++i) {
            this.keys[i] = new Uint8Array(16);
        }
 
        this.setKeyLayout(initialLayout);
 
        this.keyboardEnabled = true;
        this.lastSpeakerBit = 0;
        // The Atom has no lock lights. Report caps lock as "on" and shift
        // lock as "off" so the paste routine's lock-toggle logic is never
        // triggered (it toggles CAPSLOCK when !capsLockLight, and SHIFTLOCK
        // when shiftLockLight).
        this.capsLockLight = true;
        this.shiftLockLight = false;
        this.tapeCarrierCount = 0;
        this.tapeDcdLineLevel = false;
        this.motorOn = false;
 
        this.reset();
 
        // from ACIA
        this.runTapeTask = scheduler.newTask(() => this.runTape());
    }
 
    setKeyLayout(map) {
        this.keycodeToRowCol = getKeyMapAtom(map);
    }
 
    // from SysVIA
    clearKeys() {
        for (let i = 0; i < this.keys.length; ++i) {
            for (let j = 0; j < this.keys[i].length; ++j) {
                this.keys[i][j] = 0;
            }
        }
        this.updateKeys();
    }
 
    // from SysVIA
    disableKeyboard() {
        this.keyboardEnabled = false;
        this.clearKeys();
    }
 
    // from SysVIA
    enableKeyboard() {
        this.keyboardEnabled = true;
        this.clearKeys();
    }
 
    // from SysVIA
    set(key, val, shiftDown) {
        Eif (!this.keyboardEnabled) return;
        const colrow = this.keycodeToRowCol[!!shiftDown][key];
        if (!colrow) return;
 
        this.keys[colrow[0]][colrow[1]] = val;
        this.updateKeys();
    }
 
    // from SysVIA
    keyDown(key, shiftDown) {
        this.set(key, 1, shiftDown);
    }
 
    // from SysVIA
    keyUp(key) {
        // set up for both keymaps
        // (with and without shift)
        this.set(key, 0, true);
        this.set(key, 0, false);
    }
 
    // from SysVIA
    keyDownRaw(colrow) {
        this.keys[colrow[0]][colrow[1]] = 1;
        this.updateKeys();
    }
 
    // from SysVIA
    keyUpRaw(colrow) {
        this.keys[colrow[0]][colrow[1]] = 0;
        this.updateKeys();
    }
 
    // from SysVIA
    keyToggleRaw(colrow) {
        this.keys[colrow[0]][colrow[1]] = 1 - this.keys[colrow[0]][colrow[1]];
        this.updateKeys();
    }
 
    // from SysVIA
    hasAnyKeyDown() {
        // 10 for ATOM
        const numCols = 10;
 
        for (let i = 0; i < numCols; ++i) {
            for (let j = 0; j < 8; ++j) {
                if (this.keys[i][j]) {
                    return true;
                }
            }
        }
        return false;
    }
 
    // nothing on ATOM
    updateKeys() {}
 
    // nothing on ATOM
    polltime() {}
 
    portAUpdated() {
        this.updateKeys();
    }
 
    // nothing on ATOM
    portBUpdated() {}
 
    portCUpdated() {
        const speakerBit = (this.portcpins & 0x04) >>> 2;
        if (speakerBit !== this.lastSpeakerBit) {
            this.lastSpeakerBit = speakerBit;
            this.cpu.soundChip.speakerGenerator.pushBit(speakerBit, this.cpu.currentCycles, this.cpu.cycleSeconds);
        }
    }
 
    drivePortA() {
        this.updateKeys();
    }
 
    drivePortB() {
        // Nothing driving here.
    }
 
    drivePortC() {
        // Nothing driving here.
    }
 
    // ATOM TAPE SUPPORT
    // from ACIA on BBC
 
    // set by TAPE
    tone(freq) {
        let toneGen = this.cpu.soundChip.toneGenerator;
        if (!freq) toneGen.mute();
        else toneGen.tone(freq);
    }
 
    // nothing on ATOM
    dcdLineUpdated() {}
 
    // set by TAPE
    setTapeCarrier(level) {
        if (!level) {
            this.tapeCarrierCount = 0;
            this.tapeDcdLineLevel = false;
        } else E{
            this.tapeCarrierCount++;
            // The tape hardware doesn't raise DCD until the carrier tone
            // has persisted for a while. The BBC service manual opines,
            // "The DCD flag in the 6850 should change 0.1 to 0.4 seconds
            // after a continuous tone appears".
            // Star Drifter doesn't load without this.
            // We use 0.174s, measured on an issue 3 model B.
            // Testing on real hardware, DCD is blipped, it lowers about
            // 210us after it raises, even though the carrier tone
            // may be continuing.
            this.tapeDcdLineLevel = this.tapeCarrierCount === 209;
        }
        this.dcdLineUpdated();
    }
 
    // Receive bits from tape (called by tape.poll via PPIA, not ACIA like BBC).
    // Called once every ~208 clock cycles (208us at 1 MHz).
    // Recognition: '1' = 4 half-cycles at 1.2 kHz (duration < 8),
    //              '0' = 8 half-cycles at 2.4 kHz (duration >= 8).
    // Leader tone is a stream of '1' bits.
    receiveBit(bit) {
        bit |= 0;
        this.latchc = (this.latchc & 0xdf) | (bit << 5);
        this.recalculatePortCPins();
    }
 
    // nothing on ATOM
    receive(/*_byte*/) {}
 
    /** A tape put in starts stopped, whatever the last one was doing. */
    setTape(tape) {
        this.stopTape();
        this.tape = tape;
    }
 
    rewindTape() {
        this.stopTape();
        Eif (this.tape) this.tape.rewind();
    }
 
    /** On the Atom the recorder's PLAY is the motor: there is no relay for it to work against. */
    get playPressed() {
        return this.motorOn;
    }
 
    get tapeRunning() {
        return this.motorOn;
    }
 
    pressPlay() {
        this.playTape();
    }
 
    pressStop() {
        this.stopTape();
    }
 
    playTape() {
        Eif (this.tape) {
            this.motorOn = true;
            this.runTape();
        }
    }
 
    stopTape() {
        this.motorOn = false;
        if (this.tape) {
            const toneGen = this.cpu.soundChip.toneGenerator;
            toneGen.mute();
            this.runTapeTask.cancel();
            this.setTapeCarrier(false);
        }
    }
 
    /** Polls the tape and books the next poll; the Atom's PLAY is its motor, so a tape that runs out stops. */
    runTape() {
        Iif (!this.tape) return;
        const delay = this.tape.poll(this);
        if (delay === undefined) this.stopTape();
        else this.runTapeTask.reschedule(delay);
    }
 
    updateIrq() {}
 
    snapshotState() {
        return {
            latcha: this.latcha,
            latchb: this.latchb,
            latchc: this.latchc,
            portapins: this.portapins,
            portbpins: this.portbpins,
            portcpins: this.portcpins,
            creg: this.creg,
            keys: this.keys.map((row) => Array.from(row)),
            keyboardEnabled: this.keyboardEnabled,
            lastSpeakerBit: this.lastSpeakerBit,
        };
    }
 
    restoreState(state) {
        this.latcha = state.latcha;
        this.latchb = state.latchb;
        this.latchc = state.latchc;
        this.portapins = state.portapins;
        this.portbpins = state.portbpins;
        this.portcpins = state.portcpins;
        this.creg = state.creg;
        Eif (state.keys) {
            for (let i = 0; i < state.keys.length; i++) {
                this.keys[i].set(state.keys[i]);
            }
        }
        this.keyboardEnabled = state.keyboardEnabled;
        this.lastSpeakerBit = state.lastSpeakerBit;
    }
}