Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

basic CR implementation

Layle0eb2dfbd170b
diff --git a/Cargo.lock b/Cargo.lock
index dcbfd24..8f4d79f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -67,6 +67,22 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+[[package]]
+name = "chapa"
+version = "0.1.0"
+dependencies = [
+ "chapa-macros",
+]
+
+[[package]]
+name = "chapa-macros"
+version = "0.1.0"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "chipi"
version = "0.4.0"
@@ -153,6 +169,7 @@ dependencies = [
name = "gekko"
version = "0.1.0"
dependencies = [
+ "chapa",
"chipi 0.5.1",
"tracing",
]
diff --git a/crates/gekko/Cargo.toml b/crates/gekko/Cargo.toml
index fab89e6..984489f 100644
--- a/crates/gekko/Cargo.toml
+++ b/crates/gekko/Cargo.toml
@@ -7,4 +7,5 @@ edition = "2024"
chipi = "0.5.1"
[dependencies]
+chapa = { path = "../../../chapa/chapa" }
tracing = "0.1.44"
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index 05a740e..1408a0b 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -15,7 +15,7 @@ fn main() {
.group("msr", ["mtmsr", "mfmsr"])
.group("spr", ["mtspr", "mfspr"])
.group("store_load", ["stw", "stwu", "sth", "sthu", "lwz", "lwzu"])
- .group("compare", ["cmpi"]);
+ .group("compare", ["cmp", "cmpi"]);
// Generate the instruction type with accessor methods
builder
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index db52675..fc43484 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -1,6 +1,8 @@
+use crate::cpu::condition::ConditionRegister;
+
pub mod interpreter;
pub mod semantics;
-pub mod branch;
+pub mod condition;
#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]
pub mod lut {
@@ -14,6 +16,7 @@ pub struct Cpu {
pub lr: u32,
pub ctr: u32,
pub xer: u32,
+ pub cr: ConditionRegister,
// These are used during instruction execution to track the current
// and next PC values. In essence, by writing to `next_pc`, instructions
@@ -33,6 +36,7 @@ impl Cpu {
lr: 0,
ctr: 0,
xer: 0,
+ cr: ConditionRegister::new(),
}
}
diff --git a/crates/gekko/src/cpu/branch.rs b/crates/gekko/src/cpu/condition.rs
similarity index 55%
rename from crates/gekko/src/cpu/branch.rs
rename to crates/gekko/src/cpu/condition.rs
index 3892a8c..3daa2b6 100644
--- a/crates/gekko/src/cpu/branch.rs
+++ b/crates/gekko/src/cpu/condition.rs
@@ -55,3 +55,61 @@ impl BranchControl {
)
}
}
+
+#[chapa::bitfield(u8, width = 4, order = msb0)]
+#[derive(Clone, Copy)]
+pub struct ConditionField {
+ #[bits(0, alias = ["less_than", "fx", "fp_exception"])]
+ pub lt: bool,
+ #[bits(1, alias = ["greater_than", "fex", "fp_enabled_exception"])]
+ pub gt: bool,
+ #[bits(2, alias = ["equal", "vx", "fp_invalid_exception"])]
+ pub eq: bool,
+ #[bits(3, alias = ["summary_overflow", "ox", "fp_overflow_exception"])]
+ pub so: bool,
+}
+
+#[chapa::bitfield(u32, order = msb0)]
+#[derive(Clone, Copy)]
+pub struct ConditionRegister {
+ #[bits(0..=3)] pub cr0: ConditionField,
+ #[bits(4..=7)] pub cr1: ConditionField,
+ #[bits(8..=11)] pub cr2: ConditionField,
+ #[bits(12..=15)] pub cr3: ConditionField,
+ #[bits(16..=19)] pub cr4: ConditionField,
+ #[bits(20..=23)] pub cr5: ConditionField,
+ #[bits(24..=27)] pub cr6: ConditionField,
+ #[bits(28..=31)] pub cr7: ConditionField,
+}
+
+impl ConditionRegister {
+ #[inline]
+ pub fn set_field(&mut self, index: u8, value: ConditionField) {
+ match index {
+ 0 => self.set_cr0(value),
+ 1 => self.set_cr1(value),
+ 2 => self.set_cr2(value),
+ 3 => self.set_cr3(value),
+ 4 => self.set_cr4(value),
+ 5 => self.set_cr5(value),
+ 6 => self.set_cr6(value),
+ 7 => self.set_cr7(value),
+ _ => panic!("Invalid CR field index: {}", index),
+ }
+ }
+
+ #[inline]
+ pub fn get_field(&self, index: u8) -> ConditionField {
+ match index {
+ 0 => self.cr0(),
+ 1 => self.cr1(),
+ 2 => self.cr2(),
+ 3 => self.cr3(),
+ 4 => self.cr4(),
+ 5 => self.cr5(),
+ 6 => self.cr6(),
+ 7 => self.cr7(),
+ _ => panic!("Invalid CR field index: {}", index),
+ }
+ }
+}
\ No newline at end of file
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 5f8cc58..c7509bf 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -1,4 +1,4 @@
-use crate::cpu::branch::BranchControl;
+use crate::cpu::condition::{self, BranchControl, ConditionField};
pub fn branch<const OP: u32>(
ctx: &mut crate::gekko::Gekko,
@@ -24,8 +24,15 @@ pub fn branch<const OP: u32>(
ctx.cpu.ctr = ctx.cpu.ctr.wrapping_sub(1);
}
- // TODO: cond missing
- if !ctrl.should_branch(ctx.cpu.ctr, true) {
+ let (crf, bit) = (instr.bi() >> 2, instr.bi() & 0b11);
+ let condition = match bit {
+ 0 => ctx.cpu.cr.get_field(crf).lt(),
+ 1 => ctx.cpu.cr.get_field(crf).gt(),
+ 2 => ctx.cpu.cr.get_field(crf).eq(),
+ 3 => ctx.cpu.cr.get_field(crf).so(),
+ _ => panic!("Invalid CR bit index: {}", bit),
+ };
+ if !ctrl.should_branch(ctx.cpu.ctr, condition) {
return;
}
@@ -168,17 +175,23 @@ pub fn compare<const OP: u32>(
ctx: &mut crate::gekko::Gekko,
instr: crate::cpu::semantics::Instruction,
) {
- match OP {
- crate::cpu::lut::OP_CMPI => {
- 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,
- // };
- }
+ let (a, b) = match OP {
+ crate::cpu::lut::OP_CMP => (
+ ctx.cpu.read_gpr(instr.ra()) as i32,
+ ctx.cpu.read_gpr(instr.rb()) as i32,
+ ),
+ crate::cpu::lut::OP_CMPI => (ctx.cpu.read_gpr(instr.ra()) as i32, instr.simm()),
_ => todo!("Compare instruction with OP = {OP:#x}"),
- }
+ };
+
+ ctx.cpu.cr.set_field(
+ instr.crfd(),
+ match a.cmp(&b) {
+ std::cmp::Ordering::Less => ConditionField::new().with_lt(true),
+ std::cmp::Ordering::Equal => ConditionField::new().with_eq(true),
+ std::cmp::Ordering::Greater => ConditionField::new().with_gt(true),
+ },
+ );
}
#[rustfmt::skip] pub fn twi(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("twi") }
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 80412f4..908a5f8 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -105,5 +105,40 @@ fn dump_registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
.trim_end()
);
+ let cr_fields = [
+ ("cr0", curr.cr.cr0(), prev.cr.cr0()),
+ ("cr1", curr.cr.cr1(), prev.cr.cr1()),
+ ("cr2", curr.cr.cr2(), prev.cr.cr2()),
+ ("cr3", curr.cr.cr3(), prev.cr.cr3()),
+ ("cr4", curr.cr.cr4(), prev.cr.cr4()),
+ ("cr5", curr.cr.cr5(), prev.cr.cr5()),
+ ("cr6", curr.cr.cr6(), prev.cr.cr6()),
+ ("cr7", curr.cr.cr7(), prev.cr.cr7()),
+ ];
+
+ let fmt_cr_field = |label: &str,
+ val: gekko::cpu::condition::ConditionField,
+ prev_val: gekko::cpu::condition::ConditionField| {
+ let flags = format!(
+ "{}{}{}{}",
+ if val.lt() { "L" } else { "·" },
+ if val.gt() { "G" } else { "·" },
+ if val.eq() { "Z" } else { "·" },
+ if val.so() { "O" } else { "·" },
+ );
+ let text = format!("{}[{}] ", label, flags);
+ if val.raw() != prev_val.raw() {
+ format!("{}", text.bright_red().bold())
+ } else {
+ format!("{}", text.dimmed())
+ }
+ };
+
+ let cr_line: String = cr_fields
+ .iter()
+ .map(|(label, val, prev_val)| fmt_cr_field(label, *val, *prev_val))
+ .collect();
+ println!("{}", cr_line.trim_end());
+
println!();
}
diff --git a/crates/tinyemu/src/snaptshot.rs b/crates/tinyemu/src/snaptshot.rs
index 80f2e08..4dda481 100644
--- a/crates/tinyemu/src/snaptshot.rs
+++ b/crates/tinyemu/src/snaptshot.rs
@@ -1,8 +1,11 @@
+use gekko::cpu::condition::ConditionRegister;
+
#[derive(Clone, Copy)]
pub struct CpuSnapshot {
pub gprs: [u32; 32],
pub lr: u32,
pub ctr: u32,
+ pub cr: ConditionRegister,
}
impl CpuSnapshot {
@@ -11,6 +14,7 @@ impl CpuSnapshot {
gprs: cpu.gprs,
lr: cpu.lr,
ctr: cpu.ctr,
+ cr: cpu.cr,
}
}
}
\ No newline at end of file