A GameCube/Wii emulator and debugger written in Rust.
support wii rvz
Laylee0c751db244f
diff --git a/crates/dbglib/src/windows/cpu.rs b/crates/dbglib/src/windows/cpu.rsindex b1877d6..09f746b 100644--- a/crates/dbglib/src/windows/cpu.rs+++ b/crates/dbglib/src/windows/cpu.rs@@ -58,10 +58,22 @@ pub fn show_cpu(ui.end_row();for (name, getter) in [- ("LT", (|i: u8, c: &Gekko| c.cr.get_field(i).lt()) as fn(u8, &Gekko) -> bool),- ("GT", (|i: u8, c: &Gekko| c.cr.get_field(i).gt()) as fn(u8, &Gekko) -> bool),- ("EQ", (|i: u8, c: &Gekko| c.cr.get_field(i).eq()) as fn(u8, &Gekko) -> bool),- ("SO", (|i: u8, c: &Gekko| c.cr.get_field(i).so()) as fn(u8, &Gekko) -> bool),+ (+ "LT",+ (|i: u8, c: &Gekko| c.cr.get_field(i).lt()) as fn(u8, &Gekko) -> bool,+ ),+ (+ "GT",+ (|i: u8, c: &Gekko| c.cr.get_field(i).gt()) as fn(u8, &Gekko) -> bool,+ ),+ (+ "EQ",+ (|i: u8, c: &Gekko| c.cr.get_field(i).eq()) as fn(u8, &Gekko) -> bool,+ ),+ (+ "SO",+ (|i: u8, c: &Gekko| c.cr.get_field(i).so()) as fn(u8, &Gekko) -> bool,+ ),] {ui.label(name);for i in 0..8u8 {diff --git a/crates/dbglib/src/windows/irq.rs b/crates/dbglib/src/windows/irq.rsindex 621a1bd..3d0251c 100644--- a/crates/dbglib/src/windows/irq.rs+++ b/crates/dbglib/src/windows/irq.rs@@ -1,6 +1,6 @@use egui::{Align, Color32, Context, Grid, Layout, RichText};-use gecko::gekko::Gekko;use gecko::flipper::pi::ProcessorInterface;+use gecko::gekko::Gekko;fn centered_icon(ui: &mut egui::Ui, icon: &str, color: Color32) {ui.with_layout(Layout::top_down(Align::Center), |ui| {diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rsindex 4377135..4810fea 100644--- a/crates/gecko/src/gamecube.rs+++ b/crates/gecko/src/gamecube.rs@@ -1,5 +1,5 @@-use crate::gekko::IPL_RESET_VECTOR;use crate::flipper::exi::macronix::ExiMacronix;+use crate::gekko::IPL_RESET_VECTOR;use crate::ipl::IPL_HLE;use crate::scheduler::Scheduler;use crate::system::{GC, System};diff --git a/crates/gecko/src/gekko/interpreter/alu.rs b/crates/gecko/src/gekko/interpreter/alu.rsindex 3117697..e93ed36 100644--- a/crates/gecko/src/gekko/interpreter/alu.rs+++ b/crates/gecko/src/gekko/interpreter/alu.rs@@ -2,7 +2,10 @@pub fn alu<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {match OP {crate::gekko::lut::OP_ADDX => {- let res = ctx.gekko.read_gpr(instr.ra()).wrapping_add(ctx.gekko.read_gpr(instr.rb()));+ let res = ctx+ .gekko+ .read_gpr(instr.ra())+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));ctx.gekko.write_gpr(instr.rd(), res);if instr.rc() {ctx.gekko.update_cr0(res);@@ -44,7 +47,10 @@ pub fn alu<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekctx.gekko.update_cr0(val);}crate::gekko::lut::OP_SUBFX => {- let res = ctx.gekko.read_gpr(instr.rb()).wrapping_sub(ctx.gekko.read_gpr(instr.ra()));+ let res = ctx+ .gekko+ .read_gpr(instr.rb())+ .wrapping_sub(ctx.gekko.read_gpr(instr.ra()));ctx.gekko.write_gpr(instr.rd(), res);if instr.rc() {ctx.gekko.update_cr0(res);@@ -181,8 +187,8 @@ pub fn alu<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gek}}crate::gekko::lut::OP_MULHWX => {- let res = ((ctx.gekko.read_gpr(instr.ra()) as i32 as i64 * ctx.gekko.read_gpr(instr.rb()) as i32 as i64) >> 32)- as u32;+ let res = ((ctx.gekko.read_gpr(instr.ra()) as i32 as i64 * ctx.gekko.read_gpr(instr.rb()) as i32 as i64)+ >> 32) as u32;ctx.gekko.write_gpr(instr.rd(), res);if instr.rc() {ctx.gekko.update_cr0(res);diff --git a/crates/gecko/src/gekko/interpreter/fp_ops.rs b/crates/gecko/src/gekko/interpreter/fp_ops.rsindex 95e8e3b..dcac615 100644--- a/crates/gecko/src/gekko/interpreter/fp_ops.rs+++ b/crates/gecko/src/gekko/interpreter/fp_ops.rs@@ -111,10 +111,26 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::};ctx.gekko.cr.set_field(instr.crfd(), cf);}- crate::gekko::lut::OP_FADDX => fp_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_fpr(instr.rb())),- crate::gekko::lut::OP_FSUBX => fp_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()) - ctx.gekko.read_fpr(instr.rb())),- crate::gekko::lut::OP_FMULX => fp_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc())),- crate::gekko::lut::OP_FDIVX => fp_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()) / ctx.gekko.read_fpr(instr.rb())),+ crate::gekko::lut::OP_FADDX => fp_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_fpr(instr.rb()),+ ),+ crate::gekko::lut::OP_FSUBX => fp_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()) - ctx.gekko.read_fpr(instr.rb()),+ ),+ crate::gekko::lut::OP_FMULX => fp_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()),+ ),+ crate::gekko::lut::OP_FDIVX => fp_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()) / ctx.gekko.read_fpr(instr.rb()),+ ),crate::gekko::lut::OP_FMADDX => fp_write(ctx,&instr,@@ -158,12 +174,14 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::crate::gekko::lut::OP_FMADDSX => fp_write_single(ctx,&instr,- (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) + ctx.gekko.read_fpr(instr.rb())) as f32 as f64,+ (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) + ctx.gekko.read_fpr(instr.rb())) as f32+ as f64,),crate::gekko::lut::OP_FMSUBSX => fp_write_single(ctx,&instr,- (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) - ctx.gekko.read_fpr(instr.rb())) as f32 as f64,+ (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) - ctx.gekko.read_fpr(instr.rb())) as f32+ as f64,),crate::gekko::lut::OP_FNMADDSX => fp_write_single(ctx,diff --git a/crates/gecko/src/gekko/interpreter/ps_ops.rs b/crates/gecko/src/gekko/interpreter/ps_ops.rsindex 1b28772..f70a62f 100644--- a/crates/gecko/src/gekko/interpreter/ps_ops.rs+++ b/crates/gecko/src/gekko/interpreter/ps_ops.rs@@ -42,17 +42,17 @@ pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::ps_write(ctx, &instr, ps0, ps1);}crate::gekko::lut::OP_PS_NMADD => {- let ps0 = -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) + ctx.gekko.read_fpr(instr.rb()))- as f32 as f64;- let ps1 = -(ctx.gekko.read_ps1(instr.ra()) * ctx.gekko.read_ps1(instr.fc()) + ctx.gekko.read_ps1(instr.rb()))- as f32 as f64;+ let ps0 = -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc())+ + ctx.gekko.read_fpr(instr.rb())) as f32 as f64;+ let ps1 = -(ctx.gekko.read_ps1(instr.ra()) * ctx.gekko.read_ps1(instr.fc())+ + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;ps_write(ctx, &instr, ps0, ps1);}crate::gekko::lut::OP_PS_NMSUB => {- let ps0 = -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) - ctx.gekko.read_fpr(instr.rb()))- as f32 as f64;- let ps1 = -(ctx.gekko.read_ps1(instr.ra()) * ctx.gekko.read_ps1(instr.fc()) - ctx.gekko.read_ps1(instr.rb()))- as f32 as f64;+ let ps0 = -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc())+ - ctx.gekko.read_fpr(instr.rb())) as f32 as f64;+ let ps1 = -(ctx.gekko.read_ps1(instr.ra()) * ctx.gekko.read_ps1(instr.fc())+ - ctx.gekko.read_ps1(instr.rb())) as f32 as f64;ps_write(ctx, &instr, ps0, ps1);}crate::gekko::lut::OP_PS_MULS0 => {@@ -90,7 +90,12 @@ pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::ps_write(ctx, &instr, ps0, ps1);}crate::gekko::lut::OP_PS_MR => {- ps_write(ctx, &instr, ctx.gekko.read_fpr(instr.rb()), ctx.gekko.read_ps1(instr.rb()));+ ps_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.rb()),+ ctx.gekko.read_ps1(instr.rb()),+ );}crate::gekko::lut::OP_PS_NEG => {ps_write(@@ -117,16 +122,36 @@ pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::);}crate::gekko::lut::OP_PS_MERGE00 => {- ps_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()), ctx.gekko.read_fpr(instr.rb()));+ ps_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()),+ ctx.gekko.read_fpr(instr.rb()),+ );}crate::gekko::lut::OP_PS_MERGE01 => {- ps_write(ctx, &instr, ctx.gekko.read_fpr(instr.ra()), ctx.gekko.read_ps1(instr.rb()));+ ps_write(+ ctx,+ &instr,+ ctx.gekko.read_fpr(instr.ra()),+ ctx.gekko.read_ps1(instr.rb()),+ );}crate::gekko::lut::OP_PS_MERGE10 => {- ps_write(ctx, &instr, ctx.gekko.read_ps1(instr.ra()), ctx.gekko.read_fpr(instr.rb()));+ ps_write(+ ctx,+ &instr,+ ctx.gekko.read_ps1(instr.ra()),+ ctx.gekko.read_fpr(instr.rb()),+ );}crate::gekko::lut::OP_PS_MERGE11 => {- ps_write(ctx, &instr, ctx.gekko.read_ps1(instr.ra()), ctx.gekko.read_ps1(instr.rb()));+ ps_write(+ ctx,+ &instr,+ ctx.gekko.read_ps1(instr.ra()),+ ctx.gekko.read_ps1(instr.rb()),+ );}crate::gekko::lut::OP_PS_SEL => {let ps0 = if ctx.gekko.read_fpr(instr.ra()) >= 0.0 {diff --git a/crates/gecko/src/gekko/interpreter/psq.rs b/crates/gecko/src/gekko/interpreter/psq.rsindex 7c427a0..7813621 100644--- a/crates/gecko/src/gekko/interpreter/psq.rs+++ b/crates/gecko/src/gekko/interpreter/psq.rs@@ -115,7 +115,10 @@ fn psq_store(ctx: &mut crate::gamecube::GameCube, fs: u8, addr: u32, w: bool, gq}#[inline(always)]-pub fn store_load_psq<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {+pub fn store_load_psq<const OP: u32>(+ ctx: &mut crate::gamecube::GameCube,+ instr: crate::gekko::instruction::Instruction,+) {use crate::gekko::lut::*;if !ctx.check_fp_available() {diff --git a/crates/gecko/src/gekko/interpreter/store_load.rs b/crates/gecko/src/gekko/interpreter/store_load.rsindex 9d51d42..0efa3e2 100644--- a/crates/gecko/src/gekko/interpreter/store_load.rs+++ b/crates/gecko/src/gekko/interpreter/store_load.rs@@ -209,7 +209,10 @@ pub fn stwcx_dot(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instr}#[inline(always)]-pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {+pub fn store_load_fp<const OP: u32>(+ ctx: &mut crate::gamecube::GameCube,+ instr: crate::gekko::instruction::Instruction,+) {if !ctx.check_fp_available() {return;}diff --git a/crates/gecko/src/gekko/interpreter/system.rs b/crates/gecko/src/gekko/interpreter/system.rsindex 1ab3514..6d53af1 100644--- a/crates/gecko/src/gekko/interpreter/system.rs+++ b/crates/gecko/src/gekko/interpreter/system.rs@@ -11,8 +11,9 @@ pub fn msr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gek}crate::gekko::lut::OP_RFI => {const RFI_MSR_MASK: u32 = 0x0000_FF73;- ctx.gekko.msr =- crate::gekko::msr::Msr::from((ctx.gekko.msr.raw() & !RFI_MSR_MASK) | (ctx.gekko.spr.srr1 & RFI_MSR_MASK));+ ctx.gekko.msr = crate::gekko::msr::Msr::from(+ (ctx.gekko.msr.raw() & !RFI_MSR_MASK) | (ctx.gekko.spr.srr1 & RFI_MSR_MASK),+ );ctx.gekko.nia = ctx.gekko.spr.srr0.value() << 2;}_ => todo!("MSR instruction with OP = {OP:#x}"),@@ -41,7 +42,8 @@ pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gek923 => {ctx.gekko.spr.dmal = crate::gekko::spr::DmaLower::from_raw(val);if ctx.gekko.spr.dmal.trigger() {- ctx.mmio.process_locked_cache_dma(&ctx.gekko.spr.dmau, &ctx.gekko.spr.dmal);+ ctx.mmio+ .process_locked_cache_dma(&ctx.gekko.spr.dmau, &ctx.gekko.spr.dmal);}}_ => ctx.gekko.spr.write(spr_num, val),diff --git a/crates/gecko/src/gekko/irq.rs b/crates/gecko/src/gekko/irq.rsindex f8f7051..451d323 100644--- a/crates/gecko/src/gekko/irq.rs+++ b/crates/gecko/src/gekko/irq.rs@@ -193,7 +193,10 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {self.gekko.nia = base | IRQ_PROGRAM;- tracing::debug!(addr = format!("{:08X}", self.gekko.nia), "FP program exception triggered");+ tracing::debug!(+ addr = format!("{:08X}", self.gekko.nia),+ "FP program exception triggered"+ );}/// Guard for FP instruction dispatch. Returns true if the instruction maydiff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rsindex 5c7ee59..1b7bbb1 100644--- a/crates/gecko/src/lib.rs+++ b/crates/gecko/src/lib.rs@@ -1,8 +1,8 @@pub mod common;-pub mod gekko;pub mod dvd;pub mod flipper;pub mod gamecube;+pub mod gekko;pub mod host;pub mod idle;pub mod ipl;diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rsindex 822872d..b9f217e 100644--- a/crates/gecko/src/system.rs+++ b/crates/gecko/src/system.rs@@ -1,4 +1,3 @@-use crate::gekko::Gekko;use crate::dvd::DvdInterface;use crate::flipper::ai::AudioInterface;use crate::flipper::cp::CommandProcessor;@@ -10,6 +9,7 @@ use crate::flipper::pe::PixelEngine;use crate::flipper::pi::ProcessorInterface;use crate::flipper::si::{SerialInterface, pad};use crate::flipper::vi::VideoInterface;+use crate::gekko::Gekko;#[cfg(feature = "hooks")]use crate::hooks::{HookFilters, HookFlags, HookState, Host};use crate::host::{EmptyRenderSink, RenderSink};diff --git a/crates/image/src/dvd.rs b/crates/image/src/dvd.rsindex 29d1491..142cfae 100644--- a/crates/image/src/dvd.rs+++ b/crates/image/src/dvd.rs@@ -64,12 +64,12 @@ pub enum FstNode {}impl FstNode {- pub fn parse(fst: &[u8]) -> Self {+ pub fn parse(fst: &[u8], file_offset_shift: u32) -> Self {let total_entries = u32::from_be_bytes(fst[8..12].try_into().unwrap()) as usize;let string_table = &fst[total_entries * 12..];let mut index = 1;- let children = Self::parse_dir(fst, string_table, &mut index, total_entries);+ let children = Self::parse_dir(fst, string_table, &mut index, total_entries, file_offset_shift);Self::Directory {name: String::new(),@@ -77,7 +77,7 @@ impl FstNode {}}- fn parse_dir(fst: &[u8], strings: &[u8], index: &mut usize, end: usize) -> Vec<Self> {+ fn parse_dir(fst: &[u8], strings: &[u8], index: &mut usize, end: usize, file_offset_shift: u32) -> Vec<Self> {let mut entries = Vec::new();while *index < end {let base = *index * 12;@@ -91,12 +91,12 @@ impl FstNode {if flags == 0 {entries.push(Self::File {name,- offset,+ offset: offset << file_offset_shift,size: length,});} else {let next = length as usize;- let children = Self::parse_dir(fst, strings, index, next);+ let children = Self::parse_dir(fst, strings, index, next, file_offset_shift);entries.push(Self::Directory { name, children });}}diff --git a/crates/image/src/iso.rs b/crates/image/src/iso.rsindex b200250..c06576d 100644--- a/crates/image/src/iso.rs+++ b/crates/image/src/iso.rs@@ -20,7 +20,8 @@ impl Iso {let fst_start = header.offset_filesystem.get() as usize;let fst_end = fst_start + header.filesystem_size.get() as usize;- let filesystem = FstNode::parse(&data[fst_start..fst_end]);+ let file_offset_shift = if header.magic == [0xC2, 0x33, 0x9F, 0x3D] { 0 } else { 2 };+ let filesystem = FstNode::parse(&data[fst_start..fst_end], file_offset_shift);Iso {header,diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rsindex d0e8a87..b04b82c 100644--- a/crates/image/src/lib.rs+++ b/crates/image/src/lib.rs@@ -63,7 +63,7 @@ fn extract_from_zip(data: Vec<u8>) -> Vec<u8> {}).expect("no disc image found in ZIP");let mut entry = archive.by_index(index).unwrap();-+let mut buf = Vec::with_capacity(entry.size() as usize);entry.read_to_end(&mut buf).expect("failed to read ZIP entry");bufdiff --git a/crates/image/src/rvz.rs b/crates/image/src/rvz.rsindex 299f279..bae01c4 100644--- a/crates/image/src/rvz.rs+++ b/crates/image/src/rvz.rs@@ -11,6 +11,8 @@ const H2_COMPRESSION_TYPE: usize = 0x04;const H2_CHUNK_SIZE: usize = 0x0C;const H2_DISC_HEADER: usize = 0x10;const H2_NUM_PARTITIONS: usize = 0x90;+const H2_PARTITION_ENTRY_SIZE: usize = 0x94;+const H2_PARTITION_ENTRIES_OFFSET: usize = 0x98;const H2_NUM_RAW_DATA: usize = 0xB4;const H2_RAW_DATA_OFFSET: usize = 0xB8;const H2_RAW_DATA_SIZE: usize = 0xC0;@@ -20,10 +22,17 @@ const H2_GROUPS_SIZE: usize = 0xD0;const RAW_DATA_ENTRY_SIZE: usize = 0x18;const RVZ_GROUP_ENTRY_SIZE: usize = 0x0C;+const RVZ_PARTITION_ENTRY_SIZE: usize = 0x30;const SECTOR_SIZE: u64 = 0x8000;const RVZ_JUNK_BLOCK_SIZE: usize = 0x8000;+const WII_BLOCK_TOTAL_SIZE: u64 = 0x8000;+const WII_BLOCK_DATA_SIZE: u64 = 0x7C00;+const WII_PARTITION_DATA_OFFSET: u64 = 0x20000;+const WII_PARTITION_TABLE_OFFSET: u64 = 0x40000;+const HASH_EXCEPTION_ENTRY_SIZE: usize = 22;+#[derive(Debug, Clone, Copy, PartialEq, Eq)]enum Compression {None,@@ -46,6 +55,19 @@ struct GroupEntry {rvz_packed_size: u32,}+#[derive(Clone, Copy)]+struct WiiPartitionData {+ first_sector: u32,+ num_sectors: u32,+ group_index: u32,+ num_groups: u32,+}++#[derive(Clone, Copy)]+struct WiiPartition {+ data: [WiiPartitionData; 2],+}+pub struct Rvz {header: Header,apploader: Apploader,@@ -55,6 +77,8 @@ pub struct Rvz {compression: Compression,raw_data: Vec<RawData>,groups: Vec<GroupEntry>,+ partitions: Vec<WiiPartition>,+ data_partition: Option<usize>,file_data: Vec<u8>,}@@ -67,10 +91,10 @@ impl Rvz {let h2 = H1_SIZE;let disc_type = self::be32(&data, h2 + H2_DISC_TYPE);- assert_eq!(disc_type, 1, "only GameCube RVZ is supported (disc_type=1)");-- let num_partitions = self::be32(&data, h2 + H2_NUM_PARTITIONS);- assert_eq!(num_partitions, 0, "Wii partitions are not supported");+ assert!(+ disc_type == 1 || disc_type == 2,+ "unsupported RVZ disc_type={disc_type} (1=GameCube, 2=Wii)"+ );let compression = match self::be32(&data, h2 + H2_COMPRESSION_TYPE) {0 => Compression::None,@@ -103,31 +127,97 @@ impl Rvz {);let groups = self::parse_groups(&groups_blob, num_groups);- let mut header_bytes = [0u8; DVD_HEADER_SIZE];- self::read_into(- &data,- &disc_header,- chunk_size,- compression,- &raw_data,- &groups,- DVD_HEADER_OFFSET as u64,- &mut header_bytes,- );- let header = Header::read_from_bytes(&header_bytes).expect("invalid DVD header");+ let (partitions, data_partition) = if disc_type == 2 {+ let num_partitions = self::be32(&data, h2 + H2_NUM_PARTITIONS);+ let partition_entry_size = self::be32(&data, h2 + H2_PARTITION_ENTRY_SIZE) as usize;+ let partition_entries_off = self::be64(&data, h2 + H2_PARTITION_ENTRIES_OFFSET) as usize;+ assert!(+ partition_entry_size >= RVZ_PARTITION_ENTRY_SIZE,+ "RVZ partition_entry_size {partition_entry_size} smaller than expected baseline {RVZ_PARTITION_ENTRY_SIZE}"+ );+ let partitions_blob =+ &data[partition_entries_off..partition_entries_off + (num_partitions as usize) * partition_entry_size];+ let partitions = self::parse_partitions(partitions_blob, num_partitions, partition_entry_size);+ assert!(!partitions.is_empty(), "Wii RVZ has no partitions");++ let data_idx = self::pick_data_partition(+ &data,+ &disc_header,+ chunk_size,+ compression,+ &raw_data,+ &groups,+ &partitions,+ );+ (partitions, Some(data_idx))+ } else {+ let num_partitions = self::be32(&data, h2 + H2_NUM_PARTITIONS);+ assert_eq!(num_partitions, 0, "GameCube RVZ should have no partitions");+ (Vec::new(), None)+ };- let mut apploader_bytes = [0u8; DVD_APPLOADER_SIZE];- self::read_into(- &data,- &disc_header,- chunk_size,- compression,- &raw_data,- &groups,- DVD_APPLOADER_OFFSET as u64,- &mut apploader_bytes,- );- let apploader = Apploader::read_from_bytes(&apploader_bytes).expect("invalid apploader");+ let (header, apploader, iso_size) = match data_partition {+ None => {+ let mut header_bytes = [0u8; DVD_HEADER_SIZE];+ self::read_into(+ &data,+ &disc_header,+ chunk_size,+ compression,+ &raw_data,+ &groups,+ DVD_HEADER_OFFSET as u64,+ &mut header_bytes,+ );+ let header = Header::read_from_bytes(&header_bytes).expect("invalid DVD header");++ let mut apploader_bytes = [0u8; DVD_APPLOADER_SIZE];+ self::read_into(+ &data,+ &disc_header,+ chunk_size,+ compression,+ &raw_data,+ &groups,+ DVD_APPLOADER_OFFSET as u64,+ &mut apploader_bytes,+ );+ let apploader = Apploader::read_from_bytes(&apploader_bytes).expect("invalid apploader");++ (header, apploader, iso_size)+ }+ Some(idx) => {+ let partition = &partitions[idx];+ let partition_iso_size = self::partition_user_size(partition);++ let mut header_bytes = [0u8; DVD_HEADER_SIZE];+ self::read_partition_decrypted(+ &data,+ chunk_size,+ compression,+ &groups,+ partition,+ DVD_HEADER_OFFSET as u64,+ &mut header_bytes,+ );+ self::unshift_wii_header_offsets(&mut header_bytes);+ let header = Header::read_from_bytes(&header_bytes).expect("invalid DVD header");++ let mut apploader_bytes = [0u8; DVD_APPLOADER_SIZE];+ self::read_partition_decrypted(+ &data,+ chunk_size,+ compression,+ &groups,+ partition,+ DVD_APPLOADER_OFFSET as u64,+ &mut apploader_bytes,+ );+ let apploader = Apploader::read_from_bytes(&apploader_bytes).expect("invalid apploader");++ (header, apploader, partition_iso_size)+ }+ };Rvz {header,@@ -138,6 +228,8 @@ impl Rvz {compression,raw_data,groups,+ partitions,+ data_partition,file_data: data,}}@@ -323,6 +415,229 @@ fn rvz_unpack(src: &[u8], out_len: usize, data_offset: u64) -> Vec<u8> {out}+fn parse_partitions(blob: &[u8], count: u32, entry_size: usize) -> Vec<WiiPartition> {+ let mut out = Vec::with_capacity(count as usize);++ for i in 0..count as usize {+ let base = i * entry_size;++ let mut data = [WiiPartitionData {+ first_sector: 0,+ num_sectors: 0,+ group_index: 0,+ num_groups: 0,+ }; 2];+ for slot in 0..2 {+ let s = base + 16 + slot * 0x10;+ data[slot] = WiiPartitionData {+ first_sector: self::be32(blob, s),+ num_sectors: self::be32(blob, s + 4),+ group_index: self::be32(blob, s + 8),+ num_groups: self::be32(blob, s + 12),+ };+ }++ out.push(WiiPartition { data });+ }++ out+}++fn pick_data_partition(+ file: &[u8],+ disc_header: &[u8; 0x80],+ chunk_size: u32,+ compression: Compression,+ raw_data: &[RawData],+ groups: &[GroupEntry],+ partitions: &[WiiPartition],+) -> usize {+ let mut group_table = [0u8; 32];+ self::read_into(+ file,+ disc_header,+ chunk_size,+ compression,+ raw_data,+ groups,+ WII_PARTITION_TABLE_OFFSET,+ &mut group_table,+ );++ for g in 0..4 {+ let num = u32::from_be_bytes(group_table[g * 8..g * 8 + 4].try_into().unwrap());+ let off_shifted = u32::from_be_bytes(group_table[g * 8 + 4..g * 8 + 8].try_into().unwrap());+ if num == 0 {+ continue;+ }+ let table_off = (off_shifted as u64) << 2;++ let mut entries = vec![0u8; (num as usize) * 8];+ self::read_into(+ file,+ disc_header,+ chunk_size,+ compression,+ raw_data,+ groups,+ table_off,+ &mut entries,+ );++ for e in 0..num as usize {+ let part_off = (u32::from_be_bytes(entries[e * 8..e * 8 + 4].try_into().unwrap()) as u64) << 2;+ let part_type = u32::from_be_bytes(entries[e * 8 + 4..e * 8 + 8].try_into().unwrap());+ if part_type != 0 {+ continue;+ }+ let expected_first_sector = (part_off + WII_PARTITION_DATA_OFFSET) / WII_BLOCK_TOTAL_SIZE;+ if let Some(idx) = partitions+ .iter()+ .position(|p| (p.data[0].first_sector as u64) == expected_first_sector)+ {+ return idx;+ }+ }+ }++ panic!("Wii RVZ has no data partition (type=0) matching any RVZ partition entry");+}++fn partition_user_size(partition: &WiiPartition) -> u64 {+ partition+ .data+ .iter()+ .map(|s| (s.num_sectors as u64) * WII_BLOCK_DATA_SIZE)+ .sum()+}++fn read_partition_decrypted(+ file: &[u8],+ chunk_size: u32,+ compression: Compression,+ groups: &[GroupEntry],+ partition: &WiiPartition,+ mut offset: u64,+ buf: &mut [u8],+) {+ let sectors_per_chunk = (chunk_size as u64) / WII_BLOCK_TOTAL_SIZE;+ assert!(sectors_per_chunk >= 1, "RVZ chunk_size smaller than one Wii sector");+ let chunk_user_size = sectors_per_chunk * WII_BLOCK_DATA_SIZE;++ let mut buf_off = 0usize;+ let mut remaining = buf.len();++ while remaining > 0 {+ let mut slot_user_start: u64 = 0;+ let mut slot_opt: Option<&WiiPartitionData> = None;+ for s in &partition.data {+ let slot_user_size = (s.num_sectors as u64) * WII_BLOCK_DATA_SIZE;+ if s.num_sectors > 0 && offset >= slot_user_start && offset < slot_user_start + slot_user_size {+ slot_opt = Some(s);+ break;+ }+ slot_user_start += slot_user_size;+ }+ let slot = slot_opt.expect("partition decrypted offset out of range");++ let within_slot = offset - slot_user_start;+ let chunk_idx = within_slot / chunk_user_size;+ assert!(+ (chunk_idx as u32) < slot.num_groups,+ "partition chunk index {chunk_idx} >= num_groups {}",+ slot.num_groups+ );++ let chunk_first_sector_in_slot = chunk_idx * sectors_per_chunk;+ let sectors_in_this_chunk = core::cmp::min(+ sectors_per_chunk,+ (slot.num_sectors as u64) - chunk_first_sector_in_slot,+ );+ let this_chunk_user_size = (sectors_in_this_chunk * WII_BLOCK_DATA_SIZE) as usize;++ let group = &groups[(slot.group_index as u64 + chunk_idx) as usize];+ let chunk_disc_offset_enc = (slot.first_sector as u64 + chunk_first_sector_in_slot) * WII_BLOCK_TOTAL_SIZE;++ let chunk =+ self::decompress_partition_chunk(file, group, this_chunk_user_size, compression, chunk_disc_offset_enc);++ let byte_off_in_chunk = (within_slot - chunk_idx * chunk_user_size) as usize;+ let avail = this_chunk_user_size - byte_off_in_chunk;+ let take = core::cmp::min(avail, remaining);+ buf[buf_off..buf_off + take].copy_from_slice(&chunk[byte_off_in_chunk..byte_off_in_chunk + take]);++ buf_off += take;+ offset += take as u64;+ remaining -= take;+ }+}++fn decompress_partition_chunk(+ file: &[u8],+ group: &GroupEntry,+ this_chunk_user_size: usize,+ file_compression: Compression,+ chunk_disc_offset_enc: u64,+) -> Vec<u8> {+ if group.stored_size == 0 {+ return vec![0u8; this_chunk_user_size];+ }++ let start = group.file_offset as usize;+ let end = start + group.stored_size as usize;+ let payload = &file[start..end];++ let effective = if group.is_compressed {+ file_compression+ } else {+ Compression::None+ };++ let decompressed: Vec<u8> = match effective {+ Compression::None => payload.to_vec(),+ Compression::Zstd => {+ zstd::decode_all(std::io::Cursor::new(payload)).expect("failed to zstd-decode partition group")+ }+ };++ assert!(+ decompressed.len() >= 2,+ "partition chunk too small for hash exception count"+ );+ let num_exceptions = u16::from_be_bytes([decompressed[0], decompressed[1]]) as usize;+ let mut exceptions_size = 2 + num_exceptions * HASH_EXCEPTION_ENTRY_SIZE;+ if !group.is_compressed {+ exceptions_size = (exceptions_size + 3) & !3;+ }+ assert!(+ decompressed.len() >= exceptions_size,+ "partition chunk truncated (declared {num_exceptions} exceptions, payload {} bytes)",+ decompressed.len()+ );+ let user_segment = &decompressed[exceptions_size..];++ if group.rvz_packed_size == 0 {+ let mut out = vec![0u8; this_chunk_user_size];+ let n = core::cmp::min(user_segment.len(), this_chunk_user_size);+ out[..n].copy_from_slice(&user_segment[..n]);+ out+ } else {+ assert_eq!(+ user_segment.len(),+ group.rvz_packed_size as usize,+ "partition rvz_packed_size mismatch"+ );+ self::rvz_unpack(user_segment, this_chunk_user_size, chunk_disc_offset_enc)+ }+}++fn unshift_wii_header_offsets(bytes: &mut [u8; DVD_HEADER_SIZE]) {+ for off in [0x420, 0x424, 0x428, 0x42C, 0x430, 0x434] {+ let v = u32::from_be_bytes(bytes[off..off + 4].try_into().unwrap());+ bytes[off..off + 4].copy_from_slice(&v.wrapping_shl(2).to_be_bytes());+ }+}+const LFG_K: usize = 521;const LFG_J: usize = 32;const LFG_STATE_BYTES: usize = LFG_K * 4;@@ -422,16 +737,27 @@ impl crate::Dvd for Rvz {"read past end of disc image");- self::read_into(- &self.file_data,- &self.disc_header,- self.chunk_size,- self.compression,- &self.raw_data,- &self.groups,- offset as u64,- buf,- );+ match self.data_partition {+ None => self::read_into(+ &self.file_data,+ &self.disc_header,+ self.chunk_size,+ self.compression,+ &self.raw_data,+ &self.groups,+ offset as u64,+ buf,+ ),+ Some(idx) => self::read_partition_decrypted(+ &self.file_data,+ self.chunk_size,+ self.compression,+ &self.groups,+ &self.partitions[idx],+ offset as u64,+ buf,+ ),+ }}}diff --git a/crates/multitool/src/dvd.rs b/crates/multitool/src/dvd.rsindex 811336a..2823a01 100644--- a/crates/multitool/src/dvd.rs+++ b/crates/multitool/src/dvd.rs@@ -62,7 +62,8 @@ fn read_filesystem(dvd: &dyn Dvd) -> FstNode {let fst_size = header.filesystem_size.get() as usize;let mut buf = vec![0u8; fst_size];dvd.read_disc_into(fst_offset, &mut buf);- FstNode::parse(&buf)+ let file_offset_shift = if header.magic == [0xC2, 0x33, 0x9F, 0x3D] { 0 } else { 2 };+ FstNode::parse(&buf, file_offset_shift)}fn build_tree(node: &FstNode) -> Tree<String> {diff --git a/crates/tinytracer/src/dump.rs b/crates/tinytracer/src/dump.rsindex 33f8b45..b18eb2a 100644--- a/crates/tinytracer/src/dump.rs+++ b/crates/tinytracer/src/dump.rs@@ -44,22 +44,23 @@ pub fn registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {("cr7", curr.cr.cr7(), prev.cr.cr7()),];- let fmt_cr_field =- |label: &str, val: gecko::gekko::condition::ConditionField, prev_val: gecko::gekko::condition::ConditionField| {- let flags = format!(- "{}{}{}{}",- if val.lt() { "L" } else { "·" },- if val.gt() { "G" } else { "·" },- if val.eq() { "Z" } else { "·" },- if val.so() { "O" } else { "·" },- );- let text = format!("{}[{}] ", label, flags);- if val.raw() != prev_val.raw() {- format!("{}", text.bright_red().bold())- } else {- format!("{}", text.dimmed())- }- };+ let fmt_cr_field = |label: &str,+ val: gecko::gekko::condition::ConditionField,+ prev_val: gecko::gekko::condition::ConditionField| {+ let flags = format!(+ "{}{}{}{}",+ if val.lt() { "L" } else { "·" },+ if val.gt() { "G" } else { "·" },+ if val.eq() { "Z" } else { "·" },+ if val.so() { "O" } else { "·" },+ );+ let text = format!("{}[{}] ", label, flags);+ if val.raw() != prev_val.raw() {+ format!("{}", text.bright_red().bold())+ } else {+ format!("{}", text.dimmed())+ }+ };let cr_line: String = cr_fields.iter()diff --git a/roms/panda.dol b/other/roms/panda.dolsimilarity index 100%rename from roms/panda.dolrename to other/roms/panda.doldiff --git a/roms/panda.dol.i64 b/other/roms/panda.dol.i64similarity index 100%rename from roms/panda.dol.i64rename to other/roms/panda.dol.i64diff --git a/roms/silentskate.dol b/other/roms/silentskate.dolsimilarity index 100%rename from roms/silentskate.dolrename to other/roms/silentskate.dol