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
99
docs/usecode-jp-dispatch-analysis.md
Normal file
99
docs/usecode-jp-dispatch-analysis.md
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
# Japanese USECODE Dispatch and Search Analysis
|
||||
|
||||
**Status:** Evidence-backed reverse-engineering notes
|
||||
**Target:** Japanese No Remorse `CRUSADER.EXE` in the live Ghidra program `/ja/CRUSADER.EXE`
|
||||
**Last updated:** 2026-10-08
|
||||
|
||||
This document keeps executable addresses, decompiler findings, corpus counts, and unresolved interpretation questions out of the [USECODE format and runtime specification](./usecode-format-specification.md). The specification carries the concise behavior statements; this note records the evidence and current limits behind the Japanese executable findings. No program bytes were modified.
|
||||
|
||||
## Opcode Dispatch
|
||||
|
||||
The Japanese opcode-name source is [`usecode_opcodes.txt`](../../crusader-disasm/usecode_opcodes.txt); its header identifies the JP No Remorse extraction. The executable's `UsecodeProcess_NextOpcode` begins at `0042ddc8` (`0042ddc8..00431024`). Its range check at `0042df0b` accepts indices through `0x79` and dispatches through `g_usecode_opcode_dispatch_table` at `0042dbe0`.
|
||||
|
||||
- The table has 122 four-byte entries for `0x00..0x79`, spans `0x1e8` bytes through `0042ddc7`, and has 106 distinct targets.
|
||||
- Entries `0x71` and `0x72` share the `00430fdb` invalid-token path, which reports `Undefined token %d`. The historical names `SEARCH_RECURSIVE` and `SEARCH_SURFACE` are not implemented in this executable.
|
||||
- `0x7a` and `0x7b` are outside the table and take the invalid-token path if dispatched as opcodes. The local parser's `0x7a` event/body terminator is framing, not an executable handler; the historical `0x7b REGRESS` name is not evidence of a JP handler.
|
||||
- `0x55` and `0x56` push words `0x0402` and `0x0404`. Their relation to the historical `AND_IMPLIES` and `OR_IMPLIES` names is unknown, and the local parser does not decode them.
|
||||
- The `0x5b` handler at `0042dfaa` consumes its two-byte operand without updating line or breakpoint state. The `0x5c` handler at `0042dfbd` consumes a two-byte offset and nine-byte fixed-width name record, and may pass the name to an output helper; it does not create a debugger current-entry record.
|
||||
|
||||
## Search Objects
|
||||
|
||||
`0x70` reads a result-variable index, selector-expression byte length, and selector type (`2..6`), consumes two VM words, copies the length-delimited expression from the remaining evaluation stack, and constructs a search object through `g_usecode_search_mode_dispatch_table` at `0042dbcc`. The handler begins at `00430b7b`; `0x73` begins at `00430c50` and stores the current candidate before calling the object's first virtual method to advance. `0x74` pushes its following byte onto the evaluation stack.
|
||||
|
||||
| Type | Observed traversal | Ghidra entry / helper |
|
||||
|---:|---|---|
|
||||
| `2` | Map-cell scan; generic postfix predicate or one of five bounded fast filters | `search_selector_next_spatial_match` at `00439f4c` |
|
||||
| `3` | Map-cell scan with an auxiliary continuation path for shape-metadata classes 6 and 7 | `search_selector_next_map_cell_match_variant` at `0043a288` |
|
||||
| `4` | Linked item-id traversal with the generic predicate | `search_selector_next_linked_item_match` at `00439520` |
|
||||
| `5` | Linked traversal with an auxiliary continuation and an X-table fallback | `search_selector_next_coordinate_keyed_match` at `0043a528` |
|
||||
| `6` | Footprint-bounded scan with a vertical-contact predicate | `search_selector_next_shape_footprint_match` at `0043ae30` |
|
||||
|
||||
The type-`2` nonzero selector-mode field dispatches to inclusive X/Y-bounds filters:
|
||||
|
||||
| Mode | Candidate filter | Helper |
|
||||
|---:|---|---|
|
||||
| `1` | Exact shape id | `search_selector_scan_exact_shape` at `00438fc0` |
|
||||
| `2` | Shape family/category | `search_selector_scan_family` at `004390a0` |
|
||||
| `3` | Shape id in an inline set | `search_selector_scan_shape_set` at `00439188` |
|
||||
| `4` | Either of two shape ids | `search_selector_scan_two_shapes` at `00439284` |
|
||||
| `5` | Any item in bounds | `search_selector_scan_bounds_only` at `0043935c` |
|
||||
|
||||
The type-`2`/`3` initializer is `search_selector_init_bounds_and_mode` at `00439b6c`. Type 2 uses its fast filters when object field `+0x12` is nonzero; type 3 has a separate traversal. The type-`6` scan uses the same filter helpers.
|
||||
|
||||
## Type-6 Footprint Predicate
|
||||
|
||||
`search_selector_init_shape_footprint_query` at `0043a5c0` uses object field `+0x3f` as a reference item id and stores the selector-config byte at `+0x41`. The shape record is nine bytes at `004957ac + shape_id * 9`; its dword at offset `+1` supplies three five-bit extents:
|
||||
|
||||
- X extent: extracted field scaled by `16`.
|
||||
- Y extent: extracted field scaled by `16`.
|
||||
- Z extent: extracted field scaled by `8`; if zero while record flag bit `0` is clear, use `1`.
|
||||
|
||||
The inclusive scan bounds are X `[ref_x - x_extent + 1, ref_x + 0x100]` and Y `[ref_y - y_extent + 1, ref_y + 0x100]`. The constructor caches `ref_z + z_extent`.
|
||||
|
||||
`search_selector_match_shape_footprint_contact` at `0043ac78` accepts either relation, selected by the config byte:
|
||||
|
||||
- Bit `0`: `candidate_z == ref_z + ref_z_extent`.
|
||||
- Bit `1`: `candidate_z + candidate_z_extent == ref_z`.
|
||||
|
||||
Both relations also require the candidate footprint's lower X and Y edges (`candidate_coord - candidate_extent + 1`) to be no greater than the reference X/Y coordinates. The scan rectangle supplies the remaining limits. “Above”, “below”, and “overlap” are geometric interpretations, not recovered field names.
|
||||
|
||||
## Prefixes and Expression Tokens
|
||||
|
||||
The type-`2`/`3` initializer recognizes an uppercase fast-prefix form: `N = first_byte - '@'`, followed by `N` inline 16-bit values and `$` (`0x24`) at byte offset `1 + 2*N`. `A` plus one value selects exact-shape mode `1`; `B` plus two values selects either-of-two mode `4`; `C` through `_` with three or more values select shape-set mode `3`.
|
||||
|
||||
The type-`2`/`3` initializer leaves mode zero for a leading `$`; the generic evaluator at `00439560` then terminates immediately at `$` and returns the current top value, defaulting true for an empty stack. The type-`6` initializer at `0043a6ec` instead maps a leading `$` directly to bounds-only mode `5`.
|
||||
|
||||
The local decompiler at `Crusader-Map-Viewer/map_renderer/src/lib/usecode-decompiler.js` recognizes `$B` as a selector-token pair and obtains shape values from the VM stack. That order differs from the native uppercase buffer form (`B`, inline values, `$`). The exact stack-to-buffer transformation has not been established, so the rendered `selector_0x42` form is not independent proof of the native buffer layout.
|
||||
|
||||
The evaluator has a 32-word working stack. Confirmed operations include:
|
||||
|
||||
| Token | Observed operation |
|
||||
|---:|---|
|
||||
| `0x21` | Bitwise NOT |
|
||||
| `0x24` (`$`) | Terminate and return top value, default true when empty |
|
||||
| `0x25` (`%`) | Consume a following 16-bit literal |
|
||||
| `0x26`, `0x2b` | Bitwise AND, OR |
|
||||
| `0x3a` (`:`) | Read a shape-metadata-derived value; local tooling interprets it as family/category |
|
||||
| `0x3c..0x3e` | Signed less-than, equality, greater-than |
|
||||
| `0x3f` | Push the candidate's item-flags word |
|
||||
| `0x40` (`@`) | Read candidate shape id |
|
||||
| Uppercase tokens | Test candidate shape against inline values |
|
||||
| Lowercase tokens | Test candidate metadata against an inline set |
|
||||
| `0x60` | Push a packed value assembled from per-item and shape-record bytes |
|
||||
|
||||
For `0x60`, the low byte comes from the per-item table at `00482b28`; the high byte comes from shape-record offset `+0x0f` at `004955c8 + item_id * 0x79` for item ids `1..0xff`, otherwise zero. Token `0x2a` pushes a per-item auxiliary word at `00482b30`; item setup initializes or assigns it, but its gameplay meaning is not established. Other evaluator tokens also remain unnamed.
|
||||
|
||||
## Item Tables and Corpus
|
||||
|
||||
The labeled JP arrays are `g_item_x_by_id` (`00482b18`), `g_item_y_by_id` (`00482b1c`), `g_item_z_by_id` (`00482b20`), `g_item_shape_by_id` (`00482b24`), `g_item_flags_by_id` (`00482b2c`), and `g_item_next_id_by_id` (`00482b3c`). The selector traversals follow item links, inspect flag bit `0x08`, and in types 3/5 use an auxiliary continuation for shape-metadata classes 6/7. Type 5 can use an X-table value from a flag-`0x08` item to look up another item link; the coordinate-key relationship remains unclear.
|
||||
|
||||
The scanned JP `.unk` listings contain 412 type-`2`, 28 type-`4`, and 58 type-`6` records; no type-`3` or type-`5` records were found. This corpus absence does not imply that those executable dispatch arms are dead.
|
||||
|
||||
## Open Questions
|
||||
|
||||
- Reconcile the local decoder's `$B` order and stack operands with the native fast-prefix buffer.
|
||||
- Map all type-`2`/`3` prefix forms, including family/category and bounds-only selections.
|
||||
- Determine the gameplay meaning and producers of the auxiliary word read by token `0x2a`.
|
||||
- Identify the per-item and shape-record fields combined by token `0x60`.
|
||||
- Establish the coordinate relationship used by the type-`5` X-table fallback.
|
||||
- Find type-`3`/`5` corpus examples or otherwise characterize their executable-only paths.
|
||||
Loading…
Add table
Add a link
Reference in a new issue