2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2013-02-22 11:56:35 -05:00
|
|
|
from migen.fhdl.specials import Memory
|
2013-03-15 14:41:30 -04:00
|
|
|
from migen.fhdl.module import Module
|
2012-07-07 16:36:15 -04:00
|
|
|
from migen.bus.transactions import *
|
2013-03-30 12:28:41 -04:00
|
|
|
from migen.bank.description import CSRStorage
|
2013-04-01 15:54:21 -04:00
|
|
|
from migen.genlib.record import *
|
2013-03-03 13:27:13 -05:00
|
|
|
from migen.genlib.misc import chooser
|
2011-12-04 18:16:44 -05:00
|
|
|
|
2012-08-26 15:19:34 -04:00
|
|
|
data_width = 8
|
2011-12-08 12:47:32 -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, [
|
|
|
|
("adr", 14, DIR_M_TO_S),
|
|
|
|
("we", 1, DIR_M_TO_S),
|
|
|
|
("dat_w", data_width, DIR_M_TO_S),
|
|
|
|
("dat_r", data_width, DIR_S_TO_M)])
|
2011-12-04 18:16:44 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class Interconnect(Module):
|
|
|
|
def __init__(self, master, slaves):
|
|
|
|
self.comb += master.connect(*slaves)
|
2012-07-07 16:36:15 -04:00
|
|
|
|
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-07-07 16:36:15 -04:00
|
|
|
self.generator = generator
|
2012-12-06 11:34:48 -05:00
|
|
|
if bus is None:
|
|
|
|
bus = Interface()
|
2012-11-17 13:44:25 -05:00
|
|
|
self.bus = bus
|
2012-07-07 16:36:15 -04:00
|
|
|
self.transaction = None
|
|
|
|
self.done = False
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
if not self.done:
|
|
|
|
if self.transaction is not None:
|
|
|
|
if isinstance(self.transaction, TRead):
|
|
|
|
self.transaction.data = s.rd(self.bus.dat_r)
|
|
|
|
else:
|
|
|
|
s.wr(self.bus.we, 0)
|
|
|
|
try:
|
|
|
|
self.transaction = next(self.generator)
|
|
|
|
except StopIteration:
|
|
|
|
self.transaction = None
|
|
|
|
self.done = True
|
|
|
|
if self.transaction is not None:
|
|
|
|
s.wr(self.bus.adr, self.transaction.address)
|
|
|
|
if isinstance(self.transaction, TWrite):
|
|
|
|
s.wr(self.bus.we, 1)
|
|
|
|
s.wr(self.bus.dat_w, self.transaction.data)
|
2012-12-06 11:16:17 -05:00
|
|
|
|
|
|
|
def _compute_page_bits(nwords):
|
|
|
|
npages = (nwords - 1)//512
|
|
|
|
if npages > 0:
|
|
|
|
return bits_for(npages-1)
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
class SRAM(Module):
|
2013-03-08 18:50:57 -05:00
|
|
|
def __init__(self, mem_or_size, address, read_only=None, bus=None):
|
2012-12-06 11:16:17 -05:00
|
|
|
if isinstance(mem_or_size, Memory):
|
2013-04-01 15:54:21 -04:00
|
|
|
mem = mem_or_size
|
2012-12-06 11:16:17 -05:00
|
|
|
else:
|
2013-04-01 15:54:21 -04:00
|
|
|
mem = Memory(data_width, mem_or_size//(data_width//8))
|
|
|
|
if mem.width > data_width:
|
2013-04-14 07:55:04 -04:00
|
|
|
csrw_per_memw = (mem.width + data_width - 1)//data_width
|
2013-04-01 15:54:21 -04:00
|
|
|
word_bits = bits_for(csrw_per_memw-1)
|
2013-03-03 13:27:13 -05:00
|
|
|
else:
|
2013-04-01 15:54:21 -04:00
|
|
|
csrw_per_memw = 1
|
|
|
|
word_bits = 0
|
|
|
|
page_bits = _compute_page_bits(mem.depth + word_bits)
|
2012-12-06 11:16:17 -05:00
|
|
|
if page_bits:
|
2013-04-14 07:55:04 -04:00
|
|
|
self._page = CSRStorage(page_bits, name=mem.name_override + "_page")
|
2012-12-06 11:16:17 -05:00
|
|
|
else:
|
|
|
|
self._page = None
|
2013-03-08 18:50:57 -05:00
|
|
|
if read_only is None:
|
2013-04-01 15:54:21 -04:00
|
|
|
if hasattr(mem, "bus_read_only"):
|
|
|
|
read_only = mem.bus_read_only
|
2013-03-08 18:50:57 -05:00
|
|
|
else:
|
|
|
|
read_only = False
|
2012-12-06 11:34:48 -05:00
|
|
|
if bus is None:
|
|
|
|
bus = Interface()
|
2012-12-06 11:16:17 -05:00
|
|
|
self.bus = bus
|
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
###
|
|
|
|
|
|
|
|
self.specials += mem
|
|
|
|
port = mem.get_port(write_capable=not read_only,
|
|
|
|
we_granularity=data_width if not read_only and word_bits else 0)
|
2012-12-06 11:16:17 -05:00
|
|
|
|
|
|
|
sel = Signal()
|
|
|
|
sel_r = Signal()
|
2013-04-01 15:54:21 -04:00
|
|
|
self.sync += sel_r.eq(sel)
|
|
|
|
self.comb += sel.eq(self.bus.adr[9:] == address)
|
2013-03-03 13:27:13 -05:00
|
|
|
|
2013-04-01 15:54:21 -04:00
|
|
|
if word_bits:
|
|
|
|
word_index = Signal(word_bits)
|
|
|
|
word_expanded = Signal(csrw_per_memw*data_width)
|
2013-04-14 07:55:04 -04:00
|
|
|
self.sync += word_index.eq(self.bus.adr[:word_bits])
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += [
|
2013-03-03 13:27:13 -05:00
|
|
|
word_expanded.eq(port.dat_r),
|
|
|
|
If(sel_r,
|
2013-04-01 15:54:21 -04:00
|
|
|
chooser(word_expanded, word_index, self.bus.dat_r, n=csrw_per_memw, reverse=True)
|
2013-03-03 13:27:13 -05:00
|
|
|
)
|
|
|
|
]
|
2013-04-01 15:54:21 -04:00
|
|
|
if not read_only:
|
|
|
|
self.comb += [
|
|
|
|
If(sel & self.bus.we, port.we.eq((1 << word_bits) >> self.bus.adr[:self.word_bits])),
|
|
|
|
port.dat_w.eq(Replicate(self.bus.dat_w, csrw_per_memw))
|
2013-03-08 18:50:57 -05:00
|
|
|
]
|
2013-03-03 13:27:13 -05:00
|
|
|
else:
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += If(sel_r, self.bus.dat_r.eq(port.dat_r))
|
|
|
|
if not read_only:
|
|
|
|
self.comb += [
|
2013-03-08 18:50:57 -05:00
|
|
|
port.we.eq(sel & self.bus.we),
|
|
|
|
port.dat_w.eq(self.bus.dat_w)
|
|
|
|
]
|
2012-12-06 11:16:17 -05:00
|
|
|
|
|
|
|
if self._page is None:
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += port.adr.eq(self.bus.adr[word_bits:len(port.adr)])
|
2012-12-06 11:16:17 -05:00
|
|
|
else:
|
2013-03-30 12:28:41 -04:00
|
|
|
pv = self._page.storage
|
2013-04-01 15:54:21 -04:00
|
|
|
self.comb += port.adr.eq(Cat(self.bus.adr[word_bits:len(port.adr)-len(pv)], pv))
|
|
|
|
|
|
|
|
def get_csrs(self):
|
|
|
|
if self._page is None:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return [self._page]
|