r/EmuDev • • Dec 25 '22

Draw function not working

Every other part of the emulator seems to work correctly, except for drawing. Error is probably somewhere here. I can't provide and image, however a vertical rectangle with random pixels missing shows upl

let x_register = (self.opcode & 0x0f00) >> 8;
        let y_register = (self.opcode & 0x00f0) >> 4;
        let n = self.opcode & 0x000f;


        let x_value = self.registers[x_register as usize];
        let y_value = self.registers[y_register as usize];

        println!("{}", x_value);


        for i in 0..n {
            let sprite_line = self.ram[i as usize + self.index_register as usize];

            for bit in 0..8 {
                let x = (x_value + bit) % 63;
                let y = (y_value + i) % 31;
                
                let sprite_bit = (sprite_line >> (7 - bit)) & 1;
                let old_bit: u8 = self.display_buffer[x as usize][y as usize] as u8;
                let new_bit = old_bit ^ sprite_bit;
                self.display_buffer[x as usize][y as usize] = new_bit != 0;

                

            }
        }
0 Upvotes

7 comments sorted by

1

u/tobiasvl Dec 25 '22

Why can't you provide an image? It would probably help a lot. From your description I'm not sure what the problem is, or even what the symptoms are. Are you saying it draws a random sprite instead of the correct sprite? If so, maybe your ANNN instruction is wrong.

4

u/[deleted] Dec 25 '22

[removed] — view removed comment

-2

u/KarmaIsTooLowLol Dec 25 '22

Even if I change the number next to the modulo, the output is still incorrect.

I am trying to display the IBM logo, so the VF flag isn't needed yet.