Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DXYN - Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels and a height of N pixels.
- //Each row of 8 pixels is read as bit-coded starting from memory location I; I value doesn’t change after the execution of this instruction.
- // As described above, VF is set to 1 if any screen pixels are flipped from set to unset when the sprite is drawn, and to 0 if that doesn’t happen
- void ChipEight::DrawSpriteatVXandVYandN(uint16_t opcode){
- //Get the x, y coordinates
- int rX = this->registers[(opcode & 0x0F00) >> 8];
- int rY = this->registers[(opcode & 0x00F0) >> 4];
- int height = opcode & 0x000F;
- //We don't know if we've flipped any bits yet, so set to 0 for now.
- this->registers[0xF] = 0;
- //Loop for how many rows we want the sprite to be.
- for (int row = 0; row < height; row++)
- {
- //The pixel is just one ROW of the SPRITE which is N rows
- uint8_t pixel = this->memory[iRegister + row]; //Get each row of the sprite by incrementing I
- for (int col = 0; col < 8; col++) //Each sprite is 8 units wide
- {
- //Get the bit we want to check if it's set:
- int bit = pixel & (0b10000000 >> col);
- //Check if the bit is set
- if(bit != 0)
- {
- //We want to draw, but we must check if the bit is already set here:
- if(this->display[rX + col][rY + row] == 1)
- {
- //It is set, so we need to set the 0xF register because we will erase.
- this->registers[0xF] = 1;
- }
- //XOR the position with 1, which will flip the bit.
- this->display[rX + col][rY + row] ^= 1;
- }
- }
- }
- IncrementProgramCounter();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement