A GameCube/Wii emulator and debugger written in Rust.
refactor dsp into new model
Layle9504b6dae9b6
diff --git a/crates/dbglib/src/lib.rs b/crates/dbglib/src/lib.rsindex e4d7285..33fc4e5 100644--- a/crates/dbglib/src/lib.rs+++ b/crates/dbglib/src/lib.rs@@ -127,7 +127,7 @@ impl Debugger {break;}}- emulator.check_dsp_interrupts();+ gecko::flipper::dsp::refresh_interrupts(emulator);emulator.scheduler.schedule_in(CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE, dsp_batch_handler);@@ -152,7 +152,7 @@ impl Debugger {break;}}- emulator.check_dsp_interrupts();+ gecko::flipper::dsp::refresh_interrupts(emulator);emulator.scheduler.schedule_in(CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE, dsp_batch_handler);diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rsindex 8ede5f8..903fa90 100644--- a/crates/gecko/src/flipper/dsp.rs+++ b/crates/gecko/src/flipper/dsp.rs@@ -14,8 +14,6 @@ pub mod lut {use crate::flipper::dsp::instruction::Instruction;use crate::gamecube::GameCube;use crate::mmio::Mmio;-use crate::mmio::constants::DSP_BASE;-use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};pub struct Dsp {// Registers@@ -54,12 +52,6 @@ pub struct Dsp {pub dma_dsp_addr: u16,pub dma_ram_addr_hi: u16,pub dma_ram_addr_lo: u16,-- // Flags set by register write handlers, consumed by process_pending_dma- pub pending_aram_dma: bool,- pub pending_dsp_dma: bool,- pub pending_audio_dma: bool,- pub pending_ucode_upload: bool,}impl Dsp {@@ -97,25 +89,6 @@ impl Dsp {dma_dsp_addr: 0,dma_ram_addr_hi: 0,dma_ram_addr_lo: 0,- pending_audio_dma: false,- pending_aram_dma: false,- pending_dsp_dma: false,- pending_ucode_upload: false,- }- }-- #[inline]- pub fn process_pending_dma(&mut self, mmio: &mut Mmio) {- // ARAM DMA is handled via scheduler-- if self.pending_audio_dma {- self.pending_audio_dma = false;- self.process_audio_dma(mmio);- }-- if self.pending_ucode_upload {- self.pending_ucode_upload = false;- self.process_ucode_upload(mmio);}}@@ -151,7 +124,7 @@ impl Dsp {self.csr = self.csr.with_ar_interrupt(true);}- fn process_audio_dma(&mut self, _mmio: &mut Mmio) {+ pub fn process_audio_dma(&mut self, _mmio: &mut Mmio) {if !self.audio_dma_control.play() {return;}@@ -167,7 +140,7 @@ impl Dsp {self.csr.set_ai_interrupt(true);}- fn process_ucode_upload(&mut self, mmio: &mut Mmio) {+ pub fn process_ucode_upload(&mut self, mmio: &mut Mmio) {const UCODE_ADDR: usize = 0x8100_0000;const UCODE_SIZE: usize = 1024;let src = mmio.virt_slice(UCODE_ADDR as u32, UCODE_SIZE);@@ -183,7 +156,7 @@ impl Dsp {self.csr.set_dsp_interrupt(true);}- fn process_dsp_dma(&mut self, mmio: &mut Mmio) {+ pub fn process_dsp_dma(&mut self, mmio: &mut Mmio) {let ram_addr = ((self.dma_ram_addr_hi as u32) << 16) | self.dma_ram_addr_lo as u32;let dsp_addr = (self.dma_dsp_addr as usize) * 2; // word address -> byte offsetlet len = self.dma_length as usize;@@ -273,11 +246,6 @@ impl GameCube {}}- if self.dsp.pending_dsp_dma {- self.dsp.pending_dsp_dma = false;- self.dsp.process_dsp_dma(&mut self.mmio);- }-self.dsp.registers.pc = self.dsp.registers.nia;true}@@ -290,15 +258,14 @@ impl GameCube {break;}}- self.check_dsp_interrupts();+ refresh_interrupts(self);}}-impl MmioRw for Dsp {- const BASE: u32 = DSP_BASE;- const NAME: &'static str = "DSP";-- crate::impl_mmio_dispatch!(+crate::mmio_device_dispatch! {+ read = dsp_read,+ write = dsp_write,+ registers = [regs::ControlStatus,regs::MailboxToDspHi,regs::MailboxToDspLo,@@ -312,11 +279,12 @@ impl MmioRw for Dsp {regs::AramDmaControl,regs::AudioDmaStartAddr,regs::AudioDmaControl,- );+ ],}impl Dsp {/// Read a 16-bit word from instruction memory (IRAM 0x0000-0x0FFF, IROM 0x8000-0x8FFF).+ #[inline(always)]pub fn read_imem(&self, addr: u16) -> u16 {match addr {0x0000..0x1000 => read_word(&*self.iram, addr),@@ -325,95 +293,6 @@ impl Dsp {}}- /// Read a 16-bit word from data memory (DRAM 0x0000-0x0FFF, COEF 0x1000-0x1FFF, IFX 0xFF00-0xFFFF).- pub fn read_dmem(&mut self, addr: u16) -> u16 {- match addr {- 0x0000..0x1000 => read_word(&*self.dram, addr),- 0x1000..0x2000 => read_word(&*self.coef, addr - 0x1000),- 0xFF00..=0xFFFF => self.read_ifx(addr),- _ => 0,- }- }-- /// Read a 16-bit word from IFX register space, with mailbox side-effects.- pub fn read_ifx(&mut self, addr: u16) -> u16 {- match addr {- // CMBH (CPU Mailbox High): reading returns data + M bit.- // M is only cleared when CMBL is read.- addr::IFX_CMBH => self.mailbox_to_dsp_hi.raw(),- // CMBL (CPU Mailbox Low): reading clears CMBH.M (busy)- addr::IFX_CMBL => {- self.mailbox_to_dsp_hi.set_busy(false);- self.mailbox_to_dsp_lo.raw()- }- // DMBH (DSP Mailbox High): DSP reads back what it wrote- addr::IFX_DMBH => self.mailbox_to_cpu_hi.raw(),- // DMBL (DSP Mailbox Low): DSP reads back what it wrote (no side effects)- addr::IFX_DMBL => self.mailbox_to_cpu_lo.raw(),- // DSP DMA registers- addr::IFX_DSCR => self.dma_control.raw(),- addr::IFX_DSBL => self.dma_length,- addr::IFX_DSPA => self.dma_dsp_addr,- addr::IFX_DSMAH => self.dma_ram_addr_hi,- addr::IFX_DSMAL => self.dma_ram_addr_lo,- _ => {- tracing::warn!(addr = format!("{:04X}", addr), "Read from unknown DSP IFX register");- read_word(&*self.ifx, addr - 0xFF00)- }- }- }-- /// Write a 16-bit word to IFX register space, with mailbox side-effects.- pub fn write_ifx(&mut self, addr: u16, value: u16) {- match addr {- // DMBH (DSP Mailbox High): store data bits (14:0), busy is preserved- addr::IFX_DMBH => {- let busy = self.mailbox_to_cpu_hi.busy();- self.mailbox_to_cpu_hi = regs::MailboxToCpuHi::from_raw(value & 0x7FFF).with_busy(busy);- }- // DMBL (DSP Mailbox Low): writing sets DMBH.M- addr::IFX_DMBL => {- self.mailbox_to_cpu_lo = regs::MailboxToCpuLo::from_raw(value);- self.mailbox_to_cpu_hi.set_busy(true);- }- // DIRQ: DSP explicitly raises interrupt to CPU- addr::IFX_DIRQ => {- if value & 1 != 0 {- tracing::debug!("DSP DIRQ: requesting CPU interrupt");- self.csr.set_dsp_interrupt(true);- }- }- // CMBH/CMBL are read-only from DSP side- addr::IFX_CMBH | addr::IFX_CMBL => {}- // DSP DMA: writing DSBL triggers the transfer- addr::IFX_DSBL => {- self.dma_length = value;- self.pending_dsp_dma = true;- }- addr::IFX_DSCR => self.dma_control = core::regs::DspDmaControl::from_raw(value),- addr::IFX_DSPA => self.dma_dsp_addr = value,- addr::IFX_DSMAH => self.dma_ram_addr_hi = value,- addr::IFX_DSMAL => self.dma_ram_addr_lo = value,- _ => {- tracing::warn!(- addr = format!("{:04X}", addr),- value = format!("{:04X}", value),- "Write to unknown DSP IFX register"- );- write_word(&mut *self.ifx, addr - 0xFF00, value);- }- }- }-- /// Write a 16-bit word to data memory (DRAM 0x0000-0x0FFF, IFX 0xFF00-0xFFFF).- pub fn write_dmem(&mut self, addr: u16, value: u16) {- match addr {- 0x0000..0x1000 => write_word(&mut *self.dram, addr, value),- 0xFF00..=0xFFFF => self.write_ifx(addr, value),- _ => {}- }- }-/// Load a binary file into IROM.pub fn load_irom(&mut self, data: &[u8]) {let len = data.len().min(self.irom.len());@@ -421,6 +300,7 @@ impl Dsp {tracing::info!(size = len, "loaded DSP IROM");}+ #[inline(always)]pub fn interrupt_active(&self) -> bool {(self.csr.ai_interrupt() && self.csr.ai_interrupt_mask())|| (self.csr.ar_interrupt() && self.csr.ar_interrupt_mask())@@ -428,25 +308,102 @@ impl Dsp {}}-impl GameCube {- pub fn check_dsp_interrupts(&mut self) {- use crate::flipper::pi::InterruptFlag;+#[inline(always)]+pub fn refresh_interrupts(gc: &mut GameCube) {+ use crate::flipper::pi::InterruptFlag;++ if gc.dsp.interrupt_active() {+ gc.pi.assert_interrupt(InterruptFlag::Dsp);+ } else {+ gc.pi.clear_interrupt(InterruptFlag::Dsp);+ }+}- if self.dsp.interrupt_active() {- self.pi.assert_interrupt(InterruptFlag::Dsp);- } else {- self.pi.clear_interrupt(InterruptFlag::Dsp);+#[inline(always)]+pub fn read_dmem(gc: &mut GameCube, addr: u16) -> u16 {+ match addr {+ 0x0000..0x1000 => read_word(&*gc.dsp.dram, addr),+ 0x1000..0x2000 => read_word(&*gc.dsp.coef, addr - 0x1000),+ 0xFF00..=0xFFFF => read_ifx(gc, addr),+ _ => 0,+ }+}++#[inline(always)]+pub fn write_dmem(gc: &mut GameCube, addr: u16, value: u16) {+ match addr {+ 0x0000..0x1000 => write_word(&mut *gc.dsp.dram, addr, value),+ 0xFF00..=0xFFFF => write_ifx(gc, addr, value),+ _ => {}+ }+}++#[inline(always)]+fn read_ifx(gc: &mut GameCube, addr: u16) -> u16 {+ match addr {+ // CMBH (CPU Mailbox High): reading returns data + M bit.+ // M is only cleared when CMBL is read.+ addr::IFX_CMBH => gc.dsp.mailbox_to_dsp_hi.raw(),+ // CMBL (CPU Mailbox Low): reading clears CMBH.M (busy)+ addr::IFX_CMBL => {+ gc.dsp.mailbox_to_dsp_hi.set_busy(false);+ gc.dsp.mailbox_to_dsp_lo.raw()+ }+ // DMBH (DSP Mailbox High): DSP reads back what it wrote+ addr::IFX_DMBH => gc.dsp.mailbox_to_cpu_hi.raw(),+ // DMBL (DSP Mailbox Low): DSP reads back what it wrote (no side effects)+ addr::IFX_DMBL => gc.dsp.mailbox_to_cpu_lo.raw(),+ // DSP DMA registers+ addr::IFX_DSCR => gc.dsp.dma_control.raw(),+ addr::IFX_DSBL => gc.dsp.dma_length,+ addr::IFX_DSPA => gc.dsp.dma_dsp_addr,+ addr::IFX_DSMAH => gc.dsp.dma_ram_addr_hi,+ addr::IFX_DSMAL => gc.dsp.dma_ram_addr_lo,+ _ => {+ tracing::warn!(addr = format!("{:04X}", addr), "Read from unknown DSP IFX register");+ read_word(&*gc.dsp.ifx, addr - 0xFF00)}}+}- pub fn maybe_schedule_aram_dma(&mut self) {- if self.dsp.pending_aram_dma {- self.dsp.pending_aram_dma = false;- const ARAM_DMA_DELAY: u64 = 10_000;- self.scheduler.schedule_in(ARAM_DMA_DELAY, |gc| {- gc.dsp.process_aram_dma(&mut gc.mmio);- gc.check_dsp_interrupts();- });+#[inline(always)]+fn write_ifx(gc: &mut GameCube, addr: u16, value: u16) {+ match addr {+ // DMBH (DSP Mailbox High): store data bits (14:0), busy is preserved+ addr::IFX_DMBH => {+ let busy = gc.dsp.mailbox_to_cpu_hi.busy();+ gc.dsp.mailbox_to_cpu_hi = regs::MailboxToCpuHi::from_raw(value & 0x7FFF).with_busy(busy);+ }+ // DMBL (DSP Mailbox Low): writing sets DMBH.M+ addr::IFX_DMBL => {+ gc.dsp.mailbox_to_cpu_lo = regs::MailboxToCpuLo::from_raw(value);+ gc.dsp.mailbox_to_cpu_hi.set_busy(true);+ }+ // DIRQ: DSP explicitly raises interrupt to CPU+ addr::IFX_DIRQ => {+ if value & 1 != 0 {+ tracing::debug!("DSP DIRQ: requesting CPU interrupt");+ gc.dsp.csr.set_dsp_interrupt(true);+ }+ }+ // CMBH/CMBL are read-only from DSP side+ addr::IFX_CMBH | addr::IFX_CMBL => {}+ // DSP DMA: writing DSBL triggers the transfer, run inline.+ addr::IFX_DSBL => {+ gc.dsp.dma_length = value;+ gc.dsp.process_dsp_dma(&mut gc.mmio);+ }+ addr::IFX_DSCR => gc.dsp.dma_control = core::regs::DspDmaControl::from_raw(value),+ addr::IFX_DSPA => gc.dsp.dma_dsp_addr = value,+ addr::IFX_DSMAH => gc.dsp.dma_ram_addr_hi = value,+ addr::IFX_DSMAL => gc.dsp.dma_ram_addr_lo = value,+ _ => {+ tracing::warn!(+ addr = format!("{:04X}", addr),+ value = format!("{:04X}", value),+ "Write to unknown DSP IFX register"+ );+ write_word(&mut *gc.dsp.ifx, addr - 0xFF00, value);}}}diff --git a/crates/gecko/src/flipper/dsp/interpreter.rs b/crates/gecko/src/flipper/dsp/interpreter.rsindex ff2be22..b580635 100644--- a/crates/gecko/src/flipper/dsp/interpreter.rs+++ b/crates/gecko/src/flipper/dsp/interpreter.rs@@ -1,10 +1,13 @@+use crate::flipper::dsp;use crate::flipper::dsp::condition::BranchControl;-use crate::flipper::dsp::core::reg;use crate::flipper::dsp::core::regs::{SignExtensionMode, StatusRegister};+use crate::flipper::dsp::core::{Registers, reg};+use crate::flipper::dsp::instruction::Instruction;use crate::flipper::dsp::lut::*;+use crate::gamecube::GameCube;#[inline(always)]-fn multiply(regs: &mut crate::flipper::dsp::core::Registers, a: i16, b: i16) {+fn multiply(regs: &mut Registers, a: i16, b: i16) {let mut result = a as i32 as i64 * b as i32 as i64;if !regs.status.am() {result <<= 1;@@ -14,7 +17,7 @@ fn multiply(regs: &mut crate::flipper::dsp::core::Registers, a: i16, b: i16) {/// Compute a * b (with AM shift), then add/sub to current product.#[inline(always)]-fn multiply_accumulate<const ADD: bool>(regs: &mut crate::flipper::dsp::core::Registers, a: i16, b: i16) {+fn multiply_accumulate<const ADD: bool>(regs: &mut Registers, a: i16, b: i16) {let mut mul_result = a as i32 as i64 * b as i32 as i64;if !regs.status.am() {mul_result <<= 1;@@ -30,7 +33,7 @@ fn multiply_accumulate<const ADD: bool>(regs: &mut crate::flipper::dsp::core::Re/// Apply product carry/overflow to status: set O and OS from product flags.#[inline(always)]-fn apply_product_oc(regs: &mut crate::flipper::dsp::core::Registers, carry: bool, overflow: bool) {+fn apply_product_oc(regs: &mut Registers, carry: bool, overflow: bool) {regs.status.set_o(overflow);if overflow {regs.status.set_os(true);@@ -56,7 +59,7 @@ fn round_half_to_even(val: i64) -> (i64, bool) {/// Apply combined arithmetic + product carry/overflow for ADD+product operations./// Preserves OS from before the arithmetic flag update, then sets based on the XORed values.#[inline(always)]-fn apply_combined_add_product_oc(regs: &mut crate::flipper::dsp::core::Registers, pc: bool, po: bool, os_before: bool) {+fn apply_combined_add_product_oc(regs: &mut Registers, pc: bool, po: bool, os_before: bool) {regs.status.set_os(os_before);let c = regs.status.c();let o = regs.status.o();@@ -68,7 +71,7 @@ fn apply_combined_add_product_oc(regs: &mut crate::flipper::dsp::core::Registers}#[inline(always)]-fn move_prod_to_ac(ctx: &mut crate::gamecube::GameCube, r: u8) {+fn move_prod_to_ac(ctx: &mut GameCube, r: u8) {let prod = ctx.dsp.registers.product();let (carry, overflow) = ctx.dsp.registers.product_flags();ctx.dsp.registers.set_ac(r, prod);@@ -77,7 +80,7 @@ fn move_prod_to_ac(ctx: &mut crate::gamecube::GameCube, r: u8) {}#[inline(always)]-fn move_prod_to_ac_zero(ctx: &mut crate::gamecube::GameCube, r: u8) {+fn move_prod_to_ac_zero(ctx: &mut GameCube, r: u8) {let (carry, overflow) = ctx.dsp.registers.product_flags();let raw = ctx.dsp.registers.product();let (prod, rounding_carry) = round_half_to_even(raw);@@ -87,7 +90,7 @@ fn move_prod_to_ac_zero(ctx: &mut crate::gamecube::GameCube, r: u8) {}#[inline(always)]-fn add_prod_to_ac(ctx: &mut crate::gamecube::GameCube, r: u8) {+fn add_prod_to_ac(ctx: &mut GameCube, r: u8) {let a = ctx.dsp.registers.ac(r);let (pc, po) = ctx.dsp.registers.product_flags();let b = ctx.dsp.registers.product();@@ -102,11 +105,7 @@ fn add_prod_to_ac(ctx: &mut crate::gamecube::GameCube, r: u8) {/// REVERSED controls direction: when false (ASRN/LSRN), bit6=LEFT/!bit6=RIGHT./// When true (ASRNR/NRX variants), bit6=RIGHT/!bit6=LEFT.#[inline(always)]-fn dynamic_shift<const LOGICAL: bool, const REVERSED: bool>(- regs: &mut crate::flipper::dsp::core::Registers,- d: u8,- shift_val: i16,-) {+fn dynamic_shift<const LOGICAL: bool, const REVERSED: bool>(regs: &mut Registers, d: u8, shift_val: i16) {let low6 = (shift_val & 63) as u32;let bit6 = shift_val & 64 != 0;let amount = if bit6 { (64 - low6) % 64 } else { low6 };@@ -130,7 +129,7 @@ fn dynamic_shift<const LOGICAL: bool, const REVERSED: bool>(/// Get the ax0/ax1 operands for MULX instructions.#[inline(always)]-fn mulx_operands(regs: &crate::flipper::dsp::core::Registers, s: u8, t: u8) -> (u16, u16) {+fn mulx_operands(regs: &Registers, s: u8, t: u8) -> (u16, u16) {let a = if s != 0 { regs.axh[0] } else { regs.ax[0] };let b = if t != 0 { regs.axh[1] } else { regs.ax[1] };(a, b)@@ -138,7 +137,7 @@ fn mulx_operands(regs: &crate::flipper::dsp::core::Registers, s: u8, t: u8) -> (/// Multiply for MULX family: handles unsigned/mixed modes based on SU flag and s/t operands.#[inline(always)]-fn multiply_mulx(regs: &mut crate::flipper::dsp::core::Registers, s: u8, t: u8) {+fn multiply_mulx(regs: &mut Registers, s: u8, t: u8) {let (a_raw, b_raw) = mulx_operands(regs, s, t);let unsigned = regs.status.su();let (a, b) = if !unsigned {@@ -161,17 +160,14 @@ fn multiply_mulx(regs: &mut crate::flipper::dsp::core::Registers, s: u8, t: u8)/// Get the acS.m / axT.h operands for MULC instructions.#[inline(always)]-fn mulc_operands(regs: &crate::flipper::dsp::core::Registers, s: u8, t: u8) -> (i16, i16) {+fn mulc_operands(regs: &Registers, s: u8, t: u8) -> (i16, i16) {let a = regs.ac_mid(s) as i16;let b = regs.axh[t as usize] as i16;(a, b)}#[inline(always)]-pub fn add_sub<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn add_sub<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_ADDR => {let ss = instr.ss() as usize;@@ -282,10 +278,7 @@ pub fn add_sub<const OP: u32>(}#[inline(always)]-pub fn addr_reg<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn addr_reg<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_DAR => {let d = instr.d_14_15() as usize;@@ -311,10 +304,7 @@ pub fn addr_reg<const OP: u32>(}#[inline(always)]-pub fn cmp_test<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn cmp_test<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_CMP => {let a = ctx.dsp.registers.ac(0);@@ -386,10 +376,7 @@ pub fn cmp_test<const OP: u32>(}#[inline(always)]-pub fn control<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn control<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_JCC => {let branch_control = BranchControl::from(instr.cond());@@ -445,10 +432,7 @@ pub fn control<const OP: u32>(}#[inline(always)]-pub fn imm_alu<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn imm_alu<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_ADDI => {let d = instr.d_7_7();@@ -527,10 +511,7 @@ pub fn imm_alu<const OP: u32>(}#[inline(always)]-pub fn inc_dec<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn inc_dec<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_INCM => {let d = instr.d_7_7();@@ -588,21 +569,18 @@ pub fn inc_dec<const OP: u32>(}#[inline(always)]-pub fn load_store<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn load_store<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_LRI => {ctx.dsp.registers.write::<true>(instr.d_11_15(), instr.imm_16_31());}OP_LR => {- let value = ctx.dsp.read_dmem(instr.imm_16_31());+ let value = dsp::read_dmem(ctx, instr.imm_16_31());ctx.dsp.registers.write::<true>(instr.d_11_15(), value);}OP_SR => {let value = ctx.dsp.registers.read::<true>(instr.d_11_15());- ctx.dsp.write_dmem(instr.imm_16_31(), value);+ dsp::write_dmem(ctx, instr.imm_16_31(), value);}OP_MRR => {let value = ctx.dsp.registers.read::<true>(instr.src());@@ -610,12 +588,13 @@ pub fn load_store<const OP: u32>(}OP_SI => {let addr = 0xFF00 | (instr.mem_8_15_u16());- ctx.dsp.write_dmem(addr, instr.imm_16_31());+ dsp::write_dmem(ctx, addr, instr.imm_16_31());}OP_LRR | OP_LRRD | OP_LRRI | OP_LRRN => {let s = instr.s_9_10() as usize;let d = instr.d_11_15();- let value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);+ let addr = ctx.dsp.registers.ar[s];+ let value = dsp::read_dmem(ctx, addr);// Compute new AR before the write (write may modify WR/IX used by AR increment)let new_ar = match OP {OP_LRRD => Some(ctx.dsp.registers.decrement_ar(s)),@@ -647,7 +626,8 @@ pub fn load_store<const OP: u32>(}_ => None,};- ctx.dsp.write_dmem(ctx.dsp.registers.ar[d], value);+ let addr = ctx.dsp.registers.ar[d];+ dsp::write_dmem(ctx, addr, value);if let Some(ar) = new_ar {ctx.dsp.registers.ar[d] = ar;}@@ -655,7 +635,7 @@ pub fn load_store<const OP: u32>(OP_LRS => {let dst = reg::AX0L + instr.reg_5_7();let addr = (ctx.dsp.registers.config << 8) | instr.mem_8_15_u16();- let value = ctx.dsp.read_dmem(addr);+ let value = dsp::read_dmem(ctx, addr);ctx.dsp.registers.write::<true>(dst, value);}OP_SRSH | OP_SRS => {@@ -672,7 +652,7 @@ pub fn load_store<const OP: u32>(_ => unreachable!(),};let value = ctx.dsp.registers.read::<true>(src);- ctx.dsp.write_dmem(addr, value);+ dsp::write_dmem(ctx, addr, value);}OP_ILRR | OP_ILRRD | OP_ILRRI | OP_ILRRN => {let src = instr.s_14_15() as usize;@@ -694,7 +674,7 @@ pub fn load_store<const OP: u32>(}#[inline(always)]-pub fn logic<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::flipper::dsp::instruction::Instruction) {+pub fn logic<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_XORR => {let s = instr.s_6_6() as usize;@@ -767,7 +747,7 @@ pub fn logic<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::f}#[inline(always)]-pub fn loops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::flipper::dsp::instruction::Instruction) {+pub fn loops<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_LOOP_REG | OP_LOOPI => {let counter = match OP {@@ -804,10 +784,7 @@ pub fn loops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::f}#[inline(always)]-pub fn move_ops<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn move_ops<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_MOVR => {let ss = instr.ss();@@ -862,7 +839,7 @@ pub fn move_ops<const OP: u32>(}#[inline(always)]-pub fn mul<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::flipper::dsp::instruction::Instruction) {+pub fn mul<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_MUL => {let s = instr.r_4_4() as usize;@@ -959,10 +936,7 @@ pub fn mul<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::fli}#[inline(always)]-pub fn shifts<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn shifts<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_LSL | OP_ASL => {let r = instr.r_7_7();@@ -1060,10 +1034,7 @@ pub fn shifts<const OP: u32>(}#[inline(always)]-pub fn status<const OP: u32>(- ctx: &mut crate::gamecube::GameCube,- instr: crate::flipper::dsp::instruction::Instruction,-) {+pub fn status<const OP: u32>(ctx: &mut GameCube, instr: Instruction) {match OP {OP_SBCLR => {let idx = 6 + instr.bit();@@ -1100,13 +1071,13 @@ pub fn status<const OP: u32>(}// Extension opcode handlers-use crate::flipper::dsp::instruction::GcDspExt;+use dsp::instruction::GcDspExt;#[inline(always)]-pub fn ext_nop(_ctx: &mut crate::gamecube::GameCube, _instr: GcDspExt) {}+pub fn ext_nop(_ctx: &mut GameCube, _instr: GcDspExt) {}#[inline(always)]-pub fn ext_addr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_addr<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let r = instr.r_6_7() as usize;match OP {OP_EXT_DR => ctx.dsp.registers.ar[r] = ctx.dsp.registers.decrement_ar(r),@@ -1120,7 +1091,7 @@ pub fn ext_addr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDsp}#[inline(always)]-pub fn ext_mv(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_mv(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_4_5();let s = instr.s_6_7();let value = ctx.dsp.registers.read::<true>(reg::AC0L + s);@@ -1128,11 +1099,12 @@ pub fn ext_mv(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {}#[inline(always)]-pub fn ext_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_store<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_6_7() as usize;let s = instr.s_3_4();let value = ctx.dsp.registers.read::<true>(reg::AC0L + s);- ctx.dsp.write_dmem(ctx.dsp.registers.ar[d], value);+ let addr = ctx.dsp.registers.ar[d];+ dsp::write_dmem(ctx, addr, value);match OP {OP_EXT_S => {ctx.dsp.registers.ar[d] = ctx.dsp.registers.increment_ar(d);@@ -1146,10 +1118,11 @@ pub fn ext_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDs}#[inline(always)]-pub fn ext_load<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_load<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_2_4();let s = instr.s_6_7() as usize;- let value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);+ let addr = ctx.dsp.registers.ar[s];+ let value = dsp::read_dmem(ctx, addr);ctx.dsp.registers.write::<true>(reg::AX0L + d, value);match OP {OP_EXT_L => {@@ -1164,7 +1137,7 @@ pub fn ext_load<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDsp}#[inline(always)]-pub fn ext_load_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_load_store<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let s = instr.s_7_7() as usize;let d = instr.d_2_3();@@ -1172,16 +1145,20 @@ pub fn ext_load_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr:// SL variants: Store ac[s].m to ar[0], Load from ar[3] to AXmatch OP {OP_EXT_LS | OP_EXT_LSM | OP_EXT_LSN | OP_EXT_LSNM => {- let load_value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[0]);+ let load_addr = ctx.dsp.registers.ar[0];+ let load_value = dsp::read_dmem(ctx, load_addr);ctx.dsp.registers.write::<true>(reg::AX0L + d, load_value);let store_value = ctx.dsp.registers.ac_mid(s as u8);- ctx.dsp.write_dmem(ctx.dsp.registers.ar[3], store_value);+ let store_addr = ctx.dsp.registers.ar[3];+ dsp::write_dmem(ctx, store_addr, store_value);}_ => {// SL: Store first, then Loadlet store_value = ctx.dsp.registers.ac_mid(s as u8);- ctx.dsp.write_dmem(ctx.dsp.registers.ar[0], store_value);- let load_value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[3]);+ let store_addr = ctx.dsp.registers.ar[0];+ dsp::write_dmem(ctx, store_addr, store_value);+ let load_addr = ctx.dsp.registers.ar[3];+ let load_value = dsp::read_dmem(ctx, load_addr);ctx.dsp.registers.write::<true>(reg::AX0L + d, load_value);}}@@ -1215,19 +1192,21 @@ pub fn ext_load_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr:}#[inline(always)]-pub fn ext_ld<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_ld<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let d = instr.d_2_2();let r = instr.r_3_3();let s = instr.s_6_7() as usize;// First load from ar[s] -> AX0 half (d selects low/high)let d_reg = if d != 0 { reg::AX0H } else { reg::AX0L };- let value0 = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);+ let addr0 = ctx.dsp.registers.ar[s];+ let value0 = dsp::read_dmem(ctx, addr0);ctx.dsp.registers.write::<true>(d_reg, value0);// Second load from ar[3] -> AX1 half (r selects low/high)let r_reg = if r != 0 { reg::AX1H } else { reg::AX1L };- let value1 = ctx.dsp.read_dmem(ctx.dsp.registers.ar[3]);+ let addr1 = ctx.dsp.registers.ar[3];+ let value1 = dsp::read_dmem(ctx, addr1);ctx.dsp.registers.write::<true>(r_reg, value1);// AR increments: "" = +1/+1, "N" = +ix[s]/+1, "M" = +1/+ix[3], "NM" = +ix[s]/+ix[3]@@ -1257,14 +1236,16 @@ pub fn ext_ld<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspEx}#[inline(always)]-pub fn ext_ldax<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {+pub fn ext_ldax<const OP: u32>(ctx: &mut GameCube, instr: GcDspExt) {let s = instr.d_2_2() as usize;let r = instr.r_3_3() as usize;// Load high from ar[s]- let high = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);+ let addr_s = ctx.dsp.registers.ar[s];+ let high = dsp::read_dmem(ctx, addr_s);// Load low from ar[3]- let low = ctx.dsp.read_dmem(ctx.dsp.registers.ar[3]);+ let addr_3 = ctx.dsp.registers.ar[3];+ let low = dsp::read_dmem(ctx, addr_3);// Write to ax[r] (high and low)ctx.dsp.registers.axh[r] = high;diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rsindex 96205af..e446067 100644--- a/crates/gecko/src/flipper/dsp/regs.rs+++ b/crates/gecko/src/flipper/dsp/regs.rs@@ -1,8 +1,8 @@+use crate::flipper::dsp;+use crate::gamecube::GameCube;+use crate::mmio::traits::{MmioHandler, WriteMask};use chapa::BitEnum;-use crate::flipper::dsp::Dsp;-use crate::mmio::traits::{MmioAccess, MmioRegister};-// 0xCC00500A 2 [R/W] CSR - DSP Control/Status Register#[derive(BitEnum, Debug, PartialEq, Eq)]@@ -20,52 +20,59 @@ impl ResetVector {}}-crate::mmio_register! {- ControlStatus: u16 @ 0xCC00500A {- #[bits(0, alias = "res")]- pub reset: bool,+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct ControlStatus {+ #[bits(0, alias = "res")]+ pub reset: bool,++ #[bits(1, alias = "piint")]+ pub pi_interrupt: bool,- #[bits(1, alias = "piint")]- pub pi_interrupt: bool,+ #[bits(2)]+ pub halt: bool,- #[bits(2)]- pub halt: bool,+ #[bits(3, alias = "aiint")]+ pub ai_interrupt: bool,- #[bits(3, alias = "aiint")]- pub ai_interrupt: bool,+ #[bits(4, alias = "aiintmask")]+ pub ai_interrupt_mask: bool,- #[bits(4, alias = "aiintmask")]- pub ai_interrupt_mask: bool,+ #[bits(5, alias = "arint")]+ pub ar_interrupt: bool,- #[bits(5, alias = "arint")]- pub ar_interrupt: bool,+ #[bits(6, alias = "arintmask")]+ pub ar_interrupt_mask: bool,- #[bits(6, alias = "arintmask")]- pub ar_interrupt_mask: bool,+ #[bits(7, alias = "dspint")]+ pub dsp_interrupt: bool,- #[bits(7, alias = "dspint")]- pub dsp_interrupt: bool,+ #[bits(8, alias = "dspintmask")]+ pub dsp_interrupt_mask: bool,- #[bits(8, alias = "dspintmask")]- pub dsp_interrupt_mask: bool,+ #[bits(9, alias = "dspdma")]+ pub dma_status: bool,- #[bits(9, alias = "dspdma")]- pub dma_status: bool,+ #[bits(11)]+ pub reset_vector: ResetVector,+}+crate::mmio_reg!(ControlStatus: u16 @ 0xCC00500A);- #[bits(11)]- pub reset_vector: ResetVector,+impl Default for ControlStatus {+ fn default() -> Self {+ ControlStatus::new().with_halt(true)}}-impl MmioAccess<super::Dsp> for ControlStatus {- fn read(dsp: &super::Dsp) -> Self {- dsp.csr+impl MmioHandler<GameCube> for ControlStatus {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.csr}- fn write(self, dsp: &mut super::Dsp) {- tracing::trace!("CSR write: {:016b} (prev: {:016b})", self.raw(), dsp.csr.raw());+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ tracing::trace!("CSR write: {:016b} (prev: {:016b})", self.raw(), gc.dsp.csr.raw());- let mut csr = dsp.csr;+ let mut csr = gc.dsp.csr;if self.ai_interrupt() {csr = csr.with_ai_interrupt(false);@@ -88,70 +95,73 @@ impl MmioAccess<super::Dsp> for ControlStatus {// reset vector falling edge (bit 11: 1->0) triggers DMA of stub from main RAM into IRAM// and resets the PC to 0x0000 (IRAM) so the uploaded stub executes- if dsp.csr.reset_vector() == ResetVector::High && self.reset_vector() == ResetVector::Low {- tracing::info!("scheduling ucode upload, PC -> 0x0000 (IRAM)");- dsp.pending_ucode_upload = true;- dsp.registers.pc = 0x0000;+ let trigger_ucode_upload =+ gc.dsp.csr.reset_vector() == ResetVector::High && self.reset_vector() == ResetVector::Low;+ if trigger_ucode_upload {+ tracing::info!("ucode upload, PC -> 0x0000 (IRAM)");+ gc.dsp.registers.pc = 0x0000;}// On reset, set PC to the address indicated by the reset vectorif self.reset() {let addr = self.reset_vector().address();tracing::debug!(reset_vector = ?self.reset_vector(), pc = format!("{addr:04X}"), "DSP reset, executing from reset vector");- dsp.registers.pc = addr;+ gc.dsp.registers.pc = addr;}csr = csr.with_reset(false);- dsp.csr = csr;- }-}+ gc.dsp.csr = csr;-impl Default for ControlStatus {- fn default() -> Self {- ControlStatus::new().with_halt(true)+ // Side effects after CSR is committed+ if trigger_ucode_upload {+ gc.dsp.process_ucode_upload(&mut gc.mmio);+ }+ dsp::refresh_interrupts(gc);}}// 0xCC005000 2 [W] CPU-to-DSP Mailbox High-crate::mmio_register! {- MailboxToDspHi: u16 @ 0xCC005000 {- #[bits(0..=14)]- pub data: u16,+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct MailboxToDspHi {+ #[bits(0..=14)]+ pub data: u16,- #[bits(15)]- pub busy: bool,- }+ #[bits(15)]+ pub busy: bool,}+crate::mmio_reg!(MailboxToDspHi: u16 @ 0xCC005000);-impl MmioAccess<Dsp> for MailboxToDspHi {- fn read(dsp: &Dsp) -> Self {- dsp.mailbox_to_dsp_hi+impl MmioHandler<GameCube> for MailboxToDspHi {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.mailbox_to_dsp_hi}- fn write(self, dsp: &mut Dsp) {+ fn write(self, gc: &mut GameCube, _: WriteMask) {// CPU writing CMBH sets data bits (14:0), busy is preserved- dsp.mailbox_to_dsp_hi = self.with_busy(dsp.mailbox_to_dsp_hi.busy());+ gc.dsp.mailbox_to_dsp_hi = self.with_busy(gc.dsp.mailbox_to_dsp_hi.busy());}}// 0xCC005002 2 [W] CPU-to-DSP Mailbox Low// Writing sets CMBH.M (busy), signaling to the DSP that mail is ready.-crate::mmio_register! {- MailboxToDspLo: u16 @ 0xCC005002 {}-}+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct MailboxToDspLo {}+crate::mmio_reg!(MailboxToDspLo: u16 @ 0xCC005002);-impl MmioAccess<Dsp> for MailboxToDspLo {- fn read(dsp: &Dsp) -> Self {- dsp.mailbox_to_dsp_lo+impl MmioHandler<GameCube> for MailboxToDspLo {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.mailbox_to_dsp_lo}- fn write(self, dsp: &mut Dsp) {- dsp.mailbox_to_dsp_lo = self;- dsp.mailbox_to_dsp_hi = dsp.mailbox_to_dsp_hi.with_busy(true);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.dsp.mailbox_to_dsp_lo = self;+ gc.dsp.mailbox_to_dsp_hi = gc.dsp.mailbox_to_dsp_hi.with_busy(true);tracing::debug!(- hi = format!("{:04X}", dsp.mailbox_to_dsp_hi.raw()),+ hi = format!("{:04X}", gc.dsp.mailbox_to_dsp_hi.raw()),lo = format!("{:04X}", self.raw()),"CPU->DSP mailbox");@@ -160,26 +170,23 @@ impl MmioAccess<Dsp> for MailboxToDspLo {// 0xCC005004 2 [R] DSP-to-CPU Mailbox High (DMBH)-crate::mmio_register! {- MailboxToCpuHi: u16 @ 0xCC005004 {- #[bits(0..=14)]- pub data: u16,+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct MailboxToCpuHi {+ #[bits(0..=14)]+ pub data: u16,- #[bits(15)]- pub busy: bool,- }+ #[bits(15)]+ pub busy: bool,}+crate::mmio_reg!(MailboxToCpuHi: u16 @ 0xCC005004);-impl MmioAccess<Dsp> for MailboxToCpuHi {- fn read(dsp: &Dsp) -> Self {- dsp.mailbox_to_cpu_hi+impl MmioHandler<GameCube> for MailboxToCpuHi {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.mailbox_to_cpu_hi}- fn read_at(dsp: &mut Dsp, addr: u32, access_size: u32) -> u32 {- Self::read_sub(Self::read(dsp).raw() as u32, addr, access_size)- }-- fn write(self, _dsp: &mut Dsp) {+ fn write(self, _gc: &mut GameCube, _: WriteMask) {// Read-only from CPU side}}@@ -187,96 +194,106 @@ impl MmioAccess<Dsp> for MailboxToCpuHi {// 0xCC005006 2 [R] DSP-to-CPU Mailbox Low (DMBL)// Reading clears DMBH.M (bit 15), indicating the CPU has consumed the mail.-crate::mmio_register! {- MailboxToCpuLo: u16 @ 0xCC005006 {}-}+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct MailboxToCpuLo {}+crate::mmio_reg!(MailboxToCpuLo: u16 @ 0xCC005006);-impl MmioAccess<Dsp> for MailboxToCpuLo {- fn read(dsp: &Dsp) -> Self {- dsp.mailbox_to_cpu_lo- }-- fn read_at(dsp: &mut Dsp, addr: u32, access_size: u32) -> u32 {- let val = Self::read_sub(Self::read(dsp).raw() as u32, addr, access_size);+impl MmioHandler<GameCube> for MailboxToCpuLo {+ fn read(gc: &mut GameCube) -> Self {+ let val = gc.dsp.mailbox_to_cpu_lo;// Reading DMBL clears DMBH.M (bit 15), signaling the CPU has consumed the mail- dsp.mailbox_to_cpu_hi.set_busy(false);+ gc.dsp.mailbox_to_cpu_hi.set_busy(false);val}- fn write(self, _dsp: &mut Dsp) {+ fn write(self, _gc: &mut GameCube, _: WriteMask) {// Read-only from CPU side}}// 0xCC005012 2 [R/W] AR_INFO - ARAM Info/Size-crate::mmio_register! {- AramInfo: u16 @ 0xCC005012 => Dsp.aram_info {}-}+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramInfo {}+crate::mmio_reg!(AramInfo: u16 @ 0xCC005012);+crate::mmio_default_access!(AramInfo => GameCube.dsp.aram_info);// 0xCC005016 2 [R/W] AR_MODE - ARAM Mode-crate::mmio_register! {- AramMode: u16 @ 0xCC005016 {- #[bits(0)]- pub status: bool,- }+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramMode {+ #[bits(0)]+ pub status: bool,}+crate::mmio_reg!(AramMode: u16 @ 0xCC005016);-impl MmioAccess<super::Dsp> for AramMode {- fn read(dsp: &super::Dsp) -> Self {- dsp.aram_mode.with_status(true)+impl MmioHandler<GameCube> for AramMode {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.aram_mode.with_status(true)}- fn write(self, dsp: &mut super::Dsp) {- dsp.aram_mode = self;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.dsp.aram_mode = self;}}// 0xCC00501A 2 [R/W] AR_REFRESH - ARAM Refresh-crate::mmio_register! {- AramRefresh: u16 @ 0xCC00501A => Dsp.aram_refresh {}-}+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramRefresh {}+crate::mmio_reg!(AramRefresh: u16 @ 0xCC00501A);+crate::mmio_default_access!(AramRefresh => GameCube.dsp.aram_refresh);// 0xCC005020 4 [R/W] ARAM DMA MMIO Address-crate::mmio_register! {- AramDmaMmioAddr: u32 @ 0xCC005020 => Dsp.aram_dma_mmio_addr {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramDmaMmioAddr {}+crate::mmio_reg!(AramDmaMmioAddr: u32 @ 0xCC005020);+crate::mmio_default_access!(AramDmaMmioAddr => GameCube.dsp.aram_dma_mmio_addr);// 0xCC005024 4 [R/W] ARAM DMA ARAM Address-crate::mmio_register! {- AramDmaAramAddr: u32 @ 0xCC005024 => Dsp.aram_dma_aram_addr {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramDmaAramAddr {}+crate::mmio_reg!(AramDmaAramAddr: u32 @ 0xCC005024);+crate::mmio_default_access!(AramDmaAramAddr => GameCube.dsp.aram_dma_aram_addr);// 0xCC005030 4 [W] Audio DMA Start Address (High + Low)-crate::mmio_register! {- AudioDmaStartAddr: u32 @ 0xCC005030 => Dsp.audio_dma_start_addr {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AudioDmaStartAddr {}+crate::mmio_reg!(AudioDmaStartAddr: u32 @ 0xCC005030);+crate::mmio_default_access!(AudioDmaStartAddr => GameCube.dsp.audio_dma_start_addr);// 0xCC005036 2 [W] Audio DMA Control/Length-crate::mmio_register! {- AudioDmaControl: u16 @ 0xCC005036 {- #[bits(0..=14)]- pub length: u16,+#[chapa::bitfield(u16, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AudioDmaControl {+ #[bits(0..=14)]+ pub length: u16,- #[bits(15)]- pub play: bool,- }+ #[bits(15)]+ pub play: bool,}+crate::mmio_reg!(AudioDmaControl: u16 @ 0xCC005036);-impl MmioAccess<super::Dsp> for AudioDmaControl {- fn read(dsp: &super::Dsp) -> Self {- dsp.audio_dma_control+impl MmioHandler<GameCube> for AudioDmaControl {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.audio_dma_control}- fn write(self, dsp: &mut super::Dsp) {- dsp.audio_dma_control = self;- dsp.pending_audio_dma = true;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.dsp.audio_dma_control = self;+ gc.dsp.process_audio_dma(&mut gc.mmio);+ dsp::refresh_interrupts(gc);}}@@ -288,23 +305,29 @@ pub enum DmaDirection {AramToRam = 1,}-crate::mmio_register! {- AramDmaControl: u32 @ 0xCC005028 {- #[bits(0..=30)]- pub count: u32,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AramDmaControl {+ #[bits(0..=30)]+ pub count: u32,- #[bits(31)]- pub direction: DmaDirection,- }+ #[bits(31)]+ pub direction: DmaDirection,}+crate::mmio_reg!(AramDmaControl: u32 @ 0xCC005028);-impl MmioAccess<super::Dsp> for AramDmaControl {- fn read(dsp: &super::Dsp) -> Self {- dsp.aram_dma_control+impl MmioHandler<GameCube> for AramDmaControl {+ fn read(gc: &mut GameCube) -> Self {+ gc.dsp.aram_dma_control}- fn write(self, dsp: &mut super::Dsp) {- dsp.aram_dma_control = self;- dsp.pending_aram_dma = true;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.dsp.aram_dma_control = self;++ const ARAM_DMA_DELAY: u64 = 10_000;+ gc.scheduler.schedule_in(ARAM_DMA_DELAY, |gc| {+ gc.dsp.process_aram_dma(&mut gc.mmio);+ dsp::refresh_interrupts(gc);+ });}}diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rsindex fea9003..decdef2 100644--- a/crates/gecko/src/mmio/bus.rs+++ b/crates/gecko/src/mmio/bus.rs@@ -251,7 +251,7 @@ impl GameCube {BusTarget::Pe => self.pe.mmio_read_u8(offset),BusTarget::Pi => self.pi.mmio_read_u8(offset),BusTarget::Mi => self.mi.mmio_read_u8(offset),- BusTarget::Dsp => self.dsp.mmio_read_u8(offset),+ BusTarget::Dsp => crate::flipper::dsp::dsp_read(self, phys, 1).unwrap_or(0) as u8,BusTarget::Di => self.di.mmio_read_u8(offset),BusTarget::Si => self.si.mmio_read_u8(offset),BusTarget::Exi => self.exi.mmio_read_u8(offset),@@ -285,7 +285,7 @@ impl GameCube {BusTarget::Pe => self.pe.mmio_read_u16(offset),BusTarget::Pi => self.pi.mmio_read_u16(offset),BusTarget::Mi => self.mi.mmio_read_u16(offset),- BusTarget::Dsp => self.dsp.mmio_read_u16(offset),+ BusTarget::Dsp => crate::flipper::dsp::dsp_read(self, phys, 2).unwrap_or(0) as u16,BusTarget::Di => self.di.mmio_read_u16(offset),BusTarget::Si => self.si.mmio_read_u16(offset),BusTarget::Exi => self.exi.mmio_read_u16(offset),@@ -321,7 +321,7 @@ impl GameCube {BusTarget::Pe => self.pe.mmio_read_u32(offset),BusTarget::Pi => self.pi.mmio_read_u32(offset),BusTarget::Mi => self.mi.mmio_read_u32(offset),- BusTarget::Dsp => self.dsp.mmio_read_u32(offset),+ BusTarget::Dsp => crate::flipper::dsp::dsp_read(self, phys, 4).unwrap_or(0),BusTarget::Di => self.di.mmio_read_u32(offset),BusTarget::Si => self.si.mmio_read_u32(offset),BusTarget::Exi => self.exi.mmio_read_u32(offset),@@ -362,12 +362,7 @@ impl GameCube {}BusTarget::Pi => self.pi.mmio_write_u8(offset, val),BusTarget::Mi => self.mi.mmio_write_u8(offset, val),- BusTarget::Dsp => {- self.dsp.mmio_write_u8(offset, val);- self.dsp.process_pending_dma(&mut self.mmio);- self.maybe_schedule_aram_dma();- self.check_dsp_interrupts();- }+ BusTarget::Dsp => { crate::flipper::dsp::dsp_write(self, phys, 1, val as u32); }BusTarget::Di => {self.di.mmio_write_u8(offset, val);self.start_dvd_transfer();@@ -424,12 +419,7 @@ impl GameCube {}BusTarget::Pi => self.pi.mmio_write_u16(offset, val),BusTarget::Mi => self.mi.mmio_write_u16(offset, val),- BusTarget::Dsp => {- self.dsp.mmio_write_u16(offset, val);- self.dsp.process_pending_dma(&mut self.mmio);- self.maybe_schedule_aram_dma();- self.check_dsp_interrupts();- }+ BusTarget::Dsp => { crate::flipper::dsp::dsp_write(self, phys, 2, val as u32); }BusTarget::Di => {self.di.mmio_write_u16(offset, val);self.start_dvd_transfer();@@ -487,12 +477,7 @@ impl GameCube {}BusTarget::Pi => self.pi.mmio_write_u32(offset, val),BusTarget::Mi => self.mi.mmio_write_u32(offset, val),- BusTarget::Dsp => {- self.dsp.mmio_write_u32(offset, val);- self.dsp.process_pending_dma(&mut self.mmio);- self.maybe_schedule_aram_dma();- self.check_dsp_interrupts();- }+ BusTarget::Dsp => { crate::flipper::dsp::dsp_write(self, phys, 4, val); }BusTarget::Di => {self.di.mmio_write_u32(offset, val);self.start_dvd_transfer();diff --git a/crates/gecko/src/mmio/macros.rs b/crates/gecko/src/mmio/macros.rsindex e6fd3ee..f298814 100644--- a/crates/gecko/src/mmio/macros.rs+++ b/crates/gecko/src/mmio/macros.rs@@ -1,3 +1,91 @@+#[macro_export]+macro_rules! mmio_device_dispatch {+ (+ read = $read_fn:ident,+ write = $write_fn:ident,+ registers = [ $($reg:ty),* $(,)? ] $(,)?+ ) => {+ #[inline]+ pub fn $read_fn(+ gc: &mut $crate::gamecube::GameCube,+ addr: u32,+ size: u32,+ ) -> Option<u32> {+ $(+ if <$reg as $crate::mmio::traits::MmioRegister>::fits(addr, size) {+ return Some(+ <$reg as $crate::mmio::traits::MmioHandler<$crate::gamecube::GameCube>>+ ::read_at(gc, addr, size),+ );+ }+ )*+ None+ }++ #[inline]+ pub fn $write_fn(+ gc: &mut $crate::gamecube::GameCube,+ addr: u32,+ size: u32,+ val: u32,+ ) -> bool {+ $(+ if <$reg as $crate::mmio::traits::MmioRegister>::fits(addr, size) {+ tracing::debug!(+ reg = stringify!($reg),+ addr = format!("{addr:08X}"),+ val = format!("{val:08X}"),+ size,+ "MMIO write"+ );+ <$reg as $crate::mmio::traits::MmioHandler<$crate::gamecube::GameCube>>+ ::write_at(gc, addr, size, val);+ return true;+ }+ )*+ false+ }+ };+}++#[macro_export]+macro_rules! mmio_reg {+ ($name:ident : $raw:tt @ $addr:tt) => {+ impl $crate::mmio::traits::MmioRegister for $name {+ const ADDR: u32 = $crate::mmio::Mmio::virt_to_phys($addr);+ const SIZE: usize = ::core::mem::size_of::<$raw>();+ #[inline(always)]+ fn from_raw(raw: u32) -> Self {+ (raw as $raw).into()+ }+ #[inline(always)]+ fn to_raw(self) -> u32 {+ self.raw() as u32+ }+ }+ };+}++#[macro_export]+macro_rules! mmio_default_access {+ ($reg:ty => GameCube . $($field:ident).+) => {+ impl $crate::mmio::traits::MmioHandler<$crate::gamecube::GameCube> for $reg {+ #[inline(always)]+ fn read(gc: &mut $crate::gamecube::GameCube) -> Self {+ gc . $($field).++ }+ #[inline(always)]+ fn write(+ self,+ gc: &mut $crate::gamecube::GameCube,+ _: $crate::mmio::traits::WriteMask,+ ) {+ gc . $($field).+ = self;+ }+ }+ };+}+/// Generate `read_raw` and `write_raw` dispatch methods for a list of MMIO register types/// Inserts two methods into the enclosing `impl` block that iterate over the provided/// register types and delegate reads/writes to the appropriate `MmioAccess` impldiff --git a/crates/gecko/src/mmio/traits.rs b/crates/gecko/src/mmio/traits.rsindex 52713a2..43a7e45 100644--- a/crates/gecko/src/mmio/traits.rs+++ b/crates/gecko/src/mmio/traits.rs@@ -120,3 +120,54 @@ pub trait MmioAccess<C>: MmioRegister {Self::from_raw(merged).write(component);}}++#[derive(Copy, Clone, Debug, PartialEq, Eq)]+pub struct WriteMask(u8);++impl WriteMask {+ pub const FULL_U8: Self = Self(0b0001);+ pub const FULL_U16: Self = Self(0b0011);+ pub const FULL_U32: Self = Self(0b1111);++ /// Build a mask from the byte offset within the register and the access size in bytes.+ #[inline(always)]+ pub const fn from_offset_size(sub_offset: u32, access_size: u32) -> Self {+ Self((((1u32 << access_size) - 1) << sub_offset) as u8)+ }++ /// Was the byte at `idx` (offset from the register base) written?+ #[inline(always)]+ pub const fn byte(self, idx: u32) -> bool {+ (self.0 >> idx) & 1 != 0+ }++ /// Did this write touch any byte in `[start, end]`?+ #[inline(always)]+ pub const fn any(self, start: u32, end: u32) -> bool {+ let m = (((1u32 << (end - start)) - 1) << start) as u8;+ self.0 & m != 0+ }++ #[inline(always)]+ pub const fn raw(self) -> u8 {+ self.0+ }+}++pub trait MmioHandler<C>: MmioRegister {+ fn read(c: &mut C) -> Self;+ fn write(self, c: &mut C, mask: WriteMask);++ #[inline(always)]+ fn read_at(c: &mut C, addr: u32, access_size: u32) -> u32 {+ Self::read_sub(Self::read(c).to_raw(), addr, access_size)+ }++ #[inline(always)]+ fn write_at(c: &mut C, addr: u32, access_size: u32, val: u32) {+ let current = Self::read(c).to_raw();+ let merged = Self::write_sub(current, addr, access_size, val);+ let mask = WriteMask::from_offset_size(addr - Self::ADDR, access_size);+ Self::from_raw(merged).write(c, mask);+ }+}