aboutsummaryrefslogtreecommitdiff
path: root/src/cpu.ts
diff options
context:
space:
mode:
authorUri Shaked2019-11-19 14:35:39 +0200
committerUri Shaked2019-11-19 14:35:39 +0200
commita47e21cf3f5c2968eb2afd0ae9cd1453d0011ed8 (patch)
treece8a9be31e6898f3dc456dd1e49a8c8558b1b519 /src/cpu.ts
parentInitial commit - project skeleton (diff)
downloadavr8js-a47e21cf3f5c2968eb2afd0ae9cd1453d0011ed8.tar.gz
avr8js-a47e21cf3f5c2968eb2afd0ae9cd1453d0011ed8.tar.bz2
avr8js-a47e21cf3f5c2968eb2afd0ae9cd1453d0011ed8.zip
feat: implement some AVR instructions + tests
Diffstat (limited to '')
-rw-r--r--src/cpu.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/cpu.ts b/src/cpu.ts
new file mode 100644
index 0000000..a6e0ab6
--- /dev/null
+++ b/src/cpu.ts
@@ -0,0 +1,43 @@
+import { u16, u8 } from './types';
+
+export interface ICPU {
+ readonly data: Uint8Array;
+ readonly dataView: DataView;
+ readonly progMem: Uint16Array;
+ readonly progBytes: Uint8Array;
+ pc: u16;
+ cycles: number;
+
+ readData(addr: u16): u8;
+ writeData(addr: u16, value: u8): void;
+}
+
+export type ICPUMemoryHook = (value: u8, oldValue: u8, addr: u16) => void;
+export interface ICPUMemoryHooks {
+ [key: number]: ICPUMemoryHook;
+}
+
+export class CPU implements ICPU {
+ readonly data = new Uint8Array(16384);
+ readonly data16 = new Uint16Array(this.data.buffer);
+ readonly dataView = new DataView(this.data.buffer);
+ readonly progBytes = new Uint8Array(this.progMem.buffer);
+ readonly writeHooks: ICPUMemoryHooks = [];
+
+ pc = 0;
+ cycles = 0;
+
+ constructor(public progMem: Uint16Array) {}
+
+ readData(addr: number) {
+ return this.data[addr];
+ }
+
+ writeData(addr: number, value: number) {
+ const hook = this.writeHooks[addr];
+ if (hook) {
+ hook(value, this.data[addr], addr);
+ }
+ this.data[addr] = value;
+ }
+}