Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

basic dol parsing

Layle25507bf3b2be
diff --git a/Cargo.lock b/Cargo.lock
index 8f4d79f..0807e11 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -61,6 +61,12 @@ dependencies = [
"windows-sys",
]
+[[package]]
+name = "bitflags"
+version = "2.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -150,6 +156,40 @@ dependencies = [
"windows-sys",
]
+[[package]]
+name = "comfy-table"
+version = "7.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "958c5d6ecf1f214b4c2bbbbf6ab9523a864bd136dcf71a7e8904799acfe1ad47"
+dependencies = [
+ "crossterm",
+ "unicode-segmentation",
+ "unicode-width",
+]
+
+[[package]]
+name = "crossterm"
+version = "0.29.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8b9f2e4c67f833b660cdb0a3523065869fb35570177239812ed4c905aeff87b"
+dependencies = [
+ "bitflags",
+ "crossterm_winapi",
+ "document-features",
+ "parking_lot",
+ "rustix",
+ "winapi",
+]
+
+[[package]]
+name = "crossterm_winapi"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
+dependencies = [
+ "winapi",
+]
+
[[package]]
name = "disasm"
version = "0.1.0"
@@ -157,12 +197,37 @@ dependencies = [
"chipi 0.4.0",
]
+[[package]]
+name = "document-features"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61"
+dependencies = [
+ "litrs",
+]
+
+[[package]]
+name = "dol"
+version = "0.1.0"
+
+[[package]]
+name = "errno"
+version = "0.3.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
+dependencies = [
+ "libc",
+ "windows-sys",
+]
+
[[package]]
name = "gctool"
version = "0.1.0"
dependencies = [
"clap",
+ "comfy-table",
"disasm",
+ "dol",
]
[[package]]
@@ -171,6 +236,7 @@ version = "0.1.0"
dependencies = [
"chapa",
"chipi 0.5.1",
+ "dol",
"tracing",
]
@@ -192,6 +258,33 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+[[package]]
+name = "libc"
+version = "0.2.182"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
+
+[[package]]
+name = "litrs"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092"
+
+[[package]]
+name = "lock_api"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
+dependencies = [
+ "scopeguard",
+]
+
[[package]]
name = "log"
version = "0.4.29"
@@ -234,6 +327,29 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
+[[package]]
+name = "parking_lot"
+version = "0.12.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
+dependencies = [
+ "lock_api",
+ "parking_lot_core",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.9.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "redox_syscall",
+ "smallvec",
+ "windows-link",
+]
+
[[package]]
name = "pin-project-lite"
version = "0.2.16"
@@ -258,6 +374,15 @@ dependencies = [
"proc-macro2",
]
+[[package]]
+name = "redox_syscall"
+version = "0.5.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
+dependencies = [
+ "bitflags",
+]
+
[[package]]
name = "regex-automata"
version = "0.4.13"
@@ -275,6 +400,25 @@ version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
+[[package]]
+name = "rustix"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
+dependencies = [
+ "bitflags",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys",
+]
+
+[[package]]
+name = "scopeguard"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+
[[package]]
name = "sharded-slab"
version = "0.1.7"
@@ -394,6 +538,18 @@ version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
+[[package]]
+name = "unicode-segmentation"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
+
+[[package]]
+name = "unicode-width"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
+
[[package]]
name = "utf8parse"
version = "0.2.2"
@@ -406,6 +562,28 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
[[package]]
name = "windows-link"
version = "0.2.1"
diff --git a/Cargo.toml b/Cargo.toml
index 588089a..f785c32 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,3 +1,3 @@
[workspace]
resolver = "3"
-members = ["crates/gctool", "crates/disasm", "crates/gekko", "crates/tinyemu"]
+members = ["crates/gctool", "crates/disasm", "crates/gekko", "crates/tinyemu", "crates/dol"]
diff --git a/crates/dol/Cargo.toml b/crates/dol/Cargo.toml
new file mode 100644
index 0000000..aa35fbc
--- /dev/null
+++ b/crates/dol/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "dol"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/crates/dol/src/lib.rs b/crates/dol/src/lib.rs
new file mode 100644
index 0000000..e7a3c02
--- /dev/null
+++ b/crates/dol/src/lib.rs
@@ -0,0 +1,52 @@
+pub struct Section {
+ pub offset: u32,
+ pub vaddr: u32,
+ pub size: u32,
+}
+
+pub struct Dol {
+ pub text_sections: Vec<Section>,
+ pub data_sections: Vec<Section>,
+ pub bss_start: u32,
+ pub bss_size: u32,
+ pub entry_point: u32,
+}
+
+impl Dol {
+ pub fn parse(data: &[u8]) -> Self {
+ let mut text_sections = Vec::new();
+ let mut data_sections = Vec::new();
+
+ for i in 0..=6 {
+ let offset = u32::from_be_bytes(data[0x00 + (i * 4)..0x00 + (i * 4) + 4].try_into().unwrap());
+ let vaddr = u32::from_be_bytes(data[0x48 + (i * 4)..0x48 + (i * 4) + 4].try_into().unwrap());
+ let size = u32::from_be_bytes(data[0x90 + (i * 4)..0x90 + (i * 4) + 4].try_into().unwrap());
+
+ if size > 0 {
+ text_sections.push(Section { offset, vaddr, size });
+ }
+ }
+
+ for i in 0..=10 {
+ let offset = u32::from_be_bytes(data[0x1C + (i * 4)..0x1C + (i * 4) + 4].try_into().unwrap());
+ let vaddr = u32::from_be_bytes(data[0x64 + (i * 4)..0x64 + (i * 4) + 4].try_into().unwrap());
+ let size = u32::from_be_bytes(data[0xAC + (i * 4)..0xAC + (i * 4) + 4].try_into().unwrap());
+
+ if size > 0 {
+ data_sections.push(Section { offset, vaddr, size });
+ }
+ }
+
+ let bss_start = u32::from_be_bytes(data[0xD8..0xDC].try_into().unwrap());
+ let bss_size = u32::from_be_bytes(data[0xDC..0xE0].try_into().unwrap());
+ let entry_point = u32::from_be_bytes(data[0xE0..0xE4].try_into().unwrap());
+
+ Dol {
+ text_sections,
+ data_sections,
+ bss_start,
+ bss_size,
+ entry_point,
+ }
+ }
+}
\ No newline at end of file
diff --git a/crates/gctool/Cargo.toml b/crates/gctool/Cargo.toml
index b68e663..5e30595 100644
--- a/crates/gctool/Cargo.toml
+++ b/crates/gctool/Cargo.toml
@@ -5,4 +5,6 @@ edition = "2024"
[dependencies]
clap = { version = "4", features = ["derive"] }
-disasm = { path = "../disasm" }
\ No newline at end of file
+disasm = { path = "../disasm" }
+dol = { path = "../dol" }
+comfy-table = "7.2.2"
\ No newline at end of file
diff --git a/crates/gctool/src/main.rs b/crates/gctool/src/main.rs
index d1f2b28..d76cdca 100644
--- a/crates/gctool/src/main.rs
+++ b/crates/gctool/src/main.rs
@@ -1,7 +1,9 @@
use disasm::dsp::DspInstruction;
use disasm::gekko::GekkoInstruction;
+use dol::Dol;
use clap::{Parser, ValueEnum};
+use comfy_table::{Table, presets::ASCII_MARKDOWN};
use std::fs;
use std::process;
@@ -25,6 +27,9 @@ fn parse_offset(s: &str) -> Result<usize, String> {
struct Args {
#[arg(short, long, value_enum, default_value_t = Mode::Dol)]
mode: Mode,
+ /// Inspect file headers instead of disassembling
+ #[arg(short, long)]
+ inspect: bool,
file: String,
#[arg(value_parser = parse_offset, default_value_t = 0)]
offset: usize,
@@ -68,6 +73,40 @@ fn disassemble_dsp(data: &[u8], start: usize) {
}
}
+fn section_table(sections: &[dol::Section]) -> Table {
+ let mut table = Table::new();
+ table.load_preset(ASCII_MARKDOWN);
+ table.set_header(vec!["idx", "offset", "vaddr", "size", "end"]);
+ for (i, s) in sections.iter().enumerate() {
+ table.add_row(vec![
+ format!("{i}"),
+ format!("0x{:08X}", s.offset),
+ format!("0x{:08X}", s.vaddr),
+ format!("0x{:08X}", s.size),
+ format!("0x{:08X}", s.vaddr + s.size),
+ ]);
+ }
+ table
+}
+
+fn inspect_dol(data: &[u8]) {
+ let dol = Dol::parse(data);
+
+ println!("Text Sections ({}):", dol.text_sections.len());
+ println!("{}\n", section_table(&dol.text_sections));
+
+ println!("Data Sections ({}):", dol.data_sections.len());
+ println!("{}\n", section_table(&dol.data_sections));
+
+ println!("Entry point: 0x{:08X}\n", dol.entry_point);
+ println!(
+ "BSS: 0x{:08X} - 0x{:08X} (size: 0x{:08X})",
+ dol.bss_start,
+ dol.bss_start + dol.bss_size,
+ dol.bss_size
+ );
+}
+
fn main() {
let args = Args::parse();
@@ -76,6 +115,17 @@ fn main() {
process::exit(1);
});
+ if args.inspect {
+ match args.mode {
+ Mode::Dol => inspect_dol(&data),
+ Mode::Dsp => {
+ eprintln!("inspect is only supported for DOL files");
+ process::exit(1);
+ }
+ }
+ return;
+ }
+
let min_size = match args.mode {
Mode::Dol => 4,
Mode::Dsp => 2,
diff --git a/crates/gekko/Cargo.toml b/crates/gekko/Cargo.toml
index 984489f..fe4c320 100644
--- a/crates/gekko/Cargo.toml
+++ b/crates/gekko/Cargo.toml
@@ -9,3 +9,4 @@ chipi = "0.5.1"
[dependencies]
chapa = { path = "../../../chapa/chapa" }
tracing = "0.1.44"
+dol = { path = "../dol" }
\ No newline at end of file
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index 50fc053..7ea48b8 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -26,13 +26,13 @@ pub struct Cpu {
}
impl Cpu {
- pub fn new() -> Self {
+ pub fn new(initial_pc: u32) -> Self {
Cpu {
gprs: [0; 32],
fprs: [0.0; 32],
- pc: 0x80004000,
- cia: 0x80004000,
- nia: 0x80004004,
+ pc: initial_pc,
+ cia: initial_pc,
+ nia: initial_pc.wrapping_add(4),
lr: 0,
ctr: 0,
xer: 0,
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index bc615f4..9ba075f 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -13,10 +13,34 @@ impl Gekko {
pub fn new(path: &str) -> Self {
let mut mmu = mmu::Mmu::new();
let data = std::fs::read(path).expect("failed to read ROM");
- mmu.ram[0x3F00..0x3F00 + data.len()].copy_from_slice(&data);
+ let dol = dol::Dol::parse(&data);
+
+ // Copy TEXT sections to memory
+ for section in &dol.text_sections {
+ for i in 0..section.size {
+ let addr = section.vaddr + i;
+ let value = data[(section.offset + i) as usize];
+ mmu.virt_write_u8(addr, value);
+ }
+ }
+
+ // Copy DATA sections to memory
+ for section in &dol.data_sections {
+ for i in 0..section.size {
+ let addr = section.vaddr + i;
+ let value = data[(section.offset + i) as usize];
+ mmu.virt_write_u8(addr, value);
+ }
+ }
+
+ // Zero out the BSS section
+ for i in 0..dol.bss_size {
+ let addr = dol.bss_start + i;
+ mmu.virt_write_u8(addr, 0);
+ }
Gekko {
- cpu: cpu::Cpu::new(),
+ cpu: cpu::Cpu::new(dol.entry_point),
scheduler: scheduler::Scheduler { cycles: 0 },
mmu,
}