Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

instructions

Laylec0d04ec43924
diff --git a/.gitignore b/.gitignore
index bf3b621..469f79f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
target/
rsrc/*.dol
-rsrc/*.bin
\ No newline at end of file
+rsrc/*.bin
+.idea/
+.vscode/
\ No newline at end of file
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index ab6e10d..03829ae 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -10,8 +10,10 @@ fn main() {
.handler_mod("crate::cpu::interpreter")
.ctx_type("crate::gekko::Gekko")
.instr_type("crate::cpu::semantics::Instruction")
- .group("branch", ["bx"])
- .group("alu", ["ori", "addi", "addis"]);
+ .group("branch", ["bx", "bclrx"])
+ .group("alu", ["ori", "addi", "addis"])
+ .group("msr", ["mtmsr", "mfmsr"])
+ .group("spr", ["mtspr", "mfspr"]);
// Always regenerate the LUT dispatch tables
builder
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index dd18221..a7596e1 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -9,9 +9,16 @@ pub mod lut {
pub struct Cpu {
pub gprs: [u32; 32],
pub fprs: [f64; 32],
- pub current_pc: u32,
- pub next_pc: u32,
pub pc: u32,
+ pub lr: u32,
+ pub ctr: u32,
+ pub xer: u32,
+
+ // 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).
+ pub cia: u32, // Current Instruction Address
+ pub nia: u32, // Next Instruction Address
}
impl Cpu {
@@ -19,25 +26,12 @@ impl Cpu {
Cpu {
gprs: [0; 32],
fprs: [0.0; 32],
- current_pc: 0x100,
- next_pc: 0x104,
pc: 0x100,
+ cia: 0x100,
+ nia: 0x104,
+ lr: 0,
+ ctr: 0,
+ xer: 0,
}
}
-
- pub fn read_gpr(&self, reg: usize) -> u32 {
- self.gprs[reg]
- }
-
- pub fn write_gpr(&mut self, reg: usize, value: u32) {
- self.gprs[reg] = value;
- }
-
- pub fn read_fpr(&self, reg: usize) -> f64 {
- self.fprs[reg]
- }
-
- pub fn write_fpr(&mut self, reg: usize, value: f64) {
- self.fprs[reg] = value;
- }
}
\ No newline at end of file
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 491702b..5088253 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -1,14 +1,22 @@
pub fn branch<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
if instr.lk() {
- ctx.cpu.write_gpr(0, ctx.cpu.current_pc.wrapping_add(4));
+ ctx.cpu.lr = ctx.cpu.cia.wrapping_add(4);
}
- let target = if instr.aa() {
- instr.li() as u32
- } else {
- ctx.cpu.current_pc.wrapping_add_signed(instr.li())
+ let target = match OP {
+ crate::cpu::lut::OP_BX => {
+ if instr.aa() {
+ instr.li() as u32
+ } else {
+ ctx.cpu.cia.wrapping_add_signed(instr.li())
+ }
+ }
+ crate::cpu::lut::OP_BCLRX => {
+ ctx.cpu.lr
+ }
+ _ => todo!("branch instruction with OP = {OP:#x}")
};
- ctx.cpu.next_pc = target;
+ ctx.cpu.nia = target;
}
pub fn alu<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
@@ -31,6 +39,35 @@ pub fn alu<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::sema
}
}
+pub fn msr<const OP: u32>(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) {
+ match OP {
+ crate::cpu::lut::OP_MTMSR => {
+ println!("OP_MTMSR!!");
+ }
+ crate::cpu::lut::OP_MFMSR => {
+ println!("OP_MFMSR!!");
+ }
+ _ => todo!("MSR instruction with OP = {OP:#x}")
+ }
+}
+
+pub fn spr<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
+ match OP {
+ crate::cpu::lut::OP_MTSPR => {
+ println!("OP_MTSPR!! {}", 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,
+ _ => todo!("unimplemented SPR number {}", instr.spr())
+ }
+ }
+ _ => todo!("SPR instruction with OP = {OP:#x}")
+ }
+}
+
pub fn twi(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("twi") }
pub fn ps_cmpu0(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("ps_cmpu0") }
pub fn ps_cmpo0(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("ps_cmpo0") }
@@ -113,7 +150,6 @@ pub fn lwzux(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Inst
pub fn andcx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("andcx") }
pub fn mulhwx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mulhwx") }
pub fn mfcr(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mfcr") }
-pub fn mfmsr(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mfmsr") }
pub fn dcbf(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("dcbf") }
pub fn lbzx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("lbzx") }
pub fn negx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("negx") }
@@ -122,7 +158,6 @@ pub fn norx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instr
pub fn subfex(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("subfex") }
pub fn addex(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("addex") }
pub fn mtcrf(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mtcrf") }
-pub fn mtmsr(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mtmsr") }
pub fn stwcx_dot(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("stwcx_dot") }
pub fn stwx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("stwx") }
pub fn stwux(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("stwux") }
@@ -144,7 +179,6 @@ pub fn tlbie(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Inst
pub fn eciwx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("eciwx") }
pub fn lhzux(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("lhzux") }
pub fn xorx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("xorx") }
-pub fn mfspr(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mfspr") }
pub fn lhax(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("lhax") }
pub fn tlbia(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("tlbia") }
pub fn mftb(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mftb") }
@@ -155,7 +189,6 @@ pub fn ecowx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Inst
pub fn sthux(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("sthux") }
pub fn orx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("orx") }
pub fn divwux(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("divwux") }
-pub fn mtspr(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("mtspr") }
pub fn dcbi(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("dcbi") }
pub fn nandx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("nandx") }
pub fn divwx(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) { todo!("divwx") }
diff --git a/crates/gekko/src/cpu/semantics.rs b/crates/gekko/src/cpu/semantics.rs
index 36840b6..e285b40 100644
--- a/crates/gekko/src/cpu/semantics.rs
+++ b/crates/gekko/src/cpu/semantics.rs
@@ -15,22 +15,22 @@ impl Instruction {
#[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 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 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 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 simm(&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 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 spr(&self) -> u32 { let raw = (self.0 >> 11) & 0x3ff; (raw >> 5) | ((raw & 0x1f) << 5) }
- #[inline] pub fn bo(&self) -> u32 { (self.0 >> 21) & 0x1f }
- #[inline] pub fn bi(&self) -> u32 { (self.0 >> 16) & 0x1f }
+ #[inline] pub fn bo(&self) -> u32 { (self.0 >> 21) & 0x1f }
+ #[inline] pub fn bi(&self) -> u32 { (self.0 >> 16) & 0x1f }
}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index bec71cf..64a26cf 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -20,13 +20,13 @@ impl Gekko {
}
pub fn run_until_event(&mut self) {
- self.cpu.current_pc = self.cpu.pc;
- self.cpu.next_pc = self.cpu.current_pc.wrapping_add(4);
+ self.cpu.cia = self.cpu.pc;
+ self.cpu.nia = self.cpu.cia.wrapping_add(4);
- let instr = cpu::semantics::Instruction(self.mmu.read_u32(self.cpu.current_pc));
+ let instr = cpu::semantics::Instruction(self.mmu.read_u32(self.cpu.cia));
cpu::lut::dispatch(self, instr);
self.scheduler.cycles += 1;
- self.cpu.pc = self.cpu.next_pc;
+ self.cpu.pc = self.cpu.nia;
}
}
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index b8eed2a..9b59b5f 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -2,6 +2,23 @@ use colored::Colorize;
use disasm::gekko::GekkoInstruction;
use disasm::tokenizer::{self, AsmToken};
+#[derive(Clone, Copy)]
+struct CpuSnapshot {
+ gprs: [u32; 32],
+ lr: u32,
+ ctr: u32,
+}
+
+impl CpuSnapshot {
+ fn from_cpu(cpu: &gekko::cpu::Cpu) -> Self {
+ Self {
+ gprs: cpu.gprs,
+ lr: cpu.lr,
+ ctr: cpu.ctr,
+ }
+ }
+}
+
fn main() {
let path = std::env::args()
.nth(1)
@@ -9,6 +26,8 @@ fn main() {
let is_debug = std::env::args().any(|arg| arg == "--debug");
let mut gekko = gekko::gekko::Gekko::new(&path);
+ let mut prev_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
+
loop {
let addr = gekko.cpu.pc;
let instr = GekkoInstruction::decode(&gekko.mmu.memory[addr as usize..])
@@ -26,6 +45,13 @@ fn main() {
);
gekko.run_until_event();
+ let curr_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
+
+ if is_debug {
+ dump_registers(&curr_snapshot, &prev_snapshot);
+ }
+
+ prev_snapshot = curr_snapshot;
}
}
@@ -46,6 +72,39 @@ fn colorize(tok: &AsmToken<'_>) -> String {
}
}
+fn dump_registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
+ let fmt_reg = |label: &str, val: u32, prev_val: u32| -> String {
+ let value = format!("{:08X}", val);
+ if val != prev_val {
+ format!("{} {} ", label.yellow().bold(), value.bright_red().bold())
+ } else {
+ format!("{} {} ", label.dimmed(), value.dimmed())
+ }
+ };
+
+ for row in 0..8 {
+ let line: String = (0..4)
+ .map(|col| {
+ let i = row * 4 + col;
+ fmt_reg(&format!("r{:<2}", i), curr.gprs[i], prev.gprs[i])
+ })
+ .collect();
+ println!("{}", line.trim_end());
+ }
+
+ println!(
+ "{}",
+ format!(
+ "{}{}",
+ fmt_reg("lr ", curr.lr, prev.lr),
+ fmt_reg("ctr", curr.ctr, prev.ctr)
+ )
+ .trim_end()
+ );
+
+ println!();
+}
+
fn colorize_instr(instr: &disasm::gekko::GekkoInstruction) -> String {
let text = format!("{}", instr);
let tokens = tokenizer::tokenize(&text);