Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

shenanigans for hazel

Layleafa4664683b7
diff --git a/crates/gecko/src/cpu/interpreter.rs b/crates/gecko/src/cpu/interpreter.rs
index 2733607..f9815e8 100644
--- a/crates/gecko/src/cpu/interpreter.rs
+++ b/crates/gecko/src/cpu/interpreter.rs
@@ -20,4 +20,4 @@ pub use psq::store_load_psq;
pub use rotate::rotate;
pub use store_load::{lwarx, store_load, store_load_fp, stwcx_dot};
pub use stubs::*;
-pub use system::{mftb, msr, nop, sc, segment, spr};
+pub use system::{mftb, msr, nop, sc, segment, spr, twi};
diff --git a/crates/gecko/src/cpu/interpreter/stubs.rs b/crates/gecko/src/cpu/interpreter/stubs.rs
index 99cf3e2..180c866 100644
--- a/crates/gecko/src/cpu/interpreter/stubs.rs
+++ b/crates/gecko/src/cpu/interpreter/stubs.rs
@@ -14,7 +14,6 @@ macro_rules! stub {
}
stub! {
- twi,
tw,
mtsrin, mcrxr,
lwbrx, lswx, lswi, mfsrin,
diff --git a/crates/gecko/src/cpu/interpreter/system.rs b/crates/gecko/src/cpu/interpreter/system.rs
index 0a715e6..74cdf46 100644
--- a/crates/gecko/src/cpu/interpreter/system.rs
+++ b/crates/gecko/src/cpu/interpreter/system.rs
@@ -68,6 +68,23 @@ pub fn mftb(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction:
ctx.cpu.write_gpr(instr.rd(), val);
}
+#[inline(always)]
+pub fn twi(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
+ let a = ctx.cpu.read_gpr(instr.ra()) as i32;
+ let simm = instr.simm();
+ let to = instr.to();
+
+ let trap = (to & 0x10 != 0 && a < simm)
+ || (to & 0x08 != 0 && a > simm)
+ || (to & 0x04 != 0 && a == simm)
+ || (to & 0x02 != 0 && (a as u32) < (simm as u32))
+ || (to & 0x01 != 0 && (a as u32) > (simm as u32));
+
+ if trap {
+ ctx.cause_trap_exception();
+ }
+}
+
#[inline(always)]
pub fn nop<const OP: u32>(_ctx: &mut crate::gamecube::GameCube, _instr: crate::cpu::instruction::Instruction) {}
diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/cpu/irq.rs
index 2ec3b63..0d56057 100644
--- a/crates/gecko/src/cpu/irq.rs
+++ b/crates/gecko/src/cpu/irq.rs
@@ -50,6 +50,38 @@ impl GameCube {
tracing::debug!(addr = format!("{:08X}", self.cpu.pc), "IRQ triggered");
}
+ pub fn cause_trap_exception(&mut self) {
+ let base: u32 = if self.cpu.msr.exception_prefix() {
+ 0xFFF0_0000
+ } else {
+ 0
+ };
+
+ self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);
+ // SRR1: MSR bits 0, 5-9, 16-31 preserved; bit 14 (TRAP) set
+ self.cpu.spr.srr1 =
+ chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw() | (1 << (31 - 14));
+
+ self.cpu.msr = self
+ .cpu
+ .msr
+ .with_pow(false)
+ .with_fp(false)
+ .with_be(false)
+ .with_dr(false)
+ .with_fe1(false)
+ .with_pm(false)
+ .with_ee(false)
+ .with_fe0(false)
+ .with_ri(false)
+ .with_pr(false)
+ .with_se(false)
+ .with_ir(false)
+ .with_le(self.cpu.msr.ile());
+
+ self.cpu.nia = base | IRQ_PROGRAM;
+ }
+
pub fn cause_syscall_interrupt(&mut self) {
let base: u32 = if self.cpu.msr.exception_prefix() {
0xFFF0_0000
diff --git a/crates/gecko/src/flipper/exi/macronix.rs b/crates/gecko/src/flipper/exi/macronix.rs
index 5b216f9..ebfcd96 100644
--- a/crates/gecko/src/flipper/exi/macronix.rs
+++ b/crates/gecko/src/flipper/exi/macronix.rs
@@ -217,6 +217,10 @@ impl Sram {
}
}
+ pub fn empty() -> Self {
+ Self { data: [0; SRAM_SIZE] }
+ }
+
// TODO: WIP
pub fn fix_checksums(&mut self) {
let (a, b) = Self::compute_checksums(&self.data);