Compare commits
17
Commits
af9a4ae8ee
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cae0c8e54 | ||
|
|
707dd3a200 | ||
|
|
16a2aaee92 | ||
|
|
d59da1009a | ||
|
|
9d37d22a22 | ||
|
|
e1a8006af7 | ||
|
|
9f7895a457 | ||
|
|
5ce39fb70e | ||
|
|
0fff4d427f | ||
|
|
17d3811124 | ||
|
|
b79f35ac61 | ||
|
|
89128b8207 | ||
|
|
b0404a5da2 | ||
|
|
16cf54e5cf | ||
|
|
f031bb17b2 | ||
|
|
102aff1580 | ||
|
|
f1c5571aa6 |
Generated
+107
-4
@@ -1755,9 +1755,15 @@ version = "3.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "lemur"
|
||||
version = "0.2.7"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"atoi",
|
||||
@@ -1788,6 +1794,8 @@ dependencies = [
|
||||
"serde_json",
|
||||
"thread-priority",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"wgpu",
|
||||
"windows 0.58.0",
|
||||
"winit",
|
||||
@@ -1883,6 +1891,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
|
||||
dependencies = [
|
||||
"regex-automata 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
@@ -2068,6 +2085,16 @@ dependencies = [
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
||||
dependencies = [
|
||||
"overload",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.6"
|
||||
@@ -2408,6 +2435,12 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "owned_ttf_parser"
|
||||
version = "0.25.0"
|
||||
@@ -2701,8 +2734,17 @@ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
"regex-automata 0.4.9",
|
||||
"regex-syntax 0.8.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
||||
dependencies = [
|
||||
"regex-syntax 0.6.29",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2713,9 +2755,15 @@ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"regex-syntax 0.8.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
@@ -2906,6 +2954,15 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
@@ -3123,6 +3180,16 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-skia"
|
||||
version = "0.11.4"
|
||||
@@ -3248,6 +3315,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3358,6 +3455,12 @@ version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
|
||||
+3
-1
@@ -4,7 +4,7 @@ description = "An emulator for the Virtual Boy."
|
||||
repository = "https://git.virtual-boy.com/PVB/lemur"
|
||||
publish = false
|
||||
license = "MIT"
|
||||
version = "0.2.7"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
@@ -36,6 +36,8 @@ serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
thread-priority = "1"
|
||||
tokio = { version = "1", features = ["io-util", "macros", "net", "rt", "sync"] }
|
||||
tracing = { version = "0.1", features = ["release_max_level_info"] }
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
wgpu = "23"
|
||||
winit = { version = "0.30", features = ["serde"] }
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ A Virtual Boy emulator built around the shrooms-vb core. Written in Rust, using
|
||||
## Setup
|
||||
|
||||
Install the following dependencies:
|
||||
- `cargo`
|
||||
- `cargo` (via [rustup](https://rustup.rs/), the version from your package manager is too old)
|
||||
|
||||
Run
|
||||
```sh
|
||||
|
||||
+4
-1
@@ -23,6 +23,10 @@ ENV PATH="/osxcross/bin:$PATH" \
|
||||
CXX_x86_64-apple-darwin="o64-clang++" \
|
||||
CC_aarch64-apple-darwin="oa64-clang" \
|
||||
CXX_aarch64-apple-darwin="o6a4-clang++" \
|
||||
SHROOMS_CFLAGS_x86_64-unknown-linux-gnu="-flto" \
|
||||
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Clinker-plugin-lto -Clinker=clang -Clink-arg=-fuse-ld=lld" \
|
||||
SHROOMS_CFLAGS_x86_64-pc-windows-msvc="-flto" \
|
||||
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS="-Lnative=/xwin/crt/lib/x86_64 -Lnative=/xwin/sdk/lib/um/x86_64 -Lnative=/xwin/sdk/lib/ucrt/x86_64 -Clinker-plugin-lto -Clink-arg=-fuse-ld=lld" \
|
||||
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER="lld-link-19" \
|
||||
CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER="o64-clang" \
|
||||
CARGO_TARGET_X86_64_APPLE_DARWIN_AR="llvm-ar-19" \
|
||||
@@ -30,5 +34,4 @@ ENV PATH="/osxcross/bin:$PATH" \
|
||||
CARGO_TARGET_AARCH64_APPLE_DARWIN_AR="llvm-ar-19" \
|
||||
CROSS_COMPILE="setting-this-to-silence-a-warning-" \
|
||||
RC_PATH="llvm-rc-19" \
|
||||
RUSTFLAGS="-Lnative=/xwin/crt/lib/x86_64 -Lnative=/xwin/sdk/lib/um/x86_64 -Lnative=/xwin/sdk/lib/ucrt/x86_64" \
|
||||
MACOSX_DEPLOYMENT_TARGET="14.5"
|
||||
|
||||
@@ -8,13 +8,25 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
println!("cargo::rerun-if-changed=shrooms-vb-core");
|
||||
cc::Build::new()
|
||||
|
||||
let mut builder = cc::Build::new();
|
||||
let _ = builder.try_flags_from_environment("SHROOMS_CFLAGS");
|
||||
let opt_level = if builder.get_compiler().is_like_msvc() {
|
||||
2
|
||||
} else {
|
||||
3
|
||||
};
|
||||
builder
|
||||
.include(Path::new("shrooms-vb-core/core"))
|
||||
.opt_level(2)
|
||||
.opt_level(opt_level)
|
||||
.flag_if_supported("-fno-strict-aliasing")
|
||||
.define("VB_LITTLE_ENDIAN", None)
|
||||
.define("VB_SIGNED_PROPAGATE", None)
|
||||
.define("VB_DIV_GENERIC", None)
|
||||
.define("VB_DIRECT_EXECUTE", "on_execute")
|
||||
.define("VB_DIRECT_FRAME", "on_frame")
|
||||
.define("VB_DIRECT_READ", "on_read")
|
||||
.define("VB_DIRECT_WRITE", "on_write")
|
||||
.file(Path::new("shrooms-vb-core/core/vb.c"))
|
||||
.compile("vb");
|
||||
|
||||
|
||||
+1
-1
Submodule shrooms-vb-core updated: 57dcd8370a...155a3aa678
+9
-5
@@ -6,6 +6,7 @@ use egui::{
|
||||
ViewportCommand, ViewportId, ViewportInfo,
|
||||
};
|
||||
use gilrs::{EventType, Gilrs};
|
||||
use tracing::{error, warn};
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
@@ -230,8 +231,8 @@ impl ApplicationHandler<UserEvent> for Application {
|
||||
fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
let (sender, receiver) = oneshot::channel();
|
||||
if self.client.send_command(EmulatorCommand::Exit(sender)) {
|
||||
if let Err(err) = receiver.recv_timeout(Duration::from_secs(5)) {
|
||||
eprintln!("could not gracefully exit: {}", err);
|
||||
if let Err(error) = receiver.recv_timeout(Duration::from_secs(5)) {
|
||||
error!(%error, "could not gracefully exit.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -434,9 +435,12 @@ fn create_window_and_state(
|
||||
}
|
||||
|
||||
fn process_gamepad_input(mappings: MappingProvider, proxy: EventLoopProxy<UserEvent>) {
|
||||
let Ok(mut gilrs) = Gilrs::new() else {
|
||||
eprintln!("could not connect gamepad listener");
|
||||
return;
|
||||
let mut gilrs = match Gilrs::new() {
|
||||
Ok(gilrs) => gilrs,
|
||||
Err(error) => {
|
||||
warn!(%error, "could not connect gamepad listener");
|
||||
return;
|
||||
}
|
||||
};
|
||||
while let Some(event) = gilrs.next_event_blocking(None) {
|
||||
if event.event == EventType::Connected {
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ use anyhow::{bail, Result};
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use itertools::Itertools;
|
||||
use rubato::{FftFixedInOut, Resampler};
|
||||
use tracing::error;
|
||||
|
||||
pub struct Audio {
|
||||
#[allow(unused)]
|
||||
@@ -54,7 +55,7 @@ impl Audio {
|
||||
}
|
||||
chunk.commit_all();
|
||||
},
|
||||
move |err| eprintln!("stream error: {err}"),
|
||||
move |error| error!(%error, "stream error"),
|
||||
None,
|
||||
)?;
|
||||
stream.play()?;
|
||||
|
||||
+25
-5
@@ -15,6 +15,7 @@ use anyhow::Result;
|
||||
use atomic::Atomic;
|
||||
use bytemuck::NoUninit;
|
||||
use egui_toast::{Toast, ToastKind, ToastOptions};
|
||||
use tracing::{error, warn};
|
||||
|
||||
use crate::{audio::Audio, graphics::TextureSink};
|
||||
use shrooms_vb_core::{Sim, StopReason, EXPECTED_FRAME_SIZE};
|
||||
@@ -304,9 +305,13 @@ impl Emulator {
|
||||
}
|
||||
|
||||
fn start_debugging(&mut self, sim_id: SimId, sender: DebugSender) {
|
||||
if self.sim_state[sim_id.to_index()].load(Ordering::Acquire) != SimState::Ready {
|
||||
// Can't debug unless a game is connected
|
||||
return;
|
||||
}
|
||||
let debug = DebugInfo {
|
||||
sender,
|
||||
stop_reason: Some(DebugStopReason::Trapped),
|
||||
stop_reason: Some(DebugStopReason::Paused),
|
||||
};
|
||||
self.debuggers.insert(sim_id, debug);
|
||||
self.state
|
||||
@@ -329,7 +334,7 @@ impl Emulator {
|
||||
}
|
||||
|
||||
fn debug_interrupt(&mut self, sim_id: SimId) {
|
||||
self.debug_stop(sim_id, DebugStopReason::Trapped);
|
||||
self.debug_stop(sim_id, DebugStopReason::Paused);
|
||||
}
|
||||
|
||||
fn debug_stop(&mut self, sim_id: SimId, reason: DebugStopReason) {
|
||||
@@ -532,6 +537,12 @@ impl Emulator {
|
||||
let value = sim.read_register(register);
|
||||
let _ = done.send(value);
|
||||
}
|
||||
EmulatorCommand::WriteRegister(sim_id, register, value) => {
|
||||
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
|
||||
return;
|
||||
};
|
||||
sim.write_register(register, value);
|
||||
}
|
||||
EmulatorCommand::ReadMemory(sim_id, start, length, mut buffer, done) => {
|
||||
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
|
||||
return;
|
||||
@@ -539,6 +550,13 @@ impl Emulator {
|
||||
sim.read_memory(start, length, &mut buffer);
|
||||
let _ = done.send(buffer);
|
||||
}
|
||||
EmulatorCommand::WriteMemory(sim_id, start, buffer, done) => {
|
||||
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
|
||||
return;
|
||||
};
|
||||
sim.write_memory(start, &buffer);
|
||||
let _ = done.send(buffer);
|
||||
}
|
||||
EmulatorCommand::AddBreakpoint(sim_id, address) => {
|
||||
let Some(sim) = self.sims.get_mut(sim_id.to_index()) else {
|
||||
return;
|
||||
@@ -608,7 +626,7 @@ impl Emulator {
|
||||
return;
|
||||
}
|
||||
}
|
||||
eprintln!("{}", message);
|
||||
error!("{}", message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -626,7 +644,9 @@ pub enum EmulatorCommand {
|
||||
DebugContinue(SimId),
|
||||
DebugStep(SimId),
|
||||
ReadRegister(SimId, VBRegister, oneshot::Sender<u32>),
|
||||
WriteRegister(SimId, VBRegister, u32),
|
||||
ReadMemory(SimId, u32, usize, Vec<u8>, oneshot::Sender<Vec<u8>>),
|
||||
WriteMemory(SimId, u32, Vec<u8>, oneshot::Sender<Vec<u8>>),
|
||||
AddBreakpoint(SimId, u32),
|
||||
RemoveBreakpoint(SimId, u32),
|
||||
AddWatchpoint(SimId, u32, usize, VBWatchpointType),
|
||||
@@ -666,7 +686,7 @@ pub enum DebugStopReason {
|
||||
// We hit a watchpoint
|
||||
Watchpoint(VBWatchpointType, u32),
|
||||
// The debugger told us to pause
|
||||
Trapped,
|
||||
Paused,
|
||||
}
|
||||
|
||||
struct DebugInfo {
|
||||
@@ -704,7 +724,7 @@ impl EmulatorClient {
|
||||
match self.queue.send(command) {
|
||||
Ok(()) => true,
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
warn!(
|
||||
"could not send command {:?} as emulator is shut down",
|
||||
err.0
|
||||
);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, HashSet},
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
ops::Bound,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AddressSet {
|
||||
ranges: HashSet<(u32, usize)>,
|
||||
ranges: BTreeSet<(u32, usize)>,
|
||||
bounds: BTreeMap<u32, usize>,
|
||||
}
|
||||
|
||||
@@ -109,6 +109,22 @@ impl AddressSet {
|
||||
.next_back()
|
||||
.is_some_and(|(_, &val)| val > 0)
|
||||
}
|
||||
|
||||
pub fn start_of_range_containing(&self, address: u32) -> Option<u32> {
|
||||
if !self.contains(address) {
|
||||
return None;
|
||||
}
|
||||
self.ranges
|
||||
.range(..=(address, usize::MAX))
|
||||
.rev()
|
||||
.find_map(|&(start, length)| {
|
||||
let contains = start <= address
|
||||
&& (start as usize)
|
||||
.checked_add(length)
|
||||
.is_none_or(|end| end > address as usize);
|
||||
contains.then_some(start)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -120,6 +136,7 @@ mod tests {
|
||||
let set = AddressSet::new();
|
||||
assert!(set.is_empty());
|
||||
assert!(!set.contains(0x13374200));
|
||||
assert_eq!(set.start_of_range_containing(0x13374200), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -127,6 +144,7 @@ mod tests {
|
||||
let mut set = AddressSet::new();
|
||||
set.add(0x00000000, 0x100000000);
|
||||
assert!(set.contains(0x13374200));
|
||||
assert_eq!(set.start_of_range_containing(0x13374200), Some(0x00000000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -228,4 +246,25 @@ mod tests {
|
||||
assert!(!set.contains(address));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_find_start_of_range() {
|
||||
let mut set = AddressSet::new();
|
||||
set.add(0x13374200, 4);
|
||||
assert_eq!(set.start_of_range_containing(0x133741ff), None);
|
||||
for address in 0x13374200..0x13374204 {
|
||||
assert_eq!(set.start_of_range_containing(address), Some(0x13374200));
|
||||
}
|
||||
assert_eq!(set.start_of_range_containing(0x13374204), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_ignore_ranges_not_containing_address() {
|
||||
let mut set = AddressSet::new();
|
||||
set.add(0x10000000, 1024);
|
||||
set.add(0x30000000, 1024);
|
||||
|
||||
assert!(!set.contains(0x20000000));
|
||||
assert_eq!(set.start_of_range_containing(0x20000000), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +145,10 @@ extern "C" {
|
||||
fn vb_set_option(sim: *mut VB, key: VBOption, value: c_int);
|
||||
#[link_name = "vbSetPeer"]
|
||||
fn vb_set_peer(sim: *mut VB, peer: *mut VB);
|
||||
#[link_name = "vbSetProgramCounter"]
|
||||
fn vb_set_program_counter(sim: *mut VB, value: u32) -> u32;
|
||||
#[link_name = "vbSetProgramRegister"]
|
||||
fn vb_set_program_register(sim: *mut VB, index: c_uint, value: i32) -> i32;
|
||||
#[link_name = "vbSetReadCallback"]
|
||||
fn vb_set_read_callback(sim: *mut VB, callback: Option<OnRead>) -> Option<OnRead>;
|
||||
#[link_name = "vbSetSamples"]
|
||||
@@ -154,14 +158,19 @@ extern "C" {
|
||||
typ_: VBDataType,
|
||||
capacity: c_uint,
|
||||
) -> c_int;
|
||||
#[link_name = "vbSetSystemRegister"]
|
||||
fn vb_set_system_register(sim: *mut VB, index: c_uint, value: u32) -> u32;
|
||||
#[link_name = "vbSetUserData"]
|
||||
fn vb_set_user_data(sim: *mut VB, tag: *mut c_void);
|
||||
#[link_name = "vbSetWriteCallback"]
|
||||
fn vb_set_write_callback(sim: *mut VB, callback: Option<OnWrite>) -> Option<OnWrite>;
|
||||
#[link_name = "vbSizeOf"]
|
||||
fn vb_size_of() -> usize;
|
||||
#[link_name = "vbWrite"]
|
||||
fn vb_write(sim: *mut VB, address: u32, _type: VBDataType, value: i32) -> i32;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn on_frame(sim: *mut VB) -> c_int {
|
||||
// SAFETY: the *mut VB owns its userdata.
|
||||
// There is no way for the userdata to be null or otherwise invalid.
|
||||
@@ -170,6 +179,7 @@ extern "C" fn on_frame(sim: *mut VB) -> c_int {
|
||||
1
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn on_execute(sim: *mut VB, address: u32, _code: *const u16, _length: c_int) -> c_int {
|
||||
// SAFETY: the *mut VB owns its userdata.
|
||||
// There is no way for the userdata to be null or otherwise invalid.
|
||||
@@ -193,6 +203,7 @@ extern "C" fn on_execute(sim: *mut VB, address: u32, _code: *const u16, _length:
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn on_read(
|
||||
sim: *mut VB,
|
||||
address: u32,
|
||||
@@ -204,13 +215,13 @@ extern "C" fn on_read(
|
||||
// There is no way for the userdata to be null or otherwise invalid.
|
||||
let data: &mut VBState = unsafe { &mut *vb_get_user_data(sim).cast() };
|
||||
|
||||
if data.read_watchpoints.contains(address) {
|
||||
if let Some(start) = data.read_watchpoints.start_of_range_containing(address) {
|
||||
let watch = if data.write_watchpoints.contains(address) {
|
||||
VBWatchpointType::Access
|
||||
} else {
|
||||
VBWatchpointType::Read
|
||||
};
|
||||
data.stop_reason = Some(StopReason::Watchpoint(watch, address));
|
||||
data.stop_reason = Some(StopReason::Watchpoint(watch, start));
|
||||
}
|
||||
|
||||
// Don't stop here, the debugger expects us to break after the memory access.
|
||||
@@ -218,6 +229,7 @@ extern "C" fn on_read(
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn on_write(
|
||||
sim: *mut VB,
|
||||
address: u32,
|
||||
@@ -230,13 +242,13 @@ extern "C" fn on_write(
|
||||
// There is no way for the userdata to be null or otherwise invalid.
|
||||
let data: &mut VBState = unsafe { &mut *vb_get_user_data(sim).cast() };
|
||||
|
||||
if data.write_watchpoints.contains(address) {
|
||||
if let Some(start) = data.write_watchpoints.start_of_range_containing(address) {
|
||||
let watch = if data.read_watchpoints.contains(address) {
|
||||
VBWatchpointType::Access
|
||||
} else {
|
||||
VBWatchpointType::Write
|
||||
};
|
||||
data.stop_reason = Some(StopReason::Watchpoint(watch, address));
|
||||
data.stop_reason = Some(StopReason::Watchpoint(watch, start));
|
||||
}
|
||||
|
||||
// Don't stop here, the debugger expects us to break after the memory access.
|
||||
@@ -447,6 +459,20 @@ impl Sim {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_register(&mut self, register: VBRegister, value: u32) {
|
||||
match register {
|
||||
VBRegister::Program(index) => unsafe {
|
||||
vb_set_program_register(self.sim, index, value as i32);
|
||||
},
|
||||
VBRegister::System(index) => unsafe {
|
||||
vb_set_system_register(self.sim, index, value);
|
||||
},
|
||||
VBRegister::PC => unsafe {
|
||||
vb_set_program_counter(self.sim, value);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_memory(&mut self, start: u32, length: usize, into: &mut Vec<u8>) {
|
||||
let mut address = start;
|
||||
for _ in 0..length {
|
||||
@@ -456,6 +482,14 @@ impl Sim {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_memory(&mut self, start: u32, buffer: &[u8]) {
|
||||
let mut address = start;
|
||||
for byte in buffer {
|
||||
unsafe { vb_write(self.sim, address, VBDataType::U8, *byte as i32) };
|
||||
address = address.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_breakpoint(&mut self, address: u32) {
|
||||
let data = self.get_state();
|
||||
if let Err(index) = data.breakpoints.binary_search(&address) {
|
||||
|
||||
+134
-30
@@ -9,9 +9,10 @@ use std::{
|
||||
use tokio::{
|
||||
io::{AsyncWriteExt as _, BufReader},
|
||||
net::{TcpListener, TcpStream},
|
||||
select,
|
||||
pin, select,
|
||||
sync::{mpsc, oneshot},
|
||||
};
|
||||
use tracing::{debug, enabled, error, info, Level};
|
||||
|
||||
use crate::emulator::{
|
||||
DebugEvent, DebugStopReason, EmulatorClient, EmulatorCommand, SimId, VBWatchpointType,
|
||||
@@ -56,8 +57,9 @@ impl GdbServer {
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
select! {
|
||||
_ = run_server(sim_id, client, port, &status) => {}
|
||||
_ = run_server(sim_id, client.clone(), port, &status) => {}
|
||||
_ = rx => {
|
||||
client.send_command(EmulatorCommand::StopDebugging(sim_id));
|
||||
*status.lock().unwrap() = GdbServerStatus::Stopped;
|
||||
}
|
||||
}
|
||||
@@ -72,21 +74,52 @@ impl GdbServer {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for GdbServer {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_server(
|
||||
sim_id: SimId,
|
||||
client: EmulatorClient,
|
||||
port: u16,
|
||||
status: &Mutex<GdbServerStatus>,
|
||||
) {
|
||||
let Some(stream) = try_connect(port, status).await else {
|
||||
return;
|
||||
let (debug_sink, mut debug_source) = mpsc::unbounded_channel();
|
||||
client.send_command(EmulatorCommand::StartDebugging(sim_id, debug_sink));
|
||||
|
||||
info!("Connecting to debugger on port {port}...");
|
||||
let connect_future = try_connect(port, status);
|
||||
pin!(connect_future);
|
||||
|
||||
let stream = loop {
|
||||
select! {
|
||||
stream = &mut connect_future => {
|
||||
if let Some(stream) = stream {
|
||||
break stream;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
event = debug_source.recv() => {
|
||||
if event.is_none() {
|
||||
// The sim has stopped (or was never started)
|
||||
*status.lock().unwrap() = GdbServerStatus::Stopped;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
info!("Connected!");
|
||||
let mut connection = GdbConnection::new(sim_id, client);
|
||||
match connection.run(stream).await {
|
||||
match connection.run(stream, debug_source).await {
|
||||
Ok(()) => {
|
||||
info!("Finished debugging.");
|
||||
*status.lock().unwrap() = GdbServerStatus::Stopped;
|
||||
}
|
||||
Err(error) => {
|
||||
error!(%error, "Error from debugger.");
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(error.to_string());
|
||||
}
|
||||
}
|
||||
@@ -97,6 +130,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
|
||||
let listener = match TcpListener::bind(("127.0.0.1", port)).await {
|
||||
Ok(l) => l,
|
||||
Err(err) => {
|
||||
error!(%err, "Could not open port.");
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
|
||||
return None;
|
||||
}
|
||||
@@ -107,6 +141,7 @@ async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpSt
|
||||
Some(stream)
|
||||
}
|
||||
Err(err) => {
|
||||
error!(%err, "Could not connect to debugger.");
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
|
||||
None
|
||||
}
|
||||
@@ -147,12 +182,13 @@ impl GdbConnection {
|
||||
memory_buf: None,
|
||||
}
|
||||
}
|
||||
async fn run(&mut self, stream: TcpStream) -> Result<()> {
|
||||
let (debug_sink, mut debug_source) = mpsc::unbounded_channel();
|
||||
async fn run(
|
||||
&mut self,
|
||||
stream: TcpStream,
|
||||
mut debug_source: mpsc::UnboundedReceiver<DebugEvent>,
|
||||
) -> Result<()> {
|
||||
let (rx, mut tx) = stream.into_split();
|
||||
let mut request_source = RequestSource::new(BufReader::new(rx));
|
||||
self.client
|
||||
.send_command(EmulatorCommand::StartDebugging(self.sim_id, debug_sink));
|
||||
loop {
|
||||
let response = select! {
|
||||
maybe_event = debug_source.recv() => {
|
||||
@@ -169,9 +205,11 @@ impl GdbConnection {
|
||||
};
|
||||
if let Some(res) = response {
|
||||
let buffer = res.finish();
|
||||
match std::str::from_utf8(&buffer) {
|
||||
Ok(text) => println!("response: {text}"),
|
||||
Err(_) => println!("response: {buffer:02x?}"),
|
||||
if enabled!(Level::DEBUG) {
|
||||
match std::str::from_utf8(&buffer) {
|
||||
Ok(text) => debug!("response: {text}"),
|
||||
Err(_) => debug!("response: {buffer:02x?}"),
|
||||
}
|
||||
}
|
||||
tx.write_all(&buffer).await?;
|
||||
self.response_buf = Some(buffer);
|
||||
@@ -196,7 +234,7 @@ impl GdbConnection {
|
||||
}
|
||||
|
||||
fn handle_request(&mut self, mut req: Request<'_>) -> Result<Option<Response>> {
|
||||
println!("received {:02x?}", req);
|
||||
debug!("received {:02x?}", req);
|
||||
|
||||
if req.kind == RequestKind::Signal {
|
||||
self.client
|
||||
@@ -210,7 +248,7 @@ impl GdbConnection {
|
||||
res
|
||||
} else if req.match_str("qSupported:") {
|
||||
self.response()
|
||||
.write_str("multiprocess+;swbreak+;vContSupported+;PacketSize=10000")
|
||||
.write_str("multiprocess+;swbreak+;vContSupported+;PacketSize=10000;SupportedWatchpointTypes=x86_64,aarch64-bas,aarch64-mask")
|
||||
} else if req
|
||||
.match_some_str([
|
||||
"QThreadSuffixSupported",
|
||||
@@ -280,24 +318,37 @@ impl GdbConnection {
|
||||
} else {
|
||||
self.response()
|
||||
}
|
||||
} else if req.match_str("P") {
|
||||
let mut write_register = || {
|
||||
let register_index = req.match_hex::<usize>()?;
|
||||
let register = REGISTERS.get(register_index)?.to_vb_register();
|
||||
if !req.match_str("=") {
|
||||
return None;
|
||||
}
|
||||
let value = {
|
||||
let mut buffer = [0; 4];
|
||||
if !req.match_hex_bytes(&mut buffer) {
|
||||
return None;
|
||||
}
|
||||
u32::from_le_bytes(buffer)
|
||||
};
|
||||
self.client.send_command(EmulatorCommand::WriteRegister(
|
||||
self.sim_id,
|
||||
register,
|
||||
value,
|
||||
));
|
||||
Some(())
|
||||
};
|
||||
if let Some(()) = write_register() {
|
||||
self.response().write_str("OK")
|
||||
} else {
|
||||
self.response()
|
||||
}
|
||||
} else if let Some(op) = req.match_some_str(["m", "x"]) {
|
||||
let mut read_memory = || {
|
||||
let start = req.match_hex::<u64>()?;
|
||||
if !req.match_str(",") {
|
||||
return None;
|
||||
};
|
||||
let length = req.match_hex::<usize>()?;
|
||||
|
||||
let (start, length) = parse_memory_range(&mut req)?;
|
||||
let mut buf = self.memory_buf.take().unwrap_or_default();
|
||||
buf.clear();
|
||||
|
||||
// The v810 has a 32-bit address space.
|
||||
// Addresses wrap within that space, but we don't need to implement that for 64-bit addresses.
|
||||
// Just refuse to return any info for addresses above 0xffffffff.
|
||||
let Ok(start) = u32::try_from(start) else {
|
||||
return Some(buf);
|
||||
};
|
||||
let length = length.min((u32::MAX - start) as usize + 1);
|
||||
if length == 0 {
|
||||
return Some(buf);
|
||||
}
|
||||
@@ -331,6 +382,38 @@ impl GdbConnection {
|
||||
} else {
|
||||
self.response()
|
||||
}
|
||||
} else if let Some(op) = req.match_some_str(["M", "X"]) {
|
||||
let mut write_memory = || {
|
||||
let (start, length) = parse_memory_range(&mut req)?;
|
||||
if length == 0 {
|
||||
return Some(());
|
||||
}
|
||||
if !req.match_str(":") {
|
||||
return None;
|
||||
}
|
||||
let mut buf = self.memory_buf.take().unwrap_or_default();
|
||||
buf.resize(length, 0);
|
||||
let successful_read = if op == "M" {
|
||||
req.match_hex_bytes(&mut buf)
|
||||
} else {
|
||||
req.match_bytes(&mut buf)
|
||||
};
|
||||
if !successful_read {
|
||||
self.memory_buf = Some(buf);
|
||||
return None;
|
||||
}
|
||||
let (tx, rx) = ::oneshot::channel();
|
||||
self.client
|
||||
.send_command(EmulatorCommand::WriteMemory(self.sim_id, start, buf, tx));
|
||||
let buf = rx.recv().ok()?;
|
||||
self.memory_buf = Some(buf);
|
||||
Some(())
|
||||
};
|
||||
if write_memory().is_some() {
|
||||
self.response().write_str("OK")
|
||||
} else {
|
||||
self.response()
|
||||
}
|
||||
} else if req.match_str("Z") {
|
||||
let mut parse_request = || {
|
||||
let type_ = req.match_hex::<u8>()?;
|
||||
@@ -419,9 +502,30 @@ impl Drop for GdbConnection {
|
||||
}
|
||||
}
|
||||
|
||||
// parse a memory range into a start and a length.
|
||||
fn parse_memory_range(req: &mut Request<'_>) -> Option<(u32, usize)> {
|
||||
let start = req.match_hex::<u64>()?;
|
||||
if !req.match_str(",") {
|
||||
return None;
|
||||
};
|
||||
let length = req.match_hex::<usize>()?;
|
||||
let Ok(start) = u32::try_from(start) else {
|
||||
// The v810 has a 32-bit address space.
|
||||
// Addresses wrap within that space, but we don't need to implement that for 64-bit addresses.
|
||||
// Just refuse to return any info for addresses above 0xffffffff.
|
||||
return Some((0, 0));
|
||||
};
|
||||
let length = length.min((u32::MAX - start) as usize + 1);
|
||||
Some((start, length))
|
||||
}
|
||||
|
||||
fn debug_stop_reason_string(reason: Option<DebugStopReason>) -> String {
|
||||
let mut result = String::new();
|
||||
result += if reason.is_some() { "T05;" } else { "T00;" };
|
||||
result += if reason.is_some_and(|r| r != DebugStopReason::Paused) {
|
||||
"T05;"
|
||||
} else {
|
||||
"T00;"
|
||||
};
|
||||
if let Some(DebugStopReason::Breakpoint) = reason {
|
||||
result += "swbreak;";
|
||||
}
|
||||
@@ -442,7 +546,7 @@ fn debug_stop_reason_string(reason: Option<DebugStopReason>) -> String {
|
||||
DebugStopReason::Trace => "trace;",
|
||||
DebugStopReason::Breakpoint => "breakpoint;",
|
||||
DebugStopReason::Watchpoint(_, _) => "watchpoint;",
|
||||
DebugStopReason::Trapped => "trap;",
|
||||
DebugStopReason::Paused => "trap;",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,29 @@ impl Request<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_hex_bytes(&mut self, buffer: &mut [u8]) -> bool {
|
||||
if self.buffer.len() < buffer.len() * 2 {
|
||||
return false;
|
||||
}
|
||||
for (i, item) in buffer.iter_mut().enumerate() {
|
||||
match u8::from_radix_16(&self.buffer[(i * 2)..(i * 2) + 2]) {
|
||||
(byte, 2) => *item = byte,
|
||||
_ => return false,
|
||||
};
|
||||
}
|
||||
self.buffer = self.buffer.split_at(buffer.len()).1;
|
||||
true
|
||||
}
|
||||
|
||||
pub fn match_bytes(&mut self, buffer: &mut [u8]) -> bool {
|
||||
if self.buffer.len() < buffer.len() {
|
||||
return false;
|
||||
}
|
||||
buffer.copy_from_slice(&self.buffer[0..buffer.len()]);
|
||||
self.buffer = self.buffer.split_at(buffer.len()).1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RequestSource<R> {
|
||||
|
||||
+19
-5
@@ -3,11 +3,13 @@
|
||||
|
||||
use std::{path::PathBuf, process, time::SystemTime};
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{bail, Result};
|
||||
use app::Application;
|
||||
use clap::Parser;
|
||||
use emulator::EmulatorBuilder;
|
||||
use thread_priority::{ThreadBuilder, ThreadPriority};
|
||||
use tracing::error;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
|
||||
use winit::event_loop::{ControlFlow, EventLoop};
|
||||
|
||||
mod app;
|
||||
@@ -29,6 +31,13 @@ struct Args {
|
||||
debug_port: Option<u16>,
|
||||
}
|
||||
|
||||
fn init_logger() {
|
||||
let directives = std::env::var("RUST_LOG").unwrap_or("error,lemur=info".into());
|
||||
let filter = EnvFilter::builder().parse_lossy(directives);
|
||||
let layer = tracing_subscriber::fmt::layer().with_filter(filter);
|
||||
tracing_subscriber::registry().with(layer).init();
|
||||
}
|
||||
|
||||
fn set_panic_handler() {
|
||||
std::panic::set_hook(Box::new(|info| {
|
||||
let mut message = String::new();
|
||||
@@ -76,6 +85,8 @@ fn set_process_priority_to_high() -> Result<()> {
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
init_logger();
|
||||
|
||||
set_panic_handler();
|
||||
|
||||
#[cfg(windows)]
|
||||
@@ -84,10 +95,13 @@ fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let (mut builder, client) = EmulatorBuilder::new();
|
||||
if let Some(path) = args.rom {
|
||||
builder = builder.with_rom(&path);
|
||||
if let Some(path) = &args.rom {
|
||||
builder = builder.with_rom(path);
|
||||
}
|
||||
if args.debug_port.is_some() {
|
||||
if args.rom.is_none() {
|
||||
bail!("to start debugging, please select a game.");
|
||||
}
|
||||
builder = builder.start_paused(true);
|
||||
}
|
||||
|
||||
@@ -97,8 +111,8 @@ fn main() -> Result<()> {
|
||||
.spawn_careless(move || {
|
||||
let mut emulator = match builder.build() {
|
||||
Ok(e) => e,
|
||||
Err(err) => {
|
||||
eprintln!("Error initializing emulator: {err}");
|
||||
Err(error) => {
|
||||
error!(%error, "Error initializing emulator");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
+6
-2
@@ -3,7 +3,7 @@ use winit::event_loop::EventLoopProxy;
|
||||
|
||||
use crate::{
|
||||
app::UserEvent,
|
||||
emulator::{EmulatorClient, SimId},
|
||||
emulator::{EmulatorClient, SimId, SimState},
|
||||
gdbserver::{GdbServer, GdbServerStatus},
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@ use super::AppWindow;
|
||||
|
||||
pub struct GdbServerWindow {
|
||||
sim_id: SimId,
|
||||
client: EmulatorClient,
|
||||
port_str: String,
|
||||
connected: bool,
|
||||
quit_on_disconnect: bool,
|
||||
@@ -22,6 +23,7 @@ impl GdbServerWindow {
|
||||
pub fn new(sim_id: SimId, client: EmulatorClient, proxy: EventLoopProxy<UserEvent>) -> Self {
|
||||
Self {
|
||||
sim_id,
|
||||
client: client.clone(),
|
||||
port_str: (8080 + sim_id.to_index()).to_string(),
|
||||
connected: false,
|
||||
quit_on_disconnect: false,
|
||||
@@ -78,7 +80,9 @@ impl AppWindow for GdbServerWindow {
|
||||
self.connected = false;
|
||||
}
|
||||
let start_button = Button::new("Start");
|
||||
if ui.add_enabled(port_num.is_some(), start_button).clicked() {
|
||||
let can_start =
|
||||
port_num.is_some() && self.client.sim_state(self.sim_id) == SimState::Ready;
|
||||
if ui.add_enabled(can_start, start_button).clicked() {
|
||||
let port = port_num.unwrap();
|
||||
self.server.start(port);
|
||||
self.connected = true;
|
||||
|
||||
Reference in New Issue
Block a user