Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

debugger and dvd stuff

Layle7f0e2bd26aca
diff --git a/.gitignore b/.gitignore
index 45cc66b..8acea6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@ target/
perf.*
profile.json.gz
out*.txt
-private/
\ No newline at end of file
+private/
+*.log
\ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
index ab142f3..c35fc10 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -637,9 +637,9 @@ dependencies = [
"backend-wgpu",
"disasm",
"egui",
+ "egui-phosphor",
"egui-wgpu",
"egui-winit",
- "egui_material_icons",
"gecko",
"image 0.1.0",
"pollster",
@@ -739,6 +739,15 @@ dependencies = [
"unicode-segmentation",
]
+[[package]]
+name = "egui-phosphor"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3b2838620355debaeb7fa3cc331a1d8ab77e6387d40e65686fa468c229dbf63"
+dependencies = [
+ "egui",
+]
+
[[package]]
name = "egui-wgpu"
version = "0.33.3"
@@ -776,15 +785,6 @@ dependencies = [
"winit",
]
-[[package]]
-name = "egui_material_icons"
-version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3ff7d3715b5ee29fe75afd19420756b3e0a970149b62712af1b3204a68e2012b"
-dependencies = [
- "egui",
-]
-
[[package]]
name = "egui_plot"
version = "0.34.1"
@@ -2769,6 +2769,7 @@ dependencies = [
"disasm",
"gecko",
"image 0.1.0",
+ "scripting",
"tracing-subscriber",
]
diff --git a/crates/debugger/Cargo.toml b/crates/debugger/Cargo.toml
index 0585362..2d9a5ae 100644
--- a/crates/debugger/Cargo.toml
+++ b/crates/debugger/Cargo.toml
@@ -18,5 +18,5 @@ pollster = "0.4"
egui = "0.33"
egui-wgpu = "0.33"
egui-winit = { version = "0.33", features = ["wayland", "x11"] }
-egui_material_icons = "0.5.0"
+egui-phosphor = "0.11.0"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
diff --git a/crates/debugger/src/debugger.rs b/crates/debugger/src/debugger.rs
index 34071c9..f7cebc2 100644
--- a/crates/debugger/src/debugger.rs
+++ b/crates/debugger/src/debugger.rs
@@ -1,3 +1,12 @@
+use std::fs::File;
+use std::io::{BufWriter, Write};
+
+use disasm::gekko::GekkoInstruction;
+use disasm::tokenizer::{self, AsmToken};
+use gecko::gamecube::GameCube;
+
+const TRACE_FILENAME: &str = "trace.log";
+
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum EmulatorState {
Running,
@@ -12,6 +21,7 @@ pub struct DebuggerUi {
pub show_cpu: bool,
pub show_gx_state: bool,
pub show_mmio: bool,
+ pub show_dvd: bool,
pub show_exi: bool,
pub show_irqs: bool,
pub show_controls: bool,
@@ -19,6 +29,7 @@ pub struct DebuggerUi {
pub memory_addr_input: String,
pub run_until_addr_input: String,
pub dvd_cover_open: Option<bool>,
+ pub trace_writer: Option<BufWriter<File>>,
}
impl Default for DebuggerUi {
@@ -29,12 +40,87 @@ impl Default for DebuggerUi {
show_controls: true,
show_gx_state: false,
show_mmio: false,
+ show_dvd: false,
show_exi: false,
show_irqs: false,
memory_base: 0x8000_0000,
memory_addr_input: "80000000".to_string(),
run_until_addr_input: String::new(),
dvd_cover_open: None,
+ trace_writer: None,
+ }
+ }
+}
+
+impl DebuggerUi {
+ pub fn is_tracing(&self) -> bool {
+ self.trace_writer.is_some()
+ }
+
+ pub fn start_trace(&mut self) {
+ let file = File::create(TRACE_FILENAME).expect("failed to create trace file");
+ self.trace_writer = Some(BufWriter::new(file));
+ }
+
+ pub fn stop_trace(&mut self) {
+ if let Some(mut w) = self.trace_writer.take() {
+ let _ = w.flush();
+ }
+ }
+
+ pub fn trace_step(&mut self, emulator: &GameCube) {
+ if let Some(ref mut writer) = self.trace_writer {
+ let pc = emulator.cpu.pc;
+ let raw = emulator.mmio.virt_read_u32(pc);
+ if let Some((instr, _)) = GekkoInstruction::decode(emulator.mmio.virt_slice(pc, 4)) {
+ let text = format!("{}", instr);
+ let comment = reg_comment(&text, &emulator.cpu.gprs, &emulator.cpu.fprs);
+ const DISASM_COL: usize = 22;
+ const COMMENT_COL: usize = 50;
+ let pad = COMMENT_COL.saturating_sub(DISASM_COL + text.len());
+ if comment.is_empty() {
+ let _ = writeln!(writer, "{:08X} {:08X} {}", pc, raw, text);
+ } else {
+ let _ = writeln!(
+ writer,
+ "{:08X} {:08X} {}{}; {}",
+ pc,
+ raw,
+ text,
+ " ".repeat(pad),
+ comment
+ );
+ }
+ } else {
+ let _ = writeln!(writer, "{:08X} {:08X} <unknown>", pc, raw);
+ }
+ }
+ }
+}
+
+fn reg_comment(disasm_text: &str, gprs: &[u32; 32], fprs: &[f64; 32]) -> String {
+ let tokens = tokenizer::tokenize(disasm_text);
+ let mut parts = Vec::new();
+ let mut gpr_seen = [false; 32];
+ let mut fpr_seen = [false; 32];
+ for tok in &tokens {
+ match tok {
+ AsmToken::Gpr(n) => {
+ let n = *n as usize;
+ if !gpr_seen[n] {
+ gpr_seen[n] = true;
+ parts.push(format!("r{}={:08X}", n, gprs[n]));
+ }
+ }
+ AsmToken::Fpr(n) => {
+ let n = *n as usize;
+ if !fpr_seen[n] {
+ fpr_seen[n] = true;
+ parts.push(format!("f{}={:.6e}", n, fprs[n]));
+ }
+ }
+ _ => {}
}
}
+ parts.join(", ")
}
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index 7a40777..1836c08 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -10,7 +10,7 @@ use winit::{
use gecko::flipper::si::pad::{self, PadStatus, STICK_CENTER};
use gecko::gamecube::GameCube;
-use image::Dol;
+use image::{Dol, dvd::Dvd};
use crate::debugger::DebuggerUi;
use crate::render::RenderState;
@@ -95,6 +95,7 @@ fn main() {
.position(|a| a == "--rom")
.map(|i| &args[i + 1])
.or_else(|| args.get(1).filter(|a| !a.starts_with("--")));
+ let iso_path = args.iter().position(|a| a == "--iso").map(|i| &args[i + 1]);
let script_path = args.iter().position(|a| a == "--script").map(|i| &args[i + 1]);
let mut emulator = if let Some(ipl) = ipl_path {
@@ -106,12 +107,23 @@ fn main() {
GameCube::with_image(&dol, idle_skip)
} else {
eprintln!(
- "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--immediate] [--idle-skip]",
+ "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] [--idle-skip]",
args[0]
);
std::process::exit(1);
};
+ if let Some(iso) = iso_path {
+ if ipl_path.is_none() {
+ eprintln!("--iso requires --ipl");
+ std::process::exit(1);
+ }
+
+ let iso_data = std::fs::read(iso).expect("failed to read ISO");
+ let dvd = Dvd::parse(iso_data);
+ emulator.insert_dvd(dvd);
+ }
+
if let Some(path) = script_path {
let host = scripting::LuaScriptHost::from_file(path).expect("failed to load script");
emulator.set_script_host(Box::new(host));
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index d512c79..7ee2dac 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -137,7 +137,9 @@ impl RenderState {
);
let egui_ctx = egui::Context::default();
- egui_material_icons::initialize(&egui_ctx);
+ let mut fonts = egui::FontDefinitions::default();
+ egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular);
+ egui_ctx.set_fonts(fonts);
egui_ctx.global_style_mut(|style| {
let f = &style.visuals.window_fill;
style.visuals.window_fill = egui::Color32::from_rgba_unmultiplied(f.r(), f.g(), f.b(), 240);
@@ -183,17 +185,37 @@ impl RenderState {
}
match debugger_ui.emulator_state {
- EmulatorState::Running => emulator.run_until_vsync(),
+ EmulatorState::Running => {
+ if debugger_ui.is_tracing() {
+ emulator.prepare_frame();
+ while !emulator.vsync_pending {
+ debugger_ui.trace_step(emulator);
+ emulator.step();
+ }
+ } else {
+ emulator.run_until_vsync();
+ }
+ }
EmulatorState::Step => {
+ debugger_ui.trace_step(emulator);
emulator.step();
debugger_ui.emulator_state = EmulatorState::Paused;
}
EmulatorState::RunUntilVsync => {
- emulator.run_until_vsync();
+ if debugger_ui.is_tracing() {
+ emulator.prepare_frame();
+ while !emulator.vsync_pending {
+ debugger_ui.trace_step(emulator);
+ emulator.step();
+ }
+ } else {
+ emulator.run_until_vsync();
+ }
debugger_ui.emulator_state = EmulatorState::Paused;
}
EmulatorState::RunUntilAddress(addr) => {
while emulator.cpu.pc != addr {
+ debugger_ui.trace_step(emulator);
emulator.step();
}
debugger_ui.emulator_state = EmulatorState::Paused;
@@ -231,11 +253,11 @@ impl RenderState {
let is_paused = debugger_ui.emulator_state == EmulatorState::Paused;
let is_running = debugger_ui.emulator_state == EmulatorState::Running;
- use egui_material_icons::icons;
+ use egui_phosphor::regular as icons;
if ui
.add_enabled(
is_paused,
- egui::Button::new(format!("{} Continue", icons::ICON_PLAY_ARROW)),
+ egui::Button::new(format!("{} Continue", icons::PLAY)),
)
.clicked()
{
@@ -243,21 +265,21 @@ impl RenderState {
ui.close();
}
if ui
- .add_enabled(is_running, egui::Button::new(format!("{} Pause", icons::ICON_PAUSE)))
+ .add_enabled(is_running, egui::Button::new(format!("{} Pause", icons::PAUSE)))
.clicked()
{
debugger_ui.emulator_state = EmulatorState::Paused;
ui.close();
}
if ui
- .add_enabled(is_paused, egui::Button::new(format!("{} Step", icons::ICON_SKIP_NEXT)))
+ .add_enabled(is_paused, egui::Button::new(format!("{} Step", icons::SKIP_FORWARD)))
.clicked()
{
debugger_ui.emulator_state = EmulatorState::Step;
ui.close();
}
if ui
- .button(format!("{} Run Until VSync", icons::ICON_FAST_FORWARD))
+ .button(format!("{} Run Until VSync", icons::FAST_FORWARD))
.clicked()
{
debugger_ui.emulator_state = EmulatorState::RunUntilVsync;
@@ -269,6 +291,7 @@ impl RenderState {
ui.checkbox(&mut debugger_ui.show_cpu, "CPU");
ui.checkbox(&mut debugger_ui.show_gx_state, "GX");
ui.checkbox(&mut debugger_ui.show_mmio, "MMIO");
+ ui.checkbox(&mut debugger_ui.show_dvd, "DVD");
ui.checkbox(&mut debugger_ui.show_exi, "EXI");
ui.checkbox(&mut debugger_ui.show_irqs, "IRQ");
ui.checkbox(&mut debugger_ui.show_controls, "Controls");
@@ -280,13 +303,25 @@ impl RenderState {
windows::cpu::show_cpu(&ctx, &mut debugger_ui.show_cpu, cpu, mmio);
}
if debugger_ui.show_controls {
+ let mut start_trace = false;
+ let mut stop_trace = false;
+ let tracing = debugger_ui.is_tracing();
windows::controls::show_controls(
&ctx,
&mut debugger_ui.show_controls,
&mut debugger_ui.emulator_state,
&mut debugger_ui.run_until_addr_input,
&mut debugger_ui.dvd_cover_open,
+ tracing,
+ &mut start_trace,
+ &mut stop_trace,
);
+ if start_trace {
+ debugger_ui.start_trace();
+ }
+ if stop_trace {
+ debugger_ui.stop_trace();
+ }
}
if debugger_ui.show_gx_state {
windows::gx::show_gx(&ctx, &mut debugger_ui.show_gx_state, gx, mmio);
@@ -300,6 +335,9 @@ impl RenderState {
mmio,
);
}
+ if debugger_ui.show_dvd {
+ windows::dvd::show_dvd(&ctx, &mut debugger_ui.show_dvd, &emulator.di);
+ }
if debugger_ui.show_exi {
windows::exi::show_exi(&ctx, &mut debugger_ui.show_exi, &emulator.exi);
}
diff --git a/crates/debugger/src/windows.rs b/crates/debugger/src/windows.rs
index efb0e18..ae54615 100644
--- a/crates/debugger/src/windows.rs
+++ b/crates/debugger/src/windows.rs
@@ -1,6 +1,17 @@
pub mod controls;
pub mod cpu;
+pub mod dvd;
pub mod exi;
pub mod gx;
pub mod irq;
pub mod mmio;
+
+pub(crate) fn flag(ui: &mut egui::Ui, val: bool) {
+ use egui::{Color32, RichText};
+ let (icon, color) = if val {
+ (egui_phosphor::regular::CHECK_CIRCLE, Color32::from_rgb(100, 220, 100))
+ } else {
+ (egui_phosphor::regular::CIRCLE, Color32::from_rgb(70, 70, 70))
+ };
+ ui.label(RichText::new(icon).color(color));
+}
diff --git a/crates/debugger/src/windows/controls.rs b/crates/debugger/src/windows/controls.rs
index 0e93b08..8148c4e 100644
--- a/crates/debugger/src/windows/controls.rs
+++ b/crates/debugger/src/windows/controls.rs
@@ -1,5 +1,5 @@
use egui::Context;
-use egui_material_icons::icons;
+use egui_phosphor::regular as icons;
use crate::debugger::EmulatorState;
@@ -9,6 +9,9 @@ pub fn show_controls(
state: &mut EmulatorState,
run_until_addr_input: &mut String,
dvd_cover_open: &mut Option<bool>,
+ tracing: bool,
+ start_trace: &mut bool,
+ stop_trace: &mut bool,
) {
egui::Window::new("Controls")
.open(open)
@@ -25,7 +28,7 @@ pub fn show_controls(
if ui
.add_enabled(
is_paused,
- egui::Button::new(format!("{} Continue", icons::ICON_PLAY_ARROW)).min_size(btn_size),
+ egui::Button::new(format!("{} Continue", icons::PLAY)).min_size(btn_size),
)
.clicked()
{
@@ -35,7 +38,7 @@ pub fn show_controls(
if ui
.add_enabled(
is_running,
- egui::Button::new(format!("{} Pause", icons::ICON_PAUSE)).min_size(btn_size),
+ egui::Button::new(format!("{} Pause", icons::PAUSE)).min_size(btn_size),
)
.clicked()
{
@@ -45,7 +48,7 @@ pub fn show_controls(
if ui
.add_enabled(
is_paused,
- egui::Button::new(format!("{} Step", icons::ICON_SKIP_NEXT)).min_size(btn_size),
+ egui::Button::new(format!("{} Step", icons::SKIP_FORWARD)).min_size(btn_size),
)
.clicked()
{
@@ -53,7 +56,7 @@ pub fn show_controls(
}
if ui
- .add(egui::Button::new(format!("{} Run Until VSync", icons::ICON_FAST_FORWARD)).min_size(btn_size))
+ .add(egui::Button::new(format!("{} Run Until VSync", icons::FAST_FORWARD)).min_size(btn_size))
.clicked()
{
*state = EmulatorState::RunUntilVsync;
@@ -69,7 +72,7 @@ pub fn show_controls(
.show(ui);
if ui
- .add(egui::Button::new(format!("{} Run", icons::ICON_PLAY_ARROW)))
+ .add(egui::Button::new(format!("{} Run", icons::PLAY)))
.clicked()
{
let s = run_until_addr_input.trim().trim_start_matches("0x");
@@ -82,17 +85,38 @@ pub fn show_controls(
ui.separator();
if ui
- .add(egui::Button::new(format!("{} Open Cover", icons::ICON_EJECT)).min_size(btn_size))
+ .add(egui::Button::new(format!("{} Open Cover", icons::EJECT)).min_size(btn_size))
.clicked()
{
*dvd_cover_open = Some(true);
}
if ui
- .add(egui::Button::new(format!("{} Close Cover", icons::ICON_DISC_FULL)).min_size(btn_size))
+ .add(egui::Button::new(format!("{} Close Cover", icons::DISC)).min_size(btn_size))
.clicked()
{
*dvd_cover_open = Some(false);
}
+
+ ui.separator();
+
+ if !tracing {
+ if ui
+ .add(
+ egui::Button::new(format!("{} Start Trace", icons::RECORD))
+ .min_size(btn_size),
+ )
+ .clicked()
+ {
+ *start_trace = true;
+ }
+ } else {
+ if ui
+ .add(egui::Button::new(format!("{} Stop Trace", icons::STOP)).min_size(btn_size))
+ .clicked()
+ {
+ *stop_trace = true;
+ }
+ }
});
}
diff --git a/crates/debugger/src/windows/cpu.rs b/crates/debugger/src/windows/cpu.rs
index 5336958..ba55ee6 100644
--- a/crates/debugger/src/windows/cpu.rs
+++ b/crates/debugger/src/windows/cpu.rs
@@ -106,7 +106,7 @@ pub fn show_cpu(ctx: &Context, open: &mut bool, cpu: &Cpu, mmio: &Mmio) {
let indicator_resp = if is_pc {
Some(
ui.label(
- RichText::new(egui_material_icons::icons::ICON_PLAY_ARROW)
+ RichText::new(egui_phosphor::regular::PLAY)
.color(Color32::from_rgb(120, 220, 120)),
),
)
diff --git a/crates/debugger/src/windows/dvd.rs b/crates/debugger/src/windows/dvd.rs
new file mode 100644
index 0000000..4541d4d
--- /dev/null
+++ b/crates/debugger/src/windows/dvd.rs
@@ -0,0 +1,128 @@
+use egui::{Context, Grid};
+use gecko::dvd::DvdInterface;
+
+use super::flag;
+
+pub fn show_dvd(ctx: &Context, open: &mut bool, di: &DvdInterface) {
+ egui::Window::new("DVD")
+ .open(open)
+ .default_size(egui::vec2(350.0, 300.0))
+ .show(ctx, |ui| {
+ // Disc info
+ if let Some(dvd) = &di.dvd {
+ let game_name = String::from_utf8_lossy(&dvd.header.game_name);
+ let game_name = game_name.trim_end_matches('\0');
+ let game_code = String::from_utf8_lossy(&dvd.header.game_code);
+ let maker_code = String::from_utf8_lossy(&dvd.header.maker_code);
+
+ ui.strong("Disc");
+ Grid::new("dvd_disc_info").num_columns(2).striped(true).show(ui, |ui| {
+ ui.label("Game");
+ ui.monospace(game_name);
+ ui.end_row();
+
+ ui.label("ID");
+ ui.monospace(format!("{}{} v{}", game_code, maker_code, dvd.header.version));
+ ui.end_row();
+ });
+ } else {
+ ui.label("No disc inserted");
+ }
+
+ ui.separator();
+
+ // Status register
+ ui.strong("Status (DISR)");
+ Grid::new("dvd_status").num_columns(4).striped(true).show(ui, |ui| {
+ ui.label("TCINT");
+ flag(ui, di.status.transfer_complete());
+ ui.label("Mask");
+ flag(ui, di.status.transfer_complete_mask());
+ ui.end_row();
+
+ ui.label("DEINT");
+ flag(ui, di.status.device_error());
+ ui.label("Mask");
+ flag(ui, di.status.device_error_mask());
+ ui.end_row();
+
+ ui.label("BRKINT");
+ flag(ui, di.status.break_complete());
+ ui.label("Mask");
+ flag(ui, di.status.break_complete_mask());
+ ui.end_row();
+
+ ui.label("BRK");
+ flag(ui, di.status.brk());
+ ui.label("");
+ ui.label("");
+ ui.end_row();
+ });
+
+ ui.add_space(4.0);
+
+ // Cover register
+ ui.strong("Cover (DICVR)");
+ Grid::new("dvd_cover").num_columns(4).striped(true).show(ui, |ui| {
+ ui.label("Cover");
+ flag(ui, di.cover.cover_status());
+ ui.label("CVRINT");
+ flag(ui, di.cover.cover_interrupt());
+ ui.end_row();
+
+ ui.label("Mask");
+ flag(ui, di.cover.cover_interrupt_mask());
+ ui.label("");
+ ui.label("");
+ ui.end_row();
+ });
+
+ ui.add_space(4.0);
+
+ // Control register
+ ui.strong("Control (DICR)");
+ Grid::new("dvd_control").num_columns(4).striped(true).show(ui, |ui| {
+ ui.label("TSTART");
+ flag(ui, di.control.tstart());
+ ui.label("DMA");
+ ui.monospace(format!("{:?}", di.control.dma()));
+ ui.end_row();
+
+ ui.label("RW");
+ ui.monospace(format!("{:?}", di.control.access_mode()));
+ ui.label("");
+ ui.label("");
+ ui.end_row();
+ });
+
+ ui.add_space(4.0);
+
+ // Command buffers & DMA
+ ui.strong("Command / DMA");
+ Grid::new("dvd_cmd_dma").num_columns(2).striped(true).show(ui, |ui| {
+ ui.label("CMDBUF0");
+ ui.monospace(format!("{:#010X}", di.cmdbuf0));
+ ui.end_row();
+
+ ui.label("CMDBUF1");
+ ui.monospace(format!("{:#010X}", di.cmdbuf1));
+ ui.end_row();
+
+ ui.label("CMDBUF2");
+ ui.monospace(format!("{:#010X}", di.cmdbuf2));
+ ui.end_row();
+
+ ui.label("IMMBUF");
+ ui.monospace(format!("{:#010X}", di.immbuf));
+ ui.end_row();
+
+ ui.label("DMA Address");
+ ui.monospace(format!("{:#010X}", di.dma_address.address()));
+ ui.end_row();
+
+ ui.label("DMA Length");
+ ui.monospace(format!("{:#010X}", di.dma_length.length()));
+ ui.end_row();
+ });
+ });
+}
diff --git a/crates/debugger/src/windows/exi.rs b/crates/debugger/src/windows/exi.rs
index bb6f24c..5afbc0a 100644
--- a/crates/debugger/src/windows/exi.rs
+++ b/crates/debugger/src/windows/exi.rs
@@ -1,15 +1,7 @@
-use egui::{Color32, Context, Grid, RichText};
-use egui_material_icons::icons;
+use egui::{Context, Grid};
use gecko::flipper::exi::ExternalInterface;
-fn flag(ui: &mut egui::Ui, val: bool) {
- let (icon, color) = if val {
- (icons::ICON_RADIO_BUTTON_CHECKED, Color32::from_rgb(100, 220, 100))
- } else {
- (icons::ICON_RADIO_BUTTON_UNCHECKED, Color32::from_rgb(70, 70, 70))
- };
- ui.label(RichText::new(icon).color(color));
-}
+use super::flag;
fn chip_select_str(cs: u8) -> &'static str {
match cs {
diff --git a/crates/debugger/src/windows/gx.rs b/crates/debugger/src/windows/gx.rs
index 04694f4..9cbc074 100644
--- a/crates/debugger/src/windows/gx.rs
+++ b/crates/debugger/src/windows/gx.rs
@@ -1,5 +1,4 @@
use egui::{Context, Grid, RichText, ScrollArea};
-use egui_material_icons::icons;
use gecko::flipper::gx::GraphicsProcessor;
use gecko::flipper::gx::draw::TextureDescriptor;
use gecko::mmio::Mmio;
@@ -182,7 +181,7 @@ pub fn show_gx(ctx: &Context, open: &mut bool, gx: &GraphicsProcessor, mmio: &Mm
if call.vertices.len() > preview {
ui.label(format!(
"{} {} more vertices",
- icons::ICON_MORE_HORIZ,
+ egui_phosphor::regular::DOTS_THREE,
call.vertices.len() - preview,
));
}
diff --git a/crates/debugger/src/windows/irq.rs b/crates/debugger/src/windows/irq.rs
index 80571b6..4223775 100644
--- a/crates/debugger/src/windows/irq.rs
+++ b/crates/debugger/src/windows/irq.rs
@@ -1,5 +1,4 @@
use egui::{Align, Color32, Context, Grid, Layout, RichText};
-use egui_material_icons::icons;
use gecko::cpu::Cpu;
use gecko::flipper::pi::ProcessorInterface;
@@ -17,15 +16,15 @@ fn interrupt_row(ui: &mut egui::Ui, name: &str, pending: bool, masked: bool) {
};
let (pending_icon, pending_color) = if pending {
- (icons::ICON_RADIO_BUTTON_CHECKED, Color32::from_rgb(255, 180, 60))
+ (egui_phosphor::regular::CHECK_CIRCLE, Color32::from_rgb(255, 180, 60))
} else {
- (icons::ICON_RADIO_BUTTON_UNCHECKED, Color32::from_rgb(70, 70, 70))
+ (egui_phosphor::regular::CIRCLE, Color32::from_rgb(70, 70, 70))
};
let (mask_icon, mask_color) = if masked {
- (icons::ICON_CHECK_BOX, Color32::from_rgb(100, 180, 255))
+ (egui_phosphor::regular::CHECK_SQUARE, Color32::from_rgb(100, 180, 255))
} else {
- (icons::ICON_CHECK_BOX_OUTLINE_BLANK, Color32::from_rgb(70, 70, 70))
+ (egui_phosphor::regular::SQUARE, Color32::from_rgb(70, 70, 70))
};
ui.label(RichText::new(name).color(name_color));
@@ -48,9 +47,9 @@ pub fn show_irq(ctx: &Context, open: &mut bool, cpu: &Cpu, pi: &ProcessorInterfa
ui.label("MSR.EE");
ui.horizontal(|ui| {
let (icon, color) = if ee {
- (icons::ICON_CHECK_CIRCLE, Color32::from_rgb(80, 220, 80))
+ (egui_phosphor::regular::CHECK_CIRCLE, Color32::from_rgb(80, 220, 80))
} else {
- (icons::ICON_CANCEL, Color32::from_rgb(200, 60, 60))
+ (egui_phosphor::regular::X_CIRCLE, Color32::from_rgb(200, 60, 60))
};
ui.label(RichText::new(icon).color(color));
ui.label(if ee {
diff --git a/crates/debugger/src/windows/mmio.rs b/crates/debugger/src/windows/mmio.rs
index 387554c..6f64865 100644
--- a/crates/debugger/src/windows/mmio.rs
+++ b/crates/debugger/src/windows/mmio.rs
@@ -1,5 +1,4 @@
use egui::{Context, Grid, ScrollArea};
-use egui_material_icons::icons;
use gecko::mmio::Mmio;
pub fn show_mmio(ctx: &Context, open: &mut bool, base: &mut u32, addr_input: &mut String, mmio: &Mmio) {
@@ -18,11 +17,11 @@ pub fn show_mmio(ctx: &Context, open: &mut bool, base: &mut u32, addr_input: &mu
*base = addr & !0xF;
}
}
- if ui.button(icons::ICON_CHEVRON_LEFT).clicked() {
+ if ui.button(egui_phosphor::regular::CARET_LEFT).clicked() {
*base = base.saturating_sub(256);
*addr_input = format!("{:08X}", base);
}
- if ui.button(icons::ICON_CHEVRON_RIGHT).clicked() {
+ if ui.button(egui_phosphor::regular::CARET_RIGHT).clicked() {
*base = base.saturating_add(256);
*addr_input = format!("{:08X}", base);
}
diff --git a/crates/gecko/src/dvd.rs b/crates/gecko/src/dvd.rs
index 9f3623d..9fe9a7e 100644
--- a/crates/gecko/src/dvd.rs
+++ b/crates/gecko/src/dvd.rs
@@ -1,11 +1,32 @@
pub mod regs;
+use image::Executable;
+
+use crate::gamecube::GameCube;
use crate::mmio::constants::DI_BASE;
use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};
+const DICMDBUF0: u32 = DI_BASE + 0x08;
+const DICMDBUF1: u32 = DI_BASE + 0x0C;
+const DICMDBUF2: u32 = DI_BASE + 0x10;
+const DIIMMBUF: u32 = DI_BASE + 0x20;
+
+pub enum Command {
+ ReadDiskId,
+}
+
pub struct DvdInterface {
pub status: regs::DiStatusRegister,
pub cover: regs::DiCoverRegister,
+ pub dma_address: regs::DiDmaAddressRegister,
+ pub dma_length: regs::DiDmaLengthRegister,
+ pub control: regs::DiControlRegister,
+ pub cmdbuf0: u32,
+ pub cmdbuf1: u32,
+ pub cmdbuf2: u32,
+ pub immbuf: u32,
+ pub transfer_started: bool,
+ pub dvd: Option<image::dvd::Dvd>,
}
impl DvdInterface {
@@ -13,6 +34,15 @@ impl DvdInterface {
Self {
status: regs::DiStatusRegister::from_raw(0),
cover: regs::DiCoverRegister::from_raw(0),
+ dma_address: regs::DiDmaAddressRegister::from_raw(0),
+ dma_length: regs::DiDmaLengthRegister::from_raw(0),
+ control: regs::DiControlRegister::from_raw(0),
+ cmdbuf0: 0,
+ cmdbuf1: 0,
+ cmdbuf2: 0,
+ immbuf: 0,
+ transfer_started: false,
+ dvd: None,
}
}
@@ -22,13 +52,194 @@ impl DvdInterface {
|| (self.status.transfer_complete() && self.status.transfer_complete_mask())
|| (self.cover.cover_interrupt() && self.cover.cover_interrupt_mask())
}
+
+ #[inline(always)]
+ fn resolve_command(&mut self) -> Option<Command> {
+ let cmd = self.cmdbuf0 >> 24;
+ let sub1 = (self.cmdbuf0 >> 16) & 0xFF;
+ let sub2 = self.cmdbuf0 & 0xFFFF;
+
+ match (cmd, sub2) {
+ (0xA8, 0x0040) => Some(Command::ReadDiskId),
+ _ => {
+ tracing::error!(
+ cmd = format!("{cmd:02X}"),
+ sub1 = format!("{sub1:02X}"),
+ sub2 = format!("{sub2:04X}"),
+ cmdbuf1 = format!("{:08X}", self.cmdbuf1),
+ cmdbuf2 = format!("{:08X}", self.cmdbuf2),
+ "Unknown DI command"
+ );
+ None
+ }
+ }
+ }
+}
+
+impl GameCube {
+ #[inline(always)]
+ fn process_dvd_command(&mut self, cmd: Command, dvd: &image::dvd::Dvd) {
+ match cmd {
+ Command::ReadDiskId => {
+ let src = self.di.cmdbuf1;
+ let dst = self.di.dma_address.address();
+ let len = self.di.dma_length.length() as usize;
+
+ let buffer = self.mmio.phys_slice_mut(dst, len);
+ buffer.copy_from_slice(&dvd.data()[..len]);
+
+ tracing::debug!(
+ src = format!("{:08X}", src),
+ dst = format!("{:08X}", dst),
+ len = format!("{:08X}", len),
+ "ReadDiskId command"
+ );
+ }
+ }
+ }
+
+ #[inline(always)]
+ pub fn start_dvd_transfer(&mut self) {
+ if !self.di.transfer_started {
+ return;
+ }
+
+ let Some(dvd) = self.di.dvd.take() else {
+ self.di.status.set_device_error(true);
+ self.di.control.set_tstart(false);
+ self.di.transfer_started = false;
+ return;
+ };
+
+ if let Some(cmd) = self.di.resolve_command() {
+ self.process_dvd_command(cmd, &dvd);
+ }
+
+ self.di.dvd = Some(dvd);
+ self.di.transfer_started = false;
+
+ // The interrupt must not fire until some time!!
+ // This is important as it will break the IPL if we immediately raise it...
+ // When DVDReadDiskID issues the DI command, it returns to BS2_Tick which updates
+ // the internal state machine and then calls into restore_irq. The code assumes
+ // that the interrupt wont fire until after it has returned and updated the state.
+ // Else it will get trapped inside restore_irq, right after mtmsr, executing in a loop
+ // of the DVD dispatch handler which in turn re-issues the same command...
+ const DI_TRANSFER_DELAY: u64 = 10_000; // Based off of vxpm and hazel
+ self.scheduler
+ .schedule_in(DI_TRANSFER_DELAY, crate::scheduler::EventKind::DiTransferComplete);
+ }
+
+ pub fn complete_dvd_transfer(&mut self) {
+ self.di.control.set_tstart(false);
+ self.di.status.set_transfer_complete(true);
+ self.check_di_interrupts();
+ }
+
+ pub fn insert_dvd(&mut self, dvd: image::dvd::Dvd) {
+ tracing::info!("DVD inserted: {}", String::from_utf8_lossy(&dvd.header.game_name));
+ self.di.dvd = Some(dvd);
+ self.close_cover();
+ }
}
impl MmioRw for DvdInterface {
const BASE: u32 = DI_BASE;
- const NAME: &'static str = "DI";
+ const NAME: &'static str = "DVD";
+
+ fn read_raw(&mut self, addr: u32, access_size: u32) -> Option<u32> {
+ if <regs::DiStatusRegister as MmioRegister>::fits(addr, access_size) {
+ return Some(<regs::DiStatusRegister as MmioAccess<Self>>::read_at(
+ self,
+ addr,
+ access_size,
+ ));
+ }
+ if <regs::DiCoverRegister as MmioRegister>::fits(addr, access_size) {
+ return Some(<regs::DiCoverRegister as MmioAccess<Self>>::read_at(
+ self,
+ addr,
+ access_size,
+ ));
+ }
+ if <regs::DiDmaAddressRegister as MmioRegister>::fits(addr, access_size) {
+ return Some(<regs::DiDmaAddressRegister as MmioAccess<Self>>::read_at(
+ self,
+ addr,
+ access_size,
+ ));
+ }
+ if <regs::DiDmaLengthRegister as MmioRegister>::fits(addr, access_size) {
+ return Some(<regs::DiDmaLengthRegister as MmioAccess<Self>>::read_at(
+ self,
+ addr,
+ access_size,
+ ));
+ }
+ if <regs::DiControlRegister as MmioRegister>::fits(addr, access_size) {
+ return Some(<regs::DiControlRegister as MmioAccess<Self>>::read_at(
+ self,
+ addr,
+ access_size,
+ ));
+ }
- crate::impl_mmio_dispatch!(regs::DiStatusRegister, regs::DiCoverRegister,);
+ match addr {
+ DICMDBUF0 if access_size == 4 => Some(self.cmdbuf0),
+ DICMDBUF1 if access_size == 4 => Some(self.cmdbuf1),
+ DICMDBUF2 if access_size == 4 => Some(self.cmdbuf2),
+ DIIMMBUF if access_size == 4 => Some(self.immbuf),
+ _ => None,
+ }
+ }
+
+ fn write_raw(&mut self, addr: u32, access_size: u32, val: u32) -> bool {
+ if <regs::DiStatusRegister as MmioRegister>::fits(addr, access_size) {
+ <regs::DiStatusRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);
+ return true;
+ }
+ if <regs::DiCoverRegister as MmioRegister>::fits(addr, access_size) {
+ <regs::DiCoverRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);
+ return true;
+ }
+ if <regs::DiDmaAddressRegister as MmioRegister>::fits(addr, access_size) {
+ <regs::DiDmaAddressRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);
+ return true;
+ }
+ if <regs::DiDmaLengthRegister as MmioRegister>::fits(addr, access_size) {
+ <regs::DiDmaLengthRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);
+ return true;
+ }
+ if <regs::DiControlRegister as MmioRegister>::fits(addr, access_size) {
+ <regs::DiControlRegister as MmioAccess<Self>>::write_at(self, addr, access_size, val);
+ return true;
+ }
+
+ match addr {
+ DICMDBUF0 if access_size == 4 => {
+ self.cmdbuf0 = val;
+ tracing::debug!(
+ cmd = format!("{:02X}", val >> 24),
+ sub1 = format!("{:02X}", (val >> 16) & 0xFF),
+ sub2 = format!("{:04X}", val & 0xFFFF),
+ "DICMDBUF0 write"
+ );
+ }
+ DICMDBUF1 if access_size == 4 => {
+ self.cmdbuf1 = val;
+ tracing::debug!(val = format!("{val:08X}"), "DICMDBUF1 write");
+ }
+ DICMDBUF2 if access_size == 4 => {
+ self.cmdbuf2 = val;
+ tracing::debug!(val = format!("{val:08X}"), "DICMDBUF2 write");
+ }
+ DIIMMBUF if access_size == 4 => {
+ self.immbuf = val;
+ }
+ _ => return false,
+ }
+ true
+ }
}
impl crate::gamecube::GameCube {
diff --git a/crates/gecko/src/dvd/regs.rs b/crates/gecko/src/dvd/regs.rs
index 411982f..a6d9119 100644
--- a/crates/gecko/src/dvd/regs.rs
+++ b/crates/gecko/src/dvd/regs.rs
@@ -1,3 +1,5 @@
+use chapa::BitEnum;
+
use super::DvdInterface;
use crate::mmio::traits::MmioAccess;
@@ -92,3 +94,82 @@ impl MmioAccess<DvdInterface> for DiCoverRegister {
di.cover = cvr;
}
}
+
+// 0xCC006014 4 R/W DIMAR - DMA Memory Address Register
+
+crate::mmio_register! {
+ DiDmaAddressRegister: u32 @ 0xCC006014 {
+ #[bits(0..=25)]
+ pub address: u32,
+ }
+}
+
+impl MmioAccess<DvdInterface> for DiDmaAddressRegister {
+ fn read(di: &DvdInterface) -> Self {
+ di.dma_address
+ }
+
+ fn write(self, di: &mut DvdInterface) {
+ di.dma_address = self;
+ }
+}
+
+// 0xCC006018 4 R/W DILENGTH - DI DMA Transfer Length Register
+
+crate::mmio_register! {
+ DiDmaLengthRegister: u32 @ 0xCC006018 {
+ #[bits(0..=25)]
+ pub length: u32,
+ }
+}
+
+impl MmioAccess<DvdInterface> for DiDmaLengthRegister {
+ fn read(di: &DvdInterface) -> Self {
+ di.dma_length
+ }
+
+ fn write(self, di: &mut DvdInterface) {
+ di.dma_length = self;
+ }
+}
+
+// 0xCC00601C 4 R/W DICR - DI Control Register
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum TransferMode {
+ Immediate = 0,
+ Dma = 1,
+}
+
+#[derive(BitEnum, Debug, PartialEq, Eq)]
+pub enum AccessMode {
+ Read = 0,
+ Write = 1,
+}
+
+crate::mmio_register! {
+ DiControlRegister: u32 @ 0xCC00601C {
+ #[bits(0)]
+ pub tstart: bool,
+
+ #[bits(1)]
+ pub dma: TransferMode,
+
+ #[bits(2)]
+ pub access_mode: AccessMode,
+ }
+}
+
+impl MmioAccess<DvdInterface> for DiControlRegister {
+ fn read(di: &DvdInterface) -> Self {
+ di.control
+ }
+
+ fn write(self, di: &mut DvdInterface) {
+ di.control = self;
+
+ if self.tstart() {
+ di.transfer_started = true;
+ }
+ }
+}
diff --git a/crates/gecko/src/flipper/vi.rs b/crates/gecko/src/flipper/vi.rs
index c610fa2..b763808 100644
--- a/crates/gecko/src/flipper/vi.rs
+++ b/crates/gecko/src/flipper/vi.rs
@@ -249,8 +249,7 @@ impl GameCube {
let ticks_per_hl = self.vi.ticks_per_half_line();
if ticks_per_hl > 0 {
self.vi.half_line_scheduled = true;
- let next = self.scheduler.cycles + ticks_per_hl;
- self.scheduler.schedule_at(next, EventKind::ViHalfLine);
+ self.scheduler.schedule_in(ticks_per_hl, EventKind::ViHalfLine);
}
}
}
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index 2d62f0c..cb27521 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -155,15 +155,14 @@ impl GameCube {
emulator
}
- #[inline]
+ #[inline(always)]
pub fn step(&mut self) {
// Fire any events that are due
while let Some(event) = self.scheduler.poll() {
match event {
EventKind::VSync => {
self.vsync_pending = true;
- let next = self.scheduler.cycles + CYCLES_PER_VSYNC;
- self.scheduler.schedule_at(next, EventKind::VSync);
+ self.scheduler.schedule_in(CYCLES_PER_VSYNC, EventKind::VSync);
}
EventKind::ViHalfLine => {
self.vi.on_half_line(self.scheduler.cycles);
@@ -171,6 +170,9 @@ impl GameCube {
self.maybe_schedule_vi_half_line();
self.check_vi_interrupts();
}
+ EventKind::DiTransferComplete => {
+ self.complete_dvd_transfer();
+ }
}
}
@@ -181,15 +183,6 @@ impl GameCube {
return;
}
- // TODO: hack IPL state machine
- // if self.cpu.pc == 0x81301284 {
- // self.cpu.pc += 4;
- // }
-
- // if self.cpu.pc == 0x81300BD8 {
- // self.cpu.pc += 4;
- // }
-
// CPU pre-hook
#[cfg(feature = "scripting")]
if self.script_hook_flags.contains(HookFlags::CPU_PRE) {
@@ -244,11 +237,15 @@ impl GameCube {
self.cpu.pc = self.cpu.nia;
}
- pub fn run_until_vsync(&mut self) {
+ #[inline(always)]
+ pub fn prepare_frame(&mut self) {
self.vsync_pending = false;
- // Update SI controller input at the start of each frame
self.si.update_polling();
self.check_si_interrupts();
+ }
+
+ pub fn run_until_vsync(&mut self) {
+ self.prepare_frame();
while !self.vsync_pending {
self.step();
}
@@ -256,6 +253,7 @@ impl GameCube {
/// Read the instructions in `[start, end]` and check whether the loop is a
/// side effect free MMIO polling loop that can safely be skipped.
+ #[inline(always)]
fn is_polling_loop(&self, start: u32, end: u32) -> bool {
let count = ((end - start) / 4 + 1) as usize;
let mut buf = [0u32; IDLE_LOOP_MAX_INSTRS];
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index d31ea78..2a7f580 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -271,6 +271,7 @@ impl GameCube {
}
BusTarget::Di => {
self.di.mmio_write_u8(offset, val);
+ self.start_dvd_transfer();
self.check_di_interrupts();
}
BusTarget::Si => {
@@ -336,6 +337,7 @@ impl GameCube {
}
BusTarget::Di => {
self.di.mmio_write_u16(offset, val);
+ self.start_dvd_transfer();
self.check_di_interrupts();
}
BusTarget::Si => {
@@ -402,6 +404,7 @@ impl GameCube {
}
BusTarget::Di => {
self.di.mmio_write_u32(offset, val);
+ self.start_dvd_transfer();
self.check_di_interrupts();
}
BusTarget::Si => {
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 0b97ea0..479715d 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -8,6 +8,7 @@ pub const TIMEBASE_DIVISOR: u64 = 12;
pub enum EventKind {
VSync,
ViHalfLine,
+ DiTransferComplete,
}
pub struct Scheduler {
@@ -55,6 +56,11 @@ impl Scheduler {
self.events.push(Reverse((deadline, kind)));
}
+ pub fn schedule_in(&mut self, delay: u64, kind: EventKind) {
+ let deadline = self.cycles + delay;
+ self.schedule_at(deadline, kind);
+ }
+
pub fn poll(&mut self) -> Option<EventKind> {
if self.events.peek().map_or(false, |Reverse((d, _))| self.cycles >= *d) {
Some(self.events.pop().unwrap().0.1)
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index bd209ae..a072139 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -3,7 +3,7 @@ use egui_plot::{Line, Plot, PlotPoints};
use gecko::flipper::si::pad::{self, PadStatus, STICK_CENTER};
use gecko::flipper::vi::regs::RefreshRate;
use gecko::gamecube::GameCube;
-use image::Dol;
+use image::{Dol, dvd::Dvd};
use std::collections::VecDeque;
use std::env;
use std::sync::Arc;
@@ -489,6 +489,7 @@ fn main() {
.position(|a| a == "--rom")
.map(|i| &args[i + 1])
.or_else(|| args.get(1).filter(|a| !a.starts_with("--")));
+ let iso_path = args.iter().position(|a| a == "--iso").map(|i| &args[i + 1]);
#[cfg(feature = "scripting")]
let script_path = args.iter().position(|a| a == "--script").map(|i| &args[i + 1]);
@@ -512,12 +513,22 @@ fn main() {
GameCube::with_image(&dol, idle_skip)
} else {
eprintln!(
- "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--immediate] [--idle-skip]",
+ "usage: {} <path/to/game.dol> | --ipl <path> | --rom <path> [--iso <path>] [--immediate] [--idle-skip]",
args[0]
);
std::process::exit(1);
};
+ if let Some(iso) = iso_path {
+ if ipl_path.is_none() {
+ eprintln!("--iso requires --ipl");
+ std::process::exit(1);
+ }
+ let iso_data = std::fs::read(iso).expect("failed to read ISO");
+ let dvd = Dvd::parse(iso_data);
+ emulator.insert_dvd(dvd);
+ }
+
#[cfg(feature = "scripting")]
if let Some(path) = script_path {
let host = scripting::LuaScriptHost::from_file(path).expect("failed to load script");
diff --git a/crates/tinyemu/Cargo.toml b/crates/tinyemu/Cargo.toml
index 6905d2b..846b66b 100644
--- a/crates/tinyemu/Cargo.toml
+++ b/crates/tinyemu/Cargo.toml
@@ -3,8 +3,13 @@ name = "tinyemu"
version = "0.1.0"
edition = "2024"
+[features]
+scripting = ["gecko/scripting", "dep:scripting"]
+scripting-mut-traps = ["scripting", "gecko/scripting-mut-traps", "scripting/scripting-mut-traps"]
+
[dependencies]
gecko = { path = "../gecko" }
+scripting = { path = "../scripting", optional = true }
image = { path = "../image" }
colored = "3.1.1"
disasm = { path = "../disasm" }
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 797058c..a6bd078 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -19,6 +19,10 @@ struct Args {
#[arg(long)]
ipl: Option<String>,
+ /// Path to a GameCube ISO file
+ #[arg(long)]
+ iso: Option<String>,
+
/// Print decoded instructions and register diffs after each step
#[arg(long)]
debug: bool,
@@ -42,6 +46,11 @@ struct Args {
/// Disable ANSI escape codes
#[arg(long)]
no_ansi: bool,
+
+ /// Path to a Lua script for scripting hooks
+ #[cfg(feature = "scripting")]
+ #[arg(long)]
+ script: Option<String>,
}
fn parse_hex_addr(s: &str) -> Result<u32, String> {
@@ -71,6 +80,21 @@ fn main() {
panic!("Either --rom or --ipl must be provided");
};
+ if let Some(iso_path) = &args.iso {
+ if args.ipl.is_none() {
+ panic!("--iso requires --ipl");
+ }
+ let iso_data = std::fs::read(iso_path).expect("failed to read ISO");
+ let dvd = image::dvd::Dvd::parse(iso_data);
+ emulator.insert_dvd(dvd);
+ }
+
+ #[cfg(feature = "scripting")]
+ if let Some(path) = &args.script {
+ let host = scripting::LuaScriptHost::from_file(path).expect("failed to load script");
+ emulator.set_script_host(Box::new(host));
+ }
+
let symbols = args.elf.as_ref().map(|path| {
let elf_data = std::fs::read(path).expect("failed to read ELF file");
image::elf::parse_elf_symbols(&elf_data).expect("failed to parse ELF symbols")
diff --git a/scripts/bs2_dvd_state_logger.lua b/scripts/bs2_dvd_state_logger.lua
new file mode 100644
index 0000000..37585b9
--- /dev/null
+++ b/scripts/bs2_dvd_state_logger.lua
@@ -0,0 +1,51 @@
+STATE_MACHINE_VALUES = {
+ [0] = "BS2_START",
+ [1] = "BS2_WAIT_DVD",
+ [2] = "BS2_WAKE_DVD",
+ [3] = "BS2_LOAD_DISKID",
+ [4] = "BS2_WAIT_DISKID",
+ [5] = "BS2_CONFIRMED_COVER_CLOSED",
+ [6] = "BS2_CONFIG_AUDIO_BUFFER",
+ [7] = "BS2_WAIT_AUDIO_BUFFER",
+ [8] = "BS2_LOAD_APPLOADER_HEADER",
+ [9] = "BS2_WAIT_APPLOADER_HEADER",
+ [10] = "BS2_LOAD_APPLOADER",
+ [11] = "BS2_WAIT_APPLOADER",
+ [12] = "BS2_DRIVE_APPLOADER",
+ [13] = "BS2_WAIT_APPLOADER_LOAD",
+ [14] = "BS2_LOAD_BANNER",
+ [15] = "BS2_WAIT_BANNER",
+ [16] = "BS2_RUN_APP",
+ [17] = "BS2_COVER_CLOSED",
+ [18] = "BS2_NO_DISK",
+ [19] = "BS2_COVER_OPEN",
+ [20] = "BS2_STOP_MOTOR",
+ [21] = "BS2_WAIT_STOP_MOTOR",
+ [22] = "BS2_WRONG_DISK",
+ [23] = "BS2_FATAL_ERROR",
+ [24] = "BS2_RETRY",
+ [25] = "BS2_CANCELING",
+ [26] = "BS2_MAX_STATE"
+}
+
+-- local function log_state_machine(emu)
+-- local r3 = emu:gpr(0)
+-- local state = STATE_MACHINE_VALUES[r3] or "Unknown"
+-- log(string.format("[dvd] state machine=%d (%s)", r3, state))
+-- end
+
+local function log_state_machine_write(emu, virt_addr, phys_addr, size, value)
+ local state = STATE_MACHINE_VALUES[value] or "Unknown"
+ log(string.format("[dvd] state machine write: value=%d (%s) from pc=%08X", value, state, emu:pc()))
+end
+
+traps = {
+ -- cpu_post = {
+ -- [0x81300A9C] = log_state_machine,
+ -- },
+ bus_write_pre = {
+ virt = {
+ [0x8145d548] = log_state_machine_write,
+ },
+ },
+}
\ No newline at end of file
diff --git a/scripts/dvd_timecheck.lua b/scripts/dvd_timecheck.lua
index 1f46e37..6895499 100644
--- a/scripts/dvd_timecheck.lua
+++ b/scripts/dvd_timecheck.lua
@@ -10,5 +10,10 @@ end
traps = {
cpu_pre = {
[0x81300BD8] = timebase_log,
- }
+ },
+ bus_write_pre = {
+ virt = {
+ [0x8145d548] = timebase_log,
+ },
+ },
}
\ No newline at end of file