microoptimization for rendering

This commit is contained in:
Trevor Merritt 2024-10-17 11:35:03 -04:00
parent 4cc56db489
commit a467c8e6b0

View File

@ -432,7 +432,7 @@ impl Chip8CpuInstructions {
} }
} }
pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer { pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer {
print!("INSTRUCTION {}", self); // print!("INSTRUCTION {}", self);
let start_time = Instant::now(); let start_time = Instant::now();
let start_pc = input.registers.peek_pc(); let start_pc = input.registers.peek_pc();
input.registers.poke_pc(start_pc + 2); input.registers.poke_pc(start_pc + 2);
@ -665,21 +665,6 @@ impl Chip8CpuInstructions {
let x_offset = input.registers.peek(*x) as u16; let x_offset = input.registers.peek(*x) as u16;
let y_offset = input.registers.peek(*y) as u16; let y_offset = input.registers.peek(*y) as u16;
let mut buffer = vec![];
for byte_index in 0..*n {
buffer.push(input.memory.peek(byte_index as u16 + source_memory_offset));
}
for (idx, byte) in buffer.iter().enumerate() {
let local_x = (x_offset + idx as u16) * 64;
for bit_index in 0..8 {
input.video_memory.poke(x_offset + (y_offset + bit_index as u16), (current_byte & (0x80 >> bit_index)) != 0);
}
}
for byte_index in 0..*n { for byte_index in 0..*n {
let current_byte = input.memory.peek(byte_index as u16 + source_memory_offset); let current_byte = input.memory.peek(byte_index as u16 + source_memory_offset);
let x_offset: u16 = (x_offset + byte_index as u16) * 64; let x_offset: u16 = (x_offset + byte_index as u16) * 64;
@ -688,12 +673,16 @@ impl Chip8CpuInstructions {
} }
} }
let target = if input.video_memory.has_frame_changed {
if input.video_memory.has_frame_changed { 1u8
input.registers.poke(0xf, 1u8);
} else { } else {
input.registers.poke(0xf, 0u8); 0u8
} };
input.registers.poke(0xf, target);
} }
Chip8CpuInstructions::SkpVx(x) => { Chip8CpuInstructions::SkpVx(x) => {
// Ex9E - SKP Vx // Ex9E - SKP Vx
@ -824,7 +813,7 @@ impl Chip8CpuInstructions {
Chip8CpuInstructions::XXXXERRORINSTRUCTION => {} Chip8CpuInstructions::XXXXERRORINSTRUCTION => {}
}; };
let cycle_time = Instant::now().duration_since(start_time).as_nanos(); let cycle_time = Instant::now().duration_since(start_time).as_nanos();
println!("\t\tTook {cycle_time}ms"); // println!("\t\tTook {cycle_time}ms");
input.to_owned() input.to_owned()
} }
} }