Skip to content

Commit 47cceea

Browse files
committedMar 9, 2015
liteeth/mac: use Counter in sram and move some logic outside of fsms
1 parent b10836a commit 47cceea

File tree

1 file changed

+33
-54
lines changed
  • misoclib/com/liteeth/mac/frontend

1 file changed

+33
-54
lines changed
 

‎misoclib/com/liteeth/mac/frontend/sram.py

+33-54
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,25 @@ def __init__(self, dw, depth, nslots=2):
2525
sink.ack.reset = 1
2626

2727
# length computation
28-
cnt = Signal(lengthbits)
29-
clr_cnt = Signal()
30-
inc_cnt = Signal()
31-
inc_val = Signal(3)
28+
increment = Signal(3)
3229
self.comb += \
3330
If(sink.last_be[3],
34-
inc_val.eq(1)
31+
increment.eq(1)
3532
).Elif(sink.last_be[2],
36-
inc_val.eq(2)
33+
increment.eq(2)
3734
).Elif(sink.last_be[1],
38-
inc_val.eq(3)
35+
increment.eq(3)
3936
).Else(
40-
inc_val.eq(4)
41-
)
42-
self.sync += \
43-
If(clr_cnt,
44-
cnt.eq(0)
45-
).Elif(inc_cnt,
46-
cnt.eq(cnt+inc_val)
37+
increment.eq(4)
4738
)
39+
counter = Counter(lengthbits, increment=increment)
40+
self.submodules += counter
4841

4942
# slot computation
50-
slot = Signal(slotbits)
51-
inc_slot = Signal()
52-
self.sync += \
53-
If(inc_slot,
54-
If(slot == nslots-1,
55-
slot.eq(0),
56-
).Else(
57-
slot.eq(slot+1)
58-
)
59-
)
43+
slot = Counter(slotbits)
44+
self.submodules += slot
45+
6046
ongoing = Signal()
61-
discard = Signal()
6247

6348
# status fifo
6449
fifo = SyncFIFO([("slot", slotbits), ("length", lengthbits)], nslots)
@@ -72,13 +57,13 @@ def __init__(self, dw, depth, nslots=2):
7257
If(sink.stb & sink.sop,
7358
If(fifo.sink.ack,
7459
ongoing.eq(1),
75-
inc_cnt.eq(1),
60+
counter.ce.eq(1),
7661
NextState("WRITE")
7762
)
7863
)
7964
)
8065
fsm.act("WRITE",
81-
inc_cnt.eq(sink.stb),
66+
counter.ce.eq(sink.stb),
8267
ongoing.eq(1),
8368
If(sink.stb & sink.eop,
8469
If((sink.error & sink.last_be) != 0,
@@ -89,18 +74,19 @@ def __init__(self, dw, depth, nslots=2):
8974
)
9075
)
9176
fsm.act("DISCARD",
92-
clr_cnt.eq(1),
77+
counter.reset.eq(1),
9378
NextState("IDLE")
9479
)
80+
self.comb += [
81+
fifo.sink.slot.eq(slot.value),
82+
fifo.sink.length.eq(counter.value)
83+
]
9584
fsm.act("TERMINATE",
96-
clr_cnt.eq(1),
97-
inc_slot.eq(1),
85+
counter.reset.eq(1),
86+
slot.ce.eq(1),
9887
fifo.sink.stb.eq(1),
99-
fifo.sink.slot.eq(slot),
100-
fifo.sink.length.eq(cnt),
10188
NextState("IDLE")
10289
)
103-
10490
self.comb += [
10591
fifo.source.ack.eq(self.ev.available.clear),
10692
self.ev.available.trigger.eq(fifo.source.stb),
@@ -120,13 +106,13 @@ def __init__(self, dw, depth, nslots=2):
120106
cases = {}
121107
for n, port in enumerate(ports):
122108
cases[n] = [
123-
ports[n].adr.eq(cnt[2:]),
109+
ports[n].adr.eq(counter.value[2:]),
124110
ports[n].dat_w.eq(sink.data),
125111
If(sink.stb & ongoing,
126112
ports[n].we.eq(0xf)
127113
)
128114
]
129-
self.comb += Case(slot, cases)
115+
self.comb += Case(slot.value, cases)
130116

131117

132118
class LiteEthMACSRAMReader(Module, AutoCSR):
@@ -159,16 +145,7 @@ def __init__(self, dw, depth, nslots=2):
159145
]
160146

161147
# length computation
162-
cnt = Signal(lengthbits)
163-
clr_cnt = Signal()
164-
inc_cnt = Signal()
165-
166-
self.sync += \
167-
If(clr_cnt,
168-
cnt.eq(0)
169-
).Elif(inc_cnt,
170-
cnt.eq(cnt+4)
171-
)
148+
self.submodules.counter = counter = Counter(lengthbits, increment=4)
172149

173150
# fsm
174151
first = Signal()
@@ -179,7 +156,7 @@ def __init__(self, dw, depth, nslots=2):
179156
self.submodules += fsm
180157

181158
fsm.act("IDLE",
182-
clr_cnt.eq(1),
159+
counter.reset.eq(1),
183160
If(fifo.source.stb,
184161
NextState("CHECK")
185162
)
@@ -192,10 +169,7 @@ def __init__(self, dw, depth, nslots=2):
192169
)
193170
)
194171
length_lsb = fifo.source.length[0:2]
195-
fsm.act("SEND",
196-
source.stb.eq(1),
197-
source.sop.eq(first),
198-
source.eop.eq(last),
172+
self.comb += [
199173
If(last,
200174
If(length_lsb == 3,
201175
source.last_be.eq(0b0010)
@@ -206,9 +180,14 @@ def __init__(self, dw, depth, nslots=2):
206180
).Else(
207181
source.last_be.eq(0b0001)
208182
)
209-
),
183+
)
184+
]
185+
fsm.act("SEND",
186+
source.stb.eq(1),
187+
source.sop.eq(first),
188+
source.eop.eq(last),
210189
If(source.ack,
211-
inc_cnt.eq(~last),
190+
counter.ce.eq(~last),
212191
NextState("CHECK")
213192
)
214193
)
@@ -226,7 +205,7 @@ def __init__(self, dw, depth, nslots=2):
226205
first.eq(0)
227206
)
228207
]
229-
self.comb += last.eq(cnt + 4 >= fifo.source.length)
208+
self.comb += last.eq((counter.value + 4) >= fifo.source.length)
230209
self.sync += last_d.eq(last)
231210

232211
# memory
@@ -242,7 +221,7 @@ def __init__(self, dw, depth, nslots=2):
242221

243222
cases = {}
244223
for n, port in enumerate(ports):
245-
self.comb += ports[n].adr.eq(cnt[2:])
224+
self.comb += ports[n].adr.eq(counter.value[2:])
246225
cases[n] = [source.data.eq(port.dat_r)]
247226
self.comb += Case(rd_slot, cases)
248227

0 commit comments

Comments
 (0)
Please sign in to comment.