From 3a0741213e573f96d828c2c044caff23d41f2a9e Mon Sep 17 00:00:00 2001 From: Apexo Date: Wed, 1 Apr 2026 16:30:11 +0200 Subject: improve main loop - call cpu.tick() first, when we enter the loop; external events (which may cause an interrupt) can only happen when the loop is not running, so it makes sense to first check for interrupts before executing other instructions - don't go to sleep if an interrupt is pending --- src/lights/d3aa.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lights/d3aa.ts b/src/lights/d3aa.ts index 9b8b0ba..29dc249 100644 --- a/src/lights/d3aa.ts +++ b/src/lights/d3aa.ts @@ -169,17 +169,20 @@ export class D3AA { /** Run the CPU for the given number of cycles */ step(cycles: number) { const target = this.cpu.cycles + cycles; + while (this.cpu.cycles < target) { + this.cpu.tick(); + const before = this.cpu.cycles; + this.slpctrl.sleepUntil = 0; avrInstruction(this.cpu); const mult = this.clkctrl.cycleMultiplier; if (mult > 1) { this.cpu.cycles += (this.cpu.cycles - before) * (mult - 1); } - if (this.slpctrl.sleepUntil > this.cpu.cycles) { + if (this.slpctrl.sleepUntil > this.cpu.cycles && this.cpu.nextInterrupt < 0) { this.cpu.cycles = Math.min(this.slpctrl.sleepUntil, target); } - this.cpu.tick(); } } -- cgit v1.2.3