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 | 15x 15x 151x 151x 151x 151x 62x 62x 62x 48x 46x 46x 46x 48x 17x 17x 17x 244x 101x 151x 8x 8x 9x 9x 8x 5x 8x 11x 1x 1x 9x 10x 8x 8x 8x 11x 11x 11x 5x 11x 3x 3x 159x 159x 159x 159x 159x 159x 159x 27x 27x 27x 3x 3x 2x 3x 2x 2x 27x 27x 27x 17x 8x 8x 10x 10x 10x 10x 278x 60x 55x 53x 53x 8x 8x 8x 45x 45x 3x 3x 3x 42x 42x 42x 42x 1x 1x 1x 41x 41x 1x 1x 40x 41x 41x | import { BBC } from "./keymap.js";
const RepeatedKeyGapMs = 30;
const StoppedPollMs = 100;
// Delays are counted in the scheduler's peripheral cycles, so they stay in emulated time whatever the CPU multiplier
// is. A hook between instructions makes the key change, and is attached only while there is text to type, so only
// a paste leaves the CPU's fast path.
function instructionTimer(processor, deliver) {
let dueEpoch = null;
let hook = null;
let loadsAtStart = 0;
const timer = {
start(ms) {
loadsAtStart = processor.resetsAndLoads;
dueEpoch = processor.scheduler.epoch + (ms * processor.peripheralCyclesPerSecond) / 1000;
hook ??= processor.debugInstruction.add(() => {
if (timer.stranded() || processor.scheduler.epoch >= dueEpoch) {
dueEpoch = null;
deliver();
if (dueEpoch === null) timer.cancel();
}
return false;
});
},
cancel() {
dueEpoch = null;
hook?.remove();
hook = null;
},
running: () => dueEpoch !== null,
stranded: () => processor.resetsAndLoads !== loadsAtStart,
};
return timer;
}
// Keys typed while the machine is stopped would all go in at the cycle it starts again, so a delay that comes due
// with the machine stopped starts again once it runs.
function wallClockTimer(callback, isRunning) {
let timeout = null;
const fire = () => {
timeout = null;
callback();
};
const waitForMachine = (ms) => {
timeout = setTimeout(() => (isRunning() ? timer.start(ms) : waitForMachine(ms)), StoppedPollMs);
};
const timer = {
start(ms) {
timeout = setTimeout(() => (isRunning() ? fire() : waitForMachine(ms)), ms);
},
cancel() {
clearTimeout(timeout);
timeout = null;
},
running: () => timeout !== null,
stranded: () => false,
};
return timer;
}
/**
* The keyboard as the typist drives it, with each key pressed and released through `input` (a Keyboard's) instead
* of in the matrix. Nothing is disabled: the machine's keys are not this page's to take.
*/
function keysThrough(input, keyInterface) {
const held = new Map();
return {
get capsLockLight() {
return keyInterface.capsLockLight;
},
get shiftLockLight() {
return keyInterface.shiftLockLight;
},
keyToggleRaw(key) {
const name = key.join();
const down = !held.has(name);
if (down) held.set(name, key);
else held.delete(name);
input.keyRaw(key, down);
},
disableKeyboard() {},
enableKeyboard() {
for (const key of held.values()) input.keyRaw(key, false);
held.clear();
},
};
}
/**
* Types a key sequence into the keyboard, one key per tick. An entry is one of the model's raw keys, or a number
* of milliseconds to wait. SHIFT toggles and stays held; any other key is pressed on one tick and released on the
* next.
*/
export class Typist {
/**
* @param {object} processor
* @param {{input: object, isRunning: function(): boolean}} [through] an input, as `Keyboard.setInput` takes, to
* press the keys through one by one as someone typing would, timed by the wall clock while `isRunning` says
* the machine runs; without one they go straight into the matrix, between the processor's instructions
*/
constructor(processor, through) {
this.processor = processor;
this.keyInterface = through
? keysThrough(through.input, processor.keyboardInterface)
: processor.keyboardInterface;
this._shiftKey = processor.model.keys.SHIFT;
this._keys = [];
this._lastKey = undefined;
const deliver = () => this._deliver();
this._timer = through ? wallClockTimer(deliver, through.isRunning) : instructionTimer(processor, deliver);
}
/**
* @param {Array} keys raw keys and millisecond delays, consumed as they are sent
* @param {boolean} checkCapsAndShiftLocks bracket the keys with a lock toggle when
* the machine's lock lights say the letters would come out in the wrong case
*/
type(keys, checkCapsAndShiftLocks) {
if (this.isTyping) this.cancel();
this.keyInterface.disableKeyboard();
if (checkCapsAndShiftLocks) {
let toggleKey = null;
if (!this.keyInterface.capsLockLight) toggleKey = BBC.CAPSLOCK;
else if (this.keyInterface.shiftLockLight) toggleKey = BBC.SHIFTLOCK;
if (toggleKey) {
keys.unshift(toggleKey);
keys.push(toggleKey);
}
}
this._keys = keys;
this._lastKey = undefined;
// The first press gets the release gap too, as the key before it may have gone up just now.
this._timer.start(this.processor.model.pasteReleaseGapMs);
}
cancel() {
if (!this.isTyping) return;
// A restored machine has its own keys, so one the typist pressed before is not down in it.
if (!this._timer.stranded()) this._releaseLastKey();
this._abandon();
}
_abandon() {
this._timer.cancel();
this._lastKey = undefined;
this._keys = [];
this.keyInterface.enableKeyboard();
}
get isTyping() {
return this._keys.length > 0 || this._timer.running();
}
_releaseLastKey() {
if (this._lastKey && this._lastKey !== this._shiftKey) this.keyInterface.keyToggleRaw(this._lastKey);
}
_deliver() {
if (this._timer.stranded()) return this._abandon();
this._releaseLastKey();
if (this._keys.length === 0) {
this._lastKey = undefined;
this.keyInterface.enableKeyboard();
return;
}
const releaseGapMs = this.processor.model.pasteReleaseGapMs;
if (this._lastKey && this._lastKey !== this._shiftKey && releaseGapMs) {
this._lastKey = undefined;
this._timer.start(releaseGapMs);
return;
}
const key = this._keys[0];
const repeated = this._lastKey === key;
this._lastKey = key;
if (repeated) {
this._lastKey = undefined;
this._timer.start(RepeatedKeyGapMs);
return;
}
let delayMs = this.processor.model.pasteKeyDelayMs;
if (typeof key === "number") {
delayMs = key;
this._lastKey = undefined;
} else {
this.keyInterface.keyToggleRaw(key);
}
this._keys.shift();
this._timer.start(delayMs);
}
}
|