diff options
Diffstat (limited to 'src/chip8.cpp')
-rw-r--r-- | src/chip8.cpp | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/src/chip8.cpp b/src/chip8.cpp index 94573f0..485d061 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -168,9 +168,7 @@ void Chip8::OP_NULL() {} // 00E0: CLS // Clear the display void Chip8::OP_00E0() { - for (uint32_t i = 0; i < sizeof(video) / sizeof(video[0]); i++) { - video[i] = 0; - } + video.reset(); }; // 00EE: RET @@ -385,29 +383,34 @@ void Chip8::OP_Cxkk() { // Dxyn: DRW Vx, Vy, nibble // Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision void Chip8::OP_Dxyn() { - uint8_t Vx = (opcode & 0x0F00u) >> 8u; - uint8_t Vy = (opcode & 0x00FFu) >> 4u; - uint8_t height = opcode & 0x000Fu; + uint8_t Vx = (opcode & 0x0F00u) >> 8u; + uint8_t Vy = (opcode & 0x00F0u) >> 4u; + uint8_t height = opcode & 0x000Fu; - // Wrap if going beyond screen boundaries - uint8_t xPos = registers[Vx] % VIDEO_WIDTH; - uint8_t yPos = registers[Vy] % VIDEO_HEIGHT; + // Wrap if going beyond screen boundaries + uint8_t xPos = registers[Vx] % VIDEO_WIDTH; + uint8_t yPos = registers[Vy] % VIDEO_HEIGHT; - registers[0xF] = 0; - - for (uint32_t row = 0; row < height; ++row) { - uint8_t spriteByte = memory[index + row]; - for (uint32_t col = 0; col < 8; ++col) { - uint8_t spritePixel = spriteByte & (0x80u >> col); - uint32_t* screenPixel = &video[(yPos + row) * VIDEO_WIDTH + (xPos + col)]; + registers[0xF] = 0; - if (spritePixel) { - if(*screenPixel == 0xFFFFFFFF) - registers[0xF] = 1; - *screenPixel ^= 0xFFFFFFFF; - } + for (uint32_t row = 0; row < height; ++row) { + uint8_t spriteByte = memory[index + row]; + for (uint32_t col = 0; col < 8; ++col) { + uint8_t spritePixel = spriteByte & (0x80u >> col); + + // Calculate the coordinates in the video bitset + int x = (xPos + col) % VIDEO_WIDTH; + int y = (yPos + row) % VIDEO_HEIGHT; + + // Check collision and update the video bitset + if (spritePixel) { + if (video[y * VIDEO_WIDTH + x]) { + registers[0xF] = 1; + } + video[y * VIDEO_WIDTH + x].flip(); + } + } } - } } // Ex9E: SKP Vx |