summaryrefslogtreecommitdiff
path: root/src/peripherals/avrdx-slpctrl.ts
diff options
context:
space:
mode:
authorApexo2026-03-28 23:40:53 +0100
committerApexo2026-03-28 23:40:53 +0100
commit1b194ac4578dea8e71b0d61d1cb4d875f435ba71 (patch)
tree786019a0c6f34b458f3272bf2ecbde0de1976e0a /src/peripherals/avrdx-slpctrl.ts
downloadanduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.tar.gz
anduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.tar.bz2
anduril-sim-1b194ac4578dea8e71b0d61d1cb4d875f435ba71.zip
D3AA simulator
Diffstat (limited to '')
-rw-r--r--src/peripherals/avrdx-slpctrl.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/peripherals/avrdx-slpctrl.ts b/src/peripherals/avrdx-slpctrl.ts
new file mode 100644
index 0000000..93cae58
--- /dev/null
+++ b/src/peripherals/avrdx-slpctrl.ts
@@ -0,0 +1,35 @@
+// AVR-Dx SLPCTRL - Sleep Controller
+// Handles the SLEEP instruction by fast-forwarding to the next clock event.
+
+import type { CPU } from 'avr8js/cpu/cpu';
+
+const CTRLA = 0;
+const SEN_bm = 0x01;
+// const SMODE_gm = 0x06;
+
+export class AVRDxSLPCTRL {
+ sleepUntil: number = 0;
+
+ constructor(private cpu: CPU, private base: number) {
+ // CTRLA - sleep mode + sleep enable
+ cpu.writeHooks[base + CTRLA] = (value) => {
+ cpu.data[base + CTRLA] = value;
+ return true;
+ };
+
+ // Hook the SLEEP instruction
+ cpu.onSleep = () => {
+ this.handleSleep();
+ };
+ }
+
+ private handleSleep() {
+ const ctrla = this.cpu.data[this.base + CTRLA];
+ if (!(ctrla & SEN_bm)) return; // sleep not enabled
+
+ const nextEvent = (this.cpu as any).nextClockEvent;
+ if (nextEvent) {
+ this.sleepUntil = Math.min(this.sleepUntil, nextEvent.cycles);
+ }
+ }
+}