2012-10-04 12:22:22 -04:00
|
|
|
# Simple Processor Interface
|
|
|
|
|
2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2012-10-04 12:22:22 -04:00
|
|
|
from migen.bank.description import *
|
|
|
|
from migen.flow.actor import *
|
2013-04-30 12:55:01 -04:00
|
|
|
from migen.flow.network import *
|
|
|
|
from migen.flow import plumbing
|
|
|
|
from migen.actorlib import misc
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2012-10-09 15:11:15 -04:00
|
|
|
# layout is a list of tuples, either:
|
2012-11-29 17:36:55 -05:00
|
|
|
# - (name, nbits, [reset value], [alignment bits])
|
2012-10-09 15:11:15 -04:00
|
|
|
# - (name, sublayout)
|
|
|
|
|
|
|
|
def _convert_layout(layout):
|
|
|
|
r = []
|
|
|
|
for element in layout:
|
|
|
|
if isinstance(element[1], list):
|
|
|
|
r.append((element[0], _convert_layout(element[1])))
|
|
|
|
else:
|
|
|
|
r.append((element[0], element[1]))
|
|
|
|
return r
|
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
def _create_csrs_assign(layout, target, atomic, prefix=""):
|
|
|
|
csrs = []
|
2012-10-09 15:11:15 -04:00
|
|
|
assigns = []
|
|
|
|
for element in layout:
|
|
|
|
if isinstance(element[1], list):
|
2013-03-30 12:28:41 -04:00
|
|
|
r_csrs, r_assigns = _create_csrs_assign(element[1],
|
2012-10-09 15:11:15 -04:00
|
|
|
atomic,
|
|
|
|
getattr(target, element[0]),
|
|
|
|
element[0] + "_")
|
2013-03-30 12:28:41 -04:00
|
|
|
csrs += r_csrs
|
2012-10-09 15:11:15 -04:00
|
|
|
assigns += r_assigns
|
|
|
|
else:
|
|
|
|
name = element[0]
|
2012-11-29 17:36:55 -05:00
|
|
|
nbits = element[1]
|
2012-10-09 15:11:15 -04:00
|
|
|
if len(element) > 2:
|
|
|
|
reset = element[2]
|
|
|
|
else:
|
|
|
|
reset = 0
|
|
|
|
if len(element) > 3:
|
|
|
|
alignment = element[3]
|
|
|
|
else:
|
|
|
|
alignment = 0
|
2013-04-30 12:54:47 -04:00
|
|
|
reg = CSRStorage(nbits + alignment, reset=reset, atomic_write=atomic,
|
|
|
|
alignment_bits=alignment, name=prefix + name)
|
2013-03-30 12:28:41 -04:00
|
|
|
csrs.append(reg)
|
2013-04-30 12:54:47 -04:00
|
|
|
assigns.append(getattr(target, name).eq(reg.storage))
|
2013-03-30 12:28:41 -04:00
|
|
|
return csrs, assigns
|
2012-10-09 15:11:15 -04:00
|
|
|
|
|
|
|
(MODE_EXTERNAL, MODE_SINGLE_SHOT, MODE_CONTINUOUS) = range(3)
|
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class SingleGenerator(Module):
|
2012-10-09 15:11:15 -04:00
|
|
|
def __init__(self, layout, mode):
|
2013-04-10 13:12:42 -04:00
|
|
|
self.source = Source(_convert_layout(layout))
|
|
|
|
self.busy = Signal()
|
2013-04-30 12:54:47 -04:00
|
|
|
self._csrs, assigns = _create_csrs_assign(layout, self.source.payload, mode != MODE_SINGLE_SHOT)
|
2012-10-09 15:11:15 -04:00
|
|
|
if mode == MODE_EXTERNAL:
|
2013-04-30 12:54:47 -04:00
|
|
|
self.trigger = Signal()
|
|
|
|
trigger = self.trigger
|
2012-10-09 15:11:15 -04:00
|
|
|
elif mode == MODE_SINGLE_SHOT:
|
2013-03-30 12:28:41 -04:00
|
|
|
shoot = CSR()
|
|
|
|
self._csrs.insert(0, shoot)
|
2013-04-10 13:12:42 -04:00
|
|
|
trigger = shoot.re
|
2012-10-09 15:11:15 -04:00
|
|
|
elif mode == MODE_CONTINUOUS:
|
2013-03-30 12:28:41 -04:00
|
|
|
enable = CSRStorage()
|
|
|
|
self._csrs.insert(0, enable)
|
2013-04-10 13:12:42 -04:00
|
|
|
trigger = enable.storage
|
2012-10-09 15:11:15 -04:00
|
|
|
else:
|
|
|
|
raise ValueError
|
2013-04-10 13:12:42 -04:00
|
|
|
self.comb += self.busy.eq(self.source.stb)
|
|
|
|
stmts = [self.source.stb.eq(trigger)] + assigns
|
2013-04-30 12:54:47 -04:00
|
|
|
self.sync += If(self.source.ack | ~self.source.stb, *stmts)
|
2012-10-09 15:11:15 -04:00
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
def get_csrs(self):
|
|
|
|
return self._csrs
|
2012-10-09 15:11:15 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class Collector(Module, AutoCSR):
|
2012-10-04 12:22:22 -04:00
|
|
|
def __init__(self, layout, depth=1024):
|
2013-04-10 13:12:42 -04:00
|
|
|
self.sink = Sink(layout)
|
|
|
|
self.busy = Signal()
|
2013-04-28 12:32:46 -04:00
|
|
|
dw = sum(len(s) for s in self.sink.payload.flatten())
|
|
|
|
|
|
|
|
self._r_wa = CSRStorage(bits_for(depth-1), write_from_dev=True)
|
|
|
|
self._r_wc = CSRStorage(bits_for(depth), write_from_dev=True, atomic_write=True)
|
|
|
|
self._r_ra = CSRStorage(bits_for(depth-1))
|
|
|
|
self._r_rd = CSRStatus(dw)
|
2013-04-10 13:12:42 -04:00
|
|
|
|
|
|
|
###
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-04-28 12:32:46 -04:00
|
|
|
mem = Memory(dw, depth)
|
2013-04-10 13:12:42 -04:00
|
|
|
self.specials += mem
|
2012-11-26 12:27:59 -05:00
|
|
|
wp = mem.get_port(write_capable=True)
|
|
|
|
rp = mem.get_port()
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
self.comb += [
|
|
|
|
self.busy.eq(0),
|
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
If(self._r_wc.r != 0,
|
2013-04-28 12:32:46 -04:00
|
|
|
self.sink.ack.eq(1),
|
|
|
|
If(self.sink.stb,
|
2013-03-30 12:28:41 -04:00
|
|
|
self._r_wa.we.eq(1),
|
|
|
|
self._r_wc.we.eq(1),
|
2012-11-26 12:27:59 -05:00
|
|
|
wp.we.eq(1)
|
2012-10-04 12:22:22 -04:00
|
|
|
)
|
|
|
|
),
|
2013-03-30 12:28:41 -04:00
|
|
|
self._r_wa.dat_w.eq(self._r_wa.storage + 1),
|
|
|
|
self._r_wc.dat_w.eq(self._r_wc.storage - 1),
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
wp.adr.eq(self._r_wa.storage),
|
2013-04-28 12:32:46 -04:00
|
|
|
wp.dat_w.eq(self.sink.payload.raw_bits()),
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-03-30 12:28:41 -04:00
|
|
|
rp.adr.eq(self._r_ra.storage),
|
|
|
|
self._r_rd.status.eq(rp.dat_r)
|
2012-10-04 12:22:22 -04:00
|
|
|
]
|
2013-04-30 12:55:01 -04:00
|
|
|
|
2013-05-04 11:38:54 -04:00
|
|
|
class _DMAController(Module):
|
|
|
|
def __init__(self, bus_accessor, bus_aw, bus_dw, mode, base_reset=0, length_reset=0):
|
|
|
|
self.alignment_bits = bits_for(bus_dw//8) - 1
|
2013-04-30 12:55:01 -04:00
|
|
|
layout = [
|
2013-05-04 11:38:54 -04:00
|
|
|
("length", bus_aw + self.alignment_bits, length_reset, self.alignment_bits),
|
|
|
|
("base", bus_aw + self.alignment_bits, base_reset, self.alignment_bits)
|
2013-04-30 12:55:01 -04:00
|
|
|
]
|
|
|
|
self.generator = SingleGenerator(layout, mode)
|
2013-05-04 11:38:54 -04:00
|
|
|
self.r_busy = CSRStatus()
|
|
|
|
|
|
|
|
def get_csrs(self):
|
|
|
|
return self.generator.get_csrs() + [self.r_busy]
|
|
|
|
|
|
|
|
class DMAReadController(_DMAController):
|
|
|
|
def __init__(self, bus_accessor, *args, **kwargs):
|
2013-05-22 11:11:09 -04:00
|
|
|
bus_aw = flen(bus_accessor.address.payload.a)
|
|
|
|
bus_dw = flen(bus_accessor.data.payload.d)
|
2013-05-04 11:38:54 -04:00
|
|
|
_DMAController.__init__(self, bus_accessor, bus_aw, bus_dw, *args, **kwargs)
|
|
|
|
|
2013-04-30 12:55:01 -04:00
|
|
|
g = DataFlowGraph()
|
|
|
|
g.add_pipeline(self.generator,
|
|
|
|
misc.IntSequence(bus_aw, bus_aw),
|
|
|
|
AbstractActor(plumbing.Buffer),
|
|
|
|
bus_accessor,
|
|
|
|
AbstractActor(plumbing.Buffer))
|
|
|
|
comp_actor = CompositeActor(g)
|
|
|
|
self.submodules += comp_actor
|
|
|
|
|
|
|
|
self.data = comp_actor.q
|
|
|
|
self.busy = comp_actor.busy
|
2013-05-04 11:38:54 -04:00
|
|
|
self.comb += self.r_busy.status.eq(self.busy)
|
|
|
|
|
|
|
|
class DMAWriteController(_DMAController):
|
|
|
|
def __init__(self, bus_accessor, *args, **kwargs):
|
|
|
|
bus_aw = len(bus_accessor.address_data.payload.a)
|
|
|
|
bus_dw = len(bus_accessor.address_data.payload.d)
|
|
|
|
_DMAController.__init__(self, bus_accessor, bus_aw, bus_dw, *args, **kwargs)
|
2013-04-30 12:55:01 -04:00
|
|
|
|
2013-05-04 11:38:54 -04:00
|
|
|
g = DataFlowGraph()
|
|
|
|
adr_buffer = AbstractActor(plumbing.Buffer)
|
|
|
|
g.add_pipeline(self.generator,
|
|
|
|
misc.IntSequence(bus_aw, bus_aw),
|
|
|
|
adr_buffer)
|
|
|
|
g.add_connection(adr_buffer, bus_accessor, sink_subr=["a"])
|
|
|
|
g.add_connection(AbstractActor(plumbing.Buffer), bus_accessor, sink_subr=["d"])
|
|
|
|
comp_actor = CompositeActor(g)
|
|
|
|
self.submodules += comp_actor
|
|
|
|
|
|
|
|
self.data = comp_actor.d
|
|
|
|
self.busy = comp_actor.busy
|
|
|
|
self.comb += self.r_busy.status.eq(self.busy)
|