[cln] Code cleanup
This commit is contained in:
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
10
src/main.rs
10
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<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);
|
||||
device.set_default_font();
|
||||
|
||||
|
@@ -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<Mutex<u8>>,
|
||||
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)?;
|
||||
}
|
||||
|
@@ -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)?;
|
||||
|
@@ -23,10 +23,12 @@ impl SdlKeyboardAdapter {
|
||||
}
|
||||
|
||||
pub fn send_key_up(&self, keycode: u8) -> EmulatorResult<u8> {
|
||||
log::trace!("Sending Key up {}",keycode);
|
||||
self.keyboard_event_sender.send(KeyUp(keycode))?;
|
||||
Ok(keycode)
|
||||
}
|
||||
pub fn send_key_down(&self, keycode: u8) -> EmulatorResult<u8> {
|
||||
log::trace!("Sending Key down {}",keycode);
|
||||
self.keyboard_event_sender.send(KeyDown(keycode))?;
|
||||
Ok(keycode)
|
||||
}
|
||||
|
@@ -10,7 +10,6 @@ pub type EmulatorResult<T> = Result<T, EmulatorError>;
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum EmulatorError {
|
||||
SdlError(String),
|
||||
AllocationError,
|
||||
IOError(String),
|
||||
MutexInvalidState(String)
|
||||
}
|
||||
@@ -48,6 +47,6 @@ impl<T> From<PoisonError<T>> for EmulatorError{
|
||||
|
||||
impl From<SendError<KeyboardEvent>> for EmulatorError{
|
||||
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))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user