aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUri Shaked2021-09-10 21:16:34 +0300
committerUri Shaked2021-09-10 21:16:34 +0300
commit96a6dba4371a2e319a47335b2bf366b695b6c17a (patch)
tree1664c852cc86826a9fd6795a6512c6d8fa92a686
parentfeat(adc): ADC peripheral #13 (diff)
downloadavr8js-96a6dba4371a2e319a47335b2bf366b695b6c17a.tar.gz
avr8js-96a6dba4371a2e319a47335b2bf366b695b6c17a.tar.bz2
avr8js-96a6dba4371a2e319a47335b2bf366b695b6c17a.zip
refactor: remove the ICPU interface
Removing the interface simplifies the code
-rw-r--r--benchmark/convert-instructions.ts6
-rw-r--r--benchmark/index.ts4
-rw-r--r--demo/src/cpu-performance.ts4
-rw-r--r--src/cpu/cpu.ts43
-rw-r--r--src/cpu/instruction.ts4
-rw-r--r--src/cpu/interrupt.ts4
-rw-r--r--src/index.ts2
7 files changed, 26 insertions, 41 deletions
diff --git a/benchmark/convert-instructions.ts b/benchmark/convert-instructions.ts
index edde3cc..6ae0c24 100644
--- a/benchmark/convert-instructions.ts
+++ b/benchmark/convert-instructions.ts
@@ -8,7 +8,7 @@ let fnBody = '';
let currentInstruction = '';
let pattern = '';
let output = `
-import { ICPU } from '../src/cpu/cpu';
+import { CPU } from '../src/cpu/cpu';
function isTwoWordInstruction(opcode: number) {
return (
@@ -36,7 +36,7 @@ for (const line of input.split('\n')) {
patternToFn.push([pattern.trim(), fnName]);
} else if (line.startsWith(' }')) {
output += `
- export function ${fnName}(cpu: ICPU, opcode: number) {
+ export function ${fnName}(cpu: CPU, opcode: number) {
/*${pattern}*/
${fnBody}
cpu.cycles++;
@@ -62,7 +62,7 @@ for (const [fnPattern, fn] of patternToFn) {
output += ']';
output += `\n
-export function executeInstruction(idx: number, cpu: ICPU, opcode: number) {
+export function executeInstruction(idx: number, cpu: CPU, opcode: number) {
switch (idx) {
${executeInstructionCases}
default: instNOP(cpu, opcode);
diff --git a/benchmark/index.ts b/benchmark/index.ts
index e290352..a1ee7e7 100644
--- a/benchmark/index.ts
+++ b/benchmark/index.ts
@@ -1,4 +1,4 @@
-import { CPU, ICPU } from '../src/cpu/cpu';
+import { CPU } from '../src/cpu/cpu';
import { avrInstruction } from '../src/cpu/instruction';
import { createBenchmark } from './benchmark';
import { permutations } from './permutations';
@@ -21,7 +21,7 @@ function avrInstructionUintArray(cpu: CPU) {
}
/* Approach 2: use instMap */
-const instructionMap: { [key: number]: (cpu: ICPU, opcode: number) => void } = {};
+const instructionMap: { [key: number]: (cpu: CPU, opcode: number) => void } = {};
for (const { pattern, fn } of instructions) {
for (const opcode of permutations(pattern.replace(/ /g, '').substr(0, 16))) {
if (!instructionMap[opcode]) {
diff --git a/demo/src/cpu-performance.ts b/demo/src/cpu-performance.ts
index f61d038..a4afc73 100644
--- a/demo/src/cpu-performance.ts
+++ b/demo/src/cpu-performance.ts
@@ -1,4 +1,4 @@
-import { ICPU } from 'avr8js';
+import { CPU } from 'avr8js';
export class CPUPerformance {
private prevTime = 0;
@@ -6,7 +6,7 @@ export class CPUPerformance {
private samples = new Float32Array(64);
private sampleIndex = 0;
- constructor(private cpu: ICPU, private MHZ: number) {}
+ constructor(private cpu: CPU, private MHZ: number) {}
reset() {
this.prevTime = 0;
diff --git a/src/cpu/cpu.ts b/src/cpu/cpu.ts
index 7a518b8..2675d1d 100644
--- a/src/cpu/cpu.ts
+++ b/src/cpu/cpu.ts
@@ -11,33 +11,6 @@ import { avrInterrupt } from './interrupt';
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;
-
- /**
- * Whether the program counter (PC) can address 22 bits (the default is 16)
- */
- readonly pc22Bits: boolean;
-
- /**
- * Program counter
- */
- pc: u32;
-
- /**
- * Clock cycle counter
- */
- cycles: number;
-
- readData(addr: u16): u8;
- writeData(addr: u16, value: u8, mask?: u8): void;
- onWatchdogReset(): void;
-}
-
export type CPUMemoryHook = (value: u8, oldValue: u8, addr: u16, mask: u8) => boolean | void;
export interface CPUMemoryHooks {
[key: number]: CPUMemoryHook;
@@ -66,7 +39,7 @@ interface AVRClockEventEntry {
next: AVRClockEventEntry | null;
}
-export class CPU implements ICPU {
+export class CPU {
readonly data: Uint8Array = new Uint8Array(this.sramBytes + registerSpace);
readonly data16 = new Uint16Array(this.data.buffer);
readonly dataView = new DataView(this.data.buffer);
@@ -76,6 +49,10 @@ export class CPU implements ICPU {
private readonly pendingInterrupts: AVRInterruptConfig[] = [];
private nextClockEvent: AVRClockEventEntry | null = null;
private readonly clockEventPool: AVRClockEventEntry[] = []; // helps avoid garbage collection
+
+ /**
+ * Whether the program counter (PC) can address 22 bits (the default is 16)
+ */
readonly pc22Bits = this.progBytes.length > 0x20000;
readonly gpioPorts = new Set<AVRIOPort>();
@@ -89,8 +66,16 @@ export class CPU implements ICPU {
/* empty by default */
};
+ /**
+ * Program counter
+ */
pc: u32 = 0;
- cycles: u32 = 0;
+
+ /**
+ * Clock cycle counter
+ */
+ cycles = 0;
+
nextInterrupt: i16 = -1;
constructor(public progMem: Uint16Array, private sramBytes = 8192) {
diff --git a/src/cpu/instruction.ts b/src/cpu/instruction.ts
index 9937ec9..e2bed5d 100644
--- a/src/cpu/instruction.ts
+++ b/src/cpu/instruction.ts
@@ -10,7 +10,7 @@
* Copyright (C) 2019, 2020 Uri Shaked
*/
-import { ICPU } from './cpu';
+import { CPU } from './cpu';
import { u16 } from '../types';
function isTwoWordInstruction(opcode: u16) {
@@ -26,7 +26,7 @@ function isTwoWordInstruction(opcode: u16) {
);
}
-export function avrInstruction(cpu: ICPU) {
+export function avrInstruction(cpu: CPU) {
const opcode = cpu.progMem[cpu.pc];
if ((opcode & 0xfc00) === 0x1c00) {
diff --git a/src/cpu/interrupt.ts b/src/cpu/interrupt.ts
index 1bfa035..2e4ceb9 100644
--- a/src/cpu/interrupt.ts
+++ b/src/cpu/interrupt.ts
@@ -6,9 +6,9 @@
* Copyright (C) 2019, Uri Shaked
*/
-import { ICPU } from './cpu';
+import { CPU } from './cpu';
-export function avrInterrupt(cpu: ICPU, addr: number) {
+export function avrInterrupt(cpu: CPU, addr: number) {
const sp = cpu.dataView.getUint16(93, true);
cpu.data[sp] = cpu.pc & 0xff;
cpu.data[sp - 1] = (cpu.pc >> 8) & 0xff;
diff --git a/src/index.ts b/src/index.ts
index 15cc3fa..c1089aa 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -4,7 +4,7 @@
* Copyright (C) 2019, 2020, Uri Shaked
*/
-export { CPU, ICPU, CPUMemoryHook, CPUMemoryHooks } from './cpu/cpu';
+export { CPU, CPUMemoryHook, CPUMemoryHooks } from './cpu/cpu';
export { avrInstruction } from './cpu/instruction';
export { avrInterrupt } from './cpu/interrupt';
export {
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.