lemur/src/emulator.rs

430 lines
14 KiB
Rust
Raw Normal View History

2024-11-03 16:32:53 +00:00
use std::{
2024-11-11 05:50:57 +00:00
collections::HashMap,
2024-12-07 05:00:40 +00:00
fs::{self, File},
io::{Read, Seek, SeekFrom, Write},
2024-11-04 14:59:58 +00:00
path::{Path, PathBuf},
2024-11-05 03:18:57 +00:00
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
2024-11-10 21:08:49 +00:00
mpsc::{self, RecvError, TryRecvError},
2024-11-05 03:18:57 +00:00
Arc,
},
2024-11-03 16:32:53 +00:00
};
use anyhow::Result;
use crate::{audio::Audio, graphics::TextureSink};
2024-11-11 05:50:57 +00:00
pub use shrooms_vb_core::VBKey;
2024-11-24 01:17:28 +00:00
use shrooms_vb_core::{Sim, EXPECTED_FRAME_SIZE};
2024-11-11 05:50:57 +00:00
mod shrooms_vb_core;
2024-11-04 14:59:58 +00:00
pub struct EmulatorBuilder {
rom: Option<PathBuf>,
commands: mpsc::Receiver<EmulatorCommand>,
sim_count: Arc<AtomicUsize>,
running: Arc<[AtomicBool; 2]>,
has_game: Arc<[AtomicBool; 2]>,
2024-11-24 01:17:28 +00:00
audio_on: Arc<[AtomicBool; 2]>,
linked: Arc<AtomicBool>,
2024-11-04 14:59:58 +00:00
}
2024-11-11 05:50:57 +00:00
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum SimId {
Player1,
Player2,
}
impl SimId {
pub const fn values() -> [Self; 2] {
[Self::Player1, Self::Player2]
}
pub const fn to_index(self) -> usize {
2024-11-11 05:50:57 +00:00
match self {
Self::Player1 => 0,
Self::Player2 => 1,
}
}
}
2024-12-07 05:00:40 +00:00
struct Cart {
rom_path: PathBuf,
rom: Vec<u8>,
sram_file: File,
sram: Vec<u8>,
}
impl Cart {
fn load(rom_path: &Path, sim_id: SimId) -> Result<Self> {
let rom = fs::read(rom_path)?;
let mut sram_file = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(sram_path(rom_path, sim_id))?;
sram_file.set_len(8 * 1024)?;
let mut sram = vec![];
sram_file.read_to_end(&mut sram)?;
Ok(Cart {
rom_path: rom_path.to_path_buf(),
rom,
sram_file,
sram,
})
}
}
fn sram_path(rom_path: &Path, sim_id: SimId) -> PathBuf {
match sim_id {
SimId::Player1 => rom_path.with_extension(".p1.sram"),
SimId::Player2 => rom_path.with_extension(".p2.sram"),
}
}
2024-11-04 14:59:58 +00:00
impl EmulatorBuilder {
pub fn new() -> (Self, EmulatorClient) {
let (queue, commands) = mpsc::channel();
let builder = Self {
rom: None,
commands,
sim_count: Arc::new(AtomicUsize::new(0)),
running: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
has_game: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
2024-11-24 01:17:28 +00:00
audio_on: Arc::new([AtomicBool::new(true), AtomicBool::new(true)]),
linked: Arc::new(AtomicBool::new(false)),
2024-11-05 03:18:57 +00:00
};
let client = EmulatorClient {
queue,
sim_count: builder.sim_count.clone(),
2024-11-05 03:18:57 +00:00
running: builder.running.clone(),
has_game: builder.has_game.clone(),
2024-11-24 01:17:28 +00:00
audio_on: builder.audio_on.clone(),
linked: builder.linked.clone(),
2024-11-04 14:59:58 +00:00
};
(builder, client)
}
pub fn with_rom(self, path: &Path) -> Self {
Self {
rom: Some(path.into()),
..self
}
}
pub fn build(self) -> Result<Emulator> {
let mut emulator = Emulator::new(
self.commands,
self.sim_count,
self.running,
self.has_game,
2024-11-24 01:17:28 +00:00
self.audio_on,
self.linked,
)?;
2024-11-04 14:59:58 +00:00
if let Some(path) = self.rom {
2024-12-07 05:00:40 +00:00
emulator.load_cart(SimId::Player1, &path)?;
2024-11-04 14:59:58 +00:00
}
Ok(emulator)
}
}
2024-11-03 16:32:53 +00:00
pub struct Emulator {
2024-11-11 05:50:57 +00:00
sims: Vec<Sim>,
2024-12-07 05:00:40 +00:00
carts: [Option<Cart>; 2],
2024-11-04 14:59:58 +00:00
audio: Audio,
2024-11-03 16:32:53 +00:00
commands: mpsc::Receiver<EmulatorCommand>,
sim_count: Arc<AtomicUsize>,
running: Arc<[AtomicBool; 2]>,
has_game: Arc<[AtomicBool; 2]>,
2024-11-24 01:17:28 +00:00
audio_on: Arc<[AtomicBool; 2]>,
linked: Arc<AtomicBool>,
renderers: HashMap<SimId, TextureSink>,
2024-11-03 16:32:53 +00:00
}
impl Emulator {
fn new(
commands: mpsc::Receiver<EmulatorCommand>,
sim_count: Arc<AtomicUsize>,
running: Arc<[AtomicBool; 2]>,
has_game: Arc<[AtomicBool; 2]>,
2024-11-24 01:17:28 +00:00
audio_on: Arc<[AtomicBool; 2]>,
linked: Arc<AtomicBool>,
) -> Result<Self> {
2024-11-04 14:59:58 +00:00
Ok(Self {
2024-11-11 05:50:57 +00:00
sims: vec![],
2024-12-07 05:00:40 +00:00
carts: [None, None],
2024-11-04 14:59:58 +00:00
audio: Audio::init()?,
commands,
sim_count,
2024-11-05 03:18:57 +00:00
running,
has_game,
2024-11-24 01:17:28 +00:00
audio_on,
linked,
renderers: HashMap::new(),
2024-11-04 14:59:58 +00:00
})
2024-11-03 16:32:53 +00:00
}
2024-12-07 05:00:40 +00:00
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
let cart = Cart::load(path, sim_id)?;
self.reset_sim(sim_id, Some(cart))?;
2024-11-11 05:50:57 +00:00
Ok(())
}
pub fn start_second_sim(&mut self, rom: Option<PathBuf>) -> Result<()> {
2024-12-07 05:00:40 +00:00
let rom_path = if let Some(path) = rom {
Some(path)
2024-11-11 05:50:57 +00:00
} else {
2024-12-07 05:00:40 +00:00
self.carts[0].as_ref().map(|c| c.rom_path.clone())
};
let cart = match rom_path {
Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2)?),
None => None,
2024-11-11 05:50:57 +00:00
};
2024-12-07 05:00:40 +00:00
self.reset_sim(SimId::Player2, cart)?;
self.link_sims();
2024-11-11 05:50:57 +00:00
Ok(())
}
2024-12-07 05:00:40 +00:00
fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
let index = sim_id.to_index();
while self.sims.len() <= index {
2024-11-11 05:50:57 +00:00
self.sims.push(Sim::new());
}
self.sim_count.store(self.sims.len(), Ordering::Relaxed);
let sim = &mut self.sims[index];
2024-11-11 05:50:57 +00:00
sim.reset();
2024-12-07 05:00:40 +00:00
if let Some(cart) = new_cart {
sim.load_cart(cart.rom.clone(), cart.sram.clone())?;
self.carts[index] = Some(cart);
self.has_game[index].store(true, Ordering::Release);
}
if self.has_game[index].load(Ordering::Acquire) {
self.running[index].store(true, Ordering::Release);
}
2024-11-03 16:32:53 +00:00
Ok(())
}
fn link_sims(&mut self) {
let (first, second) = self.sims.split_at_mut(1);
let Some(first) = first.first_mut() else {
return;
};
let Some(second) = second.first_mut() else {
return;
};
first.link(second);
self.linked.store(true, Ordering::Release);
}
fn unlink_sims(&mut self) {
let Some(first) = self.sims.first_mut() else {
return;
};
first.unlink();
self.linked.store(false, Ordering::Release);
}
2024-12-07 05:00:40 +00:00
pub fn pause_sim(&mut self, sim_id: SimId) -> Result<()> {
self.running[sim_id.to_index()].store(false, Ordering::Release);
let sim = self.sims.get_mut(sim_id.to_index());
let cart = self.carts[sim_id.to_index()].as_mut();
if let (Some(sim), Some(cart)) = (sim, cart) {
sim.read_sram(&mut cart.sram);
cart.sram_file.seek(SeekFrom::Start(0))?;
cart.sram_file.write_all(&cart.sram)?;
}
Ok(())
}
2024-11-11 05:50:57 +00:00
pub fn stop_second_sim(&mut self) {
self.renderers.remove(&SimId::Player2);
self.sims.truncate(1);
self.sim_count.store(self.sims.len(), Ordering::Relaxed);
self.running[SimId::Player2.to_index()].store(false, Ordering::Release);
self.has_game[SimId::Player2.to_index()].store(false, Ordering::Release);
self.linked.store(false, Ordering::Release);
2024-11-11 05:50:57 +00:00
}
2024-11-03 16:32:53 +00:00
pub fn run(&mut self) {
let mut eye_contents = vec![0u8; 384 * 224 * 2];
2024-11-04 14:59:58 +00:00
let mut audio_samples = vec![];
2024-11-03 16:32:53 +00:00
loop {
let p1_running = self.running[SimId::Player1.to_index()].load(Ordering::Acquire);
let p2_running = self.running[SimId::Player2.to_index()].load(Ordering::Acquire);
let mut idle = p1_running || p2_running;
if p1_running && p2_running {
2024-11-11 05:50:57 +00:00
Sim::emulate_many(&mut self.sims);
} else if p1_running {
self.sims[SimId::Player1.to_index()].emulate();
} else if p2_running {
self.sims[SimId::Player2.to_index()].emulate();
2024-11-05 03:18:57 +00:00
}
for sim_id in SimId::values() {
let Some(renderer) = self.renderers.get_mut(&sim_id) else {
continue;
};
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
continue;
};
if sim.read_pixels(&mut eye_contents) {
idle = false;
if renderer.queue_render(&eye_contents).is_err() {
self.renderers.remove(&sim_id);
2024-11-11 05:50:57 +00:00
}
2024-11-03 16:32:53 +00:00
}
}
2024-11-24 01:17:28 +00:00
let p1_audio =
p1_running && self.audio_on[SimId::Player1.to_index()].load(Ordering::Acquire);
let p2_audio =
p2_running && self.audio_on[SimId::Player2.to_index()].load(Ordering::Acquire);
let weight = if p1_audio && p2_audio { 0.5 } else { 1.0 };
if p1_audio {
if let Some(sim) = self.sims.get_mut(SimId::Player1.to_index()) {
sim.read_samples(&mut audio_samples, weight);
}
}
if p2_audio {
if let Some(sim) = self.sims.get_mut(SimId::Player2.to_index()) {
sim.read_samples(&mut audio_samples, weight);
}
2024-11-11 05:50:57 +00:00
}
2024-11-24 01:17:28 +00:00
if audio_samples.is_empty() {
audio_samples.resize(EXPECTED_FRAME_SIZE, 0.0);
} else {
2024-11-10 21:08:49 +00:00
idle = false;
2024-11-04 14:59:58 +00:00
}
2024-11-24 01:17:28 +00:00
self.audio.update(&audio_samples);
audio_samples.clear();
2024-11-10 21:08:49 +00:00
if idle {
// The game is paused, and we have output all the video/audio we have.
// Block the thread until a new command comes in.
match self.commands.recv() {
Ok(command) => self.handle_command(command),
Err(RecvError) => {
return;
}
}
}
2024-11-03 16:32:53 +00:00
loop {
match self.commands.try_recv() {
Ok(command) => self.handle_command(command),
Err(TryRecvError::Empty) => {
break;
}
Err(TryRecvError::Disconnected) => {
return;
}
}
}
}
}
fn handle_command(&mut self, command: EmulatorCommand) {
match command {
2024-11-11 05:50:57 +00:00
EmulatorCommand::SetRenderer(sim_id, renderer) => {
self.renderers.insert(sim_id, renderer);
2024-11-03 16:32:53 +00:00
}
2024-11-11 05:50:57 +00:00
EmulatorCommand::LoadGame(sim_id, path) => {
2024-12-07 05:00:40 +00:00
if let Err(error) = self.load_cart(sim_id, &path) {
2024-11-05 03:18:57 +00:00
eprintln!("error loading rom: {}", error);
}
}
2024-11-11 05:50:57 +00:00
EmulatorCommand::StartSecondSim(path) => {
if let Err(error) = self.start_second_sim(path) {
eprintln!("error starting second sim: {}", error);
}
}
EmulatorCommand::StopSecondSim => {
self.stop_second_sim();
}
2024-11-05 03:18:57 +00:00
EmulatorCommand::Pause => {
2024-12-07 05:00:40 +00:00
for sim_id in SimId::values() {
if let Err(error) = self.pause_sim(sim_id) {
eprintln!("error pausing: {}", error);
}
}
2024-11-05 03:18:57 +00:00
}
EmulatorCommand::Resume => {
for sim_id in SimId::values() {
let index = sim_id.to_index();
if self.has_game[index].load(Ordering::Acquire) {
self.running[index].store(true, Ordering::Relaxed);
}
2024-11-05 03:18:57 +00:00
}
}
2024-11-24 01:17:28 +00:00
EmulatorCommand::SetAudioEnabled(p1, p2) => {
self.audio_on[SimId::Player1.to_index()].store(p1, Ordering::Release);
self.audio_on[SimId::Player2.to_index()].store(p2, Ordering::Release);
}
EmulatorCommand::Link => {
self.link_sims();
}
EmulatorCommand::Unlink => {
self.unlink_sims();
}
EmulatorCommand::Reset(sim_id) => {
if let Err(error) = self.reset_sim(sim_id, None) {
eprintln!("error resetting sim: {}", error);
2024-11-11 05:50:57 +00:00
}
2024-11-05 03:18:57 +00:00
}
2024-11-11 05:50:57 +00:00
EmulatorCommand::SetKeys(sim_id, keys) => {
if let Some(sim) = self.sims.get_mut(sim_id.to_index()) {
sim.set_keys(keys);
}
2024-11-03 16:32:53 +00:00
}
}
}
}
#[derive(Debug)]
pub enum EmulatorCommand {
SetRenderer(SimId, TextureSink),
2024-11-11 05:50:57 +00:00
LoadGame(SimId, PathBuf),
StartSecondSim(Option<PathBuf>),
StopSecondSim,
2024-11-05 03:18:57 +00:00
Pause,
Resume,
2024-11-24 01:17:28 +00:00
SetAudioEnabled(bool, bool),
Link,
Unlink,
Reset(SimId),
2024-11-11 05:50:57 +00:00
SetKeys(SimId, VBKey),
2024-11-03 16:32:53 +00:00
}
#[derive(Clone)]
2024-11-03 16:32:53 +00:00
pub struct EmulatorClient {
queue: mpsc::Sender<EmulatorCommand>,
sim_count: Arc<AtomicUsize>,
running: Arc<[AtomicBool; 2]>,
has_game: Arc<[AtomicBool; 2]>,
2024-11-24 01:17:28 +00:00
audio_on: Arc<[AtomicBool; 2]>,
linked: Arc<AtomicBool>,
2024-11-03 16:32:53 +00:00
}
impl EmulatorClient {
pub fn has_player_2(&self) -> bool {
self.sim_count.load(Ordering::Acquire) == 2
}
pub fn is_running(&self, sim_id: SimId) -> bool {
self.running[sim_id.to_index()].load(Ordering::Acquire)
}
pub fn has_game(&self, sim_id: SimId) -> bool {
self.has_game[sim_id.to_index()].load(Ordering::Acquire)
2024-11-05 03:18:57 +00:00
}
pub fn are_sims_linked(&self) -> bool {
self.linked.load(Ordering::Acquire)
}
2024-11-24 01:17:28 +00:00
pub fn is_audio_enabled(&self, sim_id: SimId) -> bool {
self.audio_on[sim_id.to_index()].load(Ordering::Acquire)
}
2024-11-03 16:32:53 +00:00
pub fn send_command(&self, command: EmulatorCommand) {
if let Err(err) = self.queue.send(command) {
eprintln!(
"could not send command {:?} as emulator is shut down",
err.0
);
}
}
}