1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
open Base
open Hardcaml
open Stdio
include Waveform_intf
let default_display_rules =
Display_rule.
[ port_name_is "clk" ~wave_format:Bit
; port_name_is "clock" ~wave_format:Bit
; port_name_is "rst" ~wave_format:Bit
; port_name_is "reset" ~wave_format:Bit
; port_name_is "clr" ~wave_format:Bit
; port_name_is "clear" ~wave_format:Bit
; port_name_is "enable" ~wave_format:Bit
; input ()
; output ()
; default
]
;;
let apply_wave_format
(t : _ Wave.t)
(format : Wave_format.t option)
(alignment : Text_alignment.t)
: _ Wave.t
=
let wave_format =
match format, Wave.get_format t with
| None, fmt -> fmt
| Some fmt, _ -> fmt
in
match t with
| Binary { name; data; style } ->
(match wave_format with
| Bit | Bit_or _ -> t
| _ ->
Data
{ name
; data
; width = 1
; wave_format = { default = wave_format; current = wave_format }
; text_alignment = alignment
; style
})
| Data { name; data; width; wave_format = _; text_alignment = _; style } ->
(match wave_format with
| (Bit | Bit_or _) when width = 1 -> Binary { name; data; style }
| _ ->
Data
{ name
; data
; width
; wave_format = { current = wave_format; default = wave_format }
; text_alignment = alignment
; style
})
| Empty _ | Divider _ | Clock _ -> t
;;
let create_wave
(type wave_data)
({ name; width; wave_format; is_pseudo_clock; wave_data; _ } :
wave_data Hardcaml.Wave_data.Wave.t)
: wave_data Wave.t
=
if is_pseudo_clock
then Clock { name; style = { style = Style.default } }
else if width = 1
&&
match wave_format with
| Bit | Bit_or _ -> true
| _ -> false
then Binary { name; data = wave_data; style = { style = Style.default } }
else
Data
{ name
; data = wave_data
; width
; wave_format = { current = wave_format; default = wave_format }
; text_alignment = Left
; style = { style = Style.default }
}
;;
let sort_ports_and_formats
(type wave_data)
(t : wave_data Wave_data.Wave.t array)
display_rules
: wave_data Wave.t array
=
let display_rules = Option.value ~default:default_display_rules display_rules in
let waves =
Array.fold
t
~init:(Map.empty (module Port_name))
~f:(fun map (wave : _ Wave_data.Wave.t) ->
match Map.add map ~key:(Port_name.of_string wave.name) ~data:wave with
| `Ok map -> map
| `Duplicate -> map)
in
let ports =
Array.map t ~f:(fun wave ->
{ Port.port_name = Port_name.of_string wave.name
; type_ = wave.typ
; width = wave.width
})
|> Array.to_list
in
Display_rules.sort_ports_and_formats display_rules ports
|> List.filter_map ~f:(function
| Divider name -> Some (Wave.Divider { name; style = { style = Style.default } })
| (Port { port; format = fmt_align_opt } : Display_rules.matched_rule) ->
Map.find waves port.port_name
|> Option.map ~f:(fun wave ->
let wave = create_wave wave in
match fmt_align_opt, wave with
| Some { wave_format; alignment }, _ ->
apply_wave_format wave wave_format alignment
| None, Data _ -> wave
| None, _ ->
Display_rules.run_rule Display_rule.Default port
|> Option.map ~f:(fun { wave_format; alignment } ->
apply_wave_format wave wave_format alignment)
|> Option.value ~default:wave))
|> Array.of_list
;;
module type Context = sig
type t [@@deriving sexp_of, equal ~localize]
val to_ctx
: ?display_rules:Display_rules.t
-> ?signals_alignment:Text_alignment.t
-> ?display_width:int
-> ?display_height:int
-> ?wave_width:int
-> ?start_cycle:int
-> ?display_values:bool
-> ?signals_width:int
-> t
-> Draw.ctx
val look_for_nth_instance_of_condition_in_waveform
: ?pos:int
-> ?span:int
-> ?negate:bool
-> n:int
-> conditions:Wave_condition.t list
-> t
-> int option
end
module Make_context (Data : Hardcaml.Wave_data.S) = struct
module Render = Render.Make (Data)
type t = Data.t Hardcaml.Wave_data.Wave.t array [@@deriving sexp_of, equal ~localize]
let to_ctx
?display_rules
?signals_alignment
?(display_width = 70)
?display_height
?(wave_width = 3)
?(start_cycle = 0)
?(display_values = false)
?signals_width
t
=
if display_width < 7
then raise_s [%message "Invalid display width. Must be >= 7." (display_width : int)];
Option.iter signals_width ~f:(fun signals_width ->
if signals_width >= display_width
then
raise_s
[%message
"Invalid signals_width. Require signals_width < display_width."
(signals_width : int)
(display_width : int)]);
let waves =
{ Waves.cfg = { Waves.Config.default with wave_width; start_cycle }
; waves = sort_ports_and_formats t display_rules
}
in
let display_height =
match display_height with
| Some display_height ->
if display_height < 3
then
raise_s
[%message "Invalid display height. Must be >= 3." (display_height : int)];
display_height
| None ->
Int.min
256
(2
+ Array.fold waves.waves ~init:0 ~f:(fun acc w ->
acc + Wave.get_height_in_chars w))
in
Render.Static.draw
?signals_alignment
?signals_width
~values:display_values
~style:Window_styles.black_on_white
~rows:display_height
~cols:display_width
waves
;;
let look_for_nth_instance_of_condition_in_waveform
?(pos = 0)
?span
?(negate = false)
~n
~(conditions : Wave_condition.t list)
(waves : Data.t Hardcaml.Wave_data.Wave.t array)
=
assert (List.length conditions > 0);
assert (pos >= 0);
let events_per_condition =
List.map conditions ~f:(fun { how_to_find; condition = _ } ->
let matches_name name =
match how_to_find with
| Wave_condition.How_to_find.Suffix suffix -> String.is_suffix name ~suffix
| Regex re -> Re.execp (Display_rule.Regexp.compile re) name
in
let potential_matches =
Array.filter_map waves ~f:(fun wave ->
if (not wave.is_pseudo_clock) && matches_name wave.name
then Some (wave.name, wave.wave_data)
else None)
in
let how_to_find_str =
match how_to_find with
| Suffix suffix -> [%string "suffix \"%{suffix}\""]
| Regex _ -> "regex"
in
match Array.length potential_matches with
| 0 ->
failwith
[%string
"Your %{how_to_find_str} didn't match any wave in the waveform! Please try \
again."]
| 1 -> snd potential_matches.(0)
| _ ->
raise_s
[%message
"Your pattern matched multiple signals in the waveform! Please be more \
specific."
(how_to_find_str : string)
~matches:(Array.map ~f:fst potential_matches : string array)])
in
let num_events =
match
List.map events_per_condition ~f:Data.length |> List.all_equal ~equal:Int.equal
with
| None ->
raise_s
[%message
"Expect all signals to have the same number of events! If this is no longer \
true, this function will have to be rewritten based on the new encoding \
scheme."]
| Some v -> v
in
let end_exclusive =
match span with
| None -> num_events
| Some span -> Int.min (pos + span) num_events
in
With_return.with_return (fun { return } ->
let num_found = ref 0 in
for i = pos to end_exclusive - 1 do
let conditions_met =
List.for_all2_exn
conditions
events_per_condition
~f:(fun { condition; how_to_find = _ } events ->
let ev = Data.get events i in
condition ev)
in
let matched = if negate then not conditions_met else conditions_met in
if matched
then (
Int.incr num_found;
if !num_found = n then return (Some i))
done;
None)
;;
end
module Make_api (Context : Context) = struct
include Context
let to_buffer
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
t
=
let buffer = Buffer.create 1024 in
let ctx =
to_ctx
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
t
in
Write.utf8 (fun s -> Buffer.add_string buffer s) ctx;
buffer
;;
let to_string
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
t
=
to_buffer
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
t
|> Buffer.contents
;;
let print
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
?(channel = Out_channel.stdout)
t
=
let ctx =
to_ctx
?display_rules
?display_width
?display_height
?display_values
?wave_width
?signals_width
?start_cycle
?signals_alignment
t
in
Write.utf8 (Out_channel.output_string channel) ctx
;;
let look_for_first_instance_of_condition_in_wave ~f ~wave_name t =
look_for_nth_instance_of_condition_in_waveform
t
~n:1
~conditions:[ { how_to_find = Suffix wave_name; condition = f } ]
;;
let look_for_wave_first_vdd_cycle ~how_to_find t =
look_for_nth_instance_of_condition_in_waveform
~n:1
~conditions:[ { condition = Bits.to_bool; how_to_find } ]
t
;;
end
module type S_with_context = sig
include Context
include S with type t := t
end
module Make (Data : Hardcaml.Wave_data.S) = struct
module Context = Make_context (Data)
module Api = Make_api (Context)
include Context
include Api
end
module By_cycle :
S_with_context
with type t = Hardcaml.Wave_data_in_cycles.t Hardcaml.Wave_data.Wave.t array =
Make (Hardcaml.Wave_data_in_cycles)
module By_event = Make (Hardcaml.Wave_data_in_events.Bits)
include Make_api (struct
type t = Hardcaml.Wave_data.t [@@deriving sexp_of, equal ~localize]
let to_ctx
?display_rules
?signals_alignment
?display_width
?display_height
?wave_width
?start_cycle
?display_values
?signals_width
t
=
match t with
| Hardcaml.Wave_data.By_cycle cycles ->
By_cycle.to_ctx
?display_rules
?signals_alignment
?display_width
?display_height
?wave_width
?start_cycle
?display_values
?signals_width
cycles
| By_event events ->
By_event.to_ctx
?display_rules
?signals_alignment
?display_width
?display_height
?wave_width
?start_cycle
?display_values
?signals_width
events
;;
let look_for_nth_instance_of_condition_in_waveform
?pos
?span
?negate
~n
~(conditions : Wave_condition.t list)
waves
=
match waves with
| Hardcaml.Wave_data.By_cycle cycles ->
By_cycle.look_for_nth_instance_of_condition_in_waveform
?pos
?span
?negate
~n
~conditions
cycles
| By_event events ->
By_event.look_for_nth_instance_of_condition_in_waveform
?pos
?span
?negate
~n
~conditions
events
;;
end)