All files / src ppia.js

96.29% Statements 130/135
83.33% Branches 30/36
76.92% Functions 30/39
97.56% Lines 120/123

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                                                                                138x 138x 138x 138x       58x   58x 58x 58x 58x 58x 58x 58x         66x                         253x   125x     128x   253x                                       176190x     176190x 176189x   176167x 176167x 176167x       1x     9x   9x 9x   12x       12x           9x 9x 9x   8x 8x   1x 1x   9x 9x 9x           205468x 205467x   92287x 92287x   83903x 83903x 83903x 839030x   83903x 83903x 83903x     29276x     29276x       29276x     29276x 29276x               29276x     1x         268454x 268454x 268454x       29549x 29549x 29549x                   138x             58x   58x 58x         58x 58x 58x   58x   58x       66x 66x       58x                   268454x       29549x 29549x 16x 16x         268454x                                                   2x 2x 2x               7x 7x       1x 1x         2x       3x       3x       1x       5x 5x 5x 5x         10x 10x 10x 5x 5x 5x           15x         6x 6x 6x 5x           12x             192x           8x 8x 8x 8x 8x 8x 8x 8x 128x     8x     8x      
import { ATOM, getKeyMapAtom } from "./keymap-atom.js";
import { WithKeyMatrix } from "./keymap.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
//        ~b1 :                 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.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.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, as port directions are fixed at what the Atom ROM's
                // reset selects (#1436).
                // D7=0 is Bit Set/Reset (BSR) for individual port C output bits.
                if (val & 0x80) break;
 
                // 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: {
                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();
    }
 
    recalculatePortCPins() {
        this.portcpins = this.latchc;
        this.drivePortC();
        this.portCUpdated();
    }
 
    // No-op hooks for subclass override
    drivePortA() {}
    drivePortC() {}
    portAUpdated() {}
    portCUpdated() {}
}
 
const AtomKeyColumns = 10;
 
// 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 WithKeyMatrix(PPIA) {
    constructor(cpu, initialLayout, scheduler) {
        super(cpu);
 
        this._initKeyMatrix(ATOM.SHIFT, AtomKeyColumns);
        this.setKeyLayout(initialLayout);
        // 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.motorOn = false;
 
        this.reset();
 
        this.runTapeTask = scheduler.newTask(() => this.runTape());
    }
 
    reset() {
        super.reset();
        this.lastSpeakerBit = (this.latchc & 0x04) >>> 2;
    }
 
    setKeyLayout(map) {
        this.keycodeToRowCol = getKeyMapAtom(map);
    }
 
    // nothing on ATOM
    updateKeys() {}
 
    // nothing on ATOM
    polltime() {}
 
    portAUpdated() {
        this.updateKeys();
    }
 
    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();
    }
 
    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);
    }
 
    // The Atom has no carrier detect.
    setTapeCarrier() {}
 
    // 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.refuseReplaysIfMotorChanges(true);
            this.motorOn = true;
            this.runTape();
        }
    }
 
    stopTape() {
        this.refuseReplaysIfMotorChanges(false);
        this.motorOn = false;
        if (this.tape) {
            const toneGen = this.cpu.soundChip.toneGenerator;
            toneGen.mute();
            this.runTapeTask.cancel();
        }
    }
 
    // The tape is not in a snapshot, and a replay would read on from where it is now.
    refuseReplaysIfMotorChanges(on) {
        if (on !== this.motorOn) this.inputLog?.refuse("the cassette motor starting or stopping");
    }
 
    /** 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,
            latchc: this.latchc,
            portapins: this.portapins,
            portbpins: this.portbpins,
            portcpins: this.portcpins,
            creg: this.creg,
            keys: this.keys.map((row) => Array.from(row)),
            lastSpeakerBit: this.lastSpeakerBit,
        };
    }
 
    restoreState(state) {
        this.latcha = state.latcha;
        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.lastSpeakerBit = state.lastSpeakerBit;
        // Which held key was forcing shift is not saved, so the matrix's shift is taken as the shift key's.
        const [shiftCol, shiftRow] = ATOM.SHIFT;
        this.shift.restore({ physicalShiftDown: !!this.keys[shiftCol][shiftRow], shiftOverrideActive: false });
    }
}