Skip to content

Commit 96a6dba

Browse files
committed
refactor: remove the ICPU interface
Removing the interface simplifies the code
1 parent ec1346b commit 96a6dba

File tree

7 files changed

+26
-41
lines changed

7 files changed

+26
-41
lines changed

benchmark/convert-instructions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let fnBody = '';
88
let currentInstruction = '';
99
let pattern = '';
1010
let output = `
11-
import { ICPU } from '../src/cpu/cpu';
11+
import { CPU } from '../src/cpu/cpu';
1212
1313
function isTwoWordInstruction(opcode: number) {
1414
return (
@@ -36,7 +36,7 @@ for (const line of input.split('\n')) {
3636
patternToFn.push([pattern.trim(), fnName]);
3737
} else if (line.startsWith(' }')) {
3838
output += `
39-
export function ${fnName}(cpu: ICPU, opcode: number) {
39+
export function ${fnName}(cpu: CPU, opcode: number) {
4040
/*${pattern}*/
4141
${fnBody}
4242
cpu.cycles++;
@@ -62,7 +62,7 @@ for (const [fnPattern, fn] of patternToFn) {
6262
output += ']';
6363

6464
output += `\n
65-
export function executeInstruction(idx: number, cpu: ICPU, opcode: number) {
65+
export function executeInstruction(idx: number, cpu: CPU, opcode: number) {
6666
switch (idx) {
6767
${executeInstructionCases}
6868
default: instNOP(cpu, opcode);

benchmark/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CPU, ICPU } from '../src/cpu/cpu';
1+
import { CPU } from '../src/cpu/cpu';
22
import { avrInstruction } from '../src/cpu/instruction';
33
import { createBenchmark } from './benchmark';
44
import { permutations } from './permutations';
@@ -21,7 +21,7 @@ function avrInstructionUintArray(cpu: CPU) {
2121
}
2222

2323
/* Approach 2: use instMap */
24-
const instructionMap: { [key: number]: (cpu: ICPU, opcode: number) => void } = {};
24+
const instructionMap: { [key: number]: (cpu: CPU, opcode: number) => void } = {};
2525
for (const { pattern, fn } of instructions) {
2626
for (const opcode of permutations(pattern.replace(/ /g, '').substr(0, 16))) {
2727
if (!instructionMap[opcode]) {

demo/src/cpu-performance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ICPU } from 'avr8js';
1+
import { CPU } from 'avr8js';
22

33
export class CPUPerformance {
44
private prevTime = 0;
55
private prevCycles = 0;
66
private samples = new Float32Array(64);
77
private sampleIndex = 0;
88

9-
constructor(private cpu: ICPU, private MHZ: number) {}
9+
constructor(private cpu: CPU, private MHZ: number) {}
1010

1111
reset() {
1212
this.prevTime = 0;

src/cpu/cpu.ts

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,6 @@ import { avrInterrupt } from './interrupt';
1111

1212
const registerSpace = 0x100;
1313

14-
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
15-
export interface ICPU {
16-
readonly data: Uint8Array;
17-
readonly dataView: DataView;
18-
readonly progMem: Uint16Array;
19-
readonly progBytes: Uint8Array;
20-
21-
/**
22-
* Whether the program counter (PC) can address 22 bits (the default is 16)
23-
*/
24-
readonly pc22Bits: boolean;
25-
26-
/**
27-
* Program counter
28-
*/
29-
pc: u32;
30-
31-
/**
32-
* Clock cycle counter
33-
*/
34-
cycles: number;
35-
36-
readData(addr: u16): u8;
37-
writeData(addr: u16, value: u8, mask?: u8): void;
38-
onWatchdogReset(): void;
39-
}
40-
4114
export type CPUMemoryHook = (value: u8, oldValue: u8, addr: u16, mask: u8) => boolean | void;
4215
export interface CPUMemoryHooks {
4316
[key: number]: CPUMemoryHook;
@@ -66,7 +39,7 @@ interface AVRClockEventEntry {
6639
next: AVRClockEventEntry | null;
6740
}
6841

69-
export class CPU implements ICPU {
42+
export class CPU {
7043
readonly data: Uint8Array = new Uint8Array(this.sramBytes + registerSpace);
7144
readonly data16 = new Uint16Array(this.data.buffer);
7245
readonly dataView = new DataView(this.data.buffer);
@@ -76,6 +49,10 @@ export class CPU implements ICPU {
7649
private readonly pendingInterrupts: AVRInterruptConfig[] = [];
7750
private nextClockEvent: AVRClockEventEntry | null = null;
7851
private readonly clockEventPool: AVRClockEventEntry[] = []; // helps avoid garbage collection
52+
53+
/**
54+
* Whether the program counter (PC) can address 22 bits (the default is 16)
55+
*/
7956
readonly pc22Bits = this.progBytes.length > 0x20000;
8057

8158
readonly gpioPorts = new Set<AVRIOPort>();
@@ -89,8 +66,16 @@ export class CPU implements ICPU {
8966
/* empty by default */
9067
};
9168

69+
/**
70+
* Program counter
71+
*/
9272
pc: u32 = 0;
93-
cycles: u32 = 0;
73+
74+
/**
75+
* Clock cycle counter
76+
*/
77+
cycles = 0;
78+
9479
nextInterrupt: i16 = -1;
9580

9681
constructor(public progMem: Uint16Array, private sramBytes = 8192) {

src/cpu/instruction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (C) 2019, 2020 Uri Shaked
1111
*/
1212

13-
import { ICPU } from './cpu';
13+
import { CPU } from './cpu';
1414
import { u16 } from '../types';
1515

1616
function isTwoWordInstruction(opcode: u16) {
@@ -26,7 +26,7 @@ function isTwoWordInstruction(opcode: u16) {
2626
);
2727
}
2828

29-
export function avrInstruction(cpu: ICPU) {
29+
export function avrInstruction(cpu: CPU) {
3030
const opcode = cpu.progMem[cpu.pc];
3131

3232
if ((opcode & 0xfc00) === 0x1c00) {

src/cpu/interrupt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Copyright (C) 2019, Uri Shaked
77
*/
88

9-
import { ICPU } from './cpu';
9+
import { CPU } from './cpu';
1010

11-
export function avrInterrupt(cpu: ICPU, addr: number) {
11+
export function avrInterrupt(cpu: CPU, addr: number) {
1212
const sp = cpu.dataView.getUint16(93, true);
1313
cpu.data[sp] = cpu.pc & 0xff;
1414
cpu.data[sp - 1] = (cpu.pc >> 8) & 0xff;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2019, 2020, Uri Shaked
55
*/
66

7-
export { CPU, ICPU, CPUMemoryHook, CPUMemoryHooks } from './cpu/cpu';
7+
export { CPU, CPUMemoryHook, CPUMemoryHooks } from './cpu/cpu';
88
export { avrInstruction } from './cpu/instruction';
99
export { avrInterrupt } from './cpu/interrupt';
1010
export {

0 commit comments

Comments
 (0)