A GameCube/Wii emulator and debugger written in Rust.
now we also have VAT
Layle1ab52ccfda63
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rsindex f1f499e..6b0b409 100644--- a/crates/gekko/src/flipper/gx.rs+++ b/crates/gekko/src/flipper/gx.rs@@ -4,17 +4,21 @@ pub mod regs;use super::pi::InterruptFlag;use crate::{- flipper::gx::constants::{BP_REG_SIZE, CP_REG_SIZE, XF_MEM_SIZE},+ flipper::gx::{+ constants::{BP_REG_SIZE, CP_REG_SIZE, VAT_REG, VCD_LO_REG, XF_MEM_SIZE},+ regs::{AttributeType, VatA, VcdLo},+ },gekko::Gekko,+ mmio::Mmio,};-use fifo::{Fifo, FifoCmd};+use fifo::FifoCmd;pub struct Gx {pub raise_interrupt: bool,bp_regs: Vec<u32>,cp_regs: Vec<u32>,xf_mem: Vec<u32>,- fifo: Fifo,+ fifo: Vec<u8>,}impl Gx {@@ -24,42 +28,55 @@ impl Gx {bp_regs: vec![0; BP_REG_SIZE],cp_regs: vec![0; CP_REG_SIZE],xf_mem: vec![0; XF_MEM_SIZE],- fifo: Fifo::new(),+ fifo: Vec::with_capacity(256),}}- pub fn mmio_write_u8(&mut self, val: u8) {- self.fifo.push_u8(val);- self.drain_fifo();+ pub fn mmio_write_u8(&mut self, mmio: &mut Mmio, val: u8) {+ self.push_u8(val);+ self.drain_fifo(mmio);}- pub fn mmio_write_u16(&mut self, val: u16) {- self.fifo.push_u16(val);- self.drain_fifo();+ pub fn mmio_write_u16(&mut self, mmio: &mut Mmio, val: u16) {+ self.push_u16(val);+ self.drain_fifo(mmio);}- pub fn mmio_write_u32(&mut self, val: u32) {- self.fifo.push_u32(val);- self.drain_fifo();+ pub fn mmio_write_u32(&mut self, mmio: &mut Mmio, val: u32) {+ self.push_u32(val);+ self.drain_fifo(mmio);}- fn drain_fifo(&mut self) {- for cmd in self.fifo.drain() {+ fn drain_fifo(&mut self, mmio: &mut Mmio) {+ for cmd in self.drain() {match cmd {FifoCmd::Cp(data) => self.load_cp(&data),FifoCmd::Xf(data) => self.load_xf(&data),FifoCmd::Bp(data) => self.load_bp(&data),- _ => self.create_draw_call(cmd),+ _ => self.create_draw_call(mmio, cmd),}}}- fn create_draw_call(&mut self, cmd: FifoCmd) {- if let FifoCmd::DrawTriangles(data) = cmd {- tracing::debug!(data = format!("{:02X?}", data), "DrawTriangles");+ fn create_draw_call(&mut self, mmio: &mut Mmio, cmd: FifoCmd) {+ if let FifoCmd::DrawTriangles(cmd, data) = cmd {+ let vertex_format_index = cmd & 0b111;+ let (pos_attr, color0_attr) = self.vcd_attr_type(vertex_format_index as usize);+ tracing::debug!(+ data = format!("{:02X?}", data),+ ?pos_attr,+ ?color0_attr,+ vertex_format_index,+ "DrawTriangles"+ );}}+ fn vcd_attr_type(&self, vertex_format_index: usize) -> (AttributeType, AttributeType) {+ let vcd_lo = VcdLo::from_raw(self.cp_regs[VCD_LO_REG + vertex_format_index]);+ (vcd_lo.pos_attr(), vcd_lo.color0_attr())+ }+fn load_bp(&mut self, data: &[u8]) {let idx = data[0] as usize;let val = u32::from_be_bytes([0, data[1], data[2], data[3]]);@@ -80,12 +97,7 @@ impl Gx {fn load_cp(&mut self, data: &[u8]) {let idx = data[0] as usize;let val = u32::from_be_bytes([data[1], data[2], data[3], data[4]]);-- const REG_BASE: usize = 0x20;- let local = idx.wrapping_sub(REG_BASE);- if local < self.cp_regs.len() {- self.cp_regs[local] = val;- }+ self.cp_regs[idx] = val;tracing::debug!(reg_idx = format!("{idx:02X}"),diff --git a/crates/gekko/src/flipper/gx/constants.rs b/crates/gekko/src/flipper/gx/constants.rsindex cf1fcaa..ef05427 100644--- a/crates/gekko/src/flipper/gx/constants.rs+++ b/crates/gekko/src/flipper/gx/constants.rs@@ -6,4 +6,7 @@ pub const DRAW_TRIANGLES_CMD: u8 = 0x90;pub const BP_REG_SIZE: usize = 0x100;pub const CP_REG_SIZE: usize = 0xc0;-pub const XF_MEM_SIZE: usize = 0x1058;\ No newline at end of file+pub const XF_MEM_SIZE: usize = 0x1058;++pub const VCD_LO_REG: usize = 0x50;+pub const VAT_REG: usize = 0x70;\ No newline at end of filediff --git a/crates/gekko/src/flipper/gx/fifo.rs b/crates/gekko/src/flipper/gx/fifo.rsindex 38f4ac6..226d5c5 100644--- a/crates/gekko/src/flipper/gx/fifo.rs+++ b/crates/gekko/src/flipper/gx/fifo.rs@@ -1,28 +1,18 @@-use crate::flipper::gx::constants::DRAW_TRIANGLES_CMD;+use crate::flipper::gx::{Gx, constants::{DRAW_TRIANGLES_CMD, VCD_LO_REG}, regs::VcdLo};use super::constants::{BP_CMD, CP_CMD, XF_CMD};-pub struct Fifo {- buf: Vec<u8>,-}--impl Fifo {- pub fn new() -> Self {- Fifo {- buf: Vec::with_capacity(256),- }- }-+impl Gx {pub fn push_u8(&mut self, val: u8) {- self.buf.push(val);+ self.fifo.push(val);}pub fn push_u16(&mut self, val: u16) {- self.buf.extend_from_slice(&val.to_be_bytes());+ self.fifo.extend_from_slice(&val.to_be_bytes());}pub fn push_u32(&mut self, val: u32) {- self.buf.extend_from_slice(&val.to_be_bytes());+ self.fifo.extend_from_slice(&val.to_be_bytes());}/// Drain complete commands from the FIFO, returning each as a `FifoCmd`.@@ -31,19 +21,19 @@ impl Fifo {let mut pos = 0;loop {- let remaining = self.buf.len() - pos;+ let remaining = self.fifo.len() - pos;if remaining == 0 {break;}- let cmd = self.buf[pos];+ let cmd = self.fifo[pos];match cmd {CP_CMD => {// 1 cmd + 1 addr + 4 data = 6 bytesif remaining < 6 {break;}- let data: [u8; 5] = self.buf[pos + 1..pos + 6].try_into().unwrap();+ let data: [u8; 5] = self.fifo[pos + 1..pos + 6].try_into().unwrap();cmds.push(FifoCmd::Cp(data));pos += 6;}@@ -52,13 +42,13 @@ impl Fifo {if remaining < 5 {break;}- let length = u16::from_be_bytes([self.buf[pos + 1], self.buf[pos + 2]]) as usize;+ let length = u16::from_be_bytes([self.fifo[pos + 1], self.fifo[pos + 2]]) as usize;let n = length + 1;let total = 5 + n * 4;if remaining < total {break;}- let data = self.buf[pos + 1..pos + total].to_vec();+ let data = self.fifo[pos + 1..pos + total].to_vec();cmds.push(FifoCmd::Xf(data));pos += total;}@@ -67,7 +57,7 @@ impl Fifo {if remaining < 5 {break;}- let data: [u8; 4] = self.buf[pos + 1..pos + 5].try_into().unwrap();+ let data: [u8; 4] = self.fifo[pos + 1..pos + 5].try_into().unwrap();cmds.push(FifoCmd::Bp(data));pos += 5;}@@ -78,14 +68,15 @@ impl Fifo {break;}- let count = u16::from_be_bytes([self.buf[pos + 1], self.buf[pos + 2]]) as usize;- let total = 3 + count * 2; // TODO: Assuming each vertex is 2 bytes+ let count = u16::from_be_bytes([self.fifo[pos + 1], self.fifo[pos + 2]]) as usize;+ let vertex_format_index = (cmd & 0b111) as usize;+ let total = 3 + count * self.vertex_stride(vertex_format_index);if remaining < total {break;}- let vertex_data = self.buf[pos + 3..pos + total].to_vec();- cmds.push(FifoCmd::DrawTriangles(vertex_data));+ let vertex_data = self.fifo[pos + 3..pos + total].to_vec();+ cmds.push(FifoCmd::DrawTriangles(cmd, vertex_data));pos += total;}@@ -97,11 +88,16 @@ impl Fifo {}if pos > 0 {- self.buf.drain(..pos);+ self.fifo.drain(..pos);}cmds}++ fn vertex_stride(&self, vertex_format_index: usize) -> usize {+ let vcd_lo = VcdLo::from_raw(self.cp_regs[VCD_LO_REG + vertex_format_index]);+ vcd_lo.pos_attr().size() + vcd_lo.color0_attr().size()+ }}#[derive(Debug)]@@ -109,5 +105,5 @@ pub enum FifoCmd {Cp([u8; 5]),Xf(Vec<u8>),Bp([u8; 4]),- DrawTriangles(Vec<u8>),+ DrawTriangles(u8, Vec<u8>),}diff --git a/crates/gekko/src/flipper/gx/regs.rs b/crates/gekko/src/flipper/gx/regs.rsindex e69de29..bcf547b 100644--- a/crates/gekko/src/flipper/gx/regs.rs+++ b/crates/gekko/src/flipper/gx/regs.rs@@ -0,0 +1,122 @@+// CP 0x70+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Debug, Clone, Copy)]+pub struct VatA {+ #[bits(0)]+ pub pos_cnt: u8,++ #[bits(1..=3)]+ pub pos_fmt: u8,++ #[bits(4..=8)]+ pub pos_shift: u8,+}++#[derive(Debug, Clone, Copy)]+pub enum PosCount {+ Xy,+ Xyz,+}++#[derive(Debug, Clone, Copy)]+pub enum ComponentFormat {+ U8,+ S8,+ U16,+ S16,+ F32,+}++impl ComponentFormat {+ pub fn size(&self) -> usize {+ match self {+ ComponentFormat::U8 | ComponentFormat::S8 => 1,+ ComponentFormat::U16 | ComponentFormat::S16 => 2,+ ComponentFormat::F32 => 4,+ }+ }+}++impl VatA {+ pub fn pos_count(&self) -> PosCount {+ match self.pos_cnt() {+ 0 => PosCount::Xy,+ 1 => PosCount::Xyz,+ _ => unreachable!(),+ }+ }++ pub fn pos_format(&self) -> ComponentFormat {+ match self.pos_fmt() {+ 0 => ComponentFormat::U8,+ 1 => ComponentFormat::S8,+ 2 => ComponentFormat::U16,+ 3 => ComponentFormat::S16,+ 4 => ComponentFormat::F32,+ _ => unreachable!(),+ }+ }++ pub fn pos_components(&self) -> usize {+ match self.pos_count() {+ PosCount::Xy => 2,+ PosCount::Xyz => 3,+ }+ }++ pub fn pos_data_size(&self) -> usize {+ self.pos_components() * self.pos_format().size()+ }+}++// CP 0x50+#[chapa::bitfield(u32, order = lsb0)]+#[derive(Debug, Clone, Copy)]+pub struct VcdLo {+ #[bits(9..=10)]+ pub position: u8,++ #[bits(13..=14)]+ pub color0: u8,+}++impl VcdLo {+ pub fn pos_attr(&self) -> AttributeType {+ match self.position() {+ 0 => AttributeType::None,+ 1 => AttributeType::Direct,+ 2 => AttributeType::Index8,+ 3 => AttributeType::Index16,+ _ => unreachable!(),+ }+ }++ pub fn color0_attr(&self) -> AttributeType {+ match self.color0() {+ 0 => AttributeType::None,+ 1 => AttributeType::Direct,+ 2 => AttributeType::Index8,+ 3 => AttributeType::Index16,+ _ => unreachable!(),+ }+ }+}++#[derive(Debug)]+pub enum AttributeType {+ None,+ Direct,+ Index8,+ Index16,+}++impl AttributeType {+ pub fn size(&self) -> usize {+ match self {+ AttributeType::None => 0,+ AttributeType::Index8 => 1,+ AttributeType::Index16 => 2,+ AttributeType::Direct => unimplemented!("from VAT?"),+ }+ }+}diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rsindex f8c8d20..1948702 100644--- a/crates/gekko/src/mmio/bus.rs+++ b/crates/gekko/src/mmio/bus.rs@@ -93,7 +93,7 @@ impl Gekko {self.exi.process_dma_transfers(&mut self.mmio);}BusTarget::Gx => {- self.gx.mmio_write_u8(val);+ self.gx.mmio_write_u8(&mut self.mmio, val);self.check_gx_pe_finish();}BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),@@ -115,7 +115,7 @@ impl Gekko {self.exi.process_dma_transfers(&mut self.mmio);}BusTarget::Gx => {- self.gx.mmio_write_u16(val);+ self.gx.mmio_write_u16(&mut self.mmio, val);self.check_gx_pe_finish();}BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),@@ -137,7 +137,7 @@ impl Gekko {self.exi.process_dma_transfers(&mut self.mmio);}BusTarget::Gx => {- self.gx.mmio_write_u32(val);+ self.gx.mmio_write_u32(&mut self.mmio, val);self.check_gx_pe_finish();}BusTarget::Fallback => self.mmio.phys_write_u32(offset, val),