Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

debugger improv for dsp

Laylee1921bbe0c1f
diff --git a/crates/dbglib/src/lib.rs b/crates/dbglib/src/lib.rs
index bf112a3..470b215 100644
--- a/crates/dbglib/src/lib.rs
+++ b/crates/dbglib/src/lib.rs
@@ -11,6 +11,7 @@ pub enum EmulatorState {
Running,
Paused,
Step,
+ StepDsp,
RunUntilVsync,
RunUntilAddress(u32),
}
@@ -93,6 +94,10 @@ impl Debugger {
emulator.step();
self.state = EmulatorState::Paused;
}
+ EmulatorState::StepDsp => {
+ emulator.tick_dsp();
+ self.state = EmulatorState::Paused;
+ }
EmulatorState::RunUntilVsync => {
if self.is_tracing() {
emulator.prepare_frame();
diff --git a/crates/dbglib/src/windows/controls.rs b/crates/dbglib/src/windows/controls.rs
index 647097a..b1a93c1 100644
--- a/crates/dbglib/src/windows/controls.rs
+++ b/crates/dbglib/src/windows/controls.rs
@@ -48,13 +48,23 @@ pub fn show_controls(
if ui
.add_enabled(
is_paused,
- egui::Button::new(format!("{} Step", icons::SKIP_FORWARD)).min_size(btn_size),
+ egui::Button::new(format!("{} Step CPU", icons::SKIP_FORWARD)).min_size(btn_size),
)
.clicked()
{
*state = EmulatorState::Step;
}
+ if ui
+ .add_enabled(
+ is_paused,
+ egui::Button::new(format!("{} Step DSP", icons::SKIP_FORWARD)).min_size(btn_size),
+ )
+ .clicked()
+ {
+ *state = EmulatorState::StepDsp;
+ }
+
if ui
.add(egui::Button::new(format!("{} Run Until VSync", icons::FAST_FORWARD)).min_size(btn_size))
.clicked()
diff --git a/crates/dbglib/src/windows/cpu.rs b/crates/dbglib/src/windows/cpu.rs
index f817f20..4e7fc3f 100644
--- a/crates/dbglib/src/windows/cpu.rs
+++ b/crates/dbglib/src/windows/cpu.rs
@@ -1,4 +1,4 @@
-use egui::{Align, Color32, Context, Grid, RichText, ScrollArea};
+use egui::{Color32, Context, Grid, RichText, ScrollArea};
use gecko::cpu::Cpu;
use gecko::mmio::Mmio;
use image::symbols::SymbolTable;
@@ -103,17 +103,14 @@ pub fn show_cpu(ctx: &Context, open: &mut bool, cpu: &Cpu, mmio: &Mmio, symbols:
let is_pc = addr == pc;
- let indicator_resp = if is_pc {
- Some(
- ui.label(
- RichText::new(egui_phosphor::regular::PLAY)
- .color(Color32::from_rgb(120, 220, 120)),
- ),
- )
+ if is_pc {
+ ui.label(
+ RichText::new(egui_phosphor::regular::PLAY)
+ .color(Color32::from_rgb(120, 220, 120)),
+ );
} else {
ui.label("");
- None
- };
+ }
ui.monospace(format!("{:#010X}", addr));
@@ -137,10 +134,6 @@ pub fn show_cpu(ctx: &Context, open: &mut bool, cpu: &Cpu, mmio: &Mmio, symbols:
ui.end_row();
- if let Some(resp) = indicator_resp {
- resp.scroll_to_me(Some(Align::Min));
- }
-
addr = addr.wrapping_add(4);
}
});
diff --git a/crates/dbglib/src/windows/dsp.rs b/crates/dbglib/src/windows/dsp.rs
index 55b445e..6402ec5 100644
--- a/crates/dbglib/src/windows/dsp.rs
+++ b/crates/dbglib/src/windows/dsp.rs
@@ -3,6 +3,22 @@ use gecko::flipper::dsp::Dsp;
use super::token_color;
+/// Return the IMEM byte slice starting at the given DSP word address,
+/// resolving IRAM (0x0000-0x0FFF) vs IROM (0x8000-0x8FFF).
+fn imem_slice(dsp: &Dsp, word_addr: u16) -> Option<&[u8]> {
+ match word_addr {
+ 0x0000..0x1000 => {
+ let off = (word_addr as usize) * 2;
+ dsp.iram.get(off..)
+ }
+ 0x8000..0x9000 => {
+ let off = (word_addr as usize - 0x8000) * 2;
+ dsp.irom.get(off..)
+ }
+ _ => None,
+ }
+}
+
pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
egui::Window::new("DSP").open(open).show(ctx, |ui| {
Grid::new("dsp_special_regs")
@@ -21,6 +37,15 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
ui.monospace(format!("{:#06X}", dsp.csr.raw()));
ui.end_row();
+ let mbox_to_dsp = ((dsp.mailbox_to_dsp_hi.raw() as u32) << 16) | dsp.mailbox_to_dsp_lo.raw() as u32;
+ let mbox_to_cpu = ((dsp.mailbox_to_cpu_hi.raw() as u32) << 16) | dsp.mailbox_to_cpu_lo.raw() as u32;
+
+ ui.label("CPU->DSP");
+ ui.monospace(format!("{:#010X}", mbox_to_dsp));
+ ui.label("DSP->CPU");
+ ui.monospace(format!("{:#010X}", mbox_to_cpu));
+ ui.end_row();
+
#[cfg(not(target_arch = "wasm32"))]
if ui.button("Dump DSP").clicked() {
let mut dump = Vec::new();
@@ -33,76 +58,77 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
ui.separator();
- ScrollArea::vertical().id_salt("dsp_disasm_scroll").show(ui, |ui| {
- Grid::new("dsp_disasm_grid")
- .num_columns(4)
- .min_col_width(0.0)
- .striped(true)
- .show(ui, |ui| {
- let mut addr = dsp.registers.pc;
- for _ in 0..20 {
- let off = (addr as usize) * 2; // word-addressed PC -> byte offset
- if off + 1 >= dsp.iram.len() {
- break;
- }
+ ScrollArea::vertical()
+ .id_salt("dsp_disasm_scroll")
+ .auto_shrink(false)
+ .show(ui, |ui| {
+ Grid::new("dsp_disasm_grid")
+ .num_columns(4)
+ .min_col_width(0.0)
+ .striped(true)
+ .show(ui, |ui| {
+ let mut addr = dsp.registers.pc;
+ for _ in 0..20 {
+ let mem = imem_slice(dsp, addr);
+ let Some(mem) = mem else { break };
+
+ let (text, words) = match disasm::dsp::GcDspInstruction::decode(mem) {
+ Some((insn, byte_len)) => (insn.to_string(), (byte_len / 2) as u16),
+ None => {
+ let raw = u16::from_be_bytes([mem[0], mem[1]]);
+ (format!(".word {:#06X}", raw), 1)
+ }
+ };
+
+ let is_pc = addr == dsp.registers.pc;
- let (text, words) = match disasm::dsp::GcDspInstruction::decode(&dsp.iram[off..]) {
- Some((insn, byte_len)) => (insn.to_string(), (byte_len / 2) as u16),
- None => {
- let raw = u16::from_be_bytes([dsp.iram[off], dsp.iram[off + 1]]);
- (format!(".word {:#06X}", raw), 1)
+ // PC indicator
+ if is_pc {
+ ui.label(
+ RichText::new(egui_phosphor::regular::PLAY).color(Color32::from_rgb(120, 220, 120)),
+ );
+ } else {
+ ui.label("");
}
- };
- let is_pc = addr == dsp.registers.pc;
+ // Address
+ ui.monospace(format!("{:#06X}", addr));
- // PC indicator
- if is_pc {
+ // Raw bytes
+ let mut raw_str = String::new();
+ for i in 0..words {
+ let w_off = (i as usize) * 2;
+ if w_off + 1 < mem.len() {
+ let w = u16::from_be_bytes([mem[w_off], mem[w_off + 1]]);
+ if !raw_str.is_empty() {
+ raw_str.push(' ');
+ }
+ raw_str.push_str(&format!("{:04X}", w));
+ }
+ }
ui.label(
- RichText::new(egui_phosphor::regular::PLAY).color(Color32::from_rgb(120, 220, 120)),
+ RichText::new(raw_str)
+ .monospace()
+ .color(Color32::from_rgb(100, 100, 100)),
);
- } else {
- ui.label("");
- }
- // Address
- ui.monospace(format!("{:#06X}", addr));
-
- // Raw bytes
- let mut raw_str = String::new();
- for i in 0..words {
- let w_off = off + (i as usize) * 2;
- if w_off + 1 < dsp.iram.len() {
- let w = u16::from_be_bytes([dsp.iram[w_off], dsp.iram[w_off + 1]]);
- if !raw_str.is_empty() {
- raw_str.push(' ');
+ // Disassembly
+ let tokens = disasm::tokenizer::tokenize(&text);
+ ui.horizontal(|ui| {
+ ui.spacing_mut().item_spacing.x = 0.0;
+ for token in &tokens {
+ let mut rt = RichText::new(token.to_string()).monospace();
+ if let Some(color) = token_color(token) {
+ rt = rt.color(color);
+ }
+ ui.label(rt);
}
- raw_str.push_str(&format!("{:04X}", w));
- }
- }
- ui.label(
- RichText::new(raw_str)
- .monospace()
- .color(Color32::from_rgb(100, 100, 100)),
- );
-
- // Disassembly
- let tokens = disasm::tokenizer::tokenize(&text);
- ui.horizontal(|ui| {
- ui.spacing_mut().item_spacing.x = 0.0;
- for token in &tokens {
- let mut rt = RichText::new(token.to_string()).monospace();
- if let Some(color) = token_color(token) {
- rt = rt.color(color);
- }
- ui.label(rt);
- }
- });
- ui.end_row();
+ });
+ ui.end_row();
- addr += words;
- }
- });
- });
+ addr += words;
+ }
+ });
+ });
});
}
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 6bbe418..2dcf252 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -232,12 +232,25 @@ impl RenderState {
ui.close();
}
if ui
- .add_enabled(is_paused, egui::Button::new(format!("{} Step", icons::SKIP_FORWARD)))
+ .add_enabled(
+ is_paused,
+ egui::Button::new(format!("{} Step CPU", icons::SKIP_FORWARD)),
+ )
.clicked()
{
debugger_ui.debugger.set_state(EmulatorState::Step);
ui.close();
}
+ if ui
+ .add_enabled(
+ is_paused,
+ egui::Button::new(format!("{} Step DSP", icons::SKIP_FORWARD)),
+ )
+ .clicked()
+ {
+ debugger_ui.debugger.set_state(EmulatorState::StepDsp);
+ ui.close();
+ }
if ui.button(format!("{} Run Until VSync", icons::FAST_FORWARD)).clicked() {
debugger_ui.debugger.set_state(EmulatorState::RunUntilVsync);
ui.close();
diff --git a/crates/disasm/src/tokenizer.rs b/crates/disasm/src/tokenizer.rs
index 92c5ae4..3576503 100644
--- a/crates/disasm/src/tokenizer.rs
+++ b/crates/disasm/src/tokenizer.rs
@@ -191,6 +191,16 @@ fn tokenize_operands<'a>(sc: &mut Scanner<'a>, tokens: &mut Vec<AsmToken<'a>>, m
continue;
}
+ // ── DSP register: $name or $name.sub ────────────────────────
+ if b == b'$' && sc.peek_at(1).is_some_and(|b| b.is_ascii_alphabetic()) {
+ let word = sc.span(|s| {
+ s.skip(1); // skip '$'
+ s.take_while(|b| b.is_ascii_alphanumeric() || *b == b'.' || *b == b'_');
+ });
+ tokens.push(AsmToken::Spr(word));
+ continue;
+ }
+
// ── Word token: identifier ───────────────────────────────────
if b.is_ascii_alphabetic() || b == b'_' {
let word = sc.take_while(|b| b.is_ascii_alphanumeric() || *b == b'_');
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index 201d386..a777eab 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -15,7 +15,7 @@ use crate::flipper::vi::VideoInterface;
#[cfg(feature = "idle-skip")]
use crate::idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector};
use crate::mmio::Mmio;
-use crate::scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler};
+use crate::scheduler::{CYCLES_PER_VSYNC, DSP_TICKS_PER_CPU_CYCLE, EventKind, Scheduler};
#[cfg(feature = "scripting")]
use crate::scripting::{HookFlags, ScriptHookFilters, ScriptHookState, ScriptHost};
use image::Executable;
@@ -174,6 +174,10 @@ impl GameCube {
EventKind::DiTransferComplete => {
self.complete_dvd_transfer();
}
+ EventKind::DspTick => {
+ self.tick_dsp();
+ self.scheduler.schedule_in(DSP_TICKS_PER_CPU_CYCLE, EventKind::DspTick);
+ }
}
}
@@ -251,12 +255,6 @@ impl GameCube {
while !self.vsync_pending {
self.step();
}
-
- // Run DSP in batches at 1:6 ratio
- let dsp_ticks = CYCLES_PER_VSYNC / 6;
- for _ in 0..dsp_ticks {
- self.tick_dsp();
- }
}
/// Read the instructions in `[start, end]` and check whether the loop is a
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 479715d..8400d12 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -3,12 +3,14 @@ use std::collections::BinaryHeap;
pub const CYCLES_PER_VSYNC: u64 = 486_000_000 / 60; // TODO: fix
pub const TIMEBASE_DIVISOR: u64 = 12;
+pub const DSP_TICKS_PER_CPU_CYCLE: u64 = 1024; // TODO: should probably be much less, batch it on vsync? need this for debugging atm
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum EventKind {
VSync,
ViHalfLine,
DiTransferComplete,
+ DspTick,
}
pub struct Scheduler {
@@ -25,6 +27,7 @@ impl Scheduler {
events: BinaryHeap::new(),
};
s.schedule_at(CYCLES_PER_VSYNC, EventKind::VSync);
+ s.schedule_at(DSP_TICKS_PER_CPU_CYCLE, EventKind::DspTick);
s
}