From ca97cb7de8a2b9293dc2d258cf408ac63a8f983a Mon Sep 17 00:00:00 2001 From: Denis-Cosmin NUTIU Date: Sun, 8 Dec 2024 12:47:21 +0200 Subject: [PATCH] implement sound module --- src/emulator.rs | 19 +++++++++++++------ src/main.rs | 4 +++- src/sound.rs | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/sound.rs diff --git a/src/emulator.rs b/src/emulator.rs index a0ab2a1..a3f688e 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -1,5 +1,6 @@ use crate::display::Display; use crate::instruction::{Instruction, ProcessorInstruction}; +use crate::sound::SoundModule; use crate::stack::Stack; use anyhow::anyhow; use log::{debug, info, trace, warn}; @@ -38,9 +39,10 @@ const FONT_SPRITES: [u8; 80] = [ ]; /// Emulator emulates the Chip8 CPU. -pub struct Emulator +pub struct Emulator where D: Display, + S: SoundModule, { /// Memory represents the emulator's memory. memory: [u8; MEMORY_SIZE], @@ -59,23 +61,27 @@ where stack_pointer: u8, /// The display_data holds all the data associated with the display display: D, + /// The sound module for making sounds. + sound_module: S, /// The stack of the emulator. stack: Stack, /// Holds the display data, each bit corresponds to a pixel. display_data: [bool; DISPLAY_WIDTH * DISPLAY_HEIGHT], } -impl Emulator +impl Emulator where D: Display, + S: SoundModule, { /// Creates a new `Emulator` instance. /// - pub fn new(display: D) -> Emulator { + pub fn new(display: D, sound_module: S) -> Emulator { let mut emulator = Emulator { memory: [0; MEMORY_SIZE], registers: [0; NUMBER_OF_REGISTERS], display, + sound_module, index_register: 0, program_counter: 0, delay_timer: 0, @@ -153,7 +159,7 @@ where /// Should make an audible beep. fn do_beep(&mut self) { - // beep, for now + self.sound_module.beep(); } /// Executes the instruction @@ -435,11 +441,12 @@ where mod tests { use super::*; use crate::display::TerminalDisplay; + use crate::sound::TerminalSound; use pretty_assertions::assert_eq; #[test] fn test_load_font_data() { - let emulator = Emulator::new(TerminalDisplay::new()); + let emulator = Emulator::new(TerminalDisplay::new(), TerminalSound); assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES) } @@ -452,7 +459,7 @@ mod tests { .expect("Failed to read test ROM"); // Test - let mut emulator = Emulator::new(TerminalDisplay::new()); + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound); emulator .load_rom("roms/ibm-logo.ch8") .expect("failed to load ROM"); diff --git a/src/main.rs b/src/main.rs index 25672e8..e17e50e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,19 @@ use crate::display::RatatuiDisplay; use crate::emulator::Emulator; +use crate::sound::TerminalSound; use env_logger; mod display; mod emulator; mod input; mod instruction; +mod sound; mod stack; fn main() -> Result<(), anyhow::Error> { env_logger::init(); - let mut emulator = Emulator::new(RatatuiDisplay::new()); + let mut emulator = Emulator::new(RatatuiDisplay::new(), TerminalSound); emulator.emulate(String::from("./roms/3-corax+.ch8"))?; diff --git a/src/sound.rs b/src/sound.rs new file mode 100644 index 0000000..d41b617 --- /dev/null +++ b/src/sound.rs @@ -0,0 +1,14 @@ +/// SoundModule represents a module which can produce sound. +pub trait SoundModule { + /// beep makes a beep sound. + fn beep(&mut self); +} + +/// TerminalSound is a simple module that makes terminal beep sound. +pub struct TerminalSound; + +impl SoundModule for TerminalSound { + fn beep(&mut self) { + print!("\x07"); + } +}