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
|
2012-02-15 10:30:16 -05:00
|
|
|
from migen.bus.simple import *
|
2012-03-08 12:14:06 -05:00
|
|
|
from migen.bus.transactions import *
|
2012-06-12 11:08:56 -04:00
|
|
|
from migen.sim.generic import Proxy, PureSimulable
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2012-02-15 10:30:16 -05:00
|
|
|
_desc = Description(
|
|
|
|
(M_TO_S, "adr", 30),
|
|
|
|
(M_TO_S, "dat_w", 32),
|
|
|
|
(S_TO_M, "dat_r", 32),
|
|
|
|
(M_TO_S, "sel", 4),
|
|
|
|
(M_TO_S, "cyc", 1),
|
|
|
|
(M_TO_S, "stb", 1),
|
|
|
|
(S_TO_M, "ack", 1),
|
|
|
|
(M_TO_S, "we", 1),
|
|
|
|
(M_TO_S, "cti", 3),
|
|
|
|
(M_TO_S, "bte", 2),
|
|
|
|
(S_TO_M, "err", 1)
|
|
|
|
)
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2012-02-15 10:30:16 -05:00
|
|
|
class Interface(SimpleInterface):
|
2012-01-27 16:20:57 -05:00
|
|
|
def __init__(self):
|
2012-06-08 12:06:12 -04:00
|
|
|
super().__init__(_desc)
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2012-02-15 10:30:16 -05:00
|
|
|
class InterconnectPointToPoint(SimpleInterconnect):
|
|
|
|
def __init__(self, master, slave):
|
2012-06-08 12:06:12 -04:00
|
|
|
super().__init__(master, [slave])
|
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
|
2012-02-15 10:30:16 -05:00
|
|
|
m2s_names = _desc.get_names(M_TO_S)
|
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
|
2012-02-15 10:30:16 -05:00
|
|
|
for name in _desc.get_names(S_TO_M):
|
2011-12-08 17:21:25 -05:00
|
|
|
source = getattr(self.target, name)
|
2012-02-15 10:30:16 -05:00
|
|
|
for i, m in enumerate(self.masters):
|
2011-12-08 17:21:25 -05:00
|
|
|
dest = getattr(m, name)
|
2012-02-15 10:30:16 -05:00
|
|
|
if name == "ack" or name == "err":
|
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-08 17:21:25 -05:00
|
|
|
|
|
|
|
# connect bus requests to round-robin selector
|
2012-02-15 10:30:16 -05:00
|
|
|
reqs = [m.cyc 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
|
2012-02-15 10:30:16 -05:00
|
|
|
hi = self.master.adr.bv.width - self.offset
|
|
|
|
comb += [slave_sel[i].eq(self.master.adr[hi-addr.bv.width:hi] == addr)
|
|
|
|
for i, addr in enumerate(self.addresses)]
|
2011-12-09 07:11:52 -05:00
|
|
|
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
|
2012-02-15 10:30:16 -05:00
|
|
|
m2s_names = _desc.get_names(M_TO_S, "cyc")
|
|
|
|
comb += [getattr(slave[1], name).eq(getattr(self.master, name))
|
2011-12-09 07:11:52 -05:00
|
|
|
for name in m2s_names for slave in self.slaves]
|
|
|
|
|
|
|
|
# combine cyc with slave selection signals
|
2012-02-15 10:30:16 -05:00
|
|
|
comb += [slave[1].cyc.eq(self.master.cyc & slave_sel[i])
|
|
|
|
for i, slave in enumerate(self.slaves)]
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# generate master ack (resp. err) by ORing all slave acks (resp. errs)
|
2012-02-15 10:30:16 -05:00
|
|
|
comb += [
|
|
|
|
self.master.ack.eq(optree("|", [slave[1].ack for slave in self.slaves])),
|
|
|
|
self.master.err.eq(optree("|", [slave[1].err for slave in self.slaves]))
|
|
|
|
]
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# mux (1-hot) slave data return
|
2012-02-15 10:30:16 -05:00
|
|
|
masked = [Replicate(slave_sel_r[i], self.master.dat_r.bv.width) & self.slaves[i][1].dat_r for i in range(len(self.slaves))]
|
|
|
|
comb.append(self.master.dat_r.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-02-15 10:30:16 -05:00
|
|
|
self._shared = Interface()
|
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()
|
2012-03-08 12:14:06 -05:00
|
|
|
|
2012-06-12 11:08:56 -04:00
|
|
|
class Tap(PureSimulable):
|
2012-06-10 06:46:24 -04:00
|
|
|
def __init__(self, bus, handler=print):
|
|
|
|
self.bus = bus
|
2012-03-08 12:14:06 -05:00
|
|
|
self.handler = handler
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
if s.rd(self.bus.ack):
|
|
|
|
assert(s.rd(self.bus.cyc) and s.rd(self.bus.stb))
|
|
|
|
if s.rd(self.bus.we):
|
|
|
|
transaction = TWrite(s.rd(self.bus.adr),
|
|
|
|
s.rd(self.bus.dat_w),
|
|
|
|
s.rd(self.bus.sel))
|
|
|
|
else:
|
|
|
|
transaction = TRead(s.rd(self.bus.adr),
|
|
|
|
s.rd(self.bus.dat_r))
|
|
|
|
self.handler(transaction)
|
|
|
|
|
2012-06-12 11:08:56 -04:00
|
|
|
class Initiator(PureSimulable):
|
2012-03-08 12:14:06 -05:00
|
|
|
def __init__(self, generator):
|
|
|
|
self.generator = generator
|
|
|
|
self.bus = Interface()
|
|
|
|
self.transaction_start = 0
|
|
|
|
self.transaction = None
|
|
|
|
self.done = False
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
if not self.done:
|
|
|
|
if self.transaction is None or s.rd(self.bus.ack):
|
|
|
|
if self.transaction is not None:
|
|
|
|
self.transaction.latency = s.cycle_counter - self.transaction_start - 1
|
|
|
|
if isinstance(self.transaction, TRead):
|
|
|
|
self.transaction.data = s.rd(self.bus.dat_r)
|
|
|
|
try:
|
|
|
|
self.transaction = next(self.generator)
|
|
|
|
except StopIteration:
|
|
|
|
self.done = True
|
|
|
|
self.transaction = None
|
|
|
|
if self.transaction is not None:
|
|
|
|
self.transaction_start = s.cycle_counter
|
|
|
|
s.wr(self.bus.cyc, 1)
|
|
|
|
s.wr(self.bus.stb, 1)
|
|
|
|
s.wr(self.bus.adr, self.transaction.address)
|
|
|
|
if isinstance(self.transaction, TWrite):
|
|
|
|
s.wr(self.bus.we, 1)
|
|
|
|
s.wr(self.bus.sel, self.transaction.sel)
|
|
|
|
s.wr(self.bus.dat_w, self.transaction.data)
|
|
|
|
else:
|
|
|
|
s.wr(self.bus.we, 0)
|
|
|
|
else:
|
|
|
|
s.wr(self.bus.cyc, 0)
|
|
|
|
s.wr(self.bus.stb, 0)
|
2012-06-10 10:40:33 -04:00
|
|
|
|
2012-06-10 11:05:10 -04:00
|
|
|
class TargetModel:
|
|
|
|
def read(self, address):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def write(self, address, data, sel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def can_ack(self, bus):
|
|
|
|
return True
|
|
|
|
|
2012-06-12 11:08:56 -04:00
|
|
|
class Target(PureSimulable):
|
2012-06-10 10:40:33 -04:00
|
|
|
def __init__(self, model):
|
|
|
|
self.bus = Interface()
|
|
|
|
self.model = model
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
bus = Proxy(s, self.bus)
|
|
|
|
if not bus.ack:
|
2012-06-10 11:05:10 -04:00
|
|
|
if self.model.can_ack(bus) and bus.cyc and bus.stb:
|
2012-06-10 10:40:33 -04:00
|
|
|
if bus.we:
|
|
|
|
self.model.write(bus.adr, bus.dat_w, bus.sel)
|
|
|
|
else:
|
|
|
|
bus.dat_r = self.model.read(bus.adr)
|
|
|
|
bus.ack = 1
|
|
|
|
else:
|
|
|
|
bus.ack = 0
|