Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

rename cpu to gekko

Layle9374e8c3f0a5
diff --git a/crates/dbglib/src/lib.rs b/crates/dbglib/src/lib.rs
index 1b43f10..36a8b00 100644
--- a/crates/dbglib/src/lib.rs
+++ b/crates/dbglib/src/lib.rs
@@ -223,7 +223,7 @@ impl Debugger {
self.drain_events(emulator);
self.trace_step(emulator);
emulator.step_cpu();
- if self.breakpoint_hit(emulator.cpu.pc) {
+ if self.breakpoint_hit(emulator.gekko.pc) {
self.state = EmulatorState::Paused;
return;
}
@@ -244,7 +244,7 @@ impl Debugger {
self.drain_events(emulator);
self.trace_step(emulator);
emulator.step_cpu();
- if self.breakpoint_hit(emulator.cpu.pc) {
+ if self.breakpoint_hit(emulator.gekko.pc) {
self.state = EmulatorState::Paused;
return;
}
@@ -255,11 +255,11 @@ impl Debugger {
self.state = EmulatorState::Paused;
}
EmulatorState::RunUntilAddress(addr) => {
- while emulator.cpu.pc != addr {
+ while emulator.gekko.pc != addr {
self.drain_events(emulator);
self.trace_step(emulator);
emulator.step_cpu();
- if self.breakpoint_hit(emulator.cpu.pc) {
+ if self.breakpoint_hit(emulator.gekko.pc) {
self.state = EmulatorState::Paused;
return;
}
@@ -274,7 +274,7 @@ impl Debugger {
}
self.trace_step(emulator);
emulator.step_cpu();
- if self.breakpoint_hit(emulator.cpu.pc) {
+ if self.breakpoint_hit(emulator.gekko.pc) {
self.state = EmulatorState::Paused;
return;
}
diff --git a/crates/dbglib/src/trace.rs b/crates/dbglib/src/trace.rs
index 1b7c4f0..fde508b 100644
--- a/crates/dbglib/src/trace.rs
+++ b/crates/dbglib/src/trace.rs
@@ -11,12 +11,12 @@ const COMMENT_COL: usize = 50;
///
/// Returns a string like `80003100 4E800020 blr ; lr=80001234`
pub fn format_trace_line(emulator: &GameCube) -> String {
- let pc = emulator.cpu.pc;
+ let pc = emulator.gekko.pc;
let raw = emulator.mmio.virt_read_u32(pc);
if let Some((instr, _)) = GekkoInstruction::decode(emulator.mmio.virt_slice(pc, 4)) {
let text = format!("{}", instr);
- let comment = reg_comment(&text, &emulator.cpu.gprs, &emulator.cpu.fprs);
+ let comment = reg_comment(&text, &emulator.gekko.gprs, &emulator.gekko.fprs);
let pad = COMMENT_COL.saturating_sub(DISASM_COL + text.len());
if comment.is_empty() {
format!("{:08X} {:08X} {}", pc, raw, text)
diff --git a/crates/dbglib/src/windows/callstack.rs b/crates/dbglib/src/windows/callstack.rs
index 05a4c58..ee7e86a 100644
--- a/crates/dbglib/src/windows/callstack.rs
+++ b/crates/dbglib/src/windows/callstack.rs
@@ -1,5 +1,5 @@
use egui::{Color32, Context, RichText, ScrollArea};
-use gecko::cpu::Cpu;
+use gecko::gekko::Gekko;
use gecko::mmio::Mmio;
use image::symbols::SymbolTable;
@@ -25,7 +25,7 @@ pub struct CallFrame {
///
/// We emit the current PC, then LR (handles leaf functions that never save
/// LR to the stack), then walk back-chain pointers reading saved LRs.
-pub fn walk_callstack(cpu: &Cpu, mmio: &Mmio, symbols: Option<&SymbolTable>) -> Vec<CallFrame> {
+pub fn walk_callstack(cpu: &Gekko, mmio: &Mmio, symbols: Option<&SymbolTable>) -> Vec<CallFrame> {
let mut frames = Vec::new();
let resolve = |addr: u32| -> (Option<String>, u32) {
@@ -93,7 +93,7 @@ pub fn walk_callstack(cpu: &Cpu, mmio: &Mmio, symbols: Option<&SymbolTable>) ->
frames
}
-pub fn show_callstack(ctx: &Context, open: &mut bool, cpu: &Cpu, mmio: &Mmio, symbols: Option<&SymbolTable>) {
+pub fn show_callstack(ctx: &Context, open: &mut bool, cpu: &Gekko, mmio: &Mmio, symbols: Option<&SymbolTable>) {
let frames = walk_callstack(cpu, mmio, symbols);
egui::Window::new("Call Stack").open(open).show(ctx, |ui| {
diff --git a/crates/dbglib/src/windows/cpu.rs b/crates/dbglib/src/windows/cpu.rs
index b9e420a..b1877d6 100644
--- a/crates/dbglib/src/windows/cpu.rs
+++ b/crates/dbglib/src/windows/cpu.rs
@@ -1,5 +1,5 @@
use egui::{Color32, Context, Grid, RichText, ScrollArea};
-use gecko::cpu::Cpu;
+use gecko::gekko::Gekko;
use gecko::mmio::Mmio;
use image::symbols::SymbolTable;
@@ -9,7 +9,7 @@ use crate::Breakpoint;
pub fn show_cpu(
ctx: &Context,
open: &mut bool,
- cpu: &Cpu,
+ cpu: &Gekko,
mmio: &Mmio,
symbols: Option<&SymbolTable>,
breakpoints: &[Breakpoint],
@@ -58,10 +58,10 @@ pub fn show_cpu(
ui.end_row();
for (name, getter) in [
- ("LT", (|i: u8, c: &Cpu| c.cr.get_field(i).lt()) as fn(u8, &Cpu) -> bool),
- ("GT", (|i: u8, c: &Cpu| c.cr.get_field(i).gt()) as fn(u8, &Cpu) -> bool),
- ("EQ", (|i: u8, c: &Cpu| c.cr.get_field(i).eq()) as fn(u8, &Cpu) -> bool),
- ("SO", (|i: u8, c: &Cpu| c.cr.get_field(i).so()) as fn(u8, &Cpu) -> 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.rs
index e4e9ea0..621a1bd 100644
--- a/crates/dbglib/src/windows/irq.rs
+++ b/crates/dbglib/src/windows/irq.rs
@@ -1,5 +1,5 @@
use egui::{Align, Color32, Context, Grid, Layout, RichText};
-use gecko::cpu::Cpu;
+use gecko::gekko::Gekko;
use gecko::flipper::pi::ProcessorInterface;
fn centered_icon(ui: &mut egui::Ui, icon: &str, color: Color32) {
@@ -33,7 +33,7 @@ fn interrupt_row(ui: &mut egui::Ui, name: &str, pending: bool, masked: bool) {
ui.end_row();
}
-pub fn show_irq(ctx: &Context, open: &mut bool, cpu: &Cpu, pi: &ProcessorInterface) {
+pub fn show_irq(ctx: &Context, open: &mut bool, cpu: &Gekko, pi: &ProcessorInterface) {
egui::Window::new("IRQ").open(open).show(ctx, |ui| {
let msr = &cpu.msr;
let spr = &cpu.spr;
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 6c66618..d5ce0dd 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -128,7 +128,7 @@ impl RenderState {
capture::save_png_async(capture::timestamped_path("game"), cap, true);
}
- let cpu = &emulator.cpu;
+ let cpu = &emulator.gekko;
let mmio = &emulator.mmio;
let gx = &emulator.gx;
@@ -282,7 +282,7 @@ impl RenderState {
dbglib::windows::exi::show_exi(&ctx, &mut debugger_ui.show_exi, &emulator.exi);
}
if debugger_ui.show_irqs {
- dbglib::windows::irq::show_irq(&ctx, &mut debugger_ui.show_irqs, &emulator.cpu, &emulator.pi);
+ dbglib::windows::irq::show_irq(&ctx, &mut debugger_ui.show_irqs, &emulator.gekko, &emulator.pi);
}
if debugger_ui.show_breakpoints {
dbglib::windows::breakpoints::show_breakpoints(
diff --git a/crates/gecko/gekko.bindings.chipi b/crates/gecko/gekko.bindings.chipi
index 18c5bc4..d641ebf 100644
--- a/crates/gecko/gekko.bindings.chipi
+++ b/crates/gecko/gekko.bindings.chipi
@@ -5,12 +5,12 @@ target rust {
output "$OUT_DIR/gekko_lut.rs"
context crate::gamecube::GameCube
- handlers crate::cpu::interpreter
+ handlers crate::gekko::interpreter
strategy fn_ptr_lut
- invalid_handler crate::cpu::interpreter::invalid
+ invalid_handler crate::gekko::interpreter::invalid
- instruction_type crate::cpu::instruction::Instruction {
+ instruction_type crate::gekko::instruction::Instruction {
output "$OUT_DIR/gekko_instr.rs"
}
diff --git a/crates/gecko/src/cpu/interpreter/alu.rs b/crates/gecko/src/cpu/interpreter/alu.rs
deleted file mode 100644
index 2d3f7d8..0000000
--- a/crates/gecko/src/cpu/interpreter/alu.rs
+++ /dev/null
@@ -1,273 +0,0 @@
-#[inline(always)]
-pub fn alu<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_ADDX => {
- let res = ctx.cpu.read_gpr(instr.ra()).wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_ADDI | crate::cpu::lut::OP_ADDIS => {
- let ra = ctx.cpu.read_gpr_or_zero(instr.ra());
- let simm = if OP == crate::cpu::lut::OP_ADDIS {
- instr.simm() << 16
- } else {
- instr.simm()
- };
- ctx.cpu.write_gpr(instr.rd(), ra.wrapping_add_signed(simm));
- }
- crate::cpu::lut::OP_ORI | crate::cpu::lut::OP_ORIS => {
- let imm = if OP == crate::cpu::lut::OP_ORIS {
- (instr.uimm() as u32) << 16
- } else {
- instr.uimm() as u32
- };
- ctx.cpu.write_gpr(instr.ra(), ctx.cpu.read_gpr(instr.rs()) | imm);
- }
- crate::cpu::lut::OP_XORI | crate::cpu::lut::OP_XORIS => {
- let imm = if OP == crate::cpu::lut::OP_XORIS {
- (instr.uimm() as u32) << 16
- } else {
- instr.uimm() as u32
- };
- ctx.cpu.write_gpr(instr.ra(), ctx.cpu.read_gpr(instr.rs()) ^ imm);
- }
- crate::cpu::lut::OP_ANDI_DOT | crate::cpu::lut::OP_ANDIS_DOT => {
- let mask = if OP == crate::cpu::lut::OP_ANDIS_DOT {
- (instr.uimm() as u32) << 16
- } else {
- instr.uimm() as u32
- };
- let val = ctx.cpu.read_gpr(instr.rs()) & mask;
- ctx.cpu.write_gpr(instr.ra(), val);
- ctx.cpu.update_cr0(val);
- }
- crate::cpu::lut::OP_SUBFX => {
- let res = ctx.cpu.read_gpr(instr.rb()).wrapping_sub(ctx.cpu.read_gpr(instr.ra()));
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_NEGX => {
- let res = (!ctx.cpu.read_gpr(instr.ra())).wrapping_add(1);
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_ADDCX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let rb = ctx.cpu.read_gpr(instr.rb());
- let (res, carry) = ra.overflowing_add(rb);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(carry);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_SUBFCX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let rb = ctx.cpu.read_gpr(instr.rb());
- let res = rb.wrapping_sub(ra);
- ctx.cpu.set_xer_ca(rb >= ra);
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_ADDEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let rb = ctx.cpu.read_gpr(instr.rb());
- let ca = ctx.cpu.xer_ca();
- let (t1, c1) = ra.overflowing_add(rb);
- let (res, c2) = t1.overflowing_add(ca);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(c1 || c2);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_SUBFEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let rb = ctx.cpu.read_gpr(instr.rb());
- let ca = ctx.cpu.xer_ca();
- let (t1, c1) = (!ra).overflowing_add(rb);
- let (res, c2) = t1.overflowing_add(ca);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(c1 || c2);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_ADDZEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let ca = ctx.cpu.xer_ca();
- let (res, carry) = ra.overflowing_add(ca);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(carry);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_SUBFZEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let ca = ctx.cpu.xer_ca();
- let (res, carry) = (!ra).overflowing_add(ca);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(carry);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_ADDMEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let ca = ctx.cpu.xer_ca();
- let (t1, c1) = ra.overflowing_add(ca);
- let (res, c2) = t1.overflowing_add(0xFFFF_FFFF);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(c1 || c2);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_SUBFMEX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let ca = ctx.cpu.xer_ca();
- let (t1, c1) = (!ra).overflowing_add(ca);
- let (res, c2) = t1.overflowing_add(0xFFFF_FFFF);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(c1 || c2);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_MULLWX => {
- let res = (ctx.cpu.read_gpr(instr.ra()) as i32 as i64)
- .wrapping_mul(ctx.cpu.read_gpr(instr.rb()) as i32 as i64) as u32;
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_DIVWUX => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let rb = ctx.cpu.read_gpr(instr.rb());
- let res = if rb == 0 { 0 } else { ra / rb };
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_DIVWX => {
- let ra = ctx.cpu.read_gpr(instr.ra()) as i32;
- let rb = ctx.cpu.read_gpr(instr.rb()) as i32;
- let res = if rb == 0 || (ra == i32::MIN && rb == -1) {
- 0u32
- } else {
- (ra / rb) as u32
- };
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_MULHWUX => {
- let res = ((ctx.cpu.read_gpr(instr.ra()) as u64 * ctx.cpu.read_gpr(instr.rb()) as u64) >> 32) as u32;
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_MULHWX => {
- let res = ((ctx.cpu.read_gpr(instr.ra()) as i32 as i64 * ctx.cpu.read_gpr(instr.rb()) as i32 as i64) >> 32)
- as u32;
- ctx.cpu.write_gpr(instr.rd(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_SUBFIC => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let simm = instr.simm() as u32;
- ctx.cpu.write_gpr(instr.rd(), simm.wrapping_sub(ra));
- ctx.cpu.set_xer_ca(simm >= ra);
- }
- crate::cpu::lut::OP_ADDIC | crate::cpu::lut::OP_ADDIC_DOT => {
- let ra = ctx.cpu.read_gpr(instr.ra());
- let (res, carry) = ra.overflowing_add(instr.simm() as u32);
- ctx.cpu.write_gpr(instr.rd(), res);
- ctx.cpu.set_xer_ca(carry);
- if OP == crate::cpu::lut::OP_ADDIC_DOT {
- ctx.cpu.update_cr0(res);
- }
- }
- crate::cpu::lut::OP_MULLI => {
- let res = (ctx.cpu.read_gpr(instr.ra()) as i32 as i64).wrapping_mul(instr.simm() as i64) as u32;
- ctx.cpu.write_gpr(instr.rd(), res);
- }
- _ => todo!("ALU instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn logical<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let rs = ctx.cpu.read_gpr(instr.rs());
- let rb = ctx.cpu.read_gpr(instr.rb());
-
- let res = match OP {
- crate::cpu::lut::OP_ANDX => rs & rb,
- crate::cpu::lut::OP_ORX => rs | rb,
- crate::cpu::lut::OP_XORX => rs ^ rb,
- crate::cpu::lut::OP_NANDX => !(rs & rb),
- crate::cpu::lut::OP_NORX => !(rs | rb),
- crate::cpu::lut::OP_ANDCX => rs & !rb,
- crate::cpu::lut::OP_ORCX => rs | !rb,
- crate::cpu::lut::OP_EQVX => !(rs ^ rb),
- crate::cpu::lut::OP_SLWX => {
- let sh = rb & 0x3F;
- if sh >= 32 { 0 } else { rs << sh }
- }
- crate::cpu::lut::OP_SRWX => {
- let sh = rb & 0x3F;
- if sh >= 32 { 0 } else { rs >> sh }
- }
- crate::cpu::lut::OP_SRAWX => {
- let sh = rb & 0x3F;
- let signed = rs as i32;
- if sh >= 32 {
- ctx.cpu.set_xer_ca(signed < 0);
- (signed >> 31) as u32
- } else if sh == 0 {
- ctx.cpu.set_xer_ca(false);
- rs
- } else {
- let mask = (1u32 << sh) - 1;
- ctx.cpu.set_xer_ca(signed < 0 && (rs & mask) != 0);
- (signed >> sh) as u32
- }
- }
- crate::cpu::lut::OP_SRAWIX => {
- let sh = instr.sh() as u32;
- let signed = rs as i32;
- if sh == 0 {
- ctx.cpu.set_xer_ca(false);
- rs
- } else {
- let mask = (1u32 << sh) - 1;
- ctx.cpu.set_xer_ca(signed < 0 && (rs & mask) != 0);
- (signed >> sh) as u32
- }
- }
- crate::cpu::lut::OP_CNTLZWX => rs.leading_zeros(),
- crate::cpu::lut::OP_EXTSHX => rs as i16 as i32 as u32,
- crate::cpu::lut::OP_EXTSBX => rs as i8 as i32 as u32,
- _ => todo!("Logical instruction with OP = {OP:#x}"),
- };
-
- ctx.cpu.write_gpr(instr.ra(), res);
- if instr.rc() {
- ctx.cpu.update_cr0(res);
- }
-}
diff --git a/crates/gecko/src/cpu/interpreter/branch.rs b/crates/gecko/src/cpu/interpreter/branch.rs
deleted file mode 100644
index 5f6c70d..0000000
--- a/crates/gecko/src/cpu/interpreter/branch.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-use crate::cpu::condition::BranchControl;
-
-#[inline(always)]
-pub fn branch<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- // Read LR before potentially overwriting LR with CIA+4 (matters for blrl/bctrl)
- let old_lr = ctx.cpu.spr.lr;
-
- if instr.lk() {
- ctx.cpu.spr.lr = ctx.cpu.cia.wrapping_add(4);
- }
-
- match OP {
- crate::cpu::lut::OP_BX => {
- ctx.cpu.nia = if instr.aa() {
- instr.li() as u32
- } else {
- ctx.cpu.cia.wrapping_add_signed(instr.li())
- }
- }
- crate::cpu::lut::OP_BCLRX | crate::cpu::lut::OP_BCX | crate::cpu::lut::OP_BCCTRX => {
- let ctrl = BranchControl::from_bo(instr.bo());
- tracing::trace!("Branch control: {ctrl:?}");
-
- if ctrl.should_decrement_ctr() {
- ctx.cpu.spr.ctr = ctx.cpu.spr.ctr.wrapping_sub(1);
- }
-
- let condition = ctx.cpu.cr.get_bit(instr.bi());
- if !ctrl.should_branch(ctx.cpu.spr.ctr, condition) {
- return;
- }
-
- match OP {
- crate::cpu::lut::OP_BCLRX => ctx.cpu.nia = old_lr,
- crate::cpu::lut::OP_BCX => {
- ctx.cpu.nia = if instr.aa() {
- instr.bd() as u32
- } else {
- ctx.cpu.cia.wrapping_add_signed(instr.bd())
- }
- }
- crate::cpu::lut::OP_BCCTRX => ctx.cpu.nia = ctx.cpu.spr.ctr,
- _ => tracing::error!("missing OP = {OP:#x}"),
- }
- }
- _ => todo!("branch instruction with OP = {OP:#x}"),
- };
-}
diff --git a/crates/gecko/src/cpu/interpreter/compare.rs b/crates/gecko/src/cpu/interpreter/compare.rs
deleted file mode 100644
index 9305373..0000000
--- a/crates/gecko/src/cpu/interpreter/compare.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-use crate::cpu::condition::ConditionRegister;
-
-#[inline(always)]
-pub fn compare<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let field = match OP {
- crate::cpu::lut::OP_CMP => ConditionRegister::field_from_ord(
- (ctx.cpu.read_gpr(instr.ra()) as i32).cmp(&(ctx.cpu.read_gpr(instr.rb()) as i32)),
- ),
- crate::cpu::lut::OP_CMPI => {
- ConditionRegister::field_from_ord((ctx.cpu.read_gpr(instr.ra()) as i32).cmp(&instr.simm()))
- }
- crate::cpu::lut::OP_CMPL => {
- ConditionRegister::field_from_ord(ctx.cpu.read_gpr(instr.ra()).cmp(&ctx.cpu.read_gpr(instr.rb())))
- }
- crate::cpu::lut::OP_CMPLI => {
- ConditionRegister::field_from_ord(ctx.cpu.read_gpr(instr.ra()).cmp(&(instr.uimm() as u32)))
- }
- _ => todo!("Compare instruction with OP = {OP:#x}"),
- };
-
- ctx.cpu.cr.set_field(instr.crfd(), field);
-}
diff --git a/crates/gecko/src/cpu/interpreter/cr_ops.rs b/crates/gecko/src/cpu/interpreter/cr_ops.rs
deleted file mode 100644
index ee421da..0000000
--- a/crates/gecko/src/cpu/interpreter/cr_ops.rs
+++ /dev/null
@@ -1,56 +0,0 @@
-use crate::cpu::condition::ConditionField;
-
-#[inline(always)]
-pub fn mcrxr(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let xer = ctx.cpu.spr.xer;
- let field = ConditionField::new()
- .with_lt(xer.summary_overflow())
- .with_gt(xer.overflow())
- .with_eq(xer.carry())
- .with_so(false);
- ctx.cpu.cr.set_field(instr.crfd(), field);
- ctx.cpu.spr.xer = xer.with_summary_overflow(false).with_overflow(false).with_carry(false);
-}
-
-#[inline(always)]
-pub fn cr_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_MTCRF => {
- let crm = instr.crm();
- let rs = ctx.cpu.read_gpr(instr.rs());
- let mut cr = ctx.cpu.cr.raw();
- for i in 0u8..8 {
- if crm & (1 << (7 - i)) != 0 {
- let shift = (7 - i) * 4;
- let mask = 0xFu32 << shift;
- cr = (cr & !mask) | (rs & mask);
- }
- }
- ctx.cpu.cr = crate::cpu::condition::ConditionRegister::from(cr);
- }
- crate::cpu::lut::OP_MFCR => {
- ctx.cpu.write_gpr(instr.rd(), ctx.cpu.cr.raw());
- }
- crate::cpu::lut::OP_MCRF => {
- let src = ctx.cpu.cr.get_field(instr.crfs());
- ctx.cpu.cr.set_field(instr.crfd(), src);
- }
- // CR bit operations
- _ => {
- let a = ctx.cpu.cr.get_bit(instr.crba());
- let b = ctx.cpu.cr.get_bit(instr.crbb());
- let result = match OP {
- crate::cpu::lut::OP_CRXOR => a ^ b,
- crate::cpu::lut::OP_CROR => a | b,
- crate::cpu::lut::OP_CRAND => a & b,
- crate::cpu::lut::OP_CREQV => a == b,
- crate::cpu::lut::OP_CRNOR => !(a | b),
- crate::cpu::lut::OP_CRNAND => !(a & b),
- crate::cpu::lut::OP_CRANDC => a & !b,
- crate::cpu::lut::OP_CRORC => a | !b,
- _ => todo!("CR instruction with OP = {OP:#x}"),
- };
- ctx.cpu.cr.set_bit(instr.crbd(), result);
- }
- }
-}
diff --git a/crates/gecko/src/cpu/interpreter/fp_ops.rs b/crates/gecko/src/cpu/interpreter/fp_ops.rs
deleted file mode 100644
index f2df6f6..0000000
--- a/crates/gecko/src/cpu/interpreter/fp_ops.rs
+++ /dev/null
@@ -1,219 +0,0 @@
-use crate::cpu::condition::ConditionField;
-use crate::cpu::fpscr::Fpscr;
-
-#[inline(always)]
-pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- if !ctx.check_fp_available() {
- return;
- }
-
- match OP {
- crate::cpu::lut::OP_MTFSFX => {
- let fm = instr.fm();
- let fb = ctx.cpu.read_fpr(instr.rb()).to_bits() as u32;
- let mut mask = 0u32;
- for i in 0u8..8 {
- if fm & (1 << (7 - i)) != 0 {
- mask |= 0xF << ((7 - i) * 4);
- }
- }
- ctx.cpu.fpscr = Fpscr::from((ctx.cpu.fpscr.raw() & !mask) | (fb & mask));
- ctx.cpu.recompute_fpscr_summary();
- }
- crate::cpu::lut::OP_MFFSX => {
- ctx.cpu
- .write_fpr(instr.rd(), f64::from_bits(ctx.cpu.fpscr.raw() as u64));
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_MTFSB0X => {
- ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() & !(1 << (31 - instr.crbd())));
- ctx.cpu.recompute_fpscr_summary();
- }
- crate::cpu::lut::OP_MTFSB1X => {
- ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() | (1 << (31 - instr.crbd())));
- ctx.cpu.recompute_fpscr_summary();
- }
- crate::cpu::lut::OP_MTFSFIX => {
- let crfd = instr.crfd();
- let imm = (instr.0 >> 12) & 0xF;
- let shift = (7 - crfd) * 4;
- let mask = 0xFu32 << shift;
- ctx.cpu.fpscr = Fpscr::from((ctx.cpu.fpscr.raw() & !mask) | (imm << shift));
- ctx.cpu.recompute_fpscr_summary();
- }
- crate::cpu::lut::OP_MCRFS => {
- let src_field = instr.crfs();
- let shift = (7 - src_field) * 4;
- let fpscr_nibble = (ctx.cpu.fpscr.raw() >> shift) & 0xF;
- ctx.cpu
- .cr
- .set_field(instr.crfd(), ConditionField::from(fpscr_nibble as u8));
- if src_field != 0 && src_field != 1 {
- ctx.cpu.fpscr = Fpscr::from(ctx.cpu.fpscr.raw() & !(0xF << shift));
- ctx.cpu.recompute_fpscr_summary();
- }
- }
- crate::cpu::lut::OP_FMRX => {
- ctx.cpu.write_fpr(instr.rd(), ctx.cpu.read_fpr(instr.rb()));
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FNEGX => {
- ctx.cpu.write_fpr(instr.rd(), -ctx.cpu.read_fpr(instr.rb()));
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FABSX => {
- ctx.cpu.write_fpr(instr.rd(), ctx.cpu.read_fpr(instr.rb()).abs());
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FNABSX => {
- ctx.cpu.write_fpr(instr.rd(), -ctx.cpu.read_fpr(instr.rb()).abs());
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FRSPX => {
- let val = ctx.cpu.read_fpr(instr.rb()) as f32 as f64;
- fp_write_single(ctx, &instr, val);
- }
- crate::cpu::lut::OP_FCTIWX => {
- let res = ctx.cpu.read_fpr(instr.rb()).round() as i32;
- ctx.cpu.write_fpr(instr.rd(), f64::from_bits(res as u32 as u64));
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FCTIWZX => {
- let res = ctx.cpu.read_fpr(instr.rb()) as i32;
- ctx.cpu.write_fpr(instr.rd(), f64::from_bits(res as u32 as u64));
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
- }
- crate::cpu::lut::OP_FCMPU | crate::cpu::lut::OP_FCMPO => {
- let fa = ctx.cpu.read_fpr(instr.ra());
- let fb = ctx.cpu.read_fpr(instr.rb());
- let cf = if fa.is_nan() || fb.is_nan() {
- ConditionField::new().with_so(true)
- } else if fa < fb {
- ConditionField::new().with_lt(true)
- } else if fa > fb {
- ConditionField::new().with_gt(true)
- } else {
- ConditionField::new().with_eq(true)
- };
- ctx.cpu.cr.set_field(instr.crfd(), cf);
- }
- crate::cpu::lut::OP_FADDX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_fpr(instr.rb())),
- crate::cpu::lut::OP_FSUBX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()) - ctx.cpu.read_fpr(instr.rb())),
- crate::cpu::lut::OP_FMULX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc())),
- crate::cpu::lut::OP_FDIVX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()) / ctx.cpu.read_fpr(instr.rb())),
- crate::cpu::lut::OP_FMADDX => fp_write(
- ctx,
- &instr,
- ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()),
- ),
- crate::cpu::lut::OP_FMSUBX => fp_write(
- ctx,
- &instr,
- ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()),
- ),
- crate::cpu::lut::OP_FNMADDX => fp_write(
- ctx,
- &instr,
- -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb())),
- ),
- crate::cpu::lut::OP_FNMSUBX => fp_write(
- ctx,
- &instr,
- -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb())),
- ),
- crate::cpu::lut::OP_FADDSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_fpr(instr.rb())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FSUBSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) - ctx.cpu.read_fpr(instr.rb())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FMULSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FDIVSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) / ctx.cpu.read_fpr(instr.rb())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FMADDSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FMSUBSX => fp_write_single(
- ctx,
- &instr,
- (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb())) as f32 as f64,
- ),
- crate::cpu::lut::OP_FNMADDSX => fp_write_single(
- ctx,
- &instr,
- (-(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()))) as f32
- as f64,
- ),
- crate::cpu::lut::OP_FNMSUBSX => fp_write_single(
- ctx,
- &instr,
- (-(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))) as f32
- as f64,
- ),
- crate::cpu::lut::OP_FSQRTX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.rb()).sqrt()),
- crate::cpu::lut::OP_FSQRTSX => {
- fp_write_single(ctx, &instr, (ctx.cpu.read_fpr(instr.rb()).sqrt()) as f32 as f64)
- }
- crate::cpu::lut::OP_FRESX => {
- fp_write_single(ctx, &instr, (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64)
- }
- crate::cpu::lut::OP_FRSQRTEX => fp_write(ctx, &instr, 1.0 / ctx.cpu.read_fpr(instr.rb()).sqrt()),
- crate::cpu::lut::OP_FSELX => {
- let fa = ctx.cpu.read_fpr(instr.ra());
- let fb = ctx.cpu.read_fpr(instr.rb());
- let fc = ctx.cpu.read_fpr(instr.fc());
- fp_write(ctx, &instr, if fa >= 0.0 { fc } else { fb });
- }
-
- _ => todo!("FP instruction with OP = {OP:#x}"),
- }
-
- ctx.check_fp_program_exception();
-}
-
-/// Write FP result to fD and optionally update CR1
-#[inline(always)]
-fn fp_write(ctx: &mut crate::gamecube::GameCube, instr: &crate::cpu::instruction::Instruction, val: f64) {
- ctx.cpu.write_fpr(instr.rd(), val);
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
-}
-
-/// Write single-precision FP result to fD.
-/// On Gekko, single-precision instructions duplicate the result into both ps0 and ps1.
-#[inline(always)]
-fn fp_write_single(ctx: &mut crate::gamecube::GameCube, instr: &crate::cpu::instruction::Instruction, val: f64) {
- ctx.cpu.write_fpr(instr.rd(), val);
- ctx.cpu.write_ps1(instr.rd(), val);
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
-}
diff --git a/crates/gecko/src/cpu/interpreter/ps_ops.rs b/crates/gecko/src/cpu/interpreter/ps_ops.rs
deleted file mode 100644
index 7776fa8..0000000
--- a/crates/gecko/src/cpu/interpreter/ps_ops.rs
+++ /dev/null
@@ -1,190 +0,0 @@
-use crate::cpu::condition::ConditionField;
-
-#[inline(always)]
-pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- if !ctx.check_fp_available() {
- return;
- }
-
- match OP {
- crate::cpu::lut::OP_PS_ADD => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_SUB => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) - ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) - ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MUL => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_DIV => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) / ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) / ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MADD => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()))
- as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) + ctx.cpu.read_ps1(instr.rb()))
- as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MSUB => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))
- as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) - ctx.cpu.read_ps1(instr.rb()))
- as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_NMADD => {
- let ps0 = -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) + ctx.cpu.read_fpr(instr.rb()))
- as f32 as f64;
- let ps1 = -(ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) + ctx.cpu.read_ps1(instr.rb()))
- as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_NMSUB => {
- let ps0 = -(ctx.cpu.read_fpr(instr.ra()) * ctx.cpu.read_fpr(instr.fc()) - ctx.cpu.read_fpr(instr.rb()))
- as f32 as f64;
- let ps1 = -(ctx.cpu.read_ps1(instr.ra()) * ctx.cpu.read_ps1(instr.fc()) - ctx.cpu.read_ps1(instr.rb()))
- as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MULS0 => {
- let c0 = ctx.cpu.read_fpr(instr.fc());
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c0) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c0) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MULS1 => {
- let c1 = ctx.cpu.read_ps1(instr.fc());
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c1) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c1) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MADDS0 => {
- let c0 = ctx.cpu.read_fpr(instr.fc());
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c0 + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c0 + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MADDS1 => {
- let c1 = ctx.cpu.read_ps1(instr.fc());
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) * c1 + ctx.cpu.read_fpr(instr.rb())) as f32 as f64;
- let ps1 = (ctx.cpu.read_ps1(instr.ra()) * c1 + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_SUM0 => {
- let ps0 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- let ps1 = ctx.cpu.read_ps1(instr.fc());
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_SUM1 => {
- let ps0 = ctx.cpu.read_fpr(instr.fc());
- let ps1 = (ctx.cpu.read_fpr(instr.ra()) + ctx.cpu.read_ps1(instr.rb())) as f32 as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_MR => {
- ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.rb()), ctx.cpu.read_ps1(instr.rb()));
- }
- crate::cpu::lut::OP_PS_NEG => {
- ps_write(
- ctx,
- &instr,
- -ctx.cpu.read_fpr(instr.rb()),
- -ctx.cpu.read_ps1(instr.rb()),
- );
- }
- crate::cpu::lut::OP_PS_ABS => {
- ps_write(
- ctx,
- &instr,
- ctx.cpu.read_fpr(instr.rb()).abs(),
- ctx.cpu.read_ps1(instr.rb()).abs(),
- );
- }
- crate::cpu::lut::OP_PS_NABS => {
- ps_write(
- ctx,
- &instr,
- -ctx.cpu.read_fpr(instr.rb()).abs(),
- -ctx.cpu.read_ps1(instr.rb()).abs(),
- );
- }
- crate::cpu::lut::OP_PS_MERGE00 => {
- ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()), ctx.cpu.read_fpr(instr.rb()));
- }
- crate::cpu::lut::OP_PS_MERGE01 => {
- ps_write(ctx, &instr, ctx.cpu.read_fpr(instr.ra()), ctx.cpu.read_ps1(instr.rb()));
- }
- crate::cpu::lut::OP_PS_MERGE10 => {
- ps_write(ctx, &instr, ctx.cpu.read_ps1(instr.ra()), ctx.cpu.read_fpr(instr.rb()));
- }
- crate::cpu::lut::OP_PS_MERGE11 => {
- ps_write(ctx, &instr, ctx.cpu.read_ps1(instr.ra()), ctx.cpu.read_ps1(instr.rb()));
- }
- crate::cpu::lut::OP_PS_SEL => {
- let ps0 = if ctx.cpu.read_fpr(instr.ra()) >= 0.0 {
- ctx.cpu.read_fpr(instr.fc())
- } else {
- ctx.cpu.read_fpr(instr.rb())
- };
- let ps1 = if ctx.cpu.read_ps1(instr.ra()) >= 0.0 {
- ctx.cpu.read_ps1(instr.fc())
- } else {
- ctx.cpu.read_ps1(instr.rb())
- };
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_CMPU0 | crate::cpu::lut::OP_PS_CMPO0 => {
- let fa = ctx.cpu.read_fpr(instr.ra());
- let fb = ctx.cpu.read_fpr(instr.rb());
- ctx.cpu.cr.set_field(instr.crfd(), fp_compare(fa, fb));
- }
- crate::cpu::lut::OP_PS_CMPU1 | crate::cpu::lut::OP_PS_CMPO1 => {
- let fa = ctx.cpu.read_ps1(instr.ra());
- let fb = ctx.cpu.read_ps1(instr.rb());
- ctx.cpu.cr.set_field(instr.crfd(), fp_compare(fa, fb));
- }
- crate::cpu::lut::OP_PS_RES => {
- let ps0 = (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64;
- let ps1 = (1.0f32 / ctx.cpu.read_ps1(instr.rb()) as f32) as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- crate::cpu::lut::OP_PS_RSQRTE => {
- let ps0 = (1.0f32 / (ctx.cpu.read_fpr(instr.rb()) as f32).sqrt()) as f64;
- let ps1 = (1.0f32 / (ctx.cpu.read_ps1(instr.rb()) as f32).sqrt()) as f64;
- ps_write(ctx, &instr, ps0, ps1);
- }
- _ => todo!("PS instruction with OP = {OP:#x}"),
- }
-
- ctx.check_fp_program_exception();
-}
-
-#[inline(always)]
-fn ps_write(ctx: &mut crate::gamecube::GameCube, instr: &crate::cpu::instruction::Instruction, ps0: f64, ps1: f64) {
- ctx.cpu.write_fpr(instr.rd(), ps0);
- ctx.cpu.write_ps1(instr.rd(), ps1);
- if instr.rc() {
- ctx.cpu.update_cr1();
- }
-}
-
-#[inline(always)]
-fn fp_compare(a: f64, b: f64) -> ConditionField {
- if a.is_nan() || b.is_nan() {
- ConditionField::new().with_so(true)
- } else if a < b {
- ConditionField::new().with_lt(true)
- } else if a > b {
- ConditionField::new().with_gt(true)
- } else {
- ConditionField::new().with_eq(true)
- }
-}
diff --git a/crates/gecko/src/cpu/interpreter/store_load.rs b/crates/gecko/src/cpu/interpreter/store_load.rs
deleted file mode 100644
index a9efcd1..0000000
--- a/crates/gecko/src/cpu/interpreter/store_load.rs
+++ /dev/null
@@ -1,426 +0,0 @@
-use crate::cpu::condition::ConditionField;
-
-#[inline(always)]
-pub fn store_load<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_STW | crate::cpu::lut::OP_STWU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STWU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STH | crate::cpu::lut::OP_STHU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- ctx.write_u16(addr, ctx.cpu.read_gpr(instr.rs()) as u16);
- if OP == crate::cpu::lut::OP_STHU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STB | crate::cpu::lut::OP_STBU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- ctx.write_u8(addr, ctx.cpu.read_gpr(instr.rs()) as u8);
- if OP == crate::cpu::lut::OP_STBU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LWZ | crate::cpu::lut::OP_LWZU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_u32(addr);
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LWZU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LBZ | crate::cpu::lut::OP_LBZU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_u8(addr) as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LBZU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LHZ | crate::cpu::lut::OP_LHZU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_u16(addr) as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LHZU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LHA | crate::cpu::lut::OP_LHAU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_u16(addr) as i16 as i32 as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LHAU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LMW => {
- let mut addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- for r in instr.rd()..32 {
- let val = ctx.read_u32(addr);
- ctx.cpu.write_gpr(r, val);
- addr = addr.wrapping_add(4);
- }
- }
- crate::cpu::lut::OP_STMW => {
- let mut addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- for r in instr.rs()..32 {
- let val = ctx.cpu.read_gpr(r);
- ctx.write_u32(addr, val);
- addr = addr.wrapping_add(4);
- }
- }
- crate::cpu::lut::OP_LWZX | crate::cpu::lut::OP_LWZUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u32(addr);
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LWZUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LBZX | crate::cpu::lut::OP_LBZUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u8(addr) as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LBZUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LHZX | crate::cpu::lut::OP_LHZUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u16(addr) as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LHZUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LHAX | crate::cpu::lut::OP_LHAUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u16(addr) as i16 as i32 as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LHAUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STWX | crate::cpu::lut::OP_STWUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STWUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STBX | crate::cpu::lut::OP_STBUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u8(addr, ctx.cpu.read_gpr(instr.rs()) as u8);
- if OP == crate::cpu::lut::OP_STBUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STHX | crate::cpu::lut::OP_STHUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u16(addr, ctx.cpu.read_gpr(instr.rs()) as u16);
- if OP == crate::cpu::lut::OP_STHUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LWBRX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u32(addr).swap_bytes();
- ctx.cpu.write_gpr(instr.rd(), val);
- }
- crate::cpu::lut::OP_LHBRX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u16(addr).swap_bytes() as u32;
- ctx.cpu.write_gpr(instr.rd(), val);
- }
- crate::cpu::lut::OP_STWBRX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()).swap_bytes());
- }
- crate::cpu::lut::OP_STHBRX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u16(addr, (ctx.cpu.read_gpr(instr.rs()) as u16).swap_bytes());
- }
- _ => todo!("Store/Load instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn lwarx(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u32(addr);
- ctx.cpu.write_gpr(instr.rd(), val);
- ctx.cpu.reserve_addr = Some(addr);
-}
-
-#[inline(always)]
-pub fn stwcx_dot(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let so = ctx.cpu.spr.xer.summary_overflow();
- let store_performed = ctx.cpu.reserve_addr.is_some();
- ctx.cpu.reserve_addr = None;
- if store_performed {
- ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
- ctx.cpu.cr.set_cr0(ConditionField::new().with_eq(true).with_so(so));
- } else {
- ctx.cpu.cr.set_cr0(ConditionField::new().with_so(so));
- }
-}
-
-#[inline(always)]
-pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- if !ctx.check_fp_available() {
- return;
- }
-
- match OP {
- crate::cpu::lut::OP_LFD | crate::cpu::lut::OP_LFDU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_f64(addr);
- ctx.cpu.write_fpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LFDU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STFD | crate::cpu::lut::OP_STFDU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- ctx.write_f64(addr, ctx.cpu.read_fpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STFDU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LFS | crate::cpu::lut::OP_LFSU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- let val = ctx.read_f32(addr);
- ctx.cpu.write_fpr(instr.rd(), val);
- ctx.cpu.write_ps1(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LFSU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STFS | crate::cpu::lut::OP_STFSU => {
- let addr = ctx.cpu.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
- ctx.write_f32(addr, ctx.cpu.read_fpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STFSU {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LFSX | crate::cpu::lut::OP_LFSUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_f32(addr);
- ctx.cpu.write_fpr(instr.rd(), val);
- ctx.cpu.write_ps1(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LFSUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_LFDX | crate::cpu::lut::OP_LFDUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_f64(addr);
- ctx.cpu.write_fpr(instr.rd(), val);
- if OP == crate::cpu::lut::OP_LFDUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STFSX | crate::cpu::lut::OP_STFSUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_f32(addr, ctx.cpu.read_fpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STFSUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STFDX | crate::cpu::lut::OP_STFDUX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_f64(addr, ctx.cpu.read_fpr(instr.rs()));
- if OP == crate::cpu::lut::OP_STFDUX {
- ctx.cpu.write_gpr(instr.ra(), addr);
- }
- }
- crate::cpu::lut::OP_STFIWX => {
- let addr = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- ctx.write_u32(addr, ctx.cpu.read_fpr(instr.rs()).to_bits() as u32);
- }
- _ => todo!("FP Store/Load instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn lswx(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let mut n = ctx.cpu.spr.xer.byte_count() as u32;
- if n == 0 {
- return;
- }
- let mut r = (instr.rd() as u32).wrapping_sub(1) & 31;
- let mut i = 0u32;
- let mut addr = ea;
- while n > 0 {
- if i == 0 {
- r = (r + 1) & 31;
- ctx.cpu.write_gpr(r as u8, 0);
- }
- let byte = ctx.read_u8(addr) as u32;
- let shift = 24 - i;
- let val = ctx.cpu.read_gpr(r as u8) | (byte << shift);
- ctx.cpu.write_gpr(r as u8, val);
- i += 8;
- if i == 32 {
- i = 0;
- }
- addr = addr.wrapping_add(1);
- n -= 1;
- }
-}
-
-#[inline(always)]
-pub fn stswx(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let mut n = ctx.cpu.spr.xer.byte_count() as u32;
- let mut r = (instr.rs() as u32).wrapping_sub(1) & 31;
- let mut i = 0u32;
- let mut addr = ea;
- while n > 0 {
- if i == 0 {
- r = (r + 1) & 31;
- }
- let byte = (ctx.cpu.read_gpr(r as u8) >> (24 - i)) as u8;
- ctx.write_u8(addr, byte);
- i += 8;
- if i == 32 {
- i = 0;
- }
- addr = addr.wrapping_add(1);
- n -= 1;
- }
-}
-
-#[inline(always)]
-pub fn lswi(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx.cpu.read_gpr_or_zero(instr.ra());
- let nb = instr.nb();
- let mut n = if nb == 0 { 32u32 } else { nb as u32 };
- let mut r = (instr.rd() as u32).wrapping_sub(1) & 31;
- let mut i = 0u32;
- let mut addr = ea;
- while n > 0 {
- if i == 0 {
- r = (r + 1) & 31;
- ctx.cpu.write_gpr(r as u8, 0);
- }
- let byte = ctx.read_u8(addr) as u32;
- let shift = 24 - i;
- let val = ctx.cpu.read_gpr(r as u8) | (byte << shift);
- ctx.cpu.write_gpr(r as u8, val);
- i += 8;
- if i == 32 {
- i = 0;
- }
- addr = addr.wrapping_add(1);
- n -= 1;
- }
-}
-
-#[inline(always)]
-pub fn stswi(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx.cpu.read_gpr_or_zero(instr.ra());
- let nb = instr.nb();
- let mut n = if nb == 0 { 32u32 } else { nb as u32 };
- let mut r = (instr.rs() as u32).wrapping_sub(1) & 31;
- let mut i = 0u32;
- let mut addr = ea;
- while n > 0 {
- if i == 0 {
- r = (r + 1) & 31;
- }
- let byte = (ctx.cpu.read_gpr(r as u8) >> (24 - i)) as u8;
- ctx.write_u8(addr, byte);
- i += 8;
- if i == 32 {
- i = 0;
- }
- addr = addr.wrapping_add(1);
- n -= 1;
- }
-}
-
-#[inline(always)]
-pub fn eciwx(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.read_u32(ea);
- ctx.cpu.write_gpr(instr.rd(), val);
-}
-
-#[inline(always)]
-pub fn ecowx(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let ea = ctx
- .cpu
- .read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let val = ctx.cpu.read_gpr(instr.rs());
- ctx.write_u32(ea, val);
-}
diff --git a/crates/gecko/src/cpu/interpreter/system.rs b/crates/gecko/src/cpu/interpreter/system.rs
deleted file mode 100644
index b1962e0..0000000
--- a/crates/gecko/src/cpu/interpreter/system.rs
+++ /dev/null
@@ -1,143 +0,0 @@
-use crate::cpu::sr::Sr;
-
-#[inline(always)]
-pub fn msr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_MTMSR => {
- ctx.cpu.msr = crate::cpu::msr::Msr::from(ctx.cpu.read_gpr(instr.rs()));
- }
- crate::cpu::lut::OP_MFMSR => {
- ctx.cpu.write_gpr(instr.rd(), ctx.cpu.msr.raw());
- }
- crate::cpu::lut::OP_RFI => {
- const RFI_MSR_MASK: u32 = 0x0000_FF73;
- ctx.cpu.msr =
- crate::cpu::msr::Msr::from((ctx.cpu.msr.raw() & !RFI_MSR_MASK) | (ctx.cpu.spr.srr1 & RFI_MSR_MASK));
- ctx.cpu.nia = ctx.cpu.spr.srr0.value() << 2;
- }
- _ => todo!("MSR instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_MTSPR => {
- let spr_num = instr.spr_swapped();
- let val = ctx.cpu.read_gpr(instr.rs());
- match spr_num {
- 22 => {
- ctx.scheduler.cancel(crate::cpu::dec::underflow_handler);
- ctx.cpu.dec.write(ctx.scheduler.cycles, val);
- ctx.cpu.spr.dec = val;
- ctx.scheduler.schedule_in(
- crate::cpu::dec::cycles_until_underflow(val),
- crate::cpu::dec::underflow_handler,
- );
- tracing::debug!(cycles = ctx.scheduler.cycles, value = val, "decrementer set");
- }
- 284 => ctx.scheduler.set_timebase_lower(val),
- 285 => ctx.scheduler.set_timebase_upper(val),
- 923 => {
- ctx.cpu.spr.dmal = crate::cpu::spr::DmaLower::from_raw(val);
- if ctx.cpu.spr.dmal.trigger() {
- ctx.mmio.process_locked_cache_dma(&ctx.cpu.spr.dmau, &ctx.cpu.spr.dmal);
- }
- }
- _ => ctx.cpu.spr.write(spr_num, val),
- }
- }
- crate::cpu::lut::OP_MFSPR => {
- let spr_num = instr.spr_swapped();
- let val = match spr_num {
- 22 => {
- ctx.cpu.spr.dec = ctx.cpu.dec.read(ctx.scheduler.cycles);
- ctx.cpu.spr.dec
- }
- 268 => ctx.scheduler.timebase_lower(),
- 269 => ctx.scheduler.timebase_upper(),
- _ => ctx.cpu.spr.read(spr_num),
- };
- ctx.cpu.write_gpr(instr.rd(), val);
- }
- _ => todo!("SPR instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn segment<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- match OP {
- crate::cpu::lut::OP_MTSR => {
- ctx.cpu.sr[instr.sr() as usize] = Sr::from_raw(ctx.cpu.read_gpr(instr.rs()));
- }
- crate::cpu::lut::OP_MFSR => {
- ctx.cpu.write_gpr(instr.rd(), ctx.cpu.sr[instr.sr() as usize].raw());
- }
- _ => todo!("Segment Register instruction with OP = {OP:#x}"),
- }
-}
-
-#[inline(always)]
-pub fn mtsrin(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let sr_idx = (ctx.cpu.read_gpr(instr.rb()) >> 28) as usize;
- ctx.cpu.sr[sr_idx] = Sr::from_raw(ctx.cpu.read_gpr(instr.rs()));
-}
-
-#[inline(always)]
-pub fn mfsrin(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let sr_idx = (ctx.cpu.read_gpr(instr.rb()) >> 28) as usize;
- ctx.cpu.write_gpr(instr.rd(), ctx.cpu.sr[sr_idx].raw());
-}
-
-#[inline(always)]
-pub fn mftb(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let tbr = instr.spr_swapped();
- let val = match tbr {
- 268 => ctx.scheduler.timebase_lower(),
- 269 => ctx.scheduler.timebase_upper(),
- _ => panic!("unknown TBR {tbr}"),
- };
- 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 tw(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let a = ctx.cpu.read_gpr(instr.ra()) as i32;
- let b = ctx.cpu.read_gpr(instr.rb()) as i32;
- let to = instr.to();
-
- let trap = (to & 0x10 != 0 && a < b)
- || (to & 0x08 != 0 && a > b)
- || (to & 0x04 != 0 && a == b)
- || (to & 0x02 != 0 && (a as u32) < (b as u32))
- || (to & 0x01 != 0 && (a as u32) > (b 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) {}
-
-#[inline(always)]
-pub fn sc(ctx: &mut crate::gamecube::GameCube, _instr: crate::cpu::instruction::Instruction) {
- ctx.cause_syscall_interrupt();
-}
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index 4513e8f..4377135 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -1,4 +1,4 @@
-use crate::cpu::IPL_RESET_VECTOR;
+use crate::gekko::IPL_RESET_VECTOR;
use crate::flipper::exi::macronix::ExiMacronix;
use crate::ipl::IPL_HLE;
use crate::scheduler::Scheduler;
@@ -34,20 +34,20 @@ impl GameCube {
let mut emulator = Self::new(IPL_ENTRY);
// BATs
- emulator.cpu.spr.dbat0u = 0x8000_1FFF;
- emulator.cpu.spr.dbat0l = 0x0000_0002;
- emulator.cpu.spr.dbat1u = 0xC000_1FFF;
- emulator.cpu.spr.dbat1l = 0x0000_002A;
- emulator.cpu.spr.dbat2u = 0x0000_1FFF;
- emulator.cpu.spr.dbat2l = 0x0000_0002;
- emulator.cpu.spr.dbat3u = 0xFFF0_001F;
- emulator.cpu.spr.dbat3l = 0xFFF0_0001;
- emulator.cpu.spr.ibat0u = 0x8000_1FFF;
- emulator.cpu.spr.ibat0l = 0x0000_0002;
- emulator.cpu.spr.ibat2u = 0x0000_1FFF;
- emulator.cpu.spr.ibat2l = 0x0000_0002;
- emulator.cpu.spr.ibat3u = 0xFFF0_001F;
- emulator.cpu.spr.ibat3l = 0xFFF0_0001;
+ emulator.gekko.spr.dbat0u = 0x8000_1FFF;
+ emulator.gekko.spr.dbat0l = 0x0000_0002;
+ emulator.gekko.spr.dbat1u = 0xC000_1FFF;
+ emulator.gekko.spr.dbat1l = 0x0000_002A;
+ emulator.gekko.spr.dbat2u = 0x0000_1FFF;
+ emulator.gekko.spr.dbat2l = 0x0000_0002;
+ emulator.gekko.spr.dbat3u = 0xFFF0_001F;
+ emulator.gekko.spr.dbat3l = 0xFFF0_0001;
+ emulator.gekko.spr.ibat0u = 0x8000_1FFF;
+ emulator.gekko.spr.ibat0l = 0x0000_0002;
+ emulator.gekko.spr.ibat2u = 0x0000_1FFF;
+ emulator.gekko.spr.ibat2l = 0x0000_0002;
+ emulator.gekko.spr.ibat3u = 0xFFF0_001F;
+ emulator.gekko.spr.ibat3l = 0xFFF0_0001;
// DVD header fields to low memory
emulator.mmio.ram[0x00..0x04].copy_from_slice(&dvd.header().game_code);
@@ -68,12 +68,12 @@ impl GameCube {
emulator.mmio.phys_write_u32(0x0C00, 0x4C00_0064);
// MSR: FP + address translation
- emulator.cpu.msr.set_floating_point_available(true);
- emulator.cpu.msr.set_data_address_translation(true);
- emulator.cpu.msr.set_instruction_address_translation(true);
+ emulator.gekko.msr.set_floating_point_available(true);
+ emulator.gekko.msr.set_data_address_translation(true);
+ emulator.gekko.msr.set_instruction_address_translation(true);
// ???
- emulator.cpu.spr.hid0 = 0x0011_C464;
+ emulator.gekko.spr.hid0 = 0x0011_C464;
// Load apploader code into RAM
let apploader_code_start = image::dvd::DVD_APPLOADER_OFFSET + 0x20;
@@ -88,7 +88,7 @@ impl GameCube {
emulator.mmio.ram[IPL_LOAD as usize..][..IPL_HLE.len()].copy_from_slice(IPL_HLE);
// R0 = apploader entry (read by IPL)
- emulator.cpu.gprs[0] = apploader_entry;
+ emulator.gekko.gprs[0] = apploader_entry;
// Insert the DVD
emulator.insert_dvd(dvd);
@@ -114,7 +114,7 @@ impl GameCube {
}
let mut emulator = GameCube::new(IPL_RESET_VECTOR);
- emulator.cpu.msr.set_ip(true);
+ emulator.gekko.msr.set_ip(true);
emulator.mmio.ipl = ipl.clone();
emulator.exi.attach_device(
ExiMacronix::CHANNEL,
diff --git a/crates/gecko/src/cpu.rs b/crates/gecko/src/gekko.rs
similarity index 98%
rename from crates/gecko/src/cpu.rs
rename to crates/gecko/src/gekko.rs
index d98c171..fbc26b4 100644
--- a/crates/gecko/src/cpu.rs
+++ b/crates/gecko/src/gekko.rs
@@ -9,7 +9,7 @@ pub mod msr;
pub mod spr;
pub mod sr;
-use crate::cpu::condition::ConditionRegister;
+use crate::gekko::condition::ConditionRegister;
#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]
pub mod lut {
@@ -18,7 +18,7 @@ pub mod lut {
pub const IPL_RESET_VECTOR: u32 = 0xFFF0_0100;
-pub struct Cpu {
+pub struct Gekko {
pub gprs: [u32; 32],
pub fprs: [f64; 32], // PS0
pub ps1s: [f64; 32], // PS1 (paired single slot 1)
@@ -37,12 +37,12 @@ pub struct Cpu {
pub reserve_addr: Option<u32>, // lwarx/stwcx. reservation address
}
-impl Cpu {
+impl Gekko {
pub fn new(initial_pc: u32) -> Self {
let mut spr = spr::Spr::default();
spr.dec = u32::MAX;
- Cpu {
+ Gekko {
gprs: [0; 32],
fprs: [0.0; 32],
ps1s: [1.0; 32],
diff --git a/crates/gecko/src/cpu/condition.rs b/crates/gecko/src/gekko/condition.rs
similarity index 100%
rename from crates/gecko/src/cpu/condition.rs
rename to crates/gecko/src/gekko/condition.rs
diff --git a/crates/gecko/src/cpu/dec.rs b/crates/gecko/src/gekko/dec.rs
similarity index 90%
rename from crates/gecko/src/cpu/dec.rs
rename to crates/gecko/src/gekko/dec.rs
index 0a868af..c35585f 100644
--- a/crates/gecko/src/cpu/dec.rs
+++ b/crates/gecko/src/gekko/dec.rs
@@ -55,15 +55,15 @@ pub fn cycles_until_underflow(value: u32) -> u64 {
}
pub fn underflow_handler<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
- gc.cpu.dec.underflow(gc.scheduler.cycles);
- gc.cpu.spr.dec = u32::MAX;
+ gc.gekko.dec.underflow(gc.scheduler.cycles);
+ gc.gekko.spr.dec = u32::MAX;
gc.scheduler
.schedule_in(cycles_until_underflow(u32::MAX), self::underflow_handler);
tracing::debug!(
cycles = gc.scheduler.cycles,
- ee = gc.cpu.msr.external_interrupt_enable(),
+ ee = gc.gekko.msr.external_interrupt_enable(),
pi_pending = gc.pi.interrupt_pending(),
- pc = format!("{:08X}", gc.cpu.pc),
+ pc = format!("{:08X}", gc.gekko.pc),
"decrementer underflow"
);
}
diff --git a/crates/gecko/src/cpu/fpscr.rs b/crates/gecko/src/gekko/fpscr.rs
similarity index 100%
rename from crates/gecko/src/cpu/fpscr.rs
rename to crates/gecko/src/gekko/fpscr.rs
diff --git a/crates/gecko/src/cpu/instruction.rs b/crates/gecko/src/gekko/instruction.rs
similarity index 100%
rename from crates/gecko/src/cpu/instruction.rs
rename to crates/gecko/src/gekko/instruction.rs
diff --git a/crates/gecko/src/cpu/interpreter.rs b/crates/gecko/src/gekko/interpreter.rs
similarity index 94%
rename from crates/gecko/src/cpu/interpreter.rs
rename to crates/gecko/src/gekko/interpreter.rs
index 389a957..c2c8a2a 100644
--- a/crates/gecko/src/cpu/interpreter.rs
+++ b/crates/gecko/src/gekko/interpreter.rs
@@ -22,6 +22,6 @@ pub use system::{mfsrin, mftb, msr, mtsrin, nop, sc, segment, spr, tw, twi};
#[cold]
#[inline(never)]
-pub fn invalid(_ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
+pub fn invalid(_ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
panic!("unimplemented Gekko opcode {:#010x}", instr.0);
}
diff --git a/crates/gecko/src/gekko/interpreter/alu.rs b/crates/gecko/src/gekko/interpreter/alu.rs
new file mode 100644
index 0000000..3117697
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/alu.rs
@@ -0,0 +1,273 @@
+#[inline(always)]
+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()));
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_ADDI | crate::gekko::lut::OP_ADDIS => {
+ let ra = ctx.gekko.read_gpr_or_zero(instr.ra());
+ let simm = if OP == crate::gekko::lut::OP_ADDIS {
+ instr.simm() << 16
+ } else {
+ instr.simm()
+ };
+ ctx.gekko.write_gpr(instr.rd(), ra.wrapping_add_signed(simm));
+ }
+ crate::gekko::lut::OP_ORI | crate::gekko::lut::OP_ORIS => {
+ let imm = if OP == crate::gekko::lut::OP_ORIS {
+ (instr.uimm() as u32) << 16
+ } else {
+ instr.uimm() as u32
+ };
+ ctx.gekko.write_gpr(instr.ra(), ctx.gekko.read_gpr(instr.rs()) | imm);
+ }
+ crate::gekko::lut::OP_XORI | crate::gekko::lut::OP_XORIS => {
+ let imm = if OP == crate::gekko::lut::OP_XORIS {
+ (instr.uimm() as u32) << 16
+ } else {
+ instr.uimm() as u32
+ };
+ ctx.gekko.write_gpr(instr.ra(), ctx.gekko.read_gpr(instr.rs()) ^ imm);
+ }
+ crate::gekko::lut::OP_ANDI_DOT | crate::gekko::lut::OP_ANDIS_DOT => {
+ let mask = if OP == crate::gekko::lut::OP_ANDIS_DOT {
+ (instr.uimm() as u32) << 16
+ } else {
+ instr.uimm() as u32
+ };
+ let val = ctx.gekko.read_gpr(instr.rs()) & mask;
+ ctx.gekko.write_gpr(instr.ra(), val);
+ ctx.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()));
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_NEGX => {
+ let res = (!ctx.gekko.read_gpr(instr.ra())).wrapping_add(1);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_ADDCX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+ let (res, carry) = ra.overflowing_add(rb);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(carry);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_SUBFCX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+ let res = rb.wrapping_sub(ra);
+ ctx.gekko.set_xer_ca(rb >= ra);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_ADDEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+ let ca = ctx.gekko.xer_ca();
+ let (t1, c1) = ra.overflowing_add(rb);
+ let (res, c2) = t1.overflowing_add(ca);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(c1 || c2);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_SUBFEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+ let ca = ctx.gekko.xer_ca();
+ let (t1, c1) = (!ra).overflowing_add(rb);
+ let (res, c2) = t1.overflowing_add(ca);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(c1 || c2);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_ADDZEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let ca = ctx.gekko.xer_ca();
+ let (res, carry) = ra.overflowing_add(ca);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(carry);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_SUBFZEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let ca = ctx.gekko.xer_ca();
+ let (res, carry) = (!ra).overflowing_add(ca);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(carry);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_ADDMEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let ca = ctx.gekko.xer_ca();
+ let (t1, c1) = ra.overflowing_add(ca);
+ let (res, c2) = t1.overflowing_add(0xFFFF_FFFF);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(c1 || c2);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_SUBFMEX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let ca = ctx.gekko.xer_ca();
+ let (t1, c1) = (!ra).overflowing_add(ca);
+ let (res, c2) = t1.overflowing_add(0xFFFF_FFFF);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(c1 || c2);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_MULLWX => {
+ let res = (ctx.gekko.read_gpr(instr.ra()) as i32 as i64)
+ .wrapping_mul(ctx.gekko.read_gpr(instr.rb()) as i32 as i64) as u32;
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_DIVWUX => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+ let res = if rb == 0 { 0 } else { ra / rb };
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_DIVWX => {
+ let ra = ctx.gekko.read_gpr(instr.ra()) as i32;
+ let rb = ctx.gekko.read_gpr(instr.rb()) as i32;
+ let res = if rb == 0 || (ra == i32::MIN && rb == -1) {
+ 0u32
+ } else {
+ (ra / rb) as u32
+ };
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_MULHWUX => {
+ let res = ((ctx.gekko.read_gpr(instr.ra()) as u64 * ctx.gekko.read_gpr(instr.rb()) as u64) >> 32) as u32;
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ 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;
+ ctx.gekko.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_SUBFIC => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let simm = instr.simm() as u32;
+ ctx.gekko.write_gpr(instr.rd(), simm.wrapping_sub(ra));
+ ctx.gekko.set_xer_ca(simm >= ra);
+ }
+ crate::gekko::lut::OP_ADDIC | crate::gekko::lut::OP_ADDIC_DOT => {
+ let ra = ctx.gekko.read_gpr(instr.ra());
+ let (res, carry) = ra.overflowing_add(instr.simm() as u32);
+ ctx.gekko.write_gpr(instr.rd(), res);
+ ctx.gekko.set_xer_ca(carry);
+ if OP == crate::gekko::lut::OP_ADDIC_DOT {
+ ctx.gekko.update_cr0(res);
+ }
+ }
+ crate::gekko::lut::OP_MULLI => {
+ let res = (ctx.gekko.read_gpr(instr.ra()) as i32 as i64).wrapping_mul(instr.simm() as i64) as u32;
+ ctx.gekko.write_gpr(instr.rd(), res);
+ }
+ _ => todo!("ALU instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn logical<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let rs = ctx.gekko.read_gpr(instr.rs());
+ let rb = ctx.gekko.read_gpr(instr.rb());
+
+ let res = match OP {
+ crate::gekko::lut::OP_ANDX => rs & rb,
+ crate::gekko::lut::OP_ORX => rs | rb,
+ crate::gekko::lut::OP_XORX => rs ^ rb,
+ crate::gekko::lut::OP_NANDX => !(rs & rb),
+ crate::gekko::lut::OP_NORX => !(rs | rb),
+ crate::gekko::lut::OP_ANDCX => rs & !rb,
+ crate::gekko::lut::OP_ORCX => rs | !rb,
+ crate::gekko::lut::OP_EQVX => !(rs ^ rb),
+ crate::gekko::lut::OP_SLWX => {
+ let sh = rb & 0x3F;
+ if sh >= 32 { 0 } else { rs << sh }
+ }
+ crate::gekko::lut::OP_SRWX => {
+ let sh = rb & 0x3F;
+ if sh >= 32 { 0 } else { rs >> sh }
+ }
+ crate::gekko::lut::OP_SRAWX => {
+ let sh = rb & 0x3F;
+ let signed = rs as i32;
+ if sh >= 32 {
+ ctx.gekko.set_xer_ca(signed < 0);
+ (signed >> 31) as u32
+ } else if sh == 0 {
+ ctx.gekko.set_xer_ca(false);
+ rs
+ } else {
+ let mask = (1u32 << sh) - 1;
+ ctx.gekko.set_xer_ca(signed < 0 && (rs & mask) != 0);
+ (signed >> sh) as u32
+ }
+ }
+ crate::gekko::lut::OP_SRAWIX => {
+ let sh = instr.sh() as u32;
+ let signed = rs as i32;
+ if sh == 0 {
+ ctx.gekko.set_xer_ca(false);
+ rs
+ } else {
+ let mask = (1u32 << sh) - 1;
+ ctx.gekko.set_xer_ca(signed < 0 && (rs & mask) != 0);
+ (signed >> sh) as u32
+ }
+ }
+ crate::gekko::lut::OP_CNTLZWX => rs.leading_zeros(),
+ crate::gekko::lut::OP_EXTSHX => rs as i16 as i32 as u32,
+ crate::gekko::lut::OP_EXTSBX => rs as i8 as i32 as u32,
+ _ => todo!("Logical instruction with OP = {OP:#x}"),
+ };
+
+ ctx.gekko.write_gpr(instr.ra(), res);
+ if instr.rc() {
+ ctx.gekko.update_cr0(res);
+ }
+}
diff --git a/crates/gecko/src/gekko/interpreter/branch.rs b/crates/gecko/src/gekko/interpreter/branch.rs
new file mode 100644
index 0000000..afc44d9
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/branch.rs
@@ -0,0 +1,48 @@
+use crate::gekko::condition::BranchControl;
+
+#[inline(always)]
+pub fn branch<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ // Read LR before potentially overwriting LR with CIA+4 (matters for blrl/bctrl)
+ let old_lr = ctx.gekko.spr.lr;
+
+ if instr.lk() {
+ ctx.gekko.spr.lr = ctx.gekko.cia.wrapping_add(4);
+ }
+
+ match OP {
+ crate::gekko::lut::OP_BX => {
+ ctx.gekko.nia = if instr.aa() {
+ instr.li() as u32
+ } else {
+ ctx.gekko.cia.wrapping_add_signed(instr.li())
+ }
+ }
+ crate::gekko::lut::OP_BCLRX | crate::gekko::lut::OP_BCX | crate::gekko::lut::OP_BCCTRX => {
+ let ctrl = BranchControl::from_bo(instr.bo());
+ tracing::trace!("Branch control: {ctrl:?}");
+
+ if ctrl.should_decrement_ctr() {
+ ctx.gekko.spr.ctr = ctx.gekko.spr.ctr.wrapping_sub(1);
+ }
+
+ let condition = ctx.gekko.cr.get_bit(instr.bi());
+ if !ctrl.should_branch(ctx.gekko.spr.ctr, condition) {
+ return;
+ }
+
+ match OP {
+ crate::gekko::lut::OP_BCLRX => ctx.gekko.nia = old_lr,
+ crate::gekko::lut::OP_BCX => {
+ ctx.gekko.nia = if instr.aa() {
+ instr.bd() as u32
+ } else {
+ ctx.gekko.cia.wrapping_add_signed(instr.bd())
+ }
+ }
+ crate::gekko::lut::OP_BCCTRX => ctx.gekko.nia = ctx.gekko.spr.ctr,
+ _ => tracing::error!("missing OP = {OP:#x}"),
+ }
+ }
+ _ => todo!("branch instruction with OP = {OP:#x}"),
+ };
+}
diff --git a/crates/gecko/src/gekko/interpreter/compare.rs b/crates/gecko/src/gekko/interpreter/compare.rs
new file mode 100644
index 0000000..5ba4787
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/compare.rs
@@ -0,0 +1,22 @@
+use crate::gekko::condition::ConditionRegister;
+
+#[inline(always)]
+pub fn compare<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let field = match OP {
+ crate::gekko::lut::OP_CMP => ConditionRegister::field_from_ord(
+ (ctx.gekko.read_gpr(instr.ra()) as i32).cmp(&(ctx.gekko.read_gpr(instr.rb()) as i32)),
+ ),
+ crate::gekko::lut::OP_CMPI => {
+ ConditionRegister::field_from_ord((ctx.gekko.read_gpr(instr.ra()) as i32).cmp(&instr.simm()))
+ }
+ crate::gekko::lut::OP_CMPL => {
+ ConditionRegister::field_from_ord(ctx.gekko.read_gpr(instr.ra()).cmp(&ctx.gekko.read_gpr(instr.rb())))
+ }
+ crate::gekko::lut::OP_CMPLI => {
+ ConditionRegister::field_from_ord(ctx.gekko.read_gpr(instr.ra()).cmp(&(instr.uimm() as u32)))
+ }
+ _ => todo!("Compare instruction with OP = {OP:#x}"),
+ };
+
+ ctx.gekko.cr.set_field(instr.crfd(), field);
+}
diff --git a/crates/gecko/src/gekko/interpreter/cr_ops.rs b/crates/gecko/src/gekko/interpreter/cr_ops.rs
new file mode 100644
index 0000000..902cbea
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/cr_ops.rs
@@ -0,0 +1,56 @@
+use crate::gekko::condition::ConditionField;
+
+#[inline(always)]
+pub fn mcrxr(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let xer = ctx.gekko.spr.xer;
+ let field = ConditionField::new()
+ .with_lt(xer.summary_overflow())
+ .with_gt(xer.overflow())
+ .with_eq(xer.carry())
+ .with_so(false);
+ ctx.gekko.cr.set_field(instr.crfd(), field);
+ ctx.gekko.spr.xer = xer.with_summary_overflow(false).with_overflow(false).with_carry(false);
+}
+
+#[inline(always)]
+pub fn cr_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ match OP {
+ crate::gekko::lut::OP_MTCRF => {
+ let crm = instr.crm();
+ let rs = ctx.gekko.read_gpr(instr.rs());
+ let mut cr = ctx.gekko.cr.raw();
+ for i in 0u8..8 {
+ if crm & (1 << (7 - i)) != 0 {
+ let shift = (7 - i) * 4;
+ let mask = 0xFu32 << shift;
+ cr = (cr & !mask) | (rs & mask);
+ }
+ }
+ ctx.gekko.cr = crate::gekko::condition::ConditionRegister::from(cr);
+ }
+ crate::gekko::lut::OP_MFCR => {
+ ctx.gekko.write_gpr(instr.rd(), ctx.gekko.cr.raw());
+ }
+ crate::gekko::lut::OP_MCRF => {
+ let src = ctx.gekko.cr.get_field(instr.crfs());
+ ctx.gekko.cr.set_field(instr.crfd(), src);
+ }
+ // CR bit operations
+ _ => {
+ let a = ctx.gekko.cr.get_bit(instr.crba());
+ let b = ctx.gekko.cr.get_bit(instr.crbb());
+ let result = match OP {
+ crate::gekko::lut::OP_CRXOR => a ^ b,
+ crate::gekko::lut::OP_CROR => a | b,
+ crate::gekko::lut::OP_CRAND => a & b,
+ crate::gekko::lut::OP_CREQV => a == b,
+ crate::gekko::lut::OP_CRNOR => !(a | b),
+ crate::gekko::lut::OP_CRNAND => !(a & b),
+ crate::gekko::lut::OP_CRANDC => a & !b,
+ crate::gekko::lut::OP_CRORC => a | !b,
+ _ => todo!("CR instruction with OP = {OP:#x}"),
+ };
+ ctx.gekko.cr.set_bit(instr.crbd(), result);
+ }
+ }
+}
diff --git a/crates/gecko/src/gekko/interpreter/fp_ops.rs b/crates/gecko/src/gekko/interpreter/fp_ops.rs
new file mode 100644
index 0000000..95e8e3b
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/fp_ops.rs
@@ -0,0 +1,219 @@
+use crate::gekko::condition::ConditionField;
+use crate::gekko::fpscr::Fpscr;
+
+#[inline(always)]
+pub fn fp_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ if !ctx.check_fp_available() {
+ return;
+ }
+
+ match OP {
+ crate::gekko::lut::OP_MTFSFX => {
+ let fm = instr.fm();
+ let fb = ctx.gekko.read_fpr(instr.rb()).to_bits() as u32;
+ let mut mask = 0u32;
+ for i in 0u8..8 {
+ if fm & (1 << (7 - i)) != 0 {
+ mask |= 0xF << ((7 - i) * 4);
+ }
+ }
+ ctx.gekko.fpscr = Fpscr::from((ctx.gekko.fpscr.raw() & !mask) | (fb & mask));
+ ctx.gekko.recompute_fpscr_summary();
+ }
+ crate::gekko::lut::OP_MFFSX => {
+ ctx.gekko
+ .write_fpr(instr.rd(), f64::from_bits(ctx.gekko.fpscr.raw() as u64));
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_MTFSB0X => {
+ ctx.gekko.fpscr = Fpscr::from(ctx.gekko.fpscr.raw() & !(1 << (31 - instr.crbd())));
+ ctx.gekko.recompute_fpscr_summary();
+ }
+ crate::gekko::lut::OP_MTFSB1X => {
+ ctx.gekko.fpscr = Fpscr::from(ctx.gekko.fpscr.raw() | (1 << (31 - instr.crbd())));
+ ctx.gekko.recompute_fpscr_summary();
+ }
+ crate::gekko::lut::OP_MTFSFIX => {
+ let crfd = instr.crfd();
+ let imm = (instr.0 >> 12) & 0xF;
+ let shift = (7 - crfd) * 4;
+ let mask = 0xFu32 << shift;
+ ctx.gekko.fpscr = Fpscr::from((ctx.gekko.fpscr.raw() & !mask) | (imm << shift));
+ ctx.gekko.recompute_fpscr_summary();
+ }
+ crate::gekko::lut::OP_MCRFS => {
+ let src_field = instr.crfs();
+ let shift = (7 - src_field) * 4;
+ let fpscr_nibble = (ctx.gekko.fpscr.raw() >> shift) & 0xF;
+ ctx.gekko
+ .cr
+ .set_field(instr.crfd(), ConditionField::from(fpscr_nibble as u8));
+ if src_field != 0 && src_field != 1 {
+ ctx.gekko.fpscr = Fpscr::from(ctx.gekko.fpscr.raw() & !(0xF << shift));
+ ctx.gekko.recompute_fpscr_summary();
+ }
+ }
+ crate::gekko::lut::OP_FMRX => {
+ ctx.gekko.write_fpr(instr.rd(), ctx.gekko.read_fpr(instr.rb()));
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FNEGX => {
+ ctx.gekko.write_fpr(instr.rd(), -ctx.gekko.read_fpr(instr.rb()));
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FABSX => {
+ ctx.gekko.write_fpr(instr.rd(), ctx.gekko.read_fpr(instr.rb()).abs());
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FNABSX => {
+ ctx.gekko.write_fpr(instr.rd(), -ctx.gekko.read_fpr(instr.rb()).abs());
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FRSPX => {
+ let val = ctx.gekko.read_fpr(instr.rb()) as f32 as f64;
+ fp_write_single(ctx, &instr, val);
+ }
+ crate::gekko::lut::OP_FCTIWX => {
+ let res = ctx.gekko.read_fpr(instr.rb()).round() as i32;
+ ctx.gekko.write_fpr(instr.rd(), f64::from_bits(res as u32 as u64));
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FCTIWZX => {
+ let res = ctx.gekko.read_fpr(instr.rb()) as i32;
+ ctx.gekko.write_fpr(instr.rd(), f64::from_bits(res as u32 as u64));
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+ }
+ crate::gekko::lut::OP_FCMPU | crate::gekko::lut::OP_FCMPO => {
+ let fa = ctx.gekko.read_fpr(instr.ra());
+ let fb = ctx.gekko.read_fpr(instr.rb());
+ let cf = if fa.is_nan() || fb.is_nan() {
+ ConditionField::new().with_so(true)
+ } else if fa < fb {
+ ConditionField::new().with_lt(true)
+ } else if fa > fb {
+ ConditionField::new().with_gt(true)
+ } else {
+ ConditionField::new().with_eq(true)
+ };
+ 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_FMADDX => fp_write(
+ ctx,
+ &instr,
+ ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) + ctx.gekko.read_fpr(instr.rb()),
+ ),
+ crate::gekko::lut::OP_FMSUBX => fp_write(
+ ctx,
+ &instr,
+ ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) - ctx.gekko.read_fpr(instr.rb()),
+ ),
+ crate::gekko::lut::OP_FNMADDX => fp_write(
+ ctx,
+ &instr,
+ -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) + ctx.gekko.read_fpr(instr.rb())),
+ ),
+ crate::gekko::lut::OP_FNMSUBX => fp_write(
+ ctx,
+ &instr,
+ -(ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc()) - ctx.gekko.read_fpr(instr.rb())),
+ ),
+ crate::gekko::lut::OP_FADDSX => fp_write_single(
+ ctx,
+ &instr,
+ (ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_fpr(instr.rb())) as f32 as f64,
+ ),
+ crate::gekko::lut::OP_FSUBSX => fp_write_single(
+ ctx,
+ &instr,
+ (ctx.gekko.read_fpr(instr.ra()) - ctx.gekko.read_fpr(instr.rb())) as f32 as f64,
+ ),
+ crate::gekko::lut::OP_FMULSX => fp_write_single(
+ ctx,
+ &instr,
+ (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc())) as f32 as f64,
+ ),
+ crate::gekko::lut::OP_FDIVSX => fp_write_single(
+ ctx,
+ &instr,
+ (ctx.gekko.read_fpr(instr.ra()) / ctx.gekko.read_fpr(instr.rb())) as f32 as f64,
+ ),
+ 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,
+ ),
+ 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,
+ ),
+ crate::gekko::lut::OP_FNMADDSX => 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,
+ ),
+ crate::gekko::lut::OP_FNMSUBSX => 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,
+ ),
+ crate::gekko::lut::OP_FSQRTX => fp_write(ctx, &instr, ctx.gekko.read_fpr(instr.rb()).sqrt()),
+ crate::gekko::lut::OP_FSQRTSX => {
+ fp_write_single(ctx, &instr, (ctx.gekko.read_fpr(instr.rb()).sqrt()) as f32 as f64)
+ }
+ crate::gekko::lut::OP_FRESX => {
+ fp_write_single(ctx, &instr, (1.0f32 / ctx.gekko.read_fpr(instr.rb()) as f32) as f64)
+ }
+ crate::gekko::lut::OP_FRSQRTEX => fp_write(ctx, &instr, 1.0 / ctx.gekko.read_fpr(instr.rb()).sqrt()),
+ crate::gekko::lut::OP_FSELX => {
+ let fa = ctx.gekko.read_fpr(instr.ra());
+ let fb = ctx.gekko.read_fpr(instr.rb());
+ let fc = ctx.gekko.read_fpr(instr.fc());
+ fp_write(ctx, &instr, if fa >= 0.0 { fc } else { fb });
+ }
+
+ _ => todo!("FP instruction with OP = {OP:#x}"),
+ }
+
+ ctx.check_fp_program_exception();
+}
+
+/// Write FP result to fD and optionally update CR1
+#[inline(always)]
+fn fp_write(ctx: &mut crate::gamecube::GameCube, instr: &crate::gekko::instruction::Instruction, val: f64) {
+ ctx.gekko.write_fpr(instr.rd(), val);
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+}
+
+/// Write single-precision FP result to fD.
+/// On Gekko, single-precision instructions duplicate the result into both ps0 and ps1.
+#[inline(always)]
+fn fp_write_single(ctx: &mut crate::gamecube::GameCube, instr: &crate::gekko::instruction::Instruction, val: f64) {
+ ctx.gekko.write_fpr(instr.rd(), val);
+ ctx.gekko.write_ps1(instr.rd(), val);
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+}
diff --git a/crates/gecko/src/gekko/interpreter/ps_ops.rs b/crates/gecko/src/gekko/interpreter/ps_ops.rs
new file mode 100644
index 0000000..1b28772
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/ps_ops.rs
@@ -0,0 +1,190 @@
+use crate::gekko::condition::ConditionField;
+
+#[inline(always)]
+pub fn ps_ops<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ if !ctx.check_fp_available() {
+ return;
+ }
+
+ match OP {
+ crate::gekko::lut::OP_PS_ADD => {
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_SUB => {
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) - ctx.gekko.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) - ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MUL => {
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) * ctx.gekko.read_fpr(instr.fc())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) * ctx.gekko.read_ps1(instr.fc())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_DIV => {
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) / ctx.gekko.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) / ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MADD => {
+ 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_MSUB => {
+ 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_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;
+ 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;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MULS0 => {
+ let c0 = ctx.gekko.read_fpr(instr.fc());
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) * c0) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) * c0) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MULS1 => {
+ let c1 = ctx.gekko.read_ps1(instr.fc());
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) * c1) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) * c1) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MADDS0 => {
+ let c0 = ctx.gekko.read_fpr(instr.fc());
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) * c0 + ctx.gekko.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) * c0 + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_MADDS1 => {
+ let c1 = ctx.gekko.read_ps1(instr.fc());
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) * c1 + ctx.gekko.read_fpr(instr.rb())) as f32 as f64;
+ let ps1 = (ctx.gekko.read_ps1(instr.ra()) * c1 + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_SUM0 => {
+ let ps0 = (ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ let ps1 = ctx.gekko.read_ps1(instr.fc());
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_SUM1 => {
+ let ps0 = ctx.gekko.read_fpr(instr.fc());
+ let ps1 = (ctx.gekko.read_fpr(instr.ra()) + ctx.gekko.read_ps1(instr.rb())) as f32 as f64;
+ 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()));
+ }
+ crate::gekko::lut::OP_PS_NEG => {
+ ps_write(
+ ctx,
+ &instr,
+ -ctx.gekko.read_fpr(instr.rb()),
+ -ctx.gekko.read_ps1(instr.rb()),
+ );
+ }
+ crate::gekko::lut::OP_PS_ABS => {
+ ps_write(
+ ctx,
+ &instr,
+ ctx.gekko.read_fpr(instr.rb()).abs(),
+ ctx.gekko.read_ps1(instr.rb()).abs(),
+ );
+ }
+ crate::gekko::lut::OP_PS_NABS => {
+ ps_write(
+ ctx,
+ &instr,
+ -ctx.gekko.read_fpr(instr.rb()).abs(),
+ -ctx.gekko.read_ps1(instr.rb()).abs(),
+ );
+ }
+ crate::gekko::lut::OP_PS_MERGE00 => {
+ 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()));
+ }
+ crate::gekko::lut::OP_PS_MERGE10 => {
+ 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()));
+ }
+ crate::gekko::lut::OP_PS_SEL => {
+ let ps0 = if ctx.gekko.read_fpr(instr.ra()) >= 0.0 {
+ ctx.gekko.read_fpr(instr.fc())
+ } else {
+ ctx.gekko.read_fpr(instr.rb())
+ };
+ let ps1 = if ctx.gekko.read_ps1(instr.ra()) >= 0.0 {
+ ctx.gekko.read_ps1(instr.fc())
+ } else {
+ ctx.gekko.read_ps1(instr.rb())
+ };
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_CMPU0 | crate::gekko::lut::OP_PS_CMPO0 => {
+ let fa = ctx.gekko.read_fpr(instr.ra());
+ let fb = ctx.gekko.read_fpr(instr.rb());
+ ctx.gekko.cr.set_field(instr.crfd(), fp_compare(fa, fb));
+ }
+ crate::gekko::lut::OP_PS_CMPU1 | crate::gekko::lut::OP_PS_CMPO1 => {
+ let fa = ctx.gekko.read_ps1(instr.ra());
+ let fb = ctx.gekko.read_ps1(instr.rb());
+ ctx.gekko.cr.set_field(instr.crfd(), fp_compare(fa, fb));
+ }
+ crate::gekko::lut::OP_PS_RES => {
+ let ps0 = (1.0f32 / ctx.gekko.read_fpr(instr.rb()) as f32) as f64;
+ let ps1 = (1.0f32 / ctx.gekko.read_ps1(instr.rb()) as f32) as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ crate::gekko::lut::OP_PS_RSQRTE => {
+ let ps0 = (1.0f32 / (ctx.gekko.read_fpr(instr.rb()) as f32).sqrt()) as f64;
+ let ps1 = (1.0f32 / (ctx.gekko.read_ps1(instr.rb()) as f32).sqrt()) as f64;
+ ps_write(ctx, &instr, ps0, ps1);
+ }
+ _ => todo!("PS instruction with OP = {OP:#x}"),
+ }
+
+ ctx.check_fp_program_exception();
+}
+
+#[inline(always)]
+fn ps_write(ctx: &mut crate::gamecube::GameCube, instr: &crate::gekko::instruction::Instruction, ps0: f64, ps1: f64) {
+ ctx.gekko.write_fpr(instr.rd(), ps0);
+ ctx.gekko.write_ps1(instr.rd(), ps1);
+ if instr.rc() {
+ ctx.gekko.update_cr1();
+ }
+}
+
+#[inline(always)]
+fn fp_compare(a: f64, b: f64) -> ConditionField {
+ if a.is_nan() || b.is_nan() {
+ ConditionField::new().with_so(true)
+ } else if a < b {
+ ConditionField::new().with_lt(true)
+ } else if a > b {
+ ConditionField::new().with_gt(true)
+ } else {
+ ConditionField::new().with_eq(true)
+ }
+}
diff --git a/crates/gecko/src/cpu/interpreter/psq.rs b/crates/gecko/src/gekko/interpreter/psq.rs
similarity index 83%
rename from crates/gecko/src/cpu/interpreter/psq.rs
rename to crates/gecko/src/gekko/interpreter/psq.rs
index e5acd2f..7c427a0 100644
--- a/crates/gecko/src/cpu/interpreter/psq.rs
+++ b/crates/gecko/src/gekko/interpreter/psq.rs
@@ -98,25 +98,25 @@ fn psq_load(ctx: &mut crate::gamecube::GameCube, fd: u8, addr: u32, w: bool, gqr
let elem_size = quant_element_size(ld_type);
dequantize(ctx, addr.wrapping_add(elem_size), ld_type, ld_scale)
};
- ctx.cpu.write_fpr(fd, ps0);
- ctx.cpu.write_ps1(fd, ps1);
+ ctx.gekko.write_fpr(fd, ps0);
+ ctx.gekko.write_ps1(fd, ps1);
}
fn psq_store(ctx: &mut crate::gamecube::GameCube, fs: u8, addr: u32, w: bool, gqr: u32) {
let st_type = gqr_st_type(gqr);
let st_scale = gqr_st_scale(gqr);
- let ps0 = ctx.cpu.read_fpr(fs);
+ let ps0 = ctx.gekko.read_fpr(fs);
quantize(ctx, addr, ps0, st_type, st_scale);
if !w {
- let ps1 = ctx.cpu.read_ps1(fs);
+ let ps1 = ctx.gekko.read_ps1(fs);
let elem_size = quant_element_size(st_type);
quantize(ctx, addr.wrapping_add(elem_size), ps1, st_type, st_scale);
}
}
#[inline(always)]
-pub fn store_load_psq<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- use crate::cpu::lut::*;
+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() {
return;
@@ -126,49 +126,49 @@ pub fn store_load_psq<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr:
// D-form loads
OP_PSQ_L | OP_PSQ_LU => {
let ea = ctx
- .cpu
+ .gekko
.read_gpr_or_zero(instr.ra())
.wrapping_add_signed(instr.disp_psq());
- let gqr = ctx.cpu.spr.read_gqr(instr.psq_i());
+ let gqr = ctx.gekko.spr.read_gqr(instr.psq_i());
psq_load(ctx, instr.fd(), ea, instr.psq_w(), gqr);
if OP == OP_PSQ_LU {
- ctx.cpu.write_gpr(instr.ra(), ea);
+ ctx.gekko.write_gpr(instr.ra(), ea);
}
}
// D-form stores
OP_PSQ_ST | OP_PSQ_STU => {
let ea = ctx
- .cpu
+ .gekko
.read_gpr_or_zero(instr.ra())
.wrapping_add_signed(instr.disp_psq());
- let gqr = ctx.cpu.spr.read_gqr(instr.psq_i());
+ let gqr = ctx.gekko.spr.read_gqr(instr.psq_i());
psq_store(ctx, instr.fs(), ea, instr.psq_w(), gqr);
if OP == OP_PSQ_STU {
- ctx.cpu.write_gpr(instr.ra(), ea);
+ ctx.gekko.write_gpr(instr.ra(), ea);
}
}
// X-form loads
OP_PSQ_LX | OP_PSQ_LUX => {
let ea = ctx
- .cpu
+ .gekko
.read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let gqr = ctx.cpu.spr.read_gqr(instr.psq_ix());
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let gqr = ctx.gekko.spr.read_gqr(instr.psq_ix());
psq_load(ctx, instr.fd(), ea, instr.psq_wx(), gqr);
if OP == OP_PSQ_LUX {
- ctx.cpu.write_gpr(instr.ra(), ea);
+ ctx.gekko.write_gpr(instr.ra(), ea);
}
}
// X-form stores
OP_PSQ_STX | OP_PSQ_STUX => {
let ea = ctx
- .cpu
+ .gekko
.read_gpr_or_zero(instr.ra())
- .wrapping_add(ctx.cpu.read_gpr(instr.rb()));
- let gqr = ctx.cpu.spr.read_gqr(instr.psq_ix());
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let gqr = ctx.gekko.spr.read_gqr(instr.psq_ix());
psq_store(ctx, instr.fs(), ea, instr.psq_wx(), gqr);
if OP == OP_PSQ_STUX {
- ctx.cpu.write_gpr(instr.ra(), ea);
+ ctx.gekko.write_gpr(instr.ra(), ea);
}
}
_ => unreachable!(),
diff --git a/crates/gecko/src/cpu/interpreter/rotate.rs b/crates/gecko/src/gekko/interpreter/rotate.rs
similarity index 57%
rename from crates/gecko/src/cpu/interpreter/rotate.rs
rename to crates/gecko/src/gekko/interpreter/rotate.rs
index f962f88..2c1f1d4 100644
--- a/crates/gecko/src/cpu/interpreter/rotate.rs
+++ b/crates/gecko/src/gekko/interpreter/rotate.rs
@@ -6,27 +6,27 @@ fn mask(mb: u32, me: u32) -> u32 {
}
#[inline(always)]
-pub fn rotate<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu::instruction::Instruction) {
- let rs = ctx.cpu.read_gpr(instr.rs());
+pub fn rotate<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let rs = ctx.gekko.read_gpr(instr.rs());
let mb = instr.mb() as u32;
let me = instr.me() as u32;
let res = match OP {
- crate::cpu::lut::OP_RLWINMX => rs.rotate_left(instr.sh() as u32) & mask(mb, me),
- crate::cpu::lut::OP_RLWIMIX => {
+ crate::gekko::lut::OP_RLWINMX => rs.rotate_left(instr.sh() as u32) & mask(mb, me),
+ crate::gekko::lut::OP_RLWIMIX => {
let m = mask(mb, me);
let r = rs.rotate_left(instr.sh() as u32);
- (r & m) | (ctx.cpu.read_gpr(instr.ra()) & !m)
+ (r & m) | (ctx.gekko.read_gpr(instr.ra()) & !m)
}
- crate::cpu::lut::OP_RLWNMX => {
- let sh = ctx.cpu.read_gpr(instr.rb()) & 0x1F;
+ crate::gekko::lut::OP_RLWNMX => {
+ let sh = ctx.gekko.read_gpr(instr.rb()) & 0x1F;
rs.rotate_left(sh) & mask(mb, me)
}
_ => todo!("Rotate instruction with OP = {OP:#x}"),
};
- ctx.cpu.write_gpr(instr.ra(), res);
+ ctx.gekko.write_gpr(instr.ra(), res);
if instr.rc() {
- ctx.cpu.update_cr0(res);
+ ctx.gekko.update_cr0(res);
}
}
diff --git a/crates/gecko/src/gekko/interpreter/store_load.rs b/crates/gecko/src/gekko/interpreter/store_load.rs
new file mode 100644
index 0000000..9d51d42
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/store_load.rs
@@ -0,0 +1,426 @@
+use crate::gekko::condition::ConditionField;
+
+#[inline(always)]
+pub fn store_load<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ match OP {
+ crate::gekko::lut::OP_STW | crate::gekko::lut::OP_STWU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ ctx.write_u32(addr, ctx.gekko.read_gpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STWU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STH | crate::gekko::lut::OP_STHU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ ctx.write_u16(addr, ctx.gekko.read_gpr(instr.rs()) as u16);
+ if OP == crate::gekko::lut::OP_STHU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STB | crate::gekko::lut::OP_STBU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ ctx.write_u8(addr, ctx.gekko.read_gpr(instr.rs()) as u8);
+ if OP == crate::gekko::lut::OP_STBU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LWZ | crate::gekko::lut::OP_LWZU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_u32(addr);
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LWZU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LBZ | crate::gekko::lut::OP_LBZU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_u8(addr) as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LBZU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LHZ | crate::gekko::lut::OP_LHZU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_u16(addr) as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LHZU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LHA | crate::gekko::lut::OP_LHAU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_u16(addr) as i16 as i32 as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LHAU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LMW => {
+ let mut addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ for r in instr.rd()..32 {
+ let val = ctx.read_u32(addr);
+ ctx.gekko.write_gpr(r, val);
+ addr = addr.wrapping_add(4);
+ }
+ }
+ crate::gekko::lut::OP_STMW => {
+ let mut addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ for r in instr.rs()..32 {
+ let val = ctx.gekko.read_gpr(r);
+ ctx.write_u32(addr, val);
+ addr = addr.wrapping_add(4);
+ }
+ }
+ crate::gekko::lut::OP_LWZX | crate::gekko::lut::OP_LWZUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u32(addr);
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LWZUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LBZX | crate::gekko::lut::OP_LBZUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u8(addr) as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LBZUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LHZX | crate::gekko::lut::OP_LHZUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u16(addr) as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LHZUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LHAX | crate::gekko::lut::OP_LHAUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u16(addr) as i16 as i32 as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LHAUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STWX | crate::gekko::lut::OP_STWUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u32(addr, ctx.gekko.read_gpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STWUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STBX | crate::gekko::lut::OP_STBUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u8(addr, ctx.gekko.read_gpr(instr.rs()) as u8);
+ if OP == crate::gekko::lut::OP_STBUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STHX | crate::gekko::lut::OP_STHUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u16(addr, ctx.gekko.read_gpr(instr.rs()) as u16);
+ if OP == crate::gekko::lut::OP_STHUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LWBRX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u32(addr).swap_bytes();
+ ctx.gekko.write_gpr(instr.rd(), val);
+ }
+ crate::gekko::lut::OP_LHBRX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u16(addr).swap_bytes() as u32;
+ ctx.gekko.write_gpr(instr.rd(), val);
+ }
+ crate::gekko::lut::OP_STWBRX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u32(addr, ctx.gekko.read_gpr(instr.rs()).swap_bytes());
+ }
+ crate::gekko::lut::OP_STHBRX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u16(addr, (ctx.gekko.read_gpr(instr.rs()) as u16).swap_bytes());
+ }
+ _ => todo!("Store/Load instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn lwarx(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u32(addr);
+ ctx.gekko.write_gpr(instr.rd(), val);
+ ctx.gekko.reserve_addr = Some(addr);
+}
+
+#[inline(always)]
+pub fn stwcx_dot(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let so = ctx.gekko.spr.xer.summary_overflow();
+ let store_performed = ctx.gekko.reserve_addr.is_some();
+ ctx.gekko.reserve_addr = None;
+ if store_performed {
+ ctx.write_u32(addr, ctx.gekko.read_gpr(instr.rs()));
+ ctx.gekko.cr.set_cr0(ConditionField::new().with_eq(true).with_so(so));
+ } else {
+ ctx.gekko.cr.set_cr0(ConditionField::new().with_so(so));
+ }
+}
+
+#[inline(always)]
+pub fn store_load_fp<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ if !ctx.check_fp_available() {
+ return;
+ }
+
+ match OP {
+ crate::gekko::lut::OP_LFD | crate::gekko::lut::OP_LFDU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_f64(addr);
+ ctx.gekko.write_fpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LFDU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STFD | crate::gekko::lut::OP_STFDU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ ctx.write_f64(addr, ctx.gekko.read_fpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STFDU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LFS | crate::gekko::lut::OP_LFSU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ let val = ctx.read_f32(addr);
+ ctx.gekko.write_fpr(instr.rd(), val);
+ ctx.gekko.write_ps1(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LFSU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STFS | crate::gekko::lut::OP_STFSU => {
+ let addr = ctx.gekko.read_gpr_or_zero(instr.ra()).wrapping_add_signed(instr.disp());
+ ctx.write_f32(addr, ctx.gekko.read_fpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STFSU {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LFSX | crate::gekko::lut::OP_LFSUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_f32(addr);
+ ctx.gekko.write_fpr(instr.rd(), val);
+ ctx.gekko.write_ps1(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LFSUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_LFDX | crate::gekko::lut::OP_LFDUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_f64(addr);
+ ctx.gekko.write_fpr(instr.rd(), val);
+ if OP == crate::gekko::lut::OP_LFDUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STFSX | crate::gekko::lut::OP_STFSUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_f32(addr, ctx.gekko.read_fpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STFSUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STFDX | crate::gekko::lut::OP_STFDUX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_f64(addr, ctx.gekko.read_fpr(instr.rs()));
+ if OP == crate::gekko::lut::OP_STFDUX {
+ ctx.gekko.write_gpr(instr.ra(), addr);
+ }
+ }
+ crate::gekko::lut::OP_STFIWX => {
+ let addr = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ ctx.write_u32(addr, ctx.gekko.read_fpr(instr.rs()).to_bits() as u32);
+ }
+ _ => todo!("FP Store/Load instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn lswx(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let mut n = ctx.gekko.spr.xer.byte_count() as u32;
+ if n == 0 {
+ return;
+ }
+ let mut r = (instr.rd() as u32).wrapping_sub(1) & 31;
+ let mut i = 0u32;
+ let mut addr = ea;
+ while n > 0 {
+ if i == 0 {
+ r = (r + 1) & 31;
+ ctx.gekko.write_gpr(r as u8, 0);
+ }
+ let byte = ctx.read_u8(addr) as u32;
+ let shift = 24 - i;
+ let val = ctx.gekko.read_gpr(r as u8) | (byte << shift);
+ ctx.gekko.write_gpr(r as u8, val);
+ i += 8;
+ if i == 32 {
+ i = 0;
+ }
+ addr = addr.wrapping_add(1);
+ n -= 1;
+ }
+}
+
+#[inline(always)]
+pub fn stswx(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let mut n = ctx.gekko.spr.xer.byte_count() as u32;
+ let mut r = (instr.rs() as u32).wrapping_sub(1) & 31;
+ let mut i = 0u32;
+ let mut addr = ea;
+ while n > 0 {
+ if i == 0 {
+ r = (r + 1) & 31;
+ }
+ let byte = (ctx.gekko.read_gpr(r as u8) >> (24 - i)) as u8;
+ ctx.write_u8(addr, byte);
+ i += 8;
+ if i == 32 {
+ i = 0;
+ }
+ addr = addr.wrapping_add(1);
+ n -= 1;
+ }
+}
+
+#[inline(always)]
+pub fn lswi(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx.gekko.read_gpr_or_zero(instr.ra());
+ let nb = instr.nb();
+ let mut n = if nb == 0 { 32u32 } else { nb as u32 };
+ let mut r = (instr.rd() as u32).wrapping_sub(1) & 31;
+ let mut i = 0u32;
+ let mut addr = ea;
+ while n > 0 {
+ if i == 0 {
+ r = (r + 1) & 31;
+ ctx.gekko.write_gpr(r as u8, 0);
+ }
+ let byte = ctx.read_u8(addr) as u32;
+ let shift = 24 - i;
+ let val = ctx.gekko.read_gpr(r as u8) | (byte << shift);
+ ctx.gekko.write_gpr(r as u8, val);
+ i += 8;
+ if i == 32 {
+ i = 0;
+ }
+ addr = addr.wrapping_add(1);
+ n -= 1;
+ }
+}
+
+#[inline(always)]
+pub fn stswi(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx.gekko.read_gpr_or_zero(instr.ra());
+ let nb = instr.nb();
+ let mut n = if nb == 0 { 32u32 } else { nb as u32 };
+ let mut r = (instr.rs() as u32).wrapping_sub(1) & 31;
+ let mut i = 0u32;
+ let mut addr = ea;
+ while n > 0 {
+ if i == 0 {
+ r = (r + 1) & 31;
+ }
+ let byte = (ctx.gekko.read_gpr(r as u8) >> (24 - i)) as u8;
+ ctx.write_u8(addr, byte);
+ i += 8;
+ if i == 32 {
+ i = 0;
+ }
+ addr = addr.wrapping_add(1);
+ n -= 1;
+ }
+}
+
+#[inline(always)]
+pub fn eciwx(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.read_u32(ea);
+ ctx.gekko.write_gpr(instr.rd(), val);
+}
+
+#[inline(always)]
+pub fn ecowx(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let ea = ctx
+ .gekko
+ .read_gpr_or_zero(instr.ra())
+ .wrapping_add(ctx.gekko.read_gpr(instr.rb()));
+ let val = ctx.gekko.read_gpr(instr.rs());
+ ctx.write_u32(ea, val);
+}
diff --git a/crates/gecko/src/gekko/interpreter/system.rs b/crates/gecko/src/gekko/interpreter/system.rs
new file mode 100644
index 0000000..1ab3514
--- /dev/null
+++ b/crates/gecko/src/gekko/interpreter/system.rs
@@ -0,0 +1,143 @@
+use crate::gekko::sr::Sr;
+
+#[inline(always)]
+pub fn msr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ match OP {
+ crate::gekko::lut::OP_MTMSR => {
+ ctx.gekko.msr = crate::gekko::msr::Msr::from(ctx.gekko.read_gpr(instr.rs()));
+ }
+ crate::gekko::lut::OP_MFMSR => {
+ ctx.gekko.write_gpr(instr.rd(), ctx.gekko.msr.raw());
+ }
+ 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.nia = ctx.gekko.spr.srr0.value() << 2;
+ }
+ _ => todo!("MSR instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ match OP {
+ crate::gekko::lut::OP_MTSPR => {
+ let spr_num = instr.spr_swapped();
+ let val = ctx.gekko.read_gpr(instr.rs());
+ match spr_num {
+ 22 => {
+ ctx.scheduler.cancel(crate::gekko::dec::underflow_handler);
+ ctx.gekko.dec.write(ctx.scheduler.cycles, val);
+ ctx.gekko.spr.dec = val;
+ ctx.scheduler.schedule_in(
+ crate::gekko::dec::cycles_until_underflow(val),
+ crate::gekko::dec::underflow_handler,
+ );
+ tracing::debug!(cycles = ctx.scheduler.cycles, value = val, "decrementer set");
+ }
+ 284 => ctx.scheduler.set_timebase_lower(val),
+ 285 => ctx.scheduler.set_timebase_upper(val),
+ 923 => {
+ 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.gekko.spr.write(spr_num, val),
+ }
+ }
+ crate::gekko::lut::OP_MFSPR => {
+ let spr_num = instr.spr_swapped();
+ let val = match spr_num {
+ 22 => {
+ ctx.gekko.spr.dec = ctx.gekko.dec.read(ctx.scheduler.cycles);
+ ctx.gekko.spr.dec
+ }
+ 268 => ctx.scheduler.timebase_lower(),
+ 269 => ctx.scheduler.timebase_upper(),
+ _ => ctx.gekko.spr.read(spr_num),
+ };
+ ctx.gekko.write_gpr(instr.rd(), val);
+ }
+ _ => todo!("SPR instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn segment<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ match OP {
+ crate::gekko::lut::OP_MTSR => {
+ ctx.gekko.sr[instr.sr() as usize] = Sr::from_raw(ctx.gekko.read_gpr(instr.rs()));
+ }
+ crate::gekko::lut::OP_MFSR => {
+ ctx.gekko.write_gpr(instr.rd(), ctx.gekko.sr[instr.sr() as usize].raw());
+ }
+ _ => todo!("Segment Register instruction with OP = {OP:#x}"),
+ }
+}
+
+#[inline(always)]
+pub fn mtsrin(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let sr_idx = (ctx.gekko.read_gpr(instr.rb()) >> 28) as usize;
+ ctx.gekko.sr[sr_idx] = Sr::from_raw(ctx.gekko.read_gpr(instr.rs()));
+}
+
+#[inline(always)]
+pub fn mfsrin(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let sr_idx = (ctx.gekko.read_gpr(instr.rb()) >> 28) as usize;
+ ctx.gekko.write_gpr(instr.rd(), ctx.gekko.sr[sr_idx].raw());
+}
+
+#[inline(always)]
+pub fn mftb(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let tbr = instr.spr_swapped();
+ let val = match tbr {
+ 268 => ctx.scheduler.timebase_lower(),
+ 269 => ctx.scheduler.timebase_upper(),
+ _ => panic!("unknown TBR {tbr}"),
+ };
+ ctx.gekko.write_gpr(instr.rd(), val);
+}
+
+#[inline(always)]
+pub fn twi(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let a = ctx.gekko.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 tw(ctx: &mut crate::gamecube::GameCube, instr: crate::gekko::instruction::Instruction) {
+ let a = ctx.gekko.read_gpr(instr.ra()) as i32;
+ let b = ctx.gekko.read_gpr(instr.rb()) as i32;
+ let to = instr.to();
+
+ let trap = (to & 0x10 != 0 && a < b)
+ || (to & 0x08 != 0 && a > b)
+ || (to & 0x04 != 0 && a == b)
+ || (to & 0x02 != 0 && (a as u32) < (b as u32))
+ || (to & 0x01 != 0 && (a as u32) > (b as u32));
+
+ if trap {
+ ctx.cause_trap_exception();
+ }
+}
+
+#[inline(always)]
+pub fn nop<const OP: u32>(_ctx: &mut crate::gamecube::GameCube, _instr: crate::gekko::instruction::Instruction) {}
+
+#[inline(always)]
+pub fn sc(ctx: &mut crate::gamecube::GameCube, _instr: crate::gekko::instruction::Instruction) {
+ ctx.cause_syscall_interrupt();
+}
diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/gekko/irq.rs
similarity index 66%
rename from crates/gecko/src/cpu/irq.rs
rename to crates/gecko/src/gekko/irq.rs
index 0e3c19d..f8f7051 100644
--- a/crates/gecko/src/cpu/irq.rs
+++ b/crates/gecko/src/gekko/irq.rs
@@ -1,4 +1,4 @@
-use crate::cpu::spr::Srr0;
+use crate::gekko::spr::Srr0;
use crate::mmio::Mmio;
use crate::system::{System, SystemId};
@@ -20,18 +20,18 @@ use crate::system::{System, SystemId};
impl<const SYSTEM: SystemId> System<SYSTEM> {
pub fn cause_external_interrupt(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
// Table 4-7. System Reset Exception—Register Settings
- self.cpu.spr.srr0 = Srr0::from(self.cpu.pc);
- self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.pc);
+ self.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw();
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -45,26 +45,26 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.pc = base | IRQ_EXTERNAL;
+ self.gekko.pc = base | IRQ_EXTERNAL;
- tracing::debug!(addr = format!("{:08X}", self.cpu.pc), "IRQ triggered");
+ tracing::debug!(addr = format!("{:08X}", self.gekko.pc), "IRQ triggered");
}
pub fn cause_decrementer_interrupt(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
- self.cpu.dec.clear_interrupt();
- self.cpu.spr.srr0 = Srr0::from(self.cpu.pc);
- self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+ self.gekko.dec.clear_interrupt();
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.pc);
+ self.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw();
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -77,26 +77,26 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.pc = base | IRQ_DECREMENTER;
+ self.gekko.pc = base | IRQ_DECREMENTER;
- tracing::debug!(addr = format!("{:08X}", self.cpu.pc), "decrementer IRQ triggered");
+ tracing::debug!(addr = format!("{:08X}", self.gekko.pc), "decrementer IRQ triggered");
}
pub fn cause_trap_exception(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
- self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.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.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw() | (1 << (31 - 14));
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -110,11 +110,11 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.nia = base | IRQ_PROGRAM;
+ self.gekko.nia = base | IRQ_PROGRAM;
- tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "trap exception triggered");
+ tracing::debug!(addr = format!("{:08X}", self.gekko.nia), "trap exception triggered");
}
/// Raise a Floating-Point Unavailable exception (0x00800).
@@ -124,17 +124,17 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
/// instruction; SRR1 is a clean copy of MSR (no status bits set).
#[inline(always)]
pub fn cause_fp_unavailable(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
- self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);
- self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.cia);
+ self.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw();
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -148,12 +148,12 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.nia = base | IRQ_FP_UNAVAILABLE;
+ self.gekko.nia = base | IRQ_FP_UNAVAILABLE;
tracing::debug!(
- addr = format!("{:08X}", self.cpu.nia),
+ addr = format!("{:08X}", self.gekko.nia),
"FP unavailable exception triggered"
);
}
@@ -164,18 +164,18 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
/// an instruction that updates FPSCR.
#[inline(always)]
pub fn cause_fp_program_exception(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
- self.cpu.spr.srr0 = Srr0::from(self.cpu.cia);
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.cia);
// SRR1[11] = floating-point enabled exception indicator
- self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw() | (1 << (31 - 11));
+ self.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw() | (1 << (31 - 11));
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -189,11 +189,11 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.nia = base | IRQ_PROGRAM;
+ self.gekko.nia = base | IRQ_PROGRAM;
- tracing::debug!(addr = format!("{:08X}", self.cpu.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 may
@@ -202,7 +202,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
/// Fuck you, motherfucker
#[inline(always)]
pub fn check_fp_available(&mut self) -> bool {
- if self.cpu.msr.floating_point_available() {
+ if self.gekko.msr.floating_point_available() {
true
} else {
self.cause_fp_unavailable();
@@ -214,23 +214,23 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
/// FPSCR[FEX] is set, raise a Program exception (0x00700).
#[inline(always)]
pub fn check_fp_program_exception(&mut self) {
- if (self.cpu.msr.fe0() || self.cpu.msr.fe1()) && self.cpu.fpscr.fex() {
+ if (self.gekko.msr.fe0() || self.gekko.msr.fe1()) && self.gekko.fpscr.fex() {
self.cause_fp_program_exception();
}
}
pub fn cause_syscall_interrupt(&mut self) {
- let base: u32 = if self.cpu.msr.exception_prefix() {
+ let base: u32 = if self.gekko.msr.exception_prefix() {
0xFFF0_0000
} else {
0
};
- self.cpu.spr.srr0 = Srr0::from(self.cpu.cia.wrapping_add(4));
- self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+ self.gekko.spr.srr0 = Srr0::from(self.gekko.cia.wrapping_add(4));
+ self.gekko.spr.srr1 = chapa::extract_bits!(self.gekko.msr; 0, 5..=9, 16..=31).raw();
- self.cpu.msr = self
- .cpu
+ self.gekko.msr = self
+ .gekko
.msr
.with_pow(false)
.with_fp(false)
@@ -244,10 +244,10 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
.with_pr(false)
.with_se(false)
.with_ir(false)
- .with_le(self.cpu.msr.ile());
+ .with_le(self.gekko.msr.ile());
- self.cpu.nia = base | IRQ_SYSTEM_CALL;
+ self.gekko.nia = base | IRQ_SYSTEM_CALL;
- tracing::debug!(addr = format!("{:08X}", self.cpu.nia), "system call IRQ triggered");
+ tracing::debug!(addr = format!("{:08X}", self.gekko.nia), "system call IRQ triggered");
}
}
diff --git a/crates/gecko/src/cpu/msr.rs b/crates/gecko/src/gekko/msr.rs
similarity index 100%
rename from crates/gecko/src/cpu/msr.rs
rename to crates/gecko/src/gekko/msr.rs
diff --git a/crates/gecko/src/cpu/spr.rs b/crates/gecko/src/gekko/spr.rs
similarity index 100%
rename from crates/gecko/src/cpu/spr.rs
rename to crates/gecko/src/gekko/spr.rs
diff --git a/crates/gecko/src/cpu/sr.rs b/crates/gecko/src/gekko/sr.rs
similarity index 100%
rename from crates/gecko/src/cpu/sr.rs
rename to crates/gecko/src/gekko/sr.rs
diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rs
index f96625d..5c7ee59 100644
--- a/crates/gecko/src/lib.rs
+++ b/crates/gecko/src/lib.rs
@@ -1,5 +1,5 @@
pub mod common;
-pub mod cpu;
+pub mod gekko;
pub mod dvd;
pub mod flipper;
pub mod gamecube;
diff --git a/crates/gecko/src/mmio.rs b/crates/gecko/src/mmio.rs
index d1d8a2c..05666a8 100644
--- a/crates/gecko/src/mmio.rs
+++ b/crates/gecko/src/mmio.rs
@@ -215,7 +215,7 @@ impl Mmio {
}
/// Process a locked cache DMA transfer triggered by writing to SPR DMAL (923).
- pub fn process_locked_cache_dma(&mut self, dmau: &crate::cpu::spr::DmaUpper, dmal: &crate::cpu::spr::DmaLower) {
+ pub fn process_locked_cache_dma(&mut self, dmau: &crate::gekko::spr::DmaUpper, dmal: &crate::gekko::spr::DmaLower) {
let ram_addr = dmau.ram_addr() << 5;
let lcache_vaddr = dmal.lcache_addr() << 5;
let lcache_paddr = (lcache_vaddr - LCACHE_BASE) as usize;
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index 22e414d..5b721d5 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -90,10 +90,10 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
// Check all 4 DBATs
let dbats = [
- (self.cpu.spr.dbat0u, self.cpu.spr.dbat0l),
- (self.cpu.spr.dbat1u, self.cpu.spr.dbat1l),
- (self.cpu.spr.dbat2u, self.cpu.spr.dbat2l),
- (self.cpu.spr.dbat3u, self.cpu.spr.dbat3l),
+ (self.gekko.spr.dbat0u, self.gekko.spr.dbat0l),
+ (self.gekko.spr.dbat1u, self.gekko.spr.dbat1l),
+ (self.gekko.spr.dbat2u, self.gekko.spr.dbat2l),
+ (self.gekko.spr.dbat3u, self.gekko.spr.dbat3l),
];
for (batu, batl) in dbats {
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 9f03eeb..3777350 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -118,8 +118,8 @@ impl<const SYSTEM: SystemId> Scheduler<SYSTEM> {
self::dsp_batch_handler::<SYSTEM>,
);
s.schedule_at(
- crate::cpu::dec::cycles_until_underflow(u32::MAX),
- crate::cpu::dec::underflow_handler::<SYSTEM>,
+ crate::gekko::dec::cycles_until_underflow(u32::MAX),
+ crate::gekko::dec::underflow_handler::<SYSTEM>,
);
s
}
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index 4d25218..822872d 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -1,4 +1,4 @@
-use crate::cpu::Cpu;
+use crate::gekko::Gekko;
use crate::dvd::DvdInterface;
use crate::flipper::ai::AudioInterface;
use crate::flipper::cp::CommandProcessor;
@@ -26,7 +26,7 @@ pub const WII: SystemId = 1;
pub struct System<const SYSTEM: SystemId> {
pub vsync_pending: bool,
- pub cpu: Cpu,
+ pub gekko: Gekko,
pub scheduler: Scheduler<SYSTEM>,
pub mmio: Mmio,
pub vi: VideoInterface,
@@ -59,7 +59,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
pub(crate) fn with_scheduler(entrypoint: u32, scheduler: Scheduler<SYSTEM>) -> Self {
System {
vsync_pending: false,
- cpu: Cpu::new(entrypoint),
+ gekko: Gekko::new(entrypoint),
scheduler,
mmio: Mmio::new(),
vi: VideoInterface::new(),
@@ -90,7 +90,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
#[inline(always)]
pub fn step_cpu(&mut self) {
- if self.cpu.msr.external_interrupt_enable() {
+ if self.gekko.msr.external_interrupt_enable() {
// Deliver external interrupt when EE=1 and any enabled PI interrupt is pending
if self.pi.interrupt_pending() {
self.cause_external_interrupt();
@@ -98,7 +98,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
return;
}
- if self.cpu.dec.interrupt_pending() {
+ if self.gekko.dec.interrupt_pending() {
self.cause_decrementer_interrupt();
self.scheduler.cycles += 2;
return;
@@ -108,7 +108,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
// CPU pre-hook
#[cfg(feature = "hooks")]
if self.hook_flags.contains(HookFlags::CPU_PRE) {
- let pc = self.cpu.pc;
+ let pc = self.gekko.pc;
if self.hook_filters.cpu_pre.matches(pc) {
if let Some(mut host) = self.hook_host.take() {
host.on_cpu_pre(self);
@@ -119,16 +119,16 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
// Fetch and execute next instruction
- self.cpu.cia = self.cpu.pc;
- self.cpu.nia = self.cpu.cia.wrapping_add(4);
- let instr = crate::cpu::instruction::Instruction(self.mmio.fetch_instruction(self.cpu.cia));
- crate::cpu::dispatch(self, instr);
+ self.gekko.cia = self.gekko.pc;
+ self.gekko.nia = self.gekko.cia.wrapping_add(4);
+ let instr = crate::gekko::instruction::Instruction(self.mmio.fetch_instruction(self.gekko.cia));
+ crate::gekko::dispatch(self, instr);
self.scheduler.cycles += 2; // TODO: Track properly?
// CPU post-hook
#[cfg(feature = "hooks")]
if self.hook_flags.contains(HookFlags::CPU_POST) {
- let pc = self.cpu.cia;
+ let pc = self.gekko.cia;
if self.hook_filters.cpu_post.matches(pc) {
if let Some(mut host) = self.hook_host.take() {
host.on_cpu_post(self);
@@ -139,7 +139,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
}
#[cfg(feature = "idle-skip")]
- match self.idle.check(self.cpu.cia, self.cpu.nia) {
+ match self.idle.check(self.gekko.cia, self.gekko.nia) {
IdleCheck::Skip => {
self.scheduler.cycles = self.scheduler.next_deadline();
}
@@ -153,7 +153,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
IdleCheck::Continue => {}
}
- self.cpu.pc = self.cpu.nia;
+ self.gekko.pc = self.gekko.nia;
}
/// Drain pending scheduler events, then execute one CPU instruction.
@@ -192,7 +192,7 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
for i in 0..count.min(buf.len()) {
buf[i] = self.mmio.fetch_instruction(start + (i as u32) * 4);
}
- crate::idle::validate_polling_loop(&buf[..count.min(buf.len())], &self.cpu.gprs)
+ crate::idle::validate_polling_loop(&buf[..count.min(buf.len())], &self.gekko.gprs)
}
pub fn frame_size(&self) -> (usize, usize) {
diff --git a/crates/scripting/src/lib.rs b/crates/scripting/src/lib.rs
index 1631d8a..c7a44b9 100644
--- a/crates/scripting/src/lib.rs
+++ b/crates/scripting/src/lib.rs
@@ -208,40 +208,40 @@ impl LuaHost {
}
fn register_emu_methods(methods: &mut impl UserDataMethods<GameCubeRef>) {
- methods.add_method("gpr", |_, this, i: u8| Ok(this.cpu.read_gpr(i)));
+ methods.add_method("gpr", |_, this, i: u8| Ok(this.gekko.read_gpr(i)));
methods.add_method_mut("set_gpr", |_, this, (i, val): (u8, u32)| {
- this.cpu.write_gpr(i, val);
+ this.gekko.write_gpr(i, val);
Ok(())
});
- methods.add_method("fpr", |_, this, i: u8| Ok(this.cpu.read_fpr(i)));
+ methods.add_method("fpr", |_, this, i: u8| Ok(this.gekko.read_fpr(i)));
methods.add_method_mut("set_fpr", |_, this, (i, val): (u8, f64)| {
- this.cpu.write_fpr(i, val);
+ this.gekko.write_fpr(i, val);
Ok(())
});
- methods.add_method("ps1", |_, this, i: u8| Ok(this.cpu.read_ps1(i)));
+ methods.add_method("ps1", |_, this, i: u8| Ok(this.gekko.read_ps1(i)));
methods.add_method_mut("set_ps1", |_, this, (i, val): (u8, f64)| {
- this.cpu.write_ps1(i, val);
+ this.gekko.write_ps1(i, val);
Ok(())
});
- methods.add_method("pc", |_, this, ()| Ok(this.cpu.pc));
+ methods.add_method("pc", |_, this, ()| Ok(this.gekko.pc));
methods.add_method_mut("set_pc", |_, this, val: u32| {
- this.cpu.nia = val;
+ this.gekko.nia = val;
Ok(())
});
- methods.add_method("lr", |_, this, ()| Ok(this.cpu.spr.lr));
+ methods.add_method("lr", |_, this, ()| Ok(this.gekko.spr.lr));
methods.add_method_mut("set_lr", |_, this, val: u32| {
- this.cpu.spr.lr = val;
+ this.gekko.spr.lr = val;
Ok(())
});
- methods.add_method("ctr", |_, this, ()| Ok(this.cpu.spr.ctr));
+ methods.add_method("ctr", |_, this, ()| Ok(this.gekko.spr.ctr));
methods.add_method_mut("set_ctr", |_, this, val: u32| {
- this.cpu.spr.ctr = val;
+ this.gekko.spr.ctr = val;
Ok(())
});
- methods.add_method("cr", |_, this, ()| Ok(this.cpu.cr.raw()));
- methods.add_method("msr", |_, this, ()| Ok(this.cpu.msr.raw()));
- methods.add_method("xer", |_, this, ()| Ok(this.cpu.spr.xer.raw()));
- methods.add_method("fpscr", |_, this, ()| Ok(this.cpu.fpscr.raw()));
+ methods.add_method("cr", |_, this, ()| Ok(this.gekko.cr.raw()));
+ methods.add_method("msr", |_, this, ()| Ok(this.gekko.msr.raw()));
+ methods.add_method("xer", |_, this, ()| Ok(this.gekko.spr.xer.raw()));
+ methods.add_method("fpscr", |_, this, ()| Ok(this.gekko.fpscr.raw()));
methods.add_method("cycles", |_, this, ()| Ok(this.scheduler.cycles));
methods.add_method("read_u8", |_, this, addr: u32| Ok(this.mmio.virt_read_u8(addr)));
@@ -491,7 +491,7 @@ impl Host<{ gecko::system::GC }> for LuaHost {
}
fn on_cpu_pre(&mut self, emu: &mut GameCube) {
- let pc = emu.cpu.pc;
+ let pc = emu.gekko.pc;
if let Some(callback) = self.cpu_pre.get(&pc)
&& let Err(err) = self.call_cpu_hook("cpu_pre", callback, emu)
{
@@ -500,7 +500,7 @@ impl Host<{ gecko::system::GC }> for LuaHost {
}
fn on_cpu_post(&mut self, emu: &mut GameCube) {
- let pc = emu.cpu.cia;
+ let pc = emu.gekko.cia;
if let Some(callback) = self.cpu_post.get(&pc)
&& let Err(err) = self.call_cpu_hook("cpu_post", callback, emu)
{
diff --git a/crates/tinytracer/src/dump.rs b/crates/tinytracer/src/dump.rs
index 5239d81..33f8b45 100644
--- a/crates/tinytracer/src/dump.rs
+++ b/crates/tinytracer/src/dump.rs
@@ -45,7 +45,7 @@ pub fn registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
];
let fmt_cr_field =
- |label: &str, val: gecko::cpu::condition::ConditionField, prev_val: gecko::cpu::condition::ConditionField| {
+ |label: &str, val: gecko::gekko::condition::ConditionField, prev_val: gecko::gekko::condition::ConditionField| {
let flags = format!(
"{}{}{}{}",
if val.lt() { "L" } else { "·" },
diff --git a/crates/tinytracer/src/main.rs b/crates/tinytracer/src/main.rs
index caab0da..45e073b 100644
--- a/crates/tinytracer/src/main.rs
+++ b/crates/tinytracer/src/main.rs
@@ -141,8 +141,8 @@ fn main() {
}
fn run_emulator(emulator: &mut gecko::gamecube::GameCube, args: &Args, symbols: Option<&image::symbols::SymbolTable>) {
- let mut prev_snapshot = CpuSnapshot::from_cpu(&emulator.cpu);
- let mut prev_pc = emulator.cpu.pc;
+ let mut prev_snapshot = CpuSnapshot::from_cpu(&emulator.gekko);
+ let mut prev_pc = emulator.gekko.pc;
let mut prev_dsp_pc = emulator.dsp.registers.pc;
let mut in_busyloop = false;
let mut current_func: Option<String> = None;
@@ -153,7 +153,7 @@ fn run_emulator(emulator: &mut gecko::gamecube::GameCube, args: &Args, symbols:
loop {
if !in_busyloop && !args.quiet && trace_cpu {
if let Some(symbols) = symbols
- && let Some(sym) = symbols.lookup_exact(emulator.cpu.pc)
+ && let Some(sym) = symbols.lookup_exact(emulator.gekko.pc)
&& sym.kind == image::symbols::SymbolKind::Func
{
let name = &sym.name;
@@ -169,8 +169,8 @@ fn run_emulator(emulator: &mut gecko::gamecube::GameCube, args: &Args, symbols:
emulator.step();
// CPU trace
- let curr_snapshot = CpuSnapshot::from_cpu(&emulator.cpu);
- let curr_pc = emulator.cpu.pc;
+ let curr_snapshot = CpuSnapshot::from_cpu(&emulator.gekko);
+ let curr_pc = emulator.gekko.pc;
if trace_cpu {
if curr_pc == prev_pc {
@@ -211,14 +211,14 @@ fn print_cpu_instruction(
debug: bool,
prefix_tag: bool,
) {
- let instr = GekkoInstruction::decode(emulator.mmio.virt_slice(emulator.cpu.pc, 4))
+ let instr = GekkoInstruction::decode(emulator.mmio.virt_slice(emulator.gekko.pc, 4))
.unwrap_or_else(|| {
dump::registers(prev_snapshot, prev_snapshot);
- dump::memory(&emulator.mmio, emulator.cpu.read_gpr(1));
+ dump::memory(&emulator.mmio, emulator.gekko.read_gpr(1));
panic!(
"Failed to decode instruction at {:08X} => {:08X}",
- emulator.cpu.pc,
- emulator.mmio.virt_read_u32(emulator.cpu.pc)
+ emulator.gekko.pc,
+ emulator.mmio.virt_read_u32(emulator.gekko.pc)
);
})
.0;
@@ -238,7 +238,7 @@ fn print_cpu_instruction(
let prefix = format!(
"{}{}: {}",
tag,
- format!("{:08X}", emulator.cpu.pc).bold(),
+ format!("{:08X}", emulator.gekko.pc).bold(),
fmt::colorize_instr(&instr)
);
diff --git a/crates/tinytracer/src/snaptshot.rs b/crates/tinytracer/src/snaptshot.rs
index 288d44d..03865c3 100644
--- a/crates/tinytracer/src/snaptshot.rs
+++ b/crates/tinytracer/src/snaptshot.rs
@@ -1,4 +1,4 @@
-use gecko::cpu::condition::ConditionRegister;
+use gecko::gekko::condition::ConditionRegister;
#[derive(Clone, Copy)]
pub struct CpuSnapshot {
@@ -10,7 +10,7 @@ pub struct CpuSnapshot {
}
impl CpuSnapshot {
- pub fn from_cpu(cpu: &gecko::cpu::Cpu) -> Self {
+ pub fn from_cpu(cpu: &gecko::gekko::Gekko) -> Self {
Self {
gprs: cpu.gprs,
fprs: cpu.fprs,
diff --git a/crates/web/src/debug_ui.rs b/crates/web/src/debug_ui.rs
index 6feabc9..ccefa5d 100644
--- a/crates/web/src/debug_ui.rs
+++ b/crates/web/src/debug_ui.rs
@@ -96,7 +96,7 @@ impl DebugState {
dbglib::windows::cpu::show_cpu(
ctx,
&mut self.show_cpu,
- &emulator.cpu,
+ &emulator.gekko,
&emulator.mmio,
self.symbols.as_ref(),
self.debugger.breakpoints(),
@@ -106,7 +106,7 @@ impl DebugState {
dbglib::windows::callstack::show_callstack(
ctx,
&mut self.show_callstack,
- &emulator.cpu,
+ &emulator.gekko,
&emulator.mmio,
self.symbols.as_ref(),
);
@@ -163,7 +163,7 @@ impl DebugState {
dbglib::windows::exi::show_exi(ctx, &mut self.show_exi, &emulator.exi);
}
if self.show_irqs {
- dbglib::windows::irq::show_irq(ctx, &mut self.show_irqs, &emulator.cpu, &emulator.pi);
+ dbglib::windows::irq::show_irq(ctx, &mut self.show_irqs, &emulator.gekko, &emulator.pi);
}
if self.show_breakpoints {
dbglib::windows::breakpoints::show_breakpoints(