Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

indexed xf (sunshine menu visible!)

Layle38b495b99647
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index 54a4928..7e0e193 100644
--- a/crates/gecko/src/flipper/gx.rs
+++ b/crates/gecko/src/flipper/gx.rs
@@ -121,6 +121,14 @@ impl GraphicsProcessor {
FifoCmd::Cp(data) => self.load_cp(&data),
FifoCmd::Xf(data) => self.load_xf(renderer, &data),
FifoCmd::Bp(data) => self.load_bp(renderer, &mmio.ram, &data),
+ FifoCmd::LoadIndexedXf {
+ cp_array_index,
+ index,
+ xf_addr,
+ xf_count,
+ } => {
+ self.load_indexed_xf(renderer, &mmio.ram, cp_array_index, index, xf_addr, xf_count);
+ }
FifoCmd::CallDisplayList { phys_addr, nbytes } => {
let addr = (phys_addr & 0x3FFFFFFF) as usize;
let len = nbytes as usize;
diff --git a/crates/gecko/src/flipper/gx/constants.rs b/crates/gecko/src/flipper/gx/constants.rs
index 2a90dae..ef9bdea 100644
--- a/crates/gecko/src/flipper/gx/constants.rs
+++ b/crates/gecko/src/flipper/gx/constants.rs
@@ -3,6 +3,10 @@ pub const CP_CMD: u8 = 0x08;
pub const XF_CMD: u8 = 0x10;
pub const CALL_DL_CMD: u8 = 0x40;
pub const INV_VTX_CACHE_CMD: u8 = 0x48;
+pub const LOAD_INDX_A_CMD: u8 = 0x20;
+pub const LOAD_INDX_B_CMD: u8 = 0x28;
+pub const LOAD_INDX_C_CMD: u8 = 0x30;
+pub const LOAD_INDX_D_CMD: u8 = 0x38;
pub const BP_CMD: u8 = 0x61;
pub const DRAW_COMMANDS_START: u8 = 0x80;
@@ -33,6 +37,10 @@ pub const ARRAY_NRM: usize = 1;
pub const ARRAY_CLR0: usize = 2;
pub const ARRAY_CLR1: usize = 3;
pub const ARRAY_TEX0: usize = 4;
+pub const ARRAY_POS_NRM_MTX: usize = 12; // INDX A
+pub const ARRAY_NRM_MTX: usize = 13; // INDX B
+pub const ARRAY_POST_MTX: usize = 14; // INDX C
+pub const ARRAY_LIGHT: usize = 15; // INDX D
// XF memory addresses
pub const XF_MODELVIEW_BASE: usize = 0x0000;
diff --git a/crates/gecko/src/flipper/gx/fifo.rs b/crates/gecko/src/flipper/gx/fifo.rs
index 5480d03..08afd59 100644
--- a/crates/gecko/src/flipper/gx/fifo.rs
+++ b/crates/gecko/src/flipper/gx/fifo.rs
@@ -1,4 +1,7 @@
-use super::constants::{BP_CMD, CALL_DL_CMD, CP_CMD, INV_VTX_CACHE_CMD, NOP_CMD, XF_CMD};
+use super::constants::{
+ ARRAY_LIGHT, ARRAY_NRM_MTX, ARRAY_POS_NRM_MTX, ARRAY_POST_MTX, BP_CMD, CALL_DL_CMD, CP_CMD, INV_VTX_CACHE_CMD,
+ LOAD_INDX_A_CMD, LOAD_INDX_B_CMD, LOAD_INDX_C_CMD, LOAD_INDX_D_CMD, NOP_CMD, XF_CMD,
+};
use crate::flipper::gx::GraphicsProcessor;
use crate::flipper::gx::constants::{
DRAW_COMMANDS_END, DRAW_COMMANDS_START, VATA_REG, VATB_REG, VATC_REG, VCD_HI_REG, VCD_LO_REG,
@@ -93,6 +96,35 @@ impl GraphicsProcessor {
cur.read_exact(&mut data).unwrap();
cmds.push(FifoCmd::Bp(data));
}
+ LOAD_INDX_A_CMD | LOAD_INDX_B_CMD | LOAD_INDX_C_CMD | LOAD_INDX_D_CMD => {
+ // 4 bytes payload: 2-byte index + 2-byte descriptor
+ if remaining < 5 {
+ cur.set_position(pos as u64);
+ break;
+ }
+ let mut payload = [0u8; 4];
+ cur.read_exact(&mut payload).unwrap();
+
+ let index = u16::from_be_bytes([payload[0], payload[1]]);
+ let descriptor = u16::from_be_bytes([payload[2], payload[3]]);
+ let xf_addr = descriptor & 0x0FFF;
+ let xf_count = ((descriptor >> 12) & 0xF) as u8 + 1;
+
+ let cp_array_index = match cmd {
+ LOAD_INDX_A_CMD => ARRAY_POS_NRM_MTX,
+ LOAD_INDX_B_CMD => ARRAY_NRM_MTX,
+ LOAD_INDX_C_CMD => ARRAY_POST_MTX,
+ LOAD_INDX_D_CMD => ARRAY_LIGHT,
+ _ => unreachable!(),
+ } as u8;
+
+ cmds.push(FifoCmd::LoadIndexedXf {
+ cp_array_index,
+ index,
+ xf_addr,
+ xf_count,
+ });
+ }
DRAW_COMMANDS_START..=DRAW_COMMANDS_END => {
// [count_hi] [count_lo] [vertex_0_data...] ...
if remaining < 3 {
@@ -165,6 +197,15 @@ pub enum FifoCmd {
Cp([u8; 5]),
Xf(Vec<u8>),
Bp([u8; 4]),
- CallDisplayList { phys_addr: u32, nbytes: u32 },
+ LoadIndexedXf {
+ cp_array_index: u8,
+ index: u16,
+ xf_addr: u16,
+ xf_count: u8,
+ },
+ CallDisplayList {
+ phys_addr: u32,
+ nbytes: u32,
+ },
DrawCall(u8, Vec<u8>),
}
diff --git a/crates/gecko/src/flipper/gx/xf.rs b/crates/gecko/src/flipper/gx/xf.rs
index a21eca1..99b021e 100644
--- a/crates/gecko/src/flipper/gx/xf.rs
+++ b/crates/gecko/src/flipper/gx/xf.rs
@@ -1,6 +1,7 @@
use super::constants::{
- DEPTH_24_BIT_MAX, XF_PROJECTION_BASE, XF_PROJECTION_END, XF_VIEWPORT_BASE, XF_VIEWPORT_END, XF_VIEWPORT_OFFSET_X,
- XF_VIEWPORT_OFFSET_Y, XF_VIEWPORT_OFFSET_Z, XF_VIEWPORT_SCALE_X, XF_VIEWPORT_SCALE_Y, XF_VIEWPORT_SCALE_Z,
+ ARRAY_BASE_REG, ARRAY_STRIDE_REG, DEPTH_24_BIT_MAX, XF_PROJECTION_BASE, XF_PROJECTION_END, XF_VIEWPORT_BASE,
+ XF_VIEWPORT_END, XF_VIEWPORT_OFFSET_X, XF_VIEWPORT_OFFSET_Y, XF_VIEWPORT_OFFSET_Z, XF_VIEWPORT_SCALE_X,
+ XF_VIEWPORT_SCALE_Y, XF_VIEWPORT_SCALE_Z,
};
use super::math::Vec3;
use super::{GraphicsProcessor, draw};
@@ -134,4 +135,50 @@ impl GraphicsProcessor {
renderer.exec(GxAction::SetViewport(self.cur_viewport));
}
}
+
+ #[inline(always)]
+ pub fn load_indexed_xf(
+ &mut self,
+ renderer: &mut dyn RenderSink,
+ ram: &[u8],
+ cp_array_index: u8,
+ index: u16,
+ xf_addr: u16,
+ xf_count: u8,
+ ) {
+ let arr_idx = cp_array_index as usize;
+ let base = (self.cp_regs[ARRAY_BASE_REG + arr_idx] & 0x3FFFFFFF) as usize;
+ let stride = self.cp_regs[ARRAY_STRIDE_REG + arr_idx] as usize;
+ let src_addr = base + (index as usize) * stride;
+ let dst_addr = xf_addr as usize;
+ let n = xf_count as usize;
+ let end = dst_addr + n;
+
+ for i in 0..n {
+ let ram_offset = src_addr + i * 4;
+ let val = u32::from_be_bytes([
+ ram[ram_offset],
+ ram[ram_offset + 1],
+ ram[ram_offset + 2],
+ ram[ram_offset + 3],
+ ]);
+ let reg = dst_addr + i;
+ if reg < self.xf_mem.len() {
+ self.xf_mem[reg] = val;
+ }
+ }
+
+ if dst_addr <= XF_PROJECTION_END && end > XF_PROJECTION_BASE {
+ self.rebuild_projection();
+ renderer.exec(GxAction::SetProjection {
+ matrix: self.projection.0,
+ is_perspective: self.xf_mem[XF_PROJECTION_END] == 0,
+ });
+ }
+
+ if dst_addr <= XF_VIEWPORT_END && end > XF_VIEWPORT_BASE {
+ self.rebuild_viewport();
+ renderer.exec(GxAction::SetViewport(self.cur_viewport));
+ }
+ }
}