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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
open! Core0
open Signal
include Fifo_intf.T
module Kinded_fifo = Fifo_intf.Kinded_fifo
let ram_wbr_safe
?ram_primitive
scope
capacity
~(write_port : _ Write_port.t)
~(read_port : _ Read_port.t)
~ram_attributes
=
let open Signal in
let ( -- ) = Scope.naming scope in
let has_distributed_ram_attribute =
List.find
ram_attributes
~f:(Rtl_attribute.equal Rtl_attribute.Vivado.Ram_style.distributed)
|> Option.is_some
in
match has_distributed_ram_attribute, ram_primitive with
| true, None ->
ram_wbr capacity ~attributes:ram_attributes ~write_port ~read_port -- "ram_wbr_data"
| true, Some _ ->
raise_s
[%message
"UNSUPPORTED: When supplying a RAM primitive the distributed ram attribute might \
be ignored. Consider supplying a distributed primitive instead."]
| false, _ ->
let ram_primitive = Option.value ram_primitive ~default:ram_rbw in
let collision =
reg
(Reg_spec.create ~clock:write_port.write_clock ())
~enable:read_port.read_enable
(write_port.write_enable
&: read_port.read_enable
&: (write_port.write_address ==: read_port.read_address))
-- "collision"
in
let data_before_collision =
reg
(Reg_spec.create ~clock:write_port.write_clock ())
~enable:read_port.read_enable
write_port.write_data
-- "data_before_collision"
in
mux2
collision
data_before_collision
(ram_primitive capacity ~attributes:ram_attributes ~write_port ~read_port
-- "ram_rbw_data")
;;
let capacity_and_used_bits showahead ram_capacity =
let actual_capacity = if showahead then ram_capacity + 1 else ram_capacity in
let used_bits = num_bits_to_represent actual_capacity in
actual_capacity, used_bits
;;
module Fifo_helper (Config : sig
val used_bits : int
val actual_capacity : int
end) =
struct
open Config
module I = struct
type 'a t =
{ clock : 'a
; clear : 'a
; wr : 'a
; rd : 'a
}
[@@deriving hardcaml]
end
module O = struct
type 'a t =
{ used : 'a [@bits used_bits]
; used_next : 'a [@bits used_bits]
; empty : 'a
; nearly_empty : 'a
; full : 'a
; nearly_full : 'a
}
[@@deriving hardcaml]
end
let create ?nearly_full ?(nearly_empty = 1) ~scope ({ clock; clear; wr; rd } : _ I.t)
: _ O.t
=
let ( -- ) = Scope.naming scope in
let reg_spec = Reg_spec.create ~clock ~clear () in
let reg ?initialize_to ~enable d =
reg
reg_spec
?clear_to:(Option.map initialize_to ~f:Signal.of_bits)
?initialize_to
~enable
d
in
let nearly_full =
match nearly_full with
| None -> actual_capacity - 1
| Some x -> x
in
let not_empty, full = wire 1, wire 1 in
let empty = ~:not_empty in
let enable = rd ^: wr in
let used = wire used_bits in
let used_plus_1 = wire used_bits in
let used_minus_1 = wire used_bits in
let used_next =
mux2 enable (mux2 rd used_minus_1 used_plus_1) used
in
used <-- reg ~enable (used_next -- "USED_NEXT") -- "USED";
used_plus_1
<-- reg ~enable ~initialize_to:(Bits.one (width used)) (used_next +:. 1)
-- "USED_PLUS_1";
used_minus_1
<-- reg ~enable ~initialize_to:(Bits.ones (width used)) (used_next -:. 1)
-- "USED_MINUS_1";
not_empty <-- reg ~enable (used_next <>:. 0) -- "not_empty";
full <-- reg ~enable (used_next ==:. actual_capacity) -- "full";
let nearly_empty =
reg ~enable ~initialize_to:Bits.vdd (used_next <=:. nearly_empty) -- "nearly_empty"
in
let nearly_full = reg ~enable (used_next >=:. nearly_full) -- "nearly_full" in
{ used; used_next; empty; nearly_empty; full; nearly_full }
;;
end
let create
?read_latency
?(showahead = false)
?nearly_empty
?nearly_full
?(overflow_check = true)
?(underflow_check = true)
?(ram_attributes = [ Rtl_attribute.Vivado.Ram_style.block ])
~scope
?ram_primitive
()
~capacity:ram_capacity
~clock
~clear
~wr
~d
~rd
=
let ( -- ) = Scope.naming scope in
Option.iter read_latency ~f:(fun read_latency ->
if showahead && read_latency <> 0
then
raise_s
[%message
"Cannot set showahead = true and read_latency <> 0 for Fifo."
(read_latency : int)
(showahead : bool)]);
let reg_spec = Reg_spec.create ~clock ~clear () in
let reg ?initialize_to ~enable d =
reg
reg_spec
?clear_to:(Option.map initialize_to ~f:Signal.of_bits)
?initialize_to
~enable
d
in
let full', empty' = wire 1, wire 1 in
let rd = if underflow_check then (rd &: ~:empty') -- "RD_INT" else rd in
let wr = if overflow_check then (wr &: ~:full') -- "WR_INT" else wr in
let abits = address_bits_for ram_capacity in
let actual_capacity, used_bits = capacity_and_used_bits showahead ram_capacity in
let module Fifo_helper =
Fifo_helper (struct
let used_bits = used_bits
let actual_capacity = actual_capacity
end)
in
let%tydi { used; used_next; empty; nearly_empty; full; nearly_full } =
Fifo_helper.create ?nearly_full ?nearly_empty ~scope { clock; clear; wr; rd }
in
full' <-- full;
empty' <-- empty;
let addr_count enable name =
let a = wire abits in
let an = mod_counter ~max:(ram_capacity - 1) a in
a
<-- add_attribute (reg ~enable an -- name) (Rtl_attribute.Vivado.extract_reset false);
a, an -- (name ^ "_NEXT")
in
let q =
if showahead
then (
let used_is_one = reg ~enable:(rd ^: wr) (used_next ==:. 1) -- "used_is_one" in
let used_gt_one = reg ~enable:(rd ^: wr) (used_next >:. 1) -- "used_gt_one" in
let memory =
let wr = wr &: (used_gt_one |: (used_is_one &: ~:rd)) in
let rd = rd &: used_gt_one in
let ra, ra_n = addr_count rd "READ_ADDRESS" in
let ra = mux2 rd ra_n ra -- "RA" in
let wa, _ = addr_count wr "WRITE_ADDRESS" in
ram_wbr_safe
?ram_primitive
scope
~ram_attributes
ram_capacity
~write_port:
{ write_clock = clock; write_enable = wr; write_address = wa; write_data = d }
~read_port:{ read_clock = clock; read_enable = vdd; read_address = ra }
-- "memory"
in
let bypass_cond = (empty &: wr |: (used_is_one &: wr &: rd)) -- "bypass_cond" in
mux2 bypass_cond d memory |> reg ~enable:(bypass_cond |: rd))
else (
let ra, _ = addr_count rd "READ_ADDRESS" in
let wa, _ = addr_count wr "WRITE_ADDRESS" in
let spec = Reg_spec.create ~clock ~clear () in
ram_rbw
~attributes:ram_attributes
ram_capacity
~write_port:
{ write_clock = clock; write_enable = wr; write_address = wa; write_data = d }
~read_port:{ read_clock = clock; read_enable = rd; read_address = ra }
|> pipeline spec ~n:(Option.value read_latency ~default:1 - 1))
in
{ q
; full
; empty
; nearly_full
; nearly_empty
; used
; rd_rst_busy = gnd
; wr_rst_busy = gnd
}
;;
let
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let spec = Reg_spec.create ~clock ~clear () in
let fifo_valid = wire 1 in
let middle_valid = wire 1 in
let fifo_rd_en = wire 1 in
let empty = ~:(fifo_valid |: middle_valid) in
let will_update_dout = ~:empty &: rd in
let will_update_middle = fifo_valid &: (middle_valid ==: will_update_dout) in
let fifo =
create
~showahead:false
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
?ram_primitive
~scope
()
~capacity
~clock
~clear
~wr
~d
~rd:fifo_rd_en
in
let middle_dout = reg spec ~enable:will_update_middle fifo.q in
fifo_rd_en <-- (~:(fifo.empty) &: ~:(middle_valid &: fifo_valid));
fifo_valid
<-- reg spec ~enable:(fifo_rd_en |: will_update_middle |: will_update_dout) fifo_rd_en;
middle_valid
<-- reg spec ~enable:(will_update_middle |: will_update_dout) will_update_middle;
{ fifo with
q = reg spec ~enable:will_update_dout (mux2 middle_valid middle_dout fifo.q)
; empty
}
;;
let showahead_fifo_of_classic_fifo
(create_fifo :
capacity:int
-> write_clock:Signal.t
-> read_clock:Signal.t
-> clear:Signal.t
-> wr:Signal.t
-> d:Signal.t
-> rd:Signal.t
-> (Signal.t, [ `Classic ]) Kinded_fifo.t)
=
Staged.stage (fun ~capacity ~write_clock ~read_clock ~clear ~wr ~d ~rd ->
let spec = Reg_spec.create ~clock:read_clock ~clear () in
let fifo_rd_en = wire 1 in
let (Classic fifo) =
create_fifo ~capacity ~write_clock ~read_clock ~clear ~wr ~d ~rd:fifo_rd_en
in
let dout_valid = reg spec ~enable:(fifo_rd_en |: rd) fifo_rd_en in
let empty = ~:dout_valid in
fifo_rd_en <-- (~:(fifo.empty) &: (~:dout_valid |: rd));
Kinded_fifo.Showahead { fifo with empty })
;;
let create_showahead_from_classic
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
=
let create_fifo ~capacity ~write_clock ~read_clock ~clear ~wr ~d ~rd =
assert (Signal.equal write_clock read_clock);
create
~showahead:false
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
?ram_primitive
~scope
()
~capacity
~clock:write_clock
~clear
~wr
~d
~rd
|> Kinded_fifo.Classic
in
let create_showahead_fifo =
Staged.unstage (showahead_fifo_of_classic_fifo create_fifo)
in
fun ~capacity ~clock ~clear ~wr ~d ~rd ->
let write_clock = clock in
let read_clock = clock in
match create_showahead_fifo ~capacity ~write_clock ~read_clock ~clear ~wr ~d ~rd with
| Kinded_fifo.Showahead fifo -> fifo
;;
let create_showahead_with_read_latency
~read_latency
?nearly_empty
?nearly_full
?(overflow_check = true)
?(underflow_check = true)
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let spec = Reg_spec.create ~clock ~clear () in
let fifo_rd_en = wire 1 in
let full', empty' = wire 1, wire 1 in
let rd = if underflow_check then rd &: ~:empty' else rd in
let wr = if overflow_check then wr &: ~:full' else wr in
let fifo =
create
~showahead:false
~read_latency
?nearly_empty
?nearly_full
?ram_attributes
?ram_primitive
~scope
()
~overflow_check
~underflow_check
~capacity
~clock
~clear
~wr
~d
~rd:fifo_rd_en
in
let read_pipeline = pipeline spec fifo_rd_en ~n:read_latency in
let read_in_progress =
reg_fb spec ~width:1 ~f:(fun q -> mux2 read_pipeline gnd (fifo_rd_en |: q))
in
let dout_valid =
reg_fb spec ~width:1 ~f:(fun q -> mux2 (rd &: q) gnd (q |: read_pipeline))
in
let dout = reg ~enable:(~:dout_valid |: (dout_valid &: rd)) spec fifo.q in
fifo_rd_en
<-- (~:(fifo.empty) &: ~:read_in_progress &: (~:dout_valid |: (dout_valid &: rd)));
let module Fifo_helper =
Fifo_helper (struct
let used_bits = num_bits_to_represent capacity
let actual_capacity = capacity
end)
in
let%tydi { used; used_next = _; empty = _; nearly_empty; full; nearly_full = _ } =
Fifo_helper.create ?nearly_full ?nearly_empty ~scope { clock; clear; wr; rd }
in
empty' <-- ~:dout_valid;
full' <-- full;
{ fifo with q = dout; empty = empty'; used; nearly_empty; full }
;;
let
?(nearly_empty = 1)
?nearly_full
~scope
fifo
~overflow_check
~underflow_check
~capacity
~clock
~clear
~wr
~rd
=
let spec = Reg_spec.create ~clock ~clear () in
let fifo_rd_en = wire 1 in
let full', empty' = wire 1, wire 1 in
let rd = if underflow_check then rd &: ~:empty' else rd in
let wr = if overflow_check then wr &: ~:full' else wr in
let fifo_valid = wire 1 in
let middle_valid = wire 1 in
let dout_valid = wire 1 in
let will_update_dout = middle_valid |: fifo_valid &: (rd |: ~:dout_valid) in
let will_update_middle = fifo_valid &: (middle_valid ==: will_update_dout) in
let middle_dout = reg spec ~enable:will_update_middle fifo.q in
let dout = reg spec ~enable:will_update_dout (mux2 middle_valid middle_dout fifo.q) in
fifo_rd_en <-- (~:(fifo.empty) &: ~:(middle_valid &: dout_valid &: fifo_valid));
fifo_valid
<-- reg spec ~enable:(fifo_rd_en |: will_update_middle |: will_update_dout) fifo_rd_en;
middle_valid
<-- reg spec ~enable:(will_update_middle |: will_update_dout) will_update_middle;
dout_valid <-- reg spec ~enable:(will_update_dout |: rd) will_update_dout;
let module Fifo_helper =
Fifo_helper (struct
let used_bits = num_bits_to_represent capacity
let actual_capacity = capacity
end)
in
let%tydi { used; used_next = _; empty = _; nearly_empty; full; nearly_full } =
Fifo_helper.create ~scope ?nearly_full ~nearly_empty { clock; clear; wr; rd }
in
empty' <-- ~:dout_valid;
full' <-- full;
{ fifo = { fifo with q = dout; empty = empty'; nearly_empty; used; full; nearly_full }
; fifo_rd_en
}
;;
let
?(nearly_empty = 1)
?nearly_full
?(overflow_check = true)
?(underflow_check = true)
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let fifo_rd_en' = wire 1 in
let fifo =
create
~showahead:false
?nearly_full
?ram_attributes
~scope
?ram_primitive
()
~overflow_check
~underflow_check
~nearly_empty
~capacity
~clock
~clear
~wr
~d
~rd:fifo_rd_en'
in
let { fifo; fifo_rd_en } =
create_showahead_with_extra_reg_wrapper
~nearly_empty
?nearly_full
~scope
fifo
~overflow_check
~underflow_check
~capacity
~clock
~clear
~wr
~rd
in
fifo_rd_en' <-- fifo_rd_en;
fifo
;;
module Clocked = struct
let create
?read_latency
?showahead
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let dom = Clocked_signal.get_domain clock in
app
(create
?read_latency
?showahead
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity)
~clock
~clear
~wr
~d
~rd
~f:(Clocked_signal.unwrap_signal ~dom)
|> map ~f:(Clocked_signal.to_clocked ~dom)
;;
let
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let dom = Clocked_signal.get_domain clock in
app
(create_classic_with_extra_reg
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity)
~clock
~clear
~wr
~d
~rd
~f:(Clocked_signal.unwrap_signal ~dom)
|> map ~f:(Clocked_signal.to_clocked ~dom)
;;
let create_showahead_from_classic
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let dom = Clocked_signal.get_domain clock in
app
(create_showahead_from_classic
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity)
~clock
~clear
~wr
~d
~rd
~f:(Clocked_signal.unwrap_signal ~dom)
|> map ~f:(Clocked_signal.to_clocked ~dom)
;;
let create_showahead_with_read_latency
~read_latency
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let dom = Clocked_signal.get_domain clock in
app
(create_showahead_with_read_latency
~read_latency
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity)
~clock
~clear
~wr
~d
~rd
~f:(Clocked_signal.unwrap_signal ~dom)
|> map ~f:(Clocked_signal.to_clocked ~dom)
;;
let
?nearly_empty
?nearly_full
~scope
fifo
~overflow_check
~underflow_check
~capacity
~clock
~clear
~wr
~rd
=
let dom = Clocked_signal.get_domain clock in
let unwrap = Clocked_signal.unwrap_signal ~dom in
let wrap = Clocked_signal.to_clocked ~dom in
let { fifo; fifo_rd_en } =
(create_showahead_with_extra_reg_wrapper
?nearly_empty
?nearly_full
~scope
(map fifo ~f:unwrap)
~overflow_check
~underflow_check
~capacity)
~clock:(unwrap clock)
~clear:(unwrap clear)
~wr:(unwrap wr)
~rd:(unwrap rd)
in
{ fifo = map fifo ~f:wrap; fifo_rd_en = wrap fifo_rd_en }
;;
let
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let dom = Clocked_signal.get_domain clock in
app
(create_showahead_with_extra_reg
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity)
~clock
~clear
~wr
~d
~rd
~f:(Clocked_signal.unwrap_signal ~dom)
|> map ~f:(Clocked_signal.to_clocked ~dom)
;;
end
module type Config = Fifo_intf.Config
module With_interface (Config : Config) = struct
let _actual_capacity, used_bits =
capacity_and_used_bits Config.showahead Config.capacity
;;
module I = struct
type 'a t =
{ clock : 'a
; clear : 'a
; wr : 'a
; d : 'a [@bits Config.data_width]
; rd : 'a
}
[@@deriving hardcaml]
end
module O = struct
type nonrec 'a t = 'a t
include Interface.Make (struct
include Fifo_intf.T
let port_names_and_widths =
{ port_names_and_widths with
q = "q", Config.data_width
; used = "used", used_bits
}
;;
end)
end
let create_fn
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
~f
(i : _ I.t)
=
f
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
()
~capacity:Config.capacity
~clock:i.clock
~clear:i.clear
~wr:i.wr
~d:i.d
~rd:i.rd
;;
let classic ?( = false) =
match extra_reg, Config.showahead with
| false, false -> create_fn ~f:(create ~showahead:false ?read_latency:None)
| true, false -> create_fn ~f:create_classic_with_extra_reg
| false, true -> create_fn ~f:create_showahead_from_classic
| true, true -> create_fn ~f:create_showahead_with_extra_reg
;;
let create = create_fn ~f:(create ~showahead:Config.showahead ?read_latency:None)
end
type 'a typed_fifo_read_result =
{ q : (Signal.t, 'a) With_valid.t2
; empty : Signal.t
; full : Signal.t
; nearly_empty : Signal.t
; nearly_full : Signal.t
; overflow : Signal.t
; read_when_empty : Signal.t
; used : Signal.t
}
let typed_fifo
(type a)
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
~(clocking : Signal.t Clocking.t)
~capacity
~(input : (Signal.t, a) With_valid.t2)
~(read : Signal.t)
(module C : Interface.S_Of_signal with type Of_signal.t = a)
=
let fifo =
create
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
~showahead:true
~capacity
~clock:clocking.clock
~clear:clocking.clear
~wr:input.valid
~d:(C.Of_signal.pack input.value)
~rd:read
()
in
let unpacked_q = C.Of_signal.unpack fifo.q in
{ q = { With_valid.valid = ~:(fifo.empty); value = unpacked_q }
; empty = fifo.empty
; full = fifo.full
; nearly_empty = fifo.nearly_empty
; nearly_full = fifo.nearly_full
; overflow = fifo.full &: input.valid
; read_when_empty = fifo.empty &: read
; used = fifo.used
}
;;
let cut_through_typed_fifo
(type a)
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
~(clocking : Signal.t Clocking.t)
~capacity
~(input : (Signal.t, a) With_valid.t2)
~(read : Signal.t)
(module C : Interface.S_Of_signal with type Of_signal.t = a)
=
let cutting_through = wire 1 in
let cutting_through_and_reading = wire 1 in
let fifo_input = { input with valid = input.valid &: ~:cutting_through_and_reading } in
let fifo_read = read &: ~:cutting_through in
let underlying_fifo =
typed_fifo
?nearly_empty
?nearly_full
?overflow_check
?underflow_check
?ram_attributes
~scope
?ram_primitive
~clocking
~capacity
~input:fifo_input
~read:fifo_read
(module C)
in
cutting_through <-- (underlying_fifo.empty &: input.valid);
cutting_through_and_reading <-- (cutting_through &: read);
let q =
{ With_valid.valid = input.valid |: underlying_fifo.q.valid
; value = C.Of_signal.mux2 underlying_fifo.empty input.value underlying_fifo.q.value
}
in
let empty = ~:(q.valid) in
let nearly_empty =
match nearly_empty with
| Some 0 -> empty
| _ -> underlying_fifo.nearly_empty
in
{ q
; empty
; full = underlying_fifo.full
; nearly_empty
; nearly_full = underlying_fifo.nearly_full
; overflow = underlying_fifo.overflow
; read_when_empty = empty &: read
; used = underlying_fifo.used
}
;;