A GameCube/Wii emulator and debugger written in Rust.
comparative tev and ipl skip patches
Layleb3e8cadf2eef
diff --git a/.gitmodules b/.gitmodulesindex e21a991..1341f47 100644--- a/.gitmodules+++ b/.gitmodules@@ -16,3 +16,6 @@[submodule "submodules/beanwii"]path = submodules/beanwiiurl = https://github.com/zaydlang/beanwii+[submodule "submodules/lily-gamecube-roms"]+ path = submodules/lily-gamecube-roms+ url = https://github.com/xOx-Liy-Codez-XD-oXo/lily-gamecube-romsdiff --git a/Cargo.lock b/Cargo.lockindex 83a83a7..2e895c6 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1276,6 +1276,7 @@ dependencies = ["bytemuck","chapa","chipi-build",+ "crc32fast","crossbeam-channel","glam","image 0.1.0",diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.weslindex 19b685d..c5b43bc 100644--- a/crates/backend-wgpu/src/shaders/main.wesl+++ b/crates/backend-wgpu/src/shaders/main.wesl@@ -142,7 +142,7 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {let ain_c = tev_alpha_in(a_c, tex_color, ras_color, regs, konst_color);let ain_d = tev_alpha_in(a_d, tex_color, ras_color, regs, konst_color);- let alpha_result = tev_combine_alpha(ain_a, ain_b, ain_c, ain_d, a_bias, a_sub, a_scale, a_clamp);+ let alpha_result = tev_combine_alpha(ain_a, ain_b, ain_c, ain_d, in_a, in_b, a_bias, a_sub, a_scale, a_clamp);// Write alpha to dest, preserve RGBregs[a_dest] = vec4(regs[a_dest].rgb, alpha_result);diff --git a/crates/backend-wgpu/src/shaders/tev_combiners.wesl b/crates/backend-wgpu/src/shaders/tev_combiners.weslindex 1c205ee..1c41150 100644--- a/crates/backend-wgpu/src/shaders/tev_combiners.wesl+++ b/crates/backend-wgpu/src/shaders/tev_combiners.wesl@@ -1,26 +1,77 @@-// TEV color combiner: result = (d (+/-) lerp(a, b, c) + bias) * scale+// TEV color combiner.+//+// In regular mode (bias != 3): result = (d (+/-) lerp(a, b, c) + bias) * scale+// In compare mode (bias == 3): `scale` and `sub` are repurposed:+// `scale` selects the TevCompareMode (0=R8, 1=GR16, 2=BGR24, 3=RGB8)+// `sub` selects the operator (false=GT, true=EQ)+// result = d + ((compare(a, b)) ? c : 0) per the selected mode+// `do_clamp` always means: if true, clamp to [0,1]; if false, leave unclamped.fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,bias: u32, sub: bool, scale: u32, do_clamp: bool) -> vec3<f32> {- let lerp = a * (vec3(1.0) - c) + b * c;-var result: vec3<f32>;- if sub {- result = d - lerp;++ if bias == 3u {+ let ai = vec3<u32>(u32(round(clamp(a.r, 0.0, 1.0) * 255.0)),+ u32(round(clamp(a.g, 0.0, 1.0) * 255.0)),+ u32(round(clamp(a.b, 0.0, 1.0) * 255.0)));+ let bi = vec3<u32>(u32(round(clamp(b.r, 0.0, 1.0) * 255.0)),+ u32(round(clamp(b.g, 0.0, 1.0) * 255.0)),+ u32(round(clamp(b.b, 0.0, 1.0) * 255.0)));++ // scale is the TevCompareMode selector.+ var mask: vec3<f32>;+ switch scale {+ case 0u: {+ // R8: scalar compare on the red channel, gate c.rgb.+ let hit = compare_u32(ai.r, bi.r, sub);+ mask = vec3<f32>(hit, hit, hit);+ }+ case 1u: {+ // GR16: r + g*256+ let pa = ai.r + ai.g * 256u;+ let pb = bi.r + bi.g * 256u;+ let hit = compare_u32(pa, pb, sub);+ mask = vec3<f32>(hit, hit, hit);+ }+ case 2u: {+ // BGR24: r + g*256 + b*65536+ let pa = ai.r + ai.g * 256u + ai.b * 65536u;+ let pb = bi.r + bi.g * 256u + bi.b * 65536u;+ let hit = compare_u32(pa, pb, sub);+ mask = vec3<f32>(hit, hit, hit);+ }+ default: {+ // RGB8: per-channel compare.+ mask = vec3<f32>(+ compare_u32(ai.r, bi.r, sub),+ compare_u32(ai.g, bi.g, sub),+ compare_u32(ai.b, bi.b, sub),+ );+ }+ }++ result = d + c * mask;} else {- result = d + lerp;- }+ let lerp = a * (vec3(1.0) - c) + b * c;- switch bias {- case 1u: { result += vec3(0.5); } // AddHalf- case 2u: { result -= vec3(0.5); } // SubHalf- default: {}- }+ if sub {+ result = d - lerp;+ } else {+ result = d + lerp;+ }++ switch bias {+ case 1u: { result += vec3(0.5); } // AddHalf+ case 2u: { result -= vec3(0.5); } // SubHalf+ default: {}+ }- switch scale {- case 1u: { result *= 2.0; } // Scale2- case 2u: { result *= 4.0; } // Scale4- case 3u: { result *= 0.5; } // Divide2- default: {}+ switch scale {+ case 1u: { result *= 2.0; } // Scale2+ case 2u: { result *= 4.0; } // Scale4+ case 3u: { result *= 0.5; } // Divide2+ default: {}+ }}if do_clamp {@@ -30,28 +81,60 @@ fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,return result;}-// Scalar version for the alpha channel-fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32, bias: u32, sub: bool, scale: u32, do_clamp: bool) -> f32 {- let lerp = a * (1.0 - c) + b * c;-+// Scalar version for the alpha channel.+fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32,+ a_rgb: vec3<f32>, b_rgb: vec3<f32>,+ bias: u32, sub: bool, scale: u32, do_clamp: bool) -> f32 {var result: f32;- if sub {- result = d - lerp;++ if bias == 3u {+ let ai = vec3<u32>(u32(round(clamp(a_rgb.r, 0.0, 1.0) * 255.0)),+ u32(round(clamp(a_rgb.g, 0.0, 1.0) * 255.0)),+ u32(round(clamp(a_rgb.b, 0.0, 1.0) * 255.0)));+ let bi = vec3<u32>(u32(round(clamp(b_rgb.r, 0.0, 1.0) * 255.0)),+ u32(round(clamp(b_rgb.g, 0.0, 1.0) * 255.0)),+ u32(round(clamp(b_rgb.b, 0.0, 1.0) * 255.0)));+ let aa = u32(round(clamp(a, 0.0, 1.0) * 255.0));+ let ba = u32(round(clamp(b, 0.0, 1.0) * 255.0));++ var hit: f32;+ switch scale {+ case 0u: { hit = compare_u32(ai.r, bi.r, sub); } // R8+ case 1u: { // GR16+ let pa = ai.r + ai.g * 256u;+ let pb = bi.r + bi.g * 256u;+ hit = compare_u32(pa, pb, sub);+ }+ case 2u: { // BGR24+ let pa = ai.r + ai.g * 256u + ai.b * 65536u;+ let pb = bi.r + bi.g * 256u + bi.b * 65536u;+ hit = compare_u32(pa, pb, sub);+ }+ default: { hit = compare_u32(aa, ba, sub); } // A8+ }++ result = d + c * hit;} else {- result = d + lerp;- }+ let lerp = a * (1.0 - c) + b * c;- switch bias {- case 1u: { result += 0.5; }- case 2u: { result -= 0.5; }- default: {}- }+ if sub {+ result = d - lerp;+ } else {+ result = d + lerp;+ }- switch scale {- case 1u: { result *= 2.0; }- case 2u: { result *= 4.0; }- case 3u: { result *= 0.5; }- default: {}+ switch bias {+ case 1u: { result += 0.5; }+ case 2u: { result -= 0.5; }+ default: {}+ }++ switch scale {+ case 1u: { result *= 2.0; }+ case 2u: { result *= 4.0; }+ case 3u: { result *= 0.5; }+ default: {}+ }}if do_clamp {@@ -60,3 +143,13 @@ fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32, bias: u32, sub: bool, scalereturn result;}++fn compare_u32(a: u32, b: u32, eq: bool) -> f32 {+ if eq {+ if a == b { return 1.0; }+ } else {+ if a > b { return 1.0; }+ }++ return 0.0;+}diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rsindex 33ca9d2..a54a3b1 100644--- a/crates/debugger/src/main.rs+++ b/crates/debugger/src/main.rs@@ -134,6 +134,10 @@ struct Args {#[arg(long)]ipl: Option<String>,+ /// Patch the IPL to skip directly to disc boot+ #[arg(long)]+ skip_ipl: bool,+/// Boot from ISO using HLE IPL (requires --iso)#[arg(long)]ipl_hle: bool,@@ -186,7 +190,7 @@ fn main() {GameCube::with_ipl_hle(dvd)} else if let Some(ref ipl) = args.ipl {let ipl_data = std::fs::read(ipl).expect("failed to read IPL");- GameCube::with_ipl(&ipl_data)+ GameCube::with_ipl(&ipl_data, args.skip_ipl)} else if let Some(ref path) = args.dol {let dol_data = std::fs::read(path).expect("failed to read DOL");let dol = Dol::parse(dol_data);diff --git a/crates/gecko/Cargo.toml b/crates/gecko/Cargo.tomlindex 2f375eb..fd35a96 100644--- a/crates/gecko/Cargo.toml+++ b/crates/gecko/Cargo.toml@@ -26,3 +26,4 @@ twox-hash = "2.1.2"crossbeam-channel = { version = "0.5.15", optional = true }multiversion = "0.8.0"rustc-hash = "2.1.2"+crc32fast = "1.5.0"diff --git a/crates/gecko/src/flipper/gx/regs.rs b/crates/gecko/src/flipper/gx/regs.rsindex f815d20..6027758 100644--- a/crates/gecko/src/flipper/gx/regs.rs+++ b/crates/gecko/src/flipper/gx/regs.rs@@ -726,6 +726,7 @@ pub enum TevBias {Zero = 0,AddHalf = 1,SubHalf = 2,+ Compare = 3,}#[derive(Debug, PartialEq, BitEnum)]diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rsindex e28a633..cfe7ac9 100644--- a/crates/gecko/src/gamecube.rs+++ b/crates/gecko/src/gamecube.rs@@ -17,12 +17,11 @@ use crate::hooks::{HookFilters, HookFlags, HookState, Host};use crate::host::{EmptyRenderSink, RenderSink};#[cfg(feature = "idle-skip")]use crate::idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector};+use crate::ipl::IPL_HLE;use crate::mmio::Mmio;use crate::scheduler::Scheduler;use image::Executable;-pub static IPL_HLE: &[u8] = include_bytes!("../../../submodules/solstice/ipl/hle/ipl/gc_custom_ipl.bin");-pub struct GameCube {pub vsync_pending: bool,pub cpu: Cpu,@@ -216,7 +215,7 @@ impl GameCube {emulator}- pub fn with_ipl(ipl: &[u8]) -> Self {+ pub fn with_ipl(ipl: &[u8], skip: bool) -> Self {// Text Sections (1):// | idx | offset | vaddr | size | end |// |-----|------------|------------|------------|------------|@@ -228,13 +227,18 @@ impl GameCube {// BSS: 0x00000000 - 0x00000000 (size: 0x00000000)// => BS2 DOL, does not apply to the actual IPL here!!+ let mut ipl = ipl.to_vec();+ if skip {+ crate::ipl::apply_skip_patch(&mut ipl);+ }+let mut emulator = GameCube::new(IPL_RESET_VECTOR);emulator.cpu.msr.set_ip(true);- emulator.mmio.ipl = ipl.to_vec();+ emulator.mmio.ipl = ipl.clone();emulator.exi.attach_device(ExiMacronix::CHANNEL,ExiMacronix::DEVICE,- Box::new(ExiMacronix::new(ipl.to_vec())),+ Box::new(ExiMacronix::new(ipl)),);// TODO: this makes 0x8130107C (NTSC BS2) exit the DVD state machine// as it forces it to enter "state 19"diff --git a/crates/gecko/src/ipl.rs b/crates/gecko/src/ipl.rsnew file mode 100644index 0000000..c54440f--- /dev/null+++ b/crates/gecko/src/ipl.rs@@ -0,0 +1,51 @@+pub static IPL_HLE: &[u8] = include_bytes!("../../../submodules/solstice/ipl/hle/ipl/gc_custom_ipl.bin");++#[derive(Clone, Copy)]+pub struct SkipPatch {+ pub name: &'static str,+ pub ipl_hash: u32,+ pub offset: u32,+ pub patch: [u8; 12],+}++// We patch this sequence here. This is the loop that ticks the DVD state machine.+// Once we reach state 16 (BS2_RUN_APP) we jump directly to boot_disc, this+// effectively skips the entire animation/menu flow and boots the disc instead.+//+// do+// {+// update_ipl_state();+// wait_retrace();+// }+// while ( get_ipl_state() != 19 && (get_ipl_state() < 5 || get_ipl_state() > 16) && get_ipl_state() != 18 );++pub const SKIP_PATCHES: &[SkipPatch] = &[+ SkipPatch {+ name: "IPL NTSC",+ ipl_hash: 0xB7E67BE1,+ offset: 0x1A7C, // vaddr 0x8130125C+ patch: [+ 0x2C, 0x03, 0x00, 0x10, // cmpwi r3, 0x10+ 0x40, 0x82, 0xFF, 0xF0, // bne poll BS2 state+ 0x4B, 0xFF, 0xF6, 0xD4, // b boot_disc+ ],+ },+ SkipPatch {+ name: "IPL PAL",+ ipl_hash: 0xE1B06B49,+ offset: 0x2B30, // vaddr 0x81302310+ patch: [+ 0x2C, 0x00, 0x00, 0x10, // cmpwi r0, 0x10+ 0x40, 0x82, 0xFF, 0xEC, // bne poll BS2 state+ 0x4B, 0xFF, 0xE5, 0x18, // b boot_disc+ ],+ },+];++pub fn apply_skip_patch(ipl: &mut [u8]) {+ let hash = crc32fast::hash(ipl);+ if let Some(patch) = SKIP_PATCHES.iter().find(|p| p.ipl_hash == hash) {+ tracing::info!("Applying skip patch: {}", patch.name);+ ipl[patch.offset as usize..patch.offset as usize + patch.patch.len()].copy_from_slice(&patch.patch);+ }+}diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rsindex 3432915..bf434ee 100644--- a/crates/gecko/src/lib.rs+++ b/crates/gecko/src/lib.rs@@ -5,6 +5,7 @@ pub mod flipper;pub mod gamecube;pub mod host;pub mod idle;+pub mod ipl;pub mod mmio;pub mod scheduler;diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rsindex 06be611..62f90e5 100644--- a/crates/tinyapp/src/main.rs+++ b/crates/tinyapp/src/main.rs@@ -22,6 +22,10 @@ struct Args {#[arg(long)]ipl: Option<String>,+ /// Patch the IPL to skip directly to disc boot+ #[arg(long)]+ skip_ipl: bool,+/// Boot from ISO using HLE IPL (requires --iso)#[arg(long)]ipl_hle: bool,@@ -76,7 +80,7 @@ fn main() {GameCube::with_ipl_hle(dvd)} else if let Some(ref ipl) = args.ipl {let ipl_data = std::fs::read(ipl).expect("failed to read IPL");- GameCube::with_ipl(&ipl_data)+ GameCube::with_ipl(&ipl_data, args.skip_ipl)} else if let Some(ref path) = args.dol {let dol_data = std::fs::read(path).expect("failed to read DOL");let dol = Dol::parse(dol_data);diff --git a/crates/tinybench/src/main.rs b/crates/tinybench/src/main.rsindex 0a1f18d..1b31c38 100644--- a/crates/tinybench/src/main.rs+++ b/crates/tinybench/src/main.rs@@ -44,7 +44,7 @@ fn main() {GameCube::with_ipl_hle(dvd)} else if let Some(ref ipl) = args.ipl {let ipl_data = std::fs::read(ipl).expect("failed to read IPL");- let mut gc = GameCube::with_ipl(&ipl_data);+ let mut gc = GameCube::with_ipl(&ipl_data, false);if let Some(ref iso_path) = args.iso {let iso_data = std::fs::read(iso_path).expect("failed to read ISO");let dvd = image::dvd::Dvd::parse(iso_data);diff --git a/crates/tinytracer/src/main.rs b/crates/tinytracer/src/main.rsindex b2b5398..ebd96b1 100644--- a/crates/tinytracer/src/main.rs+++ b/crates/tinytracer/src/main.rs@@ -107,7 +107,7 @@ fn main() {gecko::gamecube::GameCube::with_image(&dol)} else if let Some(ipl_path) = &args.ipl {let ipl_data = std::fs::read(ipl_path).expect("failed to read IPL");- gecko::gamecube::GameCube::with_ipl(&ipl_data)+ gecko::gamecube::GameCube::with_ipl(&ipl_data, false)} else {panic!("Either --dol, --ipl, or --ipl-hle must be provided");};diff --git a/crates/web/Cargo.lock b/crates/web/Cargo.lockindex b60ccc2..188ceb5 100644--- a/crates/web/Cargo.lock+++ b/crates/web/Cargo.lock@@ -934,6 +934,7 @@ dependencies = ["bytemuck","chapa","chipi-build",+ "crc32fast","glam","image","multiversion",diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rsindex f73ab5b..fdc7ab8 100644--- a/crates/web/src/lib.rs+++ b/crates/web/src/lib.rs@@ -477,7 +477,7 @@ pub fn start_emulator(rom_data: &[u8], filename: String, dsp_irom: Option<Vec<u8let name = filename.to_lowercase();let mut emulator = if name.ends_with(".bin") || name.ends_with(".ipl") {- GameCube::with_ipl(rom_data)+ GameCube::with_ipl(rom_data, false)} else {let dol = Dol::parse(rom_data.to_vec());GameCube::with_image(&dol)diff --git a/submodules/lily-gamecube-roms b/submodules/lily-gamecube-romsnew file mode 160000index 0000000..5c382c3--- /dev/null+++ b/submodules/lily-gamecube-roms@@ -0,0 +1 @@+Subproject commit 5c382c38a4fa995ce36c5cbd6f97d73c8cff8984