2012-10-04 12:22:22 -04:00
|
|
|
# Simple Processor Interface
|
|
|
|
|
|
|
|
from migen.fhdl.structure import *
|
2013-02-22 11:56:35 -05:00
|
|
|
from migen.fhdl.specials import Memory
|
2012-10-04 12:22:22 -04:00
|
|
|
from migen.bank.description import *
|
|
|
|
from migen.flow.actor import *
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def _create_registers_assign(layout, target, atomic, prefix=""):
|
|
|
|
registers = []
|
|
|
|
assigns = []
|
|
|
|
for element in layout:
|
|
|
|
if isinstance(element[1], list):
|
|
|
|
r_registers, r_assigns = _create_registers_assign(element[1],
|
|
|
|
atomic,
|
|
|
|
getattr(target, element[0]),
|
|
|
|
element[0] + "_")
|
|
|
|
registers += r_registers
|
|
|
|
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-03-12 10:45:24 -04:00
|
|
|
reg = RegisterField(nbits + alignment, reset=reset, atomic_write=atomic, name=prefix + name)
|
2012-10-09 15:11:15 -04:00
|
|
|
registers.append(reg)
|
|
|
|
assigns.append(getattr(target, name).eq(reg.field.r[alignment:]))
|
|
|
|
return registers, assigns
|
|
|
|
|
|
|
|
(MODE_EXTERNAL, MODE_SINGLE_SHOT, MODE_CONTINUOUS) = range(3)
|
|
|
|
|
|
|
|
class SingleGenerator(Actor):
|
|
|
|
def __init__(self, layout, mode):
|
|
|
|
self._mode = mode
|
2012-12-18 08:54:33 -05:00
|
|
|
Actor.__init__(self, ("source", Source, _convert_layout(layout)))
|
2012-10-09 15:11:15 -04:00
|
|
|
self._registers, self._assigns = _create_registers_assign(layout,
|
|
|
|
self.token("source"), self._mode != MODE_SINGLE_SHOT)
|
|
|
|
if mode == MODE_EXTERNAL:
|
|
|
|
self.trigger = Signal()
|
|
|
|
elif mode == MODE_SINGLE_SHOT:
|
2013-03-12 10:45:24 -04:00
|
|
|
shoot = RegisterRaw()
|
2012-10-09 15:11:15 -04:00
|
|
|
self._registers.insert(0, shoot)
|
|
|
|
self.trigger = shoot.re
|
|
|
|
elif mode == MODE_CONTINUOUS:
|
2013-03-12 10:45:24 -04:00
|
|
|
enable = RegisterField()
|
2012-10-09 15:11:15 -04:00
|
|
|
self._registers.insert(0, enable)
|
|
|
|
self.trigger = enable.field.r
|
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
def get_registers(self):
|
|
|
|
return self._registers
|
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
stb = self.endpoints["source"].stb
|
|
|
|
ack = self.endpoints["source"].ack
|
|
|
|
comb = [
|
|
|
|
self.busy.eq(stb)
|
|
|
|
]
|
|
|
|
stmts = [stb.eq(self.trigger)] + self._assigns
|
|
|
|
sync = [If(ack | ~stb, *stmts)]
|
|
|
|
return Fragment(comb, sync)
|
|
|
|
|
2012-10-04 12:22:22 -04:00
|
|
|
class Collector(Actor):
|
|
|
|
def __init__(self, layout, depth=1024):
|
2012-12-18 08:54:33 -05:00
|
|
|
Actor.__init__(self, ("sink", Sink, layout))
|
2012-10-04 12:22:22 -04:00
|
|
|
self._depth = depth
|
|
|
|
self._dw = sum(len(s) for s in self.token("sink").flatten())
|
|
|
|
|
2013-03-12 10:45:24 -04:00
|
|
|
self._r_wa = RegisterField(bits_for(self._depth-1), READ_WRITE, READ_WRITE)
|
|
|
|
self._r_wc = RegisterField(bits_for(self._depth), READ_WRITE, READ_WRITE, atomic_write=True)
|
|
|
|
self._r_ra = RegisterField(bits_for(self._depth-1), READ_WRITE, READ_ONLY)
|
|
|
|
self._r_rd = RegisterField(self._dw, READ_ONLY, WRITE_ONLY)
|
2012-10-04 12:22:22 -04:00
|
|
|
|
|
|
|
def get_registers(self):
|
2013-03-12 10:45:24 -04:00
|
|
|
return [self._r_wa, self._r_wc, self._r_ra, self._r_rd]
|
2012-10-04 12:22:22 -04:00
|
|
|
|
|
|
|
def get_fragment(self):
|
2012-11-26 12:27:59 -05:00
|
|
|
mem = Memory(self._dw, self._depth)
|
|
|
|
wp = mem.get_port(write_capable=True)
|
|
|
|
rp = mem.get_port()
|
2012-10-04 12:22:22 -04:00
|
|
|
|
|
|
|
comb = [
|
2013-03-12 10:45:24 -04:00
|
|
|
If(self._r_wc.field.r != 0,
|
2012-10-04 12:22:22 -04:00
|
|
|
self.endpoints["sink"].ack.eq(1),
|
|
|
|
If(self.endpoints["sink"].stb,
|
2013-03-12 10:45:24 -04:00
|
|
|
self._r_wa.field.we.eq(1),
|
|
|
|
self._r_wc.field.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-12 10:45:24 -04:00
|
|
|
self._r_wa.field.w.eq(self._r_wa.field.r + 1),
|
|
|
|
self._r_wc.field.w.eq(self._r_wc.field.r - 1),
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-03-12 10:45:24 -04:00
|
|
|
wp.adr.eq(self._r_wa.field.r),
|
2012-11-26 12:27:59 -05:00
|
|
|
wp.dat_w.eq(Cat(*self.token("sink").flatten())),
|
2012-10-04 12:22:22 -04:00
|
|
|
|
2013-03-12 10:45:24 -04:00
|
|
|
rp.adr.eq(self._r_ra.field.r),
|
|
|
|
self._r_rd.field.w.eq(rp.dat_r)
|
2012-10-04 12:22:22 -04:00
|
|
|
]
|
|
|
|
|
2013-02-22 11:56:35 -05:00
|
|
|
return Fragment(comb, specials={mem})
|