diff --git a/src/RNG.ts b/src/RNG.ts index 5776003..0eba3bf 100644 --- a/src/RNG.ts +++ b/src/RNG.ts @@ -18,6 +18,10 @@ class RNG { nextInt(min: number, max: number): number { return Math.floor(this.next() * (max - min + 1)) + min; } + + next255(): number { + return Math.floor(this.next() * 255); + } } export default RNG; \ No newline at end of file diff --git a/src/cpu.ts b/src/cpu.ts index 780b283..538fad6 100644 --- a/src/cpu.ts +++ b/src/cpu.ts @@ -216,20 +216,28 @@ class CPU { // Vx -= Vy this.registers[0xF] = this.registers[secondNibble] > this.registers[thirdNibble] ? 1 : 0; let subs = this.registers[secondNibble] - this.registers[thirdNibble]; + this.registers[secondNibble] = subs; this.incrementPC(); }else if(lastNibble == 0x6) { // 8XY6 // Vx >>= 1 - this.registers[secondNibble] >> 1; + this.registers[0xF] = this.registers[0xF] & 1; // ???? // Store the least significant bit of VX prior + 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.registers[0xF] = this.registers[thirdNibble] > this.registers[secondNibble] ? 1 : 0; + let subs = this.registers[thirdNibble] - this.registers[secondNibble]; + this.registers[secondNibble] = subs; this.incrementPC(); }else if(lastNibble == 0xE) { - + // 8XYE + // Vx <<= 1 + // Set VF to 1 if the most significant bit of VX prior to that shift was set + this.registers[0xF] = this.registers[0xF] >> 3; + this.registers[secondNibble] <<= 1; + this.incrementPC(); } } @@ -245,6 +253,58 @@ class CPU { this.incrementPC(); } break; + + case 0xA: { + // ANNN + // Sets I to the address NNN + this.indexReg = opcode & 0x0FFF; + this.incrementPC(); + } + break; + case 0xB:{ + // BNNN + // Jumps to the address NNN plus V0 + this.pc = this.registers[0] + opcode & 0x0FFF; + } + break; + case 0xC: { + // CXNN + // Vx = rand() & NN + let secondNibble = firstOpcode & 0xF; //0X00 + this.registers[secondNibble] = this.rng.next255() & secondOpcode; + this.incrementPC(); + } + break; + case 0xD: { + // DXYN + // draw(Vx, Vy, N) + let secondNibble = firstOpcode & 0xF; //0X00 + let thirdNibble = secondOpcode >> 4; //00Y0 + let fourthNibble = secondOpcode & 0xF; + + this.registers[0xF] = 0; + + let regX = this.registers[secondNibble]; + let regY = this.registers[thirdNibble]; + + let y = 0; + while(y < fourthNibble) { + // A sprite is always 8 bits horizontal + let pixel = this.memory[this.indexReg + y]; + let x = 0; + while(x < 8) { + const msb = 0x80; + + + x++; + } + + y++; + } + + this.incrementPC(); + } + break; } }