#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Read DSView ``.dsl`` logic-analyser captures (our own reader; no DSView or sigrok code). The format, as seen in the files themselves (Gearlynx's Lynx II captures, data recorded from a console): a zip holding ``header`` (INI-style: ``samplerate``, ``total samples``, ``total blocks``, ``probeN = name``) and one member per probe and block, ``L-/``, each the probe's samples packed one bit per sample, least significant bit first. python -I scripts/dsl.py CAPTURE.dsl # header and edge counts """ from __future__ import annotations import sys import zipfile from dataclasses import dataclass from pathlib import Path @dataclass class Capture: samplerate: int samples: int probes: dict[str, int] # name -> index zipped: zipfile.ZipFile blocks: int def bits(self, name: str) -> bytes: index = self.probes[name] return b"".join(self.zipped.read(f"L-{index}/{b}") for b in range(self.blocks)) def edges(self, name: str) -> tuple[int, list[int]]: """(level at sample 0, sample indices where the level changes).""" data = self.bits(name) first = data[0] & 1 changes = [] level = first for i, byte in enumerate(data): if byte == (0xFF if level else 0x00): continue for bit in range(8): b = (byte >> bit) & 1 if b != level: changes.append(i * 8 + bit) level = b return first, changes def ns(self, samples: int) -> float: return samples * 1e9 / self.samplerate def open_capture(path: Path) -> Capture: zipped = zipfile.ZipFile(path) header = {} for line in zipped.read("header").decode("utf-8", "replace").splitlines(): if "=" in line: key, value = (part.strip() for part in line.split("=", 1)) header[key] = value rate_text = header["samplerate"].split() rate = float(rate_text[0]) * {"Hz": 1, "KHz": 1e3, "kHz": 1e3, "MHz": 1e6}[rate_text[1]] probes = {v: int(k[5:]) for k, v in header.items() if k.startswith("probe") and k[5:].isdigit()} return Capture(int(rate), int(header["total samples"]), probes, zipped, int(header.get("total blocks", 1))) # fmt: skip def lows(capture: Capture, name: str) -> list[tuple[int, int]]: """(start, end) sample pairs of the probe's low pulses.""" first, changes = capture.edges(name) pulses = [] level = first start = 0 if first == 0 else None for at in changes: level ^= 1 if level == 0: start = at elif start is not None: pulses.append((start, at)) start = None return pulses def main(argv: list[str]) -> int: capture = open_capture(Path(argv[0])) print(f"{capture.samples:,} samples at {capture.samplerate / 1e6:g} MHz") for name in capture.probes: _first, changes = capture.edges(name) print(f" {name:8} {len(changes):>9,} edges") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))