Let sims link/unlink/reset independently
This commit is contained in:
parent
8f62ec1c52
commit
09e39b37f5
|
@ -211,8 +211,8 @@ impl GameWindow {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ui.menu("Emulation", || {
|
ui.menu("Emulation", || {
|
||||||
let has_game = self.client.has_game();
|
let has_game = self.client.has_game(self.sim_id);
|
||||||
if self.client.is_running() {
|
if self.client.is_running(self.sim_id) {
|
||||||
if ui.menu_item_config("Pause").enabled(has_game).build() {
|
if ui.menu_item_config("Pause").enabled(has_game).build() {
|
||||||
self.client.send_command(EmulatorCommand::Pause);
|
self.client.send_command(EmulatorCommand::Pause);
|
||||||
}
|
}
|
||||||
|
@ -220,7 +220,8 @@ impl GameWindow {
|
||||||
self.client.send_command(EmulatorCommand::Resume);
|
self.client.send_command(EmulatorCommand::Resume);
|
||||||
}
|
}
|
||||||
if ui.menu_item_config("Reset").enabled(has_game).build() {
|
if ui.menu_item_config("Reset").enabled(has_game).build() {
|
||||||
self.client.send_command(EmulatorCommand::Reset);
|
self.client
|
||||||
|
.send_command(EmulatorCommand::Reset(self.sim_id));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ui.menu("Video", || {
|
ui.menu("Video", || {
|
||||||
|
@ -243,11 +244,23 @@ impl GameWindow {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ui.menu("Multiplayer", || {
|
ui.menu("Multiplayer", || {
|
||||||
if self.sim_id == SimId::Player1 && ui.menu_item("Open Player 2") {
|
if self.sim_id == SimId::Player1
|
||||||
|
&& !self.client.has_player_2()
|
||||||
|
&& ui.menu_item("Open Player 2")
|
||||||
|
{
|
||||||
self.client
|
self.client
|
||||||
.send_command(EmulatorCommand::StartSecondSim(None));
|
.send_command(EmulatorCommand::StartSecondSim(None));
|
||||||
self.proxy.send_event(UserEvent::OpenPlayer2Window).unwrap();
|
self.proxy.send_event(UserEvent::OpenPlayer2Window).unwrap();
|
||||||
}
|
}
|
||||||
|
if self.client.has_player_2() {
|
||||||
|
let linked = self.client.are_sims_linked();
|
||||||
|
if linked && ui.menu_item("Unlink") {
|
||||||
|
self.client.send_command(EmulatorCommand::Unlink);
|
||||||
|
}
|
||||||
|
if !linked && ui.menu_item("Link") {
|
||||||
|
self.client.send_command(EmulatorCommand::Link);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -326,7 +339,7 @@ impl AppWindow for GameWindow {
|
||||||
WindowEvent::Resized(size) => {
|
WindowEvent::Resized(size) => {
|
||||||
self.window.handle_resize(size);
|
self.window.handle_resize(size);
|
||||||
if self.window.minimized {
|
if self.window.minimized {
|
||||||
if self.client.is_running() {
|
if self.client.is_running(self.sim_id) {
|
||||||
self.client.send_command(EmulatorCommand::Pause);
|
self.client.send_command(EmulatorCommand::Pause);
|
||||||
self.paused_due_to_minimize = true;
|
self.paused_due_to_minimize = true;
|
||||||
}
|
}
|
||||||
|
|
159
src/emulator.rs
159
src/emulator.rs
|
@ -3,7 +3,7 @@ use std::{
|
||||||
fs,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, AtomicUsize, Ordering},
|
||||||
mpsc::{self, RecvError, TryRecvError},
|
mpsc::{self, RecvError, TryRecvError},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
},
|
||||||
|
@ -20,8 +20,10 @@ mod shrooms_vb_core;
|
||||||
pub struct EmulatorBuilder {
|
pub struct EmulatorBuilder {
|
||||||
rom: Option<PathBuf>,
|
rom: Option<PathBuf>,
|
||||||
commands: mpsc::Receiver<EmulatorCommand>,
|
commands: mpsc::Receiver<EmulatorCommand>,
|
||||||
running: Arc<AtomicBool>,
|
sim_count: Arc<AtomicUsize>,
|
||||||
has_game: Arc<AtomicBool>,
|
running: Arc<[AtomicBool; 2]>,
|
||||||
|
has_game: Arc<[AtomicBool; 2]>,
|
||||||
|
linked: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||||
|
@ -47,13 +49,17 @@ impl EmulatorBuilder {
|
||||||
let builder = Self {
|
let builder = Self {
|
||||||
rom: None,
|
rom: None,
|
||||||
commands,
|
commands,
|
||||||
running: Arc::new(AtomicBool::new(false)),
|
sim_count: Arc::new(AtomicUsize::new(0)),
|
||||||
has_game: Arc::new(AtomicBool::new(false)),
|
running: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
|
||||||
|
has_game: Arc::new([AtomicBool::new(false), AtomicBool::new(false)]),
|
||||||
|
linked: Arc::new(AtomicBool::new(false)),
|
||||||
};
|
};
|
||||||
let client = EmulatorClient {
|
let client = EmulatorClient {
|
||||||
queue,
|
queue,
|
||||||
|
sim_count: builder.sim_count.clone(),
|
||||||
running: builder.running.clone(),
|
running: builder.running.clone(),
|
||||||
has_game: builder.has_game.clone(),
|
has_game: builder.has_game.clone(),
|
||||||
|
linked: builder.linked.clone(),
|
||||||
};
|
};
|
||||||
(builder, client)
|
(builder, client)
|
||||||
}
|
}
|
||||||
|
@ -66,9 +72,15 @@ impl EmulatorBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Result<Emulator> {
|
pub fn build(self) -> Result<Emulator> {
|
||||||
let mut emulator = Emulator::new(self.commands, self.running, self.has_game)?;
|
let mut emulator = Emulator::new(
|
||||||
|
self.commands,
|
||||||
|
self.sim_count,
|
||||||
|
self.running,
|
||||||
|
self.has_game,
|
||||||
|
self.linked,
|
||||||
|
)?;
|
||||||
if let Some(path) = self.rom {
|
if let Some(path) = self.rom {
|
||||||
emulator.load_rom_from_file(SimId::Player1, &path)?;
|
emulator.load_rom(SimId::Player1, &path)?;
|
||||||
}
|
}
|
||||||
Ok(emulator)
|
Ok(emulator)
|
||||||
}
|
}
|
||||||
|
@ -78,73 +90,112 @@ pub struct Emulator {
|
||||||
sims: Vec<Sim>,
|
sims: Vec<Sim>,
|
||||||
audio: Audio,
|
audio: Audio,
|
||||||
commands: mpsc::Receiver<EmulatorCommand>,
|
commands: mpsc::Receiver<EmulatorCommand>,
|
||||||
|
sim_count: Arc<AtomicUsize>,
|
||||||
|
running: Arc<[AtomicBool; 2]>,
|
||||||
|
has_game: Arc<[AtomicBool; 2]>,
|
||||||
|
linked: Arc<AtomicBool>,
|
||||||
renderers: HashMap<SimId, TextureSink>,
|
renderers: HashMap<SimId, TextureSink>,
|
||||||
running: Arc<AtomicBool>,
|
|
||||||
has_game: Arc<AtomicBool>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Emulator {
|
impl Emulator {
|
||||||
fn new(
|
fn new(
|
||||||
commands: mpsc::Receiver<EmulatorCommand>,
|
commands: mpsc::Receiver<EmulatorCommand>,
|
||||||
running: Arc<AtomicBool>,
|
sim_count: Arc<AtomicUsize>,
|
||||||
has_game: Arc<AtomicBool>,
|
running: Arc<[AtomicBool; 2]>,
|
||||||
|
has_game: Arc<[AtomicBool; 2]>,
|
||||||
|
linked: Arc<AtomicBool>,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
sims: vec![],
|
sims: vec![],
|
||||||
audio: Audio::init()?,
|
audio: Audio::init()?,
|
||||||
commands,
|
commands,
|
||||||
renderers: HashMap::new(),
|
sim_count,
|
||||||
running,
|
running,
|
||||||
has_game,
|
has_game,
|
||||||
|
linked,
|
||||||
|
renderers: HashMap::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_rom_from_file(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
pub fn load_rom(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
||||||
let bytes = fs::read(path)?;
|
let bytes = fs::read(path)?;
|
||||||
self.load_rom(sim_id, bytes)?;
|
self.reset_sim(sim_id, Some(bytes))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_second_sim(&mut self, rom: Option<PathBuf>) -> Result<()> {
|
pub fn start_second_sim(&mut self, rom: Option<PathBuf>) -> Result<()> {
|
||||||
let bytes = if let Some(path) = rom {
|
let bytes = if let Some(path) = rom {
|
||||||
fs::read(path)?
|
Some(fs::read(path)?)
|
||||||
} else if let Some(rom) = self.sims.first().and_then(|s| s.clone_rom()) {
|
|
||||||
rom
|
|
||||||
} else {
|
} else {
|
||||||
return Ok(());
|
self.sims.first().and_then(|s| s.clone_rom())
|
||||||
};
|
};
|
||||||
self.load_rom(SimId::Player2, bytes)?;
|
self.reset_sim(SimId::Player2, bytes)?;
|
||||||
let (first, second) = self.sims.split_at_mut(1);
|
self.link_sims();
|
||||||
first[0].link(&mut second[0]);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_rom(&mut self, sim_id: SimId, bytes: Vec<u8>) -> Result<()> {
|
fn reset_sim(&mut self, sim_id: SimId, new_rom: Option<Vec<u8>>) -> Result<()> {
|
||||||
while self.sims.len() <= sim_id.to_index() {
|
let index = sim_id.to_index();
|
||||||
|
while self.sims.len() <= index {
|
||||||
self.sims.push(Sim::new());
|
self.sims.push(Sim::new());
|
||||||
}
|
}
|
||||||
let sim = &mut self.sims[sim_id.to_index()];
|
self.sim_count.store(self.sims.len(), Ordering::Relaxed);
|
||||||
|
let sim = &mut self.sims[index];
|
||||||
sim.reset();
|
sim.reset();
|
||||||
|
if let Some(bytes) = new_rom {
|
||||||
sim.load_rom(bytes)?;
|
sim.load_rom(bytes)?;
|
||||||
self.has_game.store(true, Ordering::Release);
|
self.has_game[index].store(true, Ordering::Release);
|
||||||
self.running.store(true, Ordering::Release);
|
}
|
||||||
|
if self.has_game[index].load(Ordering::Acquire) {
|
||||||
|
self.running[index].store(true, Ordering::Release);
|
||||||
|
}
|
||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn stop_second_sim(&mut self) {
|
pub fn stop_second_sim(&mut self) {
|
||||||
self.renderers.remove(&SimId::Player2);
|
self.renderers.remove(&SimId::Player2);
|
||||||
self.sims.truncate(1);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self) {
|
||||||
let mut eye_contents = vec![0u8; 384 * 224 * 2];
|
let mut eye_contents = vec![0u8; 384 * 224 * 2];
|
||||||
let mut audio_samples = vec![];
|
let mut audio_samples = vec![];
|
||||||
loop {
|
loop {
|
||||||
let mut idle = true;
|
let p1_running = self.running[SimId::Player1.to_index()].load(Ordering::Acquire);
|
||||||
if self.running.load(Ordering::Acquire) {
|
let p2_running = self.running[SimId::Player2.to_index()].load(Ordering::Acquire);
|
||||||
idle = false;
|
let mut idle = p1_running || p2_running;
|
||||||
|
if p1_running && p2_running {
|
||||||
Sim::emulate_many(&mut self.sims);
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
for sim_id in SimId::values() {
|
for sim_id in SimId::values() {
|
||||||
let Some(renderer) = self.renderers.get_mut(&sim_id) else {
|
let Some(renderer) = self.renderers.get_mut(&sim_id) else {
|
||||||
continue;
|
continue;
|
||||||
|
@ -198,7 +249,7 @@ impl Emulator {
|
||||||
self.renderers.insert(sim_id, renderer);
|
self.renderers.insert(sim_id, renderer);
|
||||||
}
|
}
|
||||||
EmulatorCommand::LoadGame(sim_id, path) => {
|
EmulatorCommand::LoadGame(sim_id, path) => {
|
||||||
if let Err(error) = self.load_rom_from_file(sim_id, &path) {
|
if let Err(error) = self.load_rom(sim_id, &path) {
|
||||||
eprintln!("error loading rom: {}", error);
|
eprintln!("error loading rom: {}", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,19 +262,27 @@ impl Emulator {
|
||||||
self.stop_second_sim();
|
self.stop_second_sim();
|
||||||
}
|
}
|
||||||
EmulatorCommand::Pause => {
|
EmulatorCommand::Pause => {
|
||||||
self.running.store(false, Ordering::Release);
|
for sim in SimId::values() {
|
||||||
|
self.running[sim.to_index()].store(false, Ordering::Release);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
EmulatorCommand::Resume => {
|
EmulatorCommand::Resume => {
|
||||||
if self.has_game.load(Ordering::Acquire) {
|
for sim_id in SimId::values() {
|
||||||
self.running.store(true, Ordering::Relaxed);
|
let index = sim_id.to_index();
|
||||||
|
if self.has_game[index].load(Ordering::Acquire) {
|
||||||
|
self.running[index].store(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EmulatorCommand::Reset => {
|
|
||||||
for sim in self.sims.iter_mut() {
|
|
||||||
sim.reset();
|
|
||||||
}
|
}
|
||||||
if !self.sims.is_empty() {
|
EmulatorCommand::Link => {
|
||||||
self.running.store(true, Ordering::Release);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EmulatorCommand::SetKeys(sim_id, keys) => {
|
EmulatorCommand::SetKeys(sim_id, keys) => {
|
||||||
|
@ -243,23 +302,33 @@ pub enum EmulatorCommand {
|
||||||
StopSecondSim,
|
StopSecondSim,
|
||||||
Pause,
|
Pause,
|
||||||
Resume,
|
Resume,
|
||||||
Reset,
|
Link,
|
||||||
|
Unlink,
|
||||||
|
Reset(SimId),
|
||||||
SetKeys(SimId, VBKey),
|
SetKeys(SimId, VBKey),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct EmulatorClient {
|
pub struct EmulatorClient {
|
||||||
queue: mpsc::Sender<EmulatorCommand>,
|
queue: mpsc::Sender<EmulatorCommand>,
|
||||||
running: Arc<AtomicBool>,
|
sim_count: Arc<AtomicUsize>,
|
||||||
has_game: Arc<AtomicBool>,
|
running: Arc<[AtomicBool; 2]>,
|
||||||
|
has_game: Arc<[AtomicBool; 2]>,
|
||||||
|
linked: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EmulatorClient {
|
impl EmulatorClient {
|
||||||
pub fn is_running(&self) -> bool {
|
pub fn has_player_2(&self) -> bool {
|
||||||
self.running.load(Ordering::Acquire)
|
self.sim_count.load(Ordering::Acquire) == 2
|
||||||
}
|
}
|
||||||
pub fn has_game(&self) -> bool {
|
pub fn is_running(&self, sim_id: SimId) -> bool {
|
||||||
self.has_game.load(Ordering::Acquire)
|
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)
|
||||||
|
}
|
||||||
|
pub fn are_sims_linked(&self) -> bool {
|
||||||
|
self.linked.load(Ordering::Acquire)
|
||||||
}
|
}
|
||||||
pub fn send_command(&self, command: EmulatorCommand) {
|
pub fn send_command(&self, command: EmulatorCommand) {
|
||||||
if let Err(err) = self.queue.send(command) {
|
if let Err(err) = self.queue.send(command) {
|
||||||
|
|
|
@ -58,6 +58,8 @@ type OnFrame = extern "C" fn(sim: *mut VB) -> c_int;
|
||||||
|
|
||||||
#[link(name = "vb")]
|
#[link(name = "vb")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
#[link_name = "vbEmulate"]
|
||||||
|
fn vb_emulate(sim: *mut VB, cycles: *mut u32) -> c_int;
|
||||||
#[link_name = "vbEmulateEx"]
|
#[link_name = "vbEmulateEx"]
|
||||||
fn vb_emulate_ex(sims: *mut *mut VB, count: c_uint, cycles: *mut u32) -> c_int;
|
fn vb_emulate_ex(sims: *mut *mut VB, count: c_uint, cycles: *mut u32) -> c_int;
|
||||||
#[link_name = "vbGetCartROM"]
|
#[link_name = "vbGetCartROM"]
|
||||||
|
@ -202,6 +204,19 @@ impl Sim {
|
||||||
unsafe { vb_set_peer(self.sim, peer.sim) };
|
unsafe { vb_set_peer(self.sim, peer.sim) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn unlink(&mut self) {
|
||||||
|
let peer = unsafe { vb_get_peer(self.sim) };
|
||||||
|
if !peer.is_null() {
|
||||||
|
unsafe { vb_set_peer(peer, ptr::null_mut()) };
|
||||||
|
unsafe { vb_set_peer(self.sim, ptr::null_mut()) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn emulate(&mut self) {
|
||||||
|
let mut cycles = 20_000_000;
|
||||||
|
unsafe { vb_emulate(self.sim, &mut cycles) };
|
||||||
|
}
|
||||||
|
|
||||||
pub fn emulate_many(sims: &mut [Sim]) {
|
pub fn emulate_many(sims: &mut [Sim]) {
|
||||||
let mut cycles = 20_000_000;
|
let mut cycles = 20_000_000;
|
||||||
let count = sims.len() as c_uint;
|
let count = sims.len() as c_uint;
|
||||||
|
|
Loading…
Reference in New Issue