Source file signal.ml
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
open Base
open! Signal_intf
module Type = Signal__type
type t = Type.t
module Uid = Type.Uid
open Type
let names = Type.names
let uid = Type.uid
let add_attribute signal attribute =
match signal_id signal with
| None ->
raise_s
[%message
"attempt to add attribute to an empty signal" ~to_:(attribute : Rtl_attribute.t)]
| Some _ ->
Type.add_attribute signal attribute;
signal
;;
let add_attributes signal attributes =
List.iter attributes ~f:(fun x -> ignore (add_attribute signal x : t));
signal
;;
let signal =
match signal_id signal with
| None ->
raise_s [%message "attempt to add comment to an empty signal" ~to_:(comment : string)]
| Some _ ->
Type.set_comment signal comment;
signal
;;
let signal =
match signal_id signal with
| None -> raise_s [%message "attempt to remove comment from an empty signal"]
| Some _ ->
Type.unset_comment signal;
signal
;;
let set_names s names = Type.set_names s names
let attributes s =
match signal_id s with
| None -> []
| Some _ -> Type.get_attributes s
;;
let s =
match signal_id s with
| None -> raise_s [%message "cannot get [comment] from the empty signal"]
| Some _ -> Type.get_comment s
;;
let ( --$ ) s w =
Type.set_wave_format s w;
s
;;
module Base = struct
type nonrec t = t
let equal (t1 : t) t2 =
match t1, t2 with
| Empty, Empty -> true
| Const { constant = c1; _ }, Const { constant = c2; _ } -> Bits.equal c1 c2
| _ -> Uid.equal (uid t1) (uid t2)
;;
let width = width
let to_string = to_string
let sexp_of_t = sexp_of_t
let empty = Empty
let is_empty = function
| Empty -> true
| _ -> false
;;
let of_bits constant = Const { signal_id = make_id (Bits.width constant); constant }
let of_constant data = of_bits (Bits.of_constant data)
let to_constant signal =
if is_const signal
then Bits.to_constant (const_value signal)
else
raise_s [%message "cannot use [to_constant] on non-constant signal" ~_:(signal : t)]
;;
let ( -- ) signal name =
match signal_id signal with
| None ->
raise_s
[%message "attempt to set the name of the empty signal" ~to_:(name : string)]
| Some _ ->
Type.add_name signal name;
signal
;;
let op2 op len arg_a arg_b = Op2 { signal_id = make_id len; op; arg_a; arg_b }
let concat_msb a =
match a with
| [ a ] -> a
| _ ->
let len = List.fold a ~init:0 ~f:(fun acc a -> acc + width a) in
Cat { signal_id = make_id len; args = a }
;;
let select arg high low =
Select { signal_id = make_id (high - low + 1); arg; high; low }
;;
let ( +: ) a b = op2 Signal_add (width a) a b
let ( -: ) a b = op2 Signal_sub (width a) a b
let ( *: ) a b = op2 Signal_mulu (width a + width b) a b
let ( *+ ) a b = op2 Signal_muls (width a + width b) a b
let ( &: ) a b = op2 Signal_and (width a) a b
let ( |: ) a b = op2 Signal_or (width a) a b
let ( ^: ) a b = op2 Signal_xor (width a) a b
let ( ~: ) a = Not { signal_id = make_id (width a); arg = a }
let ( ==: ) a b = op2 Signal_eq 1 a b
let ( <: ) a b = op2 Signal_lt 1 a b
let mux select cases =
match cases with
| first_case :: _ -> Mux { signal_id = make_id (width first_case); select; cases }
| [] -> raise_s [%message "Mux with no cases"]
;;
end
module Comb_make = Comb.Make
module Unoptimized = Comb.Make (Base)
let ( <== ) a b =
match a with
| Wire { driver; _ } ->
if not (is_empty !driver)
then
raise_s
[%message
"attempt to assign wire multiple times"
~already_assigned_wire:(a : t)
~expression:(b : t)];
if width a <> width b
then
raise_s
[%message
"attempt to assign expression to wire of different width"
~wire_width:(width a : int)
~expression_width:(width b : int)
~wire:(a : t)
~expression:(b : t)];
driver := b
| _ ->
raise_s
[%message
"attempt to assign non-wire" ~assignment_target:(a : t) ~expression:(b : t)]
;;
let[@cold] raise_wire_width_0 wire =
raise_s [%message "width of wire was specified as 0" (wire : t)]
;;
let assign = ( <== )
let wire w =
let wire = Wire { signal_id = make_id w; driver = ref Empty } in
if w = 0 then raise_wire_width_0 wire;
wire
;;
let wireof s =
let x = wire (width s) in
x <== s;
x
;;
let input name width = Unoptimized.( -- ) (wire width) name
let output name s =
let w = Unoptimized.( -- ) (wire (width s)) name in
w <== s;
w
;;
module Const_prop = struct
module Base = struct
include Unoptimized
let cv s = const_value s
let eqs s n =
let d = Bits.( ==: ) (cv s) (Bits.of_int ~width:(width s) n) in
Bits.to_int d = 1
;;
let cst b = of_constant (Bits.to_constant b)
let ( +: ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( +: ) (cv a) (cv b))
| true, false when eqs a 0 -> b
| false, true when eqs b 0 -> a
| _ -> a +: b
;;
let ( -: ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( -: ) (cv a) (cv b))
| false, true when eqs b 0 -> a
| _ -> a -: b
;;
let ( *: ) a b =
let w = width a + width b in
let opt d c =
if eqs c 0
then zero w
else if eqs c 1
then zero (width c) @: d
else (
let c = cv c in
if Bits.to_int @@ Bits.popcount c <> 1
then a *: b
else (
let p = Bits.to_int @@ (Bits.floor_log2 c).value in
if p = 0 then uresize d w else uresize (d @: zero p) w))
in
match is_const a, is_const b with
| true, true -> cst (Bits.( *: ) (cv a) (cv b))
| true, false -> opt b a
| false, true -> opt a b
| _ -> a *: b
;;
let ( *+ ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( *+ ) (cv a) (cv b))
| _ -> a *+ b
;;
let ( &: ) a b =
let opt d c =
if eqs c 0 then zero (width a) else if eqs c (-1) then d else a &: b
in
match is_const a, is_const b with
| true, true -> cst (Bits.( &: ) (cv a) (cv b))
| true, false -> opt b a
| false, true -> opt a b
| _ -> a &: b
;;
let ( |: ) a b =
let opt d c =
if eqs c 0 then d else if eqs c (-1) then ones (width a) else a |: b
in
match is_const a, is_const b with
| true, true -> cst (Bits.( |: ) (cv a) (cv b))
| true, false -> opt b a
| false, true -> opt a b
| _ -> a |: b
;;
let ( ^: ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( ^: ) (cv a) (cv b))
| _ -> a ^: b
;;
let ( ==: ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( ==: ) (cv a) (cv b))
| _ -> a ==: b
;;
let ( ~: ) a =
match is_const a with
| true -> cst (Bits.( ~: ) (cv a))
| _ -> ~:a
;;
let ( <: ) a b =
match is_const a, is_const b with
| true, true -> cst (Bits.( <: ) (cv a) (cv b))
| _ -> a <: b
;;
let concat_msb l =
let optimise_consts l =
List.group l ~break:(fun a b -> not (Bool.equal (is_const a) (is_const b)))
|> List.map ~f:(function
| [] -> []
| [ x ] -> [ x ]
| h :: _ as l ->
if is_const h
then [ List.map l ~f:const_value |> Bits.concat_msb |> cst ]
else l)
|> List.concat
in
concat_msb (optimise_consts l)
;;
let mux sel els =
let len = List.length els in
if is_const sel
then (
let x = Bits.to_int (cv sel) in
let x = min x (len - 1) in
List.nth_exn els x
)
else mux sel els
;;
let select d h l =
if is_const d
then cst (Bits.select (cv d) h l)
else if l = 0 && h = width d - 1
then d
else select d h l
;;
end
module Comb = struct
include Comb_make (Base)
let is_vdd = function
| Const { constant; _ } when Bits.equal constant Bits.vdd -> true
| _ -> false
;;
let is_gnd = function
| Const { constant; _ } when Bits.equal constant Bits.gnd -> true
| _ -> false
;;
end
end
include (Const_prop.Comb : module type of Const_prop.Comb with type t := t)
let assert_width signal w msg =
if width signal <> w
then
raise_s
[%message
msg ~info:"signal has unexpected width" ~expected_width:(w : int) (signal : t)]
;;
let assert_width_or_empty signal w msg =
if (not (is_empty signal)) && width signal <> w
then
raise_s
[%message
msg
~info:"signal should have expected width or be empty"
~expected_width:(w : int)
(signal : t)]
;;
let form_spec spec enable d =
assert_width spec.reg_clock 1 "clock is invalid";
assert_width_or_empty spec.reg_reset 1 "reset is invalid";
assert_width_or_empty spec.reg_reset_value (width d) "reset value is invalid";
assert_width_or_empty spec.reg_clear 1 "clear signal is invalid";
assert_width_or_empty spec.reg_clear_value (width d) "clear value is invalid";
assert_width_or_empty spec.reg_enable 1 "enable is invalid";
assert_width_or_empty enable 1 "enable is invalid";
{ spec with
reg_reset_value =
(if is_empty spec.reg_reset_value then zero (width d) else spec.reg_reset_value)
; reg_clear_value =
(if is_empty spec.reg_clear_value then zero (width d) else spec.reg_clear_value)
; reg_enable =
(let e0 = if is_empty spec.reg_enable then vdd else spec.reg_enable in
let e1 = if is_empty enable then vdd else enable in
if is_vdd e0 && is_vdd e1
then vdd
else if is_vdd e0
then e1
else if is_vdd e1
then e0
else e0 &: e1)
}
;;
let reg spec ?(enable = vdd) d =
let spec = form_spec spec enable d in
Reg { signal_id = make_id (width d); register = spec; d }
;;
let reg_fb ?enable spec ~width ~f =
let d = wire width in
let q = reg spec ?enable d in
d <== f q;
q
;;
let rec pipeline ?(attributes = []) spec ~n ?enable d =
let maybe_add_attributes s = List.fold attributes ~init:s ~f:add_attribute in
if n = 0
then d
else
maybe_add_attributes
(reg spec ?enable (pipeline ~attributes ~n:(n - 1) spec ?enable d))
;;
let multiport_memory
?name
?(attributes = [])
size
~(write_ports : _ Write_port.t array)
~read_addresses
=
if size <= 0
then
raise_s
[%message "[Signal.multiport_memory] size must be greater than 0" (size : int)];
let write_expected =
if Array.is_empty write_ports
then raise_s [%message "[Signal.multiport_memory] requires at least one write port"]
else (
let expected = write_ports.(0) in
if address_bits_for size <> width expected.write_address
then
raise_s
[%message
"[Signal.multiport_memory] size does not match what can be addressed by \
write port"
(size : int)
~address_width:(width expected.write_address : int)];
Array.iteri write_ports ~f:(fun port write_port ->
if width write_port.write_clock <> 1
then
raise_s
[%message
"[Signal.multiport_memory] width of clock must be 1"
(port : int)
~write_enable_width:(width write_port.write_enable : int)];
if width write_port.write_enable <> 1
then
raise_s
[%message
"[Signal.multiport_memory] width of write enable must be 1"
(port : int)
~write_enable_width:(width write_port.write_enable : int)];
if width write_port.write_address <> width expected.write_address
then
raise_s
[%message
"[Signal.multiport_memory] width of write address is inconsistent"
(port : int)
~write_address_width:(width write_port.write_address : int)
~expected:(width expected.write_address : int)];
if width write_port.write_data <> width expected.write_data
then
raise_s
[%message
"[Signal.multiport_memory] width of write data is inconsistent"
(port : int)
~write_data_width:(width write_port.write_data : int)
~expected:(width expected.write_data : int)]);
expected)
in
let read_expected =
if Array.is_empty read_addresses
then raise_s [%message "[Signal.multiport_memory] requires at least one read port"]
else (
let expected = read_addresses.(0) in
if address_bits_for size <> width expected
then
raise_s
[%message
"[Signal.multiport_memory] size does not match what can be addressed by read \
port"
(size : int)
~address_width:(width expected : int)];
Array.iteri read_addresses ~f:(fun port read_address ->
if width read_address <> width expected
then
raise_s
[%message
"[Signal.multiport_memory] width of read address is inconsistent"
(port : int)
~read_address_width:(width read_address : int)
~expected:(width expected : int)]);
expected)
in
if width write_expected.write_address <> width read_expected
then
raise_s
[%message
"[Signal.multiport_memory] width of read and write addresses differ"
~write_address_width:(width write_expected.write_address : int)
~read_address_width:(width read_expected : int)];
let data_width = width write_expected.write_data in
let memory =
add_attributes
(Multiport_mem { signal_id = make_id data_width; size; write_ports })
attributes
in
Option.iter name ~f:(fun name -> ignore (memory -- name : t));
Array.map read_addresses ~f:(fun read_address ->
Mem_read_port { signal_id = make_id data_width; memory; read_address })
;;
let memory size ~write_port ~read_address =
(multiport_memory size ~write_ports:[| write_port |] ~read_addresses:[| read_address |]).(
0)
;;
let ram_rbw ?name ?attributes ~write_port ~(read_port : _ Read_port.t) size =
let spec = { Reg_spec.reg_empty with reg_clock = read_port.read_clock } in
reg
spec
~enable:read_port.read_enable
(multiport_memory
?name
?attributes
size
~write_ports:[| write_port |]
~read_addresses:[| read_port.read_address |]).(0)
;;
let ram_wbr ?name ?attributes ~write_port ~(read_port : _ Read_port.t) size =
let spec = { Reg_spec.reg_empty with reg_clock = read_port.read_clock } in
(multiport_memory
size
?name
?attributes
~write_ports:[| write_port |]
~read_addresses:[| reg spec ~enable:read_port.read_enable read_port.read_address |]).(
0)
;;
let pp fmt t = Stdlib.Format.fprintf fmt "%s" ([%sexp (t : t)] |> Sexp.to_string_hum)
module _ = Pretty_printer.Register (struct
type nonrec t = t
let module_name = "Hardcaml.Signal"
let to_string = to_bstr
end)