2023-03-08 11:01:18 +11:00
|
|
|
use crate::processor::memory::mmio::gpu::Colour;
|
2023-03-08 15:19:10 +11:00
|
|
|
pub use crate::processor::memory::mmio::joypad::{JoypadButtons, JoypadState};
|
2023-03-07 18:05:06 +11:00
|
|
|
pub use crate::{HEIGHT, WIDTH};
|
2023-03-03 18:08:28 +11:00
|
|
|
use async_ringbuf::{AsyncHeapConsumer, AsyncHeapProducer, AsyncHeapRb};
|
|
|
|
use futures::executor;
|
2023-03-02 10:20:50 +11:00
|
|
|
|
2023-03-02 11:29:54 +11:00
|
|
|
pub enum EmulatorMessage {
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:23:46 +11:00
|
|
|
pub enum RomFile {
|
|
|
|
Path(String),
|
|
|
|
Raw(Vec<u8>),
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:28:32 +11:00
|
|
|
#[cfg(feature = "async")]
|
2023-03-08 11:01:18 +11:00
|
|
|
pub trait Renderer<Format: From<Colour>>: Send {
|
2023-03-02 10:20:50 +11:00
|
|
|
fn prepare(&mut self, width: usize, height: usize);
|
|
|
|
|
2023-03-08 11:01:18 +11:00
|
|
|
fn display(&mut self, buffer: &[Format]);
|
2023-03-02 10:20:50 +11:00
|
|
|
|
|
|
|
fn set_title(&mut self, _title: String) {}
|
|
|
|
|
|
|
|
fn latest_joypad_state(&mut self) -> JoypadState;
|
|
|
|
|
|
|
|
fn set_rumble(&mut self, _rumbling: bool) {}
|
|
|
|
}
|
2023-03-03 18:08:28 +11:00
|
|
|
|
2023-03-08 11:28:32 +11:00
|
|
|
#[cfg(not(feature = "async"))]
|
|
|
|
pub trait Renderer<Format: From<Colour>> {
|
|
|
|
fn prepare(&mut self, width: usize, height: usize);
|
|
|
|
|
|
|
|
fn display(&mut self, buffer: &[Format]);
|
|
|
|
|
|
|
|
fn set_title(&mut self, _title: String) {}
|
|
|
|
|
|
|
|
fn latest_joypad_state(&mut self) -> JoypadState;
|
|
|
|
|
|
|
|
fn set_rumble(&mut self, _rumbling: bool) {}
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:08:28 +11:00
|
|
|
pub struct AudioOutput {
|
2023-03-06 21:10:52 +11:00
|
|
|
pub sample_rate: f32,
|
|
|
|
pub send_rb: AsyncHeapProducer<[f32; 2]>,
|
2023-03-08 11:28:32 +11:00
|
|
|
pub wait_for_output: bool,
|
2023-03-03 18:08:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioOutput {
|
2023-03-08 11:28:32 +11:00
|
|
|
pub fn new(sample_rate: f32, wait_for_output: bool) -> (Self, AsyncHeapConsumer<[f32; 2]>) {
|
|
|
|
let (mut output, rx) = Self::new_unfilled(sample_rate, wait_for_output);
|
2023-03-07 08:51:23 +11:00
|
|
|
|
|
|
|
executor::block_on(
|
|
|
|
output
|
|
|
|
.send_rb
|
2023-03-08 11:28:32 +11:00
|
|
|
.push_iter(vec![[0.; 2]; output.send_rb.free_len() - 1].into_iter()),
|
2023-03-07 08:51:23 +11:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(output, rx)
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:28:32 +11:00
|
|
|
pub fn new_unfilled(
|
|
|
|
sample_rate: f32,
|
|
|
|
wait_for_output: bool,
|
|
|
|
) -> (Self, AsyncHeapConsumer<[f32; 2]>) {
|
2023-03-06 21:10:52 +11:00
|
|
|
let rb_len = sample_rate as usize / (60 * 2);
|
2023-03-03 18:08:28 +11:00
|
|
|
|
2023-03-06 21:10:52 +11:00
|
|
|
let rb = AsyncHeapRb::<[f32; 2]>::new(rb_len);
|
2023-03-07 08:51:23 +11:00
|
|
|
let (send_rb, rx) = rb.split();
|
2023-03-03 18:08:28 +11:00
|
|
|
|
|
|
|
(
|
|
|
|
Self {
|
|
|
|
sample_rate,
|
|
|
|
send_rb,
|
2023-03-08 11:28:32 +11:00
|
|
|
wait_for_output,
|
2023-03-03 18:08:28 +11:00
|
|
|
},
|
|
|
|
rx,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|