Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

dsp bootstrap passes

Layle90bdc49f2f39
diff --git a/crates/gekko/src/exi/regs.rs b/crates/gekko/src/exi/regs.rs
index b91bec0..75ba027 100644
--- a/crates/gekko/src/exi/regs.rs
+++ b/crates/gekko/src/exi/regs.rs
@@ -108,9 +108,7 @@ impl MmioAccess<Exi> for Channel0Control {
// 0xCC006810 4 R/W EXI0DATA - EXI Channel 0 Immediate Data
crate::mmio_register! {
- Channel0Data: u32 @ 0xCC006810 => Exi.ch0_data {
- #[bits(0..=31, alias = "data")] pub value: u32,
- }
+ Channel0Data: u32 @ 0xCC006810 => Exi.ch0_data {}
}
// --- Channel 1 ---
@@ -183,9 +181,7 @@ impl MmioAccess<Exi> for Channel1Control {
// 0xCC006824 4 R/W EXI1DATA - EXI Channel 1 Immediate Data
crate::mmio_register! {
- Channel1Data: u32 @ 0xCC006824 => Exi.ch1_data {
- #[bits(0..=31, alias = "data")] pub value: u32,
- }
+ Channel1Data: u32 @ 0xCC006824 => Exi.ch1_data {}
}
// --- Channel 2 ---
@@ -258,7 +254,5 @@ impl MmioAccess<Exi> for Channel2Control {
// 0xCC006838 4 R/W EXI2DATA - EXI Channel 2 Immediate Data
crate::mmio_register! {
- Channel2Data: u32 @ 0xCC006838 => Exi.ch2_data {
- #[bits(0..=31, alias = "data")] pub value: u32,
- }
+ Channel2Data: u32 @ 0xCC006838 => Exi.ch2_data {}
}
diff --git a/crates/gekko/src/flipper/dsp.rs b/crates/gekko/src/flipper/dsp.rs
index 56f1484..0ab1d86 100644
--- a/crates/gekko/src/flipper/dsp.rs
+++ b/crates/gekko/src/flipper/dsp.rs
@@ -1,22 +1,130 @@
pub mod regs;
+use crate::mmio::Mmio;
use crate::mmio::constants::DSP_BASE;
use crate::mmio::traits::{MmioAccess, MmioRegister};
pub struct Dsp {
+ // IMEM = IRAM + IROM
+ pub iram: Box<[u8; 0x1000]>, // 0x0000 - 0x0FFF
+ pub irom: Box<[u8; 0x1000]>, // 0x8000 - 0x8FFF
+
+ // DMEM = DRAM + COEF + IFX
+ pub dram: Box<[u8; 0x1000]>, // 0x0000 - 0x0FFF
+ pub coef: Box<[u8; 0x1000]>, // 0x1000 - 0x1FFF
+ pub ifx: Box<[u8; 0x100]>, // 0xFF00 - 0xFFFF
+
+ // Auxiliary RAM (16 MB)
+ pub aram: Box<[u8; 16 * 1024 * 1024]>,
+
+ // I/O Registers
pub csr: regs::ControlStatus,
+ pub mailbox_to_cpu_hi: regs::MailboxToCpuHi,
+ pub mailbox_to_cpu_lo: regs::MailboxToCpuLo,
+ pub dma_mmio_addr: regs::DmaMmioAddr,
+ pub dma_aram_addr: regs::DmaAramAddr,
+ pub dma_control: regs::DmaControl,
+
+ // Flags set by register write handlers, consumed by process_pending_dma
+ pub pending_aram_dma: bool,
+ pub pending_ucode_upload: bool,
}
impl Dsp {
pub fn new() -> Self {
+ let aram = unsafe { Box::<[u8; 16 * 1024 * 1024]>::new_zeroed().assume_init() };
+ let iram = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
+ let irom = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
+ let dram = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
+ let coef = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
+ let ifx = unsafe { Box::<[u8; 0x100]>::new_zeroed().assume_init() };
+
Dsp {
- csr: regs::ControlStatus::from_raw(0),
+ iram,
+ irom,
+ dram,
+ coef,
+ ifx,
+ aram,
+ csr: regs::ControlStatus::default(),
+ mailbox_to_cpu_hi: regs::MailboxToCpuHi::from_raw(0),
+ mailbox_to_cpu_lo: regs::MailboxToCpuLo::from_raw(0),
+ dma_mmio_addr: regs::DmaMmioAddr::from_raw(0),
+ dma_aram_addr: regs::DmaAramAddr::from_raw(0),
+ dma_control: regs::DmaControl::from_raw(0),
+ pending_aram_dma: false,
+ pending_ucode_upload: false,
}
}
- crate::impl_mmio_dispatch!(regs::ControlStatus,);
+ /// Called by the bus after every DSP MMIO write
+ ///
+ /// - If an ARAM DMA was triggered (write to DmaControl), execute the transfer and
+ /// assert ARINT in the CSR
+ /// - If ucode upload (falling-edge) was detected (CSR bit 11: 1->0), copy from
+ /// ARAM[0x0000] into IRAM, assert ucode status (CSR bit 10, auto-clears on next CSR
+ /// read), and write the mailbox values (TODO: HLE)
+ #[inline]
+ pub fn process_pending_dma(&mut self, mmio: &mut Mmio) {
+ // Handle ARAM DMA
+ if self.pending_aram_dma {
+ self.pending_aram_dma = false;
+
+ let mmio_addr = self.dma_mmio_addr.raw() as usize;
+ let aram_addr = self.dma_aram_addr.raw() as usize;
+ let count = self.dma_control.count() as usize * 4;
+
+ if self.dma_control.direction() == regs::DmaDirection::AramToRam {
+ // ARAM -> main RAM
+ let src = self.aram[aram_addr..aram_addr + count].to_vec();
+ mmio.ram[mmio_addr..mmio_addr + count].copy_from_slice(&src);
+ } else {
+ // main RAM -> ARAM
+ let src = mmio.ram[mmio_addr..mmio_addr + count].to_vec();
+ self.aram[aram_addr..aram_addr + count].copy_from_slice(&src);
+ }
+
+ tracing::debug!(
+ mmio_addr = format!("{mmio_addr:08X}"),
+ aram_addr = format!("{aram_addr:08X}"),
+ count,
+ direction = ?self.dma_control.direction(),
+ "ARAM DMA complete"
+ );
+
+ // TODO: Cause actual interrupt?
+ self.csr = self.csr.with_ar_interrupt(true);
+ }
- pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ // Handle DSP ucode upload
+ if self.pending_ucode_upload {
+ self.pending_ucode_upload = false;
+
+ let count = self.dma_control.count() as usize * 4;
+ let src = self.aram[..count].to_vec();
+ self.iram[..count].copy_from_slice(&src);
+
+ tracing::debug!(count, "uploaded ucode from ARAM[0x0000] to IRAM");
+
+ // Assert ucode upload finished until next read
+ self.csr = self.csr.with_upload_ucode_finished(true);
+
+ // HLE: Write expected response to mailbox
+ self.mailbox_to_cpu_hi = regs::MailboxToCpuHi::from_raw(0x8071);
+ self.mailbox_to_cpu_lo = regs::MailboxToCpuLo::from_raw(0xFEED);
+ }
+ }
+
+ crate::impl_mmio_dispatch!(
+ regs::ControlStatus,
+ regs::MailboxToCpuHi,
+ regs::MailboxToCpuLo,
+ regs::DmaMmioAddr,
+ regs::DmaAramAddr,
+ regs::DmaControl,
+ );
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
self.read_raw(DSP_BASE + offset, 1).unwrap_or_else(|| {
tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u8");
0
@@ -29,11 +137,17 @@ impl Dsp {
}
}
- pub fn mmio_read_u16(&self, offset: u32) -> u16 {
- self.read_raw(DSP_BASE + offset, 2).unwrap_or_else(|| {
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ let addr = DSP_BASE + offset;
+ let val = self.read_raw(addr, 2).unwrap_or_else(|| {
tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u16");
0
- }) as u16
+ }) as u16;
+ // DSPInitCode (bit 10) auto-clears after being read
+ if regs::ControlStatus::contains(addr) {
+ self.csr = self.csr.with_upload_ucode_finished(false);
+ }
+ val
}
pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
@@ -42,7 +156,7 @@ impl Dsp {
}
}
- pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
self.read_raw(DSP_BASE + offset, 4).unwrap_or_else(|| {
tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u32");
0
diff --git a/crates/gekko/src/flipper/dsp/regs.rs b/crates/gekko/src/flipper/dsp/regs.rs
index 7f0fb64..dec59aa 100644
--- a/crates/gekko/src/flipper/dsp/regs.rs
+++ b/crates/gekko/src/flipper/dsp/regs.rs
@@ -1,9 +1,151 @@
-use super::Dsp;
+use chapa::BitEnum;
-// 0xCC00500A 2 R/W CSR - DSP Control/Status Register
+use crate::{flipper::dsp::Dsp, mmio::traits::MmioAccess};
+
+// 0xCC00500A 2 [R/W] CSR - DSP Control/Status Register
+
+crate::mmio_register! {
+ ControlStatus: u16 @ 0xCC00500A {
+ #[bits(0, alias = "res")]
+ pub reset: bool,
+
+ #[bits(1, alias = "piint")]
+ pub pi_interrupt: bool,
+
+ #[bits(2)]
+ pub halt: bool,
+
+ #[bits(3, alias = "aiint")]
+ pub ai_interrupt: bool,
+
+ #[bits(4, alias = "aiintmask")]
+ pub ai_interrupt_mask: bool,
+
+ #[bits(5, alias = "arint")]
+ pub ar_interrupt: bool,
+
+ #[bits(6, alias = "arintmask")]
+ pub ar_interrupt_mask: bool,
+
+ #[bits(7, alias = "dspint")]
+ pub dsp_interrupt: bool,
+
+ #[bits(8, alias = "dspintmask")]
+ pub dsp_interrupt_mask: bool,
+
+ #[bits(9, alias = "dspdma")]
+ pub dma_status: bool,
+
+ #[bits(10, alias = "ucode_status")]
+ pub upload_ucode_finished: bool,
+
+ #[bits(11, alias = "ucode")]
+ pub upload_ucode: bool,
+ }
+}
+
+impl MmioAccess<super::Dsp> for ControlStatus {
+ fn read(dsp: &super::Dsp) -> Self {
+ dsp.csr
+ }
+
+ fn write(self, dsp: &mut super::Dsp) {
+ tracing::trace!("CSR write: {:016b}", self.raw());
+
+ let mut csr = dsp.csr;
+
+ if self.ai_interrupt() {
+ csr = csr.with_ai_interrupt(false);
+ }
+ if self.ar_interrupt() {
+ csr = csr.with_ar_interrupt(false);
+ }
+ if self.dsp_interrupt() {
+ csr = csr.with_dsp_interrupt(false);
+ }
+
+ csr = csr
+ .with_pi_interrupt(self.pi_interrupt())
+ .with_halt(self.halt())
+ .with_ai_interrupt_mask(self.ai_interrupt_mask())
+ .with_ar_interrupt_mask(self.ar_interrupt_mask())
+ .with_dsp_interrupt_mask(self.dsp_interrupt_mask())
+ .with_dma_status(self.dma_status())
+ .with_upload_ucode(self.upload_ucode());
+
+ // ucode falling edge (bit 11: 1->0) schedules ucode upload from ARAM[0x0000]
+ if dsp.csr.upload_ucode() && !self.upload_ucode() {
+ tracing::debug!("scheduling ucode upload from ARAM @ 0x0000");
+ dsp.pending_ucode_upload = true;
+ }
+
+ // ACK reset bit
+ if self.reset() {
+ tracing::debug!("reset");
+ }
+ csr = csr.with_reset(false);
+
+ dsp.csr = csr;
+ }
+}
+
+impl Default for ControlStatus {
+ fn default() -> Self {
+ ControlStatus::new().with_halt(true).with_upload_ucode(true)
+ }
+}
+
+// 0xCC005004 2 [R] DSP-to-CPU Mailbox High
+
+crate::mmio_register! {
+ MailboxToCpuHi: u16 @ 0xCC005004 => Dsp.mailbox_to_cpu_hi {}
+}
+
+// 0xCC005006 2 [R] DSP-to-CPU Mailbox Low
+
+crate::mmio_register! {
+ MailboxToCpuLo: u16 @ 0xCC005006 => Dsp.mailbox_to_cpu_lo {}
+}
+
+// 0xCC005020 4 [R/W] ARAM DMA MMIO Address
+
+crate::mmio_register! {
+ DmaMmioAddr: u32 @ 0xCC005020 => Dsp.dma_mmio_addr {}
+}
+
+// 0xCC005024 4 [R/W] ARAM DMA ARAM Address
+
+crate::mmio_register! {
+ DmaAramAddr: u32 @ 0xCC005024 => Dsp.dma_aram_addr {}
+}
+
+// 0xCC005028 4 [R/W] ARAM DMA Count/Control
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum DmaDirection {
+ RamToAram = 0,
+ AramToRam = 1,
+}
crate::mmio_register! {
- ControlStatus: u16 @ 0xCC00500A => Dsp.csr {
- #[bits(0..=15, alias = "csr")] pub value: u16,
+ DmaControl: u32 @ 0xCC005028 {
+ #[bits(0..=30)]
+ pub count: u32,
+
+ #[bits(31)]
+ pub direction: DmaDirection,
+ }
+}
+
+impl MmioAccess<super::Dsp> for DmaControl {
+ fn read(dsp: &super::Dsp) -> Self {
+ dsp.dma_control
+ }
+
+ fn write(self, dsp: &mut super::Dsp) {
+ dsp.dma_control = self;
+
+ // Schedule the ARAM->RAM DMA immediately when this register is written
+ dsp.pending_aram_dma = true;
}
}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index c6f3b8b..6febf8c 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -64,7 +64,10 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_write_u8(offset, val),
- BusTarget::Dsp => self.dsp.mmio_write_u8(offset, val),
+ BusTarget::Dsp => {
+ self.dsp.mmio_write_u8(offset, val);
+ self.dsp.process_pending_dma(&mut self.mmio);
+ }
BusTarget::Exi => {
self.exi.mmio_write_u8(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
@@ -78,7 +81,10 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_write_u16(offset, val),
- BusTarget::Dsp => self.dsp.mmio_write_u16(offset, val),
+ BusTarget::Dsp => {
+ self.dsp.mmio_write_u16(offset, val);
+ self.dsp.process_pending_dma(&mut self.mmio);
+ }
BusTarget::Exi => {
self.exi.mmio_write_u16(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
@@ -93,7 +99,10 @@ impl Gekko {
match target {
BusTarget::Vi => self.vi.mmio_write_u32(offset, val),
- BusTarget::Dsp => self.dsp.mmio_write_u32(offset, val),
+ BusTarget::Dsp => {
+ self.dsp.mmio_write_u32(offset, val);
+ self.dsp.process_pending_dma(&mut self.mmio);
+ }
BusTarget::Exi => {
self.exi.mmio_write_u32(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
diff --git a/docs/GC_DSP.pdf b/docs/GC_DSP.pdf
new file mode 100644
index 0000000..dfae99f
Binary files /dev/null and b/docs/GC_DSP.pdf differ