|
|
|
@@ -552,19 +552,29 @@ def classify_frame(frame: S3Frame) -> str:
|
|
|
|
|
|
|
|
|
|
# ── Waveform file writer ───────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
def _has_metadata_pages(a5_frames) -> bool:
|
|
|
|
|
"""Whether A5 frames 1 and 2 are the global metadata pages.
|
|
|
|
|
_A5_HEADER = 12 # every A5 reply's header after the probe, in stripped bytes
|
|
|
|
|
_A5_TERM_HEADER = 11 # the TERM reply's header
|
|
|
|
|
|
|
|
|
|
Only the event-1 walk (start_key[2:4] == 0) fetches them, at 0x1002 and
|
|
|
|
|
0x1004; a non-first event's stream goes straight from the probe to sample
|
|
|
|
|
chunks. Each A5 reply echoes its address at data[2:6], so the frames say
|
|
|
|
|
which they are. Both must match: a sample chunk can sit at 0x1002, but then
|
|
|
|
|
the next one is at 0x1202, never 0x1004. (Assuming metadata pages in every
|
|
|
|
|
stream lost two bytes from every non-first event -- BE9441, 2026-10-25.)
|
|
|
|
|
|
|
|
|
|
def _raw_skip(data, stripped_len: int) -> int:
|
|
|
|
|
"""Raw bytes of `data` covering its first `stripped_len` bytes, an inner DLE
|
|
|
|
|
pair (10 02 / 10 03 / 10 04) counting as one -- the rule `probe_skip` and
|
|
|
|
|
`_strip_inner_frame_dles` already use.
|
|
|
|
|
|
|
|
|
|
Every A5 header is 12 bytes in that sense (the TERM's 11), but its raw length
|
|
|
|
|
varies with the address it echoes: 13 for the metadata pages at 0x1002 and
|
|
|
|
|
0x1004, and for a sample chunk that lands there; 12 otherwise. A fixed
|
|
|
|
|
"frames 1 and 2 are metadata pages, skip 13" cut two bytes from every
|
|
|
|
|
non-first event, which has no metadata pages (BE9441, 2026-10-25).
|
|
|
|
|
"""
|
|
|
|
|
return (len(a5_frames) > 2
|
|
|
|
|
and bytes(a5_frames[1].data[4:6]) == b"\x10\x02"
|
|
|
|
|
and bytes(a5_frames[2].data[4:6]) == b"\x10\x04")
|
|
|
|
|
raw_i = stripped = 0
|
|
|
|
|
while stripped < stripped_len and raw_i < len(data):
|
|
|
|
|
if data[raw_i] == 0x10 and raw_i + 1 < len(data) and data[raw_i + 1] in (0x02, 0x03, 0x04):
|
|
|
|
|
raw_i += 2
|
|
|
|
|
else:
|
|
|
|
|
raw_i += 1
|
|
|
|
|
stripped += 1
|
|
|
|
|
return raw_i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_body_bytes(a5_frames):
|
|
|
|
@@ -627,19 +637,13 @@ def extract_body_bytes(a5_frames):
|
|
|
|
|
body_frames = a5_frames
|
|
|
|
|
term_frame = None
|
|
|
|
|
|
|
|
|
|
meta = _has_metadata_pages(body_frames)
|
|
|
|
|
all_bytes = bytearray()
|
|
|
|
|
for fi, frame in enumerate(body_frames):
|
|
|
|
|
if fi == 0:
|
|
|
|
|
skip = probe_skip
|
|
|
|
|
elif fi in (1, 2) and meta:
|
|
|
|
|
skip = 13 # metadata pages
|
|
|
|
|
else:
|
|
|
|
|
skip = 12 # sample chunks
|
|
|
|
|
skip = probe_skip if fi == 0 else _raw_skip(frame.data, _A5_HEADER)
|
|
|
|
|
all_bytes.extend(_frame_body_bytes(frame, skip))
|
|
|
|
|
|
|
|
|
|
if term_frame is not None:
|
|
|
|
|
all_bytes.extend(_frame_body_bytes(term_frame, 11))
|
|
|
|
|
all_bytes.extend(_frame_body_bytes(term_frame, _raw_skip(term_frame.data, _A5_TERM_HEADER)))
|
|
|
|
|
|
|
|
|
|
# Find the first valid `0e 08` footer marker.
|
|
|
|
|
footer_pos = -1
|
|
|
|
@@ -802,9 +806,9 @@ def write_blastware_file(
|
|
|
|
|
# Frame contribution loop (v0.14.0 BW-exact walk).
|
|
|
|
|
# Skip values:
|
|
|
|
|
# probe (fi=0): probe_skip
|
|
|
|
|
# meta@0x1002 (fi=1): 13 (6-byte inner header) -- event-1 walk only
|
|
|
|
|
# meta@0x1004 (fi=2): 13 (6-byte inner header) -- event-1 walk only
|
|
|
|
|
# sample chunks (fi=3+): 12 (5-byte inner header)
|
|
|
|
|
# every later frame: 12 stripped bytes (_raw_skip): 13 raw for an
|
|
|
|
|
# address with an inner DLE pair (the metadata
|
|
|
|
|
# pages 0x1002 / 0x1004, or a chunk there), else 12
|
|
|
|
|
last_fi = len(body_frames) - 1
|
|
|
|
|
|
|
|
|
|
log.debug(
|
|
|
|
@@ -812,16 +816,10 @@ def write_blastware_file(
|
|
|
|
|
len(body_frames), last_fi,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
meta = _has_metadata_pages(body_frames)
|
|
|
|
|
all_bytes = bytearray()
|
|
|
|
|
|
|
|
|
|
for fi, frame in enumerate(body_frames):
|
|
|
|
|
if fi == 0:
|
|
|
|
|
skip = probe_skip
|
|
|
|
|
elif fi in (1, 2) and meta:
|
|
|
|
|
skip = 13 # metadata pages (event-1 walk only)
|
|
|
|
|
else:
|
|
|
|
|
skip = 12 # sample chunks
|
|
|
|
|
skip = probe_skip if fi == 0 else _raw_skip(frame.data, _A5_HEADER)
|
|
|
|
|
|
|
|
|
|
contribution = _frame_body_bytes(frame, skip)
|
|
|
|
|
log.debug("write_blastware_file: fi=%d skip=%d raw_data=%d contribution=%d",
|
|
|
|
@@ -829,14 +827,15 @@ def write_blastware_file(
|
|
|
|
|
all_bytes.extend(contribution)
|
|
|
|
|
|
|
|
|
|
# Terminator contributes its content, which ends with the 26-byte footer.
|
|
|
|
|
# skip=11 (not 12) because the terminator's inner frame header is 4 bytes,
|
|
|
|
|
# one shorter than chunk frames' 5-byte inner header. Confirmed 2026-04-21.
|
|
|
|
|
# 11 stripped bytes (not 12): the terminator's inner frame header is one
|
|
|
|
|
# shorter than a chunk's. Confirmed 2026-04-21.
|
|
|
|
|
if term_frame is not None:
|
|
|
|
|
term_contribution = _frame_body_bytes(term_frame, 11)
|
|
|
|
|
term_skip = _raw_skip(term_frame.data, _A5_TERM_HEADER)
|
|
|
|
|
term_contribution = _frame_body_bytes(term_frame, term_skip)
|
|
|
|
|
log.debug(
|
|
|
|
|
"write_blastware_file: term_frame data_len=%d skip=11 "
|
|
|
|
|
"write_blastware_file: term_frame data_len=%d skip=%d "
|
|
|
|
|
"contribution_len=%d first8=%s",
|
|
|
|
|
len(term_frame.data),
|
|
|
|
|
len(term_frame.data), term_skip,
|
|
|
|
|
len(term_contribution),
|
|
|
|
|
term_contribution[:8].hex() if len(term_contribution) >= 8 else term_contribution.hex(),
|
|
|
|
|
)
|
|
|
|
|