#!/usr/bin/env python3
from pathlib import Path
import json
root=Path(__file__).resolve().parents[1];out=root/'out/iodomain-observe'
def word(n):
 b=(n<<1)|(n.bit_count()%2);s=''.join('L' if b&(1<<i) else 'S' for i in range(7,-1,-1));return s[:4]+' / '+s[4:]
spec=dict(version=1,symbol_slot_ms=3000,short_on_ms=400,long_on_ms=2000,word_frame_ms=42000,transition_ms=500,sequence_ms=222500,words=['unchanged fault ID 43','pre-handoff selector bits 0..6','native selector bits 0..6','pre bit7, native bit7, four completion bits, record validity','first incomplete supply and operation stage'],extra_sequence_off_ms=12000,completed='lookup succeeded, write routine reached, both writes returned zero, readback succeeded and domain selectors match',hardware_tested=False)
(out/'observation-spec.json').write_text(json.dumps(spec,indent=2)+'\n')
lines=['# IO-domain observation words','', 'Diagnostic only. No voltage correction. WAIT/READY and all old error identifiers retain their meanings. Error 43 alone adds this sequence.', '', 'Read five complete words in order after the long inter-sequence darkness: error 43 (`SLSL / SLLS`), BEFORE, NATIVE, FLAGS, DETAIL. Every word has the original flutter preamble and eight S/L symbols. S is 0.4 seconds ON; L is 2 seconds ON. The longer gap after DETAIL identifies the sequence boundary. The full sequence repeats every 222.5 seconds. Observe two identical sequences; uncertain symbols or parity errors are inconclusive. Allow 9 minutes after failure. No decoding tool is needed: report the five literal words.', '', 'Only Error 43 uses this sequence. Other errors keep their existing single repeated word. Missing native snapshot leaves the original Error 43 display, with incomplete observability. A frozen kernel/supervisor may prevent any report.', '', 'BEFORE and NATIVE encode bit0 PMUIO2 sel18, bit1 PMUIO2 sel33, bit2 VCCIO1 sel18, bit3 VCCIO4 sel18, bit4 VCCIO6 sel18, bit5 VCCIO1 sel33, bit6 VCCIO4 sel33. The corresponding FLAGS bit supplies VCCIO6 sel33. Expected selectors encode 58 (SSLL / LSL S without spaces within groups; use the table below).', '', 'FLAGS: bit0 BEFORE VCCIO6 sel33; bit1 NATIVE VCCIO6 sel33; bits2..5 completed PMUIO2/VCCIO1/VCCIO4/VCCIO6; bit6 valid U-Boot handoff record. When invalid, BEFORE and completion flags have no meaning. Completion is an observation, not proof of rail voltage.', '', 'DETAIL: zero means all four completed. Otherwise 1 + supply_index*8 + stage. Supply indices: 0 PMUIO2, 1 VCCIO1, 2 VCCIO4, 3 VCCIO6. Stages: 0 not observed/unavailable; 1 entered; 2 lookup returned but setup not reached; 3 setup returned but voltage not read; 4 voltage read but selector routine not reached; 5 routine reached but other completion check failed; 6 selector write returned error; 7 readback failed/mismatched or invalid stage. Only the first incomplete supply is shown; flags retain the other completion states. Detailed signed results and voltages remain in the RAM record, not an operator-readable log.', '', '| Literal word | Value |','|---|---|']
lines=[x.replace('58 (SSLL / LSL S without spaces within groups; use the table below)',f'58 (`{word(58)}`)') for x in lines]
lines += [f'| `{word(n)}` | {n} |' for n in range(128)]
(out/'observation-codes.md').write_text('\n'.join(lines)+'\n')

(root/'pds/iodomain_observe/src/observation_timing.rs').write_text('pub const SEQUENCE_MS:u64='+str(spec['sequence_ms'])+';\npub const WORD_MS:u64='+str(spec['word_frame_ms'])+';\npub const DELIMITER_MS:u64='+str(spec['transition_ms'])+';\n')
