Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

a few dsp instructions

Layleecdd650345f6
diff --git a/Cargo.lock b/Cargo.lock
index 7797b75..13dffb5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -383,18 +383,18 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chapa"
-version = "0.1.1"
+version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e32c359a2c9fad1bf865975a80e3ffb1384ebae1814268ffd8d657f91af0a0fe"
+checksum = "feb604f1f94b0e32df0dfdf3fdd5e0214c95e874e038d2e05b06efb83d1019dd"
dependencies = [
"chapa-macros",
]
[[package]]
name = "chapa-macros"
-version = "0.1.1"
+version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "158582b06b8a70fbe05a0942bb254c07149ffd88d7ddb29ea2c15a6213309f0c"
+checksum = "acae94a68db251cb0b4249c5627436faf9ec8f37c21fae63db4c2f995aaf45e4"
dependencies = [
"proc-macro2",
"quote",
diff --git a/crates/debugger/src/windows/dsp.rs b/crates/debugger/src/windows/dsp.rs
index 3b8198c..321fe8b 100644
--- a/crates/debugger/src/windows/dsp.rs
+++ b/crates/debugger/src/windows/dsp.rs
@@ -23,7 +23,7 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
.striped(true)
.show(ui, |ui| {
ui.label("PC");
- ui.monospace(format!("{:#06X}", dsp.pc));
+ ui.monospace(format!("{:#06X}", dsp.registers.pc));
ui.label("Halt");
ui.label(if dsp.csr.halt() { "yes" } else { "no" });
ui.end_row();
@@ -51,7 +51,7 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
.min_col_width(0.0)
.striped(true)
.show(ui, |ui| {
- let mut addr = dsp.pc;
+ let mut addr = dsp.registers.pc;
for _ in 0..20 {
let off = (addr as usize) * 2; // word-addressed PC -> byte offset
if off + 1 >= dsp.iram.len() {
@@ -66,7 +66,7 @@ pub fn show_dsp(ctx: &Context, open: &mut bool, dsp: &Dsp) {
}
};
- let is_pc = addr == dsp.pc;
+ let is_pc = addr == dsp.registers.pc;
// PC indicator
if is_pc {
diff --git a/crates/gecko/Cargo.toml b/crates/gecko/Cargo.toml
index f6b9878..1018f0b 100644
--- a/crates/gecko/Cargo.toml
+++ b/crates/gecko/Cargo.toml
@@ -11,6 +11,6 @@ scripting-mut-traps = ["scripting"]
chipi-build = "0.7.2"
[dependencies]
-chapa = "0.1.0"
+chapa = "0.1.2"
tracing = { version = "0.1.44", features = ["release_max_level_off"] }
image = { path = "../image" }
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index 5c2c396..f4c3dd7 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -1,3 +1,5 @@
+pub mod condition;
+pub mod core;
pub mod instruction;
pub mod interpreter;
pub mod regs;
@@ -15,9 +17,7 @@ use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};
pub struct Dsp {
// Registers
- pub pc: u16,
- pub nia: u16,
- pub cia: u16,
+ pub registers: core::Registers,
// IMEM = IRAM + IROM
pub iram: Box<[u8; 0x1000]>, // 0x0000 - 0x0FFF
@@ -58,9 +58,7 @@ impl Dsp {
let ifx = unsafe { Box::<[u8; 0x100]>::new_zeroed().assume_init() };
Dsp {
- pc: 0,
- nia: 0,
- cia: 0,
+ registers: core::Registers::default(),
iram,
irom,
dram,
@@ -148,16 +146,31 @@ impl GameCube {
return;
}
- let instr = Instruction::from_be_bytes(&self.dsp.iram[self.dsp.pc as usize..]);
- self.dsp.cia = self.dsp.pc;
- self.dsp.nia = self
+ let instr = Instruction::from_be_bytes(&self.dsp.iram[self.dsp.registers.pc as usize..]);
+ self.dsp.registers.cia = self.dsp.registers.pc;
+ self.dsp.registers.nia = self
.dsp
+ .registers
.cia
.wrapping_add(crate::flipper::dsp::lut::instr_size(instr) as u16);
crate::flipper::dsp::lut::dispatch(self, instr);
- self.dsp.pc = self.dsp.nia;
+ // Check if we've reached the end of a loop stack
+ let is_end_of_loop = self.dsp.registers.nia == self.dsp.registers.loop_addr.top();
+ if !self.dsp.registers.loop_addr.is_empty() && is_end_of_loop {
+ let counter = self.dsp.registers.loop_counter.top().wrapping_sub(1);
+ if counter != 0 {
+ self.dsp.registers.loop_counter.set_top(counter);
+ self.dsp.registers.nia = self.dsp.registers.call_stack.top();
+ } else {
+ self.dsp.registers.loop_counter.pop();
+ self.dsp.registers.loop_addr.pop();
+ self.dsp.registers.call_stack.pop();
+ }
+ }
+
+ self.dsp.registers.pc = self.dsp.registers.nia;
}
}
@@ -180,6 +193,52 @@ impl MmioRw for Dsp {
}
impl Dsp {
+ /// Read a 16-bit word from instruction memory (IRAM 0x0000-0x0FFF, IROM 0x8000-0x8FFF).
+ pub fn read_imem(&self, addr: u16) -> u16 {
+ let byte_addr = addr as usize * 2;
+ let bytes = match addr as usize {
+ 0x0000..0x1000 => &self.iram[byte_addr..byte_addr + 2],
+ 0x8000..0x9000 => {
+ let offset = byte_addr - 0x8000 * 2;
+ &self.irom[offset..offset + 2]
+ }
+ _ => &[0, 0],
+ };
+ u16::from_be_bytes([bytes[0], bytes[1]])
+ }
+
+ /// Read a 16-bit word from data memory (DRAM 0x0000-0x0FFF, COEF 0x1000-0x1FFF, IFX 0xFF00-0xFFFF).
+ pub fn read_dmem(&self, addr: u16) -> u16 {
+ let byte_addr = addr as usize * 2;
+ let bytes = match addr as usize {
+ 0x0000..0x1000 => &self.dram[byte_addr..byte_addr + 2],
+ 0x1000..0x2000 => {
+ let offset = byte_addr - 0x1000 * 2;
+ &self.coef[offset..offset + 2]
+ }
+ 0xFF00..0x10000 => {
+ let offset = (addr as usize - 0xFF00) * 2;
+ &self.ifx[offset..offset + 2]
+ }
+ _ => &[0, 0],
+ };
+ u16::from_be_bytes([bytes[0], bytes[1]])
+ }
+
+ /// Write a 16-bit word to data memory (DRAM 0x0000-0x0FFF, IFX 0xFF00-0xFFFF).
+ pub fn write_dmem(&mut self, addr: u16, value: u16) {
+ let byte_addr = addr as usize * 2;
+ let bytes = value.to_be_bytes();
+ match addr as usize {
+ 0x0000..0x1000 => self.dram[byte_addr..byte_addr + 2].copy_from_slice(&bytes),
+ 0xFF00..0x10000 => {
+ let offset = (addr as usize - 0xFF00) * 2;
+ self.ifx[offset..offset + 2].copy_from_slice(&bytes);
+ }
+ _ => {}
+ }
+ }
+
pub fn interrupt_active(&self) -> bool {
(self.csr.ai_interrupt() && self.csr.ai_interrupt_mask())
|| (self.csr.ar_interrupt() && self.csr.ar_interrupt_mask())
diff --git a/crates/gecko/src/flipper/dsp/condition.rs b/crates/gecko/src/flipper/dsp/condition.rs
new file mode 100644
index 0000000..b47332d
--- /dev/null
+++ b/crates/gecko/src/flipper/dsp/condition.rs
@@ -0,0 +1,60 @@
+use crate::flipper::dsp;
+
+pub enum BranchControl {
+ GreaterThanOrEqual = 0b0000,
+ LessThan = 0b0001,
+ GreaterThan = 0b0010,
+ LessThanOrEqual = 0b0011,
+ NotZero = 0b0100,
+ Zero = 0b0101,
+ NotCarry = 0b0110,
+ Carry = 0b0111,
+ BelowS32 = 0b1000,
+ AboveS32 = 0b1001,
+ AccumulatorNotZeroExtended = 0b1010,
+ AccumulatorNotZero = 0b1011,
+ NotLogicalZero = 0b1100,
+ LogicalZero = 0b1101,
+ Overflow = 0b1110,
+ Always = 0b1111,
+}
+
+impl BranchControl {
+ pub fn from(raw: u8) -> Self {
+ assert!(raw <= 0b1111);
+ unsafe { std::mem::transmute(raw) }
+ }
+
+ pub fn evaluate(&self, dsp: &dsp::Dsp) -> bool {
+ match self {
+ Self::GreaterThanOrEqual => dsp.registers.status.overflow() == dsp.registers.status.sign(),
+ Self::LessThan => dsp.registers.status.overflow() != dsp.registers.status.sign(),
+ Self::GreaterThan => {
+ (dsp.registers.status.overflow() == dsp.registers.status.sign())
+ && !dsp.registers.status.arithmetic_zero()
+ }
+ Self::LessThanOrEqual => {
+ (dsp.registers.status.overflow() != dsp.registers.status.sign())
+ || dsp.registers.status.arithmetic_zero()
+ }
+ Self::NotZero => !dsp.registers.status.arithmetic_zero(),
+ Self::Zero => dsp.registers.status.arithmetic_zero(),
+ Self::NotCarry => !dsp.registers.status.carry(),
+ Self::Carry => dsp.registers.status.carry(),
+ Self::BelowS32 => !dsp.registers.status.above_s32(),
+ Self::AboveS32 => dsp.registers.status.above_s32(),
+ Self::AccumulatorNotZeroExtended => {
+ (dsp.registers.status.above_s32() || dsp.registers.status.top_two_bits_equal())
+ && !dsp.registers.status.arithmetic_zero()
+ }
+ Self::AccumulatorNotZero => {
+ (!dsp.registers.status.above_s32() && !dsp.registers.status.top_two_bits_equal())
+ || dsp.registers.status.arithmetic_zero()
+ }
+ Self::NotLogicalZero => !dsp.registers.status.logical_zero(),
+ Self::LogicalZero => dsp.registers.status.logical_zero(),
+ Self::Overflow => dsp.registers.status.overflow(),
+ Self::Always => true,
+ }
+ }
+}
diff --git a/crates/gecko/src/flipper/dsp/core.rs b/crates/gecko/src/flipper/dsp/core.rs
new file mode 100644
index 0000000..b7b7c38
--- /dev/null
+++ b/crates/gecko/src/flipper/dsp/core.rs
@@ -0,0 +1,204 @@
+use chapa::BitEnum;
+
+pub struct DspStack<const N: usize> {
+ data: [u16; N],
+ ptr: u8,
+}
+
+impl<const N: usize> Default for DspStack<N> {
+ fn default() -> Self {
+ Self { data: [0; N], ptr: 0 }
+ }
+}
+
+impl<const N: usize> DspStack<N> {
+ #[inline(always)]
+ pub fn top(&self) -> u16 {
+ self.data[self.ptr as usize]
+ }
+
+ #[inline(always)]
+ pub fn set_top(&mut self, value: u16) {
+ self.data[self.ptr as usize] = value;
+ }
+
+ #[inline(always)]
+ pub fn push(&mut self, value: u16) {
+ self.ptr += 1;
+ self.data[self.ptr as usize] = value;
+ }
+
+ #[inline(always)]
+ pub fn pop(&mut self) -> u16 {
+ let value = self.data[self.ptr as usize];
+ self.ptr -= 1;
+ value
+ }
+
+ #[inline(always)]
+ pub fn is_empty(&self) -> bool {
+ self.ptr == 0
+ }
+}
+
+#[derive(Default)]
+pub struct Registers {
+ pub pc: u16,
+ pub nia: u16,
+ pub cia: u16,
+ pub ar: [u16; 4],
+ pub ix: [u16; 4],
+ pub wr: [u16; 4],
+ pub call_stack: DspStack<8>, // st0
+ pub data_stack: DspStack<4>, // st1
+ pub loop_addr: DspStack<4>, // st2
+ pub loop_counter: DspStack<4>, // st3
+ pub ac0_high: u16,
+ pub ac1_high: u16,
+ pub config: u16,
+ pub status: StatusRegister,
+ pub product_low: u16,
+ pub product_mid1: u16,
+ pub product_high: u16,
+ pub product_mid2: u16,
+ pub ax: [u16; 2],
+ pub axh: [u16; 2],
+ pub ac0_low: u16,
+ pub ac1_low: u16,
+ pub ac0_mid: u16,
+ pub ac1_mid: u16,
+}
+
+impl Registers {
+ #[inline(always)]
+ pub fn sign_extended(&self) -> bool {
+ self.status.sxm() == SignExtensionMode::Bits40
+ }
+
+ #[inline(always)]
+ pub fn read<const ALLOW_SATURATION: bool>(&self, index: u8) -> u16 {
+ match index {
+ 0..=3 => self.ar[index as usize],
+ 4..=7 => self.ix[(index - 4) as usize],
+ 8..=11 => self.wr[(index - 8) as usize],
+ 12 => self.call_stack.top(),
+ 13 => self.data_stack.top(),
+ 14 => self.loop_addr.top(),
+ 15 => self.loop_counter.top(),
+ 16 => self.ac0_high,
+ 17 => self.ac1_high,
+ 18 => self.config,
+ 19 => self.status.into(),
+ 20 => self.product_low,
+ 21 => self.product_mid1,
+ 22 => self.product_high,
+ 23 => self.product_mid2,
+ 24..=25 => self.ax[(index - 24) as usize],
+ 26..=27 => self.axh[(index - 26) as usize],
+ 28 => self.ac0_low,
+ 29 => self.ac1_low,
+ 30 => {
+ if ALLOW_SATURATION && self.sign_extended() {
+ return self.saturate_ac_mid(self.ac0_high, self.ac0_mid);
+ }
+ self.ac0_mid
+ }
+ 31 => {
+ if ALLOW_SATURATION && self.sign_extended() {
+ return self.saturate_ac_mid(self.ac1_high, self.ac1_mid);
+ }
+ self.ac1_mid
+ }
+ _ => unreachable!(),
+ }
+ }
+
+ /// Saturate $acX.m: if $acX.h is not the sign extension of $acX.m,
+ /// return 0x7FFF (positive) or 0x8000 (negative).
+ #[inline(always)]
+ fn saturate_ac_mid(&self, high: u16, mid: u16) -> u16 {
+ let sign_ext = if mid & 0x8000 != 0 { 0x00FF } else { 0 };
+ if high != sign_ext {
+ if high & 0x80 != 0 { 0x8000 } else { 0x7FFF }
+ } else {
+ mid
+ }
+ }
+
+ #[inline(always)]
+ pub fn write<const ALLOW_SIGN_EXTENSION: bool>(&mut self, index: u8, value: u16) {
+ match index {
+ 0..=3 => self.ar[index as usize] = value,
+ 4..=7 => self.ix[(index - 4) as usize] = value,
+ 8..=11 => self.wr[(index - 8) as usize] = value,
+ 12 => self.call_stack.set_top(value),
+ 13 => self.data_stack.set_top(value),
+ 14 => self.loop_addr.set_top(value),
+ 15 => self.loop_counter.set_top(value),
+ 16 => self.ac0_high = value,
+ 17 => self.ac1_high = value,
+ 18 => self.config = value,
+ 19 => self.status = StatusRegister::from(value),
+ 20 => self.product_low = value,
+ 21 => self.product_mid1 = value,
+ 22 => self.product_high = value,
+ 23 => self.product_mid2 = value,
+ 24..=25 => self.ax[(index - 24) as usize] = value,
+ 26..=27 => self.axh[(index - 26) as usize] = value,
+ 28 => self.ac0_low = value,
+ 29 => self.ac1_low = value,
+ 30 => {
+ self.ac0_mid = value;
+ if ALLOW_SIGN_EXTENSION && self.sign_extended() {
+ self.ac0_high = if value & 0x8000 != 0 { 0x00FF } else { 0 };
+ self.ac0_low = 0;
+ }
+ }
+ 31 => {
+ self.ac1_mid = value;
+ if ALLOW_SIGN_EXTENSION && self.sign_extended() {
+ self.ac1_high = if value & 0x8000 != 0 { 0x00FF } else { 0 };
+ self.ac1_low = 0;
+ }
+ }
+ _ => unreachable!(),
+ }
+ }
+}
+
+#[derive(BitEnum, PartialEq, PartialOrd)]
+pub enum SignExtensionMode {
+ Bits16,
+ Bits40,
+}
+
+#[chapa::bitfield(u16, order = lsb0)]
+#[derive(Clone, Copy, Default)]
+pub struct StatusRegister {
+ #[bits(0, alias = "c")]
+ pub carry: bool,
+ #[bits(1, alias = "o")]
+ pub overflow: bool,
+ #[bits(2, alias = "z")]
+ pub arithmetic_zero: bool,
+ #[bits(3, alias = "s")]
+ pub sign: bool,
+ #[bits(4, alias = "as32")]
+ pub above_s32: bool,
+ #[bits(5, alias = "tb")]
+ pub top_two_bits_equal: bool,
+ #[bits(6, alias = "lz")]
+ pub logical_zero: bool,
+ #[bits(7, alias = "os")]
+ pub overflow_sticky: bool,
+ #[bits(9, alias = "ie")]
+ pub interrupt_enable: bool,
+ #[bits(11, alias = "eie")]
+ pub external_interrupt_enable: bool,
+ #[bits(13, alias = "am")]
+ pub product_multiply_result_by_2: bool, // when AM = 0
+ #[bits(14, alias = "sxm")]
+ pub sign_extension_mode: SignExtensionMode,
+ #[bits(15, alias = "su")]
+ pub multiplication_operands_are_signed: bool,
+}
diff --git a/crates/gecko/src/flipper/dsp/interpreter.rs b/crates/gecko/src/flipper/dsp/interpreter.rs
index feb5f21..69f9869 100644
--- a/crates/gecko/src/flipper/dsp/interpreter.rs
+++ b/crates/gecko/src/flipper/dsp/interpreter.rs
@@ -1,5 +1,6 @@
-use crate::flipper::dsp::lut::*;
+use crate::flipper::dsp::{condition::BranchControl, core::{SignExtensionMode, StatusRegister}, lut::*};
+#[inline(always)]
pub fn add_sub<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -19,6 +20,7 @@ pub fn add_sub<const OP: u32>(
}
}
+#[inline(always)]
pub fn addr_reg<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -32,6 +34,7 @@ pub fn addr_reg<const OP: u32>(
}
}
+#[inline(always)]
pub fn cmp_test<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -51,27 +54,42 @@ pub fn cmp_test<const OP: u32>(
}
}
+#[inline(always)]
pub fn control<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
- OP_NOP => todo!("nop"),
- OP_HALT => todo!("halt"),
+ OP_JCC => {
+ let branch_control = BranchControl::from(instr.cond());
+ if branch_control.evaluate(&ctx.dsp) {
+ ctx.dsp.registers.nia = instr.addr();
+ }
+ }
+ OP_NOP => {},
+ OP_HALT => {
+ ctx.dsp.csr.set_halt(true);
+ },
OP_IFCC => todo!("ifcc"),
- OP_JCC => todo!("jcc"),
OP_CALLCC => todo!("callcc"),
OP_RETCC => todo!("retcc"),
- OP_RTICC => todo!("rticc"),
+ OP_RTICC => {
+ let branch_control = BranchControl::from(instr.cond());
+ if branch_control.evaluate(&ctx.dsp) {
+ ctx.dsp.registers.status = StatusRegister::from(ctx.dsp.registers.data_stack.pop());
+ ctx.dsp.registers.nia = ctx.dsp.registers.call_stack.pop();
+ }
+ },
OP_JRCC => todo!("jrcc"),
OP_CALLRCC => todo!("callrcc"),
_ => unreachable!(),
}
}
+#[inline(always)]
pub fn imm_alu<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
OP_ADDI => todo!("addi"),
@@ -79,8 +97,17 @@ pub fn imm_alu<const OP: u32>(
OP_ANDI => todo!("andi"),
OP_ORI => todo!("ori"),
OP_CMPI => todo!("cmpi"),
- OP_ANDF => todo!("andf"),
- OP_ANDCF => todo!("andcf"),
+ OP_ANDF | OP_ANDCF => {
+ let ac_mid = if instr.d_7_7() != 0 { ctx.dsp.registers.ac1_mid } else { ctx.dsp.registers.ac0_mid };
+ let imm = instr.imm_16_31();
+ let result = ac_mid & imm;
+ let lz = match OP {
+ OP_ANDF => result == 0,
+ OP_ANDCF => result == imm,
+ _ => unreachable!(),
+ };
+ ctx.dsp.registers.status.set_lz(lz);
+ },
OP_ADDIS => todo!("addis"),
OP_CMPIS => todo!("cmpis"),
OP_LRIS => todo!("lris"),
@@ -88,6 +115,7 @@ pub fn imm_alu<const OP: u32>(
}
}
+#[inline(always)]
pub fn inc_dec<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -103,16 +131,22 @@ pub fn inc_dec<const OP: u32>(
}
}
+#[inline(always)]
pub fn load_store<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
- OP_LRI => todo!("lri"),
+ OP_LRI => {
+ ctx.dsp.registers.write::<true>(instr.d_11_15(), instr.imm_16_31());
+ },
OP_LR => todo!("lr"),
OP_SR => todo!("sr"),
OP_MRR => todo!("mrr"),
- OP_SI => todo!("si"),
+ OP_SI => {
+ let addr = 0xFF00 | (instr.mem_8_15_u16());
+ ctx.dsp.write_dmem(addr, instr.imm_16_31());
+ },
OP_LRR => todo!("lrr"),
OP_LRRD => todo!("lrrd"),
OP_LRRI => todo!("lrri"),
@@ -121,17 +155,39 @@ pub fn load_store<const OP: u32>(
OP_SRRD => todo!("srrd"),
OP_SRRI => todo!("srri"),
OP_SRRN => todo!("srrn"),
- OP_LRS => todo!("lrs"),
- OP_SRSH => todo!("srsh"),
- OP_SRS => todo!("srs"),
- OP_ILRR => todo!("ilrr"),
- OP_ILRRD => todo!("ilrrd"),
- OP_ILRRI => todo!("ilrri"),
- OP_ILRRN => todo!("ilrrn"),
+ OP_LRS => {
+ let dst = 0x18 + instr.reg_5_7();
+ let addr = ((ctx.dsp.registers.config as u16) << 8) | instr.mem_8_15_u16();
+ let value = ctx.dsp.read_dmem(addr);
+ ctx.dsp.registers.write::<true>(dst, value);
+ },
+ OP_SRSH | OP_SRS => {
+ let addr = ((ctx.dsp.registers.config as u16) << 8) | instr.mem_8_15_u16();
+ let src = match OP {
+ OP_SRSH => if instr.s_7_7() != 0 { 16 } else { 17 }, // ac0.h (16) or ac1.h (17)
+ OP_SRS => 0x1C + instr.reg_6_7(),
+ _ => unreachable!(),
+ };
+ let value = ctx.dsp.registers.read::<true>(src);
+ ctx.dsp.write_dmem(addr, value);
+ },
+ OP_ILRR | OP_ILRRD | OP_ILRRI | OP_ILRRN => {
+ let src = instr.s_14_15() as usize;
+ let dst = if instr.d_7_7() != 0 { 31u8 } else { 30u8 }; // ac1.m or ac0.m
+ let value = ctx.dsp.read_imem(ctx.dsp.registers.ar[src]);
+ ctx.dsp.registers.write::<true>(dst, value);
+ match OP {
+ OP_ILRRD => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_sub(1),
+ OP_ILRRI => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_add(1),
+ OP_ILRRN => ctx.dsp.registers.ar[src] = ctx.dsp.registers.ar[src].wrapping_add(ctx.dsp.registers.ix[src]),
+ _ => {}
+ }
+ },
_ => unreachable!(),
}
}
+#[inline(always)]
pub fn logic<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -148,19 +204,33 @@ pub fn logic<const OP: u32>(
}
}
+#[inline(always)]
pub fn loops<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
OP_LOOP_REG => todo!("loop_reg"),
- OP_BLOOP => todo!("bloop"),
+ OP_BLOOP => {
+ let reg = instr.reg_11_15();
+ let counter = ctx.dsp.registers.read::<true>(reg);
+ let end_addr = instr.addr();
+
+ if counter != 0 {
+ ctx.dsp.registers.call_stack.push(ctx.dsp.registers.pc.wrapping_add(2));
+ ctx.dsp.registers.loop_addr.push(end_addr);
+ ctx.dsp.registers.loop_counter.push(counter);
+ } else {
+ ctx.dsp.registers.nia = end_addr.wrapping_add(1);
+ }
+ },
OP_LOOPI => todo!("loopi"),
OP_BLOOPI => todo!("bloopi"),
_ => unreachable!(),
}
}
+#[inline(always)]
pub fn move_ops<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -176,6 +246,7 @@ pub fn move_ops<const OP: u32>(
}
}
+#[inline(always)]
pub fn mul<const OP: u32>(_ctx: &mut crate::gamecube::GameCube, _instr: crate::flipper::dsp::instruction::Instruction) {
match OP {
OP_MUL => todo!("mul"),
@@ -201,6 +272,7 @@ pub fn mul<const OP: u32>(_ctx: &mut crate::gamecube::GameCube, _instr: crate::f
}
}
+#[inline(always)]
pub fn shifts<const OP: u32>(
_ctx: &mut crate::gamecube::GameCube,
_instr: crate::flipper::dsp::instruction::Instruction,
@@ -223,19 +295,36 @@ pub fn shifts<const OP: u32>(
}
}
+#[inline(always)]
pub fn status<const OP: u32>(
- _ctx: &mut crate::gamecube::GameCube,
- _instr: crate::flipper::dsp::instruction::Instruction,
+ ctx: &mut crate::gamecube::GameCube,
+ instr: crate::flipper::dsp::instruction::Instruction,
) {
match OP {
- OP_SBCLR => todo!("sbclr"),
- OP_SBSET => todo!("sbset"),
- OP_M2 => todo!("m2"),
- OP_M0 => todo!("m0"),
- OP_CLR15 => todo!("clr15"),
- OP_SET15 => todo!("set15"),
- OP_SET16 => todo!("set16"),
- OP_SET40 => todo!("set40"),
+ OP_SBCLR => {
+ ctx.dsp.registers.status &= !(1 << (6 + instr.bit())) as u16;
+ }
+ OP_SBSET => {
+ ctx.dsp.registers.status |= (1 << (6 + instr.bit())) as u16;
+ },
+ OP_M2 => {
+ ctx.dsp.registers.status.set_am(false);
+ },
+ OP_M0 => {
+ ctx.dsp.registers.status.set_am(true);
+ },
+ OP_CLR15 => {
+ ctx.dsp.registers.status.set_su(false);
+ },
+ OP_SET15 => {
+ ctx.dsp.registers.status.set_su(true);
+ },
+ OP_SET16 => {
+ ctx.dsp.registers.status.set_sxm(SignExtensionMode::Bits16);
+ }
+ OP_SET40 => {
+ ctx.dsp.registers.status.set_sxm(SignExtensionMode::Bits40);
+ },
_ => unreachable!(),
}
}
diff --git a/scripts/hazel.lua b/scripts/hazel.lua
new file mode 100644
index 0000000..fba6d14
--- /dev/null
+++ b/scripts/hazel.lua
@@ -0,0 +1,27 @@
+-- toggle = false
+-- local function hack(emu, virt_addr, phys_addr, size, value)
+-- log(string.format("Toggling DSP busy bit: %s", toggle and "ON" or "OFF"))
+-- log(string.format("Original DSP read value: %08X", value))
+-- if toggle then
+-- value = value | 0x8000
+-- toggle = false
+-- else
+-- value = value & 0x7FFF
+-- toggle = true
+-- end
+-- log(string.format("Modified DSP read value: %08X", value))
+-- return value
+-- end
+
+local function ar_mode_hook(emu, virt_addr, phys_addr, size, value)
+ log(string.format("Original AR mode value: %08X", value))
+ return value | 1
+end
+
+traps = {
+ bus_read_post = {
+ virt = {
+ [0xCC005016] = ar_mode_hook,
+ },
+ },
+}
\ No newline at end of file