Skip to content

Commit

Permalink
refactor: reorganize emulator class properties and remove unused methods
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscodelahoz committed Nov 9, 2024
1 parent 7afb3b3 commit 432d7ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
42 changes: 8 additions & 34 deletions src/scripts/emulator/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ export class Chip8Emulator {

private cpuInstance: CPU;

private canvas: HTMLCanvasElement;

private emulationLoop: number = 0;

constructor(props: Chip8EmulatorProps) {
this.displayInstance = new DisplayInterface(props.canvas);
this.canvas = props.canvas;

this.displayInstance = new DisplayInterface(this.canvas);
this.keyboardInstance = new KeyBoardInterface();
this.audioInstance = new AudioInterface();

Expand All @@ -29,15 +33,15 @@ export class Chip8Emulator {

private startEmulatorLoop() {
const frameTime = 1000 / 60;

const previousTime = Date.now();

let nextFrameMidpoint = previousTime + frameTime / 2;

this.emulationLoop = window.setInterval(() => {
const currentTime = Date.now();
let cycleCount = 0;
const currentTime = Date.now();

// Run the emulator cycle up to twice per interval to catch up on missed frames
/* Run the emulator cycle up to twice per interval to catch up on missed frames */
while (nextFrameMidpoint < currentTime - frameTime && cycleCount < 2) {
try {
this.cpuInstance.cycle();
Expand Down Expand Up @@ -65,29 +69,6 @@ export class Chip8Emulator {
}
}

private readAsArrayBuffer(fileInput: HTMLInputElement): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const input: File | undefined = fileInput?.files?.[0];

if (!input) {
reject('no file input found');
}

const fileReader = new FileReader();

fileReader.readAsArrayBuffer(input as File);

fileReader.addEventListener('load', (e) => resolve(e.target?.result as ArrayBuffer));
fileReader.addEventListener('error', (error) => reject(error));
});
}

private async readFile(fileInput: GenericEvent<HTMLInputElement>) {
const arrayBuffer = await this.readAsArrayBuffer(fileInput.target);
const romData = new Uint8Array(arrayBuffer);
return romData;
}

public loadRom(romData: Uint8Array) {
this.cpuInstance.loadRom(romData);
}
Expand Down Expand Up @@ -134,13 +115,6 @@ export class Chip8Emulator {
return this.cpuInstance.getMemorySize();
}

public async startEmulation(event: GenericEvent<HTMLInputElement>) {
const romData = await this.readFile(event as GenericEvent<HTMLInputElement>);
this.stopEmulatorLoop();
this.cpuInstance.loadRom(romData);
this.startEmulatorLoop();
}

public loadRomFromData(romData: Uint8Array) {
this.stopEmulatorLoop();
this.cpuInstance.loadRom(romData);
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/emulator/interfaces/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export class DisplayInterface {
this.context.canvas.width = this.displayWidth;
this.context.canvas.height = this.displayHeight;

this.planeColors = [ ...ColorPalettesManager.getCurrentSelectedPalette() ];
this.planeColors = [
...ColorPalettesManager.getCurrentSelectedPalette(),
];
}

setPaletteColor(index: number, color: string) {
Expand Down
6 changes: 3 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import FaviconsWebpackPlugin from 'favicons-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCSSExtractPlugin from 'mini-css-extract-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import path from 'node:path';
import TerserPlugin from 'terser-webpack-plugin';
import { GenerateSW } from 'workbox-webpack-plugin';

Expand Down

0 comments on commit 432d7ec

Please sign in to comment.