From 24634637b24d144ffdf5ae6b5fb56e289126706f Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Sun, 6 Oct 2024 12:32:18 -0400 Subject: [PATCH] video rendering might work. cant focus --- emma/src/chip8/instructions.rs | 9 +++++---- emma/src/chip8/video.rs | 17 +++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/emma/src/chip8/instructions.rs b/emma/src/chip8/instructions.rs index 67a0c3e..d8eaa45 100644 --- a/emma/src/chip8/instructions.rs +++ b/emma/src/chip8/instructions.rs @@ -598,7 +598,6 @@ impl Chip8CpuInstructions { Chip8CpuInstructions::DrawVxVyNibble(x, y, n) => { // Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision. - // The interpreter reads n bytes from memory, starting at the address stored in I. @@ -770,7 +769,7 @@ impl Chip8CpuInstructions { mod test { use dimensioned::typenum::assert_type; use ratatui::crossterm::execute; - use crate::chip8::system_memory::{CHIP8FONT_0, CHIP8FONT_1, CHIP8FONT_9}; + use crate::chip8::system_memory::{CHIP8FONT_0, CHIP8FONT_1, CHIP8FONT_2, CHIP8FONT_9}; use super::*; #[test] @@ -1408,6 +1407,7 @@ mod test { let y_register = 0x2; let x_offset = 1; let y_offset = 2; + let char_offset = 0x0A; // use the font characters to draw to video memory // assert_eq!(x.memory.peek(0x0), CHIP8FONT_0[0]); @@ -1419,7 +1419,7 @@ mod test { // now lets set the X and Y to 1,2 x.registers.poke(x_register, x_offset); x.registers.poke(y_register, y_offset); - x.registers.poke_i(0x5); + x.registers.poke_i(char_offset); // we are using 5 rows. Chip8CpuInstructions::DrawVxVyNibble(x_register as u16, y_register as u16, 5).execute(&mut x); @@ -1431,13 +1431,14 @@ mod test { // 5,2->5,9 // let byte_to_check = CHIP8FONT_0[0]; for row_in_sprite in 0..5 { - let row_data = CHIP8FONT_1[row_in_sprite]; + let row_data = CHIP8FONT_2[row_in_sprite]; for bit_in_byte in 0..8 { let data_offset = (x_offset + row_in_sprite as u8) * 64 + (bit_in_byte + y_offset); let should_be_set = (row_data.shr(bit_in_byte) & 0x1) == 1; println!("DATA_OFFSET FOR {}x{} is {} when offsetting by {}x{} and should be {} working with byte {:08b}", bit_in_byte, row_in_sprite, data_offset, x_offset, y_offset, should_be_set, row_data); // assert_eq!(should_be_set, ); + assert_eq!(should_be_set, x.video_memory.peek(data_offset as u16)); } } } diff --git a/emma/src/chip8/video.rs b/emma/src/chip8/video.rs index 7ee1a77..1b5bab5 100644 --- a/emma/src/chip8/video.rs +++ b/emma/src/chip8/video.rs @@ -28,7 +28,12 @@ impl Chip8Video { } pub fn poke(&mut self, address: u16, new_value: bool) -> Self { - println!("**VIDEO** POKING {new_value} TO {address}"); + let col = address % 64; + let row = address / 32; + let old_value = self.memory[address as usize]; + if old_value != new_value { + println!("**VIDEO** TOGGLING {new_value} TO {address} / {col}x{row}"); + } self.memory[address as usize] = new_value; self.to_owned() } @@ -57,11 +62,11 @@ impl Chip8Video { pub fn format_as_string(self) -> String { let mut output = String::new(); for row in 0..32 { - let row_offset = row * 32; + let row_offset = row * 64; for column in 0..64 { let data_position = row_offset + column; - // println!("DP {} {} {} {}", data_position, row, row_offset, column); output += if self.memory[data_position] { + println!("DOT AT OFFSET:{row_offset} {row}x{column} {data_position:03x}/{data_position}"); "*" } else { " " @@ -72,8 +77,6 @@ impl Chip8Video { // println!("{}", output); output } - - } impl Default for Chip8Video { @@ -197,6 +200,9 @@ mod test { assert!(!v.peek(0x03)); assert!(!v.peek(0x05)); assert!(!v.peek(0x07)); + for i in 0x8..CHIP8_VIDEO_MEMORY { + assert!(!v.peek(i as u16)); + } } #[test] @@ -307,6 +313,5 @@ mod test { assert!(v.peek(0xC5)); assert!(!v.peek(0xC6)); assert!(v.peek(0xC7)); - } }