sourceextensions/blender/demos/Vcd.xtl
1⍝│ Vcd.xtl -- a simulation's waveform (a VCD file, as iverilog writes
2⍝│ it) read into arrays: every change of every signal as a row of
3⍝│ numbers, and any signal's value at any times. Values wider than 62
4⍝│ bits do not fit an Int (split them into narrower signals); x and z
5⍝│ read as 0. Used by the hex-run video (HexRun.xtl).
6⍝│ Import: "vc:" u_se< "Vcd"
7
8⍝━━ Reading
9
10⍝ a line's words
11ʰw̲ords ← { t → (n̲ot t = f̲irst " ") p̲artition t }
12
13⍝ a value change's value: b and its bits, or one bit
14ʰv̲alue ← { l →
15 f̲irst l = f̲irst "b" ? 2 d̲ecode (1 d̲rop ((f̲irst l i̲ndexOf " ") − 1) t̲ake l) = f̲irst "1"
16 (f̲irst l = f̲irst "1") × 1
17}
18
19⍝ a value change's signal: after the space, or after the bit
20ʰi̲d ← { l →
21 f̲irst l = f̲irst "b" ? d̲isclose f̲irst r̲ev ʰw̲ords l
22 1 d̲rop l
23}
24
25⍝│ vc:r_ead path: the VCD file at path as (names, changes): the
26⍝│ signals' names in the order they are declared, and a row per change
27⍝│ -- the time (in the file's units), the signal (its place in names,
28⍝│ from 1), the value -- in the order of the file
29⍝│ >> "vc:" u_se< "Vcd"
30⍝│ >> 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"
31⍝│ >> (names, changes) := vc:r_ead "work/t.vcd"
32⍝│ >> changes
33⍝│ 0 1 0
34⍝│ 0 2 0
35⍝│ 5 1 1
36⍝│ 5 2 10
37ˡr̲ead ← { path →
38 t ← ⎕N̲GET path
39 lines ← (n̲ot t = f̲irst "\n") p̲artition t
40 decls ← ('{ "$var " m̲atch 5 t̲ake d̲isclose ⍵ } e̲ach lines) r̲eplicate lines
41 ids ← '{ d̲isclose 4 s̲elect ʰw̲ords d̲isclose ⍵ } m̲ap decls
42 names ← '{ d̲isclose 5 s̲elect ʰw̲ords d̲isclose ⍵ } m̲ap decls
43 k ← '{ f̲irst d̲isclose ⍵ } e̲ach lines
44 keep ← k m̲ember? "#b01xz"
45 body ← keep r̲eplicate lines
46 isT ← (keep r̲eplicate k) = f̲irst "#"
47 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
48 ch ← (n̲ot isT) r̲eplicate body
49 sig ← ids i̲ndexOf '{ ʰi̲d d̲isclose ⍵ } m̲ap ch
50 v ← '{ ʰv̲alue d̲isclose ⍵ } e̲ach ch
51 (names, 2 1 t̲ranspose (3 c̲at t̲ally v) r̲eshape (f̲loor at) c̲at sig c̲at v)
52}
53
54⍝━━ Values
55
56⍝│ (vcd, name) vc:c_hanges 0: a signal's changes as rows of time and
57⍝│ value
58⍝│ >> "vc:" u_se< "Vcd"
59⍝│ >> n := "$var wire 1 ! go $end\n$enddefinitions $end\n#0\n0!\n#5\n1!\n#9\n0!\n" []N_PUT "work/t.vcd"
60⍝│ >> (vc:r_ead "work/t.vcd", "go") vc:c_hanges 0
61⍝│ 0 0
62⍝│ 5 1
63⍝│ 9 0
64ˡc̲hanges ← { ((names, changes), name) z →
65 i ← f̲irst names i̲ndexOf e̲nclose name
66 i > t̲ally names ? ⎕P̲ANIC "vcd: no signal " c̲at name
67 rows ← (i = 2 s̲elect₂ changes) r̲eplicate changes
68 2 1 t̲ranspose (2 c̲at t̲ally rows) r̲eshape (1 s̲elect₂ rows) c̲at 3 s̲elect₂ rows
69}
70
71⍝│ (vcd, name, ts) vc:a_t 0: a signal's value at each time of ts (in
72⍝│ order or not): its last change at or before it (0 before the first)
73⍝│ >> "vc:" u_se< "Vcd"
74⍝│ >> n := "$var wire 1 ! go $end\n$enddefinitions $end\n#0\n0!\n#5\n1!\n#9\n0!\n" []N_PUT "work/t.vcd"
75⍝│ >> (vc:r_ead "work/t.vcd", "go", 0 4 5 8 9 20) vc:a_t 0
76⍝│ 0 0 1 1 0 0
77ˡa̲t ← { ((names, changes), name, ts) z →
78 c ← ((names, changes), name) ˡc̲hanges 0
79 ct ← 1 s̲elect₂ c
80 n ← t̲ally ct
81 ⍝ the changes and the times sorted together, a change before a time
82 ⍝ it equals: how many changes come before a time is its last change
83 order ← g̲rade (2 × ct) c̲at 1 + 2 × ts
84 k ← (n d̲rop g̲rade order) s̲elect '+ s̲\ order ≤ n
85 (1 + k) s̲elect 0 c̲at 2 s̲elect₂ c
86}