-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordfifo.py
162 lines (129 loc) · 5.57 KB
/
recordfifo.py
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
from migen import *
from migen.genlib.record import *
from migen.genlib.fifo import _FIFOInterface, _inc, SyncFIFO, SyncFIFOBuffered
from .mem import FullyInitMemory
class InitFIFO(Module, _FIFOInterface):
"""Synchronous FIFO (first in, first out)
Read and write interfaces are accessed from the same clock domain.
If different clock domains are needed, use :class:`AsyncFIFO`.
{interface}
level : out
Number of unread entries.
replace : in
Replaces the last entry written into the FIFO with `din`. Does nothing
if that entry has already been read (i.e. the FIFO is empty).
Assert in conjunction with `we`.
"""
__doc__ = __doc__.format(interface=_FIFOInterface.__doc__)
def __init__(self, width, depth, fwft=True, init=None):
_FIFOInterface.__init__(self, width, depth)
if init:
startlevel = len(init)
else:
startlevel = 0
self.level = Signal(max=depth+1, reset=startlevel)
self.replace = Signal()
self.almost_full = Signal()
###
produce = Signal(max=depth, reset=startlevel)
consume = Signal(max=depth)
storage = FullyInitMemory(self.width, depth, init=init)
self.specials += storage
wrport = storage.get_port(write_capable=True, mode=READ_FIRST)
self.specials += wrport
self.comb += [
If(self.replace,
wrport.adr.eq(produce-1)
).Else(
wrport.adr.eq(produce)
),
wrport.dat_w.eq(self.din),
wrport.we.eq(self.we & (self.writable | self.replace))
]
self.sync += If(self.we & self.writable & ~self.replace,
_inc(produce, depth))
do_read = Signal()
self.comb += do_read.eq(self.readable & self.re)
rdport = storage.get_port(async_read=fwft, has_re=not fwft, mode=READ_FIRST)
self.specials += rdport
self.comb += [
rdport.adr.eq(consume),
self.dout.eq(rdport.dat_r)
]
if not fwft:
self.comb += rdport.re.eq(do_read)
self.sync += If(do_read, _inc(consume, depth))
self.sync += \
If(self.we & self.writable & ~self.replace,
If(~do_read, self.level.eq(self.level + 1))
).Elif(do_read,
self.level.eq(self.level - 1)
)
self.comb += [
self.writable.eq(self.level != depth),
self.readable.eq(self.level != 0),
self.almost_full.eq(self.level >= depth - 1)
]
class RecordFIFO(Module):
def __init__(self, layout, depth, init=None, name=None):
self.din = Record(layout, name= (name + "_din") if name else None)
self.dout = Record(layout, name= (name + "_dout") if name else None)
self.width = len(self.din.raw_bits())
self.submodules.fifo = InitFIFO(self.width, depth, init=init)
self.we = self.fifo.we
self.writable = self.fifo.writable
self.re = self.fifo.re
self.readable = self.fifo.readable
self.almost_full = self.fifo.almost_full
self.comb += [
self.fifo.din.eq(self.din.raw_bits()),
self.dout.raw_bits().eq(self.fifo.dout)
]
class RecordFIFOBuffered(Module):
def __init__(self, layout, depth, name=None):
self.we = Signal()
self.writable = Signal() # not full
self.re = Signal()
self.readable = Signal() # not empty
self.din = Record(layout, name= (name + "_din") if name else None)
self.dout = Record(layout, name= (name + "_dout") if name else None)
self.width = len(self.din.raw_bits())
self.submodules.fifo = SyncFIFOBuffered(self.width, depth)
self.comb += [
self.fifo.din.eq(self.din.raw_bits()),
self.writable.eq(self.fifo.writable),
self.fifo.we.eq(self.we),
self.dout.raw_bits().eq(self.fifo.dout),
self.readable.eq(self.fifo.readable),
self.fifo.re.eq(self.re)
]
class InterfaceFIFO(Module):
def __init__(self, layout, depth, name=None):
self.din = Record(layout, name= (name + "_din") if name else None)
self.dout = Record(layout, name= (name + "_dout") if name else None)
datalayout = [field for field in layout if (field[0] != "valid") and (field[0] != "ack")]
self.width = layout_len(datalayout)
self.submodules.fifo = RecordFIFO(datalayout, depth)
self.comb += [
self.din.connect(self.fifo.din, omit={"valid", "ack"}),
self.din.ack.eq(self.fifo.writable),
self.fifo.we.eq(self.din.valid),
self.fifo.dout.connect(self.dout, omit={"valid", "ack"}),
self.dout.valid.eq(self.fifo.readable),
self.fifo.re.eq(self.dout.ack)
]
class InterfaceFIFOBuffered(Module):
def __init__(self, layout, depth, name=None):
self.din = Record(layout, name= (name + "_din") if name else None)
self.dout = Record(layout, name= (name + "_dout") if name else None)
datalayout = [field for field in layout if (field != "valid") and (field != "ack")]
self.width = layout_len(datalayout)
self.submodules.fifo = RecordFIFOBuffered(datalayout, depth)
self.comb += [
self.din.connect(self.fifo.din, omit={"valid", "ack"}),
self.din.ack.eq(self.fifo.writable),
self.fifo.we.eq(self.din.valid),
self.fifo.dout.connect(self.dout),
self.dout.valid.eq(self.fifo.readable),
self.fifo.re.eq(self.dout.ack)
]