Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

not sure what im doing ill be honest

Layle8961e6efa4ff
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 095af1a..7dff10d 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -1,10 +1,40 @@
+pub mod stm;
+
+use crate::Wii;
use crate::hollywood::regs::{ArmCtrl, ArmMsg, PpcCtrl, PpcMsg};
+use crate::system::{System, SystemId};
+
+pub const IPC_EINVAL: i32 = -4;
+pub const IPC_ENOENT: i32 = -6;
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum IpcState {
- Idle,
- Processing,
- Done,
+pub trait IosDevice: Send {
+ fn open(&mut self, _wii: &mut Wii, _mode: u32) -> i32 {
+ 0
+ }
+
+ fn close(&mut self, _wii: &mut Wii) -> i32 {
+ 0
+ }
+
+ fn read(&mut self, _wii: &mut Wii, _buf: u32, _len: u32) -> i32 {
+ IPC_EINVAL
+ }
+
+ fn write(&mut self, _wii: &mut Wii, _buf: u32, _len: u32) -> i32 {
+ IPC_EINVAL
+ }
+
+ fn seek(&mut self, _wii: &mut Wii, _where_: i32, _whence: i32) -> i32 {
+ 0
+ }
+
+ fn ioctl(&mut self, _wii: &mut Wii, _cmd: u32, _in_buf: u32, _in_len: u32, _out_buf: u32, _out_len: u32) -> i32 {
+ IPC_EINVAL
+ }
+
+ fn ioctlv(&mut self, _wii: &mut Wii, _cmd: u32, _argcin: u32, _argcio: u32, _vec: u32) -> i32 {
+ IPC_EINVAL
+ }
}
pub struct Ipc {
@@ -12,7 +42,6 @@ pub struct Ipc {
pub ppcctrl: PpcCtrl,
pub armmsg: ArmMsg,
pub armctrl: ArmCtrl,
- pub state: IpcState,
}
impl Ipc {
@@ -22,7 +51,6 @@ impl Ipc {
ppcctrl: PpcCtrl::from_raw(0),
armmsg: ArmMsg::from_raw(0),
armctrl: ArmCtrl::from_raw(0),
- state: IpcState::Idle,
}
}
}
@@ -37,3 +65,14 @@ crate::mmio_device_dispatch! {
crate::hollywood::regs::ArmCtrl,
],
}
+
+pub fn deliver_response<const SYSTEM: SystemId>(
+ sys: &mut System<SYSTEM>,
+ cmd_paddr: u32,
+ result: i32,
+) {
+ sys.mmio.phys_write_u32(cmd_paddr + 0x04, result as u32);
+ sys.hollywood.ipc.armmsg = ArmMsg::from_raw(cmd_paddr);
+ sys.hollywood.ipc.ppcctrl = sys.hollywood.ipc.ppcctrl.with_ack_reply(true);
+ crate::hollywood::irq::assert_ipc(sys);
+}
diff --git a/crates/gecko/src/hollywood/ipc/stm.rs b/crates/gecko/src/hollywood/ipc/stm.rs
new file mode 100644
index 0000000..d0a7e79
--- /dev/null
+++ b/crates/gecko/src/hollywood/ipc/stm.rs
@@ -0,0 +1,3 @@
+pub struct Stm;
+
+impl super::IosDevice for Stm {}
diff --git a/crates/gecko/src/hollywood/regs.rs b/crates/gecko/src/hollywood/regs.rs
index 9aa2cfd..00f7fb3 100644
--- a/crates/gecko/src/hollywood/regs.rs
+++ b/crates/gecko/src/hollywood/regs.rs
@@ -1,6 +1,5 @@
use crate::{
System, SystemId,
- hollywood::ipc::IpcState,
mmio::traits::{MmioAccess, WriteMask},
};
@@ -49,20 +48,17 @@ impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for PpcCtrl {
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);
+ 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;
+ // PPC just ACKed a pending reply
+ let ack_happened =
+ (current.ack_reply() && incoming.ack_reply()) || (current.ack_relaunch() && incoming.ack_relaunch());
+ if ack_happened {
crate::hollywood::irq::ack_ipc(sys);
+ crate::starlet::schedule_drain(sys);
}
}
}
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index 89b493a..cd91331 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -1,7 +1,100 @@
-pub struct Starlet;
+use crate::system::SystemId;
+use crate::{System, hollywood::ipc::IosDevice};
+use std::collections::{HashMap, VecDeque};
+
+// As per zayd
+const FINALIZE_DELAY_CYCLES: u64 = 10_000;
+const ACK_TO_NEXT_DELAY_CYCLES: u64 = 500;
+
+pub struct PendingResponse {
+ pub cmd_paddr: u32,
+ pub result: i32,
+}
+
+pub struct Starlet {
+ devices: HashMap<String, Box<dyn IosDevice>>,
+ handles: HashMap<i32, String>,
+ next_fd: i32,
+ pub pending: VecDeque<PendingResponse>,
+}
impl Starlet {
pub fn new() -> Self {
- Starlet
+ Starlet {
+ devices: HashMap::new(),
+ handles: HashMap::new(),
+ next_fd: 1,
+ pending: VecDeque::new(),
+ }
+ }
+
+ /// Install a device at `path`. Replaces any existing registration.
+ pub fn register(&mut self, path: &str, dev: Box<dyn IosDevice>) {
+ self.devices.insert(path.to_owned(), dev);
+ }
+
+ /// Allocate a fresh fd and bind it to `path`.
+ pub fn allocate_fd(&mut self, path: &str) -> i32 {
+ let fd = self.next_fd;
+ self.next_fd = self.next_fd.checked_add(1).expect("Starlet fd overflow");
+ self.handles.insert(fd, path.to_owned());
+ fd
+ }
+
+ /// Drop the fd to path mapping. Returns the path the fd was bound to.
+ pub fn release_fd(&mut self, fd: i32) -> Option<String> {
+ self.handles.remove(&fd)
+ }
+
+ pub fn device_for_path(&mut self, path: &str) -> Option<&mut Box<dyn IosDevice>> {
+ self.devices.get_mut(path)
+ }
+
+ pub fn device_for_fd(&mut self, fd: i32) -> Option<&mut Box<dyn IosDevice>> {
+ let path = self.handles.get(&fd)?.clone();
+ self.devices.get_mut(&path)
}
}
+
+impl System<{ crate::WII }> {
+ pub fn initialize_starlet_devices(&mut self) {
+ self.starlet
+ .register("/dev/stm/immediate", Box::new(crate::hollywood::ipc::stm::Stm));
+ }
+}
+
+pub fn dispatch_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) {
+ let result = self::process_command(sys, cmd_paddr);
+
+ sys.starlet.pending.push_back(PendingResponse { cmd_paddr, result });
+ sys.scheduler
+ .schedule_in(FINALIZE_DELAY_CYCLES, self::deliver_pending::<SYSTEM>);
+}
+
+pub fn schedule_drain<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ sys.scheduler
+ .schedule_in(ACK_TO_NEXT_DELAY_CYCLES, self::deliver_pending::<SYSTEM>);
+}
+
+fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) -> i32 {
+ let cmd = sys.mmio.phys_read_u32(cmd_paddr);
+ tracing::warn!(
+ cmd,
+ paddr = format!("{cmd_paddr:#010X}"),
+ "Starlet command decoder not implemented; returning IPC_ENOENT"
+ );
+
+ crate::hollywood::ipc::IPC_ENOENT
+}
+
+fn deliver_pending<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ if sys.hollywood.ipc.ppcctrl.ack_reply() {
+ // PPC slot still occupied. Wait for the ack.
+ return;
+ }
+
+ let Some(p) = sys.starlet.pending.pop_front() else {
+ return;
+ };
+ crate::hollywood::ipc::deliver_response(sys, p.cmd_paddr, p.result);
+}
diff --git a/crates/gecko/src/wii.rs b/crates/gecko/src/wii.rs
index 47ea8f6..4816111 100644
--- a/crates/gecko/src/wii.rs
+++ b/crates/gecko/src/wii.rs
@@ -34,7 +34,9 @@ const APPLOADER_RETURN_TRAP: u32 = 0x6900_0000;
impl Wii {
pub fn new(entrypoint: u32) -> Self {
- Self::with_scheduler(entrypoint, Scheduler::new_wii())
+ let mut emu = Self::with_scheduler(entrypoint, Scheduler::new_wii());
+ emu.initialize_starlet_devices();
+ emu
}
pub fn with_image(exe: &impl Executable) -> Self {