Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

exi stuff

Laylee124cd1443b9
diff --git a/crates/gekko/src/exi.rs b/crates/gekko/src/exi.rs
index 34aeed0..37e5a6d 100644
--- a/crates/gekko/src/exi.rs
+++ b/crates/gekko/src/exi.rs
@@ -1,8 +1,35 @@
pub mod regs;
+use crate::mmio::Mmio;
use crate::mmio::constants::EXI_BASE;
use crate::mmio::traits::{MmioAccess, MmioRegister};
+pub trait ExiDevice {
+ fn transfer_byte(&mut self, byte: &mut u8);
+
+ fn dma_read(&mut self, buf: &mut [u8]) {
+ for b in buf.iter_mut() {
+ *b = 0;
+ self.transfer_byte(b);
+ }
+ }
+
+ fn dma_write(&mut self, buf: &[u8]) {
+ for b in buf {
+ let mut b = *b;
+ self.transfer_byte(&mut b);
+ }
+ }
+}
+
+pub struct ExiDummy;
+
+impl ExiDevice for ExiDummy {
+ fn transfer_byte(&mut self, byte: &mut u8) {
+ *byte = 0;
+ }
+}
+
pub struct Exi {
// Channel 0
pub ch0_csr: regs::Channel0Status,
@@ -22,6 +49,8 @@ pub struct Exi {
pub ch2_length: regs::Channel2DmaLength,
pub ch2_cr: regs::Channel2Control,
pub ch2_data: regs::Channel2Data,
+ // Devices: [channel][device_slot], 3 channels x 3 slots
+ devices: [[Option<Box<dyn ExiDevice>>; 3]; 3],
}
impl Exi {
@@ -42,6 +71,205 @@ impl Exi {
ch2_length: regs::Channel2DmaLength::from_raw(0),
ch2_cr: regs::Channel2Control::from_raw(0),
ch2_data: regs::Channel2Data::from_raw(0),
+ devices: [[None, None, None], [None, None, None], [None, None, None]],
+ }
+ }
+
+ pub fn dummy() -> Self {
+ let mut exi = Self::new();
+ for ch in 0..3 {
+ for slot in 0..3 {
+ exi.attach_device(ch, slot, Box::new(ExiDummy));
+ }
+ }
+ exi
+ }
+
+ pub fn attach_device(&mut self, channel: usize, slot: usize, device: Box<dyn ExiDevice>) {
+ self.devices[channel][slot] = Some(device);
+ }
+
+ /// Decode chip_select field to device slot index (0, 1, or 2)
+ fn cs_to_slot(cs: u8) -> Option<usize> {
+ match cs {
+ 0b001 => Some(0),
+ 0b010 => Some(1),
+ 0b100 => Some(2),
+ _ => None,
+ }
+ }
+
+ 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!(),
+ }
+ }
+
+ fn start_immediate_transfer_for_channel<const CHANNEL: usize>(&mut self) {
+ let (transfer_type, transfer_length, chip_select, mut bytes) = match CHANNEL {
+ 0 => (
+ self.ch0_cr.transfer_type(),
+ self.ch0_cr.transfer_length(),
+ self.ch0_csr.chip_select(),
+ self.ch0_data.raw().to_be_bytes(),
+ ),
+ 1 => (
+ self.ch1_cr.transfer_type(),
+ self.ch1_cr.transfer_length(),
+ self.ch1_csr.chip_select(),
+ self.ch1_data.raw().to_be_bytes(),
+ ),
+ 2 => (
+ self.ch2_cr.transfer_type(),
+ self.ch2_cr.transfer_length(),
+ self.ch2_csr.chip_select(),
+ self.ch2_data.raw().to_be_bytes(),
+ ),
+ _ => unreachable!(),
+ };
+
+ let slot = match Self::cs_to_slot(chip_select) {
+ Some(s) => s,
+ None => {
+ tracing::warn!(
+ channel = CHANNEL,
+ cs = chip_select,
+ "EXI immediate transfer with no/invalid chip select"
+ );
+ self.finish_transfer_for_channel::<CHANNEL>();
+ return;
+ }
+ };
+
+ let size = (transfer_length as usize) + 1;
+
+ // TODO: idk
+ if let Some(device) = &mut self.devices[CHANNEL][slot] {
+ for i in 0..size {
+ if transfer_type == 0 {
+ 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, u8, 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, u8, 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))
+ }
+
+ 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;
+ };
+
+ 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;
+ }
+ };
+
+ let len = length as usize;
+ if let Some(device) = &mut self.devices[channel][slot] {
+ match transfer_type {
+ 0 => device.dma_read(mmio.phys_slice_mut(address, len)),
+ 1 => device.dma_write(mmio.phys_slice(address, len)),
+ _ => device.dma_read(mmio.phys_slice_mut(address, len)),
+ }
+ }
+
+ 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) {
+ match CHANNEL {
+ 0 => self.ch0_data = regs::Channel0Data::from_raw(val),
+ 1 => self.ch1_data = regs::Channel1Data::from_raw(val),
+ 2 => self.ch2_data = regs::Channel2Data::from_raw(val),
+ _ => unreachable!(),
+ }
+ }
+
+ fn finish_transfer_for_channel<const CHANNEL: usize>(&mut self) {
+ match CHANNEL {
+ 0 => {
+ self.ch0_cr.set_transfer_start(false);
+ self.ch0_csr.set_tc_interrupt(true);
+ }
+ 1 => {
+ self.ch1_cr.set_transfer_start(false);
+ self.ch1_csr.set_tc_interrupt(true);
+ }
+ 2 => {
+ self.ch2_cr.set_transfer_start(false);
+ self.ch2_csr.set_tc_interrupt(true);
+ }
+ _ => unreachable!(),
}
}
diff --git a/crates/gekko/src/exi/regs.rs b/crates/gekko/src/exi/regs.rs
index 1f6dd2c..c79af17 100644
--- a/crates/gekko/src/exi/regs.rs
+++ b/crates/gekko/src/exi/regs.rs
@@ -1,11 +1,26 @@
use super::Exi;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+/// Write-1-to-clear helper for EXI CSR registers
+/// Bits 1 (EXIINT), 3 (TCINT), 11 (EXTINT) are write-1-to-clear
+/// Bit 12 (EXT) is read-only (device presence)
+fn write_csr<T: MmioRegister + Copy>(current: &mut T, new: T) {
+ let cur_raw = (*current).to_raw();
+ let new_raw = new.to_raw();
+ const W1C_MASK: u32 = (1 << 1) | (1 << 3) | (1 << 11);
+ const RO_MASK: u32 = 1 << 12;
+ let w1c_bits = (cur_raw & W1C_MASK) & !(new_raw & W1C_MASK);
+ let ro_bits = cur_raw & RO_MASK;
+ let rw_bits = new_raw & !(W1C_MASK | RO_MASK);
+ *current = T::from_raw(w1c_bits | ro_bits | rw_bits);
+}
// --- Channel 0 ---
// 0xCC006800 4 R/W EXI0CSR - EXI Channel 0 Status Register
crate::mmio_register! {
- Channel0Status: u32 @ 0xCC006800 => Exi.ch0_csr {
+ 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,
@@ -19,6 +34,15 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel0Status {
+ fn read(exi: &Exi) -> Self {
+ exi.ch0_csr
+ }
+ fn write(self, exi: &mut Exi) {
+ write_csr(&mut exi.ch0_csr, self);
+ }
+}
+
// 0xCC006804 4 R/W EXI0MAR - EXI Channel 0 DMA Start Address
crate::mmio_register! {
@@ -38,7 +62,7 @@ crate::mmio_register! {
// 0xCC00680C 4 R/W EXI0CR - EXI Channel 0 Control Register
crate::mmio_register! {
- Channel0Control: u32 @ 0xCC00680C => Exi.ch0_cr {
+ 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: u8,
@@ -46,6 +70,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel0Control {
+ fn read(exi: &Exi) -> Self {
+ exi.ch0_cr
+ }
+ fn write(self, exi: &mut Exi) {
+ 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);
+ }
+ }
+}
+
// 0xCC006810 4 R/W EXI0DATA - EXI Channel 0 Immediate Data
crate::mmio_register! {
@@ -59,7 +96,7 @@ crate::mmio_register! {
// 0xCC006814 4 R/W EXI1CSR - EXI Channel 1 Status Register
crate::mmio_register! {
- Channel1Status: u32 @ 0xCC006814 => Exi.ch1_csr {
+ 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,
@@ -72,6 +109,15 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel1Status {
+ fn read(exi: &Exi) -> Self {
+ exi.ch1_csr
+ }
+ fn write(self, exi: &mut Exi) {
+ write_csr(&mut exi.ch1_csr, self);
+ }
+}
+
// 0xCC006818 4 R/W EXI1MAR - EXI Channel 1 DMA Start Address
crate::mmio_register! {
@@ -91,7 +137,7 @@ crate::mmio_register! {
// 0xCC006820 4 R/W EXI1CR - EXI Channel 1 Control Register
crate::mmio_register! {
- Channel1Control: u32 @ 0xCC006820 => Exi.ch1_cr {
+ 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: u8,
@@ -99,6 +145,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel1Control {
+ fn read(exi: &Exi) -> Self {
+ exi.ch1_cr
+ }
+ fn write(self, exi: &mut Exi) {
+ 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);
+ }
+ }
+}
+
// 0xCC006824 4 R/W EXI1DATA - EXI Channel 1 Immediate Data
crate::mmio_register! {
@@ -112,7 +171,7 @@ crate::mmio_register! {
// 0xCC006828 4 R/W EXI2CSR - EXI Channel 2 Status Register
crate::mmio_register! {
- Channel2Status: u32 @ 0xCC006828 => Exi.ch2_csr {
+ 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,
@@ -125,6 +184,15 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel2Status {
+ fn read(exi: &Exi) -> Self {
+ exi.ch2_csr
+ }
+ fn write(self, exi: &mut Exi) {
+ write_csr(&mut exi.ch2_csr, self);
+ }
+}
+
// 0xCC00682C 4 R/W EXI2MAR - EXI Channel 2 DMA Start Address
crate::mmio_register! {
@@ -144,7 +212,7 @@ crate::mmio_register! {
// 0xCC006834 4 R/W EXI2CR - EXI Channel 2 Control Register
crate::mmio_register! {
- Channel2Control: u32 @ 0xCC006834 => Exi.ch2_cr {
+ 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: u8,
@@ -152,6 +220,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Exi> for Channel2Control {
+ fn read(exi: &Exi) -> Self {
+ exi.ch2_cr
+ }
+ fn write(self, exi: &mut Exi) {
+ 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);
+ }
+ }
+}
+
// 0xCC006838 4 R/W EXI2DATA - EXI Channel 2 Immediate Data
crate::mmio_register! {
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index 67d1634..13f47d1 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -52,7 +52,6 @@ impl Gekko {
let addr = bss_start + i;
mmio.virt_write_u8(addr, 0);
}
-
Gekko {
cpu: Cpu::new(exe.entry_point()),
scheduler: Scheduler { cycles: 0 },
@@ -61,7 +60,7 @@ impl Gekko {
pi: Pi::new(),
mi: Mi::new(),
dsp: Dsp::new(),
- exi: Exi::new(),
+ exi: Exi::dummy(),
ai: Ai::new(),
}
}
diff --git a/crates/gekko/src/mmio.rs b/crates/gekko/src/mmio.rs
index b29aff9..3b75190 100644
--- a/crates/gekko/src/mmio.rs
+++ b/crates/gekko/src/mmio.rs
@@ -153,6 +153,11 @@ impl Mmio {
&slice[offset..offset + len]
}
+ pub fn phys_slice_mut(&mut self, addr: u32, len: usize) -> &mut [u8] {
+ let (slice, offset) = self.resolve_mut(addr);
+ &mut slice[offset..offset + len]
+ }
+
/// Return a slice of virtual memory starting at `addr` with length `len`
/// This is just a thin wrapper around `phys_slice` that applies virtual-to-physical translation
pub fn virt_slice(&self, addr: u32, len: usize) -> &[u8] {
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 87afe36..d8862cf 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -82,7 +82,10 @@ impl Gekko {
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),
- BusTarget::Exi => self.exi.mmio_write_u8(offset, val),
+ BusTarget::Exi => {
+ self.exi.mmio_write_u8(offset, val);
+ self.exi.process_dma_transfers(&mut self.mmio);
+ }
BusTarget::Ai => self.ai.mmio_write_u8(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),
}
@@ -96,7 +99,10 @@ impl Gekko {
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),
- BusTarget::Exi => self.exi.mmio_write_u16(offset, val),
+ BusTarget::Exi => {
+ self.exi.mmio_write_u16(offset, val);
+ self.exi.process_dma_transfers(&mut self.mmio);
+ }
BusTarget::Ai => self.ai.mmio_write_u16(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),
}
@@ -110,7 +116,10 @@ impl Gekko {
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),
- BusTarget::Exi => self.exi.mmio_write_u32(offset, val),
+ BusTarget::Exi => {
+ self.exi.mmio_write_u32(offset, val);
+ self.exi.process_dma_transfers(&mut self.mmio);
+ }
BusTarget::Ai => self.ai.mmio_write_u32(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u32(offset, val),
}