litex/migen/flow/plumbing.py

103 lines
2.9 KiB
Python
Raw Normal View History

from migen.fhdl.std import *
2012-01-06 11:24:05 -05:00
from migen.flow.actor import *
2013-02-22 17:19:37 -05:00
from migen.genlib.record import *
from migen.genlib.misc import optree
2012-01-06 11:24:05 -05:00
2012-06-07 08:44:43 -04:00
class Buffer(PipelinedActor):
2012-01-09 09:14:42 -05:00
def __init__(self, layout):
2013-04-10 13:12:42 -04:00
self.d = Sink(layout)
self.q = Source(layout)
PipelinedActor.__init__(self, 1)
self.sync += \
If(self.pipe_ce,
self.q.payload.eq(self.d.payload),
self.q.param.eq(self.d.param)
)
2012-01-06 11:24:05 -05:00
2013-04-10 13:12:42 -04:00
class Combinator(Module):
def __init__(self, layout, subrecords):
2013-04-10 13:12:42 -04:00
self.source = Source(layout)
sinks = []
for n, r in enumerate(subrecords):
s = Sink(layout_partial(layout, *r))
setattr(self, "sink"+str(n), s)
sinks.append(s)
self.busy = Signal()
2012-01-06 11:24:05 -05:00
2013-04-01 16:15:23 -04:00
###
2014-10-17 05:08:37 -04:00
2013-04-10 13:12:42 -04:00
self.comb += [
self.busy.eq(0),
self.source.stb.eq(optree("&", [sink.stb for sink in sinks]))
]
self.comb += [sink.ack.eq(self.source.ack & self.source.stb) for sink in sinks]
self.comb += [self.source.payload.eq(sink.payload) for sink in sinks]
self.comb += [self.source.param.eq(sink.param) for sink in sinks]
2012-01-06 11:24:05 -05:00
2013-04-10 13:12:42 -04:00
class Splitter(Module):
def __init__(self, layout, subrecords):
2013-04-10 13:12:42 -04:00
self.sink = Sink(layout)
sources = []
for n, r in enumerate(subrecords):
s = Source(layout_partial(layout, *r))
setattr(self, "source"+str(n), s)
sources.append(s)
self.busy = Signal()
2014-10-17 05:08:37 -04:00
2013-04-01 16:15:23 -04:00
###
2013-04-10 13:12:42 -04:00
self.comb += [source.payload.eq(self.sink.payload) for source in sources]
self.comb += [source.param.eq(self.sink.param) for source in sources]
already_acked = Signal(len(sources))
2013-04-10 13:12:42 -04:00
self.sync += If(self.sink.stb,
2012-06-16 15:23:42 -04:00
already_acked.eq(already_acked | Cat(*[s.ack for s in sources])),
2013-04-10 13:12:42 -04:00
If(self.sink.ack, already_acked.eq(0))
2012-06-16 15:23:42 -04:00
)
2013-04-10 13:12:42 -04:00
self.comb += self.sink.ack.eq(optree("&",
2012-06-16 15:23:42 -04:00
[s.ack | already_acked[n] for n, s in enumerate(sources)]))
for n, s in enumerate(sources):
2013-04-10 13:12:42 -04:00
self.comb += s.stb.eq(self.sink.stb & ~already_acked[n])
class Multiplexer(Module):
def __init__(self, layout, n):
self.source = Source(layout)
sinks = []
for i in range(n):
sink = Sink(layout)
setattr(self, "sink"+str(i), sink)
sinks.append(sink)
self.busy = Signal()
self.sel = Signal(max=n)
2014-10-17 05:08:37 -04:00
###
2014-07-19 01:19:07 -04:00
cases = {}
for i, sink in enumerate(sinks):
cases[i] = Record.connect(sink, self.source)
self.comb += Case(self.sel, cases)
class Demultiplexer(Module):
def __init__(self, layout, n):
self.sink = Sink(layout)
sources = []
for i in range(n):
source = Source(layout)
setattr(self, "source"+str(i), source)
sources.append(source)
self.busy = Signal()
self.sel = Signal(max=n)
2014-10-17 05:08:37 -04:00
###
cases = {}
for i, source in enumerate(sources):
cases[i] = Record.connect(self.sink, source)
self.comb += Case(self.sel, cases)
# Actors whose layout should be inferred from what their single sink is connected to.
2012-06-16 15:23:42 -04:00
layout_sink = {Buffer, Splitter}
# Actors whose layout should be inferred from what their single source is connected to.
layout_source = {Buffer, Combinator}
# All actors.
actors = layout_sink | layout_source