Raw Byte Code Elimination

Status: IN PROGRESS. All sixteen gated TLCS-900 images are byte-identical to their physical dumps, but that is a build-match guarantee, not a disassembly guarantee: a .byte run that spells real code reassembles to the same bytes as an .incbin of those bytes, and passes the gate identically either way. Measured code-as-.byte remains in the Main CPU, Sub CPU Payload and SX-WSA1R prom_a and prom_b ROMs; the HDAE5000’s remaining uncommented .byte is not shown to be code (1,654 of its 1,713 B are a palette and a bitmap). Goal: Convert all executable code currently represented as raw .byte sequences to native TLCS-900 assembly mnemonics.

Context

All sixteen TLCS-900 images (twelve KN5000, four SX-WSA1R) rebuild byte-identically from source. That is necessary for correct disassembly but not sufficient: a .byte run that spells real code, or a data region disassembled into plausible-but-wrong instruction mnemonics, both reassemble to the same bytes and pass the gate cleanly. Whether a .byte run is undecoded code can only be settled by a per-byte disassembly attempt (Step 1’s method, below), not by counting .incbin directives — the table under Current Status states what that measurement currently finds, ROM by ROM.

The sub-CPU payload is down to twelve single instructions spelled as .byte (46 B in kn5000_subprogram_v142.s: ten minc1 and two ldc to DMA control registers; tools/annotated_insn_bytes.py v142/subcpu in this repository lists them), each commented “no llvm-mc spelling” although the pinned backend spells them as minc1_16, ldc_cr32 and ldc_cr16. Its last two blocked families are both closed: DSP_Bytecode_Op01/02/03 (569 B) needed a decoder fix before the encoder’s own output could round-trip (see LLVM Semantic Instructions), and the TaskEvent/FIFO/TaskSched family’s apparent ~407 B gap turned out to be a measurement bug in the round-trip prober, not a decoder limitation — of the 672 B it flagged, 50 B is genuine data (loaded as an address, never executed) and the remaining 622 B decodes and reassembles byte-exact once the prober’s own undercount is corrected. Conversions in this image are proved by round trip: disassemble the run, re-assemble that exact text, require the original bytes back.

Counting .incbin distorts in both directions. It hides real debt written as .byte, and it equally rewards pushing legitimate data into .byte — respelling a viewable PNG-backed image as hex reduces the metric while destroying the better representation. Neither direction is progress.

Scope: .byte sequences that encode native TLCS-900 CPU instructions across all sixteen TLCS-900 images. Data tables, strings, bitmaps, firmware bytecode for software interpreters, and padding are out of scope (correct as-is).

Current Status

By ROM

ROM Code .byte remaining Instruction statements Status
Main CPU (v9 / v10, each) confirmed-region backlog 0 B (the census rule still fires on 5 regions, 389 B, per image; in v10 all five are text strings: the Italian, French and German wallpaper messages, a floppy-test message and a widget-name string) + misframed islands: 145 runs / 1,495 B (v9), 139 runs / 1,426 B (v10) 330,449 (v9) / 329,465 (v10) Not complete
Main CPU (v7) 8 regions (1,236 B) the census rule still fires on, four of them the same strings as v10 and four (914 B) not yet hand-audited, plus 275 misframed islands (2,043 B), 203 of them (1,338 B) in genuine-code context 331,379 Not complete
Sub CPU Payload twelve single instructions (46 B) left as .byte with the comment “no llvm-mc spelling”: ten minc1 …,IX and two ldc to DMA control registers (kn5000_subprogram_v142.s:2538–2979); the pinned llvm-mc emits all twelve byte-exactly as minc1_16, ldc_cr32 and ldc_cr16 45,981 Not complete
Sub CPU Boot 0 1,358 Complete
Table Data 0 10,097 Complete
Custom Data 0 (data only) 0 Complete
HDAE5000 1,713 B of .byte with no per-line comment (hdae5000/tools/measure_debt.py), not shown to be code: 1,654 B of it is the palette HDAE5000_Palette_Button01 (1,024 B) and the bitmap HDAE5000_Bitmap_Button01 (630 B) (see below) 35,281 Not complete
SX-WSA1R prom_a 82 certified .byte instruction sites (226 B, 42 forms); under the current assembler pin a3a81863f87d the census script spells one form (decf), and 34 more sites in 13 shift-by-A forms reassemble byte-identically as slaa, slla or srla, which it does not try, so 47 sites in 28 forms have no spelling found so far (see below) 119,190 Not complete
SX-WSA1R prom_b 68 .byte rows marked [llvm-mc cannot encode this], all between instruction lines (wsa1/notes/promb_macro_rewrite.py --census): 66 carry an instruction reading (rrc/rlc by a count, shift-by-A, ldir, ex BC,QBC, …) and two carry unidasm’s db. The census script below counts none of them, because their comments carry no byte echo 67,855 Not complete
SX-WSA1R prom_c / prom_d 0 — survived a falsification attack 75,701 / 0 Complete

All figures at disassembly 3417969c0 (2026-10-06).

The instruction statements column is, in the disassembly repository,

python3 notes/syntax-convergence-probes/mnemonic_census.py --root <dir>

with <dir> one of v10/maincpu, v9/maincpu, v7/maincpu, v142/subcpu, subcpu/boot, hdae5000, table_data, custom_data, wsa1/prom_a … wsa1/prom_d; the TOTAL line is the figure. The census counts every instruction-shaped statement under the root — native mnemonics, tree-local synthetic names and tree macros alike — so it is not a count of native instructions. Custom data’s 0 is a fact about a pure data ROM, not a gap. The SX-WSA1R figures cover the files under each prom_* root: the kernel and DSP-driver sources both processors share (wsa1/kernel/, 938 statements; wsa1/dsp/, 97) are assembled into prom_a and prom_c but live outside both roots.

The SX-WSA1R code-as-.byte figures come from wsa1/notes/sound/wsa1_unspellable_forms.py (run from wsa1/), which counts a .byte line only when four gates agree — its comment echoes exactly its bytes, the ROM holds those bytes at the address it claims, MAME’s committed unidasm listing frames them as one instruction, and a spelling route is tried and judged by the bytes it assembles to. At be376096, prom_a’s commonest forms were or (N),N (26 sites) and sub (N),N (24). “No spelling” means no route the script tries made the assembler emit the bytes, which is not the same as the backend being unable to encode them — and the script runs whichever llvm-mc the shared build holds, so the same revision gives different counts under different builds. At be376096, every build gives 298 certified site(s), 1318 byte(s), 89 form(s); the NO SPELLING line reads:

llvm-mc build sha256 no spelling spellable
da00420dba8d (the pin be376096’s TOOLCHAIN_VERSION names) a7ee33d53676 298 0
6488ae7d (one commit after the 4d7fa4f6b37c the Wave 2 commit messages name) 9c0c0033a21f 298 0
910b50efab49 b211a23195df 292 6 — ld a,(-xix), ld (-xiy),a, ld (xix+),a, ld (xix+),wa, ld (xix+),e, tset 0,(0x2a1b76)
8e188b215251 (the pin from bc571542 until 2026-09-25) a67c563269fc 282 16 — the six, and the ten mul WA,(XIZ+d) sites

tools/wsa1_unspelt_by_toolchain.py in this repository reproduces the table: it exports a revision with git archive and runs the script once per named binary. The sources are being respelled to match; at e5f6cea1 the five post-increment / pre-decrement sites are instructions and 293 certified sites remain.

At disassembly 3417969c0, under the current pin a3a81863f87d (llvm-mc sha256 118bb4d6766e), the same tool reports prom_a: 82 certified sites, 226 B, 42 forms; 81 with NO SPELLING; the one spellable form is decf. The 2026-10-03 respells 7a9cf4ff8 (150 instructions) and 09cc4ae4d (53) converted most of the 298. The 81 overstate what is unspellable: for the thirteen shift-by-A forms (34 sites, counted by tools/wsa1_shift_by_a_spellings.py in this repository; among them the commonest, srl A,C with 7 and sla A,C with 6) the script tries only unidasm’s spelling srl a,c, which llvm-mc rejects, but the backend spells these slaa, slla and srla (SLA_A_8 … SRL_A_32 in TLCS900InstrInfo.td), and the pinned llvm-mc emits their bytes exactly (echo 'srla c' | llvm-mc -triple=tlcs900 -show-encoding prints [0xcb,0xff]). At most 47 sites in 28 forms lack a spelling.

The earlier “0 — byte runs audited and typed” for prom_a did not survive this census. Separately, the 2026-09-25 lanes retyped data framed as code in prom_a and prom_b — the opposite hazard — per their reports in notes/lanes/wave2-2026-09-25/lane-reports/. The SX-WSA1R instruction-statement figures were re-measured with the mnemonic_census.py command above at 3417969c0.

The misframed islands in the Main CPU rows are left as .byte on purpose: fixing one means re-framing an instruction already present in a neighbouring converted region, not filling a gap, and this work requires a round-trip proof per region rather than a bulk relabel. Converting a confirmed region creates new islands at its boundary, so the island count is not a fixed pool — it moves with the tree state and should be re-measured, not quoted from this page, before being used for planning.

scripts/analysis/v9_v10_undisassembled_census.py is the v9/v10 and v7 census; it needs a scratch tree and is run as --prepare, then --judge v7 and --islands v7 --max-island 63. hdae5000/tools/measure_debt.py is the HDAE5000 one and prints to stdout with no arguments.

The HD-AE5000 figure can rise as work lands, and that is correct. When twelve regions of mis-disassembled data were retyped back into .byte (hdae5000/tools/convert_misframed_data_regions.py), the measurement moved up rather than down; it has since fallen to the 1,713 B above. A number that only ever falls would mean the instrument cannot see data-framed-as-code, which is the third kind of debt and the one the byte gate is blind to in both directions.

Why the column says “instruction statements”

The figures that once occupied that column — 239,683 for the main CPU, 35,721, 1,357, 1,678 and 502 for the others, labelled native instruction counts — could not be reproduced from anything in the repository: they predate the LLVM toolchain, and they were not merely stale. The HD-AE5000’s was off by a factor of about seventy (502 against the census’s 35,281 at 3417969c0). A figure with no producer does not get restored; the column is filled from the census and labelled by what the census counts, which is every instruction-shaped statement, native or synthetic.

Two instruments exist and measure different things, so they cannot share a column:

  • notes/syntax-convergence-probes/mnemonic_census.py --root <dir> counts instruction statements per source root — the column above. Its bucket breakdown for v10 is on ROM Reconstruction.
  • llvm-mc -g emits one DWARF line row per instruction statement and none for data, which gives a per-image count that includes every source the image pulls in. Values on record in the disassembly repository’s notes: HD-AE5000 36,391, custom data 0, wsa1/prom_c 76,647, prom_d 0 — close to the census, not equal to it.

Neither is a count of native instructions; how far the tree is from that is the subject of LLVM Semantic Instructions. This page states debt, which is measured, rather than progress.

LLVM Backend Encodings Added

The encodings in the table below, which the March 2026 audit of the KN5000 trees found missing, have all been implemented in the LLVM TLCS-900 backend. That does not extend to the SX-WSA1R: prom_a still carries 81 certified .byte sites (41 forms) for which the census script finds no spelling — 47 sites in 28 forms once the shift-by-A spellings it does not try are counted (see above). Some of those forms have no instruction in the backend at the pin a3a81863f87d: the rotate-by-A group (ce f9 is rrc A,H), push of a byte register named by code (c7 f8 04, push IZL) and ALU operations on an (xrr+r8) operand (c3 03 f0 e0 e8, or (XIX+A),W).

Category Prefix Count Converted LLVM Status
JR/JRL/CALR branch instructions 0x1E etc. 1,214 Fixed (label-based)
Compact register loads (d8 prefix) 0xD8-0xEF 2,680 reg-reg + 831 ALU/LD/BIT Implemented
Word register by code (D7 prefix: QWA–QSP, the upper words of XWA–XSP) 0xD7 147 Implemented
Memory R+d8 addressing various 3,616 Implemented
Compact dst (CALL/JP/CPW/LD) various 1,038 Implemented
Short LD (compact load) 0x20-0x3F 523 Implemented
Compact imm32 loads various 684 Implemented
ld A, (R+d16) source loads 0xC3 ~970 Implemented (Mar 14)
ld (R+d16), A stores 0xF3 ~400 Implemented
Shifts/Rotates/MUL/DIV various 246 Implemented

HDAE5000: not complete

hdae5000/tools/measure_debt.py counts 1,713 B of undocumented .byte/.word operand bytes — 1,654 B of it in two contiguous blocks whose headers say what they are, the palette HDAE5000_Palette_Button01 (1,024 B) and the bitmap HDAE5000_Bitmap_Button01 (630 B; hdae5000/hdae5000_data_tables.s:14772-14865), the other 59 B in 15 short runs (tools/hdae5000_byte_runs.py in this repository) — plus 373 B more that carries a decoding comment but has not been converted to real instructions. Against a 512 KB ROM that is 0.327 % debt and 99.602 % real source (disassembly 3417969c0). Most remaining .byte in the tree is genuine data (custom-filesystem templates — the HD-AE5000 filesystem is not FAT16 — string constants, UI bitmaps, etc.), but these figures are not yet zero. There are 0 B of raw .incbin with no rebuild rule.

Execution Plan

Historical plan (March 2026), kept for its method. Step 1’s audit exists as scripts/analysis/audit_byte_code.py, the encodings of Steps 3–8 are implemented and spelled natively in the sources, and Step 10 is done: v10/maincpu/storage/fdc_routines.s holds no .byte line at disassembly 3417969c0. Paths below predate the per-version split: maincpu/ is now v10/maincpu/ (and v9/, v7/), and single_load.s lives in file_io/.

Step 1: Precise Automated Audit

Write a Python script (scripts/analysis/audit_byte_code.py) that:

  1. Parses all .s files across all ROMs
  2. Identifies .byte sequences between native instructions (code context)
  3. Attempts llvm-mc --triple=tlcs900 --disassemble on each sequence
  4. Classifies results: (a) already decodable by LLVM → immediate conversion, (b) needs LLVM backend addition, (c) confirmed data
  5. Groups code .byte by first byte (opcode prefix) to identify LLVM encoding families
  6. Outputs a report with: file, line, bytes, category, status

Step 2: Convert Already-Decodable Instructions

Some .byte sequences may already have LLVM support but were written as .byte historically. Convert them directly to native mnemonics using the disassembler output.

Verification: make clean && make all + compare_roms.py after each batch.

Step 3: LLVM Backend — Compact Register Loads

Add encoding support for compact register load instructions:

  • ld wa, 0 (D8 A8), ld xde, 0 (EA A8), ld xwa, 1 (E8 A9), etc.
  • These are 2-byte compact forms vs the 3-4 byte extended forms

Step 4: LLVM Backend — Compact Stack Pointer Arithmetic

Add encoding support for:

  • dec N, xsp (EF 6A/6E) — decrement stack pointer by N
  • inc N, xsp (EF 62/66) — increment stack pointer by N

Step 5: LLVM Backend — calr Fix

Fix calr with numeric address targets. Currently broken — emits absolute bytes instead of relative offset. Either fix the encoder to compute the relative offset, or add a new mnemonic variant.

Step 6: LLVM Backend — F2 Immediate-to-Memory Stores

Add encoding support for the F2-prefix ld (mem), imm instructions that store immediate values to memory addresses. ~358 occurrences.

Step 7: LLVM Backend — C3 R+d16 Source Loads

Add encoding support for ld A, (R+d16) source addressing (C3 prefix). ~216 occurrences. Note: the D3/E3/F3 destination variants already work; this is the source (load) direction.

Step 8: LLVM Backend — Remaining D7 (word-register-by-code) Forms

Add cps qiz, 0 and any other D7-prefix (word register by code) instructions not yet supported (~4 occurrences for cps, ~182 total D7-prefix).

Step 9: Batch Convert .byte → Native Mnemonics

After each LLVM backend addition (Steps 3-8), convert the corresponding .byte sequences in the disassembly to native instructions. Use Python scripts with binary I/O (Latin-1 safety policy). Verify byte match after each batch.

Step 10: Disassemble FDC Raw Byte Blocks

The FDC routines in maincpu/storage/fdc_routines.s contain ~434 lines of raw .byte that are actual instruction sequences. These need:

  1. Disassembly using llvm-mc --disassemble or llvm-objdump
  2. Analysis of each routine’s purpose
  3. Semantic labeling (no LABEL_XXXXXX allowed)
  4. Documentation header comments

Step 11: Disassemble Flash/Floppy Handler Blocks

maincpu/storage/flash_floppy_handlers.s and maincpu/storage/single_load.s contain ~2,272 lines of raw byte blocks that need full disassembly, semantic labeling, and documentation.

Step 12: Iterative Jump/Call Table Discovery & Disassembly

Newly disassembled code may reveal previously unidentified jump tables or call tables. These must be found and their targets disassembled, repeating until exhaustion:

  1. Scan for undiscovered tables in all newly disassembled code blocks (sequences of .long values in ROM range, lda+jp (xwa) patterns, indexed dispatch)
  2. Verify table entries are code targets (not already-disassembled or false positives)
  3. Disassemble newly discovered code targets with semantic labeling and documentation
  4. Recurse — newly disassembled code may contain more tables
  5. Terminate when no new tables or code targets are found

Scope: All ROMs (maincpu, subcpu, hdae5000, table_data).

Step 13: Final Verification & Website Sync

  1. make gate-all — every gated image byte-identical (16 TLCS-900 + 19 MN10300), or the change does not land
  2. Run scripts/analysis/sync_docs_labels.py --apply to update any new labels on the website
  3. Update rom-reconstruction.md

Ordering & Priorities

Do first: Step 1 (audit) — gives precise scope for everything else. Then: Steps 2 (free wins), 3-4 (easy LLVM additions, high impact). Then: Steps 5-8 (harder LLVM work, each unblocks batch conversions). Then: Steps 9-11 (conversion work, depends on LLVM additions). Then: Step 12 (iterative table discovery — feeds back into Steps 9-11). Finally: Step 13 (verification & sync).

Steps 3-8 are independent of each other and can be parallelized. Step 12 is iterative and may cycle back through Steps 3-11.

Verification

After each step:

  • cd technics-roms-disasm && make gate-all — every gated image (16 TLCS-900 + 19 MN10300), byte-exact or non-zero exit
  • LLVM tests: cd llvm-project && build/bin/llvm-lit llvm/test/CodeGen/TLCS900/

Do not accept a percentage in place of the gate. compare_roms.py prints Similarity: 100.00% rounded to two decimals, which in a 2 MB ROM covers up to 104 differing bytes, and it silently skips any section whose built file is missing — so a run that never assembled the six ASL mirror sections prints nine sections, all reading 100.00%, and looks identical to a passing full run. See Disassembly Workflow.

Policy Compliance

All newly disassembled code MUST:

  1. Have semantic label names (no LABEL_XXXXXX)
  2. Have documentation header comments explaining what each routine does
  3. Be verified with byte-match builds before committing
  4. Have website docs updated if labels appear on documentation pages