2013-06-08 09:49:50 -04:00
|
|
|
from migen.fhdl.std import *
|
2013-06-09 10:03:22 -04:00
|
|
|
from migen.bus.transactions import *
|
2013-06-08 09:49:50 -04:00
|
|
|
from migen.genlib import roundrobin
|
|
|
|
from migen.genlib.record import *
|
|
|
|
from migen.genlib.misc import optree
|
|
|
|
|
|
|
|
class Interface(Record):
|
2013-06-17 17:36:03 -04:00
|
|
|
def __init__(self, aw, dw, nbanks, req_queue_size, read_latency, write_latency):
|
2013-06-08 09:49:50 -04:00
|
|
|
self.aw = aw
|
|
|
|
self.dw = dw
|
|
|
|
self.nbanks = nbanks
|
2013-06-17 17:36:03 -04:00
|
|
|
self.req_queue_size = req_queue_size
|
2013-06-08 09:49:50 -04:00
|
|
|
self.read_latency = read_latency
|
|
|
|
self.write_latency = write_latency
|
|
|
|
|
|
|
|
bank_layout = [
|
|
|
|
("adr", aw, DIR_M_TO_S),
|
|
|
|
("we", 1, DIR_M_TO_S),
|
|
|
|
("stb", 1, DIR_M_TO_S),
|
2013-06-17 17:36:03 -04:00
|
|
|
("req_ack", 1, DIR_S_TO_M),
|
|
|
|
("dat_ack", 1, DIR_S_TO_M),
|
|
|
|
("lock", 1, DIR_S_TO_M)
|
2013-06-08 09:49:50 -04:00
|
|
|
]
|
|
|
|
if nbanks > 1:
|
|
|
|
layout = [("bank"+str(i), bank_layout) for i in range(nbanks)]
|
|
|
|
else:
|
|
|
|
layout = bank_layout
|
|
|
|
layout += [
|
|
|
|
("dat_w", dw, DIR_M_TO_S),
|
|
|
|
("dat_we", dw//8, DIR_M_TO_S),
|
|
|
|
("dat_r", dw, DIR_S_TO_M)
|
|
|
|
]
|
|
|
|
Record.__init__(self, layout)
|
|
|
|
|
|
|
|
def _getattr_all(l, attr):
|
|
|
|
it = iter(l)
|
|
|
|
r = getattr(next(it), attr)
|
|
|
|
for e in it:
|
|
|
|
if getattr(e, attr) != r:
|
|
|
|
raise ValueError
|
|
|
|
return r
|
|
|
|
|
|
|
|
class Crossbar(Module):
|
2013-11-23 11:51:22 -05:00
|
|
|
def __init__(self, controllers, cba_shift):
|
|
|
|
self._controllers = controllers
|
|
|
|
self._cba_shift = cba_shift
|
|
|
|
|
|
|
|
self._rca_bits = _getattr_all(controllers, "aw")
|
|
|
|
self._dw = _getattr_all(controllers, "dw")
|
|
|
|
self._nbanks = _getattr_all(controllers, "nbanks")
|
|
|
|
self._req_queue_size = _getattr_all(controllers, "req_queue_size")
|
|
|
|
self._read_latency = _getattr_all(controllers, "read_latency")
|
|
|
|
self._write_latency = _getattr_all(controllers, "write_latency")
|
|
|
|
|
|
|
|
self._bank_bits = log2_int(self._nbanks, False)
|
|
|
|
self._controller_bits = log2_int(len(self._controllers), False)
|
|
|
|
|
|
|
|
self._masters = []
|
|
|
|
|
|
|
|
def get_master(self):
|
|
|
|
if self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
lasmi_master = Interface(self._rca_bits + self._bank_bits + self._controller_bits,
|
|
|
|
self._dw, 1, self._req_queue_size, self._read_latency, self._write_latency)
|
|
|
|
self._masters.append(lasmi_master)
|
|
|
|
return lasmi_master
|
|
|
|
|
|
|
|
def do_finalize(self):
|
|
|
|
nmasters = len(self._masters)
|
|
|
|
|
|
|
|
m_ca, m_ba, m_rca = self._split_master_addresses(self._controller_bits,
|
|
|
|
self._bank_bits, self._rca_bits, self._cba_shift)
|
2013-06-08 09:49:50 -04:00
|
|
|
|
2013-11-23 11:51:22 -05:00
|
|
|
for nc, controller in enumerate(self._controllers):
|
|
|
|
if self._controller_bits:
|
2013-06-08 09:49:50 -04:00
|
|
|
controller_selected = [ca == nc for ca in m_ca]
|
|
|
|
else:
|
|
|
|
controller_selected = [1]*nmasters
|
2013-06-17 17:36:03 -04:00
|
|
|
master_req_acks = [0]*nmasters
|
|
|
|
master_dat_acks = [0]*nmasters
|
2013-11-23 11:51:22 -05:00
|
|
|
rrs = [roundrobin.RoundRobin(nmasters, roundrobin.SP_CE) for n in range(self._nbanks)]
|
2013-07-15 15:45:07 -04:00
|
|
|
self.submodules += rrs
|
|
|
|
for nb, rr in enumerate(rrs):
|
2013-06-08 09:49:50 -04:00
|
|
|
bank = getattr(controller, "bank"+str(nb))
|
|
|
|
|
2013-07-15 15:45:07 -04:00
|
|
|
# for each master, determine if another bank locks it
|
|
|
|
master_locked = []
|
2013-11-23 11:51:22 -05:00
|
|
|
for nm, master in enumerate(self._masters):
|
2013-07-15 15:45:07 -04:00
|
|
|
locked = 0
|
|
|
|
for other_nb, other_rr in enumerate(rrs):
|
|
|
|
if other_nb != nb:
|
|
|
|
other_bank = getattr(controller, "bank"+str(other_nb))
|
|
|
|
locked = locked | (other_bank.lock & (other_rr.grant == nm))
|
|
|
|
master_locked.append(locked)
|
|
|
|
|
2013-06-08 09:49:50 -04:00
|
|
|
# arbitrate
|
2013-07-15 15:45:07 -04:00
|
|
|
bank_selected = [cs & (ba == nb) & ~locked for cs, ba, locked in zip(controller_selected, m_ba, master_locked)]
|
2013-11-23 11:51:22 -05:00
|
|
|
bank_requested = [bs & master.stb for bs, master in zip(bank_selected, self._masters)]
|
2013-06-17 17:36:03 -04:00
|
|
|
self.comb += [
|
|
|
|
rr.request.eq(Cat(*bank_requested)),
|
|
|
|
rr.ce.eq(~bank.stb & ~bank.lock)
|
|
|
|
]
|
2013-06-08 09:49:50 -04:00
|
|
|
|
|
|
|
# route requests
|
|
|
|
self.comb += [
|
|
|
|
bank.adr.eq(Array(m_rca)[rr.grant]),
|
2013-11-23 11:51:22 -05:00
|
|
|
bank.we.eq(Array(self._masters)[rr.grant].we),
|
2013-06-09 17:36:32 -04:00
|
|
|
bank.stb.eq(Array(bank_requested)[rr.grant])
|
2013-06-08 09:49:50 -04:00
|
|
|
]
|
2013-07-07 12:56:43 -04:00
|
|
|
master_req_acks = [master_req_ack | ((rr.grant == nm) & bank_selected[nm] & bank.req_ack)
|
2013-06-17 17:36:03 -04:00
|
|
|
for nm, master_req_ack in enumerate(master_req_acks)]
|
|
|
|
master_dat_acks = [master_dat_ack | ((rr.grant == nm) & bank.dat_ack)
|
|
|
|
for nm, master_dat_ack in enumerate(master_dat_acks)]
|
2013-06-09 17:36:32 -04:00
|
|
|
|
2013-11-23 11:51:22 -05:00
|
|
|
self.comb += [master.req_ack.eq(master_req_ack) for master, master_req_ack in zip(self._masters, master_req_acks)]
|
|
|
|
self.comb += [master.dat_ack.eq(master_dat_ack) for master, master_dat_ack in zip(self._masters, master_dat_acks)]
|
2013-06-08 09:49:50 -04:00
|
|
|
|
|
|
|
# route data writes
|
|
|
|
controller_selected_wl = controller_selected
|
2013-11-23 11:51:22 -05:00
|
|
|
for i in range(self._write_latency):
|
2013-06-08 09:49:50 -04:00
|
|
|
n_controller_selected_wl = [Signal() for i in range(nmasters)]
|
|
|
|
self.sync += [n.eq(o) for n, o in zip(n_controller_selected_wl, controller_selected_wl)]
|
|
|
|
controller_selected_wl = n_controller_selected_wl
|
|
|
|
dat_w_maskselect = []
|
|
|
|
dat_we_maskselect = []
|
2013-11-23 11:51:22 -05:00
|
|
|
for master, selected in zip(self._masters, controller_selected_wl):
|
|
|
|
o_dat_w = Signal(self._dw)
|
|
|
|
o_dat_we = Signal(self._dw//8)
|
2013-06-08 09:49:50 -04:00
|
|
|
self.comb += If(selected,
|
|
|
|
o_dat_w.eq(master.dat_w),
|
|
|
|
o_dat_we.eq(master.dat_we)
|
|
|
|
)
|
|
|
|
dat_w_maskselect.append(o_dat_w)
|
|
|
|
dat_we_maskselect.append(o_dat_we)
|
|
|
|
self.comb += [
|
|
|
|
controller.dat_w.eq(optree("|", dat_w_maskselect)),
|
|
|
|
controller.dat_we.eq(optree("|", dat_we_maskselect))
|
|
|
|
]
|
|
|
|
|
|
|
|
# route data reads
|
2013-11-23 11:51:22 -05:00
|
|
|
if self._controller_bits:
|
|
|
|
for master in self._masters:
|
|
|
|
controller_sel = Signal(self._controller_bits)
|
|
|
|
for nc, controller in enumerate(self._controllers):
|
2013-06-08 09:49:50 -04:00
|
|
|
for nb in range(nbanks):
|
|
|
|
bank = getattr(controller, "bank"+str(nb))
|
|
|
|
self.comb += If(bank.stb & bank.ack, controller_sel.eq(nc))
|
2013-11-23 11:51:22 -05:00
|
|
|
for i in range(self._read_latency):
|
|
|
|
n_controller_sel = Signal(self._controller_bits)
|
2013-06-08 09:49:50 -04:00
|
|
|
self.sync += n_controller_sel.eq(controller_sel)
|
|
|
|
controller_sel = n_controller_sel
|
2013-11-23 11:51:22 -05:00
|
|
|
self.comb += master.dat_r.eq(Array(self._controllers)[controller_sel].dat_r)
|
2013-06-08 09:49:50 -04:00
|
|
|
else:
|
2013-11-23 11:51:22 -05:00
|
|
|
self.comb += [master.dat_r.eq(self._controllers[0].dat_r) for master in self._masters]
|
2013-06-08 09:49:50 -04:00
|
|
|
|
|
|
|
def _split_master_addresses(self, controller_bits, bank_bits, rca_bits, cba_shift):
|
|
|
|
m_ca = [] # controller address
|
|
|
|
m_ba = [] # bank address
|
|
|
|
m_rca = [] # row and column address
|
2013-11-23 11:51:22 -05:00
|
|
|
for master in self._masters:
|
|
|
|
cba = Signal(self._controller_bits + self._bank_bits)
|
|
|
|
rca = Signal(self._rca_bits)
|
2013-06-08 09:49:50 -04:00
|
|
|
cba_upper = cba_shift + controller_bits + bank_bits
|
|
|
|
self.comb += cba.eq(master.adr[cba_shift:cba_upper])
|
2013-11-23 11:51:22 -05:00
|
|
|
if cba_shift < self._rca_bits:
|
2013-06-11 12:15:49 -04:00
|
|
|
if cba_shift:
|
|
|
|
self.comb += rca.eq(Cat(master.adr[:cba_shift], master.adr[cba_upper:]))
|
|
|
|
else:
|
|
|
|
self.comb += rca.eq(master.adr[cba_upper:])
|
2013-06-08 09:49:50 -04:00
|
|
|
else:
|
|
|
|
self.comb += rca.eq(master.adr[:cba_shift])
|
|
|
|
|
2013-11-23 11:51:22 -05:00
|
|
|
if self._controller_bits:
|
|
|
|
ca = Signal(self._controller_bits)
|
|
|
|
ba = Signal(self._bank_bits)
|
2013-06-08 09:49:50 -04:00
|
|
|
self.comb += Cat(ba, ca).eq(cba)
|
|
|
|
else:
|
|
|
|
ca = None
|
|
|
|
ba = cba
|
|
|
|
|
|
|
|
m_ca.append(ca)
|
|
|
|
m_ba.append(ba)
|
|
|
|
m_rca.append(rca)
|
|
|
|
return m_ca, m_ba, m_rca
|
2013-06-09 10:03:22 -04:00
|
|
|
|
|
|
|
class Initiator(Module):
|
|
|
|
def __init__(self, generator, bus):
|
|
|
|
self.generator = generator
|
|
|
|
self.bus = bus
|
|
|
|
self.transaction_start = 0
|
|
|
|
self.transaction = None
|
|
|
|
self.transaction_end = None
|
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
def do_simulation(self, selfp):
|
|
|
|
selfp.bus.dat_w = 0
|
|
|
|
selfp.bus.dat_we = 0
|
|
|
|
|
|
|
|
if self.transaction is not None:
|
|
|
|
if selfp.bus.req_ack:
|
|
|
|
selfp.bus.stb = 0
|
|
|
|
if selfp.bus.dat_ack:
|
|
|
|
if isinstance(self.transaction, TRead):
|
|
|
|
self.transaction_end = selfp.simulator.cycle_counter + self.bus.read_latency
|
|
|
|
else:
|
|
|
|
self.transaction_end = selfp.simulator.cycle_counter + self.bus.write_latency - 1
|
2013-06-09 10:03:22 -04:00
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
if self.transaction is None or selfp.simulator.cycle_counter == self.transaction_end:
|
|
|
|
if self.transaction is not None:
|
|
|
|
self.transaction.latency = selfp.simulator.cycle_counter - self.transaction_start - 1
|
|
|
|
if isinstance(self.transaction, TRead):
|
|
|
|
self.transaction.data = selfp.bus.dat_r
|
|
|
|
else:
|
|
|
|
selfp.bus.dat_w = self.transaction.data
|
|
|
|
selfp.bus.dat_we = self.transaction.sel
|
|
|
|
try:
|
|
|
|
self.transaction = next(self.generator)
|
|
|
|
except StopIteration:
|
|
|
|
raise StopSimulation
|
|
|
|
if self.transaction is not None:
|
|
|
|
self.transaction_start = selfp.simulator.cycle_counter
|
|
|
|
selfp.bus.stb = 1
|
|
|
|
selfp.bus.adr = self.transaction.address
|
|
|
|
if isinstance(self.transaction, TRead):
|
|
|
|
selfp.bus.we = 0
|
|
|
|
else:
|
|
|
|
selfp.bus.we = 1
|
2013-06-09 10:03:22 -04:00
|
|
|
|
|
|
|
class TargetModel:
|
|
|
|
def __init__(self):
|
|
|
|
self.last_bank = 0
|
|
|
|
|
|
|
|
def read(self, bank, address):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def write(self, bank, address, data, we):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Round-robin scheduling
|
|
|
|
def select_bank(self, pending_banks):
|
|
|
|
if not pending_banks:
|
|
|
|
return -1
|
|
|
|
self.last_bank += 1
|
|
|
|
if self.last_bank > max(pending_banks):
|
|
|
|
self.last_bank = 0
|
|
|
|
while self.last_bank not in pending_banks:
|
|
|
|
self.last_bank += 1
|
|
|
|
return self.last_bank
|
|
|
|
|
2013-07-10 13:09:51 -04:00
|
|
|
class _ReqFIFO(Module):
|
|
|
|
def __init__(self, req_queue_size, bank):
|
|
|
|
self.req_queue_size = req_queue_size
|
|
|
|
self.bank = bank
|
|
|
|
self.contents = []
|
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
def do_simulation(self, selfp):
|
2013-07-10 13:09:51 -04:00
|
|
|
if len(self.contents) < self.req_queue_size:
|
2014-01-26 16:19:43 -05:00
|
|
|
if selfp.bank.stb:
|
|
|
|
self.contents.append((selfp.bank.we, selfp.bank.adr))
|
|
|
|
selfp.bank.req_ack = 1
|
2013-07-10 13:09:51 -04:00
|
|
|
else:
|
2014-01-26 16:19:43 -05:00
|
|
|
selfp.bank.req_ack = 0
|
|
|
|
selfp.bank.lock = bool(self.contents)
|
2013-07-10 13:09:51 -04:00
|
|
|
|
2013-06-09 10:03:22 -04:00
|
|
|
class Target(Module):
|
|
|
|
def __init__(self, model, *ifargs, **ifkwargs):
|
|
|
|
self.model = model
|
|
|
|
self.bus = Interface(*ifargs, **ifkwargs)
|
2013-07-10 13:09:51 -04:00
|
|
|
self.req_fifos = [_ReqFIFO(self.bus.req_queue_size, getattr(self.bus, "bank"+str(nb)))
|
|
|
|
for nb in range(self.bus.nbanks)]
|
|
|
|
self.submodules += self.req_fifos
|
2013-06-09 10:03:22 -04:00
|
|
|
self.rd_pipeline = [None]*self.bus.read_latency
|
2013-06-09 17:36:32 -04:00
|
|
|
self.wr_pipeline = [None]*(self.bus.write_latency + 1)
|
2013-06-09 10:03:22 -04:00
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
def do_simulation(self, selfp):
|
2013-06-09 10:03:22 -04:00
|
|
|
# determine banks with pending requests
|
2013-07-10 13:09:51 -04:00
|
|
|
pending_banks = set(nb for nb, rf in enumerate(self.req_fifos) if rf.contents)
|
2013-06-09 10:03:22 -04:00
|
|
|
|
|
|
|
# issue new transactions
|
|
|
|
selected_bank_n = self.model.select_bank(pending_banks)
|
2013-07-10 13:09:51 -04:00
|
|
|
selected_transaction = None
|
2013-06-09 10:03:22 -04:00
|
|
|
for nb in range(self.bus.nbanks):
|
2014-01-26 16:19:43 -05:00
|
|
|
bank = getattr(selfp.bus, "bank"+str(nb))
|
2013-06-09 10:03:22 -04:00
|
|
|
if nb == selected_bank_n:
|
2014-01-26 16:19:43 -05:00
|
|
|
bank.dat_ack = 1
|
2013-07-10 13:09:51 -04:00
|
|
|
selected_transaction = self.req_fifos[nb].contents.pop(0)
|
2013-06-09 10:03:22 -04:00
|
|
|
else:
|
2014-01-26 16:19:43 -05:00
|
|
|
bank.dat_ack = 0
|
2013-06-09 10:03:22 -04:00
|
|
|
|
|
|
|
rd_transaction = None
|
|
|
|
wr_transaction = None
|
|
|
|
if selected_bank_n >= 0:
|
2013-07-10 13:09:51 -04:00
|
|
|
we, adr = selected_transaction
|
|
|
|
if we:
|
|
|
|
wr_transaction = selected_bank_n, adr
|
2013-06-09 10:03:22 -04:00
|
|
|
else:
|
2013-07-10 13:09:51 -04:00
|
|
|
rd_transaction = selected_bank_n, adr
|
2013-06-09 10:03:22 -04:00
|
|
|
|
|
|
|
# data pipeline
|
|
|
|
self.rd_pipeline.append(rd_transaction)
|
|
|
|
self.wr_pipeline.append(wr_transaction)
|
|
|
|
done_rd_transaction = self.rd_pipeline.pop(0)
|
|
|
|
done_wr_transaction = self.wr_pipeline.pop(0)
|
|
|
|
if done_rd_transaction is not None:
|
2014-01-26 16:19:43 -05:00
|
|
|
selfp.bus.dat_r = self.model.read(done_rd_transaction[0], done_rd_transaction[1])
|
2013-06-09 10:03:22 -04:00
|
|
|
if done_wr_transaction is not None:
|
|
|
|
self.model.write(done_wr_transaction[0], done_wr_transaction[1],
|
2014-01-26 16:19:43 -05:00
|
|
|
selfp.bus.dat_w, selfp.bus.dat_we)
|