Having trouble implementing draw instruction in chip8 emulator

373 Views Asked by At

Hello everyone so basically im creating a chip8 emulator and im having a bit of trouble implementing the draw instruction. the chip 8 has a screen of 64x32 i have a array of size 32 of uint64_t. chip 8 uses a monochrome display where each bit is whether that pixel is on or off. so now I loop through that array to get that 64 bit row. Issue is I only want to xor the 8 bits corresponding to the xcordinate and after it. Not the whole 64 bit row just the xcordinate and the 7 bits after it with the sprite byte that I have.

This is my code so far for it.

  for(int i = 0; i < num_bytes; i++)
  {
    byte byte_to_draw = memory[registers->I + i];
    for(int j = 0; j < 8; j++)
    {
      // 64 bit number each bit representing on or off
      uint64_t current_row = screen[register_value_y - i];

// I want to xor here
      
    }
  }

1

There are 1 best solutions below

0
الرحمن الرحیم On

try following changes:

for (int i = 0; i < num_bytes; i++) {
    byte byte_to_draw = memory[registers->I + i];
    for (int j = 0; j < 8; j++) {
        // Calculate the x-coordinate for this pixel
        int x_coordinate = (register_value_x + j) % 64; // Wrap around if x exceeds 63

        // Get the current bit in the sprite
        byte sprite_bit = (byte_to_draw >> (7 - j)) & 0x1;

        // Get the current bit in the screen row
        uint64_t current_row = screen[register_value_y - i];

        // Perform XOR operation only for the specific bit
        if (sprite_bit == 1) {
            current_row ^= (uint64_t)1 << x_coordinate;
        }

        // Update the screen row with the modified value
        screen[register_value_y - i] = current_row;
    }
}

It mostly works . . . the code will XOR the sprite with the screen row for the specified x-coordinate and the 7 bits after it, which is the desired behavior for the Chip-8 draw instruction.