aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/cpu.ts
diff options
context:
space:
mode:
authorUri Shaked2020-03-22 21:12:03 +0200
committerGitHub2020-03-22 21:12:03 +0200
commitb3dc0329a8a7bfb8350ddc71719d4348dd4ed66c (patch)
treee85d52944ce76ac604e276f25859a26d38a0afd0 /src/cpu/cpu.ts
parentMerge pull request #25 from LironHazan/AVR8JS-24-editor-user-history (diff)
parentrefactor: added peripherals and cpu feature folders (diff)
downloadavr8js-b3dc0329a8a7bfb8350ddc71719d4348dd4ed66c.tar.gz
avr8js-b3dc0329a8a7bfb8350ddc71719d4348dd4ed66c.tar.bz2
avr8js-b3dc0329a8a7bfb8350ddc71719d4348dd4ed66c.zip
Merge pull request #22 from LironHazan/AVR8JS-21-restructure-project
refactor: add peripherals and cpu feature folders
Diffstat (limited to 'src/cpu/cpu.ts')
-rw-r--r--src/cpu/cpu.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/cpu/cpu.ts b/src/cpu/cpu.ts
new file mode 100644
index 0000000..93f79d0
--- /dev/null
+++ b/src/cpu/cpu.ts
@@ -0,0 +1,78 @@
+/**
+ * AVR 8 CPU data structures
+ * Part of AVR8js
+ *
+ * Copyright (C) 2019, Uri Shaked
+ */
+
+import { u16, u8 } from '../types';
+
+const registerSpace = 0x100;
+
+// eslint-disable-next-line @typescript-eslint/interface-name-prefix
+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 CPUMemoryHook = (value: u8, oldValue: u8, addr: u16) => boolean | void;
+export interface CPUMemoryHooks {
+ [key: number]: CPUMemoryHook;
+}
+
+export class CPU implements ICPU {
+ readonly data: Uint8Array = new Uint8Array(this.sramBytes + registerSpace);
+ readonly data16 = new Uint16Array(this.data.buffer);
+ readonly dataView = new DataView(this.data.buffer);
+ readonly progBytes = new Uint8Array(this.progMem.buffer);
+ readonly writeHooks: CPUMemoryHooks = [];
+
+ pc = 0;
+ cycles = 0;
+
+ constructor(public progMem: Uint16Array, private sramBytes = 8192) {
+ this.reset();
+ }
+
+ reset() {
+ this.data.fill(0);
+ this.SP = this.data.length - 1;
+ }
+
+ readData(addr: number) {
+ return this.data[addr];
+ }
+
+ writeData(addr: number, value: number) {
+ const hook = this.writeHooks[addr];
+ if (hook) {
+ if (hook(value, this.data[addr], addr)) {
+ return;
+ }
+ }
+ this.data[addr] = value;
+ }
+
+ get SP() {
+ return this.dataView.getUint16(93, true);
+ }
+
+ set SP(value: number) {
+ this.dataView.setUint16(93, value, true);
+ }
+
+ get SREG() {
+ return this.data[95];
+ }
+
+ get interruptsEnabled() {
+ return this.SREG & 0x80 ? true : false;
+ }
+}