Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

rfi + updated chapa to use bit ops traits

Layle0af463317a75
diff --git a/Cargo.lock b/Cargo.lock
index 09e22d5..d11526b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -82,7 +82,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chapa"
version = "0.1.0"
-source = "git+http://github.com/ioncodes/chapa#ef09ed9685421632f87fd851f6d5de1866d6c026"
+source = "git+https://github.com/ioncodes/chapa#3eb2346560b9dc6bb8ca875b9158924c8d9f5105"
dependencies = [
"chapa-macros",
]
@@ -90,7 +90,7 @@ dependencies = [
[[package]]
name = "chapa-macros"
version = "0.1.0"
-source = "git+http://github.com/ioncodes/chapa#ef09ed9685421632f87fd851f6d5de1866d6c026"
+source = "git+https://github.com/ioncodes/chapa#3eb2346560b9dc6bb8ca875b9158924c8d9f5105"
dependencies = [
"proc-macro2",
"quote",
diff --git a/crates/gekko/Cargo.toml b/crates/gekko/Cargo.toml
index 2e4b877..8fb6571 100644
--- a/crates/gekko/Cargo.toml
+++ b/crates/gekko/Cargo.toml
@@ -7,6 +7,6 @@ edition = "2024"
chipi = "0.5.1"
[dependencies]
-chapa = { git = "http://github.com/ioncodes/chapa" }
+chapa = { git = "https://github.com/ioncodes/chapa" }
tracing = "0.1.44"
image = { path = "../image" }
\ No newline at end of file
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index beee309..a9b4724 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -13,7 +13,7 @@ fn main() {
.group("branch", ["bx", "bcx", "bclrx"])
.group("alu", ["ori", "oris", "addx", "addi", "addis"])
.group("rotate", ["rlwinmx"])
- .group("msr", ["mtmsr", "mfmsr"])
+ .group("msr", ["mtmsr", "mfmsr", "rfi"])
.group("spr", ["mtspr", "mfspr"])
.group("store_load", ["stw", "stwu", "sth", "sthu", "lwz", "lwzu"])
.group("compare", ["cmp", "cmpi"]);
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index 7ea48b8..641df4c 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -1,8 +1,10 @@
-use crate::cpu::condition::ConditionRegister;
-
pub mod interpreter;
pub mod semantics;
pub mod condition;
+pub mod spr;
+pub mod msr;
+
+use crate::cpu::condition::ConditionRegister;
#[allow(dead_code, unused_variables, non_upper_case_globals, clippy::all)]
pub mod lut {
@@ -13,11 +15,9 @@ pub struct Cpu {
pub gprs: [u32; 32],
pub fprs: [f64; 32],
pub pc: u32,
- pub lr: u32,
- pub ctr: u32,
- pub xer: u32,
pub cr: ConditionRegister,
-
+ pub spr: spr::Spr,
+ pub msr: msr::Msr,
// These are used during instruction execution to track the current
// and next PC values. In essence, by writing to `next_pc`, instructions
// can change the flow of execution (e.g. for branches and jumps).
@@ -33,10 +33,9 @@ impl Cpu {
pc: initial_pc,
cia: initial_pc,
nia: initial_pc.wrapping_add(4),
- lr: 0,
- ctr: 0,
- xer: 0,
cr: ConditionRegister::new(),
+ spr: spr::Spr::default(),
+ msr: msr::Msr::default(),
}
}
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index c974c81..88f89df 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -5,7 +5,7 @@ pub fn branch<const OP: u32>(
instr: crate::cpu::semantics::Instruction,
) {
if instr.lk() {
- ctx.cpu.lr = ctx.cpu.cia.wrapping_add(4);
+ ctx.cpu.spr.lr = ctx.cpu.cia.wrapping_add(4);
}
match OP {
@@ -21,7 +21,7 @@ pub fn branch<const OP: u32>(
tracing::trace!("Branch control: {ctrl:?}");
if ctrl.should_decrement_ctr() {
- ctx.cpu.ctr = ctx.cpu.ctr.wrapping_sub(1);
+ ctx.cpu.spr.ctr = ctx.cpu.spr.ctr.wrapping_sub(1);
}
let (crf, bit) = (instr.bi() >> 2, instr.bi() & 0b11);
@@ -32,13 +32,13 @@ pub fn branch<const OP: u32>(
3 => ctx.cpu.cr.get_field(crf).so(),
_ => panic!("Invalid CR bit index: {}", bit),
};
- if !ctrl.should_branch(ctx.cpu.ctr, condition) {
+ if !ctrl.should_branch(ctx.cpu.spr.ctr, condition) {
return;
}
match OP {
crate::cpu::lut::OP_BCLRX => {
- ctx.cpu.nia = ctx.cpu.lr;
+ ctx.cpu.nia = ctx.cpu.spr.lr;
}
crate::cpu::lut::OP_BCX => {
ctx.cpu.nia = if instr.aa() {
@@ -133,7 +133,7 @@ pub fn rotate<const OP: u32>(
}
pub fn msr<const OP: u32>(
- _ctx: &mut crate::gekko::Gekko,
+ ctx: &mut crate::gekko::Gekko,
_instr: crate::cpu::semantics::Instruction,
) {
match OP {
@@ -143,6 +143,15 @@ pub fn msr<const OP: u32>(
crate::cpu::lut::OP_MFMSR => {
tracing::error!("OP_MFMSR!!");
}
+ crate::cpu::lut::OP_RFI => {
+ // "Bits SRR1[16–23, 25–27, 30–31] are placed into the corresponding bits of the MSR."
+ const RFI_MSR_MASK: u32 = 0x0000_FF73;
+ ctx.cpu.msr = (ctx.cpu.msr & !RFI_MSR_MASK) | (ctx.cpu.spr.srr1 & RFI_MSR_MASK);
+ // "If the new MSR value does not enable any pending exceptions, then the
+ // next instruction is fetched, under control of the new MSR value,
+ // from the address SRR0[0–29] || 0b00"
+ ctx.cpu.pc = ctx.cpu.spr.srr0.value() << 2;
+ }
_ => todo!("MSR instruction with OP = {OP:#x}"),
}
}
@@ -152,18 +161,13 @@ pub fn spr<const OP: u32>(
instr: crate::cpu::semantics::Instruction,
) {
match OP {
- 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_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()),
- },
+ crate::cpu::lut::OP_MTSPR => ctx
+ .cpu
+ .spr
+ .write(instr.spr_swapped(), ctx.cpu.read_gpr(instr.rs())),
+ crate::cpu::lut::OP_MFSPR => ctx
+ .cpu
+ .write_gpr(instr.rd(), ctx.cpu.spr.read(instr.spr_swapped())),
_ => todo!("SPR instruction with OP = {OP:#x}"),
}
}
diff --git a/crates/gekko/src/cpu/msr.rs b/crates/gekko/src/cpu/msr.rs
new file mode 100644
index 0000000..301472d
--- /dev/null
+++ b/crates/gekko/src/cpu/msr.rs
@@ -0,0 +1,17 @@
+#[chapa::bitfield(u32, width = 32, order = msb0)]
+#[derive(Clone, Copy, Default)]
+pub struct Msr {
+ #[bits(13, alias = "pow")] pub power_management: bool,
+ #[bits(15, alias = "ile")] pub exception_little_endian: bool,
+ #[bits(16, alias = "ee")] pub external_interrupt_enable: bool,
+ #[bits(17, alias = "pr")] pub privilege_level: bool,
+ #[bits(18, alias = "fp")] pub floating_point_available: bool,
+ #[bits(19, alias = "me")] pub machine_check_enable: bool,
+ #[bits(20, alias = "fe0")] pub floating_point_exception_mode_0: bool,
+ #[bits(21, alias = "se")] pub single_step_trace: bool,
+ #[bits(22, alias = "be")] pub branch_trace: bool,
+ #[bits(23, alias = "fe1")] pub floating_point_exception_mode_1: bool,
+ #[bits(25, alias = "ip")] pub exception_prefix: bool,
+ #[bits(26, alias = "ir")] pub instruction_address_translation: bool,
+ #[bits(27, alias = "dr")] pub data_address_translation: bool,
+}
\ No newline at end of file
diff --git a/crates/gekko/src/cpu/spr.rs b/crates/gekko/src/cpu/spr.rs
new file mode 100644
index 0000000..8b6303d
--- /dev/null
+++ b/crates/gekko/src/cpu/spr.rs
@@ -0,0 +1,120 @@
+#[chapa::bitfield(u32, width = 32, order = msb0)]
+#[derive(Clone, Copy, Default)]
+pub struct Srr0 {
+ #[bits(0..=29)]
+ pub value: u32, // low 2 bits are always 0
+}
+
+#[derive(Default)]
+pub struct Spr {
+ pub xer: u32,
+ pub lr: u32,
+ pub ctr: u32,
+ pub dsisr: u32,
+ pub dar: u32,
+ pub dec: u32,
+ pub sdr1: u32,
+ pub srr0: Srr0,
+ pub srr1: u32,
+ pub sprg0: u32,
+ pub sprg1: u32,
+ pub sprg2: u32,
+ pub sprg3: u32,
+ pub ear: u32,
+ pub pvr: u32,
+ pub ibat0u: u32,
+ pub ibat0l: u32,
+ pub ibat1u: u32,
+ pub ibat1l: u32,
+ pub ibat2u: u32,
+ pub ibat2l: u32,
+ pub ibat3u: u32,
+ pub ibat3l: u32,
+ pub dbat0u: u32,
+ pub dbat0l: u32,
+ pub dbat1u: u32,
+ pub dbat1l: u32,
+ pub dbat2u: u32,
+ pub dbat2l: u32,
+ pub dbat3u: u32,
+ pub dbat3l: u32,
+ pub dabr: u32,
+}
+
+impl Spr {
+ pub fn read(&self, spr_num: u32) -> u32 {
+ match spr_num {
+ 1 => self.xer,
+ 8 => self.lr,
+ 9 => self.ctr,
+ 18 => self.dsisr,
+ 19 => self.dar,
+ 22 => self.dec,
+ 25 => self.sdr1,
+ 26 => self.srr0.value(),
+ 27 => self.srr1,
+ 272 => self.sprg0,
+ 273 => self.sprg1,
+ 274 => self.sprg2,
+ 275 => self.sprg3,
+ 282 => self.ear,
+ 287 => self.pvr,
+ 528 => self.ibat0u,
+ 529 => self.ibat0l,
+ 530 => self.ibat1u,
+ 531 => self.ibat1l,
+ 532 => self.ibat2u,
+ 533 => self.ibat2l,
+ 534 => self.ibat3u,
+ 535 => self.ibat3l,
+ 536 => self.dbat0u,
+ 537 => self.dbat0l,
+ 538 => self.dbat1u,
+ 539 => self.dbat1l,
+ 540 => self.dbat2u,
+ 541 => self.dbat2l,
+ 542 => self.dbat3u,
+ 543 => self.dbat3l,
+ 1013 => self.dabr,
+ _ => panic!("unknown SPR {spr_num}"),
+ }
+ }
+
+ pub fn write(&mut self, spr_num: u32, value: u32) {
+ match spr_num {
+ 1 => self.xer = value,
+ 8 => self.lr = value,
+ 9 => self.ctr = value,
+ 18 => self.dsisr = value,
+ 19 => self.dar = value,
+ 22 => self.dec = value,
+ 25 => self.sdr1 = value,
+ 26 => self.srr0 = Srr0::from(value),
+ 27 => self.srr1 = value,
+ 272 => self.sprg0 = value,
+ 273 => self.sprg1 = value,
+ 274 => self.sprg2 = value,
+ 275 => self.sprg3 = value,
+ 282 => self.ear = value,
+ 287 => self.pvr = value,
+ 528 => self.ibat0u = value,
+ 529 => self.ibat0l = value,
+ 530 => self.ibat1u = value,
+ 531 => self.ibat1l = value,
+ 532 => self.ibat2u = value,
+ 533 => self.ibat2l = value,
+ 534 => self.ibat3u = value,
+ 535 => self.ibat3l = value,
+ 536 => self.dbat0u = value,
+ 537 => self.dbat0l = value,
+ 538 => self.dbat1u = value,
+ 539 => self.dbat1l = value,
+ 540 => self.dbat2u = value,
+ 541 => self.dbat2l = value,
+ 542 => self.dbat3u = value,
+ 543 => self.dbat3l = value,
+ 1013 => self.dabr = value,
+ _ => panic!("unknown SPR {spr_num}"),
+ }
+ }
+}
diff --git a/crates/tinyemu/src/snaptshot.rs b/crates/tinyemu/src/snaptshot.rs
index 4dda481..465f20e 100644
--- a/crates/tinyemu/src/snaptshot.rs
+++ b/crates/tinyemu/src/snaptshot.rs
@@ -12,8 +12,8 @@ impl CpuSnapshot {
pub fn from_cpu(cpu: &gekko::cpu::Cpu) -> Self {
Self {
gprs: cpu.gprs,
- lr: cpu.lr,
- ctr: cpu.ctr,
+ lr: cpu.spr.lr,
+ ctr: cpu.spr.ctr,
cr: cpu.cr,
}
}