Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

ipc skeleton

Layle3bee0d1eaa1c
diff --git a/crates/gecko/src/hollywood.rs b/crates/gecko/src/hollywood.rs
index 7461bce..5317eb9 100644
--- a/crates/gecko/src/hollywood.rs
+++ b/crates/gecko/src/hollywood.rs
@@ -1,2 +1,17 @@
pub mod ipc;
pub mod irq;
+pub mod regs;
+
+pub struct Hollywood {
+ pub ipc: ipc::Ipc,
+ pub irq: irq::Irq,
+}
+
+impl Hollywood {
+ pub fn new() -> Self {
+ Hollywood {
+ ipc: ipc::Ipc::new(),
+ irq: irq::Irq::new(),
+ }
+ }
+}
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 5a2cee0..095af1a 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -1,18 +1,39 @@
-use crate::system::{System, SystemId};
+use crate::hollywood::regs::{ArmCtrl, ArmMsg, PpcCtrl, PpcMsg};
-#[inline(always)]
-pub fn ipc_read<const SYSTEM: SystemId>(_sys: &mut System<SYSTEM>, addr: u32, size: u32) -> Option<u32> {
- tracing::error!("unhandled IPC read at {:#010X} (size {})", addr, size);
- Some(0)
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum IpcState {
+ Idle,
+ Processing,
+ Done,
}
-#[inline(always)]
-pub fn ipc_write<const SYSTEM: SystemId>(_sys: &mut System<SYSTEM>, addr: u32, size: u32, val: u32) -> bool {
- tracing::error!(
- "unhandled IPC write at {:#010X} (size {}, value {:#010X})",
- addr,
- size,
- val
- );
- true
+pub struct Ipc {
+ pub ppcmsg: PpcMsg,
+ pub ppcctrl: PpcCtrl,
+ pub armmsg: ArmMsg,
+ pub armctrl: ArmCtrl,
+ pub state: IpcState,
+}
+
+impl Ipc {
+ pub fn new() -> Self {
+ Ipc {
+ ppcmsg: PpcMsg::from_raw(0),
+ ppcctrl: PpcCtrl::from_raw(0),
+ armmsg: ArmMsg::from_raw(0),
+ armctrl: ArmCtrl::from_raw(0),
+ state: IpcState::Idle,
+ }
+ }
+}
+
+crate::mmio_device_dispatch! {
+ read = ipc_read,
+ write = ipc_write,
+ registers = [
+ crate::hollywood::regs::PpcMsg,
+ crate::hollywood::regs::PpcCtrl,
+ crate::hollywood::regs::ArmMsg,
+ crate::hollywood::regs::ArmCtrl,
+ ],
}
diff --git a/crates/gecko/src/hollywood/irq.rs b/crates/gecko/src/hollywood/irq.rs
index 3022cf9..d2c94c4 100644
--- a/crates/gecko/src/hollywood/irq.rs
+++ b/crates/gecko/src/hollywood/irq.rs
@@ -1,11 +1,44 @@
+use crate::hollywood::regs::{Cause, Mask};
use crate::system::{System, SystemId};
+pub struct Irq {
+ pub cause: Cause,
+ pub mask: Mask,
+}
+
+impl Irq {
+ pub fn new() -> Self {
+ Irq {
+ cause: Cause::from_raw(0),
+ mask: Mask::from_raw(0),
+ }
+ }
+}
+
#[inline(always)]
-pub fn irq_read<const SYSTEM: SystemId>(_sys: &mut System<SYSTEM>, _addr: u32, _size: u32) -> Option<u32> {
- Some(0)
+pub fn route_to_pi<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ let firing = (sys.hollywood.irq.cause.raw() & sys.hollywood.irq.mask.raw()) != 0;
+ if firing {
+ sys.pi.assert_interrupt(crate::flipper::pi::InterruptFlag::Hsp);
+ } else {
+ sys.pi.clear_interrupt(crate::flipper::pi::InterruptFlag::Hsp);
+ }
}
#[inline(always)]
-pub fn irq_write<const SYSTEM: SystemId>(_sys: &mut System<SYSTEM>, _addr: u32, _size: u32, _val: u32) -> bool {
- true
+pub fn assert_ipc<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ sys.hollywood.irq.cause = sys.hollywood.irq.cause.with_ipc(true);
+ super::irq::route_to_pi(sys);
+}
+
+#[inline(always)]
+pub fn ack_ipc<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ sys.hollywood.irq.cause = sys.hollywood.irq.cause.with_ipc(false);
+ super::irq::route_to_pi(sys);
+}
+
+crate::mmio_device_dispatch! {
+ read = irq_read,
+ write = irq_write,
+ registers = [Cause, Mask],
}
diff --git a/crates/gecko/src/hollywood/regs.rs b/crates/gecko/src/hollywood/regs.rs
new file mode 100644
index 0000000..9aa2cfd
--- /dev/null
+++ b/crates/gecko/src/hollywood/regs.rs
@@ -0,0 +1,137 @@
+use crate::{
+ System, SystemId,
+ hollywood::ipc::IpcState,
+ mmio::traits::{MmioAccess, WriteMask},
+};
+
+// 0x0D00_0000 PPCMSG
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+pub struct PpcMsg {
+ #[bits(0..=31)]
+ pub value: u32,
+}
+
+crate::mmio_reg!(PpcMsg: u32 @ 0x0D00_0000);
+crate::mmio_default_access!(PpcMsg => System.hollywood.ipc.ppcmsg);
+
+// 0x0D00_0004 PPCCTRL
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+#[rustfmt::skip]
+pub struct PpcCtrl {
+ #[bits(0)] pub execute: bool,
+ #[bits(1)] pub ack_reply: bool,
+ #[bits(2)] pub ack_relaunch: bool,
+ #[bits(3)] pub relaunch: bool,
+ #[bits(4)] pub irq_relaunch_enable: bool,
+ #[bits(5)] pub irq_reply_enable: bool,
+}
+crate::mmio_reg!(PpcCtrl: u32 @ 0x0D00_0004);
+
+impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for PpcCtrl {
+ #[inline(always)]
+ fn read(sys: &mut System<SYSTEM>) -> Self {
+ sys.hollywood.ipc.ppcctrl
+ }
+
+ #[inline(always)]
+ fn write(self, sys: &mut System<SYSTEM>, _: WriteMask) {
+ let incoming = self;
+ let current = sys.hollywood.ipc.ppcctrl;
+
+ // these are write 1 to clear
+ sys.hollywood.ipc.ppcctrl = incoming
+ .with_ack_reply(current.ack_reply() && !incoming.ack_reply())
+ .with_ack_relaunch(current.ack_relaunch() && !incoming.ack_relaunch());
+
+ // dispatch via starlet
+ if sys.hollywood.ipc.ppcctrl.execute() {
+ let cmd_paddr = sys.hollywood.ipc.ppcmsg.raw();
+ sys.hollywood.ipc.ppcctrl = sys.hollywood.ipc.ppcctrl.with_execute(false);
+ sys.hollywood.ipc.state = IpcState::Processing;
+ tracing::info!(cmd_paddr = format!("{cmd_paddr:#010X}"), "PPC launched IPC command");
+
+ // TODO: crate::starlet::dispatch_command(sys, cmd_paddr);
+ }
+
+ // PPC has ACKed the response, reset
+ if !sys.hollywood.ipc.ppcctrl.ack_reply()
+ && !sys.hollywood.ipc.ppcctrl.ack_relaunch()
+ && sys.hollywood.ipc.state == IpcState::Done
+ {
+ // TODO: check state here?
+ sys.hollywood.ipc.state = IpcState::Idle;
+ crate::hollywood::irq::ack_ipc(sys);
+ }
+ }
+}
+
+// 0x0D00_0008 ARMMSG
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+pub struct ArmMsg {
+ #[bits(0..=31)]
+ pub value: u32,
+}
+
+crate::mmio_reg!(ArmMsg: u32 @ 0x0D00_0008);
+crate::mmio_default_access!(ArmMsg => System.hollywood.ipc.armmsg);
+
+// 0x0D00_000C ARMCTRL
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+pub struct ArmCtrl {
+ #[bits(0..=31)]
+ pub value: u32,
+}
+
+crate::mmio_reg!(ArmCtrl: u32 @ 0x0D00_000C);
+crate::mmio_default_access!(ArmCtrl => System.hollywood.ipc.armctrl);
+
+// 0x0D00_0030 HW_IRQ_CAUSE
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+pub struct Cause {
+ #[bits(30)]
+ pub ipc: bool,
+}
+
+crate::mmio_reg!(Cause: u32 @ 0x0D00_0030);
+
+impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for Cause {
+ #[inline(always)]
+ fn read(sys: &mut System<SYSTEM>) -> Self {
+ sys.hollywood.irq.cause
+ }
+
+ #[inline(always)]
+ fn write(self, sys: &mut System<SYSTEM>, _: WriteMask) {
+ let cleared = sys.hollywood.irq.cause.raw() & !self.raw();
+ sys.hollywood.irq.cause = Cause::from_raw(cleared);
+ crate::hollywood::irq::route_to_pi(sys);
+ }
+}
+
+// 0x0D00_0034 HW_IRQ_MASK
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Copy, Clone, Debug)]
+pub struct Mask {
+ #[bits(30)]
+ pub ipc: bool,
+}
+
+crate::mmio_reg!(Mask: u32 @ 0x0D00_0034);
+
+impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for Mask {
+ #[inline(always)]
+ fn read(sys: &mut System<SYSTEM>) -> Self {
+ sys.hollywood.irq.mask
+ }
+
+ #[inline(always)]
+ fn write(self, sys: &mut System<SYSTEM>, _: WriteMask) {
+ sys.hollywood.irq.mask = self;
+ crate::hollywood::irq::route_to_pi(sys);
+ }
+}
diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rs
index 60baa33..21d7349 100644
--- a/crates/gecko/src/lib.rs
+++ b/crates/gecko/src/lib.rs
@@ -9,6 +9,7 @@ pub mod idle;
pub mod ipl;
pub mod mmio;
pub mod scheduler;
+pub mod starlet;
pub mod system;
pub mod wii;
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
new file mode 100644
index 0000000..89b493a
--- /dev/null
+++ b/crates/gecko/src/starlet.rs
@@ -0,0 +1,7 @@
+pub struct Starlet;
+
+impl Starlet {
+ pub fn new() -> Self {
+ Starlet
+ }
+}
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index 197530c..bb2d05d 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -10,6 +10,7 @@ use crate::flipper::pi::ProcessorInterface;
use crate::flipper::si::{SerialInterface, pad};
use crate::flipper::vi::VideoInterface;
use crate::gekko::Gekko;
+use crate::hollywood::Hollywood;
#[cfg(feature = "hooks")]
use crate::hooks::{HookFilters, HookFlags, HookState, Host};
use crate::host::{EmptyRenderSink, RenderSink};
@@ -17,6 +18,7 @@ use crate::host::{EmptyRenderSink, RenderSink};
use crate::idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector};
use crate::mmio::Mmio;
use crate::scheduler::Scheduler;
+use crate::starlet::Starlet;
use image::Executable;
pub type SystemId = u8;
@@ -41,6 +43,10 @@ pub struct System<const SYSTEM: SystemId> {
pub ai: AudioInterface,
pub mi: MemoryInterface,
+ // Wii stuff.
+ pub starlet: Starlet,
+ pub hollywood: Hollywood,
+
/// GX dispatches actions here.
pub render_sink: Box<dyn RenderSink>,
@@ -74,6 +80,9 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
ai: AudioInterface::new(),
mi: MemoryInterface::new(),
+ starlet: Starlet::new(),
+ hollywood: Hollywood::new(),
+
render_sink: Box::new(EmptyRenderSink),
#[cfg(feature = "idle-skip")]