Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

random shit idk

Layle9076f722c540
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index 1408a0b..3540cb5 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -11,7 +11,8 @@ fn main() {
.ctx_type("crate::gekko::Gekko")
.instr_type("crate::cpu::semantics::Instruction")
.group("branch", ["bx", "bcx", "bclrx"])
- .group("alu", ["ori", "addi", "addis"])
+ .group("alu", ["ori", "addx", "addi", "addis"])
+ .group("rotate", ["rlwinmx"])
.group("msr", ["mtmsr", "mfmsr"])
.group("spr", ["mtspr", "mfspr"])
.group("store_load", ["stw", "stwu", "sth", "sthu", "lwz", "lwzu"])
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index fc43484..50fc053 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -30,9 +30,9 @@ impl Cpu {
Cpu {
gprs: [0; 32],
fprs: [0.0; 32],
- pc: 0x80000100,
- cia: 0x80000100,
- nia: 0x80000104,
+ pc: 0x80004000,
+ cia: 0x80004000,
+ nia: 0x80004004,
lr: 0,
ctr: 0,
xer: 0,
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index c7509bf..20bf0aa 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -1,4 +1,4 @@
-use crate::cpu::condition::{self, BranchControl, ConditionField};
+use crate::cpu::condition::{BranchControl, ConditionField};
pub fn branch<const OP: u32>(
ctx: &mut crate::gekko::Gekko,
@@ -61,6 +61,21 @@ pub fn alu<const OP: u32>(
instr: crate::cpu::semantics::Instruction,
) {
match OP {
+ crate::cpu::lut::OP_ADDX => {
+ let (res, carry) = ctx
+ .cpu
+ .read_gpr(instr.ra())
+ .overflowing_add(ctx.cpu.read_gpr(instr.rb()));
+ ctx.cpu.write_gpr(instr.rd(), res);
+ if instr.rc() {
+ let cr0 = ConditionField::new()
+ .with_lt((res as i32) < 0)
+ .with_gt(res > 0)
+ .with_eq(res == 0)
+ .with_so(carry);
+ ctx.cpu.cr.set_cr0(cr0);
+ }
+ }
crate::cpu::lut::OP_ADDI => {
let ra = if instr.ra() == 0 {
0
@@ -95,6 +110,21 @@ pub fn alu<const OP: u32>(
}
}
+pub fn rotate<const OP: u32>(
+ ctx: &mut crate::gekko::Gekko,
+ instr: crate::cpu::semantics::Instruction,
+) {
+ match OP {
+ crate::cpu::lut::OP_RLWINMX => {
+ let r = ctx.cpu.read_gpr(instr.rs()).rotate_left(instr.sh() as u32);
+ let m = ((1u32.overflowing_shl(32 - instr.mb() as u32).0) - 1)
+ & !((1u32.overflowing_shl(31 - instr.me() as u32).0) - 1);
+ ctx.cpu.write_gpr(instr.ra(), r & m);
+ }
+ _ => todo!("Rotate instruction with OP = {OP:#x}"),
+ }
+}
+
pub fn msr<const OP: u32>(
_ctx: &mut crate::gekko::Gekko,
_instr: crate::cpu::semantics::Instruction,
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index 7d43643..bc615f4 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -13,7 +13,7 @@ impl Gekko {
pub fn new(path: &str) -> Self {
let mut mmu = mmu::Mmu::new();
let data = std::fs::read(path).expect("failed to read ROM");
- mmu.ram[..data.len()].copy_from_slice(&data);
+ mmu.ram[0x3F00..0x3F00 + data.len()].copy_from_slice(&data);
Gekko {
cpu: cpu::Cpu::new(),
diff --git a/crates/gekko/src/mmu.rs b/crates/gekko/src/mmu.rs
index 791ed4c..67bfc5b 100644
--- a/crates/gekko/src/mmu.rs
+++ b/crates/gekko/src/mmu.rs
@@ -30,7 +30,10 @@ impl Mmu {
(&self.hwr, (phys - HW_REG_BASE) as usize)
}
_ => {
- tracing::error!(phys_addr = format!("{:08X}", phys), "unmapped physical read");
+ tracing::error!(
+ phys_addr = format!("{:08X}", phys),
+ "unmapped physical read"
+ );
(&self.ram, 0)
}
}
@@ -48,7 +51,10 @@ impl Mmu {
(&mut self.hwr, (phys - HW_REG_BASE) as usize)
}
_ => {
- tracing::error!(phys_addr = format!("{:08X}", phys), "unmapped physical write");
+ tracing::error!(
+ phys_addr = format!("{:08X}", phys),
+ "unmapped physical write"
+ );
(&mut self.ram, 0)
}
}
@@ -56,33 +62,65 @@ impl Mmu {
pub fn phys_read_u8(&self, addr: u32) -> u8 {
let (slice, offset) = self.resolve(addr);
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:02X}", slice[offset]),
+ "read_u8"
+ );
slice[offset]
}
pub fn phys_read_u16(&self, addr: u32) -> u16 {
let (slice, offset) = self.resolve(addr);
- u16::from_be_bytes(slice[offset..offset + 2].try_into().unwrap())
+ let value = u16::from_be_bytes(slice[offset..offset + 2].try_into().unwrap());
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:04X}", value),
+ "read_u16"
+ );
+ value
}
pub fn phys_read_u32(&self, addr: u32) -> u32 {
let (slice, offset) = self.resolve(addr);
- u32::from_be_bytes(slice[offset..offset + 4].try_into().unwrap())
+ let value = u32::from_be_bytes(slice[offset..offset + 4].try_into().unwrap());
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:08X}", value),
+ "read_u32"
+ );
+ value
}
pub fn phys_write_u8(&mut self, addr: u32, value: u8) {
let (slice, offset) = self.resolve_mut(addr);
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:02X}", value),
+ "write_u8"
+ );
slice[offset] = value;
}
pub fn phys_write_u16(&mut self, addr: u32, value: u16) {
let (slice, offset) = self.resolve_mut(addr);
let bytes = value.to_be_bytes();
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:04X}", value),
+ "write_u16"
+ );
slice[offset..offset + 2].copy_from_slice(&bytes);
}
pub fn phys_write_u32(&mut self, addr: u32, value: u32) {
let (slice, offset) = self.resolve_mut(addr);
let bytes = value.to_be_bytes();
+ tracing::trace!(
+ phys_addr = format!("{:08X}", addr),
+ value = format!("{:08X}", value),
+ "write_u32"
+ );
slice[offset..offset + 4].copy_from_slice(&bytes);
}
@@ -125,6 +163,7 @@ impl Mmu {
// Simple virtual to physical translation that ignores caching and other MMU features
pub fn virt_to_phys(addr: u32) -> u32 {
+ tracing::trace!(virt_addr = format!("{:08X}", addr), "virt_to_phys");
addr & 0x3FFFFFFF
}
}
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 908a5f8..c592d61 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -27,7 +27,12 @@ fn main() {
loop {
if !is_busyloop {
let instr = GekkoInstruction::decode(gekko.mmu.virt_slice(gekko.cpu.pc, 4))
- .expect("failed to decode instruction")
+ .unwrap_or_else(|| {
+ dump_registers(&prev_snapshot, &prev_snapshot);
+ dump_memory(&gekko.mmu, gekko.cpu.read_gpr(1));
+
+ panic!("Failed to decode instruction at {:08X}", gekko.cpu.pc)
+ })
.0;
if is_debug {
@@ -142,3 +147,23 @@ fn dump_registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
println!();
}
+
+fn dump_memory(mmu: &gekko::mmu::Mmu, addr: u32) {
+ let aligned_addr = addr & !0xF;
+ let start = aligned_addr.wrapping_sub(0x40);
+ let data = mmu.virt_slice(start, 0x80);
+
+ for (i, line) in data.chunks(16).enumerate() {
+ let line_addr = start.wrapping_add((i as u32) * 16);
+ let hex = line
+ .chunks(4)
+ .map(|chunk| {
+ let word = u32::from_be_bytes(chunk.try_into().unwrap());
+ format!("{:08X}", word)
+ })
+ .collect::<Vec<_>>()
+ .join(" ");
+
+ println!("{} {}", format!("{:08X}:", line_addr).blue().bold(), hex);
+ }
+}
diff --git a/rsrc/roms/panda.dol.i64 b/rsrc/roms/panda.dol.i64
index 1901cb6..ae98d8f 100644
Binary files a/rsrc/roms/panda.dol.i64 and b/rsrc/roms/panda.dol.i64 differ