Set every return type in every function just to deal with webassm

This commit is contained in:
Miguel Angel 2024-09-06 13:39:12 +02:00
parent a1cedd91ef
commit bab8065322
5 changed files with 36 additions and 30 deletions

View File

@ -16,6 +16,7 @@
"@babel/core": "^7.25.2", "@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4", "@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7", "@babel/preset-typescript": "^7.24.7",
"assemblyscript": "^0.27.29",
"babel-loader": "^9.1.3", "babel-loader": "^9.1.3",
"http-server": "^14.1.1", "http-server": "^14.1.1",
"ts-loader": "^9.5.1", "ts-loader": "^9.5.1",
@ -23,5 +24,11 @@
"webpack": "^5.94.0", "webpack": "^5.94.0",
"webpack-cli": "^5.1.4", "webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4" "webpack-dev-server": "^5.0.4"
},
"exports": {
".": {
"import": "./build/release.js",
"types": "./build/release.d.ts"
}
} }
} }

View File

@ -12,7 +12,7 @@ class Chip8Audio {
this.volume = 0.1; this.volume = 0.1;
} }
startAudio(audioContext: AudioContext, volume: number) { startAudio(audioContext: AudioContext, volume: number): void{
this.volume = volume; this.volume = volume;
this.audioBrowserContext = audioContext; this.audioBrowserContext = audioContext;
this.gainNode = this.audioBrowserContext.createGain(); this.gainNode = this.audioBrowserContext.createGain();
@ -20,7 +20,7 @@ class Chip8Audio {
this.gainNode.gain.setValueAtTime(this.volume, this.audioBrowserContext.currentTime); this.gainNode.gain.setValueAtTime(this.volume, this.audioBrowserContext.currentTime);
} }
startBeep() { startBeep(): void {
if(this.oscillator == null && this.audioBrowserContext && this.gainNode) { if(this.oscillator == null && this.audioBrowserContext && this.gainNode) {
this.oscillator = this.audioBrowserContext.createOscillator(); this.oscillator = this.audioBrowserContext.createOscillator();
this.oscillator.type = 'square'; this.oscillator.type = 'square';
@ -30,7 +30,7 @@ class Chip8Audio {
} }
} }
stopBeep() { stopBeep(): void {
if(this.oscillator) { if(this.oscillator) {
this.oscillator.stop(); this.oscillator.stop();
this.oscillator = null; this.oscillator = null;

View File

@ -6,7 +6,7 @@ import Chip8Audio from './audio'
class Chip8Emulator { class Chip8Emulator {
cpu: CPU; cpu: CPU;
keyboard: Keyboard; keyboard: Keyboard;
rom: Uint8Array; rom: Uint8Array | null;
audio: Chip8Audio; audio: Chip8Audio;
romLoaded: boolean; romLoaded: boolean;
@ -17,21 +17,21 @@ class Chip8Emulator {
this.cpu = new CPU(this.keyboard, this.audio); this.cpu = new CPU(this.keyboard, this.audio);
this.rom = new Uint8Array(); this.rom = null;
this.romLoaded = false; this.romLoaded = false;
} }
loadRom(rom: Uint8Array) { loadRom(rom: Uint8Array): void {
this.rom = rom; this.rom = rom;
this.cpu.loadRom(this.rom); this.cpu.loadRom(this.rom);
this.romLoaded = true; this.romLoaded = true;
} }
isRomLoaded() { isRomLoaded(): boolean {
return this.romLoaded; return this.romLoaded;
} }
emulateCycle() { emulateCycle():void {
this.cpu.cycle(); this.cpu.cycle();
} }
@ -39,7 +39,7 @@ class Chip8Emulator {
return this.cpu.getCPUStatus(); return this.cpu.getCPUStatus();
} }
drawToCanvas(canvas: HTMLCanvasElement, size: number) { drawToCanvas(canvas: HTMLCanvasElement, size: number): void {
let ctx = canvas.getContext('2d') as CanvasRenderingContext2D; let ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
let width = 64*size; let width = 64*size;
let height = 32*size; let height = 32*size;
@ -61,19 +61,19 @@ class Chip8Emulator {
} }
} }
setUserKey(userKey: number, key: number) { setUserKey(userKey: number, key: number): void {
this.keyboard.setUserKey(userKey, key); this.keyboard.setUserKey(userKey, key);
} }
startAudio(audioContext: AudioContext, volume: number) { startAudio(audioContext: AudioContext, volume: number): void {
this.audio.startAudio(audioContext, volume); this.audio.startAudio(audioContext, volume);
} }
keyDownEvent(key: number) { keyDownEvent(key: number): void {
this.keyboard.userKeyPressedDown(key); this.keyboard.userKeyPressedDown(key);
} }
keyUpEvent(key: number) { keyUpEvent(key: number): void {
this.keyboard.userKeyPressedUp(key); this.keyboard.userKeyPressedUp(key);
} }
} }

View File

@ -54,7 +54,7 @@ class CPU {
this.setupFont(); this.setupFont();
} }
clearRegisters() { clearRegisters(): void {
this.regs.fill(0); this.regs.fill(0);
this.pc = 0x200; // Start of the program on normal programs this.pc = 0x200; // Start of the program on normal programs
this.indexReg = 0; this.indexReg = 0;
@ -65,16 +65,16 @@ class CPU {
this.rng = new RNG(0); this.rng = new RNG(0);
} }
clearMemory() { clearMemory(): void {
this.memory.fill(0); this.memory.fill(0);
} }
clearDisplay() { clearDisplay(): void {
this.displayMemory.fill(0); this.displayMemory.fill(0);
} }
private setupFont() { private setupFont(): void {
// Using address from 050 - 09F // Using address from 050 - 09F
let addressFont = 0x050; let addressFont = 0x050;
@ -101,9 +101,8 @@ class CPU {
this.memory.set(characters, addressFont); this.memory.set(characters, addressFont);
} }
public loadRom(rom: Uint8Array) { public loadRom(rom: Uint8Array): void {
// Loads a ROM into starting PC address // Loads a ROM into starting PC address
console.log(rom);
for(let i = 0; i < rom.length; i++) { for(let i = 0; i < rom.length; i++) {
this.memory[this.pc+i] = rom[i]; this.memory[this.pc+i] = rom[i];
} }
@ -130,11 +129,11 @@ class CPU {
return status; return status;
} }
private incrementPC() { private incrementPC(): void {
this.pc += 2; this.pc += 2;
} }
public cycle() { public cycle(): void {
// Fetch opcode // Fetch opcode
let firstOpcode: number = this.memory[this.pc]; let firstOpcode: number = this.memory[this.pc];
let secondOpcode: number = this.memory[this.pc + 1]; let secondOpcode: number = this.memory[this.pc + 1];

View File

@ -18,22 +18,22 @@ class Keyboard {
this.userKeyboardMap = new Map<number, number>(); this.userKeyboardMap = new Map<number, number>();
} }
private setKey(key: number, pressed: boolean) { private setKey(key: number, pressed: boolean): void {
this.keys[key] = pressed ? 1 : 0; this.keys[key] = pressed ? 1 : 0;
} }
public setUserKey(userKey: number, key: number) { public setUserKey(userKey: number, key: number): void {
this.userKeyboardMap.set(userKey, key); this.userKeyboardMap.set(userKey, key);
} }
public userKeyPressedDown(userKey: number) { public userKeyPressedDown(userKey: number): void {
let keyVal: number = this.userKeyboardMap.get(userKey) ?? -1; let keyVal: number | undefined = this.userKeyboardMap.get(userKey);
if(keyVal != -1) this.setKey(keyVal, true); if(keyVal) this.setKey(keyVal, true);
} }
public userKeyPressedUp(userKey: number) { public userKeyPressedUp(userKey: number): void {
let keyVal: number = this.userKeyboardMap.get(userKey) ?? -1; let keyVal: number | undefined = this.userKeyboardMap.get(userKey);
if(keyVal != -1) this.setKey(keyVal, false); if(keyVal) this.setKey(keyVal, false);
} }
} }