A GameCube/Wii emulator and debugger written in Rust.
refactoring
Laylec95a8bdafa68
diff --git a/crates/gekko/src/exi.rs b/crates/gekko/src/exi.rsindex 37e5a6d..3655596 100644--- a/crates/gekko/src/exi.rs+++ b/crates/gekko/src/exi.rs@@ -1,35 +1,11 @@+pub mod device;pub mod regs;+use crate::exi::regs::TransferType;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 0pub ch0_csr: regs::Channel0Status,@@ -50,7 +26,7 @@ pub struct Exi {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],+ devices: [[Option<Box<dyn device::ExiDevice>>; 3]; 3],}impl Exi {@@ -79,13 +55,13 @@ impl Exi {let mut exi = Self::new();for ch in 0..3 {for slot in 0..3 {- exi.attach_device(ch, slot, Box::new(ExiDummy));+ exi.attach_device(ch, slot, Box::new(device::ExiDummy));}}exi}- pub fn attach_device(&mut self, channel: usize, slot: usize, device: Box<dyn ExiDevice>) {+ pub fn attach_device(&mut self, channel: usize, slot: usize, device: Box<dyn device::ExiDevice>) {self.devices[channel][slot] = Some(device);}@@ -149,9 +125,10 @@ impl Exi {// TODO: idkif let Some(device) = &mut self.devices[CHANNEL][slot] {for i in 0..size {- if transfer_type == 0 {+ if transfer_type == TransferType::Read {bytes[i] = 0;}+device.transfer_byte(&mut bytes[i]);}} else {@@ -162,7 +139,7 @@ impl Exi {self.finish_transfer_for_channel::<CHANNEL>();}- fn pending_dma(&self, channel: usize) -> Option<(u8, u8, u32, u32)> {+ 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>(),@@ -171,7 +148,7 @@ impl Exi {}}- fn pending_dma_for_channel<const CHANNEL: usize>(&self) -> Option<(u8, u8, u32, u32)> {+ 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(),@@ -215,6 +192,15 @@ impl Exi {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 => {@@ -224,12 +210,13 @@ impl Exi {}};- 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)),+ 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");+ }}}diff --git a/crates/gekko/src/exi/device.rs b/crates/gekko/src/exi/device.rsnew file mode 100644index 0000000..3169fec--- /dev/null+++ b/crates/gekko/src/exi/device.rs@@ -0,0 +1,25 @@+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;+ }+}\ No newline at end of filediff --git a/crates/gekko/src/exi/regs.rs b/crates/gekko/src/exi/regs.rsindex c79af17..b91bec0 100644--- a/crates/gekko/src/exi/regs.rs+++ b/crates/gekko/src/exi/regs.rs@@ -1,3 +1,5 @@+use chapa::BitEnum;+use super::Exi;use crate::mmio::traits::{MmioAccess, MmioRegister};@@ -15,6 +17,26 @@ fn write_csr<T: MmioRegister + Copy>(current: &mut T, new: T) {*current = T::from_raw(w1c_bits | ro_bits | rw_bits);}+/// Used for the RW field in EXI Control registers to specify transfer type+#[derive(BitEnum, PartialEq, Eq)]+pub enum TransferType {+ Read = 0b00,+ Write = 0b01,+ ReadAndWrite = 0b10,+ Reserved = 0b11,+}++impl std::fmt::Debug for TransferType {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ match self {+ TransferType::Read => write!(f, "read"),+ TransferType::Write => write!(f, "write"),+ TransferType::ReadAndWrite => write!(f, "read+write"),+ TransferType::Reserved => write!(f, "reserved"),+ }+ }+}+// --- Channel 0 ---// 0xCC006800 4 R/W EXI0CSR - EXI Channel 0 Status Register@@ -65,7 +87,7 @@ 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: u8,+ #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,#[bits(4..=5, alias = "tlen")] pub transfer_length: u8,}}@@ -140,7 +162,7 @@ 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: u8,+ #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,#[bits(4..=5, alias = "tlen")] pub transfer_length: u8,}}@@ -215,7 +237,7 @@ 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: u8,+ #[bits(2..=3, alias = "rw")] pub transfer_type: TransferType,#[bits(4..=5, alias = "tlen")] pub transfer_length: u8,}}diff --git a/crates/tinyemu/src/dump.rs b/crates/tinyemu/src/dump.rsindex 1692ff7..d4be361 100644--- a/crates/tinyemu/src/dump.rs+++ b/crates/tinyemu/src/dump.rs@@ -1,4 +1,5 @@use colored::Colorize;+use gekko::exi::regs::TransferType;use crate::snaptshot::CpuSnapshot;@@ -122,15 +123,6 @@ fn fmt_clock(clk: u8) -> &'static str {}}-fn fmt_transfer_type(rw: u8) -> &'static str {- match rw {- 0 => "read",- 1 => "write",- 2 => "read+write",- _ => "undefined",- }-}-fn fmt_enabled(val: bool) -> &'static str {if val { "yes" } else { "no" }}@@ -157,7 +149,7 @@ struct ExiChannelView {dma_length: u32,transfer_start: bool,dma_mode: bool,- transfer_type: u8,+ transfer_type: TransferType,transfer_length: u8,immediate_data: u32,}@@ -195,7 +187,7 @@ fn exi_channel(ch: &ExiChannelView) {" Transfer Mode: {}",if ch.dma_mode { "DMA" } else { "immediate" });- println!(" Transfer Direction: {}", fmt_transfer_type(ch.transfer_type));+ println!(" Transfer Direction: {:?}", ch.transfer_type);println!(" Transfer Size: {} byte(s)", ch.transfer_length as u32 + 1);println!(" Immediate Data: 0x{:08X}", ch.immediate_data);}