Fix bug in Fx33 Opcode. Fx33 now works as intended.

This commit is contained in:
Rekai Musuka 2020-07-14 21:43:03 -05:00
parent 80850e6fe8
commit 9b88965cab
2 changed files with 13 additions and 24 deletions

View File

@ -400,15 +400,11 @@ impl Chip8 {
// take tens digit and place it at I + 1 // take tens digit and place it at I + 1
// take ones digit and place it at I + 2 // take ones digit and place it at I + 2
let i = self.i as usize; 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 self.memory[i] = digits[2];
let tens = digit.next().unwrap_or(0); self.memory[i + 1] = digits[1];
let hundreds = digit.next().unwrap_or(0); self.memory[i + 2] = digits[0];
self.memory[i] = hundreds as u8;
self.memory[i + 1] = tens as u8;
self.memory[i + 2] = ones as u8;
} }
fn copy_from_vx_to_memory(&mut self, x: u8) { 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 to_digits(num: u8) -> [u8; 3] {
fn digits(mut num: usize) -> impl Iterator<Item = usize> { let mut cpy = num.clone();
let mut divisor = 1; let mut digits = [0, 0, 0];
while num >= divisor * 10 {
divisor *= 10; for digit in digits.iter_mut() {
*digit = cpy % 10;
cpy /= 10;
} }
std::iter::from_fn(move || { digits
if divisor == 0 {
None
} else {
let v = num / divisor;
num %= divisor;
divisor /= 10;
Some(v)
}
})
} }
} }

View File

@ -21,7 +21,7 @@ fn main() {
let window = init_window(&event_loop); let window = init_window(&event_loop);
let mut pixels = init_pixels(&window); 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"); chip8.load_rom(rom_path).expect("Unable to load ROM");
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {