Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

ipl infrastructure and sc handler

Layle326001fbe275
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index 63093b9..e1dca9a 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -78,7 +78,7 @@ fn main() {
let rom_data = std::fs::read(&args[1]).expect("failed to read ROM");
let dol = Dol::parse(rom_data);
- let emulator = Gekko::new(&dol, idle_skip);
+ let emulator = Gekko::with_image(&dol, idle_skip);
let event_loop = EventLoop::new().unwrap();
let mut app = App {
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index b999a55..f73889e 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -13,6 +13,8 @@ pub mod lut {
include!(concat!(env!("OUT_DIR"), "/gekko_lut.rs"));
}
+pub const IPL_RESET_VECTOR: u32 = 0xFFF0_0100;
+
pub struct Cpu {
pub gprs: [u32; 32],
pub fprs: [f64; 32], // PS0
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 963df4d..2733607 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -20,4 +20,4 @@ pub use psq::store_load_psq;
pub use rotate::rotate;
pub use store_load::{lwarx, store_load, store_load_fp, stwcx_dot};
pub use stubs::*;
-pub use system::{mftb, msr, nop, segment, spr};
+pub use system::{mftb, msr, nop, sc, segment, spr};
diff --git a/crates/gekko/src/cpu/interpreter/stubs.rs b/crates/gekko/src/cpu/interpreter/stubs.rs
index 3d3e7a1..8128e8a 100644
--- a/crates/gekko/src/cpu/interpreter/stubs.rs
+++ b/crates/gekko/src/cpu/interpreter/stubs.rs
@@ -14,7 +14,7 @@ macro_rules! stub {
stub! {
twi,
- sc, tw,
+ tw,
mtsrin, mcrxr,
lwbrx, lswx, lswi, mfsrin,
stswx, stwbrx, stswi,
diff --git a/crates/gekko/src/cpu/interpreter/system.rs b/crates/gekko/src/cpu/interpreter/system.rs
index b1516c5..1709b28 100644
--- a/crates/gekko/src/cpu/interpreter/system.rs
+++ b/crates/gekko/src/cpu/interpreter/system.rs
@@ -1,4 +1,4 @@
-use crate::cpu::sr::Sr;
+use crate::cpu::{irq, spr::Srr0, sr::Sr};
pub fn msr<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
match OP {
@@ -53,3 +53,29 @@ pub fn mftb(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruc
}
pub fn nop<const OP: u32>(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) {}
+
+pub fn sc(ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) {
+ let base: u32 = if ctx.cpu.msr.exception_prefix() { 0xFFF0_0000 } else { 0 };
+
+ ctx.cpu.spr.srr0 = Srr0::from(ctx.cpu.cia.wrapping_add(4));
+ ctx.cpu.spr.srr1 = chapa::extract_bits!(ctx.cpu.msr; 0, 5..=9, 16..=31).raw();
+
+ ctx.cpu.msr = ctx
+ .cpu
+ .msr
+ .with_pow(false)
+ .with_fp(false)
+ .with_be(false)
+ .with_dr(false)
+ .with_fe1(false)
+ .with_pm(false)
+ .with_ee(false)
+ .with_fe0(false)
+ .with_ri(false)
+ .with_pr(false)
+ .with_se(false)
+ .with_ir(false)
+ .with_le(ctx.cpu.msr.ile());
+
+ ctx.cpu.nia = base | irq::IRQ_SYSTEM_CALL;
+}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index d1d4ee8..8d939c2 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,5 +1,5 @@
use crate::{
- cpu::{self, Cpu, semantics::Instruction},
+ cpu::{self, Cpu, IPL_RESET_VECTOR, semantics::Instruction},
exi::Exi,
flipper::{
dsp::Dsp,
@@ -26,8 +26,23 @@ pub struct Gekko {
}
impl Gekko {
- pub fn new(exe: &impl Executable, idle_skip: bool) -> Self {
- let mut mmio = Mmio::new();
+ pub fn new(entrypoint: u32, idle_skip: bool) -> Self {
+ Gekko {
+ vsync_pending: false,
+ cpu: Cpu::new(entrypoint),
+ scheduler: Scheduler::new(),
+ mmio: Mmio::new(),
+ vi: Vi::new(),
+ pi: Pi::new(),
+ dsp: Dsp::new(),
+ exi: Exi::dummy(),
+ gx: Gx::new(),
+ idle_skip,
+ }
+ }
+
+ pub fn with_image(exe: &impl Executable, idle_skip: bool) -> Self {
+ let mut gekko = Gekko::new(exe.entry_point(), idle_skip);
let data = exe.data();
// Copy TEXT sections to memory
@@ -35,7 +50,7 @@ impl Gekko {
for i in 0..section.size {
let addr = section.vaddr + i;
let value = data[(section.offset + i) as usize];
- mmio.virt_write_u8(addr, value);
+ gekko.mmio.virt_write_u8(addr, value);
}
}
@@ -44,7 +59,7 @@ impl Gekko {
for i in 0..section.size {
let addr = section.vaddr + i;
let value = data[(section.offset + i) as usize];
- mmio.virt_write_u8(addr, value);
+ gekko.mmio.virt_write_u8(addr, value);
}
}
@@ -52,21 +67,42 @@ impl Gekko {
let (bss_start, bss_size) = exe.bss();
for i in 0..bss_size {
let addr = bss_start + i;
- mmio.virt_write_u8(addr, 0);
+ gekko.mmio.virt_write_u8(addr, 0);
}
- Gekko {
- vsync_pending: false,
- cpu: Cpu::new(exe.entry_point()),
- scheduler: Scheduler::new(),
- mmio,
- vi: Vi::new(),
- pi: Pi::new(),
- dsp: Dsp::new(),
- exi: Exi::dummy(),
- gx: Gx::new(),
- idle_skip,
+ gekko
+ }
+
+ pub fn with_ipl(ipl: &[u8], idle_skip: bool) -> Self {
+ // Text Sections (1):
+ // | idx | offset | vaddr | size | end |
+ // |-----|------------|------------|------------|------------|
+ // | 0 | 0x00000100 | 0x81300000 | 0x001FF7E0 | 0x814FF7E0 |
+ // Data Sections (0):
+ // | idx | offset | vaddr | size | end |
+ // |-----|--------|-------|------|-----|
+ // Entry point: 0x81300000
+ // BSS: 0x00000000 - 0x00000000 (size: 0x00000000)
+
+ let dol = image::Dol::parse(ipl.to_vec());
+ let mut gekko = Gekko::new(IPL_RESET_VECTOR, idle_skip);
+ let data = dol.data();
+
+ for section in dol.text_sections().iter().chain(dol.data_sections()) {
+ for i in 0..section.size {
+ let addr = section.vaddr + i;
+ let value = data[(section.offset + i) as usize];
+ gekko.mmio.virt_write_u8(addr, value);
+ }
}
+
+ let (bss_start, bss_size) = dol.bss();
+ for i in 0..bss_size {
+ gekko.mmio.virt_write_u8(bss_start + i, 0);
+ }
+
+ gekko.mmio.ipl = ipl.to_vec();
+ gekko
}
#[inline]
diff --git a/crates/gekko/src/mmio.rs b/crates/gekko/src/mmio.rs
index 22a0d29..364945f 100644
--- a/crates/gekko/src/mmio.rs
+++ b/crates/gekko/src/mmio.rs
@@ -13,6 +13,7 @@ pub struct Mmio {
pub ram: Vec<u8>,
pub efb: Vec<u8>,
pub hwr: Vec<u8>,
+ pub ipl: Vec<u8>,
}
impl Mmio {
@@ -21,6 +22,7 @@ impl Mmio {
ram: vec![0; RAM_SIZE],
efb: vec![0; EFB_SIZE],
hwr: vec![0; HW_REG_SIZE],
+ ipl: vec![0; 0],
}
}
@@ -35,6 +37,7 @@ impl Mmio {
tracing::warn!(phys_addr = format!("{:08X}", phys), "read from mmio");
(&self.hwr, (phys - HW_REG_BASE) as usize)
}
+ IPL_BASE..=IPL_END => (&self.ipl, (phys - IPL_BASE) as usize),
_ => {
tracing::error!(phys_addr = format!("{:08X}", phys), "unmapped physical read");
(&self.ram, 0)
@@ -54,6 +57,7 @@ impl Mmio {
tracing::warn!(phys_addr = format!("{:08X}", phys), "write to mmio");
(&mut self.hwr, (phys - HW_REG_BASE) as usize)
}
+ IPL_BASE..=IPL_END => (&mut self.ipl, (phys - IPL_BASE) as usize),
_ => {
tracing::error!(phys_addr = format!("{:08X}", phys), "unmapped physical write");
(&mut self.ram, 0)
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 5856f1a..378f098 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -3,7 +3,7 @@ use crate::{
mmio::{
Mmio,
constants::{
- DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END, PI_BASE, PI_END, VI_BASE, VI_END,
+ DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END, IPL_BASE, IPL_END, PI_BASE, PI_END, VI_BASE, VI_END
},
},
};
@@ -14,6 +14,7 @@ enum BusTarget {
Dsp,
Exi,
Gx,
+ Ipl,
Fallback,
}
@@ -24,6 +25,7 @@ fn route(phys: u32) -> (BusTarget, u32) {
PI_BASE..=PI_END => (BusTarget::Pi, phys - PI_BASE),
DSP_BASE..=DSP_END => (BusTarget::Dsp, phys - DSP_BASE),
EXI_BASE..=EXI_END => (BusTarget::Exi, phys - EXI_BASE),
+ IPL_BASE..=IPL_END => (BusTarget::Ipl, phys),
GX_FIFO_BASE..=GX_FIFO_END => (BusTarget::Gx, phys - GX_FIFO_BASE),
_ => (BusTarget::Fallback, phys),
}
@@ -42,6 +44,7 @@ impl Gekko {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
+ BusTarget::Ipl => self.mmio.phys_read_u8(offset),
BusTarget::Fallback => self.mmio.phys_read_u8(offset),
}
}
@@ -58,6 +61,7 @@ impl Gekko {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
+ BusTarget::Ipl => self.mmio.phys_read_u16(offset),
BusTarget::Fallback => self.mmio.phys_read_u16(offset),
}
}
@@ -74,6 +78,7 @@ impl Gekko {
tracing::error!(addr = format!("{:08X}", addr), "Invalid GX FIFO read");
0
}
+ BusTarget::Ipl => self.mmio.phys_read_u32(offset),
BusTarget::Fallback => self.mmio.phys_read_u32(offset),
}
}
@@ -102,6 +107,7 @@ impl Gekko {
self.check_gx_pe_finish();
}
}
+ BusTarget::Ipl => self.mmio.phys_write_u8(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),
}
}
@@ -131,6 +137,7 @@ impl Gekko {
self.check_gx_pe_finish();
}
}
+ BusTarget::Ipl => self.mmio.phys_write_u16(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),
}
}
@@ -160,6 +167,7 @@ impl Gekko {
self.check_gx_pe_finish();
}
}
+ BusTarget::Ipl => self.mmio.phys_write_u32(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u32(offset, val),
}
}
diff --git a/crates/gekko/src/mmio/constants.rs b/crates/gekko/src/mmio/constants.rs
index 7e9456b..44c846b 100644
--- a/crates/gekko/src/mmio/constants.rs
+++ b/crates/gekko/src/mmio/constants.rs
@@ -32,3 +32,7 @@ pub const AI_END: u32 = 0x0C00_6FFF;
// GX Write Gather Pipe
pub const GX_FIFO_BASE: u32 = 0x0C00_8000;
pub const GX_FIFO_END: u32 = 0x0C00_801F;
+
+// IPL / Bootrom
+pub const IPL_BASE: u32 = 0x3FF0_0000;
+pub const IPL_END: u32 = 0x3FFF_FFFF;
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index 1f9852d..c269345 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -481,7 +481,7 @@ fn main() {
let rom_data = std::fs::read(&args[1]).expect("failed to read ROM");
let dol = Dol::parse(rom_data);
- let emulator = Gekko::new(&dol, idle_skip);
+ let emulator = Gekko::with_image(&dol, idle_skip);
let event_loop = EventLoop::new().unwrap();
let mut app = App {
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index fc69a89..5499286 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -12,7 +12,12 @@ use snaptshot::CpuSnapshot;
#[command(about = "Gekko CPU emulator / debugger")]
struct Args {
/// Path to the ROM/DOL file
- rom: String,
+ #[arg(long)]
+ rom: Option<String>,
+
+ /// Path to an IPL file
+ #[arg(long)]
+ ipl: Option<String>,
/// Print decoded instructions and register diffs after each step
#[arg(long)]
@@ -46,9 +51,16 @@ fn main() {
)
.init();
- let rom_data = std::fs::read(&args.rom).expect("failed to read ROM");
- let dol = image::Dol::parse(rom_data);
- let mut gekko = gekko::gekko::Gekko::new(&dol, false);
+ let mut gekko = if let Some(rom_path) = &args.rom {
+ let rom_data = std::fs::read(rom_path).expect("failed to read ROM");
+ let dol = image::Dol::parse(rom_data);
+ gekko::gekko::Gekko::with_image(&dol, false)
+ } else if let Some(ipl_path) = &args.ipl {
+ let ipl_data = std::fs::read(ipl_path).expect("failed to read IPL");
+ gekko::gekko::Gekko::with_ipl(&ipl_data, false)
+ } else {
+ panic!("Either --rom or --ipl must be provided");
+ };
let symbols = args.elf.as_ref().map(|path| {
let elf_data = std::fs::read(path).expect("failed to read ELF file");