Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

lua dbglib window, fix mailbox busy flags

Layle586c574ee800
diff --git a/crates/dbglib/src/windows.rs b/crates/dbglib/src/windows.rs
index 0c97d75..ebba8df 100644
--- a/crates/dbglib/src/windows.rs
+++ b/crates/dbglib/src/windows.rs
@@ -6,6 +6,7 @@ pub mod dvd;
pub mod exi;
pub mod gx;
pub mod irq;
+pub mod lua;
pub mod mmio;
use disasm::tokenizer::AsmToken;
diff --git a/crates/dbglib/src/windows/lua.rs b/crates/dbglib/src/windows/lua.rs
new file mode 100644
index 0000000..1c02a01
--- /dev/null
+++ b/crates/dbglib/src/windows/lua.rs
@@ -0,0 +1,213 @@
+use std::sync::Arc;
+
+use egui::text::{LayoutJob, TextFormat};
+use egui::{Color32, Context, RichText, ScrollArea, TextBuffer, TextEdit, TextStyle, Ui};
+
+const LUA_KEYWORDS: &[&str] = &[
+ "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil",
+ "not", "or", "repeat", "return", "then", "true", "until", "while",
+];
+
+const LUA_BUILTINS: &[&str] = &[
+ "log",
+ "print",
+ "string",
+ "table",
+ "math",
+ "pairs",
+ "ipairs",
+ "tonumber",
+ "tostring",
+ "type",
+ "error",
+ "assert",
+ "pcall",
+ "xpcall",
+ "select",
+ "unpack",
+ "require",
+ "format",
+ "refresh_traps",
+ "traps",
+];
+
+fn highlight_lua(ui: &Ui, text: &dyn TextBuffer, wrap_width: f32) -> Arc<egui::Galley> {
+ let src = text.as_str();
+ let font_id = TextStyle::Monospace.resolve(ui.style());
+ let mut job = LayoutJob::default();
+ job.wrap.max_width = wrap_width;
+
+ let default_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(220, 220, 220));
+ let keyword_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(200, 120, 220));
+ let builtin_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(100, 180, 255));
+ let string_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(150, 220, 150));
+ let number_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(255, 200, 100));
+ let comment_fmt = TextFormat::simple(font_id.clone(), Color32::from_rgb(120, 120, 120));
+
+ let bytes = src.as_bytes();
+ let len = bytes.len();
+ let mut i = 0;
+
+ while i < len {
+ let b = bytes[i];
+
+ // Line comments: -- (and block comments --[[ ... ]])
+ if b == b'-' && i + 1 < len && bytes[i + 1] == b'-' {
+ let start = i;
+ if i + 3 < len && bytes[i + 2] == b'[' && bytes[i + 3] == b'[' {
+ i += 4;
+ while i + 1 < len {
+ if bytes[i] == b']' && bytes[i + 1] == b']' {
+ i += 2;
+ break;
+ }
+ i += 1;
+ }
+ } else {
+ while i < len && bytes[i] != b'\n' {
+ i += 1;
+ }
+ }
+ job.append(&src[start..i], 0.0, comment_fmt.clone());
+ continue;
+ }
+
+ // Strings: "..." or '...'
+ if b == b'"' || b == b'\'' {
+ let quote = b;
+ let start = i;
+ i += 1;
+ while i < len && bytes[i] != quote {
+ if bytes[i] == b'\\' {
+ i += 1;
+ }
+ i += 1;
+ }
+ if i < len {
+ i += 1;
+ }
+ job.append(&src[start..i], 0.0, string_fmt.clone());
+ continue;
+ }
+
+ // Block strings: [[...]]
+ if b == b'[' && i + 1 < len && bytes[i + 1] == b'[' {
+ let start = i;
+ i += 2;
+ while i + 1 < len {
+ if bytes[i] == b']' && bytes[i + 1] == b']' {
+ i += 2;
+ break;
+ }
+ i += 1;
+ }
+ job.append(&src[start..i], 0.0, string_fmt.clone());
+ continue;
+ }
+
+ // Numbers
+ if b.is_ascii_digit() || (b == b'.' && i + 1 < len && bytes[i + 1].is_ascii_digit()) {
+ let start = i;
+ if b == b'0' && i + 1 < len && (bytes[i + 1] == b'x' || bytes[i + 1] == b'X') {
+ i += 2;
+ while i < len && bytes[i].is_ascii_hexdigit() {
+ i += 1;
+ }
+ } else {
+ while i < len && (bytes[i].is_ascii_digit() || bytes[i] == b'.') {
+ i += 1;
+ }
+ if i < len && (bytes[i] == b'e' || bytes[i] == b'E') {
+ i += 1;
+ if i < len && (bytes[i] == b'+' || bytes[i] == b'-') {
+ i += 1;
+ }
+ while i < len && bytes[i].is_ascii_digit() {
+ i += 1;
+ }
+ }
+ }
+ job.append(&src[start..i], 0.0, number_fmt.clone());
+ continue;
+ }
+
+ // Identifiers / keywords
+ if b.is_ascii_alphabetic() || b == b'_' {
+ let start = i;
+ while i < len && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
+ i += 1;
+ }
+ let word = &src[start..i];
+ let fmt = if LUA_KEYWORDS.contains(&word) {
+ &keyword_fmt
+ } else if LUA_BUILTINS.contains(&word) {
+ &builtin_fmt
+ } else {
+ &default_fmt
+ };
+ job.append(word, 0.0, fmt.clone());
+ continue;
+ }
+
+ // Everything else (handles multi-byte UTF-8)
+ let ch = &src[i..];
+ let c = ch.chars().next().unwrap();
+ job.append(&src[i..i + c.len_utf8()], 0.0, default_fmt.clone());
+ i += c.len_utf8();
+ }
+
+ ui.fonts_mut(|f| f.layout_job(job))
+}
+
+pub fn show_lua(
+ ctx: &Context,
+ open: &mut bool,
+ source: &mut String,
+ logs: &[String],
+ load_script: &mut bool,
+ clear_log: &mut bool,
+) {
+ egui::Window::new("Lua").open(open).show(ctx, |ui| {
+ use egui_phosphor::regular as icons;
+
+ ui.horizontal(|ui| {
+ if ui.button(format!("{} Load", icons::PLAY)).clicked() {
+ *load_script = true;
+ }
+ if ui.button(format!("{} Clear Log", icons::TRASH)).clicked() {
+ *clear_log = true;
+ }
+ });
+
+ ui.separator();
+
+ let avail = ui.available_height();
+ let editor_height = (avail * 0.6).max(120.0);
+
+ let mut layouter = |ui: &Ui, buf: &dyn TextBuffer, wrap_width: f32| highlight_lua(ui, buf, wrap_width);
+
+ ScrollArea::vertical()
+ .id_salt("lua_editor_scroll")
+ .max_height(editor_height)
+ .auto_shrink(false)
+ .show(ui, |ui| {
+ TextEdit::multiline(source)
+ .code_editor()
+ .desired_width(f32::INFINITY)
+ .layouter(&mut layouter)
+ .show(ui);
+ });
+
+ ui.separator();
+
+ ScrollArea::vertical()
+ .id_salt("lua_log_scroll")
+ .auto_shrink(false)
+ .stick_to_bottom(true)
+ .show(ui, |ui| {
+ for line in logs {
+ ui.label(RichText::new(line).monospace().color(Color32::from_rgb(200, 200, 200)));
+ }
+ });
+ });
+}
diff --git a/crates/debugger/src/debugger.rs b/crates/debugger/src/debugger.rs
index 8ee410b..1a1800d 100644
--- a/crates/debugger/src/debugger.rs
+++ b/crates/debugger/src/debugger.rs
@@ -5,6 +5,7 @@ use dbglib::Debugger;
use image::symbols::SymbolTable;
const TRACE_FILENAME: &str = "trace.log";
+const DEFAULT_LUA_SCRIPT: &str = include_str!("../../../scripts/bs2_dvd_state_logger.lua");
pub struct DebuggerUi {
pub debugger: Debugger,
@@ -18,10 +19,14 @@ pub struct DebuggerUi {
pub show_irqs: bool,
pub show_controls: bool,
pub show_callstack: bool,
+ pub show_lua: bool,
pub memory_base: u32,
pub memory_addr_input: String,
pub run_until_addr_input: String,
pub dvd_cover_open: Option<bool>,
+ pub lua_source: String,
+ pub lua_log: Vec<String>,
+ pub lua_load_pending: bool,
}
impl Default for DebuggerUi {
@@ -38,10 +43,14 @@ impl Default for DebuggerUi {
show_exi: false,
show_irqs: false,
show_callstack: false,
+ show_lua: false,
memory_base: 0x8000_0000,
memory_addr_input: "80000000".to_string(),
run_until_addr_input: String::new(),
dvd_cover_open: None,
+ lua_source: DEFAULT_LUA_SCRIPT.to_string(),
+ lua_log: Vec::new(),
+ lua_load_pending: false,
}
}
}
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 2dcf252..47d1e5d 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -184,6 +184,27 @@ impl RenderState {
}
}
+ // Drain Lua log messages from the script host
+ if debugger_ui.show_lua {
+ if let Some(ref mut host) = emulator.script_host {
+ debugger_ui.lua_log.extend(host.drain_logs());
+ }
+ }
+
+ // Load a new Lua script if requested
+ if debugger_ui.lua_load_pending {
+ debugger_ui.lua_load_pending = false;
+ match scripting::LuaScriptHost::from_source("editor", &debugger_ui.lua_source) {
+ Ok(host) => {
+ debugger_ui.lua_log.push("[lua] script loaded".to_string());
+ emulator.set_script_host(Box::new(host));
+ }
+ Err(err) => {
+ debugger_ui.lua_log.push(format!("[lua] error: {err}"));
+ }
+ }
+ }
+
debugger_ui.debugger.tick(emulator);
let frame = match self.surface.get_current_texture() {
@@ -267,6 +288,7 @@ impl RenderState {
ui.checkbox(&mut debugger_ui.show_exi, "EXI");
ui.checkbox(&mut debugger_ui.show_irqs, "IRQ");
ui.checkbox(&mut debugger_ui.show_controls, "Controls");
+ ui.checkbox(&mut debugger_ui.show_lua, "Lua");
});
});
});
@@ -336,6 +358,24 @@ impl RenderState {
if debugger_ui.show_irqs {
dbglib::windows::irq::show_irq(&ctx, &mut debugger_ui.show_irqs, &emulator.cpu, &emulator.pi);
}
+ if debugger_ui.show_lua {
+ let mut load_script = false;
+ let mut clear_log = false;
+ dbglib::windows::lua::show_lua(
+ &ctx,
+ &mut debugger_ui.show_lua,
+ &mut debugger_ui.lua_source,
+ &debugger_ui.lua_log,
+ &mut load_script,
+ &mut clear_log,
+ );
+ if clear_log {
+ debugger_ui.lua_log.clear();
+ }
+ if load_script {
+ debugger_ui.lua_load_pending = true;
+ }
+ }
});
self.egui_winit
diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rs
index 7f83cb2..d1a75d4 100644
--- a/crates/gecko/src/flipper/dsp/regs.rs
+++ b/crates/gecko/src/flipper/dsp/regs.rs
@@ -1,7 +1,7 @@
use chapa::BitEnum;
use crate::flipper::dsp::Dsp;
-use crate::mmio::traits::MmioAccess;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
// 0xCC00500A 2 [R/W] CSR - DSP Control/Status Register
@@ -127,8 +127,7 @@ crate::mmio_register! {
impl MmioAccess<Dsp> for MailboxToDspHi {
fn read(dsp: &Dsp) -> Self {
- // TODO: instantly consume it for now
- dsp.mailbox_to_dsp_hi.with_busy(false)
+ dsp.mailbox_to_dsp_hi
}
fn write(self, dsp: &mut Dsp) {
@@ -180,11 +179,16 @@ crate::mmio_register! {
impl MmioAccess<Dsp> for MailboxToCpuLo {
fn read(dsp: &Dsp) -> Self {
- // NOTE: ideally reading lo should clear hi.M, but read takes &self not &mut self.
- // The M bit will be cleared on the next hi read after lo is read.
dsp.mailbox_to_cpu_lo
}
+ fn read_at(dsp: &mut Dsp, addr: u32, access_size: u32) -> u32 {
+ let val = Self::read_sub(Self::read(dsp).raw() as u32, addr, access_size);
+ // Reading DMBL clears DMBH.M (bit 15), signaling the CPU has consumed the mail
+ dsp.mailbox_to_cpu_hi.set_busy(false);
+ val
+ }
+
fn write(self, _dsp: &mut Dsp) {
// Read-only from CPU side
}
diff --git a/crates/gecko/src/scripting.rs b/crates/gecko/src/scripting.rs
index a7e11a2..83546d9 100644
--- a/crates/gecko/src/scripting.rs
+++ b/crates/gecko/src/scripting.rs
@@ -108,6 +108,11 @@ pub trait ScriptHost {
Ok(None)
}
+ /// Drain buffered log messages emitted by the script.
+ fn drain_logs(&mut self) -> Vec<String> {
+ Vec::new()
+ }
+
/// Called before each CPU instruction executes.
fn on_cpu_pre(&mut self, emu: &mut GameCube);