libraryextensions/blender/demos/Vcd.xtl
Vcd.xtl -- a simulation's waveform (a VCD file, as iverilog writes it) read into arrays: every change of every signal as a row of numbers, and any signal's value at any times. Values wider than 62 bits do not fit an Int (split them into narrower signals); x and z read as 0. Used by the hex-run video (HexRun.xtl). Import: "vc:" u_se< "Vcd"
Reading
ʰw̲ords : Char -> Box Char
ʰw̲ords ← { t → (n̲ot t = f̲irst " ") p̲artition t }
ʰv̲alue : (Num a, Truthy a) => Char -> a
ʰv̲alue ← { l → f̲irst l = f̲irst "b" ? 2 d̲ecode (1 d̲rop ((f̲irst l i̲ndexOf " ") − 1) t̲ake l) = f̲irst "1" (f̲irst l = f̲irst "1") × 1 }
ʰi̲d : Char -> Char
ʰi̲d ← { l → f̲irst l = f̲irst "b" ? d̲isclose f̲irst r̲ev ʰw̲ords l 1 d̲rop l }
ˡr̲ead : Char -> (Box Char, Int)
vc:r_ead path: the VCD file at path as (names, changes): the signals' names in the order they are declared, and a row per change -- the time (in the file's units), the signal (its place in names, from 1), the value -- in the order of the file
"vc:" u̲se< "Vcd"
n ← "$var wire 1 ! go $end\n$var wire 4 \" n [3:0] $end\n$enddefinitions $end\n#0\n0!\nb0 \"\n#5\n1!\nb1010 \"\n" ⎕N̲PUT "work/t.vcd"
(names, changes) ← ᵛᶜr̲ead "work/t.vcd"
changes
0 1 0
0 2 0
5 1 1
5 2 10
ˡr̲ead ← { path → t ← ⎕N̲GET path lines ← (n̲ot t = f̲irst "\n") p̲artition t decls ← ('{ "$var " m̲atch 5 t̲ake d̲isclose ⍵ } e̲ach lines) r̲eplicate lines ids ← '{ d̲isclose 4 s̲elect ʰw̲ords d̲isclose ⍵ } m̲ap decls names ← '{ d̲isclose 5 s̲elect ʰw̲ords d̲isclose ⍵ } m̲ap decls k ← '{ f̲irst d̲isclose ⍵ } e̲ach lines keep ← k m̲ember? "#b01xz" body ← keep r̲eplicate lines isT ← (keep r̲eplicate k) = f̲irst "#" at ← (n̲ot isT) r̲eplicate ('+ s̲\ isT) s̲elect '{ f̲irst n̲umbers 1 d̲rop d̲isclose ⍵ } e̲ach isT r̲eplicate body ch ← (n̲ot isT) r̲eplicate body sig ← ids i̲ndexOf '{ ʰi̲d d̲isclose ⍵ } m̲ap ch v ← '{ ʰv̲alue d̲isclose ⍵ } e̲ach ch (names, 2 1 t̲ranspose (3 c̲at t̲ally v) r̲eshape (f̲loor at) c̲at sig c̲at v) }
Values
ˡc̲hanges : Any a => ((Box Char, Int), Char) -> a -> Int
(vcd, name) vc:c_hanges 0: a signal's changes as rows of time and value
"vc:" u̲se< "Vcd"
n ← "$var wire 1 ! go $end\n$enddefinitions $end\n#0\n0!\n#5\n1!\n#9\n0!\n" ⎕N̲PUT "work/t.vcd"
(ᵛᶜr̲ead "work/t.vcd", "go") ᵛᶜc̲hanges 0 0 0 5 1 9 0
ˡc̲hanges ← { ((names, changes), name) z → i ← f̲irst names i̲ndexOf e̲nclose name i > t̲ally names ? ⎕P̲ANIC "vcd: no signal " c̲at name rows ← (i = 2 s̲elect₂ changes) r̲eplicate changes 2 1 t̲ranspose (2 c̲at t̲ally rows) r̲eshape (1 s̲elect₂ rows) c̲at 3 s̲elect₂ rows }
ˡa̲t : Any a => ((Box Char, Int), Char, Int) -> a -> Int
(vcd, name, ts) vc:a_t 0: a signal's value at each time of ts (in order or not): its last change at or before it (0 before the first)
"vc:" u̲se< "Vcd"
n ← "$var wire 1 ! go $end\n$enddefinitions $end\n#0\n0!\n#5\n1!\n#9\n0!\n" ⎕N̲PUT "work/t.vcd"
(ᵛᶜr̲ead "work/t.vcd", "go", 0 4 5 8 9 20) ᵛᶜa̲t 0 0 0 1 1 0 0
ˡa̲t ← { ((names, changes), name, ts) z → c ← ((names, changes), name) ˡc̲hanges 0 ct ← 1 s̲elect₂ c n ← t̲ally ct ⍝ the changes and the times sorted together, a change before a time ⍝ it equals: how many changes come before a time is its last change order ← g̲rade (2 × ct) c̲at 1 + 2 × ts k ← (n d̲rop g̲rade order) s̲elect '+ s̲\ order ≤ n (1 + k) s̲elect 0 c̲at 2 s̲elect₂ c }