From abad89d4d0c417d4fde9aea69903f9894af6739e Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Wed, 6 Mar 2024 08:18:07 +0530 Subject: [PATCH] [cln] Code cleanup --- src/device/device.rs | 6 +----- src/device/keyboard.rs | 4 +--- src/device/timer.rs | 6 +++--- src/main.rs | 10 ++++------ src/sdl_audio_adapter.rs | 4 ++-- src/sdl_graphics_adapter.rs | 2 +- src/sdl_keyboard_adapter.rs | 2 ++ src/util.rs | 3 +-- 8 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/device/device.rs b/src/device/device.rs index 2cdd472..12b886a 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -1,9 +1,5 @@ -use std::ops::SubAssign; -use std::sync::atomic::{AtomicU16, Ordering}; -use std::sync::{Arc, Mutex, RwLock}; -use std::thread; +use std::sync::{Arc, Mutex}; use std::thread::{JoinHandle, sleep}; -use std::time::Duration; use rand::random; use crate::device::instruction::Instruction; use crate::device::keyboard::Keyboard; diff --git a/src/device/keyboard.rs b/src/device/keyboard.rs index bc44001..90268bd 100644 --- a/src/device/keyboard.rs +++ b/src/device/keyboard.rs @@ -32,7 +32,7 @@ impl Keyboard { let keyboard_event_recv_res = self.keyboard_event_receiver.try_recv(); match keyboard_event_recv_res { Ok(event) => { - log::warn!("Processing {:?}",event); + log::debug!("Processing {:?}",event); self.update_keyboard_state(event); } Err(TryRecvError::Empty) => { @@ -54,11 +54,9 @@ impl Keyboard { fn update_keyboard_state(&mut self, keyboard_event: KeyboardEvent) { match keyboard_event { KeyboardEvent::KeyUp(key) => { - log::debug!("Key Up - state {}",self.bitflags); self.bitflags &= !((1 << key) as u16); } KeyboardEvent::KeyDown(key) => { - log::debug!("Key Down - state {}",self.bitflags); self.bitflags |= 1 << key; } } diff --git a/src/device/timer.rs b/src/device/timer.rs index 5ea3869..b325c52 100644 --- a/src/device/timer.rs +++ b/src/device/timer.rs @@ -1,9 +1,9 @@ -use std::f32::consts::PI; -use std::sync::{Arc, Mutex, RwLock}; + +use std::sync::{Arc, Mutex}; use std::sync::mpsc::SendError; use std::thread::{JoinHandle, sleep}; use std::time::Duration; -use sdl2::audio::AudioQueue; + use crate::util::EmulatorResult; /// Manages the timer and the sound timer diff --git a/src/main.rs b/src/main.rs index eaf12a1..fa88ed6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ mod sdl_audio_adapter; fn main() -> EmulatorResult<()> { SimpleLogger::new().with_level(LevelFilter::Info).env().init().unwrap(); - let Porcel8ProgramArgs { filename, new_chip8_behaviour: new_chip_behaviour,draw_scale } = Porcel8ProgramArgs::parse(); + let Porcel8ProgramArgs { filename, new_chip8_behaviour,draw_scale } = Porcel8ProgramArgs::parse(); log::info!("Started emulator"); let mut timer = TimerManager::new(); @@ -45,7 +45,7 @@ fn main() -> EmulatorResult<()> { let audio_state = timer.start(); - let mut sdl_aud_adapter = SdlAudioAdapter::new(audio_state,440.0,0.5,audio_queue); + let mut sdl_aud_adapter = SdlAudioAdapter::new(audio_state,440.0,0.85,audio_queue); let (frame_buffer_for_display, frame_buffer_for_device) = get_frame_buffer_references(); @@ -53,7 +53,7 @@ fn main() -> EmulatorResult<()> { let (device_termination_signal_sender, device_termination_signal_sender_receiver) = std::sync::mpsc::channel(); let compute_handle = thread::Builder::new().name("Compute".to_string()).spawn(move || { - do_device_loop(timer, frame_buffer_for_device, device_termination_signal_sender_receiver, device_keyboard, filename, new_chip_behaviour); + do_device_loop(timer, frame_buffer_for_device, device_termination_signal_sender_receiver, device_keyboard, filename, new_chip8_behaviour); })?; @@ -73,13 +73,11 @@ fn main() -> EmulatorResult<()> { Event::KeyDown { keycode: Some(x), repeat: false, .. } => { if let Some(key_val) = get_key_index(x) { sdl_kb_adapter.send_key_down(key_val)?; - log::info!("Key+ {}",key_val) } } Event::KeyUp { keycode: Some(x), repeat: false, .. } => { if let Some(key_val) = get_key_index(x) { sdl_kb_adapter.send_key_up(key_val)?; - log::info!("Key- {}",key_val) } } _ => {} @@ -102,7 +100,7 @@ fn main() -> EmulatorResult<()> { Ok(()) } -fn do_device_loop(mut timer: TimerManager, frame_buffer: Arc>>, receiver: Receiver<()>, device_keyboard: Keyboard, rom_file_location_option: Option, new_chip_behaviour: bool) { +fn do_device_loop(timer: TimerManager, frame_buffer: Arc>>, receiver: Receiver<()>, device_keyboard: Keyboard, rom_file_location_option: Option, new_chip_behaviour: bool) { let mut device = Device::new(timer, frame_buffer, device_keyboard, new_chip_behaviour); device.set_default_font(); diff --git a/src/sdl_audio_adapter.rs b/src/sdl_audio_adapter.rs index f4f0757..855b811 100644 --- a/src/sdl_audio_adapter.rs +++ b/src/sdl_audio_adapter.rs @@ -14,7 +14,7 @@ pub struct SdlAudioAdapter { /// An Audio adapter using `AudioQueue`. impl SdlAudioAdapter { pub const SAMPLING_FREQ:i32 = 15360; - pub const SAMPLES_PER_FRAME: usize = Self::SAMPLING_FREQ as usize / 60 + 2; + pub const SAMPLES_PER_FRAME: usize = (Self::SAMPLING_FREQ as usize / 60) * 2; pub fn new(sound_timer: Arc>, freq: f32, volume: f32, @@ -35,7 +35,7 @@ impl SdlAudioAdapter { let sound_timer = self.sound_timer.lock().expect("Could not lock to play audio"); sound_timer.clone() }; - if sound_timer>0 { + if sound_timer>0 && self.audio_queue.size() < Self::SAMPLING_FREQ as u32 { self.fill_audio(); self.audio_queue.queue_audio(&self.buf)?; } diff --git a/src/sdl_graphics_adapter.rs b/src/sdl_graphics_adapter.rs index 874ba5c..1fe030d 100644 --- a/src/sdl_graphics_adapter.rs +++ b/src/sdl_graphics_adapter.rs @@ -30,7 +30,7 @@ impl SdlGraphicsAdapter { let tex_creator = window_canvas.texture_creator(); let mut tex = tex_creator.create_texture(PixelFormatEnum::RGB24, TextureAccess::Streaming, Device::FRAME_BUFFER_WIDTH as u32, Device::FRAME_BUFFER_HEIGHT as u32).expect("Failed to create tex"); - tex.with_lock(None, |u, i| { + tex.with_lock(None, |u, _i| { u.copy_from_slice(self.rgb_frame_buffer.as_slice()); })?; window_canvas.copy(&tex, None, None)?; diff --git a/src/sdl_keyboard_adapter.rs b/src/sdl_keyboard_adapter.rs index f9613a9..572f5e0 100644 --- a/src/sdl_keyboard_adapter.rs +++ b/src/sdl_keyboard_adapter.rs @@ -23,10 +23,12 @@ impl SdlKeyboardAdapter { } pub fn send_key_up(&self, keycode: u8) -> EmulatorResult { + log::trace!("Sending Key up {}",keycode); self.keyboard_event_sender.send(KeyUp(keycode))?; Ok(keycode) } pub fn send_key_down(&self, keycode: u8) -> EmulatorResult { + log::trace!("Sending Key down {}",keycode); self.keyboard_event_sender.send(KeyDown(keycode))?; Ok(keycode) } diff --git a/src/util.rs b/src/util.rs index 5d84b88..c826a93 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,7 +10,6 @@ pub type EmulatorResult = Result; #[derive(Clone, Debug)] pub enum EmulatorError { SdlError(String), - AllocationError, IOError(String), MutexInvalidState(String) } @@ -48,6 +47,6 @@ impl From> for EmulatorError{ impl From> for EmulatorError{ fn from(value: SendError) -> Self { - Self::IOError(format!("Failed to communicate keyboard stats to main thread: {}",value)) + Self::IOError(format!("Failed to communicate keyboard event to main thread: {}",value)) } } \ No newline at end of file