Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

debugger in web

Layle01347ef85c2b
diff --git a/.github/workflows/deploy-wasm.yml b/.github/workflows/deploy-wasm.yml
index 234d17b..9205b91 100644
--- a/.github/workflows/deploy-wasm.yml
+++ b/.github/workflows/deploy-wasm.yml
@@ -40,11 +40,17 @@ jobs:
- name: Build WASM
run: wasm-pack build crates/web --target web --out-dir pkg --release
+ - name: Build WASM (debug)
+ run: wasm-pack build crates/web --target web --out-dir pkg-dbg --release -- --features debug
+
- name: Prepare site
run: |
- mkdir -p _site
+ mkdir -p _site/dbg
cp crates/web/index.html _site/
cp -r crates/web/pkg _site/pkg
+ cp crates/web/index.html _site/dbg/
+ sed -i 's|./pkg/gecko_web.js|../pkg-dbg/gecko_web.js|' _site/dbg/index.html
+ cp -r crates/web/pkg-dbg _site/pkg-dbg
- uses: actions/upload-pages-artifact@v3
diff --git a/Cargo.lock b/Cargo.lock
index 64225c5..dca75fc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -655,13 +655,24 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f"
+[[package]]
+name = "dbglib"
+version = "0.1.0"
+dependencies = [
+ "backend-wgpu",
+ "disasm",
+ "egui",
+ "egui-phosphor",
+ "gecko",
+]
+
[[package]]
name = "debugger"
version = "0.1.0"
dependencies = [
"backend-wgpu",
"clap",
- "disasm",
+ "dbglib",
"egui",
"egui-phosphor",
"egui-wgpu",
@@ -1066,7 +1077,9 @@ version = "0.1.0"
dependencies = [
"backend-wgpu",
"console_error_panic_hook",
+ "dbglib",
"egui",
+ "egui-phosphor",
"egui-wgpu",
"egui-winit",
"gecko",
diff --git a/Cargo.toml b/Cargo.toml
index 1bcb6c1..5db8fed 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
"crates/image",
"crates/tinyapp",
"crates/backend-wgpu",
+ "crates/dbglib",
"crates/debugger",
"crates/scripting",
"crates/dspemu",
diff --git a/crates/dbglib/Cargo.toml b/crates/dbglib/Cargo.toml
new file mode 100644
index 0000000..e72dd2d
--- /dev/null
+++ b/crates/dbglib/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "dbglib"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+gecko = { path = "../gecko" }
+disasm = { path = "../disasm" }
+backend-wgpu = { path = "../backend-wgpu" }
+egui = "0.33.3"
+egui-phosphor = "0.11.0"
diff --git a/crates/dbglib/src/lib.rs b/crates/dbglib/src/lib.rs
new file mode 100644
index 0000000..bf112a3
--- /dev/null
+++ b/crates/dbglib/src/lib.rs
@@ -0,0 +1,124 @@
+pub mod trace;
+pub mod windows;
+
+#[cfg(not(target_arch = "wasm32"))]
+use std::io::Write;
+
+use gecko::gamecube::GameCube;
+
+#[derive(Debug, PartialEq, Eq, Clone, Copy)]
+pub enum EmulatorState {
+ Running,
+ Paused,
+ Step,
+ RunUntilVsync,
+ RunUntilAddress(u32),
+}
+
+pub struct Debugger {
+ state: EmulatorState,
+ #[cfg(not(target_arch = "wasm32"))]
+ trace_writer: Option<Box<dyn Write>>,
+}
+
+impl Debugger {
+ pub fn new() -> Self {
+ Debugger {
+ state: EmulatorState::Paused,
+ #[cfg(not(target_arch = "wasm32"))]
+ trace_writer: None,
+ }
+ }
+
+ pub fn state(&self) -> EmulatorState {
+ self.state
+ }
+
+ pub fn set_state(&mut self, state: EmulatorState) {
+ self.state = state;
+ }
+
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn is_tracing(&self) -> bool {
+ self.trace_writer.is_some()
+ }
+
+ #[cfg(target_arch = "wasm32")]
+ pub fn is_tracing(&self) -> bool {
+ false
+ }
+
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn start_trace(&mut self, writer: Box<dyn Write>) {
+ self.trace_writer = Some(writer);
+ }
+
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn stop_trace(&mut self) {
+ if let Some(mut w) = self.trace_writer.take() {
+ let _ = w.flush();
+ }
+ }
+
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn trace_step(&mut self, emulator: &GameCube) {
+ if let Some(ref mut writer) = self.trace_writer {
+ let line = trace::format_trace_line(emulator);
+ let _ = writeln!(writer, "{}", line);
+ }
+ }
+
+ #[cfg(target_arch = "wasm32")]
+ pub fn trace_step(&mut self, _emulator: &GameCube) {}
+
+ /// Execute one frame's worth of emulation based on the current state.
+ ///
+ /// After execution, transient states (`Step`, `RunUntilVsync`, `RunUntilAddress`)
+ /// automatically transition to `Paused`.
+ pub fn tick(&mut self, emulator: &mut GameCube) {
+ match self.state {
+ EmulatorState::Running => {
+ if self.is_tracing() {
+ emulator.prepare_frame();
+ while !emulator.vsync_pending {
+ self.trace_step(emulator);
+ emulator.step();
+ }
+ } else {
+ emulator.run_until_vsync();
+ }
+ }
+ EmulatorState::Step => {
+ self.trace_step(emulator);
+ emulator.step();
+ self.state = EmulatorState::Paused;
+ }
+ EmulatorState::RunUntilVsync => {
+ if self.is_tracing() {
+ emulator.prepare_frame();
+ while !emulator.vsync_pending {
+ self.trace_step(emulator);
+ emulator.step();
+ }
+ } else {
+ emulator.run_until_vsync();
+ }
+ self.state = EmulatorState::Paused;
+ }
+ EmulatorState::RunUntilAddress(addr) => {
+ while emulator.cpu.pc != addr {
+ self.trace_step(emulator);
+ emulator.step();
+ }
+ self.state = EmulatorState::Paused;
+ }
+ EmulatorState::Paused => {}
+ }
+ }
+}
+
+impl Default for Debugger {
+ fn default() -> Self {
+ Self::new()
+ }
+}
diff --git a/crates/dbglib/src/trace.rs b/crates/dbglib/src/trace.rs
new file mode 100644
index 0000000..e7155f0
--- /dev/null
+++ b/crates/dbglib/src/trace.rs
@@ -0,0 +1,58 @@
+use disasm::gekko::GekkoInstruction;
+use disasm::tokenizer::{self, AsmToken};
+use gecko::gamecube::GameCube;
+
+const DISASM_COL: usize = 22;
+const COMMENT_COL: usize = 50;
+
+/// Format a single trace line for the current PC of the emulator.
+///
+/// Returns a string like `80003100 4E800020 blr ; lr=80001234`
+pub fn format_trace_line(emulator: &GameCube) -> String {
+ let pc = emulator.cpu.pc;
+ let raw = emulator.mmio.virt_read_u32(pc);
+
+ if let Some((instr, _)) = GekkoInstruction::decode(emulator.mmio.virt_slice(pc, 4)) {
+ let text = format!("{}", instr);
+ let comment = reg_comment(&text, &emulator.cpu.gprs, &emulator.cpu.fprs);
+ let pad = COMMENT_COL.saturating_sub(DISASM_COL + text.len());
+ if comment.is_empty() {
+ format!("{:08X} {:08X} {}", pc, raw, text)
+ } else {
+ format!("{:08X} {:08X} {}{}; {}", pc, raw, text, " ".repeat(pad), comment)
+ }
+ } else {
+ format!("{:08X} {:08X} <unknown>", pc, raw)
+ }
+}
+
+/// Generate register-value comments for a disassembled instruction line.
+///
+/// Parses the disassembly text for register references and returns a comma-separated
+/// list of their current values, e.g. `r3=00000001, r4=80003000`.
+pub fn reg_comment(disasm_text: &str, gprs: &[u32; 32], fprs: &[f64; 32]) -> String {
+ let tokens = tokenizer::tokenize(disasm_text);
+ let mut parts = Vec::new();
+ let mut gpr_seen = [false; 32];
+ let mut fpr_seen = [false; 32];
+ for tok in &tokens {
+ match tok {
+ AsmToken::Gpr(n) => {
+ let n = *n as usize;
+ if !gpr_seen[n] {
+ gpr_seen[n] = true;
+ parts.push(format!("r{}={:08X}", n, gprs[n]));
+ }
+ }
+ AsmToken::Fpr(n) => {
+ let n = *n as usize;
+ if !fpr_seen[n] {
+ fpr_seen[n] = true;
+ parts.push(format!("f{}={:.6e}", n, fprs[n]));
+ }
+ }
+ _ => {}
+ }
+ }
+ parts.join(", ")
+}
diff --git a/crates/dbglib/src/windows.rs b/crates/dbglib/src/windows.rs
new file mode 100644
index 0000000..862649b
--- /dev/null
+++ b/crates/dbglib/src/windows.rs
@@ -0,0 +1,34 @@
+pub mod controls;
+pub mod cpu;
+pub mod dsp;
+pub mod dvd;
+pub mod exi;
+pub mod gx;
+pub mod irq;
+pub mod mmio;
+
+use disasm::tokenizer::AsmToken;
+use egui::{Color32, RichText};
+
+pub(crate) fn flag(ui: &mut egui::Ui, val: bool) {
+ let (icon, color) = if val {
+ (egui_phosphor::regular::CHECK_CIRCLE, Color32::from_rgb(100, 220, 100))
+ } else {
+ (egui_phosphor::regular::CIRCLE, Color32::from_rgb(70, 70, 70))
+ };
+ ui.label(RichText::new(icon).color(color));
+}
+
+pub(crate) fn token_color(token: &AsmToken<'_>) -> Option<Color32> {
+ match token {
+ AsmToken::Mnemonic(_) => Some(Color32::from_rgb(100, 180, 255)),
+ AsmToken::Gpr(_) | AsmToken::Fpr(_) | AsmToken::CrField(_) | AsmToken::Spr(_) => {
+ Some(Color32::from_rgb(255, 200, 100))
+ }
+ AsmToken::ImmSigned(_) | AsmToken::ImmUnsigned(_) | AsmToken::ImmHex(_) | AsmToken::Displacement(_) => {
+ Some(Color32::from_rgb(150, 220, 150))
+ }
+ AsmToken::BranchTarget(_) => Some(Color32::from_rgb(255, 150, 150)),
+ AsmToken::Punct(_) | AsmToken::Text(_) => None,
+ }
+}
diff --git a/crates/debugger/src/windows/controls.rs b/crates/dbglib/src/windows/controls.rs
similarity index 98%
rename from crates/debugger/src/windows/controls.rs
rename to crates/dbglib/src/windows/controls.rs
index 9dcd662..647097a 100644
--- a/crates/debugger/src/windows/controls.rs
+++ b/crates/dbglib/src/windows/controls.rs
@@ -1,7 +1,7 @@
use egui::Context;
use egui_phosphor::regular as icons;
-use crate::debugger::EmulatorState;
+use crate::EmulatorState;
pub fn show_controls(
ctx: &Context,
diff --git a/crates/debugger/src/windows/cpu.rs b/crates/dbglib/src/windows/cpu.rs
similarity index 92%
rename from crates/debugger/src/windows/cpu.rs
rename to crates/dbglib/src/windows/cpu.rs
index ba55ee6..9d17a98 100644
--- a/crates/debugger/src/windows/cpu.rs
+++ b/crates/dbglib/src/windows/cpu.rs
@@ -2,20 +2,7 @@ use egui::{Align, Color32, Context, Grid, RichText, ScrollArea};
use gecko::cpu::Cpu;
use gecko::mmio::Mmio;
-fn token_color(token: &disasm::tokenizer::AsmToken<'_>) -> Option<Color32> {
- use disasm::tokenizer::AsmToken;
- match token {
- AsmToken::Mnemonic(_) => Some(Color32::from_rgb(100, 180, 255)),
- AsmToken::Gpr(_) | AsmToken::Fpr(_) | AsmToken::CrField(_) | AsmToken::Spr(_) => {
- Some(Color32::from_rgb(255, 200, 100))
- }
- AsmToken::ImmSigned(_) | AsmToken::ImmUnsigned(_) | AsmToken::ImmHex(_) | AsmToken::Displacement(_) => {
- Some(Color32::from_rgb(150, 220, 150))
- }
- AsmToken::BranchTarget(_) => Some(Color32::from_rgb(255, 150, 150)),
- AsmToken::Punct(_) | AsmToken::Text(_) => None,
- }
-}
+use super::token_color;
pub fn show_cpu(ctx: &Context, open: &mut bool, cpu: &Cpu, mmio: &Mmio) {
egui::Window::new("CPU")
diff --git a/crates/debugger/src/windows/dsp.rs b/crates/dbglib/src/windows/dsp.rs
similarity index 86%
rename from crates/debugger/src/windows/dsp.rs
rename to crates/dbglib/src/windows/dsp.rs
index 55794fd..55b445e 100644
--- a/crates/debugger/src/windows/dsp.rs
+++ b/crates/dbglib/src/windows/dsp.rs
@@ -1,20 +1,7 @@
use egui::{Color32, Context, Grid, RichText, ScrollArea};
use gecko::flipper::dsp::Dsp;
-fn token_color(token: &disasm::tokenizer::AsmToken<'_>) -> Option<Color32> {
- use disasm::tokenizer::AsmToken;
- match token {
- AsmToken::Mnemonic(_) => Some(Color32::from_rgb(100, 180, 255)),
- AsmToken::Gpr(_) | AsmToken::Fpr(_) | AsmToken::CrField(_) | AsmToken::Spr(_) => {
- Some(Color32::from_rgb(255, 200, 100))
- }
- AsmToken::ImmSigned(_) | AsmToken::ImmUnsigned(_) | AsmToken::ImmHex(_) | AsmToken::Displacement(_) => {
- Some(Color32::from_rgb(150, 220, 150))
- }
- AsmToken::BranchTarget(_) => Some(Color32::from_rgb(255, 150, 150)),
- AsmToken::Punct(_) | AsmToken::Text(_) => None,
- }
-}
+use super::token_color;
pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
egui::Window::new("DSP").open(open).show(ctx, |ui| {
@@ -34,6 +21,7 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
ui.monospace(format!("{:#06X}", dsp.csr.raw()));
ui.end_row();
+ #[cfg(not(target_arch = "wasm32"))]
if ui.button("Dump DSP").clicked() {
let mut dump = Vec::new();
dump.extend_from_slice(&dsp.iram[..]);
diff --git a/crates/debugger/src/windows/dvd.rs b/crates/dbglib/src/windows/dvd.rs
similarity index 100%
rename from crates/debugger/src/windows/dvd.rs
rename to crates/dbglib/src/windows/dvd.rs
diff --git a/crates/debugger/src/windows/exi.rs b/crates/dbglib/src/windows/exi.rs
similarity index 100%
rename from crates/debugger/src/windows/exi.rs
rename to crates/dbglib/src/windows/exi.rs
diff --git a/crates/debugger/src/windows/gx.rs b/crates/dbglib/src/windows/gx.rs
similarity index 100%
rename from crates/debugger/src/windows/gx.rs
rename to crates/dbglib/src/windows/gx.rs
diff --git a/crates/debugger/src/windows/irq.rs b/crates/dbglib/src/windows/irq.rs
similarity index 100%
rename from crates/debugger/src/windows/irq.rs
rename to crates/dbglib/src/windows/irq.rs
diff --git a/crates/debugger/src/windows/mmio.rs b/crates/dbglib/src/windows/mmio.rs
similarity index 100%
rename from crates/debugger/src/windows/mmio.rs
rename to crates/dbglib/src/windows/mmio.rs
diff --git a/crates/debugger/Cargo.toml b/crates/debugger/Cargo.toml
index 719cbf0..2e9fbbc 100644
--- a/crates/debugger/Cargo.toml
+++ b/crates/debugger/Cargo.toml
@@ -11,14 +11,14 @@ scripting-mut-traps = ["gecko/scripting-mut-traps", "scripting/scripting-mut-tra
gecko = { path = "../gecko", features = ["scripting"] }
scripting = { path = "../scripting" }
image = { path = "../image" }
-disasm = { path = "../disasm" }
+dbglib = { path = "../dbglib" }
backend-wgpu = { path = "../backend-wgpu" }
wgpu = "28.0.0"
winit = "0.30.13"
pollster = "0.4.0"
-egui = "0.33"
-egui-wgpu = "0.33"
-egui-winit = { version = "0.33", features = ["wayland", "x11"] }
+egui = "0.33.3"
+egui-wgpu = "0.33.3"
+egui-winit = { version = "0.33.3", features = ["wayland", "x11"] }
egui-phosphor = "0.11.0"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
clap = { version = "4.6.0", features = ["derive"] }
diff --git a/crates/debugger/src/debugger.rs b/crates/debugger/src/debugger.rs
index c0c3654..6671535 100644
--- a/crates/debugger/src/debugger.rs
+++ b/crates/debugger/src/debugger.rs
@@ -1,23 +1,12 @@
use std::fs::File;
-use std::io::{BufWriter, Write};
+use std::io::BufWriter;
-use disasm::gekko::GekkoInstruction;
-use disasm::tokenizer::{self, AsmToken};
-use gecko::gamecube::GameCube;
+use dbglib::Debugger;
const TRACE_FILENAME: &str = "trace.log";
-#[derive(PartialEq, Eq, Clone, Copy)]
-pub enum EmulatorState {
- Running,
- Paused,
- Step,
- RunUntilVsync,
- RunUntilAddress(u32),
-}
-
pub struct DebuggerUi {
- pub emulator_state: EmulatorState,
+ pub debugger: Debugger,
pub show_cpu: bool,
pub show_dsp: bool,
pub show_gx_state: bool,
@@ -30,13 +19,12 @@ pub struct DebuggerUi {
pub memory_addr_input: String,
pub run_until_addr_input: String,
pub dvd_cover_open: Option<bool>,
- pub trace_writer: Option<BufWriter<File>>,
}
impl Default for DebuggerUi {
fn default() -> Self {
DebuggerUi {
- emulator_state: EmulatorState::Paused,
+ debugger: Debugger::new(),
show_cpu: true,
show_dsp: false,
show_controls: true,
@@ -49,80 +37,17 @@ impl Default for DebuggerUi {
memory_addr_input: "80000000".to_string(),
run_until_addr_input: String::new(),
dvd_cover_open: None,
- trace_writer: None,
}
}
}
impl DebuggerUi {
- pub fn is_tracing(&self) -> bool {
- self.trace_writer.is_some()
- }
-
pub fn start_trace(&mut self) {
let file = File::create(TRACE_FILENAME).expect("failed to create trace file");
- self.trace_writer = Some(BufWriter::new(file));
+ self.debugger.start_trace(Box::new(BufWriter::new(file)));
}
pub fn stop_trace(&mut self) {
- if let Some(mut w) = self.trace_writer.take() {
- let _ = w.flush();
- }
- }
-
- pub fn trace_step(&mut self, emulator: &GameCube) {
- if let Some(ref mut writer) = self.trace_writer {
- let pc = emulator.cpu.pc;
- let raw = emulator.mmio.virt_read_u32(pc);
- if let Some((instr, _)) = GekkoInstruction::decode(emulator.mmio.virt_slice(pc, 4)) {
- let text = format!("{}", instr);
- let comment = reg_comment(&text, &emulator.cpu.gprs, &emulator.cpu.fprs);
- const DISASM_COL: usize = 22;
- const COMMENT_COL: usize = 50;
- let pad = COMMENT_COL.saturating_sub(DISASM_COL + text.len());
- if comment.is_empty() {
- let _ = writeln!(writer, "{:08X} {:08X} {}", pc, raw, text);
- } else {
- let _ = writeln!(
- writer,
- "{:08X} {:08X} {}{}; {}",
- pc,
- raw,
- text,
- " ".repeat(pad),
- comment
- );
- }
- } else {
- let _ = writeln!(writer, "{:08X} {:08X} <unknown>", pc, raw);
- }
- }
- }
-}
-
-fn reg_comment(disasm_text: &str, gprs: &[u32; 32], fprs: &[f64; 32]) -> String {
- let tokens = tokenizer::tokenize(disasm_text);
- let mut parts = Vec::new();
- let mut gpr_seen = [false; 32];
- let mut fpr_seen = [false; 32];
- for tok in &tokens {
- match tok {
- AsmToken::Gpr(n) => {
- let n = *n as usize;
- if !gpr_seen[n] {
- gpr_seen[n] = true;
- parts.push(format!("r{}={:08X}", n, gprs[n]));
- }
- }
- AsmToken::Fpr(n) => {
- let n = *n as usize;
- if !fpr_seen[n] {
- fpr_seen[n] = true;
- parts.push(format!("f{}={:.6e}", n, fprs[n]));
- }
- }
- _ => {}
- }
+ self.debugger.stop_trace();
}
- parts.join(", ")
}
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index ee3d676..19fa1a5 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -17,7 +17,6 @@ use crate::render::RenderState;
mod debugger;
mod render;
-mod windows;
struct App {
emulator: GameCube,
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 62e1024..bd40bed 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -1,10 +1,10 @@
+use dbglib::EmulatorState;
use egui::ViewportId;
use gecko::gamecube::GameCube;
use std::sync::Arc;
use winit::window::Window;
-use crate::debugger::{DebuggerUi, EmulatorState};
-use crate::windows;
+use crate::debugger::DebuggerUi;
const SHADER: &str = include_str!("xfb.wgsl");
@@ -184,44 +184,7 @@ impl RenderState {
}
}
- match debugger_ui.emulator_state {
- EmulatorState::Running => {
- if debugger_ui.is_tracing() {
- emulator.prepare_frame();
- while !emulator.vsync_pending {
- debugger_ui.trace_step(emulator);
- emulator.step();
- }
- } else {
- emulator.run_until_vsync();
- }
- }
- EmulatorState::Step => {
- debugger_ui.trace_step(emulator);
- emulator.step();
- debugger_ui.emulator_state = EmulatorState::Paused;
- }
- EmulatorState::RunUntilVsync => {
- if debugger_ui.is_tracing() {
- emulator.prepare_frame();
- while !emulator.vsync_pending {
- debugger_ui.trace_step(emulator);
- emulator.step();
- }
- } else {
- emulator.run_until_vsync();
- }
- debugger_ui.emulator_state = EmulatorState::Paused;
- }
- EmulatorState::RunUntilAddress(addr) => {
- while emulator.cpu.pc != addr {
- debugger_ui.trace_step(emulator);
- emulator.step();
- }
- debugger_ui.emulator_state = EmulatorState::Paused;
- }
- EmulatorState::Paused => {}
- }
+ debugger_ui.debugger.tick(emulator);
let frame = match self.surface.get_current_texture() {
Ok(f) => f,
@@ -250,33 +213,33 @@ impl RenderState {
egui::Panel::top("menu_bar").show_inside(ui, |ui| {
egui::MenuBar::new().ui(ui, |ui| {
ui.menu_button("Emulator", |ui| {
- let is_paused = debugger_ui.emulator_state == EmulatorState::Paused;
- let is_running = debugger_ui.emulator_state == EmulatorState::Running;
+ let is_paused = debugger_ui.debugger.state() == EmulatorState::Paused;
+ let is_running = debugger_ui.debugger.state() == EmulatorState::Running;
use egui_phosphor::regular as icons;
if ui
.add_enabled(is_paused, egui::Button::new(format!("{} Continue", icons::PLAY)))
.clicked()
{
- debugger_ui.emulator_state = EmulatorState::Running;
+ debugger_ui.debugger.set_state(EmulatorState::Running);
ui.close();
}
if ui
.add_enabled(is_running, egui::Button::new(format!("{} Pause", icons::PAUSE)))
.clicked()
{
- debugger_ui.emulator_state = EmulatorState::Paused;
+ debugger_ui.debugger.set_state(EmulatorState::Paused);
ui.close();
}
if ui
.add_enabled(is_paused, egui::Button::new(format!("{} Step", icons::SKIP_FORWARD)))
.clicked()
{
- debugger_ui.emulator_state = EmulatorState::Step;
+ debugger_ui.debugger.set_state(EmulatorState::Step);
ui.close();
}
if ui.button(format!("{} Run Until VSync", icons::FAST_FORWARD)).clicked() {
- debugger_ui.emulator_state = EmulatorState::RunUntilVsync;
+ debugger_ui.debugger.set_state(EmulatorState::RunUntilVsync);
ui.close();
}
});
@@ -295,25 +258,27 @@ impl RenderState {
});
if debugger_ui.show_cpu {
- windows::cpu::show_cpu(&ctx, &mut debugger_ui.show_cpu, cpu, mmio);
+ dbglib::windows::cpu::show_cpu(&ctx, &mut debugger_ui.show_cpu, cpu, mmio);
}
if debugger_ui.show_dsp {
- windows::dsp::show_dsp(&ctx, &mut debugger_ui.show_dsp, &emulator.dsp);
+ dbglib::windows::dsp::show_dsp(&ctx, &mut debugger_ui.show_dsp, &emulator.dsp);
}
if debugger_ui.show_controls {
let mut start_trace = false;
let mut stop_trace = false;
- let tracing = debugger_ui.is_tracing();
- windows::controls::show_controls(
+ let tracing = debugger_ui.debugger.is_tracing();
+ let mut state = debugger_ui.debugger.state();
+ dbglib::windows::controls::show_controls(
&ctx,
&mut debugger_ui.show_controls,
- &mut debugger_ui.emulator_state,
+ &mut state,
&mut debugger_ui.run_until_addr_input,
&mut debugger_ui.dvd_cover_open,
tracing,
&mut start_trace,
&mut stop_trace,
);
+ debugger_ui.debugger.set_state(state);
if start_trace {
debugger_ui.start_trace();
}
@@ -322,10 +287,10 @@ impl RenderState {
}
}
if debugger_ui.show_gx_state {
- windows::gx::show_gx(&ctx, &mut debugger_ui.show_gx_state, gx, mmio);
+ dbglib::windows::gx::show_gx(&ctx, &mut debugger_ui.show_gx_state, gx, mmio);
}
if debugger_ui.show_mmio {
- windows::mmio::show_mmio(
+ dbglib::windows::mmio::show_mmio(
&ctx,
&mut debugger_ui.show_mmio,
&mut debugger_ui.memory_base,
@@ -334,13 +299,13 @@ impl RenderState {
);
}
if debugger_ui.show_dvd {
- windows::dvd::show_dvd(&ctx, &mut debugger_ui.show_dvd, &emulator.di);
+ dbglib::windows::dvd::show_dvd(&ctx, &mut debugger_ui.show_dvd, &emulator.di);
}
if debugger_ui.show_exi {
- windows::exi::show_exi(&ctx, &mut debugger_ui.show_exi, &emulator.exi);
+ dbglib::windows::exi::show_exi(&ctx, &mut debugger_ui.show_exi, &emulator.exi);
}
if debugger_ui.show_irqs {
- windows::irq::show_irq(&ctx, &mut debugger_ui.show_irqs, &emulator.cpu, &emulator.pi);
+ dbglib::windows::irq::show_irq(&ctx, &mut debugger_ui.show_irqs, &emulator.cpu, &emulator.pi);
}
});
diff --git a/crates/debugger/src/windows.rs b/crates/debugger/src/windows.rs
deleted file mode 100644
index c5cdb33..0000000
--- a/crates/debugger/src/windows.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-pub mod controls;
-pub mod cpu;
-pub mod dsp;
-pub mod dvd;
-pub mod exi;
-pub mod gx;
-pub mod irq;
-pub mod mmio;
-
-pub(crate) fn flag(ui: &mut egui::Ui, val: bool) {
- use egui::{Color32, RichText};
- let (icon, color) = if val {
- (egui_phosphor::regular::CHECK_CIRCLE, Color32::from_rgb(100, 220, 100))
- } else {
- (egui_phosphor::regular::CIRCLE, Color32::from_rgb(70, 70, 70))
- };
- ui.label(RichText::new(icon).color(color));
-}
diff --git a/crates/tinyapp/Cargo.toml b/crates/tinyapp/Cargo.toml
index 3efd37f..f032662 100644
--- a/crates/tinyapp/Cargo.toml
+++ b/crates/tinyapp/Cargo.toml
@@ -16,9 +16,9 @@ backend-wgpu = { path = "../backend-wgpu" }
wgpu = "28.0.0"
winit = "0.30.13"
pollster = "0.4.0"
-egui = "0.33"
-egui-wgpu = "0.33"
-egui-winit = { version = "0.33", features = ["wayland", "x11"] }
-egui_plot = "0.34"
+egui = "0.33.3"
+egui-wgpu = "0.33.3"
+egui-winit = { version = "0.33.3", features = ["wayland", "x11"] }
+egui_plot = "0.34.1"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
clap = { version = "4.6.0", features = ["derive"] }
diff --git a/crates/web/Cargo.toml b/crates/web/Cargo.toml
index ed24a32..e816906 100644
--- a/crates/web/Cargo.toml
+++ b/crates/web/Cargo.toml
@@ -6,10 +6,15 @@ edition = "2024"
[lib]
crate-type = ["cdylib"]
+[features]
+debug = ["dep:dbglib", "dep:egui-phosphor"]
+
[dependencies]
gecko = { path = "../gecko" }
image = { path = "../image" }
backend-wgpu = { path = "../backend-wgpu" }
+dbglib = { path = "../dbglib", optional = true }
+egui-phosphor = { version = "0.11.0", optional = true }
wgpu = { version = "28.0.0", features = ["webgl"] }
winit = { version = "0.30.13", features = ["rwh_06"] }
egui = "0.33.3"
diff --git a/crates/web/index.html b/crates/web/index.html
index 83db8cc..5ff23d2 100644
--- a/crates/web/index.html
+++ b/crates/web/index.html
@@ -40,31 +40,44 @@
gap: 12px;
}
- .file-row label {
+ .file-row .file-label {
width: 100px;
text-align: right;
color: #aaa;
font-size: 14px;
}
+ .file-picker {
+ position: relative;
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ overflow: hidden;
+ flex: 0 0 auto;
+ width: auto;
+ white-space: nowrap;
+ line-height: 1.2;
+ text-align: center;
+ }
+
.file-row .name {
color: #8f8;
font-size: 14px;
min-width: 120px;
}
- .file-row button,
+ .file-picker,
#start {
background: #333;
color: #ccc;
border: 1px solid #555;
border-radius: 6px;
- padding: 6px 16px;
+ padding: 6px 12px;
cursor: pointer;
font-size: 14px;
}
- .file-row button:hover,
+ .file-picker:hover,
#start:hover {
background: #444;
border-color: #8f8;
@@ -88,31 +101,38 @@
height: 100vh;
}
- input[type="file"] {
+ .file-picker input[type="file"] {
position: absolute;
+ inset: 0;
opacity: 0;
- width: 0;
- height: 0;
- pointer-events: none;
+ width: 100%;
+ height: 100%;
+ cursor: pointer;
}
</style>
</head>
<body>
+ <!-- shoutout to hazel, vxpm, geo, pdawg, lycoder and everyone else :> -->
+
<div id="setup">
<div class="file-row">
- <label>IPL / DOL:</label>
- <button id="btn-rom">Choose file</button>
+ <label class="file-label">IPL / DOL:</label>
+ <label class="file-picker">
+ <span>Choose file</span>
+ <input type="file" id="input-rom" accept=".dol,.bin">
+ </label>
<span class="name" id="rom-name">none</span>
</div>
- <input type="file" id="input-rom" accept=".dol,.ipl">
<div class="file-row">
- <label>DSP IROM:</label>
- <button id="btn-dsp">Choose file</button>
+ <label class="file-label">DSP IROM:</label>
+ <label class="file-picker">
+ <span>Choose file</span>
+ <input type="file" id="input-dsp" accept=".bin">
+ </label>
<span class="name" id="dsp-name">none</span>
</div>
- <input type="file" id="input-dsp" accept=".bin">
<button id="start" disabled>Start</button>
</div>
@@ -127,8 +147,6 @@
let romName = '';
let dspData = null;
- const btnRom = document.getElementById('btn-rom');
- const btnDsp = document.getElementById('btn-dsp');
const inputRom = document.getElementById('input-rom');
const inputDsp = document.getElementById('input-dsp');
const romNameEl = document.getElementById('rom-name');
@@ -143,9 +161,6 @@
});
}
- btnRom.addEventListener('click', () => inputRom.click());
- btnDsp.addEventListener('click', () => inputDsp.click());
-
inputRom.addEventListener('change', async (e) => {
if (e.target.files.length > 0) {
const file = e.target.files[0];
diff --git a/crates/web/src/debug_ui.rs b/crates/web/src/debug_ui.rs
new file mode 100644
index 0000000..aae306c
--- /dev/null
+++ b/crates/web/src/debug_ui.rs
@@ -0,0 +1,117 @@
+use dbglib::{Debugger, EmulatorState};
+use gecko::gamecube::GameCube;
+
+pub struct DebugState {
+ pub debugger: Debugger,
+ pub show_cpu: bool,
+ pub show_dsp: bool,
+ pub show_gx_state: bool,
+ pub show_mmio: bool,
+ pub show_dvd: bool,
+ pub show_exi: bool,
+ pub show_irqs: bool,
+ pub show_controls: bool,
+ pub memory_base: u32,
+ pub memory_addr_input: String,
+ pub run_until_addr_input: String,
+ pub dvd_cover_open: Option<bool>,
+}
+
+impl Default for DebugState {
+ fn default() -> Self {
+ let mut debugger = Debugger::new();
+ debugger.set_state(EmulatorState::Running);
+ DebugState {
+ debugger,
+ show_cpu: false,
+ show_dsp: false,
+ show_controls: true,
+ show_gx_state: false,
+ show_mmio: false,
+ show_dvd: false,
+ show_exi: false,
+ show_irqs: false,
+ memory_base: 0x8000_0000,
+ memory_addr_input: "80000000".to_string(),
+ run_until_addr_input: String::new(),
+ dvd_cover_open: None,
+ }
+ }
+}
+
+impl DebugState {
+ pub fn tick(&mut self, emulator: &mut GameCube) {
+ if let Some(open) = self.dvd_cover_open.take() {
+ if open {
+ emulator.open_cover();
+ } else {
+ emulator.close_cover();
+ }
+ }
+ self.debugger.tick(emulator);
+ }
+
+ pub fn show(&mut self, ctx: &egui::Context, emulator: &GameCube) {
+ egui::Window::new("Debug")
+ .title_bar(false)
+ .resizable(false)
+ .anchor(egui::Align2::LEFT_TOP, [8.0, 8.0])
+ .show(ctx, |ui| {
+ ui.horizontal(|ui| {
+ ui.checkbox(&mut self.show_controls, "Controls");
+ ui.checkbox(&mut self.show_cpu, "CPU");
+ ui.checkbox(&mut self.show_dsp, "DSP");
+ ui.checkbox(&mut self.show_gx_state, "GX");
+ ui.checkbox(&mut self.show_mmio, "MMIO");
+ ui.checkbox(&mut self.show_dvd, "DVD");
+ ui.checkbox(&mut self.show_exi, "EXI");
+ ui.checkbox(&mut self.show_irqs, "IRQ");
+ });
+ });
+
+ if self.show_cpu {
+ dbglib::windows::cpu::show_cpu(ctx, &mut self.show_cpu, &emulator.cpu, &emulator.mmio);
+ }
+ if self.show_dsp {
+ dbglib::windows::dsp::show_dsp(ctx, &mut self.show_dsp, &emulator.dsp);
+ }
+ if self.show_controls {
+ let mut start_trace = false;
+ let mut stop_trace = false;
+ let tracing = self.debugger.is_tracing();
+ let mut state = self.debugger.state();
+ dbglib::windows::controls::show_controls(
+ ctx,
+ &mut self.show_controls,
+ &mut state,
+ &mut self.run_until_addr_input,
+ &mut self.dvd_cover_open,
+ tracing,
+ &mut start_trace,
+ &mut stop_trace,
+ );
+ self.debugger.set_state(state);
+ }
+ if self.show_gx_state {
+ dbglib::windows::gx::show_gx(ctx, &mut self.show_gx_state, &emulator.gx, &emulator.mmio);
+ }
+ if self.show_mmio {
+ dbglib::windows::mmio::show_mmio(
+ ctx,
+ &mut self.show_mmio,
+ &mut self.memory_base,
+ &mut self.memory_addr_input,
+ &emulator.mmio,
+ );
+ }
+ if self.show_dvd {
+ dbglib::windows::dvd::show_dvd(ctx, &mut self.show_dvd, &emulator.di);
+ }
+ if self.show_exi {
+ dbglib::windows::exi::show_exi(ctx, &mut self.show_exi, &emulator.exi);
+ }
+ if self.show_irqs {
+ dbglib::windows::irq::show_irq(ctx, &mut self.show_irqs, &emulator.cpu, &emulator.pi);
+ }
+ }
+}
diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs
index 3c5d015..dea5b6a 100644
--- a/crates/web/src/lib.rs
+++ b/crates/web/src/lib.rs
@@ -18,6 +18,9 @@ use winit::{
window::{Window, WindowId},
};
+#[cfg(feature = "debug")]
+mod debug_ui;
+
const SHADER: &str = include_str!("../../tinyapp/src/xfb.wgsl");
struct State {
@@ -167,6 +170,12 @@ impl State {
);
let egui_ctx = egui::Context::default();
+ #[cfg(feature = "debug")]
+ {
+ let mut fonts = egui::FontDefinitions::default();
+ egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);
+ egui_ctx.set_fonts(fonts);
+ }
let egui_renderer = egui_wgpu::Renderer::new(&device, surface_format, egui_wgpu::RendererOptions::default());
let egui_winit = egui_winit::State::new(egui_ctx.clone(), ViewportId::ROOT, window.as_ref(), None, None, None);
@@ -203,7 +212,12 @@ impl State {
self.gx_renderer.resize(&self.device, width, height);
}
- fn render(&mut self, emulator: &mut GameCube, window: &Window) {
+ fn render(
+ &mut self,
+ emulator: &mut GameCube,
+ #[cfg(feature = "debug")] debug_state: &mut debug_ui::DebugState,
+ window: &Window,
+ ) {
let now = now_ms();
let delta = (now - self.last_frame_ms) / 1000.0;
self.last_frame_ms = now;
@@ -219,6 +233,9 @@ impl State {
};
let native_pct = (fps / native_hz) * 100.0;
+ #[cfg(feature = "debug")]
+ debug_state.tick(emulator);
+ #[cfg(not(feature = "debug"))]
emulator.run_until_vsync();
let frame = match self.surface.get_current_texture() {
@@ -227,7 +244,8 @@ impl State {
};
let view = frame.texture.create_view(&Default::default());
- if !emulator.gx.draw_commands.commands.is_empty() {
+ let used_gx = !emulator.gx.draw_commands.commands.is_empty();
+ if used_gx {
self.render_gx(emulator, &view);
} else {
self.render_xfb(emulator, &view);
@@ -248,6 +266,9 @@ impl State {
.show(&ctx, |ui| {
ui.label(egui::RichText::new(format!("{fps:.1} FPS {native_pct:.1}%")).monospace());
});
+
+ #[cfg(feature = "debug")]
+ debug_state.show(&ctx, emulator);
});
self.egui_winit
@@ -297,6 +318,10 @@ impl State {
}
frame.present();
+
+ if used_gx {
+ emulator.gx.draw_commands.commands.clear();
+ }
}
fn render_gx(&mut self, emulator: &mut GameCube, view: &wgpu::TextureView) {
@@ -309,7 +334,6 @@ impl State {
self.surface_config.width,
self.surface_config.height,
);
- emulator.gx.draw_commands.commands.clear();
}
fn render_xfb(&mut self, emulator: &GameCube, view: &wgpu::TextureView) {
@@ -430,6 +454,8 @@ struct App {
emulator: GameCube,
window: Option<Arc<Window>>,
state: SharedState,
+ #[cfg(feature = "debug")]
+ debug_state: debug_ui::DebugState,
}
impl ApplicationHandler for App {
@@ -481,7 +507,12 @@ impl ApplicationHandler for App {
WindowEvent::RedrawRequested => {
if let Some(window) = self.window.clone() {
if let Some(state) = self.state.borrow_mut().as_mut() {
- state.render(&mut self.emulator, &window);
+ state.render(
+ &mut self.emulator,
+ #[cfg(feature = "debug")]
+ &mut self.debug_state,
+ &window,
+ );
}
window.request_redraw();
}
@@ -517,6 +548,8 @@ pub fn start_emulator(rom_data: &[u8], filename: String, dsp_irom: Option<Vec<u8
emulator,
window: None,
state: Rc::new(RefCell::new(None)),
+ #[cfg(feature = "debug")]
+ debug_state: debug_ui::DebugState::default(),
};
event_loop.spawn_app(app);