aboutsummaryrefslogtreecommitdiff
path: root/demo/src/execute.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--demo/src/execute.ts28
1 files changed, 12 insertions, 16 deletions
diff --git a/demo/src/execute.ts b/demo/src/execute.ts
index 7b6cae0..ee5278e 100644
--- a/demo/src/execute.ts
+++ b/demo/src/execute.ts
@@ -11,6 +11,7 @@ import {
usart0Config
} from 'avr8js';
import { loadHex } from './intelhex';
+import { MicroTaskScheduler } from './task-scheduler';
// ATmega328p params
const FLASH = 0x8000;
@@ -24,8 +25,8 @@ export class AVRRunner {
readonly portD: AVRIOPort;
readonly usart: AVRUSART;
readonly speed = 16e6; // 16 MHZ
-
- private stopped = false;
+ readonly workUnitCycles = 500000;
+ readonly taskScheduler = new MicroTaskScheduler();
constructor(hex: string) {
loadHex(hex, new Uint8Array(this.program.buffer));
@@ -35,28 +36,23 @@ export class AVRRunner {
this.portC = new AVRIOPort(this.cpu, portCConfig);
this.portD = new AVRIOPort(this.cpu, portDConfig);
this.usart = new AVRUSART(this.cpu, usart0Config, this.speed);
+ this.taskScheduler.start();
}
- async execute(callback: (cpu: CPU) => void) {
- this.stopped = false;
- const workUnitCycles = 500000;
- let nextTick = this.cpu.cycles + workUnitCycles;
- for (;;) {
+ // CPU main loop
+ execute(callback: (cpu: CPU) => void) {
+ const cyclesToRun = this.cpu.cycles + this.workUnitCycles;
+ while (this.cpu.cycles < cyclesToRun) {
avrInstruction(this.cpu);
this.timer.tick();
this.usart.tick();
- if (this.cpu.cycles >= nextTick) {
- callback(this.cpu);
- await new Promise((resolve) => setTimeout(resolve, 0));
- if (this.stopped) {
- break;
- }
- nextTick += workUnitCycles;
- }
}
+
+ callback(this.cpu);
+ this.taskScheduler.postTask(() => this.execute(callback));
}
stop() {
- this.stopped = true;
+ this.taskScheduler.stop();
}
}