Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

irq fix (w1c) and pe bus routing

Layle5d4be9cf5504
diff --git a/crates/gekko/src/flipper.rs b/crates/gekko/src/flipper.rs
index 12bf87c..b583eef 100644
--- a/crates/gekko/src/flipper.rs
+++ b/crates/gekko/src/flipper.rs
@@ -1,4 +1,5 @@
pub mod dsp;
pub mod gx;
+pub mod pe;
pub mod pi;
pub mod vi;
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rs
index 29b7dc0..db2fb0d 100644
--- a/crates/gekko/src/flipper/gx.rs
+++ b/crates/gekko/src/flipper/gx.rs
@@ -4,7 +4,6 @@ pub mod fifo;
pub mod math;
pub mod regs;
-use super::pi::InterruptFlag;
use crate::{
flipper::gx::{
constants::{
@@ -754,11 +753,12 @@ fn read_index(cur: &mut Cursor<&Vec<u8>>, attr: regs::AttributeType) -> usize {
}
impl Gekko {
- /// Check if the GX stub detected a finish command and assert the PI interrupt
+ /// Check if the GX stub detected a finish command and signal PE
pub fn check_gx_pe_finish(&mut self) {
if self.gx.raise_interrupt {
self.gx.raise_interrupt = false;
- self.pi.assert_interrupt(InterruptFlag::PeFinish);
+ self.pe.signal_finish();
}
+ self.check_pe_interrupts();
}
}
diff --git a/crates/gekko/src/flipper/pe.rs b/crates/gekko/src/flipper/pe.rs
new file mode 100644
index 0000000..6480836
--- /dev/null
+++ b/crates/gekko/src/flipper/pe.rs
@@ -0,0 +1,97 @@
+pub mod regs;
+use crate::{
+ flipper::pi::InterruptFlag,
+ gekko::Gekko,
+ mmio::{
+ constants::PE_BASE,
+ traits::{MmioAccess, MmioRegister},
+ },
+};
+
+pub struct Pe {
+ pub sr: regs::InterruptStatus,
+ pub token: regs::Token,
+}
+
+impl Pe {
+ pub fn new() -> Self {
+ Self {
+ sr: regs::InterruptStatus::from_raw(0),
+ token: regs::Token::from_raw(0),
+ }
+ }
+
+ pub fn finish_interrupt_active(&self) -> bool {
+ self.sr.pe_finish_enable() && self.sr.pe_finish()
+ }
+
+ pub fn token_interrupt_active(&self) -> bool {
+ self.sr.pe_token_enable() && self.sr.pe_token()
+ }
+
+ pub fn signal_finish(&mut self) {
+ self.sr = self.sr.with_pe_finish(true);
+ }
+
+ pub fn signal_token(&mut self, token: u16) {
+ self.token = regs::Token::from_raw(token.into());
+ self.sr = self.sr.with_pe_token(true);
+ }
+
+ crate::impl_mmio_dispatch!(regs::InterruptStatus, regs::Token,);
+
+ pub fn mmio_read_u8(&mut self, offset: u32) -> u8 {
+ self.read_raw(PE_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(PE_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&mut self, offset: u32) -> u16 {
+ self.read_raw(PE_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(PE_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&mut self, offset: u32) -> u32 {
+ self.read_raw(PE_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(PE_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PE write_u32");
+ }
+ }
+}
+
+impl Gekko {
+ pub fn check_pe_interrupts(&mut self) {
+ if self.pe.token_interrupt_active() {
+ self.pi.assert_interrupt(InterruptFlag::PeToken);
+ } else {
+ self.pi.clear_interrupt(InterruptFlag::PeToken);
+ }
+
+ if self.pe.finish_interrupt_active() {
+ self.pi.assert_interrupt(InterruptFlag::PeFinish);
+ } else {
+ self.pi.clear_interrupt(InterruptFlag::PeFinish);
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/pe/regs.rs b/crates/gekko/src/flipper/pe/regs.rs
new file mode 100644
index 0000000..8e87eee
--- /dev/null
+++ b/crates/gekko/src/flipper/pe/regs.rs
@@ -0,0 +1,47 @@
+use crate::{
+ flipper::pe::Pe,
+ mmio::traits::{MmioAccess, MmioRegister},
+};
+
+crate::mmio_register! {
+ InterruptStatus: u16 @ 0xCC00100A {
+ #[bits(0)]
+ pub pe_token_enable: bool,
+
+ #[bits(1)]
+ pub pe_finish_enable: bool,
+
+ #[bits(2)]
+ pub pe_token: bool,
+
+ #[bits(3)]
+ pub pe_finish: bool,
+ }
+}
+
+impl MmioAccess<Pe> for InterruptStatus {
+ fn read(pe: &Pe) -> Self {
+ pe.sr
+ }
+
+ fn write(self, pe: &mut Pe) {
+ Self::write_at(pe, Self::ADDR, Self::SIZE as u32, self.to_raw());
+ }
+
+ fn write_at(pe: &mut Pe, addr: u32, access_size: u32, val: u32) {
+ const ENABLE_MASK: u32 = (1 << 0) | (1 << 1);
+ const STATUS_MASK: u32 = (1 << 2) | (1 << 3);
+
+ let current = pe.sr.to_raw();
+ let merged = Self::write_sub(current, addr, access_size, val);
+ let written_bits = Self::write_sub(0, addr, access_size, val);
+ let next_status = (current & STATUS_MASK) & !(written_bits & STATUS_MASK);
+ let next = (merged & ENABLE_MASK) | next_status;
+
+ pe.sr = <Self as MmioRegister>::from_raw(next);
+ }
+}
+
+crate::mmio_register! {
+ Token: u16 @ 0xCC00100E => Pe.token {}
+}
diff --git a/crates/gekko/src/flipper/pi/regs.rs b/crates/gekko/src/flipper/pi/regs.rs
index a6b03b2..40edd03 100644
--- a/crates/gekko/src/flipper/pi/regs.rs
+++ b/crates/gekko/src/flipper/pi/regs.rs
@@ -1,5 +1,4 @@
use super::Pi;
-use crate::mmio::traits::MmioRegister;
// 0xCC003000 4 r INTSR - Interrupt Cause
@@ -56,15 +55,13 @@ impl crate::mmio::traits::MmioAccess<Pi> for InterruptCause {
fn read(dev: &Pi) -> Self {
dev.intsr
}
- fn write(self, dev: &mut Pi) {
- dev.intsr = self;
- }
- fn read_at(dev: &mut Pi, addr: u32, access_size: u32) -> u32 {
- let val = Self::read_sub(dev.intsr.to_raw(), addr, access_size);
- // TODO: What should we reset??
- dev.intsr = InterruptCause::default();
- val
+ fn write(self, dev: &mut Pi) {
+ // yagcd seems to be wrong, we should not
+ // clear everything on read, but just usual w1c instead
+ const RSWST_MASK: u32 = 1 << 16;
+ let cleared = dev.intsr.raw() & !self.raw();
+ dev.intsr = InterruptCause::from_raw(cleared | (dev.intsr.raw() & RSWST_MASK));
}
}
diff --git a/crates/gekko/src/flipper/vi.rs b/crates/gekko/src/flipper/vi.rs
index 425dd02..79e5a9b 100644
--- a/crates/gekko/src/flipper/vi.rs
+++ b/crates/gekko/src/flipper/vi.rs
@@ -1,6 +1,4 @@
pub mod regs;
-#[cfg(test)]
-mod tests;
use crate::{
flipper::pi::InterruptFlag,
diff --git a/crates/gekko/src/flipper/vi/regs.rs b/crates/gekko/src/flipper/vi/regs.rs
index 1fe2e3d..76165be 100644
--- a/crates/gekko/src/flipper/vi/regs.rs
+++ b/crates/gekko/src/flipper/vi/regs.rs
@@ -200,10 +200,19 @@ crate::mmio_register! {
}
}
+// clears on writing 1 for DI register INT bit
+fn write_di<T: MmioRegister + Copy>(current: T, new: T) -> T {
+ const INT_BIT: u32 = 1 << 31;
+ let cur_raw = current.to_raw();
+ let new_raw = new.to_raw();
+ let int_bit = if new_raw & INT_BIT != 0 { 0 } else { cur_raw & INT_BIT };
+ T::from_raw((new_raw & !INT_BIT) | int_bit)
+}
+
// 0xCC002030 4 R/W DI0 - Display Interrupt 0
crate::mmio_register! {
- DisplayInterrupt0: u32 @ 0xCC002030 => Vi.di0 {
+ DisplayInterrupt0: u32 @ 0xCC002030 {
#[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
#[bits(16..=25, alias = "vct")] pub vertical_count: u16,
#[bits(28, alias = "enb")] pub enable: bool,
@@ -211,10 +220,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Vi> for DisplayInterrupt0 {
+ fn read(dev: &Vi) -> Self {
+ dev.di0
+ }
+ fn write(self, dev: &mut Vi) {
+ dev.di0 = write_di(dev.di0, self);
+ }
+}
+
// 0xCC002034 4 R/W DI1 - Display Interrupt 1
crate::mmio_register! {
- DisplayInterrupt1: u32 @ 0xCC002034 => Vi.di1 {
+ DisplayInterrupt1: u32 @ 0xCC002034 {
#[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
#[bits(16..=25, alias = "vct")] pub vertical_count: u16,
#[bits(28, alias = "enb")] pub enable: bool,
@@ -222,10 +240,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Vi> for DisplayInterrupt1 {
+ fn read(dev: &Vi) -> Self {
+ dev.di1
+ }
+ fn write(self, dev: &mut Vi) {
+ dev.di1 = write_di(dev.di1, self);
+ }
+}
+
// 0xCC002038 4 R/W DI2 - Display Interrupt 2
crate::mmio_register! {
- DisplayInterrupt2: u32 @ 0xCC002038 => Vi.di2 {
+ DisplayInterrupt2: u32 @ 0xCC002038 {
#[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
#[bits(16..=25, alias = "vct")] pub vertical_count: u16,
#[bits(28, alias = "enb")] pub enable: bool,
@@ -233,10 +260,19 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Vi> for DisplayInterrupt2 {
+ fn read(dev: &Vi) -> Self {
+ dev.di2
+ }
+ fn write(self, dev: &mut Vi) {
+ dev.di2 = write_di(dev.di2, self);
+ }
+}
+
// 0xCC00203C 4 R/W DI3 - Display Interrupt 3
crate::mmio_register! {
- DisplayInterrupt3: u32 @ 0xCC00203C => Vi.di3 {
+ DisplayInterrupt3: u32 @ 0xCC00203C {
#[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
#[bits(16..=25, alias = "vct")] pub vertical_count: u16,
#[bits(28, alias = "enb")] pub enable: bool,
@@ -244,6 +280,15 @@ crate::mmio_register! {
}
}
+impl MmioAccess<Vi> for DisplayInterrupt3 {
+ fn read(dev: &Vi) -> Self {
+ dev.di3
+ }
+ fn write(self, dev: &mut Vi) {
+ dev.di3 = write_di(dev.di3, self);
+ }
+}
+
// 0xCC002040 4 R/W DL0 - Display Latch 0
crate::mmio_register! {
diff --git a/crates/gekko/src/flipper/vi/tests.rs b/crates/gekko/src/flipper/vi/tests.rs
deleted file mode 100644
index 5dab73f..0000000
--- a/crates/gekko/src/flipper/vi/tests.rs
+++ /dev/null
@@ -1,74 +0,0 @@
-use super::*;
-use crate::mmio::traits::MmioRegister;
-
-fn vi_with_top(raw: u32) -> Vi {
- let mut vi = Vi::new();
- vi.tfbl = regs::TopFieldBase::from_raw(raw);
- vi
-}
-
-fn vi_with_dcr(raw: u16) -> Vi {
- let mut vi = Vi::new();
- vi.dcr = regs::DisplayConfiguration::from_raw(raw);
- vi
-}
-
-const TOP_OFF: u32 = regs::TopFieldBase::ADDR - VI_BASE;
-const DCR_OFF: u32 = regs::DisplayConfiguration::ADDR - VI_BASE;
-
-#[test]
-fn u16_write_upper_half_of_u32_register() {
- let mut vi = vi_with_top(0xAABBCCDD);
- vi.mmio_write_u16(TOP_OFF, 0x1234);
- assert_eq!(vi.tfbl.to_raw(), 0x1234CCDD);
-}
-
-#[test]
-fn u16_write_lower_half_of_u32_register() {
- let mut vi = vi_with_top(0xAABBCCDD);
- vi.mmio_write_u16(TOP_OFF + 2, 0x5678);
- assert_eq!(vi.tfbl.to_raw(), 0xAABB5678);
-}
-
-#[test]
-fn u16_read_upper_half_of_u32_register() {
- let mut vi = vi_with_top(0xAABBCCDD);
- assert_eq!(vi.mmio_read_u16(TOP_OFF), 0xAABB);
-}
-
-#[test]
-fn u16_read_lower_half_of_u32_register() {
- let mut vi = vi_with_top(0xAABBCCDD);
- assert_eq!(vi.mmio_read_u16(TOP_OFF + 2), 0xCCDD);
-}
-
-#[test]
-fn u8_write_each_byte_of_u32_register() {
- let mut vi = vi_with_top(0x00000000);
- vi.mmio_write_u8(TOP_OFF + 0, 0xAA);
- vi.mmio_write_u8(TOP_OFF + 1, 0xBB);
- vi.mmio_write_u8(TOP_OFF + 2, 0xCC);
- vi.mmio_write_u8(TOP_OFF + 3, 0xDD);
- assert_eq!(vi.tfbl.to_raw(), 0xAABBCCDD);
-}
-
-#[test]
-fn u8_write_does_not_disturb_other_bytes_in_u32() {
- let mut vi = vi_with_top(0xAABBCCDD);
- vi.mmio_write_u8(TOP_OFF + 1, 0xFF);
- assert_eq!(vi.tfbl.to_raw(), 0xAAFFCCDD);
-}
-
-#[test]
-fn u8_write_upper_byte_of_u16_register() {
- let mut vi = vi_with_dcr(0x00FF);
- vi.mmio_write_u8(DCR_OFF, 0xAB);
- assert_eq!(vi.dcr.to_raw(), 0xABFF);
-}
-
-#[test]
-fn u8_write_lower_byte_of_u16_register() {
- let mut vi = vi_with_dcr(0xFF00);
- vi.mmio_write_u8(DCR_OFF + 1, 0xCD);
- assert_eq!(vi.dcr.to_raw(), 0xFFCD);
-}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index c5c2644..d9ab400 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,12 +1,7 @@
use crate::{
cpu::{self, Cpu, IPL_RESET_VECTOR, semantics::Instruction},
exi::Exi,
- flipper::{
- dsp::Dsp,
- gx::Gx,
- pi::{InterruptFlag, Pi},
- vi::Vi,
- },
+ flipper::{dsp::Dsp, gx::Gx, pe::Pe, pi::Pi, vi::Vi},
idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector},
mmio::Mmio,
scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
@@ -19,6 +14,7 @@ pub struct Gekko {
pub scheduler: Scheduler,
pub mmio: Mmio,
pub vi: Vi,
+ pub pe: Pe,
pub pi: Pi,
pub dsp: Dsp,
pub exi: Exi,
@@ -34,6 +30,7 @@ impl Gekko {
scheduler: Scheduler::new(),
mmio: Mmio::new(),
vi: Vi::new(),
+ pe: Pe::new(),
pi: Pi::new(),
dsp: Dsp::new(),
exi: Exi::dummy(),
@@ -113,7 +110,6 @@ impl Gekko {
match event {
EventKind::VSync => {
self.vsync_pending = true;
- self.pi.assert_interrupt(InterruptFlag::Vi);
let next = self.scheduler.cycles + CYCLES_PER_VSYNC;
self.scheduler.schedule_at(next, EventKind::VSync);
}
@@ -122,6 +118,12 @@ impl Gekko {
self.vi.half_line_scheduled = false;
self.maybe_schedule_vi_half_line();
self.check_vi_interrupts();
+ // TODO: clear DI INT bits after propagation so they
+ // dont reassert INTSR.Vi on every subsequent halfline?
+ self.vi.di0.set_interrupt(false);
+ self.vi.di1.set_interrupt(false);
+ self.vi.di2.set_interrupt(false);
+ self.vi.di3.set_interrupt(false);
}
}
}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 32d4940..7f85873 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -3,14 +3,15 @@ use crate::{
mmio::{
Mmio,
constants::{
- DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END, IPL_BASE, IPL_END, PI_BASE, PI_END,
- VI_BASE, VI_END,
+ 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,
},
},
};
enum BusTarget {
Vi,
+ Pe,
Pi,
Dsp,
Exi,
@@ -23,6 +24,7 @@ enum BusTarget {
fn route(phys: u32) -> (BusTarget, u32) {
match phys {
VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
+ PE_BASE..=PE_END => (BusTarget::Pe, phys - PE_BASE),
PI_BASE..=PI_END => (BusTarget::Pi, phys - PI_BASE),
DSP_BASE..=DSP_END => (BusTarget::Dsp, phys - DSP_BASE),
EXI_BASE..=EXI_END => (BusTarget::Exi, phys - EXI_BASE),
@@ -46,6 +48,7 @@ impl Gekko {
}
self.vi.mmio_read_u8(offset)
}
+ BusTarget::Pe => self.pe.mmio_read_u8(offset),
BusTarget::Pi => self.pi.mmio_read_u8(offset),
BusTarget::Dsp => self.dsp.mmio_read_u8(offset),
BusTarget::Exi => self.exi.mmio_read_u8(offset),
@@ -68,6 +71,7 @@ impl Gekko {
}
self.vi.mmio_read_u16(offset)
}
+ BusTarget::Pe => self.pe.mmio_read_u16(offset),
BusTarget::Pi => self.pi.mmio_read_u16(offset),
BusTarget::Dsp => self.dsp.mmio_read_u16(offset),
BusTarget::Exi => self.exi.mmio_read_u16(offset),
@@ -92,6 +96,7 @@ impl Gekko {
}
self.vi.mmio_read_u32(offset)
}
+ BusTarget::Pe => self.pe.mmio_read_u32(offset),
BusTarget::Pi => self.pi.mmio_read_u32(offset),
BusTarget::Dsp => self.dsp.mmio_read_u32(offset),
BusTarget::Exi => self.exi.mmio_read_u32(offset),
@@ -115,6 +120,10 @@ impl Gekko {
self.check_vi_interrupts();
}
}
+ BusTarget::Pe => {
+ self.pe.mmio_write_u8(offset, val);
+ self.check_pe_interrupts();
+ }
BusTarget::Pi => self.pi.mmio_write_u8(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u8(offset, val);
@@ -150,6 +159,10 @@ impl Gekko {
self.check_vi_interrupts();
}
}
+ BusTarget::Pe => {
+ self.pe.mmio_write_u16(offset, val);
+ self.check_pe_interrupts();
+ }
BusTarget::Pi => self.pi.mmio_write_u16(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u16(offset, val);
@@ -186,6 +199,10 @@ impl Gekko {
self.check_vi_interrupts();
}
}
+ BusTarget::Pe => {
+ self.pe.mmio_write_u32(offset, val);
+ self.check_pe_interrupts();
+ }
BusTarget::Pi => self.pi.mmio_write_u32(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u32(offset, val);
diff --git a/crates/gekko/src/mmio/constants.rs b/crates/gekko/src/mmio/constants.rs
index 44c846b..c47da6e 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 PE_BASE: u32 = 0x0C00_1000;
+pub const PE_END: u32 = 0x0C00_10FF;
+
pub const VI_BASE: u32 = 0x0C00_2000;
pub const VI_END: u32 = 0x0C00_27FF;
diff --git a/crates/gekko/src/mmio/macros.rs b/crates/gekko/src/mmio/macros.rs
index 7b2b887..e6fd3ee 100644
--- a/crates/gekko/src/mmio/macros.rs
+++ b/crates/gekko/src/mmio/macros.rs
@@ -15,6 +15,13 @@ macro_rules! impl_mmio_dispatch {
#[inline]
fn write_raw(&mut self, addr: u32, access_size: u32, val: u32) -> bool {
$(if <$reg>::fits(addr, access_size) {
+ tracing::debug!(
+ reg = stringify!($reg),
+ addr = format!("{addr:08X}"),
+ val = format!("{val:08X}"),
+ size = access_size,
+ "MMIO write"
+ );
<$reg>::write_at(self, addr, access_size, val);
return true;
})*