From 25d652b1733f4e61c57788cd5f516f2bb693c765 Mon Sep 17 00:00:00 2001 From: MichaelValwin Date: Tue, 3 Sep 2024 15:08:02 +0200 Subject: [PATCH] Did some instructions --- src/cpu.ts | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 140 insertions(+), 5 deletions(-) diff --git a/src/cpu.ts b/src/cpu.ts index 74813ec..780b283 100644 --- a/src/cpu.ts +++ b/src/cpu.ts @@ -101,15 +101,150 @@ class CPU { private cycle() { // Fetch opcode let firstOpcode: number = this.memory[this.pc]; - let secondtOpcode: number = this.memory[this.pc + 1]; - let opcode: number = firstOpcode << 8 | secondtOpcode; + let secondOpcode: number = this.memory[this.pc + 1]; + let opcode: number = firstOpcode << 8 | secondOpcode; // Decode opcode - - let firstNibble = opcode >> 12; - + // X000 + let firstNibble = firstOpcode >> 4; + switch(firstNibble) { + case 0x0: + if(opcode == 0x00E0) { + // Clear the screen + this.clearDisplay(); + }else if(opcode == 0x00EE) { + // Returns from a subroutine + this.stackPointer--; + this.pc = this.stack[this.pc]; + } + this.incrementPC(); + break; + case 0x1: + // goto NNN; + let threeNib = opcode & 0x0FFF; + this.pc = threeNib; + break; + case 0x2: + // Calls subroutine at NNN + this.stack[this.stackPointer] = this.pc; // Store PC at stack + this.stackPointer++; // Increment it + this.pc = opcode & 0x0FFF; // Go to that address to execute code + break; + case 0x3: { + // 3XNN + // Skips the next instruction if VX equals NN + let secondNibble = firstOpcode & 0xF; //0X00 + let val = opcode & 0x00FF; //0x00NN + if(this.registers[secondNibble] == val){ + this.incrementPC(); + } + this.incrementPC(); + } + break; + case 0x4: { + // 4XNN + // Skips the next instruction if VX NOT equals NN + let secondNibble = firstOpcode & 0xF; //0X00 + let val = opcode & 0x00FF; //0x00NN + if(this.registers[secondNibble] != val){ + this.incrementPC(); + } + this.incrementPC(); + } + break; + case 0x5: { + // 5XY0 + // Skips the next instruction if VX == VY + let secondNibble = firstOpcode & 0xF; //0X00 + let thirdNibble = secondOpcode >> 4; //00Y0 + if(this.registers[secondNibble] == this.registers[thirdNibble]) { + this.incrementPC(); + } + this.incrementPC(); + } + break; + case 0x6: { + // 6XNN + // Sets VX to NN + let secondNibble = firstOpcode & 0xF; //0X00 + this.registers[secondNibble] = secondOpcode; + this.incrementPC(); + } + break; + case 0x7: { + // 7XNN + // Adds NN to VX + let secondNibble = firstOpcode & 0xF; //0X00 + this.registers[secondNibble] = secondOpcode; + this.incrementPC(); + } + break; + case 0x8: { + let secondNibble = firstOpcode & 0xF; //0X00 + let thirdNibble = secondOpcode >> 4; //00Y0 + let lastNibble = secondOpcode & 0xF; //000N + if(lastNibble == 0x0) { + // 8XY0 + // Vx |= Vy + this.registers[secondNibble] = this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0x1) { + // 8XY1 + // Vx |= Vy + this.registers[secondNibble] |= this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0x2) { + // 8XY2 + // Vx &= Vy + this.registers[secondNibble] &= this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0x3) { + // 8XY3 + // Vx ^= Vy + this.registers[secondNibble] ^= this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0x4) { + // 8XY4 + // Vx += Vy + let sum = this.registers[secondNibble] + this.registers[thirdNibble]; + this.registers[0xF] = sum > 255 ? 1 : 0; // Set carry flag + this.registers[secondNibble] = sum & 0xFF; // Get only the first byte + this.incrementPC(); + }else if(lastNibble == 0x5) { + // 8XY5 + // Vx -= Vy + this.registers[0xF] = this.registers[secondNibble] > this.registers[thirdNibble] ? 1 : 0; + let subs = this.registers[secondNibble] - this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0x6) { + // 8XY6 + // Vx >>= 1 + this.registers[secondNibble] >> 1; + this.incrementPC(); + }else if(lastNibble == 0x7) { + // 8XY7 + // Vx = Vy - Vx + this.registers[0xF] = this.registers[secondNibble] > this.registers[thirdNibble] ? 1 : 0; + let subs = this.registers[secondNibble] - this.registers[thirdNibble]; + this.incrementPC(); + }else if(lastNibble == 0xE) { + } + + } + break; + case 0x9: { + // 9XY0 + // Skips the next instruction if VX != VY + let secondNibble = firstOpcode & 0xF; //0X00 + let thirdNibble = secondOpcode >> 4; //00Y0 + if(this.registers[secondNibble] != this.registers[thirdNibble]) { + this.incrementPC(); + } + this.incrementPC(); + } + break; } }