A GameCube/Wii emulator and debugger written in Rust.
first batch of devices
Layle9c5001ecf65a
diff --git a/crates/gecko/src/dvd.rs b/crates/gecko/src/dvd.rsindex 26d5070..ef83367 100644--- a/crates/gecko/src/dvd.rs+++ b/crates/gecko/src/dvd.rs@@ -1,15 +1,7 @@pub mod regs;-use image::Executable;-use crate::gamecube::GameCube;-use crate::mmio::constants::DI_BASE;-use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};--const DICMDBUF0: u32 = DI_BASE + 0x08;-const DICMDBUF1: u32 = DI_BASE + 0x0C;-const DICMDBUF2: u32 = DI_BASE + 0x10;-const DIIMMBUF: u32 = DI_BASE + 0x20;+use image::Executable;pub enum Command {DriveInfo,@@ -29,7 +21,6 @@ pub struct DvdInterface {pub cmdbuf1: u32,pub cmdbuf2: u32,pub immbuf: u32,- pub transfer_started: bool,pub dvd: Option<image::dvd::Dvd>,}@@ -46,7 +37,6 @@ impl DvdInterface {cmdbuf1: 0,cmdbuf2: 0,immbuf: 0,- transfer_started: false,dvd: None,}}@@ -59,7 +49,7 @@ impl DvdInterface {}#[inline(always)]- fn resolve_command(&mut self) -> Option<Command> {+ fn resolve_command(&self) -> Option<Command> {let cmd = self.cmdbuf0 >> 24;let sub1 = (self.cmdbuf0 >> 16) & 0xFF;let sub2 = self.cmdbuf0 & 0xFFFF;@@ -85,97 +75,115 @@ impl DvdInterface {}}-impl GameCube {- #[inline(always)]- fn process_dvd_command(&mut self, cmd: Command, dvd: &image::dvd::Dvd) {- match cmd {- Command::DriveInfo => {- let dst = self.di.dma_address.address();- let buffer = self.mmio.phys_slice_mut(dst, 0x20);- buffer.copy_from_slice(&[0x69; 0x20]); // TODO: Drive Info?- }- Command::ReadSectorData => {- let src = self.di.cmdbuf1 << 2;- let dst = self.di.dma_address.address();- let len = self.di.cmdbuf2 as usize;- assert!(len == self.di.dma_length.length() as usize, "DMA length mismatch");+crate::mmio_device_dispatch! {+ read = di_read,+ write = di_write,+ registers = [+ regs::DiStatusRegister,+ regs::DiCoverRegister,+ regs::DiCommandBuf0,+ regs::DiCommandBuf1,+ regs::DiCommandBuf2,+ regs::DiDmaAddressRegister,+ regs::DiDmaLengthRegister,+ regs::DiControlRegister,+ regs::DiImmBuf,+ regs::DiConfigurationRegister,+ ],+}- let buffer = self.mmio.phys_slice_mut(dst, len);- buffer.copy_from_slice(&dvd.data()[src as usize..src as usize + len]);+#[inline(always)]+pub fn refresh_interrupts(gc: &mut GameCube) {+ use crate::flipper::pi::InterruptFlag;- tracing::debug!(- src = format!("{:08X}", src),- dst = format!("{:08X}", dst),- len = format!("{:08X}", len),- "ReadSectorData command"- );- }- Command::ReadDiskId => {- let src = self.di.cmdbuf1;- let dst = self.di.dma_address.address();- let len = self.di.dma_length.length() as usize;+ if gc.di.interrupt_active() {+ gc.pi.assert_interrupt(InterruptFlag::Di);+ } else {+ gc.pi.clear_interrupt(InterruptFlag::Di);+ }+}- let buffer = self.mmio.phys_slice_mut(dst, len);- buffer.copy_from_slice(&dvd.data()[..len]);+pub fn start_transfer(gc: &mut GameCube) {+ let Some(dvd) = gc.di.dvd.take() else {+ // TODO: Setting this causes unrecoverable error when there is no DVD+ //gc.di.status.set_device_error(true);+ gc.di.control.set_tstart(false);+ return;+ };- tracing::debug!(- src = format!("{:08X}", src),- dst = format!("{:08X}", dst),- len = format!("{:08X}", len),- "ReadDiskId command"- );- }- Command::AudioToggle(enable) => {- tracing::warn!(enable, "AudioToggle stubbed");- self.di.immbuf = 0;- }- }+ if let Some(cmd) = gc.di.resolve_command() {+ self::process_dvd_command(gc, cmd, &dvd);}- #[inline(always)]- pub fn start_dvd_transfer(&mut self) {- if !self.di.transfer_started {- return;- }-- let Some(dvd) = self.di.dvd.take() else {- // TODO: Setting this causes unrecoverable error when there is no DVD- //self.di.status.set_device_error(true);- self.di.control.set_tstart(false);- self.di.transfer_started = false;- return;- };+ gc.di.dvd = Some(dvd);++ // The interrupt must not fire until some time!!+ // This is important as it will break the IPL if we immediately raise it...+ // When DVDReadDiskID issues the DI command, it returns to BS2_Tick which updates+ // the internal state machine and then calls into restore_irq. The code assumes+ // that the interrupt wont fire until after it has returned and updated the state.+ // Else it will get trapped inside restore_irq, right after mtmsr, executing in a loop+ // of the DVD dispatch handler which in turn re-issues the same command...+ const DI_TRANSFER_DELAY: u64 = 10_000; // Based off of vxpm and hazel+ gc.scheduler.schedule_in(DI_TRANSFER_DELAY, |gc| {+ gc.di.control.set_tstart(false);+ // DMA length tracks the progress of the transfer, so when it hits 0, the+ // transfer is complete. On failure, this would denote how many bytes were+ // not transferred, but we close our eyes and just hope nothing depends on+ // that!+ gc.di.dma_length = regs::DiDmaLengthRegister::from_raw(0);+ gc.di.status.set_transfer_complete(true);+ self::refresh_interrupts(gc);+ });+}- if let Some(cmd) = self.di.resolve_command() {- self.process_dvd_command(cmd, &dvd);+#[inline(always)]+fn process_dvd_command(gc: &mut GameCube, cmd: Command, dvd: &image::dvd::Dvd) {+ match cmd {+ Command::DriveInfo => {+ let dst = gc.di.dma_address.address();+ let buffer = gc.mmio.phys_slice_mut(dst, 0x20);+ buffer.copy_from_slice(&[0x69; 0x20]); // TODO: Drive Info?+ }+ Command::ReadSectorData => {+ let src = gc.di.cmdbuf1 << 2;+ let dst = gc.di.dma_address.address();+ let len = gc.di.cmdbuf2 as usize;+ assert!(len == gc.di.dma_length.length() as usize, "DMA length mismatch");++ let buffer = gc.mmio.phys_slice_mut(dst, len);+ buffer.copy_from_slice(&dvd.data()[src as usize..src as usize + len]);++ tracing::debug!(+ src = format!("{:08X}", src),+ dst = format!("{:08X}", dst),+ len = format!("{:08X}", len),+ "ReadSectorData command"+ );+ }+ Command::ReadDiskId => {+ let src = gc.di.cmdbuf1;+ let dst = gc.di.dma_address.address();+ let len = gc.di.dma_length.length() as usize;++ let buffer = gc.mmio.phys_slice_mut(dst, len);+ buffer.copy_from_slice(&dvd.data()[..len]);++ tracing::debug!(+ src = format!("{:08X}", src),+ dst = format!("{:08X}", dst),+ len = format!("{:08X}", len),+ "ReadDiskId command"+ );+ }+ Command::AudioToggle(enable) => {+ tracing::warn!(enable, "AudioToggle stubbed");+ gc.di.immbuf = 0;}-- self.di.dvd = Some(dvd);- self.di.transfer_started = false;-- // The interrupt must not fire until some time!!- // This is important as it will break the IPL if we immediately raise it...- // When DVDReadDiskID issues the DI command, it returns to BS2_Tick which updates- // the internal state machine and then calls into restore_irq. The code assumes- // that the interrupt wont fire until after it has returned and updated the state.- // Else it will get trapped inside restore_irq, right after mtmsr, executing in a loop- // of the DVD dispatch handler which in turn re-issues the same command...- const DI_TRANSFER_DELAY: u64 = 10_000; // Based off of vxpm and hazel- self.scheduler.schedule_in(DI_TRANSFER_DELAY, |gc| {- gc.complete_dvd_transfer();- });- }-- pub fn complete_dvd_transfer(&mut self) {- self.di.control.set_tstart(false);- // DMA length tracks the progress of the transfer, so when it hits 0, the transfer is complete- // On failure, this would denote how many bytes were not transfered, but we close our eyes- // and just hope nothing depends on that!- self.di.dma_length = regs::DiDmaLengthRegister::from_raw(0);- self.di.status.set_transfer_complete(true);- self.check_di_interrupts();}+}+impl GameCube {pub fn insert_dvd(&mut self, dvd: image::dvd::Dvd) {let name = String::from_utf8_lossy(&dvd.header.game_name);let name = name.trim_end_matches('\0');@@ -183,136 +191,16 @@ impl GameCube {self.di.dvd = Some(dvd);self.close_cover();}-}--impl MmioRw for DvdInterface {- const BASE: u32 = DI_BASE;- const NAME: &'static str = "DVD";-- fn read_raw(&mut self, addr: u32, access_size: u32) -> Option<u32> {- if <regs::DiStatusRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiStatusRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }- if <regs::DiCoverRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiCoverRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }- if <regs::DiDmaAddressRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiDmaAddressRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }- if <regs::DiDmaLengthRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiDmaLengthRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }- if <regs::DiControlRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiControlRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }- if <regs::DiConfigurationRegister as MmioRegister>::fits(addr, access_size) {- return Some(<regs::DiConfigurationRegister as MmioAccess<Self>>::read_at(- self,- addr,- access_size,- ));- }-- match addr {- DICMDBUF0 if access_size == 4 => Some(self.cmdbuf0),- DICMDBUF1 if access_size == 4 => Some(self.cmdbuf1),- DICMDBUF2 if access_size == 4 => Some(self.cmdbuf2),- DIIMMBUF if access_size == 4 => Some(self.immbuf),- _ => None,- }- }-- fn write_raw(&mut self, addr: u32, access_size: u32, val: u32) -> bool {- if <regs::DiStatusRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiStatusRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }- if <regs::DiCoverRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiCoverRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }- if <regs::DiDmaAddressRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiDmaAddressRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }- if <regs::DiDmaLengthRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiDmaLengthRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }- if <regs::DiControlRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiControlRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }- if <regs::DiConfigurationRegister as MmioRegister>::fits(addr, access_size) {- <regs::DiConfigurationRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);- return true;- }-- match addr {- DICMDBUF0 if access_size == 4 => {- self.cmdbuf0 = val;- tracing::debug!(- cmd = format!("{:02X}", val >> 24),- sub1 = format!("{:02X}", (val >> 16) & 0xFF),- sub2 = format!("{:04X}", val & 0xFFFF),- "DICMDBUF0 write"- );- }- DICMDBUF1 if access_size == 4 => {- self.cmdbuf1 = val;- tracing::debug!(val = format!("{val:08X}"), "DICMDBUF1 write");- }- DICMDBUF2 if access_size == 4 => {- self.cmdbuf2 = val;- tracing::debug!(val = format!("{val:08X}"), "DICMDBUF2 write");- }- DIIMMBUF if access_size == 4 => {- self.immbuf = val;- }- _ => return false,- }- true- }-}--impl crate::gamecube::GameCube {- pub fn check_di_interrupts(&mut self) {- if self.di.interrupt_active() {- self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Di);- } else {- self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Di);- }- }pub fn open_cover(&mut self) {tracing::debug!("DVD drive cover opened");self.di.cover = self.di.cover.with_cover_interrupt(true).with_cover_status(true);- self.check_di_interrupts();+ self::refresh_interrupts(self);}pub fn close_cover(&mut self) {tracing::debug!("DVD drive cover closed");self.di.cover = self.di.cover.with_cover_status(false).with_cover_interrupt(false);- self.check_di_interrupts();+ self::refresh_interrupts(self);}}diff --git a/crates/gecko/src/dvd/regs.rs b/crates/gecko/src/dvd/regs.rsindex 4a0ed1a..5d1c886 100644--- a/crates/gecko/src/dvd/regs.rs+++ b/crates/gecko/src/dvd/regs.rs@@ -1,41 +1,43 @@-use super::DvdInterface;-use crate::mmio::traits::MmioAccess;+use crate::dvd;+use crate::gamecube::GameCube;+use crate::mmio::traits::{MmioHandler, WriteMask};use chapa::BitEnum;// 0xCC006000 4 R/W DISR - DI Status Register-crate::mmio_register! {- DiStatusRegister: u32 @ 0xCC006000 {- #[bits(0)]- pub brk: bool,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiStatusRegister {+ #[bits(0)]+ pub brk: bool,- #[bits(1)]- pub device_error_mask: bool,+ #[bits(1)]+ pub device_error_mask: bool,- #[bits(2)]- pub device_error: bool,+ #[bits(2)]+ pub device_error: bool,- #[bits(3)]- pub transfer_complete_mask: bool,+ #[bits(3)]+ pub transfer_complete_mask: bool,- #[bits(4)]- pub transfer_complete: bool,+ #[bits(4)]+ pub transfer_complete: bool,- #[bits(5)]- pub break_complete_mask: bool,+ #[bits(5)]+ pub break_complete_mask: bool,- #[bits(6)]- pub break_complete: bool,- }+ #[bits(6)]+ pub break_complete: bool,}+crate::mmio_reg!(DiStatusRegister: u32 @ 0xCC006000);-impl MmioAccess<DvdInterface> for DiStatusRegister {- fn read(di: &DvdInterface) -> Self {- di.status+impl MmioHandler<GameCube> for DiStatusRegister {+ fn read(gc: &mut GameCube) -> Self {+ gc.di.status}- fn write(self, di: &mut DvdInterface) {- let mut sr = di.status;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let mut sr = gc.di.status;if self.break_complete() {sr = sr.with_break_complete(false);@@ -57,32 +59,34 @@ impl MmioAccess<DvdInterface> for DiStatusRegister {.with_device_error_mask(self.device_error_mask()).with_transfer_complete_mask(self.transfer_complete_mask());- di.status = sr;+ gc.di.status = sr;+ dvd::refresh_interrupts(gc);}}// 0xCC006004 4 R/W DICVR - DI Cover Register-crate::mmio_register! {- DiCoverRegister: u32 @ 0xCC006004 {- #[bits(0)]- pub cover_status: bool,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiCoverRegister {+ #[bits(0)]+ pub cover_status: bool,- #[bits(1)]- pub cover_interrupt_mask: bool,+ #[bits(1)]+ pub cover_interrupt_mask: bool,- #[bits(2)]- pub cover_interrupt: bool,- }+ #[bits(2)]+ pub cover_interrupt: bool,}+crate::mmio_reg!(DiCoverRegister: u32 @ 0xCC006004);-impl MmioAccess<DvdInterface> for DiCoverRegister {- fn read(di: &DvdInterface) -> Self {- di.cover+impl MmioHandler<GameCube> for DiCoverRegister {+ fn read(gc: &mut GameCube) -> Self {+ gc.di.cover}- fn write(self, di: &mut DvdInterface) {- let mut cvr = di.cover;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let mut cvr = gc.di.cover;if self.cover_interrupt() {cvr = cvr.with_cover_interrupt(false);@@ -90,48 +94,100 @@ impl MmioAccess<DvdInterface> for DiCoverRegister {cvr = cvr.with_cover_interrupt_mask(self.cover_interrupt_mask());- di.cover = cvr;+ gc.di.cover = cvr;+ dvd::refresh_interrupts(gc);}}-// 0xCC006014 4 R/W DIMAR - DMA Memory Address Register+// 0xCC006008 4 W DICMDBUF0 - DI Command Buffer 0+// 0xCC00600C 4 W DICMDBUF1 - DI Command Buffer 1+// 0xCC006010 4 W DICMDBUF2 - DI Command Buffer 2-crate::mmio_register! {- DiDmaAddressRegister: u32 @ 0xCC006014 {- #[bits(0..=25)]- pub address: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiCommandBuf0 {+ #[bits(0..=31)]+ pub value: u32,}+crate::mmio_reg!(DiCommandBuf0: u32 @ 0xCC006008);-impl MmioAccess<DvdInterface> for DiDmaAddressRegister {- fn read(di: &DvdInterface) -> Self {- di.dma_address+impl MmioHandler<GameCube> for DiCommandBuf0 {+ fn read(gc: &mut GameCube) -> Self {+ DiCommandBuf0::from_raw(gc.di.cmdbuf0)}- fn write(self, di: &mut DvdInterface) {- di.dma_address = self;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.di.cmdbuf0 = self.raw();+ let val = self.raw();+ tracing::debug!(+ cmd = format!("{:02X}", val >> 24),+ sub1 = format!("{:02X}", (val >> 16) & 0xFF),+ sub2 = format!("{:04X}", val & 0xFFFF),+ "DICMDBUF0 write"+ );}}-// 0xCC006018 4 R/W DILENGTH - DI DMA Transfer Length Register+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiCommandBuf1 {+ #[bits(0..=31)]+ pub value: u32,+}+crate::mmio_reg!(DiCommandBuf1: u32 @ 0xCC00600C);++impl MmioHandler<GameCube> for DiCommandBuf1 {+ fn read(gc: &mut GameCube) -> Self {+ DiCommandBuf1::from_raw(gc.di.cmdbuf1)+ }-crate::mmio_register! {- DiDmaLengthRegister: u32 @ 0xCC006018 {- #[bits(0..=25)]- pub length: u32,+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.di.cmdbuf1 = self.raw();+ tracing::debug!(val = format!("{:08X}", self.raw()), "DICMDBUF1 write");}}-impl MmioAccess<DvdInterface> for DiDmaLengthRegister {- fn read(di: &DvdInterface) -> Self {- di.dma_length+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiCommandBuf2 {+ #[bits(0..=31)]+ pub value: u32,+}+crate::mmio_reg!(DiCommandBuf2: u32 @ 0xCC006010);++impl MmioHandler<GameCube> for DiCommandBuf2 {+ fn read(gc: &mut GameCube) -> Self {+ DiCommandBuf2::from_raw(gc.di.cmdbuf2)}- fn write(self, di: &mut DvdInterface) {- di.dma_length = self;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.di.cmdbuf2 = self.raw();+ tracing::debug!(val = format!("{:08X}", self.raw()), "DICMDBUF2 write");}}+// 0xCC006014 4 R/W DIMAR - DMA Memory Address Register++#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiDmaAddressRegister {+ #[bits(0..=25)]+ pub address: u32,+}+crate::mmio_reg!(DiDmaAddressRegister: u32 @ 0xCC006014);+crate::mmio_default_access!(DiDmaAddressRegister => GameCube.di.dma_address);++// 0xCC006018 4 R/W DILENGTH - DI DMA Transfer Length Register++#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiDmaLengthRegister {+ #[bits(0..=25)]+ pub length: u32,+}+crate::mmio_reg!(DiDmaLengthRegister: u32 @ 0xCC006018);+crate::mmio_default_access!(DiDmaLengthRegister => GameCube.di.dma_length);+// 0xCC00601C 4 R/W DICR - DI Control Register#[derive(BitEnum, Debug, PartialEq, Eq)]@@ -146,52 +202,81 @@ pub enum AccessMode {Write = 1,}-crate::mmio_register! {- DiControlRegister: u32 @ 0xCC00601C {- #[bits(0)]- pub tstart: bool,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiControlRegister {+ #[bits(0)]+ pub tstart: bool,- #[bits(1)]- pub dma: TransferMode,+ #[bits(1)]+ pub dma: TransferMode,- #[bits(2)]- pub access_mode: AccessMode,- }+ #[bits(2)]+ pub access_mode: AccessMode,}+crate::mmio_reg!(DiControlRegister: u32 @ 0xCC00601C);-impl MmioAccess<DvdInterface> for DiControlRegister {- fn read(di: &DvdInterface) -> Self {- di.control+impl MmioHandler<GameCube> for DiControlRegister {+ fn read(gc: &mut GameCube) -> Self {+ gc.di.control}- fn write(self, di: &mut DvdInterface) {- di.control = self;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.di.control = self;+ // tstart latches a transfer: resolve the command and run it now.+ // The scheduled "transfer complete" callback fires after the canonical+ // delay so the CPU side-effect ordering matches real hardware.if self.tstart() {- di.transfer_started = true;+ dvd::start_transfer(gc);}++ dvd::refresh_interrupts(gc);}}-crate::mmio_register! {- DiConfigurationRegister: u32 @ 0xCC006024 {- #[bits(0, readonly)]- pub config: bool+// 0xCC006020 4 W DIIMMBUF - DI Immediate Data Buffer++#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiImmBuf {+ #[bits(0..=31)]+ pub value: u32,+}+crate::mmio_reg!(DiImmBuf: u32 @ 0xCC006020);++impl MmioHandler<GameCube> for DiImmBuf {+ fn read(gc: &mut GameCube) -> Self {+ DiImmBuf::from_raw(gc.di.immbuf)+ }++ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.di.immbuf = self.raw();}}+// 0xCC006024 4 R DICFG - DI Configuration Register (read-only)++#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct DiConfigurationRegister {+ #[bits(0, readonly)]+ pub config: bool,+}+crate::mmio_reg!(DiConfigurationRegister: u32 @ 0xCC006024);+impl Default for DiConfigurationRegister {fn default() -> Self {DiConfigurationRegister::from_raw(0b1)}}-impl MmioAccess<DvdInterface> for DiConfigurationRegister {- fn read(di: &DvdInterface) -> Self {- di.config+impl MmioHandler<GameCube> for DiConfigurationRegister {+ fn read(gc: &mut GameCube) -> Self {+ gc.di.config}- fn write(self, _di: &mut DvdInterface) {+ fn write(self, _gc: &mut GameCube, _: WriteMask) {// Read-only}}diff --git a/crates/gecko/src/flipper/ai.rs b/crates/gecko/src/flipper/ai.rsindex 7698f6e..0f1b9e2 100644--- a/crates/gecko/src/flipper/ai.rs+++ b/crates/gecko/src/flipper/ai.rs@@ -1,7 +1,6 @@pub mod regs;-use crate::mmio::constants::AI_BASE;-use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};+use crate::gamecube::GameCube;/// CPU cycles per AI sample at 32kHz: 486MHz / 32000 = 15187const CYCLES_PER_SAMPLE_32K: u64 = 15187;@@ -14,7 +13,6 @@ pub struct AudioInterface {pub sample_counter: regs::AiSampleCounter,pub interrupt_timing: regs::AiInterruptTiming,pub sample_counter_base_cycle: u64,- pub sample_counter_reset_pending: bool,}impl AudioInterface {@@ -25,7 +23,6 @@ impl AudioInterface {sample_counter: regs::AiSampleCounter::from_raw(0),interrupt_timing: regs::AiInterruptTiming::from_raw(0),sample_counter_base_cycle: 0,- sample_counter_reset_pending: false,}}@@ -46,49 +43,35 @@ impl AudioInterface {};(elapsed / cycles_per_sample) as u32}-- /// Check if the sample counter has reached the interrupt timing threshold- pub fn check_sample_counter_interrupt(&mut self, current_cycles: u64) {- let threshold = self.interrupt_timing.sample_count();- if threshold == 0 {- return;- }-- let count = self.sample_count(current_cycles);- self.sample_counter = regs::AiSampleCounter::from_raw(count);-- if count >= threshold {- self.control = self.control.with_interrupt(true);- }- }}-impl MmioRw for AudioInterface {- const BASE: u32 = AI_BASE;- const NAME: &'static str = "AI";-- crate::impl_mmio_dispatch!(+crate::mmio_device_dispatch! {+ read = ai_read,+ write = ai_write,+ registers = [regs::AiControl,regs::AiVolume,regs::AiInterruptTiming,regs::AiSampleCounter,- );+ ],}-impl crate::gamecube::GameCube {- pub fn check_ai_interrupts(&mut self) {- self.ai.check_sample_counter_interrupt(self.scheduler.cycles);- if self.ai.interrupt_active() {- self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Ai);- } else {- self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Ai);+#[inline(always)]+pub fn refresh_interrupts(gc: &mut GameCube) {+ use crate::flipper::pi::InterruptFlag;++ let threshold = gc.ai.interrupt_timing.sample_count();+ if threshold != 0 {+ let count = gc.ai.sample_count(gc.scheduler.cycles);+ gc.ai.sample_counter = regs::AiSampleCounter::from_raw(count);+ if count >= threshold {+ gc.ai.control = gc.ai.control.with_interrupt(true);}}- pub fn check_sample_counter_reset(&mut self) {- if self.ai.sample_counter_reset_pending {- self.ai.sample_counter_base_cycle = self.scheduler.cycles;- self.ai.sample_counter_reset_pending = false;- }+ if gc.ai.interrupt_active() {+ gc.pi.assert_interrupt(InterruptFlag::Ai);+ } else {+ gc.pi.clear_interrupt(InterruptFlag::Ai);}}diff --git a/crates/gecko/src/flipper/ai/regs.rs b/crates/gecko/src/flipper/ai/regs.rsindex 765401e..ab0838e 100644--- a/crates/gecko/src/flipper/ai/regs.rs+++ b/crates/gecko/src/flipper/ai/regs.rs@@ -1,5 +1,6 @@-use super::AudioInterface;-use crate::mmio::traits::MmioAccess;+use crate::flipper::ai;+use crate::gamecube::GameCube;+use crate::mmio::traits::{MmioHandler, WriteMask};use chapa::BitEnum;// 0xCC006C00 4 R/W AICR - Audio Interface Control Register@@ -16,38 +17,39 @@ pub enum SampleRate {Rate32KHz = 1,}-crate::mmio_register! {- AiControl: u32 @ 0xCC006C00 {- #[bits(0, alias = "pstat")]- pub playback_status: Status,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AiControl {+ #[bits(0, alias = "pstat")]+ pub playback_status: Status,- #[bits(1, alias = "afr")]- pub sample_rate: SampleRate,+ #[bits(1, alias = "afr")]+ pub sample_rate: SampleRate,- #[bits(2, alias = "aiintmsk")]- pub interrupt_mask: bool,+ #[bits(2, alias = "aiintmsk")]+ pub interrupt_mask: bool,- #[bits(3, alias = "aiint")]- pub interrupt: bool,+ #[bits(3, alias = "aiint")]+ pub interrupt: bool,- #[bits(4, alias = "aiintvld")]- pub interrupt_valid: bool,+ #[bits(4, alias = "aiintvld")]+ pub interrupt_valid: bool,- #[bits(5, alias = "screset")]- pub sample_counter_reset: bool,+ #[bits(5, alias = "screset")]+ pub sample_counter_reset: bool,- #[bits(6)]- pub dsp_sample_rate: bool,- }+ #[bits(6)]+ pub dsp_sample_rate: bool,}+crate::mmio_reg!(AiControl: u32 @ 0xCC006C00);-impl MmioAccess<AudioInterface> for AiControl {- fn read(ai: &AudioInterface) -> Self {- ai.control+impl MmioHandler<GameCube> for AiControl {+ fn read(gc: &mut GameCube) -> Self {+ gc.ai.control}- fn write(self, ai: &mut AudioInterface) {- let mut cr = ai.control;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let mut cr = gc.ai.control;// AIINT is w1cif self.interrupt() {@@ -64,48 +66,65 @@ impl MmioAccess<AudioInterface> for AiControl {// SCRESET resets the sample counterif self.sample_counter_reset() {- ai.sample_counter_reset_pending = true;- ai.sample_counter = AiSampleCounter::from_raw(0);+ gc.ai.sample_counter_base_cycle = gc.scheduler.cycles;+ gc.ai.sample_counter = AiSampleCounter::from_raw(0);}- ai.control = cr;+ gc.ai.control = cr;+ ai::refresh_interrupts(gc);}}// 0xCC006C04 4 R/W AIVR - Audio Interface Volume Register-crate::mmio_register! {- AiVolume: u32 @ 0xCC006C04 => AudioInterface.volume {- #[bits(0..=7)]- pub left: u8,-- #[bits(8..=15)]- pub right: u8,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AiVolume {+ #[bits(0..=7)]+ pub left: u8,++ #[bits(8..=15)]+ pub right: u8,}+crate::mmio_reg!(AiVolume: u32 @ 0xCC006C04);+crate::mmio_default_access!(AiVolume => GameCube.ai.volume);// 0xCC006C08 4 R AISCNT - Audio Interface Sample Counter-crate::mmio_register! {- AiSampleCounter: u32 @ 0xCC006C08 {- #[bits(0..=30)]- pub sample_count: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AiSampleCounter {+ #[bits(0..=30)]+ pub sample_count: u32,}+crate::mmio_reg!(AiSampleCounter: u32 @ 0xCC006C08);-impl MmioAccess<AudioInterface> for AiSampleCounter {- fn read(ai: &AudioInterface) -> Self {- ai.sample_counter+impl MmioHandler<GameCube> for AiSampleCounter {+ fn read(gc: &mut GameCube) -> Self {+ let count = gc.ai.sample_count(gc.scheduler.cycles);+ AiSampleCounter::from_raw(count)}- fn write(self, _ai: &mut AudioInterface) {+ fn write(self, _gc: &mut GameCube, _: WriteMask) {tracing::warn!("attempted to write to read-only AiSampleCounter");}}// 0xCC006C0C 4 R/W AIIT - Audio Interface Interrupt Timing-crate::mmio_register! {- AiInterruptTiming: u32 @ 0xCC006C0C => AudioInterface.interrupt_timing {- #[bits(0..=30)]- pub sample_count: u32,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct AiInterruptTiming {+ #[bits(0..=30)]+ pub sample_count: u32,+}+crate::mmio_reg!(AiInterruptTiming: u32 @ 0xCC006C0C);++impl MmioHandler<GameCube> for AiInterruptTiming {+ fn read(gc: &mut GameCube) -> Self {+ gc.ai.interrupt_timing+ }++ fn write(self, gc: &mut GameCube, _: WriteMask) {+ gc.ai.interrupt_timing = self;+ ai::refresh_interrupts(gc);}}diff --git a/crates/gecko/src/flipper/exi.rs b/crates/gecko/src/flipper/exi.rsindex 206fdab..2efdf0e 100644--- a/crates/gecko/src/flipper/exi.rs+++ b/crates/gecko/src/flipper/exi.rs@@ -3,9 +3,7 @@ pub mod macronix;pub mod regs;use crate::flipper::exi::regs::TransferType;-use crate::mmio::Mmio;-use crate::mmio::constants::EXI_BASE;-use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};+use crate::gamecube::GameCube;pub struct ExternalInterface {// Channel 0@@ -78,16 +76,19 @@ impl ExternalInterface {}}- fn start_immediate_transfer(&mut self, channel: usize) {- match channel {- 0 => self.start_immediate_transfer_for_channel::<0>(),- 1 => self.start_immediate_transfer_for_channel::<1>(),- 2 => self.start_immediate_transfer_for_channel::<2>(),- _ => unreachable!(),- }+ pub fn interrupt_active(&self) -> bool {+ Self::channel_interrupt_active(&self.ch0_csr)+ || Self::channel_interrupt_active(&self.ch1_csr)+ || Self::channel_interrupt_active(&self.ch2_csr)+ }++ fn channel_interrupt_active(csr: &impl regs::ChannelStatus) -> bool {+ (csr.exi_interrupt() && csr.exi_interrupt_mask())+ || (csr.tc_interrupt() && csr.tc_interrupt_mask())+ || (csr.ext_interrupt() && csr.ext_interrupt_mask())}- fn start_immediate_transfer_for_channel<const CHANNEL: usize>(&mut self) {+ pub fn start_immediate_transfer<const CHANNEL: usize>(&mut self) {let (transfer_type, transfer_length, chip_select, mut bytes) = match CHANNEL {0 => (self.ch0_cr.transfer_type(),@@ -118,147 +119,29 @@ impl ExternalInterface {cs = chip_select,"EXI immediate transfer with no/invalid chip select");- self.finish_transfer_for_channel::<CHANNEL>();+ self.finish_transfer::<CHANNEL>();return;}};let size = (transfer_length as usize) + 1;- // TODO: idkif let Some(device) = &mut self.devices[CHANNEL][slot] {for i in 0..size {if transfer_type == TransferType::Read {bytes[i] = 0;}-device.transfer_byte(&mut bytes[i]);}} else {bytes[..size].fill(0);}- self.set_data_for_channel::<CHANNEL>(u32::from_be_bytes(bytes));- self.finish_transfer_for_channel::<CHANNEL>();- }-- fn pending_dma(&self, channel: usize) -> Option<(u8, TransferType, u32, u32)> {- match channel {- 0 => self.pending_dma_for_channel::<0>(),- 1 => self.pending_dma_for_channel::<1>(),- 2 => self.pending_dma_for_channel::<2>(),- _ => None,- }- }-- fn pending_dma_for_channel<const CHANNEL: usize>(&self) -> Option<(u8, TransferType, u32, u32)> {- let (transfer_start, dma_mode, chip_select, transfer_type, address, length) = match CHANNEL {- 0 => (- self.ch0_cr.transfer_start(),- self.ch0_cr.dma_mode(),- self.ch0_csr.chip_select(),- self.ch0_cr.transfer_type(),- self.ch0_mar.address(),- self.ch0_length.length(),- ),- 1 => (- self.ch1_cr.transfer_start(),- self.ch1_cr.dma_mode(),- self.ch1_csr.chip_select(),- self.ch1_cr.transfer_type(),- self.ch1_mar.address(),- self.ch1_length.length(),- ),- 2 => (- self.ch2_cr.transfer_start(),- self.ch2_cr.dma_mode(),- self.ch2_csr.chip_select(),- self.ch2_cr.transfer_type(),- self.ch2_mar.address(),- self.ch2_length.length(),- ),- _ => unreachable!(),- };-- if !transfer_start || !dma_mode {- return None;- }-- Some((chip_select, transfer_type, address << 5, length << 5))+ self.set_data::<CHANNEL>(u32::from_be_bytes(bytes));+ self.finish_transfer::<CHANNEL>();}- pub fn process_cs_changes(&mut self) {- let current = [- self.ch0_csr.chip_select(),- self.ch1_csr.chip_select(),- self.ch2_csr.chip_select(),- ];-- for channel in 0..3 {- let prev = self.prev_cs[channel];- let curr = current[channel];- if curr != prev && curr != 0 {- if let Some(slot) = Self::cs_to_slot(curr)- && let Some(device) = &mut self.devices[channel][slot]- {- device.on_select();- }- }- }-- self.prev_cs = current;- }-- pub fn process_dma_transfers(&mut self, mmio: &mut Mmio) {- const CHANNEL_COUNT: usize = 3;-- for channel in 0..CHANNEL_COUNT {- let Some((cs, transfer_type, address, length)) = self.pending_dma(channel) else {- continue;- };-- tracing::debug!(- channel,- cs,- ?transfer_type,- address = format!("{:08X}", address),- length,- "EXI DMA transfer"- );-- let slot = match Self::cs_to_slot(cs) {- Some(s) => s,- None => {- tracing::warn!(channel, cs, "EXI DMA transfer with no/invalid chip select");- self.finish_transfer(channel);- continue;- }- };-- if let Some(device) = &mut self.devices[channel][slot] {- match transfer_type {- TransferType::Read => device.dma_read(mmio.phys_slice_mut(address, length as usize)),- TransferType::Write => device.dma_write(mmio.phys_slice(address, length as usize)),- TransferType::ReadAndWrite | TransferType::Reserved => {- tracing::error!(channel, cs, "EXI DMA transfer with invalid/unimplemented transfer type");- }- }- }-- self.finish_transfer(channel);- }- }-- fn finish_transfer(&mut self, channel: usize) {- match channel {- 0 => self.finish_transfer_for_channel::<0>(),- 1 => self.finish_transfer_for_channel::<1>(),- 2 => self.finish_transfer_for_channel::<2>(),- _ => {}- }- }-- fn set_data_for_channel<const CHANNEL: usize>(&mut self, val: u32) {+ fn set_data<const CHANNEL: usize>(&mut self, val: u32) {match CHANNEL {0 => self.ch0_data = regs::Channel0Data::from_raw(val),1 => self.ch1_data = regs::Channel1Data::from_raw(val),@@ -267,7 +150,7 @@ impl ExternalInterface {}}- fn finish_transfer_for_channel<const CHANNEL: usize>(&mut self) {+ fn finish_transfer<const CHANNEL: usize>(&mut self) {match CHANNEL {0 => {self.ch0_cr.set_transfer_start(false);@@ -286,11 +169,10 @@ impl ExternalInterface {}}-impl MmioRw for ExternalInterface {- const BASE: u32 = EXI_BASE;- const NAME: &'static str = "EXI";-- crate::impl_mmio_dispatch!(+crate::mmio_device_dispatch! {+ read = exi_read,+ write = exi_write,+ registers = [regs::Channel0Status,regs::Channel0DmaAddress,regs::Channel0DmaLength,@@ -306,29 +188,87 @@ impl MmioRw for ExternalInterface {regs::Channel2DmaLength,regs::Channel2Control,regs::Channel2Data,- );+ ],}-impl ExternalInterface {- pub fn interrupt_active(&self) -> bool {- Self::channel_interrupt_active(&self.ch0_csr)- || Self::channel_interrupt_active(&self.ch1_csr)- || Self::channel_interrupt_active(&self.ch2_csr)+#[inline(always)]+pub fn refresh_interrupts(gc: &mut GameCube) {+ use crate::flipper::pi::InterruptFlag;++ if gc.exi.interrupt_active() {+ gc.pi.assert_interrupt(InterruptFlag::Exi);+ } else {+ gc.pi.clear_interrupt(InterruptFlag::Exi);}+}- fn channel_interrupt_active(csr: &impl regs::ChannelStatus) -> bool {- (csr.exi_interrupt() && csr.exi_interrupt_mask())- || (csr.tc_interrupt() && csr.tc_interrupt_mask())- || (csr.ext_interrupt() && csr.ext_interrupt_mask())+#[inline(always)]+pub fn on_chip_select_written<const CHANNEL: usize>(gc: &mut GameCube, new_cs: u8) {+ let prev = gc.exi.prev_cs[CHANNEL];+ if new_cs != prev && new_cs != 0 {+ if let Some(slot) = ExternalInterface::cs_to_slot(new_cs)+ && let Some(device) = &mut gc.exi.devices[CHANNEL][slot]+ {+ device.on_select();+ }}+ gc.exi.prev_cs[CHANNEL] = new_cs;}-impl crate::gamecube::GameCube {- pub fn check_exi_interrupts(&mut self) {- if self.exi.interrupt_active() {- self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Exi);- } else {- self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Exi);+pub fn run_dma<const CHANNEL: usize>(gc: &mut GameCube) {+ let (cs, transfer_type, address, length) = match CHANNEL {+ 0 => (+ gc.exi.ch0_csr.chip_select(),+ gc.exi.ch0_cr.transfer_type(),+ gc.exi.ch0_mar.address() << 5,+ gc.exi.ch0_length.length() << 5,+ ),+ 1 => (+ gc.exi.ch1_csr.chip_select(),+ gc.exi.ch1_cr.transfer_type(),+ gc.exi.ch1_mar.address() << 5,+ gc.exi.ch1_length.length() << 5,+ ),+ 2 => (+ gc.exi.ch2_csr.chip_select(),+ gc.exi.ch2_cr.transfer_type(),+ gc.exi.ch2_mar.address() << 5,+ gc.exi.ch2_length.length() << 5,+ ),+ _ => unreachable!(),+ };++ tracing::debug!(+ channel = CHANNEL,+ cs,+ ?transfer_type,+ address = format!("{:08X}", address),+ length,+ "EXI DMA transfer"+ );++ let slot = match ExternalInterface::cs_to_slot(cs) {+ Some(s) => s,+ None => {+ tracing::warn!(channel = CHANNEL, cs, "EXI DMA transfer with no/invalid chip select");+ gc.exi.finish_transfer::<CHANNEL>();+ return;+ }+ };++ if let Some(device) = &mut gc.exi.devices[CHANNEL][slot] {+ match transfer_type {+ TransferType::Read => device.dma_read(gc.mmio.phys_slice_mut(address, length as usize)),+ TransferType::Write => device.dma_write(gc.mmio.phys_slice(address, length as usize)),+ TransferType::ReadAndWrite | TransferType::Reserved => {+ tracing::error!(+ channel = CHANNEL,+ cs,+ "EXI DMA transfer with invalid/unimplemented transfer type"+ );+ }}}++ gc.exi.finish_transfer::<CHANNEL>();}diff --git a/crates/gecko/src/flipper/exi/regs.rs b/crates/gecko/src/flipper/exi/regs.rsindex 12788be..817de11 100644--- a/crates/gecko/src/flipper/exi/regs.rs+++ b/crates/gecko/src/flipper/exi/regs.rs@@ -1,7 +1,8 @@use chapa::BitEnum;-use super::ExternalInterface;-use crate::mmio::traits::{MmioAccess, MmioRegister};+use crate::flipper::exi;+use crate::gamecube::GameCube;+use crate::mmio::traits::{MmioHandler, MmioRegister, WriteMask};pub trait ChannelStatus {fn exi_interrupt(&self) -> bool;@@ -63,173 +64,246 @@ impl std::fmt::Debug for TransferType {// 0xCC006800 4 R/W EXI0CSR - EXI Channel 0 Status Register-crate::mmio_register! {- Channel0Status: u32 @ 0xCC006800 {- #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,- #[bits(1, alias = "exiint")] pub exi_interrupt: bool,- #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,- #[bits(3, alias = "tcint")] pub tc_interrupt: bool,- #[bits(4..=6, alias = "clk")] pub clock: u8,- #[bits(7..=9, alias = "cs")] pub chip_select: u8,- #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,- #[bits(11, alias = "extint")] pub ext_interrupt: bool,- #[bits(12, alias = "ext")] pub device_connected: bool,- #[bits(13, alias = "romdis")] pub rom_descramble_disabled: bool,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel0Status {+ #[bits(0, alias = "exiintmask")]+ pub exi_interrupt_mask: bool,+ #[bits(1, alias = "exiint")]+ pub exi_interrupt: bool,+ #[bits(2, alias = "tcintmask")]+ pub tc_interrupt_mask: bool,+ #[bits(3, alias = "tcint")]+ pub tc_interrupt: bool,+ #[bits(4..=6, alias = "clk")]+ pub clock: u8,+ #[bits(7..=9, alias = "cs")]+ pub chip_select: u8,+ #[bits(10, alias = "extintmask")]+ pub ext_interrupt_mask: bool,+ #[bits(11, alias = "extint")]+ pub ext_interrupt: bool,+ #[bits(12, alias = "ext")]+ pub device_connected: bool,+ #[bits(13, alias = "romdis")]+ pub rom_descramble_disabled: bool,}+crate::mmio_reg!(Channel0Status: u32 @ 0xCC006800);-impl MmioAccess<ExternalInterface> for Channel0Status {- fn read(exi: &ExternalInterface) -> Self {- exi.ch0_csr+impl MmioHandler<GameCube> for Channel0Status {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch0_csr}- fn write(self, exi: &mut ExternalInterface) {- write_csr(&mut exi.ch0_csr, self);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ write_csr(&mut gc.exi.ch0_csr, self);+ exi::on_chip_select_written::<0>(gc, self.chip_select());+ exi::refresh_interrupts(gc);}}// 0xCC006804 4 R/W EXI0MAR - EXI Channel 0 DMA Start Address-crate::mmio_register! {- Channel0DmaAddress: u32 @ 0xCC006804 => ExternalInterface.ch0_mar {- #[bits(5..=25, alias = "addr")] pub address: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel0DmaAddress {+ #[bits(5..=25, alias = "addr")]+ pub address: u32,}+crate::mmio_reg!(Channel0DmaAddress: u32 @ 0xCC006804);+crate::mmio_default_access!(Channel0DmaAddress => GameCube.exi.ch0_mar);// 0xCC006808 4 R/W EXI0LENGTH - EXI Channel 0 DMA Transfer Length-crate::mmio_register! {- Channel0DmaLength: u32 @ 0xCC006808 => ExternalInterface.ch0_length {- #[bits(5..=25, alias = "len")] pub length: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel0DmaLength {+ #[bits(5..=25, alias = "len")]+ pub length: u32,}+crate::mmio_reg!(Channel0DmaLength: u32 @ 0xCC006808);+crate::mmio_default_access!(Channel0DmaLength => GameCube.exi.ch0_length);// 0xCC00680C 4 R/W EXI0CR - EXI Channel 0 Control Register-crate::mmio_register! {- Channel0Control: u32 @ 0xCC00680C {- #[bits(0, alias = "tstart")] pub transfer_start: bool,- #[bits(1, alias = "dma")] pub dma_mode: bool,- #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,- #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel0Control {+ #[bits(0, alias = "tstart")]+ pub transfer_start: bool,+ #[bits(1, alias = "dma")]+ pub dma_mode: bool,+ #[bits(2..=3, alias = "rw")]+ pub transfer_type: TransferType,+ #[bits(4..=5, alias = "tlen")]+ pub transfer_length: u8,}+crate::mmio_reg!(Channel0Control: u32 @ 0xCC00680C);-impl MmioAccess<ExternalInterface> for Channel0Control {- fn read(exi: &ExternalInterface) -> Self {- exi.ch0_cr+impl MmioHandler<GameCube> for Channel0Control {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch0_cr}- fn write(self, exi: &mut ExternalInterface) {- let was_started = exi.ch0_cr.transfer_start();- exi.ch0_cr = self;- if self.transfer_start() && !was_started && !self.dma_mode() {- exi.start_immediate_transfer(0);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let was_started = gc.exi.ch0_cr.transfer_start();+ gc.exi.ch0_cr = self;+ if self.transfer_start() && !was_started {+ if self.dma_mode() {+ exi::run_dma::<0>(gc);+ } else {+ gc.exi.start_immediate_transfer::<0>();+ }}+ exi::refresh_interrupts(gc);}}// 0xCC006810 4 R/W EXI0DATA - EXI Channel 0 Immediate Data-crate::mmio_register! {- Channel0Data: u32 @ 0xCC006810 => ExternalInterface.ch0_data {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel0Data {}+crate::mmio_reg!(Channel0Data: u32 @ 0xCC006810);+crate::mmio_default_access!(Channel0Data => GameCube.exi.ch0_data);// --- Channel 1 ---// 0xCC006814 4 R/W EXI1CSR - EXI Channel 1 Status Register-crate::mmio_register! {- Channel1Status: u32 @ 0xCC006814 {- #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,- #[bits(1, alias = "exiint")] pub exi_interrupt: bool,- #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,- #[bits(3, alias = "tcint")] pub tc_interrupt: bool,- #[bits(4..=6, alias = "clk")] pub clock: u8,- #[bits(7..=9, alias = "cs")] pub chip_select: u8,- #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,- #[bits(11, alias = "extint")] pub ext_interrupt: bool,- #[bits(12, alias = "ext")] pub device_connected: bool,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel1Status {+ #[bits(0, alias = "exiintmask")]+ pub exi_interrupt_mask: bool,+ #[bits(1, alias = "exiint")]+ pub exi_interrupt: bool,+ #[bits(2, alias = "tcintmask")]+ pub tc_interrupt_mask: bool,+ #[bits(3, alias = "tcint")]+ pub tc_interrupt: bool,+ #[bits(4..=6, alias = "clk")]+ pub clock: u8,+ #[bits(7..=9, alias = "cs")]+ pub chip_select: u8,+ #[bits(10, alias = "extintmask")]+ pub ext_interrupt_mask: bool,+ #[bits(11, alias = "extint")]+ pub ext_interrupt: bool,+ #[bits(12, alias = "ext")]+ pub device_connected: bool,}+crate::mmio_reg!(Channel1Status: u32 @ 0xCC006814);-impl MmioAccess<ExternalInterface> for Channel1Status {- fn read(exi: &ExternalInterface) -> Self {- exi.ch1_csr+impl MmioHandler<GameCube> for Channel1Status {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch1_csr}- fn write(self, exi: &mut ExternalInterface) {- write_csr(&mut exi.ch1_csr, self);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ write_csr(&mut gc.exi.ch1_csr, self);+ exi::on_chip_select_written::<1>(gc, self.chip_select());+ exi::refresh_interrupts(gc);}}// 0xCC006818 4 R/W EXI1MAR - EXI Channel 1 DMA Start Address-crate::mmio_register! {- Channel1DmaAddress: u32 @ 0xCC006818 => ExternalInterface.ch1_mar {- #[bits(5..=25, alias = "addr")] pub address: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel1DmaAddress {+ #[bits(5..=25, alias = "addr")]+ pub address: u32,}+crate::mmio_reg!(Channel1DmaAddress: u32 @ 0xCC006818);+crate::mmio_default_access!(Channel1DmaAddress => GameCube.exi.ch1_mar);// 0xCC00681C 4 R/W EXI1LENGTH - EXI Channel 1 DMA Transfer Length-crate::mmio_register! {- Channel1DmaLength: u32 @ 0xCC00681C => ExternalInterface.ch1_length {- #[bits(5..=25, alias = "len")] pub length: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel1DmaLength {+ #[bits(5..=25, alias = "len")]+ pub length: u32,}+crate::mmio_reg!(Channel1DmaLength: u32 @ 0xCC00681C);+crate::mmio_default_access!(Channel1DmaLength => GameCube.exi.ch1_length);// 0xCC006820 4 R/W EXI1CR - EXI Channel 1 Control Register-crate::mmio_register! {- Channel1Control: u32 @ 0xCC006820 {- #[bits(0, alias = "tstart")] pub transfer_start: bool,- #[bits(1, alias = "dma")] pub dma_mode: bool,- #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,- #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel1Control {+ #[bits(0, alias = "tstart")]+ pub transfer_start: bool,+ #[bits(1, alias = "dma")]+ pub dma_mode: bool,+ #[bits(2..=3, alias = "rw")]+ pub transfer_type: TransferType,+ #[bits(4..=5, alias = "tlen")]+ pub transfer_length: u8,}+crate::mmio_reg!(Channel1Control: u32 @ 0xCC006820);-impl MmioAccess<ExternalInterface> for Channel1Control {- fn read(exi: &ExternalInterface) -> Self {- exi.ch1_cr+impl MmioHandler<GameCube> for Channel1Control {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch1_cr}- fn write(self, exi: &mut ExternalInterface) {- let was_started = exi.ch1_cr.transfer_start();- exi.ch1_cr = self;- if self.transfer_start() && !was_started && !self.dma_mode() {- exi.start_immediate_transfer(1);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let was_started = gc.exi.ch1_cr.transfer_start();+ gc.exi.ch1_cr = self;+ if self.transfer_start() && !was_started {+ if self.dma_mode() {+ exi::run_dma::<1>(gc);+ } else {+ gc.exi.start_immediate_transfer::<1>();+ }}+ exi::refresh_interrupts(gc);}}// 0xCC006824 4 R/W EXI1DATA - EXI Channel 1 Immediate Data-crate::mmio_register! {- Channel1Data: u32 @ 0xCC006824 => ExternalInterface.ch1_data {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel1Data {}+crate::mmio_reg!(Channel1Data: u32 @ 0xCC006824);+crate::mmio_default_access!(Channel1Data => GameCube.exi.ch1_data);// --- Channel 2 ---// 0xCC006828 4 R/W EXI2CSR - EXI Channel 2 Status Register-crate::mmio_register! {- Channel2Status: u32 @ 0xCC006828 {- #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,- #[bits(1, alias = "exiint")] pub exi_interrupt: bool,- #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,- #[bits(3, alias = "tcint")] pub tc_interrupt: bool,- #[bits(4..=6, alias = "clk")] pub clock: u8,- #[bits(7..=9, alias = "cs")] pub chip_select: u8,- #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,- #[bits(11, alias = "extint")] pub ext_interrupt: bool,- #[bits(12, alias = "ext")] pub device_connected: bool,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel2Status {+ #[bits(0, alias = "exiintmask")]+ pub exi_interrupt_mask: bool,+ #[bits(1, alias = "exiint")]+ pub exi_interrupt: bool,+ #[bits(2, alias = "tcintmask")]+ pub tc_interrupt_mask: bool,+ #[bits(3, alias = "tcint")]+ pub tc_interrupt: bool,+ #[bits(4..=6, alias = "clk")]+ pub clock: u8,+ #[bits(7..=9, alias = "cs")]+ pub chip_select: u8,+ #[bits(10, alias = "extintmask")]+ pub ext_interrupt_mask: bool,+ #[bits(11, alias = "extint")]+ pub ext_interrupt: bool,+ #[bits(12, alias = "ext")]+ pub device_connected: bool,}+crate::mmio_reg!(Channel2Status: u32 @ 0xCC006828);-impl MmioAccess<ExternalInterface> for Channel2Status {- fn read(exi: &ExternalInterface) -> Self {- exi.ch2_csr+impl MmioHandler<GameCube> for Channel2Status {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch2_csr}- fn write(self, exi: &mut ExternalInterface) {- write_csr(&mut exi.ch2_csr, self);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ write_csr(&mut gc.exi.ch2_csr, self);+ exi::on_chip_select_written::<2>(gc, self.chip_select());+ exi::refresh_interrupts(gc);}}@@ -237,46 +311,64 @@ impl_channel_status!(Channel0Status, Channel1Status, Channel2Status);// 0xCC00682C 4 R/W EXI2MAR - EXI Channel 2 DMA Start Address-crate::mmio_register! {- Channel2DmaAddress: u32 @ 0xCC00682C => ExternalInterface.ch2_mar {- #[bits(5..=25, alias = "addr")] pub address: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel2DmaAddress {+ #[bits(5..=25, alias = "addr")]+ pub address: u32,}+crate::mmio_reg!(Channel2DmaAddress: u32 @ 0xCC00682C);+crate::mmio_default_access!(Channel2DmaAddress => GameCube.exi.ch2_mar);// 0xCC006830 4 R/W EXI2LENGTH - EXI Channel 2 DMA Transfer Length-crate::mmio_register! {- Channel2DmaLength: u32 @ 0xCC006830 => ExternalInterface.ch2_length {- #[bits(5..=25, alias = "len")] pub length: u32,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel2DmaLength {+ #[bits(5..=25, alias = "len")]+ pub length: u32,}+crate::mmio_reg!(Channel2DmaLength: u32 @ 0xCC006830);+crate::mmio_default_access!(Channel2DmaLength => GameCube.exi.ch2_length);// 0xCC006834 4 R/W EXI2CR - EXI Channel 2 Control Register-crate::mmio_register! {- Channel2Control: u32 @ 0xCC006834 {- #[bits(0, alias = "tstart")] pub transfer_start: bool,- #[bits(1, alias = "dma")] pub dma_mode: bool,- #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,- #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,- }+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel2Control {+ #[bits(0, alias = "tstart")]+ pub transfer_start: bool,+ #[bits(1, alias = "dma")]+ pub dma_mode: bool,+ #[bits(2..=3, alias = "rw")]+ pub transfer_type: TransferType,+ #[bits(4..=5, alias = "tlen")]+ pub transfer_length: u8,}+crate::mmio_reg!(Channel2Control: u32 @ 0xCC006834);-impl MmioAccess<ExternalInterface> for Channel2Control {- fn read(exi: &ExternalInterface) -> Self {- exi.ch2_cr+impl MmioHandler<GameCube> for Channel2Control {+ fn read(gc: &mut GameCube) -> Self {+ gc.exi.ch2_cr}- fn write(self, exi: &mut ExternalInterface) {- let was_started = exi.ch2_cr.transfer_start();- exi.ch2_cr = self;- if self.transfer_start() && !was_started && !self.dma_mode() {- exi.start_immediate_transfer(2);+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let was_started = gc.exi.ch2_cr.transfer_start();+ gc.exi.ch2_cr = self;+ if self.transfer_start() && !was_started {+ if self.dma_mode() {+ exi::run_dma::<2>(gc);+ } else {+ gc.exi.start_immediate_transfer::<2>();+ }}+ exi::refresh_interrupts(gc);}}// 0xCC006838 4 R/W EXI2DATA - EXI Channel 2 Immediate Data-crate::mmio_register! {- Channel2Data: u32 @ 0xCC006838 => ExternalInterface.ch2_data {}-}+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct Channel2Data {}+crate::mmio_reg!(Channel2Data: u32 @ 0xCC006838);+crate::mmio_default_access!(Channel2Data => GameCube.exi.ch2_data);diff --git a/crates/gecko/src/flipper/si.rs b/crates/gecko/src/flipper/si.rsindex 7900ae3..7ee2abf 100644--- a/crates/gecko/src/flipper/si.rs+++ b/crates/gecko/src/flipper/si.rs@@ -1,8 +1,8 @@pub mod pad;pub mod regs;+use crate::gamecube::GameCube;use crate::mmio::constants::SI_BASE;-use crate::mmio::traits::{MmioAccess, MmioRegister};use pad::{GC_CONTROLLER_ID, PadStatus};const NUM_CHANNELS: usize = 4;@@ -52,119 +52,6 @@ impl SerialInterface {|| (self.comcsr.rdst_interrupt() && self.comcsr.rdst_interrupt_mask())}- crate::impl_mmio_dispatch!(regs::SiPoll, regs::SiComcsr, regs::SiStatusRegister,);-- pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {- match offset {- // Channel registers: each channel occupies 0x0C bytes- 0x00..=0x2F => self.read_channel_reg(offset),- // EXI clock count- 0x3C => self.exi_clock_count,- // SI I/O buffer- 0x80..=0xFC => {- let i = (offset - 0x80) as usize;- u32::from_be_bytes([- self.io_buffer[i],- self.io_buffer[i + 1],- self.io_buffer[i + 2],- self.io_buffer[i + 3],- ])- }- // SIPOLL / SICOMCSR / SISR- _ => self.read_raw(SI_BASE + offset, 4).unwrap_or_else(|| {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI read_u32");- 0- }),- }- }-- pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {- match offset {- 0x00..=0x2F | 0x3C..=0x3E | 0x80..=0xFE => {- let aligned = offset & !3;- let word = self.mmio_read_u32(aligned);- if offset & 2 == 0 {- (word >> 16) as u16- } else {- word as u16- }- }- _ => self.read_raw(SI_BASE + offset, 2).unwrap_or_else(|| {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI read_u16");- 0- }) as u16,- }- }-- pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {- match offset {- 0x00..=0x2F | 0x3C..=0x3F | 0x80..=0xFF => {- let aligned = offset & !3;- let word = self.mmio_read_u32(aligned);- let byte_pos = offset & 3;- ((word >> ((3 - byte_pos) * 8)) & 0xFF) as u8- }- _ => self.read_raw(SI_BASE + offset, 1).unwrap_or_else(|| {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI read_u8");- 0- }) as u8,- }- }-- pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {- match offset {- 0x00..=0x2F => self.write_channel_reg(offset, val),- 0x3C => self.exi_clock_count = val,- 0x80..=0xFC => {- let i = (offset - 0x80) as usize;- self.io_buffer[i..i + 4].copy_from_slice(&val.to_be_bytes());- }- _ => {- if !self.write_raw(SI_BASE + offset, 4, val) {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u32");- }- }- }- }-- pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {- match offset {- 0x00..=0x2F | 0x3C..=0x3E | 0x80..=0xFE => {- let aligned = offset & !3;- let mut word = self.read_channel_or_buf_u32_raw(aligned);- if offset & 2 == 0 {- word = (word & 0x0000_FFFF) | ((val as u32) << 16);- } else {- word = (word & 0xFFFF_0000) | (val as u32);- }- self.mmio_write_u32(aligned, word);- }- _ => {- if !self.write_raw(SI_BASE + offset, 2, val as u32) {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u16");- }- }- }- }-- pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {- match offset {- 0x00..=0x2F | 0x3C..=0x3F | 0x80..=0xFF => {- let aligned = offset & !3;- let mut word = self.read_channel_or_buf_u32_raw(aligned);- let byte_pos = offset & 3;- let shift = (3 - byte_pos) * 8;- word = (word & !(0xFF << shift)) | ((val as u32) << shift);- self.mmio_write_u32(aligned, word);- }- _ => {- if !self.write_raw(SI_BASE + offset, 1, val as u32) {- tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u8");- }- }- }- }-fn read_channel_reg(&mut self, offset: u32) -> u32 {let ch = (offset / 0x0C) as usize;match offset % 0x0C {@@ -191,27 +78,12 @@ impl SerialInterface {}}- fn read_channel_or_buf_u32_raw(&self, offset: u32) -> u32 {- match offset {- 0x00..=0x2F => {- let ch = (offset / 0x0C) as usize;- match offset % 0x0C {- 0x00 => self.channels[ch].out,- 0x04 => self.channels[ch].in_hi,- 0x08 => self.channels[ch].in_lo,- _ => 0,- }- }- 0x3C => self.exi_clock_count,- 0x80..=0xFC => {- let i = (offset - 0x80) as usize;- u32::from_be_bytes([- self.io_buffer[i],- self.io_buffer[i + 1],- self.io_buffer[i + 2],- self.io_buffer[i + 3],- ])- }+ fn channel_reg_raw(&self, aligned_offset: u32) -> u32 {+ let ch = (aligned_offset / 0x0C) as usize;+ match aligned_offset % 0x0C {+ 0x00 => self.channels[ch].out,+ 0x04 => self.channels[ch].in_hi,+ 0x08 => self.channels[ch].in_lo,_ => 0,}}@@ -261,20 +133,20 @@ impl SerialInterface {if connected {match cmd {- // return 3-byte device ID+ // Return 3 byte device ID.0x00 | 0xFF => {self.io_buffer[0] = (GC_CONTROLLER_ID >> 24) as u8;self.io_buffer[1] = (GC_CONTROLLER_ID >> 16) as u8;self.io_buffer[2] = (GC_CONTROLLER_ID >> 8) as u8;}- // return current pad data (8 bytes)+ // Return current pad data (8 bytes).0x40 => {let hi = self.pad_state[channel].encode_hi();let lo = self.pad_state[channel].encode_lo();self.io_buffer[0..4].copy_from_slice(&hi.to_be_bytes());self.io_buffer[4..8].copy_from_slice(&lo.to_be_bytes());}- // return 10-byte origin data / recalibration payload+ // Return 10 byte origin data / recalibration payload.0x41 | 0x42 => {let origin = PadStatus::encode_origin();self.io_buffer[0..10].copy_from_slice(&origin);@@ -313,12 +185,93 @@ impl SerialInterface {}}-impl crate::gamecube::GameCube {- pub fn check_si_interrupts(&mut self) {- if self.si.interrupt_active() {- self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Si);+crate::mmio_device_dispatch! {+ read = si_regs_read,+ write = si_regs_write,+ registers = [+ regs::SiPoll,+ regs::SiComcsr,+ regs::SiStatusRegister,+ ],+}++#[inline(always)]+pub fn si_read(gc: &mut GameCube, phys: u32, size: u32) -> Option<u32> {+ let offset = phys - SI_BASE;++ if (0x80..=0xFF).contains(&offset) {+ let i = (offset - 0x80) as usize;+ return Some(match size {+ 1 => gc.si.io_buffer[i] as u32,+ 2 => u16::from_be_bytes([gc.si.io_buffer[i], gc.si.io_buffer[i + 1]]) as u32,+ 4 => u32::from_be_bytes([+ gc.si.io_buffer[i],+ gc.si.io_buffer[i + 1],+ gc.si.io_buffer[i + 2],+ gc.si.io_buffer[i + 3],+ ]),+ _ => return None,+ });+ }++ if (0x00..=0x2F).contains(&offset) || (0x3C..=0x3F).contains(&offset) {+ let aligned = offset & !3;+ let word = match aligned {+ 0x00..=0x2C => gc.si.read_channel_reg(aligned),+ 0x3C => gc.si.exi_clock_count,+ _ => return None,+ };+ return Some(crate::mmio::traits::read_be_subword(word, offset & 3, size));+ }++ self::si_regs_read(gc, phys, size)+}++#[inline(always)]+pub fn si_write(gc: &mut GameCube, phys: u32, size: u32, val: u32) -> bool {+ let offset = phys - SI_BASE;++ if (0x80..=0xFF).contains(&offset) {+ let i = (offset - 0x80) as usize;+ match size {+ 1 => gc.si.io_buffer[i] = val as u8,+ 2 => gc.si.io_buffer[i..i + 2].copy_from_slice(&(val as u16).to_be_bytes()),+ 4 => gc.si.io_buffer[i..i + 4].copy_from_slice(&val.to_be_bytes()),+ _ => return false,+ }+ return true;+ }++ if (0x00..=0x2F).contains(&offset) || (0x3C..=0x3F).contains(&offset) {+ let aligned = offset & !3;+ let merged = if size == 4 {+ val} else {- self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Si);+ let current = match aligned {+ 0x00..=0x2C => gc.si.channel_reg_raw(aligned),+ 0x3C => gc.si.exi_clock_count,+ _ => return false,+ };+ crate::mmio::traits::write_be_subword(current, offset & 3, size, val)+ };+ match aligned {+ 0x00..=0x2C => gc.si.write_channel_reg(aligned, merged),+ 0x3C => gc.si.exi_clock_count = merged,+ _ => return false,}+ return true;+ }++ self::si_regs_write(gc, phys, size, val)+}++#[inline(always)]+pub fn refresh_interrupts(gc: &mut GameCube) {+ use crate::flipper::pi::InterruptFlag;++ if gc.si.interrupt_active() {+ gc.pi.assert_interrupt(InterruptFlag::Si);+ } else {+ gc.pi.clear_interrupt(InterruptFlag::Si);}}diff --git a/crates/gecko/src/flipper/si/regs.rs b/crates/gecko/src/flipper/si/regs.rsindex b0b5ec6..42550b1 100644--- a/crates/gecko/src/flipper/si/regs.rs+++ b/crates/gecko/src/flipper/si/regs.rs@@ -1,25 +1,29 @@-use super::SerialInterface;-use crate::mmio::traits::MmioAccess;+use crate::flipper::si;+use crate::gamecube::GameCube;+use crate::mmio::traits::{MmioHandler, WriteMask};use chapa::BitEnum;-// 0xCC006430 4 R/W SIPOLL - SI Poll Register-crate::mmio_register! {- SiPoll: u32 @ 0xCC006430 => SerialInterface.poll {- #[bits(0..=3)]- pub vbcpy: u8,+// 0xCC006430 4 R/W SIPOLL (SI Poll Register)- #[bits(4..=7)]- pub enable: u8,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct SiPoll {+ #[bits(0..=3)]+ pub vbcpy: u8,- #[bits(8..=15)]- pub y_times: u8,+ #[bits(4..=7)]+ pub enable: u8,- #[bits(16..=25)]- pub x_lines: u16,- }+ #[bits(8..=15)]+ pub y_times: u8,++ #[bits(16..=25)]+ pub x_lines: u16,}+crate::mmio_reg!(SiPoll: u32 @ 0xCC006430);+crate::mmio_default_access!(SiPoll => GameCube.si.poll);-// 0xCC006434 4 R/W SICOMCSR - SI Communication Control Status Register+// 0xCC006434 4 R/W SICOMCSR (SI Communication Control Status Register)#[derive(BitEnum, Debug, PartialEq, Eq)]pub enum Channel {@@ -29,56 +33,57 @@ pub enum Channel {Channel3 = 3,}-crate::mmio_register! {- SiComcsr: u32 @ 0xCC006434 {- #[bits(0)]- pub tstart: bool,+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct SiComcsr {+ #[bits(0)]+ pub tstart: bool,- #[bits(1..=2)]- pub channel: Channel,+ #[bits(1..=2)]+ pub channel: Channel,- #[bits(6)]- pub callback_enable: bool,+ #[bits(6)]+ pub callback_enable: bool,- #[bits(7)]- pub command_enable: bool,+ #[bits(7)]+ pub command_enable: bool,- #[bits(8..=14)]- pub in_length: u8,+ #[bits(8..=14)]+ pub in_length: u8,- #[bits(16..=22)]- pub out_length: u8,+ #[bits(16..=22)]+ pub out_length: u8,- #[bits(24)]- pub channel_enable: bool,+ #[bits(24)]+ pub channel_enable: bool,- #[bits(25..=26)]- pub channel_number: u8,+ #[bits(25..=26)]+ pub channel_number: u8,- #[bits(27)]- pub rdst_interrupt_mask: bool,+ #[bits(27)]+ pub rdst_interrupt_mask: bool,- #[bits(28)]- pub rdst_interrupt: bool,+ #[bits(28)]+ pub rdst_interrupt: bool,- #[bits(29)]- pub com_error: bool,+ #[bits(29)]+ pub com_error: bool,- #[bits(30)]- pub tc_interrupt_mask: bool,+ #[bits(30)]+ pub tc_interrupt_mask: bool,- #[bits(31)]- pub tc_interrupt: bool,- }+ #[bits(31)]+ pub tc_interrupt: bool,}+crate::mmio_reg!(SiComcsr: u32 @ 0xCC006434);-impl MmioAccess<SerialInterface> for SiComcsr {- fn read(si: &SerialInterface) -> Self {- si.comcsr+impl MmioHandler<GameCube> for SiComcsr {+ fn read(gc: &mut GameCube) -> Self {+ gc.si.comcsr}- fn write(self, si: &mut SerialInterface) {- let mut csr = si.comcsr;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let mut csr = gc.si.comcsr;if self.tc_interrupt() {csr = csr.with_tc_interrupt(false);@@ -98,61 +103,90 @@ impl MmioAccess<SerialInterface> for SiComcsr {.with_channel_enable(self.channel_enable()).with_channel_number(self.channel_number());- si.comcsr = csr;+ gc.si.comcsr = csr;// Process SI buffer transfer when TSTART is writtenif self.tstart() {- si.run_si_buffer();+ gc.si.run_si_buffer();}++ si::refresh_interrupts(gc);}}-// 0xCC006438 4 R/W SISR - SI Status Register-crate::mmio_register! {- SiStatusRegister: u32 @ 0xCC006438 {- // Channel 3- #[bits(0)] pub unrun3: bool,- #[bits(1)] pub ovrun3: bool,- #[bits(2)] pub coll3: bool,- #[bits(3)] pub norep3: bool,- #[bits(4)] pub wrst3: bool,- #[bits(5)] pub rdst3: bool,-- // Channel 2- #[bits(8)] pub unrun2: bool,- #[bits(9)] pub ovrun2: bool,- #[bits(10)] pub coll2: bool,- #[bits(11)] pub norep2: bool,- #[bits(12)] pub wrst2: bool,- #[bits(13)] pub rdst2: bool,-- // Channel 1- #[bits(16)] pub unrun1: bool,- #[bits(17)] pub ovrun1: bool,- #[bits(18)] pub coll1: bool,- #[bits(19)] pub norep1: bool,- #[bits(20)] pub wrst1: bool,- #[bits(21)] pub rdst1: bool,-- // Channel 0- #[bits(24)] pub unrun0: bool,- #[bits(25)] pub ovrun0: bool,- #[bits(26)] pub coll0: bool,- #[bits(27)] pub norep0: bool,- #[bits(28)] pub wrst0: bool,- #[bits(29)] pub rdst0: bool,-- #[bits(31)] pub wr: bool,- }+// 0xCC006438 4 R/W SISR (SI Status Register)++#[chapa::bitfield(u32, order = lsb0)]+#[derive(Copy, Clone, Debug)]+pub struct SiStatusRegister {+ // Channel 3+ #[bits(0)]+ pub unrun3: bool,+ #[bits(1)]+ pub ovrun3: bool,+ #[bits(2)]+ pub coll3: bool,+ #[bits(3)]+ pub norep3: bool,+ #[bits(4)]+ pub wrst3: bool,+ #[bits(5)]+ pub rdst3: bool,++ // Channel 2+ #[bits(8)]+ pub unrun2: bool,+ #[bits(9)]+ pub ovrun2: bool,+ #[bits(10)]+ pub coll2: bool,+ #[bits(11)]+ pub norep2: bool,+ #[bits(12)]+ pub wrst2: bool,+ #[bits(13)]+ pub rdst2: bool,++ // Channel 1+ #[bits(16)]+ pub unrun1: bool,+ #[bits(17)]+ pub ovrun1: bool,+ #[bits(18)]+ pub coll1: bool,+ #[bits(19)]+ pub norep1: bool,+ #[bits(20)]+ pub wrst1: bool,+ #[bits(21)]+ pub rdst1: bool,++ // Channel 0+ #[bits(24)]+ pub unrun0: bool,+ #[bits(25)]+ pub ovrun0: bool,+ #[bits(26)]+ pub coll0: bool,+ #[bits(27)]+ pub norep0: bool,+ #[bits(28)]+ pub wrst0: bool,+ #[bits(29)]+ pub rdst0: bool,++ #[bits(31)]+ pub wr: bool,}+crate::mmio_reg!(SiStatusRegister: u32 @ 0xCC006438);-impl MmioAccess<SerialInterface> for SiStatusRegister {- fn read(si: &SerialInterface) -> Self {- si.status+impl MmioHandler<GameCube> for SiStatusRegister {+ fn read(gc: &mut GameCube) -> Self {+ gc.si.status}- fn write(self, si: &mut SerialInterface) {- let mut status = si.status;+ fn write(self, gc: &mut GameCube, _: WriteMask) {+ let mut status = gc.si.status;if self.norep0() {status = status.with_norep0(false);@@ -207,9 +241,9 @@ impl MmioAccess<SerialInterface> for SiStatusRegister {}if self.wr() {- si.status = status;- si.send_channel_commands();- status = si.status;+ gc.si.status = status;+ gc.si.send_channel_commands();+ status = gc.si.status;status = status.with_wr(false).with_wrst0(false)@@ -218,6 +252,7 @@ impl MmioAccess<SerialInterface> for SiStatusRegister {.with_wrst3(false);}- si.status = status;+ gc.si.status = status;+ si::refresh_interrupts(gc);}}diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rsindex 3da605a..f89a8f1 100644--- a/crates/gecko/src/gamecube.rs+++ b/crates/gecko/src/gamecube.rs@@ -233,7 +233,7 @@ impl GameCube {pub fn prepare_frame(&mut self) {self.vsync_pending = false;self.si.update_polling();- self.check_si_interrupts();+ crate::flipper::si::refresh_interrupts(self);}pub fn run_until_vsync(&mut self) {diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rsindex decdef2..dc28cfc 100644--- a/crates/gecko/src/mmio/bus.rs+++ b/crates/gecko/src/mmio/bus.rs@@ -252,15 +252,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_read_u8(offset),BusTarget::Mi => self.mi.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),- BusTarget::Ai => {- if offset == 0x08 {- return self.ai.sample_count(self.scheduler.cycles) as u8;- }- self.ai.mmio_read_u8(offset)- }+ BusTarget::Di => crate::dvd::di_read(self, phys, 1).unwrap_or(0) as u8,+ BusTarget::Si => crate::flipper::si::si_read(self, phys, 1).unwrap_or(0) as u8,+ BusTarget::Exi => crate::flipper::exi::exi_read(self, phys, 1).unwrap_or(0) as u8,+ BusTarget::Ai => crate::flipper::ai::ai_read(self, phys, 1).unwrap_or(0) as u8,BusTarget::Gx => {tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");0@@ -286,15 +281,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_read_u16(offset),BusTarget::Mi => self.mi.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),- BusTarget::Ai => {- if offset == 0x08 || offset == 0x0A {- return self.ai.sample_count(self.scheduler.cycles) as u16;- }- self.ai.mmio_read_u16(offset)- }+ BusTarget::Di => crate::dvd::di_read(self, phys, 2).unwrap_or(0) as u16,+ BusTarget::Si => crate::flipper::si::si_read(self, phys, 2).unwrap_or(0) as u16,+ BusTarget::Exi => crate::flipper::exi::exi_read(self, phys, 2).unwrap_or(0) as u16,+ BusTarget::Ai => crate::flipper::ai::ai_read(self, phys, 2).unwrap_or(0) as u16,BusTarget::Gx => {tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");0@@ -322,15 +312,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_read_u32(offset),BusTarget::Mi => self.mi.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),- BusTarget::Ai => {- if offset == 0x08 {- return self.ai.sample_count(self.scheduler.cycles);- }- self.ai.mmio_read_u32(offset)- }+ BusTarget::Di => crate::dvd::di_read(self, phys, 4).unwrap_or(0),+ BusTarget::Si => crate::flipper::si::si_read(self, phys, 4).unwrap_or(0),+ BusTarget::Exi => crate::flipper::exi::exi_read(self, phys, 4).unwrap_or(0),+ BusTarget::Ai => crate::flipper::ai::ai_read(self, phys, 4).unwrap_or(0),BusTarget::Gx => {tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");0@@ -363,26 +348,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_write_u8(offset, val),BusTarget::Mi => self.mi.mmio_write_u8(offset, val),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();- self.check_di_interrupts();- }- BusTarget::Si => {- self.si.mmio_write_u8(offset, val);- self.check_si_interrupts();- }- BusTarget::Exi => {- self.exi.mmio_write_u8(offset, val);- self.exi.process_cs_changes();- self.exi.process_dma_transfers(&mut self.mmio);- self.check_exi_interrupts();- }- BusTarget::Ai => {- self.ai.mmio_write_u8(offset, val);- self.check_sample_counter_reset();- self.check_ai_interrupts();- }+ BusTarget::Di => { crate::dvd::di_write(self, phys, 1, val as u32); }+ BusTarget::Si => { crate::flipper::si::si_write(self, phys, 1, val as u32); }+ BusTarget::Exi => { crate::flipper::exi::exi_write(self, phys, 1, val as u32); }+ BusTarget::Ai => { crate::flipper::ai::ai_write(self, phys, 1, val as u32); }BusTarget::Gx => {let wptr = self.pi.fifo_wptr as usize;self.mmio.ram[wptr] = val;@@ -420,26 +389,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_write_u16(offset, val),BusTarget::Mi => self.mi.mmio_write_u16(offset, val),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();- self.check_di_interrupts();- }- BusTarget::Si => {- self.si.mmio_write_u16(offset, val);- self.check_si_interrupts();- }- BusTarget::Exi => {- self.exi.mmio_write_u16(offset, val);- self.exi.process_cs_changes();- self.exi.process_dma_transfers(&mut self.mmio);- self.check_exi_interrupts();- }- BusTarget::Ai => {- self.ai.mmio_write_u16(offset, val);- self.check_sample_counter_reset();- self.check_ai_interrupts();- }+ BusTarget::Di => { crate::dvd::di_write(self, phys, 2, val as u32); }+ BusTarget::Si => { crate::flipper::si::si_write(self, phys, 2, val as u32); }+ BusTarget::Exi => { crate::flipper::exi::exi_write(self, phys, 2, val as u32); }+ BusTarget::Ai => { crate::flipper::ai::ai_write(self, phys, 2, val as u32); }BusTarget::Gx => {let wptr = self.pi.fifo_wptr as usize;let bytes = val.to_be_bytes();@@ -478,26 +431,10 @@ impl GameCube {BusTarget::Pi => self.pi.mmio_write_u32(offset, val),BusTarget::Mi => self.mi.mmio_write_u32(offset, val),BusTarget::Dsp => { crate::flipper::dsp::dsp_write(self, phys, 4, val); }- BusTarget::Di => {- self.di.mmio_write_u32(offset, val);- self.start_dvd_transfer();- self.check_di_interrupts();- }- BusTarget::Si => {- self.si.mmio_write_u32(offset, val);- self.check_si_interrupts();- }- BusTarget::Exi => {- self.exi.mmio_write_u32(offset, val);- self.exi.process_cs_changes();- self.exi.process_dma_transfers(&mut self.mmio);- self.check_exi_interrupts();- }- BusTarget::Ai => {- self.ai.mmio_write_u32(offset, val);- self.check_sample_counter_reset();- self.check_ai_interrupts();- }+ BusTarget::Di => { crate::dvd::di_write(self, phys, 4, val); }+ BusTarget::Si => { crate::flipper::si::si_write(self, phys, 4, val); }+ BusTarget::Exi => { crate::flipper::exi::exi_write(self, phys, 4, val); }+ BusTarget::Ai => { crate::flipper::ai::ai_write(self, phys, 4, val); }BusTarget::Gx => {let wptr = self.pi.fifo_wptr as usize;let bytes = val.to_be_bytes();diff --git a/crates/gecko/src/mmio/traits.rs b/crates/gecko/src/mmio/traits.rsindex 43a7e45..7267708 100644--- a/crates/gecko/src/mmio/traits.rs+++ b/crates/gecko/src/mmio/traits.rs@@ -72,6 +72,20 @@ pub trait MmioRw {}}+#[inline(always)]+pub const fn read_be_subword(word: u32, sub_offset: u32, size: u32) -> u32 {+ let shift = (4 - sub_offset - size) * 8;+ let mask = if size >= 4 { !0u32 } else { (1u32 << (size * 8)) - 1 };+ (word >> shift) & mask+}++#[inline(always)]+pub const fn write_be_subword(current: u32, sub_offset: u32, size: u32, val: u32) -> u32 {+ let shift = (4 - sub_offset - size) * 8;+ let mask = if size >= 4 { !0u32 } else { (1u32 << (size * 8)) - 1 };+ (current & !(mask << shift)) | ((val & mask) << shift)+}+pub trait MmioRegister: Sized {const ADDR: u32;const SIZE: usize;