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:
Marco 2026-10-08 17:40:59 +02:00
commit 1225a3e737
28 changed files with 1776 additions and 1983 deletions

View file

@ -6,7 +6,7 @@ Question:
- What is the lowest-modification path that could realistically get the hidden debugger menu working?
- Can ordinary usecode or a `-u` startup override reach it without another fragile executable patch?
- If retail still cannot do it cleanly, what should the next comparison pass in No Regret and JP No Remorse look for?
- What does the completed No Regret comparison establish, and which runtime questions remain?
## Short Answer
@ -17,7 +17,7 @@ Current best answer:
- The hidden debugger UI is real and usable, but it expects a live seg1408 break-state object at `1478:659c/659e` and valid current-unit/runtime context.
- The cleanest non-EXE exploration path is now the `-u` usecode-root override, but current evidence still does **not** show a script-visible way to construct the break-state object or write `1478:659c/659e`.
- The smallest structurally defensible executable patch is still the current interpreter-callsite-retarget family, but that remains more complex than a one-site tweak and therefore is not the preferred next move unless cross-build comparison fails.
- The best next investigation is a comparison pass in `REGRET.EXE` and JP `/ja/CRUSADER.EXE` looking specifically for a surviving writer/bootstrap path for `1478:659c/659e`, a constructor caller for `1408:0000`, or a direct caller to the seg109 wrappers.
- The No Remorse versus No Regret static comparison is now closed for the debugger-owned surface: Regret retains a bootstrap writer and live vtable promotion that retail does not. The next discriminating check is runtime reachability; the JP build remains a separate optional comparison.
## New Live-Ghidra Findings From This Pass
@ -31,7 +31,7 @@ Current confirmed reader families:
- `usecode_debugger_open_for_current_unit` at `13a0:00af` / `13a0:0165`
- `usecode_debugger_format_expression_to_shared_buffer` at `13a0:03db` / `13a0:03f4`
- `usecode_debugger_handle_event` at `13a0:1e13`, `13a0:1e3b`, `13a0:1e5d`, `13a0:20b2`, `13a0:20b6`
- one additional seg109 local helper `FUN_13a0_1791`
- `usecode_debugger_watch_pane_draw` at `13a0:1791`
What is still missing:
@ -335,42 +335,76 @@ Why this is now preferred over more retail patch fishing:
- if No Regret or JP No Remorse kept any surviving debugger bootstrap, it could collapse the retail problem from `invent a new path` to `port or mimic one missing write/call`
- that is more likely to produce a truly minimal modification than another speculative retail patch chain
## Retail Ghidra Naming Backlog
## Retail Ghidra Surface Closure (2026-10-08)
The current note corpus now supports a tighter retail seg109 naming batch than the live authoring summary currently reflects.
The active `CRUSADER.EXE` database now has the debugger-owned surface mapped across the `13a0` UI, `1408` break-state, `1410` shared usecode-data, and `1418` interpreter components. This closes the anonymous debugger callbacks and the one missing source-pane function boundary. No executable bytes were changed.
These are the most important retail debugger-side helpers to promote explicitly in the active `CRUSADER.EXE` database before any new patch design work:
### UI Vtables And Callbacks
- `13a0:2882` = `usecode_debugger_build_menubar`
- `13a0:088f` = `usecode_debugger_source_pane_create`
- `13a0:0ae8` = `usecode_debugger_source_pane_init_view_from_break_state`
- `13a0:0ba7` = `usecode_debugger_source_pane_handle_command`
- `13a0:0f16` = `usecode_debugger_source_pane_handle_pointer_event`
- `13a0:1088` = `usecode_debugger_source_line_copy_for_display`
- `13a0:1118` = `usecode_debugger_source_pane_draw_visible_lines`
- `13a0:1413` = `usecode_debugger_source_pane_clamp_viewport`
- `13a0:15ac` = `usecode_debugger_source_pane_load_file`
- `13a0:16ee` = `usecode_debugger_watch_pane_create`
- `13a0:1791` = `usecode_debugger_watch_pane_draw`
- `13a0:193f` = `usecode_debugger_watch_pane_handle_click`
- `13a0:1c2c` = `usecode_debugger_translate_registered_event`
- `13a0:1dc6` = `usecode_debugger_forward_child_event`
- `13a0:2c2e` = `usecode_debugger_source_buffer_create_from_path`
- `13a0:2ca0` = `usecode_debugger_source_buffer_destroy`
- `13a0:2d14` = `usecode_debugger_source_buffer_open_from_path`
- `13a0:2e0a` = `usecode_debugger_source_buffer_load_text`
- `13a0:2f4f` = `usecode_debugger_source_buffer_split_lines_in_place`
- `13a0:301d` = `usecode_debugger_source_buffer_get_line_ptr`
Constructor stores and raw reads of the far-pointer tables establish these owners. Each table has 18 four-byte entries stored as little-endian offset followed by selector. The generic `analyze_vtable` result treats these as two-byte pointers, so the slot map below comes from the raw table bytes and constructor stores.
Why this batch matters before more patching:
| UI object | Vtable root | Confirmed debugger overrides |
| --- | --- | --- |
| Search/menu object | `1478:58da` | `+0x00` `usecode_debugger_menu_destroy`; the menu builder at `13a0:2882` installs this table. |
| Main debugger gump | `1478:592a` | `+0x00` `usecode_debugger_gump_destroy`; `+0x10` shared parent-move handler; `+0x14` `usecode_debugger_forward_child_event`; `+0x18` `usecode_debugger_translate_registered_event`; `+0x30` `usecode_debugger_gump_forward_base_event_if_clean`; `+0x34` shared UI dispatcher `1360:0cb2`. |
| Watch pane | `1478:5972` | `+0x00` `usecode_debugger_watch_pane_destroy`; `+0x08` `usecode_debugger_watch_pane_handle_click`; `+0x10` shared parent-move handler; `+0x30` `usecode_debugger_watch_pane_draw`; `+0x34` shared UI dispatcher. |
| Source pane | `1478:59ba` | `+0x00` `usecode_debugger_source_pane_destroy`; `+0x04` `usecode_debugger_source_pane_forward_event_to_pointer_handler`; `+0x08` `usecode_debugger_source_pane_handle_event_type_2`; `+0x10` `usecode_debugger_source_pane_handle_pointer_event`; `+0x14` `usecode_debugger_source_pane_on_layout_change`; `+0x18` `usecode_debugger_source_pane_handle_command`; `+0x30` `usecode_debugger_source_pane_draw_visible_lines`; `+0x34` shared UI dispatcher. |
- it converts the remaining patch-target area from anonymous `FUN_13a0_xxxx` bodies into named UI, event, and source-buffer lanes
- it reduces the chance of patching the wrong helper when the debugger gump is already on-screen but still miswired
- it makes runtime-only experiments easier to reason about because the gump lifecycle, source loading, and event forwarding chain become legible in-session
The missing function at `13a0:0e81` was confirmed as the source-pane table's `+0x08` callback. Its two far-return paths end at `13a0:0ec9` and `13a0:0ef8`; the next established function starts at `13a0:0ef9`. It is now defined in Ghidra as `usecode_debugger_source_pane_handle_event_type_2`, with the exact body `13a0:0e81-0ef8`. Event value `1` installs the active-gump context, sets widget flag bits `0x09`, and invokes the pane's `+0x10` callback. Value `3` clears that context and those flag bits. The corresponding user gestures remain unknown.
The current strongest provenance for this retail batch is the combined retail `000b:* -> 13a0:*` table in `ne-segment1.md` plus the one-to-one structural match against the now-closed Regret `1398:*` family.
The additional anonymous UI functions are now named from their table slot and body:
One live correction from the follow-up rename pass matters here: retail `13a0:16ee/1791/193f` reads as a watch-pane constructor/draw/click trio, while the source-pane lane remains centered on `13a0:088f/0ae8/0ba7/0f16/1088/1118/1413/15ac`. So the Regret-side structural match is still valuable, but the retail child-pane split is now sharper than the earlier first-pass list implied.
- `13a0:0aa1` = `usecode_debugger_source_pane_on_layout_change`
- `13a0:0ef9` = `usecode_debugger_source_pane_forward_event_to_pointer_handler`
- `13a0:1c08` = `usecode_debugger_gump_forward_base_event_if_clean`
- `13a0:306f` = `usecode_debugger_menu_destroy`
- `13a0:30a9` = `usecode_debugger_gump_destroy`
- `13a0:30e3` = `usecode_debugger_watch_pane_destroy`
- `13a0:311d` = `usecode_debugger_source_pane_destroy`
The break-state vtable root is now labeled `g_usecode_debugger_break_state_vtable` at `1478:65ab`. Its two entries still target `1408:046f` (`OnBreakTriggeredNoop`) and `1408:0474` (`VtableSlot1ReturnZero`), respectively. The global at `1478:659c` is now labeled `g_usecode_debugger_break_state`.
### Shared Usecode Data And Interpreter Helpers
The formerly anonymous `1410` functions close the symbol-data path used by the debugger:
- `1410:0000` = `usecode_global_data_release`; frees the loaded usecode sidecar buffers and clears their globals. It is reached from game start and restart cleanup.
- `1410:0051` = `usecode_global_data_load`; loads the usecode data and symbol sidecars during initialization and savegame load.
- `1410:039f` = `usecode_global_symbol_lookup_by_name`; searches the loaded symbol records and returns the associated value and far-pointer payload. Direct callers are the debugger expression formatter at `13a0:0309` and the `Global name` command at `13a0:25a3`.
- `1410:02e7` = `usecode_global_data_save`; quick-save at `1050:0115` passes a live file/stream object and uses its serialization callback. With a null argument, the helper writes the cached `UNKDS.DAT` bytes back under `s_usecode`.
These routines are shared usecode-data infrastructure, not a writer for `g_usecode_debugger_break_state`. The debugger object global still has recovered reads from the interpreter and UI, but no write in this retail program.
The remaining anonymous interpreter helpers are now separated by evidence:
- `1418:034f` = `usecode_interpreter_advance_source_cursor`; advances the source-cursor offset and has multiple callers inside `Interpreter_NextUsecodeOp`.
- `1418:035f` = `usecode_interpreter_context_load_source_cursor_from_global_unit`; resolves a source cursor through `EntityVmRuntime::GetSlotChunkPtrAtOffset` and stores it in the interpreter context.
- `1418:0172` remains unnamed. It copies a 200-word context block plus two trailing words, has no recovered callers, and is not needed to explain debugger record production.
The retail record lifecycle is directly connected to the interpreter dispatch body:
- opcode `0x5b` updates the line and checks break conditions through `1408:0053` at `1418:04b5`.
- opcode `0x5c` pushes the live unit/source/frame record through `1408:02f5` at `1418:051d`.
- opcode `0x57` pops the parent record at `1418:2a56`, then copies it into the child context through `1408:03b0` at `1418:2ab9`.
- opcode `0x50` pops at `1418:3378`; the interpreter hook also has a guarded unwind pop at `1418:3d78`.
Two other `13a0` functions remain anonymous by design: `13a0:304b` and `13a0:305c` call shared `1238` resource/setup helpers using `1478:8b97`, but are not entries in the debugger UI tables and have no recovered direct callers. Their registration owner is unresolved, so the Regret-side `ITEMCACH` attribution is not copied into retail. Within the debugger-connected ranges, these two wrappers and the unreferenced `1418:0172` context copy are the remaining non-debugger unknowns; the `1408` break-state family has no anonymous function entries.
### Difference From No Regret
The cross-build reference is the completed [No Regret hidden-debugger investigation](regret-hidden-debugger-investigation.md). Both builds retain the functional debugger shell, source loader, watch/expression/global commands, breakpoint state, and interpreter current-entry producer/pop lifecycle. The important differences are in UI composition and activation:
| Surface | No Remorse (`CRUSADER.EXE`) | No Regret (`REGRET.EXE`) |
| --- | --- | --- |
| Child control | `1478:5972` is a watch pane with create/draw/click functions at `13a0:16ee`, `1791`, and `193f`. | `1480:68e2` is an auxiliary ten-row source-line selector/drawing control; its selected row connects to breakpoint-marker handling. |
| Break-state bootstrap | Constructor exists at `1408:0000`, but no caller or write to `g_usecode_debugger_break_state` was recovered. The base table at `1478:65ab` remains inert. | `usecode_debugger_bootstrap_init` at `1398:0000` allocates the object, writes `1480:712c/712e`, and replaces the base vtable with `1480:6972`. |
| Break callback | Slot `0` targets `1408:046f`, a no-op, so a matched break does not open the UI through this callback. | The promoted slot `0` targets `usecode_debugger_open_for_current_unit`, providing a static open-on-break path once the object is initialized. |
| Interpreter records | The `0x5b`/`0x5c`/`0x57`/`0x50` and unwind transitions are statically connected in `Interpreter_NextUsecodeOp`. | The equivalent opcode-dispatch producer, child copy, line update, and pop lifecycle is statically connected in the recovered Regret hook. |
| Shared symbol data | `1410` includes release, load, save/stream serialization, and the name resolver used by the debugger. | `13e8` has named release/load/name-lookup routines; its investigation note does not report a matching `usecode_global_data_save` helper. |
This is a static wiring advantage, not proof of ordinary-session activation. The Regret bootstrap still has no recovered static caller, and the DOSBox runtime checks recorded in the Regret note observed `1480:712c/712e` as null during ordinary gameplay and after the tested cheat inputs. The current distinction is therefore: Regret preserves a bootstrap and live break callback that retail lacks, but runtime reachability of that bootstrap remains unproven.
## Practical Alternatives To Manual Hex Patching
## Practical Alternatives To Manual Hex Patching
@ -436,32 +470,20 @@ It should stay in the toolbox, but it should not be mistaken for a direct replac
If the goal is the minimum modification that still has a realistic chance to work, the order should now be:
1. Promote the retail seg109 naming backlog above so the remaining debugger lanes are explicit in Ghidra.
2. Use runtime-only memory seeding on a clean executable to prove or kill the bootstrap theory without committing file changes.
3. Compare `REGRET.EXE` and JP `/ja/CRUSADER.EXE` for any surviving debugger bootstrap/writer that can replace a custom retail bootstrap.
1. Keep the completed retail Ghidra surface map and current debugger-state naming as the static baseline.
2. Use read-only runtime observation to test whether a real session initializes the retail break-state object or reaches the recovered record producer.
3. If sibling-build analysis continues, focus on the still-open JP `/ja/CRUSADER.EXE` function-side scan; the No Remorse/No Regret bootstrap and callback comparison is already documented above.
4. Keep `-u` / replacement `EUSECODE.FLX` as the preferred low-risk experiment surface for any script-side proxy ideas.
5. Do **not** resume broader retail executable patching unless the runtime proof or cross-build pass yields one clear small patch plan that can be applied by script to a writable clone.
That ranking fits both the new live evidence and the user's practical constraint that complex retail patch attempts have already been unstable.
## Cross-Build Exploration Note
## Cross-Build Status
When this moves to No Regret and JP No Remorse, the focused targets should be:
The No Remorse versus No Regret pass resolves the main static comparison questions: retail has no recovered object writer or constructor caller and keeps inert break-state slots; Regret has a bootstrap writer and promotes slot `0` to the current-unit opener. Both retain interpreter record production and a broad UI/event subsystem.
1. any write to the debugger global pointer equivalent of `1478:659c/659e`
2. any caller of `1408:0000 Create` or its build-specific equivalent
3. any direct caller of `usecode_debugger_open_for_current_unit`
4. any direct caller of `usecode_debugger_open_modal`
5. any non-stub debugger vtable slot replacing retail `1408:046f` / `1408:0474`
6. any command-line or cheat/debug hotkey path that lands near the seg109 wrappers or seg1408 constructor
7. any usecode/runtime path that seeds current-unit state for the debugger without using the orphaned retail bootstrap
The remaining high-value work is dynamic confirmation of bootstrap reachability and record values. The Regret runtime note already records a null debugger pointer in ordinary tested gameplay, so its static bootstrap must not be presented as a demonstrated normal-session launch. A deeper JP `/ja/CRUSADER.EXE` function-side scan is separate work and remains open.
## Bottom Line
Retail No Remorse still looks like it shipped with a real hidden usecode debugger whose UI, event dispatcher, and break-state object all survived, but whose bootstrap path did not.
That means the lowest-modification *currently evidenced* route is no longer "guess one more retail patch". It is:
- first, look for the missing bootstrap in sibling builds,
- second, use `-u` and a replacement `EUSECODE.FLX` only as a low-risk exploration surface,
- and only third, return to the interpreter-callsite-retarget patch family if the cross-build pass gives no smaller bootstrap to port.
Retail No Remorse retains a complete static debugger surface: its menus, source and watch panes, event dispatcher, global-symbol integration, break-state object, and interpreter record lifecycle are all mapped. The specific gap relative to No Regret is bootstrap and callback wiring, not a missing UI or interpreter producer. Regret is structurally better wired, but its bootstrap's ordinary runtime reachability is still unproven. The next useful step is read-only runtime validation, not more static searching through the already closed retail debugger clusters.