2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2013-02-22 17:19:37 -05:00
|
|
|
from migen.genlib import roundrobin
|
2013-04-01 15:54:21 -04:00
|
|
|
from migen.genlib.record import *
|
2013-02-22 17:19:37 -05:00
|
|
|
from migen.genlib.misc import optree
|
2012-03-08 12:14:06 -05:00
|
|
|
from migen.bus.transactions import *
|
2013-03-15 14:41:30 -04:00
|
|
|
from migen.sim.generic import Proxy
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
_layout = [
|
|
|
|
("adr", 30, DIR_M_TO_S),
|
|
|
|
("dat_w", 32, DIR_M_TO_S),
|
|
|
|
("dat_r", 32, DIR_S_TO_M),
|
|
|
|
("sel", 4, DIR_M_TO_S),
|
|
|
|
("cyc", 1, DIR_M_TO_S),
|
|
|
|
("stb", 1, DIR_M_TO_S),
|
|
|
|
("ack", 1, DIR_S_TO_M),
|
|
|
|
("we", 1, DIR_M_TO_S),
|
|
|
|
("cti", 3, DIR_M_TO_S),
|
|
|
|
("bte", 2, DIR_M_TO_S),
|
|
|
|
("err", 1, DIR_S_TO_M)
|
|
|
|
]
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class Interface(Record):
|
2012-01-27 16:20:57 -05:00
|
|
|
def __init__(self):
|
2013-04-01 15:54:21 -04:00
|
|
|
Record.__init__(self, _layout)
|
2011-12-08 12:47:41 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class InterconnectPointToPoint(Module):
|
2012-02-15 10:30:16 -05:00
|
|
|
def __init__(self, master, slave):
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += master.connect(slave)
|
2011-12-08 17:21:25 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class Arbiter(Module):
|
2011-12-08 17:21:25 -05:00
|
|
|
def __init__(self, masters, target):
|
2013-04-01 15:54:21 -04:00
|
|
|
self.submodules.rr = roundrobin.RoundRobin(len(masters))
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
# mux master->slave signals
|
2013-04-01 15:54:21 -04:00
|
|
|
for name, size, direction in _layout:
|
|
|
|
if direction == DIR_M_TO_S:
|
|
|
|
choices = Array(getattr(m, name) for m in masters)
|
|
|
|
self.comb += getattr(target, name).eq(choices[self.rr.grant])
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
# connect slave->master signals
|
2013-04-01 15:54:21 -04:00
|
|
|
for name, size, direction in _layout:
|
|
|
|
if direction == DIR_S_TO_M:
|
|
|
|
source = getattr(target, name)
|
|
|
|
for i, m in enumerate(masters):
|
|
|
|
dest = getattr(m, name)
|
|
|
|
if name == "ack" or name == "err":
|
|
|
|
self.comb += dest.eq(source & (self.rr.grant == i))
|
|
|
|
else:
|
|
|
|
self.comb += dest.eq(source)
|
2011-12-08 17:21:25 -05:00
|
|
|
|
|
|
|
# connect bus requests to round-robin selector
|
2013-04-01 15:54:21 -04:00
|
|
|
reqs = [m.cyc for m in masters]
|
|
|
|
self.comb += self.rr.request.eq(Cat(*reqs))
|
2011-12-08 17:21:25 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class Decoder(Module):
|
2011-12-09 07:11:52 -05:00
|
|
|
# slaves is a list of pairs:
|
2012-11-28 17:18:43 -05:00
|
|
|
# 0) function that takes the address signal and returns a FHDL expression
|
|
|
|
# that evaluates to 1 when the slave is selected and 0 otherwise.
|
|
|
|
# 1) wishbone.Slave reference.
|
2011-12-09 07:11:52 -05:00
|
|
|
# register adds flip-flops after the address comparators. Improves timing,
|
|
|
|
# but breaks Wishbone combinatorial feedback.
|
2012-11-28 17:18:43 -05:00
|
|
|
def __init__(self, master, slaves, register=False):
|
2013-04-01 15:54:21 -04:00
|
|
|
ns = len(slaves)
|
2012-11-29 15:22:38 -05:00
|
|
|
slave_sel = Signal(ns)
|
|
|
|
slave_sel_r = Signal(ns)
|
2011-12-18 15:47:48 -05:00
|
|
|
|
2011-12-09 07:11:52 -05:00
|
|
|
# decode slave addresses
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += [slave_sel[i].eq(fun(master.adr))
|
|
|
|
for i, (fun, bus) in enumerate(slaves)]
|
|
|
|
if register:
|
|
|
|
self.sync += slave_sel_r.eq(slave_sel)
|
2011-12-09 07:11:52 -05:00
|
|
|
else:
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += slave_sel_r.eq(slave_sel)
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# connect master->slaves signals except cyc
|
2013-04-01 15:54:21 -04:00
|
|
|
for slave in slaves:
|
|
|
|
for name, size, direction in _layout:
|
|
|
|
if direction == DIR_M_TO_S and name != "cyc":
|
|
|
|
self.comb += getattr(slave[1], name).eq(getattr(master, name))
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# combine cyc with slave selection signals
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += [slave[1].cyc.eq(master.cyc & slave_sel[i])
|
|
|
|
for i, slave in enumerate(slaves)]
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# generate master ack (resp. err) by ORing all slave acks (resp. errs)
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += [
|
|
|
|
master.ack.eq(optree("|", [slave[1].ack for slave in slaves])),
|
|
|
|
master.err.eq(optree("|", [slave[1].err for slave in slaves]))
|
2012-02-15 10:30:16 -05:00
|
|
|
]
|
2011-12-09 07:11:52 -05:00
|
|
|
|
|
|
|
# mux (1-hot) slave data return
|
2013-05-22 11:11:09 -04:00
|
|
|
masked = [Replicate(slave_sel_r[i], flen(master.dat_r)) & slaves[i][1].dat_r for i in range(ns)]
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += master.dat_r.eq(optree("|", masked))
|
2011-12-09 07:11:52 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class InterconnectShared(Module):
|
2012-11-28 17:18:43 -05:00
|
|
|
def __init__(self, masters, slaves, register=False):
|
2013-04-01 15:54:21 -04:00
|
|
|
shared = Interface()
|
|
|
|
self.submodules += Arbiter(masters, shared)
|
|
|
|
self.submodules += Decoder(shared, slaves, register)
|
2012-03-08 12:14:06 -05:00
|
|
|
|
2013-03-15 14:41:30 -04:00
|
|
|
class Tap(Module):
|
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)
|
|
|
|
|
2013-03-15 14:41:30 -04:00
|
|
|
class Initiator(Module):
|
2012-12-06 11:34:48 -05:00
|
|
|
def __init__(self, generator, bus=None):
|
2012-03-08 12:14:06 -05:00
|
|
|
self.generator = generator
|
2012-12-06 11:34:48 -05:00
|
|
|
if bus is None:
|
|
|
|
bus = Interface()
|
2012-11-17 13:42:06 -05:00
|
|
|
self.bus = bus
|
2012-03-08 12:14:06 -05:00
|
|
|
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
|
|
|
|
|
2013-03-15 14:41:30 -04:00
|
|
|
class Target(Module):
|
2012-12-06 11:34:48 -05:00
|
|
|
def __init__(self, model, bus=None):
|
|
|
|
if bus is None:
|
|
|
|
bus = Interface()
|
2012-11-17 13:42:06 -05:00
|
|
|
self.bus = bus
|
2012-06-10 10:40:33 -04:00
|
|
|
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
|
2012-12-01 06:59:09 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class SRAM(Module):
|
2013-05-19 14:53:54 -04:00
|
|
|
def __init__(self, mem_or_size, read_only=None, init=None, bus=None):
|
2012-12-01 06:59:09 -05:00
|
|
|
if isinstance(mem_or_size, Memory):
|
2012-12-01 07:04:22 -05:00
|
|
|
assert(mem_or_size.width <= 32)
|
2013-04-01 15:54:21 -04:00
|
|
|
mem = mem_or_size
|
2012-12-01 06:59:09 -05:00
|
|
|
else:
|
2013-05-19 14:53:54 -04:00
|
|
|
mem = Memory(32, mem_or_size//4, init=init)
|
|
|
|
if read_only is None:
|
|
|
|
if hasattr(mem, "bus_read_only"):
|
|
|
|
read_only = mem.bus_read_only
|
|
|
|
else:
|
|
|
|
read_only = False
|
2012-12-06 11:34:48 -05:00
|
|
|
if bus is None:
|
|
|
|
bus = Interface()
|
2012-12-01 06:59:09 -05:00
|
|
|
self.bus = bus
|
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
###
|
|
|
|
|
2012-12-01 06:59:09 -05:00
|
|
|
# memory
|
2013-04-01 15:54:21 -04:00
|
|
|
self.specials += mem
|
2013-05-19 14:53:54 -04:00
|
|
|
port = mem.get_port(write_capable=not read_only, we_granularity=8)
|
2012-12-01 06:59:09 -05:00
|
|
|
# generate write enable signal
|
2013-05-19 14:53:54 -04:00
|
|
|
if not read_only:
|
|
|
|
self.comb += [port.we[i].eq(self.bus.cyc & self.bus.stb & self.bus.we & self.bus.sel[i])
|
|
|
|
for i in range(4)]
|
2012-12-01 06:59:09 -05:00
|
|
|
# address and data
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += [
|
2013-05-22 11:11:09 -04:00
|
|
|
port.adr.eq(self.bus.adr[:flen(port.adr)]),
|
2012-12-01 06:59:09 -05:00
|
|
|
self.bus.dat_r.eq(port.dat_r)
|
|
|
|
]
|
2013-05-19 14:53:54 -04:00
|
|
|
if not read_only:
|
|
|
|
self.comb += port.dat_w.eq(self.bus.dat_w),
|
2012-12-01 06:59:09 -05:00
|
|
|
# generate ack
|
2013-04-01 15:54:21 -04:00
|
|
|
self.sync += [
|
2012-12-01 06:59:09 -05:00
|
|
|
self.bus.ack.eq(0),
|
2013-04-01 15:54:21 -04:00
|
|
|
If(self.bus.cyc & self.bus.stb & ~self.bus.ack, self.bus.ack.eq(1))
|
2012-12-01 06:59:09 -05:00
|
|
|
]
|