protocol-emulator / all protocols

spdif

Held out: sealed in test/heldout.sha256 on 2026-10-02 and opened on 2026-11-02 (test/heldout.txt). The drawn and held-out runs are on Flexibility.

Spec source

partdocumentpage
S/PDIF, IEC 60958 type IIWikipedia, S/PDIF, revision 1378032122 (2 October 2026)p.1, S/PDIF control word components, byte 3, spec.txt l.222-224
S/PDIF, IEC 60958 type IIWikipedia, S/PDIF, revision 1378032122 (2 October 2026)p.1, text, spec.txt l.180

Datasheet limits

firmwareparameterlimitbound byneeds (cycles)bound (cycles)bound (ns)resultmarginsheet
spdifa block of 192 frames, 44.1 kHz and 1100 ppm>= 4.34896e+06 nsrun2087512089794353729.2oka cycleWikipedia, S/PDIF, revision 1378032122 (2 October 2026) p.1, S/PDIF control word components, byte 3, spec.txt l.222-224
spdifa block of 192 frames, 44.1 kHz less 1100 ppm<= 4.35854e+06 nsrun2092082089794353729.2oka cycleWikipedia, S/PDIF, revision 1378032122 (2 October 2026) p.1, S/PDIF control word components, byte 3, spec.txt l.222-224
spdifa frame, Mode 0's 192 kHz at most>= 5208.33 nsrun251108822666.7oka cycleWikipedia, S/PDIF, revision 1378032122 (2 October 2026) p.1, text, spec.txt l.180

Protocol file and firmware

test/spdif.ml
open! Core
open Protocol_emulator
open Pin_trace
module Reg = Host_port.Reg

(* S/PDIF, from Wikipedia revision 1378032122 (2 October 2026), one page, read as the
   limit table spec.txt: biphase mark (l.171, l.179), 192 frames per block of two
   subframes (l.256), 44.1 kHz at code 0000 (l.222). The subframe layout (32 slots,
   preambles B M W, then V U C P) comes from sigrok's spdif decoder, since the page
   doesn't give it. *)

let pin = 5

(* The firmware reads the line back to make its transitions, so [in_base] is the line too.
   Half a slot is 8 32991/65536 cycles, so a frame is 128 half slots (44.1 kHz at 48 MHz). *)
let config =
  { Program_config.default with
    in_base = pin
  ; out_base = pin
  ; out_count = 1
  ; set_base = pin
  ; set_count = 1
  ; period_fraction = 32991
  }
;;

(* Takes two 16-bit samples per frame from the host, left then right, LSB first in slots
   12 to 27 with the rest 0. The channel status says consumer, PCM, 44.1 kHz, accuracy
   code 00. Subframes start and end high, so the preambles are fixed levels. [y] counts
   frames to the next B; right takes a sample only if left did (empty fifo: silence). *)
let firmware =
  [%firmware
    {|
    set pins, 1              ; idle high
    set p, 8
    set y, 0
    wait tx
    mov t, now
    add t, p
    add t, p
    add t, p
    add t, p
frame:
    wait t+
    set pins, 0              ; B or M, 000 first
    jmp !tx, quiet
    pull
    mov isr, !null
    wait t+
    jmp counted
quiet:
    mov osr, null
    mov isr, null
    wait t+
counted:
    wait t+
    wait t+
    set pins, 1
    jmp y--, m
    set y, 23                ; B: 191 more frames in the block
    add y, y
    wait t+
    set pins, 0
    add y, y
    add y, y
    add y, 7
    wait t+
    set pins, 1
    wait t+
    wait t+
    jmp left
m:
    wait t+
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
left:
    set x, 7
laux:
    wait t+
    mov pins, !pins          ; slots 4 to 11, 0
    wait t+
    jmp x--, laux
lbit:
    wait t+
    mov pins, !pins          ; slots 12 to 27, the sample
    out x, 1
    jmp x--, lone
    wait t+
    jmp !osre, lbit
    jmp lvuc
lone:
    wait t+
    mov pins, !pins
    jmp !osre, lbit
lvuc:
    set x, 2
lzero:
    wait t+
    mov pins, !pins          ; V, U and C, 0
    wait t+
    jmp x--, lzero
    wait t+
    mov pins, !pins          ; P
    wait t+
    set pins, 1
    wait t+
    set pins, 0              ; W, 00011011
    mov x, isr
    jmp x--, rpull
    mov osr, null
    wait t+
    jmp right
rpull:
    pull
    wait t+
right:
    wait t+
    wait t+
    set pins, 1
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
    wait t+
    set x, 7
raux:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, raux
rbit:
    wait t+
    mov pins, !pins
    out x, 1
    jmp x--, rone
    wait t+
    jmp !osre, rbit
    jmp rvuc
rone:
    wait t+
    mov pins, !pins
    jmp !osre, rbit
rvuc:
    set x, 2
rzero:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, rzero
    wait t+
    mov pins, !pins
    wait t+
    set pins, 1
    jmp frame
|}
      ~config]
;;

let certified = [ Certified.of_firmware "spdif" firmware ]
let words = [ 0x1234; 0xfedc; 0x8001; 0x7ffe; 0x00ff; 0xa5a5 ]

(* 194 frames: two B preambles, a block apart *)
let bench =
  { Bench.name = "spdif"
  ; what =
      "Spdif.firmware: S/PDIF on OUT0, biphase mark, stereo 16-bit samples at 44.1 kHz, \
       halves of 8 32991/65536 cycles, silence once the fifo is empty"
  ; source = Timed_program.source firmware
  ; config
  ; assumption = Nothing
  ; clock_hz = Bench.clock_hz
  ; load = None
  ; stimulus = Some { bursts = [ words ]; quiet = 0; cycles = 194 * 1089 }
  }
;;

(* Start of each B or M preamble in a run (the only pulses of three half slots at 128 half
   slots per frame, 44.1 kHz), and whether it is a B (3113). *)
let frame_starts ~clock_hz (levels : Datasheet.Levels.t) =
  let half_ns = 1e9 /. (44_100. *. 128.) in
  let halves cycles =
    Float.iround_nearest_exn
      (Float.of_int cycles *. 1e9 /. Float.of_int clock_hz /. half_ns)
  in
  let pulses =
    List.folding_map levels ~init:0 ~f:(fun start (_, n) -> start + n, (start, halves n))
    |> List.tl
    |> Option.value ~default:[]
  in
  let rec scan acc = function
    | (start, 3) :: (_, a) :: (_, b) :: (_, c) :: rest ->
      let acc =
        match a, b, c with
        | 1, 1, 3 -> (start, true) :: acc
        | 3, 1, 1 -> (start, false) :: acc
        | _ -> acc
      in
      scan acc rest
    | _ :: rest -> scan acc rest
    | [] -> List.rev acc
  in
  scan [] pulses
;;

let apart starts =
  match starts with
  | [] -> []
  | _ :: later -> List.map2_exn (List.drop_last_exn starts) later ~f:(fun a b -> b - a)
;;

(* The limit table's byte 3 of the channel status (p.1, spec.txt l.222-224), which this
   firmware sends as 0: code 0000, 44.1 kHz, accuracy code 00, 1100 ppm. An accuracy is a
   mean rate, so it holds over a block of 192 frames (l.182, l.256); a frame alone is 1088
   or 1089 cycles. Mode 0's maximum, 192 kHz (l.180), is checked a frame at a time. *)
let limits =
  let sheet ~where =
    { Datasheet.Sheet.part = "S/PDIF, IEC 60958 type II"
    ; document = "Wikipedia, S/PDIF, revision 1378032122 (2 October 2026)"
    ; page = [%string "p.1, %{where}"]
    }
  in
  let control =
    sheet ~where:"S/PDIF control word components, byte 3, spec.txt l.222-224"
  in
  let block =
    Datasheet.run ~pin:Datasheet.set_pin (fun ~clock_hz levels ->
      frame_starts ~clock_hz levels
      |> List.filter_map ~f:(fun (start, b) -> Option.some_if b start)
      |> apart)
  in
  let frame =
    Datasheet.run ~pin:Datasheet.set_pin (fun ~clock_hz levels ->
      frame_starts ~clock_hz levels |> List.map ~f:fst |> apart)
  in
  let row parameter limit sheet bound =
    { Datasheet.firmware = bench.name; parameter; limit; sheet; margin = Cycle; bound }
  in
  let block_ns ~ppm = 192e9 /. (44_100. *. (1. +. (ppm /. 1e6))) in
  [ row
      "a block of 192 frames, 44.1 kHz and 1100 ppm"
      (At_least (block_ns ~ppm:1100.))
      control
      block
  ; row
      "a block of 192 frames, 44.1 kHz less 1100 ppm"
      (At_most (block_ns ~ppm:(-1100.)))
      control
      block
  ; row
      "a frame, Mode 0's 192 kHz at most"
      (At_least (1e9 /. 192_000.))
      (sheet ~where:"text, spec.txt l.180")
      frame
  ]
;;

(* sigrok's spdif decoder spends the first subframe, B and the first left, on finding its
   three pulse widths. It joins 34 cycles into the 37 the line idles high. *)
let sigrok =
  { Sigrok.clock_hz = Bench.clock_hz
  ; decoders = [ Sigrok.decoder "spdif" ~pins:[ "data", pin ] ]
  ; expect =
      [ ( "spdif=preamble"
        , List.init 9 ~f:(fun i -> if i % 2 = 0 then "Preamble W" else "Preamble M")
          |> Sigrok.lines "spdif" )
      ; ( "spdif=samples"
        , List.tl_exn words @ [ 0; 0; 0 ]
          |> List.map ~f:(fun sample -> sprintf "Audio 0x%x" (sample lsl 8))
          |> Sigrok.lines "spdif" )
      ]
  ; joins_after =
      Some
        ( 34
        , { why =
              "sigrok's spdif decoder times its first pulse from the capture's start, so \
               the line low from reset is one of its three widths and no preamble reads"
          ; first_difference = 0
          ; reads = ""
          } )
  ; rejected = None
  ; teeth =
      [ (* edge 54 is the middle of bit 2 of the first right, 0xfedc: gone, it reads 0 *)
        [ Flip { pin; edge = 54; after = 0; cycles = 10 * 1089 } ]
      ; (* edge 84 ends the first M's three halves low: two, and it reads no preamble *)
        [ Shift { pin; edge = 84; cycles = -8 } ]
      ]
  }
;;

(* three stereo frames, then silent ones as the fifo runs dry *)
let scenario =
  { Scenario.name = "spdif"
  ; peer = (fun () -> Peer.idle 0)
  ; script =
      Scenario.load
        ~assumptions:(Certified.assumptions (List.hd_exn certified))
        ~config:(Timed_program.config firmware)
        ~program:(Timed_program.words firmware)
        ()
      @ [ Write (Reg.tx, words); Scenario.start; Run (5 * 1089) ]
  ; sigrok = Some sigrok
  }
;;

let protocol =
  { Protocol.name = "spdif"
  ; certified
  ; time_triggered = []
  ; bench = [ bench ]
  ; loaded_from_hex = []
  ; limits
  ; unlimited = []
  ; swept = []
  ; not_swept =
      [ "spdif", "a fixed 64-slot subframe with no field to carry a cycle stamp" ]
  ; scenarios = [ scenario ]
  ; decoded = []
  }
;;
certified firmware spdif
    set pins, 1              ; idle high
    set p, 8
    set y, 0
    wait tx
    mov t, now
    add t, p
    add t, p
    add t, p
    add t, p
frame:
    wait t+
    set pins, 0              ; B or M, 000 first
    jmp !tx, quiet
    pull
    mov isr, !null
    wait t+
    jmp counted
quiet:
    mov osr, null
    mov isr, null
    wait t+
counted:
    wait t+
    wait t+
    set pins, 1
    jmp y--, m
    set y, 23                ; B: 191 more frames in the block
    add y, y
    wait t+
    set pins, 0
    add y, y
    add y, y
    add y, 7
    wait t+
    set pins, 1
    wait t+
    wait t+
    jmp left
m:
    wait t+
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
left:
    set x, 7
laux:
    wait t+
    mov pins, !pins          ; slots 4 to 11, 0
    wait t+
    jmp x--, laux
lbit:
    wait t+
    mov pins, !pins          ; slots 12 to 27, the sample
    out x, 1
    jmp x--, lone
    wait t+
    jmp !osre, lbit
    jmp lvuc
lone:
    wait t+
    mov pins, !pins
    jmp !osre, lbit
lvuc:
    set x, 2
lzero:
    wait t+
    mov pins, !pins          ; V, U and C, 0
    wait t+
    jmp x--, lzero
    wait t+
    mov pins, !pins          ; P
    wait t+
    set pins, 1
    wait t+
    set pins, 0              ; W, 00011011
    mov x, isr
    jmp x--, rpull
    mov osr, null
    wait t+
    jmp right
rpull:
    pull
    wait t+
right:
    wait t+
    wait t+
    set pins, 1
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
    wait t+
    set x, 7
raux:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, raux
rbit:
    wait t+
    mov pins, !pins
    out x, 1
    jmp x--, rone
    wait t+
    jmp !osre, rbit
    jmp rvuc
rone:
    wait t+
    mov pins, !pins
    jmp !osre, rbit
rvuc:
    set x, 2
rzero:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, rzero
    wait t+
    mov pins, !pins
    wait t+
    set pins, 1
    jmp frame
bench firmware spdif, 48 MHz
    set pins, 1              ; idle high
    set p, 8
    set y, 0
    wait tx
    mov t, now
    add t, p
    add t, p
    add t, p
    add t, p
frame:
    wait t+
    set pins, 0              ; B or M, 000 first
    jmp !tx, quiet
    pull
    mov isr, !null
    wait t+
    jmp counted
quiet:
    mov osr, null
    mov isr, null
    wait t+
counted:
    wait t+
    wait t+
    set pins, 1
    jmp y--, m
    set y, 23                ; B: 191 more frames in the block
    add y, y
    wait t+
    set pins, 0
    add y, y
    add y, y
    add y, 7
    wait t+
    set pins, 1
    wait t+
    wait t+
    jmp left
m:
    wait t+
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
left:
    set x, 7
laux:
    wait t+
    mov pins, !pins          ; slots 4 to 11, 0
    wait t+
    jmp x--, laux
lbit:
    wait t+
    mov pins, !pins          ; slots 12 to 27, the sample
    out x, 1
    jmp x--, lone
    wait t+
    jmp !osre, lbit
    jmp lvuc
lone:
    wait t+
    mov pins, !pins
    jmp !osre, lbit
lvuc:
    set x, 2
lzero:
    wait t+
    mov pins, !pins          ; V, U and C, 0
    wait t+
    jmp x--, lzero
    wait t+
    mov pins, !pins          ; P
    wait t+
    set pins, 1
    wait t+
    set pins, 0              ; W, 00011011
    mov x, isr
    jmp x--, rpull
    mov osr, null
    wait t+
    jmp right
rpull:
    pull
    wait t+
right:
    wait t+
    wait t+
    set pins, 1
    wait t+
    wait t+
    set pins, 0
    wait t+
    set pins, 1
    wait t+
    set x, 7
raux:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, raux
rbit:
    wait t+
    mov pins, !pins
    out x, 1
    jmp x--, rone
    wait t+
    jmp !osre, rbit
    jmp rvuc
rone:
    wait t+
    mov pins, !pins
    jmp !osre, rbit
rvuc:
    set x, 2
rzero:
    wait t+
    mov pins, !pins
    wait t+
    jmp x--, rzero
    wait t+
    mov pins, !pins
    wait t+
    set pins, 1
    jmp frame

Certificate

Proof run stamped in demo/speed.tsv: 2026-11-02T22:16:47+00:00, .

Clock window

firmwareshipped Hzlowest Hzset byhighest Hzset by
spdif4800000047947294a block of 192 frames, 44.1 kHz less 1100 ppm <= 4.35854e+06 ns48052433a block of 192 frames, 44.1 kHz and 1100 ppm >= 4.34896e+06 ns

Time taken

stagetimenote
spec2026-11-02T21:45:01+00:00new-protocol
file2026-11-02T22:16:16+00:00
certificate2026-11-02T22:16:47+00:00

spec to file: 31.2 min; spec to certificate: 31.8 min; spec to bench: -

Times are the first mark of each stage in demo/speed.tsv; a protocol nobody timed has no row.

Board witnesses

test/boardtimedemoresultcommitwindows provedteeth
spdif2026-11-18T14:53:51spdifPASS5160bcb3c79 of 9 (1 spec, 6 own, 2 derived)the first left sample's bits 4 and 5 cleared; the first half slot cut to a sample

CI decodes each capture again and holds it to the bytes sent and the windows, each tooth refused: demo/witness.py check.