From fe0b1c4bdccec7e10c1a43b3eb0a0c26d9b7fec4 Mon Sep 17 00:00:00 2001 From: Apexo Date: Thu, 2 Apr 2026 21:09:27 +0200 Subject: move sleepUntil handling to main loop This should fix the issue of wakups that are triggered when the step() functions has reached its target cycle. --- src/lights/d3aa.ts | 10 ++++++---- src/peripherals/avrdx-slpctrl.ts | 6 ------ 2 files changed, 6 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lights/d3aa.ts b/src/lights/d3aa.ts index 21eea86..80bc134 100644 --- a/src/lights/d3aa.ts +++ b/src/lights/d3aa.ts @@ -173,16 +173,18 @@ export class D3AA { while (this.cpu.cycles < target) { this.cpu.tick(); + if (this.slpctrl.sleeping !== null) { + const sleepUntil = (this.cpu as any).nextClockEvent?.cycles ?? Infinity; + this.cpu.cycles = Math.min(target, sleepUntil); + continue; + } + 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 && this.cpu.nextInterrupt < 0) { - this.cpu.cycles = Math.min(this.slpctrl.sleepUntil, target); - } } } diff --git a/src/peripherals/avrdx-slpctrl.ts b/src/peripherals/avrdx-slpctrl.ts index d592593..730d128 100644 --- a/src/peripherals/avrdx-slpctrl.ts +++ b/src/peripherals/avrdx-slpctrl.ts @@ -15,7 +15,6 @@ export type SMODE = typeof SMODE_IDLE_gc | typeof SMODE_STDBY_gc | typeof SMODE_ export type SleepCallback = (mode: SMODE) => void; export class AVRDxSLPCTRL { - sleepUntil: number = 0; sleeping: null | SMODE = null; callbacks: SleepCallback[]; @@ -45,11 +44,6 @@ export class AVRDxSLPCTRL { const mode = ctrla & SMODE_gm; this.sleeping = mode as SMODE; - const nextEvent = (this.cpu as any).nextClockEvent; - if (nextEvent) { - this.sleepUntil = nextEvent.cycles; - } - for (const cb of this.callbacks) { cb(mode as SMODE); } -- cgit v1.2.3