aboutsummaryrefslogtreecommitdiff
path: root/demo/src/execute.ts
diff options
context:
space:
mode:
authorUri Shaked2019-11-21 19:40:02 +0200
committerUri Shaked2019-11-21 20:05:23 +0200
commitb9dfd552a62a46449532d49adc0773589076c808 (patch)
tree8eb1ec1f49e7b8e097a51ee6bf266a609eafac43 /demo/src/execute.ts
parentchore: release 0.2.0 (diff)
downloadavr8js-b9dfd552a62a46449532d49adc0773589076c808.tar.gz
avr8js-b9dfd552a62a46449532d49adc0773589076c808.tar.bz2
avr8js-b9dfd552a62a46449532d49adc0773589076c808.zip
feat: add blink demo
Diffstat (limited to '')
-rw-r--r--demo/src/execute.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/demo/src/execute.ts b/demo/src/execute.ts
new file mode 100644
index 0000000..96a0411
--- /dev/null
+++ b/demo/src/execute.ts
@@ -0,0 +1,38 @@
+import { avrInstruction, AVRTimer, CPU, timer0Config } from 'avr8js';
+import { loadHex } from './intelhex';
+
+// ATmega328p params
+const FLASH = 0x8000;
+
+export class AVRRunner {
+ readonly program = new Uint16Array(FLASH);
+ readonly cpu: CPU;
+ readonly timer: AVRTimer;
+
+ private stopped = false;
+
+ constructor(hex: string) {
+ loadHex(hex, new Uint8Array(this.program.buffer));
+ this.cpu = new CPU(this.program);
+ this.timer = new AVRTimer(this.cpu, timer0Config);
+ }
+
+ async execute(callback: (cpu: CPU) => void) {
+ this.stopped = false;
+ for (;;) {
+ avrInstruction(this.cpu);
+ this.timer.tick();
+ if (this.cpu.cycles % 50000 === 0) {
+ callback(this.cpu);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ if (this.stopped) {
+ break;
+ }
+ }
+ }
+ }
+
+ stop() {
+ this.stopped = true;
+ }
+}