From 9b88965cabfa3d88ab94546e3e90a5a7bdbca9b5 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 14 Jul 2020 21:43:03 -0500 Subject: [PATCH] Fix bug in Fx33 Opcode. Fx33 now works as intended. --- src/emu.rs | 35 ++++++++++++----------------------- src/main.rs | 2 +- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/emu.rs b/src/emu.rs index 72666e2..7ae98de 100644 --- a/src/emu.rs +++ b/src/emu.rs @@ -400,15 +400,11 @@ impl Chip8 { // take tens digit and place it at I + 1 // take ones digit and place it at I + 2 let i = self.i as usize; - let mut digit = Self::digits(self.v[x as usize] as usize); + let digits: [u8; 3] = Self::to_digits(self.v[x as usize]); - let ones = digit.next().unwrap(); // Ther has to at least be a ones lol - let tens = digit.next().unwrap_or(0); - let hundreds = digit.next().unwrap_or(0); - - self.memory[i] = hundreds as u8; - self.memory[i + 1] = tens as u8; - self.memory[i + 2] = ones as u8; + self.memory[i] = digits[2]; + self.memory[i + 1] = digits[1]; + self.memory[i + 2] = digits[0]; } fn copy_from_vx_to_memory(&mut self, x: u8) { @@ -425,23 +421,16 @@ impl Chip8 { } } - // https://stackoverflow.com/questions/41536479/how-do-i-split-an-integer-into-individual-digits - fn digits(mut num: usize) -> impl Iterator { - let mut divisor = 1; - while num >= divisor * 10 { - divisor *= 10; + fn to_digits(num: u8) -> [u8; 3] { + let mut cpy = num.clone(); + let mut digits = [0, 0, 0]; + + for digit in digits.iter_mut() { + *digit = cpy % 10; + cpy /= 10; } - std::iter::from_fn(move || { - if divisor == 0 { - None - } else { - let v = num / divisor; - num %= divisor; - divisor /= 10; - Some(v) - } - }) + digits } } diff --git a/src/main.rs b/src/main.rs index 366ed3a..a80ae49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn main() { let window = init_window(&event_loop); let mut pixels = init_pixels(&window); - let rom_path = Path::new("./games/ibm_logo.ch8"); + let rom_path = Path::new("./games/test_opcode.ch8"); chip8.load_rom(rom_path).expect("Unable to load ROM"); event_loop.run(move |event, _, control_flow| {