Compare commits

...
7 Commits
8 changed files with 147 additions and 28 deletions
Generated
+1 -1
View File
@@ -1832,7 +1832,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "lemur" name = "lemur"
version = "0.5.1" version = "0.7.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"atoi", "atoi",
+1 -1
View File
@@ -4,7 +4,7 @@ description = "An emulator for the Virtual Boy."
repository = "https://git.virtual-boy.com/PVB/lemur" repository = "https://git.virtual-boy.com/PVB/lemur"
publish = false publish = false
license = "MIT" license = "MIT"
version = "0.5.1" version = "0.7.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
+28 -6
View File
@@ -43,6 +43,13 @@ impl SimId {
Self::Player2 => 1, Self::Player2 => 1,
} }
} }
pub const fn from_index(index: usize) -> Option<Self> {
match index {
0 => Some(Self::Player1),
1 => Some(Self::Player2),
_ => None,
}
}
} }
impl Display for SimId { impl Display for SimId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -171,7 +178,7 @@ pub struct Emulator {
debuggers: HashMap<SimId, DebugInfo>, debuggers: HashMap<SimId, DebugInfo>,
stdouts: HashMap<SimId, mpsc::Sender<String>>, stdouts: HashMap<SimId, mpsc::Sender<String>>,
watched_regions: HashMap<MemoryRange, Weak<MemoryRegion>>, watched_regions: HashMap<MemoryRange, Weak<MemoryRegion>>,
eye_contents: Vec<u8>, eye_contents: [Vec<u8>; 2],
audio_samples: Vec<f32>, audio_samples: Vec<f32>,
buffer: Vec<u8>, buffer: Vec<u8>,
} }
@@ -198,7 +205,7 @@ impl Emulator {
debuggers: HashMap::new(), debuggers: HashMap::new(),
stdouts: HashMap::new(), stdouts: HashMap::new(),
watched_regions: HashMap::new(), watched_regions: HashMap::new(),
eye_contents: vec![0u8; 384 * 224 * 2], eye_contents: [vec![0u8; 384 * 224 * 2], vec![0u8; 384 * 224 * 2]],
audio_samples: Vec::with_capacity(EXPECTED_FRAME_SIZE), audio_samples: Vec::with_capacity(EXPECTED_FRAME_SIZE),
buffer: vec![], buffer: vec![],
}) })
@@ -230,8 +237,15 @@ impl Emulator {
let index = sim_id.to_index(); let index = sim_id.to_index();
while self.sims.len() <= index { while self.sims.len() <= index {
let new_index = self.sims.len();
self.sims.push(Sim::new()); self.sims.push(Sim::new());
self.sim_state[index].store(SimState::NoGame, Ordering::Release); if self
.stdouts
.contains_key(&SimId::from_index(new_index).unwrap())
{
self.sims[new_index].watch_stdout(true);
}
self.sim_state[new_index].store(SimState::NoGame, Ordering::Release);
} }
let sim = &mut self.sims[index]; let sim = &mut self.sims[index];
sim.reset(); sim.reset();
@@ -476,7 +490,7 @@ impl Emulator {
// stdout // stdout
self.stdouts.retain(|sim_id, stdout| { self.stdouts.retain(|sim_id, stdout| {
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else { let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
return false; return true;
}; };
if let Some(text) = sim.take_stdout() { if let Some(text) = sim.take_stdout() {
if stdout.send(text).is_err() { if stdout.send(text).is_err() {
@@ -514,9 +528,12 @@ impl Emulator {
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else { let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
continue; continue;
}; };
if sim.read_pixels(&mut self.eye_contents) { if sim.read_pixels(&mut self.eye_contents[sim_id.to_index()]) {
idle = false; idle = false;
if renderer.queue_render(&self.eye_contents).is_err() { if renderer
.queue_render(&self.eye_contents[sim_id.to_index()])
.is_err()
{
self.renderers.remove(&sim_id); self.renderers.remove(&sim_id);
} }
} }
@@ -688,6 +705,10 @@ impl Emulator {
sim.set_keys(keys); sim.set_keys(keys);
} }
} }
EmulatorCommand::Screenshot(sim_id, sender) => {
let contents = self.eye_contents[sim_id.to_index()].clone();
let _ = sender.send(contents);
}
EmulatorCommand::Exit(done) => { EmulatorCommand::Exit(done) => {
for sim_id in SimId::values() { for sim_id in SimId::values() {
if let Err(error) = self.save_sram(sim_id) { if let Err(error) = self.save_sram(sim_id) {
@@ -747,6 +768,7 @@ pub enum EmulatorCommand {
Unlink, Unlink,
Reset(SimId), Reset(SimId),
SetKeys(SimId, VBKey), SetKeys(SimId, VBKey),
Screenshot(SimId, oneshot::Sender<Vec<u8>>),
Exit(oneshot::Sender<()>), Exit(oneshot::Sender<()>),
} }
+2 -2
View File
@@ -12,7 +12,7 @@ impl RegisterInfo {
pub fn to_description(&self) -> String { pub fn to_description(&self) -> String {
let mut string = format!("name:{}", self.name); let mut string = format!("name:{}", self.name);
if let Some(alt) = self.alt_name { if let Some(alt) = self.alt_name {
string.push_str(&format!(";alt-name:{}", alt)); string.push_str(&format!(";alt-name:{alt}"));
} }
string.push_str(&format!( string.push_str(&format!(
";bitsize:32;offset:{};encoding:uint;format:hex;set:{};dwarf:{}", ";bitsize:32;offset:{};encoding:uint;format:hex;set:{};dwarf:{}",
@@ -21,7 +21,7 @@ impl RegisterInfo {
self.dwarf self.dwarf
)); ));
if let Some(generic) = self.generic { if let Some(generic) = self.generic {
string.push_str(&format!(";generic:{}", generic)); string.push_str(&format!(";generic:{generic}"));
} }
string string
} }
+9 -2
View File
@@ -227,7 +227,7 @@ impl Mappings for InputMapping {
for (keyboard_key, keys) in &self.keys { for (keyboard_key, keys) in &self.keys {
let name = match keyboard_key { let name = match keyboard_key {
PhysicalKey::Code(code) => format!("{code:?}"), PhysicalKey::Code(code) => format!("{code:?}"),
k => format!("{:?}", k), k => format!("{k:?}"),
}; };
for key in keys.iter() { for key in keys.iter() {
results.entry(key).or_default().push(name.clone()); results.entry(key).or_default().push(name.clone());
@@ -471,11 +471,12 @@ pub enum Command {
FastForward(u32), FastForward(u32),
Reset, Reset,
PauseResume, PauseResume,
Screenshot,
// if you update this, update Command::all and add a default // if you update this, update Command::all and add a default
} }
impl Command { impl Command {
pub fn all() -> [Self; 6] { pub fn all() -> [Self; 7] {
[ [
Self::OpenRom, Self::OpenRom,
Self::Quit, Self::Quit,
@@ -483,6 +484,7 @@ impl Command {
Self::Reset, Self::Reset,
Self::FrameAdvance, Self::FrameAdvance,
Self::FastForward(0), Self::FastForward(0),
Self::Screenshot,
] ]
} }
@@ -494,6 +496,7 @@ impl Command {
Self::Reset => "Reset", Self::Reset => "Reset",
Self::FrameAdvance => "Frame Advance", Self::FrameAdvance => "Frame Advance",
Self::FastForward(_) => "Fast Forward", Self::FastForward(_) => "Fast Forward",
Self::Screenshot => "Screenshot",
} }
} }
} }
@@ -539,6 +542,10 @@ impl Default for Shortcuts {
Command::FastForward(0), Command::FastForward(0),
KeyboardShortcut::new(Modifiers::NONE, Key::Space), KeyboardShortcut::new(Modifiers::NONE, Key::Space),
); );
shortcuts.set(
Command::Screenshot,
KeyboardShortcut::new(Modifiers::NONE, Key::F12),
);
shortcuts shortcuts
} }
} }
+5 -5
View File
@@ -44,9 +44,9 @@ fn set_panic_handler() {
std::panic::set_hook(Box::new(|info| { std::panic::set_hook(Box::new(|info| {
let mut message = String::new(); let mut message = String::new();
if let Some(msg) = info.payload().downcast_ref::<&str>() { if let Some(msg) = info.payload().downcast_ref::<&str>() {
message += &format!("{}\n", msg); message += &format!("{msg}\n");
} else if let Some(msg) = info.payload().downcast_ref::<String>() { } else if let Some(msg) = info.payload().downcast_ref::<String>() {
message += &format!("{}\n", msg); message += &format!("{msg}\n");
} }
if let Some(location) = info.location() { if let Some(location) = info.location() {
message += &format!( message += &format!(
@@ -56,9 +56,9 @@ fn set_panic_handler() {
); );
} }
let backtrace = std::backtrace::Backtrace::force_capture(); let backtrace = std::backtrace::Backtrace::force_capture();
message += &format!("stack trace:\n{:#}\n", backtrace); message += &format!("stack trace:\n{backtrace:#}\n");
eprint!("{}", message); eprint!("{message}");
let Some(project_dirs) = directories::ProjectDirs::from("com", "virtual-boy", "Lemur") let Some(project_dirs) = directories::ProjectDirs::from("com", "virtual-boy", "Lemur")
else { else {
@@ -72,7 +72,7 @@ fn set_panic_handler() {
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap() .unwrap()
.as_millis(); .as_millis();
let logfile_name = format!("crash-{}.txt", timestamp); let logfile_name = format!("crash-{timestamp}.txt");
let _ = std::fs::write(data_dir.join(logfile_name), message); let _ = std::fs::write(data_dir.join(logfile_name), message);
})); }));
} }
+75 -3
View File
@@ -6,11 +6,12 @@ use crate::{
input::{Command, ShortcutProvider}, input::{Command, ShortcutProvider},
persistence::Persistence, persistence::Persistence,
}; };
use anyhow::Context as _;
use egui::{ use egui::{
Align2, Button, CentralPanel, Color32, Context, Direction, Frame, TopBottomPanel, Ui, Vec2, Align2, Button, CentralPanel, Color32, Context, Direction, Frame, TopBottomPanel, Ui, Vec2,
ViewportBuilder, ViewportCommand, ViewportId, Window, menu, ViewportBuilder, ViewportCommand, ViewportId, Window, menu,
}; };
use egui_toast::{Toast, Toasts}; use egui_toast::{Toast, ToastKind, ToastOptions, Toasts};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use winit::event_loop::EventLoopProxy; use winit::event_loop::EventLoopProxy;
@@ -69,7 +70,7 @@ impl GameWindow {
} }
} }
fn show_menu(&mut self, ctx: &Context, ui: &mut Ui) { fn show_menu(&mut self, ctx: &Context, ui: &mut Ui, toasts: &mut Toasts) {
let state = self.client.emulator_state(); let state = self.client.emulator_state();
let is_ready = self.client.sim_state(self.sim_id) == SimState::Ready; let is_ready = self.client.sim_state(self.sim_id) == SimState::Ready;
let can_pause = is_ready && state == EmulatorState::Running; let can_pause = is_ready && state == EmulatorState::Running;
@@ -113,6 +114,16 @@ impl GameWindow {
self.client self.client
.send_command(EmulatorCommand::SetSpeed(speed as f64)); .send_command(EmulatorCommand::SetSpeed(speed as f64));
} }
Command::Screenshot => {
let autopause = state == EmulatorState::Running && can_pause;
if autopause {
self.client.send_command(EmulatorCommand::Pause);
}
pollster::block_on(self.take_screenshot(toasts));
if autopause {
self.client.send_command(EmulatorCommand::Resume);
}
}
} }
} }
@@ -178,6 +189,17 @@ impl GameWindow {
self.client.send_command(EmulatorCommand::FrameAdvance); self.client.send_command(EmulatorCommand::FrameAdvance);
ui.close_menu(); ui.close_menu();
} }
ui.separator();
if ui
.add_enabled(
is_ready,
self.button_for(ui.ctx(), "Screenshot", Command::Screenshot),
)
.clicked()
{
pollster::block_on(self.take_screenshot(toasts));
ui.close_menu();
}
}); });
ui.menu_button("Options", |ui| self.show_options_menu(ctx, ui)); ui.menu_button("Options", |ui| self.show_options_menu(ctx, ui));
ui.menu_button("Multiplayer", |ui| { ui.menu_button("Multiplayer", |ui| {
@@ -262,6 +284,56 @@ impl GameWindow {
}); });
} }
async fn take_screenshot(&self, toasts: &mut Toasts) {
match self.try_take_screenshot().await {
Ok(Some(path)) => {
toasts.add(
Toast::new()
.kind(ToastKind::Info)
.options(ToastOptions::default().duration_in_seconds(5.0))
.text(format!("Saved to {path}")),
);
}
Ok(None) => {}
Err(error) => {
toasts.add(
Toast::new()
.kind(ToastKind::Error)
.options(ToastOptions::default().duration_in_seconds(5.0))
.text(format!("{error:#}")),
);
}
}
}
async fn try_take_screenshot(&self) -> anyhow::Result<Option<String>> {
let (tx, rx) = oneshot::channel();
self.client
.send_command(EmulatorCommand::Screenshot(self.sim_id, tx));
let bytes = rx.await.context("Could not take screenshot")?;
let file = rfd::AsyncFileDialog::new()
.add_filter("PNG images", &["png"])
.set_file_name("screenshot.png")
.save_file()
.await;
let Some(file) = file else {
return Ok(None);
};
if bytes.len() != 384 * 224 * 2 {
anyhow::bail!("Unexpected screenshot size");
}
let mut screencap = image::GrayImage::new(384 * 2, 224);
for (index, pixel) in bytes.into_iter().enumerate() {
let x = (index / 2) % 384 + ((index % 2) * 384);
let y = (index / 2) / 384;
screencap.put_pixel(x as u32, y as u32, image::Luma([pixel]));
}
screencap
.save(&file.path())
.context("Could not save screenshot")?;
Ok(Some(file.path().display().to_string()))
}
fn show_options_menu(&mut self, ctx: &Context, ui: &mut Ui) { fn show_options_menu(&mut self, ctx: &Context, ui: &mut Ui) {
ui.menu_button("Video", |ui| { ui.menu_button("Video", |ui| {
ui.menu_button("Screen Size", |ui| { ui.menu_button("Screen Size", |ui| {
@@ -478,7 +550,7 @@ impl AppWindow for GameWindow {
.exact_height(22.0) .exact_height(22.0)
.show(ctx, |ui| { .show(ctx, |ui| {
menu::bar(ui, |ui| { menu::bar(ui, |ui| {
self.show_menu(ctx, ui); self.show_menu(ctx, ui, &mut toasts);
}); });
}); });
if self.color_picker.is_some() { if self.color_picker.is_some() {
+26 -8
View File
@@ -1,4 +1,4 @@
use std::sync::mpsc; use std::{collections::VecDeque, sync::mpsc};
use egui::{ use egui::{
Align, CentralPanel, Context, FontFamily, Label, RichText, ScrollArea, ViewportBuilder, Align, CentralPanel, Context, FontFamily, Label, RichText, ScrollArea, ViewportBuilder,
@@ -9,20 +9,24 @@ use crate::emulator::{EmulatorClient, EmulatorCommand, SimId};
use super::AppWindow; use super::AppWindow;
const SCROLLBACK: usize = 1000;
pub struct TerminalWindow { pub struct TerminalWindow {
sim_id: SimId, sim_id: SimId,
receiver: mpsc::Receiver<String>, receiver: mpsc::Receiver<String>,
text: String, lines: VecDeque<String>,
} }
impl TerminalWindow { impl TerminalWindow {
pub fn new(sim_id: SimId, client: &EmulatorClient) -> Self { pub fn new(sim_id: SimId, client: &EmulatorClient) -> Self {
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
client.send_command(EmulatorCommand::WatchStdout(sim_id, sender)); client.send_command(EmulatorCommand::WatchStdout(sim_id, sender));
let mut lines = VecDeque::new();
lines.push_back(String::new());
Self { Self {
sim_id, sim_id,
receiver, receiver,
text: String::new(), lines,
} }
} }
} }
@@ -44,17 +48,31 @@ impl AppWindow for TerminalWindow {
fn show(&mut self, ctx: &Context) { fn show(&mut self, ctx: &Context) {
if let Ok(text) = self.receiver.try_recv() { if let Ok(text) = self.receiver.try_recv() {
self.text.push_str(&text); let mut rest = text.as_str();
while let Some(index) = rest.find('\n') {
let (line, lines) = rest.split_at(index);
let current = self.lines.back_mut().unwrap();
current.push_str(line);
self.lines.push_back(String::new());
if self.lines.len() > SCROLLBACK {
self.lines.pop_front();
}
rest = &lines[1..];
}
self.lines.back_mut().unwrap().push_str(rest);
} }
CentralPanel::default().show(ctx, |ui| { CentralPanel::default().show(ctx, |ui| {
ScrollArea::vertical() ScrollArea::vertical()
.stick_to_bottom(true) .stick_to_bottom(true)
.auto_shrink([false, false]) .auto_shrink([false, false])
.animated(false)
.show(ui, |ui| { .show(ui, |ui| {
let label = Label::new(RichText::new(&self.text).family(FontFamily::Monospace)) for line in &self.lines {
.halign(Align::LEFT) let label = Label::new(RichText::new(line).family(FontFamily::Monospace))
.wrap(); .halign(Align::LEFT)
ui.add(label); .wrap();
ui.add(label);
}
}); });
}); });
} }