Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

implement all devices that created MMIO error/warn logs, focus on IRQ. polling loop doesnt get stuck anymore, it gets past once retrace writes to it (via DI irq)

Layled335d04d7827
diff --git a/crates/gekko/src/exi.rs b/crates/gekko/src/exi.rs
index 3e608c7..ef90bb9 100644
--- a/crates/gekko/src/exi.rs
+++ b/crates/gekko/src/exi.rs
@@ -1,6 +1,6 @@
pub mod device;
-pub mod regs;
pub mod macronix;
+pub mod regs;
use crate::exi::regs::TransferType;
use crate::mmio::Mmio;
@@ -341,4 +341,26 @@ impl Exi {
tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI write_u32");
}
}
+
+ 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())
+ }
+}
+
+impl crate::gekko::Gekko {
+ 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);
+ }
+ }
}
diff --git a/crates/gekko/src/exi/device.rs b/crates/gekko/src/exi/device.rs
index fb83392..7f62ad5 100644
--- a/crates/gekko/src/exi/device.rs
+++ b/crates/gekko/src/exi/device.rs
@@ -23,4 +23,4 @@ impl ExiDevice for ExiDummy {
fn transfer_byte(&mut self, byte: &mut u8) {
*byte = 0;
}
-}
\ No newline at end of file
+}
diff --git a/crates/gekko/src/exi/regs.rs b/crates/gekko/src/exi/regs.rs
index 75ba027..920591b 100644
--- a/crates/gekko/src/exi/regs.rs
+++ b/crates/gekko/src/exi/regs.rs
@@ -3,6 +3,28 @@ use chapa::BitEnum;
use super::Exi;
use crate::mmio::traits::{MmioAccess, MmioRegister};
+pub trait ChannelStatus {
+ fn exi_interrupt(&self) -> bool;
+ fn exi_interrupt_mask(&self) -> bool;
+ fn tc_interrupt(&self) -> bool;
+ fn tc_interrupt_mask(&self) -> bool;
+ fn ext_interrupt(&self) -> bool;
+ fn ext_interrupt_mask(&self) -> bool;
+}
+
+macro_rules! impl_channel_status {
+ ($($ty:ty),*) => {
+ $(impl ChannelStatus for $ty {
+ fn exi_interrupt(&self) -> bool { Self::exi_interrupt(self) }
+ fn exi_interrupt_mask(&self) -> bool { Self::exi_interrupt_mask(self) }
+ fn tc_interrupt(&self) -> bool { Self::tc_interrupt(self) }
+ fn tc_interrupt_mask(&self) -> bool { Self::tc_interrupt_mask(self) }
+ fn ext_interrupt(&self) -> bool { Self::ext_interrupt(self) }
+ fn ext_interrupt_mask(&self) -> bool { Self::ext_interrupt_mask(self) }
+ })*
+ };
+}
+
/// 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)
@@ -211,6 +233,8 @@ impl MmioAccess<Exi> for Channel2Status {
}
}
+impl_channel_status!(Channel0Status, Channel1Status, Channel2Status);
+
// 0xCC00682C 4 R/W EXI2MAR - EXI Channel 2 DMA Start Address
crate::mmio_register! {
diff --git a/crates/gekko/src/flipper.rs b/crates/gekko/src/flipper.rs
index b583eef..341ffa1 100644
--- a/crates/gekko/src/flipper.rs
+++ b/crates/gekko/src/flipper.rs
@@ -1,5 +1,10 @@
+pub mod ai;
+pub mod cp;
+pub mod di;
pub mod dsp;
pub mod gx;
+pub mod mi;
pub mod pe;
pub mod pi;
+pub mod si;
pub mod vi;
diff --git a/crates/gekko/src/flipper/ai.rs b/crates/gekko/src/flipper/ai.rs
new file mode 100644
index 0000000..7e723c5
--- /dev/null
+++ b/crates/gekko/src/flipper/ai.rs
@@ -0,0 +1,128 @@
+pub mod regs;
+
+use crate::mmio::constants::AI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+/// CPU cycles per AI sample at 32kHz: 486MHz / 32000 = 15187
+const CYCLES_PER_SAMPLE_32K: u64 = 15187;
+/// CPU cycles per AI sample at 48kHz: 486MHz / 48000 = 10125
+const CYCLES_PER_SAMPLE_48K: u64 = 10125;
+
+pub struct Ai {
+ pub control: regs::AiControl,
+ pub volume: regs::AiVolume,
+ pub sample_counter: regs::AiSampleCounter,
+ pub interrupt_timing: regs::AiInterruptTiming,
+ pub sample_counter_base_cycle: u64,
+ pub sample_counter_reset_pending: bool,
+}
+
+impl Ai {
+ pub fn new() -> Self {
+ Self {
+ control: regs::AiControl::from_raw(0),
+ volume: regs::AiVolume::from_raw(0),
+ sample_counter: regs::AiSampleCounter::from_raw(0),
+ interrupt_timing: regs::AiInterruptTiming::from_raw(0),
+ sample_counter_base_cycle: 0,
+ sample_counter_reset_pending: false,
+ }
+ }
+
+ pub fn interrupt_active(&self) -> bool {
+ self.control.interrupt() && self.control.interrupt_mask()
+ }
+
+ /// Compute the current sample counter based on elapsed cycles
+ pub fn sample_count(&self, current_cycles: u64) -> u32 {
+ if self.control.playback_status() != regs::Status::Play {
+ return 0;
+ }
+
+ let elapsed = current_cycles.saturating_sub(self.sample_counter_base_cycle);
+ let cycles_per_sample = match self.control.sample_rate() {
+ regs::SampleRate::Rate32KHz => CYCLES_PER_SAMPLE_32K,
+ regs::SampleRate::Rate48KHz => CYCLES_PER_SAMPLE_48K,
+ };
+ (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);
+ }
+ }
+
+ crate::impl_mmio_dispatch!(
+ regs::AiControl,
+ regs::AiVolume,
+ regs::AiInterruptTiming,
+ regs::AiSampleCounter,
+ );
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
+ self.read_raw(AI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(AI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ self.read_raw(AI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(AI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(AI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(AI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u32");
+ }
+ }
+}
+
+impl crate::gekko::Gekko {
+ 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);
+ }
+ }
+
+ 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;
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/ai/regs.rs b/crates/gekko/src/flipper/ai/regs.rs
new file mode 100644
index 0000000..f3247ba
--- /dev/null
+++ b/crates/gekko/src/flipper/ai/regs.rs
@@ -0,0 +1,111 @@
+use super::Ai;
+use crate::mmio::traits::MmioAccess;
+use chapa::BitEnum;
+
+// 0xCC006C00 4 R/W AICR - Audio Interface Control Register
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum Status {
+ StopOrPause = 0,
+ Play = 1,
+}
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum SampleRate {
+ Rate48KHz = 0,
+ Rate32KHz = 1,
+}
+
+crate::mmio_register! {
+ AiControl: u32 @ 0xCC006C00 {
+ #[bits(0, alias = "pstat")]
+ pub playback_status: Status,
+
+ #[bits(1, alias = "afr")]
+ pub sample_rate: SampleRate,
+
+ #[bits(2, alias = "aiintmsk")]
+ pub interrupt_mask: bool,
+
+ #[bits(3, alias = "aiint")]
+ pub interrupt: bool,
+
+ #[bits(4, alias = "aiintvld")]
+ pub interrupt_valid: bool,
+
+ #[bits(5, alias = "screset")]
+ pub sample_counter_reset: bool,
+
+ #[bits(6)]
+ pub dsp_sample_rate: bool,
+ }
+}
+
+impl MmioAccess<Ai> for AiControl {
+ fn read(ai: &Ai) -> Self {
+ ai.control
+ }
+
+ fn write(self, ai: &mut Ai) {
+ let mut cr = ai.control;
+
+ // AIINT is w1c
+ if self.interrupt() {
+ cr = cr.with_interrupt(false);
+ }
+
+ // Passthrough
+ cr = cr
+ .with_playback_status(self.playback_status())
+ .with_sample_rate(self.sample_rate())
+ .with_interrupt_mask(self.interrupt_mask())
+ .with_interrupt_valid(self.interrupt_valid())
+ .with_dsp_sample_rate(self.dsp_sample_rate());
+
+ // SCRESET resets the sample counter
+ if self.sample_counter_reset() {
+ ai.sample_counter_reset_pending = true;
+ ai.sample_counter = AiSampleCounter::from_raw(0);
+ }
+
+ ai.control = cr;
+ }
+}
+
+// 0xCC006C04 4 R/W AIVR - Audio Interface Volume Register
+crate::mmio_register! {
+ AiVolume: u32 @ 0xCC006C04 => Ai.volume {
+ #[bits(0..=7)]
+ pub left: u8,
+
+ #[bits(8..=15)]
+ pub right: u8,
+ }
+}
+
+// 0xCC006C08 4 R AISCNT - Audio Interface Sample Counter
+
+crate::mmio_register! {
+ AiSampleCounter: u32 @ 0xCC006C08 {
+ #[bits(0..=30)]
+ pub sample_count: u32,
+ }
+}
+
+impl MmioAccess<Ai> for AiSampleCounter {
+ fn read(ai: &Ai) -> Self {
+ ai.sample_counter
+ }
+
+ fn write(self, _ai: &mut Ai) {
+ tracing::warn!("attempted to write to read-only AiSampleCounter");
+ }
+}
+
+// 0xCC006C0C 4 R/W AIIT - Audio Interface Interrupt Timing
+crate::mmio_register! {
+ AiInterruptTiming: u32 @ 0xCC006C0C => Ai.interrupt_timing {
+ #[bits(0..=30)]
+ pub sample_count: u32,
+ }
+}
diff --git a/crates/gekko/src/flipper/cp.rs b/crates/gekko/src/flipper/cp.rs
new file mode 100644
index 0000000..907355d
--- /dev/null
+++ b/crates/gekko/src/flipper/cp.rs
@@ -0,0 +1,121 @@
+pub mod regs;
+
+use crate::mmio::constants::CP_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Cp {
+ pub status: regs::CpStatus,
+ pub control: regs::CpControl,
+ pub fifo_base_lo: regs::FifoBaseLo,
+ pub fifo_base_hi: regs::FifoBaseHi,
+ pub fifo_end_lo: regs::FifoEndLo,
+ pub fifo_end_hi: regs::FifoEndHi,
+ pub fifo_hi_watermark_lo: regs::FifoHiWatermarkLo,
+ pub fifo_hi_watermark_hi: regs::FifoHiWatermarkHi,
+ pub fifo_lo_watermark_lo: regs::FifoLoWatermarkLo,
+ pub fifo_lo_watermark_hi: regs::FifoLoWatermarkHi,
+ pub fifo_rw_distance_lo: regs::FifoRwDistanceLo,
+ pub fifo_rw_distance_hi: regs::FifoRwDistanceHi,
+ pub fifo_write_ptr_lo: regs::FifoWritePtrLo,
+ pub fifo_write_ptr_hi: regs::FifoWritePtrHi,
+ pub fifo_read_ptr_lo: regs::FifoReadPtrLo,
+ pub fifo_read_ptr_hi: regs::FifoReadPtrHi,
+}
+
+impl Cp {
+ pub fn new() -> Self {
+ Self {
+ status: regs::CpStatus::from_raw(0),
+ control: regs::CpControl::from_raw(0),
+ fifo_base_lo: regs::FifoBaseLo::from_raw(0),
+ fifo_base_hi: regs::FifoBaseHi::from_raw(0),
+ fifo_end_lo: regs::FifoEndLo::from_raw(0),
+ fifo_end_hi: regs::FifoEndHi::from_raw(0),
+ fifo_hi_watermark_lo: regs::FifoHiWatermarkLo::from_raw(0),
+ fifo_hi_watermark_hi: regs::FifoHiWatermarkHi::from_raw(0),
+ fifo_lo_watermark_lo: regs::FifoLoWatermarkLo::from_raw(0),
+ fifo_lo_watermark_hi: regs::FifoLoWatermarkHi::from_raw(0),
+ fifo_rw_distance_lo: regs::FifoRwDistanceLo::from_raw(0),
+ fifo_rw_distance_hi: regs::FifoRwDistanceHi::from_raw(0),
+ fifo_write_ptr_lo: regs::FifoWritePtrLo::from_raw(0),
+ fifo_write_ptr_hi: regs::FifoWritePtrHi::from_raw(0),
+ fifo_read_ptr_lo: regs::FifoReadPtrLo::from_raw(0),
+ fifo_read_ptr_hi: regs::FifoReadPtrHi::from_raw(0),
+ }
+ }
+
+ pub fn interrupt_active(&self) -> bool {
+ (self.status.bp_interrupt() && self.control.bp_interrupt_enable())
+ || (self.status.fifo_overflow() && self.control.fifo_overflow_interrupt_enable())
+ || (self.status.fifo_underflow() && self.control.fifo_underflow_interrupt_enable())
+ }
+
+ crate::impl_mmio_dispatch!(
+ regs::CpStatus,
+ regs::CpControl,
+ regs::CpClear,
+ regs::FifoBaseLo,
+ regs::FifoBaseHi,
+ regs::FifoEndLo,
+ regs::FifoEndHi,
+ regs::FifoHiWatermarkLo,
+ regs::FifoHiWatermarkHi,
+ regs::FifoLoWatermarkLo,
+ regs::FifoLoWatermarkHi,
+ regs::FifoRwDistanceLo,
+ regs::FifoRwDistanceHi,
+ regs::FifoWritePtrLo,
+ regs::FifoWritePtrHi,
+ regs::FifoReadPtrLo,
+ regs::FifoReadPtrHi,
+ );
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
+ self.read_raw(CP_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(CP_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ self.read_raw(CP_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(CP_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(CP_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(CP_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled CP write_u32");
+ }
+ }
+}
+
+impl crate::gekko::Gekko {
+ pub fn check_cp_interrupts(&mut self) {
+ if self.cp.interrupt_active() {
+ self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Cp);
+ } else {
+ self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Cp);
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/cp/regs.rs b/crates/gekko/src/flipper/cp/regs.rs
new file mode 100644
index 0000000..b94ae53
--- /dev/null
+++ b/crates/gekko/src/flipper/cp/regs.rs
@@ -0,0 +1,91 @@
+use super::Cp;
+use crate::mmio::traits::MmioAccess;
+
+// 0xCC000000 2 R/W CP_STATUS - CP Status Register
+
+crate::mmio_register! {
+ CpStatus: u16 @ 0xCC000000 => Cp.status {
+ #[bits(0)]
+ pub fifo_overflow: bool,
+
+ #[bits(1)]
+ pub fifo_underflow: bool,
+
+ #[bits(2)]
+ pub read_idle: bool,
+
+ #[bits(3)]
+ pub cmd_idle: bool,
+
+ #[bits(4)]
+ pub bp_interrupt: bool,
+ }
+}
+
+// 0xCC000002 2 R/W CP_CTRL - CP Control Register
+
+crate::mmio_register! {
+ CpControl: u16 @ 0xCC000002 => Cp.control {
+ #[bits(0)]
+ pub gp_fifo_read_enable: bool,
+
+ #[bits(1)]
+ pub cp_interrupt_enable: bool,
+
+ #[bits(2)]
+ pub fifo_overflow_interrupt_enable: bool,
+
+ #[bits(3)]
+ pub fifo_underflow_interrupt_enable: bool,
+
+ #[bits(4)]
+ pub gp_link_enable: bool,
+
+ #[bits(5)]
+ pub bp_interrupt_enable: bool,
+ }
+}
+
+// 0xCC000004 2 W Clear Register
+
+crate::mmio_register! {
+ CpClear: u16 @ 0xCC000004 {
+ #[bits(0)]
+ pub clear_overflow: bool,
+
+ #[bits(1)]
+ pub clear_underflow: bool,
+ }
+}
+
+impl MmioAccess<Cp> for CpClear {
+ fn read(_cp: &Cp) -> Self {
+ tracing::warn!("attempted to read from write-only CpClear register");
+ Self::from_raw(0)
+ }
+
+ fn write(self, cp: &mut Cp) {
+ if self.clear_overflow() {
+ cp.status = cp.status.with_fifo_overflow(false);
+ }
+
+ if self.clear_underflow() {
+ cp.status = cp.status.with_fifo_underflow(false);
+ }
+ }
+}
+
+crate::mmio_register! { FifoBaseLo: u16 @ 0xCC000020 => Cp.fifo_base_lo {} }
+crate::mmio_register! { FifoBaseHi: u16 @ 0xCC000022 => Cp.fifo_base_hi {} }
+crate::mmio_register! { FifoEndLo: u16 @ 0xCC000024 => Cp.fifo_end_lo {} }
+crate::mmio_register! { FifoEndHi: u16 @ 0xCC000026 => Cp.fifo_end_hi {} }
+crate::mmio_register! { FifoHiWatermarkLo: u16 @ 0xCC000028 => Cp.fifo_hi_watermark_lo {} }
+crate::mmio_register! { FifoHiWatermarkHi: u16 @ 0xCC00002A => Cp.fifo_hi_watermark_hi {} }
+crate::mmio_register! { FifoLoWatermarkLo: u16 @ 0xCC00002C => Cp.fifo_lo_watermark_lo {} }
+crate::mmio_register! { FifoLoWatermarkHi: u16 @ 0xCC00002E => Cp.fifo_lo_watermark_hi {} }
+crate::mmio_register! { FifoRwDistanceLo: u16 @ 0xCC000030 => Cp.fifo_rw_distance_lo {} }
+crate::mmio_register! { FifoRwDistanceHi: u16 @ 0xCC000032 => Cp.fifo_rw_distance_hi {} }
+crate::mmio_register! { FifoWritePtrLo: u16 @ 0xCC000034 => Cp.fifo_write_ptr_lo {} }
+crate::mmio_register! { FifoWritePtrHi: u16 @ 0xCC000036 => Cp.fifo_write_ptr_hi {} }
+crate::mmio_register! { FifoReadPtrLo: u16 @ 0xCC000038 => Cp.fifo_read_ptr_lo {} }
+crate::mmio_register! { FifoReadPtrHi: u16 @ 0xCC00003A => Cp.fifo_read_ptr_hi {} }
diff --git a/crates/gekko/src/flipper/di.rs b/crates/gekko/src/flipper/di.rs
new file mode 100644
index 0000000..ad1d130
--- /dev/null
+++ b/crates/gekko/src/flipper/di.rs
@@ -0,0 +1,76 @@
+pub mod regs;
+
+use crate::mmio::constants::DI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Di {
+ pub status: regs::DiStatusRegister,
+ pub cover: regs::DiCoverRegister,
+}
+
+impl Di {
+ pub fn new() -> Self {
+ Self {
+ status: regs::DiStatusRegister::from_raw(0),
+ cover: regs::DiCoverRegister::from_raw(0),
+ }
+ }
+
+ pub fn interrupt_active(&self) -> bool {
+ (self.status.break_complete() && self.status.break_complete_mask())
+ || (self.status.device_error() && self.status.device_error_mask())
+ || (self.status.transfer_complete() && self.status.transfer_complete_mask())
+ || (self.cover.cover_interrupt() && self.cover.cover_interrupt_mask())
+ }
+
+ crate::impl_mmio_dispatch!(regs::DiStatusRegister, regs::DiCoverRegister,);
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
+ self.read_raw(DI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(DI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ self.read_raw(DI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(DI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(DI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(DI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DI write_u32");
+ }
+ }
+}
+
+impl crate::gekko::Gekko {
+ 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);
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/di/regs.rs b/crates/gekko/src/flipper/di/regs.rs
new file mode 100644
index 0000000..0fdbbe9
--- /dev/null
+++ b/crates/gekko/src/flipper/di/regs.rs
@@ -0,0 +1,94 @@
+use super::Di;
+use crate::mmio::traits::MmioAccess;
+
+// 0xCC006000 4 R/W DISR - DI Status Register
+
+crate::mmio_register! {
+ DiStatusRegister: u32 @ 0xCC006000 {
+ #[bits(0)]
+ pub brk: bool,
+
+ #[bits(1)]
+ pub device_error_mask: bool,
+
+ #[bits(2)]
+ pub device_error: bool,
+
+ #[bits(3)]
+ pub transfer_complete_mask: bool,
+
+ #[bits(4)]
+ pub transfer_complete: bool,
+
+ #[bits(5)]
+ pub break_complete_mask: bool,
+
+ #[bits(6)]
+ pub break_complete: bool,
+ }
+}
+
+impl MmioAccess<Di> for DiStatusRegister {
+ fn read(di: &Di) -> Self {
+ di.status
+ }
+
+ fn write(self, di: &mut Di) {
+ let mut sr = di.status;
+
+ if self.break_complete() {
+ sr = sr.with_break_complete(false);
+ }
+ if self.device_error() {
+ sr = sr.with_device_error(false);
+ }
+ if self.transfer_complete() {
+ sr = sr.with_transfer_complete(false);
+ }
+
+ // TODO: 0 has no effect?
+ if self.brk() {
+ sr = sr.with_brk(true);
+ }
+
+ sr = sr
+ .with_break_complete_mask(self.break_complete_mask())
+ .with_device_error_mask(self.device_error_mask())
+ .with_transfer_complete_mask(self.transfer_complete_mask());
+
+ di.status = sr;
+ }
+}
+
+// 0xCC006004 4 R/W DICVR - DI Cover Register
+
+crate::mmio_register! {
+ DiCoverRegister: u32 @ 0xCC006004 {
+ #[bits(0)]
+ pub cover_status: bool,
+
+ #[bits(1)]
+ pub cover_interrupt_mask: bool,
+
+ #[bits(2)]
+ pub cover_interrupt: bool,
+ }
+}
+
+impl MmioAccess<Di> for DiCoverRegister {
+ fn read(di: &Di) -> Self {
+ di.cover
+ }
+
+ fn write(self, di: &mut Di) {
+ let mut cvr = di.cover;
+
+ if self.cover_interrupt() {
+ cvr = cvr.with_cover_interrupt(false);
+ }
+
+ cvr = cvr.with_cover_interrupt_mask(self.cover_interrupt_mask());
+
+ di.cover = cvr;
+ }
+}
diff --git a/crates/gekko/src/flipper/dsp.rs b/crates/gekko/src/flipper/dsp.rs
index 760e31c..1a1f668 100644
--- a/crates/gekko/src/flipper/dsp.rs
+++ b/crates/gekko/src/flipper/dsp.rs
@@ -19,8 +19,12 @@ pub struct Dsp {
// I/O Registers
pub csr: regs::ControlStatus,
+ pub mailbox_to_dsp_hi: regs::MailboxToDspHi,
+ pub mailbox_to_dsp_lo: regs::MailboxToDspLo,
pub mailbox_to_cpu_hi: regs::MailboxToCpuHi,
pub mailbox_to_cpu_lo: regs::MailboxToCpuLo,
+ pub aram_info: regs::AramInfo,
+ pub aram_refresh: regs::AramRefresh,
pub aram_dma_mmio_addr: regs::AramDmaMmioAddr,
pub aram_dma_aram_addr: regs::AramDmaAramAddr,
pub aram_dma_control: regs::AramDmaControl,
@@ -47,8 +51,12 @@ impl Dsp {
ifx,
aram,
csr: regs::ControlStatus::default(),
+ mailbox_to_dsp_hi: regs::MailboxToDspHi::from_raw(0),
+ mailbox_to_dsp_lo: regs::MailboxToDspLo::from_raw(0),
mailbox_to_cpu_hi: regs::MailboxToCpuHi::from_raw(0),
mailbox_to_cpu_lo: regs::MailboxToCpuLo::from_raw(0),
+ aram_info: regs::AramInfo::from_raw(0),
+ aram_refresh: regs::AramRefresh::from_raw(0),
aram_dma_mmio_addr: regs::AramDmaMmioAddr::from_raw(0),
aram_dma_aram_addr: regs::AramDmaAramAddr::from_raw(0),
aram_dma_control: regs::AramDmaControl::from_raw(0),
@@ -69,7 +77,7 @@ impl Dsp {
if self.pending_aram_dma {
self.pending_aram_dma = false;
- let mmio_addr = self.aram_dma_mmio_addr.raw() as usize;
+ let mmio_addr = (self.aram_dma_mmio_addr.raw() & 0x3FFFFFFF) as usize;
let aram_addr = self.aram_dma_aram_addr.raw() as usize;
let count = self.aram_dma_control.count() as usize * 4;
@@ -91,7 +99,7 @@ impl Dsp {
"ARAM DMA complete"
);
- // TODO: Cause actual interrupt?
+ // Assert ARAM DMA complete interrupt in CSR
self.csr = self.csr.with_ar_interrupt(true);
}
@@ -118,8 +126,12 @@ impl Dsp {
crate::impl_mmio_dispatch!(
regs::ControlStatus,
+ regs::MailboxToDspHi,
+ regs::MailboxToDspLo,
regs::MailboxToCpuHi,
regs::MailboxToCpuLo,
+ regs::AramInfo,
+ regs::AramRefresh,
regs::AramDmaMmioAddr,
regs::AramDmaAramAddr,
regs::AramDmaControl,
@@ -163,4 +175,20 @@ impl Dsp {
tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP write_u32");
}
}
+
+ pub fn interrupt_active(&self) -> bool {
+ (self.csr.ai_interrupt() && self.csr.ai_interrupt_mask())
+ || (self.csr.ar_interrupt() && self.csr.ar_interrupt_mask())
+ || (self.csr.dsp_interrupt() && self.csr.dsp_interrupt_mask())
+ }
+}
+
+impl crate::gekko::Gekko {
+ pub fn check_dsp_interrupts(&mut self) {
+ if self.dsp.interrupt_active() {
+ self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Dsp);
+ } else {
+ self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Dsp);
+ }
+ }
}
diff --git a/crates/gekko/src/flipper/dsp/regs.rs b/crates/gekko/src/flipper/dsp/regs.rs
index 303ee42..6956cb2 100644
--- a/crates/gekko/src/flipper/dsp/regs.rs
+++ b/crates/gekko/src/flipper/dsp/regs.rs
@@ -92,6 +92,36 @@ impl Default for ControlStatus {
}
}
+// 0xCC005000 2 [W] CPU-to-DSP Mailbox High
+// TODO: Since we don't emulate the DSP processor, always read busy as 0
+
+crate::mmio_register! {
+ MailboxToDspHi: u16 @ 0xCC005000 {
+ #[bits(0..=14)]
+ pub data: u16,
+
+ #[bits(15)]
+ pub busy: bool,
+ }
+}
+
+impl MmioAccess<Dsp> for MailboxToDspHi {
+ fn read(dsp: &Dsp) -> Self {
+ // TODO: instantly consume it for now
+ dsp.mailbox_to_dsp_hi.with_busy(false)
+ }
+
+ fn write(self, dsp: &mut Dsp) {
+ dsp.mailbox_to_dsp_hi = self;
+ }
+}
+
+// 0xCC005002 2 [W] CPU-to-DSP Mailbox Low
+
+crate::mmio_register! {
+ MailboxToDspLo: u16 @ 0xCC005002 => Dsp.mailbox_to_dsp_lo {}
+}
+
// 0xCC005004 2 [R] DSP-to-CPU Mailbox High
crate::mmio_register! {
@@ -104,6 +134,18 @@ crate::mmio_register! {
MailboxToCpuLo: u16 @ 0xCC005006 => Dsp.mailbox_to_cpu_lo {}
}
+// 0xCC005012 2 [R/W] AR_INFO - ARAM Info/Size
+
+crate::mmio_register! {
+ AramInfo: u16 @ 0xCC005012 => Dsp.aram_info {}
+}
+
+// 0xCC00501A 2 [R/W] AR_REFRESH - ARAM Refresh
+
+crate::mmio_register! {
+ AramRefresh: u16 @ 0xCC00501A => Dsp.aram_refresh {}
+}
+
// 0xCC005020 4 [R/W] ARAM DMA MMIO Address
crate::mmio_register! {
diff --git a/crates/gekko/src/flipper/mi.rs b/crates/gekko/src/flipper/mi.rs
new file mode 100644
index 0000000..376092b
--- /dev/null
+++ b/crates/gekko/src/flipper/mi.rs
@@ -0,0 +1,57 @@
+pub mod regs;
+
+use crate::mmio::constants::MI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Mi {
+ pub interrupt_mask: regs::MiInterruptMask,
+}
+
+impl Mi {
+ pub fn new() -> Self {
+ Self {
+ interrupt_mask: regs::MiInterruptMask::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(regs::MiInterruptMask,);
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
+ self.read_raw(MI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(MI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ self.read_raw(MI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(MI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(MI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(MI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/mi/regs.rs b/crates/gekko/src/flipper/mi/regs.rs
new file mode 100644
index 0000000..c0dd3b0
--- /dev/null
+++ b/crates/gekko/src/flipper/mi/regs.rs
@@ -0,0 +1,22 @@
+use super::Mi;
+
+// 0xCC00401C 2 R/W Memory Interface Interrupt Mask
+
+crate::mmio_register! {
+ MiInterruptMask: u16 @ 0xCC00401C => Mi.interrupt_mask {
+ #[bits(0)]
+ pub mem0_enable: bool,
+
+ #[bits(1)]
+ pub mem1_enable: bool,
+
+ #[bits(2)]
+ pub mem2_enable: bool,
+
+ #[bits(3)]
+ pub mem3_enable: bool,
+
+ #[bits(4)]
+ pub master_enable: bool,
+ }
+}
diff --git a/crates/gekko/src/flipper/pe.rs b/crates/gekko/src/flipper/pe.rs
index 6480836..2ae881d 100644
--- a/crates/gekko/src/flipper/pe.rs
+++ b/crates/gekko/src/flipper/pe.rs
@@ -9,6 +9,11 @@ use crate::{
};
pub struct Pe {
+ pub zconf: regs::ZConfig,
+ pub alphaconf: regs::AlphaConfig,
+ pub dst_alphaconf: regs::DstAlphaConfig,
+ pub alphamode: regs::AlphaMode,
+ pub alpharead: regs::AlphaRead,
pub sr: regs::InterruptStatus,
pub token: regs::Token,
}
@@ -16,6 +21,11 @@ pub struct Pe {
impl Pe {
pub fn new() -> Self {
Self {
+ zconf: regs::ZConfig::from_raw(0),
+ alphaconf: regs::AlphaConfig::from_raw(0),
+ dst_alphaconf: regs::DstAlphaConfig::from_raw(0),
+ alphamode: regs::AlphaMode::from_raw(0),
+ alpharead: regs::AlphaRead::from_raw(0),
sr: regs::InterruptStatus::from_raw(0),
token: regs::Token::from_raw(0),
}
@@ -38,7 +48,15 @@ impl Pe {
self.sr = self.sr.with_pe_token(true);
}
- crate::impl_mmio_dispatch!(regs::InterruptStatus, regs::Token,);
+ crate::impl_mmio_dispatch!(
+ regs::ZConfig,
+ regs::AlphaConfig,
+ regs::DstAlphaConfig,
+ regs::AlphaMode,
+ regs::AlphaRead,
+ regs::InterruptStatus,
+ regs::Token,
+ );
pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
self.read_raw(PE_BASE + offset, 1).unwrap_or_else(|| {
diff --git a/crates/gekko/src/flipper/pe/regs.rs b/crates/gekko/src/flipper/pe/regs.rs
index 8e87eee..b9aebc9 100644
--- a/crates/gekko/src/flipper/pe/regs.rs
+++ b/crates/gekko/src/flipper/pe/regs.rs
@@ -3,6 +3,38 @@ use crate::{
mmio::traits::{MmioAccess, MmioRegister},
};
+// 0xCC001000 2 R/W Z Configuration
+
+crate::mmio_register! {
+ ZConfig: u16 @ 0xCC001000 => Pe.zconf {}
+}
+
+// 0xCC001002 2 R/W Alpha Configuration
+
+crate::mmio_register! {
+ AlphaConfig: u16 @ 0xCC001002 => Pe.alphaconf {}
+}
+
+// 0xCC001004 2 R/W Destination Alpha
+
+crate::mmio_register! {
+ DstAlphaConfig: u16 @ 0xCC001004 => Pe.dst_alphaconf {}
+}
+
+// 0xCC001006 2 R/W Alpha Compare Mode
+
+crate::mmio_register! {
+ AlphaMode: u16 @ 0xCC001006 => Pe.alphamode {}
+}
+
+// 0xCC001008 2 R/W Alpha Read Mode
+
+crate::mmio_register! {
+ AlphaRead: u16 @ 0xCC001008 => Pe.alpharead {}
+}
+
+// 0xCC00100A 2 R/W Interrupt Status
+
crate::mmio_register! {
InterruptStatus: u16 @ 0xCC00100A {
#[bits(0)]
diff --git a/crates/gekko/src/flipper/pi.rs b/crates/gekko/src/flipper/pi.rs
index d74cd06..06e02f3 100644
--- a/crates/gekko/src/flipper/pi.rs
+++ b/crates/gekko/src/flipper/pi.rs
@@ -11,6 +11,7 @@ const PI_FIFO_WPTR_OFFSET: u32 = 0x14;
pub struct Pi {
pub intsr: regs::InterruptCause,
pub intmr: regs::InterruptMask,
+ pub reset_code: regs::ResetCode,
/// CPU FIFO base address in physical memory
pub fifo_base: u32,
/// CPU FIFO end address in physical memory
@@ -45,6 +46,7 @@ impl Pi {
Pi {
intsr: regs::InterruptCause::default(),
intmr: regs::InterruptMask::from_raw(0),
+ reset_code: regs::ResetCode::from_raw(0),
fifo_base: 0,
fifo_end: 0,
fifo_wptr: 0,
@@ -71,7 +73,12 @@ impl Pi {
(self.intsr.raw() & self.intmr.raw()) != 0
}
- crate::impl_mmio_dispatch!(regs::InterruptCause, regs::InterruptMask,);
+ crate::impl_mmio_dispatch!(
+ regs::InterruptCause,
+ regs::InterruptMask,
+ regs::ResetCode,
+ regs::FlipperRev,
+ );
pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
self.read_raw(PI_BASE + offset, 1).unwrap_or_else(|| {
diff --git a/crates/gekko/src/flipper/pi/regs.rs b/crates/gekko/src/flipper/pi/regs.rs
index 40edd03..14813e3 100644
--- a/crates/gekko/src/flipper/pi/regs.rs
+++ b/crates/gekko/src/flipper/pi/regs.rs
@@ -118,3 +118,39 @@ crate::mmio_register! {
pub highspeed_port: bool,
}
}
+
+// 0xCC003024 4 r/w Console Reset Code
+
+crate::mmio_register! {
+ ResetCode: u32 @ 0xCC003024 {}
+}
+
+impl crate::mmio::traits::MmioAccess<Pi> for ResetCode {
+ fn read(_pi: &Pi) -> Self {
+ Self::from_raw(0)
+ }
+
+ fn write(self, _pi: &mut Pi) {
+ tracing::warn!("resetting console???");
+ }
+}
+
+// 0xCC00302C 4 r PI_FLIPPER_REV - Flipper Chip Revision
+
+crate::mmio_register! {
+ FlipperRev: u32 @ 0xCC00302C {
+ #[bits(28..=31)]
+ pub revision: u8,
+ }
+}
+
+impl crate::mmio::traits::MmioAccess<super::Pi> for FlipperRev {
+ fn read(_pi: &super::Pi) -> Self {
+ // FLIPPER_REV_C from Dolphin
+ Self::from_raw(0x2465_00B1)
+ }
+
+ fn write(self, _pi: &mut super::Pi) {
+ tracing::warn!("writing to FlipperRev???");
+ }
+}
diff --git a/crates/gekko/src/flipper/si.rs b/crates/gekko/src/flipper/si.rs
new file mode 100644
index 0000000..9de74f0
--- /dev/null
+++ b/crates/gekko/src/flipper/si.rs
@@ -0,0 +1,86 @@
+pub mod regs;
+
+use crate::mmio::constants::SI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Si {
+ pub poll: regs::SiPoll,
+ pub comcsr: regs::SiComcsr,
+ pub status: regs::SiStatusRegister,
+ pub ch0_in_buf_hi: regs::SiChannel0InBufHi,
+ pub io_buf: regs::SiIoBuf,
+}
+
+impl Si {
+ pub fn new() -> Self {
+ Self {
+ poll: regs::SiPoll::from_raw(0),
+ comcsr: regs::SiComcsr::from_raw(0),
+ status: regs::SiStatusRegister::from_raw(0),
+ ch0_in_buf_hi: regs::SiChannel0InBufHi::from_raw(0),
+ io_buf: regs::SiIoBuf::from_raw(0),
+ }
+ }
+
+ pub fn interrupt_active(&self) -> bool {
+ (self.comcsr.tc_interrupt() && self.comcsr.tc_interrupt_mask())
+ || (self.comcsr.rdst_interrupt() && self.comcsr.rdst_interrupt_mask())
+ }
+
+ crate::impl_mmio_dispatch!(
+ regs::SiPoll,
+ regs::SiComcsr,
+ regs::SiStatusRegister,
+ regs::SiChannel0InBufHi,
+ regs::SiIoBuf,
+ );
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> 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_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(SI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> 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_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(SI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(SI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled SI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(SI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled SI write_u32");
+ }
+ }
+}
+
+impl crate::gekko::Gekko {
+ pub fn check_si_interrupts(&mut self) {
+ if self.si.interrupt_active() {
+ self.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Si);
+ } else {
+ self.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Si);
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/si/regs.rs b/crates/gekko/src/flipper/si/regs.rs
new file mode 100644
index 0000000..ed25e40
--- /dev/null
+++ b/crates/gekko/src/flipper/si/regs.rs
@@ -0,0 +1,123 @@
+use super::Si;
+use crate::mmio::traits::MmioAccess;
+use chapa::BitEnum;
+
+// 0xCC006430 4 R/W SIPOLL - SI Poll Register
+crate::mmio_register! {
+ SiPoll: u32 @ 0xCC006430 => Si.poll {
+ #[bits(0..=3)]
+ pub vbcpy: u8,
+
+ #[bits(4..=7)]
+ pub enable: u8,
+
+ #[bits(8..=15)]
+ pub y_times: u8,
+
+ #[bits(16..=25)]
+ pub x_lines: u16,
+ }
+}
+
+// 0xCC006434 4 R/W SICOMCSR - SI Communication Control Status Register
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum Channel {
+ Channel0 = 0,
+ Channel1 = 1,
+ Channel2 = 2,
+ Channel3 = 3,
+}
+
+crate::mmio_register! {
+ SiComcsr: u32 @ 0xCC006434 {
+ #[bits(0)]
+ pub tstart: bool,
+
+ #[bits(1..=2)]
+ pub channel: Channel,
+
+ #[bits(6)]
+ pub callback_enable: bool,
+
+ #[bits(7)]
+ pub command_enable: bool,
+
+ #[bits(8..=14)]
+ pub in_length: u8,
+
+ #[bits(16..=22)]
+ pub out_length: u8,
+
+ #[bits(24)]
+ pub channel_enable: bool,
+
+ #[bits(25..=26)]
+ pub channel_number: u8,
+
+ #[bits(27)]
+ pub rdst_interrupt_mask: bool,
+
+ #[bits(28)]
+ pub rdst_interrupt: bool,
+
+ #[bits(29)]
+ pub com_error: bool,
+
+ #[bits(30)]
+ pub tc_interrupt_mask: bool,
+
+ #[bits(31)]
+ pub tc_interrupt: bool,
+ }
+}
+
+impl MmioAccess<Si> for SiComcsr {
+ fn read(si: &Si) -> Self {
+ si.comcsr
+ }
+
+ fn write(self, si: &mut Si) {
+ let mut csr = si.comcsr;
+
+ if self.tc_interrupt() {
+ csr = csr.with_tc_interrupt(false);
+ }
+
+ if self.rdst_interrupt() {
+ csr = csr.with_rdst_interrupt(false);
+ }
+
+ csr = csr
+ .with_tc_interrupt_mask(self.tc_interrupt_mask())
+ .with_rdst_interrupt_mask(self.rdst_interrupt_mask())
+ .with_command_enable(self.command_enable())
+ .with_callback_enable(self.callback_enable())
+ .with_channel(self.channel())
+ .with_in_length(self.in_length())
+ .with_out_length(self.out_length())
+ .with_channel_enable(self.channel_enable())
+ .with_channel_number(self.channel_number())
+ .with_tstart(self.tstart());
+
+ si.comcsr = csr;
+ }
+}
+
+// 0xCC006438 4 R/W SISR - SI Status Register
+
+crate::mmio_register! {
+ SiStatusRegister: u32 @ 0xCC006438 => Si.status {}
+}
+
+// 0xCC00643C 4 R/W SI Channel 0 Input Buffer High
+
+crate::mmio_register! {
+ SiChannel0InBufHi: u32 @ 0xCC00643C => Si.ch0_in_buf_hi {}
+}
+
+// 0xCC006480 4 R/W SI I/O Buffer
+
+crate::mmio_register! {
+ SiIoBuf: u32 @ 0xCC006480 => Si.io_buf {}
+}
diff --git a/crates/gekko/src/flipper/vi.rs b/crates/gekko/src/flipper/vi.rs
index 79e5a9b..408cdb6 100644
--- a/crates/gekko/src/flipper/vi.rs
+++ b/crates/gekko/src/flipper/vi.rs
@@ -45,6 +45,7 @@ pub struct Vi {
pub fct6: regs::FilterCoefficient6,
pub viclk: regs::ViClockSelect,
pub visel: regs::ViDtvStatus,
+ pub unknown_70: regs::ViUnknown70,
pub border_hbe: regs::BorderHbe,
pub border_hbs: regs::BorderHbs,
@@ -88,6 +89,7 @@ impl Vi {
fct6: regs::FilterCoefficient6::from_raw(0),
viclk: regs::ViClockSelect::from_raw(0),
visel: regs::ViDtvStatus::from_raw(0),
+ unknown_70: regs::ViUnknown70::from_raw(0),
border_hbe: regs::BorderHbe::from_raw(0),
border_hbs: regs::BorderHbs::from_raw(0),
half_line_count: 0,
@@ -128,6 +130,7 @@ impl Vi {
regs::FilterCoefficient6,
regs::ViClockSelect,
regs::ViDtvStatus,
+ regs::ViUnknown70,
regs::BorderHbe,
regs::BorderHbs,
);
diff --git a/crates/gekko/src/flipper/vi/regs.rs b/crates/gekko/src/flipper/vi/regs.rs
index 1fe2e3d..3beb1ff 100644
--- a/crates/gekko/src/flipper/vi/regs.rs
+++ b/crates/gekko/src/flipper/vi/regs.rs
@@ -201,6 +201,7 @@ crate::mmio_register! {
}
// 0xCC002030 4 R/W DI0 - Display Interrupt 0
+// THESE ARE NOT RESET ON WRITE 1!!
crate::mmio_register! {
DisplayInterrupt0: u32 @ 0xCC002030 => Vi.di0 {
@@ -371,6 +372,12 @@ crate::mmio_register! {
}
}
+// 0xCC002070 2 R/W God knows what this is, log for now
+
+crate::mmio_register! {
+ ViUnknown70: u16 @ 0xCC002070 => Vi.unknown_70 {}
+}
+
// 0xCC002072 2 R/W BorderHBE - Border HBE
crate::mmio_register! {
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index f54eab0..a27bd49 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,7 +1,7 @@
use crate::{
cpu::{self, Cpu, IPL_RESET_VECTOR, semantics::Instruction},
exi::{Exi, macronix::ExiMacronix},
- flipper::{dsp::Dsp, gx::Gx, pe::Pe, pi::Pi, vi::Vi},
+ flipper::{ai::Ai, cp::Cp, di::Di, dsp::Dsp, gx::Gx, mi::Mi, pe::Pe, pi::Pi, si::Si, vi::Vi},
idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector},
mmio::Mmio,
scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
@@ -19,6 +19,11 @@ pub struct Gekko {
pub dsp: Dsp,
pub exi: Exi,
pub gx: Gx,
+ pub cp: Cp,
+ pub di: Di,
+ pub si: Si,
+ pub ai: Ai,
+ pub mi: Mi,
idle: IdleDetector,
}
@@ -35,6 +40,11 @@ impl Gekko {
dsp: Dsp::new(),
exi: Exi::dummy(),
gx: Gx::new(),
+ cp: Cp::new(),
+ di: Di::new(),
+ si: Si::new(),
+ ai: Ai::new(),
+ mi: Mi::new(),
idle: IdleDetector::new(idle_skip),
}
}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index e08ad91..7bdb075 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -3,18 +3,24 @@ use crate::{
mmio::{
Mmio,
constants::{
- DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END, IPL_BASE, IPL_END, PE_BASE, PE_END,
- PI_BASE, PI_END, VI_BASE, VI_END,
+ AI_BASE, AI_END, CP_BASE, CP_END, DI_BASE, DI_END, DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE,
+ GX_FIFO_END, IPL_BASE, IPL_END, MI_BASE, MI_END, PE_BASE, PE_END, PI_BASE, PI_END, SI_BASE, SI_END,
+ VI_BASE, VI_END,
},
},
};
enum BusTarget {
+ Cp,
Vi,
Pe,
Pi,
+ Mi,
Dsp,
+ Di,
+ Si,
Exi,
+ Ai,
Gx,
Ipl,
Fallback,
@@ -23,11 +29,16 @@ enum BusTarget {
#[rustfmt::skip]
fn route(phys: u32) -> (BusTarget, u32) {
match phys {
- VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
+ CP_BASE..=CP_END => (BusTarget::Cp, phys - CP_BASE),
PE_BASE..=PE_END => (BusTarget::Pe, phys - PE_BASE),
+ VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
PI_BASE..=PI_END => (BusTarget::Pi, phys - PI_BASE),
+ MI_BASE..=MI_END => (BusTarget::Mi, phys - MI_BASE),
DSP_BASE..=DSP_END => (BusTarget::Dsp, phys - DSP_BASE),
+ DI_BASE..=DI_END => (BusTarget::Di, phys - DI_BASE),
+ SI_BASE..=SI_END => (BusTarget::Si, phys - SI_BASE),
EXI_BASE..=EXI_END => (BusTarget::Exi, phys - EXI_BASE),
+ AI_BASE..=AI_END => (BusTarget::Ai, phys - AI_BASE),
IPL_BASE..=IPL_END => (BusTarget::Ipl, phys),
GX_FIFO_BASE..=GX_FIFO_END => (BusTarget::Gx, phys - GX_FIFO_BASE),
_ => (BusTarget::Fallback, phys),
@@ -39,6 +50,7 @@ impl Gekko {
pub fn read_u8(&mut self, addr: u32) -> u8 {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => self.cp.mmio_read_u8(offset),
BusTarget::Vi => {
if offset == 0x2E {
return (self.vi.dph_value(self.scheduler.cycles) >> 8) as u8;
@@ -50,8 +62,17 @@ impl Gekko {
}
BusTarget::Pe => self.pe.mmio_read_u8(offset),
BusTarget::Pi => self.pi.mmio_read_u8(offset),
+ BusTarget::Mi => self.mi.mmio_read_u8(offset),
BusTarget::Dsp => self.dsp.mmio_read_u8(offset),
+ BusTarget::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::Gx => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
@@ -65,6 +86,7 @@ impl Gekko {
pub fn read_u16(&mut self, addr: u32) -> u16 {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => self.cp.mmio_read_u16(offset),
BusTarget::Vi => {
if offset == 0x2E {
return self.vi.dph_value(self.scheduler.cycles);
@@ -73,8 +95,17 @@ impl Gekko {
}
BusTarget::Pe => self.pe.mmio_read_u16(offset),
BusTarget::Pi => self.pi.mmio_read_u16(offset),
+ BusTarget::Mi => self.mi.mmio_read_u16(offset),
BusTarget::Dsp => self.dsp.mmio_read_u16(offset),
+ BusTarget::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::Gx => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
@@ -88,6 +119,7 @@ impl Gekko {
pub fn read_u32(&mut self, addr: u32) -> u32 {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => self.cp.mmio_read_u32(offset),
BusTarget::Vi => {
if offset == 0x2C {
let dpv = self.vi.mmio_read_u16(0x2C) as u32;
@@ -98,8 +130,17 @@ impl Gekko {
}
BusTarget::Pe => self.pe.mmio_read_u32(offset),
BusTarget::Pi => self.pi.mmio_read_u32(offset),
+ BusTarget::Mi => self.mi.mmio_read_u32(offset),
BusTarget::Dsp => self.dsp.mmio_read_u32(offset),
+ BusTarget::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::Gx => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
@@ -113,6 +154,10 @@ impl Gekko {
pub fn write_u8(&mut self, addr: u32, val: u8) {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => {
+ self.cp.mmio_write_u8(offset, val);
+ self.check_cp_interrupts();
+ }
BusTarget::Vi => {
self.vi.mmio_write_u8(offset, val);
self.maybe_schedule_vi_half_line();
@@ -125,14 +170,30 @@ impl Gekko {
self.check_pe_interrupts();
}
BusTarget::Pi => self.pi.mmio_write_u8(offset, val),
+ BusTarget::Mi => self.mi.mmio_write_u8(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u8(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
+ self.check_dsp_interrupts();
+ }
+ BusTarget::Di => {
+ self.di.mmio_write_u8(offset, val);
+ 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::Gx => {
if self.pi.is_fifo_redirected() {
@@ -153,6 +214,10 @@ impl Gekko {
pub fn write_u16(&mut self, addr: u32, val: u16) {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => {
+ self.cp.mmio_write_u16(offset, val);
+ self.check_cp_interrupts();
+ }
BusTarget::Vi => {
self.vi.mmio_write_u16(offset, val);
self.maybe_schedule_vi_half_line();
@@ -165,14 +230,30 @@ impl Gekko {
self.check_pe_interrupts();
}
BusTarget::Pi => self.pi.mmio_write_u16(offset, val),
+ BusTarget::Mi => self.mi.mmio_write_u16(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u16(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
+ self.check_dsp_interrupts();
+ }
+ BusTarget::Di => {
+ self.di.mmio_write_u16(offset, val);
+ 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::Gx => {
if self.pi.is_fifo_redirected() {
@@ -194,6 +275,10 @@ impl Gekko {
pub fn write_u32(&mut self, addr: u32, val: u32) {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
+ BusTarget::Cp => {
+ self.cp.mmio_write_u32(offset, val);
+ self.check_cp_interrupts();
+ }
BusTarget::Vi => {
self.vi.mmio_write_u32(offset, val);
self.maybe_schedule_vi_half_line();
@@ -206,14 +291,30 @@ impl Gekko {
self.check_pe_interrupts();
}
BusTarget::Pi => self.pi.mmio_write_u32(offset, val),
+ BusTarget::Mi => self.mi.mmio_write_u32(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u32(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
+ self.check_dsp_interrupts();
+ }
+ BusTarget::Di => {
+ self.di.mmio_write_u32(offset, val);
+ 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::Gx => {
if self.pi.is_fifo_redirected() {
diff --git a/crates/gekko/src/mmio/constants.rs b/crates/gekko/src/mmio/constants.rs
index c47da6e..945ee3d 100644
--- a/crates/gekko/src/mmio/constants.rs
+++ b/crates/gekko/src/mmio/constants.rs
@@ -11,6 +11,9 @@ pub const HW_REG_END: u32 = 0x0C7F_FFFF;
pub const HW_REG_SIZE: usize = 0x0080_0000; // 8 MB
// Hardware block address ranges (physical)
+pub const CP_BASE: u32 = 0x0C00_0000;
+pub const CP_END: u32 = 0x0C00_0FFF;
+
pub const PE_BASE: u32 = 0x0C00_1000;
pub const PE_END: u32 = 0x0C00_10FF;
@@ -26,6 +29,12 @@ pub const MI_END: u32 = 0x0C00_4FFF;
pub const DSP_BASE: u32 = 0x0C00_5000;
pub const DSP_END: u32 = 0x0C00_5FFF;
+pub const DI_BASE: u32 = 0x0C00_6000;
+pub const DI_END: u32 = 0x0C00_63FF;
+
+pub const SI_BASE: u32 = 0x0C00_6400;
+pub const SI_END: u32 = 0x0C00_67FF;
+
pub const EXI_BASE: u32 = 0x0C00_6800;
pub const EXI_END: u32 = 0x0C00_6BFF;