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
open Base
open Signal
include Fifo_intf.T
module Kinded_fifo = Fifo_intf.Kinded_fifo
let ram_wbr_safe
capacity
~(write_port : _ Write_port.t)
~(read_port : _ Read_port.t)
~ram_attributes
=
let open Signal 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))
in
mux2
collision
(reg
(Reg_spec.create ~clock:write_port.write_clock ())
~enable:read_port.read_enable
write_port.write_data)
(ram_rbw capacity ~attributes:ram_attributes ~write_port ~read_port)
;;
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
;;
let create
?read_latency
?(showahead = false)
?(nearly_empty = 1)
?nearly_full
?(overflow_check = true)
?(reset = Signal.empty)
?(underflow_check = true)
?(ram_attributes = [ Rtl_attribute.Vivado.Ram_style.block ])
?scope
()
~capacity:ram_capacity
~clock
~clear
~wr
~d
~rd
=
let ( -- ) =
match scope with
| Some scope -> Scope.naming scope
| None -> ( -- )
in
if Signal.is_empty clear && Signal.is_empty reset
then
raise_s
[%message "[Fifo.create] requires either a synchronous clear or asynchronous reset"];
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 ~reset () in
let reg ?clear_to ~enable d = reg (Reg_spec.override reg_spec ?clear_to) ~enable d in
let abits = address_bits_for ram_capacity in
let actual_capacity, used_bits = capacity_and_used_bits showahead ram_capacity 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 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 enable = rd ^: wr in
let used = wire used_bits in
let used_next =
mux2 enable (mux2 rd (used -:. 1) (used +:. 1)) used
in
used <== reg ~enable (used_next -- "USED_NEXT");
not_empty <== reg ~enable (used_next <>:. 0);
full <== reg ~enable (used_next ==:. actual_capacity);
let nearly_empty = reg ~enable ~clear_to:vdd (used_next <=:. nearly_empty) in
let nearly_full = reg ~enable (used_next >=:. nearly_full) in
let addr_count enable name =
let a = wire abits in
let an = mod_counter ~max:(ram_capacity - 1) a in
a <== reg ~enable an;
a -- name, an -- (name ^ "_NEXT")
in
let q =
if showahead
then (
let used_is_one = reg ~enable:(rd ^: wr) (used_next ==:. 1) in
let used_gt_one = reg ~enable:(rd ^: wr) (used_next >:. 1) 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_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 }
in
let bypass_cond = empty &: wr |: (used_is_one &: wr &: rd) 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
?reset
?underflow_check
?ram_attributes
?scope
()
~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
?reset
?underflow_check
?ram_attributes
?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
?reset
?underflow_check
?ram_attributes
?scope
()
=
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
?reset
?underflow_check
?ram_attributes
?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
?reset
?underflow_check
?ram_attributes
?scope
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let spec = Reg_spec.create ~clock ~clear () in
let fifo_rd_en = wire 1 in
let fifo =
create
~showahead:false
~read_latency
?nearly_empty
?nearly_full
?overflow_check
?reset
?underflow_check
?ram_attributes
?scope
()
~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)));
{ fifo with
q = dout
; empty = ~:dout_valid
; used =
reg_fb
spec
~width:(num_bits_to_represent (capacity + 1))
~f:(fun q ->
let read = rd &: dout_valid
and write = wr &: ~:(fifo.full) in
mux2 (write &: ~:read) (q +:. 1) (mux2 (read &: ~:write) (q -:. 1) q))
}
;;
let
?nearly_empty
?nearly_full
?overflow_check
?reset
?underflow_check
?ram_attributes
?scope
()
~capacity
~clock
~clear
~wr
~d
~rd
=
let spec = Reg_spec.create ~clock ~clear () in
let fifo_rd_en = wire 1 in
let fifo =
create
~showahead:false
?nearly_empty
?nearly_full
?overflow_check
?reset
?underflow_check
?ram_attributes
?scope
()
~capacity
~clock
~clear
~wr
~d
~rd:fifo_rd_en
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 empty = ~:dout_valid 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;
{ fifo with q = dout; empty }
;;
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
?reset
?underflow_check
?ram_attributes
?scope
~f
(i : _ I.t)
=
f
?nearly_empty
?nearly_full
?overflow_check
?reset
?underflow_check
?ram_attributes
?scope
()
~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