Refactor and enhance documentation and tooling for Crusader USECODE
- Updated retail-debugger-entry-options.md to clarify the status of the debugger in No Remorse and No Regret, including findings from recent investigations. - Revised usecode-format-specification.md to improve clarity on opcode behavior and historical naming conventions, with added references to Japanese executable analysis. - Introduced usecode-jp-dispatch-analysis.md to document findings from the Japanese No Remorse executable, detailing opcode dispatch and search object behavior. - Added ghidra_to_dosbox_memory.py, a new tool for translating Ghidra segment:offset addresses to DOSBox physical memory offsets, including comprehensive address validation. - Created test_ghidra_to_dosbox_memory.py to ensure the correctness of address translation logic and selector base loading, with unit tests covering various scenarios.
This commit is contained in:
parent
ffb1918f35
commit
1225a3e737
28 changed files with 1776 additions and 1983 deletions
|
|
@ -47,19 +47,55 @@ The shared dispatcher at `1360:0cb2` translates UI event codes `1`, `2`, `4`, `8
|
|||
|
||||
The bootstrap override table at `1480:6972` begins with two confirmed far-pointer entries: slot `+0x00` opens the current-unit debugger and slot `+0x04` formats an expression into the shared buffer. The following bytes are not treated as further vtable entries; the neighboring UI tables were identified independently from their constructor vptr stores and complete 18-entry layouts.
|
||||
|
||||
### Orphaned Menu Vtable and Function-Pointer Sweep (2026-10-08)
|
||||
|
||||
A raw far-pointer scan of data segments `1480` and `1488` found a fourth 18-entry UI table at `1480:684a`, before the three debugger-gump tables. Its slot `+0x00` points to `1398:306f`. The earlier function-xref query reported no callers because this use is through the table rather than a call instruction.
|
||||
|
||||
The constructor evidence ties the table to debugger UI: `usecode_debugger_gump_build_menus` allocates a `0x3e` object through `1360:050c`, initializes it through `1348:0124`, then stores `0x684a` into its vptr at `1398:2995`. The surrounding menu builder attaches the Search entries to this object. The table's other non-default slots point to shared UI methods at `1348:02c6`, `1348:03da`, `1348:019b`, and `1348:15d4`; the shared dispatcher remains `1360:0cb2`. These callbacks are framework UI behavior, not evidence of additional debugger-engine logic.
|
||||
|
||||
`1398:306f` is now named `usecode_debugger_menu_destroy` in Ghidra. Its body delegates cleanup to `1360:046e` and conditionally frees the object, matching the vtable's destructor slot. The exact framework class name is not recovered, so the name describes its debugger-menu ownership rather than claiming a more specific menu role.
|
||||
|
||||
The same pointer scan found `1398:304b` and `1398:305c` in static far-pointer entries at `1480:8440` and `1480:84c4`. Their decompilation calls shared setup/cleanup helpers with key `0x95ff` and the `ITEMCACH` path; they are item-cache wrappers, not debugger callbacks. Their registration owner and runtime reachability remain unresolved.
|
||||
|
||||
Two unowned byte ranges inside `1398` were also checked and are data, not missing function bodies. `1398:0e61-0e80` is a 16-word near-offset dispatch table whose targets lie inside `usecode_debugger_source_pane_handle_command`. `1398:285c-2881` is a 19-word near-offset table for `usecode_debugger_handle_event`, with targets inside that function including its shared epilogue at `1398:2858`. No listed instructions were found outside function bodies in this segment. The full segment inventory contains 39 functions; apart from the two item-cache wrappers, the only unnamed executable function in this area was the table-owned menu destructor now renamed above.
|
||||
|
||||
### Connected Segment Closure (2026-10-08)
|
||||
|
||||
The follow-up checked function boundaries and raw bytes across the connected `1398`, `13e0`, `13e8`, `13f0`, and `13f8` segments, then scanned the relevant data ranges for far-pointer references. It found no additional debugger-owned function cluster or unclassified debugger callback.
|
||||
|
||||
Newly confirmed Ghidra names:
|
||||
|
||||
- `13e8:0000` = `usecode_global_data_release`
|
||||
- `13e8:0051` = `usecode_global_data_load`
|
||||
- `13e8:039f` = `usecode_global_symbol_lookup_by_name`; the debugger expression formatter and `Global name` command both use it.
|
||||
- `13f8:0d72` = `usecode_interpreter_runtime_cleanup`
|
||||
- `13f8:0cdf` = `usecode_interpreter_runtime_initialize`
|
||||
- `13f8:1499` = `usecode_process_manager_create`
|
||||
- `13f8:167c` = `usecode_process_manager_find_entry`
|
||||
- `13f8:1d8d` = `usecode_process_manager_release_entry_reference`; the interpreter hook uses this during return cleanup.
|
||||
|
||||
One last unowned range was present in `13f0`. The opcode table at `13f0:3d9f` has 122 near-offset entries and ends at `13f0:3e92`. The bytes at `13f0:3e93-3eb7` form a separate far-returning helper, now defined as `far_alloc_two_bytes_if_null`. Its decompilation returns the input far pointer when non-null and otherwise calls the shared allocator for two bytes. No references to it were recovered, and its behavior is generic allocation support rather than debugger logic. The remaining `13f0` code before the table is the interpreter setup and the already mapped debugger hook, including its table-dispatched handlers.
|
||||
|
||||
The gap at `13f8:0d72-0dc4` was also code, not data. `13f8:0d72-0dc3` is now defined as `usecode_interpreter_runtime_cleanup`; it releases the intrinsic table and active usecode root, then calls a shared runtime cleanup. It has no recovered incoming xrefs and is generic lifecycle support. The byte at `13f8:0dc4` is a separate unreferenced `RETF` immediately before the next function at `13f8:0dc5`; it has no symbol or function body and is not debugger code.
|
||||
|
||||
The static runtime callback path is now explicit: `usecode_interpreter_create_process_context` installs the callback stored at `1480:72a4` (`13f8:10da`); that wrapper calls `usecode_debugger_interpreter_hook` at `13f8:10fa`, then continues through the process object's normal virtual dispatch. `13f8:112c` is the callback-table continuation within the wrapper's stack frame, not a separate function entry. The startup initializer at `13f8:0cdf` loads the intrinsic table, while `13f8:1499` creates the process manager and installs its callback table. These establish ordinary interpreter execution reaching the optional hook; they do not establish that the debugger object is initialized.
|
||||
|
||||
Other nearby references were checked to avoid conflating generic support with debugger code. `13e8:039f` is the shared name-table resolver used by the debugger, while the adjacent loader/release routines manage usecode-backed data. The process-entry reference release at `13f8:1d8d` is VM lifecycle support. The environment string `CRUDEBUG` at `1480:44c8` is read by `kernel_initialize` (`11e8:000c`) to set the separate kernel debug flag at `1480:4415`; no path from that setting to the usecode debugger was found. The flag’s separate consumers and its unresolved relationship to the game’s `-debug` argument are documented in [regret-crudebug-investigation.md](regret-crudebug-investigation.md). Likewise, the unreferenced context-copy helper at `13f0:0172` and trivial wrapper at `13f0:009a` have no recovered call or pointer relationship to the debugger.
|
||||
|
||||
The bootstrap remains the boundary of static certainty: `usecode_debugger_bootstrap_init` still has no recovered caller, and `1480:712c/712e` still has only its write at `1398:0064`. The hook is statically wired into interpreter execution, but whether ordinary startup reaches the bootstrap, and therefore whether break/step can open the UI in an unmodified session, still requires runtime observation.
|
||||
|
||||
### Remaining Nearby Gaps
|
||||
|
||||
The debugger-owned UI/source-buffer code, the three adjacent UI vtables, all `13e0` break-state helpers, the full table-dispatched `13f0` hook body, and the adjacent `13f8` context factory/wrapper path are now mapped to a bounded level. Remaining questions are narrower:
|
||||
The connected static code is now mapped to a bounded level. Remaining questions are runtime reachability and a few UI semantics, not unexplored debugger-owned functions:
|
||||
|
||||
- `1398:16ee` constructs the auxiliary source-line selection/rendering control. Its click, selected-row field, and line-row draw behavior are established; exact screen geometry and draw ordering remain unknown.
|
||||
- The event values handled by `1398:0e81` are established, but their user-gesture meanings are not. `1398:0ef9` forwards the same pane/event to pointer handler `1398:0f16`.
|
||||
- `1398:304b` and `1398:305c` have no callers or address-taken references. They wrap shared `1250:056a/06eb` setup/cleanup with key `0x95ff` and the item-cache path at `1480:1476+0x0a`; a cache/resource role is likely, but debugger ownership and runtime reachability are unproven.
|
||||
- `1398:306f` is another unreferenced destructor-style thunk through shared UI cleanup `1360:046e`, absent from the three recovered debugger vtables. The class owner is unresolved. The neighboring destructor entries `1398:30a9`, `1398:30e3`, and `1398:311d` are now mapped to the main gump, auxiliary child, and source pane respectively.
|
||||
- `1398:304b` and `1398:305c` have no recovered direct callers, but raw data scanning finds far-pointer entries at `1480:8440` and `1480:84c4`. Their `0x95ff` setup/cleanup calls and `ITEMCACH` path indicate item-cache/resource wrappers; exact registration ownership and runtime reachability are unresolved.
|
||||
- The interpreter has three indirect far calls, now classified by opcode. Opcode `0x0f` calls through runtime table `1480:96e6`, built by `usecode_interpreter_load_intrinsic_table` (`13f8:062f`) during usecode startup; target values depend on loaded records. Opcode `0x70` has static sub-operations `2..6` at `13f0:3468`, `3539`, `3610`, `3695`, and `3720`, converging on `13f0:38a3` through a runtime object's `+0x04` field. Opcode `0x73` calls through a stack-selected object's `+0x02` field at `13f0:3975`. These final targets remain runtime-dependent and none is needed to explain the direct opcode `0x5c` record producer.
|
||||
- `usecode_debugger_bootstrap_init`, `usecode_debugger_open_modal`, and `usecode_debugger_open_for_current_unit` have no recovered static callers. The bootstrap also has no recovered raw far-call encoding or far-pointer copy. The ordinary usecode startup chain `13f8:0cdf -> 13f8:1499 -> 13f8:062f` installs the interpreter wrapper/hook and builds runtime tables, but no static bridge to the debugger bootstrap was found. Examined cheat/input paths likewise have no references to the debugger openers, so normal-session bootstrap and deliberate UI activation remain runtime questions.
|
||||
- `usecode_debugger_bootstrap_init`, `usecode_debugger_open_modal`, and `usecode_debugger_open_for_current_unit` have no recovered static callers. The interpreter hook is reached through the normal context callback path, but no static caller or pointer registration for the debugger bootstrap was found. Examined cheat/input paths likewise have no references to the debugger openers, so normal-session bootstrap and deliberate UI activation remain runtime questions.
|
||||
- Segment:offset labels are program-specific. In `REGRET.EXE`, `13a0:01ee` and `13a0:0361` are item operations, not the similarly addressed debugger formatter functions documented for `CRUSADER.EXE`.
|
||||
|
||||
These are bounded UI/dispatch and runtime questions, not an unlocated second debugger subsystem or a missing producer. The remaining high-value checks are dynamic: verify the bootstrap is reached, capture the opcode `0x5c` record fields, and observe breakpoint/open/resume behavior in a live session.
|
||||
The static sweep found no further debugger-owned executable code in these connected segments. The remaining high-value checks are dynamic: verify the bootstrap is reached, capture the opcode `0x5c` record fields, and observe breakpoint/open/resume behavior in a live session.
|
||||
|
||||
## Verified Regret Findings
|
||||
|
||||
|
|
@ -676,7 +712,7 @@ The `Global name` command is different.
|
|||
What case `0x0f` in the dispatcher currently appears to do:
|
||||
|
||||
- prompt for a global symbol name
|
||||
- resolve that symbol through `FUN_13e8_039f`
|
||||
- resolve that symbol through `usecode_global_symbol_lookup_by_name` (`13e8:039f`)
|
||||
- copy the current bytes of the resolved global into a temporary buffer
|
||||
- prompt for replacement bytes
|
||||
- validate each entered byte is within `0x00..0xff`
|
||||
|
|
@ -2063,4 +2099,65 @@ The concrete retail follow-up should compare:
|
|||
- any retail analogue of the `1480:713b -> 1480:6972` vtable promotion
|
||||
- the retail equivalent of the Regret slot-`0` auto-open path through `usecode_debugger_break_state_update_line_and_maybe_break`
|
||||
|
||||
That remains the highest-value cross-build step. But if the immediate practical goal is only `make the debugger appear somewhere`, Regret is now the better live hack target than retail itself.
|
||||
That remains the highest-value cross-build step. But if the immediate practical goal is only `make the debugger appear somewhere`, Regret is now the better live hack target than retail itself.
|
||||
|
||||
## DOSBox MCP Runtime Investigation Workflow (2026-10-08)
|
||||
|
||||
The local DOSBox Automation MCP integration now provides a repeatable runtime path for the unresolved Regret questions above. The engine exposes memory, CPU-register, input, and screen tools; its remote `debugger` feature is still false, so this workflow does not provide instruction breakpoints, pause, continue, or CPU single-step.
|
||||
|
||||
### Verified Runtime Address Calibration
|
||||
|
||||
The dedicated test profile starts Regret with `-warp 1 -egg 250` in the Mission 1 staging room and omits the independently controlled game `-debug` flag. Its launcher starts DOSBox in a detached process so an agent terminal does not wait for the emulator to exit. Select this environment deliberately; it is useful for controlled input but does not represent a normal startup path.
|
||||
|
||||
Two fresh runs of the same DOSBox Automation version and test profile produced the same runtime mapping for selector `0x1398`:
|
||||
|
||||
- Ghidra bytes at `1398:0000` begin `55 8b ec 83 ec 04`; the four bytes at `1398:0004` are `ec 04 68 9a`.
|
||||
- Searching guest memory in two requests covering the 32 MiB DOSBox allocation found one match at physical `0x21148c`, yielding selector base `0x211488`.
|
||||
- Ghidra bytes at `1398:0031` (`83 c4 02 89 56 fe 89 46 fc 0b c2 74 1c 52 ff`) matched the 15 bytes read by MCP at physical `0x2114b9` exactly.
|
||||
- The second fresh run found the same unique signature and exact second-window match, so this selector base did not change across those two runs on this machine/profile.
|
||||
|
||||
This empirically verifies the converter for selector `0x1398` under the tested conditions; it does not establish a universal or permanent load address. Re-derive and verify bases for each investigation session and for each selector needed. In particular, do not use the `0x1398` base for selector `0x1480` (the debugger-state global at `1480:712c/712e` is in a different NE segment). Ghidra selector `0x1398` maps to NE segment `0x74`; selector `0x1480` maps to NE segment `0x91` using the verified selector-to-segment formula.
|
||||
|
||||
The loaded image relocates far-call operands: Ghidra byte `0x10` at `1398:0030` was `0x01` in guest memory. When checking a signature, prefer non-relocated bytes or account for the loader relocation; a mismatch at a relocation site alone does not disprove the base.
|
||||
|
||||
### Candidate Runtime Activities
|
||||
|
||||
Proceed in this order, keeping the initial pass read-only:
|
||||
|
||||
1. Confirm the bridge is connected to the intended engine and inspect program status and the screen. If DOSBox is already running, attach to it; do not launch a second instance. If it is stopped, deliberately choose the staging profile for this test and launch it detached.
|
||||
2. Re-establish the physical base for selector `0x1398` from its static code signature and cross-check a second non-relocated window. Search ranges must be split into requests of at most 16 MiB. Record the executable, profile, engine version, signature, match address, and independent byte check.
|
||||
3. Independently map selector `0x1480`, then read the far pointer at `1480:712c/712e` at launch and after controlled triggers. A null pointer means the hidden debugger object is not initialized at that checkpoint; writing only the global pointer is not a valid bootstrap.
|
||||
4. If the pointer resolves to a valid object, snapshot its vtable and state fields: current line `+0x72`, break/step flags `+0x74/+0x75`, callstack depth `+0x7a`, breakpoint entries, and current `0x15`-byte callstack records.
|
||||
5. Use scripted, repeatable inputs to exercise known usecode activity, sampling before and after each trigger. Compare the line, depth, unit name, and context pointers to test the opcode `0x5c` producer and the opcode `0x57`/`0x50` context transitions. Without instruction breakpoints, polling may miss short-lived state.
|
||||
6. Capture the screen around candidate entry triggers. If the game's hidden debugger becomes reachable, test its source and breakpoint UI and its own Run/step actions with scripted input; these are not DOSBox CPU stepping.
|
||||
7. Only after the read-only state is understood, consider a one-variable RAM-only intervention to the confirmed break/step flags. Save original bytes, use the staging session, compare state and screen, and restore the saved bytes. Do not patch executable code or invent a debugger-object pointer for this test.
|
||||
|
||||
`mem_read` and `mem_search` use guest physical addresses. Lua `dosbox.mem_read(seg, off, len)` applies real-mode `seg*16+off` and does not translate Ghidra protected-mode selectors. Use the MCP physical-memory tools with the independently established selector bases. The MCB memory map is useful for DOS allocations, but does not supply protected-mode selector bases. The current address converter requires a verified selector-base JSON map and fails rather than guessing.
|
||||
|
||||
### Current Attached Session (2026-10-08)
|
||||
|
||||
The running DOSBox process was attached read-only rather than started by the bridge. Its command line used `dosbox-automation-test.conf`, whose autoexec mounts the game directory and cloud-save overlay, mounts `GAME.GOG`, and runs `REGRET.EXE -warp 1 -egg 250` without `-debug`. The game was visibly in the Mission 1 staging room. Engine: DOSBox Automation `0.85.1 (7567ec4)`. The engine debugger feature is unavailable; memory reads and CPU-register snapshots are available.
|
||||
|
||||
Both selector bases were derived again from this session's memory, not copied from the prior-run map:
|
||||
|
||||
- Selector `0x1398`: Ghidra `1398:0004` begins `ec 04 68 9a`. The current guest-memory match at `0x21148c` yields base `0x211488`. A second window at `1398:0031` matched exactly at `0x2114b9` (`83 c4 02 89 56 fe 89 46 fc 0b c2 74 1c 52 ff`). At the first window, Ghidra's far-call selector operand `00 10` is relocated at runtime to `7f 01`; the call offset remains `b4 36`.
|
||||
- Selector `0x1480`: Ghidra `1480:7130` contains `DEBUGGER.C\0`. The independently searched runtime string and adjacent bytes matched at `0x2386a4`, yielding base `0x231574`. The runtime address for `1480:712c` is `0x2386a0`; its four bytes were `00 00 00 00`, so the debugger object pointer is null at this checkpoint.
|
||||
|
||||
No debugger-object fields or callstack records were read, and no usecode trigger inputs were sent: the null global does not identify a valid initialized object to inspect. No guest memory or executable bytes were written. Trigger sampling remains gated on a later checkpoint where the global points to a validated object.
|
||||
|
||||
### Explicit Restart Recheck (2026-10-08)
|
||||
|
||||
After the previous DOSBox process was closed, `start-crusader-test.cmd` was launched detached and the bridge attached to the new process. The process command line again selected `dosbox-automation-test.conf`; the screen confirmed the Mission 1 staging room, with `REGRET.EXE` active on DOSBox Automation `0.85.1 (7567ec4)`.
|
||||
|
||||
Fresh memory searches over two 16 MiB ranges reproduced the mappings independently:
|
||||
|
||||
- Selector `0x1398`: the `68 9a` word from Ghidra offset `0006` matched at `0x21148e`, giving base `0x211488`. The current read at `0x21148c` matched Ghidra offset `0004` (`ec 04 68 9a`) apart from the relocated far-call selector (`00 10` to `7f 01`); the independent window at `1398:0031` again matched exactly at `0x2114b9`.
|
||||
- Selector `0x1480`: `DEBU` at `0x2386a4`, `GGER` at `0x2386a8`, and `.C` plus NUL and `o` at `0x2386ac` matched Ghidra `1480:7130` onward. This independently yields base `0x231574`. At `1480:712c` (`0x2386a0`), the first four runtime bytes were zero, so the debugger object pointer remained null.
|
||||
|
||||
Because the pointer was null, no object fields or callstack records were read and no trigger inputs were sent. No game memory or executable bytes were written. The result agrees with the earlier attached-session check, but each base above comes from this restarted process's own signatures and byte comparisons.
|
||||
|
||||
### New-Session Handoff
|
||||
|
||||
Use this request to start the next runtime pass:
|
||||
|
||||
> Continue the No Regret hidden debugger runtime investigation documented in `docs/regret-hidden-debugger-investigation.md`. Use the DOSBox MCP tools and the dedicated staging profile only as an explicit test choice. First check whether DOSBox is already running; attach if it is, and otherwise launch `F:\Apps\Crusader No Regret\start-crusader-test.cmd` detached so the terminal returns immediately. Confirm the game is in the staging room and record the engine/profile. Re-derive selector `0x1398` from Ghidra bytes and MCP memory, then independently map selector `0x1480`; do not assume its base from `0x1398` or reuse a previous run's map without checking. Keep the first pass read-only. Read `1480:712c/712e` and, only if it points to a valid initialized object, capture the break-state fields and callstack around repeatable usecode triggers. Compare Ghidra bytes with runtime bytes while accounting for relocated operands. Do not use local/offline PyGhidra, do not patch executable bytes, and do not write game memory unless I explicitly ask for that experiment.
|
||||
Loading…
Add table
Add a link
Reference in a new issue