rewrite it in rust #1

Merged
SonicSwordcane merged 20 commits from riir into main 2024-11-10 23:34:55 +00:00
6 changed files with 255 additions and 21 deletions
Showing only changes of commit 593475960d - Show all commits

182
Cargo.lock generated
View File

@ -177,6 +177,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]]
name = "ascii"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"
[[package]] [[package]]
name = "ash" name = "ash"
version = "0.38.0+1.3.281" version = "0.38.0+1.3.281"
@ -207,7 +213,7 @@ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"cexpr", "cexpr",
"clang-sys", "clang-sys",
"itertools", "itertools 0.13.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
@ -417,6 +423,36 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "cocoa"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c"
dependencies = [
"bitflags 1.3.2",
"block",
"cocoa-foundation",
"core-foundation",
"core-graphics",
"foreign-types",
"libc",
"objc",
]
[[package]]
name = "cocoa-foundation"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7"
dependencies = [
"bitflags 1.3.2",
"block",
"core-foundation",
"core-graphics-types",
"libc",
"objc",
]
[[package]] [[package]]
name = "codespan-reporting" name = "codespan-reporting"
version = "0.11.1" version = "0.11.1"
@ -595,6 +631,27 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
[[package]]
name = "dirs-next"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "dispatch" name = "dispatch"
version = "0.2.0" version = "0.2.0"
@ -840,6 +897,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
[[package]]
name = "home"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "imgui" name = "imgui"
version = "0.12.0" version = "0.12.0"
@ -903,6 +969,15 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.13.0" version = "0.13.0"
@ -1105,6 +1180,29 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "native-dialog"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84e7038885d2aeab236bd60da9e159a5967b47cde3292da3b15ff1bec27c039f"
dependencies = [
"ascii",
"block",
"cocoa",
"core-foundation",
"dirs-next",
"objc",
"objc-foundation",
"objc_id",
"once_cell",
"raw-window-handle 0.5.2",
"thiserror",
"versions",
"wfd",
"which",
"winapi",
]
[[package]] [[package]]
name = "ndk" name = "ndk"
version = "0.8.0" version = "0.8.0"
@ -1130,7 +1228,7 @@ dependencies = [
"log", "log",
"ndk-sys 0.6.0+11769913", "ndk-sys 0.6.0+11769913",
"num_enum", "num_enum",
"raw-window-handle", "raw-window-handle 0.6.2",
"thiserror", "thiserror",
] ]
@ -1236,6 +1334,17 @@ dependencies = [
"malloc_buf", "malloc_buf",
] ]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]] [[package]]
name = "objc-sys" name = "objc-sys"
version = "0.3.5" version = "0.3.5"
@ -1439,6 +1548,15 @@ dependencies = [
"objc2-foundation", "objc2-foundation",
] ]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]] [[package]]
name = "oboe" name = "oboe"
version = "0.6.1" version = "0.6.1"
@ -1637,6 +1755,12 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8a99fddc9f0ba0a85884b8d14e3592853e787d581ca1816c91349b10e4eeab" checksum = "9c8a99fddc9f0ba0a85884b8d14e3592853e787d581ca1816c91349b10e4eeab"
[[package]]
name = "raw-window-handle"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]] [[package]]
name = "raw-window-handle" name = "raw-window-handle"
version = "0.6.2" version = "0.6.2"
@ -1670,6 +1794,17 @@ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
] ]
[[package]]
name = "redox_users"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [
"getrandom",
"libredox",
"thiserror",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@ -1829,7 +1964,8 @@ dependencies = [
"imgui", "imgui",
"imgui-wgpu", "imgui-wgpu",
"imgui-winit-support", "imgui-winit-support",
"itertools", "itertools 0.13.0",
"native-dialog",
"num-derive", "num-derive",
"num-traits", "num-traits",
"pollster", "pollster",
@ -2091,6 +2227,16 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "versions"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c73a36bc44e3039f51fbee93e39f41225f6b17b380eb70cc2aab942df06b34dd"
dependencies = [
"itertools 0.11.0",
"nom",
]
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.5.0" version = "2.5.0"
@ -2303,6 +2449,16 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "wfd"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e713040b67aae5bf1a0ae3e1ebba8cc29ab2b90da9aa1bff6e09031a8a41d7a8"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "wgpu" name = "wgpu"
version = "22.1.0" version = "22.1.0"
@ -2317,7 +2473,7 @@ dependencies = [
"naga", "naga",
"parking_lot", "parking_lot",
"profiling", "profiling",
"raw-window-handle", "raw-window-handle 0.6.2",
"smallvec", "smallvec",
"static_assertions", "static_assertions",
"wasm-bindgen", "wasm-bindgen",
@ -2345,7 +2501,7 @@ dependencies = [
"once_cell", "once_cell",
"parking_lot", "parking_lot",
"profiling", "profiling",
"raw-window-handle", "raw-window-handle 0.6.2",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
"thiserror", "thiserror",
@ -2387,7 +2543,7 @@ dependencies = [
"parking_lot", "parking_lot",
"profiling", "profiling",
"range-alloc", "range-alloc",
"raw-window-handle", "raw-window-handle 0.6.2",
"renderdoc-sys", "renderdoc-sys",
"rustc-hash", "rustc-hash",
"smallvec", "smallvec",
@ -2409,6 +2565,18 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "1.1.0" version = "1.1.0"
@ -2729,7 +2897,7 @@ dependencies = [
"orbclient", "orbclient",
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"raw-window-handle", "raw-window-handle 0.6.2",
"redox_syscall 0.4.1", "redox_syscall 0.4.1",
"rustix", "rustix",
"sctk-adwaita", "sctk-adwaita",

View File

@ -12,6 +12,7 @@ imgui = "0.12"
imgui-wgpu = { git = "https://github.com/Yatekii/imgui-wgpu-rs", rev = "2edd348" } imgui-wgpu = { git = "https://github.com/Yatekii/imgui-wgpu-rs", rev = "2edd348" }
imgui-winit-support = "0.13" imgui-winit-support = "0.13"
itertools = "0.13" itertools = "0.13"
native-dialog = "0.7"
num-derive = "0.4" num-derive = "0.4"
num-traits = "0.2" num-traits = "0.2"
pollster = "0.4" pollster = "0.4"

View File

@ -350,18 +350,28 @@ impl ApplicationHandler for App {
height = ui.window_size()[1]; height = ui.window_size()[1];
ui.menu("ROM", || { ui.menu("ROM", || {
if ui.menu_item("Open ROM") { if ui.menu_item("Open ROM") {
println!("clicked"); let rom = native_dialog::FileDialog::new()
.add_filter("Virtual Boy ROMs", &["vb", "vbrom"])
.show_open_single_file()
.unwrap();
if let Some(path) = rom {
self.client.send_command(EmulatorCommand::LoadGame(path));
}
} }
if ui.menu_item("Quit") { if ui.menu_item("Quit") {
event_loop.exit(); event_loop.exit();
} }
}); });
ui.menu("Emulation", || { ui.menu("Emulation", || {
if ui.menu_item("Pause") { if self.client.is_running() {
println!("clicked"); if ui.menu_item("Pause") {
self.client.send_command(EmulatorCommand::Pause);
}
} else if ui.menu_item("Resume") {
self.client.send_command(EmulatorCommand::Resume);
} }
if ui.menu_item("Reset") { if ui.menu_item("Reset") {
println!("clicked"); self.client.send_command(EmulatorCommand::Reset);
} }
}); });
}); });

View File

@ -1,7 +1,11 @@
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::mpsc::{self, TryRecvError}, sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, TryRecvError},
Arc,
},
}; };
use anyhow::Result; use anyhow::Result;
@ -11,6 +15,7 @@ use crate::{audio::Audio, renderer::GameRenderer, shrooms_vb_core::CoreVB};
pub struct EmulatorBuilder { pub struct EmulatorBuilder {
rom: Option<PathBuf>, rom: Option<PathBuf>,
commands: mpsc::Receiver<EmulatorCommand>, commands: mpsc::Receiver<EmulatorCommand>,
running: Arc<AtomicBool>,
} }
impl EmulatorBuilder { impl EmulatorBuilder {
@ -19,8 +24,12 @@ impl EmulatorBuilder {
let builder = Self { let builder = Self {
rom: None, rom: None,
commands, commands,
running: Arc::new(AtomicBool::new(false)),
};
let client = EmulatorClient {
queue,
running: builder.running.clone(),
}; };
let client = EmulatorClient { queue };
(builder, client) (builder, client)
} }
@ -32,7 +41,7 @@ impl EmulatorBuilder {
} }
pub fn build(self) -> Result<Emulator> { pub fn build(self) -> Result<Emulator> {
let mut emulator = Emulator::new(self.commands)?; let mut emulator = Emulator::new(self.commands, self.running)?;
if let Some(path) = self.rom { if let Some(path) = self.rom {
emulator.load_rom(&path)?; emulator.load_rom(&path)?;
} }
@ -45,21 +54,28 @@ pub struct Emulator {
audio: Audio, audio: Audio,
commands: mpsc::Receiver<EmulatorCommand>, commands: mpsc::Receiver<EmulatorCommand>,
renderer: Option<GameRenderer>, renderer: Option<GameRenderer>,
running: Arc<AtomicBool>,
has_game: bool,
} }
impl Emulator { impl Emulator {
fn new(commands: mpsc::Receiver<EmulatorCommand>) -> Result<Self> { fn new(commands: mpsc::Receiver<EmulatorCommand>, running: Arc<AtomicBool>) -> Result<Self> {
Ok(Self { Ok(Self {
sim: CoreVB::new(), sim: CoreVB::new(),
audio: Audio::init()?, audio: Audio::init()?,
commands, commands,
renderer: None, renderer: None,
running,
has_game: false,
}) })
} }
pub fn load_rom(&mut self, path: &Path) -> Result<()> { pub fn load_rom(&mut self, path: &Path) -> Result<()> {
let bytes = fs::read(path)?; let bytes = fs::read(path)?;
self.sim.reset();
self.sim.load_rom(bytes)?; self.sim.load_rom(bytes)?;
self.has_game = true;
self.running.store(true, Ordering::Release);
Ok(()) Ok(())
} }
@ -67,7 +83,9 @@ impl Emulator {
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 {
self.sim.emulate_frame(); if self.running.load(Ordering::Acquire) {
self.sim.emulate_frame();
}
if let Some(renderer) = &mut self.renderer { if let Some(renderer) = &mut self.renderer {
if self.sim.read_pixels(&mut eye_contents) { if self.sim.read_pixels(&mut eye_contents) {
renderer.render(&eye_contents); renderer.render(&eye_contents);
@ -97,6 +115,23 @@ impl Emulator {
EmulatorCommand::SetRenderer(renderer) => { EmulatorCommand::SetRenderer(renderer) => {
self.renderer = Some(renderer); self.renderer = Some(renderer);
} }
EmulatorCommand::LoadGame(path) => {
if let Err(error) = self.load_rom(&path) {
eprintln!("error loading rom: {}", error);
}
}
EmulatorCommand::Pause => {
self.running.store(false, Ordering::Release);
}
EmulatorCommand::Resume => {
if self.has_game {
self.running.store(true, Ordering::Relaxed);
}
}
EmulatorCommand::Reset => {
self.sim.reset();
self.running.store(true, Ordering::Release);
}
EmulatorCommand::PressStart => { EmulatorCommand::PressStart => {
self.sim.set_keys(0x1003); self.sim.set_keys(0x1003);
} }
@ -110,15 +145,23 @@ impl Emulator {
#[derive(Debug)] #[derive(Debug)]
pub enum EmulatorCommand { pub enum EmulatorCommand {
SetRenderer(GameRenderer), SetRenderer(GameRenderer),
LoadGame(PathBuf),
Pause,
Resume,
Reset,
PressStart, PressStart,
ReleaseStart, ReleaseStart,
} }
pub struct EmulatorClient { pub struct EmulatorClient {
queue: mpsc::Sender<EmulatorCommand>, queue: mpsc::Sender<EmulatorCommand>,
running: Arc<AtomicBool>,
} }
impl EmulatorClient { impl EmulatorClient {
pub fn is_running(&self) -> bool {
self.running.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) {
eprintln!( eprintln!(

View File

@ -1,4 +1,4 @@
use std::{path::PathBuf, thread}; use std::{path::PathBuf, process, thread};
use anyhow::Result; use anyhow::Result;
use app::App; use app::App;
@ -14,16 +14,24 @@ mod shrooms_vb_core;
#[derive(Parser)] #[derive(Parser)]
struct Args { struct Args {
rom: PathBuf, rom: Option<PathBuf>,
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
let (builder, client) = EmulatorBuilder::new(); let (mut builder, client) = EmulatorBuilder::new();
let builder = builder.with_rom(&args.rom); if let Some(path) = args.rom {
builder = builder.with_rom(&path);
}
thread::spawn(move || { thread::spawn(move || {
let mut emulator = builder.build().unwrap(); let mut emulator = match builder.build() {
Ok(e) => e,
Err(err) => {
eprintln!("Error initializing emulator: {err}");
process::exit(1);
}
};
emulator.run(); emulator.run();
}); });

View File

@ -119,6 +119,10 @@ impl CoreVB {
CoreVB { sim } CoreVB { sim }
} }
pub fn reset(&mut self) {
unsafe { vb_reset(self.sim) };
}
pub fn load_rom(&mut self, rom: Vec<u8>) -> Result<()> { pub fn load_rom(&mut self, rom: Vec<u8>) -> Result<()> {
self.unload_rom(); self.unload_rom();