From 7c2676433d2851944dc9bcce3a76fc49c0db758b Mon Sep 17 00:00:00 2001 From: MichaelValwin Date: Mon, 2 Sep 2024 16:35:16 +0200 Subject: [PATCH] Added keyboard --- .vscode/launch.json | 2 +- roms/1-chip8-logo.ch8 | Bin 0 -> 260 bytes roms/2-ibm-logo.ch8 | Bin 0 -> 132 bytes src/chip8Emulator.ts | 29 ++++++++++---- src/cpu.ts | 91 ++++++++++++++++++++++++++++++++++++++++-- src/keyboard.ts | 40 +++++++++++++++++++ 6 files changed, 149 insertions(+), 13 deletions(-) create mode 100755 roms/1-chip8-logo.ch8 create mode 100755 roms/2-ibm-logo.ch8 create mode 100644 src/keyboard.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 254dbb6..7b6e4fd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "type": "firefox", + "type": "chrome", "request": "launch", "name": "Debug chip8 emulator", "reAttach": true, diff --git a/roms/1-chip8-logo.ch8 b/roms/1-chip8-logo.ch8 new file mode 100755 index 0000000000000000000000000000000000000000..19c5cf30f469f86fce62351b46c5a68454d93429 GIT binary patch literal 260 zcmZR0kjR+8u_)kze1gEDcpxdUC=W;~EUE>P8jE^>q`{)u7vvKKfT~sjc>;@e0!fKQ zCxE2FqU%6XW6?7pX|U+)1$iMqekKrLV34;jeDL(aiv_$|8X6i53=%tbSXkKaXOLXF zci~(H1|C^iMqb&ujK?)KHPsmye*8Ii3; + + memory: Uint8Array; + displayMemory: Uint8Array; + constructor() { - this.flags = 0; + this.memory = new Uint8Array(MEMORY_SIZE); + this.displayMemory = new Uint8Array(SCREEN_WIDTH*SCREEN_HEIGHT); + this.pc = 0; + this.indexReg = 0; + this.stackPointer = 0; + this.stack = new Array(STACK_SIZE); + this.delayTimer = 0; + this.soundTimer = 0; + this.registers = new Uint8Array(REGISTERS); + + this.clearMemory(); + this.clearDisplay(); + this.clearRegisters(); + + // 0x000-0x1FF - Chip 8 interpreter + // 0x050-0x0A0 - 4x5 pixel font set (0-F) + // 0x200-0xFFF - Program ROM and work RAM + this.setupFont(); } - dosomethingcool() { - console.log("Doing something really cool"); + clearRegisters() { + this.registers.fill(0); + this.pc = 0; + this.indexReg = 0; + this.stackPointer = 0; + this.delayTimer = 0; + this.soundTimer = 0; } + + clearMemory() { + this.memory.fill(0); + } + + clearDisplay() { + this.displayMemory.fill(0); + } + + + setupFont() { + // Using address from 050 - 09F ?? + let addressFont = 0x050; + + // Every character has 5 Bytes + const characters = [ + 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 + 0x20, 0x60, 0x20, 0x20, 0x70, // 1 + 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 + 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3 + 0x90, 0x90, 0xF0, 0x10, 0x10, // 4 + 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5 + 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6 + 0xF0, 0x10, 0x20, 0x40, 0x40, // 7 + 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8 + 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9 + 0xF0, 0x90, 0xF0, 0x90, 0x90, // A + 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B + 0xF0, 0x80, 0x80, 0x80, 0xF0, // C + 0xE0, 0x90, 0x90, 0x90, 0xE0, // D + 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E + 0xF0, 0x80, 0xF0, 0x80, 0x80 // F + ]; + + this.memory.set(characters, addressFont); + } + + + + + + } export default CPU; \ No newline at end of file diff --git a/src/keyboard.ts b/src/keyboard.ts new file mode 100644 index 0000000..b2ad974 --- /dev/null +++ b/src/keyboard.ts @@ -0,0 +1,40 @@ +const KEYS = 16; + + +// Keyboard +// 1 2 3 C +// 4 5 6 D +// 7 8 9 E +// A 0 B F +class Keyboard { + keys : Uint8Array; + + userKeyboardMap: Map; + + constructor() { + this.keys = new Uint8Array(KEYS); + + // Used to check keys from the user event.keys + this.userKeyboardMap = new Map(); + } + + private setKey(key: number, pressed: boolean) { + this.keys[key] = pressed ? 1 : 0; + } + + public setUserKey(userKey: number, key: number) { + 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 userKeyPressedUp(userKey: number) { + let keyVal: number = this.userKeyboardMap.get(userKey) ?? -1; + if(keyVal != -1) this.setKey(keyVal, false); + } +} + +export default Keyboard; \ No newline at end of file