Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

dec

Laylee4535583a7bc
diff --git a/crates/gecko/src/cpu.rs b/crates/gecko/src/cpu.rs
index 2d2a0a5..045a414 100644
--- a/crates/gecko/src/cpu.rs
+++ b/crates/gecko/src/cpu.rs
@@ -1,4 +1,5 @@
pub mod condition;
+pub mod dec;
#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]
pub mod instruction;
pub mod interpreter;
@@ -23,6 +24,7 @@ pub struct Cpu {
pub pc: u32,
pub cr: ConditionRegister,
pub fpscr: u32, // TODO: FP Status and Control Register
+ pub dec: dec::Decrementer,
pub spr: spr::Spr,
pub msr: msr::Msr,
pub sr: [sr::Sr; 16], // Segment Registers
@@ -36,6 +38,9 @@ pub struct Cpu {
impl Cpu {
pub fn new(initial_pc: u32) -> Self {
+ let mut spr = spr::Spr::default();
+ spr.dec = u32::MAX;
+
Cpu {
gprs: [0; 32],
fprs: [0.0; 32],
@@ -44,7 +49,8 @@ impl Cpu {
cia: initial_pc,
nia: initial_pc.wrapping_add(4),
cr: ConditionRegister::new(),
- spr: spr::Spr::default(),
+ dec: dec::Decrementer::default(),
+ spr,
msr: msr::Msr::default(),
fpscr: 0,
sr: [sr::Sr::default(); 16],
diff --git a/crates/gecko/src/cpu/interpreter/system.rs b/crates/gecko/src/cpu/interpreter/system.rs
index e097ccf..aa4377a 100644
--- a/crates/gecko/src/cpu/interpreter/system.rs
+++ b/crates/gecko/src/cpu/interpreter/system.rs
@@ -26,6 +26,16 @@ pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu
let spr_num = instr.spr_swapped();
let val = ctx.cpu.read_gpr(instr.rs());
match spr_num {
+ 22 => {
+ ctx.scheduler.cancel(crate::cpu::dec::underflow_handler);
+ ctx.cpu.dec.write(ctx.scheduler.cycles, val);
+ ctx.cpu.spr.dec = val;
+ ctx.scheduler.schedule_in(
+ crate::cpu::dec::cycles_until_underflow(val),
+ crate::cpu::dec::underflow_handler,
+ );
+ tracing::info!(cycles = ctx.scheduler.cycles, value = val, "decrementer set");
+ }
284 => ctx.scheduler.set_timebase_lower(val),
285 => ctx.scheduler.set_timebase_upper(val),
923 => {
@@ -40,6 +50,10 @@ pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu
crate::cpu::lut::OP_MFSPR => {
let spr_num = instr.spr_swapped();
let val = match spr_num {
+ 22 => {
+ ctx.cpu.spr.dec = ctx.cpu.dec.read(ctx.scheduler.cycles);
+ ctx.cpu.spr.dec
+ }
268 => ctx.scheduler.timebase_lower(),
269 => ctx.scheduler.timebase_upper(),
_ => ctx.cpu.spr.read(spr_num),
diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/cpu/irq.rs
index 687ff68..1a3455d 100644
--- a/crates/gecko/src/cpu/irq.rs
+++ b/crates/gecko/src/cpu/irq.rs
@@ -52,6 +52,38 @@ impl GameCube {
tracing::debug!(addr = format!("{:08X}", self.cpu.pc), "IRQ triggered");
}
+ pub fn cause_decrementer_interrupt(&mut self) {
+ let base: u32 = if self.cpu.msr.exception_prefix() {
+ 0xFFF0_0000
+ } else {
+ 0
+ };
+
+ self.cpu.dec.clear_interrupt();
+ self.cpu.spr.srr0 = Srr0::from(self.cpu.pc);
+ self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+
+ self.cpu.msr = self
+ .cpu
+ .msr
+ .with_pow(false)
+ .with_fp(false)
+ .with_be(false)
+ .with_dr(false)
+ .with_fe1(false)
+ .with_ee(false)
+ .with_fe0(false)
+ .with_ri(false)
+ .with_pr(false)
+ .with_se(false)
+ .with_ir(false)
+ .with_le(self.cpu.msr.ile());
+
+ self.cpu.pc = base | IRQ_DECREMENTER;
+
+ tracing::info!(addr = format!("{:08X}", self.cpu.pc), "decrementer IRQ triggered");
+ }
+
pub fn cause_trap_exception(&mut self) {
let base: u32 = if self.cpu.msr.exception_prefix() {
0xFFF0_0000
@@ -81,6 +113,8 @@ impl GameCube {
.with_le(self.cpu.msr.ile());
self.cpu.nia = base | IRQ_PROGRAM;
+
+ tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "trap exception triggered");
}
pub fn cause_syscall_interrupt(&mut self) {
@@ -111,5 +145,7 @@ impl GameCube {
.with_le(self.cpu.msr.ile());
self.cpu.nia = base | IRQ_SYSTEM_CALL;
+
+ tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "system call IRQ triggered");
}
}
diff --git a/crates/gecko/src/flipper/ai.rs b/crates/gecko/src/flipper/ai.rs
index f638c6f..2cbc680 100644
--- a/crates/gecko/src/flipper/ai.rs
+++ b/crates/gecko/src/flipper/ai.rs
@@ -72,11 +72,34 @@ pub fn start_audio_dma(gc: &mut GameCube) {
// TODO: actually stream samples to audio output.
const AUDIO_DMA_DELAY: u64 = 10_000;
gc.scheduler.schedule_in(AUDIO_DMA_DELAY, |gc| {
+ let csr_before = gc.dsp.csr.raw();
+ let intsr_before = gc.pi.intsr.raw();
gc.dsp.audio_dma_control.set_length(0);
gc.dsp.csr.set_dma_status(false);
gc.dsp.csr.set_ai_interrupt(true);
+ tracing::info!(
+ cycles = gc.scheduler.cycles,
+ csr_before = format!("{csr_before:04X}"),
+ csr_after = format!("{:04X}", gc.dsp.csr.raw()),
+ ai_interrupt = gc.dsp.csr.ai_interrupt(),
+ ai_interrupt_mask = gc.dsp.csr.ai_interrupt_mask(),
+ ar_interrupt = gc.dsp.csr.ar_interrupt(),
+ ar_interrupt_mask = gc.dsp.csr.ar_interrupt_mask(),
+ dsp_interrupt = gc.dsp.csr.dsp_interrupt(),
+ dsp_interrupt_mask = gc.dsp.csr.dsp_interrupt_mask(),
+ pi_intsr_before = format!("{intsr_before:08X}"),
+ pi_intmr = format!("{:08X}", gc.pi.intmr.raw()),
+ "Audio DMA completion"
+ );
// gc.dsp.audio_dma_control.set_play(false);
dsp::refresh_interrupts(gc);
+ tracing::info!(
+ cycles = gc.scheduler.cycles,
+ csr = format!("{:04X}", gc.dsp.csr.raw()),
+ pi_intsr = format!("{:08X}", gc.pi.intsr.raw()),
+ pi_intmr = format!("{:08X}", gc.pi.intmr.raw()),
+ "Audio DMA completion IRQ refreshed"
+ );
});
}
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index 2214883..e9cc001 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -293,10 +293,44 @@ impl Dsp {
pub fn refresh_interrupts(gc: &mut GameCube) {
use crate::flipper::pi::InterruptFlag;
- if gc.dsp.interrupt_active() {
+ let active = gc.dsp.interrupt_active();
+ let intsr_before = gc.pi.intsr.raw();
+ let dsp_pi_pending = intsr_before & InterruptFlag::Dsp as u32 != 0;
+
+ if active {
gc.pi.assert_interrupt(InterruptFlag::Dsp);
+ if !dsp_pi_pending {
+ tracing::info!(
+ csr = format!("{:04X}", gc.dsp.csr.raw()),
+ ai_interrupt = gc.dsp.csr.ai_interrupt(),
+ ai_interrupt_mask = gc.dsp.csr.ai_interrupt_mask(),
+ ar_interrupt = gc.dsp.csr.ar_interrupt(),
+ ar_interrupt_mask = gc.dsp.csr.ar_interrupt_mask(),
+ dsp_interrupt = gc.dsp.csr.dsp_interrupt(),
+ dsp_interrupt_mask = gc.dsp.csr.dsp_interrupt_mask(),
+ pi_intsr_before = format!("{intsr_before:08X}"),
+ pi_intsr_after = format!("{:08X}", gc.pi.intsr.raw()),
+ pi_intmr = format!("{:08X}", gc.pi.intmr.raw()),
+ "DSP PI interrupt asserted"
+ );
+ }
} else {
gc.pi.clear_interrupt(InterruptFlag::Dsp);
+ if dsp_pi_pending {
+ tracing::info!(
+ csr = format!("{:04X}", gc.dsp.csr.raw()),
+ ai_interrupt = gc.dsp.csr.ai_interrupt(),
+ ai_interrupt_mask = gc.dsp.csr.ai_interrupt_mask(),
+ ar_interrupt = gc.dsp.csr.ar_interrupt(),
+ ar_interrupt_mask = gc.dsp.csr.ar_interrupt_mask(),
+ dsp_interrupt = gc.dsp.csr.dsp_interrupt(),
+ dsp_interrupt_mask = gc.dsp.csr.dsp_interrupt_mask(),
+ pi_intsr_before = format!("{intsr_before:08X}"),
+ pi_intsr_after = format!("{:08X}", gc.pi.intsr.raw()),
+ pi_intmr = format!("{:08X}", gc.pi.intmr.raw()),
+ "DSP PI interrupt cleared"
+ );
+ }
}
}
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index a8843a5..e28a633 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -244,11 +244,19 @@ impl GameCube {
#[inline(always)]
pub fn step_cpu(&mut self) {
- // Deliver external interrupt when EE=1 and any enabled PI interrupt is pending
- if self.cpu.msr.external_interrupt_enable() && self.pi.interrupt_pending() {
- self.cause_external_interrupt();
- self.scheduler.cycles += 2;
- return;
+ if self.cpu.msr.external_interrupt_enable() {
+ // Deliver external interrupt when EE=1 and any enabled PI interrupt is pending
+ if self.pi.interrupt_pending() {
+ self.cause_external_interrupt();
+ self.scheduler.cycles += 2;
+ return;
+ }
+
+ if self.cpu.dec.interrupt_pending() {
+ self.cause_decrementer_interrupt();
+ self.scheduler.cycles += 2;
+ return;
+ }
}
// CPU pre-hook
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index 7df84e4..3eb8eb7 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -3,7 +3,8 @@ use crate::gamecube::GameCube;
use crate::hooks::HookFlags;
use crate::mmio::constants::{
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,
- MI_BASE, MI_END, PE_BASE, PE_END, PI_BASE, PI_END, RAM_END, SI_BASE, SI_END, VI_BASE, VI_END,
+ HW_REG_BASE, HW_REG_END, MI_BASE, MI_END, PE_BASE, PE_END, PI_BASE, PI_END, RAM_END, SI_BASE, SI_END, VI_BASE,
+ VI_END,
};
macro_rules! bus_read_hooks {
@@ -165,7 +166,7 @@ impl GameCube {
if phys <= RAM_END {
self.mmio.ram_write_u8(phys, val);
} else {
- self.write_u8_mmio(phys, val);
+ self.write_u8_mmio(phys, addr, val);
}
});
}
@@ -177,7 +178,7 @@ impl GameCube {
if phys <= RAM_END - 1 {
self.mmio.ram_write_u16(phys, val);
} else {
- self.write_u16_mmio(phys, val);
+ self.write_u16_mmio(phys, addr, val);
}
});
}
@@ -189,7 +190,7 @@ impl GameCube {
if phys <= RAM_END - 3 {
self.mmio.ram_write_u32(phys, val);
} else {
- self.write_u32_mmio(phys, val);
+ self.write_u32_mmio(phys, addr, val);
}
});
}
@@ -199,98 +200,492 @@ impl GameCube {
#[inline(never)]
fn read_u8_mmio(&mut self, phys: u32, addr: u32) -> u8 {
match phys {
- CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 1).unwrap_or(0) as u8,
- PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 1).unwrap_or(0) as u8,
- VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 1).unwrap_or(0) as u8,
- PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 1).unwrap_or(0) as u8,
- MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 1).unwrap_or(0) as u8,
- DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 1).unwrap_or(0) as u8,
- DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 1).unwrap_or(0) as u8,
- SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 1).unwrap_or(0) as u8,
- EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 1).unwrap_or(0) as u8,
- AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 1).unwrap_or(0) as u8,
+ CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
+ AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 1).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u8,
GX_FIFO_BASE..=GX_FIFO_END => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
- _ => self.mmio.phys_read_u8(phys),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 1,
+ "Unimplemented MMIO read"
+ );
+ }
+ self.mmio.phys_read_u8(phys)
+ }
}
}
#[inline(never)]
fn read_u16_mmio(&mut self, phys: u32, addr: u32) -> u16 {
match phys {
- CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 2).unwrap_or(0) as u16,
- PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 2).unwrap_or(0) as u16,
- VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 2).unwrap_or(0) as u16,
- PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 2).unwrap_or(0) as u16,
- MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 2).unwrap_or(0) as u16,
- DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 2).unwrap_or(0) as u16,
- DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 2).unwrap_or(0) as u16,
- SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 2).unwrap_or(0) as u16,
- EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 2).unwrap_or(0) as u16,
- AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 2).unwrap_or(0) as u16,
+ CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
+ AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 2).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }) as u16,
GX_FIFO_BASE..=GX_FIFO_END => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
- _ => self.mmio.phys_read_u16(phys),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 2,
+ "Unimplemented MMIO read"
+ );
+ }
+ self.mmio.phys_read_u16(phys)
+ }
}
}
#[inline(never)]
fn read_u32_mmio(&mut self, phys: u32, addr: u32) -> u32 {
match phys {
- CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 4).unwrap_or(0),
- PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 4).unwrap_or(0),
- VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 4).unwrap_or(0),
- PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 4).unwrap_or(0),
- MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 4).unwrap_or(0),
- DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 4).unwrap_or(0),
- DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 4).unwrap_or(0),
- SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 4).unwrap_or(0),
- EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 4).unwrap_or(0),
- AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 4).unwrap_or(0),
+ CP_BASE..=CP_END => crate::flipper::cp::cp_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ PE_BASE..=PE_END => crate::flipper::pe::pe_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ VI_BASE..=VI_END => crate::flipper::vi::vi_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ PI_BASE..=PI_END => crate::flipper::pi::pi_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ MI_BASE..=MI_END => crate::flipper::mi::mi_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ DSP_BASE..=DSP_END => crate::flipper::dsp::dsp_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ DI_BASE..=DI_END => crate::dvd::di_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ SI_BASE..=SI_END => crate::flipper::si::si_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ EXI_BASE..=EXI_END => crate::flipper::exi::exi_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
+ AI_BASE..=AI_END => crate::flipper::ai::ai_read(self, phys, 4).unwrap_or_else(|| {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ 0
+ }),
GX_FIFO_BASE..=GX_FIFO_END => {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
- _ => self.mmio.phys_read_u32(phys),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ size = 4,
+ "Unimplemented MMIO read"
+ );
+ }
+ self.mmio.phys_read_u32(phys)
+ }
}
}
#[inline(never)]
- fn write_u8_mmio(&mut self, phys: u32, val: u8) {
+ fn write_u8_mmio(&mut self, phys: u32, addr: u32, val: u8) {
+ let raw = val as u32;
match phys {
CP_BASE..=CP_END => {
- crate::flipper::cp::cp_write(self, phys, 1, val as u32);
+ if !crate::flipper::cp::cp_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
PE_BASE..=PE_END => {
- crate::flipper::pe::pe_write(self, phys, 1, val as u32);
+ if !crate::flipper::pe::pe_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
VI_BASE..=VI_END => {
- crate::flipper::vi::vi_write(self, phys, 1, val as u32);
+ if !crate::flipper::vi::vi_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
PI_BASE..=PI_END => {
- crate::flipper::pi::pi_write(self, phys, 1, val as u32);
+ if !crate::flipper::pi::pi_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
MI_BASE..=MI_END => {
- crate::flipper::mi::mi_write(self, phys, 1, val as u32);
+ if !crate::flipper::mi::mi_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
DSP_BASE..=DSP_END => {
- crate::flipper::dsp::dsp_write(self, phys, 1, val as u32);
+ if !crate::flipper::dsp::dsp_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
DI_BASE..=DI_END => {
- crate::dvd::di_write(self, phys, 1, val as u32);
+ if !crate::dvd::di_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
SI_BASE..=SI_END => {
- crate::flipper::si::si_write(self, phys, 1, val as u32);
+ if !crate::flipper::si::si_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
EXI_BASE..=EXI_END => {
- crate::flipper::exi::exi_write(self, phys, 1, val as u32);
+ if !crate::flipper::exi::exi_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
AI_BASE..=AI_END => {
- crate::flipper::ai::ai_write(self, phys, 1, val as u32);
+ if !crate::flipper::ai::ai_write(self, phys, 1, raw) {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
}
GX_FIFO_BASE..=GX_FIFO_END => {
let wptr = self.pi.fifo_wptr as usize;
@@ -301,42 +696,145 @@ impl GameCube {
self.check_gx_pe_interrupts();
}
}
- _ => self.mmio.phys_write_u8(phys, val),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 1,
+ "Unimplemented MMIO write"
+ );
+ }
+ self.mmio.phys_write_u8(phys, val);
+ }
}
}
#[inline(never)]
- fn write_u16_mmio(&mut self, phys: u32, val: u16) {
+ fn write_u16_mmio(&mut self, phys: u32, addr: u32, val: u16) {
+ let raw = val as u32;
match phys {
CP_BASE..=CP_END => {
- crate::flipper::cp::cp_write(self, phys, 2, val as u32);
+ if !crate::flipper::cp::cp_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
PE_BASE..=PE_END => {
- crate::flipper::pe::pe_write(self, phys, 2, val as u32);
+ if !crate::flipper::pe::pe_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
VI_BASE..=VI_END => {
- crate::flipper::vi::vi_write(self, phys, 2, val as u32);
+ if !crate::flipper::vi::vi_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
PI_BASE..=PI_END => {
- crate::flipper::pi::pi_write(self, phys, 2, val as u32);
+ if !crate::flipper::pi::pi_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
MI_BASE..=MI_END => {
- crate::flipper::mi::mi_write(self, phys, 2, val as u32);
+ if !crate::flipper::mi::mi_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
DSP_BASE..=DSP_END => {
- crate::flipper::dsp::dsp_write(self, phys, 2, val as u32);
+ if !crate::flipper::dsp::dsp_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
DI_BASE..=DI_END => {
- crate::dvd::di_write(self, phys, 2, val as u32);
+ if !crate::dvd::di_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
SI_BASE..=SI_END => {
- crate::flipper::si::si_write(self, phys, 2, val as u32);
+ if !crate::flipper::si::si_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
EXI_BASE..=EXI_END => {
- crate::flipper::exi::exi_write(self, phys, 2, val as u32);
+ if !crate::flipper::exi::exi_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
AI_BASE..=AI_END => {
- crate::flipper::ai::ai_write(self, phys, 2, val as u32);
+ if !crate::flipper::ai::ai_write(self, phys, 2, raw) {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
}
GX_FIFO_BASE..=GX_FIFO_END => {
let wptr = self.pi.fifo_wptr as usize;
@@ -348,42 +846,144 @@ impl GameCube {
self.check_gx_pe_interrupts();
}
}
- _ => self.mmio.phys_write_u16(phys, val),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{raw:08X}"),
+ size = 2,
+ "Unimplemented MMIO write"
+ );
+ }
+ self.mmio.phys_write_u16(phys, val);
+ }
}
}
#[inline(never)]
- fn write_u32_mmio(&mut self, phys: u32, val: u32) {
+ fn write_u32_mmio(&mut self, phys: u32, addr: u32, val: u32) {
match phys {
CP_BASE..=CP_END => {
- crate::flipper::cp::cp_write(self, phys, 4, val);
+ if !crate::flipper::cp::cp_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "CP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
PE_BASE..=PE_END => {
- crate::flipper::pe::pe_write(self, phys, 4, val);
+ if !crate::flipper::pe::pe_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "PE",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
VI_BASE..=VI_END => {
- crate::flipper::vi::vi_write(self, phys, 4, val);
+ if !crate::flipper::vi::vi_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "VI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
PI_BASE..=PI_END => {
- crate::flipper::pi::pi_write(self, phys, 4, val);
+ if !crate::flipper::pi::pi_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "PI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
MI_BASE..=MI_END => {
- crate::flipper::mi::mi_write(self, phys, 4, val);
+ if !crate::flipper::mi::mi_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "MI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
DSP_BASE..=DSP_END => {
- crate::flipper::dsp::dsp_write(self, phys, 4, val);
+ if !crate::flipper::dsp::dsp_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "DSP",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
DI_BASE..=DI_END => {
- crate::dvd::di_write(self, phys, 4, val);
+ if !crate::dvd::di_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "DI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
SI_BASE..=SI_END => {
- crate::flipper::si::si_write(self, phys, 4, val);
+ if !crate::flipper::si::si_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "SI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
EXI_BASE..=EXI_END => {
- crate::flipper::exi::exi_write(self, phys, 4, val);
+ if !crate::flipper::exi::exi_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "EXI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
AI_BASE..=AI_END => {
- crate::flipper::ai::ai_write(self, phys, 4, val);
+ if !crate::flipper::ai::ai_write(self, phys, 4, val) {
+ tracing::warn!(
+ device = "AI",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
}
GX_FIFO_BASE..=GX_FIFO_END => {
let wptr = self.pi.fifo_wptr as usize;
@@ -395,7 +995,19 @@ impl GameCube {
self.check_gx_pe_interrupts();
}
}
- _ => self.mmio.phys_write_u32(phys, val),
+ _ => {
+ if (HW_REG_BASE..=HW_REG_END).contains(&phys) {
+ tracing::warn!(
+ device = "HW_REG",
+ addr = format!("{addr:08X}"),
+ phys_addr = format!("{phys:08X}"),
+ val = format!("{val:08X}"),
+ size = 4,
+ "Unimplemented MMIO write"
+ );
+ }
+ self.mmio.phys_write_u32(phys, val);
+ }
}
}
}
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 56c3d33..5adb083 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -31,10 +31,11 @@ impl Scheduler {
events: VecDeque::with_capacity(8),
};
let initial_refresh_rate = RefreshRate::Hz60; // TODO: Detect IPL and schedule accordingly
- s.schedule_at(initial_refresh_rate.cycles_per_frame(), crate::scheduler::vsync_handler);
+ s.schedule_at(initial_refresh_rate.cycles_per_frame(), self::vsync_handler);
+ s.schedule_at(CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE, self::dsp_batch_handler);
s.schedule_at(
- CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE,
- crate::scheduler::dsp_batch_handler,
+ crate::cpu::dec::cycles_until_underflow(u32::MAX),
+ crate::cpu::dec::underflow_handler,
);
s
}
@@ -75,6 +76,11 @@ impl Scheduler {
self.next_deadline = self.next_deadline.min(deadline);
}
+ pub fn cancel(&mut self, f: ScheduledFn) {
+ self.events.retain(|e| !std::ptr::fn_addr_eq(e.f, f));
+ self.refresh_deadline();
+ }
+
pub fn schedule_in(&mut self, delay: u64, f: ScheduledFn) {
let deadline = self.cycles + delay;
self.schedule_at(deadline, f);
@@ -102,15 +108,14 @@ impl Scheduler {
pub fn vsync_handler(gc: &mut GameCube) {
gc.vsync_pending = true;
let rate = gc.vi.dcr.video_format().refresh_rate();
- gc.scheduler
- .schedule_in(rate.cycles_per_frame(), crate::scheduler::vsync_handler);
+ gc.scheduler.schedule_in(rate.cycles_per_frame(), self::vsync_handler);
}
/// Reschedules itself every DSP batch.
pub fn dsp_batch_handler(gc: &mut GameCube) {
gc.execute_dsp_batch();
gc.scheduler.schedule_in(
- crate::scheduler::CPU_CYCLES_PER_DSP_TICK * crate::scheduler::DSP_BATCH_SIZE,
- crate::scheduler::dsp_batch_handler,
+ self::CPU_CYCLES_PER_DSP_TICK * self::DSP_BATCH_SIZE,
+ self::dsp_batch_handler,
);
}