From a5e49ea578b5c881241832ed1a51d9dd94b95df6 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:26:06 -0500 Subject: [PATCH 1/2] decomp(ezitext): recover PUD byte contracts and packed header decoding (#1023) --- libs/RVLMiddleware/eZiText/src/clib/zi8pud2.c | 16 ++-- ...ands-ezitext-pud-byte-contract.attempts.md | 51 ++++++++++++ ...ands-ezitext-pud-packed-header.attempts.md | 77 +++++++++++++++++++ 3 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 tools/decomp-assist/allhands-ezitext-pud-byte-contract.attempts.md create mode 100644 tools/decomp-assist/allhands-ezitext-pud-packed-header.attempts.md diff --git a/libs/RVLMiddleware/eZiText/src/clib/zi8pud2.c b/libs/RVLMiddleware/eZiText/src/clib/zi8pud2.c index 9dcb9342..b3a78b52 100644 --- a/libs/RVLMiddleware/eZiText/src/clib/zi8pud2.c +++ b/libs/RVLMiddleware/eZiText/src/clib/zi8pud2.c @@ -17,7 +17,7 @@ typedef struct ziPudHeader { ziU8 options[4]; ziPudSection sections[1]; } ziPudHeader; -ziU32 Zi8_8147FD7C(ziWChar*, ziU8, ziU8, ziWChar*, ziU16, ziU8, ziU8 ZI_NEED_WORK); +ziU8 Zi8_8147FD7C(ziWChar*, ziU8, ziU8, ziWChar*, ziU16, ziU8, ziU8 ZI_NEED_WORK); ziU32 ZiIsPhoneticChar(ziWChar character) { if ((character < 0xF305 || character > 0xF329) && (character < 0xF361 || character > 0xF37A)) return 0; @@ -56,7 +56,7 @@ ziU8 ZADP_Zi8SetPDremoveOpt(ziU8 option, struct __zi8_work_data_s* __zi8_work_da Zi8LogError(100, __zi8_work_data); return previous; } -ziU32 Zi8MatchPUDdata_ZHS(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziWChar* spelling, ziU16 spellingCapacity, ziU8 complete, ziU8 continuation ZI_NEED_WORK) { +ziU8 Zi8MatchPUDdata_ZHS(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziWChar* spelling, ziU16 spellingCapacity, ziU8 complete, ziU8 continuation ZI_NEED_WORK) { int fallback = 0; ziU8* bytePattern; ziWChar folded; @@ -88,8 +88,8 @@ ziU32 Zi8MatchPUDdata_ZHS(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* Zi8LogError(0x4F6, __zi8_work_data); return 0; } - ZI_WORK->unk_0x314 = section->count[0] * 0x100 + section->count[1]; - ZI_WORK->unk_0x310 = (ziU32)table + section->offset[0] * 0x100 + section->offset[1]; + ZI_WORK->unk_0x314 = ((ziU16)section->count[0] << 8) + (ziU16)section->count[1]; + ZI_WORK->unk_0x310 = (ziU32)table + (((ziU16)section->offset[0] << 8) + (ziU16)section->offset[1]); ZI_WORK->unk_0x318 = 0; } for (;;) { @@ -174,12 +174,12 @@ matched: } } } -ziU32 Zi8MatchPUDdata(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziU8 complete, ziU8 continuation, struct __zi8_work_data_s* __zi8_work_data) { - ziU32 result; +ziU8 Zi8MatchPUDdata(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziU8 complete, ziU8 continuation, struct __zi8_work_data_s* __zi8_work_data) { + ziU8 result; if (continuation == 0) __zi8_work_data->pudCount = 1; for (;;) { result = Zi8_8147FD7C(pattern, length, language, output, capacity, complete, continuation, __zi8_work_data); - if ((ziU8)result != 0) break; + if (result != 0) break; if (++__zi8_work_data->pudCount <= 16) { continuation = 0; } else { @@ -189,6 +189,6 @@ ziU32 Zi8MatchPUDdata(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* out } return result; } -ziU32 Zi8_8147FD7C(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziU8 complete, ziU8 continuation ZI_NEED_WORK) { +ziU8 Zi8_8147FD7C(ziWChar* pattern, ziU8 length, ziU8 language, ziWChar* output, ziU16 capacity, ziU8 complete, ziU8 continuation ZI_NEED_WORK) { return Zi8MatchPUDdata_ZHS(pattern, length, language, output, capacity, 0, 0, complete, continuation, __zi8_work_data); } diff --git a/tools/decomp-assist/allhands-ezitext-pud-byte-contract.attempts.md b/tools/decomp-assist/allhands-ezitext-pud-byte-contract.attempts.md new file mode 100644 index 00000000..7e971fcc --- /dev/null +++ b/tools/decomp-assist/allhands-ezitext-pud-byte-contract.attempts.md @@ -0,0 +1,51 @@ +# PUD byte-return producer contract, 2026-10-03 + +Base: a894ff5c; branch agent/bittle/pud-return-contract. +Owned source: zi8pud2.c only. The copied cache was refreshed by a configured +full 43U build before recording the baseline. No caller or header changed. + +## Producer and caller evidence + +Every Zi8MatchPUDdata_ZHS exit returns zero, one, or an explicitly byte-cast +copied count. Zi8_8147FD7C forwards that result directly. Zi8MatchPUDdata +receives it, tests its byte value, and returns it; it never consumes any +higher result bits. The target wrapper retains exactly this data flow. + +All external callers use byte-return declarations: zi8alpha.c and zi81key.c +for Zi8MatchPUDdata, and zi8cgetc.c for Zi8MatchPUDdata_ZHS. The previously +attempted caller-side full-word corrections were therefore based on a +misreconstructed producer chain rather than a demonstrated wide result. + +Recovered ziU8 returns for all three functions and their local forward +declaration, plus ziU8 for the wrapper result local. Its test now uses the +typed byte directly. The explicit cast of copied in the actual producer +remains. Changing the coherent chain preserves the complete compiled object; +no compensating caller masks, local aliases, storage or register constraints +are required. + +## Validation + +Baseline report: /tmp/ezi-pud-baseline.json. +Baseline object: /tmp/ezi-pud-return-contract/baseline.o. +Final report: /tmp/ezi-pud-return-full-report.json. +Full build: /tmp/ezi-pud-return-full-build.log. +Detailed gates: /tmp/ezi-pud-return-verification.txt. + +The complete zi8pud2.o file is byte-identical to baseline, including symbol +strings. Every function's disassembly is unchanged. All 1027 full-report +unit measures are unchanged: unit fuzzy 98.88764%, ZHS 98.32204%, exact +functions 6/7, exact code 720/2136 and exact data 48/120. This is a contract +correction, not a percentage gain or a new exact function. + +All six existing exact functions retain ctxdiff diffs 0 and identical counts: +ZiIsPhoneticChar 16, ZiGetZHWordSize 31, Zi8CopyZHSpelling 33, +ZADP_Zi8SetPDremoveOpt 21, Zi8MatchPUDdata 50, Zi8_8147FD7C 29. +The unmatched producer remains 349/354 instructions. Allocated section sizes, +payloads and canonical relocations are unchanged: .text 2116, extab 48, +extabindex 72. + +Pool first: identical and empty. Full 43U build passes with the approved +wrapper and WIBO_SJIS_MISSING_IMPORTS=1. DOL SHA1: +26116613f624061ba99c8d1a299aaa6efa85670d. +git diff --check passes. No remote, linking, metadata, header, caller, +assembly, volatile, dummy-storage or undefined-value change. diff --git a/tools/decomp-assist/allhands-ezitext-pud-packed-header.attempts.md b/tools/decomp-assist/allhands-ezitext-pud-packed-header.attempts.md new file mode 100644 index 00000000..121b4ff4 --- /dev/null +++ b/tools/decomp-assist/allhands-ezitext-pud-packed-header.attempts.md @@ -0,0 +1,77 @@ +# PUD packed-header halfword decoding, 2026-10-03 + +Parent: a71dfa9c; its byte-return correction is unchanged. +Owned source: two expressions in zi8pud2.c / Zi8MatchPUDdata_ZHS. + +## Recovered arithmetic and widths + +The eight-byte PUD section record contains two-byte big-endian count and +offset fields. The target separately promotes each of their four component +bytes through a halfword boundary before shifting or adding. The source +previously expressed plain byte arithmetic and omitted those four target +conversions. + +Restored explicit ziU16 promotions for count[0], count[1], offset[0] and +offset[1]. The offset is now decoded as one grouped big-endian value before +adding the table base. This reproduces the target's base/high/low load and +addition order, without a new local, helper, casted memory access or storage. + +Bounded isolated trials, all retaining 6 exact functions and 48 exact data: +- Halfword promotions with the existing offset association: + ZHS 98.52542%, unit 99.02247% +- Conventional grouped high/low offset sum: + ZHS 99.717514%, unit 99.81274%; selected +- Grouped low/high offset sum: + ZHS 99.10452%, unit 99.406364%; rejected +- Grouped multiplication form: + ZHS 99.10452%, unit 99.406364%; rejected + +All 65,536 possible packed values preserve the decoded count. Another +524,288 offset/base combinations, covering eight bases including 32-bit +wrap boundaries for every packed value, preserve the computed address. +These arithmetic tests supplement the target instruction correspondence. + +## Remaining workspace snapshot + +The target stores its incoming workspace pointer once into frame 0x24 at +function +0x3C. A width-aware access and address-provenance audit finds no +read, overlapping use, or address escape for that slot. The workspace stays +in its existing saved register. The only ordinary local whose address is +passed to a helper is the halfword at frame 0x0E. Save/restore addresses the +separate upper frame region. No source-visible distinct alias role was found. + +That single dead store remains absent. No unused local, dummy assignment, +padding, volatile or forced spill was added to manufacture it. + +## Validation + +Fresh final report: /tmp/ezi-pud-packed-full-report.json. +Full build: /tmp/ezi-pud-packed-full-build.log. +Detailed gates: /tmp/ezi-pud-packed-verification.txt. +Snapshot audit: /tmp/ezi-pud-work-snapshot-audit.txt. + +Zi8MatchPUDdata_ZHS: 98.32204% -> 99.717514%. +Unit fuzzy: 98.88764% -> 99.81274%, about 19.76 weighted bytes gained. +Exact functions remain 6/7, exact code 720/2136, exact data 48/120. +All 1027 units and their per-function fuzzy scores were checked with no +regressions; this unit/producer improvement is the only measure change. + +Producer instructions: 349 -> 353 versus target 354. +Producer size: 0x574 -> 0x584 versus target 0x588; frame remains 0x60. +After accounting solely for the missing dead store and its four-byte branch +destination displacement, every remaining instruction is identical, including +registers, immediates, calls and memory operands. All 65 direct branches and +13 calls correspond. This is not an exact match: the missing instruction +and function extent are explicitly retained as unresolved. + +All six existing exact functions retain ctxdiff diffs 0 and identical counts. +The 48-byte extab is baseline-identical. The already-unmatched 72-byte +extabindex changes only the producer extent byte at 0x2B, 0x74 -> 0x84; +canonical relocations and its 98.61111% score are unchanged. No real data +object, section extent or metadata declaration was changed. + +Pool first: identical and empty. Full 43U build passes with the approved +wrapper and WIBO_SJIS_MISSING_IMPORTS=1. DOL SHA1: +26116613f624061ba99c8d1a299aaa6efa85670d. +git diff --check passes. No remote, linking, header, new assembly, volatile, +dummy-storage, undefined-value or forced-register changes. -- 2.49.1 From 0eeffe5afc91bf1e8a3ead01f2cca6c54b48459c Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:27:52 -0500 Subject: [PATCH 2/2] decomp(ezitext): pair current and best phonetic restoration --- .../RVLMiddleware/eZiText/src/clib/zprepare.c | 6 +- .../zprepare-phonetic-restore-20261003.md | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tools/decomp-assist/zprepare-phonetic-restore-20261003.md diff --git a/libs/RVLMiddleware/eZiText/src/clib/zprepare.c b/libs/RVLMiddleware/eZiText/src/clib/zprepare.c index f337ec92..a67eb13b 100644 --- a/libs/RVLMiddleware/eZiText/src/clib/zprepare.c +++ b/libs/RVLMiddleware/eZiText/src/clib/zprepare.c @@ -280,10 +280,8 @@ savePhonetic: } if (buffers.phoneticInput[phoneticLength - 1] == 0xF360) { match->nCand--; - bestInitial = previousInitial; - bestFinal = previousFinal; - final = bestFinal; - initial = bestInitial; + bestInitial = initial = previousInitial; + bestFinal = final = previousFinal; goto savePhonetic; } if (match->nCand == 1) { diff --git a/tools/decomp-assist/zprepare-phonetic-restore-20261003.md b/tools/decomp-assist/zprepare-phonetic-restore-20261003.md new file mode 100644 index 00000000..42c5608c --- /dev/null +++ b/tools/decomp-assist/zprepare-phonetic-restore-20261003.md @@ -0,0 +1,92 @@ +# Zi8PrepareMatch paired phonetic restoration + +Base: `5d7760d341938a55e7613de85a3e1e5a3ba6bcb7`. +Leaf: `agent/bittle/prepare-phonetic-restore`. +Only `libs/RVLMiddleware/eZiText/src/clib/zprepare.c` and this log change. +The separate CHANSVm candidate and the one-key/PUD sources are untouched. + +## Retained source reconstruction + +On a failed Bopomofo parse ending in the separator, the previous initial and +final phonetics restore both the current and best outputs. The original loads +each previous halfword once and writes its two destinations immediately. +The source instead assigned the best outputs, then reloaded them to assign the +current outputs. + +Express the two paired restores directly: + +```c +bestInitial = initial = previousInitial; +bestFinal = final = previousFinal; +``` + +The original six instructions at `0x81485D24` through `0x81485D38` are now +byte-identical to the candidate's corresponding restore block. Those words +were checked against the original DOL, independently of relocation matching. +No new behavioral bug is claimed: this reconstructs the target's data flow. + +All six variables are distinct `ziWChar` halfword locals. There is no call, +volatile access, or other intervening operation between these assignments. +The two snapshots and all existing initialization remain present. Pairing the +stores introduces no alias-dependent result or new evaluation dependency. + +## Caller/callee audit + +- The original Pinyin call at `0x81485B68` resolves to `0x8147DDE8`, and the + Bopomofo call at `0x81485BEC` resolves to `0x8147E9F4`. Raw DOL branches agree + with extracted relocations. +- Pinyin receives the request's UTF-16 input, byte count, the two halfword + output arrays at match offsets `0x26`/`0x46`, byte completion at request + offset `0x22`, and halfword best outputs at match offsets `0x66`/`0x68`. +- Bopomofo receives the same four distinct halfword output locals whose + pointers and snapshots are traced in the restore block. Its count and + consumed-count result are bytes; the caller's halfword running length is + narrowed to a byte at the call exactly as in the target. +- No phonetic output-pointer, request-field width, or return-width correction + is warranted. The target's unconsumed component flag stores remain omitted; + no dummy state is introduced. +- Prior `ezi3.round2/3/4` and `fz15` logs were read. Earlier restore-order trials + did not express the paired assignments. Prior register, helper, and dummy-flag + investigations were not repeated. + +## Separate bounded trials + +| Trial | Function fuzzy | Source/target instructions | Matched data | Decision | +| --- | ---: | ---: | ---: | --- | +| Fresh baseline | 96.319145% | 939/940 | 8/68 | baseline | +| Paired restore | 96.802124% | 936/940 | 8/68 | retain | +| Joint index/phonetic-length loop increment | 96.313830% | 941/940 | 8/68 | reject | +| Both changes | 96.748940% | 937/940 | 8/68 | reject | + +The retained source removes two restore reloads and one redundant move elsewhere +in the function. Its 22 ordered call identities are unchanged. Register choices +and code layout still differ elsewhere; no exact match or linking gain is claimed. + +## Validation + +- Fresh configured 43U baseline and final `all_source`, report, and `ok` gates + pass with the assigned wibo wrapper and WIBO_SJIS_MISSING_IMPORTS=1. +- Full 1,027-unit report comparison: only zprepare changes, zero per-function + fuzzy/exact/code/data regressions. The unit still has 0/1 exact functions. +- Source-extracted baseline/candidate restore harness: 165,536 cases pass under + UBSan, covering every previous-initial halfword plus deterministic generated + states. All four outputs, both snapshots and adjacent guard halfwords agree + with an independent paired-output reference. This is a focused block test, + not a complete dictionary-engine or Wii runtime test. +- Pool before/after: identical, no strings. +- Literal advisory: no candidates/errors; its one function is skipped because + source and target extents differ. No literal coverage is claimed from that skip. +- `git diff --check`: PASS. +- DOL SHA1: `26116613f624061ba99c8d1a299aaa6efa85670d`. +- Final source SHA256: + `0df8e9d97309fe5aef8336de71b3ec7aebf549ddd024cb927fa3f93a3d3e8333`. +- Final object SHA256: + `bdd28374efdc511ad35278dbf1b2f88f553d36f0cba901af13a3105b41281b34`. + +Private local evidence: `/tmp/zprepare-contract-audit/` contains the fresh +baseline, separate trial sources/objects/reports, trials.py, test_restore.py, +focused test output, raw-address audit, pool/literal results and build logs. +No original binary/disassembly, header change, metadata, linking flags, assembly, +volatile forcing, register search, dummy storage or other-region build is included. + +GATE: validated partial source-data-flow gain; frozen for independent review. -- 2.49.1