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/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"assemblyscript": "^0.27.29",
"babel-loader": "^9.1.3",
"http-server": "^14.1.1",
"ts-loader": "^9.5.1",
@ -23,5 +24,11 @@
"webpack": "^5.94.0",
"webpack-cli": "^5.1.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;
}
startAudio(audioContext: AudioContext, volume: number) {
startAudio(audioContext: AudioContext, volume: number): void{
this.volume = volume;
this.audioBrowserContext = audioContext;
this.gainNode = this.audioBrowserContext.createGain();
@ -20,7 +20,7 @@ class Chip8Audio {
this.gainNode.gain.setValueAtTime(this.volume, this.audioBrowserContext.currentTime);
}
startBeep() {
startBeep(): void {
if(this.oscillator == null && this.audioBrowserContext && this.gainNode) {
this.oscillator = this.audioBrowserContext.createOscillator();
this.oscillator.type = 'square';
@ -30,7 +30,7 @@ class Chip8Audio {
}
}
stopBeep() {
stopBeep(): void {
if(this.oscillator) {
this.oscillator.stop();
this.oscillator = null;

View File

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

View File

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

View File

@ -18,22 +18,22 @@ class Keyboard {
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;
}
public setUserKey(userKey: number, key: number) {
public setUserKey(userKey: number, key: number): void {
this.userKeyboardMap.set(userKey, key);
}
public userKeyPressedDown(userKey: number) {
let keyVal: number = this.userKeyboardMap.get(userKey) ?? -1;
if(keyVal != -1) this.setKey(keyVal, true);
public userKeyPressedDown(userKey: number): void {
let keyVal: number | undefined = this.userKeyboardMap.get(userKey);
if(keyVal) this.setKey(keyVal, true);
}
public userKeyPressedUp(userKey: number) {
let keyVal: number = this.userKeyboardMap.get(userKey) ?? -1;
if(keyVal != -1) this.setKey(keyVal, false);
public userKeyPressedUp(userKey: number): void {
let keyVal: number | undefined = this.userKeyboardMap.get(userKey);
if(keyVal) this.setKey(keyVal, false);
}
}