[cln] Code cleanup

This commit is contained in:
2024-03-06 08:18:07 +05:30
parent ed7c6d3e1c
commit abad89d4d0
8 changed files with 15 additions and 22 deletions

View File

@@ -1,9 +1,5 @@
use std::ops::SubAssign; use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::thread::{JoinHandle, sleep}; use std::thread::{JoinHandle, sleep};
use std::time::Duration;
use rand::random; use rand::random;
use crate::device::instruction::Instruction; use crate::device::instruction::Instruction;
use crate::device::keyboard::Keyboard; use crate::device::keyboard::Keyboard;

View File

@@ -32,7 +32,7 @@ impl Keyboard {
let keyboard_event_recv_res = self.keyboard_event_receiver.try_recv(); let keyboard_event_recv_res = self.keyboard_event_receiver.try_recv();
match keyboard_event_recv_res { match keyboard_event_recv_res {
Ok(event) => { Ok(event) => {
log::warn!("Processing {:?}",event); log::debug!("Processing {:?}",event);
self.update_keyboard_state(event); self.update_keyboard_state(event);
} }
Err(TryRecvError::Empty) => { Err(TryRecvError::Empty) => {
@@ -54,11 +54,9 @@ impl Keyboard {
fn update_keyboard_state(&mut self, keyboard_event: KeyboardEvent) { fn update_keyboard_state(&mut self, keyboard_event: KeyboardEvent) {
match keyboard_event { match keyboard_event {
KeyboardEvent::KeyUp(key) => { KeyboardEvent::KeyUp(key) => {
log::debug!("Key Up - state {}",self.bitflags);
self.bitflags &= !((1 << key) as u16); self.bitflags &= !((1 << key) as u16);
} }
KeyboardEvent::KeyDown(key) => { KeyboardEvent::KeyDown(key) => {
log::debug!("Key Down - state {}",self.bitflags);
self.bitflags |= 1 << key; self.bitflags |= 1 << key;
} }
} }

View File

@@ -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::sync::mpsc::SendError;
use std::thread::{JoinHandle, sleep}; use std::thread::{JoinHandle, sleep};
use std::time::Duration; use std::time::Duration;
use sdl2::audio::AudioQueue;
use crate::util::EmulatorResult; use crate::util::EmulatorResult;
/// Manages the timer and the sound timer /// Manages the timer and the sound timer

View File

@@ -35,7 +35,7 @@ mod sdl_audio_adapter;
fn main() -> EmulatorResult<()> { fn main() -> EmulatorResult<()> {
SimpleLogger::new().with_level(LevelFilter::Info).env().init().unwrap(); 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"); log::info!("Started emulator");
let mut timer = TimerManager::new(); let mut timer = TimerManager::new();
@@ -45,7 +45,7 @@ fn main() -> EmulatorResult<()> {
let audio_state = timer.start(); 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(); 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 (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 || { 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, .. } => { Event::KeyDown { keycode: Some(x), repeat: false, .. } => {
if let Some(key_val) = get_key_index(x) { if let Some(key_val) = get_key_index(x) {
sdl_kb_adapter.send_key_down(key_val)?; sdl_kb_adapter.send_key_down(key_val)?;
log::info!("Key+ {}",key_val)
} }
} }
Event::KeyUp { keycode: Some(x), repeat: false, .. } => { Event::KeyUp { keycode: Some(x), repeat: false, .. } => {
if let Some(key_val) = get_key_index(x) { if let Some(key_val) = get_key_index(x) {
sdl_kb_adapter.send_key_up(key_val)?; sdl_kb_adapter.send_key_up(key_val)?;
log::info!("Key- {}",key_val)
} }
} }
_ => {} _ => {}
@@ -102,7 +100,7 @@ fn main() -> EmulatorResult<()> {
Ok(()) Ok(())
} }
fn do_device_loop(mut timer: TimerManager, frame_buffer: Arc<Mutex<Box<[bool; 2048]>>>, receiver: Receiver<()>, device_keyboard: Keyboard, rom_file_location_option: Option<String>, new_chip_behaviour: bool) { fn do_device_loop(timer: TimerManager, frame_buffer: Arc<Mutex<Box<[bool; 2048]>>>, receiver: Receiver<()>, device_keyboard: Keyboard, rom_file_location_option: Option<String>, new_chip_behaviour: bool) {
let mut device = Device::new(timer, frame_buffer, device_keyboard, new_chip_behaviour); let mut device = Device::new(timer, frame_buffer, device_keyboard, new_chip_behaviour);
device.set_default_font(); device.set_default_font();

View File

@@ -14,7 +14,7 @@ pub struct SdlAudioAdapter {
/// An Audio adapter using `AudioQueue`. /// An Audio adapter using `AudioQueue`.
impl SdlAudioAdapter { impl SdlAudioAdapter {
pub const SAMPLING_FREQ:i32 = 15360; 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<Mutex<u8>>, pub fn new(sound_timer: Arc<Mutex<u8>>,
freq: f32, freq: f32,
volume: f32, volume: f32,
@@ -35,7 +35,7 @@ impl SdlAudioAdapter {
let sound_timer = self.sound_timer.lock().expect("Could not lock to play audio"); let sound_timer = self.sound_timer.lock().expect("Could not lock to play audio");
sound_timer.clone() sound_timer.clone()
}; };
if sound_timer>0 { if sound_timer>0 && self.audio_queue.size() < Self::SAMPLING_FREQ as u32 {
self.fill_audio(); self.fill_audio();
self.audio_queue.queue_audio(&self.buf)?; self.audio_queue.queue_audio(&self.buf)?;
} }

View File

@@ -30,7 +30,7 @@ impl SdlGraphicsAdapter {
let tex_creator = window_canvas.texture_creator(); 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"); 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()); u.copy_from_slice(self.rgb_frame_buffer.as_slice());
})?; })?;
window_canvas.copy(&tex, None, None)?; window_canvas.copy(&tex, None, None)?;

View File

@@ -23,10 +23,12 @@ impl SdlKeyboardAdapter {
} }
pub fn send_key_up(&self, keycode: u8) -> EmulatorResult<u8> { pub fn send_key_up(&self, keycode: u8) -> EmulatorResult<u8> {
log::trace!("Sending Key up {}",keycode);
self.keyboard_event_sender.send(KeyUp(keycode))?; self.keyboard_event_sender.send(KeyUp(keycode))?;
Ok(keycode) Ok(keycode)
} }
pub fn send_key_down(&self, keycode: u8) -> EmulatorResult<u8> { pub fn send_key_down(&self, keycode: u8) -> EmulatorResult<u8> {
log::trace!("Sending Key down {}",keycode);
self.keyboard_event_sender.send(KeyDown(keycode))?; self.keyboard_event_sender.send(KeyDown(keycode))?;
Ok(keycode) Ok(keycode)
} }

View File

@@ -10,7 +10,6 @@ pub type EmulatorResult<T> = Result<T, EmulatorError>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum EmulatorError { pub enum EmulatorError {
SdlError(String), SdlError(String),
AllocationError,
IOError(String), IOError(String),
MutexInvalidState(String) MutexInvalidState(String)
} }
@@ -48,6 +47,6 @@ impl<T> From<PoisonError<T>> for EmulatorError{
impl From<SendError<KeyboardEvent>> for EmulatorError{ impl From<SendError<KeyboardEvent>> for EmulatorError{
fn from(value: SendError<KeyboardEvent>) -> Self { fn from(value: SendError<KeyboardEvent>) -> 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))
} }
} }