Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

use chipi generated newtype. detect busyloops in tinyemu

Layle7160027fdac5
diff --git a/Cargo.lock b/Cargo.lock
index dbe4caf..dcbfd24 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -73,6 +73,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0b1c8f88248e1712bda8e8b64ae1359bb01e953abd5f76c53ddabf32be88fa1"
+[[package]]
+name = "chipi"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "804b48113bfcabe42a604d0db98786210fe860c0b7ced8b9bb85aa7c74f86b85"
+
[[package]]
name = "clap"
version = "4.5.59"
@@ -132,7 +138,7 @@ dependencies = [
name = "disasm"
version = "0.1.0"
dependencies = [
- "chipi",
+ "chipi 0.4.0",
]
[[package]]
@@ -147,7 +153,7 @@ dependencies = [
name = "gekko"
version = "0.1.0"
dependencies = [
- "chipi",
+ "chipi 0.5.1",
"tracing",
]
diff --git a/crates/gekko/Cargo.toml b/crates/gekko/Cargo.toml
index 1625bbb..fab89e6 100644
--- a/crates/gekko/Cargo.toml
+++ b/crates/gekko/Cargo.toml
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[build-dependencies]
-chipi = "0.4.0"
+chipi = "0.5.1"
[dependencies]
tracing = "0.1.44"
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index bc71556..05a740e 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -17,6 +17,11 @@ fn main() {
.group("store_load", ["stw", "stwu", "sth", "sthu", "lwz", "lwzu"])
.group("compare", ["cmpi"]);
+ // Generate the instruction type with accessor methods
+ builder
+ .build_instr_type(out_dir.join("gekko_instr.rs").to_str().unwrap())
+ .expect("failed to generate instruction type");
+
// Always regenerate the LUT dispatch tables
builder
.build_lut(out_dir.join("gekko_lut.rs").to_str().unwrap())
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index 9413934..db52675 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -35,4 +35,14 @@ impl Cpu {
xer: 0,
}
}
+
+ #[inline(always)]
+ pub fn read_gpr(&self, index: u8) -> u32 {
+ self.gprs[index as usize]
+ }
+
+ #[inline(always)]
+ pub fn write_gpr(&mut self, index: u8, value: u32) {
+ self.gprs[index as usize] = value;
+ }
}
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 0d8c81c..5f8cc58 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -58,23 +58,31 @@ pub fn alu<const OP: u32>(
let ra = if instr.ra() == 0 {
0
} else {
- ctx.cpu.gprs[instr.ra()]
+ ctx.cpu.read_gpr(instr.ra())
};
- ctx.cpu.gprs[instr.rd()] = ra.wrapping_add_signed(instr.simm());
+ ctx.cpu
+ .write_gpr(instr.rd(), ra.wrapping_add_signed(instr.simm()));
}
crate::cpu::lut::OP_ADDIS => {
let ra = if instr.ra() == 0 {
0
} else {
- ctx.cpu.gprs[instr.ra()]
+ ctx.cpu.read_gpr(instr.ra())
};
- ctx.cpu.gprs[instr.rd()] = ra.wrapping_add_signed(instr.simm() << 16);
+ ctx.cpu
+ .write_gpr(instr.rd(), ra.wrapping_add_signed(instr.simm() << 16));
}
crate::cpu::lut::OP_ORI => {
- ctx.cpu.gprs[instr.ra()] = ctx.cpu.gprs[instr.rs()] | instr.uimm();
+ ctx.cpu.write_gpr(
+ instr.ra(),
+ ctx.cpu.read_gpr(instr.rs()) | instr.uimm() as u32,
+ );
}
crate::cpu::lut::OP_ORIS => {
- ctx.cpu.gprs[instr.ra()] = ctx.cpu.gprs[instr.rs()] | (instr.uimm() << 16);
+ ctx.cpu.write_gpr(
+ instr.ra(),
+ ctx.cpu.read_gpr(instr.rs()) | ((instr.uimm() as u32) << 16),
+ );
}
_ => todo!("ALU instruction with OP = {OP:#x}"),
}
@@ -100,16 +108,16 @@ pub fn spr<const OP: u32>(
instr: crate::cpu::semantics::Instruction,
) {
match OP {
- crate::cpu::lut::OP_MTSPR => match instr.spr() {
- 1 => ctx.cpu.xer = ctx.cpu.gprs[instr.rs()],
- 8 => ctx.cpu.lr = ctx.cpu.gprs[instr.rs()],
- 9 => ctx.cpu.ctr = ctx.cpu.gprs[instr.rs()],
+ crate::cpu::lut::OP_MTSPR => match instr.spr_swapped() {
+ 1 => ctx.cpu.xer = ctx.cpu.read_gpr(instr.rs()),
+ 8 => ctx.cpu.lr = ctx.cpu.read_gpr(instr.rs()),
+ 9 => ctx.cpu.ctr = ctx.cpu.read_gpr(instr.rs()),
_ => todo!("unimplemented SPR number {}", instr.spr()),
},
- crate::cpu::lut::OP_MFSPR => match instr.spr() {
- 1 => ctx.cpu.gprs[instr.rd()] = ctx.cpu.xer,
- 8 => ctx.cpu.gprs[instr.rd()] = ctx.cpu.lr,
- 9 => ctx.cpu.gprs[instr.rd()] = ctx.cpu.ctr,
+ crate::cpu::lut::OP_MFSPR => match instr.spr_swapped() {
+ 1 => ctx.cpu.write_gpr(instr.rd(), ctx.cpu.xer),
+ 8 => ctx.cpu.write_gpr(instr.rd(), ctx.cpu.lr),
+ 9 => ctx.cpu.write_gpr(instr.rd(), ctx.cpu.ctr),
_ => todo!("unimplemented SPR number {}", instr.spr()),
},
_ => todo!("SPR instruction with OP = {OP:#x}"),
@@ -122,25 +130,34 @@ pub fn store_load<const OP: u32>(
) {
match OP {
crate::cpu::lut::OP_STW | crate::cpu::lut::OP_STWU => {
- let addr = ctx.cpu.gprs[instr.ra()].wrapping_add_signed(instr.disp());
- ctx.mmu.virt_write_u32(addr, ctx.cpu.gprs[instr.rs()]);
+ let addr = ctx
+ .cpu
+ .read_gpr(instr.ra())
+ .wrapping_add_signed(instr.disp());
+ ctx.mmu.virt_write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
if OP == crate::cpu::lut::OP_STWU {
- ctx.cpu.gprs[instr.ra()] = addr;
+ ctx.cpu.write_gpr(instr.ra(), addr);
}
}
crate::cpu::lut::OP_STH | crate::cpu::lut::OP_STHU => {
- let addr = ctx.cpu.gprs[instr.ra()].wrapping_add_signed(instr.disp());
+ let addr = ctx
+ .cpu
+ .read_gpr(instr.ra())
+ .wrapping_add_signed(instr.disp());
ctx.mmu
- .virt_write_u16(addr, (ctx.cpu.gprs[instr.rs()] & 0xffff) as u16);
+ .virt_write_u16(addr, (ctx.cpu.read_gpr(instr.rs()) & 0xffff) as u16);
if OP == crate::cpu::lut::OP_STHU {
- ctx.cpu.gprs[instr.ra()] = addr;
+ ctx.cpu.write_gpr(instr.ra(), addr);
}
}
crate::cpu::lut::OP_LWZ | crate::cpu::lut::OP_LWZU => {
- let addr = ctx.cpu.gprs[instr.ra()].wrapping_add_signed(instr.disp());
- ctx.cpu.gprs[instr.rd()] = ctx.mmu.virt_read_u32(addr);
+ let addr = ctx
+ .cpu
+ .read_gpr(instr.ra())
+ .wrapping_add_signed(instr.disp());
+ ctx.cpu.write_gpr(instr.rd(), ctx.mmu.virt_read_u32(addr));
if OP == crate::cpu::lut::OP_LWZU {
- ctx.cpu.gprs[instr.ra()] = addr;
+ ctx.cpu.write_gpr(instr.ra(), addr);
}
}
_ => todo!("Store/Load instruction with OP = {OP:#x}"),
@@ -153,8 +170,8 @@ pub fn compare<const OP: u32>(
) {
match OP {
crate::cpu::lut::OP_CMPI => {
- let result = (ctx.cpu.gprs[instr.ra()] as i32).cmp(&instr.simm());
- // ctx.cpu.cr[instr.bi() as usize] = match result {
+ let _result = (ctx.cpu.read_gpr(instr.ra()) as i32).cmp(&instr.simm());
+ // ctx.cpu.cr[instr.bi() as usize] = match _result {
// std::cmp::Ordering::Less => 0b100,
// std::cmp::Ordering::Equal => 0b010,
// std::cmp::Ordering::Greater => 0b001,
diff --git a/crates/gekko/src/cpu/semantics.rs b/crates/gekko/src/cpu/semantics.rs
index 2c49c55..d3fbd56 100644
--- a/crates/gekko/src/cpu/semantics.rs
+++ b/crates/gekko/src/cpu/semantics.rs
@@ -1,37 +1,21 @@
-pub struct Instruction(pub u32);
+include!(concat!(env!("OUT_DIR"), "/gekko_instr.rs"));
-#[rustfmt::skip]
impl Instruction {
- #[inline] pub fn primary(&self) -> u32 { self.0 >> 26 }
- #[inline] pub fn xo10(&self) -> u32 { (self.0 >> 1) & 0x3ff }
- #[inline] pub fn xo9(&self) -> u32 { (self.0 >> 1) & 0x1ff }
- #[inline] pub fn xo5(&self) -> u32 { (self.0 >> 1) & 0x1f }
+ #[inline]
+ pub fn disp(&self) -> i32 {
+ self.d_15_0()
+ }
- #[inline] pub fn rd(&self) -> usize { ((self.0 >> 21) & 0x1f) as usize }
- #[inline] pub fn rs(&self) -> usize { ((self.0 >> 21) & 0x1f) as usize }
- #[inline] pub fn fd(&self) -> usize { ((self.0 >> 21) & 0x1f) as usize }
- #[inline] pub fn fs(&self) -> usize { ((self.0 >> 21) & 0x1f) as usize }
- #[inline] pub fn ra(&self) -> usize { ((self.0 >> 16) & 0x1f) as usize }
- #[inline] pub fn rb(&self) -> usize { ((self.0 >> 11) & 0x1f) as usize }
- #[inline] pub fn fc(&self) -> usize { ((self.0 >> 6) & 0x1f) as usize }
+ #[inline]
+ pub fn d_psq(&self) -> i32 {
+ self.d_11_0()
+ }
- #[inline] pub fn rc(&self) -> bool { self.0 & 1 != 0 }
- #[inline] pub fn lk(&self) -> bool { self.0 & 1 != 0 }
- #[inline] pub fn aa(&self) -> bool { (self.0 >> 1) & 1 != 0 }
- #[inline] pub fn oe(&self) -> bool { (self.0 >> 10) & 1 != 0 }
-
- #[inline] pub fn simm(&self) -> i32 { (self.0 as i16) as i32 }
- #[inline] pub fn disp(&self) -> i32 { (self.0 as i16) as i32 }
- #[inline] pub fn uimm(&self) -> u32 { self.0 & 0xffff }
- #[inline] pub fn sh(&self) -> u32 { (self.0 >> 11) & 0x1f }
- #[inline] pub fn mb(&self) -> u32 { (self.0 >> 6) & 0x1f }
- #[inline] pub fn me(&self) -> u32 { (self.0 >> 1) & 0x1f }
-
- #[inline] pub fn li(&self) -> i32 { (((self.0 & 0x03ff_fffc) as i32) << 6) >> 6 }
- #[inline] pub fn bd(&self) -> i32 { (((self.0 & 0x0000_fffc) as i32) << 16) >> 16 }
-
- #[inline] pub fn spr(&self) -> u32 { let raw = (self.0 >> 11) & 0x3ff; (raw >> 5) | ((raw & 0x1f) << 5) }
-
- #[inline] pub fn bo(&self) -> u8 { ((self.0 >> 21) & 0x1f) as u8 }
- #[inline] pub fn bi(&self) -> u8 { ((self.0 >> 16) & 0x1f) as u8 }
+ /// The SPR field in PowerPC instructions has a special encoding where
+ /// the two 5-bit halves are swapped. This method returns the decoded value
+ #[inline]
+ pub fn spr_swapped(&self) -> u32 {
+ let raw = self.spr() as u32;
+ (raw >> 5) | ((raw & 0x1f) << 5)
+ }
}
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 76b2c3f..80412f4 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -21,37 +21,53 @@ fn main() {
let mut gekko = gekko::gekko::Gekko::new(&path);
let mut prev_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
+ let mut current_addr = gekko.cpu.pc;
+ let mut is_busyloop = false;
loop {
- let instr = GekkoInstruction::decode(gekko.mmu.virt_slice(gekko.cpu.pc, 4))
- .expect("failed to decode instruction")
- .0;
-
- if is_debug {
- dbg!(&instr);
+ if !is_busyloop {
+ let instr = GekkoInstruction::decode(gekko.mmu.virt_slice(gekko.cpu.pc, 4))
+ .expect("failed to decode instruction")
+ .0;
+
+ if is_debug {
+ dbg!(&instr);
+ }
+
+ let refs = fmt::gpr_refs(&instr);
+ let comment = fmt::reg_comment(&prev_snapshot.gprs, &refs);
+
+ let prefix = format!(
+ "{}: {}",
+ format!("{:08X}", gekko.cpu.pc).bold(),
+ fmt::colorize_instr(&instr)
+ );
+ const COMMENT_COL: usize = 50;
+ let pad = COMMENT_COL.saturating_sub(fmt::visible_len(&prefix));
+
+ if comment.is_empty() {
+ println!("{}", prefix);
+ } else {
+ println!("{}{}{}", prefix, " ".repeat(pad), comment);
+ }
}
- let refs = fmt::gpr_refs(&instr);
- let comment = fmt::reg_comment(&prev_snapshot.gprs, &refs);
+ gekko.run_until_event();
- let prefix = format!(
- "{}: {}",
- format!("{:08X}", gekko.cpu.pc).bold(),
- fmt::colorize_instr(&instr)
- );
- const COMMENT_COL: usize = 50;
- let pad = COMMENT_COL.saturating_sub(fmt::visible_len(&prefix));
+ let curr_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
- if comment.is_empty() {
- println!("{}", prefix);
+ if current_addr == gekko.cpu.pc {
+ if !is_busyloop {
+ println!("{}", "Busyloop detected!".bright_red().bold());
+ is_busyloop = true;
+ }
} else {
- println!("{}{}{}", prefix, " ".repeat(pad), comment);
+ is_busyloop = false;
}
- gekko.run_until_event();
- let curr_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
+ current_addr = gekko.cpu.pc;
- if is_debug {
+ if is_debug && !is_busyloop {
dump_registers(&curr_snapshot, &prev_snapshot);
}