diff --git a/src/cpu.ts b/src/cpu.ts index bfb9ffd..e7f4872 100644 --- a/src/cpu.ts +++ b/src/cpu.ts @@ -327,11 +327,15 @@ class CPU { case 0xD: { // DXYN // draw(Vx, Vy, N) + // Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels and a height of N pixels this.regs[0xF] = 0; // Reset register VF let regX = this.regs[nib2]; let regY = this.regs[nib3]; + regX = regX % 64; + regY = regY % 32; + for(let y = 0; y < nib4; y++) { // A sprite is always 8 bits horizontal let pixel = this.memory[this.indexReg + y]; // Fetch pixel from memory starting at I reg @@ -340,7 +344,14 @@ class CPU { const MSB = 0x80; // 0b10000000 if((pixel & (MSB >> x)) != 0) { - let mem = x + regX + ((y + regY) * SCREEN_WIDTH); + let r = x + regX; + let c = ((y + regY) * SCREEN_WIDTH); + let mem = r + c; + + if(r+c > 64*32) { + mem = mem % (64*32); + } + if(this.displayMemory[mem] == 1) { this.regs[0xF] = 1; }