Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

fixed up alu

Laylec5d31f71d9a9
diff --git a/crates/gekko/build.rs b/crates/gekko/build.rs
index 9cae594..1cad1fe 100644
--- a/crates/gekko/build.rs
+++ b/crates/gekko/build.rs
@@ -11,7 +11,20 @@ fn main() {
.ctx_type("crate::gekko::Gekko")
.instr_type("crate::cpu::semantics::Instruction")
.group("branch", ["bx", "bcx", "bclrx"])
- .group("alu", ["ori", "oris", "addx", "addi", "addis"])
+ .group(
+ "alu",
+ [
+ "ori",
+ "oris",
+ "addx",
+ "addi",
+ "addis",
+ "xori",
+ "xoris",
+ "andi_dot",
+ "andis_dot",
+ ],
+ )
.group("rotate", ["rlwinmx"])
.group("msr", ["mtmsr", "mfmsr", "rfi"])
.group("spr", ["mtspr", "mfspr"])
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index 73dd23f..adb2d5c 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -53,4 +53,16 @@ impl Cpu {
pub fn write_gpr(&mut self, index: u8, value: u32) {
self.gprs[index as usize] = value;
}
+
+ #[inline]
+ pub fn update_cr0(&mut self, val: u32) {
+ let so = (self.spr.xer >> 31) != 0; // SO is copied from XER[SO]
+ self.cr.set_cr0(
+ condition::ConditionField::new()
+ .with_lt((val as i32) < 0)
+ .with_gt(val > 0)
+ .with_eq(val == 0)
+ .with_so(so),
+ );
+ }
}
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index a1fcef1..b30d6db 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -65,49 +65,55 @@ pub fn alu<const OP: u32>(
) {
match OP {
crate::cpu::lut::OP_ADDX => {
- let (res, carry) = ctx
+ let res = ctx
.cpu
.read_gpr(instr.ra())
- .overflowing_add(ctx.cpu.read_gpr(instr.rb()));
+ .wrapping_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);
+ ctx.cpu.update_cr0(res);
}
}
- crate::cpu::lut::OP_ADDI => {
+ crate::cpu::lut::OP_ADDI | crate::cpu::lut::OP_ADDIS => {
let ra = if instr.ra() == 0 {
0
} else {
ctx.cpu.read_gpr(instr.ra())
};
- ctx.cpu
- .write_gpr(instr.rd(), ra.wrapping_add_signed(instr.simm()));
+ let simm = if OP == crate::cpu::lut::OP_ADDIS {
+ instr.simm() << 16
+ } else {
+ instr.simm()
+ };
+ ctx.cpu.write_gpr(instr.rd(), ra.wrapping_add_signed(simm));
}
- crate::cpu::lut::OP_ADDIS => {
- let ra = if instr.ra() == 0 {
- 0
+ crate::cpu::lut::OP_ORI | crate::cpu::lut::OP_ORIS => {
+ let imm = if OP == crate::cpu::lut::OP_ORIS {
+ (instr.uimm() as u32) << 16
} else {
- ctx.cpu.read_gpr(instr.ra())
+ instr.uimm() as u32
};
ctx.cpu
- .write_gpr(instr.rd(), ra.wrapping_add_signed(instr.simm() << 16));
+ .write_gpr(instr.ra(), ctx.cpu.read_gpr(instr.rs()) | imm);
}
- crate::cpu::lut::OP_ORI => {
- ctx.cpu.write_gpr(
- instr.ra(),
- ctx.cpu.read_gpr(instr.rs()) | instr.uimm() as u32,
- );
+ crate::cpu::lut::OP_XORI | crate::cpu::lut::OP_XORIS => {
+ let imm = if OP == crate::cpu::lut::OP_XORIS {
+ (instr.uimm() as u32) << 16
+ } else {
+ instr.uimm() as u32
+ };
+ ctx.cpu
+ .write_gpr(instr.ra(), ctx.cpu.read_gpr(instr.rs()) ^ imm);
}
- crate::cpu::lut::OP_ORIS => {
- ctx.cpu.write_gpr(
- instr.ra(),
- ctx.cpu.read_gpr(instr.rs()) | ((instr.uimm() as u32) << 16),
- );
+ crate::cpu::lut::OP_ANDI_DOT | crate::cpu::lut::OP_ANDIS_DOT => {
+ let mask = if OP == crate::cpu::lut::OP_ANDIS_DOT {
+ (instr.uimm() as u32) << 16
+ } else {
+ instr.uimm() as u32
+ };
+ let val = ctx.cpu.read_gpr(instr.rs()) & mask;
+ ctx.cpu.write_gpr(instr.ra(), val);
+ ctx.cpu.update_cr0(val);
}
_ => todo!("ALU instruction with OP = {OP:#x}"),
}
@@ -129,7 +135,11 @@ pub fn rotate<const OP: u32>(
0xFFFF_FFFFu32 >> (me + 1)
};
let m = if mb <= me { begin & !end } else { begin | !end };
- ctx.cpu.write_gpr(instr.ra(), r & m);
+ let res = r & m;
+ ctx.cpu.write_gpr(instr.ra(), res);
+ if instr.rc() {
+ ctx.cpu.update_cr0(res);
+ }
}
_ => todo!("Rotate instruction with OP = {OP:#x}"),
}