Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

dspemu crate and a few dsp instructions, runs dsp stub fine?

Layle64a83ff72933
diff --git a/Cargo.lock b/Cargo.lock
index 13dffb5..dead422 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -403,18 +403,18 @@ dependencies = [
[[package]]
name = "chipi-build"
-version = "0.7.2"
+version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa4d92ef9c1c407a60305280f56167c8b27321f2e89a22ad11a211462f5906da"
+checksum = "2676885c88403477d9053fc020668dd50d352bd05c8ff4668a276f79655c3550"
dependencies = [
"chipi-core",
]
[[package]]
name = "chipi-core"
-version = "0.7.2"
+version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0bb9fe17b59d9ccb968f4b245484a465855910673d7e935ed2b19ae5ac710748"
+checksum = "6c00155ee6a20d40473b212f6b33d20d14525b47df5f5628e609b05c78272833"
dependencies = [
"serde",
"toml",
@@ -728,6 +728,16 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76"
+[[package]]
+name = "dspemu"
+version = "0.1.0"
+dependencies = [
+ "clap",
+ "disasm",
+ "gecko",
+ "tracing-subscriber",
+]
+
[[package]]
name = "ecolor"
version = "0.33.3"
@@ -2194,7 +2204,7 @@ version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f"
dependencies = [
- "toml_edit 0.25.8+spec-1.1.0",
+ "toml_edit",
]
[[package]]
@@ -2460,11 +2470,11 @@ dependencies = [
[[package]]
name = "serde_spanned"
-version = "0.6.9"
+version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
+checksum = "876ac351060d4f882bb1032b6369eb0aef79ad9df1ea8bc404874d8cc3d0cd98"
dependencies = [
- "serde",
+ "serde_core",
]
[[package]]
@@ -2809,23 +2819,17 @@ dependencies = [
[[package]]
name = "toml"
-version = "0.8.23"
+version = "1.1.0+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
+checksum = "f8195ca05e4eb728f4ba94f3e3291661320af739c4e43779cbdfae82ab239fcc"
dependencies = [
- "serde",
+ "indexmap",
+ "serde_core",
"serde_spanned",
- "toml_datetime 0.6.11",
- "toml_edit 0.22.27",
-]
-
-[[package]]
-name = "toml_datetime"
-version = "0.6.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
-dependencies = [
- "serde",
+ "toml_datetime",
+ "toml_parser",
+ "toml_writer",
+ "winnow",
]
[[package]]
@@ -2837,20 +2841,6 @@ dependencies = [
"serde_core",
]
-[[package]]
-name = "toml_edit"
-version = "0.22.27"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
-dependencies = [
- "indexmap",
- "serde",
- "serde_spanned",
- "toml_datetime 0.6.11",
- "toml_write",
- "winnow 0.7.15",
-]
-
[[package]]
name = "toml_edit"
version = "0.25.8+spec-1.1.0"
@@ -2858,9 +2848,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16bff38f1d86c47f9ff0647e6838d7bb362522bdf44006c7068c2b1e606f1f3c"
dependencies = [
"indexmap",
- "toml_datetime 1.1.0+spec-1.1.0",
+ "toml_datetime",
"toml_parser",
- "winnow 1.0.0",
+ "winnow",
]
[[package]]
@@ -2869,14 +2859,14 @@ version = "1.1.0+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2334f11ee363607eb04df9b8fc8a13ca1715a72ba8662a26ac285c98aabb4011"
dependencies = [
- "winnow 1.0.0",
+ "winnow",
]
[[package]]
-name = "toml_write"
-version = "0.1.2"
+name = "toml_writer"
+version = "1.1.0+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
+checksum = "d282ade6016312faf3e41e57ebbba0c073e4056dab1232ab1cb624199648f8ed"
[[package]]
name = "tracing"
@@ -3798,15 +3788,6 @@ dependencies = [
"xkbcommon-dl",
]
-[[package]]
-name = "winnow"
-version = "0.7.15"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
-dependencies = [
- "memchr",
-]
-
[[package]]
name = "winnow"
version = "1.0.0"
diff --git a/Cargo.toml b/Cargo.toml
index 97f1891..1400288 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,7 @@ members = [
"crates/backend-wgpu",
"crates/debugger",
"crates/scripting",
+ "crates/dspemu",
]
[patch.crates-io]
diff --git a/crates/debugger/Cargo.toml b/crates/debugger/Cargo.toml
index 2d9a5ae..83ad484 100644
--- a/crates/debugger/Cargo.toml
+++ b/crates/debugger/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[features]
+idle-skip = ["gecko/idle-skip"]
scripting-mut-traps = ["gecko/scripting-mut-traps", "scripting/scripting-mut-traps"]
[dependencies]
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index 1836c08..377f26d 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -87,8 +87,6 @@ fn main() {
.any(|a| a == "--immediate")
.then_some(wgpu::PresentMode::Immediate)
.unwrap_or(wgpu::PresentMode::Fifo);
- let idle_skip = args.iter().any(|a| a == "--idle-skip");
-
let ipl_path = args.iter().position(|a| a == "--ipl").map(|i| &args[i + 1]);
let rom_path = args
.iter()
@@ -100,14 +98,14 @@ fn main() {
let mut emulator = if let Some(ipl) = ipl_path {
let ipl_data = std::fs::read(ipl).expect("failed to read IPL");
- GameCube::with_ipl(&ipl_data, idle_skip)
+ GameCube::with_ipl(&ipl_data)
} else if let Some(rom) = rom_path {
let rom_data = std::fs::read(rom).expect("failed to read ROM");
let dol = Dol::parse(rom_data);
- GameCube::with_image(&dol, idle_skip)
+ GameCube::with_image(&dol)
} else {
eprintln!(
- "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] [--idle-skip]",
+ "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] ",
args[0]
);
std::process::exit(1);
diff --git a/crates/debugger/src/windows/dsp.rs b/crates/debugger/src/windows/dsp.rs
index 321fe8b..55794fd 100644
--- a/crates/debugger/src/windows/dsp.rs
+++ b/crates/debugger/src/windows/dsp.rs
@@ -1,4 +1,4 @@
-use egui::{Align, Color32, Context, Grid, RichText, ScrollArea};
+use egui::{Color32, Context, Grid, RichText, ScrollArea};
use gecko::flipper::dsp::Dsp;
fn token_color(token: &disasm::tokenizer::AsmToken<'_>) -> Option<Color32> {
@@ -112,10 +112,6 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
});
ui.end_row();
- if is_pc {
- ui.scroll_to_cursor(Some(Align::Min));
- }
-
addr += words;
}
});
diff --git a/crates/disasm/Cargo.toml b/crates/disasm/Cargo.toml
index 1da0a49..45856a3 100644
--- a/crates/disasm/Cargo.toml
+++ b/crates/disasm/Cargo.toml
@@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024"
[build-dependencies]
-chipi-build = "0.7.0"
\ No newline at end of file
+chipi-build = "0.8.2"
\ No newline at end of file
diff --git a/crates/dspemu/Cargo.toml b/crates/dspemu/Cargo.toml
new file mode 100644
index 0000000..d6d55c9
--- /dev/null
+++ b/crates/dspemu/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "dspemu"
+version = "0.1.0"
+edition = "2024"
+
+[features]
+idle-skip = ["gecko/idle-skip"]
+
+[dependencies]
+gecko = { path = "../gecko" }
+disasm = { path = "../disasm" }
+clap = { version = "4.6.0", features = ["derive"] }
+tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
diff --git a/crates/dspemu/src/main.rs b/crates/dspemu/src/main.rs
new file mode 100644
index 0000000..caad170
--- /dev/null
+++ b/crates/dspemu/src/main.rs
@@ -0,0 +1,116 @@
+use clap::Parser;
+use disasm::dsp::GcDspInstruction;
+use gecko::gamecube::GameCube;
+
+#[derive(Parser)]
+#[command(about = "DSP microcode tracer")]
+struct Args {
+ /// Path to a DSP binary (raw IRAM image, loaded at 0x0000)
+ #[arg(long)]
+ bin: String,
+
+ /// Path to a DSP ROM image (loaded into IROM at 0x8000)
+ #[arg(long)]
+ rom: Option<String>,
+
+ /// Path to a DRAM image (loaded at data memory 0x0000)
+ #[arg(long)]
+ dram: Option<String>,
+
+ /// Path to a coefficient table (loaded at data memory 0x1000)
+ #[arg(long)]
+ coef: Option<String>,
+
+ /// Start address (default: 0x0000)
+ #[arg(long, default_value = "0", value_parser = parse_hex)]
+ start: u16,
+
+ /// Stop after N instructions
+ #[arg(long)]
+ max_steps: Option<u64>,
+
+ /// Stop when PC reaches this address
+ #[arg(long, value_parser = parse_hex)]
+ until: Option<u16>,
+
+ /// Print instruction trace
+ #[arg(long, default_value_t = true)]
+ trace: bool,
+}
+
+fn parse_hex(s: &str) -> Result<u16, String> {
+ u16::from_str_radix(s.trim_start_matches("0x"), 16).map_err(|e| format!("invalid hex: {e}"))
+}
+
+fn load_into(dst: &mut [u8], path: &str) {
+ let data = std::fs::read(path).unwrap_or_else(|e| panic!("failed to read {path}: {e}"));
+ let len = data.len().min(dst.len());
+ dst[..len].copy_from_slice(&data[..len]);
+}
+
+fn main() {
+ tracing_subscriber::fmt()
+ .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
+ .init();
+
+ let args = Args::parse();
+
+ let mut emu = GameCube::new(0);
+
+ // Load DSP binary into IRAM
+ load_into(&mut *emu.dsp.iram, &args.bin);
+
+ // Load optional ROM/DRAM/COEF
+ if let Some(rom) = &args.rom {
+ load_into(&mut *emu.dsp.irom, rom);
+ }
+ if let Some(dram) = &args.dram {
+ load_into(&mut *emu.dsp.dram, dram);
+ }
+ if let Some(coef) = &args.coef {
+ load_into(&mut *emu.dsp.coef, coef);
+ }
+
+ // Set start address and enable DSP
+ emu.dsp.registers.pc = args.start;
+ emu.dsp.csr.set_reset(false);
+ emu.dsp.csr.set_halt(false);
+
+ let mut steps: u64 = 0;
+
+ loop {
+ if emu.dsp.csr.halt() {
+ eprintln!("DSP halted at PC={:04X} after {steps} steps", emu.dsp.registers.pc);
+ break;
+ }
+
+ if let Some(max) = args.max_steps {
+ if steps >= max {
+ eprintln!("reached max steps ({max}) at PC={:04X}", emu.dsp.registers.pc);
+ break;
+ }
+ }
+
+ if let Some(until) = args.until {
+ if emu.dsp.registers.pc == until {
+ eprintln!("reached target PC={until:04X} after {steps} steps");
+ break;
+ }
+ }
+
+ let pc = emu.dsp.registers.pc;
+
+ if args.trace {
+ let bytes = &emu.dsp.iram[pc as usize * 2..];
+ if let Some((insn, _)) = GcDspInstruction::decode(bytes) {
+ println!("{pc:04X} {insn}");
+ } else {
+ let w = u16::from_be_bytes([bytes[0], bytes[1]]);
+ println!("{pc:04X} .word {w:#06x}");
+ }
+ }
+
+ emu.tick_dsp();
+ steps += 1;
+ }
+}
diff --git a/crates/gctool/src/disassembly.rs b/crates/gctool/src/disassembly.rs
index 1b95640..a3f1f12 100644
--- a/crates/gctool/src/disassembly.rs
+++ b/crates/gctool/src/disassembly.rs
@@ -17,7 +17,8 @@ pub fn disassemble_ppc(data: &[u8], start: usize) {
}
pub fn disassemble_dsp(data: &[u8], start: usize) {
- let mut offset = start;
+ // start is a word address; convert to byte offset
+ let mut offset = start * 2;
while offset + 2 <= data.len() {
let word = u16::from_be_bytes(data[offset..offset + 2].try_into().unwrap());
let addr = (offset / 2) as u32;
diff --git a/crates/gecko/Cargo.toml b/crates/gecko/Cargo.toml
index 1018f0b..7727e32 100644
--- a/crates/gecko/Cargo.toml
+++ b/crates/gecko/Cargo.toml
@@ -4,11 +4,12 @@ version = "0.1.0"
edition = "2024"
[features]
+idle-skip = []
scripting = []
scripting-mut-traps = ["scripting"]
[build-dependencies]
-chipi-build = "0.7.2"
+chipi-build = "0.8.2"
[dependencies]
chapa = "0.1.2"
diff --git a/crates/gecko/chipi.toml b/crates/gecko/chipi.toml
index 6cfb3e1..96185b9 100644
--- a/crates/gecko/chipi.toml
+++ b/crates/gecko/chipi.toml
@@ -265,3 +265,20 @@ mul = [
"mulcmv", "mulcmvz", "mulcac",
"madd", "msub", "maddx", "msubx", "maddc", "msubc",
]
+
+[lut.subdecoder_instr_type_outputs]
+GcDspExt = "$OUT_DIR/dsp_ext_instr.rs"
+
+[lut.subdecoder_instr_types]
+GcDspExt = "crate::flipper::dsp::instruction::GcDspExt"
+
+[lut.subdecoder_groups.GcDspExt]
+ext_addr = ["ext_dr", "ext_ir", "ext_nr"]
+ext_load = ["ext_l", "ext_ln"]
+ext_store = ["ext_s", "ext_sn"]
+ext_load_store = [
+ "ext_ls", "ext_sl", "ext_lsn", "ext_sln",
+ "ext_lsm", "ext_slm", "ext_lsnm", "ext_slnm",
+]
+ext_ld = ["ext_ld_00", "ext_ldn_01", "ext_ldm_10", "ext_ldnm_11"]
+ext_ldax = ["ext_ldax", "ext_ldaxn", "ext_ldaxm", "ext_ldaxnm"]
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index f4c3dd7..b1dc0bc 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -20,13 +20,13 @@ pub struct Dsp {
pub registers: core::Registers,
// IMEM = IRAM + IROM
- pub iram: Box<[u8; 0x1000]>, // 0x0000 - 0x0FFF
- pub irom: Box<[u8; 0x1000]>, // 0x8000 - 0x8FFF
+ pub iram: Box<[u8; 0x2000]>, // 0x0000 - 0x0FFF
+ pub irom: Box<[u8; 0x2000]>, // 0x8000 - 0x8FFF
// DMEM = DRAM + COEF + IFX
- pub dram: Box<[u8; 0x1000]>, // 0x0000 - 0x0FFF
- pub coef: Box<[u8; 0x1000]>, // 0x1000 - 0x1FFF
- pub ifx: Box<[u8; 0x100]>, // 0xFF00 - 0xFFFF
+ pub dram: Box<[u8; 0x2000]>, // 0x0000 - 0x0FFF (0x1000 words)
+ pub coef: Box<[u8; 0x2000]>, // 0x1000 - 0x1FFF (0x1000 words)
+ pub ifx: Box<[u8; 0x200]>, // 0xFF00 - 0xFFFF (0x100 words)
// Auxiliary RAM (16 MB)
pub aram: Box<[u8; 16 * 1024 * 1024]>,
@@ -38,6 +38,7 @@ pub struct Dsp {
pub mailbox_to_cpu_hi: regs::MailboxToCpuHi,
pub mailbox_to_cpu_lo: regs::MailboxToCpuLo,
pub aram_info: regs::AramInfo,
+ pub aram_mode: regs::AramMode,
pub aram_refresh: regs::AramRefresh,
pub aram_dma_mmio_addr: regs::AramDmaMmioAddr,
pub aram_dma_aram_addr: regs::AramDmaAramAddr,
@@ -51,11 +52,11 @@ pub struct Dsp {
impl Dsp {
pub fn new() -> Self {
let aram = unsafe { Box::<[u8; 16 * 1024 * 1024]>::new_zeroed().assume_init() };
- let iram = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
- let irom = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
- let dram = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
- let coef = unsafe { Box::<[u8; 0x1000]>::new_zeroed().assume_init() };
- let ifx = unsafe { Box::<[u8; 0x100]>::new_zeroed().assume_init() };
+ let iram = unsafe { Box::<[u8; 0x2000]>::new_zeroed().assume_init() };
+ let irom = unsafe { Box::<[u8; 0x2000]>::new_zeroed().assume_init() };
+ let dram = unsafe { Box::<[u8; 0x2000]>::new_zeroed().assume_init() };
+ let coef = unsafe { Box::<[u8; 0x2000]>::new_zeroed().assume_init() };
+ let ifx = unsafe { Box::<[u8; 0x200]>::new_zeroed().assume_init() };
Dsp {
registers: core::Registers::default(),
@@ -71,6 +72,7 @@ impl Dsp {
mailbox_to_cpu_hi: regs::MailboxToCpuHi::from_raw(0),
mailbox_to_cpu_lo: regs::MailboxToCpuLo::from_raw(0),
aram_info: regs::AramInfo::from_raw(0),
+ aram_mode: regs::AramMode::from_raw(0),
aram_refresh: regs::AramRefresh::from_raw(0),
aram_dma_mmio_addr: regs::AramDmaMmioAddr::from_raw(0),
aram_dma_aram_addr: regs::AramDmaAramAddr::from_raw(0),
@@ -146,7 +148,7 @@ impl GameCube {
return;
}
- let instr = Instruction::from_be_bytes(&self.dsp.iram[self.dsp.registers.pc as usize..]);
+ let instr = Instruction::from_be_bytes(&self.dsp.iram[(self.dsp.registers.pc as usize) * 2..]);
self.dsp.registers.cia = self.dsp.registers.pc;
self.dsp.registers.nia = self
.dsp
@@ -156,6 +158,12 @@ impl GameCube {
crate::flipper::dsp::lut::dispatch(self, instr);
+ // Dispatch extension opcode if present
+ if let Some(ext) = instr.ext_opcode() {
+ let ext = instruction::GcDspExt(ext);
+ crate::flipper::dsp::lut::dispatch_gc_dsp_ext(self, ext);
+ }
+
// Check if we've reached the end of a loop stack
let is_end_of_loop = self.dsp.registers.nia == self.dsp.registers.loop_addr.top();
if !self.dsp.registers.loop_addr.is_empty() && is_end_of_loop {
@@ -185,6 +193,7 @@ impl MmioRw for Dsp {
regs::MailboxToCpuHi,
regs::MailboxToCpuLo,
regs::AramInfo,
+ regs::AramMode,
regs::AramRefresh,
regs::AramDmaMmioAddr,
regs::AramDmaAramAddr,
diff --git a/crates/gecko/src/flipper/dsp/core.rs b/crates/gecko/src/flipper/dsp/core.rs
index b7b7c38..c504f46 100644
--- a/crates/gecko/src/flipper/dsp/core.rs
+++ b/crates/gecko/src/flipper/dsp/core.rs
@@ -70,6 +70,51 @@ pub struct Registers {
}
impl Registers {
+ /// Get 40-bit accumulator as i64 (sign-extended from bit 39).
+ #[inline(always)]
+ pub fn ac(&self, idx: u8) -> i64 {
+ let (high, mid, low) = match idx {
+ 0 => (self.ac0_high, self.ac0_mid, self.ac0_low),
+ 1 => (self.ac1_high, self.ac1_mid, self.ac1_low),
+ _ => unreachable!(),
+ };
+ let raw = ((high as u64 & 0xFF) << 32) | ((mid as u64) << 16) | (low as u64);
+ ((raw as i64) << 24) >> 24
+ }
+
+ /// Update flags based on a 40-bit result of subtraction.
+ /// Sets: OS, TB, S32, S, AZ, O, C.
+ #[inline(always)]
+ pub fn update_flags_sub(&mut self, a: i64, b: i64, result: i64) {
+ let r40 = result as u64 & 0xFF_FFFF_FFFF;
+ let a40 = a as u64 & 0xFF_FFFF_FFFF;
+
+ let sign = (r40 >> 39) & 1 != 0;
+ let zero = r40 == 0;
+ // Carry for subtraction: A >= result (unsigned 40-bit), i.e. no borrow
+ let carry = a40 >= r40;
+ // Overflow: sign of A != sign of B, and sign of result != sign of A
+ let a_sign = (a as u64 >> 39) & 1;
+ let b_sign = (b as u64 >> 39) & 1;
+ let r_sign = (r40 >> 39) & 1;
+ let overflow = (a_sign != b_sign) && (r_sign != a_sign);
+ // Above s32: upper 9 bits (39:31) are not all the same
+ let upper9 = (r40 >> 31) & 0x1FF;
+ let above_s32 = upper9 != 0 && upper9 != 0x1FF;
+ // Top two bits equal
+ let tb = ((r40 >> 39) & 1) == ((r40 >> 38) & 1);
+
+ self.status.set_s(sign);
+ self.status.set_z(zero);
+ self.status.set_c(carry);
+ self.status.set_o(overflow);
+ if overflow {
+ self.status.set_os(true);
+ }
+ self.status.set_as32(above_s32);
+ self.status.set_tb(tb);
+ }
+
#[inline(always)]
pub fn sign_extended(&self) -> bool {
self.status.sxm() == SignExtensionMode::Bits40
diff --git a/crates/gecko/src/flipper/dsp/instruction.rs b/crates/gecko/src/flipper/dsp/instruction.rs
index cb19221..3286dae 100644
--- a/crates/gecko/src/flipper/dsp/instruction.rs
+++ b/crates/gecko/src/flipper/dsp/instruction.rs
@@ -1 +1,20 @@
include!(concat!(env!("OUT_DIR"), "/dsp_instr.rs"));
+include!(concat!(env!("OUT_DIR"), "/dsp_ext_instr.rs"));
+
+impl Instruction {
+ /// Extract the extension opcode byte from the instruction, if present.
+ /// Returns `Some(ext)` for instructions with upper nibble >= 3, `None` otherwise.
+ #[inline]
+ pub fn ext_opcode(&self) -> Option<u8> {
+ let nibble = (self.0 >> 12) & 0xF;
+ if nibble >= 3 {
+ Some(if nibble == 3 {
+ self.ext_9_15()
+ } else {
+ self.ext_8_15()
+ })
+ } else {
+ None
+ }
+ }
+}
diff --git a/crates/gecko/src/flipper/dsp/interpreter.rs b/crates/gecko/src/flipper/dsp/interpreter.rs
index 69f9869..48659ff 100644
--- a/crates/gecko/src/flipper/dsp/interpreter.rs
+++ b/crates/gecko/src/flipper/dsp/interpreter.rs
@@ -1,4 +1,8 @@
-use crate::flipper::dsp::{condition::BranchControl, core::{SignExtensionMode, StatusRegister}, lut::*};
+use crate::flipper::dsp::{
+ condition::BranchControl,
+ core::{SignExtensionMode, StatusRegister},
+ lut::*,
+};
#[inline(always)]
pub fn add_sub<const OP: u32>(
@@ -36,12 +40,24 @@ pub fn addr_reg<const OP: u32>(
#[inline(always)]
pub fn cmp_test<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
- OP_CMP => todo!("cmp"),
- OP_CMPAXH => todo!("cmpaxh"),
+ OP_CMP => {
+ let a = ctx.dsp.registers.ac(0);
+ let b = ctx.dsp.registers.ac(1);
+ let result = a.wrapping_sub(b);
+ ctx.dsp.registers.update_flags_sub(a, b, result);
+ }
+ OP_CMPAXH => {
+ let s = instr.s_4_4();
+ let r = instr.s_3_3();
+ let a = ctx.dsp.registers.ac(r);
+ let b = (ctx.dsp.registers.axh[s as usize] as i16 as i64) << 16;
+ let result = a.wrapping_sub(b);
+ ctx.dsp.registers.update_flags_sub(a, b, result);
+ }
OP_TST => todo!("tst"),
OP_TSTPROD => todo!("tstprod"),
OP_TSTAXH => todo!("tstaxh"),
@@ -66,10 +82,10 @@ pub fn control<const OP: u32>(
ctx.dsp.registers.nia = instr.addr();
}
}
- OP_NOP => {},
+ OP_NOP => {}
OP_HALT => {
ctx.dsp.csr.set_halt(true);
- },
+ }
OP_IFCC => todo!("ifcc"),
OP_CALLCC => todo!("callcc"),
OP_RETCC => todo!("retcc"),
@@ -79,7 +95,7 @@ pub fn control<const OP: u32>(
ctx.dsp.registers.status = StatusRegister::from(ctx.dsp.registers.data_stack.pop());
ctx.dsp.registers.nia = ctx.dsp.registers.call_stack.pop();
}
- },
+ }
OP_JRCC => todo!("jrcc"),
OP_CALLRCC => todo!("callrcc"),
_ => unreachable!(),
@@ -96,9 +112,19 @@ pub fn imm_alu<const OP: u32>(
OP_XORI => todo!("xori"),
OP_ANDI => todo!("andi"),
OP_ORI => todo!("ori"),
- OP_CMPI => todo!("cmpi"),
+ OP_CMPI => {
+ let d = instr.d_7_7();
+ let a = ctx.dsp.registers.ac(d);
+ let b = (instr.imm_16_31() as i16 as i64) << 16;
+ let result = a.wrapping_sub(b);
+ ctx.dsp.registers.update_flags_sub(a, b, result);
+ }
OP_ANDF | OP_ANDCF => {
- let ac_mid = if instr.d_7_7() != 0 { ctx.dsp.registers.ac1_mid } else { ctx.dsp.registers.ac0_mid };
+ let ac_mid = if instr.d_7_7() != 0 {
+ ctx.dsp.registers.ac1_mid
+ } else {
+ ctx.dsp.registers.ac0_mid
+ };
let imm = instr.imm_16_31();
let result = ac_mid & imm;
let lz = match OP {
@@ -107,9 +133,15 @@ pub fn imm_alu<const OP: u32>(
_ => unreachable!(),
};
ctx.dsp.registers.status.set_lz(lz);
- },
+ }
OP_ADDIS => todo!("addis"),
- OP_CMPIS => todo!("cmpis"),
+ OP_CMPIS => {
+ let d = instr.d_7_7();
+ let a = ctx.dsp.registers.ac(d);
+ let b = (instr.imm_8_15_i16() as i64) << 16;
+ let result = a.wrapping_sub(b);
+ ctx.dsp.registers.update_flags_sub(a, b, result);
+ }
OP_LRIS => todo!("lris"),
_ => unreachable!(),
}
@@ -139,18 +171,26 @@ pub fn load_store<const OP: u32>(
match OP {
OP_LRI => {
ctx.dsp.registers.write::<true>(instr.d_11_15(), instr.imm_16_31());
- },
+ }
OP_LR => todo!("lr"),
OP_SR => todo!("sr"),
OP_MRR => todo!("mrr"),
OP_SI => {
let addr = 0xFF00 | (instr.mem_8_15_u16());
ctx.dsp.write_dmem(addr, instr.imm_16_31());
- },
- OP_LRR => todo!("lrr"),
- OP_LRRD => todo!("lrrd"),
- OP_LRRI => todo!("lrri"),
- OP_LRRN => todo!("lrrn"),
+ }
+ OP_LRR | OP_LRRD | OP_LRRI | OP_LRRN => {
+ let s = instr.s_9_10() as usize;
+ let d = instr.d_11_15();
+ let value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);
+ ctx.dsp.registers.write::<true>(d, value);
+ match OP {
+ OP_LRRD => ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_sub(1),
+ OP_LRRI => ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1),
+ OP_LRRN => ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]),
+ _ => {}
+ }
+ }
OP_SRR => todo!("srr"),
OP_SRRD => todo!("srrd"),
OP_SRRI => todo!("srri"),
@@ -160,17 +200,23 @@ pub fn load_store<const OP: u32>(
let addr = ((ctx.dsp.registers.config as u16) << 8) | instr.mem_8_15_u16();
let value = ctx.dsp.read_dmem(addr);
ctx.dsp.registers.write::<true>(dst, value);
- },
+ }
OP_SRSH | OP_SRS => {
let addr = ((ctx.dsp.registers.config as u16) << 8) | instr.mem_8_15_u16();
let src = match OP {
- OP_SRSH => if instr.s_7_7() != 0 { 16 } else { 17 }, // ac0.h (16) or ac1.h (17)
+ OP_SRSH => {
+ if instr.s_7_7() != 0 {
+ 16
+ } else {
+ 17
+ }
+ } // ac0.h (16) or ac1.h (17)
OP_SRS => 0x1C + instr.reg_6_7(),
_ => unreachable!(),
};
let value = ctx.dsp.registers.read::<true>(src);
ctx.dsp.write_dmem(addr, value);
- },
+ }
OP_ILRR | OP_ILRRD | OP_ILRRI | OP_ILRRN => {
let src = instr.s_14_15() as usize;
let dst = if instr.d_7_7() != 0 { 31u8 } else { 30u8 }; // ac1.m or ac0.m
@@ -179,10 +225,12 @@ pub fn load_store<const OP: u32>(
match OP {
OP_ILRRD => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_sub(1),
OP_ILRRI => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_add(1),
- OP_ILRRN => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_add(ctx.dsp.registers.ix[src]),
+ OP_ILRRN => {
+ ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_add(ctx.dsp.registers.ix[src])
+ }
_ => {}
}
- },
+ }
_ => unreachable!(),
}
}
@@ -205,27 +253,39 @@ pub fn logic<const OP: u32>(
}
#[inline(always)]
-pub fn loops<const OP: u32>(
- ctx: &mut crate::gamecube::GameCube,
- instr: crate::flipper::dsp::instruction::Instruction,
-) {
+pub fn loops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::flipper::dsp::instruction::Instruction) {
match OP {
- OP_LOOP_REG => todo!("loop_reg"),
- OP_BLOOP => {
- let reg = instr.reg_11_15();
- let counter = ctx.dsp.registers.read::<true>(reg);
+ OP_LOOP_REG | OP_LOOPI => {
+ let counter = match OP {
+ OP_LOOP_REG => ctx.dsp.registers.read::<true>(instr.reg_11_15()),
+ OP_LOOPI => instr.imm_8_15_u8() as u16,
+ _ => unreachable!(),
+ };
+ let end_addr = ctx.dsp.registers.nia; // the instruction to loop on
+ if counter != 0 {
+ ctx.dsp.registers.call_stack.push(end_addr);
+ ctx.dsp.registers.loop_addr.push(end_addr.wrapping_add(1));
+ ctx.dsp.registers.loop_counter.push(counter);
+ } else {
+ // Skip the looped instruction
+ ctx.dsp.registers.nia = end_addr.wrapping_add(1);
+ }
+ }
+ OP_BLOOP | OP_BLOOPI => {
+ let counter = match OP {
+ OP_BLOOP => ctx.dsp.registers.read::<true>(instr.reg_11_15()),
+ OP_BLOOPI => instr.imm_8_15_u8() as u16,
+ _ => unreachable!(),
+ };
let end_addr = instr.addr();
-
if counter != 0 {
- ctx.dsp.registers.call_stack.push(ctx.dsp.registers.pc.wrapping_add(2));
- ctx.dsp.registers.loop_addr.push(end_addr);
+ ctx.dsp.registers.call_stack.push(ctx.dsp.registers.nia);
+ ctx.dsp.registers.loop_addr.push(end_addr.wrapping_add(1));
ctx.dsp.registers.loop_counter.push(counter);
} else {
ctx.dsp.registers.nia = end_addr.wrapping_add(1);
}
- },
- OP_LOOPI => todo!("loopi"),
- OP_BLOOPI => todo!("bloopi"),
+ }
_ => unreachable!(),
}
}
@@ -306,25 +366,167 @@ pub fn status<const OP: u32>(
}
OP_SBSET => {
ctx.dsp.registers.status |= (1 << (6 + instr.bit())) as u16;
- },
+ }
OP_M2 => {
ctx.dsp.registers.status.set_am(false);
- },
+ }
OP_M0 => {
ctx.dsp.registers.status.set_am(true);
- },
+ }
OP_CLR15 => {
ctx.dsp.registers.status.set_su(false);
- },
+ }
OP_SET15 => {
ctx.dsp.registers.status.set_su(true);
- },
+ }
OP_SET16 => {
ctx.dsp.registers.status.set_sxm(SignExtensionMode::Bits16);
}
OP_SET40 => {
ctx.dsp.registers.status.set_sxm(SignExtensionMode::Bits40);
- },
+ }
+ _ => unreachable!(),
+ }
+}
+
+// Extension opcode handlers
+use crate::flipper::dsp::instruction::GcDspExt;
+
+#[inline(always)]
+pub fn ext_nop(_ctx: &mut crate::gamecube::GameCube, _instr: GcDspExt) {}
+
+#[inline(always)]
+pub fn ext_addr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let r = instr.r_6_7() as usize;
+ match OP {
+ OP_EXT_DR => ctx.dsp.registers.ar[r] = ctx.dsp.registers.ar[r].wrapping_sub(1),
+ OP_EXT_IR => ctx.dsp.registers.ar[r] = ctx.dsp.registers.ar[r].wrapping_add(1),
+ OP_EXT_NR => ctx.dsp.registers.ar[r] = ctx.dsp.registers.ar[r].wrapping_add(ctx.dsp.registers.ix[r]),
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub fn ext_mv(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let d = instr.d_4_5();
+ let s = instr.s_6_7();
+ let value = ctx.dsp.registers.read::<true>(0x1C + s);
+ ctx.dsp.registers.write::<false>(0x18 + d, value);
+}
+
+#[inline(always)]
+pub fn ext_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let s = instr.s_3_4();
+ let d = instr.d_6_7() as usize;
+ let value = ctx.dsp.registers.read::<true>(0x1C + s);
+ ctx.dsp.write_dmem(ctx.dsp.registers.ar[d], value);
+ match OP {
+ OP_EXT_S => ctx.dsp.registers.ar[d] = ctx.dsp.registers.ar[d].wrapping_add(1),
+ OP_EXT_SN => ctx.dsp.registers.ar[d] = ctx.dsp.registers.ar[d].wrapping_add(ctx.dsp.registers.ix[d]),
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub fn ext_load<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let d = instr.d_2_4();
+ let s = instr.s_6_7() as usize;
+ let value = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);
+ ctx.dsp.registers.write::<false>(0x18 + d, value);
+ match OP {
+ OP_EXT_L => ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1),
+ OP_EXT_LN => ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]),
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub fn ext_load_store<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ // 'LS: load from $ar0 into $(0x18+D), store $acS.m to $ar3
+ // 'SL: load from $ar3 into $(0x18+D), store $acS.m to $ar0
+ let store_val = ctx.dsp.registers.read::<true>(30 + instr.s_7_7()); // $acS.m
+ let is_sl = matches!(OP, OP_EXT_SL | OP_EXT_SLN | OP_EXT_SLM | OP_EXT_SLNM);
+ let (load_ar, store_ar) = if is_sl { (3, 0) } else { (0, 3) };
+ let load_val = ctx.dsp.read_dmem(ctx.dsp.registers.ar[load_ar]);
+ ctx.dsp.registers.write::<false>(0x18 + instr.d_2_3(), load_val);
+ ctx.dsp.write_dmem(ctx.dsp.registers.ar[store_ar], store_val);
+ // Post-modify: N = ar0 += ix0, M = ar3 += ix3
+ match OP {
+ OP_EXT_LS | OP_EXT_SL => {
+ ctx.dsp.registers.ar[0] = ctx.dsp.registers.ar[0].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LSN | OP_EXT_SLN => {
+ ctx.dsp.registers.ar[0] = ctx.dsp.registers.ar[0].wrapping_add(ctx.dsp.registers.ix[0]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LSM | OP_EXT_SLM => {
+ ctx.dsp.registers.ar[0] = ctx.dsp.registers.ar[0].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
+ OP_EXT_LSNM | OP_EXT_SLNM => {
+ ctx.dsp.registers.ar[0] = ctx.dsp.registers.ar[0].wrapping_add(ctx.dsp.registers.ix[0]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub fn ext_ld<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let d = instr.d_2_2();
+ let r = instr.r_3_3();
+ let s = instr.s_6_7() as usize;
+ ctx.dsp
+ .registers
+ .write::<false>(0x18 + d * 2, ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]));
+ ctx.dsp
+ .registers
+ .write::<false>(0x19 + r * 2, ctx.dsp.read_dmem(ctx.dsp.registers.ar[3]));
+ match OP {
+ OP_EXT_LD_00 => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LDN_01 => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LDM_10 => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
+ OP_EXT_LDNM_11 => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub fn ext_ldax<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: GcDspExt) {
+ let r = instr.r_3_3() as usize;
+ let s = instr.s_2_2() as usize;
+ ctx.dsp.registers.axh[r] = ctx.dsp.read_dmem(ctx.dsp.registers.ar[s]);
+ ctx.dsp.registers.ax[r] = ctx.dsp.read_dmem(ctx.dsp.registers.ar[3]);
+ match OP {
+ OP_EXT_LDAX => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LDAXN => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(1);
+ }
+ OP_EXT_LDAXM => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(1);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
+ OP_EXT_LDAXNM => {
+ ctx.dsp.registers.ar[s] = ctx.dsp.registers.ar[s].wrapping_add(ctx.dsp.registers.ix[s]);
+ ctx.dsp.registers.ar[3] = ctx.dsp.registers.ar[3].wrapping_add(ctx.dsp.registers.ix[3]);
+ }
_ => unreachable!(),
}
}
diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rs
index 6956cb2..8c7d210 100644
--- a/crates/gecko/src/flipper/dsp/regs.rs
+++ b/crates/gecko/src/flipper/dsp/regs.rs
@@ -140,6 +140,12 @@ crate::mmio_register! {
AramInfo: u16 @ 0xCC005012 => Dsp.aram_info {}
}
+// 0xCC005016 2 [R/W] AR_MODE - ARAM Mode
+
+crate::mmio_register! {
+ AramMode: u16 @ 0xCC005016 => Dsp.aram_mode {}
+}
+
// 0xCC00501A 2 [R/W] AR_REFRESH - ARAM Refresh
crate::mmio_register! {
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index db81568..e68d7ec 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -15,10 +15,11 @@ use crate::{
si::{SerialInterface, pad},
vi::VideoInterface,
},
- idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector},
mmio::Mmio,
scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
};
+#[cfg(feature = "idle-skip")]
+use crate::idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector};
use image::Executable;
pub struct GameCube {
@@ -37,6 +38,7 @@ pub struct GameCube {
pub si: SerialInterface,
pub ai: AudioInterface,
pub mi: MemoryInterface,
+ #[cfg(feature = "idle-skip")]
idle: IdleDetector,
#[cfg(feature = "scripting")]
@@ -69,7 +71,7 @@ impl GameCube {
let _ = host;
}
- pub fn new(entrypoint: u32, idle_skip: bool) -> Self {
+ pub fn new(entrypoint: u32) -> Self {
GameCube {
vsync_pending: false,
cpu: Cpu::new(entrypoint),
@@ -86,7 +88,8 @@ impl GameCube {
si: SerialInterface::new(),
ai: AudioInterface::new(),
mi: MemoryInterface::new(),
- idle: IdleDetector::new(idle_skip),
+ #[cfg(feature = "idle-skip")]
+ idle: IdleDetector::new(),
#[cfg(feature = "scripting")]
script_host: None,
@@ -97,8 +100,8 @@ impl GameCube {
}
}
- pub fn with_image(exe: &impl Executable, idle_skip: bool) -> Self {
- let mut emulator = GameCube::new(exe.entry_point(), idle_skip);
+ pub fn with_image(exe: &impl Executable) -> Self {
+ let mut emulator = GameCube::new(exe.entry_point());
let data = exe.data();
// Copy TEXT sections to memory
@@ -129,7 +132,7 @@ impl GameCube {
emulator
}
- pub fn with_ipl(ipl: &[u8], idle_skip: bool) -> Self {
+ pub fn with_ipl(ipl: &[u8]) -> Self {
// Text Sections (1):
// | idx | offset | vaddr | size | end |
// |-----|------------|------------|------------|------------|
@@ -141,7 +144,7 @@ impl GameCube {
// BSS: 0x00000000 - 0x00000000 (size: 0x00000000)
// => BS2 DOL, does not apply to the actual IPL here!!
- let mut emulator = GameCube::new(IPL_RESET_VECTOR, idle_skip);
+ let mut emulator = GameCube::new(IPL_RESET_VECTOR);
emulator.cpu.msr.set_ip(true);
emulator.mmio.ipl = ipl.to_vec();
emulator.exi.attach_device(
@@ -219,6 +222,7 @@ impl GameCube {
}
}
+ #[cfg(feature = "idle-skip")]
match self.idle.check(self.cpu.cia, self.cpu.nia) {
IdleCheck::Skip => {
if let Some(deadline) = self.scheduler.next_event_deadline() {
@@ -256,6 +260,7 @@ impl GameCube {
/// Read the instructions in `[start, end]` and check whether the loop is a
/// side effect free MMIO polling loop that can safely be skipped.
+ #[cfg(feature = "idle-skip")]
#[inline(always)]
fn is_polling_loop(&self, start: u32, end: u32) -> bool {
let count = ((end - start) / 4 + 1) as usize;
diff --git a/crates/gecko/src/idle.rs b/crates/gecko/src/idle.rs
index 124b1f2..5746031 100644
--- a/crates/gecko/src/idle.rs
+++ b/crates/gecko/src/idle.rs
@@ -18,7 +18,6 @@ pub enum IdleCheck {
}
pub struct IdleDetector {
- enabled: bool,
loop_pc: Option<u32>,
loop_end: u32,
loop_count: u32,
@@ -36,9 +35,8 @@ enum LoopStatus {
}
impl IdleDetector {
- pub fn new(enabled: bool) -> Self {
+ pub fn new() -> Self {
Self {
- enabled,
loop_pc: None,
loop_end: 0,
loop_count: 0,
@@ -48,10 +46,6 @@ impl IdleDetector {
#[inline]
pub fn check(&mut self, cia: u32, nia: u32) -> IdleCheck {
- if !self.enabled {
- return IdleCheck::Continue;
- }
-
// Branching to current is always a safe idle loop
if nia == cia {
return IdleCheck::Skip;
diff --git a/crates/tinyapp/Cargo.toml b/crates/tinyapp/Cargo.toml
index 015217f..f136a45 100644
--- a/crates/tinyapp/Cargo.toml
+++ b/crates/tinyapp/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[features]
+idle-skip = ["gecko/idle-skip"]
scripting = ["gecko/scripting", "dep:scripting"]
scripting-mut-traps = ["scripting", "gecko/scripting-mut-traps", "scripting/scripting-mut-traps"]
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index a072139..2e61abd 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -481,8 +481,6 @@ fn main() {
.any(|arg| arg == "--immediate")
.then(|| wgpu::PresentMode::Immediate)
.unwrap_or(wgpu::PresentMode::Fifo);
- let idle_skip = std::env::args().any(|arg| arg == "--idle-skip");
-
let ipl_path = args.iter().position(|a| a == "--ipl").map(|i| &args[i + 1]);
let rom_path = args
.iter()
@@ -506,14 +504,14 @@ fn main() {
let mut emulator = if let Some(ipl) = ipl_path {
let ipl_data = std::fs::read(ipl).expect("failed to read IPL");
- GameCube::with_ipl(&ipl_data, idle_skip)
+ GameCube::with_ipl(&ipl_data)
} else if let Some(rom) = rom_path {
let rom_data = std::fs::read(rom).expect("failed to read ROM");
let dol = Dol::parse(rom_data);
- GameCube::with_image(&dol, idle_skip)
+ GameCube::with_image(&dol)
} else {
eprintln!(
- "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] [--idle-skip]",
+ "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] ",
args[0]
);
std::process::exit(1);
diff --git a/crates/tinyemu/Cargo.toml b/crates/tinyemu/Cargo.toml
index 846b66b..8910955 100644
--- a/crates/tinyemu/Cargo.toml
+++ b/crates/tinyemu/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[features]
+idle-skip = ["gecko/idle-skip"]
scripting = ["gecko/scripting", "dep:scripting"]
scripting-mut-traps = ["scripting", "gecko/scripting-mut-traps", "scripting/scripting-mut-traps"]
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index a6bd078..ee680c6 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -39,10 +39,6 @@ struct Args {
#[arg(long)]
quiet: bool,
- /// Skip idle loops to speed up emulation
- #[arg(long)]
- idle_skip: bool,
-
/// Disable ANSI escape codes
#[arg(long)]
no_ansi: bool,
@@ -72,10 +68,10 @@ fn main() {
let mut emulator = if let Some(rom_path) = &args.rom {
let rom_data = std::fs::read(rom_path).expect("failed to read ROM");
let dol = image::Dol::parse(rom_data);
- gecko::gamecube::GameCube::with_image(&dol, args.idle_skip)
+ 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, args.idle_skip)
+ gecko::gamecube::GameCube::with_ipl(&ipl_data)
} else {
panic!("Either --rom or --ipl must be provided");
};