2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2011-12-22 09:46:19 -05:00
|
|
|
from migen.corelogic import roundrobin
|
|
|
|
from migen.corelogic.misc import multimux, optree
|
2011-12-16 10:02:55 -05:00
|
|
|
from migen.bus.simple import Simple, get_sig_name
|
2011-12-08 12:47:41 -05:00
|
|
|
|
|
|
|
_desc = [
|
2012-01-13 11:29:05 -05:00
|
|
|
(True, "adr", 30),
|
2011-12-08 12:47:41 -05:00
|
|
|
(True, "dat", 32),
|
|
|
|
(False, "dat", 32),
|
2011-12-08 13:09:32 -05:00
|
|
|
(True, "sel", 4),
|
2011-12-08 12:47:41 -05:00
|
|
|
(True, "cyc", 1),
|
|
|
|
(True, "stb", 1),
|
|
|
|
(False, "ack", 1),
|
|
|
|
(True, "we", 1),
|
|
|
|
(True, "cti", 3),
|
|
|
|
(True, "bte", 2),
|
|
|
|
(False, "err", 1)
|
|
|
|
]
|
|
|
|
|
|
|
|
class Master(Simple):
|
2012-01-27 16:20:57 -05:00
|
|
|
def __init__(self):
|
|
|
|
Simple.__init__(self, _desc, False)
|
2011-12-08 12:47:41 -05:00
|
|
|
|
|
|
|
class Slave(Simple):
|
2012-01-27 16:20:57 -05:00
|
|
|
def __init__(self):
|
|
|
|
Simple.__init__(self, _desc, True)
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
class Arbiter:
|
|
|
|
def __init__(self, masters, target):
|
|
|
|
self.masters = masters
|
|
|
|
self.target = target
|
2012-01-20 17:07:32 -05:00
|
|
|
self.rr = roundrobin.RoundRobin(len(self.masters))
|
2011-12-08 17:21:25 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2011-12-08 17:21:25 -05:00
|
|
|
comb = []
|
|
|
|
|
|
|
|
# mux master->slave signals
|
2011-12-16 10:02:55 -05:00
|
|
|
m2s_names = [get_sig_name(x, False) for x in _desc if x[0]]
|
2011-12-08 17:21:25 -05:00
|
|
|
m2s_masters = [[getattr(m, name) for name in m2s_names] for m in self.masters]
|
|
|
|
m2s_target = [getattr(self.target, name) for name in m2s_names]
|
2011-12-22 09:46:19 -05:00
|
|
|
comb += multimux(self.rr.grant, m2s_masters, m2s_target)
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
# connect slave->master signals
|
2011-12-16 10:02:55 -05:00
|
|
|
s2m_names = [get_sig_name(x, False) for x in _desc if not x[0]]
|
2011-12-08 17:21:25 -05:00
|
|
|
for name in s2m_names:
|
|
|
|
source = getattr(self.target, name)
|
2011-12-12 18:25:25 -05:00
|
|
|
i = 0
|
2011-12-08 17:21:25 -05:00
|
|
|
for m in self.masters:
|
|
|
|
dest = getattr(m, name)
|
2011-12-12 18:25:25 -05:00
|
|
|
if name == "ack_i" or name == "err_i":
|
2011-12-16 15:30:14 -05:00
|
|
|
comb.append(dest.eq(source & (self.rr.grant == Constant(i, self.rr.grant.bv))))
|
2011-12-12 18:25:25 -05:00
|
|
|
else:
|
2011-12-16 15:30:14 -05:00
|
|
|
comb.append(dest.eq(source))
|
2011-12-12 18:25:25 -05:00
|
|
|
i += 1
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
# connect bus requests to round-robin selector
|
|
|
|
reqs = [m.cyc_o for m in self.masters]
|
2011-12-16 15:30:14 -05:00
|
|
|
comb.append(self.rr.request.eq(Cat(*reqs)))
|
2011-12-08 17:21:25 -05:00
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
return Fragment(comb) + self.rr.get_fragment()
|
2011-12-08 17:21:25 -05:00
|
|
|
|
2011-12-09 07:11:52 -05:00
|
|
|
class Decoder:
|
|
|
|
# slaves is a list of pairs:
|
|
|
|
# 0) structure.Constant defining address (always decoded on the upper bits)
|
|
|
|
# Slaves can have differing numbers of address bits, but addresses
|
|
|
|
# must not conflict.
|
|
|
|
# 1) wishbone.Slave reference
|
|
|
|
# Addresses are decoded from bit 31-offset and downwards.
|
|
|
|
# register adds flip-flops after the address comparators. Improves timing,
|
|
|
|
# but breaks Wishbone combinatorial feedback.
|
|
|
|
def __init__(self, master, slaves, offset=0, register=False):
|
|
|
|
self.master = master
|
|
|
|
self.slaves = slaves
|
|
|
|
self.offset = offset
|
|
|
|
self.register = register
|
|
|
|
|
|
|
|
addresses = [slave[0] for slave in self.slaves]
|
2011-12-16 15:30:14 -05:00
|
|
|
maxbits = max([bits_for(addr) for addr in addresses])
|
2011-12-09 07:11:52 -05:00
|
|
|
def mkconst(x):
|
|
|
|
if isinstance(x, int):
|
2011-12-16 15:30:14 -05:00
|
|
|
return Constant(x, BV(maxbits))
|
2011-12-09 07:11:52 -05:00
|
|
|
else:
|
|
|
|
return x
|
|
|
|
self.addresses = list(map(mkconst, addresses))
|
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2011-12-09 07:11:52 -05:00
|
|
|
comb = []
|
|
|
|
sync = []
|
|
|
|
|
2011-12-18 15:47:48 -05:00
|
|
|
ns = len(self.slaves)
|
|
|
|
slave_sel = Signal(BV(ns))
|
|
|
|
slave_sel_r = Signal(BV(ns))
|
|
|
|
|
2011-12-09 07:11:52 -05:00
|
|
|
# decode slave addresses
|
|
|
|
i = 0
|
|
|
|
hi = self.master.adr_o.bv.width - self.offset
|
|
|
|
for addr in self.addresses:
|
2011-12-18 15:47:48 -05:00
|
|
|
comb.append(slave_sel[i].eq(
|
2011-12-09 07:11:52 -05:00
|
|
|
self.master.adr_o[hi-addr.bv.width:hi] == addr))
|
|
|
|
i += 1
|
|
|
|
if self.register:
|
2011-12-18 15:47:48 -05:00
|
|
|
sync.append(slave_sel_r.eq(slave_sel))
|
2011-12-09 07:11:52 -05:00
|
|
|
else:
|
2011-12-18 15:47:48 -05:00
|
|
|
comb.append(slave_sel_r.eq(slave_sel))
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# connect master->slaves signals except cyc
|
2011-12-16 10:02:55 -05:00
|
|
|
m2s_names = [(get_sig_name(x, False), get_sig_name(x, True))
|
2011-12-09 07:11:52 -05:00
|
|
|
for x in _desc if x[0] and x[1] != "cyc"]
|
2011-12-16 15:30:14 -05:00
|
|
|
comb += [getattr(slave[1], name[1]).eq(getattr(self.master, name[0]))
|
2011-12-09 07:11:52 -05:00
|
|
|
for name in m2s_names for slave in self.slaves]
|
|
|
|
|
|
|
|
# combine cyc with slave selection signals
|
|
|
|
i = 0
|
|
|
|
for slave in self.slaves:
|
2011-12-18 15:47:48 -05:00
|
|
|
comb.append(slave[1].cyc_i.eq(self.master.cyc_o & slave_sel[i]))
|
2011-12-09 07:11:52 -05:00
|
|
|
i += 1
|
|
|
|
|
|
|
|
# generate master ack (resp. err) by ORing all slave acks (resp. errs)
|
2012-02-14 07:12:43 -05:00
|
|
|
comb.append(self.master.ack_i.eq(optree("|", [slave[1].ack_o for slave in self.slaves])))
|
|
|
|
comb.append(self.master.err_i.eq(optree("|", [slave[1].err_o for slave in self.slaves])))
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# mux (1-hot) slave data return
|
2011-12-22 09:46:19 -05:00
|
|
|
masked = [Replicate(slave_sel_r[i], self.master.dat_i.bv.width) & self.slaves[i][1].dat_o for i in range(len(self.slaves))]
|
2012-02-14 07:12:43 -05:00
|
|
|
comb.append(self.master.dat_i.eq(optree("|", masked)))
|
2011-12-09 07:11:52 -05:00
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
return Fragment(comb, sync)
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
class InterconnectShared:
|
|
|
|
def __init__(self, masters, slaves, offset=0, register=False):
|
2012-01-27 16:20:57 -05:00
|
|
|
self._shared = Master()
|
2011-12-09 07:11:52 -05:00
|
|
|
self._arbiter = Arbiter(masters, self._shared)
|
|
|
|
self._decoder = Decoder(self._shared, slaves, offset, register)
|
|
|
|
self.addresses = self._decoder.addresses
|
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
|
|
|
return self._arbiter.get_fragment() + self._decoder.get_fragment()
|