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 | 2x 2x 2x 2x 2x 2x 31x 31x 31x 31x 31x 31x 31x 282x 282x 282x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 200x 21x 30x 30x 1x 2x 7x 7x 7x 91x 91x 91x 91x 91x 90x 87x 86x 86x 86x 86x 66x 66x 66x 66x 86x 86x 86x 2x 393x 393x 305x 393x 393x 304x 304x 304x 90x 90x 5x 4x 4x 4x 4x 311x 308x 1x 1x 307x 307x 307x 307x 308x 309x 309x 309x 309x 309x 282x 279x 279x 279x 3x 282x 282x 282x 1x 282x 282x 282x 282x 282x 282x 282x 2x 2x 2x 282x 282x 1x 309x 309x 4x 3x 3x 3x 2x 2x 1x 200x 200x 114x 114x 86x 81x 81x | import { noteEvent } from "./analytics.js";
// The machine runs in short slices of real time on a timer, whatever the
// display is doing (issue #885). Audio gains most from the fine grain: its
// queue need only cover a slice.
const TickMs = 10;
export const RewindCaptureInterval = 50; // emulated frames, ~1 second
// Under ?audioDebug, one console line per second in which the emulator sat
// idle between ticks or a tick ran long, or the audio queue underran or
// dropped, so a click can be matched to a cause. The sound chip posts samples
// throughout execute(), so only the idle time starves the audio queue.
const AudioDebugLogIntervalMs = 1000;
const AudioDebugSlowTickMs = 30;
const AudioDebugSlowPresentMs = 30;
const VirtualMhzUpdateMs = 3333;
class VirtualSpeedUpdater {
constructor(cpuSpeed) {
this.cpuSpeed = cpuSpeed;
this.cycles = 0;
this.time = 0;
this.v = document.querySelector(".virtualMHz");
this.header = document.getElementById("virtual-mhz-header");
this.speedy = false;
this.display();
}
update(cycles, time, speedy) {
this.cycles += cycles;
this.time += time;
this.speedy = speedy;
}
display() {
// MRG would be nice to graph instantaneous speed to get some idea where the time goes.
Iif (this.cycles) {
const thisMHz = this.cycles / this.time / 1000;
this.v.textContent = thisMHz.toFixed(1);
if (this.cycles >= 10 * this.cpuSpeed) {
this.cycles = this.time = 0;
}
this.header.style.color = this.speedy ? "red" : "white";
}
setTimeout(() => this.display(), VirtualMhzUpdateMs);
}
}
/**
* Runs the machine in real time: the tick that turns wall-clock time into
* cycles, starting and stopping, the audio lead, fast-forward and the speed
* readout. Owns `running`, and dispatches a "running" event whenever it
* changes hands, "tick" on every timer tick and on every vsync that runs the
* machine (the first tick only takes the time), and "rewind-capture" every
* RewindCaptureInterval frames for whoever keeps the rewind history. Anything
* that needs the machine held still while it works (a dialog, a snapshot, the
* rewind panel, a hidden tab) takes a `pause()`; the loop runs again once every
* hold has let go, provided the user still wants it running.
*/
export class EmulationLoop extends EventTarget {
constructor({
processor,
display,
audioHandler,
dbgr,
gamepad,
keyboard,
clocksPerSecond,
cpuSpeed,
fastTape,
audioStatsNode,
}) {
super();
this.processor = processor;
this.display = display;
this.audioHandler = audioHandler;
this.dbgr = dbgr;
this.gamepad = gamepad;
this.keyboard = keyboard;
this.clocksPerSecond = clocksPerSecond;
this.maxCyclesPerTick = clocksPerSecond / 10;
this.rewindCaptureCycles = (RewindCaptureInterval * clocksPerSecond) / 50;
this.fastTape = fastTape;
this.audioStatsNode = audioStatsNode;
this.running = false;
this.fastAsPossible = false;
this.emulatedTo = 0;
this.lastEnd = 0;
this.nextTickDue = 0;
this.tickToken = null;
this.vsyncToken = null;
this.emulationLeadMs = 0;
this.rewindCycleCounter = 0;
this.wanted = false;
this.holds = new Set();
this.resumeOnVisible = null;
this.virtualSpeedUpdater = new VirtualSpeedUpdater(cpuSpeed);
this.audioDebugLog = { start: 0, ticks: 0, cycles: 0, maxIdle: 0, maxExecute: 0, maxPaint: 0, maxSnapshot: 0 };
document.addEventListener("visibilitychange", () => this.handleVisibilityChange(), false);
}
isRunning() {
return this.running;
}
go() {
this.wanted = true;
if (this.holds.size === 0) this.start();
else
console.warn(
`Emulator held by ${[...this.holds].map((hold) => hold.reason).join(", ")}; it runs when they let go`,
);
}
stop(debug) {
this.wanted = false;
this.halt();
if (debug) this.dbgr.debug(this.processor.pc);
}
/**
* Holds the machine stopped until the returned function is called. Holds
* nest, and letting go twice counts once. `reason` names the holder when a
* go() has to wait, so a hold that is never released can be found.
*/
pause(reason) {
if (this.holds.size === 0 && this.running) this.halt();
const hold = { reason };
this.holds.add(hold);
return () => {
if (!this.holds.delete(hold)) return;
if (this.holds.size === 0 && this.wanted) this.start();
};
}
start() {
if (this.running) return;
this.audioHandler.unmute();
this.running = true;
this.dispatchEvent(new Event("running"));
this.run();
}
halt() {
this.running = false;
this.dispatchEvent(new Event("running"));
this.processor.stop();
this.audioHandler.mute();
}
run() {
this.nextTickDue = 0;
this.scheduleTick(0);
this.scheduleVsyncTick();
}
toggleFastAsPossible() {
this.fastAsPossible = !this.fastAsPossible;
}
// A user-blocking task runs ahead of rendering and ordinary timers, so a stuck
// compositor does not hold the tick off too.
scheduleTick(delayMs) {
const token = (this.tickToken = {});
const fire = () => {
Eif (this.tickToken === token) this.tick();
};
Iif (window.scheduler?.postTask) window.scheduler.postTask(fire, { delay: delayMs, priority: "user-blocking" });
else window.setTimeout(fire, delayMs);
}
// Booked from the previous due time, not from now, so the period averages
// TickMs and a flyback lands at the same point of a tick every frame. A tick
// a whole period late starts afresh rather than a burst of catch-up ticks.
nextTickDelay(now) {
if (now - this.nextTickDue > TickMs) this.nextTickDue = now;
this.nextTickDue += TickMs;
return this.nextTickDue - now;
}
scheduleVsyncTick() {
const token = (this.vsyncToken = {});
window.requestAnimationFrame((vsyncTime) => {
if (this.vsyncToken !== token || !this.running) return;
this.scheduleVsyncTick();
this.vsyncTick(vsyncTime);
});
}
// Emulates up to the vsync and presents at once, so a flyback is shown at the
// first vsync after it; two share a refresh only if a timer tick reached the
// next flyback before this callback ran.
vsyncTick(vsyncTime) {
// While speedy the timer ticks run flat out.
if (this.emulatedTo !== 0 && !this.isSpeedy() && vsyncTime > this.emulatedTo) this.advance(vsyncTime, false);
this.display.present(vsyncTime);
}
isSpeedy() {
return this.fastAsPossible || (this.fastTape && this.processor.tapeInterface.motorOn);
}
tick() {
if (!this.running) {
this.emulatedTo = 0;
return;
}
const now = performance.now();
const speedy = this.isSpeedy();
this.display.setSpeedy(speedy);
this.scheduleTick(speedy ? 0 : this.nextTickDelay(now));
this.advance(now, speedy);
}
advance(now, speedy) {
// now can be a vsync timestamp, before this ran; the timings use start.
const start = performance.now();
const { processor, display, audioHandler } = this;
this.gamepad.update(processor.sysvia);
this.dispatchEvent(new Event("tick"));
if (this.emulatedTo !== 0) {
let cycles;
if (!speedy) {
const sinceLast = Math.max(0, now - this.emulatedTo);
cycles = (sinceLast * this.clocksPerSecond) / 1000;
cycles = Math.min(cycles, this.maxCyclesPerTick);
} else {
cycles = this.clocksPerSecond / 50;
}
cycles |= 0;
try {
if (!processor.execute(cycles)) {
this.stop(true);
}
audioHandler.flushChipEvents();
const end = performance.now();
this.virtualSpeedUpdater.update(cycles, end - start, speedy);
const paintMs = display.takePaintMs();
let snapshotMs = 0;
this.rewindCycleCounter += cycles;
if (this.rewindCycleCounter >= this.rewindCaptureCycles) {
this.rewindCycleCounter -= this.rewindCaptureCycles;
this.dispatchEvent(new Event("rewind-capture"));
snapshotMs = performance.now() - end;
}
Iif (this.audioStatsNode)
this.logAudioDebugTick(
start,
cycles,
speedy ? 0 : start - this.lastEnd,
end - start,
paintMs,
snapshotMs,
);
} catch (e) {
this.running = false;
noteEvent("exception", "thrown", e.stack);
this.dbgr.debug(processor.pc);
throw e;
}
if (this.keyboard.postFrameShouldPause()) {
this.stop(false);
}
}
this.emulatedTo = Math.max(this.emulatedTo, now);
this.lastEnd = performance.now();
}
// A change of audio buffer depth is taken by the picture, not the sound:
// gaining lead emulates ahead at once; losing it moves `emulatedTo` forward
// so the ticks emulate nothing until the queue has drained by that much.
setEmulationLead(leadMs) {
if (!this.running) return;
const aheadMs = leadMs - this.emulationLeadMs;
this.emulationLeadMs = leadMs;
if (aheadMs > 0) {
Iif (!this.processor.execute((aheadMs * this.clocksPerSecond) / 1000)) this.stop(true);
this.audioHandler.flushChipEvents();
} else {
this.emulatedTo -= aheadMs;
}
}
handleVisibilityChange() {
const { processor } = this;
if (document.visibilityState === "hidden") {
const keepRunningWhenHidden =
processor.tapeInterface.motorOn || processor.fdc.motorOn[0] || processor.fdc.motorOn[1];
if (!keepRunningWhenHidden && !this.resumeOnVisible) this.resumeOnVisible = this.pause("the hidden tab");
} else if (this.resumeOnVisible) {
this.resumeOnVisible();
this.resumeOnVisible = null;
}
}
logAudioDebugTick(now, cycles, idleMs, executeMs, paintMs, snapshotMs) {
const log = this.audioDebugLog;
if (log.start === 0) log.start = now;
log.ticks++;
log.cycles += cycles;
log.maxIdle = Math.max(log.maxIdle, idleMs);
log.maxExecute = Math.max(log.maxExecute, executeMs);
log.maxPaint = Math.max(log.maxPaint, paintMs);
log.maxSnapshot = Math.max(log.maxSnapshot, snapshotMs);
if (now - log.start < AudioDebugLogIntervalMs) return;
const audio = this.audioHandler.takeEventCounts();
const present = this.display.takePresentMs();
const leadMin = Number.isFinite(audio.leadMinMs) ? `${audio.leadMinMs.toFixed(1)}ms` : "(no stats)";
if (
log.maxIdle > AudioDebugSlowTickMs ||
log.maxExecute > AudioDebugSlowTickMs ||
present > AudioDebugSlowPresentMs ||
audio.stall ||
audio.skip
) {
console.log(
`${(now / 1000).toFixed(0)}s: ${log.ticks} ticks emulating ${((1000 * log.cycles) / this.clocksPerSecond).toFixed(0)}ms, ` +
`idle max ${log.maxIdle.toFixed(0)}ms, ` +
`execute max ${log.maxExecute.toFixed(0)}ms (paint ${log.maxPaint.toFixed(1)}ms), ` +
`present max ${present.toFixed(0)}ms, snapshot ${log.maxSnapshot.toFixed(1)}ms; ` +
`audio lead min ${leadMin}, stalls ${audio.stall}, skipped ${audio.skip.toFixed(0)}ms`,
);
}
log.start = now;
log.ticks = log.cycles = log.maxIdle = log.maxExecute = log.maxPaint = log.maxSnapshot = 0;
}
benchmarkCpu(numCycles) {
numCycles = numCycles || 10 * 1000 * 1000;
const oldFS = this.display.frameSkip;
this.display.frameSkip = 1000000;
const startTime = performance.now();
this.processor.execute(numCycles);
const endTime = performance.now();
this.display.frameSkip = oldFS;
const msTaken = endTime - startTime;
const virtualMhz = numCycles / msTaken / 1000;
console.log("Took " + msTaken + "ms to execute " + numCycles + " cycles");
console.log("Virtual " + virtualMhz.toFixed(2) + "MHz");
}
benchmarkVideo(numCycles) {
numCycles = numCycles || 10 * 1000 * 1000;
const oldFS = this.display.frameSkip;
this.display.frameSkip = 1000000;
const startTime = performance.now();
this.display.video.polltime(numCycles);
const endTime = performance.now();
this.display.frameSkip = oldFS;
const msTaken = endTime - startTime;
const virtualMhz = numCycles / msTaken / 1000;
console.log("Took " + msTaken + "ms to execute " + numCycles + " video cycles");
console.log("Virtual " + virtualMhz.toFixed(2) + "MHz");
}
profileCpu(arg) {
console.profile("CPU");
this.benchmarkCpu(arg);
console.profileEnd();
}
profileVideo(arg) {
console.profile("Video");
this.benchmarkVideo(arg);
console.profileEnd();
}
}
|