Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

use actual cursor

Layle9cea82ac0659
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rs
index 18d633c..594730b 100644
--- a/crates/gekko/src/flipper/gx.rs
+++ b/crates/gekko/src/flipper/gx.rs
@@ -17,6 +17,7 @@ use crate::{
mmio::Mmio,
};
use fifo::FifoCmd;
+use std::io::{Cursor, Read};
pub struct Gx {
pub raise_interrupt: bool,
@@ -88,23 +89,23 @@ impl Gx {
let vertex_count = data.len() / vertex_stride;
let mut vertices: Vec<draw::Vertex> = Vec::with_capacity(vertex_count);
- let mut cursor = 0;
+ let mut cur = Cursor::new(&data);
for i in 0..vertex_count {
// Read position index
- let pos_index = read_index(&data, &mut cursor, vcd_lo.position());
+ let pos_index = read_index(&mut cur, vcd_lo.position());
let pos_addr = pos_base + pos_index * pos_stride;
let pos_size = vat_a.pos_data_size();
let pos_data = &mmio.ram[pos_addr..pos_addr + pos_size];
// Read color0 index
- let clr0_index = read_index(&data, &mut cursor, vcd_lo.color0());
+ let clr0_index = read_index(&mut cur, vcd_lo.color0());
let clr0_addr = clr0_base + clr0_index * clr0_stride;
let clr0_size = vat_a.clr0_data_size();
let clr0_data = &mmio.ram[clr0_addr..clr0_addr + clr0_size];
// Skip tex0 bytes
- cursor += tex0_size;
+ cur.set_position(cur.position() + tex0_size as u64);
tracing::debug!(
vertex = i,
@@ -329,17 +330,17 @@ impl Gx {
}
}
-fn read_index(data: &[u8], cursor: &mut usize, attr: regs::AttributeType) -> usize {
+fn read_index(cur: &mut Cursor<&Vec<u8>>, attr: regs::AttributeType) -> usize {
match attr {
regs::AttributeType::Index8 => {
- let idx = data[*cursor] as usize;
- *cursor += 1;
- idx
+ let mut buf = [0u8; 1];
+ cur.read_exact(&mut buf).unwrap();
+ buf[0] as usize
}
regs::AttributeType::Index16 => {
- let idx = u16::from_be_bytes([data[*cursor], data[*cursor + 1]]) as usize;
- *cursor += 2;
- idx
+ let mut buf = [0u8; 2];
+ cur.read_exact(&mut buf).unwrap();
+ u16::from_be_bytes(buf) as usize
}
_ => 0,
}
diff --git a/crates/gekko/src/flipper/gx/fifo.rs b/crates/gekko/src/flipper/gx/fifo.rs
index b9cd5c3..ce2b77f 100644
--- a/crates/gekko/src/flipper/gx/fifo.rs
+++ b/crates/gekko/src/flipper/gx/fifo.rs
@@ -4,6 +4,7 @@ use crate::flipper::gx::{
constants::{DRAW_COMMANDS_END, DRAW_COMMANDS_START, VATA_REG, VCD_HI_REG, VCD_LO_REG},
regs::{AttributeType, VatA, VcdHi, VcdLo},
};
+use std::io::{Cursor, Read};
impl Gx {
pub fn push_u8(&mut self, val: u8) {
@@ -21,77 +22,89 @@ impl Gx {
/// Drain complete commands from the FIFO, returning each as a `FifoCmd`.
pub fn drain(&mut self) -> Vec<FifoCmd> {
let mut cmds = Vec::new();
- let mut pos = 0;
+ let mut cur = Cursor::new(&self.fifo);
loop {
+ let pos = cur.position() as usize;
let remaining = self.fifo.len() - pos;
if remaining == 0 {
break;
}
- let cmd = self.fifo[pos];
+ let mut cmd_buf = [0u8; 1];
+ cur.read_exact(&mut cmd_buf).unwrap();
+ let cmd = cmd_buf[0];
+
match cmd {
CP_CMD => {
- // 1 cmd + 1 addr + 4 data = 6 bytes
+ // 1 addr + 4 data = 5 bytes
if remaining < 6 {
+ cur.set_position(pos as u64);
break;
}
- let data: [u8; 5] = self.fifo[pos + 1..pos + 6].try_into().unwrap();
+ let mut data = [0u8; 5];
+ cur.read_exact(&mut data).unwrap();
cmds.push(FifoCmd::Cp(data));
- pos += 6;
}
XF_CMD => {
- // 1 cmd + 2 length + 2 addr = 5 byte header minimum
+ // 2 length + 2 addr = 4 byte header
if remaining < 5 {
+ cur.set_position(pos as u64);
break;
}
- let length = u16::from_be_bytes([self.fifo[pos + 1], self.fifo[pos + 2]]) as usize;
+ let mut header = [0u8; 4];
+ cur.read_exact(&mut header).unwrap();
+ let length = u16::from_be_bytes([header[0], header[1]]) as usize;
let n = length + 1;
let total = 5 + n * 4;
if remaining < total {
+ cur.set_position(pos as u64);
break;
}
- let data = self.fifo[pos + 1..pos + total].to_vec();
+ let mut rest = vec![0u8; n * 4];
+ cur.read_exact(&mut rest).unwrap();
+ let data = [header.as_slice(), rest.as_slice()].concat();
cmds.push(FifoCmd::Xf(data));
- pos += total;
}
BP_CMD => {
- // 1 cmd + 4 data = 5 bytes
+ // 4 data bytes
if remaining < 5 {
+ cur.set_position(pos as u64);
break;
}
- let data: [u8; 4] = self.fifo[pos + 1..pos + 5].try_into().unwrap();
+ let mut data = [0u8; 4];
+ cur.read_exact(&mut data).unwrap();
cmds.push(FifoCmd::Bp(data));
- pos += 5;
}
DRAW_COMMANDS_START..=DRAW_COMMANDS_END => {
- // 1 command + minimum 2 vertex count
- // [cmd_byte] [count_hi] [count_lo] [vertex_0_data...] [vertex_1_data...] ...
+ // [count_hi] [count_lo] [vertex_0_data...] ...
if remaining < 3 {
+ cur.set_position(pos as u64);
break;
}
-
- let count = u16::from_be_bytes([self.fifo[pos + 1], self.fifo[pos + 2]]) as usize;
+ let mut count_buf = [0u8; 2];
+ cur.read_exact(&mut count_buf).unwrap();
+ let count = u16::from_be_bytes(count_buf) as usize;
let vertex_format_index = (cmd & 0b111) as usize;
- let total = 3 + count * self.vertex_stride(vertex_format_index);
+ let vertex_data_len = count * self.vertex_stride(vertex_format_index);
+ let total = 3 + vertex_data_len;
if remaining < total {
+ cur.set_position(pos as u64);
break;
}
-
- let vertex_data = self.fifo[pos + 3..pos + total].to_vec();
+ let mut vertex_data = vec![0u8; vertex_data_len];
+ cur.read_exact(&mut vertex_data).unwrap();
cmds.push(FifoCmd::DrawCall(cmd, vertex_data));
-
- pos += total;
}
_ => {
tracing::error!(cmd = format!("{cmd:02X}"), "unknown FIFO command");
- pos += 1;
}
}
}
- if pos > 0 {
- self.fifo.drain(..pos);
+ let consumed = cur.position() as usize;
+ if consumed > 0 {
+ self.fifo.drain(..consumed);
}
cmds