protocol-emulator / all protocols
Drawn: picked from sigrok's decoders by drand round 33346612 (demo/corpus_draw_33346612.json). The drawn and held-out runs are on Flexibility.
Not available: no datasheet limits for this protocol.
Not available: no limits.
open! Core
open Protocol_emulator
open Pin_trace
module Reg = Host_port.Reg
(* SB-Projects, "Philips RC-6 Protocol", 17 July 2025, page 1: mode 0 symbols, in units of
1t, 16 carrier periods or 444 us. Leader: mark 6, space 2. A cell is a mark then a
space for a one, the reverse for a zero, 1 each; the trailer is a cell of 2 and 2. A
frame is leader, start bit 1, mode bits 000, trailer, 8 address bits, 8 command bits,
then 6 free. The line is demodulated: high for a mark, no carrier. *)
let pin = 5
let standard_unit = 21_333
let shortest_unit = 14
let config =
{ Program_config.default with
set_base = pin
; set_count = 1
; out_shift = Left
; pull_threshold = 16
}
;;
(* The host loads the unit in cycles, then per frame the trailer bit as 0 or 1, then the
address and command as one word, msb first. [t] is the next unit throughout and every
wait takes one. *)
let firmware =
[%firmware
{|
mov t, now
set pins, 0
wait tx
pull
mov p, osr ; the unit
message:
wait tx
pull
mov y, osr ; the trailer bit
wait tx
pull ; address, then command
mov t, now
add t, p
wait t+
set pins, 1 ; leader mark, 6 units
set x, 4
l0:
wait t+
jmp x--, l0
wait t+
set pins, 0 ; leader space, 2
wait t+
wait t+
set pins, 1 ; start bit, a one
wait t+
set pins, 0
wait t+
set x, 2
mode:
set pins, 0 ; three mode bits, zeros
wait t+
set pins, 1
wait t+
jmp x--, mode
jmp y--, trailer_one
set pins, 0 ; the trailer, 2 and 2
wait t+
wait t+
set pins, 1
wait t+
wait t+
jmp data
trailer_one:
set pins, 1
wait t+
wait t+
set pins, 0
wait t+
wait t+
data:
set y, 15
bit:
out x, 1
jmp x--, one
set pins, 0
wait t+
set pins, 1
wait t+
jmp y--, bit
jmp free
one:
set pins, 1
wait t+
set pins, 0
wait t+
jmp y--, bit
free:
set pins, 0
set x, 5
f0:
wait t+
jmp x--, f0 ; the signal free time, 6 units
jmp message
|}
~config
~period_floor:shortest_unit]
;;
(* The certificate holds at the unit the bench loads; the firmware works at any unit from
[shortest_unit]. *)
let certified =
[ Certified.of_firmware ~period:standard_unit ~no_wrap:true "ir_rc6" firmware ]
;;
module Message = struct
type t =
{ toggle : bool
; address : int
; command : int
}
[@@deriving sexp_of, equal]
end
let words ~toggle ~address ~command = [ Bool.to_int toggle; (address lsl 8) lor command ]
(* 16 periods of 36 kHz, in ns *)
let unit_ns = 444_444
(* the half cells from the start bit to the last data bit *)
let slots_in_frame = 44
let decode ~clock_hz levels =
let open Or_error.Let_syntax in
(* within 5%, exactly: cycles of 1e9 / clock_hz ns against ns *)
let nominal ~units = units * unit_ns * clock_hz in
let units cycles =
((cycles * 2_000_000_000) + nominal ~units:1) / (2 * nominal ~units:1)
in
let near cycles ~units =
abs ((cycles * 1_000_000_000) - nominal ~units) * 20 <= nominal ~units
in
let timed measured cycles ~units ~name =
let ns = Measured.ns ~clock_hz cycles in
if near cycles ~units
then return (Measured.add measured ~name ~ns)
else Or_error.error_s [%message "out of spec" name (ns : int)]
in
(* a run of low whose first [part] units end the last cell and the rest is signal free *)
let free measured cycles ~part =
let rest = (cycles * 1_000_000_000) - nominal ~units:part in
let ns = rest / clock_hz in
if rest * 20 >= 19 * nominal ~units:6
then return (Measured.add measured ~name:"signal free" ~ns)
else Or_error.error_s [%message "signal free time too short" (ns : int)]
in
let rec cells measured slots = function
| [] -> Or_error.error_s [%message "a frame cut short"]
| (level, cycles) :: rest ->
let have = List.length slots
and k = units cycles in
if have + k <= slots_in_frame && have < slots_in_frame
then (
let name = sprintf "%s %dt" (if level then "mark" else "space") k in
let%bind measured = timed measured cycles ~units:k ~name in
cells measured (List.init k ~f:(fun _ -> level) @ slots) rest)
else if level
then Or_error.error_s [%message "a mark in the signal free time"]
else (
let part = slots_in_frame - have in
let%map measured = free measured cycles ~part in
List.rev (List.init part ~f:(fun _ -> false) @ slots), measured, rest)
in
let message slots =
let slots = Array.of_list slots in
let bit i =
match slots.(2 * i), slots.((2 * i) + 1) with
| true, false -> return true
| false, true -> return false
| _ -> Or_error.error_s [%message "not a bi-phase cell" (i : int)]
in
let%bind start = bit 0 in
let%bind mode = List.init 3 ~f:(fun i -> bit (i + 1)) |> Or_error.all in
let%bind data = List.init 16 ~f:(fun i -> bit (6 + i)) |> Or_error.all in
let%bind () =
if start && not (List.exists mode ~f:Fn.id)
then return ()
else Or_error.error_s [%message "not mode 0" (start : bool) (mode : bool list)]
in
let%map toggle =
match slots.(8), slots.(9), slots.(10), slots.(11) with
| a, b, c, d when Bool.equal a b && Bool.equal c d && not (Bool.equal a c) ->
return a
| _ -> Or_error.error_s [%message "a trailer not of two and two"]
in
let value = List.fold data ~init:0 ~f:(fun acc b -> (acc lsl 1) lor Bool.to_int b) in
{ Message.toggle; address = value lsr 8; command = value land 0xff }
in
let rec messages measured = function
| [] -> return ([], measured)
| (true, mark) :: (false, space) :: rest ->
let%bind measured = timed measured mark ~units:6 ~name:"leader mark" in
let%bind measured = timed measured space ~units:2 ~name:"leader space" in
let%bind slots, measured, rest = cells measured [] rest in
let%bind message = message slots in
let%map messages, measured = messages measured rest in
message :: messages, measured
| _ -> Or_error.error_s [%message "not a leader"]
in
List.group levels ~break:Bool.( <> )
|> List.map ~f:(fun run -> List.hd_exn run, List.length run)
|> List.drop_while ~f:(fun (high, _) -> not high)
|> messages Measured.empty
;;
(* The copy python/bench_firmware.py holds: the unit the bench loads, 444 us at 48 MHz. *)
let bench =
{ Bench.name = "ir_rc6"
; what =
[%string
"Ir_rc6.firmware: RC-6 mode 0 frames, %{standard_unit#Int} cycles a 444 us unit, \
the line high for a mark, on OUT0"]
; source = Timed_program.source firmware
; config = Timed_program.config firmware
; assumption = Floor shortest_unit
; clock_hz = Bench.clock_hz
; load = Some standard_unit
; stimulus = None
}
;;
let limits : Datasheet.t list = []
(* A unit of 200 cycles, as a 450 kHz clock gives, keeps the simulation short: a frame
with the trailer bit low and one with it high. *)
let sent =
[ { Message.toggle = false; address = 0x1a; command = 0x35 }
; { toggle = true; address = 0x00; command = 0xff }
]
;;
let clock_hz = 450_000
let unit = 200
(* a frame is 58 units with the signal free time; sigrok's ir_rc6 ends a frame only after
12 half-bits of silence and misreads a leader within 3.5 more, so the next comes after
68 *)
let frame_cycles = 68 * unit
(* What sigrok's ir_rc6 prints, in its order: the header's six bits, its four fields, the
16 data bits, the address and the data. It handles the six bits on each edge until the
next bit comes, so the first frame's fields come twice. *)
let reading ~repeat_fields ({ toggle; address; command } : Message.t) =
let toggle = Bool.to_int toggle in
let fields =
[ "Synchronisation"; "Startbit"; "Field: 0"; [%string "Toggle: %{toggle#Int}"] ]
in
let data =
List.init 16 ~f:(fun i ->
Int.to_string ((((address lsl 8) lor command) lsr (15 - i)) land 1))
in
[ "1"; "1"; "0"; "0"; "0"; Int.to_string toggle ]
@ fields
@ (if repeat_fields then fields else [])
@ data
@ [ sprintf "Address: %02X" address; sprintf "Data: %02X" command ]
;;
let sigrok =
{ Sigrok.clock_hz
; decoders = [ Sigrok.decoder "ir_rc6" ~pins:[ "ir", pin ] ]
; expect =
[ ( "ir_rc6=bit:sync:startbit:field:togglebit:address:command"
, List.concat (List.mapi sent ~f:(fun i m -> reading ~repeat_fields:(i = 0) m))
|> Sigrok.lines "ir_rc6" )
]
; joins_after = None
; rejected = None
; (* edge 12 is the rise in the middle of the first data bit, a zero: a unit later it is
the end of the bit, which reads a one *)
teeth = [ [ Shift { pin; edge = 12; cycles = unit } ] ]
}
;;
let scenario =
let send { Message.toggle; address; command } =
[ Step.Write (Reg.tx, words ~toggle ~address ~command); Run frame_cycles ]
in
{ Scenario.name = "ir_rc6"
; 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)
()
@ [ Scenario.start; Write (Reg.tx, [ unit ]) ]
@ List.concat_map sent ~f:send
@ [ Run (8 * unit) ]
; sigrok = Some sigrok
}
;;
let protocol =
{ Protocol.name = "ir_rc6"
; certified
; time_triggered = []
; bench = [ bench ]
; loaded_from_hex = []
; limits
; unlimited =
[ ( "ir_rc6"
, "the spec gives typical times and no minimum or maximum; Ir_rc6.decode holds \
each mark and space to 5%" )
]
; swept = []
; not_swept =
[ "ir_rc6", "a 6 unit mark is 128000 cycles, past the 65536 a stamp tells apart" ]
; scenarios = []
; decoded = [ scenario ]
}
;;
mov t, now
set pins, 0
wait tx
pull
mov p, osr ; the unit
message:
wait tx
pull
mov y, osr ; the trailer bit
wait tx
pull ; address, then command
mov t, now
add t, p
wait t+
set pins, 1 ; leader mark, 6 units
set x, 4
l0:
wait t+
jmp x--, l0
wait t+
set pins, 0 ; leader space, 2
wait t+
wait t+
set pins, 1 ; start bit, a one
wait t+
set pins, 0
wait t+
set x, 2
mode:
set pins, 0 ; three mode bits, zeros
wait t+
set pins, 1
wait t+
jmp x--, mode
jmp y--, trailer_one
set pins, 0 ; the trailer, 2 and 2
wait t+
wait t+
set pins, 1
wait t+
wait t+
jmp data
trailer_one:
set pins, 1
wait t+
wait t+
set pins, 0
wait t+
wait t+
data:
set y, 15
bit:
out x, 1
jmp x--, one
set pins, 0
wait t+
set pins, 1
wait t+
jmp y--, bit
jmp free
one:
set pins, 1
wait t+
set pins, 0
wait t+
jmp y--, bit
free:
set pins, 0
set x, 5
f0:
wait t+
jmp x--, f0 ; the signal free time, 6 units
jmp message
mov t, now
set pins, 0
wait tx
pull
mov p, osr ; the unit
message:
wait tx
pull
mov y, osr ; the trailer bit
wait tx
pull ; address, then command
mov t, now
add t, p
wait t+
set pins, 1 ; leader mark, 6 units
set x, 4
l0:
wait t+
jmp x--, l0
wait t+
set pins, 0 ; leader space, 2
wait t+
wait t+
set pins, 1 ; start bit, a one
wait t+
set pins, 0
wait t+
set x, 2
mode:
set pins, 0 ; three mode bits, zeros
wait t+
set pins, 1
wait t+
jmp x--, mode
jmp y--, trailer_one
set pins, 0 ; the trailer, 2 and 2
wait t+
wait t+
set pins, 1
wait t+
wait t+
jmp data
trailer_one:
set pins, 1
wait t+
wait t+
set pins, 0
wait t+
wait t+
data:
set y, 15
bit:
out x, 1
jmp x--, one
set pins, 0
wait t+
set pins, 1
wait t+
jmp y--, bit
jmp free
one:
set pins, 1
wait t+
set pins, 0
wait t+
jmp y--, bit
free:
set pins, 0
set x, 5
f0:
wait t+
jmp x--, f0 ; the signal free time, 6 units
jmp message
ir_rc6: proved in CI by inductive_certificate, ocaml.yamlProof run stamped in demo/speed.tsv: 2026-10-24T18:51:27+01:00, .
Not available: no window for this protocol's firmware.
| stage | time | note |
|---|---|---|
| spec | 2026-10-24T18:12:52+01:00 | new-protocol |
| file | 2026-10-24T18:51:01+01:00 | |
| certificate | 2026-10-24T18:51:27+01:00 | |
| bench | 2026-10-30T14:59:56+00:00 | board run, test/board/ir_rc6 |
spec to file: 38.1 min; spec to certificate: 38.6 min; spec to bench: 5.9 days
Times are the first mark of each stage in demo/speed.tsv; a protocol nobody timed has no row.
| test/board | time | demo | result | commit | windows proved | teeth |
|---|---|---|---|---|---|---|
| ir_rc6 | 2026-11-03T07:58:44 | ir_rc6 | PASS | b938551d48 | 15 of 15 (15 own) | the first address bit's halves swapped; the first leader mark a unit short |
CI decodes each capture again and holds it to the bytes sent and the windows, each tooth refused: demo/witness.py check.