diff --git a/chip8_core/src/display.rs b/chip8_core/src/display.rs index 5202e9c..6798430 100644 --- a/chip8_core/src/display.rs +++ b/chip8_core/src/display.rs @@ -11,3 +11,12 @@ pub trait Display { /// Renders the display data on screen. fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]); } + +/// Simple display module for testing. Does nothing. +pub(crate) struct TestingDisplay; + +impl Display for TestingDisplay { + fn clear(&mut self) {} + + fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]) {} +} diff --git a/chip8_core/src/emulator.rs b/chip8_core/src/emulator.rs index c0dc75b..553f90a 100644 --- a/chip8_core/src/emulator.rs +++ b/chip8_core/src/emulator.rs @@ -485,16 +485,16 @@ where #[cfg(test)] mod tests { use super::*; - use crate::display::TerminalDisplay; + use crate::display::TestingDisplay; use crate::input::NoInput; - use crate::sound::TerminalSound; + use crate::sound::TestingSound; use pretty_assertions::assert_eq; use std::fs::File; use std::io::{Seek, SeekFrom}; #[test] fn test_load_font_data() { - let emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES) } @@ -509,7 +509,7 @@ mod tests { let _ = file.seek(SeekFrom::Start(0)); // Test - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.load_rom(file).expect("failed to load ROM"); // Assert @@ -519,7 +519,7 @@ mod tests { #[test] fn test_execute_clear_screen_instruction() { // Setup - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); for i in 10..30 { emulator.display_data[i] = true; } @@ -538,7 +538,7 @@ mod tests { #[test] fn test_execute_jump() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator .execute_instruction(Instruction::new([0x1A, 0xBC])) @@ -549,7 +549,7 @@ mod tests { #[test] fn test_execute_set_register() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); for i in 0x0..=0xF { let random_data: u8 = rand::thread_rng().gen_range(0x00..0xFF); @@ -562,7 +562,7 @@ mod tests { #[test] fn test_execute_add_value_to_register() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator .execute_instruction(Instruction::new([0x71, 0xCC])) @@ -582,7 +582,7 @@ mod tests { #[test] fn test_execute_set_index_register() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator .execute_instruction(Instruction::new([0xAA, 0xBC])) @@ -593,7 +593,7 @@ mod tests { #[test] fn test_execute_draw() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.index_register = 0xF0; emulator @@ -608,7 +608,7 @@ mod tests { #[test] fn test_execute_call() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.program_counter = 0x200; emulator @@ -621,7 +621,7 @@ mod tests { #[test] fn test_execute_return() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.stack.push(0x269); emulator @@ -633,7 +633,7 @@ mod tests { #[test] fn test_execute_set() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator .execute_instruction(Instruction::new([0x61, 0x40])) @@ -644,7 +644,7 @@ mod tests { #[test] fn test_execute_binary_or() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x6; emulator.registers[0xF] = 0x9; @@ -657,7 +657,7 @@ mod tests { #[test] fn test_execute_binary_and() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x6; emulator.registers[0xF] = 0x9; @@ -670,7 +670,7 @@ mod tests { #[test] fn test_execute_logical_xor() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0xF] = 0x9; @@ -683,7 +683,7 @@ mod tests { #[test] fn test_execute_logical_add() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0xF] = 0x9; @@ -696,7 +696,7 @@ mod tests { #[test] fn test_execute_logical_subtract_vx() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0xE] = 0x9; @@ -709,7 +709,7 @@ mod tests { #[test] fn test_execute_logical_subtract_vy() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0xF] = 0x9; @@ -722,7 +722,7 @@ mod tests { #[test] fn test_execute_logical_shift_left() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0x2] = 0x9; @@ -735,7 +735,7 @@ mod tests { #[test] fn test_execute_logical_shift_right() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x1] = 0x7; emulator.registers[0x2] = 0x9; @@ -748,7 +748,7 @@ mod tests { #[test] fn test_execute_jump_with_offset() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0x0] = 0x2; emulator @@ -760,7 +760,7 @@ mod tests { #[test] fn test_execute_random_number() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator .execute_instruction(Instruction::new([0xCA, 0xBC])) @@ -771,7 +771,7 @@ mod tests { #[test] fn test_execute_skip_equal_vx_data() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xBC; emulator @@ -783,7 +783,7 @@ mod tests { #[test] fn test_execute_skip_not_equal_vx_data() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xB1; emulator @@ -795,7 +795,7 @@ mod tests { #[test] fn test_execute_skip_equal_vx_vy() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xBC; emulator.registers[0xB] = 0xBC; @@ -808,7 +808,7 @@ mod tests { #[test] fn test_execute_skip_not_equal_vx_vy() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xBD; emulator.registers[0xB] = 0xBC; @@ -821,7 +821,7 @@ mod tests { #[test] fn test_execute_set_vx_to_delay_timer() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.delay_timer = 0xEE; emulator @@ -833,7 +833,7 @@ mod tests { #[test] fn test_execute_set_delay_timer() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xEE; emulator @@ -845,7 +845,7 @@ mod tests { #[test] fn test_execute_set_sound_timer() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xEE; emulator @@ -857,7 +857,7 @@ mod tests { #[test] fn test_execute_add_to_index() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xEE; emulator @@ -869,7 +869,7 @@ mod tests { #[test] fn test_execute_get_font_character() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xEE; emulator @@ -881,7 +881,7 @@ mod tests { #[test] fn test_execute_bcd_convert() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0xFE; emulator @@ -895,7 +895,7 @@ mod tests { #[test] fn test_execute_store_memory() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.index_register = 0x20; for i in 0..0xF { emulator.registers[i] = (0xF + i) as u8; @@ -915,7 +915,7 @@ mod tests { #[test] fn test_execute_load_memory() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.index_register = 0x20; for i in 0..0xF { emulator.memory[(emulator.index_register + i) as usize] = (0xF + i) as u8; @@ -932,7 +932,7 @@ mod tests { #[test] fn test_execute_get_key_blocking() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.program_counter = 0x10; emulator @@ -950,7 +950,7 @@ mod tests { #[test] fn test_execute_skip_key_pressed() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.program_counter = 0; emulator.registers[0xA] = 0x1; @@ -968,7 +968,7 @@ mod tests { #[test] fn test_execute_skip_key_not_pressed() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.program_counter = 0; emulator.registers[0xA] = 0x1; @@ -986,7 +986,7 @@ mod tests { #[test] fn test_execute_set_vx_to_vy() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.registers[0xA] = 0; emulator.registers[0xB] = 0xEF; @@ -999,7 +999,7 @@ mod tests { #[test] fn test_fetch_instruction() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.program_counter = 0x200; emulator.memory[0x200] = 0x00; emulator.memory[0x201] = 0xEE; @@ -1017,7 +1017,7 @@ mod tests { #[test] fn test_handle_timers() { - let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput); emulator.sound_timer = 10; emulator.delay_timer = 12; @@ -1039,11 +1039,7 @@ mod tests { } } - let mut emulator = Emulator::new( - TerminalDisplay::new(), - TestSound { did_beep: false }, - NoInput, - ); + let mut emulator = Emulator::new(TestingDisplay, TestSound { did_beep: false }, NoInput); emulator.sound_timer = 0; emulator.handle_timers(); diff --git a/chip8_core/src/sound.rs b/chip8_core/src/sound.rs index 35a0b5f..2a9f0c3 100644 --- a/chip8_core/src/sound.rs +++ b/chip8_core/src/sound.rs @@ -3,3 +3,10 @@ pub trait SoundModule { /// beep makes a beep sound. fn beep(&mut self); } + +/// A simple module for testing the sound. +pub(crate) struct TestingSound; + +impl SoundModule for TestingSound { + fn beep(&mut self) {} +}